mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-25 07:02:02 +00:00
In a gradient or a shaded area the palette colors on either side of a band border differ by more than the image changes across it, so pixel noise decided where the border ran, and it was smoothed over 0.5 px like a real edge: borders wobbled, kept stair steps and took many anchors. Each crack between two colors now records how much of the step between them the image makes next to it and a few pixels further out, how much the image changes there along the line between the two colors and in any direction, and how much it changes within each side (RGB distances, 0 to 255 a channel). A piece of outline is the contour of a smooth area when its cracks show less than half of the step; either more of it further out, a change along the line under 12, or no opaque pixels further out; some change within the sides, at least 1/32 of the change along the line; and a whole change under twice the change along the line, or under 24. Such a piece is smoothed over 4 px (a closed one over at most an eighth of its smaller width or height), keeps no corners, is fitted to at least 0.5 px and is checked against its smoothed points. The values are the same from either side of the crack. Grayscale reads each pixel as its gray, for this rule and for every edge's sub-pixel shift, which compared the pixels' colors with gray palette entries. Color traces of clean flat-color art and Black and White traces are unchanged. Specks rise by up to 0.8 per image on gradient and photo inputs, and Auto Color with Shapes leaves a 0.3 px gap there. Docs: docs/target-app-parity.md, docs/image-trace-quality.md.
829 lines
42 KiB
Rust
829 lines
42 KiB
Rust
use super::params::{Method, Mode, TraceParams};
|
||
use super::pipeline::TracedPath;
|
||
#[cfg(test)]
|
||
use super::pipeline::{bw_tones, left_out};
|
||
use super::raster::Raster;
|
||
use super::{
|
||
AtomicUsize, Crack, Loop, Ordering, PathData, Quantized, TRANSPARENT, TracedGradient, TracedShape, centerline, contour, fit, logo, shapes,
|
||
trace_mask,
|
||
};
|
||
#[cfg(test)]
|
||
use super::{denoise, quantize};
|
||
|
||
/// What tracing one colour layer needs.
|
||
pub(crate) struct Layers<'a> {
|
||
pub(crate) q: &'a Quantized,
|
||
/// The label each layer traces a pixel as: `q.labels`, or with Ignore Color for the background
|
||
/// only, those with the background [`TRANSPARENT`]. Edges are placed by `q.labels`.
|
||
pub(crate) traced: &'a [u16],
|
||
/// Each palette entry's layer, bottom (0) up.
|
||
pub(crate) rank: &'a [usize],
|
||
pub(crate) params: &'a TraceParams,
|
||
pub(crate) img: &'a Raster,
|
||
/// For each palette entry, whether Ignore Color or Keep Colors leaves it out (see `pipeline::left_out`).
|
||
pub(crate) left_out: Vec<bool>,
|
||
/// The box each palette entry's pixels lie in: `(x0, y0, x1, y1)`, the far sides excluded.
|
||
pub(crate) boxes: Vec<Option<(usize, usize, usize, usize)>>,
|
||
/// Gradients: the gradient each palette entry is, if it is one.
|
||
pub(crate) gradients: &'a [Option<TracedGradient>],
|
||
/// Black and White: the luminance of white and of black (see `bw_tones`).
|
||
pub(crate) bw_tones: [f64; 2],
|
||
/// How outlines and centre lines are fitted.
|
||
pub(crate) style: logo::Style,
|
||
pub(crate) w: usize,
|
||
pub(crate) h: usize,
|
||
/// Anchor points made so far, by every layer.
|
||
pub(crate) anchors: AtomicUsize,
|
||
pub(crate) max_anchors: usize,
|
||
}
|
||
|
||
impl Layers<'_> {
|
||
/// Ignore White, and the colors Keep Colors does not list: such a color is no part of any
|
||
/// layer (stacked layers reach under the layers above them, and must not reach under a color
|
||
/// that is left out). With `ignore_background` the background is [`TRANSPARENT`] in `traced`,
|
||
/// and the color's other areas are traced.
|
||
fn ignored(&self, label: u16) -> bool {
|
||
self.left_out.get(label as usize).copied().unwrap_or(false)
|
||
}
|
||
/// The region across a crack, for cutting outlines at their junctions: the outside pixel's
|
||
/// colour. Stacked layers end on many colours at once and share no edge: one neighbour.
|
||
fn neighbour(&self, c: Crack) -> u32 {
|
||
if self.params.method == Method::Overlapping {
|
||
return 0;
|
||
}
|
||
c.at(1).and_then(|i| self.q.labels.get(i)).map_or(u32::from(TRANSPARENT), |&l| u32::from(l))
|
||
}
|
||
/// The junction at pixel corner `(x, y)`, if there is one: where three or four regions
|
||
/// meet, or two parts of a region touch by the corner only, or an edge reaches the image's
|
||
/// border. It lies at the corner, moved across by what the edges that run up and down from it
|
||
/// say, and up or down by what those to its left and right say (the mean, where there are
|
||
/// two). It depends on the corner alone, so every region around it gets the same point; on the
|
||
/// image's border it stays on the border.
|
||
fn junction(&self, x: i32, y: i32) -> Option<vectorcraft_geom::Point> {
|
||
let label = |px: i32, py: i32| Crack::new((px, py), (1, 0), self.w, self.h).at(0).and_then(|i| self.q.labels.get(i).copied());
|
||
// The four pixels around the corner (`None` off the image), and the edges between them.
|
||
let around = [label(x - 1, y - 1), label(x, y - 1), label(x, y), label(x - 1, y)];
|
||
let edges = (0..4).filter(|&i| around[i] != around[(i + 1) % 4]).count();
|
||
let apart = |a: usize, b: usize, c: usize, d: usize| around[a] == around[c] && around[b] != around[a] && around[d] != around[a];
|
||
let touching = apart(0, 1, 2, 3) || apart(1, 2, 3, 0);
|
||
// (Stacked layers share no edges: they are cut at the image's border only.)
|
||
if edges < 3 && !touching || self.params.method == Method::Overlapping {
|
||
return None;
|
||
}
|
||
// The shift of the crack between `a` and the pixel `step` from it, if both are in the
|
||
// image and of different regions.
|
||
let shift = |a: (i32, i32), step: (i32, i32)| {
|
||
let (la, lb) = (label(a.0, a.1)?, label(a.0 + step.0, a.1 + step.1)?);
|
||
(la != lb).then(|| self.shift(Crack::new(a, step, self.w, self.h)).clamp(-0.5, 0.5))
|
||
};
|
||
let mean = |a: Option<f64>, b: Option<f64>| match (a, b) {
|
||
(Some(a), Some(b)) => (a + b) / 2.0,
|
||
(Some(v), None) | (None, Some(v)) => v,
|
||
(None, None) => 0.0,
|
||
};
|
||
let dx = mean(shift((x - 1, y - 1), (1, 0)), shift((x - 1, y), (1, 0)));
|
||
let dy = mean(shift((x - 1, y - 1), (0, 1)), shift((x, y - 1), (0, 1)));
|
||
Some(vectorcraft_geom::Point::new(f64::from(x) + dx, f64::from(y) + dy))
|
||
}
|
||
/// How far the edge lies from a crack between two pixels, towards the outside one (−0.5 to 0.5).
|
||
///
|
||
/// Each pixel of the column across the crack is read as a blend of the tones on its two sides
|
||
/// (the two pixels' palette colours; in Black and White the luminance of the image's black and of
|
||
/// the white pixels). The edge is where the blends, summed, put it: as far out as there is
|
||
/// inside tone in the outside pixels, less the outside tone in the inside ones (two pixels each
|
||
/// way, while they are on the same side and no more blended than the pixel before: more would be
|
||
/// another edge's doing).
|
||
///
|
||
/// Where the inside is at most three pixels deep with the outside's colour behind it (a thin
|
||
/// line), its two edges share those pixels and cannot be read apart; the inside tone of the
|
||
/// whole column gives the line's middle (its centroid) and width (its sum) instead. A thin gap
|
||
/// between two parts of the inside is read the same way, with the tones swapped.
|
||
///
|
||
/// Black and White then moves the edge to where the luminance crosses the threshold: by the
|
||
/// threshold's distance from the middle tone, over how steep the edge is (nothing for a
|
||
/// threshold in the middle, more the softer the edge).
|
||
///
|
||
/// The estimate is read from each side of the crack and the two readings averaged, so that
|
||
/// the shift is exactly antisymmetric (`a - b == -(b - a)` in floating point) and the two
|
||
/// regions of a crack get the same points. A single reading is not: it looks for a thin run on
|
||
/// the inside before one on the outside, so where both sides are thin (a one- or two-pixel
|
||
/// sliver beside another, as in soft gradients and noisy scans) each region got its own shift,
|
||
/// their shared piece different points, and the abutting curves left a sliver uncovered.
|
||
fn shift(&self, c: Crack) -> f64 {
|
||
(self.shift_one_side(c) - self.shift_one_side(c.flipped())) / 2.0
|
||
}
|
||
|
||
/// [`Self::shift`] read from the crack's inside pixel only.
|
||
fn shift_one_side(&self, c: Crack) -> f64 {
|
||
let label = |k: i32| c.at(k).and_then(|i| self.q.labels.get(i).copied()).filter(|&l| l != TRANSPARENT);
|
||
let (Some(la), Some(lb)) = (label(0), label(1)) else { return 0.0 };
|
||
if la == lb {
|
||
return 0.0;
|
||
}
|
||
let bw = self.params.mode == Mode::BlackAndWhite;
|
||
// Black and White: black is the image's black; white is the lightest pixel within a few
|
||
// pixels of the crack, the paper as it is here (a scan's paper is darker in one corner than
|
||
// in another, and read against the image's lightest white it would count as part ink).
|
||
let black = self.bw_tones[1];
|
||
let white = if bw {
|
||
let light = (-3..=4)
|
||
.filter_map(|k| c.at(k).and_then(|i| self.img.rgba.get(i * 4..i * 4 + 3)))
|
||
.map(|p| (f64::from(p[0]) * 299.0 + f64::from(p[1]) * 587.0 + f64::from(p[2]) * 114.0) / 1000.0);
|
||
light.fold(f64::NEG_INFINITY, f64::max).clamp((black + 32.0).min(self.bw_tones[0]), self.bw_tones[0])
|
||
} else {
|
||
self.bw_tones[0]
|
||
};
|
||
let (ca, cb) = if bw {
|
||
let tone = |l: u16| [if l == 1 { black } else { white }; 3];
|
||
(tone(la), tone(lb))
|
||
} else {
|
||
let (Some(ca), Some(cb)) = (self.own_color(la, c, 0), self.own_color(lb, c, 1)) else { return 0.0 };
|
||
(ca, cb)
|
||
};
|
||
let d = [0, 1, 2].map(|k| cb[k] - ca[k]);
|
||
let len2: f64 = d.iter().map(|v| v * v).sum();
|
||
if len2 <= 0.0 {
|
||
return 0.0;
|
||
}
|
||
let gray = self.params.mode == Mode::Grayscale;
|
||
// How much of the inside tone (0 to 1) the pixel `k` steps out has; none off the image.
|
||
// Grayscale reads each pixel as the gray it traces it as.
|
||
let inside = |k: i32| {
|
||
let Some(&[r, g, b, a]) = c.at(k).and_then(|i| self.img.rgba.get(i * 4..i * 4 + 4)) else { return 0.0 };
|
||
let p = if gray { [f64::from(super::quantize::luma([r, g, b, a])); 3] } else { [f64::from(r), f64::from(g), f64::from(b)] };
|
||
let p = if bw { [(p[0] * 299.0 + p[1] * 587.0 + p[2] * 114.0) / 1000.0; 3] } else { p };
|
||
1.0 - ((0..3).map(|k| (p[k] - ca[k]) * d[k]).sum::<f64>() / len2).clamp(0.0, 1.0)
|
||
};
|
||
// The edge from a thin run of `n` pixels starting at pixel `from` and going `dir`, by the
|
||
// amounts `tone` of the run's own tone in it and in the two pixels at either end of it.
|
||
// Returns the run's middle and width, measured from the crack towards the outside.
|
||
let thin = |from: i32, dir: i32, n: i32, tone: &dyn Fn(i32) -> f64| {
|
||
let (near, far) = (from - dir, from + n * dir);
|
||
let mut cells: Vec<(i32, f64)> = (0..n).map(|j| from + j * dir).chain([near, far]).map(|k| (k, tone(k))).collect();
|
||
for (edge, out) in [(near, -dir), (far, dir)] {
|
||
if label(edge + out) == label(edge) {
|
||
cells.push((edge + out, tone(edge + out).min(tone(edge))));
|
||
}
|
||
}
|
||
let width: f64 = cells.iter().map(|c| c.1).sum();
|
||
// Pixel `k`'s centre is `k - 0.5` from the crack.
|
||
(width > 1e-6).then(|| (cells.iter().map(|c| c.1 * (f64::from(c.0) - 0.5)).sum::<f64>() / width, width))
|
||
};
|
||
// How deep each side is here: up to three pixels of its own label, then the other's.
|
||
let depth = |dir: i32, own: u16, other: u16| {
|
||
let start = if dir < 0 { 0 } else { 1 };
|
||
let n = (1..=3).take_while(|j| label(start + j * dir) == Some(own)).count() as i32 + 1;
|
||
(n <= 3 && label(start + n * dir) == Some(other)).then_some(n)
|
||
};
|
||
let mut steep = 1.0;
|
||
let mut shift = if let Some((middle, width)) = depth(-1, la, lb).and_then(|n| thin(0, -1, n, &inside)) {
|
||
middle + width / 2.0
|
||
} else if let Some((middle, width)) = depth(1, lb, la).and_then(|n| thin(1, 1, n, &|k| 1.0 - inside(k))) {
|
||
middle - width / 2.0
|
||
} else {
|
||
let (a1, b1) = (1.0 - inside(0), inside(1));
|
||
let a2 = (label(-1) == Some(la)).then(|| (1.0 - inside(-1)).min(a1));
|
||
let b2 = (label(2) == Some(lb)).then(|| inside(2).min(b1));
|
||
// The edge's steepness per pixel: the square of the change over two pixels (1 for an
|
||
// anti-aliased edge, less for a blurred one).
|
||
let change = [a2.map(|v| 1.0 - v - b1), b2.map(|v| 1.0 - a1 - v)].into_iter().flatten().fold(f64::NAN, f64::max);
|
||
if !change.is_nan() {
|
||
steep = (change * change).clamp(0.35, 1.0);
|
||
}
|
||
b1 + b2.unwrap_or(0.0) - a1 - a2.unwrap_or(0.0)
|
||
};
|
||
if bw {
|
||
// The share of black at which a pixel turns black.
|
||
let turn = ((white - f64::from(self.params.threshold)) / (white - black).max(1.0)).clamp(0.02, 0.98);
|
||
shift += if la == 1 { 1.0 } else { -1.0 } * (0.5 - turn) / steep;
|
||
}
|
||
shift
|
||
}
|
||
|
||
/// The color of label `l` at the pixel `k` steps out from the inside pixel of crack `c`: its
|
||
/// palette entry's, or its gradient's color there.
|
||
fn own_color(&self, l: u16, c: Crack, k: i32) -> Option<[f64; 3]> {
|
||
let flat = self.q.palette.get(l as usize)?.map(f64::from);
|
||
let Some(g) = self.gradients.get(l as usize).and_then(Option::as_ref) else { return Some(flat) };
|
||
let i = c.at(k)?;
|
||
Some(g.color_at(vectorcraft_geom::Point::new((i % self.w) as f64 + 0.5, (i / self.w) as f64 + 0.5)))
|
||
}
|
||
|
||
/// What the image does across a crack, for `subpixel::low_contrast`. Distances between colors
|
||
/// are RGB distances (0 to 255 a channel), and Grayscale reads each pixel as the gray it traces
|
||
/// it as. In order:
|
||
///
|
||
/// - the share of the step from the inside color to the outside one that the image makes near
|
||
/// the crack, measured along the line between the two colors: from the mean of the two pixels
|
||
/// behind the inside pixel to the mean of the two beyond the outside one, each side only as
|
||
/// far as its own label goes;
|
||
/// - the share further out, from the next two pixels on one side to the next two on the other,
|
||
/// whatever their labels (not a number where a side has none);
|
||
/// - the near change along that line, as a distance;
|
||
/// - the whole near change, in any direction, as a distance;
|
||
/// - how much the image changes within each side: the distance between the two pixels behind
|
||
/// the inside pixel and between the two beyond the outside one, where both have the side's
|
||
/// label, averaged (not a number where neither side has two such pixels).
|
||
///
|
||
/// Where a side has no pixel near the crack, and in Black and White, whose two tones are not the
|
||
/// colors of its areas, it is [`subpixel::EDGE_STEP`]. Exactly the same read from either side.
|
||
fn step(&self, c: Crack) -> [f64; 5] {
|
||
use super::subpixel::EDGE_STEP;
|
||
let label = |k: i32| c.at(k).and_then(|i| self.q.labels.get(i).copied()).filter(|&l| l != TRANSPARENT);
|
||
let (Some(la), Some(lb)) = (label(0), label(1)) else { return EDGE_STEP };
|
||
if la == lb || self.params.mode == Mode::BlackAndWhite {
|
||
return EDGE_STEP;
|
||
}
|
||
let (Some(ca), Some(cb)) = (self.own_color(la, c, 0), self.own_color(lb, c, 1)) else { return EDGE_STEP };
|
||
let d = [0, 1, 2].map(|k| cb[k] - ca[k]);
|
||
let len2: f64 = d.iter().map(|v| v * v).sum();
|
||
if len2 <= 0.0 {
|
||
return EDGE_STEP;
|
||
}
|
||
let gray = self.params.mode == Mode::Grayscale;
|
||
// The pixel `k`, while it is in the image and not transparent (and has label `l`, if one is
|
||
// given).
|
||
let pixel = |k: i32, l: Option<u16>| {
|
||
label(k).filter(|&x| l.is_none_or(|l| x == l))?;
|
||
let &[r, g, b, a] = c.at(k).and_then(|i| self.img.rgba.get(i * 4..i * 4 + 4))? else { return None };
|
||
Some(if gray { [f64::from(super::quantize::luma([r, g, b, a])); 3] } else { [f64::from(r), f64::from(g), f64::from(b)] })
|
||
};
|
||
// The mean color of the pixels `ks` up to the first one `pixel` refuses.
|
||
let mean = |ks: [i32; 2], l: Option<u16>| {
|
||
let (mut sum, mut n) = ([0.0; 3], 0.0);
|
||
for p in ks.iter().map_while(|&k| pixel(k, l)) {
|
||
for (s, v) in sum.iter_mut().zip(p) {
|
||
*s += v;
|
||
}
|
||
n += 1.0;
|
||
}
|
||
(n > 0.0).then(|| sum.map(|s| s / n))
|
||
};
|
||
let distance = |a: [f64; 3], b: [f64; 3]| (0..3).map(|k| (b[k] - a[k]).powi(2)).sum::<f64>().sqrt();
|
||
// From `a` to `b` along the line from the inside color to the outside one, in its lengths.
|
||
let along = |a: [f64; 3], b: [f64; 3]| (0..3).map(|k| (b[k] - a[k]) * d[k]).sum::<f64>() / len2;
|
||
let (Some(a), Some(b)) = (mean([-1, -2], Some(la)), mean([2, 3], Some(lb))) else { return EDGE_STEP };
|
||
let far = match (mean([-3, -4], None), mean([4, 5], None)) {
|
||
(Some(a), Some(b)) => along(a, b),
|
||
_ => f64::NAN,
|
||
};
|
||
let (mut within, mut sides) = (0.0, 0.0);
|
||
for ([j, k], l) in [([-1, -2], la), ([2, 3], lb)] {
|
||
if let (Some(p), Some(q)) = (pixel(j, Some(l)), pixel(k, Some(l))) {
|
||
within += distance(p, q);
|
||
sides += 1.0;
|
||
}
|
||
}
|
||
let within = if sides > 0.0 { within / sides } else { f64::NAN };
|
||
let near = along(a, b);
|
||
[near, far, near * len2.sqrt(), distance(a, b), within]
|
||
}
|
||
|
||
/// Whether the pixels inside `l` (a loop along pixel edges) all have one label.
|
||
fn one_colour_within(&self, l: &Loop) -> bool {
|
||
let mut first = None;
|
||
let (y0, y1) = l.pts.iter().fold((i32::MAX, i32::MIN), |m, p| (m.0.min(p.1), m.1.max(p.1)));
|
||
for y in y0..y1 {
|
||
// The loop's upright edges that pass this row of pixels, left to right: the pixels
|
||
// between the first and the second are inside, and so on.
|
||
let mut xs: Vec<i32> = l
|
||
.pts
|
||
.iter()
|
||
.zip(l.pts.iter().cycle().skip(1))
|
||
.filter(|(a, b)| a.0 == b.0 && a.1.min(b.1) <= y && y < a.1.max(b.1))
|
||
.map(|(a, _)| a.0)
|
||
.collect();
|
||
xs.sort_unstable();
|
||
for pair in xs.as_chunks::<2>().0 {
|
||
for x in pair[0]..pair[1] {
|
||
let label = usize::try_from(y)
|
||
.ok()
|
||
.zip(usize::try_from(x).ok())
|
||
.and_then(|(y, x)| self.q.labels.get(y * self.w + x).copied().filter(|_| x < self.w));
|
||
if label.is_none() || *first.get_or_insert(label) != label {
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
first.is_some()
|
||
}
|
||
|
||
/// Shapes, Abutting: the shape the outline `sp` (fitted from the loop `l`, `hard` as that
|
||
/// loop is) is, where it may be one. The edge is shared by the regions on its two sides, so
|
||
/// both must become that same shape or neither: only a closed edge between two regions, a
|
||
/// region whose whole outline is the hole of one other (no junction on it, not on the image's
|
||
/// border), and only a shape inside the image. The shape is read from the outline run one way
|
||
/// from one start, so the region and the hole it fills get the very same one.
|
||
fn shared_shape(&self, l: &Loop, sp: &vectorcraft_geom::SubPath, hard: bool) -> Option<TracedShape> {
|
||
let (w, h) = (i32::try_from(self.w).ok()?, i32::try_from(self.h).ok()?);
|
||
if l.pts.iter().any(|&(x, y)| x <= 0 || y <= 0 || x >= w || y >= h || self.junction(x, y).is_some()) {
|
||
return None;
|
||
}
|
||
let mut one_way = sp.clone();
|
||
if one_way.signed_area() < 0.0 {
|
||
one_way.reverse();
|
||
}
|
||
let first = one_way.anchors.iter().enumerate().min_by(|a, b| a.1.p.x.total_cmp(&b.1.p.x).then(a.1.p.y.total_cmp(&b.1.p.y)))?.0;
|
||
one_way.anchors.rotate_left(first);
|
||
let shape = shapes::recognise(&one_way, hard)?;
|
||
let (wf, hf) = (self.w as f64, self.h as f64);
|
||
let inside = shape
|
||
.path()
|
||
.subpaths
|
||
.iter()
|
||
.flat_map(|s| &s.anchors)
|
||
.flat_map(|a| [a.p, a.h_in, a.h_out])
|
||
.all(|p| (0.0..=wf).contains(&p.x) && (0.0..=hf).contains(&p.y));
|
||
inside.then_some(shape)
|
||
}
|
||
|
||
/// The paths of layer `r` (palette entry `ci`), or `None` once the anchors made pass the
|
||
/// maximum.
|
||
pub(crate) fn layer(&self, r: usize, ci: usize) -> Option<Vec<TracedPath>> {
|
||
let params = self.params;
|
||
let color = *self.q.palette.get(ci)?;
|
||
let overlapping = params.method == Method::Overlapping;
|
||
let (w, h) = (self.w, self.h);
|
||
// Create Strokes: this colour's lines, left out of its areas.
|
||
let (thin, lines) = if params.strokes {
|
||
// The ink the lines are read against: the layer's colour; Black and White's own black.
|
||
let ink = if params.mode == Mode::BlackAndWhite { [self.bw_tones[usize::from(ci == 1)]; 3] } else { color.map(f64::from) };
|
||
centerline::lines(self.traced, u16::try_from(ci).unwrap_or(TRANSPARENT), w, h, params.stroke_width, self.img, ink, &self.style)
|
||
} else {
|
||
(vec![], vec![])
|
||
};
|
||
// The layer is traced within the box its pixels lie in, not the whole image.
|
||
let part = |l: u16| if overlapping { self.rank.get(l as usize).is_some_and(|&lr| lr >= r) && !self.ignored(l) } else { l as usize == ci };
|
||
let (mut x0, mut y0, mut x1, mut y1) = (w, h, 0, 0);
|
||
for (l, b) in self.boxes.iter().enumerate() {
|
||
if let Some(b) = b.filter(|_| part(l as u16)) {
|
||
(x0, y0, x1, y1) = (x0.min(b.0), y0.min(b.1), x1.max(b.2), y1.max(b.3));
|
||
}
|
||
}
|
||
if x0 >= x1 || y0 >= y1 {
|
||
return Some(vec![]);
|
||
}
|
||
let (bw, bh) = (x1 - x0, y1 - y0);
|
||
let mut mask = Vec::with_capacity(bw * bh);
|
||
for y in y0..y1 {
|
||
let row = self.traced.get(y * w + x0..y * w + x1)?;
|
||
mask.extend(row.iter().enumerate().map(|(k, &l)| l != TRANSPARENT && !thin.get(y * w + x0 + k).copied().unwrap_or(false) && part(l)));
|
||
}
|
||
// Overlapping: a part of the mask with no pixel of this color holds only colors above it,
|
||
// whose own layers fill it, and is left out. The layer reaches under the colors above it
|
||
// only within the areas that hold its own color.
|
||
if overlapping {
|
||
let (comp, sizes) = contour::label(&mask, bw, bh);
|
||
let mut own = vec![false; sizes.len()];
|
||
for (k, &c) in comp.iter().enumerate() {
|
||
if self.q.labels.get((y0 + k / bw) * w + x0 + k % bw).is_some_and(|&l| l as usize == ci)
|
||
&& let Some(o) = own.get_mut(c as usize)
|
||
{
|
||
*o = true;
|
||
}
|
||
}
|
||
for (m, &c) in mask.iter_mut().zip(&comp) {
|
||
*m = *m && own.get(c as usize).copied().unwrap_or(false);
|
||
}
|
||
}
|
||
// Without Fills, areas are outlined with a 1 px stroke.
|
||
let area_stroke = (params.strokes && !params.fills).then_some(1.0);
|
||
let min_hole = params.noise.max(1) as i64;
|
||
let mut paths = vec![];
|
||
let mut add = |path: PathData, pixels: usize, stroke: Option<f64>, shape: Option<TracedShape>, square_caps: bool| {
|
||
let n = path.anchor_count();
|
||
if self.anchors.fetch_add(n, Ordering::Relaxed).saturating_add(n) > self.max_anchors {
|
||
return false;
|
||
}
|
||
// (A gradient fills; a stroked line of it is its middle colour.)
|
||
let gradient = self.gradients.get(ci).cloned().flatten().filter(|_| stroke.is_none());
|
||
paths.push(TracedPath { path, color, pixels, stroke, shape, gradient, square_caps });
|
||
true
|
||
};
|
||
let mut comps = trace_mask(&mask, bw, bh);
|
||
drop(mask);
|
||
for comp in &mut comps {
|
||
for l in std::iter::once(&mut comp.outer).chain(&mut comp.holes) {
|
||
for p in &mut l.pts {
|
||
*p = (p.0 + x0 as i32, p.1 + y0 as i32);
|
||
}
|
||
}
|
||
}
|
||
// One component's paths: a pure function of its loops and the shared read-only state, so components are fitted in
|
||
// parallel and then added in order.
|
||
let fit_comp = |comp: &contour::Component| -> Option<(PathData, Option<TracedShape>)> {
|
||
// (`hard`: some of the outline has no anti-aliasing, and is known less exactly.)
|
||
let hard = std::cell::Cell::new(false);
|
||
// (Each loop fitted, and whether its own outline is hard.)
|
||
let fit = |l: &Loop| {
|
||
let (sp, is_hard) =
|
||
fit::fit_loop(l, w, h, &|c| (self.shift(c), self.neighbour(c), self.step(c)), &|x, y| self.junction(x, y), &self.style)?;
|
||
// Every trace straightens the pieces of an outline that lie close to a line (Snap
|
||
// Curves To Lines straightens more of them), so the outline of a single pixel or
|
||
// of a region one pixel thick can have all its pieces on one chord, and the loop
|
||
// then has no area. Filled, such a loop paints nothing, and it is left out.
|
||
// Without Fills its 1 px stroke draws it, so it is kept. (`fit::fit_closed` puts
|
||
// back the positioned outline of a loop that is one closed piece when its fit has
|
||
// no area.) The area is summed about the loop's first anchor.
|
||
if area_stroke.is_none() {
|
||
use kurbo::ParamCurveArea;
|
||
let origin = sp.anchors.first()?.p.to_vec2();
|
||
let area: f64 = (0..sp.segment_count()).map(|i| (kurbo::Affine::translate(-origin) * sp.segment(i)).signed_area()).sum();
|
||
if area.abs() < 1e-6 {
|
||
return None;
|
||
}
|
||
}
|
||
hard.set(hard.get() || is_hard);
|
||
Some((sp, is_hard))
|
||
};
|
||
let (outer, _) = fit(&comp.outer)?;
|
||
let mut subs = vec![outer];
|
||
for hole in &comp.holes {
|
||
if hole.area2.abs() / 2 < min_hole && params.noise > 0 {
|
||
continue;
|
||
}
|
||
let Some((mut sp, own_hard)) = fit(hole) else { continue };
|
||
// Shapes: a hole that is a shape is that shape, as what fills it is (the two are
|
||
// one outline: they stay one). Only where one colour fills it: the parts of
|
||
// several are not that shape, and would no longer meet it. Abutting, only where the
|
||
// region that fills it is that shape too (see `shared_shape`).
|
||
let exact = if !params.shapes || area_stroke.is_some() || !self.one_colour_within(hole) {
|
||
None
|
||
} else if overlapping {
|
||
shapes::recognise(&sp, hard.get())
|
||
} else {
|
||
// (As hard as this outline is, as the region that fills it reads it.)
|
||
self.shared_shape(hole, &sp, own_hard)
|
||
};
|
||
if let Some(exact) = exact.and_then(|shape| shape.path().subpaths.into_iter().next()) {
|
||
// (Which way round each goes: by the area of the polygon through points along it.)
|
||
let turn = |sp: &vectorcraft_geom::SubPath| {
|
||
use vectorcraft_geom::ParamCurve;
|
||
let pts: Vec<_> =
|
||
(0..sp.segment_count()).flat_map(|i| (0..6).map(move |k| sp.segment(i).eval(f64::from(k) / 6.0).to_vec2())).collect();
|
||
pts.iter().zip(pts.iter().cycle().skip(1)).map(|(a, b)| a.cross(*b)).sum::<f64>()
|
||
};
|
||
let mut exact = exact;
|
||
if (turn(&exact) < 0.0) != (turn(&sp) < 0.0) {
|
||
exact.reverse();
|
||
}
|
||
sp = exact;
|
||
}
|
||
subs.push(sp);
|
||
}
|
||
// Shapes: a filled outline without holes that is a shape becomes that shape.
|
||
let shape = match subs.as_slice() {
|
||
[only] if params.shapes && area_stroke.is_none() && overlapping => shapes::recognise(only, hard.get()),
|
||
// Abutting: only an island in one other region, whose hole it is (see `shared_shape`).
|
||
[only] if params.shapes && area_stroke.is_none() && comp.holes.is_empty() => self.shared_shape(&comp.outer, only, hard.get()),
|
||
_ => None,
|
||
};
|
||
let path = shape.map_or_else(|| PathData::new(subs), |s| s.path());
|
||
Some((path, shape))
|
||
};
|
||
// In chunks, so that the anchor budget still stops the work early: a hostile image is refused after fitting at most
|
||
// the rest of one chunk, not after every component is fitted.
|
||
let accepted = fit_in_chunks(&comps, FIT_CHUNK, fit_comp, |comp, fitted| {
|
||
let Some((path, shape)) = fitted else { return true };
|
||
add(path, comp.pixels, area_stroke, shape, false)
|
||
});
|
||
if !accepted {
|
||
return None;
|
||
}
|
||
for line in lines {
|
||
if !add(line.path, line.pixels, Some(line.width), None, line.square) {
|
||
return None;
|
||
}
|
||
}
|
||
Some(paths)
|
||
}
|
||
}
|
||
|
||
/// Test probe: every crack between two labels of `img` quantised as `trace` does (no gradients),
|
||
/// as `[shift, shift from the other side, one-sided reading, one-sided reading from the other
|
||
/// side]`.
|
||
#[cfg(test)]
|
||
pub(crate) fn shift_probe(img: &Raster, params: &TraceParams) -> Vec<[f64; 4]> {
|
||
let (w, h) = (img.width as usize, img.height as usize);
|
||
let mut q = quantize(img, params);
|
||
denoise(&mut q.labels, w, h, params.noise as usize);
|
||
let n = q.palette.len();
|
||
let rank: Vec<usize> = (0..n).collect();
|
||
let gradients = vec![None; n];
|
||
let job = Layers {
|
||
q: &q,
|
||
traced: &q.labels,
|
||
rank: &rank,
|
||
params,
|
||
img,
|
||
left_out: left_out(&q.palette, params, None, &[]),
|
||
boxes: vec![None; n],
|
||
gradients: &gradients,
|
||
bw_tones: bw_tones(img, &q),
|
||
style: params.outline_style(),
|
||
w,
|
||
h,
|
||
anchors: AtomicUsize::new(0),
|
||
max_anchors: usize::MAX,
|
||
};
|
||
let mut out = vec![];
|
||
for y in 0..h as i32 {
|
||
for x in 0..w as i32 {
|
||
for step in [(1, 0), (0, 1)] {
|
||
let c = Crack::new((x, y), step, w, h);
|
||
let (Some(a), Some(b)) = (c.at(0), c.at(1)) else { continue };
|
||
if q.labels[a] != q.labels[b] {
|
||
out.push([job.shift(c), job.shift(c.flipped()), job.shift_one_side(c), job.shift_one_side(c.flipped())]);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
out
|
||
}
|
||
|
||
/// How many components of a layer are fitted at a time (see `fit_in_chunks`).
|
||
const FIT_CHUNK: usize = 256;
|
||
|
||
/// `fit` of each of `items` over the available cores, `chunk` items at a time, each result handed to `accept` in item
|
||
/// order. Stops after the chunk in which `accept` returns false, and says whether it never did.
|
||
fn fit_in_chunks<T: Sync, R: Send>(items: &[T], chunk: usize, fit: impl Fn(&T) -> R + Sync, mut accept: impl FnMut(&T, R) -> bool) -> bool {
|
||
for part in items.chunks(chunk.max(1)) {
|
||
// (A few components are not worth starting threads for.)
|
||
let fitted = if part.len() < 16 { part.iter().map(&fit).collect() } else { parallel_map(part, &fit) };
|
||
for (item, result) in part.iter().zip(fitted) {
|
||
if !accept(item, result) {
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
true
|
||
}
|
||
|
||
/// `f` of each of `items`, in order, spread over the available cores.
|
||
pub(crate) fn parallel_map<T: Sync, R: Send>(items: &[T], f: impl Fn(&T) -> R + Sync) -> Vec<R> {
|
||
parallel_map_at_most(usize::MAX, items, f)
|
||
}
|
||
|
||
/// `f` of each of `items`, in order, spread over the available cores, at most `at_a_time` at once.
|
||
#[cfg(not(target_arch = "wasm32"))]
|
||
pub(crate) fn parallel_map_at_most<T: Sync, R: Send>(at_a_time: usize, items: &[T], f: impl Fn(&T) -> R + Sync) -> Vec<R> {
|
||
let threads = std::thread::available_parallelism().map_or(1, |n| n.get()).min(items.len()).min(at_a_time);
|
||
if threads <= 1 {
|
||
return items.iter().map(f).collect();
|
||
}
|
||
let next = AtomicUsize::new(0);
|
||
let mut done: Vec<(usize, R)> = std::thread::scope(|s| {
|
||
let workers: Vec<_> = (0..threads)
|
||
.map(|_| {
|
||
s.spawn(|| {
|
||
let mut mine = vec![];
|
||
loop {
|
||
let i = next.fetch_add(1, Ordering::Relaxed);
|
||
let Some(item) = items.get(i) else { break };
|
||
mine.push((i, f(item)));
|
||
}
|
||
mine
|
||
})
|
||
})
|
||
.collect();
|
||
// A worker's panic goes on in this thread, as if it had run here (the engine's guard
|
||
// reports it).
|
||
workers.into_iter().flat_map(|w| w.join().unwrap_or_else(|e| std::panic::resume_unwind(e))).collect()
|
||
});
|
||
done.sort_by_key(|(i, _)| *i);
|
||
done.into_iter().map(|(_, r)| r).collect()
|
||
}
|
||
|
||
/// `f` of each of `items`, in order (no threads on the web).
|
||
#[cfg(target_arch = "wasm32")]
|
||
pub(crate) fn parallel_map_at_most<T: Sync, R: Send>(_: usize, items: &[T], f: impl Fn(&T) -> R + Sync) -> Vec<R> {
|
||
items.iter().map(f).collect()
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
|
||
/// `f` of the layers of a `w × 8` image whose pixels in column `x` are the color `color(x)` and
|
||
/// have the label `label(x)`, with the palette `palette`, and of the cracks between columns
|
||
/// `split - 1` and `split`.
|
||
fn across<R>(
|
||
w: u32,
|
||
split: u32,
|
||
color: impl Fn(u32) -> [u8; 3],
|
||
label: impl Fn(u32) -> u16,
|
||
palette: &[[u8; 3]],
|
||
mode: Mode,
|
||
f: impl FnOnce(&Layers, Vec<Crack>) -> R,
|
||
) -> R {
|
||
let img = Raster::from_fn(w, 8, |x, _| {
|
||
let [r, g, b] = color(x);
|
||
[r, g, b, 255]
|
||
});
|
||
let (w, h) = (w as usize, 8);
|
||
let q = Quantized { labels: (0..w * h).map(|i| label((i % w) as u32)).collect(), palette: palette.to_vec() };
|
||
let params = TraceParams { mode, ..TraceParams::default() };
|
||
let n = palette.len();
|
||
let (rank, gradients): (Vec<usize>, Vec<Option<TracedGradient>>) = ((0..n).collect(), vec![None; n]);
|
||
let job = Layers {
|
||
q: &q,
|
||
traced: &q.labels,
|
||
rank: &rank,
|
||
params: ¶ms,
|
||
img: &img,
|
||
left_out: vec![false; n],
|
||
boxes: vec![None; n],
|
||
gradients: &gradients,
|
||
bw_tones: [255.0, 0.0],
|
||
style: params.outline_style(),
|
||
w,
|
||
h,
|
||
anchors: AtomicUsize::new(0),
|
||
max_anchors: usize::MAX,
|
||
};
|
||
let cracks = (0..h as i32).map(|y| Crack::new((split as i32 - 1, y), (1, 0), w, h)).collect();
|
||
f(&job, cracks)
|
||
}
|
||
|
||
/// [`steps_labeled`] with the label 0 left of column `split` and 1 from it.
|
||
fn steps(w: u32, split: u32, color: impl Fn(u32) -> [u8; 3], palette: &[[u8; 3]], mode: Mode) -> (Vec<[f64; 5]>, bool) {
|
||
steps_labeled(w, split, color, |x| u16::from(x >= split), palette, mode)
|
||
}
|
||
|
||
/// [`Layers::step`] of every crack between columns `split - 1` and `split` of [`across`]'s image,
|
||
/// and whether `subpixel::low_contrast` calls them a contour. Each is read from both sides, which
|
||
/// must agree exactly.
|
||
fn steps_labeled(
|
||
w: u32,
|
||
split: u32,
|
||
color: impl Fn(u32) -> [u8; 3],
|
||
label: impl Fn(u32) -> u16,
|
||
palette: &[[u8; 3]],
|
||
mode: Mode,
|
||
) -> (Vec<[f64; 5]>, bool) {
|
||
across(w, split, color, label, palette, mode, |job, cracks| {
|
||
let shares: Vec<[f64; 5]> = cracks
|
||
.into_iter()
|
||
.map(|c| {
|
||
assert_eq!(job.step(c).map(f64::to_bits), job.step(c.flipped()).map(f64::to_bits));
|
||
job.step(c)
|
||
})
|
||
.collect();
|
||
let contour = crate::subpixel::low_contrast(&shares);
|
||
(shares, contour)
|
||
})
|
||
}
|
||
|
||
#[test]
|
||
fn a_crack_in_a_smooth_area_shows_a_share_of_the_step_that_grows_further_out() {
|
||
// Two levels a pixel between colors 40 apart: 135 and 143 two pixels either side (8 levels
|
||
// in each channel), 131 and 147 four pixels either side.
|
||
let ramp = |x: u32| [(100 + 2 * x) as u8; 3];
|
||
for mode in [Mode::Color, Mode::Grayscale] {
|
||
let (shares, contour) = steps(40, 20, ramp, &[[120; 3], [160; 3]], mode);
|
||
// (The image changes along the line between the two colors, so its whole change is
|
||
// the change along that line; it changes by 2 levels a channel from pixel to pixel.)
|
||
let fits = |s: &[f64; 5]| {
|
||
(s[0] - 0.2).abs() < 1e-9
|
||
&& (s[1] - 0.4).abs() < 1e-9
|
||
&& [s[2], s[3]].iter().all(|v| (v - 8.0 * 3f64.sqrt()).abs() < 1e-9)
|
||
&& (s[4] - 2.0 * 3f64.sqrt()).abs() < 1e-9
|
||
};
|
||
assert!(shares.iter().all(fits), "{mode:?}: {shares:?}");
|
||
assert!(contour);
|
||
}
|
||
// Black and White's tones are no area's colors.
|
||
let (shares, contour) = steps(40, 20, ramp, &[[120; 3], [160; 3]], Mode::BlackAndWhite);
|
||
assert!(shares.iter().all(|s| s[0] == 1.0 && s[1] == 1.0 && s[2..].iter().all(|v| v.is_nan())) && !contour, "{shares:?}");
|
||
// By the image's border there is nothing further out.
|
||
let (shares, contour) = steps(6, 3, ramp, &[[90; 3], [130; 3]], Mode::Color);
|
||
assert!(shares.iter().all(|s| s[1].is_nan()) && contour, "{shares:?}");
|
||
}
|
||
|
||
#[test]
|
||
fn an_edge_between_flat_colors_is_an_edge_however_far_their_palette_entries_are() {
|
||
// A flat gray beside white, the gray in an entry with black: 115 of 245 levels, less than
|
||
// half the step between the two entries, but the same further out.
|
||
let flat = |x: u32| if x < 10 { [140; 3] } else { [255; 3] };
|
||
let (shares, contour) = steps(20, 10, flat, &[[10; 3], [255; 3]], Mode::Color);
|
||
assert!(shares.iter().all(|s| s[0] < 0.5 && s[0] == s[1] && s[4] == 0.0), "{shares:?}");
|
||
assert!(!contour);
|
||
// Two flat grays 6 levels apart, in entries 25 apart: under a half of the step and a
|
||
// change of under `FLAT`, but nothing changes within either side.
|
||
let close = |split: u32| move |x: u32| if x < split { [100; 3] } else { [106; 3] };
|
||
let (shares, contour) = steps(20, 10, close(10), &[[90; 3], [115; 3]], Mode::Color);
|
||
assert!(shares.iter().all(|s| (s[0] - 0.24).abs() < 1e-9 && s[0] == s[1] && s[4] == 0.0), "{shares:?}");
|
||
assert!(!contour);
|
||
// The same by the image's border, where there is nothing further out.
|
||
let (shares, contour) = steps(6, 3, close(3), &[[90; 3], [115; 3]], Mode::Color);
|
||
assert!(shares.iter().all(|s| s[1].is_nan() && s[4] == 0.0), "{shares:?}");
|
||
assert!(!contour);
|
||
// Two flat colors whose difference is square to the line between their entries: none of
|
||
// the step near the crack or further out.
|
||
let square = |x: u32| if x < 10 { [100, 100, 100] } else { [100, 106, 94] };
|
||
let (shares, contour) = steps(20, 10, square, &[[90; 3], [115; 3]], Mode::Color);
|
||
assert!(shares.iter().all(|s| s[0] == 0.0 && s[1] == 0.0 && s[4] == 0.0), "{shares:?}");
|
||
assert!(!contour);
|
||
}
|
||
|
||
#[test]
|
||
fn an_edge_beside_a_thin_flat_stroke_is_an_edge() {
|
||
// White, then a gray stroke two pixels wide in an entry far darker than it (under a half
|
||
// of the step from white), then black: four pixels out from the crack the image reaches
|
||
// black, more than the whole step.
|
||
let color = |x: u32| match x {
|
||
0..10 => [255; 3],
|
||
10..12 => [200; 3],
|
||
_ => [0; 3],
|
||
};
|
||
let label = |x: u32| match x {
|
||
0..10 => 0,
|
||
10..12 => 1,
|
||
_ => 2,
|
||
};
|
||
let (shares, contour) = steps_labeled(20, 10, color, label, &[[255; 3], [140; 3], [0; 3]], Mode::Color);
|
||
assert!(shares.iter().all(|s| s[0] < 0.5 && s[1] > 1.25 * s[0] && s[4] == 0.0), "{shares:?}");
|
||
assert!(!contour);
|
||
}
|
||
|
||
#[test]
|
||
fn grayscale_reads_an_edge_between_two_colors_by_their_grays() {
|
||
// Red beside green: grays 60 and 117 (the palette's entries), whose channels add up to the
|
||
// same 200, so read as colors the image would make none of the step between them.
|
||
let flat = |x: u32| if x < 10 { [200, 0, 0] } else { [0, 200, 0] };
|
||
let (shares, contour) = steps(20, 10, flat, &[[60; 3], [117; 3]], Mode::Grayscale);
|
||
assert!(shares.iter().all(|s| s[0] == 1.0 && s[1] == 1.0), "{shares:?}");
|
||
assert!(!contour);
|
||
}
|
||
|
||
#[test]
|
||
fn grayscale_places_an_edge_between_two_colors_by_their_grays() {
|
||
// The same red and green: by their grays, each is all of its own entry, so the edge lies on
|
||
// the crack. Read as colors, each would be mostly the other's.
|
||
let flat = |x: u32| if x < 10 { [200, 0, 0] } else { [0, 200, 0] };
|
||
let shifts: Vec<f64> = across(
|
||
20,
|
||
10,
|
||
flat,
|
||
|x| u16::from(x >= 10),
|
||
&[[60; 3], [117; 3]],
|
||
Mode::Grayscale,
|
||
|job, cracks| cracks.into_iter().map(|c| job.shift(c)).collect(),
|
||
);
|
||
assert!(shifts.iter().all(|&s| s == 0.0), "{shifts:?}");
|
||
}
|
||
|
||
#[test]
|
||
fn an_edge_between_colors_off_the_line_between_the_palettes_two_is_an_edge() {
|
||
// Red beside green traced with black and white entries: a change of 226 levels, none of it
|
||
// from black towards white.
|
||
let flat = |x: u32| if x < 10 { [160, 0, 0] } else { [0, 160, 0] };
|
||
let (shares, contour) = steps(20, 10, flat, &[[0; 3], [255; 3]], Mode::Color);
|
||
assert!(shares.iter().all(|s| s[0] == 0.0 && s[1] == 0.0 && (s[3] - 160.0 * 2f64.sqrt()).abs() < 1e-9), "{shares:?}");
|
||
assert!(!contour);
|
||
}
|
||
|
||
#[test]
|
||
fn fit_in_chunks_keeps_the_order_and_stops_at_the_first_refusal() {
|
||
let items: Vec<usize> = (0..1000).collect();
|
||
let fitted = AtomicUsize::new(0);
|
||
let mut seen = vec![];
|
||
let all = fit_in_chunks(
|
||
&items,
|
||
16,
|
||
|&i| {
|
||
fitted.fetch_add(1, Ordering::Relaxed);
|
||
i * 2
|
||
},
|
||
|&i, r| {
|
||
assert_eq!(r, i * 2);
|
||
seen.push(i);
|
||
seen.len() <= 10
|
||
},
|
||
);
|
||
assert!(!all);
|
||
// Items come in order, up to and including the one refused.
|
||
assert_eq!(seen, (0..=10).collect::<Vec<_>>());
|
||
// The early stop is kept: only the chunk holding the refusal was fitted, not the other 984 items.
|
||
assert_eq!(fitted.load(Ordering::Relaxed), 16);
|
||
}
|
||
|
||
#[test]
|
||
fn fit_in_chunks_accepts_everything_in_order() {
|
||
let items: Vec<usize> = (0..1000).collect();
|
||
let mut out = vec![];
|
||
assert!(fit_in_chunks(
|
||
&items,
|
||
7,
|
||
|&i| i + 1,
|
||
|_, r| {
|
||
out.push(r);
|
||
true
|
||
}
|
||
));
|
||
assert_eq!(out, (1..=1000).collect::<Vec<_>>());
|
||
assert!(fit_in_chunks(&[] as &[usize], 7, |&i| i, |_, _| false));
|
||
}
|
||
}
|