Skip to content

Commit

Permalink
Do not round floats in interpolate (#2052)
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianKnodt authored Nov 14, 2023
1 parent 84438eb commit 032c3e2
Showing 1 changed file with 6 additions and 3 deletions.
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

0 comments on commit 032c3e2

Please sign in to comment.