Image Trace: Full Tone merges and cuts with less repeated work

merge_mixed works out each color's Oklab value once, and again only for
the color that takes in another, and skips the distance for two colors
mixed too little to merge under either test. Median cut plans the cut of
a narrow box only while there are fewer boxes than the least count. The
traced SVGs are byte-identical for the six presets on 19 test images;
High Fidelity Photo's trace time for all 19 falls from 35.3 to 31.5 s.

The quality notes give High Fidelity Photo in Full Tone its own section,
say how far merging can bring the color count below the least count,
and state the measured trace time, memory and cost on grainy flat grays.
A test checks that a grainy dark gray traces with about as many colors
and shapes as a grainy light one, and the noisy ramp test's comment
gives the counts this tree measures.
This commit is contained in:
dotCooCoo
2026-11-16 07:54:58 -08:00
parent 783f7bb9bb
commit dd1a6a45bd
6 changed files with 107 additions and 58 deletions
+5 -4
View File
@@ -24,8 +24,9 @@ Logo, Line Art and Outline, and 50 in the other eight. Noise is 1 px in Technica
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)). 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.
traces with Palette › Full Tone at Color detail 85 and Method › Abutting (VectorCraft's own values; see
[image-trace-quality.md](image-trace-quality.md#high-fidelity-photo-in-full-tone)); 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.
@@ -86,8 +87,8 @@ color nearest to it in lightness and hue, with a difference in hue or chroma cou
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, 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
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
exports (PNG, JPEG, TIFF and the other bitmap formats) with None anti-aliasing do not show it at any
+46 -32
View File
@@ -83,8 +83,9 @@ 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 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)).
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) and [High Fidelity Photo in Full Tone](#high-fidelity-photo-in-full-tone)).
## Collapsed loops keep few segments
A small closed loop (or a piece that returns to its own junction) whose fit collapsed to a single
cubic cannot stay as one, so it kept every positioned point as a straight segment. A looser Paths
@@ -250,36 +251,8 @@ Every built-in preset other than Auto Color and Flat Logo traces at Paths 50. Co
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. 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.
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`.
the measurements below were made at those values. High Fidelity Photo's palette and Method changed
later (see [High Fidelity Photo in Full Tone](#high-fidelity-photo-in-full-tone)).
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
@@ -300,6 +273,47 @@ the other sections of this file, and in the Image Trace entries of
[target-app-parity.md](target-app-parity.md), were measured at the earlier values.
Test: `presets_have_their_paths_corners_and_noise` in `crates/trace/src/tests.rs`.
## High Fidelity Photo in Full Tone
High Fidelity Photo traces with Palette › Full Tone at Color detail 85 and Method › Abutting. It traced
with a Limited palette of 64 colors, Overlapping, before. Paths, Corners and Noise stay 50, 50 and 5.
These are VectorCraft's own values. Low Fidelity Photo keeps its Limited palette of 20 colors.
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), it splits the narrower ones too, where the image has
that many colors. The colors that look alike merge after that, and a merged color can merge again, so a
trace can end with far fewer colors than that count: a gray-to-green ramp 256 px wide with up to 8 levels
of grain a channel ends with 6 colors at Color detail 85, where the RGB test leaves 4. Full Tone's colors
merge 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), and Automatic's continuous-tone
colors do not take the least count. 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) and the SVG size 1.31 times. The trace time rose 1.7 times (18.9 to 31.5 s for all 19, with
both builds tracing 3 images at a time in the same run). 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. A flat gray 192 × 128 with
up to 3 levels of grain a channel traces as 25 colors and 244 anchors, where the Limited palette of 64
traced 1 color and 4 anchors; with up to 5 or 8 levels both trace it as 25 to 37 colors, on each of the
grays tested (28, 60, 128 and 200).
In the same run, peak memory fell on 16 of the 19 inputs, for example 906 to 625 MiB on a 1536 × 2048
cartoon, and rose on 3 small ones (by 5 to 11 MiB). Abutting accounts for the fall: the earlier preset
with Method › Abutting peaked at 523 MiB on the same cartoon, against 924 MiB with Overlapping, in another
run. 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_more_of_a_noisy_ramps_tones`,
`full_tone_traces_a_noisy_dark_ground_like_a_light_one` 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`.
## A simpler contour walk
`contour::trace_mask` follows each region's outline along the pixel cracks with the inside on the
right. It used to build a table of outgoing directions for every pixel corner in a raster pass and
File diff suppressed because one or more lines are too long