From da1b2e0070088dff289c0b4283dae4623a9143a2 Mon Sep 17 00:00:00 2001 From: marshiyar <57055687+marshiyar@users.noreply.github.com> Date: Fri, 30 Oct 2026 20:12:26 -0700 Subject: [PATCH] fix(engine): refuse strict .atn export of a partial Levels edit (#8530) layer.setAdjustment changes only the settings it names, but the Levels descriptor writes each range whole and fills the end an edit did not name with 0 or 255. A recorded {"kind":"levels","inBlack":31} was exported as Inpt [31, 255] with nothing skipped, so replaying it also reset a retained Input White. This is the Levels half of #8701 (Color Balance and Hue/Saturation, #8531). levels_complete accepts a Levels edit only when, for the master curve and every channel object, inBlack/inWhite are both given or both absent, and likewise outBlack/outWhite. set_to uses it as the levels arm of the existing completeness check, so strict export refuses the step and a non-strict export lists it as skipped. Test partial_levels_edits_refuse_strict_export fails without the fix (the export succeeds with Inpt [31, 255]) and passes with it. Fixes #8530 --- crates/engine/src/cmd/atn_cmds/adjustments.rs | 10 +++++ crates/engine/src/cmd/atn_cmds/steps_make.rs | 7 +++- crates/engine/src/cmd/atn_cmds/tests.rs | 41 +++++++++++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/cmd/atn_cmds/adjustments.rs b/crates/engine/src/cmd/atn_cmds/adjustments.rs index 5da802b800a..44439bb3ec4 100644 --- a/crates/engine/src/cmd/atn_cmds/adjustments.rs +++ b/crates/engine/src/cmd/atn_cmds/adjustments.rs @@ -66,6 +66,16 @@ fn levels_entry(chan: Option<&str>, p: &Value) -> Descriptor { e } +/// Whether the Levels settings in `p` (the master curve and every channel object) name both ends +/// of each range they touch, or neither. `levels_entry` writes a whole `Inpt` or `Otpt` pair and +/// fills the end a partial edit left out with 0 or 255, so replaying `inBlack: 31` alone would +/// also reset a retained Input White (#8530). +pub(super) fn levels_complete(p: &Value) -> bool { + let pairs_whole = + |v: &Value| [("inBlack", "inWhite"), ("outBlack", "outWhite")].iter().all(|(low, high)| jnum(v, low).is_some() == jnum(v, high).is_some()); + pairs_whole(p) && CHANNELS.iter().filter_map(|(_, k)| p.get(*k).filter(|v| v.is_object())).all(pairs_whole) +} + pub(super) fn levels_to(p: &Value) -> Option { let mut entries = vec![D::Descriptor(levels_entry(None, p))]; for (_, k) in CHANNELS { diff --git a/crates/engine/src/cmd/atn_cmds/steps_make.rs b/crates/engine/src/cmd/atn_cmds/steps_make.rs index 2fce72c36d4..c7617053fc8 100644 --- a/crates/engine/src/cmd/atn_cmds/steps_make.rs +++ b/crates/engine/src/cmd/atn_cmds/steps_make.rs @@ -1,4 +1,4 @@ -use super::adjustments::{HUE_RANGES, adjustment_by_class, adjustment_by_suffix, adjustment_steps}; +use super::adjustments::{HUE_RANGES, adjustment_by_class, adjustment_by_suffix, adjustment_steps, levels_complete}; use super::descriptors::{ PX, Target, ang, class, class_ref, code, color_desc, color_hex, desc, enm, get_bool, get_enum, get_list, get_num, get_obj, get_ref, get_text, jbool, jnum, jstr, layer_ref, layer_target, long, name_ref, pct, px, ref_class, target, target_ref, text, trimmed, @@ -762,7 +762,8 @@ pub(super) fn set_to(id: &str, p: &Value) -> Option { let kind = jstr(p, "kind")?; // `layer.setAdjustment` changes only the settings it names. Color Balance and // Hue/Saturation descriptors carry every setting, filling absent ones with defaults, - // so a partial edit would replay as one that also resets the rest (#8531). + // so a partial edit would replay as one that also resets the rest (#8531). Levels writes + // each range whole, so one end alone resets the other (#8530). // Present and of the shape the writer reads: anything else it would write as 0. let number = |p: &Value, k: &str| p.get(k).and_then(Value::as_f64).is_some_and(f64::is_finite); let numbers = |p: &Value, keys: &[&str]| keys.iter().all(|k| number(p, k)); @@ -784,6 +785,8 @@ pub(super) fn set_to(id: &str, p: &Value) -> Option { r.is_object() && numbers(r, &["hue", "saturation", "lightness"]) && bounds }) } + // Both ends of each range it names (#8530). + "levels" => levels_complete(p), _ => true, }; if !complete { diff --git a/crates/engine/src/cmd/atn_cmds/tests.rs b/crates/engine/src/cmd/atn_cmds/tests.rs index 011e9aa5e51..8a41ea0347b 100644 --- a/crates/engine/src/cmd/atn_cmds/tests.rs +++ b/crates/engine/src/cmd/atn_cmds/tests.rs @@ -2278,6 +2278,47 @@ fn partial_color_balance_and_hue_saturation_edits_refuse_strict_export() { } } +/// Levels, like Color Balance and Hue/Saturation: the descriptor writes both ends of the Input and +/// of the Output range, filling the end an edit did not name with 0 or 255, so replaying +/// `inBlack: 31` alone would also reset a retained Input White (#8530). Strict export refuses it; +/// a pair given whole, or none of it, still exports and round-trips. +#[test] +fn partial_levels_edits_refuse_strict_export() { + let one = |p: Value| Action { name: "Probe".into(), steps: vec![("layer.setAdjustment".into(), p)], ..Default::default() }; + for p in [ + json!({"kind": "levels", "inBlack": 31}), + json!({"kind": "levels", "inWhite": 239}), + json!({"kind": "levels", "outBlack": 10}), + json!({"kind": "levels", "outWhite": 240}), + json!({"kind": "levels", "inBlack": 31, "inWhite": 239, "outWhite": 240}), + json!({"kind": "levels", "red": {"inBlack": 2}}), + json!({"kind": "levels", "inBlack": 1, "inWhite": 250, "blue": {"outBlack": 3}}), + // A value of the wrong shape is no value: the other end is then alone. + json!({"kind": "levels", "inBlack": 31, "inWhite": "239"}), + ] { + let err = export("Audit8", &[&one(p.clone())], &[], false).unwrap_err(); + assert!(err.contains("`layer.setAdjustment`"), "{p}: {err}"); + let (_, skipped) = export("Audit8", &[&one(p.clone())], &[], true).unwrap(); + assert_eq!(skipped, vec![Skipped { action: "Probe".into(), step: 0, id: "layer.setAdjustment".into() }], "{p}"); + } + for p in [ + json!({"kind": "levels", "inBlack": 31, "inWhite": 239}), + json!({"kind": "levels", "outBlack": 10, "outWhite": 240}), + json!({"kind": "levels", "inBlack": 31, "inWhite": 239, "outBlack": 10, "outWhite": 240, "gamma": 1.2}), + json!({"kind": "levels", "gamma": 0.8}), + json!({"kind": "levels", "inBlack": 1, "inWhite": 250, "red": {"inBlack": 2, "inWhite": 200}}), + ] { + let (bytes, skipped) = export("Audit8", &[&one(p.clone())], &[], false).unwrap_or_else(|e| panic!("{p}: {e}")); + assert!(skipped.is_empty(), "{p}"); + let steps = import(&bytes).unwrap().actions[0].steps.clone(); + let (id, back) = steps.first().unwrap(); + assert_eq!(id, "layer.setAdjustment"); + for k in ["inBlack", "inWhite", "outBlack", "outWhite"] { + assert_eq!(back.get(k), p.get(k), "{p}: {k} in {back}"); + } + } +} + /// 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]