PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Image Rotation: Arbitrary keeps Color Sampler points on their pixel (#7372) (#7373)
David Mannd459ca7939bd
diff --git a/crates/engine/src/mode_cmds.rs b/crates/engine/src/mode_cmds.rsindex 4625368ff..fbe58af6a 100644--- a/crates/engine/src/mode_cmds.rs+++ b/crates/engine/src/mode_cmds.rs@@ -78,13 +78,15 @@ pub(crate) fn rotate_arbitrary(s: &mut Session, p: &Value) -> Result<Value> {}let bg = s.tools.background;// Color Sampler points turn with the canvas (the same map the edit applies to the geometry).+ // They are pixel indices, so map the pixel's centre, as the warp samples the pixels.{let old = s.active().ok_or(EngineError::NoDocument)?.doc.size;let new = rotated_size(old, deg);let (sn, cs) = deg.to_radians().sin_cos();let (cx, cy) = (f64::from(old.width) / 2.0, f64::from(old.height) / 2.0);let (nx, ny) = (f64::from(new.width) / 2.0, f64::from(new.height) / 2.0);- s.set_sampler_map(Affine { m: [cs, sn, -sn, cs, nx - (cs * cx - sn * cy), ny - (sn * cx + cs * cy)] });+ let a = Affine { m: [cs, sn, -sn, cs, nx - (cs * cx - sn * cy), ny - (sn * cx + cs * cy)] };+ s.set_sampler_map(Affine::translate(0.5, 0.5).then(&a).then(&Affine::translate(-0.5, -0.5)));}let size = s.edit("Rotate Canvas", |doc, _| {let old = doc.size;diff --git a/crates/engine/src/sampler_cmds.rs b/crates/engine/src/sampler_cmds.rsindex 0894ab72e..57787b2e8 100644--- a/crates/engine/src/sampler_cmds.rs+++ b/crates/engine/src/sampler_cmds.rs@@ -365,7 +365,7 @@ mod tests {#[test]fn a_right_angle_rotation_moves_points() {- // 90° clockwise about the centre: the doc centre stays put.+ // 90° clockwise about the centre: pixel (x, y) of the 100×80 canvas goes to (79 - y, x).let mut s = session();s.execute("view.colorSamplers.add", json!({"x": 50, "y": 40})).unwrap();s.execute("image.rotation.arbitrary", json!({"angle": 90, "direction": "cw"})).unwrap();@@ -373,8 +373,7 @@ mod tests {assert_eq!(p.len(), 1);let pos = p[0]["position"].as_array().unwrap();let (x, y) = (pos[0].as_f64().unwrap(), pos[1].as_f64().unwrap());- // The new canvas is 80×100, so the centre is (40, 50).- assert!((x - 40.0).abs() < 1e-6 && (y - 50.0).abs() < 1e-6, "{pos:?}");+ assert!((x - 39.0).abs() < 1e-6 && (y - 50.0).abs() < 1e-6, "{pos:?}");}#[test]@@ -406,15 +405,46 @@ mod tests {}}+ #[test]+ fn an_arbitrary_rotation_keeps_points_on_their_pixel() {+ // The pixel at (3, 3) of the 100×80 document, and where each turn moves it.+ for (angle, direction, at) in [(90, "cw", [76.0, 3.0]), (90, "ccw", [3.0, 96.0]), (180, "cw", [96.0, 76.0])] {+ 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("view.colorSamplers.add", json!({"x": 0, "y": 0})).unwrap();+ s.execute("image.rotation.arbitrary", json!({"angle": angle, "direction": direction})).unwrap();+ assert_eq!(s.take_samplers_removed(), 0, "{angle} {direction}: the corner point stays on the canvas");+ let p = points(&mut s);+ assert_eq!(p.len(), 2, "{angle} {direction}: the corner point stays on the canvas");+ assert_eq!(p[0]["position"], json!(at), "{angle} {direction}");+ assert_eq!(p[0]["rgb"], json!([0, 0, 0]), "{angle} {direction}");+ }+ }+#[test]fn a_rotated_crop_moves_points() {- // The crop frame is centred on the point, so the point lands at the new canvas centre.+ // The crop frame is centred on the point's pixel; turned 90° that pixel lands at (10, 9).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(50, 40, 51, 41), &black);+ Ok(())+ })+ .unwrap();s.execute("view.colorSamplers.add", json!({"x": 50, "y": 40})).unwrap();s.execute("image.crop", json!({"x": 40, "y": 30, "width": 20, "height": 20, "angle": 90})).unwrap();let p = points(&mut s);assert_eq!(p.len(), 1);- assert_eq!(p[0]["position"], json!([10.0, 10.0]));+ assert_eq!(p[0]["position"], json!([10.0, 9.0]));+ assert_eq!(p[0]["rgb"], json!([0, 0, 0]));}#[test]