PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Merge remote-tracking branch 'origin/main' into fix/atn-new-layer-options
Finn Newick4cc24567f1ae
diff --cc crates/engine/src/atn_cmds/tests.rsindex 41c2a5f6e,f50189fb0..f5bb632c0--- a/crates/engine/src/atn_cmds/tests.rs+++ b/crates/engine/src/atn_cmds/tests.rs@@@ -1033,77 -1037,132 +1037,203 @@@ fn remove_white_clears_the_targeted_layassert!(bottom.surface().unwrap().sample_channel(2, 2, 1) > 0.99, "and kept its pixels");}+/// #7502, #7512, #7513, #7514: a New Layer step that clips to the layer below, has a colour+/// label, fills with the neutral colour or goes below the active layer isn't exported, since the+/// descriptor carries only its name, opacity and mode. Those options at their defaults still+/// export, and the step round-trips.+#[test]+fn new_layer_options_the_descriptor_cannot_carry_refuse_strict_export() {+ let new_layer = |p: Value| Action { name: "Probe".into(), steps: vec![("layer.new.layer".into(), p)], ..Default::default() };+ for p in [+ json!({"name": "New", "clipToPrevious": true}),+ json!({"name": "New", "color": "red"}),+ json!({"name": "New", "mode": "Overlay", "fillNeutral": true}),+ json!({"name": "New", "position": "below"}),+ json!({"name": "New", "clipToPrevious": 1}),+ ] {+ assert!(export("Audit7", &[&new_layer(p.clone())], &[], false).is_err(), "{p}");+ let (_, skipped) = export("Audit7", &[&new_layer(p.clone())], &[], true).unwrap();+ assert_eq!(skipped, vec![Skipped { action: "Probe".into(), step: 0, id: "layer.new.layer".into() }], "{p}");+ }+ let defaults =+ json!({"name": "New", "mode": "Multiply", "opacity": 0.6, "clipToPrevious": false, "color": "none", "fillNeutral": false, "position": "above"});+ let (bytes, skipped) = export("Audit7", &[&new_layer(defaults)], &[], false).unwrap();+ assert!(skipped.is_empty());+ let imported = import(&bytes).unwrap();+ assert_eq!(imported.actions[0].steps, vec![("layer.new.layer".to_string(), json!({"name": "New", "mode": "Multiply", "opacity": 0.6}))]);+}++/// Options the exported descriptors can't carry refuse strict export (and a partial export skips+/// the step) instead of exporting a step that replays differently; the forms they can carry still+/// export (#7196, #7236, #7246, #7287, #7503, #7506, #7507).+#[test]+fn options_the_descriptors_cannot_carry_refuse_strict_export() {+ let one = |id: &str, p: Value| Action { name: "Probe".into(), steps: vec![(id.into(), p)], ..Default::default() };+ let refused = [+ ("layer.groupLayers", json!({"layer": 3, "name": "Group"})),+ ("layer.newFillLayer.solidColor", json!({"color": "#c0603080"})),+ ("layer.newFillLayer.solidColor", json!({"color": "teal"})),+ ("file.new", json!({"width": 24, "height": 18, "background": "black"})),+ ("file.new", json!({"width": 24, "height": 18, "background": "#ab2233"})),+ ("file.new", json!({"width": 24, "height": 18, "background": "backgroundColor", "backgroundColor": [0.2, 0.4, 0.6]})),+ ("select.loadSelection", json!({"channel": "Alpha 1", "invert": true})),+ ("select.loadSelection", json!({"channel": "Alpha 1", "operation": "subtract"})),+ ("layer.setProps", json!({"opacity": 0.5, "locked": true})),+ ("layer.setProps", json!({"opacity": 0.5, "clipped": true})),+ ("layer.setProps", json!({"layers": [2, 3], "opacity": 0.5})),+ ("filter.noise.addNoise", json!({"amount": 30, "seed": 2147483648_u64})),+ ("filter.noise.addNoise", json!({"amount": 30, "seed": 4294967295_u64})),+ ];+ for (id, p) in refused {+ assert!(export("Audit7", &[&one(id, p.clone())], &[], false).is_err(), "{id} {p}");+ let (_, skipped) = export("Audit7", &[&one(id, p.clone())], &[], true).unwrap();+ assert_eq!(skipped.len(), 1, "{id} {p}");+ }+ let carried = [+ ("layer.groupLayers", json!({"name": "Group"})),+ ("layer.newFillLayer.solidColor", json!({"color": "#c06030"})),+ ("file.new", json!({"width": 24, "height": 18, "background": "white"})),+ ("file.new", json!({"width": 24, "height": 18, "background": "transparent"})),+ ("file.new", json!({"width": 24, "height": 18, "background": "backgroundColor"})),+ ("select.loadSelection", json!({"channel": "Alpha 1", "invert": false, "operation": "new"})),+ // A named target (a numeric one is #7248, #7262).+ ("layer.setProps", json!({"layer": "Pixels", "opacity": 0.5, "visible": true})),+ ("filter.noise.addNoise", json!({"amount": 30, "seed": 2147483647})),+ ];+ for (id, p) in carried {+ let (bytes, skipped) = export("Audit7", &[&one(id, p.clone())], &[], false).unwrap_or_else(|e| panic!("{id} {p}: {e}"));+ assert!(skipped.is_empty(), "{id} {p}");+ let imported = import(&bytes).unwrap();+ assert_eq!(imported.actions[0].steps.first().map(|(i, _)| i.as_str()), Some(id), "{p}");+ }+}++ /// 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]+ fn smart_blur_quality_uses_photoshop_codes() {+ let smart_blur =+ |quality: D, mode: &str| desc("null").with("Rds ", D::Double(6.0)).with("Thsh", D::Double(60.0)).with("SmBQ", quality).with("SmBM", enm("SmBM", mode));+ // Every quality writes Photoshop's code, as a double radius/threshold and both enums,+ // and reads back as the same step.+ for (quality, code) in [("low", "SBQL"), ("medium", "SBQM"), ("high", "SBQH")] {+ let params = json!({"radius": 6, "threshold": 60, "quality": quality, "mode": "edgeOnly"});+ let a = Action::new("Q", vec![("filter.blur.smartBlur".into(), params.clone())]);+ let (bytes, skipped) = export("S", &[&a], &[], false).unwrap();+ assert!(skipped.is_empty());+ let file = atn::parse(&bytes).unwrap();+ let it = &file.actions[0].items[0];+ assert_eq!(it.event.id(), "SmrB");+ assert_eq!(it.descriptor.as_ref(), Some(&smart_blur(enm("SmBQ", code), "SBME")), "{quality}");+ let back = import(&bytes).unwrap();+ assert!(back.unsupported.is_empty(), "{quality}");+ assert_eq!(back.actions[0].steps, vec![("filter.blur.smartBlur".to_string(), params)]);+ }++ // Photoshop-shaped steps (each quality and mode, as Photoshop stores them) and the codes+ // PhotoCraft wrote before this fix read as the right quality.+ let cases = [+ ("SBQH", "SBMN", "high", "normal"),+ ("SBQM", "SBMO", "medium", "overlayEdge"),+ ("SBQL", "SBME", "low", "edgeOnly"),+ ("Hgh ", "SBMN", "high", "normal"),+ ("Mdm ", "SBMN", "medium", "normal"),+ ("Low ", "SBMN", "low", "normal"),+ ];+ let items = cases.iter().map(|(q, m, _, _)| item("SmrB", "Smart Blur", Some(smart_blur(enm("SmBQ", q), m)))).collect();+ let data = set_of("Blurs", vec![("Smart", items)]);+ let mut s = Session::new();+ s.execute("actions.load", json!({"data": data})).unwrap();+ let steps = &s.actions.list[0].steps;+ for ((q, _, quality, mode), (id, p)) in cases.iter().zip(steps) {+ assert_eq!(id, "filter.blur.smartBlur", "{q}");+ assert_eq!(p, &json!({"radius": 6, "threshold": 60, "quality": quality, "mode": mode}), "{q}");+ }++ // An unknown code, a quality of the wrong type, NaN and a missing quality never panic: the+ // unknown ones stay Photoshop steps, kept verbatim.+ let odd = [+ smart_blur(enm("SmBQ", "Xxxx"), "SBMN"),+ smart_blur(D::Integer(2), "SBMN"),+ smart_blur(enm("SmBQ", "SBQH"), "Xxxx"),+ desc("null").with("Rds ", D::Double(f64::NAN)).with("SmBQ", enm("SmBQ", "SBQM")),+ desc("null"),+ ];+ let items: Vec<AtnItem> = odd.iter().map(|d| item("SmrB", "Smart Blur", Some(d.clone()))).collect();+ let imported = import(&photocraft_paint::tile::b64_decode(&set_of("Odd", vec![("Odd", items)])).unwrap()).unwrap();+ let steps = &imported.actions[0].steps;+ assert_eq!(steps.len(), odd.len());+ assert_eq!(steps[0].0, PHOTOSHOP_STEP, "an unknown quality is kept verbatim");+ assert_eq!(steps[2].0, PHOTOSHOP_STEP, "an unknown mode is kept verbatim");+ assert_eq!(imported.unsupported.iter().map(|u| u.step).collect::<Vec<_>>(), [0, 2]);+ // A wrong-type quality is left out (the filter's default applies), as for every enum field.+ assert_eq!(steps[1], ("filter.blur.smartBlur".to_string(), json!({"radius": 6, "threshold": 60, "mode": "normal"})));+ // An unknown quality or a non-string one is not exported.+ for bad in [json!({"quality": "best"}), json!({"quality": 3}), json!({"radius": "x"})] {+ let a = Action::new("Bad", vec![("filter.blur.smartBlur".into(), bad.clone())]);+ assert!(export("S", &[&a], &[], false).is_err(), "{bad}");+ }+ }++ /// #5830: Shadows/Highlights (`adaptCorrect`) recorded by Photoshop, with string IDs, plays as+ /// Image › Adjustments › Shadows/Highlights instead of stopping as a step with no equivalent.+ #[test]+ fn photoshop_shadows_highlights_steps_play() {+ let tones = |amount: f64, width: f64, radius: i32| {+ D::Descriptor(desc("adaptCorrectTones").with("amount", pct(amount)).with("width", pct(width)).with("radius", D::Integer(radius)))+ };+ let d = desc("null")+ .with("shadowMode", tones(35.0, 50.0, 30))+ .with("highlightMode", tones(20.0, 40.0, 24))+ .with("blackClip", D::Double(0.01))+ .with("whiteClip", D::Double(0.02))+ .with("center", D::Integer(5))+ .with("colorCorrection", D::Integer(20));+ let e = event_for(&item("adaptCorrect", "Sombra/Realce", None).event).expect("adaptCorrect is mapped");+ let Mapped::Steps(steps) = (e.from)(&d) else { panic!("Shadows/Highlights maps to a step") };+ assert_eq!(steps.len(), 1);+ let (id, p) = &steps[0];+ assert_eq!(id, "image.adjustments.shadowsHighlights");+ let mut p = p.clone();+ normalise(&mut p);+ assert_eq!(+ p,+ json!({"shadowAmount": 35, "shadowTone": 50, "shadowRadius": 30, "highlightAmount": 20, "highlightTone": 40, "highlightRadius": 24, "blackClip": 0.01, "whiteClip": 0.02, "midtone": 5, "color": 20})+ );+ // The same settings as char IDs read the same; absent keys are left to the defaults.+ let codes = desc("null")+ .with("sdwM", D::Descriptor(desc("adaptCorrectTones").with("Amnt", pct(35.0)).with("Wdth", pct(50.0)).with("Rds ", D::Integer(30))))+ .with("BlcC", D::Double(0.01));+ let Mapped::Steps(steps) = (e.from)(&codes) else { panic!("char IDs map too") };+ let mut q = steps[0].1.clone();+ normalise(&mut q);+ assert_eq!(q, json!({"shadowAmount": 35, "shadowTone": 50, "shadowRadius": 30, "blackClip": 0.01}));+ // And it runs on a document.+ let mut s = Session::new();+ s.execute("file.new", json!({"width": 16, "height": 16, "background": "#404040"})).unwrap();+ s.execute(id, p).unwrap();+ }++ #[test]+ fn save_while_recording_includes_pending_steps() {+ let mut s = Session::new();+ s.execute("file.new", json!({"width": 8, "height": 8})).unwrap();+ s.execute("actions.record", json!({"name": "Pending"})).unwrap();+ s.execute("layer.new.layer", json!({"name": "Recorded"})).unwrap();++ // While still recording (without calling actions.stop), save the action.+ let saved = s.execute("actions.save", json!({"action": "Pending"})).unwrap();+ assert_eq!(saved["steps"], 1, "pending steps must be flushed and included in save");+ assert_eq!(saved["skipped"], json!([]));++ // After save, recording continues and additional steps are flushed on stop without duplication.+ s.execute("layer.new.layer", json!({"name": "Second"})).unwrap();+ s.execute("actions.stop", json!({})).unwrap();++ let action = s.execute("actions.get", json!({"action": "Pending"})).unwrap();+ assert_eq!(action["steps"].as_array().unwrap().len(), 2, "both steps must be present without duplication");+ }+/// Photoshop's Close has no document target: a Close of a specific document must not be/// exported as a plain Close, which would close the active document on playback.#[test]