Skip to content

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

Merge remote-tracking branch 'origin/main' into fix/atn-transform-selection

Finn Newick54ede8403703
diff --cc crates/engine/src/atn_cmds/tests.rs
index 30bc733a5,f50189fb0..5235090fa
--- a/crates/engine/src/atn_cmds/tests.rs
+++ b/crates/engine/src/atn_cmds/tests.rs
@@@ -1033,33 -1037,132 +1037,159 @@@ fn remove_white_clears_the_targeted_lay
assert!(bottom.surface().unwrap().sample_channel(2, 2, 1) > 0.99, "and kept its pixels");
}
+/// #7515: a Transform Selection's interpolation survives the round trip as a layer transform's
+/// does. #7516-#7519: a reference point other than the centre, a skew or a warp can't be written,
+/// so the step is refused instead of replaying as a plain scale.
+#[test]
+fn transform_selection_keeps_its_interpolation_and_refuses_what_it_cannot_carry() {
+ let one = |id: &str, p: Value| Action { name: "Probe".into(), steps: vec![(id.into(), p)], ..Default::default() };
+ let p = json!({"dx": 0.7, "dy": 0, "scaleX": 140, "scaleY": 80, "interpolation": "nearest"});
+ let (bytes, skipped) = export("Audit7", &[&one("select.transformSelection", p.clone())], &[], false).unwrap();
+ assert!(skipped.is_empty());
+ let imported = import(&bytes).unwrap();
+ assert_eq!(imported.actions[0].steps, vec![("select.transformSelection".to_string(), p)]);
+ for id in ["select.transformSelection", "edit.transform"] {
+ for p in [
+ json!({"scaleX": 140, "reference": "topLeft"}),
+ json!({"scaleX": 140, "reference": [3, 4]}),
+ json!({"scaleX": 140, "skewX": 12}),
+ json!({"scaleX": 140, "skewY": -5}),
+ json!({"warp": "arc"}),
+ json!({"corners": [[0, 0], [10, 0], [10, 10], [0, 10]]}),
+ ] {
+ assert!(export("Audit7", &[&one(id, p.clone())], &[], false).is_err(), "{id} {p}");
+ }
+ let carried = json!({"scaleX": 140, "reference": "center", "skewX": 0, "skewY": 0});
+ assert!(export("Audit7", &[&one(id, carried.clone())], &[], false).is_ok(), "{id} {carried}");
+ }
+}
+
+ /// Smart Blur's quality uses Photoshop's `SmBQ` codes (`SBQL`/`SBQM`/`SBQH`): measured in
+ /// Photoshop, the old `Hgh `/`Mdm `/`Low ` play back as its fallback quality. Old codes still read.
+ #[test]
+ fn smart_blur_quality_uses_photoshop_codes() {
+ let smart_blur =
+ |quality: D, mode: &str| desc("null").with("Rds ", D::Double(6.0)).with("Thsh", D::Double(60.0)).with("SmBQ", quality).with("SmBM", enm("SmBM", mode));
+ // Every quality writes Photoshop's code, as a double radius/threshold and both enums,
+ // and reads back as the same step.
+ for (quality, code) in [("low", "SBQL"), ("medium", "SBQM"), ("high", "SBQH")] {
+ let params = json!({"radius": 6, "threshold": 60, "quality": quality, "mode": "edgeOnly"});
+ let a = Action::new("Q", vec![("filter.blur.smartBlur".into(), params.clone())]);
+ let (bytes, skipped) = export("S", &[&a], &[], false).unwrap();
+ assert!(skipped.is_empty());
+ let file = atn::parse(&bytes).unwrap();
+ let it = &file.actions[0].items[0];
+ assert_eq!(it.event.id(), "SmrB");
+ assert_eq!(it.descriptor.as_ref(), Some(&smart_blur(enm("SmBQ", code), "SBME")), "{quality}");
+ let back = import(&bytes).unwrap();
+ assert!(back.unsupported.is_empty(), "{quality}");
+ assert_eq!(back.actions[0].steps, vec![("filter.blur.smartBlur".to_string(), params)]);
+ }
+
+ // Photoshop-shaped steps (each quality and mode, as Photoshop stores them) and the codes
+ // PhotoCraft wrote before this fix read as the right quality.
+ let cases = [
+ ("SBQH", "SBMN", "high", "normal"),
+ ("SBQM", "SBMO", "medium", "overlayEdge"),
+ ("SBQL", "SBME", "low", "edgeOnly"),
+ ("Hgh ", "SBMN", "high", "normal"),
+ ("Mdm ", "SBMN", "medium", "normal"),
+ ("Low ", "SBMN", "low", "normal"),
+ ];
+ let items = cases.iter().map(|(q, m, _, _)| item("SmrB", "Smart Blur", Some(smart_blur(enm("SmBQ", q), m)))).collect();
+ let data = set_of("Blurs", vec![("Smart", items)]);
+ let mut s = Session::new();
+ s.execute("actions.load", json!({"data": data})).unwrap();
+ let steps = &s.actions.list[0].steps;
+ for ((q, _, quality, mode), (id, p)) in cases.iter().zip(steps) {
+ assert_eq!(id, "filter.blur.smartBlur", "{q}");
+ assert_eq!(p, &json!({"radius": 6, "threshold": 60, "quality": quality, "mode": mode}), "{q}");
+ }
+
+ // An unknown code, a quality of the wrong type, NaN and a missing quality never panic: the
+ // unknown ones stay Photoshop steps, kept verbatim.
+ let odd = [
+ smart_blur(enm("SmBQ", "Xxxx"), "SBMN"),
+ smart_blur(D::Integer(2), "SBMN"),
+ smart_blur(enm("SmBQ", "SBQH"), "Xxxx"),
+ desc("null").with("Rds ", D::Double(f64::NAN)).with("SmBQ", enm("SmBQ", "SBQM")),
+ desc("null"),
+ ];
+ let items: Vec<AtnItem> = odd.iter().map(|d| item("SmrB", "Smart Blur", Some(d.clone()))).collect();
+ let imported = import(&photocraft_paint::tile::b64_decode(&set_of("Odd", vec![("Odd", items)])).unwrap()).unwrap();
+ let steps = &imported.actions[0].steps;
+ assert_eq!(steps.len(), odd.len());
+ assert_eq!(steps[0].0, PHOTOSHOP_STEP, "an unknown quality is kept verbatim");
+ assert_eq!(steps[2].0, PHOTOSHOP_STEP, "an unknown mode is kept verbatim");
+ assert_eq!(imported.unsupported.iter().map(|u| u.step).collect::<Vec<_>>(), [0, 2]);
+ // A wrong-type quality is left out (the filter's default applies), as for every enum field.
+ assert_eq!(steps[1], ("filter.blur.smartBlur".to_string(), json!({"radius": 6, "threshold": 60, "mode": "normal"})));
+ // An unknown quality or a non-string one is not exported.
+ for bad in [json!({"quality": "best"}), json!({"quality": 3}), json!({"radius": "x"})] {
+ let a = Action::new("Bad", vec![("filter.blur.smartBlur".into(), bad.clone())]);
+ assert!(export("S", &[&a], &[], false).is_err(), "{bad}");
+ }
+ }
+
+ /// #5830: Shadows/Highlights (`adaptCorrect`) recorded by Photoshop, with string IDs, plays as
+ /// Image › Adjustments › Shadows/Highlights instead of stopping as a step with no equivalent.
+ #[test]
+ fn photoshop_shadows_highlights_steps_play() {
+ let tones = |amount: f64, width: f64, radius: i32| {
+ D::Descriptor(desc("adaptCorrectTones").with("amount", pct(amount)).with("width", pct(width)).with("radius", D::Integer(radius)))
+ };
+ let d = desc("null")
+ .with("shadowMode", tones(35.0, 50.0, 30))
+ .with("highlightMode", tones(20.0, 40.0, 24))
+ .with("blackClip", D::Double(0.01))
+ .with("whiteClip", D::Double(0.02))
+ .with("center", D::Integer(5))
+ .with("colorCorrection", D::Integer(20));
+ let e = event_for(&item("adaptCorrect", "Sombra/Realce", None).event).expect("adaptCorrect is mapped");
+ let Mapped::Steps(steps) = (e.from)(&d) else { panic!("Shadows/Highlights maps to a step") };
+ assert_eq!(steps.len(), 1);
+ let (id, p) = &steps[0];
+ assert_eq!(id, "image.adjustments.shadowsHighlights");
+ let mut p = p.clone();
+ normalise(&mut p);
+ assert_eq!(
+ p,
+ json!({"shadowAmount": 35, "shadowTone": 50, "shadowRadius": 30, "highlightAmount": 20, "highlightTone": 40, "highlightRadius": 24, "blackClip": 0.01, "whiteClip": 0.02, "midtone": 5, "color": 20})
+ );
+ // The same settings as char IDs read the same; absent keys are left to the defaults.
+ let codes = desc("null")
+ .with("sdwM", D::Descriptor(desc("adaptCorrectTones").with("Amnt", pct(35.0)).with("Wdth", pct(50.0)).with("Rds ", D::Integer(30))))
+ .with("BlcC", D::Double(0.01));
+ let Mapped::Steps(steps) = (e.from)(&codes) else { panic!("char IDs map too") };
+ let mut q = steps[0].1.clone();
+ normalise(&mut q);
+ assert_eq!(q, json!({"shadowAmount": 35, "shadowTone": 50, "shadowRadius": 30, "blackClip": 0.01}));
+ // And it runs on a document.
+ let mut s = Session::new();
+ s.execute("file.new", json!({"width": 16, "height": 16, "background": "#404040"})).unwrap();
+ s.execute(id, p).unwrap();
+ }
+
+ #[test]
+ fn save_while_recording_includes_pending_steps() {
+ let mut s = Session::new();
+ s.execute("file.new", json!({"width": 8, "height": 8})).unwrap();
+ s.execute("actions.record", json!({"name": "Pending"})).unwrap();
+ s.execute("layer.new.layer", json!({"name": "Recorded"})).unwrap();
+
+ // While still recording (without calling actions.stop), save the action.
+ let saved = s.execute("actions.save", json!({"action": "Pending"})).unwrap();
+ assert_eq!(saved["steps"], 1, "pending steps must be flushed and included in save");
+ assert_eq!(saved["skipped"], json!([]));
+
+ // After save, recording continues and additional steps are flushed on stop without duplication.
+ s.execute("layer.new.layer", json!({"name": "Second"})).unwrap();
+ s.execute("actions.stop", json!({})).unwrap();
+
+ let action = s.execute("actions.get", json!({"action": "Pending"})).unwrap();
+ assert_eq!(action["steps"].as_array().unwrap().len(), 2, "both steps must be present without duplication");
+ }
+
/// Photoshop's Close has no document target: a Close of a specific document must not be
/// exported as a plain Close, which would close the active document on playback.
#[test]