@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
|
@@ -1,18 +1,5 @@
|
|
|
1
|
-
// src/
|
|
1
|
+
// src/measure-turn.ts
|
|
2
2
|
import path2 from "path";
|
|
3
|
-
import {
|
|
4
|
-
parseIkiModel,
|
|
5
|
-
loadIkiModel,
|
|
6
|
-
IkiFormatError,
|
|
7
|
-
StandardParameter
|
|
8
|
-
} from "@ikijs/format";
|
|
9
|
-
import {
|
|
10
|
-
EditorDocument,
|
|
11
|
-
packAtlas,
|
|
12
|
-
uvRectFor,
|
|
13
|
-
generateIkiFromLayerSet,
|
|
14
|
-
parseLayerRoles
|
|
15
|
-
} from "@ikijs/editor";
|
|
16
3
|
|
|
17
4
|
// src/node-images.ts
|
|
18
5
|
import sharp from "sharp";
|
|
@@ -181,6 +168,9 @@ function detectAlphaBbox(rgba, width, height) {
|
|
|
181
168
|
function cropToBuffer(rgba, width, height, bbox) {
|
|
182
169
|
return sharp(rgba, { raw: { width, height, channels: 4 } }).extract({ left: bbox.x, top: bbox.y, width: bbox.w, height: bbox.h }).png().toBuffer();
|
|
183
170
|
}
|
|
171
|
+
function encodeOverlayPng(filePath, svg) {
|
|
172
|
+
return sharp(filePath, { limitInputPixels: MAX_INPUT_PIXELS }).composite([{ input: Buffer.from(svg) }]).png().toBuffer();
|
|
173
|
+
}
|
|
184
174
|
async function renderAtlasToDataUri(crops, layout, quantizeColors) {
|
|
185
175
|
const byId = /* @__PURE__ */ new Map();
|
|
186
176
|
for (const crop of crops) byId.set(crop.id, crop);
|
|
@@ -210,7 +200,388 @@ async function renderAtlasToDataUri(crops, layout, quantizeColors) {
|
|
|
210
200
|
return `data:image/png;base64,${page.toString("base64")}`;
|
|
211
201
|
}
|
|
212
202
|
|
|
203
|
+
// src/measure-turn.ts
|
|
204
|
+
var DEFAULT_IRIS = { hueMin: 230, hueMax: 300, satMin: 0.22 };
|
|
205
|
+
var IRIS_V_MIN = 0.18;
|
|
206
|
+
var IRIS_V_MAX = 0.95;
|
|
207
|
+
var IRIS_MEAN_V_MIN = 0.32;
|
|
208
|
+
var CLOSE_RADIUS = 3;
|
|
209
|
+
var CLOSE_RADIUS_REFERENCE_PX = 1254;
|
|
210
|
+
var BLOB_MAX_FRAC = 0.12;
|
|
211
|
+
var KEYED_SPAN_MAX_FRAC = 0.95;
|
|
212
|
+
var MIN_AREA_REFERENCE_PIXELS = 1254 * 1254;
|
|
213
|
+
var MIN_AREA = 300;
|
|
214
|
+
var PAIR_DX_MIN_FRAC = 0.08;
|
|
215
|
+
var PAIR_DX_MAX_FRAC = 0.35;
|
|
216
|
+
var PAIR_DY_MAX_FRAC = 0.05;
|
|
217
|
+
var MAX_CANDIDATES = 12;
|
|
218
|
+
var HEAD_BAND = 10;
|
|
219
|
+
var ALPHA_OPAQUE = 128;
|
|
220
|
+
var BG_HUE_MIN = 220;
|
|
221
|
+
var BG_HUE_MAX = 260;
|
|
222
|
+
var BG_SAT_MAX = 0.2;
|
|
223
|
+
var BG_V_MIN = 0.55;
|
|
224
|
+
var BG_V_BLACK = 0.03;
|
|
225
|
+
function rgbToHsv(r, g, b) {
|
|
226
|
+
r /= 255;
|
|
227
|
+
g /= 255;
|
|
228
|
+
b /= 255;
|
|
229
|
+
const max = Math.max(r, g, b);
|
|
230
|
+
const min = Math.min(r, g, b);
|
|
231
|
+
const d = max - min;
|
|
232
|
+
let h = 0;
|
|
233
|
+
if (d > 0) {
|
|
234
|
+
if (max === r) h = (g - b) / d % 6;
|
|
235
|
+
else if (max === g) h = (b - r) / d + 2;
|
|
236
|
+
else h = (r - g) / d + 4;
|
|
237
|
+
h *= 60;
|
|
238
|
+
if (h < 0) h += 360;
|
|
239
|
+
}
|
|
240
|
+
return [h, max === 0 ? 0 : d / max, max];
|
|
241
|
+
}
|
|
242
|
+
function dilate(mask, width, height, r) {
|
|
243
|
+
const out = new Uint8Array(width * height);
|
|
244
|
+
for (let y = 0; y < height; y++) {
|
|
245
|
+
for (let x = 0; x < width; x++) {
|
|
246
|
+
if (!mask[y * width + x]) continue;
|
|
247
|
+
for (let dy = -r; dy <= r; dy++) {
|
|
248
|
+
for (let dx = -r; dx <= r; dx++) {
|
|
249
|
+
const yy = y + dy;
|
|
250
|
+
const xx = x + dx;
|
|
251
|
+
if (yy >= 0 && yy < height && xx >= 0 && xx < width)
|
|
252
|
+
out[yy * width + xx] = 1;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return out;
|
|
258
|
+
}
|
|
259
|
+
function erode(mask, width, height, r) {
|
|
260
|
+
const out = new Uint8Array(width * height);
|
|
261
|
+
for (let y = 0; y < height; y++) {
|
|
262
|
+
for (let x = 0; x < width; x++) {
|
|
263
|
+
if (!mask[y * width + x]) continue;
|
|
264
|
+
let keep = true;
|
|
265
|
+
for (let dy = -r; dy <= r && keep; dy++) {
|
|
266
|
+
for (let dx = -r; dx <= r; dx++) {
|
|
267
|
+
const yy = y + dy;
|
|
268
|
+
const xx = x + dx;
|
|
269
|
+
if (yy < 0 || yy >= height || xx < 0 || xx >= width || !mask[yy * width + xx]) {
|
|
270
|
+
keep = false;
|
|
271
|
+
break;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
if (keep) out[y * width + x] = 1;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
return out;
|
|
279
|
+
}
|
|
280
|
+
function closeMask(mask, width, height, r) {
|
|
281
|
+
return erode(dilate(mask, width, height, r), width, height, r);
|
|
282
|
+
}
|
|
283
|
+
function components(mask, width, height, minArea) {
|
|
284
|
+
const seen = new Uint8Array(width * height);
|
|
285
|
+
const out = [];
|
|
286
|
+
for (let i = 0; i < width * height; i++) {
|
|
287
|
+
if (!mask[i] || seen[i]) continue;
|
|
288
|
+
const stack = [i];
|
|
289
|
+
seen[i] = 1;
|
|
290
|
+
let x0 = width;
|
|
291
|
+
let x1 = 0;
|
|
292
|
+
let y0 = height;
|
|
293
|
+
let y1 = 0;
|
|
294
|
+
let n = 0;
|
|
295
|
+
let sx = 0;
|
|
296
|
+
let sy = 0;
|
|
297
|
+
while (stack.length) {
|
|
298
|
+
const p = stack.pop();
|
|
299
|
+
const x = p % width;
|
|
300
|
+
const y = p / width | 0;
|
|
301
|
+
n++;
|
|
302
|
+
sx += x;
|
|
303
|
+
sy += y;
|
|
304
|
+
if (x < x0) x0 = x;
|
|
305
|
+
if (x > x1) x1 = x;
|
|
306
|
+
if (y < y0) y0 = y;
|
|
307
|
+
if (y > y1) y1 = y;
|
|
308
|
+
const neighbours = [];
|
|
309
|
+
if (x > 0) neighbours.push(p - 1);
|
|
310
|
+
if (x < width - 1) neighbours.push(p + 1);
|
|
311
|
+
if (y > 0) neighbours.push(p - width);
|
|
312
|
+
if (y < height - 1) neighbours.push(p + width);
|
|
313
|
+
for (const q of neighbours) {
|
|
314
|
+
if (seen[q] || !mask[q]) continue;
|
|
315
|
+
seen[q] = 1;
|
|
316
|
+
stack.push(q);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
if (n >= minArea) {
|
|
320
|
+
out.push({
|
|
321
|
+
x0,
|
|
322
|
+
x1,
|
|
323
|
+
y0,
|
|
324
|
+
y1,
|
|
325
|
+
w: x1 - x0 + 1,
|
|
326
|
+
h: y1 - y0 + 1,
|
|
327
|
+
area: n,
|
|
328
|
+
cx: sx / n,
|
|
329
|
+
cy: sy / n
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
return out.sort((a, b) => b.area - a.area);
|
|
334
|
+
}
|
|
335
|
+
function foregroundSpan(mask, width, height, rowLo, rowHi) {
|
|
336
|
+
let left = width;
|
|
337
|
+
let right = -1;
|
|
338
|
+
for (let y = Math.max(0, rowLo); y <= Math.min(height - 1, rowHi); y++) {
|
|
339
|
+
for (let x = 0; x < width; x++) {
|
|
340
|
+
if (!mask[y * width + x]) continue;
|
|
341
|
+
if (x < left) left = x;
|
|
342
|
+
if (x > right) right = x;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
return { left, right };
|
|
346
|
+
}
|
|
347
|
+
function headHalfOf(span) {
|
|
348
|
+
return (span.right - span.left + 1) / 2;
|
|
349
|
+
}
|
|
350
|
+
function resolveIris(override) {
|
|
351
|
+
const iris = { ...DEFAULT_IRIS, ...override };
|
|
352
|
+
if (iris.hueMin < 0 || iris.hueMax > 360 || iris.hueMin >= iris.hueMax) {
|
|
353
|
+
throw new AutoRigInputError(
|
|
354
|
+
`iris hue window must be 0 <= hueMin < hueMax <= 360, got ${iris.hueMin}..${iris.hueMax}`
|
|
355
|
+
);
|
|
356
|
+
}
|
|
357
|
+
if (iris.satMin < 0 || iris.satMin > 1) {
|
|
358
|
+
throw new AutoRigInputError(
|
|
359
|
+
`iris satMin must be within 0..1, got ${iris.satMin}`
|
|
360
|
+
);
|
|
361
|
+
}
|
|
362
|
+
return iris;
|
|
363
|
+
}
|
|
364
|
+
async function measureTurnImage(filePath, iris) {
|
|
365
|
+
const { width, height, rgba, hasAlpha } = await decodePng(filePath);
|
|
366
|
+
const n = width * height;
|
|
367
|
+
let transparent = false;
|
|
368
|
+
if (hasAlpha) {
|
|
369
|
+
for (let i = 0; i < n; i++) {
|
|
370
|
+
if (rgba[i * 4 + 3] < ALPHA_OPAQUE) {
|
|
371
|
+
transparent = true;
|
|
372
|
+
break;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
const maskMode = hasAlpha && transparent ? "alpha" : "keyed";
|
|
377
|
+
const irisMask = new Uint8Array(n);
|
|
378
|
+
const fg = new Uint8Array(n);
|
|
379
|
+
const value = new Float32Array(n);
|
|
380
|
+
for (let i = 0; i < n; i++) {
|
|
381
|
+
if (rgba[i * 4 + 3] < ALPHA_OPAQUE) continue;
|
|
382
|
+
const [hue, s, v] = rgbToHsv(rgba[i * 4], rgba[i * 4 + 1], rgba[i * 4 + 2]);
|
|
383
|
+
if (maskMode === "alpha") {
|
|
384
|
+
fg[i] = 1;
|
|
385
|
+
} else {
|
|
386
|
+
const isBg = hue >= BG_HUE_MIN && hue <= BG_HUE_MAX && s < BG_SAT_MAX && v > BG_V_MIN || v < BG_V_BLACK;
|
|
387
|
+
if (!isBg) fg[i] = 1;
|
|
388
|
+
}
|
|
389
|
+
if (hue >= iris.hueMin && hue <= iris.hueMax && s > iris.satMin && v > IRIS_V_MIN && v < IRIS_V_MAX) {
|
|
390
|
+
irisMask[i] = 1;
|
|
391
|
+
value[i] = v;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
const minArea = Math.max(
|
|
395
|
+
1,
|
|
396
|
+
Math.round(MIN_AREA * n / MIN_AREA_REFERENCE_PIXELS)
|
|
397
|
+
);
|
|
398
|
+
const closeRadius = Math.max(
|
|
399
|
+
1,
|
|
400
|
+
Math.round(
|
|
401
|
+
CLOSE_RADIUS * Math.max(width, height) / CLOSE_RADIUS_REFERENCE_PX
|
|
402
|
+
)
|
|
403
|
+
);
|
|
404
|
+
const meanValue = (b) => {
|
|
405
|
+
let count = 0;
|
|
406
|
+
let total = 0;
|
|
407
|
+
for (let y = b.y0; y <= b.y1; y++) {
|
|
408
|
+
for (let x = b.x0; x <= b.x1; x++) {
|
|
409
|
+
const i = y * width + x;
|
|
410
|
+
if (!irisMask[i]) continue;
|
|
411
|
+
count++;
|
|
412
|
+
total += value[i];
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
return count ? total / count : 0;
|
|
416
|
+
};
|
|
417
|
+
const candidates = components(
|
|
418
|
+
closeMask(irisMask, width, height, closeRadius),
|
|
419
|
+
width,
|
|
420
|
+
height,
|
|
421
|
+
minArea
|
|
422
|
+
).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);
|
|
423
|
+
let best = null;
|
|
424
|
+
for (let i = 0; i < candidates.length; i++) {
|
|
425
|
+
for (let j = i + 1; j < candidates.length; j++) {
|
|
426
|
+
const a = candidates[i];
|
|
427
|
+
const b = candidates[j];
|
|
428
|
+
const dx = Math.abs(a.cx - b.cx);
|
|
429
|
+
const dy = Math.abs(a.cy - b.cy);
|
|
430
|
+
if (dx < width * PAIR_DX_MIN_FRAC || dx > width * PAIR_DX_MAX_FRAC)
|
|
431
|
+
continue;
|
|
432
|
+
if (dy > height * PAIR_DY_MAX_FRAC) continue;
|
|
433
|
+
const score = a.area + b.area;
|
|
434
|
+
if (best === null || score > best.score) {
|
|
435
|
+
const pair = a.cx <= b.cx ? [a, b] : [b, a];
|
|
436
|
+
best = { score, pair };
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
if (best === null) {
|
|
441
|
+
throw new AutoRigInputError(
|
|
442
|
+
`no iris pair found in ${filePath} among ${candidates.length} candidate blobs (iris hue ${iris.hueMin}..${iris.hueMax}, sat > ${iris.satMin})`
|
|
443
|
+
);
|
|
444
|
+
}
|
|
445
|
+
const [irisL, irisR] = best.pair;
|
|
446
|
+
const eyeRow = Math.round((irisL.cy + irisR.cy) / 2);
|
|
447
|
+
const { left, right } = foregroundSpan(
|
|
448
|
+
fg,
|
|
449
|
+
width,
|
|
450
|
+
height,
|
|
451
|
+
eyeRow - HEAD_BAND,
|
|
452
|
+
eyeRow + HEAD_BAND
|
|
453
|
+
);
|
|
454
|
+
if (right <= left) {
|
|
455
|
+
throw new AutoRigInputError(
|
|
456
|
+
`no head span at the eye row (y=${eyeRow}) in ${filePath}: the ${maskMode} foreground mask is empty there`
|
|
457
|
+
);
|
|
458
|
+
}
|
|
459
|
+
if (maskMode === "keyed" && right - left + 1 > width * KEYED_SPAN_MAX_FRAC) {
|
|
460
|
+
throw new AutoRigInputError(
|
|
461
|
+
`head span at the eye row (y=${eyeRow}) in ${filePath} covers ${right - left + 1}/${width} px: background not keyed / not an engine render`
|
|
462
|
+
);
|
|
463
|
+
}
|
|
464
|
+
return {
|
|
465
|
+
file: filePath,
|
|
466
|
+
width,
|
|
467
|
+
height,
|
|
468
|
+
maskMode,
|
|
469
|
+
irisL,
|
|
470
|
+
irisR,
|
|
471
|
+
eyeRow,
|
|
472
|
+
head: {
|
|
473
|
+
left,
|
|
474
|
+
right,
|
|
475
|
+
cx: (left + right) / 2,
|
|
476
|
+
half: headHalfOf({ left, right })
|
|
477
|
+
},
|
|
478
|
+
pairCx: (irisL.cx + irisR.cx) / 2,
|
|
479
|
+
eyeGap: irisR.cx - irisL.cx
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
function farNearWidths(m, turnSign) {
|
|
483
|
+
return turnSign < 0 ? { far: m.irisL.w, near: m.irisR.w } : { far: m.irisR.w, near: m.irisL.w };
|
|
484
|
+
}
|
|
485
|
+
function overlaySvg(m) {
|
|
486
|
+
const box = (b, stroke) => `<rect x="${b.x0}" y="${b.y0}" width="${b.w}" height="${b.h}" fill="none" stroke="${stroke}" stroke-width="3"/>`;
|
|
487
|
+
const tick = (x, half, stroke) => `<line x1="${x}" y1="${m.eyeRow - half}" x2="${x}" y2="${m.eyeRow + half}" stroke="${stroke}" stroke-width="3"/>`;
|
|
488
|
+
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>";
|
|
489
|
+
}
|
|
490
|
+
async function writeOverlay(debugDir, label, m) {
|
|
491
|
+
const outPath = path2.join(
|
|
492
|
+
debugDir,
|
|
493
|
+
`${label}-${path2.basename(m.file, ".png")}.debug.png`
|
|
494
|
+
);
|
|
495
|
+
writeFileAtomic(outPath, await encodeOverlayPng(m.file, overlaySvg(m)));
|
|
496
|
+
return outPath;
|
|
497
|
+
}
|
|
498
|
+
var IRIS_HEIGHT_SCALE_TOLERANCE = 0.1;
|
|
499
|
+
async function measureTurnReference(input) {
|
|
500
|
+
try {
|
|
501
|
+
const iris = resolveIris(input.iris);
|
|
502
|
+
const frontPath = resolveInputPath(input.front);
|
|
503
|
+
const turnedPath = resolveInputPath(input.turned);
|
|
504
|
+
const debugDir = input.debugDir === void 0 ? null : resolveOutputDir(input.debugDir);
|
|
505
|
+
const front = await measureTurnImage(frontPath, iris);
|
|
506
|
+
const turned = await measureTurnImage(turnedPath, iris);
|
|
507
|
+
const frontIrisH = (front.irisL.h + front.irisR.h) / 2;
|
|
508
|
+
const turnedIrisH = (turned.irisL.h + turned.irisR.h) / 2;
|
|
509
|
+
if (Math.abs(turnedIrisH - frontIrisH) / frontIrisH > IRIS_HEIGHT_SCALE_TOLERANCE) {
|
|
510
|
+
throw new AutoRigInputError(
|
|
511
|
+
`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`
|
|
512
|
+
);
|
|
513
|
+
}
|
|
514
|
+
const frontShift = front.pairCx - front.head.cx;
|
|
515
|
+
const turnedShift = turned.pairCx - turned.head.cx;
|
|
516
|
+
const shift = turnedShift - frontShift;
|
|
517
|
+
const turnSign = shift < 0 ? -1 : 1;
|
|
518
|
+
const f = farNearWidths(front, turnSign);
|
|
519
|
+
const t = farNearWidths(turned, turnSign);
|
|
520
|
+
const debug = debugDir === null ? void 0 : [
|
|
521
|
+
await writeOverlay(debugDir, "front", front),
|
|
522
|
+
await writeOverlay(debugDir, "turned", turned)
|
|
523
|
+
];
|
|
524
|
+
return {
|
|
525
|
+
ok: true,
|
|
526
|
+
front,
|
|
527
|
+
turned,
|
|
528
|
+
farEyeRatio: t.far / t.near / (f.far / f.near),
|
|
529
|
+
eyeShift: shift / front.head.half,
|
|
530
|
+
silhouetteRatio: turned.head.half / front.head.half,
|
|
531
|
+
turnSign,
|
|
532
|
+
...debug === void 0 ? {} : { debug }
|
|
533
|
+
};
|
|
534
|
+
} catch (err) {
|
|
535
|
+
if (err instanceof AutoRigInputError)
|
|
536
|
+
return { ok: false, error: err.message };
|
|
537
|
+
throw err;
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
var n1 = (v) => v.toFixed(1);
|
|
541
|
+
var n3 = (v) => v.toFixed(3);
|
|
542
|
+
function imageBlock(label, m) {
|
|
543
|
+
return [
|
|
544
|
+
`# ${label} ${m.file} ${m.width}x${m.height} mask: ${m.maskMode}`,
|
|
545
|
+
`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)}`,
|
|
546
|
+
`head ${m.head.left}..${m.head.right} centre ${n1(m.head.cx)} half ${n1(m.head.half)} pair centre ${n1(m.pairCx)}`
|
|
547
|
+
];
|
|
548
|
+
}
|
|
549
|
+
function formatTurnReport(result) {
|
|
550
|
+
const { front, turned } = result;
|
|
551
|
+
const f = farNearWidths(front, result.turnSign);
|
|
552
|
+
const t = farNearWidths(turned, result.turnSign);
|
|
553
|
+
const lines = [
|
|
554
|
+
`farEyeRatio ${n3(result.farEyeRatio)} far/near iris width ${t.far}/${t.near} turned over ${f.far}/${f.near} front`,
|
|
555
|
+
`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)`,
|
|
556
|
+
`silhouetteRatio ${n3(result.silhouetteRatio)} head half ${n1(turned.head.half)} px turned over ${n1(front.head.half)} px front`,
|
|
557
|
+
`turnSign ${result.turnSign} (the head turned toward the image's ${result.turnSign < 0 ? "left" : "right"})`,
|
|
558
|
+
"",
|
|
559
|
+
...imageBlock("front", front),
|
|
560
|
+
"",
|
|
561
|
+
...imageBlock("turned", turned)
|
|
562
|
+
];
|
|
563
|
+
if (result.debug !== void 0) {
|
|
564
|
+
lines.push("", ...result.debug.map((p) => `debug overlay ${p}`));
|
|
565
|
+
}
|
|
566
|
+
return lines.join("\n");
|
|
567
|
+
}
|
|
568
|
+
|
|
213
569
|
// src/tools.ts
|
|
570
|
+
import path3 from "path";
|
|
571
|
+
import {
|
|
572
|
+
parseIkiModel,
|
|
573
|
+
loadIkiModel,
|
|
574
|
+
IkiFormatError,
|
|
575
|
+
StandardParameter
|
|
576
|
+
} from "@ikijs/format";
|
|
577
|
+
import {
|
|
578
|
+
EditorDocument,
|
|
579
|
+
packAtlas,
|
|
580
|
+
uvRectFor,
|
|
581
|
+
generateIkiFromLayerSet,
|
|
582
|
+
parseLayerRoles,
|
|
583
|
+
TurnTargetError
|
|
584
|
+
} from "@ikijs/editor";
|
|
214
585
|
function coerceModel(model) {
|
|
215
586
|
try {
|
|
216
587
|
const parsed = typeof model === "string" ? loadIkiModel(model) : parseIkiModel(model);
|
|
@@ -354,6 +725,38 @@ function expectInput(label, fn) {
|
|
|
354
725
|
);
|
|
355
726
|
}
|
|
356
727
|
}
|
|
728
|
+
function eyeRowOf(layers) {
|
|
729
|
+
const centreY = (role) => {
|
|
730
|
+
const layer = layers.find((l) => l.role === role);
|
|
731
|
+
return layer && layer.bbox.y + layer.bbox.h / 2;
|
|
732
|
+
};
|
|
733
|
+
const pairRow = (left, right) => {
|
|
734
|
+
const a = centreY(left);
|
|
735
|
+
const b = centreY(right);
|
|
736
|
+
return a === void 0 || b === void 0 ? void 0 : (a + b) / 2;
|
|
737
|
+
};
|
|
738
|
+
const row = pairRow("iris_L", "iris_R") ?? pairRow("eye_L", "eye_R");
|
|
739
|
+
if (row === void 0) {
|
|
740
|
+
throw new Error("auto-rig: no eye pair to measure the head width at");
|
|
741
|
+
}
|
|
742
|
+
return Math.round(row);
|
|
743
|
+
}
|
|
744
|
+
function foregroundSpanOfRows(rowLeft, rowRight, rowLo, rowHi) {
|
|
745
|
+
let left = Infinity;
|
|
746
|
+
let right = -Infinity;
|
|
747
|
+
for (let y = Math.max(0, rowLo); y <= Math.min(rowLeft.length - 1, rowHi); y++) {
|
|
748
|
+
if (rowLeft[y] < left) left = rowLeft[y];
|
|
749
|
+
if (rowRight[y] > right) right = rowRight[y];
|
|
750
|
+
}
|
|
751
|
+
return { left, right };
|
|
752
|
+
}
|
|
753
|
+
function facePlateHalfOf(layers) {
|
|
754
|
+
const face = layers.find((l) => l.role === "face");
|
|
755
|
+
if (face === void 0) {
|
|
756
|
+
throw new Error("auto-rig: no face layer to measure the plate against");
|
|
757
|
+
}
|
|
758
|
+
return face.cropW / 2;
|
|
759
|
+
}
|
|
357
760
|
async function autoRigFromLayers(input) {
|
|
358
761
|
try {
|
|
359
762
|
const layers = input.layers;
|
|
@@ -376,7 +779,7 @@ async function autoRigFromLayers(input) {
|
|
|
376
779
|
}
|
|
377
780
|
const resolvedLayers = layers.map((layer) => {
|
|
378
781
|
const resolved = resolveInputPath(layer.path);
|
|
379
|
-
return { resolved, fileName: layer.fileName ??
|
|
782
|
+
return { resolved, fileName: layer.fileName ?? path3.basename(resolved) };
|
|
380
783
|
});
|
|
381
784
|
const rolePairs = expectInput(
|
|
382
785
|
"role parsing",
|
|
@@ -386,8 +789,10 @@ async function autoRigFromLayers(input) {
|
|
|
386
789
|
let canvasW = 0;
|
|
387
790
|
let canvasH = 0;
|
|
388
791
|
let totalPixels = 0;
|
|
792
|
+
let opaque = new Uint8Array(0);
|
|
389
793
|
const layerInputs = [];
|
|
390
794
|
const crops = [];
|
|
795
|
+
const rowSpansByRole = [];
|
|
391
796
|
for (let i = 0; i < resolvedLayers.length; i++) {
|
|
392
797
|
const { resolved, fileName } = resolvedLayers[i];
|
|
393
798
|
const png = await decodePng(resolved);
|
|
@@ -410,6 +815,7 @@ async function autoRigFromLayers(input) {
|
|
|
410
815
|
`canvas ${canvasW}x${canvasH} exceeds ${MAX_CANVAS_DIM}`
|
|
411
816
|
);
|
|
412
817
|
}
|
|
818
|
+
opaque = new Uint8Array(canvasW * canvasH);
|
|
413
819
|
} else if (png.width !== canvasW || png.height !== canvasH) {
|
|
414
820
|
throw new AutoRigInputError(
|
|
415
821
|
`layer "${fileName}" size ${png.width}x${png.height} differs from canvas ${canvasW}x${canvasH}`
|
|
@@ -429,6 +835,18 @@ async function autoRigFromLayers(input) {
|
|
|
429
835
|
);
|
|
430
836
|
}
|
|
431
837
|
const buffer = await cropToBuffer(png.rgba, png.width, png.height, bbox);
|
|
838
|
+
const rowLeft = new Int32Array(canvasH).fill(canvasW);
|
|
839
|
+
const rowRight = new Int32Array(canvasH).fill(-1);
|
|
840
|
+
for (let y = 0; y < canvasH; y++) {
|
|
841
|
+
for (let x = 0; x < canvasW; x++) {
|
|
842
|
+
const p = y * canvasW + x;
|
|
843
|
+
if (png.rgba[p * 4 + 3] < ALPHA_OPAQUE) continue;
|
|
844
|
+
opaque[p] = 1;
|
|
845
|
+
if (x < rowLeft[y]) rowLeft[y] = x;
|
|
846
|
+
if (x > rowRight[y]) rowRight[y] = x;
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
rowSpansByRole.push({ role, rowLeft, rowRight });
|
|
432
850
|
layerInputs.push({
|
|
433
851
|
role,
|
|
434
852
|
fileName,
|
|
@@ -440,10 +858,56 @@ async function autoRigFromLayers(input) {
|
|
|
440
858
|
});
|
|
441
859
|
crops.push({ id: role, buffer, width: bbox.w, height: bbox.h });
|
|
442
860
|
}
|
|
443
|
-
const
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
861
|
+
const eyeRow = eyeRowOf(layerInputs);
|
|
862
|
+
const span = foregroundSpan(
|
|
863
|
+
opaque,
|
|
864
|
+
canvasW,
|
|
865
|
+
canvasH,
|
|
866
|
+
eyeRow - HEAD_BAND,
|
|
867
|
+
eyeRow + HEAD_BAND
|
|
868
|
+
);
|
|
869
|
+
const headHalfWidth = span.right > span.left ? headHalfOf(span) : void 0;
|
|
870
|
+
const headHalfWidthApplied = headHalfWidth !== void 0 && headHalfWidth > facePlateHalfOf(layerInputs);
|
|
871
|
+
let headEdges;
|
|
872
|
+
if (headHalfWidthApplied) {
|
|
873
|
+
const bandLo = eyeRow - HEAD_BAND;
|
|
874
|
+
const bandHi = eyeRow + HEAD_BAND;
|
|
875
|
+
const left = [];
|
|
876
|
+
const right = [];
|
|
877
|
+
for (const { role, rowLeft, rowRight } of rowSpansByRole) {
|
|
878
|
+
const own = foregroundSpanOfRows(rowLeft, rowRight, bandLo, bandHi);
|
|
879
|
+
if (own.right <= own.left) continue;
|
|
880
|
+
left.push({ role, x: own.left - canvasW / 2 });
|
|
881
|
+
right.push({ role, x: own.right - canvasW / 2 });
|
|
882
|
+
}
|
|
883
|
+
headEdges = { left, right };
|
|
884
|
+
}
|
|
885
|
+
let turn;
|
|
886
|
+
let model;
|
|
887
|
+
try {
|
|
888
|
+
const { eyeShift, farEyeRatio, silhouetteRatio, noseShift, mouthShift } = input.turnTargets ?? {};
|
|
889
|
+
model = generateIkiFromLayerSet(
|
|
890
|
+
layerInputs,
|
|
891
|
+
{ width: canvasW, height: canvasH },
|
|
892
|
+
{
|
|
893
|
+
turnTargets: {
|
|
894
|
+
eyeShift,
|
|
895
|
+
farEyeRatio,
|
|
896
|
+
silhouetteRatio,
|
|
897
|
+
noseShift,
|
|
898
|
+
mouthShift,
|
|
899
|
+
...headHalfWidthApplied ? { headHalfWidth } : {}
|
|
900
|
+
},
|
|
901
|
+
onTurnSolved: (report) => {
|
|
902
|
+
turn = report;
|
|
903
|
+
},
|
|
904
|
+
...headEdges === void 0 ? {} : { headEdges }
|
|
905
|
+
}
|
|
906
|
+
);
|
|
907
|
+
} catch (e) {
|
|
908
|
+
if (!(e instanceof TurnTargetError)) throw e;
|
|
909
|
+
throw new AutoRigInputError(e.message);
|
|
910
|
+
}
|
|
447
911
|
const doc = new EditorDocument(model);
|
|
448
912
|
const layout = packAtlas(
|
|
449
913
|
crops.map((c) => ({ id: c.id, width: c.width, height: c.height }))
|
|
@@ -480,7 +944,11 @@ async function autoRigFromLayers(input) {
|
|
|
480
944
|
path: outPath,
|
|
481
945
|
canvas: { width: canvasW, height: canvasH },
|
|
482
946
|
partCount: finalModel.parts.length,
|
|
483
|
-
atlasBytes: dataUri.length
|
|
947
|
+
atlasBytes: dataUri.length,
|
|
948
|
+
...headHalfWidth === void 0 ? {} : { headHalfWidth },
|
|
949
|
+
headHalfWidthApplied,
|
|
950
|
+
...headEdges === void 0 ? {} : { headEdges },
|
|
951
|
+
...turn === void 0 ? {} : { turn }
|
|
484
952
|
};
|
|
485
953
|
} catch (err) {
|
|
486
954
|
if (err instanceof AutoRigInputError)
|
|
@@ -491,7 +959,7 @@ async function autoRigFromLayers(input) {
|
|
|
491
959
|
|
|
492
960
|
// src/measure.ts
|
|
493
961
|
import fs2 from "fs";
|
|
494
|
-
import
|
|
962
|
+
import path4 from "path";
|
|
495
963
|
var IRIS_RATIO_MIN = 0.45;
|
|
496
964
|
var IRIS_RATIO_MAX = 0.72;
|
|
497
965
|
var EYE_ASPECT_MIN = 0.5;
|
|
@@ -592,8 +1060,8 @@ async function measureDir(absDir) {
|
|
|
592
1060
|
const layers = {};
|
|
593
1061
|
const empty = [];
|
|
594
1062
|
for (const f of files) {
|
|
595
|
-
const role =
|
|
596
|
-
const m = await layerStats(
|
|
1063
|
+
const role = path4.basename(f, ".png");
|
|
1064
|
+
const m = await layerStats(path4.join(absDir, f));
|
|
597
1065
|
if (!m) {
|
|
598
1066
|
empty.push(role);
|
|
599
1067
|
continue;
|
|
@@ -724,7 +1192,7 @@ function formatMeasureReport(result) {
|
|
|
724
1192
|
|
|
725
1193
|
// src/compose.ts
|
|
726
1194
|
import fs3 from "fs";
|
|
727
|
-
import
|
|
1195
|
+
import path5 from "path";
|
|
728
1196
|
import sharp2 from "sharp";
|
|
729
1197
|
var CANVAS = 1100;
|
|
730
1198
|
var EYE_W = 128;
|
|
@@ -849,7 +1317,7 @@ async function partBuffer(role, cfg, partsDir, inMemory) {
|
|
|
849
1317
|
if (inMemory !== void 0) {
|
|
850
1318
|
img = sharp2(inMemory);
|
|
851
1319
|
} else {
|
|
852
|
-
const srcPath =
|
|
1320
|
+
const srcPath = path5.join(partsDir, cfg.src);
|
|
853
1321
|
if (!fs3.existsSync(srcPath)) {
|
|
854
1322
|
if (cfg.optional) return null;
|
|
855
1323
|
throw new AutoRigInputError(
|
|
@@ -904,7 +1372,7 @@ function blankCanvas() {
|
|
|
904
1372
|
});
|
|
905
1373
|
}
|
|
906
1374
|
async function prepEyeSplit(partsDir) {
|
|
907
|
-
const srcPath =
|
|
1375
|
+
const srcPath = path5.join(partsDir, EYEWHITE_SRC);
|
|
908
1376
|
if (!fs3.existsSync(srcPath)) {
|
|
909
1377
|
throw new AutoRigInputError(`missing eyewhite source: ${srcPath}`);
|
|
910
1378
|
}
|
|
@@ -977,12 +1445,12 @@ async function composeLayersFromParts(input) {
|
|
|
977
1445
|
);
|
|
978
1446
|
if (part === null) {
|
|
979
1447
|
skipped.push(role);
|
|
980
|
-
fs3.rmSync(
|
|
1448
|
+
fs3.rmSync(path5.join(outDir, `${role}.png`), { force: true });
|
|
981
1449
|
continue;
|
|
982
1450
|
}
|
|
983
1451
|
const { left, top } = placement(role, cfg, part.w, part.h);
|
|
984
1452
|
const layer = await blankCanvas().composite([{ input: part.buf, left, top }]).png().toBuffer();
|
|
985
|
-
const outPath =
|
|
1453
|
+
const outPath = path5.join(outDir, `${role}.png`);
|
|
986
1454
|
writeFileAtomic(outPath, layer);
|
|
987
1455
|
layers.push({
|
|
988
1456
|
role,
|
|
@@ -1002,7 +1470,7 @@ async function composeLayersFromParts(input) {
|
|
|
1002
1470
|
background: { r: 245, g: 245, b: 248, alpha: 1 }
|
|
1003
1471
|
}
|
|
1004
1472
|
}).composite(preview).png().toBuffer();
|
|
1005
|
-
const previewPath =
|
|
1473
|
+
const previewPath = path5.join(outDir, "preview.png");
|
|
1006
1474
|
writeFileAtomic(previewPath, previewPng);
|
|
1007
1475
|
return {
|
|
1008
1476
|
ok: true,
|
|
@@ -1023,7 +1491,7 @@ async function composeLayersFromParts(input) {
|
|
|
1023
1491
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
1024
1492
|
import { z } from "zod";
|
|
1025
1493
|
function createIkiMcpServer() {
|
|
1026
|
-
const server = new McpServer({ name: "iki", version: "0.
|
|
1494
|
+
const server = new McpServer({ name: "iki", version: "0.8.0" });
|
|
1027
1495
|
server.registerTool(
|
|
1028
1496
|
"validate_iki",
|
|
1029
1497
|
{
|
|
@@ -1103,6 +1571,15 @@ function createIkiMcpServer() {
|
|
|
1103
1571
|
),
|
|
1104
1572
|
quantizeColors: z.number().int().min(2).max(256).optional().describe(
|
|
1105
1573
|
"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."
|
|
1574
|
+
),
|
|
1575
|
+
turnTargets: z.object({
|
|
1576
|
+
eyeShift: z.number().optional(),
|
|
1577
|
+
farEyeRatio: z.number().optional(),
|
|
1578
|
+
silhouetteRatio: z.number().optional(),
|
|
1579
|
+
noseShift: z.number().optional(),
|
|
1580
|
+
mouthShift: z.number().optional()
|
|
1581
|
+
}).optional().describe(
|
|
1582
|
+
"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."
|
|
1106
1583
|
)
|
|
1107
1584
|
}
|
|
1108
1585
|
},
|
|
@@ -1191,10 +1668,49 @@ function createIkiMcpServer() {
|
|
|
1191
1668
|
}
|
|
1192
1669
|
}
|
|
1193
1670
|
);
|
|
1671
|
+
server.registerTool(
|
|
1672
|
+
"measure_turn_reference",
|
|
1673
|
+
{
|
|
1674
|
+
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.",
|
|
1675
|
+
inputSchema: {
|
|
1676
|
+
front: z.string().describe("Front-facing PNG file path (resolved against cwd)."),
|
|
1677
|
+
turned: z.string().describe("The same character turned, as a PNG file path."),
|
|
1678
|
+
iris: z.object({
|
|
1679
|
+
hueMin: z.number().min(0).max(360),
|
|
1680
|
+
hueMax: z.number().min(0).max(360),
|
|
1681
|
+
satMin: z.number().min(0).max(1)
|
|
1682
|
+
}).partial().optional().describe(
|
|
1683
|
+
"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."
|
|
1684
|
+
),
|
|
1685
|
+
debugDir: z.string().optional().describe(
|
|
1686
|
+
"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."
|
|
1687
|
+
)
|
|
1688
|
+
}
|
|
1689
|
+
},
|
|
1690
|
+
async (args) => {
|
|
1691
|
+
try {
|
|
1692
|
+
const r = await measureTurnReference(args);
|
|
1693
|
+
const text = r.ok ? formatTurnReport(r) : `INVALID: ${r.error}`;
|
|
1694
|
+
return {
|
|
1695
|
+
content: [{ type: "text", text }],
|
|
1696
|
+
structuredContent: { ...r }
|
|
1697
|
+
};
|
|
1698
|
+
} catch (err) {
|
|
1699
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
1700
|
+
return {
|
|
1701
|
+
content: [{ type: "text", text: `Unexpected error: ${error}` }],
|
|
1702
|
+
structuredContent: { ok: false, error },
|
|
1703
|
+
isError: true
|
|
1704
|
+
};
|
|
1705
|
+
}
|
|
1706
|
+
}
|
|
1707
|
+
);
|
|
1194
1708
|
return server;
|
|
1195
1709
|
}
|
|
1196
1710
|
|
|
1197
1711
|
export {
|
|
1712
|
+
measureTurnReference,
|
|
1713
|
+
formatTurnReport,
|
|
1198
1714
|
validateIki,
|
|
1199
1715
|
describeIki,
|
|
1200
1716
|
listStandardParameters,
|
|
@@ -1204,4 +1720,4 @@ export {
|
|
|
1204
1720
|
composeLayersFromParts,
|
|
1205
1721
|
createIkiMcpServer
|
|
1206
1722
|
};
|
|
1207
|
-
//# sourceMappingURL=chunk-
|
|
1723
|
+
//# sourceMappingURL=chunk-JPWDKP43.mjs.map
|