inlaytrace 0.2.0 → 0.3.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 +40 -18
- package/dist/index.d.ts +65 -1
- package/dist/index.js +799 -45
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,41 +1,63 @@
|
|
|
1
1
|
# inlaytrace
|
|
2
2
|
|
|
3
|
+
## 0.3.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- New `closeGaps` option: a line broken by a small gap (up to 1.6 times its width, and 5.5 px) is joined before tracing, in line art's ink (`mode: 'binary'`), Outline art's lines (`strokes`) and `centerline`. Dashes and gaps drawn on purpose stay open. Off by default.
|
|
8
|
+
|
|
9
|
+
## 0.3.0
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- Painted lines stay through smoothing and clean-up: `boostLines` can mark the pixels it darkens (a `lines` array), `smoothShading` takes a `keep` mask it leaves alone, and the tracer's new `lines` option stops a marked line from being folded into a neighbour as a sliver. A dark outline between two dark areas (trousers against a night background) now keeps them apart.
|
|
14
|
+
- New `boostLines(rgba, width, height, radius?)`: darkens faint painted outlines (thin lines darker than both their sides, found with a black-hat filter) before the palette is picked, so a mouth on a shadowed face or a line between two near-equal colours labels apart from them instead of being lost. Short dark marks are left alone as texture.
|
|
15
|
+
- New `smoothShading(rgba, width, height)`: an edge-preserving (bilateral) filter for painted art, run before picking the palette. Brush and JPEG noise inside a shadow evens out, so the shadow traces as one shape instead of flecks of its two nearest colours, while the edges between colours stay sharp.
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- `centerlineToSvg` escapes the drawing's width and height like every other value it writes.
|
|
20
|
+
- Faster colour labelling: converting colours to OKLab reads linear light from a table instead of a power per channel, which cut a 24-colour trace of an 820 × 1162 painted poster from 20 s to 11 s with the same output.
|
|
21
|
+
- Gradient fills no longer smear one shape into its neighbour on painted art (a face into the hair or shadow beside it): a gradient must stay within 20 RGB levels of the image, and must draw each of the bands it replaces no worse than that band's own colour did.
|
|
22
|
+
- Smaller files with Keep painted outlines: with the tracer's `lines` option, the pieces one painted line labels as (a few dark shades along its length) now join each other instead of each staying a shape, and a marked piece too short to part anything folds into its neighbour like any sliver. 9–15% smaller on a painted film poster, with its outlines kept.
|
|
23
|
+
- Strokes keep sharp corners sharp (a flag's swallowtail no longer comes out rounded), and in outline art a dot drawn on a line's end (a flagpole's knob) stays a shape instead of being lost under the stroke.
|
|
24
|
+
|
|
3
25
|
## 0.2.0
|
|
4
26
|
|
|
5
27
|
### Minor Changes
|
|
6
28
|
|
|
7
|
-
-
|
|
8
|
-
-
|
|
9
|
-
-
|
|
10
|
-
|
|
29
|
+
- `stacked: true` now works for flat art too: shapes come back to front, each whole, with the parts hidden behind other shapes completed (a disc behind a bar traces as a whole disc). Which shape is in front is read from the junctions where two shapes' shared edge ends. Photos (`photo: true`) stack as before.
|
|
30
|
+
- Flat outline illustrations (thin coloured outlines on white, pale tinted fills) now trace. Speck clean-up folds a speck into its nearest colour, and no longer dissolves a thin stroke that lossy compression split into two shades of its ink. `peakPalette` takes a new `width` option: given it, pale tints are found by their flatness (new options `minTint`, `minFlat`), and a colour that is only ever an anti-aliased edge is left out. Slivers under 2 px wide fold into a neighbour of nearly the same colour. New option `photo` keeps the old clean-up for photos (specks join the colour they share the most edge with, no sliver pass), which is what a stacked `quantizePalette` trace wants. Binary and centerline modes take `threshold: 'auto'` (Otsu, falling back to 128 on a page with nothing to split). A thin closed shape (a 3 px letter stem) no longer collapses to a line, and the fitter no longer grows spikes on thin letters. On a new outline fixture, Logo went from 13.0% to 2.1% of pixels wrong; the badge, emblems and photos (with `photo`) score the same.
|
|
31
|
+
- New `strokes` option for flat outline art: in palette mode, the colours drawn mostly as thin, long lines are traced down their middle as one stroked path each (`InlayPath.stroke` is the width measured from the ink), with their anti-aliased edges and darker shades joined in, and the shapes either side meet beneath them. Small lettering, short marks and solid shapes stay shapes. `scale` says how far the image was enlarged, so the line widths scale with it. Off by default; output without it is unchanged.
|
|
32
|
+
|
|
11
33
|
`toSvg` and `centerlineToSvg` now escape the attribute values they write, so a palette colour given with a quote in it can't end its attribute.
|
|
12
|
-
-
|
|
13
|
-
-
|
|
34
|
+
- `peakPalette` finds colours spread over several histogram bins (a photo's red lettering, a small decal) instead of stopping at the greys; flat art's palettes are unchanged. New option `minDetail`. Stacked shapes now reach under the neighbours painted after them, so no hairline seam shows where two shapes meet (12% closer on photos, about a third more path data).
|
|
35
|
+
- Lines within a degree of horizontal, vertical or 45° are now exactly that, so a square stays square after JPEG and lines either side of a junction line up. `fitChain` takes `snap: false` to keep lines as fitted.
|
|
14
36
|
|
|
15
37
|
### Patch Changes
|
|
16
38
|
|
|
17
|
-
-
|
|
18
|
-
-
|
|
19
|
-
-
|
|
20
|
-
-
|
|
21
|
-
-
|
|
22
|
-
-
|
|
23
|
-
-
|
|
24
|
-
-
|
|
25
|
-
-
|
|
39
|
+
- `centerline(rgba, width, height, options)` (and `traceCenterline`, `centerlineToSvg`): single strokes down the middle of the ink, for pen plotters and engraving, with the stroke width measured from the ink or set.
|
|
40
|
+
- Add a funding link (Ko-fi) to the package metadata.
|
|
41
|
+
- New `gradients` option: soft shading that a palette splits into bands of tints is traced as one shape with a linear or radial gradient (`path.gradient`, written by `toSvg` as `<linearGradient>` / `<radialGradient>`), when the gradient fits much better than a flat colour. Off by default.
|
|
42
|
+
- Lines about a pixel wide stay in one piece at a slant, instead of breaking into dashes or vanishing.
|
|
43
|
+
- Centerline and outline-art strokes run straight through junctions. Thinning bends the skeleton toward each junction, so an X of thick strokes came out pinched at its crossing and a T's stem bent to meet the bar; within a stroke width of a junction the arms are now cut back and drawn straight on to where their own lines meet.
|
|
44
|
+
- Small text and dots beside flat art now trace. Thin anti-aliased strokes (1.5 px grey letters) were labelled as the paper, pale edges as a nearby colour, and each letter then cleared as a speck. In flat art (not `photo`), a pixel between colours and close to none of them is now read as a mix of the pair that explains it best; one next to only one other colour keeps its own; and a speck that stands well apart from every colour around it stays from an eighth of the speck area up, so raising `speckle` still clears it. Poster output is unchanged.
|
|
45
|
+
- In images with exactly flat colours, such as most PNG exports, edges are placed between pixels from their anti-aliasing instead of on the pixel grid: straight sides land where they really are, and small shapes come out cleaner (2–16% fewer wrong pixels on the PNG fixtures, for the same nodes). Whether an image qualifies is judged from its pixels, so typical JPEG and WebP traces and Poster are unchanged.
|
|
46
|
+
- `toSvg` escapes the drawing's width and height like every other value it writes.
|
|
47
|
+
- Lines one or two pixels wide keep their width to their ends. Where such a line's outline turned back at its end inside one fitted run (an arm of a T or a Y), its two sides were joined at a point, so each arm tapered to nothing and thin outlines looked broken.
|
|
26
48
|
|
|
27
49
|
## 0.1.3
|
|
28
50
|
|
|
29
51
|
### Patch Changes
|
|
30
52
|
|
|
31
|
-
-
|
|
32
|
-
-
|
|
53
|
+
- New `shapes` option: a closed outline that is a circle, an ellipse, a rectangle, a rounded rectangle or a regular polygon is drawn as that exact shape instead of fitted curves (on the sample badge, 0.70% → 0.54% wrong pixels). Off by default.
|
|
54
|
+
- `detectSymmetry(rgba, width, height, options?)` finds the likeliest mirror axes (vertical and horizontal) and rotational orders (2–8 by default) in an image, each scored by the share of the drawing that matches its reflection or turn.
|
|
33
55
|
|
|
34
56
|
## 0.1.2
|
|
35
57
|
|
|
36
58
|
### Patch Changes
|
|
37
59
|
|
|
38
|
-
-
|
|
60
|
+
- Photos: `quantizePalette(rgba, n)` picks a photo's colours by k-means in OKLab (deterministic), and the `stacked` option draws each shape whole, larger shapes first, instead of cutting holes (same picture, less path data, no hairline seams). Also fixes speck merging, which could strand fragments of merged specks as new specks: on photos that left thousands of extra shapes, and a larger `speckle` gave more shapes rather than fewer.
|
|
39
61
|
|
|
40
62
|
## 0.1.1
|
|
41
63
|
|
package/dist/index.d.ts
CHANGED
|
@@ -202,7 +202,8 @@ type InlayOptions = FitOptions & {
|
|
|
202
202
|
/**
|
|
203
203
|
* How many times the drawing was enlarged before tracing (an upscaled small
|
|
204
204
|
* image, or a full-size trace of one usually traced smaller). The widths
|
|
205
|
-
* `strokes` takes as lines (2.5–8 px) grow with it
|
|
205
|
+
* `strokes` takes as lines (2.5–8 px) grow with it, and so do the gaps
|
|
206
|
+
* `closeGaps` closes. Default 1.
|
|
206
207
|
*/
|
|
207
208
|
scale?: number;
|
|
208
209
|
/**
|
|
@@ -212,6 +213,20 @@ type InlayOptions = FitOptions & {
|
|
|
212
213
|
* pixel art. Off by default: a cutter or a plotter can't draw a gradient.
|
|
213
214
|
*/
|
|
214
215
|
gradients?: boolean;
|
|
216
|
+
/**
|
|
217
|
+
* Painted lines (one entry per pixel, non-zero on a line), as `boostLines`
|
|
218
|
+
* marks them. A region mostly on them is a drawn line, not one ink's two
|
|
219
|
+
* shades, so it folds only into another piece of line: a dark outline
|
|
220
|
+
* between two dark shapes is nearly their colour, and folding it in joined
|
|
221
|
+
* them.
|
|
222
|
+
*/
|
|
223
|
+
lines?: ArrayLike<number>;
|
|
224
|
+
/**
|
|
225
|
+
* Join a line's ends across small breaks (up to 1.6 times the line's width,
|
|
226
|
+
* and 5.5 px) before tracing (gaps.ts): line art's ink, and Outline art's
|
|
227
|
+
* lines (`strokes`). Off by default: some gaps are drawn.
|
|
228
|
+
*/
|
|
229
|
+
closeGaps?: boolean;
|
|
215
230
|
} & (
|
|
216
231
|
/**
|
|
217
232
|
* `merge` folds colours together before any region is built, so the edge
|
|
@@ -274,6 +289,51 @@ export declare function mergeLabels(labels: Int32Array, colors: string[], merge:
|
|
|
274
289
|
/** Otsu's threshold over opaque pixels: the split that best separates two populations. */
|
|
275
290
|
export declare function otsu(lum: Uint8Array, alpha: Uint8Array): number;
|
|
276
291
|
//#endregion
|
|
292
|
+
//#region src/smooth.d.ts
|
|
293
|
+
/**
|
|
294
|
+
* Edge-preserving smoothing for painted art, before its palette is picked.
|
|
295
|
+
*
|
|
296
|
+
* A painted poster's skin shadow is a soft ramp with brush and JPEG noise on
|
|
297
|
+
* it. Labelled pixel by pixel, the noise flips each pixel between the two
|
|
298
|
+
* nearest palette colours, and the shadow traces as ragged patches. A
|
|
299
|
+
* bilateral filter averages each pixel with neighbours of a similar colour
|
|
300
|
+
* only, so the noise inside a shadow evens out and the edges between colours
|
|
301
|
+
* (an arm against the dark coat, a line on a face) stay where they are.
|
|
302
|
+
*
|
|
303
|
+
* On a painted film poster traced at 2× in Illustration, this turned the
|
|
304
|
+
* blotchy arm and neck shadows into whole shapes, kept the face lines, and cut
|
|
305
|
+
* the file from 3.3 MB to 2.1 MB; the filter took 4 s of the trace. Stronger
|
|
306
|
+
* (radius 6, three passes at sigma 25) began to wash out lines in the hair.
|
|
307
|
+
*/
|
|
308
|
+
/**
|
|
309
|
+
* A smoothed copy of `rgba` (`width` × `height`, RGBA). Transparent pixels, and
|
|
310
|
+
* any marked in `keep` (one entry per pixel, non-zero to keep), are left as
|
|
311
|
+
* they are and never averaged into their neighbours.
|
|
312
|
+
*/
|
|
313
|
+
export declare function smoothShading(rgba: ArrayLike<number>, width: number, height: number, keep?: ArrayLike<number>): Uint8ClampedArray;
|
|
314
|
+
/**
|
|
315
|
+
* Painted outlines made plain, before the palette is picked. A painter's line
|
|
316
|
+
* between two near-equal colours (a mouth on a shadowed face, dark trousers
|
|
317
|
+
* against a dark background) is only a few levels darker than either side, so
|
|
318
|
+
* it labels as one of them and is lost. A black-hat filter (the image's
|
|
319
|
+
* closing minus the image, over `radius` px) finds lines up to about twice
|
|
320
|
+
* that wide that are darker than both sides; each is darkened by twice its
|
|
321
|
+
* depth, at most LINE_MAX, colour kept, so it labels apart from both sides.
|
|
322
|
+
* Only lines that run a while are kept: brick, foliage and brush texture are
|
|
323
|
+
* dark too, but short. When `lines` (one entry per pixel) is given, each
|
|
324
|
+
* pixel darkened is marked in it with 1, for `smoothShading`'s `keep` and
|
|
325
|
+
* the tracer's `lines`, which leave them be.
|
|
326
|
+
*
|
|
327
|
+
* On a painted film poster at 2× (radius 3), with smoothing, this brought back
|
|
328
|
+
* the mouth, eyes, brows and jaw lines Illustration lost, and the outline
|
|
329
|
+
* parting dark trousers from a dark background, for twice the paths (2.1 to
|
|
330
|
+
* 3.6 MB). The trousers merged with the background until the lines were marked:
|
|
331
|
+
* the outline was found, but smoothing blurred it back into its sides, and,
|
|
332
|
+
* left unsmoothed, it labelled as the near-black of the trousers, close
|
|
333
|
+
* enough to the dark grey on either side to be folded in as a sliver.
|
|
334
|
+
*/
|
|
335
|
+
export declare function boostLines(rgba: ArrayLike<number>, width: number, height: number, radius?: number, lines?: Uint8Array): Uint8ClampedArray;
|
|
336
|
+
//#endregion
|
|
277
337
|
//#region src/centerline.d.ts
|
|
278
338
|
type CenterlineOptions = {
|
|
279
339
|
/** Ink is darker than this (0–255); 'auto' splits ink from paper by the image's histogram (Otsu); null compares each pixel with its neighbourhood instead. */
|
|
@@ -288,6 +348,10 @@ type CenterlineOptions = {
|
|
|
288
348
|
strokeWidth?: number;
|
|
289
349
|
/** The stroke colour. Default black. */
|
|
290
350
|
color?: string;
|
|
351
|
+
/** Join a line's ends across small breaks before tracing (gaps.ts). Off by default. */
|
|
352
|
+
closeGaps?: boolean;
|
|
353
|
+
/** How many times the drawing was enlarged before tracing: gap closing's pixel limits grow with it. Default 1. */
|
|
354
|
+
scale?: number;
|
|
291
355
|
};
|
|
292
356
|
/** One stroked path: open subpaths (and closed loops), drawn with round caps and joins. */
|
|
293
357
|
type CenterlinePath = {
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
//#region src/shapes.ts
|
|
2
2
|
/** How closely an outline must follow a shape: RMS and worst distance, in px, of its pixel-edge midpoints. */
|
|
3
|
-
const MAX_RMS = .3;
|
|
3
|
+
const MAX_RMS$1 = .3;
|
|
4
4
|
const MAX_OFF = .85;
|
|
5
5
|
/** An ellipse whose axes differ by less than this is drawn as a circle. */
|
|
6
6
|
const ROUND = .015;
|
|
@@ -69,7 +69,7 @@ function fits(points, distance) {
|
|
|
69
69
|
if (d > MAX_OFF) return false;
|
|
70
70
|
sum += d * d;
|
|
71
71
|
}
|
|
72
|
-
return Math.sqrt(sum / points.length) <= MAX_RMS;
|
|
72
|
+
return Math.sqrt(sum / points.length) <= MAX_RMS$1;
|
|
73
73
|
}
|
|
74
74
|
/** Into a frame centred on (cx, cy) and turned by `angle`. */
|
|
75
75
|
const local = (p, cx, cy, cos, sin) => {
|
|
@@ -1259,10 +1259,6 @@ function isBlend(rgb, lab, picked, tolerance) {
|
|
|
1259
1259
|
return false;
|
|
1260
1260
|
}
|
|
1261
1261
|
function oklab([r, g, b]) {
|
|
1262
|
-
const lin = (c) => {
|
|
1263
|
-
c /= 255;
|
|
1264
|
-
return c <= .04045 ? c / 12.92 : ((c + .055) / 1.055) ** 2.4;
|
|
1265
|
-
};
|
|
1266
1262
|
const [lr, lg, lb] = [
|
|
1267
1263
|
lin(r),
|
|
1268
1264
|
lin(g),
|
|
@@ -1277,6 +1273,24 @@ function oklab([r, g, b]) {
|
|
|
1277
1273
|
.0259040371 * l + .7827717662 * m - .808675766 * s
|
|
1278
1274
|
];
|
|
1279
1275
|
}
|
|
1276
|
+
const linear = (c) => {
|
|
1277
|
+
c /= 255;
|
|
1278
|
+
return c <= .04045 ? c / 12.92 : ((c + .055) / 1.055) ** 2.4;
|
|
1279
|
+
};
|
|
1280
|
+
/**
|
|
1281
|
+
* sRGB to linear light, from a table at 1/16 steps: labelling reads hundreds of
|
|
1282
|
+
* mixes per in-between colour (every pair of a 24-colour palette), and the
|
|
1283
|
+
* power was half its time. Interpolated, it is within 1e-7 of the formula.
|
|
1284
|
+
*/
|
|
1285
|
+
const LIN_STEPS = 16;
|
|
1286
|
+
const LIN = Float64Array.from({ length: 4082 }, (_, i) => linear(i / LIN_STEPS));
|
|
1287
|
+
function lin(c) {
|
|
1288
|
+
if (!(c > 0)) return 0;
|
|
1289
|
+
if (c >= 255) return linear(c);
|
|
1290
|
+
const x = c * LIN_STEPS;
|
|
1291
|
+
const i = Math.floor(x);
|
|
1292
|
+
return LIN[i] + (LIN[i + 1] - LIN[i]) * (x - i);
|
|
1293
|
+
}
|
|
1280
1294
|
/**
|
|
1281
1295
|
* A photo's palette: `maxColors` colours that best summarise it, by k-means in
|
|
1282
1296
|
* OKLab (so the clusters are perceptual, not RGB boxes).
|
|
@@ -1876,7 +1890,9 @@ function complete(p0, t0, p1, t1, visible, straight0, straight1, hidden) {
|
|
|
1876
1890
|
* the skeleton toward each junction, so within a stroke width of one the
|
|
1877
1891
|
* arms are dropped and drawn straight on to where their own lines meet.
|
|
1878
1892
|
* Where strokes cross or branch, chains that carry on in the same
|
|
1879
|
-
* direction are joined, so a plotter lifts its pen less.
|
|
1893
|
+
* direction are joined, so a plotter lifts its pen less. Thinning also
|
|
1894
|
+
* rounds a sharp corner off, turning inside it, so a corner's arms are
|
|
1895
|
+
* drawn straight on to where they meet.
|
|
1880
1896
|
* 5. Fit each chain with the same curve fitter as outlines.
|
|
1881
1897
|
*/
|
|
1882
1898
|
/** The ink's skeleton as fitted subpaths (open, or closed for a loop), and the stroke width measured from the ink. `thinned` is the skeleton, if already thinned; it is pruned in place. */
|
|
@@ -1885,7 +1901,7 @@ function skeletonStrokes(ink, w, h, o, thinned) {
|
|
|
1885
1901
|
const measured = strokeWidthOf(skel, ink, w);
|
|
1886
1902
|
prune(skel, w, h, Math.max(3, Math.round(measured)));
|
|
1887
1903
|
const subs = [];
|
|
1888
|
-
for (const c of joinStraight(refitJunctions(walk(skel, w, h), measured))) {
|
|
1904
|
+
for (const c of joinStraight(refitJunctions(walk(skel, w, h), measured)).map((c) => refitCorners(c, measured, ink, w, h))) {
|
|
1889
1905
|
if (c.pts.length < 2) continue;
|
|
1890
1906
|
const cubics = c.pts.length < 4 ? polyline(c.pts) : fitChain(c.pts, c.closed, {
|
|
1891
1907
|
tolerance: o.tolerance,
|
|
@@ -1945,7 +1961,7 @@ function regionWidths(ink, w, h, comp, m) {
|
|
|
1945
1961
|
};
|
|
1946
1962
|
}
|
|
1947
1963
|
/** The eight neighbours P2…P9, clockwise from north, as offsets. */
|
|
1948
|
-
const DX = [
|
|
1964
|
+
const DX$1 = [
|
|
1949
1965
|
0,
|
|
1950
1966
|
1,
|
|
1951
1967
|
1,
|
|
@@ -1955,7 +1971,7 @@ const DX = [
|
|
|
1955
1971
|
-1,
|
|
1956
1972
|
-1
|
|
1957
1973
|
];
|
|
1958
|
-
const DY = [
|
|
1974
|
+
const DY$1 = [
|
|
1959
1975
|
-1,
|
|
1960
1976
|
-1,
|
|
1961
1977
|
0,
|
|
@@ -1968,8 +1984,8 @@ const DY = [
|
|
|
1968
1984
|
function neighbours(img, w, h, x, y) {
|
|
1969
1985
|
const p = [];
|
|
1970
1986
|
for (let k = 0; k < 8; k++) {
|
|
1971
|
-
const nx = x + DX[k];
|
|
1972
|
-
const ny = y + DY[k];
|
|
1987
|
+
const nx = x + DX$1[k];
|
|
1988
|
+
const ny = y + DY$1[k];
|
|
1973
1989
|
p.push(nx >= 0 && nx < w && ny >= 0 && ny < h ? img[ny * w + nx] : 0);
|
|
1974
1990
|
}
|
|
1975
1991
|
return p;
|
|
@@ -2000,7 +2016,7 @@ function pieces(p) {
|
|
|
2000
2016
|
seen[k] = true;
|
|
2001
2017
|
while (stack.length) {
|
|
2002
2018
|
const a = stack.pop();
|
|
2003
|
-
for (let b = 0; b < 8; b++) if (p[b] && !seen[b] && Math.abs(DX[a] - DX[b]) <= 1 && Math.abs(DY[a] - DY[b]) <= 1) {
|
|
2019
|
+
for (let b = 0; b < 8; b++) if (p[b] && !seen[b] && Math.abs(DX$1[a] - DX$1[b]) <= 1 && Math.abs(DY$1[a] - DY$1[b]) <= 1) {
|
|
2004
2020
|
seen[b] = true;
|
|
2005
2021
|
stack.push(b);
|
|
2006
2022
|
}
|
|
@@ -2039,7 +2055,7 @@ function thin(ink, w, h) {
|
|
|
2039
2055
|
img[(y + 1) * W + x + 1] = 1;
|
|
2040
2056
|
live.push((y + 1) * W + x + 1);
|
|
2041
2057
|
}
|
|
2042
|
-
const off = DX.map((dx, k) => DY[k] * W + dx);
|
|
2058
|
+
const off = DX$1.map((dx, k) => DY$1[k] * W + dx);
|
|
2043
2059
|
const mask = (i) => {
|
|
2044
2060
|
let m = 0;
|
|
2045
2061
|
for (let k = 0; k < 8; k++) if (img[i + off[k]]) m |= 1 << k;
|
|
@@ -2077,13 +2093,13 @@ function thin(ink, w, h) {
|
|
|
2077
2093
|
for (let y = 0; y < h; y++) for (let x = 0; x < w; x++) out[y * w + x] = img[(y + 1) * W + x + 1];
|
|
2078
2094
|
return out;
|
|
2079
2095
|
}
|
|
2080
|
-
const degree = (img, w, h, i) => neighbours(img, w, h, i % w, i / w | 0).reduce((s, v) => s + v, 0);
|
|
2096
|
+
const degree$1 = (img, w, h, i) => neighbours(img, w, h, i % w, i / w | 0).reduce((s, v) => s + v, 0);
|
|
2081
2097
|
/** Remove branches from an end to a junction shorter than `min` pixels, until none are left. */
|
|
2082
2098
|
function prune(skel, w, h, min) {
|
|
2083
2099
|
for (let pass = 0; pass < 4; pass++) {
|
|
2084
2100
|
let removed = false;
|
|
2085
2101
|
for (let i = 0; i < skel.length; i++) {
|
|
2086
|
-
if (!skel[i] || degree(skel, w, h, i) !== 1) continue;
|
|
2102
|
+
if (!skel[i] || degree$1(skel, w, h, i) !== 1) continue;
|
|
2087
2103
|
const path = [i];
|
|
2088
2104
|
let prev = -1;
|
|
2089
2105
|
let cur = i;
|
|
@@ -2093,12 +2109,12 @@ function prune(skel, w, h, min) {
|
|
|
2093
2109
|
const y = cur / w | 0;
|
|
2094
2110
|
const next = [];
|
|
2095
2111
|
for (let k = 0; k < 8; k++) {
|
|
2096
|
-
const nx = x + DX[k];
|
|
2097
|
-
const ny = y + DY[k];
|
|
2112
|
+
const nx = x + DX$1[k];
|
|
2113
|
+
const ny = y + DY$1[k];
|
|
2098
2114
|
const j = ny * w + nx;
|
|
2099
2115
|
if (nx >= 0 && nx < w && ny >= 0 && ny < h && skel[j] && j !== prev && !path.includes(j)) next.push(j);
|
|
2100
2116
|
}
|
|
2101
|
-
if (next.length !== 1 || degree(skel, w, h, next[0]) > 2) {
|
|
2117
|
+
if (next.length !== 1 || degree$1(skel, w, h, next[0]) > 2) {
|
|
2102
2118
|
atJunction = next.length >= 1;
|
|
2103
2119
|
break;
|
|
2104
2120
|
}
|
|
@@ -2122,7 +2138,7 @@ function prune(skel, w, h, min) {
|
|
|
2122
2138
|
function walk(skel, w, h) {
|
|
2123
2139
|
const n = w * h;
|
|
2124
2140
|
const isNode = new Uint8Array(n);
|
|
2125
|
-
for (let i = 0; i < n; i++) if (skel[i] && degree(skel, w, h, i) !== 2) isNode[i] = 1;
|
|
2141
|
+
for (let i = 0; i < n; i++) if (skel[i] && degree$1(skel, w, h, i) !== 2) isNode[i] = 1;
|
|
2126
2142
|
const group = new Int32Array(n).fill(-1);
|
|
2127
2143
|
const centres = [];
|
|
2128
2144
|
for (let i = 0; i < n; i++) {
|
|
@@ -2141,10 +2157,10 @@ function walk(skel, w, h) {
|
|
|
2141
2157
|
const x = j % w;
|
|
2142
2158
|
const y = j / w | 0;
|
|
2143
2159
|
for (let k = 0; k < 8; k++) {
|
|
2144
|
-
const nx = x + DX[k];
|
|
2145
|
-
const ny = y + DY[k];
|
|
2160
|
+
const nx = x + DX$1[k];
|
|
2161
|
+
const ny = y + DY$1[k];
|
|
2146
2162
|
const m = ny * w + nx;
|
|
2147
|
-
if (nx >= 0 && nx < w && ny >= 0 && ny < h && isNode[m] && group[m] < 0 && degree(skel, w, h, m) > 2 && degree(skel, w, h, j) > 2) {
|
|
2163
|
+
if (nx >= 0 && nx < w && ny >= 0 && ny < h && isNode[m] && group[m] < 0 && degree$1(skel, w, h, m) > 2 && degree$1(skel, w, h, j) > 2) {
|
|
2148
2164
|
group[m] = g;
|
|
2149
2165
|
stack.push(m);
|
|
2150
2166
|
}
|
|
@@ -2173,8 +2189,8 @@ function walk(skel, w, h) {
|
|
|
2173
2189
|
5,
|
|
2174
2190
|
7
|
|
2175
2191
|
]) {
|
|
2176
|
-
const nx = x + DX[k];
|
|
2177
|
-
const ny = y + DY[k];
|
|
2192
|
+
const nx = x + DX$1[k];
|
|
2193
|
+
const ny = y + DY$1[k];
|
|
2178
2194
|
const j = ny * w + nx;
|
|
2179
2195
|
if (nx >= 0 && nx < w && ny >= 0 && ny < h && skel[j] && j !== from && !used[j] && !(isNode[j] && group[j] === group[from] && group[from] >= 0)) return j;
|
|
2180
2196
|
}
|
|
@@ -2187,8 +2203,8 @@ function walk(skel, w, h) {
|
|
|
2187
2203
|
const x = i % w;
|
|
2188
2204
|
const y = i / w | 0;
|
|
2189
2205
|
for (let k = 0; k < 8; k++) {
|
|
2190
|
-
const nx = x + DX[k];
|
|
2191
|
-
const ny = y + DY[k];
|
|
2206
|
+
const nx = x + DX$1[k];
|
|
2207
|
+
const ny = y + DY$1[k];
|
|
2192
2208
|
const j = ny * w + nx;
|
|
2193
2209
|
if (nx < 0 || nx >= w || ny < 0 || ny >= h || !skel[j] || used[j]) continue;
|
|
2194
2210
|
if (isNode[j]) {
|
|
@@ -2356,6 +2372,111 @@ function refitJunctions(chains, width) {
|
|
|
2356
2372
|
};
|
|
2357
2373
|
});
|
|
2358
2374
|
}
|
|
2375
|
+
/** A chain turns this much (radians) across a corner before it's redrawn as one: 60°. */
|
|
2376
|
+
const CORNER_TURN = Math.PI / 3;
|
|
2377
|
+
/**
|
|
2378
|
+
* Sharp corners drawn sharp. Thinning a stroke that turns sharply cuts the
|
|
2379
|
+
* corner: the skeleton turns inside it, short of the point by up to a few
|
|
2380
|
+
* stroke widths (a flag's swallowtail comes out rounded). Where the chain's
|
|
2381
|
+
* direction just before a point and just after it (measured outside a stroke
|
|
2382
|
+
* width of it, as at junctions) differs by more than CORNER_TURN, the points
|
|
2383
|
+
* within a stroke width are replaced by where the two arms' lines meet, if
|
|
2384
|
+
* the ink runs on toward that point past where the stroke's edge would be.
|
|
2385
|
+
* A rounded corner or a small ring turns as much, but its ink stops half a
|
|
2386
|
+
* width out, so it keeps its shape.
|
|
2387
|
+
*/
|
|
2388
|
+
function refitCorners(c, width, ink, w, h) {
|
|
2389
|
+
const pts = c.pts;
|
|
2390
|
+
const n = pts.length;
|
|
2391
|
+
const cut = Math.max(2, width);
|
|
2392
|
+
const reach = Math.max(6, width);
|
|
2393
|
+
if (n < 4) return c;
|
|
2394
|
+
const s = [0];
|
|
2395
|
+
for (let i = 1; i < n; i++) s.push(s[i - 1] + Math.hypot(pts[i].x - pts[i - 1].x, pts[i].y - pts[i - 1].y));
|
|
2396
|
+
const at = (t) => {
|
|
2397
|
+
let lo = 1;
|
|
2398
|
+
let hi = n - 1;
|
|
2399
|
+
while (lo < hi) {
|
|
2400
|
+
const mid = lo + hi >> 1;
|
|
2401
|
+
if (s[mid] < t) lo = mid + 1;
|
|
2402
|
+
else hi = mid;
|
|
2403
|
+
}
|
|
2404
|
+
const j = lo;
|
|
2405
|
+
const f = s[j] > s[j - 1] ? (t - s[j - 1]) / (s[j] - s[j - 1]) : 0;
|
|
2406
|
+
return {
|
|
2407
|
+
x: pts[j - 1].x + f * (pts[j].x - pts[j - 1].x),
|
|
2408
|
+
y: pts[j - 1].y + f * (pts[j].y - pts[j - 1].y)
|
|
2409
|
+
};
|
|
2410
|
+
};
|
|
2411
|
+
const unit = (a, b) => {
|
|
2412
|
+
const l = Math.hypot(b.x - a.x, b.y - a.y) || 1;
|
|
2413
|
+
return {
|
|
2414
|
+
x: (b.x - a.x) / l,
|
|
2415
|
+
y: (b.y - a.y) / l
|
|
2416
|
+
};
|
|
2417
|
+
};
|
|
2418
|
+
const isInk = (p) => {
|
|
2419
|
+
const x = Math.floor(p.x);
|
|
2420
|
+
const y = Math.floor(p.y);
|
|
2421
|
+
return x >= 0 && y >= 0 && x < w && y < h && ink[y * w + x] === 1;
|
|
2422
|
+
};
|
|
2423
|
+
const found = [];
|
|
2424
|
+
for (let i = 1; i < n - 1; i++) {
|
|
2425
|
+
if (s[i] - cut - reach < 0 || s[i] + cut + reach > s[n - 1]) continue;
|
|
2426
|
+
const pa = at(s[i] - cut);
|
|
2427
|
+
const d1 = unit(at(s[i] - cut - reach), pa);
|
|
2428
|
+
const qa = at(s[i] + cut);
|
|
2429
|
+
const d2 = unit(qa, at(s[i] + cut + reach));
|
|
2430
|
+
const turn = Math.acos(Math.max(-1, Math.min(1, d1.x * d2.x + d1.y * d2.y)));
|
|
2431
|
+
if (turn < CORNER_TURN) continue;
|
|
2432
|
+
const det = d1.x * -d2.y - d1.y * -d2.x;
|
|
2433
|
+
if (Math.abs(det) < 1e-6) continue;
|
|
2434
|
+
const u = ((qa.x - pa.x) * -d2.y - (qa.y - pa.y) * -d2.x) / det;
|
|
2435
|
+
if (u < 0) continue;
|
|
2436
|
+
const apex = {
|
|
2437
|
+
x: pa.x + u * d1.x,
|
|
2438
|
+
y: pa.y + u * d1.y
|
|
2439
|
+
};
|
|
2440
|
+
const off = Math.hypot(apex.x - pts[i].x, apex.y - pts[i].y);
|
|
2441
|
+
if (off < .5 || off > 3 * width || !isInk(apex)) continue;
|
|
2442
|
+
const past = width / 2 + 1.5;
|
|
2443
|
+
if (!isInk({
|
|
2444
|
+
x: pts[i].x + (apex.x - pts[i].x) / off * past,
|
|
2445
|
+
y: pts[i].y + (apex.y - pts[i].y) / off * past
|
|
2446
|
+
})) continue;
|
|
2447
|
+
const last = found[found.length - 1];
|
|
2448
|
+
if (last && s[i] - s[last.i] < 2 * cut) {
|
|
2449
|
+
if (turn > last.turn) found[found.length - 1] = {
|
|
2450
|
+
i,
|
|
2451
|
+
turn,
|
|
2452
|
+
apex
|
|
2453
|
+
};
|
|
2454
|
+
} else found.push({
|
|
2455
|
+
i,
|
|
2456
|
+
turn,
|
|
2457
|
+
apex
|
|
2458
|
+
});
|
|
2459
|
+
}
|
|
2460
|
+
if (!found.length) return c;
|
|
2461
|
+
const out = [];
|
|
2462
|
+
let k = 0;
|
|
2463
|
+
for (let i = 0; i < n; i++) {
|
|
2464
|
+
const f = found[k];
|
|
2465
|
+
if (f && s[i] > s[f.i] - cut) {
|
|
2466
|
+
if (s[i] < s[f.i] + cut) continue;
|
|
2467
|
+
out.push(f.apex);
|
|
2468
|
+
k++;
|
|
2469
|
+
i--;
|
|
2470
|
+
continue;
|
|
2471
|
+
}
|
|
2472
|
+
out.push(pts[i]);
|
|
2473
|
+
}
|
|
2474
|
+
for (; k < found.length; k++) out.push(found[k].apex);
|
|
2475
|
+
return {
|
|
2476
|
+
...c,
|
|
2477
|
+
pts: out
|
|
2478
|
+
};
|
|
2479
|
+
}
|
|
2359
2480
|
/** The direction a chain leaves its start, measured a few pixels along it. */
|
|
2360
2481
|
function leaving(pts) {
|
|
2361
2482
|
const a = pts[0];
|
|
@@ -2497,6 +2618,15 @@ const FLAT_MIN = 6;
|
|
|
2497
2618
|
const GAIN = .5;
|
|
2498
2619
|
/** …and come within this of the bands' own error. */
|
|
2499
2620
|
const BANDS_SLACK = 1.1;
|
|
2621
|
+
/**
|
|
2622
|
+
* …and stay this close to the image (RMS, RGB levels). Soft shading fits to
|
|
2623
|
+
* within noise; a fit this far off is smearing distinct shapes together (a
|
|
2624
|
+
* face and the hair beside it on a painted poster, whose palette is far off
|
|
2625
|
+
* both, so the bands' error is no guide).
|
|
2626
|
+
*/
|
|
2627
|
+
const MAX_RMS = 20;
|
|
2628
|
+
/** A band with at least this many sampled pixels must be drawn no worse by the gradient than by its own colour. */
|
|
2629
|
+
const BAND_MIN = 16;
|
|
2500
2630
|
/** A radial fit replaces the linear one only when this much closer. */
|
|
2501
2631
|
const RADIAL_GAIN = .8;
|
|
2502
2632
|
/** Pixels a shade is fitted on, at most. */
|
|
@@ -2570,12 +2700,14 @@ function shadings(rgba, w, h, comp, regions, colorOf) {
|
|
|
2570
2700
|
const ys = [];
|
|
2571
2701
|
const cs = [];
|
|
2572
2702
|
const bands = [];
|
|
2703
|
+
const ids = [];
|
|
2573
2704
|
for (let k = 0; k < all.length; k += stride) {
|
|
2574
2705
|
const i = all[k];
|
|
2575
2706
|
xs.push(i % w + .5);
|
|
2576
2707
|
ys.push(Math.floor(i / w) + .5);
|
|
2577
2708
|
cs.push(rgb(i));
|
|
2578
2709
|
bands.push(colors[comp[i]]);
|
|
2710
|
+
ids.push(comp[i]);
|
|
2579
2711
|
}
|
|
2580
2712
|
const g = fitShading(xs, ys, cs);
|
|
2581
2713
|
if (!g) continue;
|
|
@@ -2588,7 +2720,8 @@ function shadings(rgba, w, h, comp, regions, colorOf) {
|
|
|
2588
2720
|
const rms = (f) => Math.sqrt(cs.reduce((s, c, j) => s + d3(c, f(j)) ** 2, 0) / n);
|
|
2589
2721
|
const flat = rms(() => mean);
|
|
2590
2722
|
const banded = rms((j) => bands[j]);
|
|
2591
|
-
if (flat < FLAT_MIN || g.rms > GAIN * flat || g.rms > BANDS_SLACK * banded) continue;
|
|
2723
|
+
if (flat < FLAT_MIN || g.rms > MAX_RMS || g.rms > GAIN * flat || g.rms > BANDS_SLACK * banded) continue;
|
|
2724
|
+
if (worseOnABand(g.gradient, xs, ys, cs, bands, ids)) continue;
|
|
2592
2725
|
out.push({
|
|
2593
2726
|
comps: [...groups.get(r)],
|
|
2594
2727
|
gradient: g.gradient,
|
|
@@ -2656,6 +2789,28 @@ function shadings(rgba, w, h, comp, regions, colorOf) {
|
|
|
2656
2789
|
}
|
|
2657
2790
|
/** A gradient's colour at a point. */
|
|
2658
2791
|
const colorAt = (g, x, y) => ramp(g)(x, y);
|
|
2792
|
+
/**
|
|
2793
|
+
* Whether the gradient draws any one band of the shade worse than the band's
|
|
2794
|
+
* own flat colour did: a gradient that suits the whole on average can still
|
|
2795
|
+
* paint one member (a shirt's fold shadow, a strand of hair) the wrong colour.
|
|
2796
|
+
*/
|
|
2797
|
+
function worseOnABand(g, xs, ys, cs, bands, ids) {
|
|
2798
|
+
const at = ramp(g);
|
|
2799
|
+
const err = /* @__PURE__ */ new Map();
|
|
2800
|
+
cs.forEach((c, j) => {
|
|
2801
|
+
const e = err.get(ids[j]) ?? [
|
|
2802
|
+
0,
|
|
2803
|
+
0,
|
|
2804
|
+
0
|
|
2805
|
+
];
|
|
2806
|
+
e[0] += dist2(c, at(xs[j], ys[j]));
|
|
2807
|
+
e[1] += dist2(c, bands[j]);
|
|
2808
|
+
e[2]++;
|
|
2809
|
+
err.set(ids[j], e);
|
|
2810
|
+
});
|
|
2811
|
+
for (const [grad, band, n] of err.values()) if (n >= BAND_MIN && grad > BANDS_SLACK ** 2 * band) return true;
|
|
2812
|
+
return false;
|
|
2813
|
+
}
|
|
2659
2814
|
/** A gradient as a function of the point, its stops parsed once. */
|
|
2660
2815
|
function ramp(g) {
|
|
2661
2816
|
const offsets = g.stops.map((s) => s.offset);
|
|
@@ -2930,6 +3085,290 @@ const clamp = (v) => Math.max(0, Math.min(255, v));
|
|
|
2930
3085
|
const dist2 = (a, b) => (a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2 + (a[2] - b[2]) ** 2;
|
|
2931
3086
|
const round$1 = (v, places) => Math.round(v * 10 ** places) / 10 ** places;
|
|
2932
3087
|
//#endregion
|
|
3088
|
+
//#region src/gaps.ts
|
|
3089
|
+
/**
|
|
3090
|
+
* Gap closing (#145): join a line's ends across a small break, before tracing,
|
|
3091
|
+
* so a drawn outline that came out in pieces (a lossy save, a faint stretch of
|
|
3092
|
+
* pen, a stroke that stops just short of the one it meets) is one line again,
|
|
3093
|
+
* and a fill inside it stays inside.
|
|
3094
|
+
*
|
|
3095
|
+
* After Fourey, Tschumperlé and Revoy, "A fast and efficient semi-guided
|
|
3096
|
+
* algorithm for flat coloring line-arts" (VMV 2018, GIMP's "Fill by line art
|
|
3097
|
+
* detection"), on the skeleton rather than the ink's border:
|
|
3098
|
+
*
|
|
3099
|
+
* 1. Thin the ink and prune its spurs. Each skeleton end is a line's end; the
|
|
3100
|
+
* way it points is measured a few widths back along it, and so is the
|
|
3101
|
+
* line's own width there (a local width, not the drawing's: GIMP's lesson).
|
|
3102
|
+
* 2. Two ends that point at each other across a gap join. An end that points
|
|
3103
|
+
* at other ink close by reaches it: the T that stops short.
|
|
3104
|
+
* 3. A join crosses one gap only: along it, the ink of the line it starts
|
|
3105
|
+
* from, then paper, then the ink it reaches, and nothing else. The paper it
|
|
3106
|
+
* crosses is at most GAP_WIDTHS times the line's width and GAP_MAX px, so
|
|
3107
|
+
* a gap that's drawn (a dashed line, a box left open on purpose) stays
|
|
3108
|
+
* open.
|
|
3109
|
+
* 4. The best joins (shortest gap) are painted first, as ink the line's own
|
|
3110
|
+
* width; each end joins once.
|
|
3111
|
+
*
|
|
3112
|
+
* Written for this project (MIT).
|
|
3113
|
+
*/
|
|
3114
|
+
/** The widest gap closed, in the line's own widths. A drawn gap (dashes, an open box) is wider. */
|
|
3115
|
+
const GAP_WIDTHS = 1.6;
|
|
3116
|
+
/** …but never less than this (px): a hairline's breaks are a pixel or two whatever its width. */
|
|
3117
|
+
const GAP_MIN = 2.5;
|
|
3118
|
+
/**
|
|
3119
|
+
* …and never more (px). A break from a lossy save or a faint stretch of pen
|
|
3120
|
+
* is a few pixels whatever the pen; on a heavy line a gap of a width is
|
|
3121
|
+
* plainly drawn (dashes 8 px wide with 8 px between, a chimney stopping 10 px
|
|
3122
|
+
* short of a roof).
|
|
3123
|
+
*/
|
|
3124
|
+
const GAP_MAX = 5.5;
|
|
3125
|
+
/** An end's direction is measured this many of the line's widths back along it (at least END_BACK_MIN px). */
|
|
3126
|
+
const END_BACK = 2.5;
|
|
3127
|
+
const END_BACK_MIN = 4;
|
|
3128
|
+
/** A line shorter than this (px) has no direction to speak of: a dot, a speck. */
|
|
3129
|
+
const END_MIN = 3;
|
|
3130
|
+
/** How straight at the other end a join must point: cos 40°. */
|
|
3131
|
+
const FACING = Math.cos(40 * Math.PI / 180);
|
|
3132
|
+
const DX = [
|
|
3133
|
+
0,
|
|
3134
|
+
1,
|
|
3135
|
+
1,
|
|
3136
|
+
1,
|
|
3137
|
+
0,
|
|
3138
|
+
-1,
|
|
3139
|
+
-1,
|
|
3140
|
+
-1
|
|
3141
|
+
];
|
|
3142
|
+
const DY = [
|
|
3143
|
+
-1,
|
|
3144
|
+
-1,
|
|
3145
|
+
0,
|
|
3146
|
+
1,
|
|
3147
|
+
1,
|
|
3148
|
+
1,
|
|
3149
|
+
0,
|
|
3150
|
+
-1
|
|
3151
|
+
];
|
|
3152
|
+
/**
|
|
3153
|
+
* Close small gaps in `ink` (1 = ink), in place. Returns how many joins were
|
|
3154
|
+
* painted.
|
|
3155
|
+
*/
|
|
3156
|
+
function closeGaps(ink, w, h, o = {}) {
|
|
3157
|
+
const k = Math.max(1, o.scale ?? 1);
|
|
3158
|
+
const skel = o.skel ?? thin(ink, w, h);
|
|
3159
|
+
const width = strokeWidthOf(skel, ink, w);
|
|
3160
|
+
prune(skel, w, h, Math.max(3, Math.round(width)));
|
|
3161
|
+
const ends = endsOf(skel, ink, w, h, width);
|
|
3162
|
+
if (!ends.length) return 0;
|
|
3163
|
+
const reachOf = (e) => Math.min(GAP_MAX * k, Math.max(GAP_MIN * k, GAP_WIDTHS * e.width));
|
|
3164
|
+
const cross = (ax, ay, bx, by) => crossing(ink, o.blocked, w, h, ax, ay, bx, by);
|
|
3165
|
+
const joins = [];
|
|
3166
|
+
let widest = 0;
|
|
3167
|
+
for (const e of ends) widest = Math.max(widest, e.width);
|
|
3168
|
+
const cell = Math.ceil(widest + GAP_MAX * k + 2);
|
|
3169
|
+
const grid = /* @__PURE__ */ new Map();
|
|
3170
|
+
const key = (cx, cy) => cy * 65536 + cx;
|
|
3171
|
+
for (const e of ends) {
|
|
3172
|
+
const k = key(Math.floor(e.x / cell), Math.floor(e.y / cell));
|
|
3173
|
+
const list = grid.get(k);
|
|
3174
|
+
if (list) list.push(e);
|
|
3175
|
+
else grid.set(k, [e]);
|
|
3176
|
+
}
|
|
3177
|
+
for (const a of ends) {
|
|
3178
|
+
const cx = Math.floor(a.x / cell);
|
|
3179
|
+
const cy = Math.floor(a.y / cell);
|
|
3180
|
+
for (let gy = cy - 1; gy <= cy + 1; gy++) for (let gx = cx - 1; gx <= cx + 1; gx++) for (const b of grid.get(key(gx, gy)) ?? []) {
|
|
3181
|
+
if (b.i <= a.i) continue;
|
|
3182
|
+
const vx = b.x - a.x;
|
|
3183
|
+
const vy = b.y - a.y;
|
|
3184
|
+
const d = Math.hypot(vx, vy);
|
|
3185
|
+
const reach = Math.min(reachOf(a), reachOf(b));
|
|
3186
|
+
if (d === 0 || d > (a.width + b.width) / 2 + reach + 2) continue;
|
|
3187
|
+
if ((a.dx * vx + a.dy * vy) / d < FACING || -(b.dx * vx + b.dy * vy) / d < FACING) continue;
|
|
3188
|
+
const gap = cross(a.x, a.y, b.x, b.y);
|
|
3189
|
+
if (gap <= 0 || gap > reach) continue;
|
|
3190
|
+
joins.push({
|
|
3191
|
+
a,
|
|
3192
|
+
b,
|
|
3193
|
+
tx: b.x,
|
|
3194
|
+
ty: b.y,
|
|
3195
|
+
gap,
|
|
3196
|
+
width: Math.min(a.width, b.width)
|
|
3197
|
+
});
|
|
3198
|
+
}
|
|
3199
|
+
}
|
|
3200
|
+
for (const a of ends) {
|
|
3201
|
+
const hit = reach(ink, o.blocked, w, h, a, reachOf(a));
|
|
3202
|
+
if (!hit) continue;
|
|
3203
|
+
joins.push({
|
|
3204
|
+
a,
|
|
3205
|
+
b: null,
|
|
3206
|
+
tx: hit.x,
|
|
3207
|
+
ty: hit.y,
|
|
3208
|
+
gap: hit.gap * 1.25,
|
|
3209
|
+
width: a.width
|
|
3210
|
+
});
|
|
3211
|
+
}
|
|
3212
|
+
joins.sort((p, q) => p.gap - q.gap);
|
|
3213
|
+
const used = /* @__PURE__ */ new Set();
|
|
3214
|
+
let painted = 0;
|
|
3215
|
+
for (const j of joins) {
|
|
3216
|
+
if (used.has(j.a.i) || j.b && used.has(j.b.i)) continue;
|
|
3217
|
+
if (cross(j.a.x, j.a.y, j.tx, j.ty) <= 0) continue;
|
|
3218
|
+
used.add(j.a.i);
|
|
3219
|
+
if (j.b) used.add(j.b.i);
|
|
3220
|
+
paint(ink, w, h, j.a.x, j.a.y, j.tx, j.ty, j.width / 2);
|
|
3221
|
+
painted++;
|
|
3222
|
+
}
|
|
3223
|
+
return painted;
|
|
3224
|
+
}
|
|
3225
|
+
/** Each skeleton end of a line long enough to have a direction: where it is, which way it points, and the line's width there. */
|
|
3226
|
+
function endsOf(skel, ink, w, h, width) {
|
|
3227
|
+
const ends = [];
|
|
3228
|
+
const back = Math.max(END_BACK_MIN, Math.round(END_BACK * width));
|
|
3229
|
+
for (let i = 0; i < skel.length; i++) {
|
|
3230
|
+
if (!skel[i]) continue;
|
|
3231
|
+
const x = i % w;
|
|
3232
|
+
const y = i / w | 0;
|
|
3233
|
+
if (degree(skel, w, h, x, y) !== 1) continue;
|
|
3234
|
+
const path = [i];
|
|
3235
|
+
let prev = -1;
|
|
3236
|
+
let cur = i;
|
|
3237
|
+
while (path.length <= back) {
|
|
3238
|
+
const cx = cur % w;
|
|
3239
|
+
const cy = cur / w | 0;
|
|
3240
|
+
const next = [];
|
|
3241
|
+
for (let k = 0; k < 8; k++) {
|
|
3242
|
+
const nx = cx + DX[k];
|
|
3243
|
+
const ny = cy + DY[k];
|
|
3244
|
+
const j = ny * w + nx;
|
|
3245
|
+
if (nx < 0 || nx >= w || ny < 0 || ny >= h || !skel[j] || j === prev || path.includes(j)) continue;
|
|
3246
|
+
next.push(j);
|
|
3247
|
+
}
|
|
3248
|
+
if (next.length === 2 && touch(next[0], next[1], w)) {
|
|
3249
|
+
const diag = (j) => j % w !== cx && (j / w | 0) !== cy;
|
|
3250
|
+
const [on, by] = diag(next[0]) ? [next[0], next[1]] : [next[1], next[0]];
|
|
3251
|
+
path.push(by);
|
|
3252
|
+
next.length = 0;
|
|
3253
|
+
next.push(on);
|
|
3254
|
+
}
|
|
3255
|
+
if (next.length !== 1) break;
|
|
3256
|
+
prev = cur;
|
|
3257
|
+
cur = next[0];
|
|
3258
|
+
path.push(cur);
|
|
3259
|
+
}
|
|
3260
|
+
if (path.length < END_MIN) continue;
|
|
3261
|
+
const bx = cur % w;
|
|
3262
|
+
const by = cur / w | 0;
|
|
3263
|
+
const l = Math.hypot(x - bx, y - by);
|
|
3264
|
+
if (!l) continue;
|
|
3265
|
+
const mid = path[Math.floor(path.length / 2)];
|
|
3266
|
+
ends.push({
|
|
3267
|
+
i,
|
|
3268
|
+
x: x + .5,
|
|
3269
|
+
y: y + .5,
|
|
3270
|
+
dx: (x - bx) / l,
|
|
3271
|
+
dy: (y - by) / l,
|
|
3272
|
+
width: Math.max(1, 2 * radiusAt(ink, w, h, mid % w, mid / w | 0, 2 * width + 2))
|
|
3273
|
+
});
|
|
3274
|
+
}
|
|
3275
|
+
return ends;
|
|
3276
|
+
}
|
|
3277
|
+
const touch = (a, b, w) => Math.abs(a % w - b % w) <= 1 && Math.abs((a / w | 0) - (b / w | 0)) <= 1;
|
|
3278
|
+
function degree(img, w, h, x, y) {
|
|
3279
|
+
let d = 0;
|
|
3280
|
+
for (let k = 0; k < 8; k++) {
|
|
3281
|
+
const nx = x + DX[k];
|
|
3282
|
+
const ny = y + DY[k];
|
|
3283
|
+
if (nx >= 0 && nx < w && ny >= 0 && ny < h && img[ny * w + nx]) d++;
|
|
3284
|
+
}
|
|
3285
|
+
return d;
|
|
3286
|
+
}
|
|
3287
|
+
/** Distance from pixel (x, y)'s centre to the nearest paper, up to `max`: half the line's width there. */
|
|
3288
|
+
function radiusAt(ink, w, h, x, y, max) {
|
|
3289
|
+
let best = max;
|
|
3290
|
+
const r = Math.ceil(max);
|
|
3291
|
+
for (let dy = -r; dy <= r; dy++) for (let dx = -r; dx <= r; dx++) {
|
|
3292
|
+
const nx = x + dx;
|
|
3293
|
+
const ny = y + dy;
|
|
3294
|
+
if (nx < 0 || nx >= w || ny < 0 || ny >= h || ink[ny * w + nx]) continue;
|
|
3295
|
+
const d = Math.hypot(Math.max(0, Math.abs(dx) - .5), Math.max(0, Math.abs(dy) - .5));
|
|
3296
|
+
if (d < best) best = d;
|
|
3297
|
+
}
|
|
3298
|
+
return best;
|
|
3299
|
+
}
|
|
3300
|
+
/**
|
|
3301
|
+
* Along the segment from (ax, ay) to (bx, by), sampled every half pixel: the
|
|
3302
|
+
* length of paper crossed, when it's ink, then one stretch of paper, then
|
|
3303
|
+
* ink; 0 when it never leaves the ink; -1 when it crosses more than one gap
|
|
3304
|
+
* (another line in between) or ends on paper.
|
|
3305
|
+
*/
|
|
3306
|
+
function crossing(ink, blocked, w, h, ax, ay, bx, by) {
|
|
3307
|
+
const d = Math.hypot(bx - ax, by - ay);
|
|
3308
|
+
const steps = Math.max(1, Math.ceil(d * 2));
|
|
3309
|
+
let state = 0;
|
|
3310
|
+
let out = 0;
|
|
3311
|
+
let back = 0;
|
|
3312
|
+
for (let s = 0; s <= steps; s++) {
|
|
3313
|
+
const t = s / steps;
|
|
3314
|
+
const px = Math.floor(ax + (bx - ax) * t);
|
|
3315
|
+
const py = Math.floor(ay + (by - ay) * t);
|
|
3316
|
+
if (px < 0 || px >= w || py < 0 || py >= h || blocked?.[py * w + px]) return -1;
|
|
3317
|
+
const on = ink[py * w + px] === 1;
|
|
3318
|
+
if (state === 0 && !on) {
|
|
3319
|
+
state = 1;
|
|
3320
|
+
out = s;
|
|
3321
|
+
} else if (state === 1 && on) {
|
|
3322
|
+
state = 2;
|
|
3323
|
+
back = s;
|
|
3324
|
+
} else if (state === 2 && !on) return -1;
|
|
3325
|
+
}
|
|
3326
|
+
if (state === 1) return -1;
|
|
3327
|
+
return state === 0 ? 0 : (back - out) * d / steps;
|
|
3328
|
+
}
|
|
3329
|
+
/** Straight on from end `e`: out of its own ink, across paper no wider than `max`, into other ink. */
|
|
3330
|
+
function reach(ink, blocked, w, h, e, max) {
|
|
3331
|
+
const step = .5;
|
|
3332
|
+
let left = false;
|
|
3333
|
+
let gap = 0;
|
|
3334
|
+
for (let t = step; t <= e.width + max + 1; t += step) {
|
|
3335
|
+
const x = e.x + e.dx * t;
|
|
3336
|
+
const y = e.y + e.dy * t;
|
|
3337
|
+
const px = Math.floor(x);
|
|
3338
|
+
const py = Math.floor(y);
|
|
3339
|
+
if (px < 0 || px >= w || py < 0 || py >= h || blocked?.[py * w + px]) return null;
|
|
3340
|
+
const on = ink[py * w + px] === 1;
|
|
3341
|
+
if (!left) {
|
|
3342
|
+
if (!on) left = true;
|
|
3343
|
+
else continue;
|
|
3344
|
+
}
|
|
3345
|
+
if (!on) {
|
|
3346
|
+
gap += step;
|
|
3347
|
+
if (gap > max) return null;
|
|
3348
|
+
continue;
|
|
3349
|
+
}
|
|
3350
|
+
return {
|
|
3351
|
+
x: x + e.dx * Math.min(1, e.width / 2),
|
|
3352
|
+
y: y + e.dy * Math.min(1, e.width / 2),
|
|
3353
|
+
gap
|
|
3354
|
+
};
|
|
3355
|
+
}
|
|
3356
|
+
return null;
|
|
3357
|
+
}
|
|
3358
|
+
/** Ink along the segment, `r` px either side of it (a round-capped stroke). */
|
|
3359
|
+
function paint(ink, w, h, ax, ay, bx, by, r) {
|
|
3360
|
+
const rr = Math.max(.75, r);
|
|
3361
|
+
const vx = bx - ax;
|
|
3362
|
+
const vy = by - ay;
|
|
3363
|
+
const len2 = vx * vx + vy * vy || 1;
|
|
3364
|
+
for (let y = Math.max(0, Math.floor(Math.min(ay, by) - rr)); y <= Math.min(h - 1, Math.ceil(Math.max(ay, by) + rr)); y++) for (let x = Math.max(0, Math.floor(Math.min(ax, bx) - rr)); x <= Math.min(w - 1, Math.ceil(Math.max(ax, bx) + rr)); x++) {
|
|
3365
|
+
const px = x + .5;
|
|
3366
|
+
const py = y + .5;
|
|
3367
|
+
const t = Math.max(0, Math.min(1, ((px - ax) * vx + (py - ay) * vy) / len2));
|
|
3368
|
+
if ((px - ax - vx * t) ** 2 + (py - ay - vy * t) ** 2 <= rr * rr) ink[y * w + x] = 1;
|
|
3369
|
+
}
|
|
3370
|
+
}
|
|
3371
|
+
//#endregion
|
|
2933
3372
|
//#region src/inlay.ts
|
|
2934
3373
|
/**
|
|
2935
3374
|
* Inlay, our shared-edge tracer: every preset, from logos to photos.
|
|
@@ -3008,7 +3447,8 @@ function trace(rgba, w, h, o) {
|
|
|
3008
3447
|
if (!o.pixels && !(o.mode === "palette" && o.photo)) joinDiagonals(labels, w, h, rgba, drawn);
|
|
3009
3448
|
const labs = o.mode === "palette" && !o.photo ? colors.map((c) => c ? oklab(hexToRgb(c)) : null) : void 0;
|
|
3010
3449
|
mergeSpecks(labels, w, h, Math.max(0, Math.round(o.speckle)) ** 2, labs);
|
|
3011
|
-
if (
|
|
3450
|
+
if (o.mode === "binary" && o.closeGaps && closeInkGaps(labels, w, h, o.scale)) mergeSpecks(labels, w, h, Math.max(0, Math.round(o.speckle)) ** 2);
|
|
3451
|
+
if (labs && !o.pixels) mergeSlivers(labels, w, h, labs, o.lines);
|
|
3012
3452
|
const lines = o.mode === "palette" && o.strokes && labs && !o.pixels ? strokesOf(rgba, labels, w, h, colors, labs, drawn, o) : [];
|
|
3013
3453
|
let { comp, compLabel, compArea } = components(labels, w, h, (l) => drawn[l]);
|
|
3014
3454
|
const gradientOf = /* @__PURE__ */ new Map();
|
|
@@ -3623,6 +4063,14 @@ function labelPalette(rgba, w, h, palette, flat) {
|
|
|
3623
4063
|
drawn: [...palette.map(() => true), false]
|
|
3624
4064
|
};
|
|
3625
4065
|
}
|
|
4066
|
+
/** Line art's ink is label 0: close its gaps (gaps.ts), the joins becoming ink. Whether anything was joined. */
|
|
4067
|
+
function closeInkGaps(labels, w, h, scale) {
|
|
4068
|
+
const ink = new Uint8Array(w * h);
|
|
4069
|
+
for (let i = 0; i < ink.length; i++) ink[i] = labels[i] === 0 ? 1 : 0;
|
|
4070
|
+
if (!closeGaps(ink, w, h, { scale })) return false;
|
|
4071
|
+
for (let i = 0; i < ink.length; i++) if (ink[i]) labels[i] = 0;
|
|
4072
|
+
return true;
|
|
4073
|
+
}
|
|
3626
4074
|
function labelBinary(rgba, w, h, threshold) {
|
|
3627
4075
|
const n = w * h;
|
|
3628
4076
|
const lum = new Uint8Array(n);
|
|
@@ -3944,21 +4392,35 @@ function mergeSpecksOnce(labels, w, h, minArea, labs) {
|
|
|
3944
4392
|
const SLIVER_WIDTH = 2;
|
|
3945
4393
|
/** A sliver joins a neighbour only this close in colour (OKLab): two shades of one ink (0.08–0.11 apart), not a grey hairline on white (#d4d4d4 is 0.13 from it). */
|
|
3946
4394
|
const SLIVER_DISTANCE = .12;
|
|
4395
|
+
/** A painted line's piece spans this many px (bounding-box diagonal) before it is kept from folding into a shape. */
|
|
4396
|
+
const LINE_PIECE_SPAN = 16;
|
|
3947
4397
|
/**
|
|
3948
4398
|
* Fold slivers into a neighbour of nearly the same colour. JPEG and WebP
|
|
3949
4399
|
* smear a thin stroke into two shades of its ink (a 3 px letter stem came
|
|
3950
4400
|
* out dark at its edges and lighter down the middle), and a one-pixel strip
|
|
3951
4401
|
* of one shade inside the other fitted as a spike shooting out of the
|
|
3952
4402
|
* letter. A hairline of a colour of its own has no neighbour that close, so
|
|
3953
|
-
* it stays. Within a pass a sliver waits if a neighbour has already moved,
|
|
3954
|
-
*
|
|
4403
|
+
* it stays. Within a pass a sliver waits if a neighbour has already moved, so
|
|
4404
|
+
* two side by side don't just swap colours.
|
|
4405
|
+
*
|
|
4406
|
+
* A region mostly on a painted line marked in `lines` folds only into another
|
|
4407
|
+
* such region, sliver or not: a dark outline between two dark shapes is
|
|
4408
|
+
* nearly their colour, and folding it in joined them, but one line labels as
|
|
4409
|
+
* several dark shades along its length, and keeping each piece apart cost a
|
|
4410
|
+
* painted film poster a third of its shapes. A line piece shorter than
|
|
4411
|
+
* LINE_PIECE_SPAN is too short to part anything, and folds as any sliver.
|
|
3955
4412
|
*/
|
|
3956
|
-
function mergeSlivers(labels, w, h, labs) {
|
|
4413
|
+
function mergeSlivers(labels, w, h, labs, lines) {
|
|
3957
4414
|
for (let pass = 0; pass < 4; pass++) {
|
|
3958
4415
|
const { comp, compArea } = components(labels, w, h, () => true);
|
|
3959
4416
|
const m = compArea.length;
|
|
3960
4417
|
const perimeter = new Int32Array(m);
|
|
3961
4418
|
const label = new Int32Array(m);
|
|
4419
|
+
const onLine = new Int32Array(m);
|
|
4420
|
+
const x0 = new Int32Array(m).fill(w);
|
|
4421
|
+
const x1 = new Int32Array(m).fill(-1);
|
|
4422
|
+
const y0 = new Int32Array(m).fill(h);
|
|
4423
|
+
const y1 = new Int32Array(m).fill(-1);
|
|
3962
4424
|
const around = /* @__PURE__ */ new Map();
|
|
3963
4425
|
const touch = (a, b) => {
|
|
3964
4426
|
const id = comp[a];
|
|
@@ -3969,23 +4431,40 @@ function mergeSlivers(labels, w, h, labs) {
|
|
|
3969
4431
|
};
|
|
3970
4432
|
for (let y = 0; y < h; y++) for (let x = 0; x < w; x++) {
|
|
3971
4433
|
const i = y * w + x;
|
|
3972
|
-
|
|
4434
|
+
const c = comp[i];
|
|
4435
|
+
label[c] = labels[i];
|
|
4436
|
+
if (lines?.[i]) {
|
|
4437
|
+
onLine[c]++;
|
|
4438
|
+
if (x < x0[c]) x0[c] = x;
|
|
4439
|
+
if (x > x1[c]) x1[c] = x;
|
|
4440
|
+
if (y < y0[c]) y0[c] = y;
|
|
4441
|
+
if (y > y1[c]) y1[c] = y;
|
|
4442
|
+
}
|
|
3973
4443
|
if (x < w - 1 && comp[i + 1] !== comp[i]) touch(i, i + 1), touch(i + 1, i);
|
|
3974
4444
|
if (y < h - 1 && comp[i + w] !== comp[i]) touch(i, i + w), touch(i + w, i);
|
|
3975
4445
|
}
|
|
3976
4446
|
const to = new Int32Array(m).fill(-1);
|
|
3977
4447
|
let changed = false;
|
|
4448
|
+
const onALine = (id) => 2 * onLine[id] > compArea[id];
|
|
3978
4449
|
for (const [id, near] of [...around].sort((a, b) => compArea[a[0]] - compArea[b[0]])) {
|
|
3979
|
-
|
|
4450
|
+
const sliver = 2 * compArea[id] / perimeter[id] < SLIVER_WIDTH;
|
|
4451
|
+
const line = onALine(id);
|
|
4452
|
+
if (!sliver && !line) continue;
|
|
3980
4453
|
const mine = labs[label[id]];
|
|
3981
4454
|
if (!mine || [...near].some((n) => to[n] >= 0)) continue;
|
|
3982
|
-
|
|
3983
|
-
|
|
3984
|
-
|
|
3985
|
-
const
|
|
3986
|
-
|
|
3987
|
-
|
|
3988
|
-
|
|
4455
|
+
const nearest = (ok) => {
|
|
4456
|
+
let best = -1;
|
|
4457
|
+
let bd = SLIVER_DISTANCE;
|
|
4458
|
+
for (const n of near) {
|
|
4459
|
+
const lab = labs[label[n]];
|
|
4460
|
+
const d = lab && ok(n) ? Math.hypot(lab[0] - mine[0], lab[1] - mine[1], lab[2] - mine[2]) : Infinity;
|
|
4461
|
+
if (d < bd) bd = d, best = label[n];
|
|
4462
|
+
}
|
|
4463
|
+
return best;
|
|
4464
|
+
};
|
|
4465
|
+
const sides = new Set([...near].filter((n) => !onALine(n)).map((n) => label[n]));
|
|
4466
|
+
let best = nearest(line ? (n) => onALine(n) && !sides.has(label[n]) : () => true);
|
|
4467
|
+
if (best < 0 && line && sliver && Math.hypot(x1[id] - x0[id], y1[id] - y0[id]) < LINE_PIECE_SPAN) best = nearest(() => true);
|
|
3989
4468
|
if (best < 0) continue;
|
|
3990
4469
|
to[id] = best;
|
|
3991
4470
|
changed = true;
|
|
@@ -4150,8 +4629,16 @@ function strokesOf(rgba, labels, w, h, colors, labs, drawn, o) {
|
|
|
4150
4629
|
}
|
|
4151
4630
|
if (kept < LINE_SHARE * all) continue;
|
|
4152
4631
|
for (let i = 0; i < n; i++) if (ink[i] && !fits(pieces.comp[i])) ink[i] = skel[i] = 0;
|
|
4153
|
-
const { subs, measured } = skeletonStrokes(ink, w, h, o,
|
|
4632
|
+
const { subs, measured } = skeletonStrokes(ink, w, h, o, o.closeGaps && closeGaps(ink, w, h, {
|
|
4633
|
+
scale: k,
|
|
4634
|
+
blocked: line,
|
|
4635
|
+
skel
|
|
4636
|
+
}) > 0 ? void 0 : skel);
|
|
4154
4637
|
if (!subs.length) continue;
|
|
4638
|
+
for (const i of beyondStroke(ink, w, h, subs, measured)) {
|
|
4639
|
+
ink[i] = 0;
|
|
4640
|
+
labels[i] = l;
|
|
4641
|
+
}
|
|
4155
4642
|
out.push({
|
|
4156
4643
|
d: serializePath(subs),
|
|
4157
4644
|
fill: colors[l],
|
|
@@ -4199,6 +4686,77 @@ function strokesOf(rgba, labels, w, h, colors, labs, drawn, o) {
|
|
|
4199
4686
|
}
|
|
4200
4687
|
return out;
|
|
4201
4688
|
}
|
|
4689
|
+
/** Ink past a stroke's edge (px) before it counts as ink the stroke misses: anti-aliasing and fitting stay within it. */
|
|
4690
|
+
const BEYOND = .75;
|
|
4691
|
+
/** A piece of missed ink is kept only if this many pixels or more; less is the edge's noise. */
|
|
4692
|
+
const BEYOND_AREA = 6;
|
|
4693
|
+
/**
|
|
4694
|
+
* The ink a stroke of `width` along `subs` doesn't cover at the stroke's open
|
|
4695
|
+
* ends, in pieces of at least BEYOND_AREA pixels, each with the ink within
|
|
4696
|
+
* half the width of it so it comes out solid: a dot drawn on a line's end.
|
|
4697
|
+
* Missed ink elsewhere (the tip of a sharp corner, which a centreline turns
|
|
4698
|
+
* inside) is left to the line: as a shape it reads as a blob, not a point.
|
|
4699
|
+
*/
|
|
4700
|
+
function beyondStroke(ink, w, h, subs, width) {
|
|
4701
|
+
const n = w * h;
|
|
4702
|
+
const covered = new Uint8Array(n);
|
|
4703
|
+
const r = width / 2 + BEYOND;
|
|
4704
|
+
const stamp = (p) => {
|
|
4705
|
+
for (let y = Math.max(0, Math.floor(p.y - r)); y <= Math.min(h - 1, Math.ceil(p.y + r)); y++) for (let x = Math.max(0, Math.floor(p.x - r)); x <= Math.min(w - 1, Math.ceil(p.x + r)); x++) if ((x + .5 - p.x) ** 2 + (y + .5 - p.y) ** 2 <= r * r) covered[y * w + x] = 1;
|
|
4706
|
+
};
|
|
4707
|
+
for (const sp of subs) {
|
|
4708
|
+
const m = sp.nodes.length;
|
|
4709
|
+
if (m === 1) stamp(sp.nodes[0]);
|
|
4710
|
+
for (let i = 0; i < (sp.closed ? m : m - 1); i++) {
|
|
4711
|
+
const a = sp.nodes[i];
|
|
4712
|
+
const b = sp.nodes[(i + 1) % m];
|
|
4713
|
+
const c = [
|
|
4714
|
+
a,
|
|
4715
|
+
a.out ?? a,
|
|
4716
|
+
b.in ?? b,
|
|
4717
|
+
b
|
|
4718
|
+
];
|
|
4719
|
+
const steps = Math.max(2, Math.ceil(2 * (Math.hypot(c[1].x - c[0].x, c[1].y - c[0].y) + Math.hypot(c[2].x - c[1].x, c[2].y - c[1].y) + Math.hypot(c[3].x - c[2].x, c[3].y - c[2].y))));
|
|
4720
|
+
for (let k = 0; k <= steps; k++) stamp(pointAt(c, k / steps));
|
|
4721
|
+
}
|
|
4722
|
+
}
|
|
4723
|
+
const missed = new Int32Array(n);
|
|
4724
|
+
for (let i = 0; i < n; i++) missed[i] = ink[i] && !covered[i] ? 1 : 0;
|
|
4725
|
+
const pieces = components(missed, w, h, (v) => v === 1);
|
|
4726
|
+
const ends = subs.filter((sp) => !sp.closed && sp.nodes.length > 1).flatMap((sp) => [sp.nodes[0], sp.nodes[sp.nodes.length - 1]]);
|
|
4727
|
+
const atEnd = new Uint8Array(pieces.compArea.length);
|
|
4728
|
+
const near = width + 1;
|
|
4729
|
+
for (let i = 0; i < n; i++) if (missed[i] && !atEnd[pieces.comp[i]]) {
|
|
4730
|
+
const x = i % w + .5;
|
|
4731
|
+
const y = Math.floor(i / w) + .5;
|
|
4732
|
+
if (ends.some((e) => (e.x - x) ** 2 + (e.y - y) ** 2 <= near * near)) atEnd[pieces.comp[i]] = 1;
|
|
4733
|
+
}
|
|
4734
|
+
const out = [];
|
|
4735
|
+
const taken = new Uint8Array(n);
|
|
4736
|
+
for (let i = 0; i < n; i++) if (missed[i] && atEnd[pieces.comp[i]] && pieces.compArea[pieces.comp[i]] >= BEYOND_AREA) {
|
|
4737
|
+
taken[i] = 1;
|
|
4738
|
+
out.push(i);
|
|
4739
|
+
}
|
|
4740
|
+
let front = out.slice();
|
|
4741
|
+
for (let step = 0; step < Math.round(width / 2) && front.length; step++) {
|
|
4742
|
+
const next = [];
|
|
4743
|
+
for (const i of front) {
|
|
4744
|
+
const x = i % w;
|
|
4745
|
+
for (const j of [
|
|
4746
|
+
x > 0 ? i - 1 : -1,
|
|
4747
|
+
x < w - 1 ? i + 1 : -1,
|
|
4748
|
+
i - w,
|
|
4749
|
+
i + w
|
|
4750
|
+
]) if (j >= 0 && j < n && ink[j] && !taken[j]) {
|
|
4751
|
+
taken[j] = 1;
|
|
4752
|
+
next.push(j);
|
|
4753
|
+
out.push(j);
|
|
4754
|
+
}
|
|
4755
|
+
}
|
|
4756
|
+
front = next;
|
|
4757
|
+
}
|
|
4758
|
+
return out;
|
|
4759
|
+
}
|
|
4202
4760
|
/** The median of `values` weighted by `weights`, over the indices `ok` takes; 0 if none. */
|
|
4203
4761
|
function weightedMedian(values, weights, ok) {
|
|
4204
4762
|
const order = [];
|
|
@@ -4511,6 +5069,192 @@ function toSubpath(cubics) {
|
|
|
4511
5069
|
};
|
|
4512
5070
|
}
|
|
4513
5071
|
//#endregion
|
|
5072
|
+
//#region src/smooth.ts
|
|
5073
|
+
/**
|
|
5074
|
+
* Edge-preserving smoothing for painted art, before its palette is picked.
|
|
5075
|
+
*
|
|
5076
|
+
* A painted poster's skin shadow is a soft ramp with brush and JPEG noise on
|
|
5077
|
+
* it. Labelled pixel by pixel, the noise flips each pixel between the two
|
|
5078
|
+
* nearest palette colours, and the shadow traces as ragged patches. A
|
|
5079
|
+
* bilateral filter averages each pixel with neighbours of a similar colour
|
|
5080
|
+
* only, so the noise inside a shadow evens out and the edges between colours
|
|
5081
|
+
* (an arm against the dark coat, a line on a face) stay where they are.
|
|
5082
|
+
*
|
|
5083
|
+
* On a painted film poster traced at 2× in Illustration, this turned the
|
|
5084
|
+
* blotchy arm and neck shadows into whole shapes, kept the face lines, and cut
|
|
5085
|
+
* the file from 3.3 MB to 2.1 MB; the filter took 4 s of the trace. Stronger
|
|
5086
|
+
* (radius 6, three passes at sigma 25) began to wash out lines in the hair.
|
|
5087
|
+
*/
|
|
5088
|
+
/** Neighbourhood radius, px. */
|
|
5089
|
+
const RADIUS = 4;
|
|
5090
|
+
/** Spatial falloff (Gaussian sigma, px). */
|
|
5091
|
+
const SIGMA_SPACE = 3;
|
|
5092
|
+
/** Colour falloff (Gaussian sigma, RGB distance). */
|
|
5093
|
+
const SIGMA_COLOR = 30;
|
|
5094
|
+
const PASSES = 3;
|
|
5095
|
+
/**
|
|
5096
|
+
* A smoothed copy of `rgba` (`width` × `height`, RGBA). Transparent pixels, and
|
|
5097
|
+
* any marked in `keep` (one entry per pixel, non-zero to keep), are left as
|
|
5098
|
+
* they are and never averaged into their neighbours.
|
|
5099
|
+
*/
|
|
5100
|
+
function smoothShading(rgba, width, height, keep) {
|
|
5101
|
+
const dxs = [];
|
|
5102
|
+
const dys = [];
|
|
5103
|
+
const ws = [];
|
|
5104
|
+
for (let dy = -4; dy <= RADIUS; dy++) for (let dx = -4; dx <= RADIUS; dx++) if (dx * dx + dy * dy <= 16) {
|
|
5105
|
+
dxs.push(dx);
|
|
5106
|
+
dys.push(dy);
|
|
5107
|
+
ws.push(Math.exp(-(dx * dx + dy * dy) / (2 * SIGMA_SPACE ** 2)));
|
|
5108
|
+
}
|
|
5109
|
+
const n = ws.length;
|
|
5110
|
+
const offsets = Int32Array.from(dxs, (dx, k) => (dys[k] * width + dx) * 4);
|
|
5111
|
+
const spatial = Float32Array.from(ws);
|
|
5112
|
+
const near = Float32Array.from({ length: 195076 }, (_, d2) => Math.exp(-d2 / (2 * SIGMA_COLOR ** 2)));
|
|
5113
|
+
let src = Uint8ClampedArray.from(rgba);
|
|
5114
|
+
const kept = [];
|
|
5115
|
+
if (keep) {
|
|
5116
|
+
for (let p = 0; p < width * height; p++) if (keep[p] && src[p * 4 + 3] >= 128) {
|
|
5117
|
+
kept.push(p, src[p * 4 + 3]);
|
|
5118
|
+
src[p * 4 + 3] = 0;
|
|
5119
|
+
}
|
|
5120
|
+
}
|
|
5121
|
+
for (let pass = 0; pass < PASSES; pass++) {
|
|
5122
|
+
const out = new Uint8ClampedArray(src);
|
|
5123
|
+
for (let y = 0; y < height; y++) for (let x = 0; x < width; x++) {
|
|
5124
|
+
const i = (y * width + x) * 4;
|
|
5125
|
+
if (src[i + 3] < 128) continue;
|
|
5126
|
+
const r = src[i];
|
|
5127
|
+
const g = src[i + 1];
|
|
5128
|
+
const b = src[i + 2];
|
|
5129
|
+
const inside = x >= RADIUS && y >= RADIUS && x < width - RADIUS && y < height - RADIUS;
|
|
5130
|
+
let sr = 0;
|
|
5131
|
+
let sg = 0;
|
|
5132
|
+
let sb = 0;
|
|
5133
|
+
let sw = 0;
|
|
5134
|
+
for (let k = 0; k < n; k++) {
|
|
5135
|
+
if (!inside) {
|
|
5136
|
+
const nx = x + dxs[k];
|
|
5137
|
+
const ny = y + dys[k];
|
|
5138
|
+
if (nx < 0 || ny < 0 || nx >= width || ny >= height) continue;
|
|
5139
|
+
}
|
|
5140
|
+
const j = i + offsets[k];
|
|
5141
|
+
if (src[j + 3] < 128) continue;
|
|
5142
|
+
const dr = src[j] - r;
|
|
5143
|
+
const dg = src[j + 1] - g;
|
|
5144
|
+
const db = src[j + 2] - b;
|
|
5145
|
+
const w = spatial[k] * near[dr * dr + dg * dg + db * db];
|
|
5146
|
+
sr += w * src[j];
|
|
5147
|
+
sg += w * src[j + 1];
|
|
5148
|
+
sb += w * src[j + 2];
|
|
5149
|
+
sw += w;
|
|
5150
|
+
}
|
|
5151
|
+
out[i] = sr / sw;
|
|
5152
|
+
out[i + 1] = sg / sw;
|
|
5153
|
+
out[i + 2] = sb / sw;
|
|
5154
|
+
}
|
|
5155
|
+
src = out;
|
|
5156
|
+
}
|
|
5157
|
+
for (let k = 0; k < kept.length; k += 2) src[kept[k] * 4 + 3] = kept[k + 1];
|
|
5158
|
+
return src;
|
|
5159
|
+
}
|
|
5160
|
+
/** A line pixel is this much darker than its closing (0–255 lightness) at least. */
|
|
5161
|
+
const LINE_DEPTH = 6;
|
|
5162
|
+
/** And darkened by this many times that depth, */
|
|
5163
|
+
const LINE_GAIN = 2;
|
|
5164
|
+
/** but by no more than this: a line already well apart from its sides (brows on skin) keeps its colour. */
|
|
5165
|
+
const LINE_MAX = 30;
|
|
5166
|
+
/** A line is kept only when it spans this many times the radius (bounding-box diagonal): texture is short. */
|
|
5167
|
+
const LINE_SPAN = 4;
|
|
5168
|
+
/**
|
|
5169
|
+
* Painted outlines made plain, before the palette is picked. A painter's line
|
|
5170
|
+
* between two near-equal colours (a mouth on a shadowed face, dark trousers
|
|
5171
|
+
* against a dark background) is only a few levels darker than either side, so
|
|
5172
|
+
* it labels as one of them and is lost. A black-hat filter (the image's
|
|
5173
|
+
* closing minus the image, over `radius` px) finds lines up to about twice
|
|
5174
|
+
* that wide that are darker than both sides; each is darkened by twice its
|
|
5175
|
+
* depth, at most LINE_MAX, colour kept, so it labels apart from both sides.
|
|
5176
|
+
* Only lines that run a while are kept: brick, foliage and brush texture are
|
|
5177
|
+
* dark too, but short. When `lines` (one entry per pixel) is given, each
|
|
5178
|
+
* pixel darkened is marked in it with 1, for `smoothShading`'s `keep` and
|
|
5179
|
+
* the tracer's `lines`, which leave them be.
|
|
5180
|
+
*
|
|
5181
|
+
* On a painted film poster at 2× (radius 3), with smoothing, this brought back
|
|
5182
|
+
* the mouth, eyes, brows and jaw lines Illustration lost, and the outline
|
|
5183
|
+
* parting dark trousers from a dark background, for twice the paths (2.1 to
|
|
5184
|
+
* 3.6 MB). The trousers merged with the background until the lines were marked:
|
|
5185
|
+
* the outline was found, but smoothing blurred it back into its sides, and,
|
|
5186
|
+
* left unsmoothed, it labelled as the near-black of the trousers, close
|
|
5187
|
+
* enough to the dark grey on either side to be folded in as a sliver.
|
|
5188
|
+
*/
|
|
5189
|
+
function boostLines(rgba, width, height, radius = 2, lines) {
|
|
5190
|
+
const n = width * height;
|
|
5191
|
+
const out = Uint8ClampedArray.from(rgba);
|
|
5192
|
+
const lum = new Float32Array(n);
|
|
5193
|
+
for (let i = 0; i < n; i++) lum[i] = .299 * out[i * 4] + .587 * out[i * 4 + 1] + .114 * out[i * 4 + 2];
|
|
5194
|
+
const sweep = (src, max) => {
|
|
5195
|
+
const across = new Float32Array(n);
|
|
5196
|
+
const down = new Float32Array(n);
|
|
5197
|
+
for (let y = 0; y < height; y++) for (let x = 0; x < width; x++) {
|
|
5198
|
+
let v = src[y * width + x];
|
|
5199
|
+
for (let k = Math.max(0, x - radius); k <= Math.min(width - 1, x + radius); k++) v = max ? Math.max(v, src[y * width + k]) : Math.min(v, src[y * width + k]);
|
|
5200
|
+
across[y * width + x] = v;
|
|
5201
|
+
}
|
|
5202
|
+
for (let y = 0; y < height; y++) for (let x = 0; x < width; x++) {
|
|
5203
|
+
let v = across[y * width + x];
|
|
5204
|
+
for (let k = Math.max(0, y - radius); k <= Math.min(height - 1, y + radius); k++) v = max ? Math.max(v, across[k * width + x]) : Math.min(v, across[k * width + x]);
|
|
5205
|
+
down[y * width + x] = v;
|
|
5206
|
+
}
|
|
5207
|
+
return down;
|
|
5208
|
+
};
|
|
5209
|
+
const closed = sweep(sweep(lum, true), false);
|
|
5210
|
+
const depth = new Float32Array(n);
|
|
5211
|
+
for (let i = 0; i < n; i++) if (out[i * 4 + 3] >= 128 && closed[i] - lum[i] > LINE_DEPTH) depth[i] = closed[i] - lum[i];
|
|
5212
|
+
const seen = new Uint8Array(n);
|
|
5213
|
+
const stack = [];
|
|
5214
|
+
const piece = [];
|
|
5215
|
+
const span = LINE_SPAN * radius;
|
|
5216
|
+
for (let s = 0; s < n; s++) {
|
|
5217
|
+
if (!depth[s] || seen[s]) continue;
|
|
5218
|
+
seen[s] = 1;
|
|
5219
|
+
stack.push(s);
|
|
5220
|
+
piece.length = 0;
|
|
5221
|
+
let x0 = width;
|
|
5222
|
+
let x1 = 0;
|
|
5223
|
+
let y0 = height;
|
|
5224
|
+
let y1 = 0;
|
|
5225
|
+
while (stack.length) {
|
|
5226
|
+
const i = stack.pop();
|
|
5227
|
+
piece.push(i);
|
|
5228
|
+
const x = i % width;
|
|
5229
|
+
const y = (i - x) / width;
|
|
5230
|
+
x0 = Math.min(x0, x);
|
|
5231
|
+
x1 = Math.max(x1, x);
|
|
5232
|
+
y0 = Math.min(y0, y);
|
|
5233
|
+
y1 = Math.max(y1, y);
|
|
5234
|
+
for (let dy = -1; dy <= 1; dy++) for (let dx = -1; dx <= 1; dx++) {
|
|
5235
|
+
const nx = x + dx;
|
|
5236
|
+
const ny = y + dy;
|
|
5237
|
+
if (nx < 0 || ny < 0 || nx >= width || ny >= height) continue;
|
|
5238
|
+
const j = ny * width + nx;
|
|
5239
|
+
if (depth[j] && !seen[j]) {
|
|
5240
|
+
seen[j] = 1;
|
|
5241
|
+
stack.push(j);
|
|
5242
|
+
}
|
|
5243
|
+
}
|
|
5244
|
+
}
|
|
5245
|
+
if (Math.hypot(x1 - x0, y1 - y0) < span) continue;
|
|
5246
|
+
for (const i of piece) {
|
|
5247
|
+
const target = Math.max(0, lum[i] - Math.min(LINE_MAX, LINE_GAIN * depth[i]));
|
|
5248
|
+
const f = lum[i] > 0 ? target / lum[i] : 0;
|
|
5249
|
+
out[i * 4] *= f;
|
|
5250
|
+
out[i * 4 + 1] *= f;
|
|
5251
|
+
out[i * 4 + 2] *= f;
|
|
5252
|
+
if (lines) lines[i] = 1;
|
|
5253
|
+
}
|
|
5254
|
+
}
|
|
5255
|
+
return out;
|
|
5256
|
+
}
|
|
5257
|
+
//#endregion
|
|
4514
5258
|
//#region src/centerline.ts
|
|
4515
5259
|
/**
|
|
4516
5260
|
* Centerline tracing: single strokes down the middle of the ink, for pen
|
|
@@ -4536,7 +5280,7 @@ function toSubpath(cubics) {
|
|
|
4536
5280
|
const traceCenterline = (rgba, w, h, o) => centerlineToSvg(centerline(rgba, w, h, o));
|
|
4537
5281
|
function centerlineToSvg(r) {
|
|
4538
5282
|
const paths = r.paths.map((p) => `<path d="${attr(p.d)}" fill="none" stroke="${attr(p.stroke)}" stroke-width="${attr(round(p.strokeWidth))}" stroke-linecap="round" stroke-linejoin="round"/>`);
|
|
4539
|
-
return `<svg xmlns="http://www.w3.org/2000/svg" width="${r.width}" height="${r.height}" viewBox="0 0 ${r.width} ${r.height}">\n${paths.join("\n")}\n</svg>\n`;
|
|
5283
|
+
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${paths.join("\n")}\n</svg>\n`;
|
|
4540
5284
|
}
|
|
4541
5285
|
const round = (n) => Math.round(n * 100) / 100;
|
|
4542
5286
|
function centerline(rgba, w, h, o) {
|
|
@@ -4545,7 +5289,17 @@ function centerline(rgba, w, h, o) {
|
|
|
4545
5289
|
mergeSpecks(labels, w, h, Math.max(0, Math.round(o.speckle)) ** 2);
|
|
4546
5290
|
const ink = new Uint8Array(w * h);
|
|
4547
5291
|
for (let i = 0; i < ink.length; i++) ink[i] = labels[i] === 0 ? 1 : 0;
|
|
4548
|
-
const
|
|
5292
|
+
const skel = o.closeGaps ? thin(ink, w, h) : void 0;
|
|
5293
|
+
const joined = !!skel && closeGaps(ink, w, h, {
|
|
5294
|
+
scale: o.scale,
|
|
5295
|
+
skel
|
|
5296
|
+
}) > 0;
|
|
5297
|
+
if (joined) {
|
|
5298
|
+
for (let i = 0; i < ink.length; i++) if (ink[i]) labels[i] = 0;
|
|
5299
|
+
mergeSpecks(labels, w, h, Math.max(0, Math.round(o.speckle)) ** 2);
|
|
5300
|
+
for (let i = 0; i < ink.length; i++) ink[i] = labels[i] === 0 ? 1 : 0;
|
|
5301
|
+
}
|
|
5302
|
+
const { subs, measured } = skeletonStrokes(ink, w, h, o, joined ? void 0 : skel);
|
|
4549
5303
|
const width = o.strokeWidth && o.strokeWidth > 0 ? o.strokeWidth : measured;
|
|
4550
5304
|
return {
|
|
4551
5305
|
width: w,
|
|
@@ -4742,4 +5496,4 @@ function detectSymmetry(rgba, w, h, options = {}) {
|
|
|
4742
5496
|
return out.sort((a, b) => b.score - a.score);
|
|
4743
5497
|
}
|
|
4744
5498
|
//#endregion
|
|
4745
|
-
export { centerline, centerlineToSvg, colorAt, detectSymmetry, exactPalette, fitChain, isFlat, mapPoints, mergeLabels, oklab, otsu, parsePath, peakPalette, pointAt, quantizePalette, serializePath, staircase, toSvg, trace, traceCenterline, traceInlay };
|
|
5499
|
+
export { boostLines, centerline, centerlineToSvg, colorAt, detectSymmetry, exactPalette, fitChain, isFlat, mapPoints, mergeLabels, oklab, otsu, parsePath, peakPalette, pointAt, quantizePalette, serializePath, smoothShading, staircase, toSvg, trace, traceCenterline, traceInlay };
|
package/package.json
CHANGED