PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Distort: a quad dragged past the horizon draws only its front side, in the preview and the result (#4863) (#4870)
F4LCON96c0241b26e9
diff --git a/crates/algo/src/transform.rs b/crates/algo/src/transform.rsindex 8f841e745..7d1dd0fe6 100644--- a/crates/algo/src/transform.rs+++ b/crates/algo/src/transform.rs@@ -43,6 +43,27 @@ impl Homography {((m[0] * x + m[1] * y + m[2]) / w, (m[3] * x + m[4] * y + m[5]) / w)}+ /// The homogeneous `w` of `(x, y)`: its sign is the side of the horizon the point maps to+ /// (0 on the horizon itself).+ pub fn w(&self, x: f64, y: f64) -> f64 {+ let m = &self.0;+ m[6] * x + m[7] * y + m[8]+ }++ /// The side of the horizon (+1 or −1) that most corners of `r` (`[x0, y0, x1, y1]`) map to,+ /// the centre's on a tie. A Distort dragged past the horizon maps the rest of the source to the+ /// far side, where it would show as a mirrored copy wrapped round infinity; drawing keeps to+ /// this side, as Photoshop's does (#4863).+ pub fn front_side(&self, r: [f64; 4]) -> f64 {+ let ahead = [(r[0], r[1]), (r[2], r[1]), (r[2], r[3]), (r[0], r[3])].iter().filter(|(x, y)| self.w(*x, *y) > 0.0).count();+ match ahead {+ 3 | 4 => 1.0,+ 0 | 1 => -1.0,+ _ if self.w((r[0] + r[2]) / 2.0, (r[1] + r[3]) / 2.0) >= 0.0 => 1.0,+ _ => -1.0,+ }+ }+pub fn mul(&self, o: &Homography) -> Homography {let (a, b) = (&self.0, &o.0);let mut r = [0.0; 9];@@ -286,6 +307,10 @@ fn warp(let fwd_w = [(sx0, sy0), (sx1, sy0), (sx1, sy1), (sx0, sy1)].map(|(x, y)| h.0[6] * x + h.0[7] * y + h.0[8]);let side = [1.0, -1.0].into_iter().find(|&s| fwd_w.iter().all(|&c| s * c > 0.0));let w_lo = 1.0 / fwd_w.iter().fold(0.0f64, |m, c| m.max(c.abs()));+ // A source straddling the horizon draws only its front side: the inverse w at a pixel has the+ // sign of the forward w at the source point it samples, and the other side would show as a+ // mirrored copy wrapped round infinity (#4863).+ let front = side.is_none().then(|| h.front_side([src_rect.x0 as f64, src_rect.y0 as f64, src_rect.x1 as f64, src_rect.y1 as f64]));let bounds = corner_bounds(&corners);let dst = match (clip, bounds) {// The warped corners only bound the result while the whole source is in front of the horizon.@@ -377,6 +402,9 @@ fn warp(let mut acc = [0.0f64; 8];for y in t.y0..t.y1 {for x in t.x0..t.x1 {+ if front.is_some_and(|s| s * inv.w(x as f64 + 0.5, y as f64 + 0.5) <= 0.0) {+ continue;+ }let (u, v) = inv.apply(x as f64 + 0.5, y as f64 + 0.5);let (u, v) = (u - 0.5, v - 0.5);if u < src_rect.x0 as f64 - 1.0 || v < src_rect.y0 as f64 - 1.0 || u > src_rect.x1 as f64 || v > src_rect.y1 as f64 {@@ -563,6 +591,44 @@ mod tests {}}+ /// #4863: a Distort with the bottom corners pulled in and one dragged past the horizon (a+ /// concave quad) leaves part of the source behind the horizon. That part maps to the far side,+ /// where it showed as a mirrored copy wrapped round infinity; only the front side is drawn.+ #[test]+ fn a_distort_past_the_horizon_draws_only_its_front_side() {+ let mut s = rgba();+ s.fill_rect(Rect::new(0, 0, 100, 100), &[0.0, 0.5, 1.0, 1.0]);+ let r = s.content_bounds();+ let h = Homography::rect_to_quad([0.0, 0.0, 100.0, 100.0], [[0.0, 0.0], [300.0, 0.0], [60.0, 60.0], [0.0, 300.0]]).unwrap();+ let corners = [(0.0, 0.0), (100.0, 0.0), (100.0, 100.0), (0.0, 100.0)].map(|(x, y)| h.w(x, y));+ assert!(corners.iter().any(|w| *w > 0.0) && corners.iter().any(|w| *w < 0.0), "the source straddles the horizon: {corners:?}");+ let front = h.front_side([0.0, 0.0, 100.0, 100.0]);+ let inv = h.inverse().unwrap();+ let clip = Rect::new(-400, -400, 700, 700);+ for interp in [Interp::Nearest, Interp::Bilinear] {+ let w = warp_surface_clipped(&s, r, &h, interp, clip).unwrap();+ let px = w.read_region(clip);+ let (mut drawn, mut behind) = (0, 0);+ for y in clip.y0..clip.y1 {+ for x in clip.x0..clip.x1 {+ let i = ((y - clip.y0) * clip.width() as i32 + (x - clip.x0)) as usize * 4;+ if px[i + 3] > 0.0 {+ drawn += 1;+ // The source point this pixel samples is on the far side of the horizon.+ if front * inv.w(x as f64 + 0.5, y as f64 + 0.5) <= 0.0 {+ behind += 1;+ }+ }+ }+ }+ assert!(drawn > 1000, "{interp:?}: the front side is drawn ({drawn} px)");+ assert_eq!(behind, 0, "{interp:?}: {behind} pixels come from behind the horizon");+ }+ // A quad wholly in front of the horizon is drawn exactly as before.+ let ok = Homography::rect_to_quad([0.0, 0.0, 100.0, 100.0], [[0.0, 0.0], [300.0, 0.0], [155.0, 155.0], [0.0, 300.0]]).unwrap();+ assert_eq!(ok.front_side([0.0, 0.0, 100.0, 100.0]), ok.w(50.0, 50.0).signum());+ }+#[test]fn identity_and_translation_are_exact() {let mut s = rgba();diff --git a/crates/ui-egui/src/transform_tool.rs b/crates/ui-egui/src/transform_tool.rsindex 8ee9c08e6..a4f64baf9 100644--- a/crates/ui-egui/src/transform_tool.rs+++ b/crates/ui-egui/src/transform_tool.rs@@ -2033,6 +2033,15 @@ pub fn cursor(app: &PhotocraftApp, p: [f64; 2], quick_split: bool) -> Option<Cur})}+/// For each vertex of the preview's `n` × `n` grid over the unit square (row by row), whether it+/// maps to the front side of `h`'s horizon. A Distort dragged past the horizon sends the rest of+/// the image to the far side, where its cells would wrap round infinity; the preview leaves them+/// out, as the committed warp does (#4863).+fn front_vertices(h: &Homography, n: usize) -> Vec<bool> {+ let front = h.front_side([0.0, 0.0, 1.0, 1.0]);+ (0..=n).flat_map(|j| (0..=n).map(move |i| (i as f64 / n as f64, j as f64 / n as f64))).map(|(u, v)| front * h.w(u, v) > 1e-9).collect()+}+/// Warped preview of the moving pixels plus the box, handles and reference point.pub fn draw_overlay(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform) {let (Some(t), Some(pv)) = (&app.ui.transform, &app.transform_preview) else { return };@@ -2069,6 +2078,7 @@ pub fn draw_overlay(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform};let mut mesh = egui::Mesh::with_texture(tex);let tint = Color32::from_white_alpha((pv.opacity.clamp(0.0, 1.0) * 255.0) as u8);+ let ahead = front_vertices(&h, n);for j in 0..=n {for i in 0..=n {let (u, v) = (i as f64 / n as f64, j as f64 / n as f64);@@ -2079,8 +2089,12 @@ pub fn draw_overlay(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXformfor j in 0..n {for i in 0..n {let a = (j * (n + 1) + i) as u32;- mesh.add_triangle(a, a + 1, a + n as u32 + 2);- mesh.add_triangle(a, a + n as u32 + 2, a + n as u32 + 1);+ for tri in [[a, a + 1, a + n as u32 + 2], [a, a + n as u32 + 2, a + n as u32 + 1]] {+ // A cell with a corner past the horizon would stretch across the canvas (#4863).+ if tri.iter().all(|&k| ahead[k as usize]) {+ mesh.add_triangle(tri[0], tri[1], tri[2]);+ }+ }}}painter.add(clip_to_canvas(mesh, canvas_size(pv), |p| xf.to_screen(p.x, p.y)));@@ -4045,6 +4059,20 @@ mod tests {/// #1767: just outside a corner is the rotate zone: the OS pointer hides for the drawn curved/// arrow, which bends round that corner.+ #[test]+ fn the_preview_leaves_out_cells_past_the_horizon() {+ // #4863: the bottom corners pulled in and one dragged far out: a concave quad whose+ // source straddles the horizon. Its cells there are left out; an ordinary quad keeps all.+ let past = Homography::rect_to_quad([0.0, 0.0, 1.0, 1.0], [[0.0, 0.0], [300.0, 0.0], [60.0, 60.0], [0.0, 300.0]]).unwrap();+ let ahead = front_vertices(&past, 24);+ assert!(ahead.iter().any(|a| *a) && ahead.iter().any(|a| !*a), "some of the grid is past the horizon");+ // Three corners are on one side (drawn); the top-left one is past the horizon.+ assert!(ahead[24 * 25 + 24] && ahead[24] && ahead[24 * 25], "the three corners in front are drawn");+ assert!(!ahead[0], "the corner past the horizon is left out");+ let plain = Homography::rect_to_quad([0.0, 0.0, 1.0, 1.0], [[0.0, 0.0], [300.0, 0.0], [280.0, 260.0], [0.0, 300.0]]).unwrap();+ assert!(front_vertices(&plain, 24).iter().all(|a| *a));+ }+#[test]fn hovering_just_outside_a_corner_asks_for_the_rotate_cursor() {let mut app = app_with_square(256, photocraft_geom::Rect::new(32, 32, 160, 160));