Skip to content

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

Merge PR #7759: Actions: a setting the reference app's event cannot hold refuses the step

aiongga53c1ed1d744
diff --cc crates/engine/src/atn_cmds.rs
index 4170de955,900d69147..403e22d5a
--- a/crates/engine/src/atn_cmds.rs
+++ b/crates/engine/src/atn_cmds.rs
@@@ -969,18 -881,51 +969,58 @@@ macro_rules! ev
Some(m) => step($id, Value::Object(m)),
None => Mapped::Unsupported { harmless: false },
},
- to: |_, p| if pixel_target(p) && !($skip)(p) { fields_to(p, $fields, desc("null")) } else { None },
- to: |_, p| if pixel_target(p) && holds_its_settings($id, p) { fields_to(p, $fields, desc("null")) } else { None },
++ to: |_, p| if pixel_target(p) && !($skip)(p) && holds_its_settings($id, p) { fields_to(p, $fields, desc("null")) } else { None },
}
};
}
+/// Minimum / Maximum with Preserve: Roundness. The descriptor here maps the radius only, and
+/// Photoshop's encoding of the shape isn't verified, so such a step is not exported: it would
+/// replay as the default Squareness (#7233).
+fn preserves_roundness(p: &Value) -> bool {
+ p.get("preserve").and_then(Value::as_str) == Some("roundness")
+}
+
+ /// Settings a mapped event has no field for, by command: `(param, the value the event plays as)`.
+ /// Photoshop's event has nowhere to put these, so a step that sets one cannot be written: it
+ /// would import as a different edit, silently. Such a step is refused, the way [`pixel_target`]
+ /// refuses a filter aimed at a mask -- and no key Photoshop does not define is invented for it,
+ /// which the clean-room rule forbids and which would make the file wrong in another reader.
+ ///
+ /// A step that leaves the setting at the value the event plays as exports as before.
+ /// How a setting the event cannot carry behaves once the step is replayed without it. Which of
+ /// the two a setting takes follows the command's own reading of it, not the parameter spec.
+ enum Lost {
+ /// Replay uses this value, so only a step already at it survives. Numbers and strings are
+ /// both compared as written, since the commands below read these settings as written.
+ ReplaysAs(&'static str),
+ /// The command reads only this one spelling as anything other than what replay does, so every
+ /// other value survives -- a differently cased one included, because it too plays as replay's.
+ DiffersAt(&'static str),
+ }
+
+ const UNHOLDABLE: &[(&str, &[(&str, Lost)])] = &[
+ // Crst carries ClSz only; replay seeds the pattern from 0 (#7668).
+ ("filter.pixelate.crystallize", &[("seed", Lost::ReplaysAs("0"))]),
+ // RdlB carries Amnt, BlrM and BlrQ; replay spins or zooms about the middle of the layer.
+ ("filter.blur.radialBlur", &[("centerX", Lost::ReplaysAs("0.5")), ("centerY", Lost::ReplaysAs("0.5"))]),
+ // Mnm /Mxm carry Rds only; replay keeps squareness, and so does the command for anything but
+ // the exact string "roundness" -- see `preserve` in filters.rs.
+ ("filter.other.minimum", &[("preserve", Lost::DiffersAt("roundness"))]),
+ ("filter.other.maximum", &[("preserve", Lost::DiffersAt("roundness"))]),
+ ];
+
+ /// Whether every setting of `p` that its event cannot hold would play the same without it
+ /// (see [`UNHOLDABLE`]).
+ fn holds_its_settings(id: &str, p: &Value) -> bool {
+ let Some((_, settings)) = UNHOLDABLE.iter().find(|(cmd, _)| *cmd == id) else { return true };
+ settings.iter().all(|(param, lost)| match (p.get(param), lost) {
+ (None | Some(Value::Null), _) => true,
+ (Some(v), Lost::ReplaysAs(played)) => v.as_str() == Some(played) || v.as_f64().is_some_and(|n| played.parse::<f64>().is_ok_and(|d| n == d)),
+ (Some(v), Lost::DiffersAt(spelling)) => v.as_str() != Some(spelling),
+ })
+ }
+
/// Whether `p` edits the layer's pixels: no `"target"` or `"target":"pixels"`. Photoshop's
/// descriptor for a plain event has no field for a mask, Quick Mask or channel target, so such
/// a step can't be exported faithfully.
diff --cc crates/engine/src/atn_cmds/tests.rs
index 715fabb8e,1ec05e932..26c00287a
--- a/crates/engine/src/atn_cmds/tests.rs
+++ b/crates/engine/src/atn_cmds/tests.rs
@@@ -199,50 -199,53 +199,97 @@@ fn save_round_trips_through_photoshop_e
assert_eq!(r["set"], "Ours");
}
+/// `image.adjustments.selectiveColor` takes a range as an array (`"reds": [c, m, y, k]`) or as
+/// the scalar form that names one range (`"colors": "reds", "cyan": 35`). Only the arrays were
+/// written, so a step recorded the other way exported as an adjustment that changes nothing, with
+/// `skipped` empty (#7672).
+#[test]
+fn a_selective_color_step_in_the_scalar_form_keeps_its_values() {
+ let back = |params: Value| -> Value {
+ let mut s = Session::new();
+ s.actions.list.push(Action::new("A", vec![("image.adjustments.selectiveColor".into(), params)]));
+ let r = s.execute("actions.save", json!({"action": "A"})).unwrap();
+ assert_eq!(r["skipped"], json!([]));
+ let imported = import(&photocraft_paint::tile::b64_decode(r["data"].as_str().unwrap()).unwrap()).unwrap();
+ imported.actions[0].steps[0].1.clone()
+ };
+
+ // The numbers are read back as numbers; a percentage may arrive as 35 or 35.0.
+ let range =
+ |step: &Value, name: &str| -> Vec<f64> { step[name].as_array().unwrap_or(&Vec::new()).iter().map(|v| v.as_f64().unwrap_or(f64::NAN)).collect() };
+
+ // The reported step: one range, one channel.
+ let scalar = back(json!({"method": "absolute", "colors": "reds", "cyan": 35}));
+ assert_eq!(scalar["method"], "absolute");
+ assert_eq!(range(&scalar, "reds"), [35.0, 0.0, 0.0, 0.0], "{scalar}");
+
+ // The scalar form over an array keeps both, as playing the step would.
+ let both = back(json!({"reds": [10, 20, 30, 40], "colors": "reds", "cyan": 35}));
+ assert_eq!(range(&both, "reds"), [35.0, 20.0, 30.0, 40.0], "{both}");
+
+ // The array form is unchanged.
+ let array = back(json!({"method": "relative", "yellows": [0, 0, -15, 5]}));
+ assert_eq!(range(&array, "yellows"), [0.0, 0.0, -15.0, 5.0], "{array}");
+
+ // A channel the command would refuse is not quietly dropped: the step is refused too, since
+ // writing it would export an adjustment that is not the one recorded. `null` is absent, as
+ // the command reads it.
+ let mut s = Session::new();
+ s.actions.list.push(Action::new("A", vec![("image.adjustments.selectiveColor".into(), json!({"colors": "reds", "cyan": "35"}))]));
+ let e = s.execute("actions.save", json!({"action": "A"})).unwrap_err().to_string();
+ assert!(e.contains("image.adjustments.selectiveColor"), "{e}");
+
+ let nulled = back(json!({"colors": "reds", "cyan": 35, "magenta": null}));
+ assert_eq!(range(&nulled, "reds"), [35.0, 0.0, 0.0, 0.0], "{nulled}");
+}
+
+ /// A setting Photoshop's event has no field for is dropped by export, and the step then plays back
+ /// as a different edit: Crystallize's `seed` (#7668), Radial Blur's centre, Minimum's and
+ /// Maximum's `preserve`. Such a step is refused, as one with no equivalent event already is.
+ #[test]
+ fn a_setting_the_event_cannot_hold_is_refused() {
+ for (id, params) in [
+ ("filter.pixelate.crystallize", json!({"cellSize": 5, "seed": 37})),
+ ("filter.blur.radialBlur", json!({"amount": 10, "centerX": 0.25, "centerY": 0.5})),
+ ("filter.other.minimum", json!({"radius": 4, "preserve": "roundness"})),
+ ("filter.other.maximum", json!({"radius": 4, "preserve": "roundness"})),
+ ] {
+ let mut s = Session::new();
+ s.actions.list.push(Action::new("A", vec![(id.into(), params)]));
+ let before = s.actions.list[0].steps.clone();
+ let e = s.execute("actions.save", json!({"action": "A"})).unwrap_err().to_string();
+ assert!(e.contains(id), "{id}: {e}");
+ // A refused save leaves the action as it was: the step is still there to be played.
+ assert_eq!(s.actions.list[0].steps, before, "{id}");
+ let r = s.execute("actions.save", json!({"action": "A", "skipUnsupported": true})).unwrap();
+ assert_eq!(r["skipped"], json!([{"action": "A", "step": 0, "id": id}]), "{id}");
+ assert_eq!(s.actions.list[0].steps, before, "{id}: skipping a step does not drop it");
+ }
+ }
+
+ /// The same steps at the value the event plays as, and with the setting absent: written as before.
+ #[test]
+ fn settings_at_the_value_the_event_plays_still_export() {
+ for (id, params) in [
+ ("filter.pixelate.crystallize", json!({"cellSize": 5, "seed": 0})),
+ ("filter.pixelate.crystallize", json!({"cellSize": 5})),
+ ("filter.blur.radialBlur", json!({"amount": 10, "centerX": 0.5, "centerY": 0.5})),
+ ("filter.blur.radialBlur", json!({"amount": 10})),
+ ("filter.other.minimum", json!({"radius": 4, "preserve": "squareness"})),
+ ("filter.other.maximum", json!({"radius": 4})),
+ // `preserve` reaches the filter through a comparison with the exact string "roundness",
+ // so these play as squareness -- replay's own value -- and lose nothing by export (#7759
+ // review).
+ ("filter.other.minimum", json!({"radius": 4, "preserve": "Squareness"})),
+ ("filter.other.maximum", json!({"radius": 4, "preserve": "Roundness"})),
+ ] {
+ let mut s = Session::new();
+ s.actions.list.push(Action::new("A", vec![(id.into(), params.clone())]));
+ let r = s.execute("actions.save", json!({"action": "A"})).unwrap_or_else(|e| panic!("{id} {params}: {e}"));
+ assert_eq!(r["skipped"], json!([]), "{id} {params}");
+ }
+ }
+
#[test]
fn every_mapped_command_exports_and_imports_back() {
let cases: Vec<(&str, Value)> = vec![