Combine bound checks

This commit is contained in:
Kornel 2021-02-04 17:51:35 +00:00
parent 790a410a55
commit 1021e9845a

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,23 +88,20 @@ 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(match i.checked_sub(bpp).map(|x| (buf.get(x).copied(), last_line.get(x).copied())) {
Some(x) => { Some((Some(left), Some(left_up))) => {
let b = buf[x]; cur.wrapping_add(paeth_predictor(left, up, left_up))
cur.wrapping_add(paeth_predictor(b, last, last_line[x]))
} }
None => cur.wrapping_add(last), _ => cur.wrapping_add(up),
}); });
} }
} }