Fewer pushes

This commit is contained in:
Kornel 2021-02-04 13:36:22 +00:00
parent aaee0a6a2e
commit c318ac78e1

View file

@ -71,15 +71,13 @@ pub fn unfilter_line(filter: u8, bpp: usize, data: &[u8], last_line: &[u8], buf:
} }
1 => { 1 => {
for (i, byte) in data.iter().enumerate() { for (i, byte) in data.iter().enumerate() {
match i.checked_sub(bpp) { buf.push(match i.checked_sub(bpp) {
Some(x) => { Some(x) => {
let b = buf[x]; let b = buf[x];
buf.push(byte.wrapping_add(b)); byte.wrapping_add(b)
} }
None => { None => *byte,
buf.push(*byte); });
}
};
} }
} }
2 => { 2 => {
@ -95,58 +93,44 @@ pub fn unfilter_line(filter: u8, bpp: usize, data: &[u8], last_line: &[u8], buf:
} }
3 => { 3 => {
for (i, byte) in data.iter().enumerate() { for (i, byte) in data.iter().enumerate() {
if last_line.is_empty() { buf.push(if last_line.is_empty() {
match i.checked_sub(bpp) { match i.checked_sub(bpp) {
Some(x) => { Some(x) => {
let b = buf[x]; let b = buf[x];
buf.push(byte.wrapping_add(b >> 1)); byte.wrapping_add(b >> 1)
} }
None => { None => *byte,
buf.push(*byte); }
}
};
} else { } else {
match i.checked_sub(bpp) { match i.checked_sub(bpp) {
Some(x) => { Some(x) => {
let b = buf[x]; let b = buf[x];
buf.push(byte.wrapping_add( byte.wrapping_add(((u16::from(b) + u16::from(last_line[i])) >> 1) as u8)
((u16::from(b) + u16::from(last_line[i])) >> 1) as u8,
));
} }
None => { None => byte.wrapping_add(last_line[i] >> 1),
buf.push(byte.wrapping_add(last_line[i] >> 1)); }
} });
};
};
} }
} }
4 => { 4 => {
for (i, byte) in data.iter().enumerate() { for (i, byte) in data.iter().enumerate() {
if last_line.is_empty() { buf.push(if last_line.is_empty() {
match i.checked_sub(bpp) { match i.checked_sub(bpp) {
Some(x) => { Some(x) => {
let b = buf[x]; let b = buf[x];
buf.push(byte.wrapping_add(b)); byte.wrapping_add(b)
} }
None => { None => *byte,
buf.push(*byte); }
}
};
} else { } else {
match i.checked_sub(bpp) { match i.checked_sub(bpp) {
Some(x) => { Some(x) => {
let b = buf[x]; let b = buf[x];
buf.push(byte.wrapping_add(paeth_predictor( byte.wrapping_add(paeth_predictor(b, last_line[i], last_line[x]))
b,
last_line[i],
last_line[x],
)));
} }
None => { None => byte.wrapping_add(last_line[i]),
buf.push(byte.wrapping_add(last_line[i])); }
} });
};
};
} }
} }
_ => unreachable!(), _ => unreachable!(),