@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
|
@@ -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,7 +835,19 @@ async function autoRigFromLayers(input) {
|
|
|
429
835
|
);
|
|
430
836
|
}
|
|
431
837
|
const buffer = await cropToBuffer(png.rgba, png.width, png.height, bbox);
|
|
432
|
-
|
|
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 });
|
|
850
|
+
const layer = {
|
|
433
851
|
role,
|
|
434
852
|
fileName,
|
|
435
853
|
canvasW,
|
|
@@ -437,13 +855,69 @@ async function autoRigFromLayers(input) {
|
|
|
437
855
|
bbox,
|
|
438
856
|
cropW: bbox.w,
|
|
439
857
|
cropH: bbox.h
|
|
440
|
-
}
|
|
858
|
+
};
|
|
859
|
+
if (role === "face") {
|
|
860
|
+
const rowHalfWidths = [];
|
|
861
|
+
for (let y = bbox.y; y < bbox.y + bbox.h; y++) {
|
|
862
|
+
rowHalfWidths.push(
|
|
863
|
+
rowRight[y] >= rowLeft[y] ? headHalfOf({ left: rowLeft[y], right: rowRight[y] }) : 0
|
|
864
|
+
);
|
|
865
|
+
}
|
|
866
|
+
layer.rowHalfWidths = rowHalfWidths;
|
|
867
|
+
}
|
|
868
|
+
layerInputs.push(layer);
|
|
441
869
|
crops.push({ id: role, buffer, width: bbox.w, height: bbox.h });
|
|
442
870
|
}
|
|
443
|
-
const
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
871
|
+
const eyeRow = eyeRowOf(layerInputs);
|
|
872
|
+
const span = foregroundSpan(
|
|
873
|
+
opaque,
|
|
874
|
+
canvasW,
|
|
875
|
+
canvasH,
|
|
876
|
+
eyeRow - HEAD_BAND,
|
|
877
|
+
eyeRow + HEAD_BAND
|
|
878
|
+
);
|
|
879
|
+
const headHalfWidth = span.right > span.left ? headHalfOf(span) : void 0;
|
|
880
|
+
const headHalfWidthApplied = headHalfWidth !== void 0 && headHalfWidth > facePlateHalfOf(layerInputs);
|
|
881
|
+
let headEdges;
|
|
882
|
+
if (headHalfWidthApplied) {
|
|
883
|
+
const bandLo = eyeRow - HEAD_BAND;
|
|
884
|
+
const bandHi = eyeRow + HEAD_BAND;
|
|
885
|
+
const left = [];
|
|
886
|
+
const right = [];
|
|
887
|
+
for (const { role, rowLeft, rowRight } of rowSpansByRole) {
|
|
888
|
+
const own = foregroundSpanOfRows(rowLeft, rowRight, bandLo, bandHi);
|
|
889
|
+
if (own.right <= own.left) continue;
|
|
890
|
+
left.push({ role, x: own.left - canvasW / 2 });
|
|
891
|
+
right.push({ role, x: own.right - canvasW / 2 });
|
|
892
|
+
}
|
|
893
|
+
headEdges = { left, right };
|
|
894
|
+
}
|
|
895
|
+
let turn;
|
|
896
|
+
let model;
|
|
897
|
+
try {
|
|
898
|
+
const { eyeShift, farEyeRatio, silhouetteRatio, noseShift, mouthShift } = input.turnTargets ?? {};
|
|
899
|
+
model = generateIkiFromLayerSet(
|
|
900
|
+
layerInputs,
|
|
901
|
+
{ width: canvasW, height: canvasH },
|
|
902
|
+
{
|
|
903
|
+
turnTargets: {
|
|
904
|
+
eyeShift,
|
|
905
|
+
farEyeRatio,
|
|
906
|
+
silhouetteRatio,
|
|
907
|
+
noseShift,
|
|
908
|
+
mouthShift,
|
|
909
|
+
...headHalfWidthApplied ? { headHalfWidth } : {}
|
|
910
|
+
},
|
|
911
|
+
onTurnSolved: (report) => {
|
|
912
|
+
turn = report;
|
|
913
|
+
},
|
|
914
|
+
...headEdges === void 0 ? {} : { headEdges }
|
|
915
|
+
}
|
|
916
|
+
);
|
|
917
|
+
} catch (e) {
|
|
918
|
+
if (!(e instanceof TurnTargetError)) throw e;
|
|
919
|
+
throw new AutoRigInputError(e.message);
|
|
920
|
+
}
|
|
447
921
|
const doc = new EditorDocument(model);
|
|
448
922
|
const layout = packAtlas(
|
|
449
923
|
crops.map((c) => ({ id: c.id, width: c.width, height: c.height }))
|
|
@@ -480,7 +954,11 @@ async function autoRigFromLayers(input) {
|
|
|
480
954
|
path: outPath,
|
|
481
955
|
canvas: { width: canvasW, height: canvasH },
|
|
482
956
|
partCount: finalModel.parts.length,
|
|
483
|
-
atlasBytes: dataUri.length
|
|
957
|
+
atlasBytes: dataUri.length,
|
|
958
|
+
...headHalfWidth === void 0 ? {} : { headHalfWidth },
|
|
959
|
+
headHalfWidthApplied,
|
|
960
|
+
...headEdges === void 0 ? {} : { headEdges },
|
|
961
|
+
...turn === void 0 ? {} : { turn }
|
|
484
962
|
};
|
|
485
963
|
} catch (err) {
|
|
486
964
|
if (err instanceof AutoRigInputError)
|
|
@@ -491,7 +969,7 @@ async function autoRigFromLayers(input) {
|
|
|
491
969
|
|
|
492
970
|
// src/measure.ts
|
|
493
971
|
import fs2 from "fs";
|
|
494
|
-
import
|
|
972
|
+
import path4 from "path";
|
|
495
973
|
var IRIS_RATIO_MIN = 0.45;
|
|
496
974
|
var IRIS_RATIO_MAX = 0.72;
|
|
497
975
|
var EYE_ASPECT_MIN = 0.5;
|
|
@@ -592,8 +1070,8 @@ async function measureDir(absDir) {
|
|
|
592
1070
|
const layers = {};
|
|
593
1071
|
const empty = [];
|
|
594
1072
|
for (const f of files) {
|
|
595
|
-
const role =
|
|
596
|
-
const m = await layerStats(
|
|
1073
|
+
const role = path4.basename(f, ".png");
|
|
1074
|
+
const m = await layerStats(path4.join(absDir, f));
|
|
597
1075
|
if (!m) {
|
|
598
1076
|
empty.push(role);
|
|
599
1077
|
continue;
|
|
@@ -724,7 +1202,7 @@ function formatMeasureReport(result) {
|
|
|
724
1202
|
|
|
725
1203
|
// src/compose.ts
|
|
726
1204
|
import fs3 from "fs";
|
|
727
|
-
import
|
|
1205
|
+
import path5 from "path";
|
|
728
1206
|
import sharp2 from "sharp";
|
|
729
1207
|
var CANVAS = 1100;
|
|
730
1208
|
var EYE_W = 128;
|
|
@@ -849,7 +1327,7 @@ async function partBuffer(role, cfg, partsDir, inMemory) {
|
|
|
849
1327
|
if (inMemory !== void 0) {
|
|
850
1328
|
img = sharp2(inMemory);
|
|
851
1329
|
} else {
|
|
852
|
-
const srcPath =
|
|
1330
|
+
const srcPath = path5.join(partsDir, cfg.src);
|
|
853
1331
|
if (!fs3.existsSync(srcPath)) {
|
|
854
1332
|
if (cfg.optional) return null;
|
|
855
1333
|
throw new AutoRigInputError(
|
|
@@ -904,7 +1382,7 @@ function blankCanvas() {
|
|
|
904
1382
|
});
|
|
905
1383
|
}
|
|
906
1384
|
async function prepEyeSplit(partsDir) {
|
|
907
|
-
const srcPath =
|
|
1385
|
+
const srcPath = path5.join(partsDir, EYEWHITE_SRC);
|
|
908
1386
|
if (!fs3.existsSync(srcPath)) {
|
|
909
1387
|
throw new AutoRigInputError(`missing eyewhite source: ${srcPath}`);
|
|
910
1388
|
}
|
|
@@ -977,12 +1455,12 @@ async function composeLayersFromParts(input) {
|
|
|
977
1455
|
);
|
|
978
1456
|
if (part === null) {
|
|
979
1457
|
skipped.push(role);
|
|
980
|
-
fs3.rmSync(
|
|
1458
|
+
fs3.rmSync(path5.join(outDir, `${role}.png`), { force: true });
|
|
981
1459
|
continue;
|
|
982
1460
|
}
|
|
983
1461
|
const { left, top } = placement(role, cfg, part.w, part.h);
|
|
984
1462
|
const layer = await blankCanvas().composite([{ input: part.buf, left, top }]).png().toBuffer();
|
|
985
|
-
const outPath =
|
|
1463
|
+
const outPath = path5.join(outDir, `${role}.png`);
|
|
986
1464
|
writeFileAtomic(outPath, layer);
|
|
987
1465
|
layers.push({
|
|
988
1466
|
role,
|
|
@@ -1002,7 +1480,7 @@ async function composeLayersFromParts(input) {
|
|
|
1002
1480
|
background: { r: 245, g: 245, b: 248, alpha: 1 }
|
|
1003
1481
|
}
|
|
1004
1482
|
}).composite(preview).png().toBuffer();
|
|
1005
|
-
const previewPath =
|
|
1483
|
+
const previewPath = path5.join(outDir, "preview.png");
|
|
1006
1484
|
writeFileAtomic(previewPath, previewPng);
|
|
1007
1485
|
return {
|
|
1008
1486
|
ok: true,
|
|
@@ -1023,7 +1501,7 @@ async function composeLayersFromParts(input) {
|
|
|
1023
1501
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
1024
1502
|
import { z } from "zod";
|
|
1025
1503
|
function createIkiMcpServer() {
|
|
1026
|
-
const server = new McpServer({ name: "iki", version: "0.
|
|
1504
|
+
const server = new McpServer({ name: "iki", version: "0.8.1" });
|
|
1027
1505
|
server.registerTool(
|
|
1028
1506
|
"validate_iki",
|
|
1029
1507
|
{
|
|
@@ -1103,6 +1581,15 @@ function createIkiMcpServer() {
|
|
|
1103
1581
|
),
|
|
1104
1582
|
quantizeColors: z.number().int().min(2).max(256).optional().describe(
|
|
1105
1583
|
"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."
|
|
1584
|
+
),
|
|
1585
|
+
turnTargets: z.object({
|
|
1586
|
+
eyeShift: z.number().optional(),
|
|
1587
|
+
farEyeRatio: z.number().optional(),
|
|
1588
|
+
silhouetteRatio: z.number().optional(),
|
|
1589
|
+
noseShift: z.number().optional(),
|
|
1590
|
+
mouthShift: z.number().optional()
|
|
1591
|
+
}).optional().describe(
|
|
1592
|
+
"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
1593
|
)
|
|
1107
1594
|
}
|
|
1108
1595
|
},
|
|
@@ -1191,10 +1678,49 @@ function createIkiMcpServer() {
|
|
|
1191
1678
|
}
|
|
1192
1679
|
}
|
|
1193
1680
|
);
|
|
1681
|
+
server.registerTool(
|
|
1682
|
+
"measure_turn_reference",
|
|
1683
|
+
{
|
|
1684
|
+
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.",
|
|
1685
|
+
inputSchema: {
|
|
1686
|
+
front: z.string().describe("Front-facing PNG file path (resolved against cwd)."),
|
|
1687
|
+
turned: z.string().describe("The same character turned, as a PNG file path."),
|
|
1688
|
+
iris: z.object({
|
|
1689
|
+
hueMin: z.number().min(0).max(360),
|
|
1690
|
+
hueMax: z.number().min(0).max(360),
|
|
1691
|
+
satMin: z.number().min(0).max(1)
|
|
1692
|
+
}).partial().optional().describe(
|
|
1693
|
+
"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."
|
|
1694
|
+
),
|
|
1695
|
+
debugDir: z.string().optional().describe(
|
|
1696
|
+
"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."
|
|
1697
|
+
)
|
|
1698
|
+
}
|
|
1699
|
+
},
|
|
1700
|
+
async (args) => {
|
|
1701
|
+
try {
|
|
1702
|
+
const r = await measureTurnReference(args);
|
|
1703
|
+
const text = r.ok ? formatTurnReport(r) : `INVALID: ${r.error}`;
|
|
1704
|
+
return {
|
|
1705
|
+
content: [{ type: "text", text }],
|
|
1706
|
+
structuredContent: { ...r }
|
|
1707
|
+
};
|
|
1708
|
+
} catch (err) {
|
|
1709
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
1710
|
+
return {
|
|
1711
|
+
content: [{ type: "text", text: `Unexpected error: ${error}` }],
|
|
1712
|
+
structuredContent: { ok: false, error },
|
|
1713
|
+
isError: true
|
|
1714
|
+
};
|
|
1715
|
+
}
|
|
1716
|
+
}
|
|
1717
|
+
);
|
|
1194
1718
|
return server;
|
|
1195
1719
|
}
|
|
1196
1720
|
|
|
1197
1721
|
export {
|
|
1722
|
+
measureTurnReference,
|
|
1723
|
+
formatTurnReport,
|
|
1198
1724
|
validateIki,
|
|
1199
1725
|
describeIki,
|
|
1200
1726
|
listStandardParameters,
|
|
@@ -1204,4 +1730,4 @@ export {
|
|
|
1204
1730
|
composeLayersFromParts,
|
|
1205
1731
|
createIkiMcpServer
|
|
1206
1732
|
};
|
|
1207
|
-
//# sourceMappingURL=chunk-
|
|
1733
|
+
//# sourceMappingURL=chunk-LIWK4PQV.mjs.map
|