mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-30 02:07:10 +01:00
Image Trace: Flat Logo keeps gray bands along black when the gray is named some levels off
With a gray named 6 to 20 levels off the color its flat areas have, a gray band 3 or 4 pixels wide along black, with edges blurred by 0.6 pixels, traced as black and white: no band pixel came within PURE of the named gray, so the run rule and the sharp-edge halt in across_edge never kept the gray. unmix now takes the colors of each class's flat areas (area_colors). For a class with a flat area PURE or more from its named color, pixels within PURE of one of those colors also count as the class's pixels for the LINE run rule. The sharp-edge halt stops at a pixel near one of those colors, or at a pixel within PURE of the named color whose previous pixel on the path is too, so a strip at the named color 2 or more pixels wide keeps its gray while single edge pixels near the named color do not. The halt is checked before the walk counts a pixel as past the class, since a pixel of the area's color can lie past the named color on the line. On the band test images (gray 230, 200 and 128 named 6 or 12 levels off, 3 and 4 pixel bands beside a flat square of the gray), 0 to 33 band samples of 48 or 64 traced gray before, and 40 to 61 after. Images whose classes have no flat area off their named color trace as before.
This commit is contained in:
1 parent
4167e0abca
commit
908f8c5cb5
2 files changed
+164
-17
No files matched your search
@@ -1111,6 +1111,41 @@ fn a_soft_gray_band_along_black_traces_as_black_and_white() {
|
||||
assert!((36..=42).contains(&blur_three), "bands 4 px wide blurred by 3 px: {blur_three} of 192 samples traced gray");
|
||||
}
|
||||
|
||||
/// A gray band 3 or 4 pixels wide along a black disc, with edges blurred by 0.6 pixels and a flat
|
||||
/// square of the same gray elsewhere, keeps at least 4 of every 5 samples gray when the gray is named
|
||||
/// 6 or 12 levels lighter or darker than the band's color, as it does when the gray is named exactly:
|
||||
/// the pixels of the square's color count as pixels of the gray.
|
||||
#[test]
|
||||
fn a_gray_band_along_black_keeps_its_gray_when_named_some_levels_off_its_flat_areas() {
|
||||
const INK: [u8; 3] = [4, 4, 3];
|
||||
for gray in [230u8, 200, 128] {
|
||||
for off in [-12i16, -6, 6, 12] {
|
||||
let Ok(named) = u8::try_from(i16::from(gray) + off) else { continue };
|
||||
for wide in [3u8, 4] {
|
||||
let img = render(120, 60, 0.6, |x, y| {
|
||||
let band = (40.0..43.0 + f64::from(wide)).contains(&x) && (20.0..36.0).contains(&y);
|
||||
let square = (80.0..110.0).contains(&x) && (15.0..45.0).contains(&y);
|
||||
Some(if (x - 25.0).hypot(y - 28.0) <= 18.0 {
|
||||
INK
|
||||
} else if band || square {
|
||||
[gray; 3]
|
||||
} else {
|
||||
WHITE
|
||||
})
|
||||
});
|
||||
let res = trace(&img, &black_gray_white([named; 3]));
|
||||
let kept = (0..wide)
|
||||
.flat_map(|c| (20..36).map(move |y| Point::new(43.5 + f64::from(c), f64::from(y) + 0.5)))
|
||||
.filter(|&at| top_color(&res, at) == Some([named; 3]))
|
||||
.count();
|
||||
let all = 16 * usize::from(wide);
|
||||
assert!(5 * kept >= 4 * all, "gray {gray} named {named}, {wide} px band: {kept} of {all} samples traced gray");
|
||||
assert_eq!(top_color(&res, Point::new(95.0, 30.0)), Some([named; 3]), "gray {gray} named {named}: the square");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A soft-edged gray spoke two or three pixels wide running straight out of a black disc, blurred
|
||||
/// by 1 or 1.5 pixels, loses 3 or 4 of its 16 samples nearest the disc and keeps the rest. Blurred
|
||||
/// by 2 pixels, a spoke three pixels wide loses 2 or 3 of them, and one two pixels wide traces as
|
||||
|
||||
+129
-17
@@ -27,12 +27,12 @@ const NEARBY: usize = 6;
|
||||
const WALK: usize = 5;
|
||||
/// A pixel whose neighbors are all this close to it (in 0..1 premultiplied RGBA) lies in a flat area.
|
||||
const FLAT: f64 = 1.5 / 255.0;
|
||||
/// How many joined pixels within [`PURE`] of one class are taken as art of that class, such as a
|
||||
/// one-pixel line. The edge pixels of a curve that come within `PURE` of a class join in far fewer.
|
||||
/// A straight edge whose anti-aliased pixels come within `PURE` of
|
||||
/// a class gives the same pixels as a line of that class, and keeps the class too. A one-pixel line
|
||||
/// between two classes that is shorter, falls between pixels or is blurred is read as a blend of
|
||||
/// those two classes.
|
||||
/// How many joined pixels within [`PURE`] of one class (or of the colors of its flat areas, for a
|
||||
/// class named some levels off them, [`area_colors`]) are taken as art of that class, such as a
|
||||
/// one-pixel line. The edge pixels of a curve that come within `PURE` of a class join in far fewer. A
|
||||
/// straight edge whose anti-aliased pixels come within `PURE` of a class gives the same pixels as a
|
||||
/// line of that class, and keeps the class too. A one-pixel line between two classes that is shorter,
|
||||
/// falls between pixels or is blurred is read as a blend of those two classes.
|
||||
const LINE: usize = 2 * NEARBY + 1;
|
||||
|
||||
struct Subset {
|
||||
@@ -209,24 +209,33 @@ pub(crate) fn unmix(pix: &[Px], width: usize, height: usize, classes: &[[f64; 4]
|
||||
// ([`flat_classes`], [`RowDistances::around`]); the other mix lies as near as the steps that
|
||||
// `across_edge` counts when those are fewer. For the best mix,
|
||||
// the classes held by flat areas between two classes count as well ([`between_classes`]). A
|
||||
// pixel within `PURE` of a class that is joined to at least [`LINE`] such pixels of that class
|
||||
// pixel of a class (within `PURE` of it, or of the colors of its flat areas when it is named some
|
||||
// levels off them, [`area_colors`]) that is joined to at least [`LINE`] such pixels of that class
|
||||
// (itself included) keeps it ([`pure_runs`]), as a one-pixel line of gray between black and white.
|
||||
let flat = flat_classes(pix, &stands_for, width, height);
|
||||
let between = between_classes(pix, &flat, &mixed, width, height);
|
||||
let runs = pure_runs(&pure, width, height);
|
||||
let colors = area_colors(pix, classes, &flat, &between);
|
||||
let of_class = (!colors.iter().all(Vec::is_empty)).then(|| {
|
||||
pure.iter()
|
||||
.zip(pix)
|
||||
.map(|(&q, px)| q.or_else(|| (0..k).find(|&c| colors.get(c).is_some_and(|cs| near_area_color(px, cs)))))
|
||||
.collect::<Vec<_>>()
|
||||
});
|
||||
let of_class = of_class.as_deref().unwrap_or(&pure);
|
||||
let runs = pure_runs(of_class, width, height);
|
||||
let mut flat_rows = RowDistances::new(|j| [flat.get(j).copied().flatten(), None], width, height, k);
|
||||
let mut between_rows = (!between.is_empty()).then(|| RowDistances::new(|j| between.get(j).copied().unwrap_or([None; 2]), width, height, k));
|
||||
let mut dist = vec![NEARBY + 1; k];
|
||||
let mut kept = vec![NEARBY + 1; k];
|
||||
for (i, key) in tied {
|
||||
if pure.get(i).copied().flatten().is_some() && runs.get(i).is_some_and(|&r| r >= LINE) {
|
||||
if of_class.get(i).copied().flatten().is_some() && runs.get(i).is_some_and(|&r| r >= LINE) {
|
||||
continue;
|
||||
}
|
||||
let Some(mixes) = mixed.get(&key) else { continue };
|
||||
let edges: Vec<(&Weights, usize)> = mixes
|
||||
.tied
|
||||
.iter()
|
||||
.filter_map(|m| across_edge(pix, (&flat, &stands_for), (width, height), i, classes, &mixes.best, m).map(|r| (m, r)))
|
||||
.filter_map(|m| across_edge(pix, (&flat, &stands_for, &colors), (width, height), i, classes, &mixes.best, m).map(|r| (m, r)))
|
||||
.collect();
|
||||
if edges.is_empty() {
|
||||
continue;
|
||||
@@ -258,7 +267,10 @@ pub(crate) fn unmix(pix: &[Px], width: usize, height: usize, classes: &[[f64; 4]
|
||||
/// `other` gets past the class left out within [`WALK`] steps through the eight neighbors, and so
|
||||
/// does one that keeps going toward the other. When the first step of one path comes to a pixel
|
||||
/// that stands for its class of `other` (the edge on that side is sharp), the other path stops at
|
||||
/// a pixel within [`PURE`] of the class left out that it comes to before it gets past that class.
|
||||
/// a pixel of the class left out that it comes to before it gets past that class: a pixel within
|
||||
/// [`PURE`] of it, or, for a class named some levels off the colors of its flat areas
|
||||
/// ([`area_colors`]), within `PURE` of one of those colors, or within `PURE` of the class as named
|
||||
/// together with the pixel before it.
|
||||
/// For a class left out that lies beyond both classes of `other` on that line, the pixel has a
|
||||
/// neighbor on each side of that class, or a neighbor on each side of the pixel itself, none past
|
||||
/// the class of `other` nearer the class left out, and a chain of neighbors from the pixel to that
|
||||
@@ -279,7 +291,7 @@ pub(crate) fn unmix(pix: &[Px], width: usize, height: usize, classes: &[[f64; 4]
|
||||
/// path does not come to such a pixel, and when a class left out lies beyond both classes of `other`.
|
||||
fn across_edge(
|
||||
pix: &[Px],
|
||||
(flat, stands_for): (&[Option<usize>], &[Option<usize>]),
|
||||
(flat, stands_for, colors): (&[Option<usize>], &[Option<usize>], &AreaColors),
|
||||
(width, height): (usize, usize),
|
||||
i: usize,
|
||||
classes: &[[f64; 4]],
|
||||
@@ -356,11 +368,19 @@ fn across_edge(
|
||||
}
|
||||
false
|
||||
};
|
||||
// Whether pixel `j` comes within `PURE` of class `c`.
|
||||
let within = |j: usize, c: usize| {
|
||||
// Whether pixel `j` comes within `PURE` of class `c` as named.
|
||||
let named = |j: usize, c: usize| {
|
||||
let (Some(q), Some(cv)) = (pix.get(j), classes.get(c)) else { return false };
|
||||
(0..4).map(|k| (f64::from(q[k]) - cv[k]).powi(2)).sum::<f64>().sqrt() < PURE
|
||||
};
|
||||
// Whether pixel `j`, reached from pixel `prev`, is a pixel of class `c`: within `PURE` of it, or,
|
||||
// for a class named some levels off the colors of its flat areas ([`area_colors`]), within `PURE`
|
||||
// of one of those colors, or within `PURE` of the class as named with `prev` too (the class as
|
||||
// named lies across at least two pixels there, which the pixels of a black-to-white edge do not).
|
||||
let within = |prev: usize, j: usize, c: usize| match colors.get(c) {
|
||||
Some(cs) if !cs.is_empty() => pix.get(j).is_some_and(|q| near_area_color(q, cs)) || (named(prev, c) && named(j, c)),
|
||||
_ => named(j, c),
|
||||
};
|
||||
// The neighbor of pixel `j` near the line that lies farthest toward `a` (`toward` -1) or `b`
|
||||
// (`toward` 1), with its place.
|
||||
let next = |j: usize, toward: f64| around(j).filter_map(|n| at(n).map(|tn| (n, tn))).max_by(|p, q| (p.1 * toward).total_cmp(&(q.1 * toward)));
|
||||
@@ -368,19 +388,22 @@ fn across_edge(
|
||||
// pixel before (the first step is taken whatever `i` itself is). `None` when it does not get more
|
||||
// than `PURE` past `tc` in [`WALK`] steps; otherwise the steps to the first pixel past it that
|
||||
// stands for `class` (`stands_for`), or `NEARBY + 1`. With `halt`, the path also ends at a pixel
|
||||
// within `PURE` of that class that it comes to before it gets past `tc`.
|
||||
// of that class ([`within`]) that it comes to before it gets past `tc`.
|
||||
let walk = |toward: f64, tc: f64, class: usize, halt: Option<usize>| {
|
||||
let (mut cur, mut t) = (i, f64::NEG_INFINITY * toward);
|
||||
let mut past = false;
|
||||
for step in 1..=WALK {
|
||||
let prev = cur;
|
||||
match next(cur, toward) {
|
||||
Some((j, tj)) if (tj - t) * toward > 0.0 => (cur, t) = (j, tj),
|
||||
_ => break,
|
||||
}
|
||||
past = past || (t - tc) * toward * len > PURE;
|
||||
if !past && halt.is_some_and(|h| within(cur, h)) {
|
||||
// (A pixel of a flat area's color can lie past `tc` itself, when the class is named some
|
||||
// levels off that color: the path stops there when it was not past `tc` before.)
|
||||
if !past && halt.is_some_and(|h| within(prev, cur, h)) {
|
||||
break;
|
||||
}
|
||||
past = past || (t - tc) * toward * len > PURE;
|
||||
if past && stands_for.get(cur).copied().flatten() == Some(class) {
|
||||
return Some(step);
|
||||
}
|
||||
@@ -484,6 +507,39 @@ fn between_classes(pix: &[Px], flat: &[Option<usize>], mixed: &HashMap<[u32; 4],
|
||||
held
|
||||
}
|
||||
|
||||
/// For each class with a flat area [`PURE`] or more from it (as when it is named some levels off its
|
||||
/// color), the colors of its flat areas, each at least `PURE / 2` from the others: the flat areas that
|
||||
/// stand for it (`flat`) and those whose mix holds it most (`between`, [`between_classes`]). Empty for
|
||||
/// a class whose flat areas all come within `PURE` of it.
|
||||
fn area_colors(pix: &[Px], classes: &[[f64; 4]], flat: &[Option<usize>], between: &[[Option<usize>; 2]]) -> Vec<Vec<Px>> {
|
||||
let mut colors: Vec<Vec<Px>> = vec![Vec::new(); classes.len()];
|
||||
let mut off_named = vec![false; classes.len()];
|
||||
for (i, px) in pix.iter().enumerate() {
|
||||
let Some(c) = flat.get(i).copied().flatten().or_else(|| between.get(i).and_then(|b| b[0])) else { continue };
|
||||
let (Some(cv), Some(seen), Some(off)) = (classes.get(c), colors.get_mut(c), off_named.get_mut(c)) else { continue };
|
||||
*off = *off || (0..4).map(|j| (f64::from(px[j]) - cv[j]).powi(2)).sum::<f64>().sqrt() >= PURE;
|
||||
if !seen.iter().any(|q| distance(px, q) < PURE / 2.0) {
|
||||
seen.push(*px);
|
||||
}
|
||||
}
|
||||
for (seen, off) in colors.iter_mut().zip(off_named) {
|
||||
if !off {
|
||||
seen.clear();
|
||||
}
|
||||
}
|
||||
colors
|
||||
}
|
||||
|
||||
/// Whether `px` comes within [`PURE`] of one of `colors`.
|
||||
fn near_area_color(px: &Px, colors: &[Px]) -> bool {
|
||||
colors.iter().any(|q| distance(px, q) < PURE)
|
||||
}
|
||||
|
||||
/// How far apart two pixels are (in 0..1 premultiplied RGBA).
|
||||
fn distance(a: &Px, b: &Px) -> f64 {
|
||||
(0..4).map(|j| (f64::from(a[j]) - f64::from(b[j])).powi(2)).sum::<f64>().sqrt()
|
||||
}
|
||||
|
||||
/// For each of `k` classes, how far along each row the nearest pixel holding that class (`at`, the
|
||||
/// classes pixel `j` holds) lies from each pixel, up to `NEARBY + 1` when none lies within
|
||||
/// [`NEARBY`]. A row is worked out when it is first asked for, so a few pixels cost a few rows.
|
||||
@@ -612,6 +668,9 @@ fn in_flat_area(pix: &[Px], width: usize, height: usize, x: usize, y: usize) ->
|
||||
/// Each class's weight in a pixel's mix.
|
||||
type Weights = Vec<(usize, f32)>;
|
||||
|
||||
/// For each class, the colors of its flat areas that lie [`PURE`] or more from it ([`area_colors`]).
|
||||
type AreaColors = [Vec<Px>];
|
||||
|
||||
/// A mix tried for a pixel: its squared error with its penalty, the penalty, and its classes with
|
||||
/// their weights (the first `n`).
|
||||
#[derive(Clone, Copy)]
|
||||
@@ -803,6 +862,59 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_column_as_light_as_a_class_keeps_it_beside_a_flat_area_some_levels_off_it() {
|
||||
// As above, with a flat area of the gray some levels darker than it is named to the right of
|
||||
// the white: a column of `LINE` rows at the named value is still gray art.
|
||||
let classes = vec![[0.9, 0.9, 0.9, 1.0], [0.0, 0.0, 0.0, 1.0], [1.0, 1.0, 1.0, 1.0], [0.0, 0.0, 0.0, 0.0]];
|
||||
let mut pix = Vec::new();
|
||||
for _ in 0..LINE {
|
||||
pix.extend([[0.0f32, 0.0, 0.0, 1.0]; 5]);
|
||||
pix.push([0.9, 0.9, 0.9, 1.0]);
|
||||
pix.extend([[1.0f32, 1.0, 1.0, 1.0]; 5]);
|
||||
pix.extend([[0.87f32, 0.87, 0.87, 1.0]; 5]);
|
||||
}
|
||||
let (pl, _, _) = unmix(&pix, 16, LINE, &classes);
|
||||
for y in 0..LINE {
|
||||
assert_eq!(pl[0][y * 16 + 5], 1.0, "gray in row {y}");
|
||||
assert!(pl[0][y * 16 + 13] > 0.9, "the flat area in row {y}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_short_strip_as_light_as_a_class_traces_the_same_beside_a_flat_area_some_levels_off_it() {
|
||||
// Three rows of flat black, a strip 1 to 3 pixels wide at the gray's value and flat white: with
|
||||
// a flat area of the gray some levels darker than it is named to the right of the white, the
|
||||
// strip's pixels take the same mixes as without it (a strip 2 or 3 pixels wide stays gray).
|
||||
let classes = vec![[0.9, 0.9, 0.9, 1.0], [0.0, 0.0, 0.0, 1.0], [1.0, 1.0, 1.0, 1.0], [0.0, 0.0, 0.0, 0.0]];
|
||||
for wide in [1usize, 2, 3] {
|
||||
let rows = |area: bool| {
|
||||
let mut pix = Vec::new();
|
||||
for _ in 0..3 {
|
||||
pix.extend([[0.0f32, 0.0, 0.0, 1.0]; 5]);
|
||||
pix.extend(vec![[0.9f32, 0.9, 0.9, 1.0]; wide]);
|
||||
pix.extend([[1.0f32, 1.0, 1.0, 1.0]; 5]);
|
||||
if area {
|
||||
pix.extend([[0.87f32, 0.87, 0.87, 1.0]; 5]);
|
||||
}
|
||||
}
|
||||
pix
|
||||
};
|
||||
let w = 10 + wide;
|
||||
let (alone, _, _) = unmix(&rows(false), w, 3, &classes);
|
||||
let (beside, _, _) = unmix(&rows(true), w + 5, 3, &classes);
|
||||
for y in 0..3 {
|
||||
for x in 0..w {
|
||||
let mix = |pl: &[Vec<f32>], w: usize| pl.iter().take(3).map(|p| p[y * w + x]).collect::<Vec<_>>();
|
||||
assert_eq!(mix(&beside, w + 5), mix(&alone, w), "{wide} px strip: the mix at ({x}, {y})");
|
||||
}
|
||||
}
|
||||
if wide > 1 {
|
||||
assert_eq!(beside[0][w + 5 + 5], 1.0, "{wide} px strip: gray");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_edge_pixel_takes_no_class_beyond_both_classes_beside_it() {
|
||||
// A pale ground, a gray and a charcoal on one line: a row of flat gray, one edge pixel halfway
|
||||
|
||||
Reference in new issue
Block a user