Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Perspective Crop: draw the crop quad's side of the horizon (#7557) (#7558)

David Mann5225b592fe98
diff --git a/crates/algo/src/transform.rs b/crates/algo/src/transform.rs
index 3e67f1cde..91260bd28 100644
--- a/crates/algo/src/transform.rs
+++ b/crates/algo/src/transform.rs
@@ -333,7 +333,12 @@ fn warp(
// 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]));
+ // A clip wholly on one side of the inverse horizon only samples that side, so that side is drawn
+ // (a Perspective Crop low on a converging photo has most of the layer beyond its horizon, #7557).
+ let clip_w = clip.map(|c| [(c.x0, c.y0), (c.x1, c.y0), (c.x1, c.y1), (c.x0, c.y1)].map(|(x, y)| inv.w(x as f64, y as f64)));
+ let clip_side = clip_w.and_then(|cw| [1.0, -1.0].into_iter().find(|&s| cw.iter().all(|&w| s * w > 0.0)));
+ let front =
+ side.is_none().then(|| clip_side.unwrap_or_else(|| 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.
@@ -713,6 +718,22 @@ mod tests {
assert_eq!(ok.front_side([0.0, 0.0, 100.0, 100.0]), ok.w(50.0, 50.0).signum());
}
+ /// #7557: a Perspective Crop quad low on a strongly converging photo puts the horizon across
+ /// the layer with most of it (and its centre) on the far side. The clip maps wholly into the
+ /// quad, so its side is the one drawn, not the source's majority.
+ #[test]
+ fn a_clip_wholly_on_one_side_draws_that_side() {
+ let mut s = rgba();
+ s.fill_rect(Rect::new(0, 0, 100, 100), &[1.0, 0.0, 0.0, 1.0]);
+ let q = [[40.0, 70.0], [60.0, 70.0], [90.0, 95.0], [10.0, 95.0]];
+ let h = quad_to_rect(&q, 50.0, 39.0).unwrap();
+ assert!(h.front_side([0.0, 0.0, 100.0, 100.0]) * h.w(50.0, 80.0) < 0.0, "the source's majority is behind the quad's side");
+ for interp in [Interp::Nearest, Interp::Bilinear, Interp::Bicubic] {
+ let w = warp_surface_clipped(&s, s.content_bounds(), &h, interp, Rect::new(0, 0, 50, 39)).unwrap();
+ assert_eq!(w.rgba(25, 20), [1.0, 0.0, 0.0, 1.0], "{interp:?}");
+ }
+ }
+
#[test]
fn identity_and_translation_are_exact() {
let mut s = rgba();
diff --git a/crates/engine/src/perspective_crop_cmds.rs b/crates/engine/src/perspective_crop_cmds.rs
index 776b2b4d0..0cd1f0160 100644
--- a/crates/engine/src/perspective_crop_cmds.rs
+++ b/crates/engine/src/perspective_crop_cmds.rs
@@ -311,6 +311,19 @@ mod tests {
assert_eq!(corner, [0.0, 1.0, 0.0, 1.0]);
}
+ /// #7557: a quad low on a strongly converging photo (its horizon crosses the canvas, with the
+ /// canvas centre beyond it) is cropped, not left blank in the background colour.
+ #[test]
+ fn a_quad_below_a_horizon_across_the_canvas_keeps_its_content() {
+ let mut doc = Document::with_background("red", Size::new(100, 100), ColorMode::Rgb, SampleType::U8, Color::WHITE);
+ doc.layers[0].surface_mut().unwrap().fill_rect(Rect::from_xywh(0, 0, 100, 100), &[1.0, 0.0, 0.0, 1.0]);
+ let mut s = Session::new();
+ s.add_document(doc, None);
+ s.execute(CMD, json!({"corners": [[40, 70], [60, 70], [90, 95], [10, 95]], "width": 50, "height": 39})).unwrap();
+ let px = s.active().unwrap().doc.layers[0].surface().unwrap().rgba(25, 20);
+ assert_eq!(px, [1.0, 0.0, 0.0, 1.0]);
+ }
+
/// Bad quads and params are refused with an error, and the document is untouched.
#[test]
fn perspective_crop_rejects_bad_quads() {