Auto: Preserve finite HDR constants in sharpen

Use wider arithmetic only when finite inputs overflow a Float32 axial difference. Add a regression while preserving the existing ramp results.
This commit is contained in:
chasse20 committed 2026-11-16 23:41:27 +00:00
1 parent c469b530b6
commit 591845cfa2
2 files changed
+22 -2

No files matched your search

+12 -2
View File
@@ -30,11 +30,21 @@ fn sharpen(context: &CookContext) -> CookResult {
std::array::from_fn(|channel| {
// Keep the axial differences separate to preserve Float32 cancellation order
let twice_center = 2.0 * center[channel];
let across = twice_center - (left[channel] + right[channel]);
let down = twice_center - (top[channel] + bottom[channel]);
let across = axial_difference(center[channel], twice_center, left[channel], right[channel]);
let down = axial_difference(center[channel], twice_center, top[channel], bottom[channel]);
// Float outputs retain HDR values but discard negative sharpening lobes
(center[channel] + (across * horizontal + down * vertical)).max(0.0)
})
})])
}
fn axial_difference(center: f32, twice_center: f32, first: f32, second: f32) -> f32 {
let difference = twice_center - (first + second);
if difference.is_finite() || ![center, first, second].iter().all(|value| value.is_finite()) {
return difference;
}
// Finite HDR inputs can overflow both Float32 sums and turn their difference into NaN
(2.0 * f64::from(center) - (f64::from(first) + f64::from(second))) as f32
}
@@ -64,6 +64,16 @@ fn sharpen_preserves_float_order_on_ramps() {
}
}
#[test]
fn sharpen_preserves_finite_hdr_constant() {
let constant = 2.0e38;
let input = Image::filled(16, 16, Kind::Gray, [constant; 4]);
let output = cook("sharpen", &[Some(input)], &[("intensity", Value::Float(1.0))], [16, 16], true);
assert!(output.data.iter().all(|value| value.to_bits() == constant.to_bits()));
}
#[test]
fn blur_uses_fractional_box_coverage() {
let input = Image::gray(16, 16, |x, y| f32::from(x == 8 && y == 8));