From 5e1884e8b90f4dce554bd4101cf810a06d4a3d44 Mon Sep 17 00:00:00 2001 From: kubuntugamer Date: Sun, 29 Nov 2026 21:28:30 -0600 Subject: [PATCH] fix(paint): refuse the Background Eraser on any partial channel target (#18096) The channel check refused a single colour channel of an RGB document and let two of the three through, erasing them. The reference application refuses any target that does not cover the composite, so compare the targeted set's size with the document's colour channels instead of testing for exactly one. Existing tests pinned the old, unmeasured two-of-three behaviour: the partial-set matrix now covers CMYK and Lab only, the transparency-lock preview test targets all three channels, and the multi-channel integration tests check the refusal on the Background and resolve a named layer on the composite. --- .../background_eraser_channel_target_tests.rs | 11 ++- ...ckground_eraser_transparency_lock_tests.rs | 3 +- ...kground_eraser_two_channel_target_tests.rs | 80 +++++++++++++++++++ crates/engine-paint/src/cmd/eraser_cmds.rs | 9 ++- .../engine/tests/it/multi_channel_editing.rs | 37 ++++++--- 5 files changed, 121 insertions(+), 19 deletions(-) create mode 100644 crates/engine-paint/src/cmd/background_eraser_two_channel_target_tests.rs diff --git a/crates/engine-paint/src/cmd/background_eraser_channel_target_tests.rs b/crates/engine-paint/src/cmd/background_eraser_channel_target_tests.rs index 0a40dc59b1..5ae2c2ab0a 100644 --- a/crates/engine-paint/src/cmd/background_eraser_channel_target_tests.rs +++ b/crates/engine-paint/src/cmd/background_eraser_channel_target_tests.rs @@ -1,8 +1,9 @@ //! The Background Eraser with one colour channel of an RGB document targeted (#12488): the //! reference application refuses with an alert, erases nothing and adds no History state, with -//! Lock transparent pixels off and on. The composite erases as before; the partial sets that -//! weren't measured (two of three channels, CMYK inks and Lab components) keep erasing those -//! channels. 32-bit documents are left to #11912, where the reference refuses the tool outright. +//! Lock transparent pixels off and on. The composite erases as before; two of the three channels +//! refuse too, and are covered in `background_eraser_two_channel_target_tests.rs` (#18096). The +//! partial sets that weren't measured (CMYK inks and Lab components) keep erasing those channels. +//! 32-bit documents are left to #11912, where the reference refuses the tool outright. use super::transparency_lock_tests::{session, target}; use super::*; @@ -94,9 +95,7 @@ fn the_composite_erases_as_before() { } #[test] -fn partial_sets_that_were_not_measured_keep_erasing() { - let mut s = session(8); - assert_erases(&mut s, stroke(&DRAG, json!({"target": {"colors": [1, 2]}})), "RGB green and blue"); +fn partial_sets_in_cmyk_and_lab_keep_erasing() { for (mode, components) in [("cmyk", 4), ("lab", 3)] { for k in 0..components { let mut s = crate::session(); diff --git a/crates/engine-paint/src/cmd/background_eraser_transparency_lock_tests.rs b/crates/engine-paint/src/cmd/background_eraser_transparency_lock_tests.rs index c38f532b49..08328d60bd 100644 --- a/crates/engine-paint/src/cmd/background_eraser_transparency_lock_tests.rs +++ b/crates/engine-paint/src/cmd/background_eraser_transparency_lock_tests.rs @@ -83,7 +83,8 @@ fn with_a_colour_channel_target_the_preview_matches_the_commit_on_a_locked_layer s.execute("edit.fill", json!({"color": "#3a7fd0", "opacity": 37})).unwrap(); s.execute("select.deselect", json!({})).unwrap(); s.execute("layer.setProps", json!({"locks": {"transparency": true}})).unwrap(); - let opts = |pts: &[[f64; 2]]| json!({"points": pts, "size": 16, "hardness": 0.5, "opacity": 0.53, "target": {"colors": [1, 2]}}); + // All three colour channels: two of them refuse the tool (#18096). + let opts = |pts: &[[f64; 2]]| json!({"points": pts, "size": 16, "hardness": 0.5, "opacity": 0.53, "target": {"colors": [0, 1, 2]}}); let all = [[8.0, 9.0], [32.0, 10.0], [56.0, 11.0]]; let mut live = live_background_eraser(&s, &opts(&all[..1])).unwrap(); for q in &all[1..] { diff --git a/crates/engine-paint/src/cmd/background_eraser_two_channel_target_tests.rs b/crates/engine-paint/src/cmd/background_eraser_two_channel_target_tests.rs new file mode 100644 index 0000000000..961d4ca067 --- /dev/null +++ b/crates/engine-paint/src/cmd/background_eraser_two_channel_target_tests.rs @@ -0,0 +1,80 @@ +//! With two of an RGB document's three colour channels targeted, `paint.backgroundEraser` refuses +//! with the reference application's alert (#18096): nothing is written, no History state, and the +//! live preview refuses too. All three colour channels erase as the composite. Partial sets in +//! CMYK and Lab documents were not measured and keep erasing those channels (#12488). + +use super::transparency_lock_tests::{session, target}; +use super::*; + +const CLICK: [[f64; 2]; 1] = [[32.0, 20.0]]; +const DRAG: [[f64; 2]; 3] = [[8.0, 20.0], [32.0, 20.0], [56.0, 20.0]]; + +fn stroke(points: &[[f64; 2]], extra: Value) -> Value { + let mut p = json!({"points": points, "size": 20, "hardness": 1, "tolerance": 50}); + if let (Some(p), Some(extra)) = (p.as_object_mut(), extra.as_object()) { + p.extend(extra.clone()); + } + p +} + +/// Runs `p` and the live preview of its first point, expecting both refused with the alert text +/// and the document (pixels, layers, History) left as it was. +fn assert_refused(s: &mut Session, p: Value, case: &str) { + let (doc, steps) = (s.active().unwrap().doc.clone(), s.active().unwrap().history.past_len()); + let mut first = p.clone(); + first["points"] = json!([p["points"][0]]); + let live = live_background_eraser(s, &first).err().map(|e| e.to_string()); + assert_eq!(live.as_deref(), Some(CHANNEL_TARGET_REFUSED), "{case}: the live preview shows nothing"); + let err = s.execute("paint.backgroundEraser", p).unwrap_err().to_string(); + assert_eq!(err, CHANNEL_TARGET_REFUSED, "{case}"); + let st = s.active().unwrap(); + assert!(std::sync::Arc::ptr_eq(&doc, &st.doc), "{case}: nothing erased"); + assert_eq!(st.history.past_len(), steps, "{case}: no History step"); +} + +/// Runs `p`, expecting it to erase as one History step. +fn assert_erases(s: &mut Session, p: Value, case: &str) { + let id = s.active().unwrap().active_layer.unwrap(); + let (before, steps) = (s.active().unwrap().doc.layer(id).unwrap().surface().unwrap().clone(), s.active().unwrap().history.past_len()); + s.execute("paint.backgroundEraser", p).unwrap_or_else(|e| panic!("{case}: {e}")); + let st = s.active().unwrap(); + assert!(*st.doc.layer(id).unwrap().surface().unwrap() != before, "{case}: erased"); + assert_eq!(st.history.past_len(), steps + 1, "{case}: one History step"); +} + +#[test] +fn two_colour_channels_from_the_channels_panel_refuse_the_click_and_the_drag() { + for depth in [8, 16] { + for (first, second) in [("red", "green"), ("green", "blue"), ("red", "blue")] { + for points in [&CLICK[..], &DRAG[..]] { + let mut s = session(depth); + s.execute("channel.target", json!({"channel": first})).unwrap(); + s.execute("channel.target", json!({"channel": second, "extend": true})).unwrap(); + assert_refused(&mut s, stroke(points, json!({})), &format!("{depth}-bit, {first} + {second}, {} points", points.len())); + } + } + } +} + +#[test] +fn two_colour_channels_as_the_target_param_refuse_too() { + for colors in [[0, 1], [0, 2], [1, 2]] { + let mut s = session(8); + assert_refused(&mut s, stroke(&DRAG, json!({"target": {"colors": colors}})), &format!("colors {colors:?}")); + } +} + +#[test] +fn all_three_colour_channels_still_erase_as_the_composite() { + for depth in [8, 16] { + let mut s = session(depth); + s.execute("channel.target", json!({"channel": "red"})).unwrap(); + s.execute("channel.target", json!({"channel": "green", "extend": true})).unwrap(); + s.execute("channel.target", json!({"channel": "blue", "extend": true})).unwrap(); + assert_erases(&mut s, stroke(&CLICK, json!({})), &format!("{depth}-bit, the three channels from the panel")); + assert_eq!(target(&s).surface().unwrap().rgba(32, 20)[3], 0.0, "{depth}-bit: erased to transparency"); + + let mut s = session(depth); + assert_erases(&mut s, stroke(&DRAG, json!({"target": {"colors": [0, 1, 2]}})), &format!("{depth}-bit, colors [0, 1, 2]")); + } +} diff --git a/crates/engine-paint/src/cmd/eraser_cmds.rs b/crates/engine-paint/src/cmd/eraser_cmds.rs index 019c715cbf..8a81ee30a8 100644 --- a/crates/engine-paint/src/cmd/eraser_cmds.rs +++ b/crates/engine-paint/src/cmd/eraser_cmds.rs @@ -199,8 +199,9 @@ fn background_eraser_setup(s: &Session, p: &Value) -> Result<(Stroke, BgEraseSet return Err(e); } let colors = crate::color_channels::target(s, p)?; - // Measured: one colour channel of an RGB document. Other partial sets keep erasing those channels. - if d.doc.mode == ColorMode::Rgb && colors.is_some_and(|c| c.len() == 1) { + // Measured: any colour-channel set of an RGB document short of all three, one channel as much + // as two (#12488, #18096). CMYK inks and Lab components were not measured and keep erasing. + if d.doc.mode == ColorMode::Rgb && colors.is_some_and(|c| c.len() < d.doc.mode.color_channels()) { return Err(EngineError::Other(CHANNEL_TARGET_REFUSED.into())); } Ok((Stroke { brush, points: pts }, bs, id, colors)) @@ -344,3 +345,7 @@ mod transparency_lock_tests; #[cfg(test)] #[path = "background_eraser_channel_target_tests.rs"] mod channel_target_tests; + +#[cfg(test)] +#[path = "background_eraser_two_channel_target_tests.rs"] +mod two_channel_target_tests; diff --git a/crates/engine/tests/it/multi_channel_editing.rs b/crates/engine/tests/it/multi_channel_editing.rs index 87c8fa0eac..6c6207cc67 100644 --- a/crates/engine/tests/it/multi_channel_editing.rs +++ b/crates/engine/tests/it/multi_channel_editing.rs @@ -331,7 +331,9 @@ fn explicit_layer_restriction_protects_the_layer_actually_edited() { } #[test] -fn background_eraser_on_an_opaque_background_edits_only_selected_channels() { +fn background_eraser_refuses_a_partial_channel_target_on_an_opaque_background() { + // A channel target short of the composite refuses the tool (#18096), the opaque Background + // included: the preview and the command both fail and nothing is written. for depth in [8, 16, 32] { let mut s = Session::new(); s.execute("file.new", json!({"width":32,"height":24,"depth":depth})).unwrap(); @@ -339,15 +341,14 @@ fn background_eraser_on_an_opaque_background_edits_only_selected_channels() { subset(&mut s); let before = surface(&s); let id = s.active().unwrap().active_layer.unwrap(); + let steps = s.active().unwrap().history.past_len(); let p = json!({"points":[[12,12]],"size":12,"hardness":1,"tolerance":100}); - let live = photocraft_engine::eraser_cmds::live_background_eraser(&s, &p).unwrap(); - let shown = live.doc.layer(id).unwrap().surface().unwrap().clone(); - s.execute("paint.backgroundEraser", p).unwrap(); - let after = surface(&s); - unchanged(&before, &after); - assert!(before != after, "depth {depth}"); - assert!(shown == after, "preview depth {depth}"); - assert_eq!(before.format(), after.format()); + assert!(photocraft_engine::eraser_cmds::live_background_eraser(&s, &p).is_err(), "preview depth {depth}"); + let err = s.execute("paint.backgroundEraser", p).unwrap_err().to_string(); + assert!(err.contains("target channels do not cover the composite"), "depth {depth}: {err}"); + assert!(before == surface(&s), "depth {depth}: nothing erased"); + assert_eq!(s.active().unwrap().history.past_len(), steps, "depth {depth}: no History step"); + assert_eq!(before.format(), surface(&s).format()); assert!(s.active().unwrap().doc.is_background(id)); } } @@ -1029,7 +1030,8 @@ fn named_layers_resolve_identically_for_painting_and_component_restoration() { ("image.adjustments.shadowsHighlights", json!({"shadowAmount":50})), ("filter.lensCorrection", json!({"distortion":20})), ("paint.magicEraser", json!({"x":10,"y":10,"tolerance":255})), - ("paint.backgroundEraser", json!({"points":[[8,8],[22,12]],"size":9,"tolerance":100})), + // The Background Eraser refuses a partial colour-channel target (#18096); it resolves a + // named layer with the composite in its own test below. ("path.fill", json!({"color":"#ffffff"})), ("path.stroke", json!({"size":9,"color":"#ffffff"})), ] { @@ -1051,6 +1053,21 @@ fn named_layers_resolve_identically_for_painting_and_component_restoration() { } } +#[test] +fn the_background_eraser_resolves_a_named_layer_on_the_composite() { + let mut s = setup("rgb", 32); + let id = s.active().unwrap().active_layer.unwrap(); + let name = s.active().unwrap().doc.layer(id).unwrap().name.clone(); + let before = surface(&s); + s.execute("layer.new.layer", json!({})).unwrap(); + s.execute("edit.fill", json!({"color":"#333333"})).unwrap(); + let active = surface(&s); + s.execute("paint.backgroundEraser", json!({"points":[[8,8],[22,12]],"size":9,"tolerance":100,"layer":name})).unwrap(); + assert!(surface(&s) == active, "the active layer is untouched"); + let changed = s.active().unwrap().doc.layer(id).unwrap().surface().unwrap(); + assert!(&before != changed, "the named layer is edited"); +} + #[test] fn patch_and_content_aware_move_previews_protect_the_same_components_as_commit() { use photocraft_engine::retouch_cmds;