Skip to content

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

Merge PR #8527: fix(atn): export Black & White adjustment layers with Photoshop's BanW class (#8481)

David Mannee85da2ffd69
diff --cc crates/engine/src/atn_cmds.rs
index 7d34ac59a,e9e199e89..3932f3380
--- a/crates/engine/src/atn_cmds.rs
+++ b/crates/engine/src/atn_cmds.rs
@@@ -3354,9 -3113,8 +3363,9 @@@ const EVENTS: &[Event] = &
adjustment_event!("ClrB", "ClrB", "colorBalance", "Color Balance"),
adjustment_event!("Vbrn", "Vbrn", "vibrance", "Vibrance"),
adjustment_event!("Exps", "Exps", "exposure", "Exposure"),
- adjustment_event!("PhtF", "PhtF", "photoFilter", "Photo Filter"),
+ // Photoshop runs Photo Filter by name only: `PhtF` is the class, not an event (#8301).
+ adjustment_event!("", "PhtF", "photoFilter", "Photo Filter"),
- adjustment_event!("", "BlnW", "blackAndWhite", "Black & White", "image.adjustments.blackWhite"),
+ adjustment_event!("", "BanW", "blackAndWhite", "Black & White", "image.adjustments.blackWhite"),
adjustment_event!("ChnM", "ChnM", "channelMixer", "Channel Mixer"),
adjustment_event!("GdMp", "GdMp", "gradientMapEvent", "Gradient Map", "image.adjustments.gradientMap"),
adjustment_event!("SlcC", "SlcC", "selectiveColor", "Selective Color"),
diff --cc crates/engine/src/atn_cmds/tests.rs
index e210b71d6,2844a140f..c633ab471
--- a/crates/engine/src/atn_cmds/tests.rs
+++ b/crates/engine/src/atn_cmds/tests.rs
@@@ -1739,72 -1444,42 +1739,108 @@@ fn events_photoshop_only_runs_by_name_e
}
}
+/// #8301: Photoshop stops on a Photo Filter step saved as `PhtF` ("not currently available")
+/// and runs the same descriptor under the name `photoFilter`. A recorded step saves by name,
+/// a Photoshop-authored step re-saves by name, and actions saved before with `PhtF` still load
+/// and play the same.
+#[test]
+fn photo_filter_saves_under_the_name_photoshop_runs() {
+ let params = json!({"color": "#0022cd", "density": 67, "preserveLuminosity": false});
+ let mut s = Session::new();
+ s.execute("file.new", json!({"width": 8, "height": 8, "background": "#808080"})).unwrap();
+ s.execute("actions.record", json!({"name": "Filter"})).unwrap();
+ s.execute("image.adjustments.photoFilter", params.clone()).unwrap();
+ s.execute("actions.stop", json!({})).unwrap();
+ let saved = s.execute("actions.save", json!({"action": "Filter"})).unwrap();
+ assert_eq!(saved["skipped"], json!([]), "{saved}");
+ let file = atn::parse(&photocraft_paint::tile::b64_decode(saved["data"].as_str().unwrap()).unwrap()).unwrap();
+ let step = &file.actions[0].items[0];
+ assert_eq!(step.event, AtnEvent::Name("photoFilter".into()), "never the code PhtF");
+ assert_eq!(step.dictionary, "Photo Filter".as_bytes());
+ let d = step.descriptor.clone().unwrap();
+ for key in ["Clr ", "Dnst", "PrsL"] {
+ assert!(d.get(key).is_some(), "the descriptor keeps {key}: {d:?}");
+ }
+
+ // A Photoshop-authored step (by name) and an old PhotoCraft one (`PhtF`) both load to the
+ // command, and saving them again writes the name.
+ let data =
+ set_of("S", vec![("Photoshop", vec![item("photoFilter", "Photo Filter", Some(d.clone()))]), ("Legacy", vec![item("PhtF", "Photo Filter", Some(d))])]);
+ let mut t = Session::new();
+ let r = t.execute("actions.load", json!({"data": data})).unwrap();
+ assert_eq!(r["unsupported"], json!([]), "{r}");
+ for a in &t.actions.list {
+ assert_eq!(a.steps.len(), 1, "{}", a.name);
+ assert_eq!(a.steps[0].0, "image.adjustments.photoFilter", "{}", a.name);
+ let mut p = a.steps[0].1.clone();
+ normalise(&mut p);
+ assert_eq!(p, params, "{}", a.name);
+ }
+ let resaved = t.execute("actions.save", json!({"set": "S"})).unwrap();
+ assert_eq!(resaved["skipped"], json!([]), "{resaved}");
+ let file = atn::parse(&photocraft_paint::tile::b64_decode(resaved["data"].as_str().unwrap()).unwrap()).unwrap();
+ let events: Vec<String> = file.actions.iter().flat_map(|a| a.items.iter().map(|i| i.event.id())).collect();
+ assert_eq!(events, ["photoFilter", "photoFilter"]);
+
+ // Both play like the command run directly, at 8 and 16 bits.
+ for depth in [8, 16] {
+ let pixel = |s: &Session| {
+ let st = s.active().unwrap();
+ let surface = st.doc.layers.first().unwrap().surface().unwrap();
+ [0, 1, 2].map(|c| surface.sample_channel(3, 3, c))
+ };
+ let new_doc = |t: &mut Session| {
+ t.execute("file.new", json!({"width": 8, "height": 8, "background": "#808080", "depth": depth})).unwrap();
+ };
+ new_doc(&mut t);
+ t.execute("image.adjustments.photoFilter", params.clone()).unwrap();
+ let direct = pixel(&t);
+ assert_ne!(direct, [128.0 / 255.0; 3], "the filter changes the grey");
+ for action in ["Photoshop", "Legacy"] {
+ new_doc(&mut t);
+ let r = t.execute("actions.play", json!({"action": action})).unwrap();
+ assert!(r.get("failed").is_none(), "{action}: {r}");
+ assert_eq!(pixel(&t), direct, "{action} at {depth} bits");
+ }
+ }
+}
+
+ /// A Black & White adjustment layer saves with Photoshop's `BanW` class (#8481): measured in
+ /// Photoshop, a Make whose type is `BlnW` is refused. Photoshop's own `BanW` Make loads, and so
+ /// does the `BlnW` one PhotoCraft wrote before.
+ #[test]
+ fn black_white_adjustment_layer_saves_as_photoshops_class_and_old_class_still_imports() {
+ let id = "layer.newAdjustmentLayer.blackWhite";
+ let params = json!({"reds": 17, "yellows": 83, "greens": -12, "cyans": 107, "blues": 51, "magentas": 133, "tint": true, "tintColor": "#c08040"});
+ let mut s = Session::new();
+ s.actions.list.push(Action::new("BW", vec![(id.into(), params.clone())]));
+ let r = s.execute("actions.save", json!({"action": "BW"})).unwrap();
+ assert_eq!(r["skipped"], json!([]));
+ let saved = photocraft_paint::tile::b64_decode(r["data"].as_str().unwrap()).unwrap();
+ let file = atn::parse(&saved).unwrap();
+ let make = file.actions[0].items[0].descriptor.as_ref().unwrap();
+ let kind = get_obj(make, "Usng").and_then(|u| get_obj(u, "Type")).unwrap();
+ assert_eq!(trimmed(&kind.class_id), "BanW");
+ let mut t = Session::new();
+ let r = t.execute("actions.load", json!({"data": b64(&saved)})).unwrap();
+ assert_eq!(r["unsupported"], json!([]), "{r}");
+ assert_eq!(t.actions.list[0].steps, vec![(id.to_string(), params)]);
+
+ // Photoshop's minimal Make (no settings) and an old PhotoCraft Make with a setting both load,
+ // the setting kept; near misses stay Photoshop steps.
+ let make = |class: &str| {
+ let kind = if class == "BlnW" { desc(class).with("Rd ", long(17.0)) } else { desc(class) };
+ let using = desc("AdjL").with("Type", D::Descriptor(kind));
+ item("Mk ", "Make", Some(desc("null").with("null", class_ref("AdjL")).with("Usng", D::Descriptor(using))))
+ };
+ let data = set_of("S", vec![("A", ["BanW", "BlnW", "BANW", "BlnWx"].into_iter().map(make).collect())]);
+ let mut t = Session::new();
+ let r = t.execute("actions.load", json!({"data": data})).unwrap();
+ let steps: Vec<&str> = t.actions.list[0].steps.iter().map(|(id, _)| id.as_str()).collect();
+ assert_eq!(steps, [id, id, PHOTOSHOP_STEP, PHOTOSHOP_STEP], "{r}");
+ assert_eq!(t.actions.list[0].steps[1].1["reds"], 17, "{r}");
+ }
+
/// 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]