From bacc886a6d41b14ee68f83de9f4a855b56bb8820 Mon Sep 17 00:00:00 2001 From: jonathanwxh-cell Date: Mon, 12 Oct 2026 20:32:45 +0800 Subject: [PATCH] Preserve signed HDR colors during surface transforms (#3857) Co-authored-by: jonathnwxh <258340642+jonathanwxh-cell@users.noreply.github.com> Co-authored-by: Codex --- crates/algo/src/transform.rs | 41 +++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/crates/algo/src/transform.rs b/crates/algo/src/transform.rs index e684f87b968..1480ec2b3ee 100644 --- a/crates/algo/src/transform.rs +++ b/crates/algo/src/transform.rs @@ -368,7 +368,8 @@ fn warp(src: &Surface, src_rect: Rect, h: &Homography, interp: Interp, clip: Opt any = true; let o = ((y - t.y0) as usize * w + (x - t.x0) as usize) * n; for c in 0..a { - outp[o + c] = (acc[c] / al).clamp(0.0, 1.0) as f32; + // Integer storage clips on write; float colours retain their signed/HDR range. + outp[o + c] = (acc[c] / al) as f32; } outp[o + a] = al as f32; } @@ -482,6 +483,44 @@ mod tests { } } + #[test] + fn translation_preserves_colour_range_at_every_depth() { + use photocraft_color::{ColorMode, SampleType}; + + let h = Homography([1.0, 0.0, 7.0, 0.0, 1.0, -3.0, 0.0, 0.0, 1.0]); + for sample in SampleType::ALL { + for mode in [ColorMode::Rgb, ColorMode::Grayscale] { + for alpha in [false, true] { + let mut s = Surface::new(PixelFormat::new(mode, sample, alpha)); + let mut pixel = if mode == ColorMode::Rgb { vec![2.0, -0.25, 0.5] } else { vec![2.0] }; + if alpha { + pixel.push(0.5); + } + s.fill_rect(Rect::new(4, 4, 8, 8), &pixel); + let mut expected = s.pixel(5, 5); + if !alpha { + expected.push(1.0); + } + for interp in [Interp::Nearest, Interp::Bilinear, Interp::Bicubic] { + let out = warp_surface(&s, s.content_bounds(), &h, interp).unwrap(); + assert_eq!(out.pixel(12, 2), expected, "{sample:?}/{mode:?}/alpha={alpha}/{interp:?}"); + assert_eq!(out.pixel(0, 0).last(), Some(&0.0)); + } + } + } + } + } + + #[test] + fn bilinear_translation_interpolates_signed_hdr_colours() { + let mut s = Surface::new(PixelFormat::RGBA32F); + s.write_pixel(0, 0, &[2.0, -0.5, 0.25, 0.5]); + s.write_pixel(1, 0, &[4.0, -0.25, 0.75, 0.5]); + let h = Homography([1.0, 0.0, 0.5, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]); + let out = warp_surface(&s, s.content_bounds(), &h, Interp::Bilinear).unwrap(); + assert_eq!(out.pixel(1, 0), vec![3.0, -0.375, 0.5, 0.5]); + } + #[test] fn scale_doubles_size_and_rotation_keeps_area() { let mut s = rgba();