diff --git a/crates/nodes/src/atomic/sharpen.rs b/crates/nodes/src/atomic/sharpen.rs index 43aeaac..4542deb 100644 --- a/crates/nodes/src/atomic/sharpen.rs +++ b/crates/nodes/src/atomic/sharpen.rs @@ -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 +} diff --git a/crates/nodes/tests/atomic_filter_parity.rs b/crates/nodes/tests/atomic_filter_parity.rs index 0815022..ce507cd 100644 --- a/crates/nodes/tests/atomic_filter_parity.rs +++ b/crates/nodes/tests/atomic_filter_parity.rs @@ -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));