PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Free Transform: a rotated sprite's soft edge keeps its colour instead of a light border (fixes #6203)
Marco Arnulfo59d42f399803
diff --git a/crates/algo/src/transform.rs b/crates/algo/src/transform.rsindex 050d08c32..256494fb7 100644--- a/crates/algo/src/transform.rs+++ b/crates/algo/src/transform.rs@@ -467,8 +467,11 @@ fn warp(any = true;let o = ((y - t.y0) as usize * w + (x - t.x0) as usize) * n;for c in 0..a {- // Integer storage clips on write; float colours retain their signed/HDR range.- outp[o + c] = (acc[c] / al) as f32;+ // Un-premultiply by the unclamped alpha: bicubic overshoots it past 1 next to+ // partly transparent pixels, and dividing by the clamped 1 would lighten the+ // colour by the overshoot (#6203). Integer storage clips on write; float+ // colours retain their signed/HDR range.+ outp[o + c] = (acc[c] / acc[a]) as f32;}outp[o + a] = al as f32;}@@ -739,6 +742,32 @@ mod tests {assert!((p[0] - 0.2).abs() < 0.01 && p[3] > 0.99, "{p:?}");}+ /// #6203: a bicubic rotation of a sprite whose edge pixels are partly transparent overshoots+ /// its alpha past 1. The colour must stay the sprite's, not grow with the overshoot into a+ /// light border.+ #[test]+ fn rotation_keeps_the_colour_of_a_soft_edged_sprite() {+ let mut s = rgba();+ s.fill_rect(Rect::new(10, 10, 30, 30), &[0.8, 0.6, 0.4, 0.5]);+ s.fill_rect(Rect::new(11, 11, 29, 29), &[0.8, 0.6, 0.4, 1.0]);+ let (c, k) = (20.0, std::f64::consts::FRAC_1_SQRT_2 * 10.0);+ let q = [[c, c - 2.0 * k], [c + 2.0 * k, c], [c, c + 2.0 * k], [c - 2.0 * k, c]];+ let h = Homography::rect_to_quad([10.0, 10.0, 30.0, 30.0], q).unwrap();+ for interp in [Interp::Bicubic, Interp::Bilinear] {+ let out = warp_surface(&s, s.content_bounds(), &h, interp).unwrap();+ let b = out.content_bounds();+ for y in b.y0..b.y1 {+ for x in b.x0..b.x1 {+ let p = out.pixel(x, y);+ if p[3] > 0.0 {+ let off = (0..3).map(|i| (p[i] - [0.8, 0.6, 0.4][i]).abs()).fold(0.0f32, f32::max);+ assert!(off <= 1.0 / 255.0, "{interp:?} ({x}, {y}): {p:?}");+ }+ }+ }+ }+ }+#[test]fn large_downscale_is_prefiltered() {// A 1px checkerboard shrunk 8× must come out ~50% grey, not aliased black/white.diff --git a/crates/algo/src/warp.rs b/crates/algo/src/warp.rsindex 188de07d4..a110eac6c 100644--- a/crates/algo/src/warp.rs+++ b/crates/algo/src/warp.rs@@ -374,7 +374,8 @@ pub fn warp_triangles(src: &Surface, src_rect: Rect, verts: &[([f64; 2], [f64; 2any = true;let b = o * n;for c in 0..a {- outp[b + c] = (acc[c] / al).clamp(0.0, 1.0) as f32;+ // The unclamped alpha, so a bicubic overshoot past 1 doesn't lighten the colour (#6203).+ outp[b + c] = (acc[c] / acc[a]).clamp(0.0, 1.0) as f32;}outp[b + a] = al as f32;}@@ -498,6 +499,28 @@ mod tests {}}+ #[test]+ fn a_rotated_soft_edged_sprite_keeps_its_colour() {+ // #6203, as in Free Transform: the bicubic alpha overshoots past 1 next to the half+ // transparent edge, and must not lighten the colour.+ let mut src = Surface::new(PixelFormat::new(ColorMode::Rgb, SampleType::U8, true));+ src.fill_rect(Rect::new(10, 10, 30, 30), &[0.8, 0.6, 0.4, 0.5]);+ src.fill_rect(Rect::new(11, 11, 29, 29), &[0.8, 0.6, 0.4, 1.0]);+ let k = std::f64::consts::FRAC_1_SQRT_2;+ let rotate = |x: f64, y: f64| (20.0 + (x - 20.0) * k - (y - 20.0) * k, 20.0 + (x - 20.0) * k + (y - 20.0) * k);+ let out = warp_mesh_surface(&src, src.content_bounds(), &rotate, Interp::Bicubic);+ let b = out.content_bounds();+ for y in b.y0..b.y1 {+ for x in b.x0..b.x1 {+ let p = out.pixel(x, y);+ if p[3] > 0.0 {+ let off = (0..3).map(|i| (p[i] - [0.8, 0.6, 0.4][i]).abs()).fold(0.0f32, f32::max);+ assert!(off <= 1.0 / 255.0, "({x}, {y}): {p:?}");+ }+ }+ }+ }+#[test]fn translation_and_known_arc_points() {let src = sample(SampleType::U8);