PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Merge PR #8324: fix(actions): Hue/Saturation uses the reference app's hue key H in .atn files (#8302, #8308)
aiongg182116cb6d43
diff --cc crates/engine/src/atn_cmds/tests.rsindex d20d7acad,a5d2229dc..4b05e3d4a--- a/crates/engine/src/atn_cmds/tests.rs+++ b/crates/engine/src/atn_cmds/tests.rs@@@ -1811,95 -1402,45 +1811,138 @@@ fn function_keys_keep_shift_and_commandassert_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"}));+}++/// A crop with W x H x Resolution output options must not export as a plain rectangle crop+/// that keeps the old size and resolution on replay: the mapper has no field for them.+#[test]+fn crop_with_output_size_or_resolution_is_not_exported_as_plain_crop() {+ for params in [+ json!({"x": 1, "y": 1, "width": 4, "height": 3, "targetWidth": 8, "targetHeight": 6, "resolution": 144, "resample": "nearest"}),+ json!({"x": 1, "y": 1, "width": 4, "height": 3, "resolution": 144}),+ json!({"x": 1, "y": 1, "width": 4, "height": 3, "targetWidth": 8, "targetHeight": 6}),+ ] {+ let sized = Action { name: "Probe".into(), steps: vec![("image.crop".into(), params)], ..Default::default() };+ assert!(export("Audit", &[&sized], &[], false).is_err());+ let (_, skipped) = export("Audit", &[&sized], &[], true).unwrap();+ assert_eq!(skipped, vec![Skipped { action: "Probe".into(), step: 0, id: "image.crop".into() }]);+ }+ let plain = Action { name: "Plain".into(), steps: vec![("image.crop".into(), json!({"x": 1, "y": 1, "width": 4, "height": 3}))], ..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![("image.crop".to_string(), json!({"x": 1, "y": 1, "width": 4, "height": 3, "deleteCroppedPixels": true}))]);+}++/// `image.imageSize` with `resample: "none"` ignores width, height and percent; the `.atn`+/// round trip must not turn them into a pixel resize (#7189).+#[test]+fn image_size_resample_none_keeps_ignoring_its_size_through_atn_export() {+ let event = event_for(&export_item("image.imageSize", &json!({"resolution": 72}), "S").expect("exports").event).expect("a known event");+ let back = |p: Value| {+ let item = export_item("image.imageSize", &p, "S")?;+ let Mapped::Steps(steps) = (event.from)(item.descriptor.as_ref().unwrap()) else { panic!("does not import back") };+ let mut back = steps[0].1.clone();+ normalise(&mut back);+ Some(back)+ };+ for sized in [json!({"width": 12, "height": 10}), json!({"width": 12}), json!({"percent": 200}), json!({"width": 12, "percent": 50, "height": 3})] {+ let mut p = sized.clone();+ p["resolution"] = json!(144);+ p["resample"] = json!("none");+ assert_eq!(back(p.clone()), Some(json!({"resolution": 144, "resample": "none"})), "{p}");+ }+ // A step with nothing left to record is refused rather than exported as an empty (unsupported) event.+ assert_eq!(back(json!({"width": 12, "height": 10, "resample": "none"})), None);+ // Real resizes are untouched.+ assert_eq!(back(json!({"width": 12, "height": 10, "resample": "bicubic"})), Some(json!({"width": 12, "height": 10, "resample": "bicubic"})));+}+++ /// #8302, #8308: Hue/Saturation's hue is `H ` in Photoshop's `Hst2`, both ways. It was written+ /// and read as `Hue `: exported actions failed in Photoshop, and Photoshop-recorded ones lost+ /// their hue on import with no warning. PhotoCraft's own older files (`Hue `) still read.+ #[test]+ fn hue_saturation_uses_photoshops_hue_key() {+ let params = json!({"colorize": false, "hue": 30, "saturation": 20, "lightness": -7, "greens": {"hue": -12, "saturation": 0, "lightness": 0, "range": [75, 105, 135, 165]}});+ let a = Action::new("H", vec![("image.adjustments.hueSaturation".into(), params.clone())]);+ let (bytes, skipped) = export("S", &[&a], &[], false).unwrap();+ assert!(skipped.is_empty());+ let file = atn::parse(&bytes).unwrap();+ let d = file.actions[0].items[0].descriptor.clone().expect("a descriptor");+ let entries: Vec<Descriptor> =+ get_list(&d, "Adjs").expect("Adjs").iter().filter_map(|e| if let D::Descriptor(e) = e { Some(e.clone()) } else { None }).collect();+ assert_eq!(entries.len(), 2);+ assert_eq!(get_num(&entries[0], "H "), Some(30.0), "the master hue under H");+ assert_eq!(get_num(&entries[1], "H "), Some(-12.0), "a range's hue under H");+ assert!(entries.iter().all(|e| get_num(e, "Hue ").is_none()), "no Hue key");++ // A Photoshop-recorded step (H) and an older PhotoCraft one (Hue ) both import with the hue.+ let step = |key: &str| {+ desc("null")+ .with("presetKind", enm("presetKindType", "presetKindCustom"))+ .with("Clrz", D::Boolean(false))+ .with("Adjs", D::List(vec![D::Descriptor(desc("Hst2").with(key, long(30.0)).with("Strt", long(20.0)).with("Lght", long(-7.0)))]))+ };+ for key in ["H ", "Hue "] {+ let e = event_for(&item("HStr", "", None).event).expect("HStr imports");+ match (e.from)(&step(key)) {+ Mapped::Steps(steps) => {+ assert_eq!(steps[0].0, "image.adjustments.hueSaturation");+ assert_eq!(steps[0].1["hue"], json!(30.0), "{key:?}: {}", steps[0].1);+ assert_eq!(steps[0].1["saturation"], json!(20.0));+ }+ Mapped::Unsupported { .. } => panic!("{key:?} is unsupported"),+ }+ }+ // Through a whole file, saved and loaded back: the hue survives.+ let back = import(&bytes).unwrap();+ let mut p = back.actions[0].steps[0].1.clone();+ normalise(&mut p);+ assert_eq!(p["hue"], json!(30), "{p}");+ }