Skip to content

Commit

Permalink
Fix regressions in PNG filter
Browse files Browse the repository at this point in the history
Fixes image-rs#305.  This partially reverts image-rs#298, but keeps the correct parts.
  • Loading branch information
mbrubeck committed Feb 26, 2015
1 parent 7ed0dae commit 441c1eb
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/png/filter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::num::SignedInt;
use std::iter::range_step_inclusive;

#[derive(FromPrimitive, Debug)]
pub enum FilterType {
Expand Down Expand Up @@ -72,7 +71,7 @@ pub fn filter(method: FilterType, bpp: usize, previous: &[u8], current: &mut [u8
match method {
FilterType::NoFilter => (),
FilterType::Sub => {
for i in range_step_inclusive(len-1, bpp, -1) {
for i in (bpp..len).rev() {
current[i] = current[i] - current[i - bpp];
}
}
Expand All @@ -82,21 +81,21 @@ pub fn filter(method: FilterType, bpp: usize, previous: &[u8], current: &mut [u8
}
}
FilterType::Avg => {
for i in (0..bpp) {
current[i] = current[i] - previous[i] / 2;
for i in (bpp..len) {
current[i] = current[i] - ((current[i - bpp] as i16 + previous[i] as i16) / 2) as u8;
}

for i in range_step_inclusive(len-1, bpp, -1) {
current[i] = current[i] - ((current[i - bpp] as i16 + previous[i] as i16) / 2) as u8;
for i in (0..bpp) {
current[i] = current[i] - previous[i] / 2;
}
}
FilterType::Paeth => {
for i in (0..bpp) {
current[i] = current[i] - filter_paeth(0, previous[i], 0);
for i in (bpp..len) {
current[i] = current[i] - filter_paeth(current[i - bpp], previous[i], previous[i - bpp]);
}

for i in range_step_inclusive(len-1, bpp, -1) {
current[i] = current[i] - filter_paeth(current[i - bpp], previous[i], previous[i - bpp]);
for i in (0..bpp) {
current[i] = current[i] - filter_paeth(0, previous[i], 0);
}
}
}
Expand Down

0 comments on commit 441c1eb

Please sign in to comment.