inlaytrace 0.3.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 +6 -0
- package/dist/index.d.ts +12 -1
- package/dist/index.js +328 -21
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
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
|
+
|
|
3
9
|
## 0.3.0
|
|
4
10
|
|
|
5
11
|
### Minor Changes
|
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
|
/**
|
|
@@ -220,6 +221,12 @@ type InlayOptions = FitOptions & {
|
|
|
220
221
|
* them.
|
|
221
222
|
*/
|
|
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;
|
|
223
230
|
} & (
|
|
224
231
|
/**
|
|
225
232
|
* `merge` folds colours together before any region is built, so the edge
|
|
@@ -341,6 +348,10 @@ type CenterlineOptions = {
|
|
|
341
348
|
strokeWidth?: number;
|
|
342
349
|
/** The stroke colour. Default black. */
|
|
343
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;
|
|
344
355
|
};
|
|
345
356
|
/** One stroked path: open subpaths (and closed loops), drawn with round caps and joins. */
|
|
346
357
|
type CenterlinePath = {
|
package/dist/index.js
CHANGED
|
@@ -1961,7 +1961,7 @@ function regionWidths(ink, w, h, comp, m) {
|
|
|
1961
1961
|
};
|
|
1962
1962
|
}
|
|
1963
1963
|
/** The eight neighbours P2…P9, clockwise from north, as offsets. */
|
|
1964
|
-
const DX = [
|
|
1964
|
+
const DX$1 = [
|
|
1965
1965
|
0,
|
|
1966
1966
|
1,
|
|
1967
1967
|
1,
|
|
@@ -1971,7 +1971,7 @@ const DX = [
|
|
|
1971
1971
|
-1,
|
|
1972
1972
|
-1
|
|
1973
1973
|
];
|
|
1974
|
-
const DY = [
|
|
1974
|
+
const DY$1 = [
|
|
1975
1975
|
-1,
|
|
1976
1976
|
-1,
|
|
1977
1977
|
0,
|
|
@@ -1984,8 +1984,8 @@ const DY = [
|
|
|
1984
1984
|
function neighbours(img, w, h, x, y) {
|
|
1985
1985
|
const p = [];
|
|
1986
1986
|
for (let k = 0; k < 8; k++) {
|
|
1987
|
-
const nx = x + DX[k];
|
|
1988
|
-
const ny = y + DY[k];
|
|
1987
|
+
const nx = x + DX$1[k];
|
|
1988
|
+
const ny = y + DY$1[k];
|
|
1989
1989
|
p.push(nx >= 0 && nx < w && ny >= 0 && ny < h ? img[ny * w + nx] : 0);
|
|
1990
1990
|
}
|
|
1991
1991
|
return p;
|
|
@@ -2016,7 +2016,7 @@ function pieces(p) {
|
|
|
2016
2016
|
seen[k] = true;
|
|
2017
2017
|
while (stack.length) {
|
|
2018
2018
|
const a = stack.pop();
|
|
2019
|
-
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) {
|
|
2020
2020
|
seen[b] = true;
|
|
2021
2021
|
stack.push(b);
|
|
2022
2022
|
}
|
|
@@ -2055,7 +2055,7 @@ function thin(ink, w, h) {
|
|
|
2055
2055
|
img[(y + 1) * W + x + 1] = 1;
|
|
2056
2056
|
live.push((y + 1) * W + x + 1);
|
|
2057
2057
|
}
|
|
2058
|
-
const off = DX.map((dx, k) => DY[k] * W + dx);
|
|
2058
|
+
const off = DX$1.map((dx, k) => DY$1[k] * W + dx);
|
|
2059
2059
|
const mask = (i) => {
|
|
2060
2060
|
let m = 0;
|
|
2061
2061
|
for (let k = 0; k < 8; k++) if (img[i + off[k]]) m |= 1 << k;
|
|
@@ -2093,13 +2093,13 @@ function thin(ink, w, h) {
|
|
|
2093
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];
|
|
2094
2094
|
return out;
|
|
2095
2095
|
}
|
|
2096
|
-
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);
|
|
2097
2097
|
/** Remove branches from an end to a junction shorter than `min` pixels, until none are left. */
|
|
2098
2098
|
function prune(skel, w, h, min) {
|
|
2099
2099
|
for (let pass = 0; pass < 4; pass++) {
|
|
2100
2100
|
let removed = false;
|
|
2101
2101
|
for (let i = 0; i < skel.length; i++) {
|
|
2102
|
-
if (!skel[i] || degree(skel, w, h, i) !== 1) continue;
|
|
2102
|
+
if (!skel[i] || degree$1(skel, w, h, i) !== 1) continue;
|
|
2103
2103
|
const path = [i];
|
|
2104
2104
|
let prev = -1;
|
|
2105
2105
|
let cur = i;
|
|
@@ -2109,12 +2109,12 @@ function prune(skel, w, h, min) {
|
|
|
2109
2109
|
const y = cur / w | 0;
|
|
2110
2110
|
const next = [];
|
|
2111
2111
|
for (let k = 0; k < 8; k++) {
|
|
2112
|
-
const nx = x + DX[k];
|
|
2113
|
-
const ny = y + DY[k];
|
|
2112
|
+
const nx = x + DX$1[k];
|
|
2113
|
+
const ny = y + DY$1[k];
|
|
2114
2114
|
const j = ny * w + nx;
|
|
2115
2115
|
if (nx >= 0 && nx < w && ny >= 0 && ny < h && skel[j] && j !== prev && !path.includes(j)) next.push(j);
|
|
2116
2116
|
}
|
|
2117
|
-
if (next.length !== 1 || degree(skel, w, h, next[0]) > 2) {
|
|
2117
|
+
if (next.length !== 1 || degree$1(skel, w, h, next[0]) > 2) {
|
|
2118
2118
|
atJunction = next.length >= 1;
|
|
2119
2119
|
break;
|
|
2120
2120
|
}
|
|
@@ -2138,7 +2138,7 @@ function prune(skel, w, h, min) {
|
|
|
2138
2138
|
function walk(skel, w, h) {
|
|
2139
2139
|
const n = w * h;
|
|
2140
2140
|
const isNode = new Uint8Array(n);
|
|
2141
|
-
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;
|
|
2142
2142
|
const group = new Int32Array(n).fill(-1);
|
|
2143
2143
|
const centres = [];
|
|
2144
2144
|
for (let i = 0; i < n; i++) {
|
|
@@ -2157,10 +2157,10 @@ function walk(skel, w, h) {
|
|
|
2157
2157
|
const x = j % w;
|
|
2158
2158
|
const y = j / w | 0;
|
|
2159
2159
|
for (let k = 0; k < 8; k++) {
|
|
2160
|
-
const nx = x + DX[k];
|
|
2161
|
-
const ny = y + DY[k];
|
|
2160
|
+
const nx = x + DX$1[k];
|
|
2161
|
+
const ny = y + DY$1[k];
|
|
2162
2162
|
const m = ny * w + nx;
|
|
2163
|
-
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) {
|
|
2164
2164
|
group[m] = g;
|
|
2165
2165
|
stack.push(m);
|
|
2166
2166
|
}
|
|
@@ -2189,8 +2189,8 @@ function walk(skel, w, h) {
|
|
|
2189
2189
|
5,
|
|
2190
2190
|
7
|
|
2191
2191
|
]) {
|
|
2192
|
-
const nx = x + DX[k];
|
|
2193
|
-
const ny = y + DY[k];
|
|
2192
|
+
const nx = x + DX$1[k];
|
|
2193
|
+
const ny = y + DY$1[k];
|
|
2194
2194
|
const j = ny * w + nx;
|
|
2195
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;
|
|
2196
2196
|
}
|
|
@@ -2203,8 +2203,8 @@ function walk(skel, w, h) {
|
|
|
2203
2203
|
const x = i % w;
|
|
2204
2204
|
const y = i / w | 0;
|
|
2205
2205
|
for (let k = 0; k < 8; k++) {
|
|
2206
|
-
const nx = x + DX[k];
|
|
2207
|
-
const ny = y + DY[k];
|
|
2206
|
+
const nx = x + DX$1[k];
|
|
2207
|
+
const ny = y + DY$1[k];
|
|
2208
2208
|
const j = ny * w + nx;
|
|
2209
2209
|
if (nx < 0 || nx >= w || ny < 0 || ny >= h || !skel[j] || used[j]) continue;
|
|
2210
2210
|
if (isNode[j]) {
|
|
@@ -3085,6 +3085,290 @@ const clamp = (v) => Math.max(0, Math.min(255, v));
|
|
|
3085
3085
|
const dist2 = (a, b) => (a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2 + (a[2] - b[2]) ** 2;
|
|
3086
3086
|
const round$1 = (v, places) => Math.round(v * 10 ** places) / 10 ** places;
|
|
3087
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
|
|
3088
3372
|
//#region src/inlay.ts
|
|
3089
3373
|
/**
|
|
3090
3374
|
* Inlay, our shared-edge tracer: every preset, from logos to photos.
|
|
@@ -3163,6 +3447,7 @@ function trace(rgba, w, h, o) {
|
|
|
3163
3447
|
if (!o.pixels && !(o.mode === "palette" && o.photo)) joinDiagonals(labels, w, h, rgba, drawn);
|
|
3164
3448
|
const labs = o.mode === "palette" && !o.photo ? colors.map((c) => c ? oklab(hexToRgb(c)) : null) : void 0;
|
|
3165
3449
|
mergeSpecks(labels, w, h, Math.max(0, Math.round(o.speckle)) ** 2, labs);
|
|
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);
|
|
3166
3451
|
if (labs && !o.pixels) mergeSlivers(labels, w, h, labs, o.lines);
|
|
3167
3452
|
const lines = o.mode === "palette" && o.strokes && labs && !o.pixels ? strokesOf(rgba, labels, w, h, colors, labs, drawn, o) : [];
|
|
3168
3453
|
let { comp, compLabel, compArea } = components(labels, w, h, (l) => drawn[l]);
|
|
@@ -3778,6 +4063,14 @@ function labelPalette(rgba, w, h, palette, flat) {
|
|
|
3778
4063
|
drawn: [...palette.map(() => true), false]
|
|
3779
4064
|
};
|
|
3780
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
|
+
}
|
|
3781
4074
|
function labelBinary(rgba, w, h, threshold) {
|
|
3782
4075
|
const n = w * h;
|
|
3783
4076
|
const lum = new Uint8Array(n);
|
|
@@ -4336,7 +4629,11 @@ function strokesOf(rgba, labels, w, h, colors, labs, drawn, o) {
|
|
|
4336
4629
|
}
|
|
4337
4630
|
if (kept < LINE_SHARE * all) continue;
|
|
4338
4631
|
for (let i = 0; i < n; i++) if (ink[i] && !fits(pieces.comp[i])) ink[i] = skel[i] = 0;
|
|
4339
|
-
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);
|
|
4340
4637
|
if (!subs.length) continue;
|
|
4341
4638
|
for (const i of beyondStroke(ink, w, h, subs, measured)) {
|
|
4342
4639
|
ink[i] = 0;
|
|
@@ -4992,7 +5289,17 @@ function centerline(rgba, w, h, o) {
|
|
|
4992
5289
|
mergeSpecks(labels, w, h, Math.max(0, Math.round(o.speckle)) ** 2);
|
|
4993
5290
|
const ink = new Uint8Array(w * h);
|
|
4994
5291
|
for (let i = 0; i < ink.length; i++) ink[i] = labels[i] === 0 ? 1 : 0;
|
|
4995
|
-
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);
|
|
4996
5303
|
const width = o.strokeWidth && o.strokeWidth > 0 ? o.strokeWidth : measured;
|
|
4997
5304
|
return {
|
|
4998
5305
|
width: w,
|
package/package.json
CHANGED