Skip to content

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

Merge PR #7611: Refuse strict ATN export of Select and Mask with brush strokes

aiongg9d2612fc01af
diff --cc crates/engine/src/atn_cmds.rs
index 80e49e5a8,840274201..c68151e2b
--- a/crates/engine/src/atn_cmds.rs
+++ b/crates/engine/src/atn_cmds.rs
@@@ -2907,6 -2624,15 +2907,16 @@@ macro_rules! adjustment_event
const RIPPLE_SIZE: &[(&str, &str)] = &[("Sml", "small"), ("Mdm", "medium"), ("Lrg", "large")];
+ /// Select and Mask to Photoshop. The descriptor has no field for the Refine Edge Brush, so a
-/// step with `refineStrokes` can't be exported faithfully.
++/// step with `refineStrokes` can't be exported faithfully. One with `sampleAllLayers` is not
++/// exported here either: it exports as `smartBrushWorkspace`.
+ fn refine_edge_to(_: &str, p: &Value) -> Option<Descriptor> {
- if !pixel_target(p) || p.get("refineStrokes").is_some_and(|v| !v.is_null() && v.as_array().is_none_or(|a| !a.is_empty())) {
++ if !pixel_target(p) || p.get("sampleAllLayers").is_some() || p.get("refineStrokes").is_some_and(|v| !v.is_null() && v.as_array().is_none_or(|a| !a.is_empty())) {
+ return None;
+ }
+ fields_to(p, REFINE_EDGE, desc("null"))
+ }
+
/// Select and Mask (`refineSelectionEdge`). Smart Radius, decontamination and the output follow
/// Photoshop's; the edge itself is PhotoCraft's own matting, so results differ in detail.
const REFINE_EDGE: &[F] = &[
@@@ -3447,24 -3018,11 +3457,24 @@@ const EVENTS: &[Event] = &
Some(m) => step("select.refineEdge", Value::Object(m)),
None => Mapped::Unsupported { harmless: false },
},
- to: |_, p| if p.get("sampleAllLayers").is_some() { None } else { fields_to(p, REFINE_EDGE, desc("null")) },
+ to: refine_edge_to,
},
+ ev!("", "smartBrushWorkspace", "Select and Mask", "select.refineEdge", SMART_BRUSH),
+ ev!("", "autoCutout", "Select Subject", "select.subject", &[F("sampleAllLayers", "sampleAllLayers", Kind::Bool)]),
+ Event { code: "ClrR", name: "colorRange", label: "Color Range", ids: &["select.colorRange"], from: color_range_from, to: color_range_to },
// Edit.
Event { code: "Fl ", name: "fill", label: "Fill", ids: &["edit.fill", "paint.bucket"], from: fill_from, to: fill_to },
- ev!("Strk", "stroke", "Stroke", "edit.stroke", STROKE),
+ Event {
+ code: "Strk",
+ name: "stroke",
+ label: "Stroke",
+ ids: &["edit.stroke"],
+ from: |d| match fields_from(d, STROKE) {
+ Some(m) => step("edit.stroke", Value::Object(m)),
+ None => Mapped::Unsupported { harmless: false },
+ },
+ to: stroke_to,
+ },
ev!("copy", "copyEvent", "Copy", "edit.copy", &[]),
ev!("CpyM", "copyMerged", "Copy Merged", "edit.copyMerged", &[]),
ev!("cut ", "cut", "Cut", "edit.cut", &[]),
diff --cc crates/engine/src/atn_cmds/tests.rs
index 26c00287a,9419e6689..be52645d7
--- a/crates/engine/src/atn_cmds/tests.rs
+++ b/crates/engine/src/atn_cmds/tests.rs
@@@ -1841,50 -1388,25 +1841,73 @@@ 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"}));
+}
++
+ /// Photoshop's Select and Mask descriptor as mapped has no field for the Refine Edge Brush,
+ /// so a step with brush strokes must not export as a plain Select and Mask without them.
+ #[test]
+ fn refine_edge_with_brush_strokes_is_not_exported_without_them() {
+ for mode in ["add", "subtract"] {
+ let brushed = Action {
+ name: "Probe".into(),
+ steps: vec![("select.refineEdge".into(), json!({"radius": 1, "refineStrokes": [{"points": [[5, 4]], "size": 4, "mode": mode}]}))],
+ ..Default::default()
+ };
+ assert!(export("Audit", &[&brushed], &[], false).is_err(), "{mode}");
+ let (_, skipped) = export("Audit", &[&brushed], &[], true).unwrap();
+ assert_eq!(skipped, vec![Skipped { action: "Probe".into(), step: 0, id: "select.refineEdge".into() }]);
+ }
+ for params in [json!({"radius": 1}), json!({"radius": 1, "refineStrokes": []}), json!({"radius": 1, "refineStrokes": null})] {
+ let plain = Action { name: "Plain".into(), steps: vec![("select.refineEdge".into(), params)], ..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![("select.refineEdge".to_string(), json!({"radius": 1}))]);
+ }
+ }