PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Crop: Color Samplers scale with a W x H crop (#7012)
David Mann30505e3b53f2
diff --git a/crates/engine/src/image_cmds.rs b/crates/engine/src/image_cmds.rsindex 3e870aa68..48c43b102 100644--- a/crates/engine/src/image_cmds.rs+++ b/crates/engine/src/image_cmds.rs@@ -480,7 +480,7 @@ fn crop(s: &mut Session, p: &Value) -> Result<Value> {if r.is_empty() {return Err(EngineError::Other("nothing to crop: pass x/y/width/height or make a selection".into()));}- s.set_sampler_map(photocraft_geom::Affine::translate(-f64::from(r.x0), -f64::from(r.y0)));+ s.set_sampler_map(target.sampler_map(r));let extension = s.tools.background;s.edit("Crop", |doc, active| {let original = doc.size;@@ -513,6 +513,19 @@ impl CropTarget {doc.resolution_dpi = r;}}++ /// Where Color Sampler points go: shifted by the crop's origin, then scaled with the pixels+ /// when [`Self::apply`] resamples the cropped area `r` (as Image Size scales them).+ fn sampler_map(&self, r: Rect) -> photocraft_geom::Affine {+ let shift = photocraft_geom::Affine::translate(-f64::from(r.x0), -f64::from(r.y0));+ match self.size {+ Some((w, h)) if r.width() > 0 && r.height() > 0 => {+ let (sx, sy) = (f64::from(w) / f64::from(r.width()), f64::from(h) / f64::from(r.height()));+ shift.then(&photocraft_geom::Affine { m: [sx, 0.0, 0.0, sy, 0.0, 0.0] })+ }+ _ => shift,+ }+ }}/// The highest resolution (ppi) a crop may set, as for Image Size.@@ -631,7 +644,7 @@ fn rotated_crop(s: &mut Session, r: Rect, angle: f64, delete: bool, content_awarcrate::analysis_cmds::compound(s, "Crop", |s| {crate::mode_cmds::rotate_arbitrary(s, &json!({"angle": angle, "direction": "ccw"}))?;// Color Sampler points follow the crop's origin shift too (the rotation moved them above).- s.set_sampler_map(photocraft_geom::Affine::translate(-f64::from(target.x0), -f64::from(target.y0)));+ s.set_sampler_map(crop_target.sampler_map(target));let extension = s.tools.background;s.edit("Crop", |doc, active| {crop_doc(doc, CMD, target, delete, extension)?;diff --git a/crates/engine/src/sampler_cmds.rs b/crates/engine/src/sampler_cmds.rsindex 472ecb672..b7e92550f 100644--- a/crates/engine/src/sampler_cmds.rs+++ b/crates/engine/src/sampler_cmds.rs@@ -388,6 +388,24 @@ mod tests {assert_eq!(p[0]["position"], json!([10.0, 10.0]));}+ #[test]+ fn a_crop_to_a_target_size_scales_points() {+ // The crop resamples the 20 x 20 area to 40 x 40: the point (10, 10 after the shift) doubles.+ for angle in [None, Some(90)] {+ let mut s = session();+ s.execute("view.colorSamplers.add", json!({"x": 50, "y": 40})).unwrap();+ let mut p = json!({"x": 40, "y": 30, "width": 20, "height": 20, "targetWidth": 40, "targetHeight": 40});+ if let Some(a) = angle {+ p["angle"] = json!(a);+ }+ s.execute("image.crop", p).unwrap();+ let p = points(&mut s);+ assert_eq!(p.len(), 1, "angle {angle:?}");+ assert_eq!(p[0]["position"], json!([20.0, 20.0]), "angle {angle:?}");+ assert_eq!(s.take_samplers_removed(), 0, "angle {angle:?}");+ }+ }+#[test]fn bad_params_and_no_document_are_errors_not_panics() {let mut s = Session::new();