mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-17 22:33:08 +00:00
Image Trace: High Fidelity Photo traces in Full Tone with abutting shapes
High Fidelity Photo traced with a Limited palette of 64 colors as overlapping shapes. It now traces with Palette Full Tone at Color detail 85 and Method Abutting; Paths, Corners and Noise stay 50, 50 and 5. These are VectorCraft's own values. Palette Full Tone splits its colors until none is wider than its limit, then, while it has fewer colors than Color detail's value, splits the narrower ones too, where the image has that many. Its colors then merge as one only when they look alike: thoroughly mixed and less than 5 apart in Oklab x 100, or mixed in patches (compression blocks) and less than 1.5 apart. This applies to every Full Tone trace, so an existing Full Tone trace or saved preset traces differently when traced again. Automatic's continuous-tone colors keep the earlier count and the RGB merge. On 19 photos, cartoons and synthetic shapes, the mean pixel difference summed over the inputs falls from 80.5 to 65.6 levels: by more than 1% on 14 inputs and by less than 1% either way on the other 5. Anchors rise 1.66 times, the SVG size 1.31 times and the trace time 1.9 times; flat synthetic art costs the most (12 to 400 anchors on one image). Low Fidelity Photo, 16 Colors, 6 Colors, Black and White Logo and Auto Color give byte-identical results. Supersedes #2699 by andeecollard, which moved the photo presets to Full Tone.
This commit is contained in:
@@ -144,7 +144,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"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 (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\" clip group under the paths holds strokes along the edges two filled paths share, 1.5 image pixels wide (VectorCraft's own width), in the color of the lower path (on an image stretched more one way than another, each stroke's outline in image pixels as a filled path), clipped to the image's box within the traced paths' box; paths that became live shapes get none; 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, split into at least colorDetail colors where the image has that many before colors that look alike merge (less than 5 apart in Oklab × 100 and thoroughly mixed, or less than 1.5 and mixed in patches), automatic: flat art with its own colours, else as many as the tones need without fullTone's least count, merging close RGB colors, 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\" clip group under the paths holds strokes along the edges two filled paths share, 1.5 image pixels wide (VectorCraft's own width), in the color of the lower path (on an image stretched more one way than another, each stroke's outline in image pixels as a filled path), clipped to the image's box within the traced paths' box; paths that became live shapes get none; 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)
|
||||
),
|
||||
|
||||
@@ -38,11 +38,12 @@ pub enum Palette {
|
||||
#[serde(alias = "Limited")]
|
||||
Limited,
|
||||
/// As many colours as the image's tones need, up to [`FULL_TONE_MAX`] (photos, gradients):
|
||||
/// `color_detail` sets how close two colours may be and stay apart.
|
||||
/// `color_detail` sets how close two colours may be and stay apart. Before merging colors that
|
||||
/// look alike, it splits to at least one color per step of `color_detail` where the image has that many.
|
||||
#[serde(alias = "Full Tone")]
|
||||
FullTone,
|
||||
/// Flat art (a few colours cover nearly all of it) traces with exactly those colours, anything
|
||||
/// else as Full Tone.
|
||||
/// else with as many as its tones need, without Full Tone's least count and merging close RGB colors.
|
||||
#[serde(alias = "Automatic")]
|
||||
Automatic,
|
||||
/// The most used colours of a swatch library ([`TraceParams::swatches`]), at most `colors` of
|
||||
@@ -243,7 +244,10 @@ pub fn preset(name: &str) -> Option<TraceParams> {
|
||||
// many as its tones need where it is not.
|
||||
// (Color detail 85: a colour on a hundredth of the picture is still one of its colours.)
|
||||
"auto color" => TraceParams { palette: Palette::Automatic, color_detail: 85.0, ..color(6, 65.0, 60.0, 16, Method::Abutting) },
|
||||
"high fidelity photo" => color(64, 50.0, 50.0, 5, Method::Overlapping),
|
||||
// A photo's tones: as many colors as they need (Full Tone), split until no color spans more
|
||||
// than about 21 levels of a channel and into at least 85 (Color detail 85) before colors that
|
||||
// look alike merge, as abutting shapes. VectorCraft's own values.
|
||||
"high fidelity photo" => TraceParams { palette: Palette::FullTone, color_detail: 85.0, ..color(64, 50.0, 50.0, 5, Method::Abutting) },
|
||||
"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, 8, Method::Abutting),
|
||||
|
||||
@@ -93,7 +93,7 @@ pub(crate) fn quantize_keeping(img: &Raster, params: &TraceParams) -> (Quantized
|
||||
let g = l.round().clamp(0.0, 255.0) as u8;
|
||||
[g, g, g]
|
||||
});
|
||||
let q = merge_mixed(compact(labels, palette.collect()), img.width as usize, img.height as usize, &[]);
|
||||
let q = merge_mixed(compact(labels, palette.collect()), img.width as usize, img.height as usize, &[], Alike::Rgb);
|
||||
(if settles_wide(params) { settled_wide(q, img, &own, true) } else { settled(q, img, &own) }, vec![])
|
||||
}
|
||||
// Logo mode traces without quantising; asked directly, it quantises like Color.
|
||||
@@ -175,12 +175,14 @@ pub(crate) fn quantize_keeping(img: &Raster, params: &TraceParams) -> (Quantized
|
||||
img.rgba.as_chunks::<4>().0.iter().map(|p| if p[3] < 128 { TRANSPARENT } else { lut[hk.key(p[0], p[1], p[2])] }).collect();
|
||||
let palette = centers.iter().map(|c| [c[0].round() as u8, c[1].round() as u8, c[2].round() as u8]).collect();
|
||||
let q = compact(labels, palette);
|
||||
// A swatch library's colours are used as they are.
|
||||
// A swatch library's colours are used as they are. Full Tone's colors merge only when
|
||||
// they look alike (see `Alike`).
|
||||
if library {
|
||||
return (q, false);
|
||||
}
|
||||
let alike = if params.palette == Palette::FullTone { Alike::Seen } else { Alike::Rgb };
|
||||
let before = q.palette.len();
|
||||
let q = merge_mixed(q, img.width as usize, img.height as usize, &rare);
|
||||
let q = merge_mixed(q, img.width as usize, img.height as usize, &rare, alike);
|
||||
let merged = q.palette.len() < before;
|
||||
(q, merged)
|
||||
};
|
||||
@@ -309,6 +311,27 @@ pub(crate) fn tests_oklab(c: [u8; 3]) -> [f64; 3] {
|
||||
oklab(c.map(f64::from))
|
||||
}
|
||||
|
||||
/// Which colors [`merge_mixed`] takes for one color that noise split.
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||
enum Alike {
|
||||
/// Close in RGB: under 30 levels apart and thoroughly mixed, or under 12 and mixed in patches.
|
||||
/// A limited palette's colors are few and far apart, so two this close are one color.
|
||||
Rgb,
|
||||
/// Close to the eye: under [`FULL_TONE_ALIKE`] apart in Oklab and thoroughly mixed, or under
|
||||
/// [`FULL_TONE_PATCHES`] and mixed in patches (compression blocks). Full Tone's neighboring tones
|
||||
/// of a smooth photo are about as close in RGB as noise makes one color, and mixed along their
|
||||
/// soft border, so the RGB test would take most of them for one color.
|
||||
Seen,
|
||||
}
|
||||
|
||||
/// How far apart (Oklab × 100) two thoroughly mixed Full Tone colors may lie and still be merged
|
||||
/// as one color that noise split: a little more than the least difference one sees (see [`oklab`]).
|
||||
const FULL_TONE_ALIKE: f64 = 5.0;
|
||||
|
||||
/// How far apart (Oklab × 100) two Full Tone colors mixed in patches may lie and still be merged:
|
||||
/// the blocks of one flat color that compression shifted by a few levels each.
|
||||
const FULL_TONE_PATCHES: f64 = 1.5;
|
||||
|
||||
/// `q` with the colours that noise made of one colour merged, largest mix first.
|
||||
///
|
||||
/// Asked for more colours than a picture has, k-means splits a flat colour along its noise, and
|
||||
@@ -317,8 +340,9 @@ pub(crate) fn tests_oklab(c: [u8; 3]) -> [f64; 3] {
|
||||
/// taken in RGB) and thoroughly mixed: the pixels of the lesser one have, on average, more than
|
||||
/// half a neighbour of the other each; or they are closer still (under 12 levels) and mixed in
|
||||
/// patches (compression blocks). The bands of a gradient are close too, but each keeps to itself
|
||||
/// and they stay apart. The colours in `keep` merge with none.
|
||||
fn merge_mixed(q: Quantized, w: usize, h: usize, keep: &[[u8; 3]]) -> Quantized {
|
||||
/// and they stay apart. The colours in `keep` merge with none. With [`Alike::Seen`], the distances are
|
||||
/// taken in Oklab (see [`FULL_TONE_ALIKE`] and [`FULL_TONE_PATCHES`]).
|
||||
fn merge_mixed(q: Quantized, w: usize, h: usize, keep: &[[u8; 3]], alike: Alike) -> Quantized {
|
||||
let k = q.palette.len();
|
||||
// A rare colour kept on purpose (see `rare_colours`) is distinct by choice: it merges with none.
|
||||
let kept: Vec<bool> = q.palette.iter().map(|c| keep.contains(c)).collect();
|
||||
@@ -350,8 +374,18 @@ fn merge_mixed(q: Quantized, w: usize, h: usize, keep: &[[u8; 3]]) -> Quantized
|
||||
continue;
|
||||
}
|
||||
let mixed = touch[a * k + b] / count[a].min(count[b]);
|
||||
let apart = (0..3).map(|c| (color[a][c] - color[b][c]).powi(2)).sum::<f64>().sqrt();
|
||||
if (mixed > 0.5 && apart < 30.0 || mixed > 0.15 && apart < 12.0) && best.is_none_or(|x| mixed > x.0) {
|
||||
let merges = match alike {
|
||||
Alike::Rgb => {
|
||||
let apart = (0..3).map(|c| (color[a][c] - color[b][c]).powi(2)).sum::<f64>().sqrt();
|
||||
mixed > 0.5 && apart < 30.0 || mixed > 0.15 && apart < 12.0
|
||||
}
|
||||
Alike::Seen => {
|
||||
let (la, lb) = (oklab(color[a]), oklab(color[b]));
|
||||
let apart = (0..3).map(|c| (la[c] - lb[c]).powi(2)).sum::<f64>().sqrt();
|
||||
mixed > 0.5 && apart < FULL_TONE_ALIKE || mixed > 0.15 && apart < FULL_TONE_PATCHES
|
||||
}
|
||||
};
|
||||
if merges && best.is_none_or(|x| mixed > x.0) {
|
||||
best = Some((mixed, a, b));
|
||||
}
|
||||
}
|
||||
@@ -410,6 +444,33 @@ mod tests {
|
||||
assert!(own.iter().all(|o| *o == own[0]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn full_tone_merges_only_mixed_colors_that_look_alike() {
|
||||
// Two colors in a checkerboard: every pixel has four neighbors of the other color.
|
||||
let mixed =
|
||||
|a: [u8; 3], b: [u8; 3]| Quantized { labels: (0..16 * 16).map(|i| ((i % 16 + i / 16) % 2) as u16).collect(), palette: vec![a, b] };
|
||||
let colors = |q: Quantized, alike: Alike| merge_mixed(q, 16, 16, &[], alike).palette.len();
|
||||
// Gray and a greener gray, 22 levels apart (about 6.4 in Oklab): one color in RGB, two that look different.
|
||||
let (gray, green) = ([128, 128, 128], [128, 150, 128]);
|
||||
assert_eq!(colors(mixed(gray, green), Alike::Rgb), 1);
|
||||
assert_eq!(colors(mixed(gray, green), Alike::Seen), 2);
|
||||
// Two grays 3 levels apart (about 1 in Oklab): one color either way.
|
||||
assert_eq!(colors(mixed(gray, [131; 3]), Alike::Rgb), 1);
|
||||
assert_eq!(colors(mixed(gray, [131; 3]), Alike::Seen), 1);
|
||||
// Side by side, not mixed: two colors either way.
|
||||
let halves = Quantized { labels: (0..16 * 16).map(|i| u16::from(i % 16 >= 8)).collect(), palette: vec![[128; 3], [131; 3]] };
|
||||
assert_eq!(colors(halves.clone(), Alike::Rgb), 2);
|
||||
assert_eq!(colors(halves, Alike::Seen), 2);
|
||||
// In 8 × 8 blocks, mixed in patches: grays 3 levels apart merge either way; grays 6 levels
|
||||
// apart (about 10 in RGB, more than 1.5 in Oklab) merge only in RGB.
|
||||
let blocks =
|
||||
|b: [u8; 3]| Quantized { labels: (0..16 * 16).map(|i| ((i % 16 / 8 + i / 16 / 8) % 2) as u16).collect(), palette: vec![gray, b] };
|
||||
assert_eq!(colors(blocks([131; 3]), Alike::Rgb), 1);
|
||||
assert_eq!(colors(blocks([131; 3]), Alike::Seen), 1);
|
||||
assert_eq!(colors(blocks([134; 3]), Alike::Rgb), 1);
|
||||
assert_eq!(colors(blocks([134; 3]), Alike::Seen), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_picture_with_no_pixels_is_not_shared_out() {
|
||||
// No band for each of the rows of a picture with no columns, however many rows it has.
|
||||
|
||||
@@ -138,6 +138,12 @@ pub(super) fn labs(colors: &[[f64; 3]]) -> Vec<[f64; 3]> {
|
||||
/// The most colours Full Tone (and Automatic on continuous-tone images) makes.
|
||||
pub const FULL_TONE_MAX: usize = 256;
|
||||
|
||||
/// The fewest colors Palette › Full Tone splits into, before merging, at Color detail `detail`
|
||||
/// (0–1): one per step of Color detail, 85 at 85. Automatic does not use it.
|
||||
pub(super) fn full_tone_least(detail: f64) -> usize {
|
||||
(detail.clamp(0.0, 1.0) * 100.0).round() as usize
|
||||
}
|
||||
|
||||
/// A histogram bin in use: its index, mean colour and pixel count.
|
||||
pub(super) type Used = (usize, [f64; 3], f64);
|
||||
|
||||
@@ -152,11 +158,14 @@ pub(super) fn color_palette(used: &[Used], params: &TraceParams) -> Vec<[f64; 3]
|
||||
// Full Tone: split until no colour box is wider than 96 (few colours) … 8 (many) levels per
|
||||
// channel, which is one histogram bin.
|
||||
let full_tone = || refine(used, centers(&median_cut(used, FULL_TONE_MAX, 96.0 - 88.0 * detail)));
|
||||
// Palette › Full Tone also splits into at least one color per step of Color detail, where the
|
||||
// picture has that many (see [`full_tone_least`]).
|
||||
let full_tone_palette = || refine(used, centers(&median_cut_least(used, FULL_TONE_MAX, 96.0 - 88.0 * detail, full_tone_least(detail))));
|
||||
// A Limited palette of 9 to 16 colors keeps a nearly flat dark ground in one color (see [`kmeans_rgb`]).
|
||||
let limited = || kmeans_rgb(used, k, if FEW_COLORS.contains(&k) { LEAST_APART } else { 0.0 });
|
||||
match params.palette {
|
||||
Palette::Limited => limited(),
|
||||
Palette::FullTone => full_tone(),
|
||||
Palette::FullTone => full_tone_palette(),
|
||||
Palette::Automatic => match flat_colors(used, detail) {
|
||||
Some(n) => flat_palette(used, n),
|
||||
None => full_tone(),
|
||||
@@ -285,7 +294,7 @@ const GRAY: f64 = 3.0;
|
||||
/// (see [`median_cut_apart`]). A large, nearly flat dark ground then keeps one color, and the
|
||||
/// color it would have taken goes to another cut.
|
||||
fn kmeans_rgb(used: &[Used], k: usize, apart: f64) -> Vec<[f64; 3]> {
|
||||
refine(used, centers(&median_cut_apart(used, k, 0.0, apart)))
|
||||
refine(used, centers(&median_cut_apart(used, k, 0.0, apart, 0)))
|
||||
}
|
||||
|
||||
/// Divisive clustering in the manner of median cut, but each box is cut where that leaves its two
|
||||
@@ -295,21 +304,32 @@ fn kmeans_rgb(used: &[Used], k: usize, apart: f64) -> Vec<[f64; 3]> {
|
||||
/// and leaves two smaller colours in one box. Boxes are cut while there are fewer than `k` and
|
||||
/// some box spans more than `spread` levels of a channel.
|
||||
pub(super) fn median_cut(used: &[Used], k: usize, spread: f64) -> Vec<ColorBox> {
|
||||
median_cut_apart(used, k, spread, 0.0)
|
||||
median_cut_apart(used, k, spread, 0.0, 0)
|
||||
}
|
||||
|
||||
/// [`median_cut`], except when both sides of a cut have a dark gray mean color (see [`DARK`] and
|
||||
/// [`GRAY`]) and the two means lie `d` (Oklab × 100) apart with `d` < `apart`: then the cut's gain
|
||||
/// is scaled by (`d` / `apart`)². With `apart` 0 it is [`median_cut`].
|
||||
fn median_cut_apart(used: &[Used], k: usize, spread: f64, apart: f64) -> Vec<ColorBox> {
|
||||
/// [`median_cut`], then, while there are fewer than `least` boxes, cutting the boxes no wider than
|
||||
/// `spread` too, the one whose cut gains most first.
|
||||
fn median_cut_least(used: &[Used], k: usize, spread: f64, least: usize) -> Vec<ColorBox> {
|
||||
median_cut_apart(used, k, spread, 0.0, least)
|
||||
}
|
||||
|
||||
/// [`median_cut`] with at least `least` boxes where the bins allow (see [`median_cut_least`]),
|
||||
/// except when both sides of a cut have a dark gray mean color (see [`DARK`] and [`GRAY`]) and the
|
||||
/// two means lie `d` (Oklab × 100) apart with `d` < `apart`: then the cut's gain is scaled by
|
||||
/// (`d` / `apart`)². With `apart` 0 and `least` 0 it is [`median_cut`].
|
||||
fn median_cut_apart(used: &[Used], k: usize, spread: f64, apart: f64, least: usize) -> Vec<ColorBox> {
|
||||
if used.is_empty() {
|
||||
return vec![ColorBox { mean: [255.0; 3], weight: 0.0, peak: 0.0 }];
|
||||
}
|
||||
/// A box's bins and its best cut: (gain, bins sorted along the cut's channel, bins on the first side).
|
||||
/// A box's bins, its widest span over the channels, and its best cut: (gain, bins sorted along
|
||||
/// the cut's channel, bins on the first side).
|
||||
struct Cut {
|
||||
bins: Vec<usize>,
|
||||
widest: f64,
|
||||
best: Option<(f64, Vec<usize>, usize)>,
|
||||
}
|
||||
// Boxes no wider than this are never cut.
|
||||
let narrowest = if least > 0 { 0.0 } else { spread.max(0.0) };
|
||||
// How much of a cut's gain counts when its sides' mean colors are `a` and `b`: all of it when
|
||||
// they look different or either is not a dark gray, less as two dark grays look more alike.
|
||||
let dark_gray = |c: [f64; 3]| c[0] < DARK && c[1].hypot(c[2]) < GRAY;
|
||||
@@ -328,7 +348,7 @@ fn median_cut_apart(used: &[Used], k: usize, spread: f64, apart: f64) -> Vec<Col
|
||||
v.clone().fold(f64::NEG_INFINITY, f64::max) - v.fold(f64::INFINITY, f64::min)
|
||||
})
|
||||
.fold(0.0, f64::max);
|
||||
if bins.len() >= 2 && widest > spread.max(0.0) {
|
||||
if bins.len() >= 2 && widest > narrowest {
|
||||
for ch in 0..3 {
|
||||
let mut sorted = bins.clone();
|
||||
sorted.sort_by(|x, y| used[*x].1[ch].total_cmp(&used[*y].1[ch]));
|
||||
@@ -366,12 +386,19 @@ fn median_cut_apart(used: &[Used], k: usize, spread: f64, apart: f64) -> Vec<Col
|
||||
}
|
||||
}
|
||||
}
|
||||
Cut { bins, best }
|
||||
Cut { bins, widest, best }
|
||||
};
|
||||
let mut boxes = vec![plan((0..used.len()).collect())];
|
||||
while boxes.len() < k {
|
||||
let gain = |c: &Cut| c.best.as_ref().map_or(f64::NEG_INFINITY, |b| b.0);
|
||||
let Some(bi) = (0..boxes.len()).filter(|&i| boxes[i].best.is_some()).max_by(|&a, &b| gain(&boxes[a]).total_cmp(&gain(&boxes[b]))) else {
|
||||
// The boxes wider than `spread` first; when none is left and there are fewer than `least`,
|
||||
// any box with a cut.
|
||||
let most = |wide: bool| {
|
||||
(0..boxes.len())
|
||||
.filter(|&i| boxes[i].best.is_some() && (!wide || boxes[i].widest > spread.max(0.0)))
|
||||
.max_by(|&a, &b| gain(&boxes[a]).total_cmp(&gain(&boxes[b])))
|
||||
};
|
||||
let Some(bi) = most(true).or_else(|| if boxes.len() < least { most(false) } else { None }) else {
|
||||
break;
|
||||
};
|
||||
let Some((_, mut sorted, at)) = boxes.swap_remove(bi).best else { break };
|
||||
@@ -467,7 +494,9 @@ mod tests {
|
||||
assert_eq!(palette(Palette::DocumentLibrary, colors), plain(colors as usize), "{colors} colors");
|
||||
}
|
||||
let detail = (TraceParams::default().color_detail / 100.0).clamp(0.0, 1.0);
|
||||
assert_eq!(palette(Palette::FullTone, 16), refine(&used, centers(&median_cut(&used, FULL_TONE_MAX, 96.0 - 88.0 * detail))));
|
||||
// Full Tone's median cut makes at least one color per step of Color detail.
|
||||
let full_tone = refine(&used, centers(&median_cut_least(&used, FULL_TONE_MAX, 96.0 - 88.0 * detail, full_tone_least(detail))));
|
||||
assert_eq!(palette(Palette::FullTone, 16), full_tone);
|
||||
let automatic = match flat_colors(&used, detail) {
|
||||
Some(n) => plain(n),
|
||||
None => refine(&used, centers(&median_cut(&used, FULL_TONE_MAX, 96.0 - 88.0 * detail))),
|
||||
@@ -475,6 +504,32 @@ mod tests {
|
||||
assert_eq!(palette(Palette::Automatic, 16), automatic);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn full_tone_makes_one_color_per_step_of_color_detail() {
|
||||
// 256 grays, 1,200 pixels each: the width limit alone gives fewer colors than Color detail.
|
||||
let ramp = bins((0..256).map(|g| ([f64::from(g); 3], 1200.0)));
|
||||
let colors = |used: &[Used], color_detail: f64| {
|
||||
color_palette(used, &TraceParams { palette: Palette::FullTone, color_detail, ..TraceParams::default() }).len()
|
||||
};
|
||||
assert!(median_cut(&ramp, FULL_TONE_MAX, 96.0 - 88.0 * 0.85).len() < 85);
|
||||
assert_eq!(colors(&ramp, 85.0), 85);
|
||||
assert_eq!(colors(&ramp, 40.0), 40);
|
||||
// A picture of three colors keeps three.
|
||||
let three = bins([([0.0; 3], 100.0), ([128.0; 3], 100.0), ([255.0; 3], 100.0)]);
|
||||
assert_eq!(colors(&three, 85.0), 3);
|
||||
// A large, finely shaded area does not take the count from a small pair of reds 40 levels
|
||||
// apart, wider than one color may be at Color detail 85: the pair keeps two colors.
|
||||
let shaded = bins((0..128).map(|g| ([f64::from(g); 3], 10_000.0)).chain([([200.0, 0.0, 0.0], 1.0), ([240.0, 0.0, 0.0], 1.0)]));
|
||||
let reds = color_palette(&shaded, &TraceParams { palette: Palette::FullTone, color_detail: 85.0, ..TraceParams::default() });
|
||||
assert!(reds.len() >= 85, "{} colors", reds.len());
|
||||
for red in [200.0, 240.0] {
|
||||
assert!(reds.iter().any(|c| (c[0] - red).abs() < 1.0 && c[1] < 1.0), "no color for red {red}: {reds:?}");
|
||||
}
|
||||
// Automatic's continuous-tone colors are not raised to the count.
|
||||
let automatic = color_palette(&ramp, &TraceParams { palette: Palette::Automatic, color_detail: 85.0, ..TraceParams::default() });
|
||||
assert!(automatic.len() < 85, "{} colors with Automatic", automatic.len());
|
||||
}
|
||||
|
||||
/// `colors`, each with `n` pixels, as histogram bins.
|
||||
fn bins(colors: impl IntoIterator<Item = ([f64; 3], f64)>) -> Vec<Used> {
|
||||
colors.into_iter().enumerate().map(|(i, (c, n))| (i, c, n)).collect()
|
||||
|
||||
@@ -944,6 +944,16 @@ fn presets_have_their_paths_corners_and_noise() {
|
||||
assert_eq!(colors, [64, 20, 3, 6, 16, 8]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn high_fidelity_photo_traces_in_full_tone() {
|
||||
// As many colors as the tones need, split until no color spans more than about 21 levels, as
|
||||
// abutting shapes.
|
||||
let p = preset("High Fidelity Photo").unwrap();
|
||||
assert_eq!((p.palette, p.color_detail, p.method), (Palette::FullTone, 85.0, Method::Abutting));
|
||||
// Low Fidelity Photo keeps its 20 colors.
|
||||
assert_eq!(preset("Low Fidelity Photo").unwrap().palette, Palette::Limited);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn params_json_roundtrip_and_partial() {
|
||||
let p = preset("6 Colors").unwrap();
|
||||
@@ -1213,12 +1223,14 @@ fn full_tone_follows_the_tones_the_image_has() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn automatic_traces_flat_art_with_its_colours_and_photos_in_full_tone() {
|
||||
fn automatic_traces_flat_art_with_its_colours_and_photos_in_continuous_tone() {
|
||||
let flat = palette(&flat_quads(), Palette::Automatic, 2, 50.0);
|
||||
assert_eq!(flat.len(), 4, "the four flat colours, whatever `colors` says: {flat:?}");
|
||||
assert!(flat.iter().any(|c| c[0] > 200 && c[1] < 60 && c[2] < 60), "red is one of them: {flat:?}");
|
||||
let tone = palette(&gradient(), Palette::Automatic, 2, 50.0);
|
||||
assert_eq!(tone.len(), palette(&gradient(), Palette::FullTone, 2, 50.0).len(), "continuous tone: as Full Tone");
|
||||
// Continuous tone: as many colors as the tones need, without Full Tone's least count.
|
||||
let full = palette(&gradient(), Palette::FullTone, 2, 50.0);
|
||||
assert!(tone.len() > 4 && tone.len() <= full.len(), "{} colors, {} in Full Tone", tone.len(), full.len());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1713,6 +1725,39 @@ fn jitter(x: u32, y: u32, salt: u32) -> f64 {
|
||||
f64::from(v & 0xFFFF) / 32768.0 - 1.0
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn full_tone_keeps_a_noisy_ramps_neighboring_tones() {
|
||||
// Gray to green with grain: Full Tone's neighboring tones are close in RGB and mixed along
|
||||
// their borders, and still look different.
|
||||
let img = Raster::from_fn(256, 96, |x, y| {
|
||||
let v = |k: u32, base: f64, slope: f64| (base + slope * f64::from(x) + 8.0 * jitter(x, y, k)).round().clamp(0.0, 255.0) as u8;
|
||||
[v(0, 100.0, 0.0), v(1, 40.0, 0.7), v(2, 100.0, 0.0), 255]
|
||||
});
|
||||
let params = TraceParams { mode: Mode::Color, palette: Palette::FullTone, color_detail: 85.0, ..TraceParams::default() };
|
||||
// Median cut gives at least 85 (one per step of Color detail); merged where they look alike, 6
|
||||
// are left, and merged as close RGB colors, 5 would be.
|
||||
let n = quantize(&img, ¶ms).palette.len();
|
||||
assert!(n >= 6, "{n} tones");
|
||||
// Automatic traces continuous tone without Full Tone's least count and keeps merging close RGB colors.
|
||||
let automatic = quantize(&img, &TraceParams { palette: Palette::Automatic, ..params }).palette.len();
|
||||
assert!(automatic < n, "{automatic} tones with Automatic, {n} with Full Tone");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn full_tone_merges_a_flat_colors_compression_blocks() {
|
||||
// Three flat colors stored in 8 × 8 blocks, each block off by up to 8 levels a channel, with a
|
||||
// little grain: the blocks of one color look alike and are mixed in patches, so each flat
|
||||
// color traces as one shape in Full Tone too, not as squares of near colors.
|
||||
let bases = [[200.0, 150.0, 120.0], [70.0, 120.0, 190.0], [40.0, 90.0, 40.0]];
|
||||
let img = Raster::from_fn(192, 128, |x, y| {
|
||||
let base: [f64; 3] = bases[(x / 64) as usize];
|
||||
let c = |k: u32| (base[k as usize] + 8.0 * jitter(x / 8, y / 8, k) + 1.5 * jitter(x, y, k + 7)).round().clamp(0.0, 255.0) as u8;
|
||||
[c(0), c(1), c(2), 255]
|
||||
});
|
||||
let res = trace(&img, &preset("High Fidelity Photo").unwrap());
|
||||
assert_eq!((res.palette.len(), res.paths.len()), (3, 3), "{:?}", res.palette);
|
||||
}
|
||||
|
||||
/// Three flat colours (a navy field, a yellow disc, a green bar) drawn anti-aliased, with `noise`
|
||||
/// levels of noise per channel.
|
||||
fn three_colour_art(noise: f64) -> Raster {
|
||||
@@ -2013,7 +2058,7 @@ fn a_nearly_flat_dark_ground_keeps_one_color() {
|
||||
|
||||
#[test]
|
||||
fn a_noisy_gradient_keeps_its_tones_without_specks() {
|
||||
// Tones 0–255 across with noise larger than High Fidelity Photo's 64 steps are apart.
|
||||
// Tones 0–255 across with up to 5 levels of noise a channel.
|
||||
let img = Raster::from_fn(256, 96, |x, y| {
|
||||
let c = |k: u32| (f64::from(x) + 5.0 * jitter(x, y, k)).round().clamp(0.0, 255.0) as u8;
|
||||
[c(0), c(1), c(2), 255]
|
||||
|
||||
@@ -23,7 +23,9 @@ versions. Among those presets, Corners is 100 in Technical Drawing, 75 in Defaul
|
||||
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)).
|
||||
[image-trace-quality.md](image-trace-quality.md#preset-paths-corners-and-noise)). High Fidelity Photo
|
||||
traces with Palette › Full Tone at Color detail 85 and Method › Abutting (VectorCraft's own values); Low
|
||||
Fidelity Photo traces with a Limited palette of 20 colors, Overlapping.
|
||||
|
||||
Sliders apply on release, and their numeric fields can be edited directly. Each committed
|
||||
adjustment re-traces the selected object as one undo step and changes the preset to Custom.
|
||||
@@ -83,8 +85,8 @@ leaves out every white area whatever `ignoreBackground` is.
|
||||
color nearest to it in lightness and hue, with a difference in hue or chroma counted three times
|
||||
as much as one in lightness, so a gray or a muted brown takes a black, white or other neutral
|
||||
library color rather than a saturated color of the same lightness.
|
||||
**Abutting and Overlapping.** Abutting is the default Method, and the Default, Auto Color, 3 Colors,
|
||||
6 Colors and 16 Colors presets use it. With Abutting, two neighboring colors share one exact edge. The
|
||||
**Abutting and Overlapping.** Abutting is the default Method, and the Default, Auto Color, High Fidelity
|
||||
Photo, 3 Colors, 6 Colors and 16 Colors presets use it. With Abutting, two neighboring colors share one exact edge. The
|
||||
canvas smooths the edges of each path on its own, so each of the two paths covers only part of the
|
||||
pixels along that edge, and a thin line of the background can show between the colors. Web browsers
|
||||
and PDF viewers usually draw an exported SVG or PDF file the same way and show the same line. Raster
|
||||
|
||||
@@ -83,7 +83,7 @@ gradient, texture, sensor noise):
|
||||
Photo trace was 86,000 anchors under the old limit; a noisier scan of the same size would have been
|
||||
refused. At 8,000,000 anchors a trace keeps about 0.7 GB, with a peak of about 0.9 GB while tracing.
|
||||
The preset rows were measured with the earlier preset values: High Fidelity Photo at Paths 90, Corners 25
|
||||
and Noise 4, Low Fidelity Photo at Paths 60, Noise 12 and 16 Colors at Paths 70, Corners 55 and Noise 10
|
||||
and Noise 4 with a Limited palette of 64 colors and Overlapping, Low Fidelity Photo at Paths 60, Noise 12 and 16 Colors at Paths 70, Corners 55 and Noise 10
|
||||
(see [Preset Paths, Corners and Noise](#preset-paths-corners-and-noise)).
|
||||
## Collapsed loops keep few segments
|
||||
A small closed loop (or a piece that returns to its own junction) whose fit collapsed to a single
|
||||
@@ -252,6 +252,35 @@ versions of these presets use the same Paths, Corners and Noise. 6 Colors and 16
|
||||
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.
|
||||
|
||||
High Fidelity Photo later moved to Palette › Full Tone at Color detail 85 and Method › Abutting (it was a
|
||||
Limited palette of 64 colors, Overlapping); Paths, Corners and Noise stay 50, 50 and 5. These are
|
||||
VectorCraft's own values. Palette › Full Tone splits its colors until none spans more than its width
|
||||
limit, then, while it has fewer colors than Color detail's value (85 at 85), splits the narrower ones
|
||||
too, where the image has that many colors; the colors that look alike then merge, so a trace can end
|
||||
with fewer. Automatic's continuous-tone colors do not take that count. Full Tone's colors merge as one
|
||||
color when they are thoroughly mixed and less than 5 apart in Oklab × 100, or mixed in patches
|
||||
(compression blocks) and less than 1.5 apart; the other palettes, Automatic included, keep the RGB test
|
||||
(under 30 levels and thoroughly mixed, or under 12 and mixed in patches). These rules apply to every
|
||||
trace with Palette › Full Tone, so an existing Full Tone trace or saved preset traces differently when
|
||||
it is traced again.
|
||||
|
||||
On 19 photos, cartoons and synthetic shapes of 400 to 4,000 px, the mean pixel difference summed over the
|
||||
inputs fell from 80.5 to 65.6 levels. It fell by more than 1% on 14 inputs (2.88 to 2.19 and 2.59 to 2.48
|
||||
on two photos, the second a JPEG at quality 40; 1.88 to 1.45 on a 4000 × 3000 photo; 12.40 to 7.44 on an
|
||||
upscaled cartoon) and changed by less than 1% on the other 5. Anchors rose 1.66 times (1.98 million to
|
||||
3.29 million), the SVG size 1.31 times, and the trace time 1.9 times (14.1 to 27.1 s for all 19, 3 at a
|
||||
time). Flat synthetic art costs the most: 12 to 400 anchors on a 4000 × 3000 image of two flat colors, and
|
||||
about 3 times the anchors on two images of synthetic shapes (400 × 300 and 1600 × 1200), with the same
|
||||
pixel difference. Peak memory fell on every input in the same pass:
|
||||
942 to 626 MiB on a 1536 × 2048 cartoon. Low Fidelity Photo, 16 Colors, 6 Colors, Black and White Logo
|
||||
and Auto Color give byte-identical SVGs. On three flat colors stored in 8 × 8 blocks, each block off by
|
||||
up to 8 levels a channel, High Fidelity Photo traces 3 colors as 3 paths; without the patch rule it traced
|
||||
7 colors as 74 paths. Tests: `high_fidelity_photo_traces_in_full_tone`,
|
||||
`full_tone_keeps_a_noisy_ramps_neighboring_tones` and `full_tone_merges_a_flat_colors_compression_blocks`
|
||||
in `crates/trace/src/tests.rs`, `full_tone_merges_only_mixed_colors_that_look_alike` in
|
||||
`crates/trace/src/quantize.rs`, and `full_tone_makes_one_color_per_step_of_color_detail` in
|
||||
`crates/trace/src/quantize/palette.rs`.
|
||||
|
||||
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
|
||||
(16 Colors), 27 to 14 (Black and White Logo) and 43 to 28 (Line Art); Silhouettes rises from 13 to 15 and
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user