PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Actions: Photoshop Shadows/Highlights steps play (#5830) (#5854)
Michael Higginsadbd40ef5503
diff --git a/crates/engine/src/atn_cmds.rs b/crates/engine/src/atn_cmds.rsindex 41badd86f..999ba5c19 100644--- a/crates/engine/src/atn_cmds.rs+++ b/crates/engine/src/atn_cmds.rs@@ -2372,6 +2372,52 @@ fn image_size_from(d: &Descriptor) -> Mapped {step("image.imageSize", Value::Object(p))}+/// Shadows/Highlights (`adaptCorrect`, #5830): the shadow and highlight tones are `shadowMode`+/// and `highlightMode` objects (amount and width in %, radius in px), then the black and white+/// clips (%), midtone contrast (`center`) and colour correction. Each key is read as its char ID+/// or its string ID, whichever the file used; absent keys keep the command's defaults.+const SHADOWS_HIGHLIGHTS_TONES: [(&str, &str, &str); 2] = [("sdwM", "shadowMode", "shadow"), ("hglM", "highlightMode", "highlight")];+const SHADOWS_HIGHLIGHTS_TONE: [(&str, &str, &str); 3] = [("Amnt", "amount", "Amount"), ("Wdth", "width", "Tone"), ("Rds ", "radius", "Radius")];+const SHADOWS_HIGHLIGHTS: [(&str, &str, &str); 4] =+ [("BlcC", "blackClip", "blackClip"), ("WhtC", "whiteClip", "whiteClip"), ("Cntr", "center", "midtone"), ("ClrC", "colorCorrection", "color")];++fn shadows_highlights_from(d: &Descriptor) -> Mapped {+ let mut m = Map::new();+ for (code, name, prefix) in SHADOWS_HIGHLIGHTS_TONES {+ let Some(t) = get_obj(d, code).or_else(|| get_obj(d, name)) else { continue };+ for (code, name, suffix) in SHADOWS_HIGHLIGHTS_TONE {+ if let Some(v) = get_num(t, code).or_else(|| get_num(t, name)) {+ m.insert(format!("{prefix}{suffix}"), Value::from(v));+ }+ }+ }+ for (code, name, param) in SHADOWS_HIGHLIGHTS {+ if let Some(v) = get_num(d, code).or_else(|| get_num(d, name)) {+ m.insert(param.into(), Value::from(v));+ }+ }+ step("image.adjustments.shadowsHighlights", Value::Object(m))+}++fn shadows_highlights_to(_: &str, p: &Value) -> Option<Descriptor> {+ let mut d = desc("null");+ for (_, name, prefix) in SHADOWS_HIGHLIGHTS_TONES {+ let mut t = desc("adaptCorrectTones");+ for (_, key, suffix) in SHADOWS_HIGHLIGHTS_TONE {+ if let Some(v) = jnum(p, &format!("{prefix}{suffix}")) {+ t = t.with(key, if suffix == "Radius" { long(v) } else { pct(v) });+ }+ }+ d = d.with(name, D::Descriptor(t));+ }+ for (_, key, param) in SHADOWS_HIGHLIGHTS {+ if let Some(v) = jnum(p, param) {+ d = d.with(key, if matches!(param, "midtone" | "color") { long(v) } else { D::Double(v) });+ }+ }+ Some(d)+}+fn image_size_to(_: &str, p: &Value) -> Option<Descriptor> {let mut d = desc("null");if let Some(pc) = jnum(p, "percent") {@@ -2661,6 +2707,14 @@ const EVENTS: &[Event] = &[ev!("Crst", "crystallize", "Crystallize", "filter.pixelate.crystallize", &[F("ClSz", "cellSize", Kind::Long)]),Event { code: "GEfc", name: "filterGallery", label: "Filter Gallery", ids: GALLERY_IDS, from: gallery_from, to: gallery_to },// Adjustments (Image › Adjustments).+ Event {+ code: "",+ name: "adaptCorrect",+ label: "Shadows/Highlights",+ ids: &["image.adjustments.shadowsHighlights"],+ from: shadows_highlights_from,+ to: shadows_highlights_to,+ },Event {code: "Lvls",name: "levels",diff --git a/crates/engine/src/atn_cmds/tests.rs b/crates/engine/src/atn_cmds/tests.rsindex 098dc9806..59dbbf13c 100644--- a/crates/engine/src/atn_cmds/tests.rs+++ b/crates/engine/src/atn_cmds/tests.rs@@ -186,6 +186,10 @@ fn every_mapped_command_exports_and_imports_back() {json!({"colorize": false, "hue": 10, "saturation": -20, "lightness": 0, "reds": {"hue": 5, "saturation": 0, "lightness": 0, "range": [315, 345, 15, 45]}}),),("image.adjustments.brightnessContrast", json!({"brightness": 10, "contrast": -5, "legacy": false})),+ (+ "image.adjustments.shadowsHighlights",+ json!({"shadowAmount": 60, "shadowTone": 40, "shadowRadius": 25, "highlightAmount": 10, "highlightTone": 50, "highlightRadius": 30, "blackClip": 0.02, "whiteClip": 0.05, "midtone": 15, "color": 20}),+ ),("image.adjustments.threshold", json!({"level": 100})),("image.adjustments.posterize", json!({"levels": 4})),("image.adjustments.invert", json!({})),@@ -1033,6 +1037,45 @@ fn remove_white_clears_the_targeted_layer_not_the_converted_background() {assert!(bottom.surface().unwrap().sample_channel(2, 2, 1) > 0.99, "and kept its pixels");}+/// #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();