Skip to content

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

Merge PR #8182: fix(actions): .atn export of Image Size with resample none no longer adds a pixel resize (#7189)

aionggb1cd5e802b2f
diff --cc crates/engine/src/atn_cmds/tests.rs
index f9446c174,385b7025a..d20d7acad
--- a/crates/engine/src/atn_cmds/tests.rs
+++ b/crates/engine/src/atn_cmds/tests.rs
@@@ -1811,71 -1388,26 +1811,95 @@@ fn function_keys_keep_shift_and_command
assert_eq!(super::function_key_name(7, false, false), "F7");
}
+/// A param a PhotoCraft step leaves out exports as PhotoCraft's default wherever Photoshop plays
+/// the missing key differently (checked against Photoshop): Canvas Size anchors top left,
+/// Apply Image blends Normal, Color Balance turns preserve luminosity off, and a Paint Bucket
+/// step without a tolerance doesn't play at all.
+#[test]
+fn defaults_photoshop_plays_differently_are_written() {
+ let exported = |id: &str, p: Value| export_item(id, &p, "S").and_then(|i| i.descriptor).unwrap_or_else(|| panic!("{id} does not export"));
+ let bucket = exported("paint.bucket", json!({"x": 1, "y": 2}));
+ assert_eq!((get_num(&bucket, "Tlrn"), get_bool(&bucket, "AntA")), (Some(32.0), Some(true)));
+ let canvas = exported("image.canvasSize", json!({"width": 10, "height": 10}));
+ assert_eq!((get_enum(&canvas, "Hrzn").as_deref(), get_enum(&canvas, "Vrtc").as_deref()), (Some("Cntr"), Some("Cntr")));
+ let apply = exported("image.applyImage", json!({}));
+ assert_eq!(get_obj(&apply, "With").and_then(|w| get_enum(w, "Clcl")).as_deref(), Some("Mltp"));
+ let balance = exported("image.adjustments.colorBalance", json!({"midtones": [10, 0, 0]}));
+ assert_eq!(get_bool(&balance, "PrsL"), Some(true));
+ assert_eq!(get_list(&balance, "ShdL").map(<[D]>::len), Some(3));
+}
+
+/// Photoshop sets one colour per step: a step that sets both doesn't export rather than drop one.
+#[test]
+fn set_colors_exports_only_a_single_colour() {
+ assert!(export_item("tools.setColors", &json!({"foreground": "#ff0000", "background": "#0000ff"}), "S").is_none());
+ assert!(export_item("tools.setColors", &json!({"foregroundCmyk": [0, 0, 0, 100], "background": "#0000ff"}), "S").is_none());
+ assert!(export_item("tools.setColors", &json!({"background": "#0000ff"}), "S").is_some());
+}
+
+/// Photoshop's reset of an adjustment layer (`set` with an empty `T`) stays a reset: `kind`
+/// only joins params that edit something, as any key makes `layer.setAdjustment` merge.
+#[test]
+fn adjustment_layer_reset_still_resets() {
+ let set = |t: Descriptor| desc("null").with("null", target_ref("AdjL")).with("T ", D::Descriptor(t));
+ let reset = set(desc("HStr").with("presetKind", enm("presetKindType", "presetKindDefault")));
+ let edit = set(desc("ClrB").with("MdtL", D::List(vec![long(10.0), long(0.0), long(0.0)])));
+ let file = AtnFile {
+ name: UnicodeString::new_nul("Reset"),
+ expanded: false,
+ actions: vec![AtnAction {
+ name: UnicodeString::new_nul("A"),
+ items: vec![item("setd", "Set", Some(reset)), item("setd", "Set", Some(edit))],
+ ..Default::default()
+ }],
+ warnings: vec![],
+ };
+ let steps = import(&atn::write(&file).unwrap()).unwrap().actions.remove(0).steps;
+ assert_eq!(steps[0], ("layer.setAdjustment".to_string(), json!({})));
+ assert_eq!(steps[1].1, json!({"midtones": [10, 0, 0], "kind": "colorBalance"}));
+}
+
+/// A crop with W x H x Resolution output options must not export as a plain rectangle crop
+/// that keeps the old size and resolution on replay: the mapper has no field for them.
+#[test]
+fn crop_with_output_size_or_resolution_is_not_exported_as_plain_crop() {
+ for params in [
+ json!({"x": 1, "y": 1, "width": 4, "height": 3, "targetWidth": 8, "targetHeight": 6, "resolution": 144, "resample": "nearest"}),
+ json!({"x": 1, "y": 1, "width": 4, "height": 3, "resolution": 144}),
+ json!({"x": 1, "y": 1, "width": 4, "height": 3, "targetWidth": 8, "targetHeight": 6}),
+ ] {
+ let sized = Action { name: "Probe".into(), steps: vec![("image.crop".into(), params)], ..Default::default() };
+ assert!(export("Audit", &[&sized], &[], false).is_err());
+ let (_, skipped) = export("Audit", &[&sized], &[], true).unwrap();
+ assert_eq!(skipped, vec![Skipped { action: "Probe".into(), step: 0, id: "image.crop".into() }]);
+ }
+ let plain = Action { name: "Plain".into(), steps: vec![("image.crop".into(), json!({"x": 1, "y": 1, "width": 4, "height": 3}))], ..Default::default() };
+ let (bytes, skipped) = export("Audit", &[&plain], &[], false).unwrap();
+ assert!(skipped.is_empty());
+ let imported = import(&bytes).unwrap();
+ assert_eq!(imported.actions[0].steps, vec![("image.crop".to_string(), json!({"x": 1, "y": 1, "width": 4, "height": 3, "deleteCroppedPixels": true}))]);
+}
++
+ /// `image.imageSize` with `resample: "none"` ignores width, height and percent; the `.atn`
+ /// round trip must not turn them into a pixel resize (#7189).
+ #[test]
+ fn image_size_resample_none_keeps_ignoring_its_size_through_atn_export() {
+ let event = event_for(&export_item("image.imageSize", &json!({"resolution": 72}), "S").expect("exports").event).expect("a known event");
+ let back = |p: Value| {
+ let item = export_item("image.imageSize", &p, "S")?;
+ let Mapped::Steps(steps) = (event.from)(item.descriptor.as_ref().unwrap()) else { panic!("does not import back") };
+ let mut back = steps[0].1.clone();
+ normalise(&mut back);
+ Some(back)
+ };
+ for sized in [json!({"width": 12, "height": 10}), json!({"width": 12}), json!({"percent": 200}), json!({"width": 12, "percent": 50, "height": 3})] {
+ let mut p = sized.clone();
+ p["resolution"] = json!(144);
+ p["resample"] = json!("none");
+ assert_eq!(back(p.clone()), Some(json!({"resolution": 144, "resample": "none"})), "{p}");
+ }
+ // A step with nothing left to record is refused rather than exported as an empty (unsupported) event.
+ assert_eq!(back(json!({"width": 12, "height": 10, "resample": "none"})), None);
+ // Real resizes are untouched.
+ assert_eq!(back(json!({"width": 12, "height": 10, "resample": "bicubic"})), Some(json!({"width": 12, "height": 10, "resample": "bicubic"})));
+ }