From 6bd3773e0743d21bb28d76ca7026298462f967ea Mon Sep 17 00:00:00 2001 From: David Mann Date: Sun, 8 Nov 2026 01:40:59 +0100 Subject: [PATCH] Address CodeRabbit review: match the foreground at the document's precision, pin the protect-off row The sampled colour is read from stored pixels, but the foreground was compared unrounded, so in 8- and 16-bit documents a foreground between two levels missed its own stored colour: Find Edges erased it, and a foreground stored as the sample no longer protected anything. The foreground is now rounded to the document's sample type first (32-bit float unchanged). protect_off_is_unchanged now pins the whole alpha row, not only the first and last block. Co-Authored-By: Claude Opus 5.5 --- crates/paint/src/bg_erase.rs | 12 +++++- .../src/bg_erase_protect_foreground_tests.rs | 41 +++++++++++++++++-- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/crates/paint/src/bg_erase.rs b/crates/paint/src/bg_erase.rs index 8220eff783a..314bf5ab78f 100644 --- a/crates/paint/src/bg_erase.rs +++ b/crates/paint/src/bg_erase.rs @@ -4,6 +4,7 @@ //! Colours are compared in the surface's own colour channels (RGB, CMYK, Gray, Lab… at any depth), //! so nothing is converted through RGB; only alpha changes. +use photocraft_color::{SampleType, read_sample, write_sample}; use photocraft_geom::Rect; use photocraft_raster::{Surface, from_rgba}; use serde::{Deserialize, Serialize}; @@ -62,6 +63,15 @@ fn protected_distance(d: f32, tol: f32, limit: f32) -> f32 { d * (tol / limit) } +/// `v` as the document stores it: rounded to the 8- or 16-bit levels, unchanged in 32-bit float. +/// The sampled colour is read from stored pixels, so the foreground is compared at the same +/// precision; otherwise a foreground between two levels misses its own stored colour (#11903). +fn at_sample_precision(sample: SampleType, v: f32) -> f32 { + let mut bytes = [0u8; 4]; + write_sample(&mut bytes, sample, 0, v); + read_sample(&bytes, sample, 0) +} + /// Erase along `stroke`. The brush opacity is ignored (Photoshop's Background Eraser has none); /// the selection limits the effect. Returns the damaged rectangle; empty when the surface has no /// alpha channel. @@ -90,7 +100,7 @@ pub fn background_eraser_effect(pre: &Surface, bs: &BgEraseSettings) -> Option Vec { from_rgba(&fmt, [c[0], c[1], c[2], 1.0])[..nc].to_vec() }; let swatch = native(bs.background); - let fg = native(bs.foreground); + let fg: Vec = native(bs.foreground).into_iter().map(|v| at_sample_precision(fmt.sample, v)).collect(); let (sampling, limits, protect_foreground) = (bs.sampling, bs.limits, bs.protect_foreground); let mut once: Option>> = None; let match_w = move |d: f32| -> f32 { diff --git a/crates/paint/src/bg_erase_protect_foreground_tests.rs b/crates/paint/src/bg_erase_protect_foreground_tests.rs index 42e3a18c5af..c94604072fb 100644 --- a/crates/paint/src/bg_erase_protect_foreground_tests.rs +++ b/crates/paint/src/bg_erase_protect_foreground_tests.rs @@ -130,10 +130,9 @@ fn find_edges_keeps_the_foreground_colour_itself() { #[test] fn protect_off_is_unchanged() { // The drag without protection, as main gives it before the graded erase of #11901: today's - // pre-stroke matching over overlapping dabs. Pinned so the protection change cannot leak in. - let off = drag(PixelFormat::RGBA8, None); - assert_eq!(off[0], 0); - assert_eq!(off[7], 255); + // pre-stroke matching over overlapping dabs. The whole row is pinned so the protection change + // cannot leak in (#11901 will change it to the reference's 0 30 75 114 156 198 238 255). + assert_eq!(drag(PixelFormat::RGBA8, None), [0, 0, 0, 0, 0, 0, 92, 255]); let mut s = blocks(PixelFormat::RGBA8); let mut t = s.clone(); let on_far = BgEraseSettings { foreground: [0.0, 0.0, 1.0, 1.0], protect_foreground: true, ..settings(None) }; @@ -172,3 +171,37 @@ fn protection_works_in_the_documents_own_channels() { assert_eq!(*v, 1.0, "gray: block {i} kept, {a:?}"); } } + +#[test] +fn the_foreground_is_matched_at_the_documents_precision() { + // Mid grey 0.5 lies between two 8-bit (and 16-bit) levels and is stored one level up, nearer + // white. The stored foreground colour must still count as the foreground, and a foreground + // that is stored as the sampled colour must still protect everything. + let grey = [0.5, 0.5, 0.5, 1.0]; + for mode in [ColorMode::Rgb, ColorMode::Grayscale] { + for sample in [SampleType::U8, SampleType::U16, SampleType::F32] { + let fmt = PixelFormat::new(mode, sample, true); + let bs = BgEraseSettings { + sampling: Sampling::Once, + limits: Limits::FindEdges, + tolerance: 1.0, + protect_foreground: true, + foreground: grey, + ..Default::default() + }; + // White (the sample) next to the foreground grey. + let mut s = Surface::new(fmt); + s.fill_rect(Rect::new(0, 0, 100, 100), &from_rgba(&fmt, [1.0; 4])); + s.fill_rect(Rect::new(100, 0, 200, 100), &from_rgba(&fmt, grey)); + apply_background_eraser(&mut s, &stroke(&[(50.0, 50.0), (150.0, 50.0)]), &bs, None); + assert_eq!(s.rgba(50, 50)[3], 0.0, "{fmt:?}: the sample erased"); + assert_eq!(s.rgba(150, 50)[3], 1.0, "{fmt:?}: the stored foreground colour kept"); + // The foreground grey is also the sample. + let mut s = Surface::new(fmt); + s.fill_rect(Rect::new(0, 0, 200, 100), &from_rgba(&fmt, grey)); + let t = s.clone(); + apply_background_eraser(&mut s, &stroke(&[(50.0, 50.0), (150.0, 50.0)]), &bs, None); + assert_eq!(s, t, "{fmt:?}: a foreground stored as the sample erases nothing"); + } + } +}