inlaytrace 0.5.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 +13 -0
- package/README.md +23 -19
- package/dist/index.d.ts +51 -4
- package/dist/index.js +853 -62
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
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
|
+
|
|
10
|
+
## 0.6.0
|
|
11
|
+
|
|
12
|
+
### Minor Changes
|
|
13
|
+
|
|
14
|
+
- New `sticker(result, options)` and `stickerSvg(result, options)`: a solid backing behind a trace, so it prints and cuts as one sticker. The backing follows the art's outline grown by a margin (holes filled, tight inside corners rounded off so a blade can follow them, then traced into one smooth path), or is the smallest circle, upright oval, square or rectangle round it, with rounded corners if asked. Options give the margin, colour and corners, and a cut line with bleed: the backing reaches past the cut so a cut that lands a little off never shows a sliver of the background. A background shape is left out, and a sheet of objects gets a backing each.
|
|
15
|
+
|
|
3
16
|
## 0.5.0
|
|
4
17
|
|
|
5
18
|
### 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,16 +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
|
+
- `sticker(result, options)` / `stickerSvg(…)`: a solid backing behind a trace,
|
|
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 around it, with an optional cut line
|
|
96
|
+
and bleed.
|
|
93
97
|
|
|
94
98
|
## How it works
|
|
95
99
|
|
|
96
|
-
1. **Label** every pixel with a palette
|
|
97
|
-
|
|
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
|
|
98
102
|
a red/blue edge never grows a purple sliver.
|
|
99
|
-
2. **Clean up**: a pixel whose
|
|
103
|
+
2. **Clean up**: a pixel whose neighbors nearly all disagree joins them;
|
|
100
104
|
regions smaller than `speckle`² merge into their surroundings.
|
|
101
|
-
3. **Walk the cracks** between differently
|
|
105
|
+
3. **Walk the cracks** between differently labeled pixels. Where three regions
|
|
102
106
|
meet is a junction; each boundary between junctions is one chain, with one
|
|
103
107
|
region on either side.
|
|
104
108
|
4. **Fit each chain once**: corners are two straight sides meeting near the
|
|
@@ -106,7 +110,7 @@ says what each does.
|
|
|
106
110
|
cubic Béziers with matching tangents, smoothed only as much as the local
|
|
107
111
|
curvature allows.
|
|
108
112
|
5. **Assemble** each region from its chains (reversed where it sees them from
|
|
109
|
-
the other side).
|
|
113
|
+
the other side). Neighbors share the same fitted edge exactly.
|
|
110
114
|
|
|
111
115
|
The full write-up, with measurements, is in the
|
|
112
116
|
[docs](https://github.com/mtblck/inlaytrace/blob/main/docs/how-it-works/inlay.md);
|
|
@@ -117,16 +121,16 @@ every export.
|
|
|
117
121
|
|
|
118
122
|
## Measured
|
|
119
123
|
|
|
120
|
-
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
|
|
121
125
|
clean source art: 0.54% wrong pixels with 175 nodes with `shapes` on (0.70%
|
|
122
|
-
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
|
|
123
127
|
on its own. At 2048 px it traces in about
|
|
124
128
|
1.3 s (VTracer: 17.5 s, timed by hand). On pixel art both draw every pixel exactly; Inlay uses
|
|
125
129
|
about 14% fewer nodes and is about 4× faster on a 1024 px sprite.
|
|
126
130
|
|
|
127
131
|
On three photos (quantized palette, stacked), rendered back and compared with
|
|
128
|
-
each photo: a mean error of
|
|
129
|
-
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.
|
|
130
134
|
|
|
131
135
|
## License
|
|
132
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
|
/**
|
|
@@ -516,4 +520,47 @@ type SymmetryOptions = {
|
|
|
516
520
|
* no foreground.
|
|
517
521
|
*/
|
|
518
522
|
export declare function detectSymmetry(rgba: ArrayLike<number>, w: number, h: number, options?: SymmetryOptions): Symmetry[];
|
|
519
|
-
|
|
523
|
+
type StickerShape = 'contour' | 'circle' | 'oval' | 'square' | 'rectangle';
|
|
524
|
+
type StickerOptions = {
|
|
525
|
+
/** The backing's shape: the drawing's outline grown by `margin` ('contour', the default), or a circle, oval, square or rectangle round it. */
|
|
526
|
+
shape?: StickerShape;
|
|
527
|
+
/** Px from the drawing to the backing's edge (the cut). Default 4% of the drawing's longer side. */
|
|
528
|
+
margin?: number;
|
|
529
|
+
/** Contour: inside corners are rounded to at least this radius, in px, so a blade can follow them. Default half the margin. */
|
|
530
|
+
roundOff?: number;
|
|
531
|
+
/** Square and rectangle: corner radius, as a share (0–1) of half the shorter side. 0, the default, is sharp; 1 makes a rectangle a pill. */
|
|
532
|
+
corners?: number;
|
|
533
|
+
/** The backing's colour. Default '#ffffff'. */
|
|
534
|
+
color?: string;
|
|
535
|
+
/** Also give the cut line: the backing's edge before `bleed`. */
|
|
536
|
+
cutLine?: boolean;
|
|
537
|
+
/** Px the backing reaches past the cut line. Default 0. */
|
|
538
|
+
bleed?: number;
|
|
539
|
+
/** Leave out a background (a filled shape at least 95% of the drawing's width and height), as it isn't part of the art. Default true. */
|
|
540
|
+
background?: boolean;
|
|
541
|
+
};
|
|
542
|
+
type StickerResult = {
|
|
543
|
+
/** The backing: a filled shape, drawn behind the art. Its `d` is empty when there's nothing to back. */
|
|
544
|
+
backing: InlayPath;
|
|
545
|
+
/** Path data for the cut line, when `cutLine` was asked for. */
|
|
546
|
+
cutLine?: string;
|
|
547
|
+
/** The sticker's bounds in the drawing's px (the backing's, with its bleed). They can reach past the drawing's own edges. */
|
|
548
|
+
box: {
|
|
549
|
+
x: number;
|
|
550
|
+
y: number;
|
|
551
|
+
width: number;
|
|
552
|
+
height: number;
|
|
553
|
+
};
|
|
554
|
+
/** The art the backing was made for: the drawing's shapes, less its background (with `background`, the default). */
|
|
555
|
+
art: InlayPath[];
|
|
556
|
+
};
|
|
557
|
+
/** The backing for a trace, and optionally its cut line. */
|
|
558
|
+
export declare function sticker(r: InlayResult, o?: StickerOptions): StickerResult;
|
|
559
|
+
/**
|
|
560
|
+
* The sticker as an SVG document: the backing (`id="sticker-backing"`), the
|
|
561
|
+
* art over it, and the cut line on top (`id="cut-line"`, an unfilled
|
|
562
|
+
* hairline). The page is the sticker's box, so it can reach past the
|
|
563
|
+
* drawing's own edges.
|
|
564
|
+
*/
|
|
565
|
+
export declare function stickerSvg(r: InlayResult, o?: StickerOptions): string;
|
|
566
|
+
export type { AbortSignalLike, CenterlineOptions, CenterlineResult, Cubic, FitOptions, Gradient, GradientStop, ImageLike, InlayOptions, InlayPath, InlayResult, MirrorSymmetry, PathNode, PresetDefaults, PresetName, Pt, Rgb, RotationalSymmetry, StickerOptions, StickerResult, StickerShape, Subpath, Symmetry, SymmetryOptions, TraceAsyncOptions, TraceOptions, TraceStage, TraceStep };
|
package/dist/index.js
CHANGED
|
@@ -197,11 +197,11 @@ function close(sub) {
|
|
|
197
197
|
nodes.pop();
|
|
198
198
|
}
|
|
199
199
|
}
|
|
200
|
-
const num$
|
|
200
|
+
const num$2 = (n) => {
|
|
201
201
|
const r = Math.round(n * 1e3) / 1e3;
|
|
202
202
|
return Object.is(r, -0) ? "0" : String(r);
|
|
203
203
|
};
|
|
204
|
-
const pt = (p) => `${num$
|
|
204
|
+
const pt = (p) => `${num$2(p.x)} ${num$2(p.y)}`;
|
|
205
205
|
function segment(a, b) {
|
|
206
206
|
return a.out || b.in ? `C${pt(a.out ?? a)} ${pt(b.in ?? b)} ${pt(b)}` : `L${pt(b)}`;
|
|
207
207
|
}
|
|
@@ -253,7 +253,7 @@ const LOOSE = {
|
|
|
253
253
|
};
|
|
254
254
|
/** An ellipse whose axes differ by less than this is drawn as a circle. */
|
|
255
255
|
const ROUND = .015;
|
|
256
|
-
const KAPPA = .5522847498307936;
|
|
256
|
+
const KAPPA$1 = .5522847498307936;
|
|
257
257
|
/** Area, centroid and central second moments (per unit area) of a closed polygon. */
|
|
258
258
|
function moments(pts) {
|
|
259
259
|
let a = 0;
|
|
@@ -354,8 +354,8 @@ function quarter(cx, cy, cos, sin, a, b, ox, oy, t) {
|
|
|
354
354
|
const p = (u, v) => world(ox + u, oy + v, cx, cy, cos, sin);
|
|
355
355
|
return [
|
|
356
356
|
p(a * c0, b * s0),
|
|
357
|
-
p(a * c0 - KAPPA * a * s0, b * s0 + KAPPA * b * c0),
|
|
358
|
-
p(a * c1 + KAPPA * a * s1, b * s1 - KAPPA * b * c1),
|
|
357
|
+
p(a * c0 - KAPPA$1 * a * s0, b * s0 + KAPPA$1 * b * c0),
|
|
358
|
+
p(a * c1 + KAPPA$1 * a * s1, b * s1 - KAPPA$1 * b * c1),
|
|
359
359
|
p(a * c1, b * s1)
|
|
360
360
|
];
|
|
361
361
|
}
|
|
@@ -760,7 +760,7 @@ function fitOnce(points, closed, o, smoothLoop, fine) {
|
|
|
760
760
|
}
|
|
761
761
|
const run = smoothed.true;
|
|
762
762
|
const t = norm(sub$1(run[1], run[run.length - 2]));
|
|
763
|
-
const loop = flatten(fitMixed(smoothed, o.tolerance, t, mul(t, -1), 0));
|
|
763
|
+
const loop = flatten$1(fitMixed(smoothed, o.tolerance, t, mul(t, -1), 0));
|
|
764
764
|
return o.snap === false ? loop : snapLines(loop, true);
|
|
765
765
|
}
|
|
766
766
|
const breaks = corners.map((c) => ({
|
|
@@ -793,10 +793,10 @@ function fitOnce(points, closed, o, smoothLoop, fine) {
|
|
|
793
793
|
run[run.length - 1] = to.at;
|
|
794
794
|
out.push(...fitBetween(smooth(run, false, o.smoothing ?? SMOOTH$1), o.tolerance));
|
|
795
795
|
}
|
|
796
|
-
return o.snap === false ? flatten(out) : snapLines(flatten(out), closed);
|
|
796
|
+
return o.snap === false ? flatten$1(out) : snapLines(flatten$1(out), closed);
|
|
797
797
|
}
|
|
798
798
|
/** Straight runs written as exact lines, so they serialise as L, not C. */
|
|
799
|
-
function flatten(cubics) {
|
|
799
|
+
function flatten$1(cubics) {
|
|
800
800
|
return cubics.map((c) => isFlat(c) ? line(c[0], c[3]) : c);
|
|
801
801
|
}
|
|
802
802
|
/** Lines within this many degrees of horizontal, vertical or 45° are made exactly that... */
|
|
@@ -1465,7 +1465,7 @@ function peakPalette(rgba, maxColors, { minDistance = .07, minShare = .002, minD
|
|
|
1465
1465
|
if (picked.length >= maxColors) break;
|
|
1466
1466
|
const share = it.weight / opaque;
|
|
1467
1467
|
if (share < minDetail && picked.length > 0) break;
|
|
1468
|
-
if (!picked.every((p) => distance(p.lab, it.lab) >= minDistance)) continue;
|
|
1468
|
+
if (!picked.every((p) => distance$1(p.lab, it.lab) >= minDistance)) continue;
|
|
1469
1469
|
if ((share < minShare || flat !== null && share < EDGE_SHARE && it.flatWeight < EDGE_FLAT * it.weight) && isBlend(it.rgb, it.lab, picked, minDistance)) continue;
|
|
1470
1470
|
picked.push({
|
|
1471
1471
|
lab: it.lab,
|
|
@@ -1551,7 +1551,7 @@ function weigh(items, radius) {
|
|
|
1551
1551
|
}
|
|
1552
1552
|
for (const it of items) {
|
|
1553
1553
|
const [cl, ca, cb] = it.lab.map(cell);
|
|
1554
|
-
for (let i = cl - 1; i <= cl + 1; i++) for (let j = ca - 1; j <= ca + 1; j++) for (let k = cb - 1; k <= cb + 1; k++) for (const o of grid.get(`${i},${j},${k}`) ?? []) if (distance(o.lab, it.lab) <= radius) {
|
|
1554
|
+
for (let i = cl - 1; i <= cl + 1; i++) for (let j = ca - 1; j <= ca + 1; j++) for (let k = cb - 1; k <= cb + 1; k++) for (const o of grid.get(`${i},${j},${k}`) ?? []) if (distance$1(o.lab, it.lab) <= radius) {
|
|
1555
1555
|
it.weight += o.n;
|
|
1556
1556
|
it.flatWeight += o.flat;
|
|
1557
1557
|
if (o.n > it.n) it.peak = false;
|
|
@@ -1588,7 +1588,7 @@ function flatTints(flat, opaque, picked, room, minTint, minFlat) {
|
|
|
1588
1588
|
const tint = (it) => it.peak && it.lab[0] >= TINT_LIGHTNESS && it.weight >= minFlat * opaque;
|
|
1589
1589
|
for (const it of items.filter(tint).sort((a, b) => b.weight - a.weight)) {
|
|
1590
1590
|
if (out.length >= room) break;
|
|
1591
|
-
if ([...picked, ...out].every((p) => distance(p.lab, it.lab) >= minTint)) out.push({
|
|
1591
|
+
if ([...picked, ...out].every((p) => distance$1(p.lab, it.lab) >= minTint)) out.push({
|
|
1592
1592
|
lab: it.lab,
|
|
1593
1593
|
rgb: it.rgb
|
|
1594
1594
|
});
|
|
@@ -1641,13 +1641,13 @@ function closeFills(flat, opaque, picked, room, radius, minShare) {
|
|
|
1641
1641
|
const passed = /* @__PURE__ */ new Set();
|
|
1642
1642
|
while (out.length < room) {
|
|
1643
1643
|
const all = [...picked, ...out];
|
|
1644
|
-
const left = bins.filter((b) => !passed.has(b.key) && b.lab[0] < TINT_LIGHTNESS && all.every((p) => distance(p.lab, b.lab) > radius));
|
|
1644
|
+
const left = bins.filter((b) => !passed.has(b.key) && b.lab[0] < TINT_LIGHTNESS && all.every((p) => distance$1(p.lab, b.lab) > radius));
|
|
1645
1645
|
for (const b of left) b.weight = 0;
|
|
1646
1646
|
weigh(left, radius);
|
|
1647
1647
|
let best;
|
|
1648
1648
|
for (const b of left) if (!best || b.weight > best.weight) best = b;
|
|
1649
1649
|
if (!best || best.weight < minShare * opaque) break;
|
|
1650
|
-
const spot = left.filter((o) => distance(o.lab, best.lab) <= radius);
|
|
1650
|
+
const spot = left.filter((o) => distance$1(o.lab, best.lab) <= radius);
|
|
1651
1651
|
const sum = [
|
|
1652
1652
|
0,
|
|
1653
1653
|
0,
|
|
@@ -1664,7 +1664,7 @@ function closeFills(flat, opaque, picked, room, radius, minShare) {
|
|
|
1664
1664
|
sum[2] / n
|
|
1665
1665
|
];
|
|
1666
1666
|
const lab = oklab(rgb);
|
|
1667
|
-
if (inRamp(rgb, lab, all, RAMP_TOLERANCE, RAMP_SPAN * radius) || !all.every((p) => distance(p.lab, lab) > radius)) for (const o of spot) passed.add(o.key);
|
|
1667
|
+
if (inRamp(rgb, lab, all, RAMP_TOLERANCE, RAMP_SPAN * radius) || !all.every((p) => distance$1(p.lab, lab) > radius)) for (const o of spot) passed.add(o.key);
|
|
1668
1668
|
else out.push({
|
|
1669
1669
|
rgb,
|
|
1670
1670
|
lab
|
|
@@ -1680,11 +1680,11 @@ function closeFills(flat, opaque, picked, room, radius, minShare) {
|
|
|
1680
1680
|
* the truck's grey was 0.021 off it.
|
|
1681
1681
|
*/
|
|
1682
1682
|
function inRamp(rgb, lab, picked, tolerance, span) {
|
|
1683
|
-
const near = picked.filter((p) => distance(p.lab, lab) <= span);
|
|
1683
|
+
const near = picked.filter((p) => distance$1(p.lab, lab) <= span);
|
|
1684
1684
|
for (let i = 0; i < near.length; i++) for (let j = i + 1; j < near.length; j++) {
|
|
1685
1685
|
const a = near[i].rgb;
|
|
1686
1686
|
const b = near[j].rgb;
|
|
1687
|
-
if (distance(near[i].lab, near[j].lab) > span) continue;
|
|
1687
|
+
if (distance$1(near[i].lab, near[j].lab) > span) continue;
|
|
1688
1688
|
const d = [
|
|
1689
1689
|
b[0] - a[0],
|
|
1690
1690
|
b[1] - a[1],
|
|
@@ -1698,7 +1698,7 @@ function inRamp(rgb, lab, picked, tolerance, span) {
|
|
|
1698
1698
|
1,
|
|
1699
1699
|
2
|
|
1700
1700
|
].map((k) => Math.min(255, Math.max(0, a[k] + t * d[k])));
|
|
1701
|
-
if (distance(oklab(mix), lab) < tolerance) return true;
|
|
1701
|
+
if (distance$1(oklab(mix), lab) < tolerance) return true;
|
|
1702
1702
|
}
|
|
1703
1703
|
return false;
|
|
1704
1704
|
}
|
|
@@ -1718,7 +1718,7 @@ function isBlend(rgb, lab, picked, tolerance) {
|
|
|
1718
1718
|
const len = d[0] * d[0] + d[1] * d[1] + d[2] * d[2];
|
|
1719
1719
|
if (!len) continue;
|
|
1720
1720
|
const t = Math.min(1, Math.max(0, ((rgb[0] - a[0]) * d[0] + (rgb[1] - a[1]) * d[1] + (rgb[2] - a[2]) * d[2]) / len));
|
|
1721
|
-
if (distance(oklab([
|
|
1721
|
+
if (distance$1(oklab([
|
|
1722
1722
|
a[0] + t * d[0],
|
|
1723
1723
|
a[1] + t * d[1],
|
|
1724
1724
|
a[2] + t * d[2]
|
|
@@ -1880,7 +1880,7 @@ function quantizePalette(rgba, maxColors, { iterations = 16 } = {}) {
|
|
|
1880
1880
|
return [...new Set(hexes)];
|
|
1881
1881
|
}
|
|
1882
1882
|
const distance2 = (a, b) => (a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2 + (a[2] - b[2]) ** 2;
|
|
1883
|
-
const distance = (a, b) => Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2]);
|
|
1883
|
+
const distance$1 = (a, b) => Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2]);
|
|
1884
1884
|
const toHex = (rgb) => `#${rgb.map((c) => Math.round(c).toString(16).padStart(2, "0")).join("")}`;
|
|
1885
1885
|
/**
|
|
1886
1886
|
* Pixel art's palette: its colours exactly, most-used first. Pixel art has a
|
|
@@ -3728,7 +3728,7 @@ function toSvg(r) {
|
|
|
3728
3728
|
const head = defs.length ? `<defs>\n${defs.join("\n")}\n</defs>\n` : "";
|
|
3729
3729
|
return `<svg xmlns="http://www.w3.org/2000/svg" width="${attr(r.width)}" height="${attr(r.height)}" viewBox="0 0 ${attr(r.width)} ${attr(r.height)}">\n${head}${body}\n</svg>\n`;
|
|
3730
3730
|
}
|
|
3731
|
-
const num = (v) => String(Math.round(v * 100) / 100);
|
|
3731
|
+
const num$1 = (v) => String(Math.round(v * 100) / 100);
|
|
3732
3732
|
/** A short hash (FNV-1a, base 36). */
|
|
3733
3733
|
function fnv(s) {
|
|
3734
3734
|
let h = 2166136261;
|
|
@@ -3737,8 +3737,8 @@ function fnv(s) {
|
|
|
3737
3737
|
}
|
|
3738
3738
|
/** A gradient element, in the drawing's pixels. */
|
|
3739
3739
|
function gradientMarkup(g, id) {
|
|
3740
|
-
const stops = g.stops.map((s) => `<stop offset="${num(s.offset * 100)}%" stop-color="${attr(s.color)}"/>`).join("");
|
|
3741
|
-
return g.type === "linear" ? `<linearGradient id="${id}" gradientUnits="userSpaceOnUse" x1="${num(g.x1)}" y1="${num(g.y1)}" x2="${num(g.x2)}" y2="${num(g.y2)}">${stops}</linearGradient>` : `<radialGradient id="${id}" gradientUnits="userSpaceOnUse" cx="${num(g.cx)}" cy="${num(g.cy)}" r="${num(g.r)}">${stops}</radialGradient>`;
|
|
3740
|
+
const stops = g.stops.map((s) => `<stop offset="${num$1(s.offset * 100)}%" stop-color="${attr(s.color)}"/>`).join("");
|
|
3741
|
+
return g.type === "linear" ? `<linearGradient id="${id}" gradientUnits="userSpaceOnUse" x1="${num$1(g.x1)}" y1="${num$1(g.y1)}" x2="${num$1(g.x2)}" y2="${num$1(g.y2)}">${stops}</linearGradient>` : `<radialGradient id="${id}" gradientUnits="userSpaceOnUse" cx="${num$1(g.cx)}" cy="${num$1(g.cy)}" r="${num$1(g.r)}">${stops}</radialGradient>`;
|
|
3742
3742
|
}
|
|
3743
3743
|
/**
|
|
3744
3744
|
* Trace RGBA pixels (`w` × `h`, 4 bytes a pixel, as from canvas getImageData)
|
|
@@ -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;
|
|
@@ -8000,4 +8233,562 @@ function detectSymmetry(rgba, w, h, options = {}) {
|
|
|
8000
8233
|
});
|
|
8001
8234
|
return out.sort((a, b) => b.score - a.score);
|
|
8002
8235
|
}
|
|
8003
|
-
|
|
8236
|
+
// src/sticker.ts
|
|
8237
|
+
/**
|
|
8238
|
+
* Stickers: a solid backing behind a trace, so it can be printed and cut out
|
|
8239
|
+
* as one piece.
|
|
8240
|
+
*
|
|
8241
|
+
* The backing's shape is either the drawing's own outline grown by a margin
|
|
8242
|
+
* ('contour', a die-cut sticker) or a plain circle, oval, square or rectangle
|
|
8243
|
+
* round it. For a contour the shapes are drawn into a mask, their holes are
|
|
8244
|
+
* filled (a sticker is one solid piece, so the inside of an O is backing
|
|
8245
|
+
* too), the mask is grown by the margin with an exact distance transform, and
|
|
8246
|
+
* tight inside corners are rounded off so a blade can follow them; then Inlay
|
|
8247
|
+
* traces the mask, so the backing is one smooth path like any other shape.
|
|
8248
|
+
*
|
|
8249
|
+
* A cut line is the backing's edge without the bleed: the backing reaches
|
|
8250
|
+
* `bleed` past it, so a cut that lands a little off the line never shows a
|
|
8251
|
+
* sliver of the background.
|
|
8252
|
+
*/
|
|
8253
|
+
/** A shape at least this share of the drawing's width and height is its background (as the app's objects.ts). */
|
|
8254
|
+
const BACKGROUND = .95;
|
|
8255
|
+
/** The mask's longer side, in cells, at most; and its cells per drawing px, at most. */
|
|
8256
|
+
const GRID = 1024;
|
|
8257
|
+
const MAX_SCALE = 4;
|
|
8258
|
+
/** The circle and ellipse as cubics: the handle length for a quarter turn. */
|
|
8259
|
+
const KAPPA = .5522847498;
|
|
8260
|
+
const num = (v) => String(Math.round(v * 100) / 100);
|
|
8261
|
+
/** The backing for a trace, and optionally its cut line. */
|
|
8262
|
+
function sticker(r, o = {}) {
|
|
8263
|
+
const shape = o.shape ?? "contour";
|
|
8264
|
+
const color = o.color ?? "#ffffff";
|
|
8265
|
+
const art = o.background === false ? r.paths : r.paths.filter((p) => !isBackground(p, r));
|
|
8266
|
+
const shapes = art.map((p) => ({
|
|
8267
|
+
subs: parsePath(p.d),
|
|
8268
|
+
stroke: p.stroke ?? 0
|
|
8269
|
+
}));
|
|
8270
|
+
const box = boundsOf(shapes);
|
|
8271
|
+
if (!box) return {
|
|
8272
|
+
backing: {
|
|
8273
|
+
d: "",
|
|
8274
|
+
fill: color
|
|
8275
|
+
},
|
|
8276
|
+
...o.cutLine ? { cutLine: "" } : {},
|
|
8277
|
+
box: {
|
|
8278
|
+
x: 0,
|
|
8279
|
+
y: 0,
|
|
8280
|
+
width: r.width,
|
|
8281
|
+
height: r.height
|
|
8282
|
+
},
|
|
8283
|
+
art
|
|
8284
|
+
};
|
|
8285
|
+
const side = Math.max(box.x1 - box.x0, box.y1 - box.y0, 1);
|
|
8286
|
+
const margin = Math.max(0, o.margin ?? .04 * side);
|
|
8287
|
+
const bleed = Math.max(0, o.bleed ?? 0);
|
|
8288
|
+
const roundOff = Math.max(0, o.roundOff ?? margin / 2);
|
|
8289
|
+
const pad = margin + bleed + roundOff + 2;
|
|
8290
|
+
const scale = Math.min(MAX_SCALE, GRID / (side + 2 * pad));
|
|
8291
|
+
const ox = box.x0 - pad;
|
|
8292
|
+
const oy = box.y0 - pad;
|
|
8293
|
+
const w = Math.ceil((box.x1 - box.x0 + 2 * pad) * scale);
|
|
8294
|
+
const h = Math.ceil((box.y1 - box.y0 + 2 * pad) * scale);
|
|
8295
|
+
const toGrid = (p) => ({
|
|
8296
|
+
x: (p.x - ox) * scale,
|
|
8297
|
+
y: (p.y - oy) * scale
|
|
8298
|
+
});
|
|
8299
|
+
const art0 = new Uint8Array(w * h);
|
|
8300
|
+
for (const s of shapes) {
|
|
8301
|
+
const rings = s.subs.map((sub) => flatten(mapPoints([sub], toGrid)[0]));
|
|
8302
|
+
if (s.stroke > 0) strokeRings(art0, w, h, rings, s.stroke / 2 * scale);
|
|
8303
|
+
else fillRings(art0, w, h, rings);
|
|
8304
|
+
}
|
|
8305
|
+
fillHoles(art0, w, h);
|
|
8306
|
+
const fromGrid = (p) => ({
|
|
8307
|
+
x: p.x / scale + ox,
|
|
8308
|
+
y: p.y / scale + oy
|
|
8309
|
+
});
|
|
8310
|
+
let backingD;
|
|
8311
|
+
let cutD;
|
|
8312
|
+
let out;
|
|
8313
|
+
if (shape === "contour") {
|
|
8314
|
+
const grown = (by) => {
|
|
8315
|
+
const m = grow(art0, w, h, (by + roundOff) * scale);
|
|
8316
|
+
if (roundOff > 0) shrink(m, w, h, roundOff * scale);
|
|
8317
|
+
fillHoles(m, w, h);
|
|
8318
|
+
return m;
|
|
8319
|
+
};
|
|
8320
|
+
const trace = (m) => {
|
|
8321
|
+
const rgba = new Uint8ClampedArray(w * h * 4);
|
|
8322
|
+
for (let i = 0; i < w * h; i++) {
|
|
8323
|
+
const v = m[i] ? 0 : 255;
|
|
8324
|
+
rgba[i * 4] = rgba[i * 4 + 1] = rgba[i * 4 + 2] = v;
|
|
8325
|
+
rgba[i * 4 + 3] = 255;
|
|
8326
|
+
}
|
|
8327
|
+
return serializePath(traceRaw(rgba, w, h, {
|
|
8328
|
+
mode: "binary",
|
|
8329
|
+
threshold: 128,
|
|
8330
|
+
speckle: 0,
|
|
8331
|
+
tolerance: .75,
|
|
8332
|
+
cornerAngle: 60
|
|
8333
|
+
}).paths.flatMap((p) => mapPoints(parsePath(p.d), fromGrid)));
|
|
8334
|
+
};
|
|
8335
|
+
const back = grown(margin + bleed);
|
|
8336
|
+
backingD = trace(back);
|
|
8337
|
+
if (o.cutLine) cutD = bleed > 0 ? trace(grown(margin)) : backingD;
|
|
8338
|
+
out = maskBounds(back, w, h, fromGrid);
|
|
8339
|
+
} else {
|
|
8340
|
+
const hull = hullOf(art0, w, h).map(fromGrid);
|
|
8341
|
+
const make = (by) => geometric(shape, hull, box, by, o.corners ?? 0, margin);
|
|
8342
|
+
const back = make(margin + bleed);
|
|
8343
|
+
backingD = back.d;
|
|
8344
|
+
if (o.cutLine) cutD = bleed > 0 ? make(margin).d : back.d;
|
|
8345
|
+
out = back.box;
|
|
8346
|
+
}
|
|
8347
|
+
return {
|
|
8348
|
+
backing: {
|
|
8349
|
+
d: backingD,
|
|
8350
|
+
fill: color
|
|
8351
|
+
},
|
|
8352
|
+
...o.cutLine ? { cutLine: cutD ?? "" } : {},
|
|
8353
|
+
box: {
|
|
8354
|
+
x: out.x0,
|
|
8355
|
+
y: out.y0,
|
|
8356
|
+
width: out.x1 - out.x0,
|
|
8357
|
+
height: out.y1 - out.y0
|
|
8358
|
+
},
|
|
8359
|
+
art
|
|
8360
|
+
};
|
|
8361
|
+
}
|
|
8362
|
+
/**
|
|
8363
|
+
* The sticker as an SVG document: the backing (`id="sticker-backing"`), the
|
|
8364
|
+
* art over it, and the cut line on top (`id="cut-line"`, an unfilled
|
|
8365
|
+
* hairline). The page is the sticker's box, so it can reach past the
|
|
8366
|
+
* drawing's own edges.
|
|
8367
|
+
*/
|
|
8368
|
+
function stickerSvg(r, o = {}) {
|
|
8369
|
+
const s = sticker(r, o);
|
|
8370
|
+
const { x, y, width, height } = s.box;
|
|
8371
|
+
const svg = toSvg({
|
|
8372
|
+
...r,
|
|
8373
|
+
paths: s.art
|
|
8374
|
+
});
|
|
8375
|
+
const open = `<svg xmlns="http://www.w3.org/2000/svg" width="${num(width)}" height="${num(height)}" viewBox="${num(x)} ${num(y)} ${num(width)} ${num(height)}">\n`;
|
|
8376
|
+
const backing = s.backing.d ? `<path id="sticker-backing" d="${attr(s.backing.d)}" fill="${attr(s.backing.fill)}"/>\n` : "";
|
|
8377
|
+
const cut = s.cutLine ? `\n<path id="cut-line" d="${attr(s.cutLine)}" fill="none" stroke="#ec008c" stroke-width="1" vector-effect="non-scaling-stroke"/>` : "";
|
|
8378
|
+
return svg.replace(/^<svg[^>]*>\n/, open + backing).replace(/\n<\/svg>\n$/, `${cut}\n</svg>\n`);
|
|
8379
|
+
}
|
|
8380
|
+
/** Whether a shape is the drawing's background: filled, and at least 95% of its width and height. */
|
|
8381
|
+
function isBackground(p, r) {
|
|
8382
|
+
if (p.stroke) return false;
|
|
8383
|
+
const b = boundsOf([{
|
|
8384
|
+
subs: parsePath(p.d),
|
|
8385
|
+
stroke: 0
|
|
8386
|
+
}]);
|
|
8387
|
+
return !!b && b.x1 - b.x0 >= BACKGROUND * r.width && b.y1 - b.y0 >= BACKGROUND * r.height;
|
|
8388
|
+
}
|
|
8389
|
+
/** The shapes' bounds, by their nodes and handles (a curve stays inside its handles), and half a stroke round lines. */
|
|
8390
|
+
function boundsOf(shapes) {
|
|
8391
|
+
let x0 = 1e400;
|
|
8392
|
+
let y0 = 1e400;
|
|
8393
|
+
let x1 = -1e400;
|
|
8394
|
+
let y1 = -1e400;
|
|
8395
|
+
for (const s of shapes) {
|
|
8396
|
+
const e = s.stroke / 2;
|
|
8397
|
+
for (const sub of s.subs) for (const n of sub.nodes) for (const p of [
|
|
8398
|
+
n,
|
|
8399
|
+
n.in,
|
|
8400
|
+
n.out
|
|
8401
|
+
]) {
|
|
8402
|
+
if (!p) continue;
|
|
8403
|
+
x0 = Math.min(x0, p.x - e);
|
|
8404
|
+
y0 = Math.min(y0, p.y - e);
|
|
8405
|
+
x1 = Math.max(x1, p.x + e);
|
|
8406
|
+
y1 = Math.max(y1, p.y + e);
|
|
8407
|
+
}
|
|
8408
|
+
}
|
|
8409
|
+
return x0 <= x1 ? {
|
|
8410
|
+
x0,
|
|
8411
|
+
y0,
|
|
8412
|
+
x1,
|
|
8413
|
+
y1
|
|
8414
|
+
} : null;
|
|
8415
|
+
}
|
|
8416
|
+
/** A subpath as points: each node, and along a curve a point every cell or so. Closed rings repeat their first point at the end. */
|
|
8417
|
+
function flatten(s) {
|
|
8418
|
+
const out = [];
|
|
8419
|
+
const n = s.nodes.length;
|
|
8420
|
+
if (!n) return out;
|
|
8421
|
+
out.push({
|
|
8422
|
+
x: s.nodes[0].x,
|
|
8423
|
+
y: s.nodes[0].y
|
|
8424
|
+
});
|
|
8425
|
+
const last = s.closed ? n : n - 1;
|
|
8426
|
+
for (let i = 0; i < last; i++) {
|
|
8427
|
+
const a = s.nodes[i];
|
|
8428
|
+
const b = s.nodes[(i + 1) % n];
|
|
8429
|
+
if (!a.out && !b.in) {
|
|
8430
|
+
out.push({
|
|
8431
|
+
x: b.x,
|
|
8432
|
+
y: b.y
|
|
8433
|
+
});
|
|
8434
|
+
continue;
|
|
8435
|
+
}
|
|
8436
|
+
const c1 = a.out ?? a;
|
|
8437
|
+
const c2 = b.in ?? b;
|
|
8438
|
+
const len = Math.hypot(c1.x - a.x, c1.y - a.y) + Math.hypot(c2.x - c1.x, c2.y - c1.y) + Math.hypot(b.x - c2.x, b.y - c2.y);
|
|
8439
|
+
const k = Math.min(256, Math.max(1, Math.ceil(len)));
|
|
8440
|
+
for (let j = 1; j <= k; j++) {
|
|
8441
|
+
const t = j / k;
|
|
8442
|
+
const u = 1 - t;
|
|
8443
|
+
out.push({
|
|
8444
|
+
x: u * u * u * a.x + 3 * u * u * t * c1.x + 3 * u * t * t * c2.x + t * t * t * b.x,
|
|
8445
|
+
y: u * u * u * a.y + 3 * u * u * t * c1.y + 3 * u * t * t * c2.y + t * t * t * b.y
|
|
8446
|
+
});
|
|
8447
|
+
}
|
|
8448
|
+
}
|
|
8449
|
+
return out;
|
|
8450
|
+
}
|
|
8451
|
+
/** Fill one shape's rings into the mask by the nonzero rule, at cell centres. */
|
|
8452
|
+
function fillRings(mask, w, h, rings) {
|
|
8453
|
+
const rows = Array.from({ length: h }, () => []);
|
|
8454
|
+
for (const ring of rings) for (let i = 0; i < ring.length; i++) {
|
|
8455
|
+
const a = ring[i];
|
|
8456
|
+
const b = ring[(i + 1) % ring.length];
|
|
8457
|
+
if (a.y === b.y) continue;
|
|
8458
|
+
const dir = a.y < b.y ? 1 : -1;
|
|
8459
|
+
const [p, q] = a.y < b.y ? [a, b] : [b, a];
|
|
8460
|
+
const from = Math.max(0, Math.ceil(p.y - .5));
|
|
8461
|
+
const to = Math.min(h - 1, Math.ceil(q.y - .5) - 1);
|
|
8462
|
+
for (let y = from; y <= to; y++) {
|
|
8463
|
+
const cy = y + .5;
|
|
8464
|
+
rows[y].push(p.x + (cy - p.y) / (q.y - p.y) * (q.x - p.x), dir);
|
|
8465
|
+
}
|
|
8466
|
+
}
|
|
8467
|
+
for (let y = 0; y < h; y++) {
|
|
8468
|
+
const row = rows[y];
|
|
8469
|
+
if (!row.length) continue;
|
|
8470
|
+
const xs = [];
|
|
8471
|
+
for (let i = 0; i < row.length; i += 2) xs.push({
|
|
8472
|
+
x: row[i],
|
|
8473
|
+
d: row[i + 1]
|
|
8474
|
+
});
|
|
8475
|
+
xs.sort((a, b) => a.x - b.x);
|
|
8476
|
+
let wind = 0;
|
|
8477
|
+
for (let i = 0; i < xs.length - 1; i++) {
|
|
8478
|
+
wind += xs[i].d;
|
|
8479
|
+
if (!wind) continue;
|
|
8480
|
+
const from = Math.max(0, Math.ceil(xs[i].x - .5));
|
|
8481
|
+
const to = Math.min(w - 1, Math.ceil(xs[i + 1].x - .5) - 1);
|
|
8482
|
+
for (let x = from; x <= to; x++) mask[y * w + x] = 1;
|
|
8483
|
+
}
|
|
8484
|
+
}
|
|
8485
|
+
}
|
|
8486
|
+
/** Stamp a line's rings into the mask as round-capped strokes `r` cells wide each side. */
|
|
8487
|
+
function strokeRings(mask, w, h, rings, r) {
|
|
8488
|
+
const stamp = (cx, cy) => {
|
|
8489
|
+
const rr = Math.max(r, .5);
|
|
8490
|
+
for (let y = Math.max(0, Math.floor(cy - rr)); y <= Math.min(h - 1, Math.ceil(cy + rr)); y++) for (let x = Math.max(0, Math.floor(cx - rr)); x <= Math.min(w - 1, Math.ceil(cx + rr)); x++) if ((x + .5 - cx) ** 2 + (y + .5 - cy) ** 2 <= rr * rr) mask[y * w + x] = 1;
|
|
8491
|
+
};
|
|
8492
|
+
for (const ring of rings) for (let i = 0; i < ring.length; i++) {
|
|
8493
|
+
const a = ring[i];
|
|
8494
|
+
const b = ring[i + 1] ?? a;
|
|
8495
|
+
const k = Math.max(1, Math.ceil(Math.hypot(b.x - a.x, b.y - a.y) / Math.max(.5, r / 2)));
|
|
8496
|
+
for (let j = 0; j < k; j++) stamp(a.x + (b.x - a.x) * j / k, a.y + (b.y - a.y) * j / k);
|
|
8497
|
+
}
|
|
8498
|
+
}
|
|
8499
|
+
/** Fill the mask's holes: every empty cell that can't reach the edge through empty cells. */
|
|
8500
|
+
function fillHoles(mask, w, h) {
|
|
8501
|
+
const out = new Uint8Array(w * h);
|
|
8502
|
+
const stack = [];
|
|
8503
|
+
const push = (i) => {
|
|
8504
|
+
if (!mask[i] && !out[i]) {
|
|
8505
|
+
out[i] = 1;
|
|
8506
|
+
stack.push(i);
|
|
8507
|
+
}
|
|
8508
|
+
};
|
|
8509
|
+
for (let x = 0; x < w; x++) {
|
|
8510
|
+
push(x);
|
|
8511
|
+
push((h - 1) * w + x);
|
|
8512
|
+
}
|
|
8513
|
+
for (let y = 0; y < h; y++) {
|
|
8514
|
+
push(y * w);
|
|
8515
|
+
push(y * w + w - 1);
|
|
8516
|
+
}
|
|
8517
|
+
while (stack.length) {
|
|
8518
|
+
const i = stack.pop();
|
|
8519
|
+
const x = i % w;
|
|
8520
|
+
if (x > 0) push(i - 1);
|
|
8521
|
+
if (x < w - 1) push(i + 1);
|
|
8522
|
+
if (i >= w) push(i - w);
|
|
8523
|
+
if (i < w * (h - 1)) push(i + w);
|
|
8524
|
+
}
|
|
8525
|
+
for (let i = 0; i < w * h; i++) if (!out[i]) mask[i] = 1;
|
|
8526
|
+
}
|
|
8527
|
+
const INF = 0x56bc75e2d63100000;
|
|
8528
|
+
/** Squared distance from each cell to the nearest cell where `seed` is set (Felzenszwalb and Huttenlocher's exact transform). */
|
|
8529
|
+
function distance(seed, w, h) {
|
|
8530
|
+
const f = new Float64Array(w * h);
|
|
8531
|
+
for (let i = 0; i < w * h; i++) f[i] = seed(i) ? 0 : INF;
|
|
8532
|
+
const n = Math.max(w, h);
|
|
8533
|
+
const g = new Float64Array(n);
|
|
8534
|
+
const d = new Float64Array(n);
|
|
8535
|
+
const v = new Int32Array(n);
|
|
8536
|
+
const z = new Float64Array(n + 1);
|
|
8537
|
+
const pass = (len) => {
|
|
8538
|
+
let k = 0;
|
|
8539
|
+
v[0] = 0;
|
|
8540
|
+
z[0] = -0x56bc75e2d63100000;
|
|
8541
|
+
z[1] = INF;
|
|
8542
|
+
for (let q = 1; q < len; q++) {
|
|
8543
|
+
let s = (g[q] + q * q - (g[v[k]] + v[k] * v[k])) / (2 * q - 2 * v[k]);
|
|
8544
|
+
while (s <= z[k]) {
|
|
8545
|
+
k--;
|
|
8546
|
+
s = (g[q] + q * q - (g[v[k]] + v[k] * v[k])) / (2 * q - 2 * v[k]);
|
|
8547
|
+
}
|
|
8548
|
+
k++;
|
|
8549
|
+
v[k] = q;
|
|
8550
|
+
z[k] = s;
|
|
8551
|
+
z[k + 1] = INF;
|
|
8552
|
+
}
|
|
8553
|
+
k = 0;
|
|
8554
|
+
for (let q = 0; q < len; q++) {
|
|
8555
|
+
while (z[k + 1] < q) k++;
|
|
8556
|
+
const p = v[k];
|
|
8557
|
+
d[q] = (q - p) * (q - p) + g[p];
|
|
8558
|
+
}
|
|
8559
|
+
};
|
|
8560
|
+
for (let x = 0; x < w; x++) {
|
|
8561
|
+
for (let y = 0; y < h; y++) g[y] = f[y * w + x];
|
|
8562
|
+
pass(h);
|
|
8563
|
+
for (let y = 0; y < h; y++) f[y * w + x] = d[y];
|
|
8564
|
+
}
|
|
8565
|
+
for (let y = 0; y < h; y++) {
|
|
8566
|
+
for (let x = 0; x < w; x++) g[x] = f[y * w + x];
|
|
8567
|
+
pass(w);
|
|
8568
|
+
for (let x = 0; x < w; x++) f[y * w + x] = d[x];
|
|
8569
|
+
}
|
|
8570
|
+
return f;
|
|
8571
|
+
}
|
|
8572
|
+
/** A new mask: every cell within `r` cells of the mask. */
|
|
8573
|
+
function grow(mask, w, h, r) {
|
|
8574
|
+
const d = distance((i) => mask[i] === 1, w, h);
|
|
8575
|
+
const out = new Uint8Array(w * h);
|
|
8576
|
+
const rr = r * r;
|
|
8577
|
+
for (let i = 0; i < w * h; i++) if (d[i] <= rr) out[i] = 1;
|
|
8578
|
+
return out;
|
|
8579
|
+
}
|
|
8580
|
+
/** Shrink the mask in place: keep only cells more than `r` cells from its outside. */
|
|
8581
|
+
function shrink(mask, w, h, r) {
|
|
8582
|
+
const d = distance((i) => mask[i] === 0, w, h);
|
|
8583
|
+
const rr = r * r;
|
|
8584
|
+
for (let i = 0; i < w * h; i++) if (mask[i] && d[i] <= rr) mask[i] = 0;
|
|
8585
|
+
}
|
|
8586
|
+
/** The mask's bounds, in the drawing's px. */
|
|
8587
|
+
function maskBounds(mask, w, h, from) {
|
|
8588
|
+
let x0 = w;
|
|
8589
|
+
let y0 = h;
|
|
8590
|
+
let x1 = -1;
|
|
8591
|
+
let y1 = -1;
|
|
8592
|
+
for (let y = 0; y < h; y++) for (let x = 0; x < w; x++) if (mask[y * w + x]) {
|
|
8593
|
+
if (x < x0) x0 = x;
|
|
8594
|
+
if (x > x1) x1 = x;
|
|
8595
|
+
if (y < y0) y0 = y;
|
|
8596
|
+
if (y > y1) y1 = y;
|
|
8597
|
+
}
|
|
8598
|
+
const a = from({
|
|
8599
|
+
x: x0,
|
|
8600
|
+
y: y0
|
|
8601
|
+
});
|
|
8602
|
+
const b = from({
|
|
8603
|
+
x: x1 + 1,
|
|
8604
|
+
y: y1 + 1
|
|
8605
|
+
});
|
|
8606
|
+
return {
|
|
8607
|
+
x0: a.x,
|
|
8608
|
+
y0: a.y,
|
|
8609
|
+
x1: b.x,
|
|
8610
|
+
y1: b.y
|
|
8611
|
+
};
|
|
8612
|
+
}
|
|
8613
|
+
/** The convex hull of the mask's cells (their corners), in cells. */
|
|
8614
|
+
function hullOf(mask, w, h) {
|
|
8615
|
+
const pts = [];
|
|
8616
|
+
for (let y = 0; y < h; y++) {
|
|
8617
|
+
let a = -1;
|
|
8618
|
+
let b = -1;
|
|
8619
|
+
for (let x = 0; x < w; x++) if (mask[y * w + x]) {
|
|
8620
|
+
if (a < 0) a = x;
|
|
8621
|
+
b = x;
|
|
8622
|
+
}
|
|
8623
|
+
if (a < 0) continue;
|
|
8624
|
+
pts.push({
|
|
8625
|
+
x: a,
|
|
8626
|
+
y
|
|
8627
|
+
}, {
|
|
8628
|
+
x: a,
|
|
8629
|
+
y: y + 1
|
|
8630
|
+
}, {
|
|
8631
|
+
x: b + 1,
|
|
8632
|
+
y
|
|
8633
|
+
}, {
|
|
8634
|
+
x: b + 1,
|
|
8635
|
+
y: y + 1
|
|
8636
|
+
});
|
|
8637
|
+
}
|
|
8638
|
+
pts.sort((p, q) => p.x - q.x || p.y - q.y);
|
|
8639
|
+
if (pts.length < 3) return pts;
|
|
8640
|
+
const cross = (o, a, b) => (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x);
|
|
8641
|
+
const lower = [];
|
|
8642
|
+
for (const p of pts) {
|
|
8643
|
+
while (lower.length >= 2 && cross(lower[lower.length - 2], lower[lower.length - 1], p) <= 0) lower.pop();
|
|
8644
|
+
lower.push(p);
|
|
8645
|
+
}
|
|
8646
|
+
const upper = [];
|
|
8647
|
+
for (let i = pts.length - 1; i >= 0; i--) {
|
|
8648
|
+
const p = pts[i];
|
|
8649
|
+
while (upper.length >= 2 && cross(upper[upper.length - 2], upper[upper.length - 1], p) <= 0) upper.pop();
|
|
8650
|
+
upper.push(p);
|
|
8651
|
+
}
|
|
8652
|
+
return lower.slice(0, -1).concat(upper.slice(0, -1));
|
|
8653
|
+
}
|
|
8654
|
+
/** The smallest circle round the points (Welzl's, iterative; the points come shuffled by a fixed seed, so the result is repeatable). */
|
|
8655
|
+
function enclosingCircle(input) {
|
|
8656
|
+
const pts = input.slice();
|
|
8657
|
+
let seed = 1;
|
|
8658
|
+
for (let i = pts.length - 1; i > 0; i--) {
|
|
8659
|
+
seed = seed * 16807 % 2147483647;
|
|
8660
|
+
const j = seed % (i + 1);
|
|
8661
|
+
[pts[i], pts[j]] = [pts[j], pts[i]];
|
|
8662
|
+
}
|
|
8663
|
+
const inside = (c, p) => Math.hypot(p.x - c.x, p.y - c.y) <= c.r + 1e-7;
|
|
8664
|
+
const two = (a, b) => ({
|
|
8665
|
+
x: (a.x + b.x) / 2,
|
|
8666
|
+
y: (a.y + b.y) / 2,
|
|
8667
|
+
r: Math.hypot(a.x - b.x, a.y - b.y) / 2
|
|
8668
|
+
});
|
|
8669
|
+
const three = (a, b, c) => {
|
|
8670
|
+
const d = 2 * (a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y));
|
|
8671
|
+
if (Math.abs(d) < 1e-12) return [
|
|
8672
|
+
two(a, b),
|
|
8673
|
+
two(a, c),
|
|
8674
|
+
two(b, c)
|
|
8675
|
+
].reduce((m, k) => k.r > m.r ? k : m);
|
|
8676
|
+
const a2 = a.x * a.x + a.y * a.y;
|
|
8677
|
+
const b2 = b.x * b.x + b.y * b.y;
|
|
8678
|
+
const c2 = c.x * c.x + c.y * c.y;
|
|
8679
|
+
const x = (a2 * (b.y - c.y) + b2 * (c.y - a.y) + c2 * (a.y - b.y)) / d;
|
|
8680
|
+
const y = (a2 * (c.x - b.x) + b2 * (a.x - c.x) + c2 * (b.x - a.x)) / d;
|
|
8681
|
+
return {
|
|
8682
|
+
x,
|
|
8683
|
+
y,
|
|
8684
|
+
r: Math.hypot(a.x - x, a.y - y)
|
|
8685
|
+
};
|
|
8686
|
+
};
|
|
8687
|
+
if (!pts.length) return {
|
|
8688
|
+
x: 0,
|
|
8689
|
+
y: 0,
|
|
8690
|
+
r: 0
|
|
8691
|
+
};
|
|
8692
|
+
let c = {
|
|
8693
|
+
x: pts[0].x,
|
|
8694
|
+
y: pts[0].y,
|
|
8695
|
+
r: 0
|
|
8696
|
+
};
|
|
8697
|
+
for (let i = 1; i < pts.length; i++) {
|
|
8698
|
+
if (inside(c, pts[i])) continue;
|
|
8699
|
+
c = {
|
|
8700
|
+
x: pts[i].x,
|
|
8701
|
+
y: pts[i].y,
|
|
8702
|
+
r: 0
|
|
8703
|
+
};
|
|
8704
|
+
for (let j = 0; j < i; j++) {
|
|
8705
|
+
if (inside(c, pts[j])) continue;
|
|
8706
|
+
c = two(pts[i], pts[j]);
|
|
8707
|
+
for (let k = 0; k < j; k++) if (!inside(c, pts[k])) c = three(pts[i], pts[j], pts[k]);
|
|
8708
|
+
}
|
|
8709
|
+
}
|
|
8710
|
+
return c;
|
|
8711
|
+
}
|
|
8712
|
+
/** An ellipse (a circle when rx = ry) as four cubics. */
|
|
8713
|
+
function ellipseD(cx, cy, rx, ry) {
|
|
8714
|
+
const kx = KAPPA * rx;
|
|
8715
|
+
const ky = KAPPA * ry;
|
|
8716
|
+
return `M${num(cx + rx)} ${num(cy)}C${num(cx + rx)} ${num(cy + ky)} ${num(cx + kx)} ${num(cy + ry)} ${num(cx)} ${num(cy + ry)}C${num(cx - kx)} ${num(cy + ry)} ${num(cx - rx)} ${num(cy + ky)} ${num(cx - rx)} ${num(cy)}C${num(cx - rx)} ${num(cy - ky)} ${num(cx - kx)} ${num(cy - ry)} ${num(cx)} ${num(cy - ry)}C${num(cx + kx)} ${num(cy - ry)} ${num(cx + rx)} ${num(cy - ky)} ${num(cx + rx)} ${num(cy)}Z`;
|
|
8717
|
+
}
|
|
8718
|
+
/** A rectangle with corners of radius `r` (sharp at 0), clockwise from the top left. */
|
|
8719
|
+
function rectD(x0, y0, x1, y1, r) {
|
|
8720
|
+
if (r <= 0) return `M${num(x0)} ${num(y0)}L${num(x1)} ${num(y0)}L${num(x1)} ${num(y1)}L${num(x0)} ${num(y1)}Z`;
|
|
8721
|
+
const k = KAPPA * r;
|
|
8722
|
+
return `M${num(x0 + r)} ${num(y0)}L${num(x1 - r)} ${num(y0)}C${num(x1 - r + k)} ${num(y0)} ${num(x1)} ${num(y0 + r - k)} ${num(x1)} ${num(y0 + r)}L${num(x1)} ${num(y1 - r)}C${num(x1)} ${num(y1 - r + k)} ${num(x1 - r + k)} ${num(y1)} ${num(x1 - r)} ${num(y1)}L${num(x0 + r)} ${num(y1)}C${num(x0 + r - k)} ${num(y1)} ${num(x0)} ${num(y1 - r + k)} ${num(x0)} ${num(y1 - r)}L${num(x0)} ${num(y0 + r)}C${num(x0)} ${num(y0 + r - k)} ${num(x0 + r - k)} ${num(y0)} ${num(x0 + r)} ${num(y0)}Z`;
|
|
8723
|
+
}
|
|
8724
|
+
/**
|
|
8725
|
+
* A plain backing round the art's hull, `by` px out. Corners are sized on the
|
|
8726
|
+
* cut (`margin` out) and grow with the bleed, so the backing's edge stays the
|
|
8727
|
+
* same distance past the cut all the way round.
|
|
8728
|
+
*/
|
|
8729
|
+
function geometric(shape, hull, box, by, corners, margin) {
|
|
8730
|
+
if (shape === "circle") {
|
|
8731
|
+
const c = enclosingCircle(hull);
|
|
8732
|
+
const r = c.r + by;
|
|
8733
|
+
return {
|
|
8734
|
+
d: ellipseD(c.x, c.y, r, r),
|
|
8735
|
+
box: {
|
|
8736
|
+
x0: c.x - r,
|
|
8737
|
+
y0: c.y - r,
|
|
8738
|
+
x1: c.x + r,
|
|
8739
|
+
y1: c.y + r
|
|
8740
|
+
}
|
|
8741
|
+
};
|
|
8742
|
+
}
|
|
8743
|
+
let x0 = 1e400;
|
|
8744
|
+
let y0 = 1e400;
|
|
8745
|
+
let x1 = -1e400;
|
|
8746
|
+
let y1 = -1e400;
|
|
8747
|
+
for (const p of hull) {
|
|
8748
|
+
x0 = Math.min(x0, p.x);
|
|
8749
|
+
y0 = Math.min(y0, p.y);
|
|
8750
|
+
x1 = Math.max(x1, p.x);
|
|
8751
|
+
y1 = Math.max(y1, p.y);
|
|
8752
|
+
}
|
|
8753
|
+
if (!(x0 <= x1)) ({x0, y0, x1, y1} = box);
|
|
8754
|
+
const cx = (x0 + x1) / 2;
|
|
8755
|
+
const cy = (y0 + y1) / 2;
|
|
8756
|
+
if (shape === "oval") {
|
|
8757
|
+
const a = Math.max((x1 - x0) / 2, 1e-6);
|
|
8758
|
+
const b = Math.max((y1 - y0) / 2, 1e-6);
|
|
8759
|
+
let k = 0;
|
|
8760
|
+
for (const p of hull) k = Math.max(k, Math.hypot((p.x - cx) / a, (p.y - cy) / b));
|
|
8761
|
+
const rx = a * k + by;
|
|
8762
|
+
const ry = b * k + by;
|
|
8763
|
+
return {
|
|
8764
|
+
d: ellipseD(cx, cy, rx, ry),
|
|
8765
|
+
box: {
|
|
8766
|
+
x0: cx - rx,
|
|
8767
|
+
y0: cy - ry,
|
|
8768
|
+
x1: cx + rx,
|
|
8769
|
+
y1: cy + ry
|
|
8770
|
+
}
|
|
8771
|
+
};
|
|
8772
|
+
}
|
|
8773
|
+
if (shape === "square") {
|
|
8774
|
+
const s = Math.max(x1 - x0, y1 - y0) / 2;
|
|
8775
|
+
x0 = cx - s;
|
|
8776
|
+
x1 = cx + s;
|
|
8777
|
+
y0 = cy - s;
|
|
8778
|
+
y1 = cy + s;
|
|
8779
|
+
}
|
|
8780
|
+
const cut = Math.min(x1 - x0, y1 - y0) / 2 + margin;
|
|
8781
|
+
const r = Math.min(1, Math.max(0, corners)) * cut;
|
|
8782
|
+
const R = r > 0 ? r + (by - margin) : 0;
|
|
8783
|
+
const b = {
|
|
8784
|
+
x0: x0 - by,
|
|
8785
|
+
y0: y0 - by,
|
|
8786
|
+
x1: x1 + by,
|
|
8787
|
+
y1: y1 + by
|
|
8788
|
+
};
|
|
8789
|
+
return {
|
|
8790
|
+
d: rectD(b.x0, b.y0, b.x1, b.y1, Math.min(R, (b.x1 - b.x0) / 2, (b.y1 - b.y0) / 2)),
|
|
8791
|
+
box: b
|
|
8792
|
+
};
|
|
8793
|
+
}
|
|
8794
|
+
export { boostLines, centerline, centerlineToSvg, deblur, detectSymmetry, edgeBlur, exactPalette, fitChain, isFlat, mapPoints, oklab, parsePath, peakPalette, pointAt, presets, quantizePalette, serializePath, smoothShading, sticker, stickerSvg, toSvg, trace, traceAsync, traceCenterline, traceInlay };
|
package/package.json
CHANGED