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
+10 -6
View File
@@ -328,8 +328,10 @@ fn median_cut_apart(used: &[Used], k: usize, spread: f64, apart: f64, least: usi
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) };
// Boxes no wider than `spread` are cut only while there are fewer than `least` boxes. A box made
// when there are already `least` boxes or more is never cut unless it is wider, so its cut is
// not planned.
let narrowest = |boxes_after: usize| if boxes_after < least { 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;
@@ -340,7 +342,8 @@ fn median_cut_apart(used: &[Used], k: usize, spread: f64, apart: f64, least: usi
}
((0..3).map(|q| (a[q] - b[q]).powi(2)).sum::<f64>() / (apart * apart)).min(1.0)
};
let plan = |bins: Vec<usize>| {
let plan = |bins: Vec<usize>, boxes_after: usize| {
let narrowest = narrowest(boxes_after);
let mut best: Option<(f64, Vec<usize>, usize)> = None;
let widest = (0..3)
.map(|ch| {
@@ -388,7 +391,7 @@ fn median_cut_apart(used: &[Used], k: usize, spread: f64, apart: f64, least: usi
}
Cut { bins, widest, best }
};
let mut boxes = vec![plan((0..used.len()).collect())];
let mut boxes = vec![plan((0..used.len()).collect(), 1)];
while boxes.len() < k {
let gain = |c: &Cut| c.best.as_ref().map_or(f64::NEG_INFINITY, |b| b.0);
// The boxes wider than `spread` first; when none is left and there are fewer than `least`,
@@ -403,8 +406,9 @@ fn median_cut_apart(used: &[Used], k: usize, spread: f64, apart: f64, least: usi
};
let Some((_, mut sorted, at)) = boxes.swap_remove(bi).best else { break };
let tail = sorted.split_off(at);
boxes.push(plan(sorted));
boxes.push(plan(tail));
let after = boxes.len() + 2;
boxes.push(plan(sorted, after));
boxes.push(plan(tail, after));
}
boxes
.iter()