PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Merge remote-tracking branch 'origin/main' into fix/atn-modify-bucket
Finn Newick9ea964fa5116
diff --cc crates/engine/src/atn_cmds/tests.rsindex c5624551c,f50189fb0..d1d2ddf77--- a/crates/engine/src/atn_cmds/tests.rs+++ b/crates/engine/src/atn_cmds/tests.rs@@@ -1033,33 -1037,132 +1037,159 @@@ fn remove_white_clears_the_targeted_layassert!(bottom.surface().unwrap().sample_channel(2, 2, 1) > 0.99, "and kept its pixels");}+/// #7540-#7542: Feather, Contract and Smooth (and Expand) with Apply Effect at Canvas Bounds, and+/// #7543-#7546: a pattern Paint Bucket with a chosen pattern, scale, angle or phase, can't be+/// written, so strict export refuses them. Their defaults still export and round-trip.+#[test]+fn canvas_bound_modify_and_placed_pattern_bucket_refuse_strict_export() {+ let one = |id: &str, p: Value| Action { name: "Probe".into(), steps: vec![(id.into(), p)], ..Default::default() };+ for id in ["select.modify.feather", "select.modify.expand", "select.modify.contract", "select.modify.smooth"] {+ assert!(export("Audit7", &[&one(id, json!({"radius": 2, "applyAtCanvasBounds": true}))], &[], false).is_err(), "{id}");+ let plain = json!({"radius": 2, "applyAtCanvasBounds": false});+ let (bytes, _) = export("Audit7", &[&one(id, plain)], &[], false).unwrap_or_else(|e| panic!("{id}: {e}"));+ assert_eq!(import(&bytes).unwrap().actions[0].steps, vec![(id.to_string(), json!({"radius": 2}))]);+ }+ let bucket = |extra: Value| {+ let mut p = json!({"x": 5, "y": 5, "contents": "pattern", "opacity": 70, "tolerance": 80});+ if let (Some(p), Some(e)) = (p.as_object_mut(), extra.as_object()) {+ p.extend(e.clone());+ }+ one("paint.bucket", p)+ };+ for extra in [json!({"pattern": "AuditPattern"}), json!({"scale": 50}), json!({"angle": 30}), json!({"phase": [3, 5]})] {+ assert!(export("Audit7", &[&bucket(extra.clone())], &[], false).is_err(), "{extra}");+ }+ for extra in [json!({}), json!({"scale": 100, "angle": 0, "phase": [0, 0]})] {+ assert!(export("Audit7", &[&bucket(extra.clone())], &[], false).is_ok(), "{extra}");+ }+}++ /// 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]