Combine bound checks

This commit is contained in:
Kornel 2021-02-04 17:51:35 +00:00
parent 09de3a90ce
commit c46228befc

View file

@ -72,11 +72,9 @@ pub fn unfilter_line(filter: u8, bpp: usize, data: &[u8], last_line: &[u8], buf:
} }
1 => { 1 => {
for (i, &cur) in data.iter().enumerate() { for (i, &cur) in data.iter().enumerate() {
buf.push(match i.checked_sub(bpp) { let prev_byte = i.checked_sub(bpp).and_then(|x| buf.get(x).copied());
Some(x) => { buf.push(match prev_byte {
let b = buf[x]; Some(b) => cur.wrapping_add(b),
cur.wrapping_add(b)
}
None => cur, None => cur,
}); });
} }
@ -90,24 +88,26 @@ pub fn unfilter_line(filter: u8, bpp: usize, data: &[u8], last_line: &[u8], buf:
} }
3 => { 3 => {
for (i, (&cur, &last)) in data.iter().zip(last_line).enumerate() { for (i, (&cur, &last)) in data.iter().zip(last_line).enumerate() {
buf.push(match i.checked_sub(bpp) { let prev_byte = i.checked_sub(bpp).and_then(|x| buf.get(x).copied());
Some(x) => { buf.push(match prev_byte {
let b = buf[x]; Some(b) => cur.wrapping_add(((u16::from(b) + u16::from(last)) >> 1) as u8),
cur.wrapping_add(((u16::from(b) + u16::from(last)) >> 1) as u8)
}
None => cur.wrapping_add(last >> 1), None => cur.wrapping_add(last >> 1),
}); });
} }
} }
4 => { 4 => {
for (i, (&cur, &last)) in data.iter().zip(last_line).enumerate() { for (i, (&cur, &up)) in data.iter().zip(last_line).enumerate() {
buf.push(match i.checked_sub(bpp) { buf.push(
Some(x) => { match i
let b = buf[x]; .checked_sub(bpp)
cur.wrapping_add(paeth_predictor(b, last, last_line[x])) .map(|x| (buf.get(x).copied(), last_line.get(x).copied()))
{
Some((Some(left), Some(left_up))) => {
cur.wrapping_add(paeth_predictor(left, up, left_up))
} }
None => cur.wrapping_add(last), _ => cur.wrapping_add(up),
}); },
);
} }
} }
_ => unreachable!(), _ => unreachable!(),