Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use stb's paeth predictor #540

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 10 additions & 17 deletions src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,24 +318,17 @@ impl Default for AdaptiveFilterType {
}
}

/// Adapted from stb_image.h 2.30
fn filter_paeth_decode(a: u8, b: u8, c: u8) -> u8 {
// Decoding seems to optimize better with this algorithm
let pa = (i16::from(b) - i16::from(c)).abs();
let pb = (i16::from(a) - i16::from(c)).abs();
let pc = ((i16::from(a) - i16::from(c)) + (i16::from(b) - i16::from(c))).abs();

let mut out = a;
let mut min = pa;

if pb < min {
min = pb;
out = b;
}
if pc < min {
out = c;
}

out
// This formulation looks very different from the reference in the PNG spec, but is
// actually equivalent and has favorable data dependencies and admits straightforward
// generation of branch-free code, which helps performance significantly.
let thresh = c as i16 * 3 - (a as i16 + b as i16);
let lo = a.min(b);
let hi = a.max(b);
let t0 = if thresh >= hi as i16 { lo } else { c };
let t1 = if thresh <= lo as i16 { hi } else { t0 };
t1
}

#[cfg(feature = "unstable")]
Expand Down
Loading