From a7b4b63449fe95bfc68ef36947eaa8225dc6372b Mon Sep 17 00:00:00 2001 From: crastinator <141459787+crastinator@users.noreply.github.com> Date: Wed, 2 Dec 2026 17:24:49 +0200 Subject: [PATCH] Background Eraser: Contiguous erases behind a partly matching line only as strongly as the line (#18063) Contiguous now caps each pixel's erase strength by the strongest path to it from the tip centre (a widest-path search over the pre-stroke match weights), so the sampled colour behind a weak barrier keeps the barrier's alpha instead of going to 0. The tip centre itself is no barrier. Find Edges keeps the plain connected / not connected flood fill; Discontiguous is unchanged. --- crates/paint/src/bg_erase.rs | 79 +++++++++++- .../src/bg_erase_contiguous_barrier_tests.rs | 119 ++++++++++++++++++ 2 files changed, 195 insertions(+), 3 deletions(-) create mode 100644 crates/paint/src/bg_erase_contiguous_barrier_tests.rs diff --git a/crates/paint/src/bg_erase.rs b/crates/paint/src/bg_erase.rs index f0a0ed892bf..85b630dcbf8 100644 --- a/crates/paint/src/bg_erase.rs +++ b/crates/paint/src/bg_erase.rs @@ -228,10 +228,21 @@ pub fn background_eraser_effect(pre: &Surface, bs: &BgEraseSettings) -> Option impl Iterator { + let (x, y) = (i % w, i / w); + [(x > 0).then(|| i - 1), (x + 1 < w).then(|| i + 1), (y > 0).then(|| i - w), (y + 1 < h).then(|| i + w)].into_iter().flatten() +} + /// Zeroes the weights not 4-connected to `(sx, sy)` through non-zero weights. fn keep_connected(weight: &mut [f32], w: usize, h: usize, sx: usize, sy: usize) { let mut keep = vec![false; w * h]; @@ -303,6 +320,58 @@ fn keep_connected(weight: &mut [f32], w: usize, h: usize, sx: usize, sy: usize) } } +/// Like [`keep_connected`], but each kept weight becomes the strength of the strongest path to it: +/// the largest, over all such paths, of the smallest weight on the path, its own included. The +/// start pixel itself is not a barrier: it becomes infinite, so it caps nothing. +fn strongest_paths(weight: &mut [f32], w: usize, h: usize, sx: usize, sy: usize) { + use std::collections::BinaryHeap; + let n = w * h; + let mut best = vec![0.0f32; n]; + let si = sy * w + sx; + if weight.get(si).is_some_and(|v| *v > 0.0) { + // Widest-path search, strongest first. Weights are non-negative, whose bit patterns sort + // like their values (infinity too). Pixels as strong as the one being visited go on a plain + // stack: in a large uniform area that is almost every pixel, so the heap stays small. + let mut done = vec![false; n]; + let mut heap = BinaryHeap::new(); + best[si] = f32::INFINITY; + heap.push((best[si].to_bits(), si)); + let mut stack = Vec::new(); + while let Some((bits, i)) = heap.pop() { + if done[i] || f32::from_bits(bits) < best[i] { + continue; + } + stack.push(i); + while let Some(c) = stack.pop() { + if done[c] { + continue; + } + done[c] = true; + let level = best[c]; + for j in neighbours(c, w, h) { + let v = level.min(weight[j]); + if done[j] || v <= best[j] { + continue; + } + best[j] = v; + if v >= level { + stack.push(j); + } else { + heap.push((v.to_bits(), j)); + } + } + } + } + } + weight.copy_from_slice(&best); +} + +/// How strongly the strongest path from the tip centre reaches pixel `i` before it: the largest +/// [`strongest_paths`] strength among its 4-neighbours (infinite for the start pixel's neighbours). +fn path_strength(strength: &[f32], w: usize, h: usize, i: usize) -> f32 { + neighbours(i, w, h).map(|j| strength[j]).fold(0.0, f32::max) +} + #[cfg(test)] #[path = "bg_erase_protect_foreground_tests.rs"] mod protect_foreground_tests; @@ -323,6 +392,10 @@ mod decontamination_tests; #[path = "bg_erase_lowest_pressure_tests.rs"] mod lowest_pressure_tests; +#[cfg(test)] +#[path = "bg_erase_contiguous_barrier_tests.rs"] +mod contiguous_barrier_tests; + #[cfg(test)] mod tests { use super::*; diff --git a/crates/paint/src/bg_erase_contiguous_barrier_tests.rs b/crates/paint/src/bg_erase_contiguous_barrier_tests.rs new file mode 100644 index 00000000000..5970fa84dfd --- /dev/null +++ b/crates/paint/src/bg_erase_contiguous_barrier_tests.rs @@ -0,0 +1,119 @@ +//! Background Eraser, Limits Contiguous (#18063): what lies behind a partly matching line is erased +//! only as strongly as the line itself. Expected values are the reference application's +//! single-click measurements from the issue (8-bit RGB, 40 px hard round tip, Tolerance 50, +//! Sampling Continuous): the line (200,60,40) keeps alpha 29 and so does the sampled colour +//! (200,40,40) beyond it. + +use photocraft_color::PixelFormat; +use photocraft_geom::Rect; +use photocraft_raster::{Surface, from_rgba}; + +use crate::bg_erase::{BgEraseSettings, apply_background_eraser}; +use crate::replace::{Limits, Sampling}; +use crate::{BrushSettings, Stroke, StrokePoint}; + +const BLOCK: [i32; 3] = [200, 40, 40]; +const LINE: [i32; 3] = [200, 60, 40]; +/// The measured alpha of the line, and of the block beyond it with Contiguous. +const LINE_ALPHA: i32 = 29; +const LEVELS: i32 = 5; + +fn rgb(c: [i32; 3]) -> [f32; 4] { + [c[0] as f32 / 255.0, c[1] as f32 / 255.0, c[2] as f32 / 255.0, 1.0] +} + +fn settings(limits: Limits) -> BgEraseSettings { + BgEraseSettings { sampling: Sampling::Continuous, limits, tolerance: 0.5, ..Default::default() } +} + +/// One click with a 40 px hard round tip at `at`. +fn click(s: &mut Surface, at: (f64, f64), limits: Limits) { + let stroke = Stroke { + brush: BrushSettings { size: 40.0, hardness: 1.0, spacing: 0.25, pressure_size: false, ..Default::default() }, + points: vec![StrokePoint::new(at.0, at.1, 1.0)], + }; + apply_background_eraser(s, &stroke, &settings(limits), None).unwrap(); +} + +/// A 100 x 200 block of [`BLOCK`] with a 2 px column of [`LINE`] at x 50-51 over `line_rows`. +fn block_with_line(line_rows: (i32, i32)) -> Surface { + let fmt = PixelFormat::RGBA8; + let mut s = Surface::new(fmt); + s.fill_rect(Rect::new(0, 0, 100, 200), &from_rgba(&fmt, rgb(BLOCK))); + s.fill_rect(Rect::new(50, line_rows.0, 52, line_rows.1), &from_rgba(&fmt, rgb(LINE))); + s +} + +fn alpha(s: &Surface, x: i32, y: i32) -> i32 { + (s.rgba(x, y)[3] * 255.0).round() as i32 +} + +#[test] +fn contiguous_erases_beyond_a_partly_matching_line_only_as_strongly_as_the_line() { + let mut s = block_with_line((0, 200)); + click(&mut s, (38.0, 100.0), Limits::Contiguous); + assert_eq!(alpha(&s, 38, 100), 0, "the sampled colour before the line is erased fully"); + for x in 50..52 { + let got = alpha(&s, x, 100); + assert!((got - LINE_ALPHA).abs() <= LEVELS, "line x {x}: alpha {got}, measured {LINE_ALPHA}"); + } + for x in 52..57 { + let got = alpha(&s, x, 100); + assert!((got - LINE_ALPHA).abs() <= LEVELS, "beyond the line x {x}: alpha {got}, measured {LINE_ALPHA}"); + } +} + +#[test] +fn discontiguous_still_erases_beyond_the_line_fully() { + let mut s = block_with_line((0, 200)); + click(&mut s, (38.0, 100.0), Limits::Discontiguous); + for x in 52..57 { + assert_eq!(alpha(&s, x, 100), 0, "beyond the line x {x}"); + } +} + +#[test] +fn a_way_around_the_line_still_erases_fully() { + // The line ends inside the tip, so the colour beyond it is reached without crossing it. + let mut s = block_with_line((0, 105)); + click(&mut s, (38.0, 100.0), Limits::Contiguous); + for x in 52..57 { + assert_eq!(alpha(&s, x, 100), 0, "beyond the line x {x}, reached around its end"); + } +} + +#[test] +fn a_low_contrast_edge_reached_directly_is_erased_as_today() { + // No line: the right half is the line's colour, reached straight from the tip centre. + let fmt = PixelFormat::RGBA8; + let mut s = Surface::new(fmt); + s.fill_rect(Rect::new(0, 0, 50, 200), &from_rgba(&fmt, rgb(BLOCK))); + s.fill_rect(Rect::new(50, 0, 100, 200), &from_rgba(&fmt, rgb(LINE))); + let mut d = s.clone(); + click(&mut s, (38.0, 100.0), Limits::Contiguous); + click(&mut d, (38.0, 100.0), Limits::Discontiguous); + for x in 30..58 { + assert_eq!(alpha(&s, x, 100), alpha(&d, x, 100), "x {x}: Contiguous differs from Discontiguous"); + } + assert!((alpha(&s, 55, 100) - LINE_ALPHA).abs() <= LEVELS); +} + +#[test] +fn a_partly_matching_pixel_under_the_tip_centre_is_no_barrier() { + // Background Swatch samples the block colour while the hotspot sits on one pixel of the line's + // colour: the exact matches around it are still erased fully, as before #18063. + let fmt = PixelFormat::RGBA8; + let mut s = Surface::new(fmt); + s.fill_rect(Rect::new(0, 0, 100, 200), &from_rgba(&fmt, rgb(BLOCK))); + s.fill_rect(Rect::new(38, 100, 39, 101), &from_rgba(&fmt, rgb(LINE))); + let stroke = Stroke { + brush: BrushSettings { size: 40.0, hardness: 1.0, spacing: 0.25, pressure_size: false, ..Default::default() }, + points: vec![StrokePoint::new(38.5, 100.5, 1.0)], + }; + let bs = BgEraseSettings { sampling: Sampling::BackgroundSwatch, background: rgb(BLOCK), ..settings(Limits::Contiguous) }; + apply_background_eraser(&mut s, &stroke, &bs, None).unwrap(); + for x in [30, 37, 39, 45] { + assert_eq!(alpha(&s, x, 100), 0, "exact match x {x} next to the hotspot"); + } + assert!((alpha(&s, 38, 100) - LINE_ALPHA).abs() <= LEVELS, "the hotspot pixel itself"); +}