inlaytrace 0.6.0 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/README.md +20 -20
- package/dist/index.d.ts +7 -3
- package/dist/index.js +272 -39
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
# inlaytrace
|
|
2
2
|
|
|
3
|
+
## 0.6.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Poster draws each outline in one ink, end to end. A drawing's dark outline that the clusters split into several near-blacks came out as lengths of two or three of them, with a kink and a step in the line's width at each seam. Now a thin line blotched with a chain of close clusters takes the one nearest its mean color, along its whole length. Fills and shading are untouched, and photos trace as they did (within 0.3% of the same error, in 1% fewer bytes); tracing takes about 15% longer.
|
|
8
|
+
- Poster draws a drawing's dark outlines smoothly, as one ink. Its clusters split one ink into several near-identical shades, blotched into each other: small pieces at the outline's edge were folded into the paper beside them, which left notches, and the rest traced as hundreds of pieces with a kink where two met. Now a small region joins a neighbor close to its own color when there is one, rather than whichever color it touches most; and where two clusters under 0.035 apart (OKLab) are blotched together, the region takes one of them. Tints that meet along a clean edge, as bands of shading do, stay apart. Photos trace about 11% closer to the source, in about 5% fewer bytes.
|
|
9
|
+
|
|
3
10
|
## 0.6.0
|
|
4
11
|
|
|
5
12
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# Inlay
|
|
2
2
|
|
|
3
3
|
A raster-to-vector tracer whose shapes **share their edges exactly**, like
|
|
4
|
-
inlaid wood: every boundary between two
|
|
5
|
-
used by both
|
|
4
|
+
inlaid wood: every boundary between two colors is traced and fitted once and
|
|
5
|
+
used by both neighbors, so layers never gap or overlap. Straight edges come out
|
|
6
6
|
as single lines, corners as single sharp points, and round things as smooth
|
|
7
7
|
curves. Pixel art keeps its exact staircase.
|
|
8
8
|
|
|
@@ -28,7 +28,7 @@ Versions are 0.x while the API settles: a minor version (0.1 → 0.2) may change
|
|
|
28
28
|
it, and the [changelog](https://github.com/mtblck/inlaytrace/blob/main/packages/inlay/CHANGELOG.md)
|
|
29
29
|
says how.
|
|
30
30
|
From 1.0, a minor version can change the traced output (different paths or
|
|
31
|
-
|
|
31
|
+
colors) but not the API, and never makes the measured quality worse; a major
|
|
32
32
|
version is any change to an export, option, preset or result type. Pin an exact
|
|
33
33
|
version if you need byte-identical SVGs. The [full policy](https://github.com/mtblck/inlaytrace/blob/main/docs/package.md#versions)
|
|
34
34
|
says what counts as what.
|
|
@@ -61,15 +61,15 @@ PNGs need nothing else. For JPEG, WebP, GIF or TIFF, install
|
|
|
61
61
|
|
|
62
62
|
| Preset | For |
|
|
63
63
|
| --- | --- |
|
|
64
|
-
| `logo` (default) | Few flat
|
|
65
|
-
| `illustration` | More
|
|
64
|
+
| `logo` (default) | Few flat colors (8), crisp corners, shapes cut out to tile, exact circles and rectangles. |
|
|
65
|
+
| `illustration` | More colors (24), smooth curves, shapes in layers (each whole, a disc behind a bar stays a disc), soft shading as gradients. |
|
|
66
66
|
| `outline` | Illustration, with thin drawn outlines (2.5–8 px; give `scale` if the image was enlarged) traced down their middle as round-capped strokes, and the shapes either side meeting beneath them. |
|
|
67
|
-
| `lineart` | Ink drawings and scans: ink darker than the image's own ink/paper split (Otsu;
|
|
67
|
+
| `lineart` | Ink drawings and scans: ink darker than the image's own ink/paper split (Otsu; colored ink works too), small breaks in lines closed. Only the ink is drawn. |
|
|
68
68
|
| `centerline` | Single strokes down the middle of the ink, at the width measured from it, for pen plotters and engraving. |
|
|
69
|
-
| `poster` | Photos as flat
|
|
70
|
-
| `pixel` | Pixel art: exact
|
|
69
|
+
| `poster` | Photos as flat color bands: colors clustered (k-means in OKLab, deterministic), each shape whole, larger first. |
|
|
70
|
+
| `pixel` | Pixel art: exact colors, no clean-up of lone pixels, no curve fitting; every boundary is its pixel staircase. |
|
|
71
71
|
|
|
72
|
-
Any option overrides the preset: `colors`, `palette` (your own
|
|
72
|
+
Any option overrides the preset: `colors`, `palette` (your own colors, as
|
|
73
73
|
`#rrggbb`), `merge` (`{ '#from': '#into' }`), `speckle`, `tolerance`,
|
|
74
74
|
`cornerAngle`, `layers`, `shapes`, `strokes`, `gradients`, `closeGaps`,
|
|
75
75
|
`threshold`, `invert`, `strokeWidth`, `deblur`, `smoothShading`, `keepOutlines`, `grayscale` and
|
|
@@ -89,20 +89,20 @@ says what each does.
|
|
|
89
89
|
- `detectSymmetry(rgba, width, height)`: the likeliest mirror axes and
|
|
90
90
|
rotational orders in an image, each scored by how much of the drawing
|
|
91
91
|
matches (the app offers to enforce one at 95% or more).
|
|
92
|
-
- `oklab(rgb)`: the perceptual
|
|
92
|
+
- `oklab(rgb)`: the perceptual color space the palette and labeling use.
|
|
93
93
|
- `sticker(result, options)` / `stickerSvg(…)`: a solid backing behind a trace,
|
|
94
94
|
so it prints and cuts as one sticker: the art's outline grown by a margin,
|
|
95
|
-
or a circle, oval, square or rectangle
|
|
95
|
+
or a circle, oval, square or rectangle around it, with an optional cut line
|
|
96
96
|
and bleed.
|
|
97
97
|
|
|
98
98
|
## How it works
|
|
99
99
|
|
|
100
|
-
1. **Label** every pixel with a palette
|
|
101
|
-
|
|
100
|
+
1. **Label** every pixel with a palette color in OKLab. A pixel between two
|
|
101
|
+
colors (anti-aliasing) takes the nearest color *among its neighbors*, so
|
|
102
102
|
a red/blue edge never grows a purple sliver.
|
|
103
|
-
2. **Clean up**: a pixel whose
|
|
103
|
+
2. **Clean up**: a pixel whose neighbors nearly all disagree joins them;
|
|
104
104
|
regions smaller than `speckle`² merge into their surroundings.
|
|
105
|
-
3. **Walk the cracks** between differently
|
|
105
|
+
3. **Walk the cracks** between differently labeled pixels. Where three regions
|
|
106
106
|
meet is a junction; each boundary between junctions is one chain, with one
|
|
107
107
|
region on either side.
|
|
108
108
|
4. **Fit each chain once**: corners are two straight sides meeting near the
|
|
@@ -110,7 +110,7 @@ says what each does.
|
|
|
110
110
|
cubic Béziers with matching tangents, smoothed only as much as the local
|
|
111
111
|
curvature allows.
|
|
112
112
|
5. **Assemble** each region from its chains (reversed where it sees them from
|
|
113
|
-
the other side).
|
|
113
|
+
the other side). Neighbors share the same fitted edge exactly.
|
|
114
114
|
|
|
115
115
|
The full write-up, with measurements, is in the
|
|
116
116
|
[docs](https://github.com/mtblck/inlaytrace/blob/main/docs/how-it-works/inlay.md);
|
|
@@ -121,16 +121,16 @@ every export.
|
|
|
121
121
|
|
|
122
122
|
## Measured
|
|
123
123
|
|
|
124
|
-
On a 640 px JPEG of a five-
|
|
124
|
+
On a 640 px JPEG of a five-color badge, scored by rendering at 4× against the
|
|
125
125
|
clean source art: 0.54% wrong pixels with 175 nodes with `shapes` on (0.70%
|
|
126
|
-
and 177 without), against 0.85% with 271 nodes for VTracer tracing each
|
|
126
|
+
and 177 without), against 0.85% with 271 nodes for VTracer tracing each color
|
|
127
127
|
on its own. At 2048 px it traces in about
|
|
128
128
|
1.3 s (VTracer: 17.5 s, timed by hand). On pixel art both draw every pixel exactly; Inlay uses
|
|
129
129
|
about 14% fewer nodes and is about 4× faster on a 1024 px sprite.
|
|
130
130
|
|
|
131
131
|
On three photos (quantized palette, stacked), rendered back and compared with
|
|
132
|
-
each photo: a mean error of
|
|
133
|
-
and a
|
|
132
|
+
each photo: a mean error of 9.2 against VTracer's 14.3, with 57% fewer shapes
|
|
133
|
+
and a 9% smaller file.
|
|
134
134
|
|
|
135
135
|
## License
|
|
136
136
|
|
package/dist/index.d.ts
CHANGED
|
@@ -182,8 +182,11 @@ type InlayOptions = FitOptions & {
|
|
|
182
182
|
*/
|
|
183
183
|
stacked?: boolean;
|
|
184
184
|
/**
|
|
185
|
-
* Photos: specks join the colour they share the most edge with,
|
|
186
|
-
*
|
|
185
|
+
* Photos: specks join the colour they share the most edge with, of those
|
|
186
|
+
* about as near their own as the nearest (see `SPECK_NEAR`), and slivers
|
|
187
|
+
* are left alone. Clusters nearly the same colour that are blotched into
|
|
188
|
+
* each other settle into one (see `PHOTO_CLOSE_PAIR`), and a line blotched with a chain of them takes one
|
|
189
|
+
* colour end to end (see `settleLines`); not with `faint: false`. Flat art's clean-up (specks join the nearest colour, and
|
|
187
190
|
* slivers of one ink's two shades fold together) keeps thin strokes whole,
|
|
188
191
|
* but on photos it kept so many real regions that files grew by half.
|
|
189
192
|
*/
|
|
@@ -216,7 +219,8 @@ type InlayOptions = FitOptions & {
|
|
|
216
219
|
* the image passes between without an edge (a barely-there gradient in a
|
|
217
220
|
* background, banded by close tints) join, in their commonest colour.
|
|
218
221
|
* Palette mode, not photos or pixel art. On by default; the presets turn it
|
|
219
|
-
* off with your own palette, whose every colour you chose.
|
|
222
|
+
* off with your own palette, whose every colour you chose. Off, a photo's
|
|
223
|
+
* close clusters are not settled into one either (see `photo`).
|
|
220
224
|
*/
|
|
221
225
|
faint?: boolean;
|
|
222
226
|
/**
|
package/dist/index.js
CHANGED
|
@@ -3765,15 +3765,24 @@ function* traceSteps(rgba, w, h, o) {
|
|
|
3765
3765
|
const { labels, colors, drawn } = o.mode === "palette" ? labelPalette(rgba, w, h, o.palette, !o.photo) : labelBinary(rgba, w, h, o.threshold);
|
|
3766
3766
|
const clean = o.mode === "palette" && !o.pixels && !o.photo && lossless(rgba, labels, w, h, colors);
|
|
3767
3767
|
if (o.mode === "palette" && o.merge) mergeLabels(labels, colors, o.merge);
|
|
3768
|
-
if (o.mode === "palette" && !o.
|
|
3768
|
+
if (o.mode === "palette" && !o.pixels) {
|
|
3769
|
+
if (o.photo) {
|
|
3770
|
+
if (o.faint !== false) {
|
|
3771
|
+
settleLines(labels, rgba, w, h, colors, o.lines);
|
|
3772
|
+
settleClosePairs(labels, rgba, w, h, colors, o.lines, PHOTO_CLOSE_PAIR, PHOTO_INTERLEAVED);
|
|
3773
|
+
}
|
|
3774
|
+
} else settleClosePairs(labels, rgba, w, h, colors, o.lines);
|
|
3775
|
+
}
|
|
3769
3776
|
yield {
|
|
3770
3777
|
stage: "clean",
|
|
3771
3778
|
done: 0
|
|
3772
3779
|
};
|
|
3773
3780
|
if (!o.pixels) despur(labels, w, h);
|
|
3774
3781
|
if (!o.pixels && !(o.mode === "palette" && o.photo)) joinDiagonals(labels, w, h, rgba, drawn);
|
|
3775
|
-
const
|
|
3776
|
-
|
|
3782
|
+
const photo = o.mode === "palette" && !!o.photo;
|
|
3783
|
+
const allLabs = o.mode === "palette" ? colors.map((c) => c ? oklab(hexToRgb(c)) : null) : void 0;
|
|
3784
|
+
const labs = photo ? void 0 : allLabs;
|
|
3785
|
+
mergeSpecks(labels, w, h, Math.max(0, Math.round(o.speckle)) ** 2, allLabs, photo);
|
|
3777
3786
|
if (o.mode === "binary" && o.closeGaps && closeInkGaps(labels, w, h, o.scale)) mergeSpecks(labels, w, h, Math.max(0, Math.round(o.speckle)) ** 2);
|
|
3778
3787
|
if (labs && !o.pixels) mergeSlivers(labels, w, h, labs, o.lines);
|
|
3779
3788
|
const lines = o.mode === "palette" && o.strokes && labs && !o.pixels ? strokesOf(rgba, labels, w, h, colors, labs, drawn, o) : [];
|
|
@@ -4609,6 +4618,21 @@ const CLOSE_PALE = .85;
|
|
|
4609
4618
|
/** Edges between the two colours, per pixel of the rarer one, past which a region is speckled with both, not two fills side by side. */
|
|
4610
4619
|
const INTERLEAVED = .1;
|
|
4611
4620
|
/**
|
|
4621
|
+
* Photos (Poster): the same, between clusters this close, in regions this
|
|
4622
|
+
* blotched. A cluster is a mean, not a colour that is there, and 24 of them
|
|
4623
|
+
* put several along one shade: a drawing's black outline took five, 0.02–0.03
|
|
4624
|
+
* from one to the next, blotched into each other at 0.9–1.3 edges a pixel,
|
|
4625
|
+
* and traced as hundreds of pieces with a kink where each pair met. Its
|
|
4626
|
+
* shading's two tints are 0.05 apart and meet along one edge (0.01 a pixel),
|
|
4627
|
+
* so they stay two. The distance is what a photo allows: a wooden table's
|
|
4628
|
+
* grain is two tints 0.035–0.04 apart, as blotched as the ink, and at 0.04
|
|
4629
|
+
* it went flat; at flat art's 0.07 and 0.1 photos were 17% further from the
|
|
4630
|
+
* photo. At these they are 1% closer than without, in 9% fewer bytes
|
|
4631
|
+
* (`score:photos`). At 0.5 edges a pixel the outline stayed in pieces.
|
|
4632
|
+
*/
|
|
4633
|
+
const PHOTO_CLOSE_PAIR = .035;
|
|
4634
|
+
const PHOTO_INTERLEAVED = .2;
|
|
4635
|
+
/**
|
|
4612
4636
|
* A shade between two close palette colours goes one way, for the whole
|
|
4613
4637
|
* region it fills, not pixel by pixel. A truck's bumper (#4e5057) lay between
|
|
4614
4638
|
* its blue-grey cab and its neutral-grey bars, 0.03 from each, and noise
|
|
@@ -4616,42 +4640,229 @@ const INTERLEAVED = .1;
|
|
|
4616
4640
|
* colours together, when it is blotched with both (more than INTERLEAVED
|
|
4617
4641
|
* edges between them per pixel of the rarer), takes whichever is nearer its
|
|
4618
4642
|
* mean colour. Two fills that meet along a clean edge are left as they are,
|
|
4619
|
-
* and so are painted lines marked by keepOutlines.
|
|
4643
|
+
* and so are painted lines marked by keepOutlines. `close` and `interleaved`
|
|
4644
|
+
* are CLOSE_PAIR and INTERLEAVED, or a photo's.
|
|
4620
4645
|
*/
|
|
4621
|
-
function settleClosePairs(labels, rgba, w, h, colors, lines) {
|
|
4646
|
+
function settleClosePairs(labels, rgba, w, h, colors, lines, close = CLOSE_PAIR, interleaved = INTERLEAVED) {
|
|
4622
4647
|
const labs = colors.map((c) => c ? oklab(hexToRgb(c)) : null);
|
|
4623
4648
|
const dist = (a, b) => Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2]);
|
|
4649
|
+
const count = new Int32Array(colors.length);
|
|
4650
|
+
for (let i = 0; i < labels.length; i++) count[labels[i]]++;
|
|
4651
|
+
const of = Array.from(count, (n) => new Int32Array(n));
|
|
4652
|
+
count.fill(0);
|
|
4653
|
+
for (let i = 0; i < labels.length; i++) of[labels[i]][count[labels[i]]++] = i;
|
|
4654
|
+
const reached = new Int32Array(labels.length);
|
|
4655
|
+
let pair = 0;
|
|
4624
4656
|
for (let a = 0; a < colors.length; a++) for (let b = a + 1; b < colors.length; b++) {
|
|
4625
4657
|
const la = labs[a];
|
|
4626
4658
|
const lb = labs[b];
|
|
4627
|
-
if (!la || !lb || dist(la, lb) >=
|
|
4628
|
-
|
|
4629
|
-
const
|
|
4630
|
-
const
|
|
4631
|
-
|
|
4632
|
-
const
|
|
4633
|
-
|
|
4634
|
-
|
|
4635
|
-
|
|
4636
|
-
|
|
4637
|
-
|
|
4638
|
-
|
|
4639
|
-
|
|
4640
|
-
|
|
4641
|
-
|
|
4642
|
-
|
|
4643
|
-
|
|
4644
|
-
|
|
4645
|
-
|
|
4646
|
-
|
|
4659
|
+
if (!la || !lb || !of[a].length || !of[b].length || dist(la, lb) >= close || la[0] >= CLOSE_PALE || lb[0] >= CLOSE_PALE) continue;
|
|
4660
|
+
pair++;
|
|
4661
|
+
const inPair = (j) => (labels[j] === a || labels[j] === b) && !lines?.[j];
|
|
4662
|
+
const region = new Int32Array(of[a].length + of[b].length);
|
|
4663
|
+
let changed = false;
|
|
4664
|
+
for (const seeds of [of[a], of[b]]) for (const seed of seeds) {
|
|
4665
|
+
if (reached[seed] === pair || lines?.[seed]) continue;
|
|
4666
|
+
let n = 0;
|
|
4667
|
+
region[n++] = seed;
|
|
4668
|
+
reached[seed] = pair;
|
|
4669
|
+
let na = 0;
|
|
4670
|
+
let edges = 0;
|
|
4671
|
+
const sum = [
|
|
4672
|
+
0,
|
|
4673
|
+
0,
|
|
4674
|
+
0
|
|
4675
|
+
];
|
|
4676
|
+
for (let k = 0; k < n; k++) {
|
|
4677
|
+
const i = region[k];
|
|
4678
|
+
const x = i % w;
|
|
4679
|
+
if (labels[i] === a) na++;
|
|
4680
|
+
for (let c = 0; c < 3; c++) sum[c] += rgba[i * 4 + c];
|
|
4681
|
+
const right = x < w - 1 ? i + 1 : -1;
|
|
4682
|
+
const down = i + w < w * h ? i + w : -1;
|
|
4683
|
+
if (right >= 0 && inPair(right) && labels[right] !== labels[i]) edges++;
|
|
4684
|
+
if (down >= 0 && inPair(down) && labels[down] !== labels[i]) edges++;
|
|
4685
|
+
if (right >= 0 && reached[right] !== pair && inPair(right)) reached[right] = pair, region[n++] = right;
|
|
4686
|
+
if (down >= 0 && reached[down] !== pair && inPair(down)) reached[down] = pair, region[n++] = down;
|
|
4687
|
+
if (x > 0 && reached[i - 1] !== pair && inPair(i - 1)) reached[i - 1] = pair, region[n++] = i - 1;
|
|
4688
|
+
if (i >= w && reached[i - w] !== pair && inPair(i - w)) reached[i - w] = pair, region[n++] = i - w;
|
|
4689
|
+
}
|
|
4690
|
+
const nb = n - na;
|
|
4691
|
+
if (!na || !nb || edges <= interleaved * Math.min(na, nb)) continue;
|
|
4647
4692
|
const mean = oklab([
|
|
4648
|
-
sum[
|
|
4649
|
-
sum[
|
|
4650
|
-
sum[
|
|
4693
|
+
sum[0] / n,
|
|
4694
|
+
sum[1] / n,
|
|
4695
|
+
sum[2] / n
|
|
4651
4696
|
]);
|
|
4652
|
-
to
|
|
4697
|
+
const to = dist(mean, la) <= dist(mean, lb) ? a : b;
|
|
4698
|
+
for (let k = 0; k < n; k++) labels[region[k]] = to;
|
|
4699
|
+
changed = true;
|
|
4700
|
+
}
|
|
4701
|
+
if (changed) {
|
|
4702
|
+
const all = new Int32Array(of[a].length + of[b].length);
|
|
4703
|
+
all.set(of[a]);
|
|
4704
|
+
all.set(of[b], of[a].length);
|
|
4705
|
+
of[a] = all.filter((i) => labels[i] === a);
|
|
4706
|
+
of[b] = all.filter((i) => labels[i] === b);
|
|
4707
|
+
}
|
|
4708
|
+
}
|
|
4709
|
+
}
|
|
4710
|
+
/** A line of ink in a photo is no wider than this, in pixels (a 2048 px drawing's outline was 9–11); more in a larger image, in step with its long edge. */
|
|
4711
|
+
const SHADE_LINE_WIDEST = 16;
|
|
4712
|
+
const SHADE_LINE_AT = 2048;
|
|
4713
|
+
/**
|
|
4714
|
+
* This share of a line's region is thin, long parts: an outline's were
|
|
4715
|
+
* 0.66–1, a drawing's fills 0–0.1, a cat's fur 0.33. (Its own number, not
|
|
4716
|
+
* Outline art's LINE_SHARE, which the fur passes.)
|
|
4717
|
+
*/
|
|
4718
|
+
const SHADE_LINE_SHARE = .5;
|
|
4719
|
+
/**
|
|
4720
|
+
* The colours a line's region holds are no further apart than this (OKLab):
|
|
4721
|
+
* an outline's five near-blacks were 0.078 end to end. Past it the chain is a
|
|
4722
|
+
* ramp, shading along a thin feature (a bar from #1e1e1e to #3c3c3c is 0.12),
|
|
4723
|
+
* however evenly its pixels sit around their mean.
|
|
4724
|
+
*/
|
|
4725
|
+
const SHADE_EXTENT = .1;
|
|
4726
|
+
/** Pixels further than this (OKLab) from their region's mean are another shade: a drawing's two tints of shading were 0.05 apart. */
|
|
4727
|
+
const SHADE_SPREAD = .05;
|
|
4728
|
+
/** One shade has at most this share of such pixels: ink's anti-aliased rims gave 0.03–0.05, a cat's fur 0.3–0.5. */
|
|
4729
|
+
const SHADE_STRAYS = .2;
|
|
4730
|
+
/**
|
|
4731
|
+
* Photos (Poster): a line drawn in one ink takes one colour, end to end.
|
|
4732
|
+
* `settleClosePairs` settles two clusters at a time, so of the five an outline
|
|
4733
|
+
* was blotched with, each stretch went to whichever of its two was nearer: the
|
|
4734
|
+
* line came out in two or three near-blacks, lengths of each side by side,
|
|
4735
|
+
* with a kink where each seam met the line's edge and a step in its width
|
|
4736
|
+
* (each claims the soft rim differently). Here the blotched regions of every
|
|
4737
|
+
* close pair are joined where they overlap, so a chain of clusters along one
|
|
4738
|
+
* line is one region, and the region takes the colour, of those it holds,
|
|
4739
|
+
* nearest its mean. A chain can also drift a long way (a cat's fur ran through
|
|
4740
|
+
* 17 clusters, a pot's two tints through the one between them), so only a
|
|
4741
|
+
* region that is a line settles: SHADE_LINE_SHARE of it in thin, long parts,
|
|
4742
|
+
* its colours within SHADE_EXTENT of each other, and its pixels one shade
|
|
4743
|
+
* (SHADE_STRAYS). The rest is left to `settleClosePairs`.
|
|
4744
|
+
*/
|
|
4745
|
+
function settleLines(labels, rgba, w, h, colors, lines) {
|
|
4746
|
+
const labs = colors.map((c) => c ? oklab(hexToRgb(c)) : null);
|
|
4747
|
+
const dist = (a, b) => Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2]);
|
|
4748
|
+
const count = new Int32Array(colors.length);
|
|
4749
|
+
for (let i = 0; i < labels.length; i++) count[labels[i]]++;
|
|
4750
|
+
const of = Array.from(count, (n) => new Int32Array(n));
|
|
4751
|
+
count.fill(0);
|
|
4752
|
+
for (let i = 0; i < labels.length; i++) of[labels[i]][count[labels[i]]++] = i;
|
|
4753
|
+
const reached = new Int32Array(labels.length);
|
|
4754
|
+
const parent = new Int32Array(labels.length).fill(-1);
|
|
4755
|
+
const find = (i) => {
|
|
4756
|
+
let r = i;
|
|
4757
|
+
while (parent[r] !== r) r = parent[r];
|
|
4758
|
+
while (parent[i] !== r) {
|
|
4759
|
+
const next = parent[i];
|
|
4760
|
+
parent[i] = r;
|
|
4761
|
+
i = next;
|
|
4762
|
+
}
|
|
4763
|
+
return r;
|
|
4764
|
+
};
|
|
4765
|
+
let pair = 0;
|
|
4766
|
+
let any = false;
|
|
4767
|
+
for (let a = 0; a < colors.length; a++) for (let b = a + 1; b < colors.length; b++) {
|
|
4768
|
+
const la = labs[a];
|
|
4769
|
+
const lb = labs[b];
|
|
4770
|
+
if (!la || !lb || !of[a].length || !of[b].length || dist(la, lb) >= PHOTO_CLOSE_PAIR || la[0] >= CLOSE_PALE || lb[0] >= CLOSE_PALE) continue;
|
|
4771
|
+
pair++;
|
|
4772
|
+
const inPair = (j) => (labels[j] === a || labels[j] === b) && !lines?.[j];
|
|
4773
|
+
const region = new Int32Array(of[a].length + of[b].length);
|
|
4774
|
+
for (const seeds of [of[a], of[b]]) for (const seed of seeds) {
|
|
4775
|
+
if (reached[seed] === pair || lines?.[seed]) continue;
|
|
4776
|
+
let n = 0;
|
|
4777
|
+
region[n++] = seed;
|
|
4778
|
+
reached[seed] = pair;
|
|
4779
|
+
let na = 0;
|
|
4780
|
+
let edges = 0;
|
|
4781
|
+
for (let k = 0; k < n; k++) {
|
|
4782
|
+
const i = region[k];
|
|
4783
|
+
const x = i % w;
|
|
4784
|
+
if (labels[i] === a) na++;
|
|
4785
|
+
const right = x < w - 1 ? i + 1 : -1;
|
|
4786
|
+
const down = i + w < w * h ? i + w : -1;
|
|
4787
|
+
if (right >= 0 && inPair(right) && labels[right] !== labels[i]) edges++;
|
|
4788
|
+
if (down >= 0 && inPair(down) && labels[down] !== labels[i]) edges++;
|
|
4789
|
+
if (right >= 0 && reached[right] !== pair && inPair(right)) reached[right] = pair, region[n++] = right;
|
|
4790
|
+
if (down >= 0 && reached[down] !== pair && inPair(down)) reached[down] = pair, region[n++] = down;
|
|
4791
|
+
if (x > 0 && reached[i - 1] !== pair && inPair(i - 1)) reached[i - 1] = pair, region[n++] = i - 1;
|
|
4792
|
+
if (i >= w && reached[i - w] !== pair && inPair(i - w)) reached[i - w] = pair, region[n++] = i - w;
|
|
4793
|
+
}
|
|
4794
|
+
const nb = n - na;
|
|
4795
|
+
if (!na || !nb || edges <= PHOTO_INTERLEAVED * Math.min(na, nb)) continue;
|
|
4796
|
+
any = true;
|
|
4797
|
+
let root = -1;
|
|
4798
|
+
for (let k = 0; k < n; k++) {
|
|
4799
|
+
const i = region[k];
|
|
4800
|
+
if (parent[i] < 0) parent[i] = root < 0 ? root = i : root;
|
|
4801
|
+
else {
|
|
4802
|
+
const r = find(i);
|
|
4803
|
+
if (root < 0) root = r;
|
|
4804
|
+
else if (r !== root) parent[r] = root;
|
|
4805
|
+
}
|
|
4806
|
+
}
|
|
4653
4807
|
}
|
|
4654
|
-
|
|
4808
|
+
}
|
|
4809
|
+
if (!any) return;
|
|
4810
|
+
const ids = /* @__PURE__ */ new Map();
|
|
4811
|
+
const has = [];
|
|
4812
|
+
const part = new Int32Array(labels.length).fill(VOID);
|
|
4813
|
+
for (let i = 0; i < labels.length; i++) {
|
|
4814
|
+
if (parent[i] < 0) continue;
|
|
4815
|
+
const r = find(i);
|
|
4816
|
+
let id = ids.get(r);
|
|
4817
|
+
if (id === void 0) ids.set(r, id = has.push(/* @__PURE__ */ new Set()) - 1);
|
|
4818
|
+
part[i] = id;
|
|
4819
|
+
has[id].add(labels[i]);
|
|
4820
|
+
}
|
|
4821
|
+
for (let i = 0; i < labels.length; i++) if (part[i] !== VOID && has[part[i]].size < 3) part[i] = VOID;
|
|
4822
|
+
const np = has.length;
|
|
4823
|
+
const { thin } = thinParts(part, w, h, (l) => l >= 0, Math.max(SHADE_LINE_WIDEST, Math.round(SHADE_LINE_WIDEST * Math.max(w, h) / SHADE_LINE_AT)));
|
|
4824
|
+
const area = new Int32Array(np);
|
|
4825
|
+
const inLines = new Int32Array(np);
|
|
4826
|
+
const sum = new Float64Array(np * 3);
|
|
4827
|
+
for (let i = 0; i < labels.length; i++) {
|
|
4828
|
+
const c = part[i];
|
|
4829
|
+
if (c < 0) continue;
|
|
4830
|
+
area[c]++;
|
|
4831
|
+
if (thin[i]) inLines[c]++;
|
|
4832
|
+
for (let k = 0; k < 3; k++) sum[c * 3 + k] += rgba[i * 4 + k];
|
|
4833
|
+
}
|
|
4834
|
+
const extent = (held) => {
|
|
4835
|
+
let most = 0;
|
|
4836
|
+
for (const p of held) for (const q of held) if (p < q) most = Math.max(most, dist(labs[p], labs[q]));
|
|
4837
|
+
return most;
|
|
4838
|
+
};
|
|
4839
|
+
const line = Array.from(area, (n, c) => n > 0 && inLines[c] >= SHADE_LINE_SHARE * n && extent(has[c]) <= SHADE_EXTENT);
|
|
4840
|
+
const means = Array.from(area, (n, c) => oklab([
|
|
4841
|
+
sum[c * 3] / n,
|
|
4842
|
+
sum[c * 3 + 1] / n,
|
|
4843
|
+
sum[c * 3 + 2] / n
|
|
4844
|
+
]));
|
|
4845
|
+
const strays = new Int32Array(np);
|
|
4846
|
+
for (let i = 0; i < labels.length; i++) {
|
|
4847
|
+
const c = part[i];
|
|
4848
|
+
if (c >= 0 && line[c] && dist(oklab([
|
|
4849
|
+
rgba[i * 4],
|
|
4850
|
+
rgba[i * 4 + 1],
|
|
4851
|
+
rgba[i * 4 + 2]
|
|
4852
|
+
]), means[c]) > SHADE_SPREAD) strays[c]++;
|
|
4853
|
+
}
|
|
4854
|
+
const to = new Int32Array(np).fill(-1);
|
|
4855
|
+
for (let c = 0; c < np; c++) {
|
|
4856
|
+
if (!line[c] || strays[c] > SHADE_STRAYS * area[c]) continue;
|
|
4857
|
+
let nearest = 1e400;
|
|
4858
|
+
for (const l of [...has[c]].sort((p, q) => p - q)) {
|
|
4859
|
+
const d = dist(means[c], labs[l]);
|
|
4860
|
+
if (d < nearest) nearest = d, to[c] = l;
|
|
4861
|
+
}
|
|
4862
|
+
}
|
|
4863
|
+
for (let i = 0; i < labels.length; i++) {
|
|
4864
|
+
const c = part[i];
|
|
4865
|
+
if (c >= 0 && to[c] >= 0) labels[i] = to[c];
|
|
4655
4866
|
}
|
|
4656
4867
|
}
|
|
4657
4868
|
/**
|
|
@@ -4725,9 +4936,9 @@ function components(labels, w, h, keep) {
|
|
|
4725
4936
|
* label they took and are stranded as a new speck. On photos that left
|
|
4726
4937
|
* thousands of fragments, and a larger `speckle` left more, not fewer.
|
|
4727
4938
|
*/
|
|
4728
|
-
function mergeSpecks(labels, w, h, minArea, labs) {
|
|
4939
|
+
function mergeSpecks(labels, w, h, minArea, labs, photo = false) {
|
|
4729
4940
|
if (minArea <= 1) return;
|
|
4730
|
-
for (let pass = 0; pass < 8; pass++) if (!mergeSpecksOnce(labels, w, h, minArea, labs)) return;
|
|
4941
|
+
for (let pass = 0; pass < 8; pass++) if (!mergeSpecksOnce(labels, w, h, minArea, labs, photo)) return;
|
|
4731
4942
|
}
|
|
4732
4943
|
/**
|
|
4733
4944
|
* A speck at least this far (OKLab) from every colour around it is a shape,
|
|
@@ -4742,8 +4953,21 @@ const SPECK_CONTRAST = .25;
|
|
|
4742
4953
|
*/
|
|
4743
4954
|
const SPECK_SHAPE = 1 / 8;
|
|
4744
4955
|
const SPECK_MIN = 6;
|
|
4956
|
+
/**
|
|
4957
|
+
* Photos: a speck joins the colour it shares the most edge with, but only of
|
|
4958
|
+
* the colours around it within this (OKLab) of the nearest to its own. By
|
|
4959
|
+
* edge alone, a drawing's black outline lost bites to the paper: Poster's
|
|
4960
|
+
* clusters split the black into five near-blacks (0.02–0.08 apart), in pieces
|
|
4961
|
+
* of a few dozen pixels, and a piece at the outline's edge touched more paper
|
|
4962
|
+
* (0.75 away) than ink, so it became paper and left a notch: 9% of the
|
|
4963
|
+
* outline's pixels went that way. Shades this close are one ink (as
|
|
4964
|
+
* `SLIVER_DISTANCE`). With no margin at all (the nearest colour, as flat
|
|
4965
|
+
* art) photos kept a quarter more shapes and bytes; with this one they are
|
|
4966
|
+
* 10% closer to the photo for 4% more bytes (`score:photos`).
|
|
4967
|
+
*/
|
|
4968
|
+
const SPECK_NEAR = .1;
|
|
4745
4969
|
/** One pass of mergeSpecks; false when there was nothing left to merge. */
|
|
4746
|
-
function mergeSpecksOnce(labels, w, h, minArea, labs) {
|
|
4970
|
+
function mergeSpecksOnce(labels, w, h, minArea, labs, photo = false) {
|
|
4747
4971
|
const { comp, compArea } = components(labels, w, h, () => true);
|
|
4748
4972
|
const small = compArea.map((a, id) => ({
|
|
4749
4973
|
a,
|
|
@@ -4762,7 +4986,7 @@ function mergeSpecksOnce(labels, w, h, minArea, labs) {
|
|
|
4762
4986
|
const end = start[id + 1];
|
|
4763
4987
|
const own = labels[pixels[begin]];
|
|
4764
4988
|
const votes = /* @__PURE__ */ new Map();
|
|
4765
|
-
let joined =
|
|
4989
|
+
let joined = 0;
|
|
4766
4990
|
for (let k = begin; k < end; k++) {
|
|
4767
4991
|
const i = pixels[k];
|
|
4768
4992
|
const x = i % w;
|
|
@@ -4770,25 +4994,34 @@ function mergeSpecksOnce(labels, w, h, minArea, labs) {
|
|
|
4770
4994
|
const vote = (j) => {
|
|
4771
4995
|
const l = labels[j];
|
|
4772
4996
|
if (l !== own) votes.set(l, (votes.get(l) ?? 0) + 1);
|
|
4773
|
-
else if (comp[j] !== id) joined
|
|
4997
|
+
else if (comp[j] !== id) joined++;
|
|
4774
4998
|
};
|
|
4775
4999
|
if (x > 0) vote(i - 1);
|
|
4776
5000
|
if (x < w - 1) vote(i + 1);
|
|
4777
5001
|
if (y > 0) vote(i - w);
|
|
4778
5002
|
if (y < h - 1) vote(i + w);
|
|
4779
5003
|
}
|
|
4780
|
-
if (joined && labs) continue;
|
|
5004
|
+
if (joined && labs && !photo) continue;
|
|
4781
5005
|
let best = own;
|
|
4782
5006
|
let most = 0;
|
|
4783
5007
|
const mine = labs?.[own];
|
|
4784
5008
|
if (mine) {
|
|
5009
|
+
const far = (l) => {
|
|
5010
|
+
const lab = labs[l];
|
|
5011
|
+
return lab ? Math.hypot(lab[0] - mine[0], lab[1] - mine[1], lab[2] - mine[2]) : 1e400;
|
|
5012
|
+
};
|
|
4785
5013
|
let near = 1e400;
|
|
4786
5014
|
for (const [l, v] of votes) {
|
|
4787
|
-
const
|
|
4788
|
-
const d = lab ? Math.hypot(lab[0] - mine[0], lab[1] - mine[1], lab[2] - mine[2]) : 1e400;
|
|
5015
|
+
const d = far(l);
|
|
4789
5016
|
if (d < near || d === near && v > most) near = d, most = v, best = l;
|
|
4790
5017
|
}
|
|
4791
|
-
if (
|
|
5018
|
+
if (photo) {
|
|
5019
|
+
if (joined && near > SPECK_NEAR) continue;
|
|
5020
|
+
const inRange = (l) => labs[l] ? far(l) <= near + SPECK_NEAR : near > SPECK_NEAR;
|
|
5021
|
+
most = 0;
|
|
5022
|
+
let bestFar = 1e400;
|
|
5023
|
+
for (const [l, v] of votes) if (inRange(l) && (v > most || v === most && far(l) < bestFar)) most = v, best = l, bestFar = far(l);
|
|
5024
|
+
} else if (near > SPECK_CONTRAST && near < 1e400 && end - begin >= Math.max(SPECK_MIN, minArea * SPECK_SHAPE)) continue;
|
|
4792
5025
|
} else for (const [l, v] of votes) if (v > most) most = v, best = l;
|
|
4793
5026
|
if (best !== own) {
|
|
4794
5027
|
for (let k = begin; k < end; k++) labels[pixels[k]] = best;
|
package/package.json
CHANGED