Merge pull request #14205 from davidmann4/fix/12633-art-history-tolerance-threshold

Art History Brush: Tolerance threshold per stroke start (#12633)
This commit is contained in:
David Mann authored and GitHub committed 2026-11-19 18:38:35 +01:00
commit de06bae36c
3 files changed
+199 -17

No files matched your search

+34 -13
View File
@@ -442,6 +442,7 @@ pub struct StampParams<'a> {
pub hardness: f32,
pub opacity: f32,
pub flow: f32,
/// Tolerance as a fraction (Tolerance 50 % is `0.5`).
pub tolerance: f32,
/// Destination tile edge; `0` processes the whole buffer as one tile.
pub tile: i32,
@@ -581,26 +582,41 @@ fn dab_intersects(dab: &Dab, tile: (i32, i32, i32, i32)) -> bool {
x0 < tile.2 && x1 > tile.0 && y0 < tile.3 && y1 > tile.1
}
/// Mean difference between `src` and `dest` over the dab's covered pixels (working-space peak is
/// 1, so a tolerance of 50% is `0.5`). Per pixel it is the larger of the mean colour change and the
/// alpha change: an opaque pixel's unchanged alpha must not water a colour change down, and the
/// Eraser keeps the colour and lowers only the alpha. Fully transparent source samples are left
/// out; a dab whose samples are all transparent has a mean of `0.0`. `None` if nothing is covered.
fn dab_mean_diff(p: &StampParams<'_>, dab: &Dab) -> Option<f32> {
let (x0, y0, x1, y1) = dab_bounds(dab)?;
/// Radius, in pixels, of the neighbourhood around a stroke's start that Tolerance measures.
const START_RADIUS: f32 = 2.5;
/// Difference, in working-space units, that 100 % Tolerance asks for. The reference application
/// starts strokes on a 20-level change (about 8 %) at Tolerance 10 but not 20, and on red over grey
/// (about 50 %) at 80 but not 90, which a linear threshold of `0.6 × Tolerance` reproduces.
const TOLERANCE_SCALE: f32 = 0.6;
/// Mean difference between `src` and `dest` around the dab's centre, where its stroke starts
/// (working-space peak is 1). Per pixel it is the larger of the mean colour change and the alpha
/// change: an opaque pixel's unchanged alpha must not water a colour change down, and the Eraser
/// keeps the colour and lowers only the alpha. Fully transparent source samples are left out; a
/// start whose samples are all transparent has a mean of `0.0`. `None` if nothing is there.
fn start_diff(p: &StampParams<'_>, dab: &Dab) -> Option<f32> {
if !(dab.x.is_finite() && dab.y.is_finite()) {
return None;
}
let x0 = floor_i32(dab.x - START_RADIUS);
let y0 = floor_i32(dab.y - START_RADIUS);
let x1 = floor_i32(dab.x + START_RADIUS).saturating_add(1);
let y1 = floor_i32(dab.y + START_RADIUS).saturating_add(1);
let mut sampled = vec![0.0f32; p.ch];
let mut sum = 0.0f32;
let mut count = 0u32;
let mut transparent = 0u32;
for y in y0..y1 {
for x in x0..x1 {
if coverage_at(dab, x, y, p.hardness) <= 0.0 {
// The history state at the same position, not the dab's rotated read.
let (cx, cy) = (x as f32 + 0.5, y as f32 + 0.5);
if (cx - dab.x).hypot(cy - dab.y) > START_RADIUS {
continue;
}
let Some(di) = origin_index(p.dest_origin, x, y, p.dest_w, p.dest_h, p.ch) else { continue };
let Some(dst) = p.dest.get(di..di.saturating_add(p.ch)) else { continue };
let (sx, sy) = rotated_source(dab, x, y);
if !sample_bilinear(SrcView { data: p.src, w: p.src_w, h: p.src_h, origin: p.src_origin, ch: p.ch, alpha: p.alpha }, sx, sy, &mut sampled) {
if !sample_bilinear(SrcView { data: p.src, w: p.src_w, h: p.src_h, origin: p.src_origin, ch: p.ch, alpha: p.alpha }, cx, cy, &mut sampled) {
continue;
}
let nch = p.ch.min(dst.len()).min(sampled.len());
@@ -636,8 +652,10 @@ fn dab_mean_diff(p: &StampParams<'_>, dab: &Dab) -> Option<f32> {
/// Stamp posed dabs into `dest`, tile by tile, reading `src` at each dab's rotated positions.
///
/// Sequential dabs overlap: dab *n* sees dab *n − 1*. Returns how many destination pixels were
/// written (tolerance-skipped dabs count as zero).
/// Sequential dabs overlap: dab *n* sees dab *n − 1*. Tolerance is decided where a dab starts
/// (around its centre), so a dab that starts on a changed area also paints the unchanged pixels
/// it reaches. Returns how many destination pixels were written (tolerance-skipped dabs count as
/// zero).
pub fn stamp(p: &mut StampParams<'_>) -> Result<u32, StampError> {
dest_pixels(p)?;
if p.src_w == 0 || p.src_h == 0 {
@@ -653,9 +671,10 @@ pub fn stamp(p: &mut StampParams<'_>) -> Result<u32, StampError> {
let tiles = tile_grid(p.dest_w, p.dest_h, p.dest_origin, p.tile);
let mut written = 0u32;
let dabs: Vec<Dab> = p.dabs.to_vec();
let threshold = p.tolerance * TOLERANCE_SCALE;
for dab in &dabs {
let Some(bounds) = dab_bounds(dab) else { continue };
if p.tolerance > 0.0 && dab_mean_diff(p, dab).is_some_and(|m| m < p.tolerance) {
if p.tolerance > 0.0 && start_diff(p, dab).is_some_and(|m| m < threshold) {
continue;
}
let overlapping: Vec<(i32, i32, i32, i32)> = tiles.iter().copied().filter(|t| dab_intersects(dab, *t)).collect();
@@ -676,3 +695,5 @@ pub fn stamp(p: &mut StampParams<'_>) -> Result<u32, StampError> {
#[cfg(test)]
mod tests;
#[cfg(test)]
mod tolerance_tests;
+6 -4
View File
@@ -23,7 +23,8 @@ fn tolerance_ignores_fully_transparent_samples() {
}
});
let dab = Dab { x: 1.0, y: 0.5, theta: 0.0, radius: 2.0, pressure: 1.0 };
for tolerance in [0.6, 0.3] {
// Mean difference 0.4: under the 0.6 x Tolerance threshold at 1.0, over it at 0.3.
for tolerance in [1.0, 0.3] {
let mut dest = before.clone();
let written = stamp(&mut StampParams {
dest: &mut dest,
@@ -46,7 +47,7 @@ fn tolerance_ignores_fully_transparent_samples() {
alpha: Some(ch - 1),
})
.unwrap();
if tolerance == 0.6 {
if tolerance == 1.0 {
assert_eq!(written, 0, "channels={ch}, hidden={hidden}");
assert_eq!(dest, before);
} else {
@@ -377,12 +378,13 @@ fn tolerance_zero_writes_where_history_differs() {
#[test]
fn tolerance_ignores_unchanged_alpha() {
// Colour moved by 0.3 and alpha did not: the 0.25 threshold must not see a 0.225 average.
// Colour moved by 0.3 and alpha did not: the 0.27 threshold (Tolerance 45 %) must not see a
// 0.225 average.
let (w, h, ch) = (48usize, 48usize, 4);
let src = fill(w, h, ch, |_, _, c| if c == 3 { 1.0 } else { 0.6 });
let dest0 = fill(w, h, ch, |_, _, c| if c == 3 { 1.0 } else { 0.3 });
let dab = Dab { x: 24.0, y: 24.0, theta: 0.0, radius: 8.0, pressure: 1.0 };
for (tolerance, paints) in [(0.25, true), (0.35, false)] {
for (tolerance, paints) in [(0.45, true), (0.55, false)] {
let mut dest = dest0.clone();
let mut p = StampParams {
dest: &mut dest,
@@ -0,0 +1,159 @@
//! Tolerance decides where a stroke may start, with the reference application's threshold (#12633).
use super::*;
const W: usize = 80;
const H: usize = 40;
const CH: usize = 4;
fn level(v: u8) -> f32 {
f32::from(v) / 255.0
}
/// Source: grey on the left half, green on the right half (the measured Setup state).
fn setup_source() -> Vec<f32> {
image(|x| if x < W / 2 { [128, 128, 128] } else { [40, 160, 60] })
}
/// Current layer: red on the left half (far from grey), 20 levels off the green on the right.
fn current_layer() -> Vec<f32> {
image(|x| if x < W / 2 { [255, 0, 0] } else { [60, 180, 80] })
}
fn image(rgb: impl Fn(usize) -> [u8; 3]) -> Vec<f32> {
let mut v = Vec::with_capacity(W * H * CH);
for _ in 0..H {
for x in 0..W {
let [r, g, b] = rgb(x);
v.extend_from_slice(&[level(r), level(g), level(b), 1.0]);
}
}
v
}
fn dab(x: f32, radius: f32) -> Dab {
Dab { x, y: H as f32 / 2.0, theta: 0.0, radius, pressure: 1.0 }
}
/// Stamps `dabs` at `tolerance` (a fraction, Tolerance 50 % = 0.5) and returns the new layer.
fn paint(dest: &[f32], dabs: &[Dab], tolerance: f32) -> Vec<f32> {
let src = setup_source();
let mut out = dest.to_vec();
let mut p = StampParams {
dest: &mut out,
dest_w: W,
dest_h: H,
dest_origin: (0, 0),
src: &src,
src_w: W,
src_h: H,
src_origin: (0, 0),
ch: CH,
dabs,
hardness: 1.0,
opacity: 1.0,
flow: 1.0,
tolerance,
tile: 0,
selection: None,
lock_transparent: false,
alpha: Some(3),
};
stamp(&mut p).unwrap();
out
}
/// Pixels changed in columns `x0..x1`.
fn changed(before: &[f32], after: &[f32], x0: usize, x1: usize) -> usize {
let mut n = 0;
for y in 0..H {
for x in x0..x1 {
let i = (y * W + x) * CH;
if (0..CH).any(|c| (before[i + c] - after[i + c]).abs() > 1.0e-4) {
n += 1;
}
}
}
n
}
const LEFT: f32 = 12.0;
const RIGHT: f32 = 68.0;
#[test]
fn a_small_difference_starts_strokes_at_10_but_not_at_20() {
let before = current_layer();
let dabs = [dab(RIGHT, 6.0)];
assert!(changed(&before, &paint(&before, &dabs, 0.10), 40, 80) > 0, "20 levels off paints at Tolerance 10");
assert_eq!(changed(&before, &paint(&before, &dabs, 0.20), 0, 80), 0, "20 levels off paints nothing at Tolerance 20");
}
#[test]
fn red_over_grey_starts_strokes_up_to_80_and_nothing_from_90() {
let before = current_layer();
let dabs = [dab(LEFT, 6.0)];
for tolerance in [0.60, 0.80] {
assert!(changed(&before, &paint(&before, &dabs, tolerance), 0, 40) > 0, "red over grey paints at {tolerance}");
}
let both = [dab(LEFT, 6.0), dab(RIGHT, 6.0)];
for tolerance in [0.90, 0.95, 0.99, 1.0] {
assert_eq!(changed(&before, &paint(&before, &both, tolerance), 0, 80), 0, "nothing paints at {tolerance}");
}
}
#[test]
fn tolerance_zero_paints_everywhere() {
let before = current_layer();
let after = paint(&before, &[dab(LEFT, 6.0), dab(RIGHT, 6.0)], 0.0);
assert!(changed(&before, &after, 0, 40) > 0);
assert!(changed(&before, &after, 40, 80) > 0);
// Even where the layer equals the source, the dab is stamped (nothing visible changes).
let src = setup_source();
let mut out = src.clone();
let d = [dab(LEFT, 6.0)];
let mut p = StampParams {
dest: &mut out,
dest_w: W,
dest_h: H,
dest_origin: (0, 0),
src: &src,
src_w: W,
src_h: H,
src_origin: (0, 0),
ch: CH,
dabs: &d,
hardness: 1.0,
opacity: 1.0,
flow: 1.0,
tolerance: 0.0,
tile: 0,
selection: None,
lock_transparent: false,
alpha: Some(3),
};
assert!(stamp(&mut p).unwrap() > 0);
}
/// Several dabs across both halves at Tolerance 20: only those starting on the red half paint, and
/// the right half changes only where a red-half dab reaches into it.
#[test]
fn at_20_strokes_start_only_on_the_large_difference() {
let before = current_layer();
let dabs: Vec<Dab> = [8.0, 20.0, 36.0, 52.0, 64.0, 76.0].iter().map(|&x| dab(x, 6.0)).collect();
let after = paint(&before, &dabs, 0.20);
assert!(changed(&before, &after, 0, 40) > 0);
assert_eq!(changed(&before, &after, 44, 80), 0, "no dab starts on the 20-level half");
assert!(changed(&before, &after, 40, 44) > 0, "the dab at x 36 runs into the right half");
}
/// The rule is where a stroke starts, not the mean under the whole dab: a big dab that starts just
/// inside the red half paints over the right half too, and one that starts just inside the right
/// half paints nothing although a large part of it covers red.
#[test]
fn the_start_decides_not_the_whole_dab() {
let before = current_layer();
let started_on_red = paint(&before, &[dab(37.0, 14.0)], 0.80);
assert!(changed(&before, &started_on_red, 40, 80) > 0, "a stroke started on red runs into the green half");
let started_on_green = paint(&before, &[dab(43.0, 14.0)], 0.20);
assert_eq!(changed(&before, &started_on_green, 0, 80), 0, "a stroke started on the 20-level half stays unpainted");
}