PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Keep Color Sampler points on their pixel after flips and turns (#6720)
NetterSilverdfb4aa70ae42
diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rsindex ced8462c9..09ea34233 100644--- a/crates/engine/src/commands.rs+++ b/crates/engine/src/commands.rs@@ -117,7 +117,10 @@ fn has_selection(s: &Session) -> std::result::Result<(), String> {fn turn(s: &mut Session, label: &str, t: crate::canvas_geom::Turn) -> Result<Value> {if let Some(d) = s.active() {let a = t.affine(f64::from(d.doc.size.width), f64::from(d.doc.size.height));- s.set_sampler_map(a);+ // Sampler points are pixel indices, not pixel edges: map the pixel's centre, so the point+ // lands on the same pixel `Turn::pixel_map` moves the pixels to (`w - 1 - x`).+ let half = photocraft_geom::Affine::translate(0.5, 0.5);+ s.set_sampler_map(half.then(&a).then(&photocraft_geom::Affine::translate(-0.5, -0.5)));}s.edit(label, |doc, _| {crate::canvas_geom::turn_canvas(doc, t);diff --git a/crates/engine/src/sampler_cmds.rs b/crates/engine/src/sampler_cmds.rsindex 472ecb672..0894ab72e 100644--- a/crates/engine/src/sampler_cmds.rs+++ b/crates/engine/src/sampler_cmds.rs@@ -377,6 +377,35 @@ mod tests {assert!((x - 40.0).abs() < 1e-6 && (y - 50.0).abs() < 1e-6, "{pos:?}");}+ #[test]+ fn flips_and_right_angle_turns_keep_points_on_their_pixel() {+ use crate::canvas_geom::Turn;+ for (id, turn) in [+ ("image.imageRotation.flipCanvasHorizontal", Turn::FlipHorizontal),+ ("image.imageRotation.flipCanvasVertical", Turn::FlipVertical),+ ("image.imageRotation.180", Turn::Rotate180),+ ("image.imageRotation.90cw", Turn::Cw90),+ ("image.imageRotation.90ccw", Turn::Ccw90),+ ] {+ // A black pixel at (3, 3) of a white 100×80 document, with a point on it.+ let mut s = session();+ s.edit("paint", |doc, _| {+ let surf = doc.layers[0].surface_mut().unwrap();+ let black = photocraft_raster::from_rgba(&surf.format(), [0.0, 0.0, 0.0, 1.0]);+ surf.fill_rect(Rect::new(3, 3, 4, 4), &black);+ Ok(())+ })+ .unwrap();+ s.execute("view.colorSamplers.add", json!({"x": 3, "y": 3})).unwrap();+ s.execute(id, json!({})).unwrap();+ let (x, y) = turn.pixel_map(100, 80)(3, 3);+ let p = points(&mut s);+ assert_eq!(p.len(), 1, "{id}");+ assert_eq!(p[0]["position"], json!([f64::from(x), f64::from(y)]), "{id}");+ assert_eq!(p[0]["rgb"], json!([0, 0, 0]), "{id}");+ }+ }+#[test]fn a_rotated_crop_moves_points() {// The crop frame is centred on the point, so the point lands at the new canvas centre.