Remove special case for empty last line

This commit is contained in:
Kornel 2021-02-04 13:48:28 +00:00
parent c318ac78e1
commit 7ebdb8bb1a
2 changed files with 17 additions and 39 deletions

View file

@ -65,6 +65,7 @@ pub fn filter_line(filter: u8, bpp: usize, data: &[u8], last_line: &[u8], buf: &
pub fn unfilter_line(filter: u8, bpp: usize, data: &[u8], last_line: &[u8], buf: &mut Vec<u8>) { pub fn unfilter_line(filter: u8, bpp: usize, data: &[u8], last_line: &[u8], buf: &mut Vec<u8>) {
buf.clear(); buf.clear();
buf.reserve(data.len()); buf.reserve(data.len());
assert_eq!(data.len(), last_line.len());
match filter { match filter {
0 => { 0 => {
buf.extend_from_slice(data); buf.extend_from_slice(data);
@ -81,55 +82,31 @@ pub fn unfilter_line(filter: u8, bpp: usize, data: &[u8], last_line: &[u8], buf:
} }
} }
2 => { 2 => {
if last_line.is_empty() { buf.extend(
buf.extend_from_slice(data); data.iter()
} else { .zip(last_line.iter())
buf.extend( .map(|(cur, last)| cur.wrapping_add(*last)),
data.iter() );
.zip(last_line.iter())
.map(|(cur, last)| cur.wrapping_add(*last)),
);
};
} }
3 => { 3 => {
for (i, byte) in data.iter().enumerate() { for (i, byte) in data.iter().enumerate() {
buf.push(if last_line.is_empty() { buf.push(match i.checked_sub(bpp) {
match i.checked_sub(bpp) { Some(x) => {
Some(x) => { let b = buf[x];
let b = buf[x]; byte.wrapping_add(((u16::from(b) + u16::from(last_line[i])) >> 1) as u8)
byte.wrapping_add(b >> 1)
}
None => *byte,
}
} else {
match i.checked_sub(bpp) {
Some(x) => {
let b = buf[x];
byte.wrapping_add(((u16::from(b) + u16::from(last_line[i])) >> 1) as u8)
}
None => byte.wrapping_add(last_line[i] >> 1),
} }
None => byte.wrapping_add(last_line[i] >> 1),
}); });
} }
} }
4 => { 4 => {
for (i, byte) in data.iter().enumerate() { for (i, byte) in data.iter().enumerate() {
buf.push(if last_line.is_empty() { buf.push(match i.checked_sub(bpp) {
match i.checked_sub(bpp) { Some(x) => {
Some(x) => { let b = buf[x];
let b = buf[x]; byte.wrapping_add(paeth_predictor(b, last_line[i], last_line[x]))
byte.wrapping_add(b)
}
None => *byte,
}
} else {
match i.checked_sub(bpp) {
Some(x) => {
let b = buf[x];
byte.wrapping_add(paeth_predictor(b, last_line[i], last_line[x]))
}
None => byte.wrapping_add(last_line[i]),
} }
None => byte.wrapping_add(last_line[i]),
}); });
} }
} }

View file

@ -294,6 +294,7 @@ impl PngImage {
last_pass = pass; last_pass = pass;
} }
} }
last_line.resize(line.data.len(), 0);
unfilter_line( unfilter_line(
line.filter, line.filter,
bpp, bpp,