Skip to content

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

Refuse strict .atn export of plain events whose target is not the layer pixels (#7564)

NetterSilver92721dd8c4d7
diff --git a/crates/engine/src/atn_cmds.rs b/crates/engine/src/atn_cmds.rs
index a32256920..d5c688f0a 100644
--- a/crates/engine/src/atn_cmds.rs
+++ b/crates/engine/src/atn_cmds.rs
@@ -881,11 +881,18 @@ macro_rules! ev {
Some(m) => step($id, Value::Object(m)),
None => Mapped::Unsupported { harmless: false },
},
- to: |_, p| fields_to(p, $fields, desc("null")),
+ to: |_, p| if pixel_target(p) { fields_to(p, $fields, desc("null")) } else { None },
}
};
}
+/// 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.
+fn pixel_target(p: &Value) -> bool {
+ p.get("target").is_none_or(|t| t.as_str() == Some("pixels"))
+}
+
const INTERPOLATION: &[(&str, &str)] = &[
("Bcbc", "bicubic"),
("Blnr", "bilinear"),
diff --git a/crates/engine/src/atn_cmds/tests.rs b/crates/engine/src/atn_cmds/tests.rs
index 335a3cbea..c4fe00eb9 100644
--- a/crates/engine/src/atn_cmds/tests.rs
+++ b/crates/engine/src/atn_cmds/tests.rs
@@ -1068,3 +1068,24 @@ fn fractional_translation_only_transform_keeps_offset_and_interpolation() {
assert_eq!(steps[0].1.get("dx").and_then(Value::as_f64), Some(0.5));
assert_eq!(steps[0].1.get("interpolation"), Some(&json!("bilinear")));
}
+
+/// A filter recorded against a layer mask must not export as a plain filter that edits the
+/// layer's pixels on replay: Photoshop's descriptor has no field for PhotoCraft's `target`.
+#[test]
+fn add_noise_with_mask_target_is_not_exported_as_pixel_filter() {
+ let masked = Action {
+ name: "Probe".into(),
+ steps: vec![("filter.noise.addNoise".into(), json!({"amount": 75, "seed": 123, "target": "mask"}))],
+ ..Default::default()
+ };
+ assert!(export("Audit", &[&masked], &[], false).is_err());
+ let (_, skipped) = export("Audit", &[&masked], &[], true).unwrap();
+ assert_eq!(skipped, vec![Skipped { action: "Probe".into(), step: 0, id: "filter.noise.addNoise".into() }]);
+ for params in [json!({"amount": 75, "seed": 123}), json!({"amount": 75, "seed": 123, "target": "pixels"})] {
+ let plain = Action { name: "Plain".into(), steps: vec![("filter.noise.addNoise".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![("filter.noise.addNoise".to_string(), json!({"amount": 75, "seed": 123}))]);
+ }
+}