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

Do not round floats in interpolate #2052

Merged
merged 1 commit into from
Nov 14, 2023
Merged
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
9 changes: 6 additions & 3 deletions src/imageops/sample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,17 +428,20 @@ pub fn interpolate_bilinear<P: Pixel>(

// hack to get around not being able to construct a generic Pixel
let mut out = samples[0];
// hack to see if primitive is an integer or a float
let is_float = P::Subpixel::DEFAULT_MAX_VALUE.to_f32().unwrap() == 1.0;
for (i, c) in out.channels_mut().iter_mut().enumerate() {
let v = wff * sff[i] + wfc * sfc[i] + wcf * scf[i] + wcc * scc[i];
// this rounding may introduce quantization errors,
// but cannot do anything about it.
*c = <P::Subpixel as NumCast>::from(v.round()).unwrap_or({
// Specifically what is meant is that many samples may deviate
// from the mean value of the originals, but it's not possible to fix that.
*c = <P::Subpixel as NumCast>::from(if is_float { v } else { v.round() }).unwrap_or({
if v < 0.0 {
P::Subpixel::DEFAULT_MIN_VALUE
} else {
P::Subpixel::DEFAULT_MAX_VALUE
}
})
});
}
Some(out)
}
Expand Down