From 7ebdb8bb1a4a73e588334972661c6dc04fd423bd Mon Sep 17 00:00:00 2001 From: Kornel Date: Thu, 4 Feb 2021 13:48:28 +0000 Subject: [PATCH] Remove special case for empty last line --- src/filters.rs | 55 +++++++++++++++----------------------------------- src/png/mod.rs | 1 + 2 files changed, 17 insertions(+), 39 deletions(-) diff --git a/src/filters.rs b/src/filters.rs index 8fc7722e..527f4fbd 100644 --- a/src/filters.rs +++ b/src/filters.rs @@ -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) { buf.clear(); buf.reserve(data.len()); + assert_eq!(data.len(), last_line.len()); match filter { 0 => { buf.extend_from_slice(data); @@ -81,55 +82,31 @@ pub fn unfilter_line(filter: u8, bpp: usize, data: &[u8], last_line: &[u8], buf: } } 2 => { - if last_line.is_empty() { - buf.extend_from_slice(data); - } else { - buf.extend( - data.iter() - .zip(last_line.iter()) - .map(|(cur, last)| cur.wrapping_add(*last)), - ); - }; + buf.extend( + data.iter() + .zip(last_line.iter()) + .map(|(cur, last)| cur.wrapping_add(*last)), + ); } 3 => { for (i, byte) in data.iter().enumerate() { - buf.push(if last_line.is_empty() { - match i.checked_sub(bpp) { - Some(x) => { - let b = buf[x]; - 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), + buf.push(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), }); } } 4 => { for (i, byte) in data.iter().enumerate() { - buf.push(if last_line.is_empty() { - match i.checked_sub(bpp) { - Some(x) => { - let b = buf[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]), + buf.push(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]), }); } } diff --git a/src/png/mod.rs b/src/png/mod.rs index eddc44bf..1399cf64 100644 --- a/src/png/mod.rs +++ b/src/png/mod.rs @@ -294,6 +294,7 @@ impl PngImage { last_pass = pass; } } + last_line.resize(line.data.len(), 0); unfilter_line( line.filter, bpp,