diff --git a/crates/engine/src/cmd/buildcmds.rs b/crates/engine/src/cmd/buildcmds.rs index 66c9ec8fc4..2e5b2874fa 100644 --- a/crates/engine/src/cmd/buildcmds.rs +++ b/crates/engine/src/cmd/buildcmds.rs @@ -144,7 +144,7 @@ pub fn specs() -> Vec { "Make", ["Object", "Image Trace"], None, - "{id?: image or Image Trace group (default: selection), preset?: name (imageTrace.presets), savedPreset?: {settings} (a saved preset's settings, which the journal records so a replay traces with them after the preset is changed, renamed or deleted; used in place of the saved preset `preset` names), enhanced?: false (the preset's enhanced version: with Auto Grouping, and with Shapes in all but High Fidelity Photo; Flat Logo has none and is traced as it is), params?: {mode: \"blackAndWhite\"|\"grayscale\"|\"color\"|\"logo\", threshold: 0-255, palette: \"limited\"|\"fullTone\"|\"automatic\"|\"documentLibrary\" (color mode; limited: the `colors` best colours, fullTone: as many as the tones need, more with colorDetail, automatic: flat art with its own colours, else fullTone, documentLibrary: at most `colors` of the library's colours, as they are), colors: 2-256, colorDetail: 0-100 (fullTone, automatic), library: \"document\" (the document's swatches) or a swatch library id or name (swatch.library.list; documentLibrary), colorGroup?: name (color mode, documentLibrary with library \"document\": the colors of that document color group instead of every swatch; \"\" is every swatch), paths: 0-100, corners: 0-100, noise: px, method: \"abutting\"|\"overlapping\" (abutting with fills, not mode logo: a \"Seam Fills\" group under the paths holds 1.5 px strokes along the edges two filled paths share, in the color of the lower path, so that no light hairline shows along those edges; the paths are not changed), ignoreWhite (leave out white areas), ignoreColor?: \"#rrggbb\" (with ignoreWhite: the colour left out instead of white), ignoreBackground: false (with ignoreWhite: only the areas of the ignored color that reach the image's edge are left out), keepColors?: [\"#rrggbb\"…] (not mode logo: only the palette colors within about 30 levels of a listed color are traced; empty: every color), snapCurvesToLines, shapes: false (filled outlines that are a circle, an ellipse, a rectangle or a regular polygon come out as that live shape), autoGrouping: false (each path that has others inside it is grouped with them), gradients: false (grayscale and color: an area whose colour changes evenly along one direction is one path with a linear gradient fill), fills: true, strokes: false (Create: lines up to strokeWidth wide become stroked centre lines; without fills, wider areas become stroked outlines), strokeWidth: px (10), logoColors?: [\"#rrggbb\"…] (mode logo: flat-colour logos only; colours found automatically or named here, at most ten; white is its own color, and a named color with every channel 245 or more is taken as that white, except with logoColors named, logoWhite false and no ignoreWhite; noise = areas under noise/10 px ignored; edges to a fraction of a pixel, sharp corners restored, lines and circles snapped; enclosed transparent areas are painted white while white is its own color, and stay holes with ignoreWhite or without a white of its own; fills/strokes are ignored; gradients and photos are refused, judged by the image's own colors also when logoColors are named), logoWhite: true (mode logo with logoColors named; false traces the named colors only: white areas take the nearest of them, a named white is kept and enclosed transparent areas stay holes; with ignoreWhite, white areas are left out either way)}, view?: as imageTrace.setView (default: the traced object's own, else tracingResult), ids?: [id…] (instead of id)} → {id, paths, anchors, colors, warnings: [text…] (the image is over 1500 px on a side, so it traces more slowly; or it was traced scaled down; Flat Logo names only the size it traced a larger image at; or a keepColors color matches none the trace found, or nothing was traced)}", + "{id?: image or Image Trace group (default: selection), preset?: name (imageTrace.presets), savedPreset?: {settings} (a saved preset's settings, which the journal records so a replay traces with them after the preset is changed, renamed or deleted; used in place of the saved preset `preset` names), enhanced?: false (the preset's enhanced version: with Auto Grouping, and with Shapes in all but High Fidelity Photo; Flat Logo has none and is traced as it is), params?: {mode: \"blackAndWhite\"|\"grayscale\"|\"color\"|\"logo\", threshold: 0-255, palette: \"limited\"|\"fullTone\"|\"automatic\"|\"documentLibrary\" (color mode; limited: the `colors` best colours, fullTone: as many as the tones need, more with colorDetail, automatic: flat art with its own colours, else fullTone, documentLibrary: at most `colors` of the library's colours, as they are), colors: 2-256, colorDetail: 0-100 (fullTone, automatic), library: \"document\" (the document's swatches) or a swatch library id or name (swatch.library.list; documentLibrary), colorGroup?: name (color mode, documentLibrary with library \"document\": the colors of that document color group instead of every swatch; \"\" is every swatch), paths: 0-100, corners: 0-100, noise: px (an area smaller than this joins the color it shares the longest border with; when that color is far from its own and the area is 2 px or more, or a single pixel whose longest border is not tied between colors, it joins the neighboring color nearest its pixels among those sharing at least a tenth of that border), method: \"abutting\"|\"overlapping\" (abutting with fills, not mode logo: a \"Seam Fills\" group under the paths holds 1.5 px strokes along the edges two filled paths share, in the color of the lower path, so that no light hairline shows along those edges; the paths are not changed), ignoreWhite (leave out white areas), ignoreColor?: \"#rrggbb\" (with ignoreWhite: the colour left out instead of white), ignoreBackground: false (with ignoreWhite: only the areas of the ignored color that reach the image's edge are left out), keepColors?: [\"#rrggbb\"…] (not mode logo: only the palette colors within about 30 levels of a listed color are traced; empty: every color), snapCurvesToLines, shapes: false (filled outlines that are a circle, an ellipse, a rectangle or a regular polygon come out as that live shape), autoGrouping: false (each path that has others inside it is grouped with them), gradients: false (grayscale and color: an area whose colour changes evenly along one direction is one path with a linear gradient fill), fills: true, strokes: false (Create: lines up to strokeWidth wide become stroked centre lines; without fills, wider areas become stroked outlines), strokeWidth: px (10), logoColors?: [\"#rrggbb\"…] (mode logo: flat-colour logos only; colours found automatically or named here, at most ten; white is its own color, and a named color with every channel 245 or more is taken as that white, except with logoColors named, logoWhite false and no ignoreWhite; noise = areas under noise/10 px ignored; edges to a fraction of a pixel, sharp corners restored, lines and circles snapped; enclosed transparent areas are painted white while white is its own color, and stay holes with ignoreWhite or without a white of its own; fills/strokes are ignored; gradients and photos are refused, judged by the image's own colors also when logoColors are named), logoWhite: true (mode logo with logoColors named; false traces the named colors only: white areas take the nearest of them, a named white is kept and enclosed transparent areas stay holes; with ignoreWhite, white areas are left out either way)}, view?: as imageTrace.setView (default: the traced object's own, else tracingResult), ids?: [id…] (instead of id)} → {id, paths, anchors, colors, warnings: [text…] (the image is over 1500 px on a side, so it traces more slowly; or it was traced scaled down; Flat Logo names only the size it traced a larger image at; or a keepColors color matches none the trace found, or nothing was traced)}", has_selection_or_ids, |s, p| trace_make(s, p, false) ), diff --git a/crates/trace/src/params.rs b/crates/trace/src/params.rs index 156d6fbef2..7b850fc344 100644 --- a/crates/trace/src/params.rs +++ b/crates/trace/src/params.rs @@ -82,8 +82,10 @@ pub struct TraceParams { pub paths: f64, /// Corners 0–100: higher keeps more corners. pub corners: f64, - /// Noise: areas smaller than this many pixels are ignored. (Flat Logo mode: areas smaller than - /// `noise / 10` pixels of the source image.) + /// Noise: areas smaller than this many pixels are ignored: each joins the color it shares the + /// longest border with, or, when that color is far from its own, the color beside it nearest + /// its source pixels. (Flat Logo mode: areas smaller than `noise / 10` pixels of the source + /// image.) pub noise: u32, pub method: Method, /// Ignore Color: drop the areas of one colour (no background shape): white, unless @@ -244,10 +246,10 @@ pub fn preset(name: &str) -> Option { "high fidelity photo" => color(64, 50.0, 50.0, 5, Method::Overlapping), "low fidelity photo" => color(20, 50.0, 50.0, 10, Method::Overlapping), "3 colors" => color(3, 50.0, 50.0, 15, Method::Abutting), - "6 colors" => color(6, 50.0, 50.0, 15, Method::Abutting), - "16 colors" => color(16, 50.0, 50.0, 15, Method::Abutting), + "6 colors" => color(6, 50.0, 50.0, 8, Method::Abutting), + "16 colors" => color(16, 50.0, 50.0, 8, Method::Abutting), "shades of gray" => TraceParams { mode: Mode::Grayscale, colors: 8, paths: 50.0, corners: 50.0, noise: 15, method: Method::Overlapping, ..d }, - "black and white logo" => bw(128, 50.0, 75.0, 25, true), + "black and white logo" => bw(128, 50.0, 75.0, 5, true), "sketched art" => bw(150, 50.0, 50.0, 20, false), "silhouettes" => bw(200, 50.0, 50.0, 90, false), "line art" => bw(128, 50.0, 75.0, 20, false), diff --git a/crates/trace/src/pipeline.rs b/crates/trace/src/pipeline.rs index cb635e7493..0e3abbadda 100644 --- a/crates/trace/src/pipeline.rs +++ b/crates/trace/src/pipeline.rs @@ -270,7 +270,10 @@ fn trace_paths(img: &Raster, params: &TraceParams, max_anchors: usize, steps: &( drop(grays); // Noise clears specks, not the regions of a rare colour Automatic kept on purpose. let keep: Vec = q.palette.iter().map(|c| rare.contains(c)).collect(); - quantize::denoise_keeping(&mut q.labels, w, h, params.noise as usize, &keep); + // 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) }; + quantize::denoise_nearest(&mut q.labels, w, h, params.noise as usize, &keep, Some(&source)); 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 diff --git a/crates/trace/src/quantize.rs b/crates/trace/src/quantize.rs index a54fdfacc7..ea8fa00ac8 100644 --- a/crates/trace/src/quantize.rs +++ b/crates/trace/src/quantize.rs @@ -11,7 +11,9 @@ mod rare; use cut_edge::{choosing_pixels, compact, settled, settled_wide, settles_wide}; pub(crate) use cut_edge::{continuous_tone, settle_cut_edges}; pub use denoise::denoise; +#[cfg(test)] pub(crate) use denoise::denoise_keeping; +pub(crate) use denoise::{Source, denoise_nearest}; pub use palette::FULL_TONE_MAX; use palette::{Bin, HistKey, TracedAs, Used, color_palette, flat_count, flat_palette, kmeans_1d, labs, nearest_1d, nearest_lab, nearest_library}; #[cfg(test)] @@ -296,6 +298,11 @@ fn oklab(c: [f64; 3]) -> [f64; 3] { ] } +/// [`oklab`] of each palette color. +pub(crate) fn palette_labs(palette: &[[u8; 3]]) -> Vec<[f64; 3]> { + palette.iter().map(|c| oklab(c.map(f64::from))).collect() +} + /// [`oklab`] of an sRGB colour, for the tests. #[cfg(test)] pub(crate) fn tests_oklab(c: [u8; 3]) -> [f64; 3] { diff --git a/crates/trace/src/quantize/denoise.rs b/crates/trace/src/quantize/denoise.rs index a929691466..5bc237926a 100644 --- a/crates/trace/src/quantize/denoise.rs +++ b/crates/trace/src/quantize/denoise.rs @@ -8,16 +8,49 @@ pub fn denoise(labels: &mut [u16], w: usize, h: usize, min_area: usize) { denoise_keeping(labels, w, h, min_area, &[]); } +/// How far apart (Oklab × 100) a small region's color and the color it shares the longest +/// border with must be before it merges into the color beside it that is nearest its source +/// pixels instead (among those with at least [`NEAREST_BORDER`] of the longest border): a dark +/// speck on the edge of a light area joins the dark outline beside it, not the light area. +pub(crate) const NEAREST_APART: f64 = 10.0; + +/// See [`NEAREST_APART`]. +pub(crate) const NEAREST_BORDER: f64 = 0.1; + +/// The fewest pixels a region needs for [`NEAREST_APART`] to apply when two or more colors tie +/// for its longest border. Such a lone pixel merges by border alone: on an anti-aliased corner it +/// holds a mix of the colors on both sides, and joining the nearer one can cut the corner off a +/// shape. +pub(crate) const NEAREST_PIXELS: u32 = 2; + +/// The colors [`denoise_nearest`] compares a small region with. +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. + pub rgba: &'a [u8], + /// Whether the labels are grays (Grayscale, Black and White): the source pixels are compared as their gray. + pub gray: bool, +} + /// [`denoise`], except that a region of a label `keep` marks (a rare colour kept on purpose, see /// [`rare_colours`]) that is at least [`RARE_ISLAND`] pixels stays whatever `min_area` is: Noise /// clears specks, not a colour's only regions. Smaller regions of it go as any other. pub(crate) fn denoise_keeping(labels: &mut [u16], w: usize, h: usize, min_area: usize, keep: &[bool]) { + denoise_nearest(labels, w, h, min_area, keep, None); +} + +/// [`denoise_keeping`]; with `source`, a region below `min_area` whose color is at least +/// [`NEAREST_APART`] from the color it shares the longest border with merges into the color +/// beside it nearest its source pixels, averaged, among those sharing at least [`NEAREST_BORDER`] +/// of the longest border ([`NEAREST_PIXELS`] limits this for a single pixel). Every region below +/// `min_area` still goes. +pub(crate) fn denoise_nearest(labels: &mut [u16], w: usize, h: usize, min_area: usize, keep: &[bool], source: Option<&Source>) { use std::cmp::Reverse; use std::collections::BinaryHeap; // Every index below is a `u32`, so a pixel costs 8 bytes and a region 18 instead of a `Vec` per region. An image // of `u32::MAX` pixels or more (far above the decoder's cap) is left as it is. - const NONE: u32 = u32::MAX; if min_area <= 1 || w == 0 || h == 0 || w.checked_mul(h) != Some(labels.len()) || labels.len() >= NONE as usize { return; } @@ -66,14 +99,8 @@ pub(crate) fn denoise_keeping(labels: &mut [u16], w: usize, h: usize, min_area: colour.push(label); parent.push(id); } - fn find(parent: &mut [u32], mut a: u32) -> u32 { - while parent[a as usize] != a { - let up = parent[parent[a as usize] as usize]; - parent[a as usize] = up; - a = up; - } - a - } + // Only when it can be read: the labels' colors and the source pixels must cover every label and every pixel. + let source = source.filter(|s| colour.iter().all(|&c| (c as usize) < s.labs.len()) && labels.len().checked_mul(4) == Some(s.rgba.len())); // Only regions below `min_area` are ever taken from the queue; a larger one ends the loop. let mut pending: BinaryHeap<_> = (0..size.len() as u32).filter(|&id| (size[id as usize] as usize) < min_area).map(|id| Reverse((size[id as usize], id))).collect(); @@ -110,9 +137,24 @@ pub(crate) fn denoise_keeping(labels: &mut [u16], w: usize, h: usize, min_area: by_colour.clear(); by_colour.extend(borders.iter().map(|&(other, border)| (colour[other as usize], border))); merge_counts(&mut by_colour); - let Some(&(chosen, _)) = by_colour.iter().max_by_key(|(label, border)| (*border, Reverse(*label))) else { + let Some(&(mut chosen, longest)) = by_colour.iter().max_by_key(|(label, border)| (*border, Reverse(*label))) else { continue; // an opaque island surrounded only by transparency stays intact }; + if let Some(source) = source + && (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 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() + .filter(|&&(_, border)| border as f64 >= NEAREST_BORDER * longest as f64) + .min_by(|a, b| to(a.0).total_cmp(&to(b.0)).then(b.1.cmp(&a.1))) + { + chosen = nearest; + } + } // All adjacent regions of the chosen colour become connected by this merge. Absorb // them together, keeping the largest one as the survivor. let Some(target) = borders @@ -143,6 +185,44 @@ pub(crate) fn denoise_keeping(labels: &mut [u16], w: usize, h: usize, min_area: } } +/// The end of a pixel list, and the region of a transparent pixel. +const NONE: u32 = u32::MAX; + +/// The root of `a` in the union-find `parent`, halving the paths on the way. +fn find(parent: &mut [u32], mut a: u32) -> u32 { + while parent[a as usize] != a { + let up = parent[parent[a as usize] as usize]; + parent[a as usize] = up; + a = up; + } + 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); + 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); + } + n += 1.0; + } + 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]; + } + super::oklab(mean) +} + +/// The distance between two colors in Oklab × 100; infinite when `q` is missing. +fn distance(p: &[f64; 3], q: Option<&[f64; 3]>) -> f64 { + q.map_or(f64::INFINITY, |q| (0..3).map(|k| (p[k] - q[k]).powi(2)).sum::().sqrt()) +} + /// Sort `(key, count)` pairs and add up the counts of equal keys. fn merge_counts(pairs: &mut Vec<(K, usize)>) { pairs.sort_unstable_by_key(|p| p.0); @@ -285,6 +365,157 @@ mod tests { } } + /// A light ground, a dark ink, a tone barely darker than the ground and a dark brown close to the ink. + const GROUND_INK_TONE_BROWN: [[u8; 3]; 4] = [[235, 225, 210], [30, 25, 20], [228, 218, 203], [50, 42, 35]]; + + /// `labels` denoised at `min_area`, reading the source image that paints each label its palette color. + fn denoised_nearest(labels: &[u16], w: usize, h: usize, min_area: usize) -> Vec { + let labs = crate::quantize::palette_labs(&GROUND_INK_TONE_BROWN); + let rgba: Vec = + 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 })); + out + } + + /// A ground with a dark line two pixels wide down column 12 and 13, and a 2 × 2 speck of `speck` at (10, 10) that + /// touches the line on one side and the ground on three. + fn speck_beside_a_line(speck: u16) -> (Vec, usize, usize) { + let (w, h) = (30, 30); + let mut labels = vec![0u16; w * h]; + for y in 0..h { + labels[y * w + 12] = 1; + labels[y * w + 13] = 1; + } + for (x, y) in SPECK { + labels[y * w + x] = speck; + } + (labels, w, h) + } + + const SPECK: [(usize, usize); 4] = [(10, 10), (11, 10), (10, 11), (11, 11)]; + + /// A dark speck mostly on the light ground but touching a dark line joins the line, the color nearest it, not the + /// ground it shares more border with. + #[test] + fn a_dark_speck_beside_a_dark_line_joins_the_line() { + let (labels, w, h) = speck_beside_a_line(3); + let out = denoised_nearest(&labels, w, h, 15); + for (x, y) in SPECK { + assert_eq!(out[y * w + x], 1, "({x}, {y})"); + } + let mut blind = labels.clone(); + denoise_keeping(&mut blind, w, h, 15, &[]); + assert_eq!(blind[10 * w + 10], 0, "by border alone it joins the ground"); + } + + /// A speck whose color is close to the color it shares the longest border with joins that color, as before, + /// even though another color touches it. + #[test] + fn a_speck_close_to_its_longest_border_color_joins_it() { + let (labels, w, h) = speck_beside_a_line(2); + let out = denoised_nearest(&labels, w, h, 15); + let mut blind = labels.clone(); + denoise_keeping(&mut blind, w, h, 15, &[]); + assert_eq!(out, blind); + assert_eq!(out[10 * w + 10], 0); + } + + /// A color that touches the speck along less than a tenth of its longest border is not chosen, however near. + #[test] + fn a_color_with_a_short_border_is_not_chosen() { + let (w, h) = (30, 30); + let mut labels = vec![0u16; w * h]; + // A 3 × 3 speck of brown in the ground, and a line of ink 16 pixels long (above Noise) that touches the speck at + // one pixel edge. + for y in 10..13 { + for x in 10..13 { + labels[y * w + x] = 3; + } + } + for x in 13..29 { + labels[10 * w + x] = 1; + } + // The brown speck has 11 pixel edges against the ground and 1 against the ink: 1 < 0.1 × 11. + let out = denoised_nearest(&labels, w, h, 15); + assert_eq!(out[11 * w + 11], 0); + assert!(out[10 * w + 13..10 * w + 29].iter().all(|&l| l == 1), "the line stays"); + } + + /// A lone pixel tied between two colors for its longest border merges by border alone, even when the other + /// color is nearer its source pixel. + #[test] + fn a_lone_pixel_tied_between_colors_merges_by_border() { + let (w, h) = (20, 20); + // Ground in the top-left, ink everywhere else, and one brown pixel at the corner with two edges on each. + let mut labels: Vec = (0..w * h).map(|i| u16::from(i % w >= 11 || i / w >= 11)).collect(); + labels[10 * w + 10] = 3; + let out = denoised_nearest(&labels, w, h, 5); + let mut blind = labels.clone(); + denoise_keeping(&mut blind, w, h, 5, &[]); + assert_eq!(out, blind); + assert_eq!(out[10 * w + 10], 0); + // With the ground one column wider the pixel has three edges on the ground and one on the ink: no longer tied, + // it joins the ink, the color nearest it. + for y in 0..11 { + labels[y * w + 11] = 0; + } + let out = denoised_nearest(&labels, w, h, 5); + assert_eq!(out[10 * w + 10], 1); + } + + /// Every region below Noise still goes: the nearest-color merge only changes where it goes. + #[test] + fn noise_still_removes_every_region_below_it() { + let (w, h) = (60, 50); + for seed in 1..6 { + let labels = noisy(w, h, 4, 0, seed); + for min_area in [2, 3, 5, 8, 15, 25] { + let out = denoised_nearest(&labels, w, h, min_area); + let mut seen = vec![false; w * h]; + for start in 0..w * h { + if seen[start] { + continue; + } + seen[start] = true; + let mut stack = vec![start]; + let mut count = 0; + while let Some(i) = stack.pop() { + count += 1; + let (x, y) = (i % w, i / w); + for j in [(x > 0).then(|| i - 1), (x + 1 < w).then(|| i + 1), (y > 0).then(|| i - w), (y + 1 < h).then(|| i + w)] + .into_iter() + .flatten() + { + if !seen[j] && out[j] == out[i] { + seen[j] = true; + stack.push(j); + } + } + } + assert!(count >= min_area, "seed {seed}, Noise {min_area}: a region of {count} pixels at {start}"); + } + } + } + } + + /// Without colors or a source image for every label and pixel, every region merges as it always did. + #[test] + fn a_source_that_does_not_cover_the_labels_is_not_read() { + let (labels, w, h) = speck_beside_a_line(3); + 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]; + let mut out = labels.clone(); + denoise_nearest(&mut out, w, h, 15, &[], Some(&Source { labs: &labs, rgba: &rgba, 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 })); + assert_eq!(out, blind); + } + /// A minimum so large that regions keep merging into big ones. #[test] fn compact_denoise_matches_the_reference_with_a_large_min_area() { diff --git a/crates/trace/src/tests.rs b/crates/trace/src/tests.rs index 85e9d09778..cc7b1e323c 100644 --- a/crates/trace/src/tests.rs +++ b/crates/trace/src/tests.rs @@ -899,10 +899,10 @@ fn presets_have_their_paths_corners_and_noise() { ("High Fidelity Photo", 50.0, 50.0, 5), ("Low Fidelity Photo", 50.0, 50.0, 10), ("3 Colors", 50.0, 50.0, 15), - ("6 Colors", 50.0, 50.0, 15), - ("16 Colors", 50.0, 50.0, 15), + ("6 Colors", 50.0, 50.0, 8), + ("16 Colors", 50.0, 50.0, 8), ("Shades of Gray", 50.0, 50.0, 15), - ("Black and White Logo", 50.0, 75.0, 25), + ("Black and White Logo", 50.0, 75.0, 5), ("Sketched Art", 50.0, 50.0, 20), ("Silhouettes", 50.0, 50.0, 90), ("Line Art", 50.0, 75.0, 20), @@ -1928,7 +1928,9 @@ fn contour_spread(res: &TraceResult) -> (f64, usize) { #[test] fn the_borders_of_a_gradients_bands_follow_its_contours() { - let res = trace(&wavy_bands(), &sixteen_colors()); + // At the Noise this test was written for: at 16 Colors' Noise 8 the 6 levels of noise leave more + // small regions along the borders. + let res = trace(&wavy_bands(), &TraceParams { noise: 15, ..sixteen_colors() }); // The square's sides are edges: it keeps its four corners. let [l, t, r, b] = WAVY_SQUARE; let square = res.paths.iter().find(|p| p.color.iter().all(|&c| c < 40)).expect("the dark square"); diff --git a/docs/image-trace-adjustments.md b/docs/image-trace-adjustments.md index 22557771e2..2bd58e4df4 100644 --- a/docs/image-trace-adjustments.md +++ b/docs/image-trace-adjustments.md @@ -20,9 +20,9 @@ its current location. Opening the controls does not add a document undo step. The built-in presets other than Auto Color and Flat Logo all use Paths 50, and so do their enhanced versions. Among those presets, Corners is 100 in Technical Drawing, 75 in Default, Black and White -Logo, Line Art and Outline, and 50 in the other eight. Noise is 1 px in Technical Drawing, 5 in High Fidelity Photo, 10 in Low Fidelity Photo, 15 in 3 Colors, -6 Colors, 16 Colors and Shades of Gray, 20 in Sketched Art, Line Art and Outline, 25 in Default and -Black and White Logo, and 90 in Silhouettes (see +Logo, Line Art and Outline, and 50 in the other eight. Noise is 1 px in Technical Drawing, 5 in High Fidelity Photo and Black and White Logo, 8 in 6 Colors +and 16 Colors, 10 in Low Fidelity Photo, 15 in 3 Colors and Shades of Gray, 20 in Sketched Art, Line +Art and Outline, 25 in Default, and 90 in Silhouettes (see [image-trace-quality.md](image-trace-quality.md#preset-paths-corners-and-noise)). Sliders apply on release, and their numeric fields can be edited directly. Each committed diff --git a/docs/image-trace-quality.md b/docs/image-trace-quality.md index dacf436036..228c1ef9f8 100644 --- a/docs/image-trace-quality.md +++ b/docs/image-trace-quality.md @@ -237,10 +237,10 @@ Every built-in preset other than Auto Color and Flat Logo traces at Paths 50. Co | High Fidelity Photo | 50 | 50 | 5 | 90, 25, 4 | | Low Fidelity Photo | 50 | 50 | 10 | 60, 50, 12 | | 3 Colors | 50 | 50 | 15 | 60, 60, 20 | -| 6 Colors | 50 | 50 | 15 | 65, 60, 16 | -| 16 Colors | 50 | 50 | 15 | 70, 55, 10 | +| 6 Colors | 50 | 50 | 8 | 65, 60, 16 | +| 16 Colors | 50 | 50 | 8 | 70, 55, 10 | | Shades of Gray | 50 | 50 | 15 | 60, 50, 10 | -| Black and White Logo | 50 | 75 | 25 | 95, 80, 8 | +| Black and White Logo | 50 | 75 | 5 | 95, 80, 8 | | Sketched Art | 50 | 50 | 20 | 50, 50, 100 | | Silhouettes | 50 | 50 | 90 | 40, 60, 30 | | Line Art | 50 | 75 | 20 | 80, 70, 10 | @@ -248,7 +248,9 @@ Every built-in preset other than Auto Color and Flat Logo traces at Paths 50. Co | Outline | 50 | 75 | 20 | 80, 70, 10 | The palettes, color counts, thresholds and other options of the presets did not change. The enhanced -versions of these presets use the same Paths, Corners and Noise. +versions of these presets use the same Paths, Corners and Noise. 6 Colors and 16 Colors were at Noise 15 +and Black and White Logo at 25 until [Noise joins the nearest color](#noise-joins-the-nearest-color); +the measurements below were made at those values. Measured with the harness below (section 1, each preset on the inputs it is meant for), before and after: anchors fall in 10 of the 12 changed presets, for example 5,494 to 3,136 (High Fidelity Photo), 33 to 27 @@ -309,6 +311,38 @@ identical settings on unchanged current source and the candidate, including sepa crops and white-composited PNG comparisons. These local fixtures are not repository assets. The parity score is unchanged. +## Noise joins the nearest color + +A region below Noise used to join the color it shares the longest border with. Along a thin dark +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 +VectorCraft's own. 6 Colors and 16 Colors move from Noise 15 to 8, and Black and White Logo from 25 to 5. + +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: + +| preset | three illustrations with fine dark lines | four other illustrations | synthetic shapes and photos | +|:--|:--|:--|:--| +| 16 Colors | 8.15 / 10.80 / 8.69 → 5.82 / 6.25 / 6.01; anchors +72% to +99% | −1.8% to −11.9%; anchors +20% to +39% | within 0.08 of a level; anchors 0% to +37% | +| 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 +`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. + ## Pieces returning to a junction An outline piece can return to its starting junction while being fitted as an open chain. At loose Corners/Paths settings, a nonzero positioned boundary could collapse to one cubic and lose a small diff --git a/docs/mcp.md b/docs/mcp.md index a987c40c85..57c349d335 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -3847,6 +3847,12 @@ already walks Image Trace. Short agent entry points: `paths` and `anchors` count the traced paths only. With `keepColors` in `params`, the reply's `warnings` name each listed color that no traced color matches, and say 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 + 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` checks them; without `name` it is a new "Tracing Preset N", and `newName` renames a saved preset (the reply's diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index 74ce58c5d6..85d4412e45 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -1541,6 +1541,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur - **Image Trace, cut-out edges choose no color:** in Color and Grayscale, the edge pixels that take the color beside them, and the semi-transparent pixels within two pixels of them whose color is also the opaque art's color blended with the background, do not count when the palette or the grays are chosen, so the edge of a cut-out image no longer adds a second shade of a color to Auto Color's palette, takes away an outline's color or takes a gray of its own; beside those pixels, the anti-aliasing between a color and an outline drawn along the edge is no color of Auto Color's own when it lies at least 0.45 of the way from the color to the outline and at least one in 32 of its pixels has both around it (edge pixels that choose no color count when they lie within 2.5 Oklab × 100 of the nearest of those colors): its pixels trace as the outline, so a thin outline stays a ring of few pieces, except beside an outline along the edge with a region of 8 pixels of its own, where each takes the nearer of the two, so a wider outline is not drawn wider; an outline over the edge in the color of other art traces in that color, while an opaque rim drawn along the edge keeps its color, also where other art reaches it; a thin semi-transparent outline of another color still counts, and opaque images trace as before; code-drawn cut-out outline, rim, opaque line and grayscale edge tests in `crates/trace`. - **Image Trace, blurred edges:** in Color with a Limited palette of at most 16 colors (3 Colors, 6 Colors, 16 Colors) and in Grayscale with at most 16 grays (Shades of Gray), by VectorCraft's own rule (RGB distances, 0 to 255 a channel), a blend pixel with no unblended pixel of its own color within 4 pixels takes the nearer side of the pair of colors within 8 pixels that it is best a blend of, of the 8 colors within 8 pixels nearest to its color, when that pair explains it twice as well as its own color, or when it lies between the two and off the line between them by less than 0.15 of their distance apart; it keeps its color when it is one of the pair. Grayscale compares lightness. The in-between tones of an edge blurred over several pixels no longer trace as rings around it. High Fidelity Photo, Low Fidelity Photo and Black and White trace as before, and so do Automatic, Full Tone and Document Library palettes. Noisy and textured images settle more of their pixels. By `crates/trace/tests/quality` (256 px variants), 16 Colors on blurred flat art went from 9.0 paths to 3.0, 158 anchors to 34 and 0.239 px off to 0.069, and 6 Colors from 6.0 paths to 3.0 and 0.226 px off to 0.071; askew shapes take up to 11 px more area in colors the art does not have, and with 16 Colors the largest distance from a thin line's outline to the trace goes from 23.5 px to 45.3 (see [quality notes](image-trace-quality.md#blurred-edges)). Code-drawn blurred-disc and grayscale ramp tests in `crates/trace`. - **Image Trace, preset Paths, Corners and Noise:** every built-in preset but Auto Color and Flat Logo traces at Paths 50, and so do their enhanced versions; among those presets, Corners is 100 in Technical Drawing, 75 in Default, Black and White Logo, Line Art and Outline and 50 in the other eight, and Noise is 1 to 90 px by preset. Palettes and color counts are unchanged. Figures for these presets in the other Image Trace entries were measured at the earlier values. By `crates/trace/tests/quality`, High Fidelity Photo goes from 5,494 anchors to 3,136 and Black and White Logo from 27 to 14, while 16 Colors on flat art goes from 0.100 px off to 0.123 (see [quality notes](image-trace-quality.md#preset-paths-corners-and-noise)). Test `presets_have_their_paths_corners_and_noise` in `crates/trace`. +- **Image Trace, Noise joins the nearest color:** in Color, Grayscale and Black and White, a region below Noise whose longest-border color is 10 or more apart from its own (Oklab × 100) joins the neighboring color nearest its source pixels, among those sharing at least a tenth of that border, so thin dark lines no longer break into dashes; a single pixel tied between colors joins by border (VectorCraft's own rule and values). 6 Colors and 16 Colors use Noise 8 and Black and White Logo Noise 5. On three illustrations with fine dark lines the mean difference falls 29% to 42% (16 Colors) with 72% to 99% more anchors (see [quality notes](image-trace-quality.md#noise-joins-the-nearest-color)). Tests in `crates/trace/src/quantize/denoise.rs`. Scores and hours unchanged. - **Image Trace, simpler contour walk:** `contour::trace_mask` works out each turn from the pixels during the walk, with no precomputed direction table and no assertion; identical loops to the previous walk on every 4 × 4 mask (see [quality notes](image-trace-quality.md#a-simpler-contour-walk)). - **Image Trace, measured:** `crates/trace/tests/quality` traces drawings made from known vector art with every preset and [`image-trace-quality.md`](image-trace-quality.md) records the numbers against the baseline. Black and White, Grayscale and Color place each edge by the blend of the pixels across it (to a fraction of a pixel on anti-aliased and blurred images; hard-edged images are read as staircases) and fit outlines as Flat Logo does. *Measured* on each preset's own inputs, all sizes and variants: Default's outlines went from 0.31 px off on average to 0.09 (largest 1.51 to 1.02), Black and White Logo's from 0.27 to 0.06 with 0.3 kinks per trace where it had 15, and the gaps between abutting paths from 64 px to 22. On clean curved shapes Black and White Logo is 0.02 px off (the open tracer measured beside it: 0.20). The palette is made of the picture's own colours (blends along edges take none of it, colours that noise made of one colour merge, a blend pixel goes to one of the two colours it lies between; #1504): 16 Colors on flat art, all variants, went from 70 paths to 4.2 and from 5.1 px off to 0.13, with no path left in a colour the art doesn't have (5.9 before). Photos are smoothed within their tones before they are quantised and stacked layers go by lightness (#1516): High Fidelity Photo went from 3,653 paths to 121 with its colour error down from 2.30 to 1.75, and a 16 megapixel photo-like image is traced in 3.5 s where 4 megapixels were refused. Since then, by the same harness: outlines are cut at their junctions and each shared piece is fitted once (#1535: gaps and overlaps between abutting paths 22 to 50 px of area down to under 2); layers are traced within the box of their pixels and a few at a time (#1546: a 16 megapixel photo 3.9 GB to 0.9 GB); black and white are read against the image's own black and the paper near each edge (#1563, for thin lines in blurred and scanned art); Ignore Color and the Auto Color and Outline presets (#1574); Snap Curves To Lines levels lines within 2.5 degrees of level or upright (#1593, the angle our choice); Create: Strokes puts centre lines in the middle of their ink and makes them as wide as it (#1595: Outline 0.295 to 0.052 px from the true centre line, 18.4 to 2.7 anchors a stroke); sharp corners whose pixel is mostly outside the shape stay corners (#1609: Default 69% to 76% kept). Three options, each off unless asked for as the legacy presets have them: Shapes (#1609: 57 to 62% of plain circles, ellipses, rectangles and regular polygons come out as live shapes, rounded rectangles rarely), Auto Grouping (#1618: paths grouped with what lies inside them, the same picture) and Gradients (#1627: linear gradients traced as one gradient-filled path; on gradient inputs 6 Colors' colour error 2.12 to 0.62 with 6.4 to 3.4 paths, none found in flat art). Snap Curves To Lines moves no end of a line by more than 1.6 px, so long lines a degree or two off stay as the image has them (#1647: Technical Drawing's largest distance 4.21 px to 1.46). Flat Logo takes the tones a scan's uneven paper makes of one colour for one colour (#1657: on scans 470 paths and 21 px off to 8.5 paths and 0.09 px). Shapes finds rounded rectangles and shapes in aliased, blurred and scanned art (#1845: 57 to 62% of the plain shapes up to 98 to 100%, no false shape but one line 2 px wide). Stroked lines get square caps where their ink is cut square, and a wide end that turned towards a corner goes back on its line (#1846: Outline's largest distance 2.94 px to 0.88, 14 anchors to 10). Each of the 14 documented presets has an enhanced version with Auto Grouping and, in all but High Fidelity Photo, Shapes (#1847), measured beside its preset: equal or better within 0.005 px and 1% of edge ΔE on every preset's own inputs (Default 0.091 to 0.083 px); Auto Grouping keeps traced order in its groups, which removed a light seam along the counters of letters. Gradients grows a gradient over the pixels noise and soft rims took off it (#1848: blurred and noisy linear gradients 3 to 11 paths beside the gradient down to 0.5 with 6 Colors, 24 to 56 down to 0.5 to 1.5 with High Fidelity Photo). Silhouettes is 0.52 px off because its threshold is 200 (0.11 px at 128). Still weak, by the same numbers: rounded rectangles and other line-and-arc outlines take 12 to 16 anchors (the fitter does not split lines from arcs), blurred and scanned polygons lose corners, hairlines under half a pixel are lost to the threshold, aliased art is about 0.25 px off, a scan's uneven paper splits into tones when more colours are asked for than the art has, stroke junctions stay at their skeleton pixels, a three-stop gradient in a small or scanned disc is not found, radial gradients stay bands (a draft, #1849, traces them; the documented option is for linear ones, so it waits for a decision), near-regular polygons a few dozen pixels across are made regular with Shapes (up to 0.5 px off), and aliased stroked lines are 0.8 px off at worst. - **Gradient mesh from an image:** Object › Create Gradient Mesh (`object.mesh.create`) on an embedded image makes a mesh of 1 to 50 rows and columns over the picture's rectangle, placed with the image's transform; each point takes the alpha-weighted average colour and the opacity of the area around it, and Appearance and Highlight apply. A linked image is refused with a message to embed it (#1676). Scores and hours unchanged.