From 2d627f491668cf12358f6d4c858e2678974f2730 Mon Sep 17 00:00:00 2001 From: Terra854 Date: Wed, 2 Dec 2026 17:23:23 +0800 Subject: [PATCH] Fix Art History Brush tolerance colour distance --- crates/algo/src/art_history.rs | 39 +++++---- .../art_history/tolerance_distance_tests.rs | 83 +++++++++++++++++++ 2 files changed, 104 insertions(+), 18 deletions(-) create mode 100644 crates/algo/src/art_history/tolerance_distance_tests.rs diff --git a/crates/algo/src/art_history.rs b/crates/algo/src/art_history.rs index 01dcbd306da..621a97d1d81 100644 --- a/crates/algo/src/art_history.rs +++ b/crates/algo/src/art_history.rs @@ -585,17 +585,16 @@ fn dab_intersects(dab: &Dab, tile: (i32, i32, i32, i32)) -> bool { /// 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. +/// Keep the existing alpha threshold in working-space units. Colour distances use this same +/// scale, so their threshold is equivalent to Euclidean distance greater than Tolerance. 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 { +/// (working-space peak is 1). Per pixel it is the larger of the scaled Euclidean colour distance +/// and the alpha change. The flag records whether colour contributes to the mean, requiring a +/// strict threshold while alpha-only changes keep their inclusive threshold. Fully transparent +/// source samples are left out; an entirely transparent start has a mean of `0.0`. +fn start_diff(p: &StampParams<'_>, dab: &Dab) -> Option<(f32, bool)> { if !(dab.x.is_finite() && dab.y.is_finite()) { return None; } @@ -604,9 +603,11 @@ fn start_diff(p: &StampParams<'_>, dab: &Dab) -> Option { 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; + // Avoid summation drift turning equal per-pixel distances into a larger neighbourhood mean. + let mut sum = 0.0f64; let mut count = 0u32; let mut transparent = 0u32; + let mut colour_contributes = false; for y in y0..y1 { for x in x0..x1 { // The history state at the same position, not the dab's rotated read. @@ -627,27 +628,27 @@ fn start_diff(p: &StampParams<'_>, dab: &Dab) -> Option { transparent = transparent.saturating_add(1); continue; } - let (mut colour, mut colours, mut alpha) = (0.0f32, 0u32, 0.0f32); + let (mut colour, mut alpha) = (0.0f32, 0.0f32); for i in 0..nch { let Some((&s, &d)) = sampled.get(i).zip(dst.get(i)) else { continue }; if p.alpha == Some(i) { alpha = (s - d).abs(); } else { - colour += (s - d).abs(); - colours += 1; + colour += (s - d) * (s - d); } } - let colour = if colours == 0 { 0.0 } else { colour / colours as f32 }; - sum += colour.max(alpha); + let colour = colour.sqrt() * TOLERANCE_SCALE; + colour_contributes |= colour > alpha; + sum += f64::from(colour.max(alpha)); count = count.saturating_add(1); } } if count == 0 { // Stamping a fully transparent sample leaves the pixel as it is. - return (transparent > 0).then_some(0.0); + return (transparent > 0).then_some((0.0, false)); } - let mean = sum / count as f32; - mean.is_finite().then_some(mean) + let mean = (sum / f64::from(count)) as f32; + mean.is_finite().then_some((mean, colour_contributes)) } /// Stamp posed dabs into `dest`, tile by tile, reading `src` at each dab's rotated positions. @@ -674,7 +675,7 @@ pub fn stamp(p: &mut StampParams<'_>) -> Result { let threshold = p.tolerance * TOLERANCE_SCALE; for dab in &dabs { let Some(bounds) = dab_bounds(dab) else { continue }; - if p.tolerance > 0.0 && start_diff(p, dab).is_some_and(|m| m < threshold) { + if p.tolerance > 0.0 && start_diff(p, dab).is_some_and(|(m, colour)| m < threshold || (colour && m == threshold)) { continue; } let overlapping: Vec<(i32, i32, i32, i32)> = tiles.iter().copied().filter(|t| dab_intersects(dab, *t)).collect(); @@ -696,4 +697,6 @@ pub fn stamp(p: &mut StampParams<'_>) -> Result { #[cfg(test)] mod tests; #[cfg(test)] +mod tolerance_distance_tests; +#[cfg(test)] mod tolerance_tests; diff --git a/crates/algo/src/art_history/tolerance_distance_tests.rs b/crates/algo/src/art_history/tolerance_distance_tests.rs new file mode 100644 index 00000000000..18100e9a4e3 --- /dev/null +++ b/crates/algo/src/art_history/tolerance_distance_tests.rs @@ -0,0 +1,83 @@ +//! #18227: per-stroke Tolerance measures Euclidean colour distance. + +use super::*; + +fn written(source: [f32; 4], current: [f32; 4], tolerance: f32) -> u32 { + let source = source.repeat(16 * 16); + let mut dest = current.repeat(16 * 16); + let dab = Dab { x: 8.5, y: 8.5, theta: 0.0, radius: 1.0, pressure: 1.0 }; + stamp(&mut StampParams { + dest: &mut dest, + dest_w: 16, + dest_h: 16, + dest_origin: (0, 0), + src: &source, + src_w: 16, + src_h: 16, + src_origin: (0, 0), + ch: 4, + dabs: std::slice::from_ref(&dab), + hardness: 1.0, + opacity: 1.0, + flow: 1.0, + tolerance, + tile: 0, + selection: None, + lock_transparent: false, + alpha: Some(3), + }) + .unwrap() +} + +fn grey(value: f32) -> [f32; 4] { + [value, value, value, 1.0] +} + +#[test] +fn a_single_changed_channel_has_the_same_weight_in_rgb() { + let source = grey(128.0 / 255.0); + for channel in 0..3 { + for (delta, tolerance, paints) in + [(20.0, 0.05, true), (28.0, 0.10, true), (56.0, 0.20, true), (80.0, 0.30, true), (104.0, 0.40, true), (96.0, 0.40, false)] + { + let mut current = source; + current[channel] += delta / 255.0; + assert_eq!(written(source, current, tolerance) > 0, paints, "channel {channel}, delta {delta}, Tolerance {tolerance}"); + } + } +} + +#[test] +fn measured_greys_and_red_cross_the_euclidean_threshold() { + let source = grey(128.0 / 255.0); + for (levels, tolerance, paints) in + [(188.0, 0.40, true), (68.0, 0.40, true), (184.0, 0.40, false), (72.0, 0.40, false), (1.0, 0.80, true), (1.0, 0.90, false)] + { + assert_eq!(written(source, grey(levels / 255.0), tolerance) > 0, paints, "grey {levels}, Tolerance {tolerance}"); + } + assert!(written(source, [1.0, 0.0, 0.0, 1.0], 0.86) > 0); + assert_eq!(written(source, [1.0, 0.0, 0.0, 1.0], 0.87), 0); +} + +#[test] +fn colour_requires_strictly_more_than_tolerance_and_zero_always_starts() { + let source = grey(0.0); + assert_eq!(written(source, [0.5, 0.0, 0.0, 1.0], 0.5), 0); + assert!(written(source, [0.5, 0.0, 0.0, 1.0], 0.49) > 0); + assert!(written(source, source, 0.0) > 0); +} + +#[test] +fn alpha_keeps_its_existing_scale_and_inclusive_threshold() { + let source = grey(0.5); + assert!(written(source, [0.5, 0.5, 0.5, 0.4], 1.0) > 0); + assert!(written(source, [0.5, 0.5, 0.5, 0.6], 0.5) > 0); + assert_eq!(written(source, [0.5, 0.5, 0.5, 0.8], 0.5), 0); +} + +#[test] +fn fully_transparent_source_samples_still_do_not_paint() { + for tolerance in [0.0, 0.1, 1.0] { + assert_eq!(written([1.0, 0.0, 0.0, 0.0], grey(0.0), tolerance), 0); + } +}