Merge pull request #19030

Co-authored-by: autocraft-bot[bot] <354215646+autocraft-bot[bot]@users.noreply.github.com>
This commit is contained in:
davidmann4andautocraft-bot[bot] authored and autocraft-bot[bot] committed 2026-12-02 22:19:56 +00:00
commit ea054fc0c7
2 files changed
+104 -18

No files matched your search

+21 -18
View File
@@ -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<f32> {
/// (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<f32> {
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<f32> {
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<u32, StampError> {
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<u32, StampError> {
#[cfg(test)]
mod tests;
#[cfg(test)]
mod tolerance_distance_tests;
#[cfg(test)]
mod tolerance_tests;
@@ -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);
}
}