@pooder/kit 5.3.0 → 5.4.0

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.
Files changed (73) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/index.d.mts +249 -36
  3. package/dist/index.d.ts +249 -36
  4. package/dist/index.js +2374 -1049
  5. package/dist/index.mjs +2375 -1050
  6. package/package.json +1 -1
  7. package/src/extensions/background.ts +178 -85
  8. package/src/extensions/dieline.ts +1149 -1030
  9. package/src/extensions/dielineShape.ts +109 -0
  10. package/src/extensions/feature.ts +482 -366
  11. package/src/extensions/film.ts +148 -76
  12. package/src/extensions/geometry.ts +210 -44
  13. package/src/extensions/image.ts +244 -114
  14. package/src/extensions/ruler.ts +471 -268
  15. package/src/extensions/sceneLayoutModel.ts +28 -6
  16. package/src/extensions/sceneVisibility.ts +3 -10
  17. package/src/extensions/tracer.ts +1019 -980
  18. package/src/extensions/white-ink.ts +284 -231
  19. package/src/services/CanvasService.ts +543 -11
  20. package/src/services/renderSpec.ts +37 -2
  21. package/.test-dist/src/CanvasService.js +0 -249
  22. package/.test-dist/src/ViewportSystem.js +0 -75
  23. package/.test-dist/src/background.js +0 -203
  24. package/.test-dist/src/bridgeSelection.js +0 -20
  25. package/.test-dist/src/constraints.js +0 -237
  26. package/.test-dist/src/coordinate.js +0 -74
  27. package/.test-dist/src/dieline.js +0 -818
  28. package/.test-dist/src/edgeScale.js +0 -12
  29. package/.test-dist/src/extensions/background.js +0 -203
  30. package/.test-dist/src/extensions/bridgeSelection.js +0 -20
  31. package/.test-dist/src/extensions/constraints.js +0 -237
  32. package/.test-dist/src/extensions/dieline.js +0 -828
  33. package/.test-dist/src/extensions/edgeScale.js +0 -12
  34. package/.test-dist/src/extensions/feature.js +0 -825
  35. package/.test-dist/src/extensions/featureComplete.js +0 -32
  36. package/.test-dist/src/extensions/film.js +0 -167
  37. package/.test-dist/src/extensions/geometry.js +0 -545
  38. package/.test-dist/src/extensions/image.js +0 -1529
  39. package/.test-dist/src/extensions/index.js +0 -30
  40. package/.test-dist/src/extensions/maskOps.js +0 -279
  41. package/.test-dist/src/extensions/mirror.js +0 -104
  42. package/.test-dist/src/extensions/ruler.js +0 -345
  43. package/.test-dist/src/extensions/sceneLayout.js +0 -96
  44. package/.test-dist/src/extensions/sceneLayoutModel.js +0 -196
  45. package/.test-dist/src/extensions/sceneVisibility.js +0 -62
  46. package/.test-dist/src/extensions/size.js +0 -331
  47. package/.test-dist/src/extensions/tracer.js +0 -538
  48. package/.test-dist/src/extensions/white-ink.js +0 -1190
  49. package/.test-dist/src/extensions/wrappedOffsets.js +0 -33
  50. package/.test-dist/src/feature.js +0 -826
  51. package/.test-dist/src/featureComplete.js +0 -32
  52. package/.test-dist/src/film.js +0 -167
  53. package/.test-dist/src/geometry.js +0 -506
  54. package/.test-dist/src/image.js +0 -1250
  55. package/.test-dist/src/index.js +0 -18
  56. package/.test-dist/src/maskOps.js +0 -270
  57. package/.test-dist/src/mirror.js +0 -104
  58. package/.test-dist/src/renderSpec.js +0 -2
  59. package/.test-dist/src/ruler.js +0 -343
  60. package/.test-dist/src/sceneLayout.js +0 -99
  61. package/.test-dist/src/sceneLayoutModel.js +0 -196
  62. package/.test-dist/src/sceneView.js +0 -40
  63. package/.test-dist/src/sceneVisibility.js +0 -42
  64. package/.test-dist/src/services/CanvasService.js +0 -249
  65. package/.test-dist/src/services/ViewportSystem.js +0 -76
  66. package/.test-dist/src/services/index.js +0 -24
  67. package/.test-dist/src/services/renderSpec.js +0 -2
  68. package/.test-dist/src/size.js +0 -332
  69. package/.test-dist/src/tracer.js +0 -544
  70. package/.test-dist/src/units.js +0 -30
  71. package/.test-dist/src/white-ink.js +0 -829
  72. package/.test-dist/src/wrappedOffsets.js +0 -33
  73. package/.test-dist/tests/run.js +0 -94
@@ -1,980 +1,1019 @@
1
- /**
2
- * Image Tracer Utility
3
- * Converts raster images (URL/Base64) to SVG Path Data using Marching Squares algorithm.
4
- */
5
-
6
- import paper from "paper";
7
- import {
8
- circularMorphology,
9
- createMask,
10
- fillHoles,
11
- polygonSignedArea,
12
- type MaskMode,
13
- } from "./maskOps";
14
-
15
- interface Point {
16
- x: number;
17
- y: number;
18
- }
19
-
20
- interface Bounds {
21
- x: number;
22
- y: number;
23
- width: number;
24
- height: number;
25
- }
26
-
27
- type ComponentMode = "largest" | "all";
28
-
29
- interface ForceConnectResult {
30
- mask: Uint8Array;
31
- appliedDilateRadius: number;
32
- appliedErodeRadius: number;
33
- reachedSingleComponent: boolean;
34
- rawContourCount: number;
35
- selectedContourCount: number;
36
- }
37
-
38
- export interface ImageTraceOptions {
39
- threshold?: number;
40
- simplifyTolerance?: number;
41
- expand?: number;
42
- smoothing?: boolean;
43
- scaleToWidth?: number;
44
- scaleToHeight?: number;
45
- debug?: boolean;
46
- }
47
-
48
- export class ImageTracer {
49
- /**
50
- * Main entry point: Traces an image URL to an SVG path string.
51
- * @param imageUrl The URL or Base64 string of the image.
52
- * @param options Configuration options.
53
- */
54
- public static async trace(
55
- imageUrl: string,
56
- options: ImageTraceOptions = {},
57
- ): Promise<string> {
58
- const { pathData } = await this.traceWithBounds(imageUrl, options);
59
- return pathData;
60
- }
61
-
62
- public static async traceWithBounds(
63
- imageUrl: string,
64
- options: ImageTraceOptions = {},
65
- ): Promise<{ pathData: string; baseBounds: Bounds; bounds: Bounds }> {
66
- const img = await this.loadImage(imageUrl);
67
- const width = img.width;
68
- const height = img.height;
69
- if (width <= 0 || height <= 0) {
70
- const w = options.scaleToWidth ?? 0;
71
- const h = options.scaleToHeight ?? 0;
72
- return {
73
- pathData: `M 0 0 L ${w} 0 L ${w} ${h} L 0 ${h} Z`,
74
- baseBounds: { x: 0, y: 0, width: w, height: h },
75
- bounds: { x: 0, y: 0, width: w, height: h },
76
- };
77
- }
78
- const debug = options.debug === true;
79
- const debugLog = (message: string, payload?: Record<string, unknown>) => {
80
- if (!debug) return;
81
- if (payload) {
82
- console.info(`[ImageTracer] ${message}`, payload);
83
- return;
84
- }
85
- console.info(`[ImageTracer] ${message}`);
86
- };
87
-
88
- // Draw to canvas and get pixel data
89
- const canvas = document.createElement("canvas");
90
- canvas.width = width;
91
- canvas.height = height;
92
- const ctx = canvas.getContext("2d");
93
- if (!ctx) throw new Error("Could not get 2D context");
94
-
95
- ctx.drawImage(img, 0, 0);
96
- const imageData = ctx.getImageData(0, 0, width, height);
97
-
98
- // Strategy: fixed internal morphology + single-component target.
99
- const threshold = options.threshold ?? 10;
100
- const expand = Math.max(0, Math.floor(options.expand ?? 0));
101
- const simplifyTolerance = options.simplifyTolerance ?? 2.5;
102
- const useSmoothing = options.smoothing !== false;
103
- const componentMode: ComponentMode = "all";
104
- const minComponentArea = 0;
105
- const maxDim = Math.max(width, height);
106
- const maskMode: MaskMode = "auto";
107
- const whiteThreshold = 240;
108
- const alphaOpaqueCutoff = 250;
109
- const preprocessDilateRadius = Math.max(
110
- 2,
111
- Math.floor(Math.max(maxDim * 0.012, expand * 0.35)),
112
- );
113
- const preprocessErodeRadius = Math.max(
114
- 1,
115
- Math.floor(preprocessDilateRadius * 0.65),
116
- );
117
- const smoothDilateRadius = Math.max(
118
- 1,
119
- Math.floor(preprocessDilateRadius * 0.25),
120
- );
121
- const smoothErodeRadius = Math.max(1, Math.floor(smoothDilateRadius * 0.8));
122
- const connectStartDilateRadius = Math.max(
123
- 1,
124
- Math.floor(Math.max(maxDim * 0.006, expand * 0.2)),
125
- );
126
- const connectMaxDilateRadius = Math.max(
127
- connectStartDilateRadius,
128
- Math.floor(Math.max(maxDim * 0.2, expand * 2.5)),
129
- );
130
- const connectErodeRatio = 0.65;
131
-
132
- debugLog("traceWithBounds:start", {
133
- width,
134
- height,
135
- threshold,
136
- expand,
137
- simplifyTolerance,
138
- smoothing: useSmoothing,
139
- strategy: {
140
- maskMode,
141
- whiteThreshold,
142
- alphaOpaqueCutoff,
143
- fillHoles: true,
144
- preprocessDilateRadius,
145
- preprocessErodeRadius,
146
- smoothDilateRadius,
147
- smoothErodeRadius,
148
- connectEnabled: true,
149
- connectStartDilateRadius,
150
- connectMaxDilateRadius,
151
- connectErodeRatio,
152
- },
153
- });
154
-
155
- // Padding must cover morphology and expansion margins.
156
- const padding =
157
- Math.max(
158
- preprocessDilateRadius,
159
- smoothDilateRadius,
160
- connectMaxDilateRadius,
161
- expand,
162
- ) + 2;
163
- const paddedWidth = width + padding * 2;
164
- const paddedHeight = height + padding * 2;
165
- const summarizeMaskContours = (m: Uint8Array) => {
166
- const summary = this.summarizeAllContours(
167
- m,
168
- paddedWidth,
169
- paddedHeight,
170
- minComponentArea,
171
- );
172
- return {
173
- rawContourCount: summary.rawCount,
174
- selectedContourCount: summary.selectedCount,
175
- };
176
- };
177
-
178
- let mask = createMask(imageData, {
179
- threshold,
180
- padding,
181
- paddedWidth,
182
- paddedHeight,
183
- maskMode,
184
- whiteThreshold,
185
- alphaOpaqueCutoff,
186
- });
187
- if (debug) {
188
- debugLog(
189
- "traceWithBounds:mask:after-create",
190
- summarizeMaskContours(mask),
191
- );
192
- }
193
-
194
- mask = circularMorphology(
195
- mask,
196
- paddedWidth,
197
- paddedHeight,
198
- preprocessDilateRadius,
199
- "dilate",
200
- );
201
- mask = fillHoles(mask, paddedWidth, paddedHeight);
202
- mask = circularMorphology(
203
- mask,
204
- paddedWidth,
205
- paddedHeight,
206
- preprocessErodeRadius,
207
- "erode",
208
- );
209
- mask = fillHoles(mask, paddedWidth, paddedHeight);
210
- if (debug) {
211
- debugLog("traceWithBounds:mask:after-preprocess", {
212
- dilateRadius: preprocessDilateRadius,
213
- erodeRadius: preprocessErodeRadius,
214
- ...summarizeMaskContours(mask),
215
- });
216
- }
217
-
218
- mask = circularMorphology(
219
- mask,
220
- paddedWidth,
221
- paddedHeight,
222
- smoothDilateRadius,
223
- "dilate",
224
- );
225
- mask = fillHoles(mask, paddedWidth, paddedHeight);
226
- mask = circularMorphology(
227
- mask,
228
- paddedWidth,
229
- paddedHeight,
230
- smoothErodeRadius,
231
- "erode",
232
- );
233
- mask = fillHoles(mask, paddedWidth, paddedHeight);
234
- if (debug) {
235
- debugLog("traceWithBounds:mask:after-smooth", {
236
- dilateRadius: smoothDilateRadius,
237
- erodeRadius: smoothErodeRadius,
238
- ...summarizeMaskContours(mask),
239
- });
240
- }
241
-
242
- const beforeConnectSummary = summarizeMaskContours(mask);
243
- if (beforeConnectSummary.selectedContourCount <= 1) {
244
- debugLog("traceWithBounds:mask:connect-skipped", {
245
- reason: "already-single-component",
246
- before: beforeConnectSummary,
247
- });
248
- } else {
249
- const connectResult = this.findForceConnectResult(
250
- mask,
251
- paddedWidth,
252
- paddedHeight,
253
- minComponentArea,
254
- connectStartDilateRadius,
255
- connectMaxDilateRadius,
256
- connectErodeRatio,
257
- );
258
- if (debug) {
259
- debugLog("traceWithBounds:mask:after-connect", {
260
- before: beforeConnectSummary,
261
- appliedDilateRadius: connectResult.appliedDilateRadius,
262
- appliedErodeRadius: connectResult.appliedErodeRadius,
263
- reachedSingleComponent: connectResult.reachedSingleComponent,
264
- after: {
265
- rawContourCount: connectResult.rawContourCount,
266
- selectedContourCount: connectResult.selectedContourCount,
267
- },
268
- });
269
- }
270
- mask = connectResult.mask;
271
- }
272
-
273
- if (debug) {
274
- const afterConnectSummary = summarizeMaskContours(mask);
275
- if (afterConnectSummary.selectedContourCount > 1) {
276
- debugLog("traceWithBounds:mask:connect-warning", {
277
- reason: "still-multi-component-after-connect-search",
278
- summary: afterConnectSummary,
279
- });
280
- }
281
- }
282
-
283
- const baseMask = mask;
284
- const baseContoursRaw = this.traceAllContours(
285
- baseMask,
286
- paddedWidth,
287
- paddedHeight,
288
- );
289
- const baseContours = this.selectContours(
290
- baseContoursRaw,
291
- componentMode,
292
- minComponentArea,
293
- );
294
-
295
- if (!baseContours.length) {
296
- // Fallback: Return a rectangular outline matching dimensions
297
- const w = options.scaleToWidth ?? width;
298
- const h = options.scaleToHeight ?? height;
299
- debugLog("fallback:no-base-contour", { width: w, height: h });
300
- return {
301
- pathData: `M 0 0 L ${w} 0 L ${w} ${h} L 0 ${h} Z`,
302
- baseBounds: { x: 0, y: 0, width: w, height: h },
303
- bounds: { x: 0, y: 0, width: w, height: h },
304
- };
305
- }
306
-
307
- const baseUnpaddedContours = baseContours
308
- .map((contour) =>
309
- this.clampPointsToImageBounds(
310
- contour.map((p) => ({
311
- x: p.x - padding,
312
- y: p.y - padding,
313
- })),
314
- width,
315
- height,
316
- ),
317
- )
318
- .filter((contour) => contour.length > 2);
319
-
320
- if (!baseUnpaddedContours.length) {
321
- const w = options.scaleToWidth ?? width;
322
- const h = options.scaleToHeight ?? height;
323
- debugLog("fallback:empty-base-contours", { width: w, height: h });
324
- return {
325
- pathData: `M 0 0 L ${w} 0 L ${w} ${h} L 0 ${h} Z`,
326
- baseBounds: { x: 0, y: 0, width: w, height: h },
327
- bounds: { x: 0, y: 0, width: w, height: h },
328
- };
329
- }
330
-
331
- let baseBounds = this.boundsFromPoints(
332
- this.flattenContours(baseUnpaddedContours),
333
- );
334
-
335
- let maskExpanded = baseMask;
336
- if (expand > 0) {
337
- maskExpanded = circularMorphology(
338
- baseMask,
339
- paddedWidth,
340
- paddedHeight,
341
- expand,
342
- "dilate",
343
- );
344
- }
345
-
346
- const expandedContoursRaw = this.traceAllContours(
347
- maskExpanded,
348
- paddedWidth,
349
- paddedHeight,
350
- );
351
- const expandedContours = this.selectContours(
352
- expandedContoursRaw,
353
- componentMode,
354
- minComponentArea,
355
- );
356
- if (!expandedContours.length) {
357
- debugLog("fallback:no-expanded-contour", {
358
- baseBounds,
359
- width,
360
- height,
361
- expand,
362
- });
363
- return {
364
- pathData: `M 0 0 L ${width} 0 L ${width} ${height} L 0 ${height} Z`,
365
- baseBounds,
366
- bounds: baseBounds,
367
- };
368
- }
369
-
370
- // Keep expanded coordinates in the unpadded space without clamping to
371
- // original image bounds. If the shape touches an edge, clamping would
372
- // drop one-sided expand distance (e.g. bottom/right expansion).
373
- const expandedUnpaddedContours = expandedContours
374
- .map((contour) =>
375
- contour.map((p) => ({
376
- x: p.x - padding,
377
- y: p.y - padding,
378
- })),
379
- )
380
- .filter((contour) => contour.length > 2);
381
- if (!expandedUnpaddedContours.length) {
382
- debugLog("fallback:empty-expanded-contours", {
383
- baseBounds,
384
- width,
385
- height,
386
- expand,
387
- });
388
- return {
389
- pathData: `M 0 0 L ${width} 0 L ${width} ${height} L 0 ${height} Z`,
390
- baseBounds,
391
- bounds: baseBounds,
392
- };
393
- }
394
-
395
- let globalBounds = this.boundsFromPoints(
396
- this.flattenContours(expandedUnpaddedContours),
397
- );
398
-
399
- // Post-processing (Scale)
400
- let finalContours = expandedUnpaddedContours;
401
- if (options.scaleToWidth && options.scaleToHeight) {
402
- finalContours = this.scaleContours(
403
- expandedUnpaddedContours,
404
- options.scaleToWidth,
405
- options.scaleToHeight,
406
- globalBounds,
407
- );
408
- globalBounds = this.boundsFromPoints(this.flattenContours(finalContours));
409
-
410
- const baseScaledContours = this.scaleContours(
411
- baseUnpaddedContours,
412
- options.scaleToWidth,
413
- options.scaleToHeight,
414
- baseBounds,
415
- );
416
- baseBounds = this.boundsFromPoints(
417
- this.flattenContours(baseScaledContours),
418
- );
419
- }
420
-
421
- // Simplify and Generate SVG
422
- debugLog("traceWithBounds:contours", {
423
- baseContourCount: baseContoursRaw.length,
424
- baseSelectedCount: baseContours.length,
425
- expandedContourCount: expandedContoursRaw.length,
426
- expandedSelectedCount: expandedContours.length,
427
- baseBounds,
428
- expandedBounds: globalBounds,
429
- expandedDeltaX: globalBounds.width - baseBounds.width,
430
- expandedDeltaY: globalBounds.height - baseBounds.height,
431
- expandedMayOverflowImageBounds: expand > 0,
432
- useSmoothing,
433
- componentMode,
434
- });
435
-
436
- if (useSmoothing) {
437
- return {
438
- pathData: this.contoursToSVGPaper(finalContours, simplifyTolerance),
439
- baseBounds,
440
- bounds: globalBounds,
441
- };
442
- } else {
443
- const simplifiedContours = finalContours
444
- .map((points) => this.douglasPeucker(points, simplifyTolerance))
445
- .filter((points) => points.length > 2);
446
- const pathData =
447
- this.contoursToSVG(simplifiedContours) ||
448
- this.contoursToSVG(finalContours);
449
- return {
450
- pathData,
451
- baseBounds,
452
- bounds: globalBounds,
453
- };
454
- }
455
- }
456
-
457
- private static pickPrimaryContour(contours: Point[][]): Point[] | null {
458
- if (contours.length === 0) return null;
459
- return contours.reduce((best, cur) => {
460
- if (!best) return cur;
461
- const bestArea = Math.abs(polygonSignedArea(best));
462
- const curArea = Math.abs(polygonSignedArea(cur));
463
- if (curArea !== bestArea) return curArea > bestArea ? cur : best;
464
- return cur.length > best.length ? cur : best;
465
- }, contours[0]);
466
- }
467
-
468
- private static flattenContours(contours: Point[][]): Point[] {
469
- return contours.flatMap((contour) => contour);
470
- }
471
-
472
- private static contourCentroid(points: Point[]): Point {
473
- if (!points.length) return { x: 0, y: 0 };
474
- const sum = points.reduce(
475
- (acc, p) => ({ x: acc.x + p.x, y: acc.y + p.y }),
476
- { x: 0, y: 0 },
477
- );
478
- return {
479
- x: sum.x / points.length,
480
- y: sum.y / points.length,
481
- };
482
- }
483
-
484
- private static pointInPolygon(point: Point, polygon: Point[]): boolean {
485
- let inside = false;
486
- const { x, y } = point;
487
- for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
488
- const xi = polygon[i].x;
489
- const yi = polygon[i].y;
490
- const xj = polygon[j].x;
491
- const yj = polygon[j].y;
492
- const intersects =
493
- yi > y !== yj > y &&
494
- x < ((xj - xi) * (y - yi)) / (yj - yi || Number.EPSILON) + xi;
495
- if (intersects) inside = !inside;
496
- }
497
- return inside;
498
- }
499
-
500
- private static keepOutermostContours(contours: Point[][]): Point[][] {
501
- if (contours.length <= 1) return contours;
502
-
503
- const sorted = [...contours].sort(
504
- (a, b) => Math.abs(polygonSignedArea(b)) - Math.abs(polygonSignedArea(a)),
505
- );
506
- const selected: Point[][] = [];
507
- for (const contour of sorted) {
508
- const centroid = this.contourCentroid(contour);
509
- const isNested = selected.some((outer) =>
510
- this.pointInPolygon(centroid, outer),
511
- );
512
- if (!isNested) {
513
- selected.push(contour);
514
- }
515
- }
516
- return selected;
517
- }
518
-
519
- private static summarizeAllContours(
520
- mask: Uint8Array,
521
- width: number,
522
- height: number,
523
- minComponentArea: number,
524
- ): { rawCount: number; selectedCount: number } {
525
- const raw = this.traceAllContours(mask, width, height);
526
- const selected = this.selectContours(raw, "all", minComponentArea);
527
- return {
528
- rawCount: raw.length,
529
- selectedCount: selected.length,
530
- };
531
- }
532
-
533
- private static findForceConnectResult(
534
- sourceMask: Uint8Array,
535
- width: number,
536
- height: number,
537
- minComponentArea: number,
538
- startDilateRadius: number,
539
- maxDilateRadius: number,
540
- erodeRatio: number,
541
- ): ForceConnectResult {
542
- const initial = this.summarizeAllContours(
543
- sourceMask,
544
- width,
545
- height,
546
- minComponentArea,
547
- );
548
- if (initial.selectedCount <= 1) {
549
- return {
550
- mask: sourceMask,
551
- appliedDilateRadius: 0,
552
- appliedErodeRadius: 0,
553
- reachedSingleComponent: true,
554
- rawContourCount: initial.rawCount,
555
- selectedContourCount: initial.selectedCount,
556
- };
557
- }
558
-
559
- const normalizedStart = Math.max(1, Math.floor(startDilateRadius));
560
- const normalizedMax = Math.max(
561
- normalizedStart,
562
- Math.floor(maxDilateRadius),
563
- );
564
- const normalizedErodeRatio = Math.max(0, erodeRatio);
565
- const evaluate = (dilateRadius: number) => {
566
- const erodeRadius = Math.max(
567
- 1,
568
- Math.floor(dilateRadius * normalizedErodeRatio),
569
- );
570
- let mask = sourceMask;
571
- mask = circularMorphology(mask, width, height, dilateRadius, "dilate");
572
- mask = fillHoles(mask, width, height);
573
- mask = circularMorphology(mask, width, height, erodeRadius, "erode");
574
- mask = fillHoles(mask, width, height);
575
- const summary = this.summarizeAllContours(
576
- mask,
577
- width,
578
- height,
579
- minComponentArea,
580
- );
581
- return {
582
- dilateRadius,
583
- erodeRadius,
584
- mask,
585
- rawCount: summary.rawCount,
586
- selectedCount: summary.selectedCount,
587
- };
588
- };
589
-
590
- let low = normalizedStart - 1;
591
- let high = normalizedStart;
592
- let highResult = evaluate(high);
593
- while (high < normalizedMax && highResult.selectedCount > 1) {
594
- low = high;
595
- high = Math.min(
596
- normalizedMax,
597
- Math.max(high + 1, Math.floor(high * 1.6)),
598
- );
599
- highResult = evaluate(high);
600
- }
601
-
602
- if (highResult.selectedCount > 1) {
603
- return {
604
- mask: highResult.mask,
605
- appliedDilateRadius: highResult.dilateRadius,
606
- appliedErodeRadius: highResult.erodeRadius,
607
- reachedSingleComponent: false,
608
- rawContourCount: highResult.rawCount,
609
- selectedContourCount: highResult.selectedCount,
610
- };
611
- }
612
-
613
- let best = highResult;
614
- while (low + 1 < high) {
615
- const mid = Math.floor((low + high) / 2);
616
- const midResult = evaluate(mid);
617
- if (midResult.selectedCount <= 1) {
618
- best = midResult;
619
- high = mid;
620
- } else {
621
- low = mid;
622
- }
623
- }
624
-
625
- return {
626
- mask: best.mask,
627
- appliedDilateRadius: best.dilateRadius,
628
- appliedErodeRadius: best.erodeRadius,
629
- reachedSingleComponent: true,
630
- rawContourCount: best.rawCount,
631
- selectedContourCount: best.selectedCount,
632
- };
633
- }
634
-
635
- private static selectContours(
636
- contours: Point[][],
637
- mode: ComponentMode,
638
- minComponentArea: number,
639
- ): Point[][] {
640
- if (!contours.length) return [];
641
- if (mode === "largest") {
642
- const primary = this.pickPrimaryContour(contours);
643
- return primary ? [primary] : [];
644
- }
645
-
646
- const threshold = Math.max(0, minComponentArea);
647
- if (threshold <= 0) {
648
- return this.keepOutermostContours(contours);
649
- }
650
-
651
- const filtered = contours.filter(
652
- (contour) => Math.abs(polygonSignedArea(contour)) >= threshold,
653
- );
654
- if (filtered.length > 0) {
655
- return this.keepOutermostContours(filtered);
656
- }
657
-
658
- const primary = this.pickPrimaryContour(contours);
659
- return primary ? [primary] : [];
660
- }
661
-
662
- private static boundsFromPoints(points: Point[]): Bounds {
663
- let minX = Infinity;
664
- let minY = Infinity;
665
- let maxX = -Infinity;
666
- let maxY = -Infinity;
667
-
668
- for (const p of points) {
669
- if (p.x < minX) minX = p.x;
670
- if (p.y < minY) minY = p.y;
671
- if (p.x > maxX) maxX = p.x;
672
- if (p.y > maxY) maxY = p.y;
673
- }
674
-
675
- if (!Number.isFinite(minX) || !Number.isFinite(minY)) {
676
- return { x: 0, y: 0, width: 0, height: 0 };
677
- }
678
-
679
- return {
680
- x: minX,
681
- y: minY,
682
- width: maxX - minX,
683
- height: maxY - minY,
684
- };
685
- }
686
-
687
- /**
688
- * Traces all contours in the mask with optimized start-point detection
689
- */
690
- private static traceAllContours(
691
- mask: Uint8Array,
692
- width: number,
693
- height: number,
694
- ): Point[][] {
695
- const visited = new Uint8Array(width * height);
696
- const allContours: Point[][] = [];
697
-
698
- for (let y = 0; y < height; y++) {
699
- for (let x = 0; x < width; x++) {
700
- const idx = y * width + x;
701
- if (mask[idx] && !visited[idx]) {
702
- // Only start a new trace if it's a potential outer boundary (left edge)
703
- const isLeftEdge = x === 0 || mask[idx - 1] === 0;
704
- if (isLeftEdge) {
705
- const contour = this.marchingSquares(
706
- mask,
707
- visited,
708
- x,
709
- y,
710
- width,
711
- height,
712
- );
713
- if (contour.length > 2) {
714
- allContours.push(contour);
715
- }
716
- }
717
- }
718
- }
719
- }
720
- return allContours;
721
- }
722
-
723
- private static loadImage(url: string): Promise<HTMLImageElement> {
724
- return new Promise((resolve, reject) => {
725
- const img = new Image();
726
- img.crossOrigin = "Anonymous";
727
- img.onload = () => resolve(img);
728
- img.onerror = (e) => reject(e);
729
- img.src = url;
730
- });
731
- }
732
-
733
- /**
734
- * Moore-Neighbor Tracing Algorithm
735
- * More robust for irregular shapes than simple Marching Squares walker.
736
- */
737
- private static marchingSquares(
738
- mask: Uint8Array,
739
- visited: Uint8Array,
740
- startX: number,
741
- startY: number,
742
- width: number,
743
- height: number,
744
- ): Point[] {
745
- const isSolid = (x: number, y: number): boolean => {
746
- if (x < 0 || x >= width || y < 0 || y >= height) return false;
747
- return mask[y * width + x] === 1;
748
- };
749
-
750
- const points: Point[] = [];
751
-
752
- // Moore-Neighbor Tracing
753
- // We enter from the Left (since we scan Left->Right), so "backtrack" is Left.
754
- // B = (startX - 1, startY)
755
- // P = (startX, startY)
756
-
757
- let cx = startX;
758
- let cy = startY;
759
-
760
- // Start backtrack direction: Left (since we found it scanning from left)
761
- // Directions: 0=Up, 1=UpRight, 2=Right, 3=DownRight, 4=Down, 5=DownLeft, 6=Left, 7=UpLeft
762
- // Offsets for 8 neighbors starting from Up (0,-1) clockwise
763
- const neighbors = [
764
- { x: 0, y: -1 },
765
- { x: 1, y: -1 },
766
- { x: 1, y: 0 },
767
- { x: 1, y: 1 },
768
- { x: 0, y: 1 },
769
- { x: -1, y: 1 },
770
- { x: -1, y: 0 },
771
- { x: -1, y: -1 },
772
- ];
773
-
774
- // Backtrack is Left -> Index 6.
775
- let backtrack = 6;
776
-
777
- const maxSteps = width * height * 3;
778
- let steps = 0;
779
-
780
- do {
781
- points.push({ x: cx, y: cy });
782
- visited[cy * width + cx] = 1; // Mark as visited to avoid re-starting here
783
-
784
- // Search for next solid neighbor in clockwise order, starting from backtrack
785
- let found = false;
786
-
787
- for (let i = 0; i < 8; i++) {
788
- const idx = (backtrack + 1 + i) % 8;
789
- const nx = cx + neighbors[idx].x;
790
- const ny = cy + neighbors[idx].y;
791
-
792
- if (isSolid(nx, ny)) {
793
- cx = nx;
794
- cy = ny;
795
- backtrack = (idx + 4 + 1) % 8;
796
- found = true;
797
- break;
798
- }
799
- }
800
-
801
- if (!found) break;
802
-
803
- steps++;
804
- } while ((cx !== startX || cy !== startY) && steps < maxSteps);
805
-
806
- return points;
807
- }
808
-
809
- /**
810
- * Douglas-Peucker Line Simplification
811
- */
812
- private static douglasPeucker(points: Point[], tolerance: number): Point[] {
813
- if (points.length <= 2) return points;
814
-
815
- const sqTolerance = tolerance * tolerance;
816
- let maxSqDist = 0;
817
- let index = 0;
818
-
819
- const first = points[0];
820
- const last = points[points.length - 1];
821
-
822
- for (let i = 1; i < points.length - 1; i++) {
823
- const sqDist = this.getSqSegDist(points[i], first, last);
824
- if (sqDist > maxSqDist) {
825
- index = i;
826
- maxSqDist = sqDist;
827
- }
828
- }
829
-
830
- if (maxSqDist > sqTolerance) {
831
- // Check if closed loop?
832
- // If closed loop, we shouldn't simplify start/end connection too much?
833
- // Douglas-Peucker works on segments.
834
- const left = this.douglasPeucker(points.slice(0, index + 1), tolerance);
835
- const right = this.douglasPeucker(points.slice(index), tolerance);
836
- return left.slice(0, left.length - 1).concat(right);
837
- } else {
838
- return [first, last];
839
- }
840
- }
841
-
842
- private static getSqSegDist(p: Point, p1: Point, p2: Point): number {
843
- let x = p1.x;
844
- let y = p1.y;
845
- let dx = p2.x - x;
846
- let dy = p2.y - y;
847
-
848
- if (dx !== 0 || dy !== 0) {
849
- const t = ((p.x - x) * dx + (p.y - y) * dy) / (dx * dx + dy * dy);
850
- if (t > 1) {
851
- x = p2.x;
852
- y = p2.y;
853
- } else if (t > 0) {
854
- x += dx * t;
855
- y += dy * t;
856
- }
857
- }
858
-
859
- dx = p.x - x;
860
- dy = p.y - y;
861
-
862
- return dx * dx + dy * dy;
863
- }
864
-
865
- private static scalePoints(
866
- points: Point[],
867
- targetWidth: number,
868
- targetHeight: number,
869
- bounds: { x: number; y: number; width: number; height: number },
870
- ): Point[] {
871
- if (points.length === 0) return points;
872
-
873
- if (bounds.width === 0 || bounds.height === 0) return points;
874
-
875
- const scaleX = targetWidth / bounds.width;
876
- const scaleY = targetHeight / bounds.height;
877
-
878
- return points.map((p) => ({
879
- x: (p.x - bounds.x) * scaleX,
880
- y: (p.y - bounds.y) * scaleY,
881
- }));
882
- }
883
-
884
- private static scaleContours(
885
- contours: Point[][],
886
- targetWidth: number,
887
- targetHeight: number,
888
- bounds: { x: number; y: number; width: number; height: number },
889
- ): Point[][] {
890
- return contours.map((points) =>
891
- this.scalePoints(points, targetWidth, targetHeight, bounds),
892
- );
893
- }
894
-
895
- private static clampPointsToImageBounds(
896
- points: Point[],
897
- width: number,
898
- height: number,
899
- ): Point[] {
900
- const maxX = Math.max(0, width);
901
- const maxY = Math.max(0, height);
902
- return points.map((p) => ({
903
- x: Math.max(0, Math.min(maxX, p.x)),
904
- y: Math.max(0, Math.min(maxY, p.y)),
905
- }));
906
- }
907
-
908
- private static pointsToSVG(points: Point[]): string {
909
- if (points.length === 0) return "";
910
- const head = points[0];
911
- const tail = points.slice(1);
912
-
913
- return (
914
- `M ${head.x} ${head.y} ` +
915
- tail.map((p) => `L ${p.x} ${p.y}`).join(" ") +
916
- " Z"
917
- );
918
- }
919
-
920
- private static contoursToSVG(contours: Point[][]): string {
921
- return contours
922
- .filter((points) => points.length > 2)
923
- .map((points) => this.pointsToSVG(points))
924
- .join(" ")
925
- .trim();
926
- }
927
-
928
- private static ensurePaper() {
929
- if (!paper.project) {
930
- paper.setup(new paper.Size(100, 100));
931
- }
932
- }
933
-
934
- private static pointsToSVGPaper(points: Point[], tolerance: number): string {
935
- if (points.length < 3) return this.pointsToSVG(points);
936
-
937
- this.ensurePaper();
938
-
939
- // Create Path
940
- const path = new paper.Path({
941
- segments: points.map((p) => [p.x, p.y]),
942
- closed: true,
943
- });
944
-
945
- // Simplify
946
- path.simplify(tolerance);
947
-
948
- const data = path.pathData;
949
- path.remove();
950
-
951
- return data;
952
- }
953
-
954
- private static contoursToSVGPaper(
955
- contours: Point[][],
956
- tolerance: number,
957
- ): string {
958
- const normalizedContours = contours.filter((points) => points.length > 2);
959
- if (!normalizedContours.length) return "";
960
- if (normalizedContours.length === 1) {
961
- return this.pointsToSVGPaper(normalizedContours[0], tolerance);
962
- }
963
-
964
- this.ensurePaper();
965
- const compound = new paper.CompoundPath({ insert: false });
966
- for (const points of normalizedContours) {
967
- const child = new paper.Path({
968
- segments: points.map((p) => [p.x, p.y]),
969
- closed: true,
970
- insert: false,
971
- });
972
- child.simplify(tolerance);
973
- compound.addChild(child);
974
- }
975
-
976
- const data = compound.pathData || this.contoursToSVG(normalizedContours);
977
- compound.remove();
978
- return data;
979
- }
980
- }
1
+ /**
2
+ * Image Tracer Utility
3
+ * Converts raster images (URL/Base64) to SVG Path Data using Marching Squares algorithm.
4
+ */
5
+
6
+ import paper from "paper";
7
+ import {
8
+ circularMorphology,
9
+ createMask,
10
+ fillHoles,
11
+ polygonSignedArea,
12
+ type MaskMode,
13
+ } from "./maskOps";
14
+
15
+ interface Point {
16
+ x: number;
17
+ y: number;
18
+ }
19
+
20
+ interface Bounds {
21
+ x: number;
22
+ y: number;
23
+ width: number;
24
+ height: number;
25
+ }
26
+
27
+ type ComponentMode = "largest" | "all";
28
+
29
+ interface ForceConnectResult {
30
+ mask: Uint8Array;
31
+ appliedDilateRadius: number;
32
+ appliedErodeRadius: number;
33
+ reachedSingleComponent: boolean;
34
+ rawContourCount: number;
35
+ selectedContourCount: number;
36
+ }
37
+
38
+ export interface ImageTraceOptions {
39
+ threshold?: number;
40
+ simplifyTolerance?: number;
41
+ expand?: number;
42
+ smoothing?: boolean;
43
+ scaleToWidth?: number;
44
+ scaleToHeight?: number;
45
+ debug?: boolean;
46
+ }
47
+
48
+ export class ImageTracer {
49
+ /**
50
+ * Main entry point: Traces an image URL to an SVG path string.
51
+ * @param imageUrl The URL or Base64 string of the image.
52
+ * @param options Configuration options.
53
+ */
54
+ public static async trace(
55
+ imageUrl: string,
56
+ options: ImageTraceOptions = {},
57
+ ): Promise<string> {
58
+ const { pathData } = await this.traceWithBounds(imageUrl, options);
59
+ return pathData;
60
+ }
61
+
62
+ public static async traceWithBounds(
63
+ imageUrl: string,
64
+ options: ImageTraceOptions = {},
65
+ ): Promise<{ pathData: string; baseBounds: Bounds; bounds: Bounds }> {
66
+ const img = await this.loadImage(imageUrl);
67
+ const width = img.width;
68
+ const height = img.height;
69
+ if (width <= 0 || height <= 0) {
70
+ const w = options.scaleToWidth ?? 0;
71
+ const h = options.scaleToHeight ?? 0;
72
+ return {
73
+ pathData: `M 0 0 L ${w} 0 L ${w} ${h} L 0 ${h} Z`,
74
+ baseBounds: { x: 0, y: 0, width: w, height: h },
75
+ bounds: { x: 0, y: 0, width: w, height: h },
76
+ };
77
+ }
78
+ const debug = options.debug === true;
79
+ const debugLog = (message: string, payload?: Record<string, unknown>) => {
80
+ if (!debug) return;
81
+ if (payload) {
82
+ console.info(`[ImageTracer] ${message}`, payload);
83
+ return;
84
+ }
85
+ console.info(`[ImageTracer] ${message}`);
86
+ };
87
+
88
+ // Draw to canvas and get pixel data
89
+ const canvas = document.createElement("canvas");
90
+ canvas.width = width;
91
+ canvas.height = height;
92
+ const ctx = canvas.getContext("2d");
93
+ if (!ctx) throw new Error("Could not get 2D context");
94
+
95
+ ctx.drawImage(img, 0, 0);
96
+ const imageData = ctx.getImageData(0, 0, width, height);
97
+
98
+ // Strategy: fixed internal morphology + single-component target.
99
+ const threshold = options.threshold ?? 10;
100
+ const expand = Math.max(0, Math.floor(options.expand ?? 0));
101
+ const simplifyTolerance = options.simplifyTolerance ?? 2.5;
102
+ const useSmoothing = options.smoothing !== false;
103
+ const componentMode: ComponentMode = "all";
104
+ const minComponentArea = 0;
105
+ const maxDim = Math.max(width, height);
106
+ const maskMode: MaskMode = "auto";
107
+ const whiteThreshold = 240;
108
+ const alphaOpaqueCutoff = 250;
109
+ const preprocessDilateRadius = Math.max(
110
+ 2,
111
+ Math.floor(Math.max(maxDim * 0.012, expand * 0.35)),
112
+ );
113
+ const preprocessErodeRadius = Math.max(
114
+ 1,
115
+ Math.floor(preprocessDilateRadius * 0.65),
116
+ );
117
+ const smoothDilateRadius = Math.max(
118
+ 1,
119
+ Math.floor(preprocessDilateRadius * 0.25),
120
+ );
121
+ const smoothErodeRadius = Math.max(1, Math.floor(smoothDilateRadius * 0.8));
122
+ const connectStartDilateRadius = Math.max(
123
+ 1,
124
+ Math.floor(Math.max(maxDim * 0.006, expand * 0.2)),
125
+ );
126
+ const connectMaxDilateRadius = Math.max(
127
+ connectStartDilateRadius,
128
+ Math.floor(Math.max(maxDim * 0.2, expand * 2.5)),
129
+ );
130
+ const connectErodeRatio = 0.65;
131
+
132
+ debugLog("traceWithBounds:start", {
133
+ width,
134
+ height,
135
+ threshold,
136
+ expand,
137
+ simplifyTolerance,
138
+ smoothing: useSmoothing,
139
+ strategy: {
140
+ maskMode,
141
+ whiteThreshold,
142
+ alphaOpaqueCutoff,
143
+ fillHoles: true,
144
+ preprocessDilateRadius,
145
+ preprocessErodeRadius,
146
+ smoothDilateRadius,
147
+ smoothErodeRadius,
148
+ connectEnabled: true,
149
+ connectStartDilateRadius,
150
+ connectMaxDilateRadius,
151
+ connectErodeRatio,
152
+ },
153
+ });
154
+
155
+ // Padding must cover morphology and expansion margins.
156
+ const padding =
157
+ Math.max(
158
+ preprocessDilateRadius,
159
+ smoothDilateRadius,
160
+ connectMaxDilateRadius,
161
+ expand,
162
+ ) + 2;
163
+ const paddedWidth = width + padding * 2;
164
+ const paddedHeight = height + padding * 2;
165
+ const summarizeMaskContours = (m: Uint8Array) => {
166
+ const summary = this.summarizeAllContours(
167
+ m,
168
+ paddedWidth,
169
+ paddedHeight,
170
+ minComponentArea,
171
+ );
172
+ return {
173
+ rawContourCount: summary.rawCount,
174
+ selectedContourCount: summary.selectedCount,
175
+ };
176
+ };
177
+
178
+ let mask = createMask(imageData, {
179
+ threshold,
180
+ padding,
181
+ paddedWidth,
182
+ paddedHeight,
183
+ maskMode,
184
+ whiteThreshold,
185
+ alphaOpaqueCutoff,
186
+ });
187
+ if (debug) {
188
+ debugLog(
189
+ "traceWithBounds:mask:after-create",
190
+ summarizeMaskContours(mask),
191
+ );
192
+ }
193
+
194
+ mask = circularMorphology(
195
+ mask,
196
+ paddedWidth,
197
+ paddedHeight,
198
+ preprocessDilateRadius,
199
+ "dilate",
200
+ );
201
+ mask = fillHoles(mask, paddedWidth, paddedHeight);
202
+ mask = circularMorphology(
203
+ mask,
204
+ paddedWidth,
205
+ paddedHeight,
206
+ preprocessErodeRadius,
207
+ "erode",
208
+ );
209
+ mask = fillHoles(mask, paddedWidth, paddedHeight);
210
+ if (debug) {
211
+ debugLog("traceWithBounds:mask:after-preprocess", {
212
+ dilateRadius: preprocessDilateRadius,
213
+ erodeRadius: preprocessErodeRadius,
214
+ ...summarizeMaskContours(mask),
215
+ });
216
+ }
217
+
218
+ mask = circularMorphology(
219
+ mask,
220
+ paddedWidth,
221
+ paddedHeight,
222
+ smoothDilateRadius,
223
+ "dilate",
224
+ );
225
+ mask = fillHoles(mask, paddedWidth, paddedHeight);
226
+ mask = circularMorphology(
227
+ mask,
228
+ paddedWidth,
229
+ paddedHeight,
230
+ smoothErodeRadius,
231
+ "erode",
232
+ );
233
+ mask = fillHoles(mask, paddedWidth, paddedHeight);
234
+ if (debug) {
235
+ debugLog("traceWithBounds:mask:after-smooth", {
236
+ dilateRadius: smoothDilateRadius,
237
+ erodeRadius: smoothErodeRadius,
238
+ ...summarizeMaskContours(mask),
239
+ });
240
+ }
241
+
242
+ const beforeConnectSummary = summarizeMaskContours(mask);
243
+ if (beforeConnectSummary.selectedContourCount <= 1) {
244
+ debugLog("traceWithBounds:mask:connect-skipped", {
245
+ reason: "already-single-component",
246
+ before: beforeConnectSummary,
247
+ });
248
+ } else {
249
+ const connectResult = this.findForceConnectResult(
250
+ mask,
251
+ paddedWidth,
252
+ paddedHeight,
253
+ minComponentArea,
254
+ connectStartDilateRadius,
255
+ connectMaxDilateRadius,
256
+ connectErodeRatio,
257
+ );
258
+ if (debug) {
259
+ debugLog("traceWithBounds:mask:after-connect", {
260
+ before: beforeConnectSummary,
261
+ appliedDilateRadius: connectResult.appliedDilateRadius,
262
+ appliedErodeRadius: connectResult.appliedErodeRadius,
263
+ reachedSingleComponent: connectResult.reachedSingleComponent,
264
+ after: {
265
+ rawContourCount: connectResult.rawContourCount,
266
+ selectedContourCount: connectResult.selectedContourCount,
267
+ },
268
+ });
269
+ }
270
+ mask = connectResult.mask;
271
+ }
272
+
273
+ if (debug) {
274
+ const afterConnectSummary = summarizeMaskContours(mask);
275
+ if (afterConnectSummary.selectedContourCount > 1) {
276
+ debugLog("traceWithBounds:mask:connect-warning", {
277
+ reason: "still-multi-component-after-connect-search",
278
+ summary: afterConnectSummary,
279
+ });
280
+ }
281
+ }
282
+
283
+ const baseMask = mask;
284
+ const baseContoursRaw = this.traceAllContours(
285
+ baseMask,
286
+ paddedWidth,
287
+ paddedHeight,
288
+ );
289
+ const baseContours = this.selectContours(
290
+ baseContoursRaw,
291
+ componentMode,
292
+ minComponentArea,
293
+ );
294
+
295
+ if (!baseContours.length) {
296
+ // Fallback: Return a rectangular outline matching dimensions
297
+ const w = options.scaleToWidth ?? width;
298
+ const h = options.scaleToHeight ?? height;
299
+ debugLog("fallback:no-base-contour", { width: w, height: h });
300
+ return {
301
+ pathData: `M 0 0 L ${w} 0 L ${w} ${h} L 0 ${h} Z`,
302
+ baseBounds: { x: 0, y: 0, width: w, height: h },
303
+ bounds: { x: 0, y: 0, width: w, height: h },
304
+ };
305
+ }
306
+
307
+ const baseUnpaddedContours = baseContours
308
+ .map((contour) =>
309
+ contour.map((p) => ({
310
+ x: p.x - padding,
311
+ y: p.y - padding,
312
+ })),
313
+ )
314
+ .filter((contour) => contour.length > 2);
315
+
316
+ if (!baseUnpaddedContours.length) {
317
+ const w = options.scaleToWidth ?? width;
318
+ const h = options.scaleToHeight ?? height;
319
+ debugLog("fallback:empty-base-contours", { width: w, height: h });
320
+ return {
321
+ pathData: `M 0 0 L ${w} 0 L ${w} ${h} L 0 ${h} Z`,
322
+ baseBounds: { x: 0, y: 0, width: w, height: h },
323
+ bounds: { x: 0, y: 0, width: w, height: h },
324
+ };
325
+ }
326
+
327
+ let baseBounds = this.boundsFromPoints(
328
+ this.flattenContours(baseUnpaddedContours),
329
+ );
330
+
331
+ let maskExpanded = baseMask;
332
+ if (expand > 0) {
333
+ maskExpanded = circularMorphology(
334
+ baseMask,
335
+ paddedWidth,
336
+ paddedHeight,
337
+ expand,
338
+ "dilate",
339
+ );
340
+ }
341
+
342
+ const expandedContoursRaw = this.traceAllContours(
343
+ maskExpanded,
344
+ paddedWidth,
345
+ paddedHeight,
346
+ );
347
+ const expandedContours = this.selectContours(
348
+ expandedContoursRaw,
349
+ componentMode,
350
+ minComponentArea,
351
+ );
352
+ if (!expandedContours.length) {
353
+ debugLog("fallback:no-expanded-contour", {
354
+ baseBounds,
355
+ width,
356
+ height,
357
+ expand,
358
+ });
359
+ return {
360
+ pathData: `M 0 0 L ${width} 0 L ${width} ${height} L 0 ${height} Z`,
361
+ baseBounds,
362
+ bounds: baseBounds,
363
+ };
364
+ }
365
+
366
+ // Keep expanded coordinates in the unpadded space without clamping to
367
+ // original image bounds. If the shape touches an edge, clamping would
368
+ // drop one-sided expand distance (e.g. bottom/right expansion).
369
+ const expandedUnpaddedContours = expandedContours
370
+ .map((contour) =>
371
+ contour.map((p) => ({
372
+ x: p.x - padding,
373
+ y: p.y - padding,
374
+ })),
375
+ )
376
+ .filter((contour) => contour.length > 2);
377
+ if (!expandedUnpaddedContours.length) {
378
+ debugLog("fallback:empty-expanded-contours", {
379
+ baseBounds,
380
+ width,
381
+ height,
382
+ expand,
383
+ });
384
+ return {
385
+ pathData: `M 0 0 L ${width} 0 L ${width} ${height} L 0 ${height} Z`,
386
+ baseBounds,
387
+ bounds: baseBounds,
388
+ };
389
+ }
390
+
391
+ let globalBounds = this.boundsFromPoints(
392
+ this.flattenContours(expandedUnpaddedContours),
393
+ );
394
+
395
+ // Post-processing (Scale)
396
+ let finalContours = expandedUnpaddedContours;
397
+ if (options.scaleToWidth && options.scaleToHeight) {
398
+ finalContours = this.scaleContours(
399
+ expandedUnpaddedContours,
400
+ options.scaleToWidth,
401
+ options.scaleToHeight,
402
+ globalBounds,
403
+ );
404
+ globalBounds = this.boundsFromPoints(this.flattenContours(finalContours));
405
+
406
+ const baseScaledContours = this.scaleContours(
407
+ baseUnpaddedContours,
408
+ options.scaleToWidth,
409
+ options.scaleToHeight,
410
+ baseBounds,
411
+ );
412
+ baseBounds = this.boundsFromPoints(
413
+ this.flattenContours(baseScaledContours),
414
+ );
415
+ }
416
+
417
+ if (expand > 0) {
418
+ const expectedExpandedBounds = {
419
+ x: baseBounds.x - expand,
420
+ y: baseBounds.y - expand,
421
+ width: baseBounds.width + expand * 2,
422
+ height: baseBounds.height + expand * 2,
423
+ };
424
+ if (
425
+ expectedExpandedBounds.width > 0 &&
426
+ expectedExpandedBounds.height > 0 &&
427
+ globalBounds.width > 0 &&
428
+ globalBounds.height > 0
429
+ ) {
430
+ const shouldNormalizeExpandBounds =
431
+ Math.abs(globalBounds.x - expectedExpandedBounds.x) > 1 ||
432
+ Math.abs(globalBounds.y - expectedExpandedBounds.y) > 1 ||
433
+ Math.abs(globalBounds.width - expectedExpandedBounds.width) > 1 ||
434
+ Math.abs(globalBounds.height - expectedExpandedBounds.height) > 1;
435
+ if (shouldNormalizeExpandBounds) {
436
+ const beforeNormalize = globalBounds;
437
+ finalContours = this.translateContours(
438
+ this.scaleContours(
439
+ finalContours,
440
+ expectedExpandedBounds.width,
441
+ expectedExpandedBounds.height,
442
+ globalBounds,
443
+ ),
444
+ expectedExpandedBounds.x,
445
+ expectedExpandedBounds.y,
446
+ );
447
+ globalBounds = this.boundsFromPoints(
448
+ this.flattenContours(finalContours),
449
+ );
450
+ debugLog("traceWithBounds:expand-normalized", {
451
+ expand,
452
+ expectedExpandedBounds,
453
+ beforeNormalize,
454
+ afterNormalize: globalBounds,
455
+ });
456
+ }
457
+ }
458
+ }
459
+
460
+ // Simplify and Generate SVG
461
+ debugLog("traceWithBounds:contours", {
462
+ baseContourCount: baseContoursRaw.length,
463
+ baseSelectedCount: baseContours.length,
464
+ expandedContourCount: expandedContoursRaw.length,
465
+ expandedSelectedCount: expandedContours.length,
466
+ baseBounds,
467
+ expandedBounds: globalBounds,
468
+ expandedDeltaX: globalBounds.width - baseBounds.width,
469
+ expandedDeltaY: globalBounds.height - baseBounds.height,
470
+ expandedMayOverflowImageBounds: expand > 0,
471
+ useSmoothing,
472
+ componentMode,
473
+ });
474
+
475
+ if (useSmoothing) {
476
+ return {
477
+ pathData: this.contoursToSVGPaper(finalContours, simplifyTolerance),
478
+ baseBounds,
479
+ bounds: globalBounds,
480
+ };
481
+ } else {
482
+ const simplifiedContours = finalContours
483
+ .map((points) => this.douglasPeucker(points, simplifyTolerance))
484
+ .filter((points) => points.length > 2);
485
+ const pathData =
486
+ this.contoursToSVG(simplifiedContours) ||
487
+ this.contoursToSVG(finalContours);
488
+ return {
489
+ pathData,
490
+ baseBounds,
491
+ bounds: globalBounds,
492
+ };
493
+ }
494
+ }
495
+
496
+ private static pickPrimaryContour(contours: Point[][]): Point[] | null {
497
+ if (contours.length === 0) return null;
498
+ return contours.reduce((best, cur) => {
499
+ if (!best) return cur;
500
+ const bestArea = Math.abs(polygonSignedArea(best));
501
+ const curArea = Math.abs(polygonSignedArea(cur));
502
+ if (curArea !== bestArea) return curArea > bestArea ? cur : best;
503
+ return cur.length > best.length ? cur : best;
504
+ }, contours[0]);
505
+ }
506
+
507
+ private static flattenContours(contours: Point[][]): Point[] {
508
+ return contours.flatMap((contour) => contour);
509
+ }
510
+
511
+ private static contourCentroid(points: Point[]): Point {
512
+ if (!points.length) return { x: 0, y: 0 };
513
+ const sum = points.reduce(
514
+ (acc, p) => ({ x: acc.x + p.x, y: acc.y + p.y }),
515
+ { x: 0, y: 0 },
516
+ );
517
+ return {
518
+ x: sum.x / points.length,
519
+ y: sum.y / points.length,
520
+ };
521
+ }
522
+
523
+ private static pointInPolygon(point: Point, polygon: Point[]): boolean {
524
+ let inside = false;
525
+ const { x, y } = point;
526
+ for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
527
+ const xi = polygon[i].x;
528
+ const yi = polygon[i].y;
529
+ const xj = polygon[j].x;
530
+ const yj = polygon[j].y;
531
+ const intersects =
532
+ yi > y !== yj > y &&
533
+ x < ((xj - xi) * (y - yi)) / (yj - yi || Number.EPSILON) + xi;
534
+ if (intersects) inside = !inside;
535
+ }
536
+ return inside;
537
+ }
538
+
539
+ private static keepOutermostContours(contours: Point[][]): Point[][] {
540
+ if (contours.length <= 1) return contours;
541
+
542
+ const sorted = [...contours].sort(
543
+ (a, b) => Math.abs(polygonSignedArea(b)) - Math.abs(polygonSignedArea(a)),
544
+ );
545
+ const selected: Point[][] = [];
546
+ for (const contour of sorted) {
547
+ const centroid = this.contourCentroid(contour);
548
+ const isNested = selected.some((outer) =>
549
+ this.pointInPolygon(centroid, outer),
550
+ );
551
+ if (!isNested) {
552
+ selected.push(contour);
553
+ }
554
+ }
555
+ return selected;
556
+ }
557
+
558
+ private static summarizeAllContours(
559
+ mask: Uint8Array,
560
+ width: number,
561
+ height: number,
562
+ minComponentArea: number,
563
+ ): { rawCount: number; selectedCount: number } {
564
+ const raw = this.traceAllContours(mask, width, height);
565
+ const selected = this.selectContours(raw, "all", minComponentArea);
566
+ return {
567
+ rawCount: raw.length,
568
+ selectedCount: selected.length,
569
+ };
570
+ }
571
+
572
+ private static findForceConnectResult(
573
+ sourceMask: Uint8Array,
574
+ width: number,
575
+ height: number,
576
+ minComponentArea: number,
577
+ startDilateRadius: number,
578
+ maxDilateRadius: number,
579
+ erodeRatio: number,
580
+ ): ForceConnectResult {
581
+ const initial = this.summarizeAllContours(
582
+ sourceMask,
583
+ width,
584
+ height,
585
+ minComponentArea,
586
+ );
587
+ if (initial.selectedCount <= 1) {
588
+ return {
589
+ mask: sourceMask,
590
+ appliedDilateRadius: 0,
591
+ appliedErodeRadius: 0,
592
+ reachedSingleComponent: true,
593
+ rawContourCount: initial.rawCount,
594
+ selectedContourCount: initial.selectedCount,
595
+ };
596
+ }
597
+
598
+ const normalizedStart = Math.max(1, Math.floor(startDilateRadius));
599
+ const normalizedMax = Math.max(
600
+ normalizedStart,
601
+ Math.floor(maxDilateRadius),
602
+ );
603
+ const normalizedErodeRatio = Math.max(0, erodeRatio);
604
+ const evaluate = (dilateRadius: number) => {
605
+ const erodeRadius = Math.max(
606
+ 1,
607
+ Math.floor(dilateRadius * normalizedErodeRatio),
608
+ );
609
+ let mask = sourceMask;
610
+ mask = circularMorphology(mask, width, height, dilateRadius, "dilate");
611
+ mask = fillHoles(mask, width, height);
612
+ mask = circularMorphology(mask, width, height, erodeRadius, "erode");
613
+ mask = fillHoles(mask, width, height);
614
+ const summary = this.summarizeAllContours(
615
+ mask,
616
+ width,
617
+ height,
618
+ minComponentArea,
619
+ );
620
+ return {
621
+ dilateRadius,
622
+ erodeRadius,
623
+ mask,
624
+ rawCount: summary.rawCount,
625
+ selectedCount: summary.selectedCount,
626
+ };
627
+ };
628
+
629
+ let low = normalizedStart - 1;
630
+ let high = normalizedStart;
631
+ let highResult = evaluate(high);
632
+ while (high < normalizedMax && highResult.selectedCount > 1) {
633
+ low = high;
634
+ high = Math.min(
635
+ normalizedMax,
636
+ Math.max(high + 1, Math.floor(high * 1.6)),
637
+ );
638
+ highResult = evaluate(high);
639
+ }
640
+
641
+ if (highResult.selectedCount > 1) {
642
+ return {
643
+ mask: highResult.mask,
644
+ appliedDilateRadius: highResult.dilateRadius,
645
+ appliedErodeRadius: highResult.erodeRadius,
646
+ reachedSingleComponent: false,
647
+ rawContourCount: highResult.rawCount,
648
+ selectedContourCount: highResult.selectedCount,
649
+ };
650
+ }
651
+
652
+ let best = highResult;
653
+ while (low + 1 < high) {
654
+ const mid = Math.floor((low + high) / 2);
655
+ const midResult = evaluate(mid);
656
+ if (midResult.selectedCount <= 1) {
657
+ best = midResult;
658
+ high = mid;
659
+ } else {
660
+ low = mid;
661
+ }
662
+ }
663
+
664
+ return {
665
+ mask: best.mask,
666
+ appliedDilateRadius: best.dilateRadius,
667
+ appliedErodeRadius: best.erodeRadius,
668
+ reachedSingleComponent: true,
669
+ rawContourCount: best.rawCount,
670
+ selectedContourCount: best.selectedCount,
671
+ };
672
+ }
673
+
674
+ private static selectContours(
675
+ contours: Point[][],
676
+ mode: ComponentMode,
677
+ minComponentArea: number,
678
+ ): Point[][] {
679
+ if (!contours.length) return [];
680
+ if (mode === "largest") {
681
+ const primary = this.pickPrimaryContour(contours);
682
+ return primary ? [primary] : [];
683
+ }
684
+
685
+ const threshold = Math.max(0, minComponentArea);
686
+ if (threshold <= 0) {
687
+ return this.keepOutermostContours(contours);
688
+ }
689
+
690
+ const filtered = contours.filter(
691
+ (contour) => Math.abs(polygonSignedArea(contour)) >= threshold,
692
+ );
693
+ if (filtered.length > 0) {
694
+ return this.keepOutermostContours(filtered);
695
+ }
696
+
697
+ const primary = this.pickPrimaryContour(contours);
698
+ return primary ? [primary] : [];
699
+ }
700
+
701
+ private static boundsFromPoints(points: Point[]): Bounds {
702
+ let minX = Infinity;
703
+ let minY = Infinity;
704
+ let maxX = -Infinity;
705
+ let maxY = -Infinity;
706
+
707
+ for (const p of points) {
708
+ if (p.x < minX) minX = p.x;
709
+ if (p.y < minY) minY = p.y;
710
+ if (p.x > maxX) maxX = p.x;
711
+ if (p.y > maxY) maxY = p.y;
712
+ }
713
+
714
+ if (!Number.isFinite(minX) || !Number.isFinite(minY)) {
715
+ return { x: 0, y: 0, width: 0, height: 0 };
716
+ }
717
+
718
+ return {
719
+ x: minX,
720
+ y: minY,
721
+ width: maxX - minX,
722
+ height: maxY - minY,
723
+ };
724
+ }
725
+
726
+ /**
727
+ * Traces all contours in the mask with optimized start-point detection
728
+ */
729
+ private static traceAllContours(
730
+ mask: Uint8Array,
731
+ width: number,
732
+ height: number,
733
+ ): Point[][] {
734
+ const visited = new Uint8Array(width * height);
735
+ const allContours: Point[][] = [];
736
+
737
+ for (let y = 0; y < height; y++) {
738
+ for (let x = 0; x < width; x++) {
739
+ const idx = y * width + x;
740
+ if (mask[idx] && !visited[idx]) {
741
+ // Only start a new trace if it's a potential outer boundary (left edge)
742
+ const isLeftEdge = x === 0 || mask[idx - 1] === 0;
743
+ if (isLeftEdge) {
744
+ const contour = this.marchingSquares(
745
+ mask,
746
+ visited,
747
+ x,
748
+ y,
749
+ width,
750
+ height,
751
+ );
752
+ if (contour.length > 2) {
753
+ allContours.push(contour);
754
+ }
755
+ }
756
+ }
757
+ }
758
+ }
759
+ return allContours;
760
+ }
761
+
762
+ private static loadImage(url: string): Promise<HTMLImageElement> {
763
+ return new Promise((resolve, reject) => {
764
+ const img = new Image();
765
+ img.crossOrigin = "Anonymous";
766
+ img.onload = () => resolve(img);
767
+ img.onerror = (e) => reject(e);
768
+ img.src = url;
769
+ });
770
+ }
771
+
772
+ /**
773
+ * Moore-Neighbor Tracing Algorithm
774
+ * More robust for irregular shapes than simple Marching Squares walker.
775
+ */
776
+ private static marchingSquares(
777
+ mask: Uint8Array,
778
+ visited: Uint8Array,
779
+ startX: number,
780
+ startY: number,
781
+ width: number,
782
+ height: number,
783
+ ): Point[] {
784
+ const isSolid = (x: number, y: number): boolean => {
785
+ if (x < 0 || x >= width || y < 0 || y >= height) return false;
786
+ return mask[y * width + x] === 1;
787
+ };
788
+
789
+ const points: Point[] = [];
790
+
791
+ // Moore-Neighbor Tracing
792
+ // We enter from the Left (since we scan Left->Right), so "backtrack" is Left.
793
+ // B = (startX - 1, startY)
794
+ // P = (startX, startY)
795
+
796
+ let cx = startX;
797
+ let cy = startY;
798
+
799
+ // Start backtrack direction: Left (since we found it scanning from left)
800
+ // Directions: 0=Up, 1=UpRight, 2=Right, 3=DownRight, 4=Down, 5=DownLeft, 6=Left, 7=UpLeft
801
+ // Offsets for 8 neighbors starting from Up (0,-1) clockwise
802
+ const neighbors = [
803
+ { x: 0, y: -1 },
804
+ { x: 1, y: -1 },
805
+ { x: 1, y: 0 },
806
+ { x: 1, y: 1 },
807
+ { x: 0, y: 1 },
808
+ { x: -1, y: 1 },
809
+ { x: -1, y: 0 },
810
+ { x: -1, y: -1 },
811
+ ];
812
+
813
+ // Backtrack is Left -> Index 6.
814
+ let backtrack = 6;
815
+
816
+ const maxSteps = width * height * 3;
817
+ let steps = 0;
818
+
819
+ do {
820
+ points.push({ x: cx, y: cy });
821
+ visited[cy * width + cx] = 1; // Mark as visited to avoid re-starting here
822
+
823
+ // Search for next solid neighbor in clockwise order, starting from backtrack
824
+ let found = false;
825
+
826
+ for (let i = 0; i < 8; i++) {
827
+ const idx = (backtrack + 1 + i) % 8;
828
+ const nx = cx + neighbors[idx].x;
829
+ const ny = cy + neighbors[idx].y;
830
+
831
+ if (isSolid(nx, ny)) {
832
+ cx = nx;
833
+ cy = ny;
834
+ backtrack = (idx + 4 + 1) % 8;
835
+ found = true;
836
+ break;
837
+ }
838
+ }
839
+
840
+ if (!found) break;
841
+
842
+ steps++;
843
+ } while ((cx !== startX || cy !== startY) && steps < maxSteps);
844
+
845
+ return points;
846
+ }
847
+
848
+ /**
849
+ * Douglas-Peucker Line Simplification
850
+ */
851
+ private static douglasPeucker(points: Point[], tolerance: number): Point[] {
852
+ if (points.length <= 2) return points;
853
+
854
+ const sqTolerance = tolerance * tolerance;
855
+ let maxSqDist = 0;
856
+ let index = 0;
857
+
858
+ const first = points[0];
859
+ const last = points[points.length - 1];
860
+
861
+ for (let i = 1; i < points.length - 1; i++) {
862
+ const sqDist = this.getSqSegDist(points[i], first, last);
863
+ if (sqDist > maxSqDist) {
864
+ index = i;
865
+ maxSqDist = sqDist;
866
+ }
867
+ }
868
+
869
+ if (maxSqDist > sqTolerance) {
870
+ // Check if closed loop?
871
+ // If closed loop, we shouldn't simplify start/end connection too much?
872
+ // Douglas-Peucker works on segments.
873
+ const left = this.douglasPeucker(points.slice(0, index + 1), tolerance);
874
+ const right = this.douglasPeucker(points.slice(index), tolerance);
875
+ return left.slice(0, left.length - 1).concat(right);
876
+ } else {
877
+ return [first, last];
878
+ }
879
+ }
880
+
881
+ private static getSqSegDist(p: Point, p1: Point, p2: Point): number {
882
+ let x = p1.x;
883
+ let y = p1.y;
884
+ let dx = p2.x - x;
885
+ let dy = p2.y - y;
886
+
887
+ if (dx !== 0 || dy !== 0) {
888
+ const t = ((p.x - x) * dx + (p.y - y) * dy) / (dx * dx + dy * dy);
889
+ if (t > 1) {
890
+ x = p2.x;
891
+ y = p2.y;
892
+ } else if (t > 0) {
893
+ x += dx * t;
894
+ y += dy * t;
895
+ }
896
+ }
897
+
898
+ dx = p.x - x;
899
+ dy = p.y - y;
900
+
901
+ return dx * dx + dy * dy;
902
+ }
903
+
904
+ private static scalePoints(
905
+ points: Point[],
906
+ targetWidth: number,
907
+ targetHeight: number,
908
+ bounds: { x: number; y: number; width: number; height: number },
909
+ ): Point[] {
910
+ if (points.length === 0) return points;
911
+
912
+ if (bounds.width === 0 || bounds.height === 0) return points;
913
+
914
+ const scaleX = targetWidth / bounds.width;
915
+ const scaleY = targetHeight / bounds.height;
916
+
917
+ return points.map((p) => ({
918
+ x: (p.x - bounds.x) * scaleX,
919
+ y: (p.y - bounds.y) * scaleY,
920
+ }));
921
+ }
922
+
923
+ private static scaleContours(
924
+ contours: Point[][],
925
+ targetWidth: number,
926
+ targetHeight: number,
927
+ bounds: { x: number; y: number; width: number; height: number },
928
+ ): Point[][] {
929
+ return contours.map((points) =>
930
+ this.scalePoints(points, targetWidth, targetHeight, bounds),
931
+ );
932
+ }
933
+
934
+ private static translateContours(
935
+ contours: Point[][],
936
+ offsetX: number,
937
+ offsetY: number,
938
+ ): Point[][] {
939
+ return contours.map((points) =>
940
+ points.map((p) => ({
941
+ x: p.x + offsetX,
942
+ y: p.y + offsetY,
943
+ })),
944
+ );
945
+ }
946
+
947
+ private static pointsToSVG(points: Point[]): string {
948
+ if (points.length === 0) return "";
949
+ const head = points[0];
950
+ const tail = points.slice(1);
951
+
952
+ return (
953
+ `M ${head.x} ${head.y} ` +
954
+ tail.map((p) => `L ${p.x} ${p.y}`).join(" ") +
955
+ " Z"
956
+ );
957
+ }
958
+
959
+ private static contoursToSVG(contours: Point[][]): string {
960
+ return contours
961
+ .filter((points) => points.length > 2)
962
+ .map((points) => this.pointsToSVG(points))
963
+ .join(" ")
964
+ .trim();
965
+ }
966
+
967
+ private static ensurePaper() {
968
+ if (!paper.project) {
969
+ paper.setup(new paper.Size(100, 100));
970
+ }
971
+ }
972
+
973
+ private static pointsToSVGPaper(points: Point[], tolerance: number): string {
974
+ if (points.length < 3) return this.pointsToSVG(points);
975
+
976
+ this.ensurePaper();
977
+
978
+ // Create Path
979
+ const path = new paper.Path({
980
+ segments: points.map((p) => [p.x, p.y]),
981
+ closed: true,
982
+ });
983
+
984
+ // Simplify
985
+ path.simplify(tolerance);
986
+
987
+ const data = path.pathData;
988
+ path.remove();
989
+
990
+ return data;
991
+ }
992
+
993
+ private static contoursToSVGPaper(
994
+ contours: Point[][],
995
+ tolerance: number,
996
+ ): string {
997
+ const normalizedContours = contours.filter((points) => points.length > 2);
998
+ if (!normalizedContours.length) return "";
999
+ if (normalizedContours.length === 1) {
1000
+ return this.pointsToSVGPaper(normalizedContours[0], tolerance);
1001
+ }
1002
+
1003
+ this.ensurePaper();
1004
+ const compound = new paper.CompoundPath({ insert: false });
1005
+ for (const points of normalizedContours) {
1006
+ const child = new paper.Path({
1007
+ segments: points.map((p) => [p.x, p.y]),
1008
+ closed: true,
1009
+ insert: false,
1010
+ });
1011
+ child.simplify(tolerance);
1012
+ compound.addChild(child);
1013
+ }
1014
+
1015
+ const data = compound.pathData || this.contoursToSVG(normalizedContours);
1016
+ compound.remove();
1017
+ return data;
1018
+ }
1019
+ }