Repository navigation
Expand file tree
/
Copy pathcore.js
More file actions
2191 lines (2061 loc) · 89 KB
/
Copy pathcore.js
File metadata and controls
2191 lines (2061 loc) · 89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* brushtrokes core — 分析 + 多风格笔触生成 + 时间轴
*
* analyze() 在降采样工作分辨率上算结构场(分层 / 边缘 / 方向 / 显著性)
* STYLES 风格注册表:每种风格 = 一套工序 + 一套专属笔触语言
* buildStages() 按 opts.style 分派到对应风格构建器
* buildTimeline()把工序串成严格不重叠的时间轴
*
* 笔触 packed 记录:[d, color, width, start, dur, detail, layer, len, alpha, kind]
* kind 的语义见 render.js 的 KIND。
*/
(function (root) {
'use strict';
const KIND_NORMAL = 0, KIND_GLAZE = 1, KIND_SCUMBLE = 2, KIND_IMPASTO = 3, KIND_FILL = 4;
const KIND_DOT = 5, KIND_GLOW = 6, KIND_PIXEL = 7;
const clamp = (v, a, b) => (v < a ? a : v > b ? b : v);
/* 安全批量追加。
* 不能用 `arr.push(...big)`:笔触数量多时(素描把细腻程度拉满会到十几万条)
* 会超出引擎对展开参数的限制,抛 "Maximum call stack size exceeded"。
* 分块追加不受这个限制。 */
function appendAll(dst, src) {
const CH = 4096;
for (let i = 0; i < src.length; i += CH) {
const chunk = src.slice(i, i + CH);
for (let j = 0; j < chunk.length; j++) dst.push(chunk[j]);
}
}
/* ---------------------------------------------------------------- 高斯 */
function gaussKernel(sigma) {
const r = Math.max(1, Math.ceil(sigma * 3));
const k = new Float32Array(2 * r + 1);
let s = 0;
for (let i = -r; i <= r; i++) { const v = Math.exp(-(i * i) / (2 * sigma * sigma)); k[i + r] = v; s += v; }
for (let i = 0; i < k.length; i++) k[i] /= s;
return { k, r };
}
function blurH(src, dst, w, h, ker) {
const { k, r } = ker;
for (let y = 0; y < h; y++) {
const off = y * w;
for (let x = 0; x < w; x++) {
let s = 0;
for (let i = -r; i <= r; i++) {
const xx = x + i < 0 ? 0 : (x + i >= w ? w - 1 : x + i);
s += src[off + xx] * k[i + r];
}
dst[off + x] = s;
}
}
}
function blurV(src, dst, w, h, ker) {
const { k, r } = ker;
for (let y = 0; y < h; y++) {
for (let x = 0; x < w; x++) {
let s = 0;
for (let i = -r; i <= r; i++) {
const yy = y + i < 0 ? 0 : (y + i >= h ? h - 1 : y + i);
s += src[yy * w + x] * k[i + r];
}
dst[y * w + x] = s;
}
}
}
function gaussBlur(src, w, h, sigma) {
if (sigma < 0.3) return new Float32Array(src);
const ker = gaussKernel(sigma);
const tmp = new Float32Array(w * h), out = new Float32Array(w * h);
blurH(src, tmp, w, h, ker);
blurV(tmp, out, w, h, ker);
return out;
}
/* ---------------------------------------------------------------- 降采样 */
function downscale(rgba, w, h, tw, th) {
const out = new Float32Array(tw * th * 3);
const fx = w / tw, fy = h / th;
for (let y = 0; y < th; y++) {
const y0 = Math.floor(y * fy), y1 = Math.min(h, Math.max(y0 + 1, Math.floor((y + 1) * fy)));
for (let x = 0; x < tw; x++) {
const x0 = Math.floor(x * fx), x1 = Math.min(w, Math.max(x0 + 1, Math.floor((x + 1) * fx)));
let r = 0, g = 0, b = 0, n = 0;
for (let yy = y0; yy < y1; yy++) {
for (let xx = x0; xx < x1; xx++) {
const i = (yy * w + xx) * 4;
r += rgba[i]; g += rgba[i + 1]; b += rgba[i + 2]; n++;
}
}
const o = (y * tw + x) * 3;
out[o] = r / n; out[o + 1] = g / n; out[o + 2] = b / n;
}
}
return out;
}
/* ---------------------------------------------------------------- Sobel */
function sobel(gray, w, h) {
const gx = new Float32Array(w * h), gy = new Float32Array(w * h);
for (let y = 0; y < h; y++) {
const ym = (y > 0 ? y - 1 : 0) * w, yp = (y < h - 1 ? y + 1 : h - 1) * w, yc = y * w;
for (let x = 0; x < w; x++) {
const xm = x > 0 ? x - 1 : 0, xp = x < w - 1 ? x + 1 : w - 1;
const a = gray[ym + xm], b = gray[ym + x], c = gray[ym + xp];
const d = gray[yc + xm], f = gray[yc + xp];
const g = gray[yp + xm], hh = gray[yp + x], i = gray[yp + xp];
gx[yc + x] = (c + 2 * f + i) - (a + 2 * d + g);
gy[yc + x] = (g + 2 * hh + i) - (a + 2 * b + c);
}
}
return { gx, gy };
}
/* ---------------------------------------------------------------- Canny */
function canny(gray, w, h, sigma, lo, hi) {
const sm = gaussBlur(gray, w, h, sigma);
const { gx, gy } = sobel(sm, w, h);
const mag = new Float32Array(w * h);
for (let i = 0; i < w * h; i++) mag[i] = Math.hypot(gx[i], gy[i]);
const nms = new Float32Array(w * h);
for (let y = 1; y < h - 1; y++) {
for (let x = 1; x < w - 1; x++) {
const i = y * w + x;
const ang = Math.atan2(gy[i], gx[i]);
const a = (ang * 180 / Math.PI + 180) % 180;
let d1, d2;
if (a < 22.5 || a >= 157.5) { d1 = i - 1; d2 = i + 1; }
else if (a < 67.5) { d1 = i - w - 1; d2 = i + w + 1; }
else if (a < 112.5) { d1 = i - w; d2 = i + w; }
else { d1 = i - w + 1; d2 = i + w - 1; }
const m = mag[i];
nms[i] = (m >= mag[d1] && m >= mag[d2]) ? m : 0;
}
}
const out = new Float32Array(w * h);
const stack = new Int32Array(w * h);
let sp = 0;
for (let i = 0; i < w * h; i++) if (nms[i] >= hi) { out[i] = 1; stack[sp++] = i; }
while (sp > 0) {
const i = stack[--sp];
const x = i % w, y = (i / w) | 0;
for (let dy = -1; dy <= 1; dy++) {
for (let dx = -1; dx <= 1; dx++) {
const nx = x + dx, ny = y + dy;
if (nx < 0 || ny < 0 || nx >= w || ny >= h) continue;
const j = ny * w + nx;
if (out[j] === 0 && nms[j] >= lo) { out[j] = 1; stack[sp++] = j; }
}
}
}
return out;
}
/* ---------------------------------------------------------------- k-means */
function kmeans2(f1, f2, n, k, iters, seed) {
let st = seed || 123456789;
const rnd = () => { st = (st * 1103515245 + 12345) & 0x7fffffff; return st / 0x7fffffff; };
const sampleN = Math.min(n, 24000);
const idx = new Int32Array(sampleN);
for (let i = 0; i < sampleN; i++) idx[i] = Math.floor(rnd() * n);
const cx = new Float32Array(k), cy = new Float32Array(k);
cx[0] = f1[idx[0]]; cy[0] = f2[idx[0]];
for (let c = 1; c < k; c++) {
let best = -1, bestD = -1;
for (let t = 0; t < 2000; t++) {
const p = idx[Math.floor(rnd() * sampleN)];
let dmin = 1e9;
for (let j = 0; j < c; j++) {
const dx = f1[p] - cx[j], dy = f2[p] - cy[j];
const d = dx * dx + dy * dy;
if (d < dmin) dmin = d;
}
if (dmin > bestD) { bestD = dmin; best = p; }
}
cx[c] = f1[best]; cy[c] = f2[best];
}
const lab = new Int32Array(n);
for (let it = 0; it < iters; it++) {
const sx = new Float64Array(k), sy = new Float64Array(k), cnt = new Float64Array(k);
for (let i = 0; i < n; i++) {
let bi = 0, bd = 1e18;
for (let c = 0; c < k; c++) {
const dx = f1[i] - cx[c], dy = f2[i] - cy[c];
const d = dx * dx + dy * dy;
if (d < bd) { bd = d; bi = c; }
}
lab[i] = bi; sx[bi] += f1[i]; sy[bi] += f2[i]; cnt[bi]++;
}
for (let c = 0; c < k; c++) {
if (cnt[c] > 0) { cx[c] = sx[c] / cnt[c]; cy[c] = sy[c] / cnt[c]; }
}
}
return { lab, cx, cy };
}
/* ---------------------------------------------------------------- 分析 */
function analyze(rgba, W, H, opts) {
const want = opts.workRes === 0 ? Math.max(W, H) : (opts.workRes || 1400);
const maxSide = clamp(want, 320, Math.max(W, H));
const sc = Math.min(1, maxSide / Math.max(W, H));
const AW = Math.max(8, Math.round(W * sc)), AH = Math.max(8, Math.round(H * sc));
const rgb = downscale(rgba, W, H, AW, AH);
const n = AW * AH;
const L = new Float32Array(n), S = new Float32Array(n), gray = new Float32Array(n);
for (let i = 0; i < n; i++) {
const r = rgb[i * 3] / 255, g = rgb[i * 3 + 1] / 255, b = rgb[i * 3 + 2] / 255;
const mx = Math.max(r, g, b), mn = Math.min(r, g, b);
L[i] = (0.2126 * r + 0.7152 * g + 0.0722 * b) * 100;
S[i] = mx > 0 ? ((mx - mn) / mx) * 100 : 0;
gray[i] = L[i] / 100;
}
const Lb = gaussBlur(L, AW, AH, 2.4), Sb = gaussBlur(S, AW, AH, 2.4);
const k = clamp(opts.layers || 5, 2, 8);
const km = kmeans2(Lb, Sb, n, k, 12, opts.seed || 20261109);
const order = Array.from({ length: k }, (_, i) => i)
.sort((a, b) => (km.cx[a] + 0.15 * km.cy[a]) - (km.cx[b] + 0.15 * km.cy[b]));
const remap = new Int32Array(k);
order.forEach((c, i) => { remap[c] = i; });
const labels = new Uint8Array(n);
for (let i = 0; i < n; i++) labels[i] = remap[km.lab[i]];
const { gx, gy } = sobel(gray, AW, AH);
const Jxx = gaussBlur((() => { const a = new Float32Array(n); for (let i = 0; i < n; i++) a[i] = gx[i] * gx[i]; return a; })(), AW, AH, 3.0);
const Jxy = gaussBlur((() => { const a = new Float32Array(n); for (let i = 0; i < n; i++) a[i] = gx[i] * gy[i]; return a; })(), AW, AH, 3.0);
const Jyy = gaussBlur((() => { const a = new Float32Array(n); for (let i = 0; i < n; i++) a[i] = gy[i] * gy[i]; return a; })(), AW, AH, 3.0);
const coh = new Float32Array(n), thHi = new Float32Array(n), mag = new Float32Array(n);
const c2 = new Float32Array(n), s2 = new Float32Array(n);
for (let i = 0; i < n; i++) {
const a = Jxx[i], b = Jxy[i], c = Jyy[i];
const tr = a + c;
const t = Math.sqrt(Math.max(0, 0.25 * tr * tr - (a * c - b * b)));
const l1 = 0.5 * tr + t, l2 = 0.5 * tr - t;
coh[i] = (l1 - l2) / (l1 + l2 + 1e-6);
thHi[i] = 0.5 * Math.atan2(2 * b, a - c) + Math.PI / 2;
mag[i] = Math.sqrt(tr);
c2[i] = Math.cos(2 * thHi[i]) * (tr + 1e-6);
s2[i] = Math.sin(2 * thHi[i]) * (tr + 1e-6);
}
const Cb = gaussBlur(c2, AW, AH, 14.0), Sb2 = gaussBlur(s2, AW, AH, 14.0);
const thLo = new Float32Array(n);
for (let i = 0; i < n; i++) thLo[i] = 0.5 * Math.atan2(Sb2[i], Cb[i]);
const eMajor = canny(gray, AW, AH, 3.0, 0.12, 0.30);
const eFine = canny(gray, AW, AH, 1.2, 0.08, 0.22);
const eFineS = gaussBlur(eFine, AW, AH, 1.3);
let mx = 1e-6; for (let i = 0; i < n; i++) if (mag[i] > mx) mx = mag[i];
const sal = new Float32Array(n);
for (let i = 0; i < n; i++) {
const gm = Math.min(1, mag[i] / mx);
sal[i] = 0.30 * gm + 0.26 * coh[i] + 0.24 * eFineS[i] + 0.20 * gm;
}
const salS = gaussBlur(sal, AW, AH, 2.2);
let smax = 1e-6; for (let i = 0; i < n; i++) if (salS[i] > smax) smax = salS[i];
for (let i = 0; i < n; i++) salS[i] = Math.min(1, salS[i] / smax);
const DX = new Float32Array(n), DY = new Float32Array(n);
for (let i = 0; i < n; i++) {
const th = (coh[i] < 0.28 && salS[i] < 0.18) ? thLo[i] : thHi[i];
DX[i] = Math.cos(th); DY[i] = Math.sin(th);
}
return {
AW, AH, sxa: W / AW, sya: H / AH, rgb, labels, L, S, gray,
coh, sal: salS, DX, DY, eMajor, eFine,
layers: order.map((c, i) => ({ id: i, L: km.cx[c], S: km.cy[c] })),
};
}
/* ---------------------------------------------------------------- 轮廓提取(marching squares)
* 输出每个连通区域的闭合轮廓(图像坐标),供「色块」风格平涂。
* 轮廓方向不规范化,渲染端统一用 evenodd 填充,孔洞自然正确。
*/
function layerContours(A, layer, minArea, labelSrc) {
const labels = labelSrc || A.labels;
const { AW, AH, sxa, sya } = A;
const at = (x, y) => (x < 0 || y < 0 || x >= AW || y >= AH) ? 0 : (labels[y * AW + x] === layer ? 1 : 0);
const AX = [], AY = [], BX = [], BY = [];
const seg = (ax, ay, bx, by) => { AX.push(ax); AY.push(ay); BX.push(bx); BY.push(by); };
for (let y = -1; y < AH; y++) {
for (let x = -1; x < AW; x++) {
const tl = at(x, y), tr = at(x + 1, y), br = at(x + 1, y + 1), bl = at(x, y + 1);
const bits = tl | (tr << 1) | (br << 2) | (bl << 3);
if (bits === 0 || bits === 15) continue;
// 用 2 倍整数坐标表示边中点,保证相邻格子端点精确相接
const tx = 2 * x + 1, ty = 2 * y;
const rx = 2 * x + 2, ry = 2 * y + 1;
const bx = 2 * x + 1, by = 2 * y + 2;
const lx = 2 * x, ly = 2 * y + 1;
switch (bits) {
case 1: seg(lx, ly, tx, ty); break;
case 2: seg(tx, ty, rx, ry); break;
case 3: seg(lx, ly, rx, ry); break;
case 4: seg(rx, ry, bx, by); break;
case 5: seg(lx, ly, tx, ty); seg(rx, ry, bx, by); break;
case 6: seg(tx, ty, bx, by); break;
case 7: seg(lx, ly, bx, by); break;
case 8: seg(bx, by, lx, ly); break;
case 9: seg(bx, by, tx, ty); break;
case 10: seg(tx, ty, rx, ry); seg(bx, by, lx, ly); break;
case 11: seg(bx, by, rx, ry); break;
case 12: seg(rx, ry, lx, ly); break;
case 13: seg(rx, ry, tx, ty); break;
case 14: seg(tx, ty, lx, ly); break;
}
}
}
const M = AX.length;
if (!M) return [];
const keyOf = (x, y) => x * 65536 + y;
const map = new Map();
for (let i = 0; i < M; i++) {
const ka = keyOf(AX[i], AY[i]), kb = keyOf(BX[i], BY[i]);
let la = map.get(ka); if (!la) { la = []; map.set(ka, la); } la.push(i);
if (kb !== ka) { let lb = map.get(kb); if (!lb) { lb = []; map.set(kb, lb); } lb.push(i); }
}
const used = new Uint8Array(M);
const loops = [];
for (let i = 0; i < M; i++) {
if (used[i]) continue;
used[i] = 1;
const px = [AX[i]], py = [AY[i]];
let cx = BX[i], cy = BY[i];
const sx0 = AX[i], sy0 = AY[i];
let guard = M + 8;
while (guard-- > 0) {
px.push(cx); py.push(cy);
if (cx === sx0 && cy === sy0) break;
const list = map.get(keyOf(cx, cy));
if (!list) break;
let nxt = -1;
for (let t = 0; t < list.length; t++) if (!used[list[t]]) { nxt = list[t]; break; }
if (nxt < 0) break;
used[nxt] = 1;
if (AX[nxt] === cx && AY[nxt] === cy) { cx = BX[nxt]; cy = BY[nxt]; }
else { cx = AX[nxt]; cy = AY[nxt]; }
}
if (px.length < 4) continue;
let area = 0;
for (let q = 0; q < px.length; q++) {
const r = (q + 1) % px.length;
area += px[q] * py[r] - px[r] * py[q];
}
area = Math.abs(area) / 2 / 4; // 2 倍坐标 → /4
if (area < minArea) continue;
const pts = new Float64Array(px.length * 2);
for (let q = 0; q < px.length; q++) {
pts[q * 2] = (px[q] / 2) * sxa;
pts[q * 2 + 1] = (py[q] / 2) * sya;
}
loops.push({ pts, area, len: perim(pts) });
}
return loops;
}
function perim(pts) {
let s = 0;
const n = pts.length / 2;
for (let i = 0; i < n; i++) {
const j = (i + 1) % n;
s += Math.hypot(pts[j * 2] - pts[i * 2], pts[j * 2 + 1] - pts[i * 2 + 1]);
}
return s;
}
/* 闭合轮廓的曲线平滑:对每个点用左右邻居做加权平均(Chaikin 式松弛),
* 把 marching squares 的台阶磨圆。迭代次数越多越圆滑。 */
function smoothLoop(pts, iters) {
let cur = Array.from(pts);
for (let it = 0; it < iters; it++) {
const n = cur.length / 2;
if (n < 4) break;
const out = new Array(n * 2);
for (let i = 0; i < n; i++) {
const ip = (i - 1 + n) % n, inx = (i + 1) % n;
// 0.25 / 0.5 / 0.25 加权,既平滑又不会明显收缩
out[i * 2] = cur[ip * 2] * 0.25 + cur[i * 2] * 0.5 + cur[inx * 2] * 0.25;
out[i * 2 + 1] = cur[ip * 2 + 1] * 0.25 + cur[i * 2 + 1] * 0.5 + cur[inx * 2 + 1] * 0.25;
}
cur = out;
}
return cur;
}
/* 沿法线把闭合轮廓向外推开 d 个像素。
* 相邻色块各自描边后,两次抗锯齿会在交界处留下一道底色细缝,
* 每块向外膨胀一点点就能互相压住,缝就没了。 */
function inflateLoop(pts, d) {
const n = pts.length / 2;
if (n < 3 || d <= 0) return Array.from(pts);
// 先判方向,保证「向外」一致
let area2 = 0;
for (let i = 0; i < n; i++) {
const j = (i + 1) % n;
area2 += pts[i * 2] * pts[j * 2 + 1] - pts[j * 2] * pts[i * 2 + 1];
}
const sign = area2 >= 0 ? 1 : -1;
const out = new Array(n * 2);
for (let i = 0; i < n; i++) {
const ip = (i - 1 + n) % n, inx = (i + 1) % n;
// 前后两条边的方向
let ax = pts[i * 2] - pts[ip * 2], ay = pts[i * 2 + 1] - pts[ip * 2 + 1];
let bx = pts[inx * 2] - pts[i * 2], by = pts[inx * 2 + 1] - pts[i * 2 + 1];
const la = Math.hypot(ax, ay) || 1e-9, lb = Math.hypot(bx, by) || 1e-9;
ax /= la; ay /= la; bx /= lb; by /= lb;
// 两条边的法线取平均
let nx = (-ay + -by) * 0.5, ny = (ax + bx) * 0.5;
const ln = Math.hypot(nx, ny) || 1e-9;
nx = (nx / ln) * d * sign; ny = (ny / ln) * d * sign;
out[i * 2] = pts[i * 2] + nx;
out[i * 2 + 1] = pts[i * 2 + 1] + ny;
}
return out;
}
/* Ramer–Douglas–Peucker(开链) */
function rdp(pts, a0, b0, eps) {
const keep = new Uint8Array(b0 - a0 + 1);
keep[0] = 1; keep[b0 - a0] = 1;
const stack = [[a0, b0]];
while (stack.length) {
const [a, b] = stack.pop();
if (b - a < 2) continue;
const ax = pts[a * 2], ay = pts[a * 2 + 1];
const bx = pts[b * 2], by = pts[b * 2 + 1];
const dx = bx - ax, dy = by - ay;
const dd = Math.hypot(dx, dy) || 1e-9;
let best = -1, bestD = -1;
for (let i = a + 1; i < b; i++) {
const px = pts[i * 2] - ax, py = pts[i * 2 + 1] - ay;
const d = Math.abs(px * dy - py * dx) / dd;
if (d > bestD) { bestD = d; best = i; }
}
if (bestD > eps) { keep[best - a0] = 1; stack.push([a, best]); stack.push([best, b]); }
}
const out = [];
for (let i = a0; i <= b0; i++) if (keep[i - a0]) out.push(pts[i * 2], pts[i * 2 + 1]);
return out;
}
/* 闭合轮廓简化:首尾重合会让 RDP 退化(所有点到弦的距离都是 0),
* 所以先在离起点最远处切成两条开链,分别简化后再接起来。 */
function simplify(pts, eps) {
const n = pts.length / 2;
if (n <= 3) return Array.from(pts);
const closed = (Math.abs(pts[0] - pts[(n - 1) * 2]) < 1e-6 &&
Math.abs(pts[1] - pts[(n - 1) * 2 + 1]) < 1e-6);
const m = closed ? n - 1 : n;
if (m <= 3) return Array.from(pts.slice(0, m * 2));
if (!closed) return rdp(pts, 0, m - 1, eps);
const x0 = pts[0], y0 = pts[1];
let far = 1, farD = -1;
for (let i = 1; i < m; i++) {
const d = (pts[i * 2] - x0) * (pts[i * 2] - x0) + (pts[i * 2 + 1] - y0) * (pts[i * 2 + 1] - y0);
if (d > farD) { farD = d; far = i; }
}
if (far <= 0 || far >= m - 1) return Array.from(pts.slice(0, m * 2));
const A1 = rdp(pts, 0, far, eps);
const B1 = rdp(pts, far, m - 1, eps);
const out = A1.slice();
for (let i = 2; i < B1.length; i += 2) out.push(B1[i], B1[i + 1]);
return out;
}
/* ---------------------------------------------------------------- 采样 / 取色 */
function resample(P, K, out) {
const m = P.length / 2 | 0;
if (m < 2) {
// 只有一个点(或没有):退化成重复该点
out.length = 0;
const x0 = m ? P[0] : 0, y0 = m ? P[1] : 0;
for (let q = 0; q < K; q++) out.push(x0, y0);
return 0;
}
const seg = new Float32Array(m);
let total = 0;
for (let i = 0; i < m; i++) {
const j = Math.min(i + 1, m - 1);
const dx = P[j * 2] - P[i * 2], dy = P[j * 2 + 1] - P[i * 2 + 1];
const d = Math.hypot(dx, dy);
seg[i] = d; total += d;
}
if (total < 1e-6) { out.length = 0; for (let q = 0; q < K; q++) out.push(P[0], P[1]); return total; }
const cum = new Float32Array(m);
let acc = 0;
for (let i = 0; i < m; i++) { cum[i] = acc; acc += seg[i]; }
out.length = 0;
let j = 0;
for (let q = 0; q < K; q++) {
const target = (q / (K - 1)) * total;
while (j < m - 2 && cum[j + 1] < target) j++;
const t0 = cum[j], t1 = (j + 1 < m) ? cum[j + 1] : total;
const f = Math.max(0, Math.min(1, (target - t0) / Math.max(t1 - t0, 1e-9)));
out.push(P[j * 2] + (P[(j + 1) * 2] - P[j * 2]) * f,
P[j * 2 + 1] + (P[(j + 1) * 2 + 1] - P[j * 2 + 1]) * f);
}
return total;
}
function makePainter(A, rgba, W, H, seed) {
let st = seed || 1;
const rnd = () => { st = (st * 1103515245 + 12345) & 0x7fffffff; return st / 0x7fffffff; };
function sampleA(field, x, y) {
const ax = clamp(Math.round(x / A.sxa), 0, A.AW - 1);
const ay = clamp(Math.round(y / A.sya), 0, A.AH - 1);
return field[ay * A.AW + ax];
}
function sampleRGB(x, y) {
const xi = clamp(Math.round(x), 0, W - 1), yi = clamp(Math.round(y), 0, H - 1);
const i = (yi * W + xi) * 4;
return [rgba[i], rgba[i + 1], rgba[i + 2]];
}
function lum(x, y) {
const c = sampleRGB(x, y);
return c[0] * 0.299 + c[1] * 0.587 + c[2] * 0.114;
}
const hex = (r, g, b) => '#' +
Math.round(clamp(r, 0, 255)).toString(16).padStart(2, '0') +
Math.round(clamp(g, 0, 255)).toString(16).padStart(2, '0') +
Math.round(clamp(b, 0, 255)).toString(16).padStart(2, '0');
/* 沿方向场行走,返回扁平折线。
* opt:
* breakLayer 是否在跨分层时止笔(默认 true)
* colorTol 跨色容差,999 = 不限制
* rot90 方向场旋转 90°(交叉排线)
* requireEdge + edgeField 只在边缘上走,走偏了向法线方向重新吸附
* maxTurn 每步最大转角,压住「打卷」
* angle/jitter 锁定方向(排线)
*/
function trace(x0, y0, len, layer, steps, opt) {
opt = opt || {};
const breakLayer = opt.breakLayer !== false;
const colorTol = opt.colorTol === undefined ? 120 : opt.colorTol;
const rot = opt.rot90 ? Math.PI / 2 : 0;
const reqEdge = opt.requireEdge;
const edgeF = opt.edgeField || A.eFine;
const maxTurn = opt.maxTurn;
const lockAng = opt.angle;
const jerk = opt.jitter || 0;
const P = [x0, y0];
let px = x0, py = y0;
const base = sampleRGB(px, py);
let hx, hy;
{
const a0 = (lockAng !== undefined)
? lockAng + (rnd() * 2 - 1) * jerk
: Math.atan2(sampleA(A.DY, px, py), sampleA(A.DX, px, py)) + rot;
hx = Math.cos(a0); hy = Math.sin(a0);
}
const step = Math.max(1.2, len / Math.max(2, steps));
for (let s = 0; s < steps; s++) {
let fx, fy;
if (lockAng !== undefined) {
fx = Math.cos(lockAng); fy = Math.sin(lockAng);
} else {
const th = Math.atan2(sampleA(A.DY, px, py), sampleA(A.DX, px, py)) + rot;
fx = Math.cos(th); fy = Math.sin(th);
}
if (fx * hx + fy * hy < 0) { fx = -fx; fy = -fy; }
// 方向场与惯性混合,避免方向场抖动直接传到笔迹上
let vx = 0.58 * hx + 0.42 * fx, vy = 0.58 * hy + 0.42 * fy;
let mg = Math.hypot(vx, vy) + 1e-9;
vx /= mg; vy /= mg;
// 限制转角:把新方向朝上一方向拉回来
if (maxTurn !== undefined) {
const prev = Math.atan2(hy, hx);
let da = Math.atan2(vy, vx) - prev;
while (da > Math.PI) da -= Math.PI * 2;
while (da < -Math.PI) da += Math.PI * 2;
if (da > maxTurn) da = maxTurn;
else if (da < -maxTurn) da = -maxTurn;
vx = Math.cos(prev + da); vy = Math.sin(prev + da);
}
let nx = px + vx * step, ny = py + vy * step;
if (nx < 1 || ny < 1 || nx >= W - 1 || ny >= H - 1) break;
// 走偏边缘时,沿法线方向吸附回脊线
if (reqEdge && sampleA(edgeF, nx, ny) < reqEdge) {
const pxp = -vy, pyp = vx;
let bestX = 0, bestY = 0, bestV = -1;
for (const d of [1, -1, 2, -2, 0.6, -0.6, 1.4, -1.4]) {
const tx = px + pxp * d, ty = py + pyp * d;
const v = sampleA(edgeF, tx, ty);
if (v > bestV) { bestV = v; bestX = tx; bestY = ty; }
}
if (bestV < reqEdge) break;
nx = bestX; ny = bestY;
}
if (breakLayer && sampleA(A.labels, nx, ny) !== layer) break;
if (colorTol < 999) {
const c2 = sampleRGB(nx, ny);
if (Math.abs(c2[0] - base[0]) + Math.abs(c2[1] - base[1]) + Math.abs(c2[2] - base[2]) > colorTol) break;
}
px = nx; py = ny;
P.push(px, py);
const nvx = px - P[P.length - 4], nvy = py - P[P.length - 3];
const nd = Math.hypot(nvx, nvy) + 1e-9;
hx = nvx / nd; hy = nvy / nd;
}
return P;
}
const Q = [];
function pickColor(Qpts, K, desat, tint, tintAmt) {
const idxs = [0, Math.max(1, K >> 2), K >> 1, Math.max(1, (3 * K) >> 2), K - 1];
const acc = [[], [], []];
for (const q of idxs) {
const c = sampleRGB(Qpts[q * 2], Qpts[q * 2 + 1]);
acc[0].push(c[0]); acc[1].push(c[1]); acc[2].push(c[2]);
}
const med = acc.map(a => { a.sort((x, y) => x - y); return a[a.length >> 1]; });
let r = med[0], g = med[1], b = med[2];
if (desat > 0) {
const gy = 0.299 * r + 0.587 * g + 0.114 * b;
r = r * (1 - desat) + gy * desat; g = g * (1 - desat) + gy * desat; b = b * (1 - desat) + gy * desat;
}
if (tint && tintAmt > 0) {
r = r * (1 - tintAmt) + tint[0] * tintAmt;
g = g * (1 - tintAmt) + tint[1] * tintAmt;
b = b * (1 - tintAmt) + tint[2] * tintAmt;
}
const j = 1 + (rnd() * 2 - 1) * 0.03;
return hex(r * j, g * j, b * j);
}
/* 沿方向场取色生成笔触(油画主力) */
function make(sx, sy, len, wid, lay, steps, detail, kind, alpha, desat, tint, tintAmt, tag, opt) {
const out = [];
for (let i = 0; i < sx.length; i++) {
const P = trace(sx[i], sy[i], len[i], lay[i], steps, opt);
const np = P.length / 2;
if (np < 2) continue;
let arcLen = 0;
for (let q = 1; q < np; q++) arcLen += Math.hypot(P[q * 2] - P[(q - 1) * 2], P[q * 2 + 1] - P[(q - 1) * 2 + 1]);
const K = clamp(Math.ceil(arcLen / 7), 3, 12);
const arc = resample(P, K, Q);
if (Q.length < 4) continue;
const c = pickColor(Q, Q.length / 2, desat || 0, tint, tintAmt || 0);
let d = 'M' + Q[0].toFixed(1) + ' ' + Q[1].toFixed(1);
for (let q = 1; q < Q.length / 2; q++) d += 'L' + Q[q * 2].toFixed(1) + ' ' + Q[q * 2 + 1].toFixed(1);
out.push({
d, c, w: +wid[i].toFixed(2), a: +alpha.toFixed(3), kind,
len: +arc.toFixed(2), layer: lay[i] | 0, detail: detail ? 1 : 0, tag: tag || '',
});
}
return out;
}
/* 颜色由回调直接给定(线描 / 素描) */
function makeRaw(sx, sy, len, wid, lay, steps, detail, kind, alpha, colorFn, tag, opt) {
const out = [];
for (let i = 0; i < sx.length; i++) {
const P = trace(sx[i], sy[i], len[i], lay[i], steps, opt);
if (P.length < 4) continue;
const K = clamp(Math.ceil(len[i] / 6), 3, 10);
const Q2 = [];
const arc = resample(P, K, Q2);
if (Q2.length < 4) continue;
const c = colorFn(Q2, i);
if (!c) continue;
let d = 'M' + Q2[0].toFixed(1) + ' ' + Q2[1].toFixed(1);
for (let q = 1; q < Q2.length / 2; q++) d += 'L' + Q2[q * 2].toFixed(1) + ' ' + Q2[q * 2 + 1].toFixed(1);
out.push({
d, c, w: +wid[i].toFixed(2), a: +alpha.toFixed(3), kind,
len: +arc.toFixed(2), layer: lay[i] | 0, detail: detail ? 1 : 0, tag: tag || '',
});
}
return out;
}
function grid(sp, jit, rect) {
const [x0, y0, x1, y1] = rect || [0, 0, W, H];
const X = [], Y = [];
for (let y = y0 + sp * 0.5; y < y1; y += sp) {
for (let x = x0 + sp * 0.5; x < x1; x += sp) {
const px = x + (rnd() * 2 - 1) * jit * sp;
const py = y + (rnd() * 2 - 1) * jit * sp;
if (px > 1 && px < W - 2 && py > 1 && py < H - 2) { X.push(px); Y.push(py); }
}
}
return { X, Y };
}
function edgeSeeds(rect, thr, sp, field) {
const [x0, y0, x1, y1] = rect || [0, 0, W, H];
const f = field || A.eFine;
const X = [], Y = [];
const seen = new Set();
const spx = Math.max(1, sp);
for (let y = y0; y < y1; y += 1) {
for (let x = x0; x < x1; x += 1) {
if (sampleA(f, x, y) < 0.5) continue;
const key = ((x / spx) | 0) * 200003 + ((y / spx) | 0);
if (seen.has(key)) continue;
seen.add(key);
const px = x + (rnd() * 2 - 1) * 0.6, py = y + (rnd() * 2 - 1) * 0.6;
if (px > 1 && px < W - 2 && py > 1 && py < H - 2) { X.push(px); Y.push(py); }
}
}
return { X, Y };
}
return { make, makeRaw, grid, edgeSeeds, sampleA, sampleRGB, lum, hex, rnd, trace };
}
/* ---------------------------------------------------------------- 底色推导 */
function groundColor(P, W, H, opts) {
opts = opts || {};
const mode = opts.mode || 'dark';
const darkFrac = opts.darkFrac === undefined ? 0.28 : opts.darkFrac;
const hist = new Uint32Array(256);
for (let y = 0; y < H; y += 3) {
for (let x = 0; x < W; x += 3) {
hist[clamp(P.lum(x, y) | 0, 0, 255)]++;
}
}
let tot = 0; for (let i = 0; i < 256; i++) tot += hist[i];
let need = tot * darkFrac, th = 255;
for (let i = 0; i < 256; i++) { need -= hist[i]; if (need <= 0) { th = i; break; } }
let r = 0, g = 0, b = 0, n = 0;
for (let y = 0; y < H; y += 2) {
for (let x = 0; x < W; x += 2) {
const c = P.sampleRGB(x, y);
if (c[0] * 0.299 + c[1] * 0.587 + c[2] * 0.114 > th) continue;
r += c[0]; g += c[1]; b += c[2]; n++;
}
}
if (!n) return [40, 34, 28];
r /= n; g /= n; b /= n;
if (mode === 'warm') { r *= 0.86; g *= 0.98; b = Math.min(255, b * 1.04); }
if (mode === 'dim') { r *= 0.55; g *= 0.55; b *= 0.58; }
return [r, g, b];
}
/* ================================================================ 风格一:油画
* 十道古典工序,笔触带起收锋、厚涂、罩染、干扫。
*/
const OIL_STAGE = [
['imprimatura', '刷底色', 3.5],
['underdrawing', '起稿', 5.0],
['grisaille', '单色底层', 8.0],
['block-in', '铺大色', 7.0],
['massing', '塑造', 12.0],
['modelling', '塑形', 16.0],
['glazing', '罩染', 5.0],
['scumbling', '干扫', 4.0],
['detail', '细部', 26.0],
['impasto', '提白', 3.5],
];
function buildOil(A, rgba, W, H, opts, onStage) {
const P = makePainter(A, rgba, W, H, opts.seed || 20261109);
const base = Math.max(W, H) / 1527;
const s = (opts.scale || 1) * Math.max(0.45, base);
const stages = [];
const WT = {}, LB = {};
OIL_STAGE.forEach(([n, l, w]) => { WT[n] = w; LB[n] = l; });
/* 笔触预算:细腻程度拉满时笔触会到 20 万条 / 22MB,手机端扛不住。
* 按工序权重等比稀释,保持「大笔在前、细笔在后」的比例关系。 */
const OIL_MAX = 130000;
const oilW = OIL_STAGE.reduce((a, x) => a + x[2], 0);
const push = (name, ss) => {
const allow = Math.max(600, Math.round(OIL_MAX * (WT[name] || 5) / oilW));
let kept = ss;
if (ss.length > allow) {
const stride = ss.length / allow;
kept = [];
for (let i = 0; i < allow; i++) kept.push(ss[Math.min(ss.length - 1, Math.floor(i * stride))]);
}
stages.push({ name, label: LB[name] || name, weight: WT[name] || 5, strokes: kept });
if (onStage) onStage(name, kept.length, LB[name] || name);
};
const GB = groundColor(P, W, H, { mode: 'warm', darkFrac: 0.28 });
// 1 刷底色
{
const X = [], Y = [], L = [], Wd = [], Lay = [];
const rowH = 20 * s;
for (let y = 9 * s; y < H - 8; y += rowH) {
let x = 3;
while (x < W - 40 * s) {
const x1 = Math.min(x + (140 + P.rnd() * 80) * s, W - 4);
if (x1 - x < 20 * s) break;
const n = Math.max(3, Math.ceil((x1 - x) / (40 * s)) + 1);
for (let k = 0; k < n; k++) {
const xx = x + (x1 - x) * k / (n - 1);
X.push(xx); Y.push(y + (P.rnd() * 2 - 1) * 3 * s);
L.push(x1 - x); Wd.push((21 + P.rnd() * 9) * s);
Lay.push(P.sampleA(A.labels, xx, y));
}
x = x1 - 12 * s;
if (x1 >= W - 4) break;
}
}
push('imprimatura', P.make(X, Y, L, Wd, Lay, 4, 0, KIND_NORMAL, 1.0, 0, GB, 0.55, 'base'));
}
// 2 起稿
{
const X = [], Y = [], L = [], Wd = [], Lay = [];
const sp = 8 * s, seen = new Set();
for (let y = 1; y < A.AH - 1; y += 1) {
for (let x = 1; x < A.AW - 1; x += 1) {
if (A.eMajor[y * A.AW + x] < 1) continue;
if (P.rnd() > 0.5) continue;
const fx = x * A.sxa, fy = y * A.sya;
const key = ((fx / sp) | 0) * 200000 + ((fy / sp) | 0);
if (seen.has(key)) continue;
seen.add(key);
X.push(fx); Y.push(fy);
L.push((10 + P.rnd() * 16) * s); Wd.push((0.9 + P.rnd() * 0.8) * s);
Lay.push(P.sampleA(A.labels, fx, fy));
}
}
push('underdrawing', P.make(X, Y, L, Wd, Lay, 4, 0, KIND_NORMAL, 0.55, 0, [58, 44, 34], 0.72, 'under'));
}
// 3 单色底层
{
const all = [];
for (const [sp, w0, w1, l0, l1] of [[42, 20, 30, 50, 92], [25, 12, 20, 28, 56]]) {
const g = P.grid(sp * s, 0.5);
const L = [], Wd = [], Lay = [];
for (let i = 0; i < g.X.length; i++) {
const c = P.sampleA(A.coh, g.X[i], g.Y[i]);
L.push((l0 + P.rnd() * (l1 - l0)) * (0.6 + 0.7 * c) * s);
Wd.push((w0 + P.rnd() * (w1 - w0)) * s);
Lay.push(P.sampleA(A.labels, g.X[i], g.Y[i]));
}
appendAll(all, P.make(g.X, g.Y, L, Wd, Lay, 5, 0, KIND_NORMAL, 0.8, 0.72, [70, 58, 46], 0.34, 'gris'));
}
push('grisaille', all);
}
// 4 铺大色
{
const all = [];
for (const [sp, w0, w1] of [[40, 15, 28], [24, 10, 19]]) {
const g = P.grid(sp * s, 0.5);
const L = [], Wd = [], Lay = [];
for (let i = 0; i < g.X.length; i++) {
const c = P.sampleA(A.coh, g.X[i], g.Y[i]);
L.push((46 + P.rnd() * 40) * (0.6 + 0.7 * c) * s);
Wd.push((w0 + P.rnd() * (w1 - w0)) * s);
Lay.push(P.sampleA(A.labels, g.X[i], g.Y[i]));
}
appendAll(all, P.make(g.X, g.Y, L, Wd, Lay, 5, 0, KIND_NORMAL, 1.0, 0.04, null, 0, 'block'));
}
push('block-in', all);
}
// 5 塑造
{
const all = [];
for (const [sp, w0, w1, l0, l1, smin] of [[24, 14, 19, 26, 50, 0.03], [18, 8, 13, 20, 40, 0.05], [14, 6, 9, 16, 34, 0.07]]) {
const g = P.grid(sp * s, 0.5);
const X = [], Y = [], L = [], Wd = [], Lay = [];
for (let i = 0; i < g.X.length; i++) {
if (P.sampleA(A.sal, g.X[i], g.Y[i]) < smin) continue;
const c = P.sampleA(A.coh, g.X[i], g.Y[i]);
X.push(g.X[i]); Y.push(g.Y[i]);
L.push((l0 + P.rnd() * (l1 - l0)) * (0.6 + 0.7 * c) * s);
Wd.push((w0 + P.rnd() * (w1 - w0)) * s);
Lay.push(P.sampleA(A.labels, g.X[i], g.Y[i]));
}
appendAll(all, P.make(X, Y, L, Wd, Lay, 5, 0, KIND_NORMAL, 1.0, 0, null, 0, 'mass'));
}
push('massing', all);
}
// 6 塑形
{
const all = [];
for (const [sp, w0, w1, l0, l1, amin] of [[13.5, 4.5, 7, 12, 26, 0.10], [10, 3.2, 5, 9, 20, 0.14], [8, 2.6, 4, 7, 16, 0.18]]) {
const g = P.grid(sp * s, 0.5);
const X = [], Y = [], L = [], Wd = [], Lay = [];
for (let i = 0; i < g.X.length; i++) {
if (P.sampleA(A.sal, g.X[i], g.Y[i]) < amin) continue;
const c = P.sampleA(A.coh, g.X[i], g.Y[i]);
X.push(g.X[i]); Y.push(g.Y[i]);
L.push((l0 + P.rnd() * (l1 - l0)) * (0.6 + 0.7 * c) * s);
Wd.push((w0 + P.rnd() * (w1 - w0)) * s);
Lay.push(P.sampleA(A.labels, g.X[i], g.Y[i]));
}
appendAll(all, P.make(X, Y, L, Wd, Lay, 5, 0, KIND_NORMAL, 0.94, 0, null, 0, 'mod'));
}
push('modelling', all);
}
// 7 罩染:只罩最暗 30%
{
const lum = [];
for (let y = 0; y < H; y += 5) for (let x = 0; x < W; x += 5) lum.push(P.lum(x, y));
lum.sort((a, b) => a - b);
const thr = lum[Math.floor(lum.length * 0.30)];
const g = P.grid(30 * s, 0.6);
const X = [], Y = [], L = [], Wd = [], Lay = [];
for (let i = 0; i < g.X.length; i++) {
if (P.lum(g.X[i], g.Y[i]) > thr) continue;
if (P.sampleA(A.sal, g.X[i], g.Y[i]) < 0.04) continue;
const cc = P.sampleA(A.coh, g.X[i], g.Y[i]);
X.push(g.X[i]); Y.push(g.Y[i]);
L.push((40 + P.rnd() * 42) * (0.6 + 0.7 * cc) * s);
Wd.push((14 + P.rnd() * 12) * s);
Lay.push(P.sampleA(A.labels, g.X[i], g.Y[i]));
}
push('glazing', P.make(X, Y, L, Wd, Lay, 5, 0, KIND_GLAZE, 0.22, 0.05, null, 0, 'glaze'));
}
// 8 干扫:亮部 + 低饱和 + 低结构
{
const lum = [];
for (let y = 0; y < H; y += 5) for (let x = 0; x < W; x += 5) lum.push(P.lum(x, y));
lum.sort((a, b) => a - b);
const thr = lum[Math.floor(lum.length * 0.68)];
const g = P.grid(28 * s, 0.5);
const X = [], Y = [], L = [], Wd = [], Lay = [];
for (let i = 0; i < g.X.length; i++) {
const c = P.sampleRGB(g.X[i], g.Y[i]);
const gv = c[0] * 0.299 + c[1] * 0.587 + c[2] * 0.114;
if (gv < thr) continue;
const mx = Math.max(c[0], c[1], c[2]), mn = Math.min(c[0], c[1], c[2]);
if (mx > 0 && ((mx - mn) / mx) * 100 > 32) continue;
// 干扫只用于结构弱的区域;脸部虽有亮度但结构明确,必须排除,否则糊一层白线
if (P.sampleA(A.sal, g.X[i], g.Y[i]) > 0.30) continue;
if (P.sampleA(A.coh, g.X[i], g.Y[i]) > 0.45) continue;
const cc = P.sampleA(A.coh, g.X[i], g.Y[i]);
X.push(g.X[i]); Y.push(g.Y[i]);
L.push((34 + P.rnd() * 38) * (0.6 + 0.7 * cc) * s);
Wd.push((9 + P.rnd() * 9) * s);
Lay.push(P.sampleA(A.labels, g.X[i], g.Y[i]));
}
push('scumbling', P.make(X, Y, L, Wd, Lay, 5, 0, KIND_SCUMBLE, 0.20, 0, null, 0, 'scum'));
}
// 9 细部:自动 ROI + 三遍
{
const rois = opts.rois && opts.rois.length ? opts.rois : autoROIs(A, opts.autoRois === undefined ? 6 : opts.autoRois);
const det = [];
for (const r of rois) {
const g = P.grid(3.4 * s, 0.55, r);
const X = [], Y = [], L = [], Wd = [], Lay = [];
for (let i = 0; i < g.X.length; i++) {
if (P.sampleA(A.sal, g.X[i], g.Y[i]) < 0.06) continue;
const c = P.sampleA(A.coh, g.X[i], g.Y[i]);
X.push(g.X[i]); Y.push(g.Y[i]);
L.push((0.45 + P.rnd() * 0.55) * 9.5 * (0.7 + 0.45 * c) * s);
Wd.push((0.85 + P.rnd() * 0.85) * s);
Lay.push(P.sampleA(A.labels, g.X[i], g.Y[i]));
}
appendAll(det, P.make(X, Y, L, Wd, Lay, 4, 1, KIND_NORMAL, 1.0, 0, null, 0, 'det'));
}
for (const r of rois) {
const g = P.grid(5.6 * s, 0.5, [r[0] + 1.7 * s, r[1] + 1.7 * s, r[2], r[3]]);
const X = [], Y = [], L = [], Wd = [], Lay = [];
for (let i = 0; i < g.X.length; i++) {
if (P.sampleA(A.sal, g.X[i], g.Y[i]) < 0.10) continue;
const c = P.sampleA(A.coh, g.X[i], g.Y[i]);
X.push(g.X[i]); Y.push(g.Y[i]);
L.push((0.5 + P.rnd() * 0.5) * 6.0 * (0.7 + 0.45 * c) * s);
Wd.push((0.9 + P.rnd() * 0.9) * s);
Lay.push(P.sampleA(A.labels, g.X[i], g.Y[i]));
}
appendAll(det, P.make(X, Y, L, Wd, Lay, 4, 1, KIND_NORMAL, 1.0, 0, null, 0, 'det2'));
}
for (const r of rois.slice(0, 5)) {
const e = P.edgeSeeds(r, 0.5, 2.6 * s);
const L = [], Wd = [], Lay = [];
for (let i = 0; i < e.X.length; i++) {
const c = P.sampleA(A.coh, e.X[i], e.Y[i]);
L.push((0.5 + P.rnd() * 0.5) * 6.0 * (0.75 + 0.4 * c) * s);
Wd.push((0.8 + P.rnd() * 0.6) * s);
Lay.push(P.sampleA(A.labels, e.X[i], e.Y[i]));
}
appendAll(det, P.make(e.X, e.Y, L, Wd, Lay, 3, 1, KIND_NORMAL, 1.0, 0, null, 0, 'micro'));
}
push('detail', det);
}
// 10 提白
{
const X = [], Y = [], L = [], Wd = [], Lay = [];
const seen = new Set();
const step = Math.max(4, Math.floor(Math.max(W, H) / 240));
for (let y = 6; y < H - 6; y += step) {
for (let x = 6; x < W - 6; x += step) {
const g = P.lum(x, y);