@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/index.js
CHANGED
|
@@ -35,14 +35,16 @@ __export(index_exports, {
|
|
|
35
35
|
createIkiMcpServer: () => createIkiMcpServer,
|
|
36
36
|
describeIki: () => describeIki,
|
|
37
37
|
formatMeasureReport: () => formatMeasureReport,
|
|
38
|
+
formatTurnReport: () => formatTurnReport,
|
|
38
39
|
listStandardParameters: () => listStandardParameters,
|
|
39
40
|
measureLayers: () => measureLayers,
|
|
41
|
+
measureTurnReference: () => measureTurnReference,
|
|
40
42
|
validateIki: () => validateIki
|
|
41
43
|
});
|
|
42
44
|
module.exports = __toCommonJS(index_exports);
|
|
43
45
|
|
|
44
46
|
// src/tools.ts
|
|
45
|
-
var
|
|
47
|
+
var import_node_path3 = __toESM(require("path"));
|
|
46
48
|
var import_format = require("@ikijs/format");
|
|
47
49
|
var import_editor2 = require("@ikijs/editor");
|
|
48
50
|
|
|
@@ -213,6 +215,9 @@ function detectAlphaBbox(rgba, width, height) {
|
|
|
213
215
|
function cropToBuffer(rgba, width, height, bbox) {
|
|
214
216
|
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();
|
|
215
217
|
}
|
|
218
|
+
function encodeOverlayPng(filePath, svg) {
|
|
219
|
+
return (0, import_sharp.default)(filePath, { limitInputPixels: MAX_INPUT_PIXELS }).composite([{ input: Buffer.from(svg) }]).png().toBuffer();
|
|
220
|
+
}
|
|
216
221
|
async function renderAtlasToDataUri(crops, layout, quantizeColors) {
|
|
217
222
|
const byId = /* @__PURE__ */ new Map();
|
|
218
223
|
for (const crop of crops) byId.set(crop.id, crop);
|
|
@@ -242,6 +247,373 @@ async function renderAtlasToDataUri(crops, layout, quantizeColors) {
|
|
|
242
247
|
return `data:image/png;base64,${page.toString("base64")}`;
|
|
243
248
|
}
|
|
244
249
|
|
|
250
|
+
// src/measure-turn.ts
|
|
251
|
+
var import_node_path2 = __toESM(require("path"));
|
|
252
|
+
var DEFAULT_IRIS = { hueMin: 230, hueMax: 300, satMin: 0.22 };
|
|
253
|
+
var IRIS_V_MIN = 0.18;
|
|
254
|
+
var IRIS_V_MAX = 0.95;
|
|
255
|
+
var IRIS_MEAN_V_MIN = 0.32;
|
|
256
|
+
var CLOSE_RADIUS = 3;
|
|
257
|
+
var CLOSE_RADIUS_REFERENCE_PX = 1254;
|
|
258
|
+
var BLOB_MAX_FRAC = 0.12;
|
|
259
|
+
var KEYED_SPAN_MAX_FRAC = 0.95;
|
|
260
|
+
var MIN_AREA_REFERENCE_PIXELS = 1254 * 1254;
|
|
261
|
+
var MIN_AREA = 300;
|
|
262
|
+
var PAIR_DX_MIN_FRAC = 0.08;
|
|
263
|
+
var PAIR_DX_MAX_FRAC = 0.35;
|
|
264
|
+
var PAIR_DY_MAX_FRAC = 0.05;
|
|
265
|
+
var MAX_CANDIDATES = 12;
|
|
266
|
+
var HEAD_BAND = 10;
|
|
267
|
+
var ALPHA_OPAQUE = 128;
|
|
268
|
+
var BG_HUE_MIN = 220;
|
|
269
|
+
var BG_HUE_MAX = 260;
|
|
270
|
+
var BG_SAT_MAX = 0.2;
|
|
271
|
+
var BG_V_MIN = 0.55;
|
|
272
|
+
var BG_V_BLACK = 0.03;
|
|
273
|
+
function rgbToHsv(r, g, b) {
|
|
274
|
+
r /= 255;
|
|
275
|
+
g /= 255;
|
|
276
|
+
b /= 255;
|
|
277
|
+
const max = Math.max(r, g, b);
|
|
278
|
+
const min = Math.min(r, g, b);
|
|
279
|
+
const d = max - min;
|
|
280
|
+
let h = 0;
|
|
281
|
+
if (d > 0) {
|
|
282
|
+
if (max === r) h = (g - b) / d % 6;
|
|
283
|
+
else if (max === g) h = (b - r) / d + 2;
|
|
284
|
+
else h = (r - g) / d + 4;
|
|
285
|
+
h *= 60;
|
|
286
|
+
if (h < 0) h += 360;
|
|
287
|
+
}
|
|
288
|
+
return [h, max === 0 ? 0 : d / max, max];
|
|
289
|
+
}
|
|
290
|
+
function dilate(mask, width, height, r) {
|
|
291
|
+
const out = new Uint8Array(width * height);
|
|
292
|
+
for (let y = 0; y < height; y++) {
|
|
293
|
+
for (let x = 0; x < width; x++) {
|
|
294
|
+
if (!mask[y * width + x]) continue;
|
|
295
|
+
for (let dy = -r; dy <= r; dy++) {
|
|
296
|
+
for (let dx = -r; dx <= r; dx++) {
|
|
297
|
+
const yy = y + dy;
|
|
298
|
+
const xx = x + dx;
|
|
299
|
+
if (yy >= 0 && yy < height && xx >= 0 && xx < width)
|
|
300
|
+
out[yy * width + xx] = 1;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
return out;
|
|
306
|
+
}
|
|
307
|
+
function erode(mask, width, height, r) {
|
|
308
|
+
const out = new Uint8Array(width * height);
|
|
309
|
+
for (let y = 0; y < height; y++) {
|
|
310
|
+
for (let x = 0; x < width; x++) {
|
|
311
|
+
if (!mask[y * width + x]) continue;
|
|
312
|
+
let keep = true;
|
|
313
|
+
for (let dy = -r; dy <= r && keep; dy++) {
|
|
314
|
+
for (let dx = -r; dx <= r; dx++) {
|
|
315
|
+
const yy = y + dy;
|
|
316
|
+
const xx = x + dx;
|
|
317
|
+
if (yy < 0 || yy >= height || xx < 0 || xx >= width || !mask[yy * width + xx]) {
|
|
318
|
+
keep = false;
|
|
319
|
+
break;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
if (keep) out[y * width + x] = 1;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
return out;
|
|
327
|
+
}
|
|
328
|
+
function closeMask(mask, width, height, r) {
|
|
329
|
+
return erode(dilate(mask, width, height, r), width, height, r);
|
|
330
|
+
}
|
|
331
|
+
function components(mask, width, height, minArea) {
|
|
332
|
+
const seen = new Uint8Array(width * height);
|
|
333
|
+
const out = [];
|
|
334
|
+
for (let i = 0; i < width * height; i++) {
|
|
335
|
+
if (!mask[i] || seen[i]) continue;
|
|
336
|
+
const stack = [i];
|
|
337
|
+
seen[i] = 1;
|
|
338
|
+
let x0 = width;
|
|
339
|
+
let x1 = 0;
|
|
340
|
+
let y0 = height;
|
|
341
|
+
let y1 = 0;
|
|
342
|
+
let n = 0;
|
|
343
|
+
let sx = 0;
|
|
344
|
+
let sy = 0;
|
|
345
|
+
while (stack.length) {
|
|
346
|
+
const p = stack.pop();
|
|
347
|
+
const x = p % width;
|
|
348
|
+
const y = p / width | 0;
|
|
349
|
+
n++;
|
|
350
|
+
sx += x;
|
|
351
|
+
sy += y;
|
|
352
|
+
if (x < x0) x0 = x;
|
|
353
|
+
if (x > x1) x1 = x;
|
|
354
|
+
if (y < y0) y0 = y;
|
|
355
|
+
if (y > y1) y1 = y;
|
|
356
|
+
const neighbours = [];
|
|
357
|
+
if (x > 0) neighbours.push(p - 1);
|
|
358
|
+
if (x < width - 1) neighbours.push(p + 1);
|
|
359
|
+
if (y > 0) neighbours.push(p - width);
|
|
360
|
+
if (y < height - 1) neighbours.push(p + width);
|
|
361
|
+
for (const q of neighbours) {
|
|
362
|
+
if (seen[q] || !mask[q]) continue;
|
|
363
|
+
seen[q] = 1;
|
|
364
|
+
stack.push(q);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
if (n >= minArea) {
|
|
368
|
+
out.push({
|
|
369
|
+
x0,
|
|
370
|
+
x1,
|
|
371
|
+
y0,
|
|
372
|
+
y1,
|
|
373
|
+
w: x1 - x0 + 1,
|
|
374
|
+
h: y1 - y0 + 1,
|
|
375
|
+
area: n,
|
|
376
|
+
cx: sx / n,
|
|
377
|
+
cy: sy / n
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
return out.sort((a, b) => b.area - a.area);
|
|
382
|
+
}
|
|
383
|
+
function foregroundSpan(mask, width, height, rowLo, rowHi) {
|
|
384
|
+
let left = width;
|
|
385
|
+
let right = -1;
|
|
386
|
+
for (let y = Math.max(0, rowLo); y <= Math.min(height - 1, rowHi); y++) {
|
|
387
|
+
for (let x = 0; x < width; x++) {
|
|
388
|
+
if (!mask[y * width + x]) continue;
|
|
389
|
+
if (x < left) left = x;
|
|
390
|
+
if (x > right) right = x;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
return { left, right };
|
|
394
|
+
}
|
|
395
|
+
function headHalfOf(span) {
|
|
396
|
+
return (span.right - span.left + 1) / 2;
|
|
397
|
+
}
|
|
398
|
+
function resolveIris(override) {
|
|
399
|
+
const iris = { ...DEFAULT_IRIS, ...override };
|
|
400
|
+
if (iris.hueMin < 0 || iris.hueMax > 360 || iris.hueMin >= iris.hueMax) {
|
|
401
|
+
throw new AutoRigInputError(
|
|
402
|
+
`iris hue window must be 0 <= hueMin < hueMax <= 360, got ${iris.hueMin}..${iris.hueMax}`
|
|
403
|
+
);
|
|
404
|
+
}
|
|
405
|
+
if (iris.satMin < 0 || iris.satMin > 1) {
|
|
406
|
+
throw new AutoRigInputError(
|
|
407
|
+
`iris satMin must be within 0..1, got ${iris.satMin}`
|
|
408
|
+
);
|
|
409
|
+
}
|
|
410
|
+
return iris;
|
|
411
|
+
}
|
|
412
|
+
async function measureTurnImage(filePath, iris) {
|
|
413
|
+
const { width, height, rgba, hasAlpha } = await decodePng(filePath);
|
|
414
|
+
const n = width * height;
|
|
415
|
+
let transparent = false;
|
|
416
|
+
if (hasAlpha) {
|
|
417
|
+
for (let i = 0; i < n; i++) {
|
|
418
|
+
if (rgba[i * 4 + 3] < ALPHA_OPAQUE) {
|
|
419
|
+
transparent = true;
|
|
420
|
+
break;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
const maskMode = hasAlpha && transparent ? "alpha" : "keyed";
|
|
425
|
+
const irisMask = new Uint8Array(n);
|
|
426
|
+
const fg = new Uint8Array(n);
|
|
427
|
+
const value = new Float32Array(n);
|
|
428
|
+
for (let i = 0; i < n; i++) {
|
|
429
|
+
if (rgba[i * 4 + 3] < ALPHA_OPAQUE) continue;
|
|
430
|
+
const [hue, s, v] = rgbToHsv(rgba[i * 4], rgba[i * 4 + 1], rgba[i * 4 + 2]);
|
|
431
|
+
if (maskMode === "alpha") {
|
|
432
|
+
fg[i] = 1;
|
|
433
|
+
} else {
|
|
434
|
+
const isBg = hue >= BG_HUE_MIN && hue <= BG_HUE_MAX && s < BG_SAT_MAX && v > BG_V_MIN || v < BG_V_BLACK;
|
|
435
|
+
if (!isBg) fg[i] = 1;
|
|
436
|
+
}
|
|
437
|
+
if (hue >= iris.hueMin && hue <= iris.hueMax && s > iris.satMin && v > IRIS_V_MIN && v < IRIS_V_MAX) {
|
|
438
|
+
irisMask[i] = 1;
|
|
439
|
+
value[i] = v;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
const minArea = Math.max(
|
|
443
|
+
1,
|
|
444
|
+
Math.round(MIN_AREA * n / MIN_AREA_REFERENCE_PIXELS)
|
|
445
|
+
);
|
|
446
|
+
const closeRadius = Math.max(
|
|
447
|
+
1,
|
|
448
|
+
Math.round(
|
|
449
|
+
CLOSE_RADIUS * Math.max(width, height) / CLOSE_RADIUS_REFERENCE_PX
|
|
450
|
+
)
|
|
451
|
+
);
|
|
452
|
+
const meanValue = (b) => {
|
|
453
|
+
let count = 0;
|
|
454
|
+
let total = 0;
|
|
455
|
+
for (let y = b.y0; y <= b.y1; y++) {
|
|
456
|
+
for (let x = b.x0; x <= b.x1; x++) {
|
|
457
|
+
const i = y * width + x;
|
|
458
|
+
if (!irisMask[i]) continue;
|
|
459
|
+
count++;
|
|
460
|
+
total += value[i];
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
return count ? total / count : 0;
|
|
464
|
+
};
|
|
465
|
+
const candidates = components(
|
|
466
|
+
closeMask(irisMask, width, height, closeRadius),
|
|
467
|
+
width,
|
|
468
|
+
height,
|
|
469
|
+
minArea
|
|
470
|
+
).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);
|
|
471
|
+
let best = null;
|
|
472
|
+
for (let i = 0; i < candidates.length; i++) {
|
|
473
|
+
for (let j = i + 1; j < candidates.length; j++) {
|
|
474
|
+
const a = candidates[i];
|
|
475
|
+
const b = candidates[j];
|
|
476
|
+
const dx = Math.abs(a.cx - b.cx);
|
|
477
|
+
const dy = Math.abs(a.cy - b.cy);
|
|
478
|
+
if (dx < width * PAIR_DX_MIN_FRAC || dx > width * PAIR_DX_MAX_FRAC)
|
|
479
|
+
continue;
|
|
480
|
+
if (dy > height * PAIR_DY_MAX_FRAC) continue;
|
|
481
|
+
const score = a.area + b.area;
|
|
482
|
+
if (best === null || score > best.score) {
|
|
483
|
+
const pair = a.cx <= b.cx ? [a, b] : [b, a];
|
|
484
|
+
best = { score, pair };
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
if (best === null) {
|
|
489
|
+
throw new AutoRigInputError(
|
|
490
|
+
`no iris pair found in ${filePath} among ${candidates.length} candidate blobs (iris hue ${iris.hueMin}..${iris.hueMax}, sat > ${iris.satMin})`
|
|
491
|
+
);
|
|
492
|
+
}
|
|
493
|
+
const [irisL, irisR] = best.pair;
|
|
494
|
+
const eyeRow = Math.round((irisL.cy + irisR.cy) / 2);
|
|
495
|
+
const { left, right } = foregroundSpan(
|
|
496
|
+
fg,
|
|
497
|
+
width,
|
|
498
|
+
height,
|
|
499
|
+
eyeRow - HEAD_BAND,
|
|
500
|
+
eyeRow + HEAD_BAND
|
|
501
|
+
);
|
|
502
|
+
if (right <= left) {
|
|
503
|
+
throw new AutoRigInputError(
|
|
504
|
+
`no head span at the eye row (y=${eyeRow}) in ${filePath}: the ${maskMode} foreground mask is empty there`
|
|
505
|
+
);
|
|
506
|
+
}
|
|
507
|
+
if (maskMode === "keyed" && right - left + 1 > width * KEYED_SPAN_MAX_FRAC) {
|
|
508
|
+
throw new AutoRigInputError(
|
|
509
|
+
`head span at the eye row (y=${eyeRow}) in ${filePath} covers ${right - left + 1}/${width} px: background not keyed / not an engine render`
|
|
510
|
+
);
|
|
511
|
+
}
|
|
512
|
+
return {
|
|
513
|
+
file: filePath,
|
|
514
|
+
width,
|
|
515
|
+
height,
|
|
516
|
+
maskMode,
|
|
517
|
+
irisL,
|
|
518
|
+
irisR,
|
|
519
|
+
eyeRow,
|
|
520
|
+
head: {
|
|
521
|
+
left,
|
|
522
|
+
right,
|
|
523
|
+
cx: (left + right) / 2,
|
|
524
|
+
half: headHalfOf({ left, right })
|
|
525
|
+
},
|
|
526
|
+
pairCx: (irisL.cx + irisR.cx) / 2,
|
|
527
|
+
eyeGap: irisR.cx - irisL.cx
|
|
528
|
+
};
|
|
529
|
+
}
|
|
530
|
+
function farNearWidths(m, turnSign) {
|
|
531
|
+
return turnSign < 0 ? { far: m.irisL.w, near: m.irisR.w } : { far: m.irisR.w, near: m.irisL.w };
|
|
532
|
+
}
|
|
533
|
+
function overlaySvg(m) {
|
|
534
|
+
const box = (b, stroke) => `<rect x="${b.x0}" y="${b.y0}" width="${b.w}" height="${b.h}" fill="none" stroke="${stroke}" stroke-width="3"/>`;
|
|
535
|
+
const tick = (x, half, stroke) => `<line x1="${x}" y1="${m.eyeRow - half}" x2="${x}" y2="${m.eyeRow + half}" stroke="${stroke}" stroke-width="3"/>`;
|
|
536
|
+
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>";
|
|
537
|
+
}
|
|
538
|
+
async function writeOverlay(debugDir, label, m) {
|
|
539
|
+
const outPath = import_node_path2.default.join(
|
|
540
|
+
debugDir,
|
|
541
|
+
`${label}-${import_node_path2.default.basename(m.file, ".png")}.debug.png`
|
|
542
|
+
);
|
|
543
|
+
writeFileAtomic(outPath, await encodeOverlayPng(m.file, overlaySvg(m)));
|
|
544
|
+
return outPath;
|
|
545
|
+
}
|
|
546
|
+
var IRIS_HEIGHT_SCALE_TOLERANCE = 0.1;
|
|
547
|
+
async function measureTurnReference(input) {
|
|
548
|
+
try {
|
|
549
|
+
const iris = resolveIris(input.iris);
|
|
550
|
+
const frontPath = resolveInputPath(input.front);
|
|
551
|
+
const turnedPath = resolveInputPath(input.turned);
|
|
552
|
+
const debugDir = input.debugDir === void 0 ? null : resolveOutputDir(input.debugDir);
|
|
553
|
+
const front = await measureTurnImage(frontPath, iris);
|
|
554
|
+
const turned = await measureTurnImage(turnedPath, iris);
|
|
555
|
+
const frontIrisH = (front.irisL.h + front.irisR.h) / 2;
|
|
556
|
+
const turnedIrisH = (turned.irisL.h + turned.irisR.h) / 2;
|
|
557
|
+
if (Math.abs(turnedIrisH - frontIrisH) / frontIrisH > IRIS_HEIGHT_SCALE_TOLERANCE) {
|
|
558
|
+
throw new AutoRigInputError(
|
|
559
|
+
`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`
|
|
560
|
+
);
|
|
561
|
+
}
|
|
562
|
+
const frontShift = front.pairCx - front.head.cx;
|
|
563
|
+
const turnedShift = turned.pairCx - turned.head.cx;
|
|
564
|
+
const shift = turnedShift - frontShift;
|
|
565
|
+
const turnSign = shift < 0 ? -1 : 1;
|
|
566
|
+
const f = farNearWidths(front, turnSign);
|
|
567
|
+
const t = farNearWidths(turned, turnSign);
|
|
568
|
+
const debug = debugDir === null ? void 0 : [
|
|
569
|
+
await writeOverlay(debugDir, "front", front),
|
|
570
|
+
await writeOverlay(debugDir, "turned", turned)
|
|
571
|
+
];
|
|
572
|
+
return {
|
|
573
|
+
ok: true,
|
|
574
|
+
front,
|
|
575
|
+
turned,
|
|
576
|
+
farEyeRatio: t.far / t.near / (f.far / f.near),
|
|
577
|
+
eyeShift: shift / front.head.half,
|
|
578
|
+
silhouetteRatio: turned.head.half / front.head.half,
|
|
579
|
+
turnSign,
|
|
580
|
+
...debug === void 0 ? {} : { debug }
|
|
581
|
+
};
|
|
582
|
+
} catch (err) {
|
|
583
|
+
if (err instanceof AutoRigInputError)
|
|
584
|
+
return { ok: false, error: err.message };
|
|
585
|
+
throw err;
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
var n1 = (v) => v.toFixed(1);
|
|
589
|
+
var n3 = (v) => v.toFixed(3);
|
|
590
|
+
function imageBlock(label, m) {
|
|
591
|
+
return [
|
|
592
|
+
`# ${label} ${m.file} ${m.width}x${m.height} mask: ${m.maskMode}`,
|
|
593
|
+
`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)}`,
|
|
594
|
+
`head ${m.head.left}..${m.head.right} centre ${n1(m.head.cx)} half ${n1(m.head.half)} pair centre ${n1(m.pairCx)}`
|
|
595
|
+
];
|
|
596
|
+
}
|
|
597
|
+
function formatTurnReport(result) {
|
|
598
|
+
const { front, turned } = result;
|
|
599
|
+
const f = farNearWidths(front, result.turnSign);
|
|
600
|
+
const t = farNearWidths(turned, result.turnSign);
|
|
601
|
+
const lines = [
|
|
602
|
+
`farEyeRatio ${n3(result.farEyeRatio)} far/near iris width ${t.far}/${t.near} turned over ${f.far}/${f.near} front`,
|
|
603
|
+
`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)`,
|
|
604
|
+
`silhouetteRatio ${n3(result.silhouetteRatio)} head half ${n1(turned.head.half)} px turned over ${n1(front.head.half)} px front`,
|
|
605
|
+
`turnSign ${result.turnSign} (the head turned toward the image's ${result.turnSign < 0 ? "left" : "right"})`,
|
|
606
|
+
"",
|
|
607
|
+
...imageBlock("front", front),
|
|
608
|
+
"",
|
|
609
|
+
...imageBlock("turned", turned)
|
|
610
|
+
];
|
|
611
|
+
if (result.debug !== void 0) {
|
|
612
|
+
lines.push("", ...result.debug.map((p) => `debug overlay ${p}`));
|
|
613
|
+
}
|
|
614
|
+
return lines.join("\n");
|
|
615
|
+
}
|
|
616
|
+
|
|
245
617
|
// src/tools.ts
|
|
246
618
|
function coerceModel(model) {
|
|
247
619
|
try {
|
|
@@ -386,6 +758,38 @@ function expectInput(label, fn) {
|
|
|
386
758
|
);
|
|
387
759
|
}
|
|
388
760
|
}
|
|
761
|
+
function eyeRowOf(layers) {
|
|
762
|
+
const centreY = (role) => {
|
|
763
|
+
const layer = layers.find((l) => l.role === role);
|
|
764
|
+
return layer && layer.bbox.y + layer.bbox.h / 2;
|
|
765
|
+
};
|
|
766
|
+
const pairRow = (left, right) => {
|
|
767
|
+
const a = centreY(left);
|
|
768
|
+
const b = centreY(right);
|
|
769
|
+
return a === void 0 || b === void 0 ? void 0 : (a + b) / 2;
|
|
770
|
+
};
|
|
771
|
+
const row = pairRow("iris_L", "iris_R") ?? pairRow("eye_L", "eye_R");
|
|
772
|
+
if (row === void 0) {
|
|
773
|
+
throw new Error("auto-rig: no eye pair to measure the head width at");
|
|
774
|
+
}
|
|
775
|
+
return Math.round(row);
|
|
776
|
+
}
|
|
777
|
+
function foregroundSpanOfRows(rowLeft, rowRight, rowLo, rowHi) {
|
|
778
|
+
let left = Infinity;
|
|
779
|
+
let right = -Infinity;
|
|
780
|
+
for (let y = Math.max(0, rowLo); y <= Math.min(rowLeft.length - 1, rowHi); y++) {
|
|
781
|
+
if (rowLeft[y] < left) left = rowLeft[y];
|
|
782
|
+
if (rowRight[y] > right) right = rowRight[y];
|
|
783
|
+
}
|
|
784
|
+
return { left, right };
|
|
785
|
+
}
|
|
786
|
+
function facePlateHalfOf(layers) {
|
|
787
|
+
const face = layers.find((l) => l.role === "face");
|
|
788
|
+
if (face === void 0) {
|
|
789
|
+
throw new Error("auto-rig: no face layer to measure the plate against");
|
|
790
|
+
}
|
|
791
|
+
return face.cropW / 2;
|
|
792
|
+
}
|
|
389
793
|
async function autoRigFromLayers(input) {
|
|
390
794
|
try {
|
|
391
795
|
const layers = input.layers;
|
|
@@ -408,7 +812,7 @@ async function autoRigFromLayers(input) {
|
|
|
408
812
|
}
|
|
409
813
|
const resolvedLayers = layers.map((layer) => {
|
|
410
814
|
const resolved = resolveInputPath(layer.path);
|
|
411
|
-
return { resolved, fileName: layer.fileName ??
|
|
815
|
+
return { resolved, fileName: layer.fileName ?? import_node_path3.default.basename(resolved) };
|
|
412
816
|
});
|
|
413
817
|
const rolePairs = expectInput(
|
|
414
818
|
"role parsing",
|
|
@@ -418,8 +822,10 @@ async function autoRigFromLayers(input) {
|
|
|
418
822
|
let canvasW = 0;
|
|
419
823
|
let canvasH = 0;
|
|
420
824
|
let totalPixels = 0;
|
|
825
|
+
let opaque = new Uint8Array(0);
|
|
421
826
|
const layerInputs = [];
|
|
422
827
|
const crops = [];
|
|
828
|
+
const rowSpansByRole = [];
|
|
423
829
|
for (let i = 0; i < resolvedLayers.length; i++) {
|
|
424
830
|
const { resolved, fileName } = resolvedLayers[i];
|
|
425
831
|
const png = await decodePng(resolved);
|
|
@@ -442,6 +848,7 @@ async function autoRigFromLayers(input) {
|
|
|
442
848
|
`canvas ${canvasW}x${canvasH} exceeds ${MAX_CANVAS_DIM}`
|
|
443
849
|
);
|
|
444
850
|
}
|
|
851
|
+
opaque = new Uint8Array(canvasW * canvasH);
|
|
445
852
|
} else if (png.width !== canvasW || png.height !== canvasH) {
|
|
446
853
|
throw new AutoRigInputError(
|
|
447
854
|
`layer "${fileName}" size ${png.width}x${png.height} differs from canvas ${canvasW}x${canvasH}`
|
|
@@ -461,6 +868,18 @@ async function autoRigFromLayers(input) {
|
|
|
461
868
|
);
|
|
462
869
|
}
|
|
463
870
|
const buffer = await cropToBuffer(png.rgba, png.width, png.height, bbox);
|
|
871
|
+
const rowLeft = new Int32Array(canvasH).fill(canvasW);
|
|
872
|
+
const rowRight = new Int32Array(canvasH).fill(-1);
|
|
873
|
+
for (let y = 0; y < canvasH; y++) {
|
|
874
|
+
for (let x = 0; x < canvasW; x++) {
|
|
875
|
+
const p = y * canvasW + x;
|
|
876
|
+
if (png.rgba[p * 4 + 3] < ALPHA_OPAQUE) continue;
|
|
877
|
+
opaque[p] = 1;
|
|
878
|
+
if (x < rowLeft[y]) rowLeft[y] = x;
|
|
879
|
+
if (x > rowRight[y]) rowRight[y] = x;
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
rowSpansByRole.push({ role, rowLeft, rowRight });
|
|
464
883
|
layerInputs.push({
|
|
465
884
|
role,
|
|
466
885
|
fileName,
|
|
@@ -472,10 +891,56 @@ async function autoRigFromLayers(input) {
|
|
|
472
891
|
});
|
|
473
892
|
crops.push({ id: role, buffer, width: bbox.w, height: bbox.h });
|
|
474
893
|
}
|
|
475
|
-
const
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
894
|
+
const eyeRow = eyeRowOf(layerInputs);
|
|
895
|
+
const span = foregroundSpan(
|
|
896
|
+
opaque,
|
|
897
|
+
canvasW,
|
|
898
|
+
canvasH,
|
|
899
|
+
eyeRow - HEAD_BAND,
|
|
900
|
+
eyeRow + HEAD_BAND
|
|
901
|
+
);
|
|
902
|
+
const headHalfWidth = span.right > span.left ? headHalfOf(span) : void 0;
|
|
903
|
+
const headHalfWidthApplied = headHalfWidth !== void 0 && headHalfWidth > facePlateHalfOf(layerInputs);
|
|
904
|
+
let headEdges;
|
|
905
|
+
if (headHalfWidthApplied) {
|
|
906
|
+
const bandLo = eyeRow - HEAD_BAND;
|
|
907
|
+
const bandHi = eyeRow + HEAD_BAND;
|
|
908
|
+
const left = [];
|
|
909
|
+
const right = [];
|
|
910
|
+
for (const { role, rowLeft, rowRight } of rowSpansByRole) {
|
|
911
|
+
const own = foregroundSpanOfRows(rowLeft, rowRight, bandLo, bandHi);
|
|
912
|
+
if (own.right <= own.left) continue;
|
|
913
|
+
left.push({ role, x: own.left - canvasW / 2 });
|
|
914
|
+
right.push({ role, x: own.right - canvasW / 2 });
|
|
915
|
+
}
|
|
916
|
+
headEdges = { left, right };
|
|
917
|
+
}
|
|
918
|
+
let turn;
|
|
919
|
+
let model;
|
|
920
|
+
try {
|
|
921
|
+
const { eyeShift, farEyeRatio, silhouetteRatio, noseShift, mouthShift } = input.turnTargets ?? {};
|
|
922
|
+
model = (0, import_editor2.generateIkiFromLayerSet)(
|
|
923
|
+
layerInputs,
|
|
924
|
+
{ width: canvasW, height: canvasH },
|
|
925
|
+
{
|
|
926
|
+
turnTargets: {
|
|
927
|
+
eyeShift,
|
|
928
|
+
farEyeRatio,
|
|
929
|
+
silhouetteRatio,
|
|
930
|
+
noseShift,
|
|
931
|
+
mouthShift,
|
|
932
|
+
...headHalfWidthApplied ? { headHalfWidth } : {}
|
|
933
|
+
},
|
|
934
|
+
onTurnSolved: (report) => {
|
|
935
|
+
turn = report;
|
|
936
|
+
},
|
|
937
|
+
...headEdges === void 0 ? {} : { headEdges }
|
|
938
|
+
}
|
|
939
|
+
);
|
|
940
|
+
} catch (e) {
|
|
941
|
+
if (!(e instanceof import_editor2.TurnTargetError)) throw e;
|
|
942
|
+
throw new AutoRigInputError(e.message);
|
|
943
|
+
}
|
|
479
944
|
const doc = new import_editor2.EditorDocument(model);
|
|
480
945
|
const layout = (0, import_editor2.packAtlas)(
|
|
481
946
|
crops.map((c) => ({ id: c.id, width: c.width, height: c.height }))
|
|
@@ -512,7 +977,11 @@ async function autoRigFromLayers(input) {
|
|
|
512
977
|
path: outPath,
|
|
513
978
|
canvas: { width: canvasW, height: canvasH },
|
|
514
979
|
partCount: finalModel.parts.length,
|
|
515
|
-
atlasBytes: dataUri.length
|
|
980
|
+
atlasBytes: dataUri.length,
|
|
981
|
+
...headHalfWidth === void 0 ? {} : { headHalfWidth },
|
|
982
|
+
headHalfWidthApplied,
|
|
983
|
+
...headEdges === void 0 ? {} : { headEdges },
|
|
984
|
+
...turn === void 0 ? {} : { turn }
|
|
516
985
|
};
|
|
517
986
|
} catch (err) {
|
|
518
987
|
if (err instanceof AutoRigInputError)
|
|
@@ -523,12 +992,12 @@ async function autoRigFromLayers(input) {
|
|
|
523
992
|
|
|
524
993
|
// src/compose.ts
|
|
525
994
|
var import_node_fs3 = __toESM(require("fs"));
|
|
526
|
-
var
|
|
995
|
+
var import_node_path5 = __toESM(require("path"));
|
|
527
996
|
var import_sharp2 = __toESM(require("sharp"));
|
|
528
997
|
|
|
529
998
|
// src/measure.ts
|
|
530
999
|
var import_node_fs2 = __toESM(require("fs"));
|
|
531
|
-
var
|
|
1000
|
+
var import_node_path4 = __toESM(require("path"));
|
|
532
1001
|
var IRIS_RATIO_MIN = 0.45;
|
|
533
1002
|
var IRIS_RATIO_MAX = 0.72;
|
|
534
1003
|
var EYE_ASPECT_MIN = 0.5;
|
|
@@ -629,8 +1098,8 @@ async function measureDir(absDir) {
|
|
|
629
1098
|
const layers = {};
|
|
630
1099
|
const empty = [];
|
|
631
1100
|
for (const f of files) {
|
|
632
|
-
const role =
|
|
633
|
-
const m = await layerStats(
|
|
1101
|
+
const role = import_node_path4.default.basename(f, ".png");
|
|
1102
|
+
const m = await layerStats(import_node_path4.default.join(absDir, f));
|
|
634
1103
|
if (!m) {
|
|
635
1104
|
empty.push(role);
|
|
636
1105
|
continue;
|
|
@@ -708,7 +1177,11 @@ async function measureDir(absDir) {
|
|
|
708
1177
|
}
|
|
709
1178
|
for (const [role, why] of [
|
|
710
1179
|
["body", "without it the character reads as a floating head on head-turn"],
|
|
711
|
-
["hair_back", "without it the silhouette is flat behind the face"]
|
|
1180
|
+
["hair_back", "without it the silhouette is flat behind the face"],
|
|
1181
|
+
[
|
|
1182
|
+
"nose",
|
|
1183
|
+
"without it nothing on the face slides on the head turn \u2014 the features stay on the plate"
|
|
1184
|
+
]
|
|
712
1185
|
]) {
|
|
713
1186
|
if (layers[role] === void 0) warnings.push(`${role}: missing \u2014 ${why}.`);
|
|
714
1187
|
}
|
|
@@ -762,13 +1235,6 @@ var IRIS_W = Math.round(EYE_W * 0.5625);
|
|
|
762
1235
|
var EYEWHITE_SRC = "eyewhite.png";
|
|
763
1236
|
var EYE_LASH_LUMA = 120;
|
|
764
1237
|
var LASH_KEEP_FRACTION = 0.5;
|
|
765
|
-
var NOSE_INK_TOL = 20;
|
|
766
|
-
var NOSE_INK_LOW = 8;
|
|
767
|
-
var NOSE_GROW = 2;
|
|
768
|
-
var NOSE_MIN_PIXELS = 12;
|
|
769
|
-
var NOSE_MAX_CUT_FRACTION = 0.25;
|
|
770
|
-
var NOSE_ROW_MIN_PIXELS = 8;
|
|
771
|
-
var NOSE_FILL_REACH = 8;
|
|
772
1238
|
var DEFAULT_LAYOUT = {
|
|
773
1239
|
// Back hair and body sit behind the face. Both are OPTIONAL: a parts dir
|
|
774
1240
|
// without them still composes (head-only character).
|
|
@@ -778,6 +1244,11 @@ var DEFAULT_LAYOUT = {
|
|
|
778
1244
|
// not a floating head.
|
|
779
1245
|
body: { src: "body.png", cx: 550, cy: 1017, w: 840, optional: true },
|
|
780
1246
|
face: { src: "face.png", cx: 550, cy: 475, w: 400 },
|
|
1247
|
+
// The nose, drawn on its own (the face is drawn without one): the auto-rig
|
|
1248
|
+
// leads the head turn with it and keys the other features' slide on its
|
|
1249
|
+
// presence, so without it the features stay on the face plate. Sits between
|
|
1250
|
+
// the eyes and above the mouth. OPTIONAL: a parts dir without it composes.
|
|
1251
|
+
nose: { src: "nose.png", cx: 550, cy: 586, w: 40, optional: true },
|
|
781
1252
|
mouth: { src: "mouth.png", cx: 550, cy: 619, w: 68 },
|
|
782
1253
|
// Cross-fades with `mouth` on ParamMouthOpenY (opacity, not scaleY) once the
|
|
783
1254
|
// auto-rig sees both roles. Same cx/w as `mouth` so the lip width matches;
|
|
@@ -819,6 +1290,7 @@ var ORDER = [
|
|
|
819
1290
|
"hair_back",
|
|
820
1291
|
"body",
|
|
821
1292
|
"face",
|
|
1293
|
+
"nose",
|
|
822
1294
|
"mouth",
|
|
823
1295
|
"mouth_open",
|
|
824
1296
|
"eye_L",
|
|
@@ -880,7 +1352,7 @@ async function partBuffer(role, cfg, partsDir, inMemory) {
|
|
|
880
1352
|
if (inMemory !== void 0) {
|
|
881
1353
|
img = (0, import_sharp2.default)(inMemory);
|
|
882
1354
|
} else {
|
|
883
|
-
const srcPath =
|
|
1355
|
+
const srcPath = import_node_path5.default.join(partsDir, cfg.src);
|
|
884
1356
|
if (!import_node_fs3.default.existsSync(srcPath)) {
|
|
885
1357
|
if (cfg.optional) return null;
|
|
886
1358
|
throw new AutoRigInputError(
|
|
@@ -935,7 +1407,7 @@ function blankCanvas() {
|
|
|
935
1407
|
});
|
|
936
1408
|
}
|
|
937
1409
|
async function prepEyeSplit(partsDir) {
|
|
938
|
-
const srcPath =
|
|
1410
|
+
const srcPath = import_node_path5.default.join(partsDir, EYEWHITE_SRC);
|
|
939
1411
|
if (!import_node_fs3.default.existsSync(srcPath)) {
|
|
940
1412
|
throw new AutoRigInputError(`missing eyewhite source: ${srcPath}`);
|
|
941
1413
|
}
|
|
@@ -980,164 +1452,6 @@ async function prepEyeSplit(partsDir) {
|
|
|
980
1452
|
lash: await cropToBuffer(lash, W, H, region)
|
|
981
1453
|
};
|
|
982
1454
|
}
|
|
983
|
-
function cutNose(rgba, box) {
|
|
984
|
-
const W = CANVAS;
|
|
985
|
-
const x0 = Math.max(0, box.x0);
|
|
986
|
-
const y0 = Math.max(0, box.y0);
|
|
987
|
-
const x1 = Math.min(W, box.x1);
|
|
988
|
-
const y1 = Math.min(W, box.y1);
|
|
989
|
-
if (x1 - x0 < 4 || y1 - y0 < 4) return null;
|
|
990
|
-
const bw = x1 - x0;
|
|
991
|
-
const bh = y1 - y0;
|
|
992
|
-
const at = (x, y) => (y * W + x) * 4;
|
|
993
|
-
const median = (v) => v.sort((a, b) => a - b)[v.length >> 1];
|
|
994
|
-
const all = [[], [], []];
|
|
995
|
-
const rows = [];
|
|
996
|
-
for (let y = y0; y < y1; y++) {
|
|
997
|
-
const row = [[], [], []];
|
|
998
|
-
for (let x = x0; x < x1; x++) {
|
|
999
|
-
const i = at(x, y);
|
|
1000
|
-
if (rgba[i + 3] === 0) continue;
|
|
1001
|
-
for (let c = 0; c < 3; c++) {
|
|
1002
|
-
row[c].push(rgba[i + c]);
|
|
1003
|
-
all[c].push(rgba[i + c]);
|
|
1004
|
-
}
|
|
1005
|
-
}
|
|
1006
|
-
rows.push(row);
|
|
1007
|
-
}
|
|
1008
|
-
const opaqueCount = all[0].length;
|
|
1009
|
-
if (opaqueCount === 0) return null;
|
|
1010
|
-
const skin = all.map(median);
|
|
1011
|
-
const skinRow = rows.map(
|
|
1012
|
-
(row) => row[0].length < NOSE_ROW_MIN_PIXELS ? skin : row.map(median)
|
|
1013
|
-
);
|
|
1014
|
-
const distAt = (i, y) => {
|
|
1015
|
-
const ref = skinRow[y - y0];
|
|
1016
|
-
return Math.max(
|
|
1017
|
-
Math.abs(rgba[i] - ref[0]),
|
|
1018
|
-
Math.abs(rgba[i + 1] - ref[1]),
|
|
1019
|
-
Math.abs(rgba[i + 2] - ref[2])
|
|
1020
|
-
);
|
|
1021
|
-
};
|
|
1022
|
-
const dist = new Uint8Array(bw * bh);
|
|
1023
|
-
const ink = new Uint8Array(bw * bh);
|
|
1024
|
-
const queue = [];
|
|
1025
|
-
let inkCount = 0;
|
|
1026
|
-
for (let y = y0; y < y1; y++) {
|
|
1027
|
-
for (let x = x0; x < x1; x++) {
|
|
1028
|
-
const i = at(x, y);
|
|
1029
|
-
if (rgba[i + 3] === 0) continue;
|
|
1030
|
-
const d = distAt(i, y);
|
|
1031
|
-
const k = (y - y0) * bw + (x - x0);
|
|
1032
|
-
dist[k] = d;
|
|
1033
|
-
if (d > NOSE_INK_TOL) {
|
|
1034
|
-
ink[k] = 1;
|
|
1035
|
-
queue.push(k);
|
|
1036
|
-
inkCount++;
|
|
1037
|
-
}
|
|
1038
|
-
}
|
|
1039
|
-
}
|
|
1040
|
-
if (inkCount < NOSE_MIN_PIXELS) return null;
|
|
1041
|
-
while (queue.length > 0) {
|
|
1042
|
-
const k = queue.pop();
|
|
1043
|
-
const kx = k % bw;
|
|
1044
|
-
const ky = (k - kx) / bw;
|
|
1045
|
-
for (let dy = -NOSE_GROW; dy <= NOSE_GROW; dy++) {
|
|
1046
|
-
for (let dx = -NOSE_GROW; dx <= NOSE_GROW; dx++) {
|
|
1047
|
-
const nx = kx + dx;
|
|
1048
|
-
const ny = ky + dy;
|
|
1049
|
-
if (nx < 0 || nx >= bw || ny < 0 || ny >= bh) continue;
|
|
1050
|
-
const n = ny * bw + nx;
|
|
1051
|
-
if (ink[n] === 0 && dist[n] > NOSE_INK_LOW) {
|
|
1052
|
-
ink[n] = 1;
|
|
1053
|
-
queue.push(n);
|
|
1054
|
-
}
|
|
1055
|
-
}
|
|
1056
|
-
}
|
|
1057
|
-
}
|
|
1058
|
-
const cut = new Uint8Array(bw * bh);
|
|
1059
|
-
for (let y = 0; y < bh; y++) {
|
|
1060
|
-
for (let x = 0; x < bw; x++) {
|
|
1061
|
-
if (ink[y * bw + x] === 0) continue;
|
|
1062
|
-
for (let dy = -NOSE_GROW; dy <= NOSE_GROW; dy++) {
|
|
1063
|
-
for (let dx = -NOSE_GROW; dx <= NOSE_GROW; dx++) {
|
|
1064
|
-
const yy = y + dy;
|
|
1065
|
-
const xx = x + dx;
|
|
1066
|
-
if (yy >= 0 && yy < bh && xx >= 0 && xx < bw) cut[yy * bw + xx] = 1;
|
|
1067
|
-
}
|
|
1068
|
-
}
|
|
1069
|
-
}
|
|
1070
|
-
}
|
|
1071
|
-
let cutCount = 0;
|
|
1072
|
-
for (let y = 0; y < bh; y++) {
|
|
1073
|
-
for (let x = 0; x < bw; x++) {
|
|
1074
|
-
if (cut[y * bw + x] === 0) continue;
|
|
1075
|
-
const a = rgba[at(x0 + x, y0 + y) + 3];
|
|
1076
|
-
if (a === 0) continue;
|
|
1077
|
-
if (a < 255) return null;
|
|
1078
|
-
cutCount++;
|
|
1079
|
-
}
|
|
1080
|
-
}
|
|
1081
|
-
if (cutCount > NOSE_MAX_CUT_FRACTION * opaqueCount) return null;
|
|
1082
|
-
const face = Buffer.from(rgba);
|
|
1083
|
-
const nose = Buffer.from(rgba);
|
|
1084
|
-
for (let i = 3; i < nose.length; i += 4) nose[i] = 0;
|
|
1085
|
-
let minX = W;
|
|
1086
|
-
let minY = W;
|
|
1087
|
-
let maxX = -1;
|
|
1088
|
-
let maxY = -1;
|
|
1089
|
-
for (let y = 0; y < bh; y++) {
|
|
1090
|
-
const cy = y0 + y;
|
|
1091
|
-
let x = 0;
|
|
1092
|
-
while (x < bw) {
|
|
1093
|
-
if (cut[y * bw + x] === 0) {
|
|
1094
|
-
x++;
|
|
1095
|
-
continue;
|
|
1096
|
-
}
|
|
1097
|
-
const start = x;
|
|
1098
|
-
while (x < bw && cut[y * bw + x] === 1) x++;
|
|
1099
|
-
const end = x;
|
|
1100
|
-
const skinAt = (bx) => {
|
|
1101
|
-
for (let step = 0; step < NOSE_FILL_REACH && bx >= 0 && bx < bw; step++) {
|
|
1102
|
-
const i = at(x0 + bx, cy);
|
|
1103
|
-
if (rgba[i + 3] > 0 && cut[y * bw + bx] === 0) {
|
|
1104
|
-
if (distAt(i, cy) <= NOSE_INK_TOL / 2) return i;
|
|
1105
|
-
}
|
|
1106
|
-
bx += bx < start ? -1 : 1;
|
|
1107
|
-
}
|
|
1108
|
-
return -1;
|
|
1109
|
-
};
|
|
1110
|
-
const l = skinAt(start - 1);
|
|
1111
|
-
const r = skinAt(end);
|
|
1112
|
-
for (let k = start; k < end; k++) {
|
|
1113
|
-
const cx = x0 + k;
|
|
1114
|
-
const i = at(cx, cy);
|
|
1115
|
-
if (rgba[i + 3] === 0) continue;
|
|
1116
|
-
const t = (k - start + 1) / (end - start + 1);
|
|
1117
|
-
for (let c = 0; c < 3; c++) {
|
|
1118
|
-
const rowSkin = skinRow[y];
|
|
1119
|
-
const lc = l >= 0 ? rgba[l + c] : r >= 0 ? rgba[r + c] : rowSkin[c];
|
|
1120
|
-
const rc = r >= 0 ? rgba[r + c] : l >= 0 ? rgba[l + c] : rowSkin[c];
|
|
1121
|
-
face[i + c] = Math.round(lc * (1 - t) + rc * t);
|
|
1122
|
-
}
|
|
1123
|
-
nose[i] = rgba[i];
|
|
1124
|
-
nose[i + 1] = rgba[i + 1];
|
|
1125
|
-
nose[i + 2] = rgba[i + 2];
|
|
1126
|
-
nose[i + 3] = rgba[i + 3];
|
|
1127
|
-
if (cx < minX) minX = cx;
|
|
1128
|
-
if (cx > maxX) maxX = cx;
|
|
1129
|
-
if (cy < minY) minY = cy;
|
|
1130
|
-
if (cy > maxY) maxY = cy;
|
|
1131
|
-
}
|
|
1132
|
-
}
|
|
1133
|
-
}
|
|
1134
|
-
if (maxX < 0) return null;
|
|
1135
|
-
return {
|
|
1136
|
-
face,
|
|
1137
|
-
nose,
|
|
1138
|
-
bbox: { x: minX, y: minY, w: maxX - minX + 1, h: maxY - minY + 1 }
|
|
1139
|
-
};
|
|
1140
|
-
}
|
|
1141
1455
|
async function composeLayersFromParts(input) {
|
|
1142
1456
|
try {
|
|
1143
1457
|
const outDir = resolveOutputDir(input.outDir);
|
|
@@ -1156,7 +1470,6 @@ async function composeLayersFromParts(input) {
|
|
|
1156
1470
|
const layers = [];
|
|
1157
1471
|
const skipped = [];
|
|
1158
1472
|
const preview = [];
|
|
1159
|
-
let faceRaw;
|
|
1160
1473
|
for (const role of ORDER) {
|
|
1161
1474
|
const cfg = layout[role];
|
|
1162
1475
|
const part = await partBuffer(
|
|
@@ -1167,19 +1480,13 @@ async function composeLayersFromParts(input) {
|
|
|
1167
1480
|
);
|
|
1168
1481
|
if (part === null) {
|
|
1169
1482
|
skipped.push(role);
|
|
1170
|
-
import_node_fs3.default.rmSync(
|
|
1483
|
+
import_node_fs3.default.rmSync(import_node_path5.default.join(outDir, `${role}.png`), { force: true });
|
|
1171
1484
|
continue;
|
|
1172
1485
|
}
|
|
1173
1486
|
const { left, top } = placement(role, cfg, part.w, part.h);
|
|
1174
|
-
const
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
const outPath = import_node_path4.default.join(outDir, `${role}.png`);
|
|
1178
|
-
if (role === "face") {
|
|
1179
|
-
faceRaw = await composed.raw().toBuffer();
|
|
1180
|
-
} else {
|
|
1181
|
-
writeFileAtomic(outPath, await composed.png().toBuffer());
|
|
1182
|
-
}
|
|
1487
|
+
const layer = await blankCanvas().composite([{ input: part.buf, left, top }]).png().toBuffer();
|
|
1488
|
+
const outPath = import_node_path5.default.join(outDir, `${role}.png`);
|
|
1489
|
+
writeFileAtomic(outPath, layer);
|
|
1183
1490
|
layers.push({
|
|
1184
1491
|
role,
|
|
1185
1492
|
path: outPath,
|
|
@@ -1190,48 +1497,6 @@ async function composeLayersFromParts(input) {
|
|
|
1190
1497
|
});
|
|
1191
1498
|
preview.push({ input: part.buf, left, top });
|
|
1192
1499
|
}
|
|
1193
|
-
const placed = (role) => layers.find((l) => l.role === role);
|
|
1194
|
-
const eyeL = placed("eye_L");
|
|
1195
|
-
const eyeR = placed("eye_R");
|
|
1196
|
-
const mouth = placed("mouth");
|
|
1197
|
-
const noseCut = cutNose(faceRaw, {
|
|
1198
|
-
x0: eyeR.left + eyeR.width,
|
|
1199
|
-
x1: eyeL.left,
|
|
1200
|
-
y0: Math.round(
|
|
1201
|
-
(eyeL.top + eyeL.height / 2 + eyeR.top + eyeR.height / 2) / 2
|
|
1202
|
-
),
|
|
1203
|
-
y1: mouth.top - 2
|
|
1204
|
-
});
|
|
1205
|
-
const rawCanvas = {
|
|
1206
|
-
raw: { width: CANVAS, height: CANVAS, channels: 4 }
|
|
1207
|
-
};
|
|
1208
|
-
const facePath = import_node_path4.default.join(outDir, "face.png");
|
|
1209
|
-
const nosePath = import_node_path4.default.join(outDir, "nose.png");
|
|
1210
|
-
if (noseCut === null) {
|
|
1211
|
-
writeFileAtomic(
|
|
1212
|
-
facePath,
|
|
1213
|
-
await (0, import_sharp2.default)(faceRaw, rawCanvas).png().toBuffer()
|
|
1214
|
-
);
|
|
1215
|
-
import_node_fs3.default.rmSync(nosePath, { force: true });
|
|
1216
|
-
skipped.push("nose");
|
|
1217
|
-
} else {
|
|
1218
|
-
writeFileAtomic(
|
|
1219
|
-
facePath,
|
|
1220
|
-
await (0, import_sharp2.default)(noseCut.face, rawCanvas).png().toBuffer()
|
|
1221
|
-
);
|
|
1222
|
-
writeFileAtomic(
|
|
1223
|
-
nosePath,
|
|
1224
|
-
await (0, import_sharp2.default)(noseCut.nose, rawCanvas).png().toBuffer()
|
|
1225
|
-
);
|
|
1226
|
-
layers.splice(layers.findIndex((l) => l.role === "face") + 1, 0, {
|
|
1227
|
-
role: "nose",
|
|
1228
|
-
path: nosePath,
|
|
1229
|
-
width: noseCut.bbox.w,
|
|
1230
|
-
height: noseCut.bbox.h,
|
|
1231
|
-
left: noseCut.bbox.x,
|
|
1232
|
-
top: noseCut.bbox.y
|
|
1233
|
-
});
|
|
1234
|
-
}
|
|
1235
1500
|
const previewPng = await (0, import_sharp2.default)({
|
|
1236
1501
|
create: {
|
|
1237
1502
|
width: CANVAS,
|
|
@@ -1240,7 +1505,7 @@ async function composeLayersFromParts(input) {
|
|
|
1240
1505
|
background: { r: 245, g: 245, b: 248, alpha: 1 }
|
|
1241
1506
|
}
|
|
1242
1507
|
}).composite(preview).png().toBuffer();
|
|
1243
|
-
const previewPath =
|
|
1508
|
+
const previewPath = import_node_path5.default.join(outDir, "preview.png");
|
|
1244
1509
|
writeFileAtomic(previewPath, previewPng);
|
|
1245
1510
|
return {
|
|
1246
1511
|
ok: true,
|
|
@@ -1261,7 +1526,7 @@ async function composeLayersFromParts(input) {
|
|
|
1261
1526
|
var import_mcp = require("@modelcontextprotocol/sdk/server/mcp.js");
|
|
1262
1527
|
var import_zod = require("zod");
|
|
1263
1528
|
function createIkiMcpServer() {
|
|
1264
|
-
const server = new import_mcp.McpServer({ name: "iki", version: "0.
|
|
1529
|
+
const server = new import_mcp.McpServer({ name: "iki", version: "0.8.0" });
|
|
1265
1530
|
server.registerTool(
|
|
1266
1531
|
"validate_iki",
|
|
1267
1532
|
{
|
|
@@ -1341,6 +1606,15 @@ function createIkiMcpServer() {
|
|
|
1341
1606
|
),
|
|
1342
1607
|
quantizeColors: import_zod.z.number().int().min(2).max(256).optional().describe(
|
|
1343
1608
|
"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."
|
|
1609
|
+
),
|
|
1610
|
+
turnTargets: import_zod.z.object({
|
|
1611
|
+
eyeShift: import_zod.z.number().optional(),
|
|
1612
|
+
farEyeRatio: import_zod.z.number().optional(),
|
|
1613
|
+
silhouetteRatio: import_zod.z.number().optional(),
|
|
1614
|
+
noseShift: import_zod.z.number().optional(),
|
|
1615
|
+
mouthShift: import_zod.z.number().optional()
|
|
1616
|
+
}).optional().describe(
|
|
1617
|
+
"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."
|
|
1344
1618
|
)
|
|
1345
1619
|
}
|
|
1346
1620
|
},
|
|
@@ -1362,7 +1636,7 @@ function createIkiMcpServer() {
|
|
|
1362
1636
|
server.registerTool(
|
|
1363
1637
|
"compose_layers_from_parts",
|
|
1364
1638
|
{
|
|
1365
|
-
description: "Composes AI-generated part PNGs into canvas-aligned, role-named PNG layers on disk, ready for auto_rig_from_layers (the eyewhite split,
|
|
1639
|
+
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.",
|
|
1366
1640
|
inputSchema: {
|
|
1367
1641
|
partsDir: import_zod.z.string().describe(
|
|
1368
1642
|
"Directory of the source part PNGs (resolved against cwd)."
|
|
@@ -1379,7 +1653,7 @@ function createIkiMcpServer() {
|
|
|
1379
1653
|
h: import_zod.z.number()
|
|
1380
1654
|
}).partial()
|
|
1381
1655
|
).optional().describe(
|
|
1382
|
-
"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)."
|
|
1656
|
+
"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)."
|
|
1383
1657
|
)
|
|
1384
1658
|
}
|
|
1385
1659
|
},
|
|
@@ -1429,6 +1703,43 @@ function createIkiMcpServer() {
|
|
|
1429
1703
|
}
|
|
1430
1704
|
}
|
|
1431
1705
|
);
|
|
1706
|
+
server.registerTool(
|
|
1707
|
+
"measure_turn_reference",
|
|
1708
|
+
{
|
|
1709
|
+
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.",
|
|
1710
|
+
inputSchema: {
|
|
1711
|
+
front: import_zod.z.string().describe("Front-facing PNG file path (resolved against cwd)."),
|
|
1712
|
+
turned: import_zod.z.string().describe("The same character turned, as a PNG file path."),
|
|
1713
|
+
iris: import_zod.z.object({
|
|
1714
|
+
hueMin: import_zod.z.number().min(0).max(360),
|
|
1715
|
+
hueMax: import_zod.z.number().min(0).max(360),
|
|
1716
|
+
satMin: import_zod.z.number().min(0).max(1)
|
|
1717
|
+
}).partial().optional().describe(
|
|
1718
|
+
"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."
|
|
1719
|
+
),
|
|
1720
|
+
debugDir: import_zod.z.string().optional().describe(
|
|
1721
|
+
"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."
|
|
1722
|
+
)
|
|
1723
|
+
}
|
|
1724
|
+
},
|
|
1725
|
+
async (args) => {
|
|
1726
|
+
try {
|
|
1727
|
+
const r = await measureTurnReference(args);
|
|
1728
|
+
const text = r.ok ? formatTurnReport(r) : `INVALID: ${r.error}`;
|
|
1729
|
+
return {
|
|
1730
|
+
content: [{ type: "text", text }],
|
|
1731
|
+
structuredContent: { ...r }
|
|
1732
|
+
};
|
|
1733
|
+
} catch (err) {
|
|
1734
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
1735
|
+
return {
|
|
1736
|
+
content: [{ type: "text", text: `Unexpected error: ${error}` }],
|
|
1737
|
+
structuredContent: { ok: false, error },
|
|
1738
|
+
isError: true
|
|
1739
|
+
};
|
|
1740
|
+
}
|
|
1741
|
+
}
|
|
1742
|
+
);
|
|
1432
1743
|
return server;
|
|
1433
1744
|
}
|
|
1434
1745
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -1438,8 +1749,10 @@ function createIkiMcpServer() {
|
|
|
1438
1749
|
createIkiMcpServer,
|
|
1439
1750
|
describeIki,
|
|
1440
1751
|
formatMeasureReport,
|
|
1752
|
+
formatTurnReport,
|
|
1441
1753
|
listStandardParameters,
|
|
1442
1754
|
measureLayers,
|
|
1755
|
+
measureTurnReference,
|
|
1443
1756
|
validateIki
|
|
1444
1757
|
});
|
|
1445
1758
|
//# sourceMappingURL=index.js.map
|