@ikijs/mcp 0.7.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 +30 -2
- package/dist/{chunk-GDLPXXJ3.mjs → chunk-JPWDKP43.mjs} +547 -31
- package/dist/chunk-JPWDKP43.mjs.map +1 -0
- package/dist/cli.js +530 -17
- package/dist/cli.js.map +1 -1
- package/dist/cli.mjs +1 -1
- package/dist/index.d.mts +163 -1
- package/dist/index.d.ts +163 -1
- package/dist/index.js +534 -17
- 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,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;
|
|
@@ -883,7 +1352,7 @@ async function partBuffer(role, cfg, partsDir, inMemory) {
|
|
|
883
1352
|
if (inMemory !== void 0) {
|
|
884
1353
|
img = (0, import_sharp2.default)(inMemory);
|
|
885
1354
|
} else {
|
|
886
|
-
const srcPath =
|
|
1355
|
+
const srcPath = import_node_path5.default.join(partsDir, cfg.src);
|
|
887
1356
|
if (!import_node_fs3.default.existsSync(srcPath)) {
|
|
888
1357
|
if (cfg.optional) return null;
|
|
889
1358
|
throw new AutoRigInputError(
|
|
@@ -938,7 +1407,7 @@ function blankCanvas() {
|
|
|
938
1407
|
});
|
|
939
1408
|
}
|
|
940
1409
|
async function prepEyeSplit(partsDir) {
|
|
941
|
-
const srcPath =
|
|
1410
|
+
const srcPath = import_node_path5.default.join(partsDir, EYEWHITE_SRC);
|
|
942
1411
|
if (!import_node_fs3.default.existsSync(srcPath)) {
|
|
943
1412
|
throw new AutoRigInputError(`missing eyewhite source: ${srcPath}`);
|
|
944
1413
|
}
|
|
@@ -1011,12 +1480,12 @@ async function composeLayersFromParts(input) {
|
|
|
1011
1480
|
);
|
|
1012
1481
|
if (part === null) {
|
|
1013
1482
|
skipped.push(role);
|
|
1014
|
-
import_node_fs3.default.rmSync(
|
|
1483
|
+
import_node_fs3.default.rmSync(import_node_path5.default.join(outDir, `${role}.png`), { force: true });
|
|
1015
1484
|
continue;
|
|
1016
1485
|
}
|
|
1017
1486
|
const { left, top } = placement(role, cfg, part.w, part.h);
|
|
1018
1487
|
const layer = await blankCanvas().composite([{ input: part.buf, left, top }]).png().toBuffer();
|
|
1019
|
-
const outPath =
|
|
1488
|
+
const outPath = import_node_path5.default.join(outDir, `${role}.png`);
|
|
1020
1489
|
writeFileAtomic(outPath, layer);
|
|
1021
1490
|
layers.push({
|
|
1022
1491
|
role,
|
|
@@ -1036,7 +1505,7 @@ async function composeLayersFromParts(input) {
|
|
|
1036
1505
|
background: { r: 245, g: 245, b: 248, alpha: 1 }
|
|
1037
1506
|
}
|
|
1038
1507
|
}).composite(preview).png().toBuffer();
|
|
1039
|
-
const previewPath =
|
|
1508
|
+
const previewPath = import_node_path5.default.join(outDir, "preview.png");
|
|
1040
1509
|
writeFileAtomic(previewPath, previewPng);
|
|
1041
1510
|
return {
|
|
1042
1511
|
ok: true,
|
|
@@ -1057,7 +1526,7 @@ async function composeLayersFromParts(input) {
|
|
|
1057
1526
|
var import_mcp = require("@modelcontextprotocol/sdk/server/mcp.js");
|
|
1058
1527
|
var import_zod = require("zod");
|
|
1059
1528
|
function createIkiMcpServer() {
|
|
1060
|
-
const server = new import_mcp.McpServer({ name: "iki", version: "0.
|
|
1529
|
+
const server = new import_mcp.McpServer({ name: "iki", version: "0.8.0" });
|
|
1061
1530
|
server.registerTool(
|
|
1062
1531
|
"validate_iki",
|
|
1063
1532
|
{
|
|
@@ -1137,6 +1606,15 @@ function createIkiMcpServer() {
|
|
|
1137
1606
|
),
|
|
1138
1607
|
quantizeColors: import_zod.z.number().int().min(2).max(256).optional().describe(
|
|
1139
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."
|
|
1140
1618
|
)
|
|
1141
1619
|
}
|
|
1142
1620
|
},
|
|
@@ -1225,6 +1703,43 @@ function createIkiMcpServer() {
|
|
|
1225
1703
|
}
|
|
1226
1704
|
}
|
|
1227
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
|
+
);
|
|
1228
1743
|
return server;
|
|
1229
1744
|
}
|
|
1230
1745
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -1234,8 +1749,10 @@ function createIkiMcpServer() {
|
|
|
1234
1749
|
createIkiMcpServer,
|
|
1235
1750
|
describeIki,
|
|
1236
1751
|
formatMeasureReport,
|
|
1752
|
+
formatTurnReport,
|
|
1237
1753
|
listStandardParameters,
|
|
1238
1754
|
measureLayers,
|
|
1755
|
+
measureTurnReference,
|
|
1239
1756
|
validateIki
|
|
1240
1757
|
});
|
|
1241
1758
|
//# sourceMappingURL=index.js.map
|