@ikijs/mcp 0.7.0 → 0.8.1
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 +30 -2
- package/dist/{chunk-GDLPXXJ3.mjs → chunk-LIWK4PQV.mjs} +559 -33
- package/dist/chunk-LIWK4PQV.mjs.map +1 -0
- package/dist/cli.js +542 -19
- package/dist/cli.js.map +1 -1
- package/dist/cli.mjs +1 -1
- package/dist/index.d.mts +168 -1
- package/dist/index.d.ts +168 -1
- package/dist/index.js +546 -19
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +5 -1
- package/package.json +2 -2
- package/dist/chunk-GDLPXXJ3.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,7 +868,19 @@ async function autoRigFromLayers(input) {
|
|
|
461
868
|
);
|
|
462
869
|
}
|
|
463
870
|
const buffer = await cropToBuffer(png.rgba, png.width, png.height, bbox);
|
|
464
|
-
|
|
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 });
|
|
883
|
+
const layer = {
|
|
465
884
|
role,
|
|
466
885
|
fileName,
|
|
467
886
|
canvasW,
|
|
@@ -469,13 +888,69 @@ async function autoRigFromLayers(input) {
|
|
|
469
888
|
bbox,
|
|
470
889
|
cropW: bbox.w,
|
|
471
890
|
cropH: bbox.h
|
|
472
|
-
}
|
|
891
|
+
};
|
|
892
|
+
if (role === "face") {
|
|
893
|
+
const rowHalfWidths = [];
|
|
894
|
+
for (let y = bbox.y; y < bbox.y + bbox.h; y++) {
|
|
895
|
+
rowHalfWidths.push(
|
|
896
|
+
rowRight[y] >= rowLeft[y] ? headHalfOf({ left: rowLeft[y], right: rowRight[y] }) : 0
|
|
897
|
+
);
|
|
898
|
+
}
|
|
899
|
+
layer.rowHalfWidths = rowHalfWidths;
|
|
900
|
+
}
|
|
901
|
+
layerInputs.push(layer);
|
|
473
902
|
crops.push({ id: role, buffer, width: bbox.w, height: bbox.h });
|
|
474
903
|
}
|
|
475
|
-
const
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
904
|
+
const eyeRow = eyeRowOf(layerInputs);
|
|
905
|
+
const span = foregroundSpan(
|
|
906
|
+
opaque,
|
|
907
|
+
canvasW,
|
|
908
|
+
canvasH,
|
|
909
|
+
eyeRow - HEAD_BAND,
|
|
910
|
+
eyeRow + HEAD_BAND
|
|
911
|
+
);
|
|
912
|
+
const headHalfWidth = span.right > span.left ? headHalfOf(span) : void 0;
|
|
913
|
+
const headHalfWidthApplied = headHalfWidth !== void 0 && headHalfWidth > facePlateHalfOf(layerInputs);
|
|
914
|
+
let headEdges;
|
|
915
|
+
if (headHalfWidthApplied) {
|
|
916
|
+
const bandLo = eyeRow - HEAD_BAND;
|
|
917
|
+
const bandHi = eyeRow + HEAD_BAND;
|
|
918
|
+
const left = [];
|
|
919
|
+
const right = [];
|
|
920
|
+
for (const { role, rowLeft, rowRight } of rowSpansByRole) {
|
|
921
|
+
const own = foregroundSpanOfRows(rowLeft, rowRight, bandLo, bandHi);
|
|
922
|
+
if (own.right <= own.left) continue;
|
|
923
|
+
left.push({ role, x: own.left - canvasW / 2 });
|
|
924
|
+
right.push({ role, x: own.right - canvasW / 2 });
|
|
925
|
+
}
|
|
926
|
+
headEdges = { left, right };
|
|
927
|
+
}
|
|
928
|
+
let turn;
|
|
929
|
+
let model;
|
|
930
|
+
try {
|
|
931
|
+
const { eyeShift, farEyeRatio, silhouetteRatio, noseShift, mouthShift } = input.turnTargets ?? {};
|
|
932
|
+
model = (0, import_editor2.generateIkiFromLayerSet)(
|
|
933
|
+
layerInputs,
|
|
934
|
+
{ width: canvasW, height: canvasH },
|
|
935
|
+
{
|
|
936
|
+
turnTargets: {
|
|
937
|
+
eyeShift,
|
|
938
|
+
farEyeRatio,
|
|
939
|
+
silhouetteRatio,
|
|
940
|
+
noseShift,
|
|
941
|
+
mouthShift,
|
|
942
|
+
...headHalfWidthApplied ? { headHalfWidth } : {}
|
|
943
|
+
},
|
|
944
|
+
onTurnSolved: (report) => {
|
|
945
|
+
turn = report;
|
|
946
|
+
},
|
|
947
|
+
...headEdges === void 0 ? {} : { headEdges }
|
|
948
|
+
}
|
|
949
|
+
);
|
|
950
|
+
} catch (e) {
|
|
951
|
+
if (!(e instanceof import_editor2.TurnTargetError)) throw e;
|
|
952
|
+
throw new AutoRigInputError(e.message);
|
|
953
|
+
}
|
|
479
954
|
const doc = new import_editor2.EditorDocument(model);
|
|
480
955
|
const layout = (0, import_editor2.packAtlas)(
|
|
481
956
|
crops.map((c) => ({ id: c.id, width: c.width, height: c.height }))
|
|
@@ -512,7 +987,11 @@ async function autoRigFromLayers(input) {
|
|
|
512
987
|
path: outPath,
|
|
513
988
|
canvas: { width: canvasW, height: canvasH },
|
|
514
989
|
partCount: finalModel.parts.length,
|
|
515
|
-
atlasBytes: dataUri.length
|
|
990
|
+
atlasBytes: dataUri.length,
|
|
991
|
+
...headHalfWidth === void 0 ? {} : { headHalfWidth },
|
|
992
|
+
headHalfWidthApplied,
|
|
993
|
+
...headEdges === void 0 ? {} : { headEdges },
|
|
994
|
+
...turn === void 0 ? {} : { turn }
|
|
516
995
|
};
|
|
517
996
|
} catch (err) {
|
|
518
997
|
if (err instanceof AutoRigInputError)
|
|
@@ -523,12 +1002,12 @@ async function autoRigFromLayers(input) {
|
|
|
523
1002
|
|
|
524
1003
|
// src/compose.ts
|
|
525
1004
|
var import_node_fs3 = __toESM(require("fs"));
|
|
526
|
-
var
|
|
1005
|
+
var import_node_path5 = __toESM(require("path"));
|
|
527
1006
|
var import_sharp2 = __toESM(require("sharp"));
|
|
528
1007
|
|
|
529
1008
|
// src/measure.ts
|
|
530
1009
|
var import_node_fs2 = __toESM(require("fs"));
|
|
531
|
-
var
|
|
1010
|
+
var import_node_path4 = __toESM(require("path"));
|
|
532
1011
|
var IRIS_RATIO_MIN = 0.45;
|
|
533
1012
|
var IRIS_RATIO_MAX = 0.72;
|
|
534
1013
|
var EYE_ASPECT_MIN = 0.5;
|
|
@@ -629,8 +1108,8 @@ async function measureDir(absDir) {
|
|
|
629
1108
|
const layers = {};
|
|
630
1109
|
const empty = [];
|
|
631
1110
|
for (const f of files) {
|
|
632
|
-
const role =
|
|
633
|
-
const m = await layerStats(
|
|
1111
|
+
const role = import_node_path4.default.basename(f, ".png");
|
|
1112
|
+
const m = await layerStats(import_node_path4.default.join(absDir, f));
|
|
634
1113
|
if (!m) {
|
|
635
1114
|
empty.push(role);
|
|
636
1115
|
continue;
|
|
@@ -883,7 +1362,7 @@ async function partBuffer(role, cfg, partsDir, inMemory) {
|
|
|
883
1362
|
if (inMemory !== void 0) {
|
|
884
1363
|
img = (0, import_sharp2.default)(inMemory);
|
|
885
1364
|
} else {
|
|
886
|
-
const srcPath =
|
|
1365
|
+
const srcPath = import_node_path5.default.join(partsDir, cfg.src);
|
|
887
1366
|
if (!import_node_fs3.default.existsSync(srcPath)) {
|
|
888
1367
|
if (cfg.optional) return null;
|
|
889
1368
|
throw new AutoRigInputError(
|
|
@@ -938,7 +1417,7 @@ function blankCanvas() {
|
|
|
938
1417
|
});
|
|
939
1418
|
}
|
|
940
1419
|
async function prepEyeSplit(partsDir) {
|
|
941
|
-
const srcPath =
|
|
1420
|
+
const srcPath = import_node_path5.default.join(partsDir, EYEWHITE_SRC);
|
|
942
1421
|
if (!import_node_fs3.default.existsSync(srcPath)) {
|
|
943
1422
|
throw new AutoRigInputError(`missing eyewhite source: ${srcPath}`);
|
|
944
1423
|
}
|
|
@@ -1011,12 +1490,12 @@ async function composeLayersFromParts(input) {
|
|
|
1011
1490
|
);
|
|
1012
1491
|
if (part === null) {
|
|
1013
1492
|
skipped.push(role);
|
|
1014
|
-
import_node_fs3.default.rmSync(
|
|
1493
|
+
import_node_fs3.default.rmSync(import_node_path5.default.join(outDir, `${role}.png`), { force: true });
|
|
1015
1494
|
continue;
|
|
1016
1495
|
}
|
|
1017
1496
|
const { left, top } = placement(role, cfg, part.w, part.h);
|
|
1018
1497
|
const layer = await blankCanvas().composite([{ input: part.buf, left, top }]).png().toBuffer();
|
|
1019
|
-
const outPath =
|
|
1498
|
+
const outPath = import_node_path5.default.join(outDir, `${role}.png`);
|
|
1020
1499
|
writeFileAtomic(outPath, layer);
|
|
1021
1500
|
layers.push({
|
|
1022
1501
|
role,
|
|
@@ -1036,7 +1515,7 @@ async function composeLayersFromParts(input) {
|
|
|
1036
1515
|
background: { r: 245, g: 245, b: 248, alpha: 1 }
|
|
1037
1516
|
}
|
|
1038
1517
|
}).composite(preview).png().toBuffer();
|
|
1039
|
-
const previewPath =
|
|
1518
|
+
const previewPath = import_node_path5.default.join(outDir, "preview.png");
|
|
1040
1519
|
writeFileAtomic(previewPath, previewPng);
|
|
1041
1520
|
return {
|
|
1042
1521
|
ok: true,
|
|
@@ -1057,7 +1536,7 @@ async function composeLayersFromParts(input) {
|
|
|
1057
1536
|
var import_mcp = require("@modelcontextprotocol/sdk/server/mcp.js");
|
|
1058
1537
|
var import_zod = require("zod");
|
|
1059
1538
|
function createIkiMcpServer() {
|
|
1060
|
-
const server = new import_mcp.McpServer({ name: "iki", version: "0.
|
|
1539
|
+
const server = new import_mcp.McpServer({ name: "iki", version: "0.8.1" });
|
|
1061
1540
|
server.registerTool(
|
|
1062
1541
|
"validate_iki",
|
|
1063
1542
|
{
|
|
@@ -1137,6 +1616,15 @@ function createIkiMcpServer() {
|
|
|
1137
1616
|
),
|
|
1138
1617
|
quantizeColors: import_zod.z.number().int().min(2).max(256).optional().describe(
|
|
1139
1618
|
"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."
|
|
1619
|
+
),
|
|
1620
|
+
turnTargets: import_zod.z.object({
|
|
1621
|
+
eyeShift: import_zod.z.number().optional(),
|
|
1622
|
+
farEyeRatio: import_zod.z.number().optional(),
|
|
1623
|
+
silhouetteRatio: import_zod.z.number().optional(),
|
|
1624
|
+
noseShift: import_zod.z.number().optional(),
|
|
1625
|
+
mouthShift: import_zod.z.number().optional()
|
|
1626
|
+
}).optional().describe(
|
|
1627
|
+
"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."
|
|
1140
1628
|
)
|
|
1141
1629
|
}
|
|
1142
1630
|
},
|
|
@@ -1225,6 +1713,43 @@ function createIkiMcpServer() {
|
|
|
1225
1713
|
}
|
|
1226
1714
|
}
|
|
1227
1715
|
);
|
|
1716
|
+
server.registerTool(
|
|
1717
|
+
"measure_turn_reference",
|
|
1718
|
+
{
|
|
1719
|
+
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.",
|
|
1720
|
+
inputSchema: {
|
|
1721
|
+
front: import_zod.z.string().describe("Front-facing PNG file path (resolved against cwd)."),
|
|
1722
|
+
turned: import_zod.z.string().describe("The same character turned, as a PNG file path."),
|
|
1723
|
+
iris: import_zod.z.object({
|
|
1724
|
+
hueMin: import_zod.z.number().min(0).max(360),
|
|
1725
|
+
hueMax: import_zod.z.number().min(0).max(360),
|
|
1726
|
+
satMin: import_zod.z.number().min(0).max(1)
|
|
1727
|
+
}).partial().optional().describe(
|
|
1728
|
+
"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."
|
|
1729
|
+
),
|
|
1730
|
+
debugDir: import_zod.z.string().optional().describe(
|
|
1731
|
+
"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."
|
|
1732
|
+
)
|
|
1733
|
+
}
|
|
1734
|
+
},
|
|
1735
|
+
async (args) => {
|
|
1736
|
+
try {
|
|
1737
|
+
const r = await measureTurnReference(args);
|
|
1738
|
+
const text = r.ok ? formatTurnReport(r) : `INVALID: ${r.error}`;
|
|
1739
|
+
return {
|
|
1740
|
+
content: [{ type: "text", text }],
|
|
1741
|
+
structuredContent: { ...r }
|
|
1742
|
+
};
|
|
1743
|
+
} catch (err) {
|
|
1744
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
1745
|
+
return {
|
|
1746
|
+
content: [{ type: "text", text: `Unexpected error: ${error}` }],
|
|
1747
|
+
structuredContent: { ok: false, error },
|
|
1748
|
+
isError: true
|
|
1749
|
+
};
|
|
1750
|
+
}
|
|
1751
|
+
}
|
|
1752
|
+
);
|
|
1228
1753
|
return server;
|
|
1229
1754
|
}
|
|
1230
1755
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -1234,8 +1759,10 @@ function createIkiMcpServer() {
|
|
|
1234
1759
|
createIkiMcpServer,
|
|
1235
1760
|
describeIki,
|
|
1236
1761
|
formatMeasureReport,
|
|
1762
|
+
formatTurnReport,
|
|
1237
1763
|
listStandardParameters,
|
|
1238
1764
|
measureLayers,
|
|
1765
|
+
measureTurnReference,
|
|
1239
1766
|
validateIki
|
|
1240
1767
|
});
|
|
1241
1768
|
//# sourceMappingURL=index.js.map
|