Merge #18618 into the merge queue batch

Magic Eraser: a contiguous fill stays in the selection's bounding rectangle (#18068)
This commit is contained in:
David Mann committed 2026-12-01 10:56:18 +01:00
commit 0b58ed23af
3 files changed
+165 -2

No files matched your search

+21 -1
View File
@@ -133,12 +133,30 @@ fn magic_eraser(s: &mut Session, p: &Value) -> Result<Value> {
return Ok(damage(s, Rect::EMPTY, false));
}
}
/// The bounding rectangle of a selection whose pixels are all fully selected or not selected, on the canvas.
/// `None` for one with partly selected pixels (feathered, anti-aliased), and for an empty one.
fn hard_bounds(selection: &photocraft_raster::Surface, canvas: Rect) -> Option<Rect> {
let bounds = selection.content_bounds();
if bounds.is_empty() || selection.default_pixel().first().is_some_and(|v| *v > 0.0) {
return None;
}
let channels = selection.channels().max(1);
let hard = (bounds.y0..bounds.y1).step_by(64).all(|y| {
let rows = Rect::new(bounds.x0, y, bounds.x1, y.saturating_add(64).min(bounds.y1));
selection.read_region(rows).chunks_exact(channels).all(|px| px[0] <= 0.0 || px[0] >= 1.0)
});
hard.then(|| bounds.intersect(&canvas))
}
let (area, img) = if sample_all {
crate::selection_cmds::sample_rgba8(s, true)?
} else {
let d = s.active().ok_or(EngineError::NoDocument)?;
let surf = d.doc.layer(id).and_then(Layer::surface).ok_or(EngineError::NoLayer(id))?;
(d.doc.bounds(), sel::rgba8_image(surf, d.doc.bounds()))
// #18068: with a selection, a contiguous fill runs inside the selection's bounding rectangle, over selected
// and unselected pixels alike, and a seed outside that rectangle finds nothing. A selection with partly
// selected pixels and Sample All Layers were not measured and keep the whole canvas.
let area = d.doc.selection.as_ref().filter(|_| contiguous).and_then(|m| hard_bounds(m, d.doc.bounds())).unwrap_or_else(|| d.doc.bounds());
(area, sel::rgba8_image(surf, area))
};
// The reference's colour distance is measured for 8-bit RGB only (#11995).
let rgb8 = s.active().is_some_and(|d| d.doc.mode == ColorMode::Rgb && d.doc.depth == SampleType::U8);
@@ -337,6 +355,8 @@ pub fn specs() -> Vec<CommandSpec> {
]
}
#[cfg(test)]
mod magic_eraser_contiguous_selection_bounds_tests;
#[cfg(test)]
mod magic_eraser_mode_refusal_tests;
#[cfg(test)]
@@ -0,0 +1,142 @@
//! #18068: with a selection and Contiguous on, the Magic Eraser's fill stays inside the selection's bounding
//! rectangle, over selected and unselected pixels alike, so it cannot reach another selected part by a path outside
//! it, and a click outside that rectangle erases nothing. The documents are the issue's, at a tenth of the size.
//! Not measured, so as before: a selection with partly selected pixels, and Sample All Layers.
use serde_json::{Value, json};
use crate::Session;
/// Red inside selection A and inside selection B, on the bar.
const IN_A: (i32, i32) = (10, 30);
const IN_B: (i32, i32) = (70, 30);
/// On the bar between A and B's side of the cut: inside the bounding rectangle, not selected.
const BESIDE_A: (i32, i32) = (25, 30);
/// The middle of the bar, between A and B: red when the bar is not cut.
const BETWEEN: (i32, i32) = (40, 30);
/// The top of the arch, above the bounding rectangle.
const ON_THE_ARCH: (i32, i32) = (40, 6);
/// White above the bounding rectangle, joined to the white of A and B above the bar.
const WHITE_ABOVE: (i32, i32) = (40, 12);
/// White inside A, above the bar.
const WHITE_IN_A: (i32, i32) = (10, 22);
fn fill(s: &mut Session, [x, y, w, h]: [i32; 4], color: &str) {
s.execute("select.rect", json!({"x": x, "y": y, "width": w, "height": h, "antiAlias": false})).unwrap();
s.execute("edit.fill", json!({"color": color})).unwrap();
s.execute("select.deselect", json!({})).unwrap();
}
/// An 80 x 45 white document with a red bar across rows 25 to 35.
fn uncut_bar() -> Session {
let mut s = crate::session();
s.execute("file.new", json!({"width": 80, "height": 45, "mode": "rgb", "depth": 8, "background": "#ffffff"})).unwrap();
fill(&mut s, [0, 25, 80, 10], "#ff0000");
s
}
/// The bar cut between x 35 and 45, its two halves joined only by a red arch that rises to row 5, above the bounding
/// rectangle of the selections.
fn cut_bar() -> Session {
let mut s = uncut_bar();
fill(&mut s, [35, 25, 10, 10], "#ffffff");
for arch in [[30, 5, 3, 20], [47, 5, 3, 20], [30, 5, 20, 3]] {
fill(&mut s, arch, "#ff0000");
}
s
}
/// Selections A (x 0 to 20) and B (x 60 to 80), rows 20 to 40: their bounding rectangle is the full width of those
/// rows.
fn select_a_and_b(s: &mut Session) {
s.execute("select.rect", json!({"x": 0, "y": 20, "width": 20, "height": 20})).unwrap();
s.execute("select.rect", json!({"x": 60, "y": 20, "width": 20, "height": 20, "mode": "add"})).unwrap();
}
fn erase(s: &mut Session, (x, y): (i32, i32), options: Value) -> Value {
let mut p = json!({"x": x, "y": y});
p.as_object_mut().unwrap().extend(options.as_object().unwrap().clone());
s.execute("paint.magicEraser", p).unwrap()
}
fn alpha(s: &Session, (x, y): (i32, i32)) -> f32 {
let d = s.active().unwrap();
d.doc.layer(d.active_layer.unwrap()).unwrap().surface().unwrap().rgba(x, y)[3]
}
/// How many History states there are, and the name of the newest.
fn history(s: &Session) -> (usize, Option<String>) {
let history = &s.active().unwrap().history;
(history.past_len(), history.undo_label().map(String::from))
}
#[test]
fn a_contiguous_fill_cannot_reach_another_selected_part_round_the_outside() {
for anti_alias in [true, false] {
let mut s = cut_bar();
select_a_and_b(&mut s);
let states = history(&s).0;
let reply = erase(&mut s, IN_A, json!({"antiAlias": anti_alias}));
assert_eq!(reply["erased"], true, "anti-alias {anti_alias}");
assert_eq!(alpha(&s, IN_A), 0.0, "anti-alias {anti_alias}: the red of A is erased");
assert_eq!(alpha(&s, IN_B), 1.0, "anti-alias {anti_alias}: B is only joined to A by the arch, outside the rectangle");
assert_eq!(alpha(&s, BESIDE_A), 1.0, "anti-alias {anti_alias}: the erase stays clipped to the selection");
assert_eq!(history(&s), (states + 1, Some("Magic Eraser".into())), "anti-alias {anti_alias}");
}
}
#[test]
fn a_seed_outside_the_bounding_rectangle_erases_nothing() {
for (mut s, seed, case) in [(cut_bar(), ON_THE_ARCH, "the arch"), (uncut_bar(), WHITE_ABOVE, "white above the rectangle")] {
select_a_and_b(&mut s);
let (before, states) = (s.active().unwrap().doc.clone(), history(&s));
let reply = erase(&mut s, seed, json!({}));
assert_eq!(reply, json!({"erased": false, "damage": [0, 0, 0, 0]}), "{case}");
assert!(s.active().unwrap().doc == before, "{case}: nothing changed");
assert_eq!(history(&s), states, "{case}: no History state");
}
}
#[test]
fn a_seed_in_the_rectangle_erases_the_selected_part_of_its_region() {
// The uncut bar joins A and B inside the rectangle, through pixels that are not selected.
for (seed, case) in [(BETWEEN, "between A and B"), (IN_A, "inside A")] {
let mut s = uncut_bar();
select_a_and_b(&mut s);
let states = history(&s).0;
erase(&mut s, seed, json!({}));
assert_eq!((alpha(&s, IN_A), alpha(&s, IN_B)), (0.0, 0.0), "{case}: the red of A and B is erased");
assert_eq!(alpha(&s, BETWEEN), 1.0, "{case}: the unselected red stays");
assert_eq!(alpha(&s, WHITE_IN_A), 1.0, "{case}: the white of A stays");
assert_eq!(history(&s), (states + 1, Some("Magic Eraser".into())), "{case}");
}
}
#[test]
fn contiguous_off_and_no_selection_keep_the_whole_canvas() {
// Contiguous off: every matching pixel, so the rectangle plays no part.
let mut s = cut_bar();
select_a_and_b(&mut s);
erase(&mut s, ON_THE_ARCH, json!({"contiguous": false}));
assert_eq!((alpha(&s, IN_A), alpha(&s, IN_B)), (0.0, 0.0), "Contiguous off erases the matching red of A and B");
// No selection: the fill follows the arch.
let mut s = cut_bar();
erase(&mut s, IN_A, json!({}));
assert_eq!((alpha(&s, ON_THE_ARCH), alpha(&s, IN_B)), (0.0, 0.0), "without a selection the whole joined red is erased");
}
/// Not measured, so as before: the region is found on the whole canvas and reaches B round the arch.
#[test]
fn a_soft_selection_and_sample_all_layers_keep_the_whole_canvas() {
let mut s = cut_bar();
select_a_and_b(&mut s);
s.execute("select.modify.feather", json!({"radius": 1})).unwrap();
erase(&mut s, IN_A, json!({}));
assert_eq!((alpha(&s, IN_A), alpha(&s, IN_B)), (0.0, 0.0), "a feathered selection");
let mut s = cut_bar();
select_a_and_b(&mut s);
erase(&mut s, IN_A, json!({"sampleAllLayers": true}));
assert_eq!((alpha(&s, IN_A), alpha(&s, IN_B)), (0.0, 0.0), "Sample All Layers");
}
@@ -96,7 +96,8 @@ fn duotone_and_multichannel_keep_todays_behaviour() {
for (case, mode) in [("Duotone", photocraft_color::ColorMode::Duotone), ("Multichannel", photocraft_color::ColorMode::Multichannel)] {
let mut s = session(mode, SampleType::U8);
let id = s.active().unwrap().active_layer.unwrap();
let r = s.execute("paint.magicEraser", json!({"x": 2, "y": 2, "antiAlias": false}));
// Inside the selection: a click outside its bounding rectangle erases nothing (#18068).
let r = s.execute("paint.magicEraser", json!({"x": 12, "y": 12, "antiAlias": false}));
assert!(r.is_ok(), "{case}: keeps today's behaviour, answers {r:?}");
assert_eq!(r.unwrap()["erased"], true, "{case}");
assert_eq!(s.active().unwrap().doc.layer(id).unwrap().name, "Layer 0", "{case}");