@ikijs/mcp 0.6.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +31 -3
- package/dist/{chunk-JTK6HWKT.mjs → chunk-JPWDKP43.mjs} +562 -250
- package/dist/chunk-JPWDKP43.mjs.map +1 -0
- package/dist/cli.js +545 -236
- package/dist/cli.js.map +1 -1
- package/dist/cli.mjs +1 -1
- package/dist/index.d.mts +173 -7
- package/dist/index.d.ts +173 -7
- package/dist/index.js +549 -236
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +5 -1
- package/package.json +2 -2
- package/dist/chunk-JTK6HWKT.mjs.map +0 -1
package/dist/cli.js
CHANGED
|
@@ -28,7 +28,7 @@ var import_mcp = require("@modelcontextprotocol/sdk/server/mcp.js");
|
|
|
28
28
|
var import_zod = require("zod");
|
|
29
29
|
|
|
30
30
|
// src/tools.ts
|
|
31
|
-
var
|
|
31
|
+
var import_node_path3 = __toESM(require("path"));
|
|
32
32
|
var import_format = require("@ikijs/format");
|
|
33
33
|
var import_editor2 = require("@ikijs/editor");
|
|
34
34
|
|
|
@@ -199,6 +199,9 @@ function detectAlphaBbox(rgba, width, height) {
|
|
|
199
199
|
function cropToBuffer(rgba, width, height, bbox) {
|
|
200
200
|
return (0, import_sharp.default)(rgba, { raw: { width, height, channels: 4 } }).extract({ left: bbox.x, top: bbox.y, width: bbox.w, height: bbox.h }).png().toBuffer();
|
|
201
201
|
}
|
|
202
|
+
function encodeOverlayPng(filePath, svg) {
|
|
203
|
+
return (0, import_sharp.default)(filePath, { limitInputPixels: MAX_INPUT_PIXELS }).composite([{ input: Buffer.from(svg) }]).png().toBuffer();
|
|
204
|
+
}
|
|
202
205
|
async function renderAtlasToDataUri(crops, layout, quantizeColors) {
|
|
203
206
|
const byId = /* @__PURE__ */ new Map();
|
|
204
207
|
for (const crop of crops) byId.set(crop.id, crop);
|
|
@@ -228,6 +231,373 @@ async function renderAtlasToDataUri(crops, layout, quantizeColors) {
|
|
|
228
231
|
return `data:image/png;base64,${page.toString("base64")}`;
|
|
229
232
|
}
|
|
230
233
|
|
|
234
|
+
// src/measure-turn.ts
|
|
235
|
+
var import_node_path2 = __toESM(require("path"));
|
|
236
|
+
var DEFAULT_IRIS = { hueMin: 230, hueMax: 300, satMin: 0.22 };
|
|
237
|
+
var IRIS_V_MIN = 0.18;
|
|
238
|
+
var IRIS_V_MAX = 0.95;
|
|
239
|
+
var IRIS_MEAN_V_MIN = 0.32;
|
|
240
|
+
var CLOSE_RADIUS = 3;
|
|
241
|
+
var CLOSE_RADIUS_REFERENCE_PX = 1254;
|
|
242
|
+
var BLOB_MAX_FRAC = 0.12;
|
|
243
|
+
var KEYED_SPAN_MAX_FRAC = 0.95;
|
|
244
|
+
var MIN_AREA_REFERENCE_PIXELS = 1254 * 1254;
|
|
245
|
+
var MIN_AREA = 300;
|
|
246
|
+
var PAIR_DX_MIN_FRAC = 0.08;
|
|
247
|
+
var PAIR_DX_MAX_FRAC = 0.35;
|
|
248
|
+
var PAIR_DY_MAX_FRAC = 0.05;
|
|
249
|
+
var MAX_CANDIDATES = 12;
|
|
250
|
+
var HEAD_BAND = 10;
|
|
251
|
+
var ALPHA_OPAQUE = 128;
|
|
252
|
+
var BG_HUE_MIN = 220;
|
|
253
|
+
var BG_HUE_MAX = 260;
|
|
254
|
+
var BG_SAT_MAX = 0.2;
|
|
255
|
+
var BG_V_MIN = 0.55;
|
|
256
|
+
var BG_V_BLACK = 0.03;
|
|
257
|
+
function rgbToHsv(r, g, b) {
|
|
258
|
+
r /= 255;
|
|
259
|
+
g /= 255;
|
|
260
|
+
b /= 255;
|
|
261
|
+
const max = Math.max(r, g, b);
|
|
262
|
+
const min = Math.min(r, g, b);
|
|
263
|
+
const d = max - min;
|
|
264
|
+
let h = 0;
|
|
265
|
+
if (d > 0) {
|
|
266
|
+
if (max === r) h = (g - b) / d % 6;
|
|
267
|
+
else if (max === g) h = (b - r) / d + 2;
|
|
268
|
+
else h = (r - g) / d + 4;
|
|
269
|
+
h *= 60;
|
|
270
|
+
if (h < 0) h += 360;
|
|
271
|
+
}
|
|
272
|
+
return [h, max === 0 ? 0 : d / max, max];
|
|
273
|
+
}
|
|
274
|
+
function dilate(mask, width, height, r) {
|
|
275
|
+
const out = new Uint8Array(width * height);
|
|
276
|
+
for (let y = 0; y < height; y++) {
|
|
277
|
+
for (let x = 0; x < width; x++) {
|
|
278
|
+
if (!mask[y * width + x]) continue;
|
|
279
|
+
for (let dy = -r; dy <= r; dy++) {
|
|
280
|
+
for (let dx = -r; dx <= r; dx++) {
|
|
281
|
+
const yy = y + dy;
|
|
282
|
+
const xx = x + dx;
|
|
283
|
+
if (yy >= 0 && yy < height && xx >= 0 && xx < width)
|
|
284
|
+
out[yy * width + xx] = 1;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
return out;
|
|
290
|
+
}
|
|
291
|
+
function erode(mask, width, height, r) {
|
|
292
|
+
const out = new Uint8Array(width * height);
|
|
293
|
+
for (let y = 0; y < height; y++) {
|
|
294
|
+
for (let x = 0; x < width; x++) {
|
|
295
|
+
if (!mask[y * width + x]) continue;
|
|
296
|
+
let keep = true;
|
|
297
|
+
for (let dy = -r; dy <= r && keep; dy++) {
|
|
298
|
+
for (let dx = -r; dx <= r; dx++) {
|
|
299
|
+
const yy = y + dy;
|
|
300
|
+
const xx = x + dx;
|
|
301
|
+
if (yy < 0 || yy >= height || xx < 0 || xx >= width || !mask[yy * width + xx]) {
|
|
302
|
+
keep = false;
|
|
303
|
+
break;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
if (keep) out[y * width + x] = 1;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
return out;
|
|
311
|
+
}
|
|
312
|
+
function closeMask(mask, width, height, r) {
|
|
313
|
+
return erode(dilate(mask, width, height, r), width, height, r);
|
|
314
|
+
}
|
|
315
|
+
function components(mask, width, height, minArea) {
|
|
316
|
+
const seen = new Uint8Array(width * height);
|
|
317
|
+
const out = [];
|
|
318
|
+
for (let i = 0; i < width * height; i++) {
|
|
319
|
+
if (!mask[i] || seen[i]) continue;
|
|
320
|
+
const stack = [i];
|
|
321
|
+
seen[i] = 1;
|
|
322
|
+
let x0 = width;
|
|
323
|
+
let x1 = 0;
|
|
324
|
+
let y0 = height;
|
|
325
|
+
let y1 = 0;
|
|
326
|
+
let n = 0;
|
|
327
|
+
let sx = 0;
|
|
328
|
+
let sy = 0;
|
|
329
|
+
while (stack.length) {
|
|
330
|
+
const p = stack.pop();
|
|
331
|
+
const x = p % width;
|
|
332
|
+
const y = p / width | 0;
|
|
333
|
+
n++;
|
|
334
|
+
sx += x;
|
|
335
|
+
sy += y;
|
|
336
|
+
if (x < x0) x0 = x;
|
|
337
|
+
if (x > x1) x1 = x;
|
|
338
|
+
if (y < y0) y0 = y;
|
|
339
|
+
if (y > y1) y1 = y;
|
|
340
|
+
const neighbours = [];
|
|
341
|
+
if (x > 0) neighbours.push(p - 1);
|
|
342
|
+
if (x < width - 1) neighbours.push(p + 1);
|
|
343
|
+
if (y > 0) neighbours.push(p - width);
|
|
344
|
+
if (y < height - 1) neighbours.push(p + width);
|
|
345
|
+
for (const q of neighbours) {
|
|
346
|
+
if (seen[q] || !mask[q]) continue;
|
|
347
|
+
seen[q] = 1;
|
|
348
|
+
stack.push(q);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
if (n >= minArea) {
|
|
352
|
+
out.push({
|
|
353
|
+
x0,
|
|
354
|
+
x1,
|
|
355
|
+
y0,
|
|
356
|
+
y1,
|
|
357
|
+
w: x1 - x0 + 1,
|
|
358
|
+
h: y1 - y0 + 1,
|
|
359
|
+
area: n,
|
|
360
|
+
cx: sx / n,
|
|
361
|
+
cy: sy / n
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
return out.sort((a, b) => b.area - a.area);
|
|
366
|
+
}
|
|
367
|
+
function foregroundSpan(mask, width, height, rowLo, rowHi) {
|
|
368
|
+
let left = width;
|
|
369
|
+
let right = -1;
|
|
370
|
+
for (let y = Math.max(0, rowLo); y <= Math.min(height - 1, rowHi); y++) {
|
|
371
|
+
for (let x = 0; x < width; x++) {
|
|
372
|
+
if (!mask[y * width + x]) continue;
|
|
373
|
+
if (x < left) left = x;
|
|
374
|
+
if (x > right) right = x;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
return { left, right };
|
|
378
|
+
}
|
|
379
|
+
function headHalfOf(span) {
|
|
380
|
+
return (span.right - span.left + 1) / 2;
|
|
381
|
+
}
|
|
382
|
+
function resolveIris(override) {
|
|
383
|
+
const iris = { ...DEFAULT_IRIS, ...override };
|
|
384
|
+
if (iris.hueMin < 0 || iris.hueMax > 360 || iris.hueMin >= iris.hueMax) {
|
|
385
|
+
throw new AutoRigInputError(
|
|
386
|
+
`iris hue window must be 0 <= hueMin < hueMax <= 360, got ${iris.hueMin}..${iris.hueMax}`
|
|
387
|
+
);
|
|
388
|
+
}
|
|
389
|
+
if (iris.satMin < 0 || iris.satMin > 1) {
|
|
390
|
+
throw new AutoRigInputError(
|
|
391
|
+
`iris satMin must be within 0..1, got ${iris.satMin}`
|
|
392
|
+
);
|
|
393
|
+
}
|
|
394
|
+
return iris;
|
|
395
|
+
}
|
|
396
|
+
async function measureTurnImage(filePath, iris) {
|
|
397
|
+
const { width, height, rgba, hasAlpha } = await decodePng(filePath);
|
|
398
|
+
const n = width * height;
|
|
399
|
+
let transparent = false;
|
|
400
|
+
if (hasAlpha) {
|
|
401
|
+
for (let i = 0; i < n; i++) {
|
|
402
|
+
if (rgba[i * 4 + 3] < ALPHA_OPAQUE) {
|
|
403
|
+
transparent = true;
|
|
404
|
+
break;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
const maskMode = hasAlpha && transparent ? "alpha" : "keyed";
|
|
409
|
+
const irisMask = new Uint8Array(n);
|
|
410
|
+
const fg = new Uint8Array(n);
|
|
411
|
+
const value = new Float32Array(n);
|
|
412
|
+
for (let i = 0; i < n; i++) {
|
|
413
|
+
if (rgba[i * 4 + 3] < ALPHA_OPAQUE) continue;
|
|
414
|
+
const [hue, s, v] = rgbToHsv(rgba[i * 4], rgba[i * 4 + 1], rgba[i * 4 + 2]);
|
|
415
|
+
if (maskMode === "alpha") {
|
|
416
|
+
fg[i] = 1;
|
|
417
|
+
} else {
|
|
418
|
+
const isBg = hue >= BG_HUE_MIN && hue <= BG_HUE_MAX && s < BG_SAT_MAX && v > BG_V_MIN || v < BG_V_BLACK;
|
|
419
|
+
if (!isBg) fg[i] = 1;
|
|
420
|
+
}
|
|
421
|
+
if (hue >= iris.hueMin && hue <= iris.hueMax && s > iris.satMin && v > IRIS_V_MIN && v < IRIS_V_MAX) {
|
|
422
|
+
irisMask[i] = 1;
|
|
423
|
+
value[i] = v;
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
const minArea = Math.max(
|
|
427
|
+
1,
|
|
428
|
+
Math.round(MIN_AREA * n / MIN_AREA_REFERENCE_PIXELS)
|
|
429
|
+
);
|
|
430
|
+
const closeRadius = Math.max(
|
|
431
|
+
1,
|
|
432
|
+
Math.round(
|
|
433
|
+
CLOSE_RADIUS * Math.max(width, height) / CLOSE_RADIUS_REFERENCE_PX
|
|
434
|
+
)
|
|
435
|
+
);
|
|
436
|
+
const meanValue = (b) => {
|
|
437
|
+
let count = 0;
|
|
438
|
+
let total = 0;
|
|
439
|
+
for (let y = b.y0; y <= b.y1; y++) {
|
|
440
|
+
for (let x = b.x0; x <= b.x1; x++) {
|
|
441
|
+
const i = y * width + x;
|
|
442
|
+
if (!irisMask[i]) continue;
|
|
443
|
+
count++;
|
|
444
|
+
total += value[i];
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
return count ? total / count : 0;
|
|
448
|
+
};
|
|
449
|
+
const candidates = components(
|
|
450
|
+
closeMask(irisMask, width, height, closeRadius),
|
|
451
|
+
width,
|
|
452
|
+
height,
|
|
453
|
+
minArea
|
|
454
|
+
).filter((b) => b.w < width * BLOB_MAX_FRAC && b.h < height * BLOB_MAX_FRAC).filter((b) => meanValue(b) > IRIS_MEAN_V_MIN).slice(0, MAX_CANDIDATES);
|
|
455
|
+
let best = null;
|
|
456
|
+
for (let i = 0; i < candidates.length; i++) {
|
|
457
|
+
for (let j = i + 1; j < candidates.length; j++) {
|
|
458
|
+
const a = candidates[i];
|
|
459
|
+
const b = candidates[j];
|
|
460
|
+
const dx = Math.abs(a.cx - b.cx);
|
|
461
|
+
const dy = Math.abs(a.cy - b.cy);
|
|
462
|
+
if (dx < width * PAIR_DX_MIN_FRAC || dx > width * PAIR_DX_MAX_FRAC)
|
|
463
|
+
continue;
|
|
464
|
+
if (dy > height * PAIR_DY_MAX_FRAC) continue;
|
|
465
|
+
const score = a.area + b.area;
|
|
466
|
+
if (best === null || score > best.score) {
|
|
467
|
+
const pair = a.cx <= b.cx ? [a, b] : [b, a];
|
|
468
|
+
best = { score, pair };
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
if (best === null) {
|
|
473
|
+
throw new AutoRigInputError(
|
|
474
|
+
`no iris pair found in ${filePath} among ${candidates.length} candidate blobs (iris hue ${iris.hueMin}..${iris.hueMax}, sat > ${iris.satMin})`
|
|
475
|
+
);
|
|
476
|
+
}
|
|
477
|
+
const [irisL, irisR] = best.pair;
|
|
478
|
+
const eyeRow = Math.round((irisL.cy + irisR.cy) / 2);
|
|
479
|
+
const { left, right } = foregroundSpan(
|
|
480
|
+
fg,
|
|
481
|
+
width,
|
|
482
|
+
height,
|
|
483
|
+
eyeRow - HEAD_BAND,
|
|
484
|
+
eyeRow + HEAD_BAND
|
|
485
|
+
);
|
|
486
|
+
if (right <= left) {
|
|
487
|
+
throw new AutoRigInputError(
|
|
488
|
+
`no head span at the eye row (y=${eyeRow}) in ${filePath}: the ${maskMode} foreground mask is empty there`
|
|
489
|
+
);
|
|
490
|
+
}
|
|
491
|
+
if (maskMode === "keyed" && right - left + 1 > width * KEYED_SPAN_MAX_FRAC) {
|
|
492
|
+
throw new AutoRigInputError(
|
|
493
|
+
`head span at the eye row (y=${eyeRow}) in ${filePath} covers ${right - left + 1}/${width} px: background not keyed / not an engine render`
|
|
494
|
+
);
|
|
495
|
+
}
|
|
496
|
+
return {
|
|
497
|
+
file: filePath,
|
|
498
|
+
width,
|
|
499
|
+
height,
|
|
500
|
+
maskMode,
|
|
501
|
+
irisL,
|
|
502
|
+
irisR,
|
|
503
|
+
eyeRow,
|
|
504
|
+
head: {
|
|
505
|
+
left,
|
|
506
|
+
right,
|
|
507
|
+
cx: (left + right) / 2,
|
|
508
|
+
half: headHalfOf({ left, right })
|
|
509
|
+
},
|
|
510
|
+
pairCx: (irisL.cx + irisR.cx) / 2,
|
|
511
|
+
eyeGap: irisR.cx - irisL.cx
|
|
512
|
+
};
|
|
513
|
+
}
|
|
514
|
+
function farNearWidths(m, turnSign) {
|
|
515
|
+
return turnSign < 0 ? { far: m.irisL.w, near: m.irisR.w } : { far: m.irisR.w, near: m.irisL.w };
|
|
516
|
+
}
|
|
517
|
+
function overlaySvg(m) {
|
|
518
|
+
const box = (b, stroke) => `<rect x="${b.x0}" y="${b.y0}" width="${b.w}" height="${b.h}" fill="none" stroke="${stroke}" stroke-width="3"/>`;
|
|
519
|
+
const tick = (x, half, stroke) => `<line x1="${x}" y1="${m.eyeRow - half}" x2="${x}" y2="${m.eyeRow + half}" stroke="${stroke}" stroke-width="3"/>`;
|
|
520
|
+
return `<svg width="${m.width}" height="${m.height}">` + box(m.irisL, "#ff0000") + box(m.irisR, "#00c000") + tick(m.head.left, 40, "#0000ff") + tick(m.head.right, 40, "#0000ff") + tick(m.head.cx, 60, "#ff8800") + tick(m.pairCx, 60, "#ff00ff") + "</svg>";
|
|
521
|
+
}
|
|
522
|
+
async function writeOverlay(debugDir, label, m) {
|
|
523
|
+
const outPath = import_node_path2.default.join(
|
|
524
|
+
debugDir,
|
|
525
|
+
`${label}-${import_node_path2.default.basename(m.file, ".png")}.debug.png`
|
|
526
|
+
);
|
|
527
|
+
writeFileAtomic(outPath, await encodeOverlayPng(m.file, overlaySvg(m)));
|
|
528
|
+
return outPath;
|
|
529
|
+
}
|
|
530
|
+
var IRIS_HEIGHT_SCALE_TOLERANCE = 0.1;
|
|
531
|
+
async function measureTurnReference(input) {
|
|
532
|
+
try {
|
|
533
|
+
const iris = resolveIris(input.iris);
|
|
534
|
+
const frontPath = resolveInputPath(input.front);
|
|
535
|
+
const turnedPath = resolveInputPath(input.turned);
|
|
536
|
+
const debugDir = input.debugDir === void 0 ? null : resolveOutputDir(input.debugDir);
|
|
537
|
+
const front = await measureTurnImage(frontPath, iris);
|
|
538
|
+
const turned = await measureTurnImage(turnedPath, iris);
|
|
539
|
+
const frontIrisH = (front.irisL.h + front.irisR.h) / 2;
|
|
540
|
+
const turnedIrisH = (turned.irisL.h + turned.irisR.h) / 2;
|
|
541
|
+
if (Math.abs(turnedIrisH - frontIrisH) / frontIrisH > IRIS_HEIGHT_SCALE_TOLERANCE) {
|
|
542
|
+
throw new AutoRigInputError(
|
|
543
|
+
`front and turned images are not at the same scale: mean iris height ${frontIrisH.toFixed(1)}px front vs ${turnedIrisH.toFixed(1)}px turned, more than ${(IRIS_HEIGHT_SCALE_TOLERANCE * 100).toFixed(0)}% apart \u2014 a yaw does not change iris height, so the pair must share the same framing and head size`
|
|
544
|
+
);
|
|
545
|
+
}
|
|
546
|
+
const frontShift = front.pairCx - front.head.cx;
|
|
547
|
+
const turnedShift = turned.pairCx - turned.head.cx;
|
|
548
|
+
const shift = turnedShift - frontShift;
|
|
549
|
+
const turnSign = shift < 0 ? -1 : 1;
|
|
550
|
+
const f = farNearWidths(front, turnSign);
|
|
551
|
+
const t = farNearWidths(turned, turnSign);
|
|
552
|
+
const debug = debugDir === null ? void 0 : [
|
|
553
|
+
await writeOverlay(debugDir, "front", front),
|
|
554
|
+
await writeOverlay(debugDir, "turned", turned)
|
|
555
|
+
];
|
|
556
|
+
return {
|
|
557
|
+
ok: true,
|
|
558
|
+
front,
|
|
559
|
+
turned,
|
|
560
|
+
farEyeRatio: t.far / t.near / (f.far / f.near),
|
|
561
|
+
eyeShift: shift / front.head.half,
|
|
562
|
+
silhouetteRatio: turned.head.half / front.head.half,
|
|
563
|
+
turnSign,
|
|
564
|
+
...debug === void 0 ? {} : { debug }
|
|
565
|
+
};
|
|
566
|
+
} catch (err) {
|
|
567
|
+
if (err instanceof AutoRigInputError)
|
|
568
|
+
return { ok: false, error: err.message };
|
|
569
|
+
throw err;
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
var n1 = (v) => v.toFixed(1);
|
|
573
|
+
var n3 = (v) => v.toFixed(3);
|
|
574
|
+
function imageBlock(label, m) {
|
|
575
|
+
return [
|
|
576
|
+
`# ${label} ${m.file} ${m.width}x${m.height} mask: ${m.maskMode}`,
|
|
577
|
+
`iris widths ${m.irisL.w} / ${m.irisR.w} px centres ${n1(m.irisL.cx)} / ${n1(m.irisR.cx)} eye row ${m.eyeRow} gap ${n1(m.eyeGap)}`,
|
|
578
|
+
`head ${m.head.left}..${m.head.right} centre ${n1(m.head.cx)} half ${n1(m.head.half)} pair centre ${n1(m.pairCx)}`
|
|
579
|
+
];
|
|
580
|
+
}
|
|
581
|
+
function formatTurnReport(result) {
|
|
582
|
+
const { front, turned } = result;
|
|
583
|
+
const f = farNearWidths(front, result.turnSign);
|
|
584
|
+
const t = farNearWidths(turned, result.turnSign);
|
|
585
|
+
const lines = [
|
|
586
|
+
`farEyeRatio ${n3(result.farEyeRatio)} far/near iris width ${t.far}/${t.near} turned over ${f.far}/${f.near} front`,
|
|
587
|
+
`eyeShift ${n3(result.eyeShift)} pair-vs-head centre ${n1(turned.pairCx - turned.head.cx)} px turned, ${n1(front.pairCx - front.head.cx)} px front, over front head half ${n1(front.head.half)} px (hh)`,
|
|
588
|
+
`silhouetteRatio ${n3(result.silhouetteRatio)} head half ${n1(turned.head.half)} px turned over ${n1(front.head.half)} px front`,
|
|
589
|
+
`turnSign ${result.turnSign} (the head turned toward the image's ${result.turnSign < 0 ? "left" : "right"})`,
|
|
590
|
+
"",
|
|
591
|
+
...imageBlock("front", front),
|
|
592
|
+
"",
|
|
593
|
+
...imageBlock("turned", turned)
|
|
594
|
+
];
|
|
595
|
+
if (result.debug !== void 0) {
|
|
596
|
+
lines.push("", ...result.debug.map((p) => `debug overlay ${p}`));
|
|
597
|
+
}
|
|
598
|
+
return lines.join("\n");
|
|
599
|
+
}
|
|
600
|
+
|
|
231
601
|
// src/tools.ts
|
|
232
602
|
function coerceModel(model) {
|
|
233
603
|
try {
|
|
@@ -372,6 +742,38 @@ function expectInput(label, fn) {
|
|
|
372
742
|
);
|
|
373
743
|
}
|
|
374
744
|
}
|
|
745
|
+
function eyeRowOf(layers) {
|
|
746
|
+
const centreY = (role) => {
|
|
747
|
+
const layer = layers.find((l) => l.role === role);
|
|
748
|
+
return layer && layer.bbox.y + layer.bbox.h / 2;
|
|
749
|
+
};
|
|
750
|
+
const pairRow = (left, right) => {
|
|
751
|
+
const a = centreY(left);
|
|
752
|
+
const b = centreY(right);
|
|
753
|
+
return a === void 0 || b === void 0 ? void 0 : (a + b) / 2;
|
|
754
|
+
};
|
|
755
|
+
const row = pairRow("iris_L", "iris_R") ?? pairRow("eye_L", "eye_R");
|
|
756
|
+
if (row === void 0) {
|
|
757
|
+
throw new Error("auto-rig: no eye pair to measure the head width at");
|
|
758
|
+
}
|
|
759
|
+
return Math.round(row);
|
|
760
|
+
}
|
|
761
|
+
function foregroundSpanOfRows(rowLeft, rowRight, rowLo, rowHi) {
|
|
762
|
+
let left = Infinity;
|
|
763
|
+
let right = -Infinity;
|
|
764
|
+
for (let y = Math.max(0, rowLo); y <= Math.min(rowLeft.length - 1, rowHi); y++) {
|
|
765
|
+
if (rowLeft[y] < left) left = rowLeft[y];
|
|
766
|
+
if (rowRight[y] > right) right = rowRight[y];
|
|
767
|
+
}
|
|
768
|
+
return { left, right };
|
|
769
|
+
}
|
|
770
|
+
function facePlateHalfOf(layers) {
|
|
771
|
+
const face = layers.find((l) => l.role === "face");
|
|
772
|
+
if (face === void 0) {
|
|
773
|
+
throw new Error("auto-rig: no face layer to measure the plate against");
|
|
774
|
+
}
|
|
775
|
+
return face.cropW / 2;
|
|
776
|
+
}
|
|
375
777
|
async function autoRigFromLayers(input) {
|
|
376
778
|
try {
|
|
377
779
|
const layers = input.layers;
|
|
@@ -394,7 +796,7 @@ async function autoRigFromLayers(input) {
|
|
|
394
796
|
}
|
|
395
797
|
const resolvedLayers = layers.map((layer) => {
|
|
396
798
|
const resolved = resolveInputPath(layer.path);
|
|
397
|
-
return { resolved, fileName: layer.fileName ??
|
|
799
|
+
return { resolved, fileName: layer.fileName ?? import_node_path3.default.basename(resolved) };
|
|
398
800
|
});
|
|
399
801
|
const rolePairs = expectInput(
|
|
400
802
|
"role parsing",
|
|
@@ -404,8 +806,10 @@ async function autoRigFromLayers(input) {
|
|
|
404
806
|
let canvasW = 0;
|
|
405
807
|
let canvasH = 0;
|
|
406
808
|
let totalPixels = 0;
|
|
809
|
+
let opaque = new Uint8Array(0);
|
|
407
810
|
const layerInputs = [];
|
|
408
811
|
const crops = [];
|
|
812
|
+
const rowSpansByRole = [];
|
|
409
813
|
for (let i = 0; i < resolvedLayers.length; i++) {
|
|
410
814
|
const { resolved, fileName } = resolvedLayers[i];
|
|
411
815
|
const png = await decodePng(resolved);
|
|
@@ -428,6 +832,7 @@ async function autoRigFromLayers(input) {
|
|
|
428
832
|
`canvas ${canvasW}x${canvasH} exceeds ${MAX_CANVAS_DIM}`
|
|
429
833
|
);
|
|
430
834
|
}
|
|
835
|
+
opaque = new Uint8Array(canvasW * canvasH);
|
|
431
836
|
} else if (png.width !== canvasW || png.height !== canvasH) {
|
|
432
837
|
throw new AutoRigInputError(
|
|
433
838
|
`layer "${fileName}" size ${png.width}x${png.height} differs from canvas ${canvasW}x${canvasH}`
|
|
@@ -447,6 +852,18 @@ async function autoRigFromLayers(input) {
|
|
|
447
852
|
);
|
|
448
853
|
}
|
|
449
854
|
const buffer = await cropToBuffer(png.rgba, png.width, png.height, bbox);
|
|
855
|
+
const rowLeft = new Int32Array(canvasH).fill(canvasW);
|
|
856
|
+
const rowRight = new Int32Array(canvasH).fill(-1);
|
|
857
|
+
for (let y = 0; y < canvasH; y++) {
|
|
858
|
+
for (let x = 0; x < canvasW; x++) {
|
|
859
|
+
const p = y * canvasW + x;
|
|
860
|
+
if (png.rgba[p * 4 + 3] < ALPHA_OPAQUE) continue;
|
|
861
|
+
opaque[p] = 1;
|
|
862
|
+
if (x < rowLeft[y]) rowLeft[y] = x;
|
|
863
|
+
if (x > rowRight[y]) rowRight[y] = x;
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
rowSpansByRole.push({ role, rowLeft, rowRight });
|
|
450
867
|
layerInputs.push({
|
|
451
868
|
role,
|
|
452
869
|
fileName,
|
|
@@ -458,10 +875,56 @@ async function autoRigFromLayers(input) {
|
|
|
458
875
|
});
|
|
459
876
|
crops.push({ id: role, buffer, width: bbox.w, height: bbox.h });
|
|
460
877
|
}
|
|
461
|
-
const
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
878
|
+
const eyeRow = eyeRowOf(layerInputs);
|
|
879
|
+
const span = foregroundSpan(
|
|
880
|
+
opaque,
|
|
881
|
+
canvasW,
|
|
882
|
+
canvasH,
|
|
883
|
+
eyeRow - HEAD_BAND,
|
|
884
|
+
eyeRow + HEAD_BAND
|
|
885
|
+
);
|
|
886
|
+
const headHalfWidth = span.right > span.left ? headHalfOf(span) : void 0;
|
|
887
|
+
const headHalfWidthApplied = headHalfWidth !== void 0 && headHalfWidth > facePlateHalfOf(layerInputs);
|
|
888
|
+
let headEdges;
|
|
889
|
+
if (headHalfWidthApplied) {
|
|
890
|
+
const bandLo = eyeRow - HEAD_BAND;
|
|
891
|
+
const bandHi = eyeRow + HEAD_BAND;
|
|
892
|
+
const left = [];
|
|
893
|
+
const right = [];
|
|
894
|
+
for (const { role, rowLeft, rowRight } of rowSpansByRole) {
|
|
895
|
+
const own = foregroundSpanOfRows(rowLeft, rowRight, bandLo, bandHi);
|
|
896
|
+
if (own.right <= own.left) continue;
|
|
897
|
+
left.push({ role, x: own.left - canvasW / 2 });
|
|
898
|
+
right.push({ role, x: own.right - canvasW / 2 });
|
|
899
|
+
}
|
|
900
|
+
headEdges = { left, right };
|
|
901
|
+
}
|
|
902
|
+
let turn;
|
|
903
|
+
let model;
|
|
904
|
+
try {
|
|
905
|
+
const { eyeShift, farEyeRatio, silhouetteRatio, noseShift, mouthShift } = input.turnTargets ?? {};
|
|
906
|
+
model = (0, import_editor2.generateIkiFromLayerSet)(
|
|
907
|
+
layerInputs,
|
|
908
|
+
{ width: canvasW, height: canvasH },
|
|
909
|
+
{
|
|
910
|
+
turnTargets: {
|
|
911
|
+
eyeShift,
|
|
912
|
+
farEyeRatio,
|
|
913
|
+
silhouetteRatio,
|
|
914
|
+
noseShift,
|
|
915
|
+
mouthShift,
|
|
916
|
+
...headHalfWidthApplied ? { headHalfWidth } : {}
|
|
917
|
+
},
|
|
918
|
+
onTurnSolved: (report) => {
|
|
919
|
+
turn = report;
|
|
920
|
+
},
|
|
921
|
+
...headEdges === void 0 ? {} : { headEdges }
|
|
922
|
+
}
|
|
923
|
+
);
|
|
924
|
+
} catch (e) {
|
|
925
|
+
if (!(e instanceof import_editor2.TurnTargetError)) throw e;
|
|
926
|
+
throw new AutoRigInputError(e.message);
|
|
927
|
+
}
|
|
465
928
|
const doc = new import_editor2.EditorDocument(model);
|
|
466
929
|
const layout = (0, import_editor2.packAtlas)(
|
|
467
930
|
crops.map((c) => ({ id: c.id, width: c.width, height: c.height }))
|
|
@@ -498,7 +961,11 @@ async function autoRigFromLayers(input) {
|
|
|
498
961
|
path: outPath,
|
|
499
962
|
canvas: { width: canvasW, height: canvasH },
|
|
500
963
|
partCount: finalModel.parts.length,
|
|
501
|
-
atlasBytes: dataUri.length
|
|
964
|
+
atlasBytes: dataUri.length,
|
|
965
|
+
...headHalfWidth === void 0 ? {} : { headHalfWidth },
|
|
966
|
+
headHalfWidthApplied,
|
|
967
|
+
...headEdges === void 0 ? {} : { headEdges },
|
|
968
|
+
...turn === void 0 ? {} : { turn }
|
|
502
969
|
};
|
|
503
970
|
} catch (err) {
|
|
504
971
|
if (err instanceof AutoRigInputError)
|
|
@@ -509,12 +976,12 @@ async function autoRigFromLayers(input) {
|
|
|
509
976
|
|
|
510
977
|
// src/compose.ts
|
|
511
978
|
var import_node_fs3 = __toESM(require("fs"));
|
|
512
|
-
var
|
|
979
|
+
var import_node_path5 = __toESM(require("path"));
|
|
513
980
|
var import_sharp2 = __toESM(require("sharp"));
|
|
514
981
|
|
|
515
982
|
// src/measure.ts
|
|
516
983
|
var import_node_fs2 = __toESM(require("fs"));
|
|
517
|
-
var
|
|
984
|
+
var import_node_path4 = __toESM(require("path"));
|
|
518
985
|
var IRIS_RATIO_MIN = 0.45;
|
|
519
986
|
var IRIS_RATIO_MAX = 0.72;
|
|
520
987
|
var EYE_ASPECT_MIN = 0.5;
|
|
@@ -615,8 +1082,8 @@ async function measureDir(absDir) {
|
|
|
615
1082
|
const layers = {};
|
|
616
1083
|
const empty = [];
|
|
617
1084
|
for (const f of files) {
|
|
618
|
-
const role =
|
|
619
|
-
const m = await layerStats(
|
|
1085
|
+
const role = import_node_path4.default.basename(f, ".png");
|
|
1086
|
+
const m = await layerStats(import_node_path4.default.join(absDir, f));
|
|
620
1087
|
if (!m) {
|
|
621
1088
|
empty.push(role);
|
|
622
1089
|
continue;
|
|
@@ -694,7 +1161,11 @@ async function measureDir(absDir) {
|
|
|
694
1161
|
}
|
|
695
1162
|
for (const [role, why] of [
|
|
696
1163
|
["body", "without it the character reads as a floating head on head-turn"],
|
|
697
|
-
["hair_back", "without it the silhouette is flat behind the face"]
|
|
1164
|
+
["hair_back", "without it the silhouette is flat behind the face"],
|
|
1165
|
+
[
|
|
1166
|
+
"nose",
|
|
1167
|
+
"without it nothing on the face slides on the head turn \u2014 the features stay on the plate"
|
|
1168
|
+
]
|
|
698
1169
|
]) {
|
|
699
1170
|
if (layers[role] === void 0) warnings.push(`${role}: missing \u2014 ${why}.`);
|
|
700
1171
|
}
|
|
@@ -748,13 +1219,6 @@ var IRIS_W = Math.round(EYE_W * 0.5625);
|
|
|
748
1219
|
var EYEWHITE_SRC = "eyewhite.png";
|
|
749
1220
|
var EYE_LASH_LUMA = 120;
|
|
750
1221
|
var LASH_KEEP_FRACTION = 0.5;
|
|
751
|
-
var NOSE_INK_TOL = 20;
|
|
752
|
-
var NOSE_INK_LOW = 8;
|
|
753
|
-
var NOSE_GROW = 2;
|
|
754
|
-
var NOSE_MIN_PIXELS = 12;
|
|
755
|
-
var NOSE_MAX_CUT_FRACTION = 0.25;
|
|
756
|
-
var NOSE_ROW_MIN_PIXELS = 8;
|
|
757
|
-
var NOSE_FILL_REACH = 8;
|
|
758
1222
|
var DEFAULT_LAYOUT = {
|
|
759
1223
|
// Back hair and body sit behind the face. Both are OPTIONAL: a parts dir
|
|
760
1224
|
// without them still composes (head-only character).
|
|
@@ -764,6 +1228,11 @@ var DEFAULT_LAYOUT = {
|
|
|
764
1228
|
// not a floating head.
|
|
765
1229
|
body: { src: "body.png", cx: 550, cy: 1017, w: 840, optional: true },
|
|
766
1230
|
face: { src: "face.png", cx: 550, cy: 475, w: 400 },
|
|
1231
|
+
// The nose, drawn on its own (the face is drawn without one): the auto-rig
|
|
1232
|
+
// leads the head turn with it and keys the other features' slide on its
|
|
1233
|
+
// presence, so without it the features stay on the face plate. Sits between
|
|
1234
|
+
// the eyes and above the mouth. OPTIONAL: a parts dir without it composes.
|
|
1235
|
+
nose: { src: "nose.png", cx: 550, cy: 586, w: 40, optional: true },
|
|
767
1236
|
mouth: { src: "mouth.png", cx: 550, cy: 619, w: 68 },
|
|
768
1237
|
// Cross-fades with `mouth` on ParamMouthOpenY (opacity, not scaleY) once the
|
|
769
1238
|
// auto-rig sees both roles. Same cx/w as `mouth` so the lip width matches;
|
|
@@ -805,6 +1274,7 @@ var ORDER = [
|
|
|
805
1274
|
"hair_back",
|
|
806
1275
|
"body",
|
|
807
1276
|
"face",
|
|
1277
|
+
"nose",
|
|
808
1278
|
"mouth",
|
|
809
1279
|
"mouth_open",
|
|
810
1280
|
"eye_L",
|
|
@@ -866,7 +1336,7 @@ async function partBuffer(role, cfg, partsDir, inMemory) {
|
|
|
866
1336
|
if (inMemory !== void 0) {
|
|
867
1337
|
img = (0, import_sharp2.default)(inMemory);
|
|
868
1338
|
} else {
|
|
869
|
-
const srcPath =
|
|
1339
|
+
const srcPath = import_node_path5.default.join(partsDir, cfg.src);
|
|
870
1340
|
if (!import_node_fs3.default.existsSync(srcPath)) {
|
|
871
1341
|
if (cfg.optional) return null;
|
|
872
1342
|
throw new AutoRigInputError(
|
|
@@ -921,7 +1391,7 @@ function blankCanvas() {
|
|
|
921
1391
|
});
|
|
922
1392
|
}
|
|
923
1393
|
async function prepEyeSplit(partsDir) {
|
|
924
|
-
const srcPath =
|
|
1394
|
+
const srcPath = import_node_path5.default.join(partsDir, EYEWHITE_SRC);
|
|
925
1395
|
if (!import_node_fs3.default.existsSync(srcPath)) {
|
|
926
1396
|
throw new AutoRigInputError(`missing eyewhite source: ${srcPath}`);
|
|
927
1397
|
}
|
|
@@ -966,164 +1436,6 @@ async function prepEyeSplit(partsDir) {
|
|
|
966
1436
|
lash: await cropToBuffer(lash, W, H, region)
|
|
967
1437
|
};
|
|
968
1438
|
}
|
|
969
|
-
function cutNose(rgba, box) {
|
|
970
|
-
const W = CANVAS;
|
|
971
|
-
const x0 = Math.max(0, box.x0);
|
|
972
|
-
const y0 = Math.max(0, box.y0);
|
|
973
|
-
const x1 = Math.min(W, box.x1);
|
|
974
|
-
const y1 = Math.min(W, box.y1);
|
|
975
|
-
if (x1 - x0 < 4 || y1 - y0 < 4) return null;
|
|
976
|
-
const bw = x1 - x0;
|
|
977
|
-
const bh = y1 - y0;
|
|
978
|
-
const at = (x, y) => (y * W + x) * 4;
|
|
979
|
-
const median = (v) => v.sort((a, b) => a - b)[v.length >> 1];
|
|
980
|
-
const all = [[], [], []];
|
|
981
|
-
const rows = [];
|
|
982
|
-
for (let y = y0; y < y1; y++) {
|
|
983
|
-
const row = [[], [], []];
|
|
984
|
-
for (let x = x0; x < x1; x++) {
|
|
985
|
-
const i = at(x, y);
|
|
986
|
-
if (rgba[i + 3] === 0) continue;
|
|
987
|
-
for (let c = 0; c < 3; c++) {
|
|
988
|
-
row[c].push(rgba[i + c]);
|
|
989
|
-
all[c].push(rgba[i + c]);
|
|
990
|
-
}
|
|
991
|
-
}
|
|
992
|
-
rows.push(row);
|
|
993
|
-
}
|
|
994
|
-
const opaqueCount = all[0].length;
|
|
995
|
-
if (opaqueCount === 0) return null;
|
|
996
|
-
const skin = all.map(median);
|
|
997
|
-
const skinRow = rows.map(
|
|
998
|
-
(row) => row[0].length < NOSE_ROW_MIN_PIXELS ? skin : row.map(median)
|
|
999
|
-
);
|
|
1000
|
-
const distAt = (i, y) => {
|
|
1001
|
-
const ref = skinRow[y - y0];
|
|
1002
|
-
return Math.max(
|
|
1003
|
-
Math.abs(rgba[i] - ref[0]),
|
|
1004
|
-
Math.abs(rgba[i + 1] - ref[1]),
|
|
1005
|
-
Math.abs(rgba[i + 2] - ref[2])
|
|
1006
|
-
);
|
|
1007
|
-
};
|
|
1008
|
-
const dist = new Uint8Array(bw * bh);
|
|
1009
|
-
const ink = new Uint8Array(bw * bh);
|
|
1010
|
-
const queue = [];
|
|
1011
|
-
let inkCount = 0;
|
|
1012
|
-
for (let y = y0; y < y1; y++) {
|
|
1013
|
-
for (let x = x0; x < x1; x++) {
|
|
1014
|
-
const i = at(x, y);
|
|
1015
|
-
if (rgba[i + 3] === 0) continue;
|
|
1016
|
-
const d = distAt(i, y);
|
|
1017
|
-
const k = (y - y0) * bw + (x - x0);
|
|
1018
|
-
dist[k] = d;
|
|
1019
|
-
if (d > NOSE_INK_TOL) {
|
|
1020
|
-
ink[k] = 1;
|
|
1021
|
-
queue.push(k);
|
|
1022
|
-
inkCount++;
|
|
1023
|
-
}
|
|
1024
|
-
}
|
|
1025
|
-
}
|
|
1026
|
-
if (inkCount < NOSE_MIN_PIXELS) return null;
|
|
1027
|
-
while (queue.length > 0) {
|
|
1028
|
-
const k = queue.pop();
|
|
1029
|
-
const kx = k % bw;
|
|
1030
|
-
const ky = (k - kx) / bw;
|
|
1031
|
-
for (let dy = -NOSE_GROW; dy <= NOSE_GROW; dy++) {
|
|
1032
|
-
for (let dx = -NOSE_GROW; dx <= NOSE_GROW; dx++) {
|
|
1033
|
-
const nx = kx + dx;
|
|
1034
|
-
const ny = ky + dy;
|
|
1035
|
-
if (nx < 0 || nx >= bw || ny < 0 || ny >= bh) continue;
|
|
1036
|
-
const n = ny * bw + nx;
|
|
1037
|
-
if (ink[n] === 0 && dist[n] > NOSE_INK_LOW) {
|
|
1038
|
-
ink[n] = 1;
|
|
1039
|
-
queue.push(n);
|
|
1040
|
-
}
|
|
1041
|
-
}
|
|
1042
|
-
}
|
|
1043
|
-
}
|
|
1044
|
-
const cut = new Uint8Array(bw * bh);
|
|
1045
|
-
for (let y = 0; y < bh; y++) {
|
|
1046
|
-
for (let x = 0; x < bw; x++) {
|
|
1047
|
-
if (ink[y * bw + x] === 0) continue;
|
|
1048
|
-
for (let dy = -NOSE_GROW; dy <= NOSE_GROW; dy++) {
|
|
1049
|
-
for (let dx = -NOSE_GROW; dx <= NOSE_GROW; dx++) {
|
|
1050
|
-
const yy = y + dy;
|
|
1051
|
-
const xx = x + dx;
|
|
1052
|
-
if (yy >= 0 && yy < bh && xx >= 0 && xx < bw) cut[yy * bw + xx] = 1;
|
|
1053
|
-
}
|
|
1054
|
-
}
|
|
1055
|
-
}
|
|
1056
|
-
}
|
|
1057
|
-
let cutCount = 0;
|
|
1058
|
-
for (let y = 0; y < bh; y++) {
|
|
1059
|
-
for (let x = 0; x < bw; x++) {
|
|
1060
|
-
if (cut[y * bw + x] === 0) continue;
|
|
1061
|
-
const a = rgba[at(x0 + x, y0 + y) + 3];
|
|
1062
|
-
if (a === 0) continue;
|
|
1063
|
-
if (a < 255) return null;
|
|
1064
|
-
cutCount++;
|
|
1065
|
-
}
|
|
1066
|
-
}
|
|
1067
|
-
if (cutCount > NOSE_MAX_CUT_FRACTION * opaqueCount) return null;
|
|
1068
|
-
const face = Buffer.from(rgba);
|
|
1069
|
-
const nose = Buffer.from(rgba);
|
|
1070
|
-
for (let i = 3; i < nose.length; i += 4) nose[i] = 0;
|
|
1071
|
-
let minX = W;
|
|
1072
|
-
let minY = W;
|
|
1073
|
-
let maxX = -1;
|
|
1074
|
-
let maxY = -1;
|
|
1075
|
-
for (let y = 0; y < bh; y++) {
|
|
1076
|
-
const cy = y0 + y;
|
|
1077
|
-
let x = 0;
|
|
1078
|
-
while (x < bw) {
|
|
1079
|
-
if (cut[y * bw + x] === 0) {
|
|
1080
|
-
x++;
|
|
1081
|
-
continue;
|
|
1082
|
-
}
|
|
1083
|
-
const start = x;
|
|
1084
|
-
while (x < bw && cut[y * bw + x] === 1) x++;
|
|
1085
|
-
const end = x;
|
|
1086
|
-
const skinAt = (bx) => {
|
|
1087
|
-
for (let step = 0; step < NOSE_FILL_REACH && bx >= 0 && bx < bw; step++) {
|
|
1088
|
-
const i = at(x0 + bx, cy);
|
|
1089
|
-
if (rgba[i + 3] > 0 && cut[y * bw + bx] === 0) {
|
|
1090
|
-
if (distAt(i, cy) <= NOSE_INK_TOL / 2) return i;
|
|
1091
|
-
}
|
|
1092
|
-
bx += bx < start ? -1 : 1;
|
|
1093
|
-
}
|
|
1094
|
-
return -1;
|
|
1095
|
-
};
|
|
1096
|
-
const l = skinAt(start - 1);
|
|
1097
|
-
const r = skinAt(end);
|
|
1098
|
-
for (let k = start; k < end; k++) {
|
|
1099
|
-
const cx = x0 + k;
|
|
1100
|
-
const i = at(cx, cy);
|
|
1101
|
-
if (rgba[i + 3] === 0) continue;
|
|
1102
|
-
const t = (k - start + 1) / (end - start + 1);
|
|
1103
|
-
for (let c = 0; c < 3; c++) {
|
|
1104
|
-
const rowSkin = skinRow[y];
|
|
1105
|
-
const lc = l >= 0 ? rgba[l + c] : r >= 0 ? rgba[r + c] : rowSkin[c];
|
|
1106
|
-
const rc = r >= 0 ? rgba[r + c] : l >= 0 ? rgba[l + c] : rowSkin[c];
|
|
1107
|
-
face[i + c] = Math.round(lc * (1 - t) + rc * t);
|
|
1108
|
-
}
|
|
1109
|
-
nose[i] = rgba[i];
|
|
1110
|
-
nose[i + 1] = rgba[i + 1];
|
|
1111
|
-
nose[i + 2] = rgba[i + 2];
|
|
1112
|
-
nose[i + 3] = rgba[i + 3];
|
|
1113
|
-
if (cx < minX) minX = cx;
|
|
1114
|
-
if (cx > maxX) maxX = cx;
|
|
1115
|
-
if (cy < minY) minY = cy;
|
|
1116
|
-
if (cy > maxY) maxY = cy;
|
|
1117
|
-
}
|
|
1118
|
-
}
|
|
1119
|
-
}
|
|
1120
|
-
if (maxX < 0) return null;
|
|
1121
|
-
return {
|
|
1122
|
-
face,
|
|
1123
|
-
nose,
|
|
1124
|
-
bbox: { x: minX, y: minY, w: maxX - minX + 1, h: maxY - minY + 1 }
|
|
1125
|
-
};
|
|
1126
|
-
}
|
|
1127
1439
|
async function composeLayersFromParts(input) {
|
|
1128
1440
|
try {
|
|
1129
1441
|
const outDir = resolveOutputDir(input.outDir);
|
|
@@ -1142,7 +1454,6 @@ async function composeLayersFromParts(input) {
|
|
|
1142
1454
|
const layers = [];
|
|
1143
1455
|
const skipped = [];
|
|
1144
1456
|
const preview = [];
|
|
1145
|
-
let faceRaw;
|
|
1146
1457
|
for (const role of ORDER) {
|
|
1147
1458
|
const cfg = layout[role];
|
|
1148
1459
|
const part = await partBuffer(
|
|
@@ -1153,19 +1464,13 @@ async function composeLayersFromParts(input) {
|
|
|
1153
1464
|
);
|
|
1154
1465
|
if (part === null) {
|
|
1155
1466
|
skipped.push(role);
|
|
1156
|
-
import_node_fs3.default.rmSync(
|
|
1467
|
+
import_node_fs3.default.rmSync(import_node_path5.default.join(outDir, `${role}.png`), { force: true });
|
|
1157
1468
|
continue;
|
|
1158
1469
|
}
|
|
1159
1470
|
const { left, top } = placement(role, cfg, part.w, part.h);
|
|
1160
|
-
const
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
const outPath = import_node_path4.default.join(outDir, `${role}.png`);
|
|
1164
|
-
if (role === "face") {
|
|
1165
|
-
faceRaw = await composed.raw().toBuffer();
|
|
1166
|
-
} else {
|
|
1167
|
-
writeFileAtomic(outPath, await composed.png().toBuffer());
|
|
1168
|
-
}
|
|
1471
|
+
const layer = await blankCanvas().composite([{ input: part.buf, left, top }]).png().toBuffer();
|
|
1472
|
+
const outPath = import_node_path5.default.join(outDir, `${role}.png`);
|
|
1473
|
+
writeFileAtomic(outPath, layer);
|
|
1169
1474
|
layers.push({
|
|
1170
1475
|
role,
|
|
1171
1476
|
path: outPath,
|
|
@@ -1176,48 +1481,6 @@ async function composeLayersFromParts(input) {
|
|
|
1176
1481
|
});
|
|
1177
1482
|
preview.push({ input: part.buf, left, top });
|
|
1178
1483
|
}
|
|
1179
|
-
const placed = (role) => layers.find((l) => l.role === role);
|
|
1180
|
-
const eyeL = placed("eye_L");
|
|
1181
|
-
const eyeR = placed("eye_R");
|
|
1182
|
-
const mouth = placed("mouth");
|
|
1183
|
-
const noseCut = cutNose(faceRaw, {
|
|
1184
|
-
x0: eyeR.left + eyeR.width,
|
|
1185
|
-
x1: eyeL.left,
|
|
1186
|
-
y0: Math.round(
|
|
1187
|
-
(eyeL.top + eyeL.height / 2 + eyeR.top + eyeR.height / 2) / 2
|
|
1188
|
-
),
|
|
1189
|
-
y1: mouth.top - 2
|
|
1190
|
-
});
|
|
1191
|
-
const rawCanvas = {
|
|
1192
|
-
raw: { width: CANVAS, height: CANVAS, channels: 4 }
|
|
1193
|
-
};
|
|
1194
|
-
const facePath = import_node_path4.default.join(outDir, "face.png");
|
|
1195
|
-
const nosePath = import_node_path4.default.join(outDir, "nose.png");
|
|
1196
|
-
if (noseCut === null) {
|
|
1197
|
-
writeFileAtomic(
|
|
1198
|
-
facePath,
|
|
1199
|
-
await (0, import_sharp2.default)(faceRaw, rawCanvas).png().toBuffer()
|
|
1200
|
-
);
|
|
1201
|
-
import_node_fs3.default.rmSync(nosePath, { force: true });
|
|
1202
|
-
skipped.push("nose");
|
|
1203
|
-
} else {
|
|
1204
|
-
writeFileAtomic(
|
|
1205
|
-
facePath,
|
|
1206
|
-
await (0, import_sharp2.default)(noseCut.face, rawCanvas).png().toBuffer()
|
|
1207
|
-
);
|
|
1208
|
-
writeFileAtomic(
|
|
1209
|
-
nosePath,
|
|
1210
|
-
await (0, import_sharp2.default)(noseCut.nose, rawCanvas).png().toBuffer()
|
|
1211
|
-
);
|
|
1212
|
-
layers.splice(layers.findIndex((l) => l.role === "face") + 1, 0, {
|
|
1213
|
-
role: "nose",
|
|
1214
|
-
path: nosePath,
|
|
1215
|
-
width: noseCut.bbox.w,
|
|
1216
|
-
height: noseCut.bbox.h,
|
|
1217
|
-
left: noseCut.bbox.x,
|
|
1218
|
-
top: noseCut.bbox.y
|
|
1219
|
-
});
|
|
1220
|
-
}
|
|
1221
1484
|
const previewPng = await (0, import_sharp2.default)({
|
|
1222
1485
|
create: {
|
|
1223
1486
|
width: CANVAS,
|
|
@@ -1226,7 +1489,7 @@ async function composeLayersFromParts(input) {
|
|
|
1226
1489
|
background: { r: 245, g: 245, b: 248, alpha: 1 }
|
|
1227
1490
|
}
|
|
1228
1491
|
}).composite(preview).png().toBuffer();
|
|
1229
|
-
const previewPath =
|
|
1492
|
+
const previewPath = import_node_path5.default.join(outDir, "preview.png");
|
|
1230
1493
|
writeFileAtomic(previewPath, previewPng);
|
|
1231
1494
|
return {
|
|
1232
1495
|
ok: true,
|
|
@@ -1245,7 +1508,7 @@ async function composeLayersFromParts(input) {
|
|
|
1245
1508
|
|
|
1246
1509
|
// src/server.ts
|
|
1247
1510
|
function createIkiMcpServer() {
|
|
1248
|
-
const server = new import_mcp.McpServer({ name: "iki", version: "0.
|
|
1511
|
+
const server = new import_mcp.McpServer({ name: "iki", version: "0.8.0" });
|
|
1249
1512
|
server.registerTool(
|
|
1250
1513
|
"validate_iki",
|
|
1251
1514
|
{
|
|
@@ -1325,6 +1588,15 @@ function createIkiMcpServer() {
|
|
|
1325
1588
|
),
|
|
1326
1589
|
quantizeColors: import_zod.z.number().int().min(2).max(256).optional().describe(
|
|
1327
1590
|
"Palette-quantize the atlas PNG to this many colours (2..256). Flat-shaded art keeps its look at 256 and the model shrinks to about a quarter; omit for lossless."
|
|
1591
|
+
),
|
|
1592
|
+
turnTargets: import_zod.z.object({
|
|
1593
|
+
eyeShift: import_zod.z.number().optional(),
|
|
1594
|
+
farEyeRatio: import_zod.z.number().optional(),
|
|
1595
|
+
silhouetteRatio: import_zod.z.number().optional(),
|
|
1596
|
+
noseShift: import_zod.z.number().optional(),
|
|
1597
|
+
mouthShift: import_zod.z.number().optional()
|
|
1598
|
+
}).optional().describe(
|
|
1599
|
+
"What the head turn is fitted to, as `measure_turn_reference` reports it off a front/turned reference pair. `eyeShift` (and the optional `noseShift`/`mouthShift`, derived from it when absent) is how far that feature slides across the head at full turn, as a fraction of the head's half-width \u2014 a MAGNITUDE: the rig turns both ways, so the sign is ignored. `farEyeRatio` is the far/near eye width at full turn over that same ratio at rest, `silhouetteRatio` the head's half-width turned over at rest. The half-width the shifts are fractions of is measured off the layers themselves, so it is not an input. An omitted field falls back to a default, which is clamped to what this layer set can do (the result's `turn.clamped` lists the ones that were, `turn.achieved` what the rig reaches); a target you DO pass that this layer set cannot reach comes back as `INVALID: \u2026` naming the field and the attainable range, since it is a measurement rather than a preference."
|
|
1328
1600
|
)
|
|
1329
1601
|
}
|
|
1330
1602
|
},
|
|
@@ -1346,7 +1618,7 @@ function createIkiMcpServer() {
|
|
|
1346
1618
|
server.registerTool(
|
|
1347
1619
|
"compose_layers_from_parts",
|
|
1348
1620
|
{
|
|
1349
|
-
description: "Composes AI-generated part PNGs into canvas-aligned, role-named PNG layers on disk, ready for auto_rig_from_layers (the eyewhite split,
|
|
1621
|
+
description: "Composes AI-generated part PNGs into canvas-aligned, role-named PNG layers on disk, ready for auto_rig_from_layers (the eyewhite split, alpha-trim/white-key, and placement pipeline the character-generation skill needs), with the same geometry report measure_layers returns standalone included inline. face, mouth, eyewhite, iris, brow, hair_front are required; hair_back, body, mouth_open, nose are optional \u2014 without a nose layer the rig does not slide the features on the head turn.",
|
|
1350
1622
|
inputSchema: {
|
|
1351
1623
|
partsDir: import_zod.z.string().describe(
|
|
1352
1624
|
"Directory of the source part PNGs (resolved against cwd)."
|
|
@@ -1363,7 +1635,7 @@ function createIkiMcpServer() {
|
|
|
1363
1635
|
h: import_zod.z.number()
|
|
1364
1636
|
}).partial()
|
|
1365
1637
|
).optional().describe(
|
|
1366
|
-
"Per-role override merged over the built-in defaults, keyed by role \u2014 hair_back, body, face, mouth, mouth_open, eye_L, eye_R, iris_L, iris_R, lash_L, lash_R, brow_L, brow_R, hair_front \u2014 e.g. `{ eye_L: { cx: 660 } }`. `w` and `h` are integers in 1..1100; omit `h` to keep the part's own aspect, set it to stretch (set it on a sclera and its lash together, or the blink fold tears)."
|
|
1638
|
+
"Per-role override merged over the built-in defaults, keyed by role \u2014 hair_back, body, face, nose, mouth, mouth_open, eye_L, eye_R, iris_L, iris_R, lash_L, lash_R, brow_L, brow_R, hair_front \u2014 e.g. `{ eye_L: { cx: 660 } }`. `w` and `h` are integers in 1..1100; omit `h` to keep the part's own aspect, set it to stretch (set it on a sclera and its lash together, or the blink fold tears)."
|
|
1367
1639
|
)
|
|
1368
1640
|
}
|
|
1369
1641
|
},
|
|
@@ -1413,6 +1685,43 @@ function createIkiMcpServer() {
|
|
|
1413
1685
|
}
|
|
1414
1686
|
}
|
|
1415
1687
|
);
|
|
1688
|
+
server.registerTool(
|
|
1689
|
+
"measure_turn_reference",
|
|
1690
|
+
{
|
|
1691
|
+
description: "Measures how far a head turns between two images of the same character \u2014 a front view and a turned one, at the SAME scale \u2014 as three ratios, so a rigged turn can be compared to a reference turn (or to an earlier rig) without registering the images: farEyeRatio (turned far/near iris width over the same ratio at rest), eyeShift (how far the eye pair slides across the head, in units of the FRONT head half-width; NEGATIVE = toward the image's left), silhouetteRatio (head half-width, turned over front). The ratios divide out crop and position, not scale, so the pair must already share framing and head size; since a yaw never changes iris height, a pair whose mean iris height differs by more than 10% is refused instead of measured as a turn. Reads engine renders (transparent backdrop) and opaque reference art alike.",
|
|
1692
|
+
inputSchema: {
|
|
1693
|
+
front: import_zod.z.string().describe("Front-facing PNG file path (resolved against cwd)."),
|
|
1694
|
+
turned: import_zod.z.string().describe("The same character turned, as a PNG file path."),
|
|
1695
|
+
iris: import_zod.z.object({
|
|
1696
|
+
hueMin: import_zod.z.number().min(0).max(360),
|
|
1697
|
+
hueMax: import_zod.z.number().min(0).max(360),
|
|
1698
|
+
satMin: import_zod.z.number().min(0).max(1)
|
|
1699
|
+
}).partial().optional().describe(
|
|
1700
|
+
"Iris colour window override merged over the violet default (hue 230..300, sat > 0.22) \u2014 set it for a character whose eyes are another colour."
|
|
1701
|
+
),
|
|
1702
|
+
debugDir: import_zod.z.string().optional().describe(
|
|
1703
|
+
"Existing directory (resolved against cwd; confined to the working directory) to write `front-<name>.debug.png` / `turned-<name>.debug.png` overlays into, with the iris boxes and head edges drawn."
|
|
1704
|
+
)
|
|
1705
|
+
}
|
|
1706
|
+
},
|
|
1707
|
+
async (args) => {
|
|
1708
|
+
try {
|
|
1709
|
+
const r = await measureTurnReference(args);
|
|
1710
|
+
const text = r.ok ? formatTurnReport(r) : `INVALID: ${r.error}`;
|
|
1711
|
+
return {
|
|
1712
|
+
content: [{ type: "text", text }],
|
|
1713
|
+
structuredContent: { ...r }
|
|
1714
|
+
};
|
|
1715
|
+
} catch (err) {
|
|
1716
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
1717
|
+
return {
|
|
1718
|
+
content: [{ type: "text", text: `Unexpected error: ${error}` }],
|
|
1719
|
+
structuredContent: { ok: false, error },
|
|
1720
|
+
isError: true
|
|
1721
|
+
};
|
|
1722
|
+
}
|
|
1723
|
+
}
|
|
1724
|
+
);
|
|
1416
1725
|
return server;
|
|
1417
1726
|
}
|
|
1418
1727
|
|