Image Trace: Noise leaves cut-edge blends out of a speck's color

When Noise picks the nearest color for a speck, the speck's average now
leaves out the semi-transparent pixels along a cut-out image's edge that
are the color beside them blended with the transparent background.
settle_cut_edges returns which pixels those are. A speck made only of
them merges by border. In Grayscale the speck's gray is read with luma,
the gray Grayscale traces with, and the test probe shift_probe denoises
as a trace does.

The docs now say the rule applies in Color and Grayscale (in Black and
White a region has only one neighboring color, so Black and White Logo
changes only by its Noise value), name `ea3f81d9c3` as the base of the
measurements, and say that the gradient-border test sets Noise 15
because at Noise 8 its borders stray 0.92 px against a 0.9 px limit.
This commit is contained in:
dotCooCoo
2026-11-15 09:01:14 -08:00
parent e02faf2293
commit 3dad1013a4
8 changed files with 130 additions and 47 deletions
+6 -3
View File
@@ -5,13 +5,13 @@ use super::params::{Method, Mode, TraceParams};
use super::pipeline::TracedPath;
#[cfg(test)]
use super::pipeline::{bw_tones, left_out};
#[cfg(test)]
use super::quantize;
use super::raster::{Raster, TraceError};
use super::{
AtomicUsize, Crack, Loop, Ordering, PathData, Quantized, TRANSPARENT, TracedGradient, TracedShape, centerline, contour, fit, logo, shapes,
trace_mask,
};
#[cfg(test)]
use super::{denoise, quantize};
/// What tracing one colour layer needs.
pub(crate) struct Layers<'a> {
@@ -1005,7 +1005,10 @@ impl Layers<'_> {
pub(crate) fn shift_probe(img: &Raster, params: &TraceParams) -> Vec<[f64; 4]> {
let (w, h) = (img.width as usize, img.height as usize);
let mut q = quantize(img, params);
denoise(&mut q.labels, w, h, params.noise as usize);
let labs = super::quantize::palette_labs(&q.palette);
let source =
super::quantize::Source { labs: &labs, rgba: &img.rgba, blended: &[], gray: matches!(params.mode, Mode::Grayscale | Mode::BlackAndWhite) };
super::quantize::denoise_nearest(&mut q.labels, w, h, params.noise as usize, &[], Some(&source));
let n = q.palette.len();
let rank: Vec<usize> = (0..n).collect();
let gradients = vec![None; n];
+8 -4
View File
@@ -250,9 +250,11 @@ fn trace_paths(img: &Raster, params: &TraceParams, max_anchors: usize, steps: &(
let (mut q, rare) = quantize::quantize_keeping(without.as_ref().unwrap_or(img), params);
go_on()?;
// The semi-transparent edge of a cut-out image traces as the color beside it.
if matches!(params.mode, Mode::Color | Mode::Grayscale) {
quantize::settle_cut_edges(&mut q, img, params.mode == Mode::Grayscale);
}
let blended = if matches!(params.mode, Mode::Color | Mode::Grayscale) {
quantize::settle_cut_edges(&mut q, img, params.mode == Mode::Grayscale)
} else {
Vec::new()
};
drop(without);
// Each gradient is a palette entry of its own (its colour: the gradient's middle).
let mut gradients: Vec<Option<TracedGradient>> = vec![None; q.palette.len()];
@@ -272,8 +274,10 @@ fn trace_paths(img: &Raster, params: &TraceParams, max_anchors: usize, steps: &(
let keep: Vec<bool> = q.palette.iter().map(|c| rare.contains(c)).collect();
// A speck far from the color around it joins the color beside it nearest its own pixels.
let labs = quantize::palette_labs(&q.palette);
let source = quantize::Source { labs: &labs, rgba: &img.rgba, gray: matches!(params.mode, Mode::Grayscale | Mode::BlackAndWhite) };
let source =
quantize::Source { labs: &labs, rgba: &img.rgba, blended: &blended, gray: matches!(params.mode, Mode::Grayscale | Mode::BlackAndWhite) };
quantize::denoise_nearest(&mut q.labels, w, h, params.noise as usize, &keep, Some(&source));
drop(blended);
go_on()?;
// Ignore Color, background only: the labels traced, with the ignored color's areas that reach
// the edge left out. Its other areas are traced with the rest. Edges are still placed by
+7 -2
View File
@@ -594,12 +594,15 @@ pub(super) fn compact(mut labels: Vec<u16>, palette: Vec<[u8; 3]>) -> Quantized
/// when one of the fully opaque pixels it reaches has it, or when its color is a blend of none of
/// them, as with a thin semi-transparent outline of another color. Pixels outside the image do not
/// count as transparent. With `gray` (Grayscale), a pixel's color is its gray (see [`luma`]).
pub(crate) fn settle_cut_edges(q: &mut Quantized, img: &Raster, gray: bool) {
/// Returns which cut-edge pixels are a blend of the color of a fully opaque pixel they reach (see
/// [`edge_blend_of`]), whether or not they took that color (empty when `q` does not match `img`).
pub(crate) fn settle_cut_edges(q: &mut Quantized, img: &Raster, gray: bool) -> Vec<bool> {
let (w, h) = (img.width as usize, img.height as usize);
let px = img.rgba.as_chunks::<4>().0;
if q.labels.len() != w * h || px.len() != w * h {
return;
return Vec::new();
}
let mut blended = vec![false; w * h];
let color = |p: &[u8; 4]| if gray { [luma(*p); 3] } else { [p[0], p[1], p[2]] };
let edge = cut_edge(img);
let mut changed = false;
@@ -627,6 +630,7 @@ pub(crate) fn settle_cut_edges(q: &mut Quantized, img: &Raster, gray: bool) {
}
}
});
blended[i] = !near.is_empty();
if keep || near.is_empty() {
continue;
}
@@ -641,6 +645,7 @@ pub(crate) fn settle_cut_edges(q: &mut Quantized, img: &Raster, gray: bool) {
if changed {
*q = compact(std::mem::take(&mut q.labels), std::mem::take(&mut q.palette));
}
blended
}
/// Calls `visit` with the index of each fully opaque pixel of `img` within two pixels of the pixel
+54 -18
View File
@@ -4,6 +4,8 @@ use super::rare::RARE_ISLAND;
/// Merge whole 4-connected regions, smallest first, into their most common neighbouring label.
/// The merge order does not depend on `min_area`: a higher threshold continues the same sequence
/// instead of splitting a region already merged at a lower threshold. Transparency is a barrier.
/// It reads only the labels. A trace also reads the image, and a speck far from the color around
/// it joins the color beside it nearest its pixels instead.
pub fn denoise(labels: &mut [u16], w: usize, h: usize, min_area: usize) {
denoise_keeping(labels, w, h, min_area, &[]);
}
@@ -27,9 +29,13 @@ pub(crate) const NEAREST_PIXELS: u32 = 2;
pub(crate) struct Source<'a> {
/// Each label's color in Oklab × 100 ([`super::palette_labs`]).
pub labs: &'a [[f64; 3]],
/// The image the labels were made from, 4 bytes a pixel.
/// The prepared image the labels were made from, 4 bytes a pixel.
pub rgba: &'a [u8],
/// Whether the labels are grays (Grayscale, Black and White): the source pixels are compared as their gray.
/// The pixels along a cut edge that [`super::settle_cut_edges`] finds to be the color of an opaque pixel beside
/// them blended with the transparent background. They are not read.
pub blended: &'a [bool],
/// Whether the labels are grays (Grayscale, Black and White): the pixels are compared as their gray
/// ([`super::luma`]).
pub gray: bool,
}
@@ -144,8 +150,8 @@ pub(crate) fn denoise_nearest(labels: &mut [u16], w: usize, h: usize, min_area:
&& (area >= NEAREST_PIXELS || by_colour.iter().filter(|&&(_, border)| border == longest).count() == 1)
&& let Some(own) = source.labs.get(colour[id as usize] as usize)
&& distance(own, source.labs.get(chosen as usize)) >= NEAREST_APART
&& let Some(mean) = mean_lab(head[id as usize], &next, source)
{
let mean = mean_lab(head[id as usize], &next, source);
let to = |c: u16| distance(&mean, source.labs.get(c as usize));
if let Some(&(nearest, _)) = by_colour
.iter()
@@ -198,24 +204,26 @@ fn find(parent: &mut [u32], mut a: u32) -> u32 {
a
}
/// The source pixels of the list that starts at `first` (linked through `next`), averaged, in Oklab × 100.
fn mean_lab(first: u32, next: &[u32], source: &Source) -> [f64; 3] {
let (mut sum, mut n) = ([0.0f64; 3], 0.0f64);
/// The source pixels of the list that starts at `first` (linked through `next`), averaged, in Oklab × 100, read as
/// `source` says, leaving out the blended cut-edge pixels. `None` when every pixel is one of them.
fn mean_lab(first: u32, next: &[u32], source: &Source) -> Option<[f64; 3]> {
let (mut sum, mut n) = ([0.0f64; 3], 0u32);
let mut pixel = first;
while pixel != NONE {
if let Some(p) = source.rgba.get(pixel as usize * 4..pixel as usize * 4 + 3) {
for (s, &v) in sum.iter_mut().zip(p) {
*s += f64::from(v);
let blended = source.blended.get(pixel as usize).copied().unwrap_or(false);
if !blended && let Some(&[r, g, b, a]) = source.rgba.get(pixel as usize * 4..pixel as usize * 4 + 4) {
let p = if source.gray { [f64::from(super::luma([r, g, b, a])); 3] } else { [r, g, b].map(f64::from) };
for (s, v) in sum.iter_mut().zip(p) {
*s += v;
}
n += 1.0;
n += 1;
}
pixel = next.get(pixel as usize).copied().unwrap_or(NONE);
}
let mut mean = sum.map(|s| s / n.max(1.0));
if source.gray {
mean = [0.299 * mean[0] + 0.587 * mean[1] + 0.114 * mean[2]; 3];
if n == 0 {
return None;
}
super::oklab(mean)
Some(super::oklab(sum.map(|s| s / f64::from(n))))
}
/// The distance between two colors in Oklab × 100; infinite when `q` is missing.
@@ -374,7 +382,7 @@ mod tests {
let rgba: Vec<u8> =
labels.iter().flat_map(|&l| GROUND_INK_TONE_BROWN.get(l as usize).copied().unwrap_or([0; 3]).into_iter().chain([255])).collect();
let mut out = labels.to_vec();
denoise_nearest(&mut out, w, h, min_area, &[], Some(&Source { labs: &labs, rgba: &rgba, gray: false }));
denoise_nearest(&mut out, w, h, min_area, &[], Some(&Source { labs: &labs, rgba: &rgba, blended: &[], gray: false }));
out
}
@@ -506,14 +514,42 @@ mod tests {
let mut blind = labels.clone();
denoise_keeping(&mut blind, w, h, 15, &[]);
let labs = crate::quantize::palette_labs(&GROUND_INK_TONE_BROWN[..2]);
let rgba = vec![0u8; w * h * 4];
// Every pixel ink: read, the speck would join the line.
let rgba: Vec<u8> = (0..w * h).flat_map(|_| [30, 25, 20, 255]).collect();
let mut out = labels.clone();
denoise_nearest(&mut out, w, h, 15, &[], Some(&Source { labs: &labs, rgba: &rgba, gray: false }));
denoise_nearest(&mut out, w, h, 15, &[], Some(&Source { labs: &labs, rgba: &rgba, blended: &[], gray: false }));
assert_eq!(out, blind);
let labs = crate::quantize::palette_labs(&GROUND_INK_TONE_BROWN);
let mut out = labels.clone();
denoise_nearest(&mut out, w, h, 15, &[], Some(&Source { labs: &labs, rgba: &rgba[4..], gray: false }));
denoise_nearest(&mut out, w, h, 15, &[], Some(&Source { labs: &labs, rgba: &rgba[4..], blended: &[], gray: false }));
assert_eq!(out, blind);
let mut out = labels.clone();
denoise_nearest(&mut out, w, h, 15, &[], Some(&Source { labs: &labs, rgba: &rgba, blended: &[], gray: false }));
assert_eq!(out[10 * w + 10], 1, "a source that covers them is read");
}
/// A speck made only of cut-edge pixels that are the color beside them blended with the transparent background
/// merges by border alone.
#[test]
fn a_speck_of_blended_cut_edge_pixels_merges_by_border() {
let (labels, w, h) = speck_beside_a_line(3);
let labs = crate::quantize::palette_labs(&GROUND_INK_TONE_BROWN);
let rgba: Vec<u8> =
labels.iter().flat_map(|&l| GROUND_INK_TONE_BROWN.get(l as usize).copied().unwrap_or([0; 3]).into_iter().chain([255])).collect();
let mut blended = vec![false; w * h];
for (x, y) in SPECK {
blended[y * w + x] = true;
}
let mut out = labels.clone();
denoise_nearest(&mut out, w, h, 15, &[], Some(&Source { labs: &labs, rgba: &rgba, blended: &blended, gray: false }));
let mut blind = labels.clone();
denoise_keeping(&mut blind, w, h, 15, &[]);
assert_eq!(out, blind);
// One pixel that is not such a blend is enough to read it.
blended[10 * w + 10] = false;
let mut out = labels.clone();
denoise_nearest(&mut out, w, h, 15, &[], Some(&Source { labs: &labs, rgba: &rgba, blended: &blended, gray: false }));
assert_eq!(out[10 * w + 10], 1);
}
/// A minimum so large that regions keep merging into big ones.
+25 -2
View File
@@ -716,13 +716,35 @@ fn a_cut_out_edge_in_grayscale_ties_by_its_gray() {
let t = TRANSPARENT;
let q = Quantized { labels: vec![0, 2, 1, t, t, t], palette: vec![[50, 50, 50], [150, 150, 150], [90, 90, 90]] };
let mut gray = q.clone();
crate::quantize::settle_cut_edges(&mut gray, &img, true);
let marked = crate::quantize::settle_cut_edges(&mut gray, &img, true);
assert_eq!(gray.palette.get(gray.labels[1] as usize), Some(&[50, 50, 50]));
assert_eq!(marked, [false, true, false, false, false, false], "the edge pixel is marked as a blend");
let mut color = q;
crate::quantize::settle_cut_edges(&mut color, &img, false);
assert_eq!(color.palette.get(color.labels[1] as usize), Some(&[150, 150, 150]));
}
#[test]
fn a_cut_out_edge_pixel_already_of_the_color_beside_it_is_marked_when_a_blend_of_it() {
// A semi-transparent dark red edge pixel between opaque red and blue pixels, already labeled red: it keeps red,
// and is marked as a blend for Noise. A purple one labeled red is a blend of neither pixel's color alone and is
// not marked.
for (edge, blend) in [([128, 0, 0, 192], true), ([128, 0, 128, 192], false)] {
let img = Raster::from_fn(3, 2, |x, y| match (x, y) {
(0, 0) => [255, 0, 0, 255],
(1, 0) => edge,
(2, 0) => [0, 0, 255, 255],
_ => [0, 0, 0, 0],
});
let t = TRANSPARENT;
let q = Quantized { labels: vec![0, 0, 1, t, t, t], palette: vec![[255, 0, 0], [0, 0, 255]] };
let mut settled = q.clone();
let marked = crate::quantize::settle_cut_edges(&mut settled, &img, false);
assert_eq!(settled, q, "{edge:?}");
assert_eq!(marked, [false, blend, false, false, false, false], "{edge:?}");
}
}
#[test]
fn a_semi_transparent_line_across_a_gap_keeps_its_own_color() {
// A light block, a transparent column, then a semi-transparent tan line: the block lies within
@@ -761,8 +783,9 @@ fn a_thin_semi_transparent_outline_keeps_its_own_color() {
let q = quantize(&img, &TraceParams { mode, colors: 3, ..TraceParams::default() });
assert_ne!(q.labels[9 * 60 + 30], q.labels[30 * 60 + 30], "{mode:?}, alpha {alpha}: the outline has a color of its own");
let mut settled = q.clone();
crate::quantize::settle_cut_edges(&mut settled, &img, mode == Mode::Grayscale);
let marked = crate::quantize::settle_cut_edges(&mut settled, &img, mode == Mode::Grayscale);
assert_eq!(settled, q, "{mode:?}, alpha {alpha}");
assert!(!marked.contains(&true), "{mode:?}, alpha {alpha}: the outline is no blend of the square");
}
}
}
+23 -14
View File
@@ -317,15 +317,20 @@ A region below Noise used to join the color it shares the longest border with. A
line on a light ground, a dark speck of the line touches the ground on more sides than the line, so
it joined the ground and the line broke into dashes. Now, when the color of the longest border is
10 or more apart from the region's own color (Oklab × 100), the region joins the neighboring color
nearest the average of its source pixels, among the colors that share at least a tenth of the
longest border. A single pixel whose longest border is tied between colors joins by border as
before, so an anti-aliased corner pixel stays with the shape's corner. The rule changes only which
color a region joins, and higher Noise still continues the same merge sequence. The values are
nearest the average of its pixels, among the colors that share at least a tenth of the longest
border. The average is taken in the image as prepared for tracing (deblocked, and smoothed for a
photo) and leaves out the semi-transparent pixels along a cut-out image's edge that are the color
beside them blended with the transparent background. A single pixel whose longest border is tied
between colors keeps the previous rule. The rule changes only which
color a region joins, and higher Noise still continues the same merge sequence. It applies in Color
and Grayscale: in Black and White a region has only one neighboring color. The values are
VectorCraft's own. 6 Colors and 16 Colors move from Noise 15 to 8, and Black and White Logo from 25 to 5.
The new Noise values were chosen with the rule in place, and the measurements below include both
changes.
Measured on 17 local images (synthetic shapes and photos at several sizes, a geometric drawing and
eight illustrations; not repository assets), against the same `main` without the change, with the
mean absolute difference per channel (0 to 255) against the image on white:
eight illustrations; not repository assets), against `main` at `ea3f81d9c3`, with the mean absolute
difference per channel (0 to 255) against the image on white:
| preset | three illustrations with fine dark lines | four other illustrations | synthetic shapes and photos |
|:--|:--|:--|:--|
@@ -333,15 +338,19 @@ mean absolute difference per channel (0 to 255) against the image on white:
| 6 Colors | 7.24 / 9.14 / 9.21 → 5.87 / 6.66 / 7.87; anchors +55% to +73% | −0.6% to −3.5%; anchors +13% to +20% | within 0.03; anchors 0% to +22% |
| High Fidelity Photo | 8.42 / 10.11 / 7.46 → 6.85 / 7.54 / 6.00; anchors +31% to +51% | −1.4% to −5.8%; anchors −9% to +4% | within 0.08; anchors −6% to +5% |
Black and White Logo's pixels that differ from the image thresholded at 128 fall from 2.00 / 2.42 /
1.96% to 1.49 / 1.58 / 1.23% on the three illustrations and from 1.19 to 2.37% to 1.14 to 1.94% on the
other four, with 5% to 64% more anchors; the synthetic inputs change by 0.01% or less. A simple flat
illustration changes by 1.1% or less in every preset, and the geometric drawing keeps its anchor
count and difference in every preset. Tests: `a_dark_speck_beside_a_dark_line_joins_the_line`,
`a_speck_close_to_its_longest_border_color_joins_it`, `a_color_with_a_short_border_is_not_chosen`,
`a_lone_pixel_tied_between_colors_merges_by_border`, `noise_still_removes_every_region_below_it` and
With Black and White Logo, where only the Noise value changes, the pixels that differ from the image
thresholded at 128 fall from 2.00 / 2.42 / 1.96% to 1.49 / 1.58 / 1.23% on the three illustrations,
with 37% to 64% more anchors, and from 1.19 to 2.37% to 1.14 to 1.94% on the other four, with 5% to
41% more anchors; the synthetic inputs change by 0.01% or less. On a simple flat illustration the
difference changes by 1.0% or less in every preset, with up to 21% more anchors, and the geometric
drawing keeps its anchor count and difference in every preset. Tests:
`a_dark_speck_beside_a_dark_line_joins_the_line`, `a_speck_close_to_its_longest_border_color_joins_it`,
`a_color_with_a_short_border_is_not_chosen`, `a_lone_pixel_tied_between_colors_merges_by_border`,
`a_speck_of_settled_cut_edge_pixels_merges_by_border`, `noise_still_removes_every_region_below_it` and
`a_source_that_does_not_cover_the_labels_is_not_read` in `crates/trace/src/quantize/denoise.rs`.
`the_borders_of_a_gradients_bands_follow_its_contours` runs at Noise 15, the value it was written for.
`the_borders_of_a_gradients_bands_follow_its_contours` now traces at Noise 15: at 16 Colors' new
Noise 8 the borders of its noisy gradient stray 0.92 px from the contours, above the test's 0.9 px
limit.
## Pieces returning to a junction
An outline piece can return to its starting junction while being fitted as an open chain. At loose
+6 -3
View File
@@ -3849,9 +3849,12 @@ already walks Image Trace. Short agent entry points:
when nothing was traced.
`noise` (Color, Grayscale and Black and White, not Flat Logo) merges areas smaller than that many pixels into a
neighboring color. Each one joins the color it shares the longest border with, unless that color is 10 or more apart from its own (Oklab
× 100). Then it joins the neighboring color nearest the average of its source pixels, among the colors that share at
least a tenth of the longest border. A single pixel whose longest border is tied between two or more colors always
joins by border. These distances and fractions are VectorCraft's own values. The built-in presets 6 Colors and 16
× 100). Then it joins the neighboring color nearest the average of its pixels, among the colors that share at
least a tenth of the longest border. The average is taken in the image as prepared for tracing (deblocked, and
smoothed for a photo) and leaves out the semi-transparent pixels along a cut-out image's edge that are the color
beside them blended with the transparent background. A single pixel whose
longest border is tied between two or more colors always joins by border. In Black and White an area has only one
neighboring color. These distances and fractions are VectorCraft's own values. The built-in presets 6 Colors and 16
Colors use Noise 8, and Black and White Logo uses Noise 5.
`imageTrace.presets.save {name?, newName?, preset?, enhanced?, params?}` saves tracing settings as a preset kept with
the preferences: `params` over `preset`'s (default: the saved preset `name`, else Default), checked as `imageTrace.make`
File diff suppressed because one or more lines are too long