Ignore hidden RGB when Magic Wand selects transparency (#2273)

This commit is contained in:
Harrison O'Connor-Hoover authored and GitHub committed 2026-10-10 01:51:28 -04:00
1 parent bf1036a273
commit 34382a65ca
2 files changed
+92 -3

No files matched your search

+32 -3
View File
@@ -151,11 +151,12 @@ pub fn combine(old: Option<&Surface>, new: &[f32], area: Rect, mode: SelectionMo
}
fn close(a: [f32; 4], b: [f32; 4], tol: f32) -> bool {
(0..4).all(|c| (a[c] - b[c]).abs() * 255.0 <= tol + 1e-3)
(a[3] == 0.0 && b[3] == 0.0) || (0..4).all(|c| (a[c] - b[c]).abs() * 255.0 <= tol + 1e-3)
}
/// Pixels similar to the seed pixel (per-channel difference ≤ `tolerance`
/// levels, alpha included). `contiguous` limits to the 4-connected region
/// levels, alpha included; fully transparent pixels match regardless of hidden RGB).
/// `contiguous` limits to the 4-connected region
/// around the seed. `px` covers `area`.
pub fn magic_wand(px: &[[f32; 4]], area: Rect, seed: (i32, i32), tolerance: f32, contiguous: bool, anti_alias: bool) -> Vec<f32> {
let (w, h) = (area.width() as usize, area.height() as usize);
@@ -252,7 +253,9 @@ pub fn wand_region(img: &[[u8; 4]], area: Rect, seed: (i32, i32), tolerance: f32
let (w, h) = (area.width() as usize, area.height() as usize);
let target = img[(seed.1 - area.y0) as usize * w + (seed.0 - area.x0) as usize];
let tol = (tolerance + 1e-3).floor().max(0.0) as i32;
let similar = |p: [u8; 4]| (0..4).all(|c| (p[c] as i32 - target[c] as i32).abs() <= tol);
// Copying a selection can leave RGB under zero alpha. Those invisible colours must not
// split a transparent region into pieces, as if the copied source were still visible.
let similar = |p: [u8; 4]| (target[3] == 0 && p[3] == 0) || (0..4).all(|c| (p[c] as i32 - target[c] as i32).abs() <= tol);
let mut marks = vec![0u8; w * h];
let (mut bx0, mut by0, mut bx1, mut by1) = (usize::MAX, usize::MAX, 0usize, 0usize);
if contiguous {
@@ -819,6 +822,32 @@ mod tests {
assert_eq!(m.iter().filter(|v| **v > 0.0).count(), 3);
}
#[test]
fn wand_ignores_hidden_rgb_only_when_both_pixels_are_fully_transparent() {
let area = Rect::new(0, 0, 6, 1);
let pixels = [[255, 0, 0, 0], [0, 255, 0, 0], [255, 0, 0, 128], [253, 0, 0, 128], [0, 0, 255, 0], [255, 0, 0, 255]];
let floats: Vec<_> = pixels.iter().map(|p| p.map(|v| f32::from(v) / 255.0)).collect();
for (seed, tolerance, contiguous, expected) in [
((0, 0), 0.0, true, [1.0, 1.0, 0.0, 0.0, 0.0, 0.0]),
((0, 0), 0.0, false, [1.0, 1.0, 0.0, 0.0, 1.0, 0.0]),
((4, 0), 32.0, false, [1.0, 1.0, 0.0, 0.0, 1.0, 0.0]),
((2, 0), 0.0, false, [0.0, 0.0, 1.0, 0.0, 0.0, 0.0]),
((2, 0), 2.0, true, [0.0, 0.0, 1.0, 1.0, 0.0, 0.0]),
((5, 0), 32.0, false, [0.0, 0.0, 0.0, 0.0, 0.0, 1.0]),
] {
assert_eq!(magic_wand(&floats, area, seed, tolerance, contiguous, false), expected);
let region = wand_region(&pixels, area, seed, tolerance, contiguous, false).unwrap();
for (x, value) in expected.into_iter().enumerate() {
assert_eq!(region.at(x as i32, 0), value, "seed={seed:?}, tolerance={tolerance}, x={x}");
}
}
// Hidden RGB cannot introduce extra edges into the anti-aliased selection either.
let plain: Vec<_> = pixels.iter().map(|p| if p[3] == 0 { [0; 4] } else { *p }).collect();
for contiguous in [false, true] {
assert_eq!(wand_region(&pixels, area, (0, 0), 0.0, contiguous, true), wand_region(&plain, area, (0, 0), 0.0, contiguous, true));
}
}
#[test]
fn color_range_falloff() {
let px = vec![[1.0, 0.0, 0.0, 1.0], [0.9, 0.0, 0.0, 1.0], [0.0, 0.0, 1.0, 1.0], [1.0, 0.0, 0.0, 0.0]];
@@ -0,0 +1,60 @@
//! #2260: copying an irregular selection retains RGB under zero alpha. The Magic Wand
//! must select the visible transparent area, independently of those hidden colours.
use photocraft_doc::LayerId;
use photocraft_engine::Session;
use photocraft_geom::Rect;
use serde_json::json;
#[test]
fn wand_after_irregular_layer_copy_ignores_hidden_rgb() {
for depth in [8, 16, 32] {
let mut s = Session::new();
s.execute("file.new", json!({"width": 32, "height": 24, "depth": depth, "background": "#ff0000"})).unwrap();
let original = s.active().unwrap().active_layer.unwrap();
s.edit("Two-colour source", |doc, _| {
doc.layer_mut(original).unwrap().surface_mut().unwrap().fill_rect(Rect::new(16, 0, 32, 24), &[0.0, 0.0, 1.0, 1.0]);
Ok(())
})
.unwrap();
let area = s.active().unwrap().doc.bounds();
let source = s.active().unwrap().doc.layer(original).unwrap().surface().unwrap().read_region(area);
s.execute("select.lasso", json!({"points": [[4, 4], [28, 4], [4, 20]], "antiAlias": false})).unwrap();
let copied = LayerId(s.execute("layer.new.layerViaCopy", json!({})).unwrap()["layer"].as_u64().unwrap());
s.execute("layer.setProps", json!({"layer": original.0, "visible": false})).unwrap();
let copied_pixels = s.active().unwrap().doc.layer(copied).unwrap().surface().unwrap().clone();
assert!(s.active().unwrap().doc.selection.is_none(), "Layer via Copy deselects");
// The copied triangle does not divide the canvas: every transparent pixel connects
// to (0, 0), including differently coloured zero-alpha pixels inside its bounding box.
assert_eq!(copied_pixels.rgba(24, 16)[3], 0.0);
assert_eq!(copied_pixels.rgba(6, 6)[3], 1.0);
for (tolerance, contiguous, all_layers) in [(0, true, false), (32, false, false), (32, true, true)] {
let steps = s.active().unwrap().history.past_len();
s.execute(
"select.magicWand",
json!({"x": 0, "y": 0, "tolerance": tolerance, "contiguous": contiguous, "antiAlias": false, "sampleAllLayers": all_layers}),
)
.unwrap();
let st = s.active().unwrap();
let mask = st.doc.selection.as_ref().unwrap();
for y in area.y0..area.y1 {
for x in area.x0..area.x1 {
let expected = if copied_pixels.rgba(x, y)[3] == 0.0 { 1.0 } else { 0.0 };
assert_eq!(
mask.sample_channel(x, y, 0),
expected,
"depth={depth}, tolerance={tolerance}, contiguous={contiguous}, allLayers={all_layers}, at ({x}, {y})"
);
}
}
assert_eq!(st.doc.layer(original).unwrap().surface().unwrap().read_region(area), source, "copy and selection preserve the original pixels");
assert_eq!(st.doc.layer(copied).unwrap().surface().unwrap().read_region(area), copied_pixels.read_region(area), "the wand only changes selection");
assert_eq!(st.history.past_len(), steps + 1);
assert!(s.undo());
let st = s.active().unwrap();
assert!(st.doc.selection.is_none(), "one undo clears the wand selection");
assert!(st.doc.layer(copied).is_some(), "undo preserves the preceding copy");
assert!(!st.doc.layer(original).unwrap().visible);
}
}
}