@xenra/dsh-draworld 0.1.40 → 0.1.46
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +86 -12
- package/client.js +437 -97
- package/index.js +1026 -71
- package/package.json +1 -1
- package/pixelops.js +1288 -0
- package/pixels.js +182 -19
package/pixelops.js
CHANGED
|
@@ -11,6 +11,1184 @@ import {
|
|
|
11
11
|
|
|
12
12
|
const dataUrlOf = (surf) => `data:image/png;base64,${encodePng(surf.data, surf.w, surf.h).toString("base64")}`;
|
|
13
13
|
|
|
14
|
+
/** 某点的亮度(0~255),越界就夹回边缘。 */
|
|
15
|
+
function lumAt(data, w, h, x, y) {
|
|
16
|
+
const xi = Math.max(0, Math.min(w - 1, Math.round(x)));
|
|
17
|
+
const yi = Math.max(0, Math.min(h - 1, Math.round(y)));
|
|
18
|
+
const i = (yi * w + xi) * 4;
|
|
19
|
+
return 0.299 * data[i] + 0.587 * data[i + 1] + 0.114 * data[i + 2];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// ------------------------------------------------------------------ 笔触复刻管线
|
|
23
|
+
/**
|
|
24
|
+
* 复刻的五步(0.1.42):不再是「按网格采样、每格铺一根短线」,而是按画师的次序分层。
|
|
25
|
+
*
|
|
26
|
+
* ① 铺底:k-means 把参考图并成 4~6 个大色区,用**大色块 fill** 铺满剪影
|
|
27
|
+
* (十几个多边形,不是几千笔)——先有「画完七成」的骨架,也彻底堵住白底露缝。
|
|
28
|
+
* ② 塑形:沿**结构张量**方向场铺中大笔触,跨 2~4 格、宽而半透明,
|
|
29
|
+
* 笔迹顺着形体走向弯,而不是沿像素网格横平竖直地扫。
|
|
30
|
+
* ③ 过渡:只在梯度带(明暗交界)补中间色,冷暖两色都从原图采,隔格交替。
|
|
31
|
+
* ④ 收边:拿色阶边界 trace 出的轮廓落**短线**,短、硬、沿切线方向——这一步决定画面干不干净。
|
|
32
|
+
* ⑤ 点睛:只在最亮、对比最强的地方点极小的高光笔,笔数少但视觉权重最大。
|
|
33
|
+
*
|
|
34
|
+
* 五步各成一层(可单独显隐/重跑),笔刷由大到小。方向场是结构张量的主轴旋转 90°:
|
|
35
|
+
* 张量主轴在边缘处垂直于边缘,旋转后正好平行于边缘;在平缓的渐变区给出形体的流动方向。
|
|
36
|
+
* 它不是亮度梯度本身,所以不会在明暗交界处横着甩笔。
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
const PAINT_STAGES = [
|
|
40
|
+
{ key: "base", name: "固有色平铺", kind: "fill" },
|
|
41
|
+
{ key: "block", name: "一级暗部", kind: "brush", brush: "hard" },
|
|
42
|
+
{ key: "blend", name: "二级暗部", kind: "brush", brush: "softround" },
|
|
43
|
+
{ key: "edge", name: "精线稿", kind: "brush", brush: "liner" },
|
|
44
|
+
{ key: "accent", name: "高光", kind: "brush", brush: "glow" },
|
|
45
|
+
{ key: "sketch", name: "构图草稿", kind: "brush", brush: "pencil" },
|
|
46
|
+
{ key: "lineColor", name: "线稿上色", kind: "brush", brush: "liner" },
|
|
47
|
+
{ key: "occlusion", name: "闭塞线", kind: "brush", brush: "hard" },
|
|
48
|
+
{ key: "lightblock", name: "铺光大关系", kind: "brush", brush: "softround" },
|
|
49
|
+
{ key: "texture", name: "材质纹理", kind: "brush", brush: "pencil" },
|
|
50
|
+
{ key: "iridescent", name: "镭射反光", kind: "brush", brush: "glow" },
|
|
51
|
+
{ key: "atmosphere", name: "氛围光效", kind: "brush", brush: "airbrush" },
|
|
52
|
+
{ key: "grade", name: "调色调整", kind: "grade" },
|
|
53
|
+
];
|
|
54
|
+
const PAINT_STAGE_KEYS = PAINT_STAGES.map((s) => s.key);
|
|
55
|
+
|
|
56
|
+
const hex2 = (v) => Math.max(0, Math.min(255, Math.round(v))).toString(16).padStart(2, "0");
|
|
57
|
+
const rgbLum = (r, g, b) => 0.299 * r + 0.587 * g + 0.114 * b;
|
|
58
|
+
const rgbHex = (r, g, b) => `#${hex2(r)}${hex2(g)}${hex2(b)}`;
|
|
59
|
+
|
|
60
|
+
function hexLum(hex) {
|
|
61
|
+
const s = String(hex || "").replace("#", "");
|
|
62
|
+
if (s.length < 6) return 255;
|
|
63
|
+
return rgbLum(parseInt(s.slice(0, 2), 16), parseInt(s.slice(2, 4), 16), parseInt(s.slice(4, 6), 16));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** 颜色色度(两条对立色轴):两条颜色只差色相时 ΔL 很小,但 Δchroma 会明显。 */
|
|
67
|
+
function chromaOf(r, g, b) {
|
|
68
|
+
return [r - g, (r + g) / 2 - b];
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** "base,block,edge" → Set;空值给全部五步。 */
|
|
72
|
+
function parsePaintStages(value) {
|
|
73
|
+
const raw = Array.isArray(value) ? value.join(",") : String(value || "");
|
|
74
|
+
const words = raw.toLowerCase().split(/[,\s|]+/).filter(Boolean);
|
|
75
|
+
if (words.length === 0) return new Set(PAINT_STAGE_KEYS);
|
|
76
|
+
const aliases = {
|
|
77
|
+
under: "base", base: "base", fill: "base",
|
|
78
|
+
block: "block", shape: "block", shadow1: "block",
|
|
79
|
+
blend: "blend", transition: "blend", shadow2: "blend",
|
|
80
|
+
edge: "edge", line: "edge", outline: "edge",
|
|
81
|
+
accent: "accent", highlight: "accent", light: "accent",
|
|
82
|
+
sketch: "sketch", draft: "sketch",
|
|
83
|
+
linecolor: "lineColor", colorline: "lineColor",
|
|
84
|
+
occlusion: "occlusion", ao: "occlusion",
|
|
85
|
+
lightblock: "lightblock", lighting: "lightblock",
|
|
86
|
+
texture: "texture", grain: "texture",
|
|
87
|
+
iridescent: "iridescent", rainbow: "iridescent",
|
|
88
|
+
atmosphere: "atmosphere", ambient: "atmosphere",
|
|
89
|
+
grade: "grade", tone: "grade",
|
|
90
|
+
};
|
|
91
|
+
const out = new Set();
|
|
92
|
+
for (const word of words) {
|
|
93
|
+
const key = aliases[word] || word;
|
|
94
|
+
if (PAINT_STAGE_KEYS.includes(key)) out.add(key);
|
|
95
|
+
}
|
|
96
|
+
return out.size ? out : new Set(PAINT_STAGE_KEYS);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* 按「空间分桶 + 桶内按分数」抽稀:重要区域(脸、手、飘带)不会被整体稀释,
|
|
101
|
+
* 同时保证每个分桶至少留 2 笔,不会出现整块空白。
|
|
102
|
+
*/
|
|
103
|
+
function pickWeighted(cands, cap, w, h, cols = 8, rows = 8) {
|
|
104
|
+
if (cands.length <= cap) return cands;
|
|
105
|
+
const buckets = Array.from({ length: cols * rows }, () => []);
|
|
106
|
+
for (const c of cands) {
|
|
107
|
+
const bx = Math.max(0, Math.min(cols - 1, Math.floor((c.x / w) * cols)));
|
|
108
|
+
const by = Math.max(0, Math.min(rows - 1, Math.floor((c.y / h) * rows)));
|
|
109
|
+
buckets[by * cols + bx].push(c);
|
|
110
|
+
}
|
|
111
|
+
const weights = buckets.map((list) => {
|
|
112
|
+
let sum = 0;
|
|
113
|
+
for (const c of list) sum += c.score;
|
|
114
|
+
return sum + list.length * 0.5;
|
|
115
|
+
});
|
|
116
|
+
const totalWeight = weights.reduce((a, b) => a + b, 0) || 1;
|
|
117
|
+
const quotas = weights.map((wt, i) => {
|
|
118
|
+
const ceil = Math.min(buckets[i].length, 2);
|
|
119
|
+
return Math.max(ceil, Math.round((cap * wt) / totalWeight));
|
|
120
|
+
});
|
|
121
|
+
const out = [];
|
|
122
|
+
for (let i = 0; i < buckets.length; i += 1) {
|
|
123
|
+
if (buckets[i].length === 0) continue;
|
|
124
|
+
buckets[i].sort((a, b) => b.score - a.score);
|
|
125
|
+
out.push(...buckets[i].slice(0, Math.min(quotas[i], buckets[i].length)));
|
|
126
|
+
}
|
|
127
|
+
if (out.length > cap) {
|
|
128
|
+
out.sort((a, b) => b.score - a.score);
|
|
129
|
+
out.length = cap;
|
|
130
|
+
} else if (out.length < cap) {
|
|
131
|
+
const chosen = new Set(out);
|
|
132
|
+
const rest = cands.filter((c) => !chosen.has(c)).sort((a, b) => b.score - a.score);
|
|
133
|
+
out.push(...rest.slice(0, cap - out.length));
|
|
134
|
+
}
|
|
135
|
+
return out;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** 把落笔在局部坐标里的笔迹两端、落在纯白背景上的点剪掉。 */
|
|
139
|
+
function trimOnWhite(points, d, w, h, cut = 248) {
|
|
140
|
+
const onWhite = points.map((p) => lumAt(d, w, h, p[0], p[1]) >= cut);
|
|
141
|
+
let bestStart = -1;
|
|
142
|
+
let bestLen = 0;
|
|
143
|
+
let start = -1;
|
|
144
|
+
for (let i = 0; i <= points.length; i += 1) {
|
|
145
|
+
const solid = i < points.length && !onWhite[i];
|
|
146
|
+
if (solid && start < 0) start = i;
|
|
147
|
+
if (!solid && start >= 0) {
|
|
148
|
+
const len = i - start;
|
|
149
|
+
if (len > bestLen) {
|
|
150
|
+
bestLen = len;
|
|
151
|
+
bestStart = start;
|
|
152
|
+
}
|
|
153
|
+
start = -1;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return bestLen >= 2 ? points.slice(bestStart, bestStart + bestLen) : [];
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const toCanvas = (points, rect) => points.map((p) => [
|
|
160
|
+
Math.round(rect.x + p[0]),
|
|
161
|
+
Math.round(rect.y + p[1]),
|
|
162
|
+
p[2] === undefined ? undefined : Number(p[2].toFixed(3)),
|
|
163
|
+
]);
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* 取一格:白占比、非白主色(量化众数,不用均值——均值会把明暗混成泥)、
|
|
167
|
+
* 最亮/最暗像素、局部对比。stride 最大 2,避免细结构被跳采。
|
|
168
|
+
*/
|
|
169
|
+
function paintSample(d, w, h, cx, cy, size, whiteCut) {
|
|
170
|
+
const half = Math.max(1, Math.round(size / 2));
|
|
171
|
+
const stride = size >= 24 ? 2 : 1;
|
|
172
|
+
const buckets = new Map();
|
|
173
|
+
let n = 0, white = 0, lumSum = 0;
|
|
174
|
+
let maxLum = -1, minLum = 256, inkMaxLum = -1;
|
|
175
|
+
let bright = [255, 255, 255], dark = [0, 0, 0], inkBright = [255, 255, 255];
|
|
176
|
+
let meanR = 0, meanG = 0, meanB = 0;
|
|
177
|
+
for (let y = Math.round(cy) - half; y <= Math.round(cy) + half; y += stride) {
|
|
178
|
+
for (let x = Math.round(cx) - half; x <= Math.round(cx) + half; x += stride) {
|
|
179
|
+
if (x < 0 || y < 0 || x >= w || y >= h) continue;
|
|
180
|
+
const i = (y * w + x) * 4;
|
|
181
|
+
if (d[i + 3] < 16) continue;
|
|
182
|
+
const r = d[i], g = d[i + 1], b = d[i + 2];
|
|
183
|
+
const l = rgbLum(r, g, b);
|
|
184
|
+
n += 1;
|
|
185
|
+
lumSum += l;
|
|
186
|
+
meanR += r; meanG += g; meanB += b;
|
|
187
|
+
if (l > maxLum) {
|
|
188
|
+
maxLum = l;
|
|
189
|
+
bright = [r, g, b];
|
|
190
|
+
}
|
|
191
|
+
if (l < minLum) {
|
|
192
|
+
minLum = l;
|
|
193
|
+
dark = [r, g, b];
|
|
194
|
+
}
|
|
195
|
+
if (l >= whiteCut) {
|
|
196
|
+
white += 1;
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
const key = ((r >> 4) << 8) | ((g >> 4) << 4) | (b >> 4);
|
|
200
|
+
const bucket = buckets.get(key) || { r: 0, g: 0, b: 0, n: 0 };
|
|
201
|
+
bucket.r += r; bucket.g += g; bucket.b += b; bucket.n += 1;
|
|
202
|
+
buckets.set(key, bucket);
|
|
203
|
+
if (l > inkMaxLum) {
|
|
204
|
+
inkMaxLum = l;
|
|
205
|
+
inkBright = [r, g, b];
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
if (n === 0) return null;
|
|
210
|
+
let top = null;
|
|
211
|
+
for (const bucket of buckets.values()) {
|
|
212
|
+
if (!top || bucket.n > top.n) top = bucket;
|
|
213
|
+
}
|
|
214
|
+
const inkN = top ? top.n : 0;
|
|
215
|
+
const inkRgb = top ? [top.r / top.n, top.g / top.n, top.b / top.n] : [meanR / n, meanG / n, meanB / n];
|
|
216
|
+
return {
|
|
217
|
+
rgb: [meanR / n, meanG / n, meanB / n],
|
|
218
|
+
hex: rgbHex(meanR / n, meanG / n, meanB / n),
|
|
219
|
+
inkRgb,
|
|
220
|
+
inkHex: rgbHex(inkRgb[0], inkRgb[1], inkRgb[2]),
|
|
221
|
+
inkN,
|
|
222
|
+
inkFrac: inkN / n,
|
|
223
|
+
inkMaxLum,
|
|
224
|
+
inkBright,
|
|
225
|
+
lum: lumSum / n,
|
|
226
|
+
whiteFrac: white / n,
|
|
227
|
+
maxLum,
|
|
228
|
+
minLum,
|
|
229
|
+
bright,
|
|
230
|
+
dark,
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* 结构张量方向场:对 R/G/B 三个通道分别求梯度再累加 J,
|
|
236
|
+
* 这样白袍/皮肤这种「亮度平、色相差」的地方也有方向信号,不会 mag≈0。
|
|
237
|
+
* 方向按轴存储(cos2θ/sin2θ),插值时不会在 α 与 α+π 之间塌成零向量。
|
|
238
|
+
*/
|
|
239
|
+
function buildDirectionField(cut, grid) {
|
|
240
|
+
const { w, h, data } = cut;
|
|
241
|
+
const channels = [0, 1, 2].map(() => new Float32Array(w * h));
|
|
242
|
+
for (let p = 0, i = 0; p < w * h; p += 1, i += 4) {
|
|
243
|
+
if (data[i + 3] < 16) {
|
|
244
|
+
channels[0][p] = 255;
|
|
245
|
+
channels[1][p] = 255;
|
|
246
|
+
channels[2][p] = 255;
|
|
247
|
+
} else {
|
|
248
|
+
channels[0][p] = data[i];
|
|
249
|
+
channels[1][p] = data[i + 1];
|
|
250
|
+
channels[2][p] = data[i + 2];
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
const boxBlur = (src) => {
|
|
254
|
+
const out = new Float32Array(src.length);
|
|
255
|
+
for (let y = 0; y < h; y += 1) {
|
|
256
|
+
for (let x = 0; x < w; x += 1) {
|
|
257
|
+
let sum = 0;
|
|
258
|
+
let cnt = 0;
|
|
259
|
+
for (let dy = -1; dy <= 1; dy += 1) {
|
|
260
|
+
const yy = y + dy;
|
|
261
|
+
if (yy < 0 || yy >= h) continue;
|
|
262
|
+
for (let dx = -1; dx <= 1; dx += 1) {
|
|
263
|
+
const xx = x + dx;
|
|
264
|
+
if (xx < 0 || xx >= w) continue;
|
|
265
|
+
sum += src[yy * w + xx];
|
|
266
|
+
cnt += 1;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
out[y * w + x] = sum / cnt;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
return out;
|
|
273
|
+
};
|
|
274
|
+
for (let c = 0; c < 3; c += 1) {
|
|
275
|
+
let smooth = channels[c];
|
|
276
|
+
for (let pass = 0; pass < 2; pass += 1) smooth = boxBlur(smooth);
|
|
277
|
+
channels[c] = smooth;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const gw = Math.max(1, Math.ceil(w / grid));
|
|
281
|
+
const gh = Math.max(1, Math.ceil(h / grid));
|
|
282
|
+
const txx = new Float32Array(gw * gh);
|
|
283
|
+
const txy = new Float32Array(gw * gh);
|
|
284
|
+
const tyy = new Float32Array(gw * gh);
|
|
285
|
+
const px = (c, x, y) => channels[c][Math.max(0, Math.min(h - 1, y)) * w + Math.max(0, Math.min(w - 1, x))];
|
|
286
|
+
for (let gy = 0; gy < gh; gy += 1) {
|
|
287
|
+
for (let gx = 0; gx < gw; gx += 1) {
|
|
288
|
+
const cx = gx * grid + grid / 2;
|
|
289
|
+
const cy = gy * grid + grid / 2;
|
|
290
|
+
const span = Math.max(2, Math.round(grid / 2) + 2);
|
|
291
|
+
let xx = 0, xy = 0, yy = 0, n = 0;
|
|
292
|
+
for (let dy = -span; dy <= span; dy += 2) {
|
|
293
|
+
for (let dx = -span; dx <= span; dx += 2) {
|
|
294
|
+
const x = Math.round(cx + dx);
|
|
295
|
+
const y = Math.round(cy + dy);
|
|
296
|
+
if (x < 1 || y < 1 || x >= w - 1 || y >= h - 1) continue;
|
|
297
|
+
for (let c = 0; c < 3; c += 1) {
|
|
298
|
+
const gxv = (px(c, x + 1, y) - px(c, x - 1, y)) / 2;
|
|
299
|
+
const gyv = (px(c, x, y + 1) - px(c, x, y - 1)) / 2;
|
|
300
|
+
xx += gxv * gxv;
|
|
301
|
+
xy += gxv * gyv;
|
|
302
|
+
yy += gyv * gyv;
|
|
303
|
+
}
|
|
304
|
+
n += 1;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
const idx = gy * gw + gx;
|
|
308
|
+
txx[idx] = n ? xx / (n * 3) : 0;
|
|
309
|
+
txy[idx] = n ? xy / (n * 3) : 0;
|
|
310
|
+
tyy[idx] = n ? yy / (n * 3) : 0;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
const smoothTensor = (src) => {
|
|
314
|
+
const out = new Float32Array(src.length);
|
|
315
|
+
for (let gy = 0; gy < gh; gy += 1) {
|
|
316
|
+
for (let gx = 0; gx < gw; gx += 1) {
|
|
317
|
+
let sum = 0;
|
|
318
|
+
let cnt = 0;
|
|
319
|
+
for (let dy = -1; dy <= 1; dy += 1) {
|
|
320
|
+
const yy = gy + dy;
|
|
321
|
+
if (yy < 0 || yy >= gh) continue;
|
|
322
|
+
for (let dx = -1; dx <= 1; dx += 1) {
|
|
323
|
+
const xx = gx + dx;
|
|
324
|
+
if (xx < 0 || xx >= gw) continue;
|
|
325
|
+
sum += src[yy * gw + xx];
|
|
326
|
+
cnt += 1;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
out[gy * gw + gx] = sum / cnt;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
return out;
|
|
333
|
+
};
|
|
334
|
+
for (let pass = 0; pass < 2; pass += 1) {
|
|
335
|
+
txx.set(smoothTensor(txx));
|
|
336
|
+
txy.set(smoothTensor(txy));
|
|
337
|
+
tyy.set(smoothTensor(tyy));
|
|
338
|
+
}
|
|
339
|
+
const cos2 = new Float32Array(gw * gh);
|
|
340
|
+
const sin2 = new Float32Array(gw * gh);
|
|
341
|
+
const mag = new Float32Array(gw * gh);
|
|
342
|
+
const coh = new Float32Array(gw * gh);
|
|
343
|
+
let magSum = 0;
|
|
344
|
+
let cohSum = 0;
|
|
345
|
+
let cells = 0;
|
|
346
|
+
for (let i = 0; i < gw * gh; i += 1) {
|
|
347
|
+
const xx = txx[i];
|
|
348
|
+
const xy = txy[i];
|
|
349
|
+
const yy = tyy[i];
|
|
350
|
+
const tr = xx + yy;
|
|
351
|
+
const diff = xx - yy;
|
|
352
|
+
const disc = Math.sqrt(diff * diff + 4 * xy * xy);
|
|
353
|
+
// 主轴 = 梯度主方向;等色线方向 = 主轴 + 90°
|
|
354
|
+
const phi = 0.5 * Math.atan2(2 * xy, diff);
|
|
355
|
+
const edge = phi + Math.PI / 2;
|
|
356
|
+
cos2[i] = Math.cos(2 * edge);
|
|
357
|
+
sin2[i] = Math.sin(2 * edge);
|
|
358
|
+
mag[i] = Math.sqrt(tr);
|
|
359
|
+
coh[i] = tr > 1e-6 ? disc / (tr + 1e-6) : 0;
|
|
360
|
+
magSum += mag[i];
|
|
361
|
+
cohSum += coh[i];
|
|
362
|
+
cells += 1;
|
|
363
|
+
}
|
|
364
|
+
return { gw, gh, grid, cos2, sin2, mag, coh, avgMag: magSum / Math.max(1, cells), avgCoh: cohSum / Math.max(1, cells) };
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/** 双线性取方向场:插值 cos2θ/sin2θ,再 atan2/2 还原成轴方向。 */
|
|
368
|
+
function sampleField(field, x, y) {
|
|
369
|
+
const fx = x / field.grid - 0.5;
|
|
370
|
+
const fy = y / field.grid - 0.5;
|
|
371
|
+
const x0 = Math.floor(fx);
|
|
372
|
+
const y0 = Math.floor(fy);
|
|
373
|
+
const tx = fx - x0;
|
|
374
|
+
const ty = fy - y0;
|
|
375
|
+
const cell = (gx, gy) => {
|
|
376
|
+
const cx = Math.max(0, Math.min(field.gw - 1, gx));
|
|
377
|
+
const cy = Math.max(0, Math.min(field.gh - 1, gy));
|
|
378
|
+
return cy * field.gw + cx;
|
|
379
|
+
};
|
|
380
|
+
const i00 = cell(x0, y0);
|
|
381
|
+
const i10 = cell(x0 + 1, y0);
|
|
382
|
+
const i01 = cell(x0, y0 + 1);
|
|
383
|
+
const i11 = cell(x0 + 1, y0 + 1);
|
|
384
|
+
const w00 = (1 - tx) * (1 - ty);
|
|
385
|
+
const w10 = tx * (1 - ty);
|
|
386
|
+
const w01 = (1 - tx) * ty;
|
|
387
|
+
const w11 = tx * ty;
|
|
388
|
+
const c = field.cos2[i00] * w00 + field.cos2[i10] * w10 + field.cos2[i01] * w01 + field.cos2[i11] * w11;
|
|
389
|
+
const s = field.sin2[i00] * w00 + field.sin2[i10] * w10 + field.sin2[i01] * w01 + field.sin2[i11] * w11;
|
|
390
|
+
return {
|
|
391
|
+
angle: 0.5 * Math.atan2(s, c),
|
|
392
|
+
mag: field.mag[i00] * w00 + field.mag[i10] * w10 + field.mag[i01] * w01 + field.mag[i11] * w11,
|
|
393
|
+
coh: field.coh[i00] * w00 + field.coh[i10] * w10 + field.coh[i01] * w01 + field.coh[i11] * w11,
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
function blendAngle(a, b, t) {
|
|
398
|
+
const d = ((b - a + Math.PI) % (Math.PI * 2) + Math.PI * 2) % (Math.PI * 2) - Math.PI;
|
|
399
|
+
return a + d * t;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
function clampTurn(a, b, maxTurn) {
|
|
403
|
+
const d = ((b - a + Math.PI) % (Math.PI * 2) + Math.PI * 2) % (Math.PI * 2) - Math.PI;
|
|
404
|
+
return a + Math.max(-maxTurn, Math.min(maxTurn, d));
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* 单向一刷:从起笔点沿方向场走到收笔点,带逐点压感(起 0.95 → 收 0.35),
|
|
409
|
+
* 不再「中心向两端对称」,所以笔触有起收、有方向。
|
|
410
|
+
*/
|
|
411
|
+
function paintFlowPath(field, w, h, x, y, length, angle, count) {
|
|
412
|
+
const seg = length / Math.max(1, count - 1);
|
|
413
|
+
// 短笔可以更弯,长笔更直
|
|
414
|
+
const maxTurn = Math.max(0.05, Math.min(0.4, 9 / Math.max(12, length)));
|
|
415
|
+
let px = x - Math.cos(angle) * (length / 2);
|
|
416
|
+
let py = y - Math.sin(angle) * (length / 2);
|
|
417
|
+
let a = angle;
|
|
418
|
+
const pts = [];
|
|
419
|
+
for (let i = 0; i < count; i += 1) {
|
|
420
|
+
const t = count <= 1 ? 0 : i / (count - 1);
|
|
421
|
+
pts.push([
|
|
422
|
+
Math.max(0, Math.min(w - 1, px)),
|
|
423
|
+
Math.max(0, Math.min(h - 1, py)),
|
|
424
|
+
Number((0.95 - 0.6 * t).toFixed(3)),
|
|
425
|
+
]);
|
|
426
|
+
const f = sampleField(field, px, py);
|
|
427
|
+
if (f.coh >= 0.15) {
|
|
428
|
+
a = clampTurn(a, blendAngle(a, f.angle, 0.12 + 0.55 * f.coh), maxTurn);
|
|
429
|
+
}
|
|
430
|
+
px += Math.cos(a) * seg;
|
|
431
|
+
py += Math.sin(a) * seg;
|
|
432
|
+
}
|
|
433
|
+
return pts;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
function hexToRgb(hex) {
|
|
437
|
+
const s = String(hex || "").replace("#", "");
|
|
438
|
+
return [parseInt(s.slice(0, 2), 16) || 0, parseInt(s.slice(2, 4), 16) || 0, parseInt(s.slice(4, 6), 16) || 0];
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/** 每个像素相对「所属色阶」亮多少/暗多少:负=暗部,正=亮部。 */
|
|
442
|
+
function buildToneMap(cut, paletteHex) {
|
|
443
|
+
const { w, h, data } = cut;
|
|
444
|
+
const tone = new Int16Array(w * h);
|
|
445
|
+
const pal = (paletteHex || []).map(hexToRgb);
|
|
446
|
+
if (pal.length === 0) return tone;
|
|
447
|
+
for (let p = 0, i = 0; p < w * h; p += 1, i += 4) {
|
|
448
|
+
if (data[i + 3] < 16) continue;
|
|
449
|
+
const r = data[i], g = data[i + 1], b = data[i + 2];
|
|
450
|
+
let best = 0;
|
|
451
|
+
let bestD = Infinity;
|
|
452
|
+
for (let k = 0; k < pal.length; k += 1) {
|
|
453
|
+
const d = (r - pal[k][0]) ** 2 + (g - pal[k][1]) ** 2 + (b - pal[k][2]) ** 2;
|
|
454
|
+
if (d < bestD) {
|
|
455
|
+
bestD = d;
|
|
456
|
+
best = k;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
tone[p] = Math.round(rgbLum(r, g, b) - rgbLum(pal[best][0], pal[best][1], pal[best][2]));
|
|
460
|
+
}
|
|
461
|
+
return tone;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/** 铺底按「颜色 + 位置」拆部件;拆错不致命,上层 clip 到整组兜底。 */
|
|
465
|
+
function splitFillParts(fills, w, h) {
|
|
466
|
+
const parts = { skin: [], hair: [], top: [], bottom: [], prop: [] };
|
|
467
|
+
for (const fill of fills) {
|
|
468
|
+
const [r, g, b] = hexToRgb(fill.color);
|
|
469
|
+
const [cx, cy] = polygonCentroid(fill.points);
|
|
470
|
+
const y = cy / h;
|
|
471
|
+
const x = cx / w;
|
|
472
|
+
const lum = rgbLum(r, g, b);
|
|
473
|
+
let key;
|
|
474
|
+
if (r - b > 18 && lum > 70 && y < 0.78 && x > 0.12 && x < 0.88) key = "skin";
|
|
475
|
+
else if (lum < 70 && y < 0.45) key = "hair";
|
|
476
|
+
else if (y > 0.62) key = "bottom";
|
|
477
|
+
else if (x < 0.12 || x > 0.88) key = "prop";
|
|
478
|
+
else key = "top";
|
|
479
|
+
parts[key].push(fill);
|
|
480
|
+
}
|
|
481
|
+
const labels = { skin: "皮肤", hair: "头发", top: "上装", bottom: "下装", prop: "道具" };
|
|
482
|
+
return Object.keys(parts)
|
|
483
|
+
.filter((k) => parts[k].length > 0)
|
|
484
|
+
.map((k) => ({ key: k, name: labels[k], fills: parts[k] }));
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
/** HSL 色相旋转,给镭射层按 x 位置铺彩虹。 */
|
|
488
|
+
function shiftHueHex(hex, degrees) {
|
|
489
|
+
const [r0, g0, b0] = hexToRgb(hex).map((v) => v / 255);
|
|
490
|
+
const max = Math.max(r0, g0, b0);
|
|
491
|
+
const min = Math.min(r0, g0, b0);
|
|
492
|
+
let h = 0;
|
|
493
|
+
let s = 0;
|
|
494
|
+
const l = (max + min) / 2;
|
|
495
|
+
if (max !== min) {
|
|
496
|
+
const d = max - min;
|
|
497
|
+
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
|
|
498
|
+
if (max === r0) h = (g0 - b0) / d + (g0 < b0 ? 6 : 0);
|
|
499
|
+
else if (max === g0) h = (b0 - r0) / d + 2;
|
|
500
|
+
else h = (r0 - g0) / d + 4;
|
|
501
|
+
h /= 6;
|
|
502
|
+
}
|
|
503
|
+
h = (h + degrees / 360 + 1) % 1;
|
|
504
|
+
const hue2rgb = (p, q, t) => {
|
|
505
|
+
let tt = t;
|
|
506
|
+
if (tt < 0) tt += 1;
|
|
507
|
+
if (tt > 1) tt -= 1;
|
|
508
|
+
if (tt < 1 / 6) return p + (q - p) * 6 * tt;
|
|
509
|
+
if (tt < 1 / 2) return q;
|
|
510
|
+
if (tt < 2 / 3) return p + (q - p) * (2 / 3 - tt) * 6;
|
|
511
|
+
return p;
|
|
512
|
+
};
|
|
513
|
+
const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
|
|
514
|
+
const p = 2 * l - q;
|
|
515
|
+
const rgb = [hue2rgb(p, q, h + 1 / 3), hue2rgb(p, q, h), hue2rgb(p, q, h - 1 / 3)];
|
|
516
|
+
return rgbHex(rgb[0] * 255, rgb[1] * 255, rgb[2] * 255);
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
/** 多边形整体向质心缩 inset 像素:给下面的线稿让出 1px 缝。 */
|
|
520
|
+
function shrinkPolygon(points, inset) {
|
|
521
|
+
if (!(inset > 0)) return points;
|
|
522
|
+
const [cx, cy] = polygonCentroid(points);
|
|
523
|
+
return points.map((p) => {
|
|
524
|
+
const dx = p[0] - cx;
|
|
525
|
+
const dy = p[1] - cy;
|
|
526
|
+
const len = Math.hypot(dx, dy) || 1;
|
|
527
|
+
const k = Math.max(0, len - inset) / len;
|
|
528
|
+
return [cx + dx * k, cy + dy * k];
|
|
529
|
+
});
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
/** 从画布四边 flood-fill 出的「白背景」连通域;被火焰/人物切成几块的背景也都能整块剔掉。 */
|
|
533
|
+
function whiteBackgroundMask(cut) {
|
|
534
|
+
const { w, h, data } = cut;
|
|
535
|
+
const mask = new Uint8Array(w * h);
|
|
536
|
+
const stack = new Int32Array(w * h);
|
|
537
|
+
let top = 0;
|
|
538
|
+
const isWhite = (p) => {
|
|
539
|
+
const i = p * 4;
|
|
540
|
+
if (data[i + 3] < 16) return true;
|
|
541
|
+
const r = data[i], g = data[i + 1], b = data[i + 2];
|
|
542
|
+
const lum = rgbLum(r, g, b);
|
|
543
|
+
const chroma = Math.max(r, g, b) - Math.min(r, g, b);
|
|
544
|
+
return lum >= 250 && chroma <= 8;
|
|
545
|
+
};
|
|
546
|
+
const seed = (p) => {
|
|
547
|
+
if (mask[p] || !isWhite(p)) return;
|
|
548
|
+
mask[p] = 1;
|
|
549
|
+
stack[top] = p;
|
|
550
|
+
top += 1;
|
|
551
|
+
};
|
|
552
|
+
for (let x = 0; x < w; x += 1) {
|
|
553
|
+
seed(x);
|
|
554
|
+
seed((h - 1) * w + x);
|
|
555
|
+
}
|
|
556
|
+
for (let y = 0; y < h; y += 1) {
|
|
557
|
+
seed(y * w);
|
|
558
|
+
seed(y * w + w - 1);
|
|
559
|
+
}
|
|
560
|
+
while (top > 0) {
|
|
561
|
+
top -= 1;
|
|
562
|
+
const p = stack[top];
|
|
563
|
+
const x = p % w;
|
|
564
|
+
const y = (p - x) / w;
|
|
565
|
+
if (x > 0) seed(p - 1);
|
|
566
|
+
if (x < w - 1) seed(p + 1);
|
|
567
|
+
if (y > 0) seed(p - w);
|
|
568
|
+
if (y < h - 1) seed(p + w);
|
|
569
|
+
}
|
|
570
|
+
return mask;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
function polygonCentroid(points) {
|
|
574
|
+
let x = 0;
|
|
575
|
+
let y = 0;
|
|
576
|
+
for (const p of points) {
|
|
577
|
+
x += p[0];
|
|
578
|
+
y += p[1];
|
|
579
|
+
}
|
|
580
|
+
return [x / Math.max(1, points.length), y / Math.max(1, points.length)];
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
function scaleRgb(rgb, k) {
|
|
584
|
+
return rgb.map((v) => Math.max(0, Math.min(255, Math.round(v * k))));
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
/** 往白里提亮 t:multiply 层用局部色直接乘会把暗部压死,提亮后再压才接近原图。 */
|
|
588
|
+
function lightenHex(hex, t) {
|
|
589
|
+
const [r, g, b] = hexToRgb(hex);
|
|
590
|
+
return rgbHex(r + (255 - r) * t, g + (255 - g) * t, b + (255 - b) * t);
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
/**
|
|
594
|
+
* 铺底层:色阶量化后的**大色块 fill**。
|
|
595
|
+
* 白背景按「四边连通的纯白域」整块剔除(不再靠面积比例),
|
|
596
|
+
* 每个色块再补一圈同色 2px 描边,堵住相邻色阶之间 1px 的白缝。
|
|
597
|
+
*/
|
|
598
|
+
function paintBaseFromBitmap(cut, rect, opts) {
|
|
599
|
+
const { w, h, data } = cut;
|
|
600
|
+
const colorCount = Math.max(3, Math.min(12, Math.round(Number(opts.baseColors) || 8)));
|
|
601
|
+
// 默认先取 120 块;不够 95% 覆盖率就按面积继续补,上限 400
|
|
602
|
+
const initialFills = Math.max(8, Math.min(400, Math.round(Number(opts.baseFills) || 120)));
|
|
603
|
+
const hardCap = Math.min(400, Math.max(initialFills, 400));
|
|
604
|
+
const minArea = Math.max(8, Math.round(Number(opts.baseMinArea) || (w * h) / 6000));
|
|
605
|
+
const inset = Math.max(0, opts.inset === undefined ? 1.2 : (Number(opts.inset) || 0));
|
|
606
|
+
const res = posterizeFills(cut.data, w, h, {
|
|
607
|
+
colors: colorCount,
|
|
608
|
+
minArea,
|
|
609
|
+
tolerance: Math.max(0.5, Number(opts.baseTolerance) || 1.4),
|
|
610
|
+
maxFills: hardCap,
|
|
611
|
+
denoise: 2,
|
|
612
|
+
thinKeep: 24,
|
|
613
|
+
lines: opts.wantEdge === true,
|
|
614
|
+
minLength: Number(opts.edgeMinLength) || 24,
|
|
615
|
+
maxLines: Math.max(1, Number(opts.edgeMaxLines) || 1800),
|
|
616
|
+
});
|
|
617
|
+
const bg = opts.bgMask || whiteBackgroundMask(cut);
|
|
618
|
+
const isBackgroundFill = (fill) => {
|
|
619
|
+
if (hexLum(fill.color) < 248) return false;
|
|
620
|
+
const [cx, cy] = polygonCentroid(fill.points);
|
|
621
|
+
const at = (x, y) => bg[Math.max(0, Math.min(h - 1, Math.round(y))) * w + Math.max(0, Math.min(w - 1, Math.round(x)))];
|
|
622
|
+
if (at(cx, cy)) return true;
|
|
623
|
+
let onBg = 0;
|
|
624
|
+
for (const p of fill.points) if (at(p[0], p[1])) onBg += 1;
|
|
625
|
+
return onBg / Math.max(1, fill.points.length) > 0.8;
|
|
626
|
+
};
|
|
627
|
+
let subjectPixels = 0;
|
|
628
|
+
for (let p = 0, i = 0; p < w * h; p += 1, i += 4) {
|
|
629
|
+
if (data[i + 3] >= 16 && !bg[p]) subjectPixels += 1;
|
|
630
|
+
}
|
|
631
|
+
const candidates = (res.fills || []).filter((fill) => !isBackgroundFill(fill));
|
|
632
|
+
const chosen = candidates.slice(0, initialFills);
|
|
633
|
+
let coveredArea = chosen.reduce((n, f) => n + f.area, 0);
|
|
634
|
+
let coverage = subjectPixels ? (coveredArea / subjectPixels) * 100 : 100;
|
|
635
|
+
// 按覆盖率贪心补:默认 120 块不够 95% 就继续加,直到 400 或补够
|
|
636
|
+
for (let i = chosen.length; i < candidates.length && coverage < 95 && chosen.length < hardCap; i += 1) {
|
|
637
|
+
chosen.push(candidates[i]);
|
|
638
|
+
coveredArea += candidates[i].area;
|
|
639
|
+
coverage = subjectPixels ? (coveredArea / subjectPixels) * 100 : 100;
|
|
640
|
+
}
|
|
641
|
+
const shift = (points) => points.map((p) => [Math.round(rect.x + p[0]), Math.round(rect.y + p[1])]);
|
|
642
|
+
const makeFill = (f) => ({
|
|
643
|
+
color: f.color,
|
|
644
|
+
points: shift(shrinkPolygon(f.points, inset)),
|
|
645
|
+
holes: [],
|
|
646
|
+
area: f.area,
|
|
647
|
+
});
|
|
648
|
+
const outlineOf = (f) => ({
|
|
649
|
+
color: f.color,
|
|
650
|
+
size: 2.4,
|
|
651
|
+
opacity: 1,
|
|
652
|
+
points: f.points.concat([f.points[0]]),
|
|
653
|
+
});
|
|
654
|
+
const fills = chosen.map(makeFill);
|
|
655
|
+
const outlines = fills.map((f) => ({
|
|
656
|
+
color: f.color,
|
|
657
|
+
size: 2.4,
|
|
658
|
+
opacity: 1,
|
|
659
|
+
points: f.points.concat([f.points[0]]),
|
|
660
|
+
}));
|
|
661
|
+
// 06a–06e:按颜色 + 位置把铺底拆部件(拆错不致命,上层 clip 到 role:base 兜底)
|
|
662
|
+
const parts = splitFillParts(chosen, w, h).map((part) => ({
|
|
663
|
+
key: part.key,
|
|
664
|
+
name: part.name,
|
|
665
|
+
fills: part.fills.map(makeFill),
|
|
666
|
+
outlines: part.fills.map((f) => outlineOf(makeFill(f))),
|
|
667
|
+
}));
|
|
668
|
+
return {
|
|
669
|
+
fills,
|
|
670
|
+
outlines,
|
|
671
|
+
parts,
|
|
672
|
+
paths: res.paths || [],
|
|
673
|
+
stats: {
|
|
674
|
+
colorCount,
|
|
675
|
+
minArea,
|
|
676
|
+
fills: fills.length,
|
|
677
|
+
parts: parts.map((p) => ({ key: p.key, name: p.name, fills: p.fills.length })),
|
|
678
|
+
requestedFills: initialFills,
|
|
679
|
+
candidateFills: candidates.length,
|
|
680
|
+
droppedFills: (res.fills || []).length - fills.length,
|
|
681
|
+
subjectPixels,
|
|
682
|
+
coveragePct: Number(Math.min(100, coverage).toFixed(1)),
|
|
683
|
+
palette: res.stats ? res.stats.palette : null,
|
|
684
|
+
},
|
|
685
|
+
};
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
/** 给算完的五步估一个「非白区域被笔迹/填色盖住多少」的覆盖率。 */
|
|
689
|
+
function estimateCoverage(cut, stages) {
|
|
690
|
+
const { w, h, data } = cut;
|
|
691
|
+
const cell = 8;
|
|
692
|
+
const cw = Math.ceil(w / cell);
|
|
693
|
+
const ch = Math.ceil(h / cell);
|
|
694
|
+
const subject = new Uint8Array(cw * ch);
|
|
695
|
+
const cover = new Uint8Array(cw * ch);
|
|
696
|
+
let subjectCells = 0;
|
|
697
|
+
for (let y = 0; y < h; y += cell) {
|
|
698
|
+
for (let x = 0; x < w; x += cell) {
|
|
699
|
+
const i = (y * w + x) * 4;
|
|
700
|
+
const gx = Math.min(cw - 1, x / cell | 0);
|
|
701
|
+
const gy = Math.min(ch - 1, y / cell | 0);
|
|
702
|
+
if (data[i + 3] < 16) continue;
|
|
703
|
+
const r = data[i], g = data[i + 1], b = data[i + 2];
|
|
704
|
+
if (rgbLum(r, g, b) < 250 || Math.max(r, g, b) - Math.min(r, g, b) > 8) {
|
|
705
|
+
subject[gy * cw + gx] = 1;
|
|
706
|
+
subjectCells += 1;
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
const markPolygon = (points) => {
|
|
711
|
+
if (!points || points.length < 3) return;
|
|
712
|
+
let y0 = Infinity, y1 = -Infinity;
|
|
713
|
+
for (const p of points) {
|
|
714
|
+
y0 = Math.min(y0, p[1]);
|
|
715
|
+
y1 = Math.max(y1, p[1]);
|
|
716
|
+
}
|
|
717
|
+
y0 = Math.max(0, Math.floor(y0 / cell));
|
|
718
|
+
y1 = Math.min(ch - 1, Math.ceil(y1 / cell));
|
|
719
|
+
for (let gy = y0; gy <= y1; gy += 1) {
|
|
720
|
+
const yy = gy * cell + cell / 2;
|
|
721
|
+
const xs = [];
|
|
722
|
+
for (let i = 0, j = points.length - 1; i < points.length; j = i, i += 1) {
|
|
723
|
+
const a = points[j], b = points[i];
|
|
724
|
+
if ((a[1] > yy) !== (b[1] > yy)) {
|
|
725
|
+
xs.push(a[0] + ((yy - a[1]) * (b[0] - a[0])) / (b[1] - a[1] || 1e-9));
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
xs.sort((a, b) => a - b);
|
|
729
|
+
for (let k = 0; k + 1 < xs.length; k += 2) {
|
|
730
|
+
const gx0 = Math.max(0, Math.floor(xs[k] / cell));
|
|
731
|
+
const gx1 = Math.min(cw - 1, Math.floor(xs[k + 1] / cell));
|
|
732
|
+
for (let gx = gx0; gx <= gx1; gx += 1) cover[gy * cw + gx] = 1;
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
};
|
|
736
|
+
const markStroke = (points, size) => {
|
|
737
|
+
const r = Math.max(1, (size || 2) / 2);
|
|
738
|
+
for (let i = 1; i < points.length; i += 1) {
|
|
739
|
+
const a = points[i - 1], b = points[i];
|
|
740
|
+
const dist = Math.hypot(b[0] - a[0], b[1] - a[1]);
|
|
741
|
+
const steps = Math.max(1, Math.ceil(dist / cell));
|
|
742
|
+
for (let k = 0; k <= steps; k += 1) {
|
|
743
|
+
const x = a[0] + ((b[0] - a[0]) * k) / steps;
|
|
744
|
+
const y = a[1] + ((b[1] - a[1]) * k) / steps;
|
|
745
|
+
const gx0 = Math.max(0, Math.floor((x - r) / cell));
|
|
746
|
+
const gx1 = Math.min(cw - 1, Math.floor((x + r) / cell));
|
|
747
|
+
const gy0 = Math.max(0, Math.floor((y - r) / cell));
|
|
748
|
+
const gy1 = Math.min(ch - 1, Math.floor((y + r) / cell));
|
|
749
|
+
for (let gy = gy0; gy <= gy1; gy += 1) {
|
|
750
|
+
for (let gx = gx0; gx <= gx1; gx += 1) cover[gy * cw + gx] = 1;
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
};
|
|
755
|
+
for (const stage of stages) {
|
|
756
|
+
if (stage.kind === "fill") for (const f of stage.fills || []) markPolygon(f.points);
|
|
757
|
+
else for (const s of stage.strokes || []) markStroke(s.points, s.size);
|
|
758
|
+
}
|
|
759
|
+
let covered = 0;
|
|
760
|
+
for (let i = 0; i < subject.length; i += 1) if (subject[i] && cover[i]) covered += 1;
|
|
761
|
+
return {
|
|
762
|
+
subjectCells,
|
|
763
|
+
coveredCells: covered,
|
|
764
|
+
coveragePct: subjectCells ? Number(((covered / subjectCells) * 100).toFixed(1)) : 0,
|
|
765
|
+
};
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
/**
|
|
769
|
+
* 五步复刻主函数(宿主侧;有面板时也优先走这里,保证两条路径同一结果)。
|
|
770
|
+
* 返回 { stages, total, stats }。
|
|
771
|
+
*/
|
|
772
|
+
function paintStrokesFromBitmap(cut, rect, palette, opts = {}) {
|
|
773
|
+
const { w, h, data } = cut;
|
|
774
|
+
const enabled = parsePaintStages(opts.stages);
|
|
775
|
+
const step = Math.max(5, Math.min(40, Math.round(Number(opts.step) || 10)));
|
|
776
|
+
const maxStrokes = Math.max(200, Math.min(40000, Number(opts.maxStrokes) || 4000));
|
|
777
|
+
const whiteCut = Math.min(252, Math.max(225, opts.minLum === undefined ? 246 : Number(opts.minLum)));
|
|
778
|
+
let seed = 20261130;
|
|
779
|
+
const rnd = () => {
|
|
780
|
+
seed = (seed * 1103515245 + 12345) % 2147483648;
|
|
781
|
+
return seed / 2147483648;
|
|
782
|
+
};
|
|
783
|
+
const field = buildDirectionField(cut, Math.max(8, Math.round(step * 1.0)));
|
|
784
|
+
const bgMask = whiteBackgroundMask(cut);
|
|
785
|
+
const stages = [];
|
|
786
|
+
// 预算真正跟着 maxStrokes 走(不再有常量硬顶)
|
|
787
|
+
const caps = {
|
|
788
|
+
block: Math.round(maxStrokes * 0.28),
|
|
789
|
+
blend: Math.round(maxStrokes * 0.15),
|
|
790
|
+
edge: Math.round(maxStrokes * 0.12),
|
|
791
|
+
accent: Math.round(maxStrokes * 0.03),
|
|
792
|
+
occlusion: Math.round(maxStrokes * 0.05),
|
|
793
|
+
lightblock: Math.round(maxStrokes * 0.04),
|
|
794
|
+
texture: Math.round(maxStrokes * 0.1),
|
|
795
|
+
iridescent: Math.round(maxStrokes * 0.015),
|
|
796
|
+
atmosphere: Math.round(maxStrokes * 0.015),
|
|
797
|
+
};
|
|
798
|
+
|
|
799
|
+
const quant = paintBaseFromBitmap(cut, rect, { ...opts, bgMask, wantEdge: true });
|
|
800
|
+
const toneMap = quant && quant.stats && quant.stats.palette ? buildToneMap(cut, quant.stats.palette) : null;
|
|
801
|
+
const toneAt = (x, y) => (toneMap ? toneMap[Math.max(0, Math.min(h - 1, Math.round(y))) * w + Math.max(0, Math.min(w - 1, Math.round(x)))] : 0);
|
|
802
|
+
|
|
803
|
+
// ① 铺底(06a–06e 部件)
|
|
804
|
+
if (enabled.has("base")) {
|
|
805
|
+
const fills = quant ? quant.fills : [];
|
|
806
|
+
const outlines = quant ? quant.outlines : [];
|
|
807
|
+
stages.push({
|
|
808
|
+
key: "base",
|
|
809
|
+
name: "固有色平铺",
|
|
810
|
+
kind: "fill",
|
|
811
|
+
brush: null,
|
|
812
|
+
fills,
|
|
813
|
+
outlines,
|
|
814
|
+
parts: quant ? quant.parts : [],
|
|
815
|
+
stats: {
|
|
816
|
+
items: fills.length,
|
|
817
|
+
fills: fills.length,
|
|
818
|
+
outlines: outlines.length,
|
|
819
|
+
parts: quant && quant.stats ? quant.stats.parts : null,
|
|
820
|
+
droppedFills: quant && quant.stats ? quant.stats.droppedFills : 0,
|
|
821
|
+
colorCount: quant && quant.stats ? quant.stats.colorCount : null,
|
|
822
|
+
posterizeCoveragePct: quant && quant.stats ? quant.stats.coveragePct : null,
|
|
823
|
+
},
|
|
824
|
+
});
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
// ② 塑形:把整个形体用中大笔铺满,不只是暗部
|
|
828
|
+
if (enabled.has("block")) {
|
|
829
|
+
const cap = caps.block;
|
|
830
|
+
const gap = Math.max(10, Math.round(Math.min(w, h) / 70));
|
|
831
|
+
const cands = [];
|
|
832
|
+
const cx0 = w / 2;
|
|
833
|
+
const cy0 = h / 2;
|
|
834
|
+
for (let y = gap * 0.5; y < h; y += gap) {
|
|
835
|
+
for (let x = gap * 0.5; x < w; x += gap) {
|
|
836
|
+
const px = x + (rnd() - 0.5) * gap * 0.4;
|
|
837
|
+
const py = y + (rnd() - 0.5) * gap * 0.4;
|
|
838
|
+
const s = paintSample(data, w, h, px, py, gap * 0.9, whiteCut);
|
|
839
|
+
if (!s) continue;
|
|
840
|
+
// 白背景连通域里不落笔(白袍内部不算背景,仍会画)
|
|
841
|
+
if (bgMask[Math.max(0, Math.min(h - 1, Math.round(py))) * w + Math.max(0, Math.min(w - 1, Math.round(px)))]) continue;
|
|
842
|
+
if (s.inkFrac < 0.1) continue;
|
|
843
|
+
if (s.whiteFrac > 0.95) continue;
|
|
844
|
+
if (toneAt(px, py) > -4) continue; // 一级暗部只画比固有色暗的地方
|
|
845
|
+
const f = sampleField(field, px, py);
|
|
846
|
+
const centerWeight = 1 - 0.35 * Math.hypot((px - cx0) / (w / 2), (py - cy0) / (h / 2));
|
|
847
|
+
const score = 1 + f.mag * 0.12 + s.inkFrac * 8 + centerWeight;
|
|
848
|
+
cands.push({ x: px, y: py, s, f, score });
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
const picked = pickWeighted(cands, cap, w, h);
|
|
852
|
+
const strokes = picked.map((c) => {
|
|
853
|
+
const width = Math.max(6, gap * (1.15 + 0.3 * rnd()));
|
|
854
|
+
const len = width * (1.8 + 0.8 * rnd());
|
|
855
|
+
const count = Math.max(5, Math.min(14, Math.round(len / 6)));
|
|
856
|
+
const pts = trimOnWhite(paintFlowPath(field, w, h, c.x, c.y, len, c.f.angle, count), data, w, h);
|
|
857
|
+
if (pts.length < 2) return null;
|
|
858
|
+
return {
|
|
859
|
+
color: lightenHex(c.s.inkHex, 0.35),
|
|
860
|
+
size: Number(width.toFixed(2)),
|
|
861
|
+
opacity: Number((0.35 + 0.18 * rnd()).toFixed(2)),
|
|
862
|
+
points: toCanvas(pts, rect),
|
|
863
|
+
};
|
|
864
|
+
}).filter(Boolean);
|
|
865
|
+
// 浅色先铺、深色后压:亮度降序
|
|
866
|
+
strokes.sort((a, b) => hexLum(b.color) - hexLum(a.color));
|
|
867
|
+
stages.push({ key: "block", name: "一级暗部", kind: "brush", brush: "hard", taper: true, minSize: 0.4, strokes, stats: { items: strokes.length, strokes: strokes.length, candidates: cands.length, gap } });
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
// ③ 过渡:明暗交界补中间色,冷暖从原图采
|
|
871
|
+
if (enabled.has("blend")) {
|
|
872
|
+
const cap = caps.blend;
|
|
873
|
+
const gap = Math.max(6, Math.round(step * 0.9));
|
|
874
|
+
const cands = [];
|
|
875
|
+
for (let y = gap * 0.5; y < h; y += gap) {
|
|
876
|
+
for (let x = gap * 0.5; x < w; x += gap) {
|
|
877
|
+
const px = x + (rnd() - 0.5) * gap * 0.5;
|
|
878
|
+
const py = y + (rnd() - 0.5) * gap * 0.5;
|
|
879
|
+
const s = paintSample(data, w, h, px, py, gap * 0.8, whiteCut);
|
|
880
|
+
if (!s || s.inkFrac < 0.08) continue;
|
|
881
|
+
if (bgMask[Math.max(0, Math.min(h - 1, Math.round(py))) * w + Math.max(0, Math.min(w - 1, Math.round(px)))]) continue;
|
|
882
|
+
if (s.whiteFrac > 0.9) continue;
|
|
883
|
+
const f = sampleField(field, px, py);
|
|
884
|
+
if (f.mag < 6) continue;
|
|
885
|
+
if (toneAt(px, py) > -8) continue; // 二级暗部只补更深的交界
|
|
886
|
+
const ox = Math.cos(f.angle) * gap * 1.4;
|
|
887
|
+
const oy = Math.sin(f.angle) * gap * 1.4;
|
|
888
|
+
const flip = ((x / gap | 0) + (y / gap | 0)) % 2 === 0;
|
|
889
|
+
const s2 = paintSample(data, w, h, px + (flip ? ox : -ox), py + (flip ? oy : -oy), gap * 0.65, whiteCut);
|
|
890
|
+
// 对侧取色必须真的是形体色;取到白底就退回本格色,不落白笔
|
|
891
|
+
const warm = s2 && s2.inkN > 0 && s2.whiteFrac < 0.15 ? s2 : s;
|
|
892
|
+
cands.push({ x: px, y: py, s: flip ? s : warm, f, score: 1 + f.mag * 0.1 + s.inkFrac });
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
const picked = pickWeighted(cands, cap, w, h);
|
|
896
|
+
const strokes = picked.map((c) => {
|
|
897
|
+
const width = Math.max(3, gap * (0.7 + 0.25 * rnd()));
|
|
898
|
+
const len = width * (1.8 + 0.9 * rnd());
|
|
899
|
+
const pts = trimOnWhite(paintFlowPath(field, w, h, c.x, c.y, len, c.f.angle, 4), data, w, h);
|
|
900
|
+
if (pts.length < 2) return null;
|
|
901
|
+
return {
|
|
902
|
+
color: lightenHex(c.s.inkHex, 0.28),
|
|
903
|
+
size: Number(width.toFixed(2)),
|
|
904
|
+
opacity: Number((0.35 + 0.18 * rnd()).toFixed(2)),
|
|
905
|
+
points: toCanvas(pts, rect),
|
|
906
|
+
};
|
|
907
|
+
}).filter(Boolean);
|
|
908
|
+
strokes.sort((a, b) => hexLum(b.color) - hexLum(a.color));
|
|
909
|
+
stages.push({ key: "blend", name: "二级暗部", kind: "brush", brush: "softround", taper: true, minSize: 0.45, strokes, stats: { items: strokes.length, strokes: strokes.length, candidates: cands.length } });
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
// ④ 收边:色阶边界 → 沿切线的短硬笔;ΔL 或 Δchroma 任一过线就描
|
|
913
|
+
if (enabled.has("edge") && quant && quant.paths.length) {
|
|
914
|
+
const cap = caps.edge;
|
|
915
|
+
const gap = Math.max(4, Math.round(step * 0.5));
|
|
916
|
+
const paths = [];
|
|
917
|
+
for (const path of quant.paths) {
|
|
918
|
+
const items = [];
|
|
919
|
+
let length = 0;
|
|
920
|
+
let carry = 0;
|
|
921
|
+
for (let i = 1; i < path.length; i += 1) {
|
|
922
|
+
const ax = path[i - 1][0];
|
|
923
|
+
const ay = path[i - 1][1];
|
|
924
|
+
const bx = path[i][0];
|
|
925
|
+
const by = path[i][1];
|
|
926
|
+
const segLen = Math.hypot(bx - ax, by - ay);
|
|
927
|
+
if (segLen < 0.01) continue;
|
|
928
|
+
length += segLen;
|
|
929
|
+
while (carry <= segLen) {
|
|
930
|
+
const t = carry / segLen;
|
|
931
|
+
const lx = ax + (bx - ax) * t;
|
|
932
|
+
const ly = ay + (by - ay) * t;
|
|
933
|
+
const angle = Math.atan2(by - ay, bx - ax);
|
|
934
|
+
const nx = -Math.sin(angle);
|
|
935
|
+
const ny = Math.cos(angle);
|
|
936
|
+
// 法向取 5 个点,选「非白像素最多」的那个,跨白缝时线不断
|
|
937
|
+
let s = null;
|
|
938
|
+
for (let k = -2; k <= 2; k += 1) {
|
|
939
|
+
const cand = paintSample(data, w, h, lx + nx * k * 1.5, ly + ny * k * 1.5, 2, whiteCut);
|
|
940
|
+
if (cand && (!s || cand.inkN > s.inkN || (cand.inkN === s.inkN && cand.whiteFrac < s.whiteFrac))) s = cand;
|
|
941
|
+
}
|
|
942
|
+
if (s && s.inkN > 0) {
|
|
943
|
+
const a = paintSample(data, w, h, lx + nx * 4, ly + ny * 4, 2, whiteCut);
|
|
944
|
+
const b = paintSample(data, w, h, lx - nx * 4, ly - ny * 4, 2, whiteCut);
|
|
945
|
+
if (a && b) {
|
|
946
|
+
const dL = Math.abs(a.lum - b.lum);
|
|
947
|
+
const ca = chromaOf(a.rgb[0], a.rgb[1], a.rgb[2]);
|
|
948
|
+
const cb = chromaOf(b.rgb[0], b.rgb[1], b.rgb[2]);
|
|
949
|
+
const dC = Math.hypot(ca[0] - cb[0], ca[1] - cb[1]);
|
|
950
|
+
if (dL >= 6 || dC >= 10) items.push({ lx, ly, angle, s, a, b });
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
carry += gap;
|
|
954
|
+
}
|
|
955
|
+
carry -= segLen;
|
|
956
|
+
}
|
|
957
|
+
if (items.length) paths.push({ items, length });
|
|
958
|
+
}
|
|
959
|
+
// 配额按 path 长度比例分配,内部结构不会全被外轮廓挤掉
|
|
960
|
+
const totalLength = paths.reduce((n, p) => n + p.length, 0) || 1;
|
|
961
|
+
const strokes = [];
|
|
962
|
+
for (const path of paths) {
|
|
963
|
+
const quota = Math.max(2, Math.round((cap * path.length) / totalLength));
|
|
964
|
+
const stepN = Math.max(1, Math.floor(path.items.length / quota));
|
|
965
|
+
for (let i = 0; i < path.items.length && strokes.length < cap; i += stepN) {
|
|
966
|
+
const it = path.items[i];
|
|
967
|
+
const dark = it.a.lum <= it.b.lum ? it.a : it.b;
|
|
968
|
+
const color = scaleRgb(dark.inkRgb, 0.7);
|
|
969
|
+
const len = 8 + rnd() * 6;
|
|
970
|
+
const half = len / 2;
|
|
971
|
+
const jitter = (rnd() - 0.5) * 0.14;
|
|
972
|
+
const ca = Math.cos(it.angle + jitter);
|
|
973
|
+
const sa = Math.sin(it.angle + jitter);
|
|
974
|
+
strokes.push({
|
|
975
|
+
color: rgbHex(color[0], color[1], color[2]),
|
|
976
|
+
size: Number((1.4 + rnd() * 1).toFixed(2)),
|
|
977
|
+
opacity: Number((0.72 + 0.18 * rnd()).toFixed(2)),
|
|
978
|
+
points: [
|
|
979
|
+
[Math.round(rect.x + it.lx - ca * half), Math.round(rect.y + it.ly - sa * half)],
|
|
980
|
+
[Math.round(rect.x + it.lx + ca * half), Math.round(rect.y + it.ly + sa * half)],
|
|
981
|
+
],
|
|
982
|
+
});
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
strokes.sort((a, b) => hexLum(b.color) - hexLum(a.color));
|
|
986
|
+
stages.push({ key: "edge", name: "精线稿", kind: "brush", brush: "liner", taper: false, strokes, stats: { items: strokes.length, strokes: strokes.length, paths: paths.length } });
|
|
987
|
+
}
|
|
988
|
+
|
|
989
|
+
// ⑤ 点睛:最亮、对比最强的点
|
|
990
|
+
if (enabled.has("accent")) {
|
|
991
|
+
const cap = caps.accent;
|
|
992
|
+
const gap = Math.max(6, Math.round(step * 0.8));
|
|
993
|
+
const cands = [];
|
|
994
|
+
for (let y = gap * 0.5; y < h; y += gap) {
|
|
995
|
+
for (let x = gap * 0.5; x < w; x += gap) {
|
|
996
|
+
const s = paintSample(data, w, h, x, y, gap * 0.9, whiteCut);
|
|
997
|
+
if (!s || s.inkFrac < 0.15) continue;
|
|
998
|
+
if (bgMask[Math.max(0, Math.min(h - 1, Math.round(y))) * w + Math.max(0, Math.min(w - 1, Math.round(x)))]) continue;
|
|
999
|
+
if (s.whiteFrac > 0.9) continue;
|
|
1000
|
+
const contrast = s.inkMaxLum - s.minLum;
|
|
1001
|
+
const score = s.inkMaxLum - 140 + contrast * 0.6;
|
|
1002
|
+
if (s.inkMaxLum < 170 || contrast < 30 || score < 40) continue;
|
|
1003
|
+
if (toneAt(x, y) < 6) continue; // 高光只点比固有色亮的地方
|
|
1004
|
+
cands.push({ x, y, s, score });
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
cands.sort((a, b) => b.score - a.score);
|
|
1008
|
+
const minDist = Math.max(14, step * 1.4);
|
|
1009
|
+
const chosen = [];
|
|
1010
|
+
const strokes = [];
|
|
1011
|
+
for (const cand of cands) {
|
|
1012
|
+
if (strokes.length >= cap) break;
|
|
1013
|
+
if (chosen.some((c) => Math.hypot(c.x - cand.x, c.y - cand.y) < minDist)) continue;
|
|
1014
|
+
const f = sampleField(field, cand.x, cand.y);
|
|
1015
|
+
const pts = trimOnWhite(paintFlowPath(field, w, h, cand.x, cand.y, 3 + rnd() * 5, f.angle, 3), data, w, h);
|
|
1016
|
+
if (pts.length < 2) continue;
|
|
1017
|
+
chosen.push(cand);
|
|
1018
|
+
strokes.push({
|
|
1019
|
+
color: rgbHex(cand.s.inkBright[0], cand.s.inkBright[1], cand.s.inkBright[2]),
|
|
1020
|
+
size: Number((1.6 + rnd() * 1.8).toFixed(2)),
|
|
1021
|
+
opacity: Number((0.8 + 0.15 * rnd()).toFixed(2)),
|
|
1022
|
+
points: toCanvas(pts, rect),
|
|
1023
|
+
});
|
|
1024
|
+
}
|
|
1025
|
+
stages.push({ key: "accent", name: "高光", kind: "brush", brush: "glow", taper: true, minSize: 0.5, strokes, stats: { items: strokes.length, strokes: strokes.length, candidates: cands.length } });
|
|
1026
|
+
}
|
|
1027
|
+
|
|
1028
|
+
const edgeStage = stages.find((s) => s.key === "edge");
|
|
1029
|
+
const accentStage = stages.find((s) => s.key === "accent");
|
|
1030
|
+
|
|
1031
|
+
// 01 构图草稿:色阶边界的粗折线,低不透明度、锁定
|
|
1032
|
+
if (enabled.has("sketch")) {
|
|
1033
|
+
const strokes = [];
|
|
1034
|
+
for (const path of quant && quant.paths ? quant.paths : []) {
|
|
1035
|
+
if (strokes.length >= 120) break;
|
|
1036
|
+
let length = 0;
|
|
1037
|
+
for (let i = 1; i < path.length; i += 1) length += Math.hypot(path[i][0] - path[i - 1][0], path[i][1] - path[i - 1][1]);
|
|
1038
|
+
if (length < 40) continue;
|
|
1039
|
+
const pts = path.filter((_, i) => i % 3 === 0 || i === path.length - 1).map((p) => [Math.round(rect.x + p[0]), Math.round(rect.y + p[1])]);
|
|
1040
|
+
if (pts.length < 2) continue;
|
|
1041
|
+
strokes.push({ color: "#6b7280", size: 1.8, opacity: 0.5, points: pts });
|
|
1042
|
+
}
|
|
1043
|
+
stages.push({ key: "sketch", name: "构图草稿", kind: "brush", brush: "pencil", opacity: 0.35, locked: true, strokes, stats: { items: strokes.length, strokes: strokes.length } });
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
// 04 线稿上色:复用 03 的线形,颜色取原图;渲染时 clip 到 03、锁定透明像素
|
|
1047
|
+
if (enabled.has("lineColor") && edgeStage && edgeStage.strokes.length > 0) {
|
|
1048
|
+
const strokes = edgeStage.strokes.slice(0, 600).map((line) => {
|
|
1049
|
+
const mid = line.points[Math.floor(line.points.length / 2)];
|
|
1050
|
+
const samp = paintSample(data, w, h, mid[0] - rect.x, mid[1] - rect.y, 3, whiteCut);
|
|
1051
|
+
return {
|
|
1052
|
+
color: samp ? samp.inkHex : line.color,
|
|
1053
|
+
size: Number((line.size * 1.5 + 1).toFixed(2)),
|
|
1054
|
+
opacity: 0.9,
|
|
1055
|
+
points: line.points,
|
|
1056
|
+
};
|
|
1057
|
+
});
|
|
1058
|
+
stages.push({ key: "lineColor", name: "线稿上色", kind: "brush", brush: "liner", clipTo: "role:line", lockAlpha: true, strokes, stats: { items: strokes.length, strokes: strokes.length } });
|
|
1059
|
+
}
|
|
1060
|
+
|
|
1061
|
+
// 05 闭塞线:比固有色暗很多的窄缝/交界
|
|
1062
|
+
if (enabled.has("occlusion")) {
|
|
1063
|
+
const cap = caps.occlusion;
|
|
1064
|
+
const gap = Math.max(5, Math.round(step * 0.6));
|
|
1065
|
+
const strokes = [];
|
|
1066
|
+
for (let y = gap * 0.5; y < h && strokes.length < cap; y += gap) {
|
|
1067
|
+
for (let x = gap * 0.5; x < w && strokes.length < cap; x += gap) {
|
|
1068
|
+
if (bgMask[Math.min(h - 1, Math.round(y)) * w + Math.min(w - 1, Math.round(x))]) continue;
|
|
1069
|
+
if (toneAt(x, y) > -35) continue;
|
|
1070
|
+
const s = paintSample(data, w, h, x, y, gap * 0.8, whiteCut);
|
|
1071
|
+
if (!s || s.inkN === 0) continue;
|
|
1072
|
+
const f = sampleField(field, x, y);
|
|
1073
|
+
const len = 5 + rnd() * 6;
|
|
1074
|
+
const pts = trimOnWhite(paintFlowPath(field, w, h, x, y, len, f.angle, 3), data, w, h);
|
|
1075
|
+
if (pts.length < 2) continue;
|
|
1076
|
+
const dark = scaleRgb(s.dark, 0.45);
|
|
1077
|
+
strokes.push({ color: rgbHex(dark[0], dark[1], dark[2]), size: Number((1.2 + rnd() * 1.2).toFixed(2)), opacity: 0.55, points: toCanvas(pts, rect) });
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
1080
|
+
stages.push({ key: "occlusion", name: "闭塞线", kind: "brush", brush: "hard", strokes, stats: { items: strokes.length, strokes: strokes.length } });
|
|
1081
|
+
}
|
|
1082
|
+
|
|
1083
|
+
// 09 铺光大关系:比固有色亮的区域,宽而低透明度
|
|
1084
|
+
if (enabled.has("lightblock")) {
|
|
1085
|
+
const cap = caps.lightblock;
|
|
1086
|
+
const gap = Math.max(18, Math.round(step * 2));
|
|
1087
|
+
const strokes = [];
|
|
1088
|
+
for (let y = gap * 0.5; y < h && strokes.length < cap; y += gap) {
|
|
1089
|
+
for (let x = gap * 0.5; x < w && strokes.length < cap; x += gap) {
|
|
1090
|
+
if (bgMask[Math.min(h - 1, Math.round(y)) * w + Math.min(w - 1, Math.round(x))]) continue;
|
|
1091
|
+
if (toneAt(x, y) < 8) continue;
|
|
1092
|
+
const s = paintSample(data, w, h, x, y, gap * 0.9, whiteCut);
|
|
1093
|
+
if (!s || s.inkN === 0) continue;
|
|
1094
|
+
const f = sampleField(field, x, y);
|
|
1095
|
+
const width = gap * (0.8 + 0.3 * rnd());
|
|
1096
|
+
const pts = trimOnWhite(paintFlowPath(field, w, h, x, y, width * 2.2, f.angle, 5), data, w, h);
|
|
1097
|
+
if (pts.length < 2) continue;
|
|
1098
|
+
strokes.push({ color: s.inkHex, size: Number(width.toFixed(2)), opacity: Number((0.08 + 0.08 * rnd()).toFixed(2)), points: toCanvas(pts, rect) });
|
|
1099
|
+
}
|
|
1100
|
+
}
|
|
1101
|
+
stages.push({ key: "lightblock", name: "铺光大关系", kind: "brush", brush: "softround", taper: true, minSize: 0.3, strokes, stats: { items: strokes.length, strokes: strokes.length } });
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
// 10 材质纹理:程序化纸纹/碎笔,渲染时 clip 到 role:base
|
|
1105
|
+
if (enabled.has("texture")) {
|
|
1106
|
+
const cap = caps.texture;
|
|
1107
|
+
const gap = Math.max(5, Math.round(step * 0.7));
|
|
1108
|
+
const strokes = [];
|
|
1109
|
+
for (let y = gap * 0.5; y < h && strokes.length < cap; y += gap) {
|
|
1110
|
+
for (let x = gap * 0.5; x < w && strokes.length < cap; x += gap) {
|
|
1111
|
+
if (rnd() < 0.45) continue;
|
|
1112
|
+
if (bgMask[Math.min(h - 1, Math.round(y)) * w + Math.min(w - 1, Math.round(x))]) continue;
|
|
1113
|
+
const s = paintSample(data, w, h, x, y, gap * 0.7, whiteCut);
|
|
1114
|
+
if (!s || s.inkFrac < 0.1) continue;
|
|
1115
|
+
const f = sampleField(field, x, y);
|
|
1116
|
+
const len = 2 + rnd() * 5;
|
|
1117
|
+
const pts = toCanvas(paintFlowPath(field, w, h, x, y, len, f.angle, 2), rect);
|
|
1118
|
+
if (pts.length < 2) continue;
|
|
1119
|
+
strokes.push({ color: "#8b8f98", size: Number((0.8 + rnd() * 0.8).toFixed(2)), opacity: Number((0.02 + 0.03 * rnd()).toFixed(2)), points: pts });
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1122
|
+
stages.push({ key: "texture", name: "材质纹理", kind: "brush", brush: "pencil", clipTo: "role:base", strokes, stats: { items: strokes.length, strokes: strokes.length } });
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
// 12 镭射反光:沿 11 的高光位置铺色相渐变
|
|
1126
|
+
if (enabled.has("iridescent") && accentStage && accentStage.strokes.length > 0) {
|
|
1127
|
+
const strokes = accentStage.strokes.slice(0, 90).map((line) => {
|
|
1128
|
+
const x = line.points[0][0];
|
|
1129
|
+
const color = shiftHueHex(line.color, ((x / w) - 0.5) * 200);
|
|
1130
|
+
return { color, size: Number((line.size * 1.1).toFixed(2)), opacity: 0.18, points: line.points };
|
|
1131
|
+
});
|
|
1132
|
+
stages.push({ key: "iridescent", name: "镭射反光", kind: "brush", brush: "glow", clipTo: "role:highlight", strokes, stats: { items: strokes.length, strokes: strokes.length } });
|
|
1133
|
+
}
|
|
1134
|
+
|
|
1135
|
+
// 13 氛围光效:画面边缘的大范围色光,overlay 很淡
|
|
1136
|
+
if (enabled.has("atmosphere")) {
|
|
1137
|
+
const cap = caps.atmosphere;
|
|
1138
|
+
const stride = Math.max(30, Math.round(Math.min(w, h) / 12));
|
|
1139
|
+
const strokes = [];
|
|
1140
|
+
for (let y = stride * 0.5; y < h && strokes.length < cap; y += stride) {
|
|
1141
|
+
for (let x = stride * 0.5; x < w && strokes.length < cap; x += stride) {
|
|
1142
|
+
const edge = x < w * 0.18 || x > w * 0.82 || y < h * 0.18 || y > h * 0.9;
|
|
1143
|
+
if (!edge) continue;
|
|
1144
|
+
const s = paintSample(data, w, h, x, y, stride * 0.8, whiteCut);
|
|
1145
|
+
if (!s) continue;
|
|
1146
|
+
const f = sampleField(field, x, y);
|
|
1147
|
+
const width = stride * (1.2 + 0.6 * rnd());
|
|
1148
|
+
const pts = paintFlowPath(field, w, h, x, y, width * 2, f.angle, 4);
|
|
1149
|
+
if (pts.length < 2) continue;
|
|
1150
|
+
strokes.push({ color: s.inkHex, size: Number(width.toFixed(2)), opacity: Number((0.03 + 0.03 * rnd()).toFixed(2)), points: toCanvas(pts, rect) });
|
|
1151
|
+
}
|
|
1152
|
+
}
|
|
1153
|
+
stages.push({ key: "atmosphere", name: "氛围光效", kind: "brush", brush: "airbrush", strokes, stats: { items: strokes.length, strokes: strokes.length } });
|
|
1154
|
+
}
|
|
1155
|
+
|
|
1156
|
+
// 14 调色调整:参数化,不产生笔迹;宿主建一个 grade 层
|
|
1157
|
+
if (enabled.has("grade")) {
|
|
1158
|
+
stages.push({ key: "grade", name: "调色调整", kind: "grade", brush: null, grade: { brightness: 0, contrast: 1, saturation: 1, hue: 0 }, strokes: [], stats: { items: 0 } });
|
|
1159
|
+
}
|
|
1160
|
+
|
|
1161
|
+
const total = stages.reduce((n, s) => n + (s.kind === "fill" ? s.fills.length : (s.kind === "grade" ? 0 : s.strokes.length)), 0);
|
|
1162
|
+
const strokeTotal = stages.reduce((n, s) => n + (s.kind === "fill" || s.kind === "grade" ? 0 : s.strokes.length), 0);
|
|
1163
|
+
const coverage = estimateCoverage(cut, stages);
|
|
1164
|
+
return {
|
|
1165
|
+
stages,
|
|
1166
|
+
total,
|
|
1167
|
+
stats: {
|
|
1168
|
+
step,
|
|
1169
|
+
maxStrokes,
|
|
1170
|
+
caps,
|
|
1171
|
+
field: { grid: field.grid, cells: field.gw * field.gh, avgMag: Number(field.avgMag.toFixed(2)), avgCoh: Number(field.avgCoh.toFixed(3)) },
|
|
1172
|
+
coveragePct: coverage.coveragePct,
|
|
1173
|
+
subjectCells: coverage.subjectCells,
|
|
1174
|
+
coveredCells: coverage.coveredCells,
|
|
1175
|
+
baseCoveragePct: quant && quant.stats ? quant.stats.coveragePct : null,
|
|
1176
|
+
droppedFills: quant && quant.stats ? quant.stats.droppedFills : 0,
|
|
1177
|
+
strokeTotal,
|
|
1178
|
+
stages: stages.map((s) => ({
|
|
1179
|
+
key: s.key,
|
|
1180
|
+
name: s.name,
|
|
1181
|
+
kind: s.kind,
|
|
1182
|
+
items: s.kind === "fill" ? s.fills.length : s.strokes.length,
|
|
1183
|
+
points: s.kind === "fill" ? 0 : s.strokes.reduce((n, x) => n + x.points.length, 0),
|
|
1184
|
+
})),
|
|
1185
|
+
total,
|
|
1186
|
+
capped: strokeTotal >= maxStrokes,
|
|
1187
|
+
disabled: PAINT_STAGE_KEYS.filter((k) => !enabled.has(k)),
|
|
1188
|
+
},
|
|
1189
|
+
};
|
|
1190
|
+
}
|
|
1191
|
+
|
|
14
1192
|
function refLayer(doc, wanted) {
|
|
15
1193
|
const layers = (doc.layers || []).filter((l) => l.image);
|
|
16
1194
|
if (wanted) return layers.find((l) => l.id === wanted || l.name === wanted) || null;
|
|
@@ -152,6 +1330,26 @@ export async function runHeadless(kind, params, doc, resolve) {
|
|
|
152
1330
|
const surf = await refSurface(doc, resolve, params);
|
|
153
1331
|
const rect = clampRect(params.rect, surf.w, surf.h);
|
|
154
1332
|
const cut = cropSurface(surf, rect);
|
|
1333
|
+
const wantPaint = params.style === "paint";
|
|
1334
|
+
// paint 五步管线不用描线/k-means;这里直接返回,省掉 2~3 秒的死计算
|
|
1335
|
+
if (wantPaint) {
|
|
1336
|
+
const paint = paintStrokesFromBitmap(cut, rect, null, {
|
|
1337
|
+
step: params.paintStep,
|
|
1338
|
+
maxStrokes: params.maxStrokes,
|
|
1339
|
+
minLum: params.paintMinLum,
|
|
1340
|
+
stages: params.paintStages,
|
|
1341
|
+
baseColors: params.paintColors,
|
|
1342
|
+
baseFills: params.paintBlocks,
|
|
1343
|
+
baseMinArea: params.paintBaseMinArea,
|
|
1344
|
+
edgeMinLength: params.paintEdgeMinLength,
|
|
1345
|
+
edgeMaxLines: params.paintEdgeMaxLines,
|
|
1346
|
+
});
|
|
1347
|
+
return {
|
|
1348
|
+
ok: true, rect, paths: [], widths: [], lineWidth: null, closed: [], fills: [], palette: [],
|
|
1349
|
+
mode: "paint", smooth: null, fit: null, inkRatio: null, lines: 0, points: 0, dropped: 0, headless: true,
|
|
1350
|
+
paint: paint.total, paintStages: paint.stages, paintStats: paint.stats,
|
|
1351
|
+
};
|
|
1352
|
+
}
|
|
155
1353
|
const mask = inkMask(cut.data, cut.w, cut.h, params.threshold);
|
|
156
1354
|
let inkPx = 0;
|
|
157
1355
|
for (let i = 0; i < mask.length; i += 1) inkPx += mask[i];
|
|
@@ -300,6 +1498,7 @@ export async function runHeadless(kind, params, doc, resolve) {
|
|
|
300
1498
|
const rect = clampRect(params.rect, W, H);
|
|
301
1499
|
const ref = await refSurface(doc, resolve, params);
|
|
302
1500
|
const drawn = await rasterDoc({ ...doc, layers: (doc.layers || []).map((l) => ({ ...l, image: null })) }, { resolve, withImages: false });
|
|
1501
|
+
if (params.paint === true) return comparePaintSurfaces(ref, drawn, rect);
|
|
303
1502
|
return compareSurfaces(ref, drawn, rect);
|
|
304
1503
|
}
|
|
305
1504
|
throw new Error(`无界面模式不支持这个像素任务:${kind}`);
|
|
@@ -379,6 +1578,95 @@ export function compareSurfaces(ref, drawn, rect) {
|
|
|
379
1578
|
};
|
|
380
1579
|
}
|
|
381
1580
|
|
|
1581
|
+
/** 彩稿验收:按「实际覆盖 + RGB 色差」而不是墨迹掩码算,topGaps 里带色差大的格子。 */
|
|
1582
|
+
export function comparePaintSurfaces(ref, drawn, rect) {
|
|
1583
|
+
const rd = ref.data;
|
|
1584
|
+
const dd = drawn.data;
|
|
1585
|
+
const diff = makeSurface(rect.w, rect.h);
|
|
1586
|
+
const out = diff.data;
|
|
1587
|
+
let refPx = 0;
|
|
1588
|
+
let drawnPx = 0;
|
|
1589
|
+
let covered = 0;
|
|
1590
|
+
let extra = 0;
|
|
1591
|
+
let colorSum = 0;
|
|
1592
|
+
let colorN = 0;
|
|
1593
|
+
const GRID = 32;
|
|
1594
|
+
const cells = new Map();
|
|
1595
|
+
for (let y = 0; y < rect.h; y += 1) {
|
|
1596
|
+
for (let x = 0; x < rect.w; x += 1) {
|
|
1597
|
+
const si = ((rect.y + y) * ref.w + (rect.x + x)) * 4;
|
|
1598
|
+
const di = (y * rect.w + x) * 4;
|
|
1599
|
+
const ra = rd[si + 3];
|
|
1600
|
+
const da = dd[di + 3];
|
|
1601
|
+
const isRef = ra > 16;
|
|
1602
|
+
const isDraw = da > 16;
|
|
1603
|
+
if (isRef) refPx += 1;
|
|
1604
|
+
if (isDraw) drawnPx += 1;
|
|
1605
|
+
if (isRef && isDraw) {
|
|
1606
|
+
covered += 1;
|
|
1607
|
+
const dist = (Math.abs(rd[si] - dd[di]) + Math.abs(rd[si + 1] - dd[di + 1]) + Math.abs(rd[si + 2] - dd[di + 2])) / 3;
|
|
1608
|
+
colorSum += dist;
|
|
1609
|
+
colorN += 1;
|
|
1610
|
+
out[di] = Math.round((rd[si] + dd[di]) / 2);
|
|
1611
|
+
out[di + 1] = Math.round((rd[si + 1] + dd[di + 1]) / 2);
|
|
1612
|
+
out[di + 2] = Math.round((rd[si + 2] + dd[di + 2]) / 2);
|
|
1613
|
+
out[di + 3] = 140;
|
|
1614
|
+
if (dist >= 50) {
|
|
1615
|
+
const key = `${Math.floor((x / rect.w) * GRID)},${Math.floor((y / rect.h) * GRID)}`;
|
|
1616
|
+
const cell = cells.get(key) || { x0: Infinity, y0: Infinity, x1: -Infinity, y1: -Infinity, n: 0 };
|
|
1617
|
+
cell.n += 1;
|
|
1618
|
+
cell.x0 = Math.min(cell.x0, rect.x + x);
|
|
1619
|
+
cell.y0 = Math.min(cell.y0, rect.y + y);
|
|
1620
|
+
cell.x1 = Math.max(cell.x1, rect.x + x);
|
|
1621
|
+
cell.y1 = Math.max(cell.y1, rect.y + y);
|
|
1622
|
+
cells.set(key, cell);
|
|
1623
|
+
}
|
|
1624
|
+
} else if (isRef) {
|
|
1625
|
+
// 参考图有色而画布没有:漏画
|
|
1626
|
+
out[di] = 255;
|
|
1627
|
+
out[di + 1] = 0;
|
|
1628
|
+
out[di + 2] = 255;
|
|
1629
|
+
out[di + 3] = 235;
|
|
1630
|
+
const key = `${Math.floor((x / rect.w) * GRID)},${Math.floor((y / rect.h) * GRID)}`;
|
|
1631
|
+
const cell = cells.get(key) || { x0: Infinity, y0: Infinity, x1: -Infinity, y1: -Infinity, n: 0 };
|
|
1632
|
+
cell.n += 1;
|
|
1633
|
+
cell.x0 = Math.min(cell.x0, rect.x + x);
|
|
1634
|
+
cell.y0 = Math.min(cell.y0, rect.y + y);
|
|
1635
|
+
cell.x1 = Math.max(cell.x1, rect.x + x);
|
|
1636
|
+
cell.y1 = Math.max(cell.y1, rect.y + y);
|
|
1637
|
+
cells.set(key, cell);
|
|
1638
|
+
} else if (isDraw) {
|
|
1639
|
+
out[di] = 0;
|
|
1640
|
+
out[di + 1] = 210;
|
|
1641
|
+
out[di + 2] = 255;
|
|
1642
|
+
out[di + 3] = 200;
|
|
1643
|
+
extra += 1;
|
|
1644
|
+
}
|
|
1645
|
+
}
|
|
1646
|
+
}
|
|
1647
|
+
const gaps = [...cells.values()].sort((a, b) => b.n - a.n).slice(0, 4).map((c) => ({
|
|
1648
|
+
x: Math.round(c.x0), y: Math.round(c.y0), w: Math.round(c.x1 - c.x0) + 1, h: Math.round(c.y1 - c.y0) + 1, missed: c.n,
|
|
1649
|
+
}));
|
|
1650
|
+
return {
|
|
1651
|
+
ok: true,
|
|
1652
|
+
dataUrl: dataUrlOf(diff),
|
|
1653
|
+
rect,
|
|
1654
|
+
metrics: {
|
|
1655
|
+
rect,
|
|
1656
|
+
refPixels: refPx,
|
|
1657
|
+
drawnPixels: drawnPx,
|
|
1658
|
+
coveredPixels: covered,
|
|
1659
|
+
extraPixels: extra,
|
|
1660
|
+
coveragePct: refPx ? Number(((covered / refPx) * 100).toFixed(1)) : 100,
|
|
1661
|
+
extraPct: drawnPx ? Number(((extra / drawnPx) * 100).toFixed(1)) : 0,
|
|
1662
|
+
meanColorDiff: colorN ? Number((colorSum / colorN).toFixed(1)) : null,
|
|
1663
|
+
meanDeltaE: colorN ? Number((((colorSum / colorN) / 255) * 100).toFixed(1)) : null,
|
|
1664
|
+
topGaps: gaps,
|
|
1665
|
+
},
|
|
1666
|
+
headless: true,
|
|
1667
|
+
};
|
|
1668
|
+
}
|
|
1669
|
+
|
|
382
1670
|
/** 闭合轮廓取内部主色(宿主版:按点在多边形内采样,不依赖 canvas 遮罩)。 */
|
|
383
1671
|
function regionFillsFromBitmap(cut, traced, palette, opts = {}) {
|
|
384
1672
|
const minFill = Math.max(20, Number(opts.minFill) || 320);
|