@ikijs/mcp 0.21.0 → 0.22.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 +5 -5
- package/dist/{chunk-EKGWFKU2.mjs → chunk-SWRVRVYV.mjs} +1000 -118
- package/dist/chunk-SWRVRVYV.mjs.map +1 -0
- package/dist/cli.js +1014 -147
- package/dist/cli.js.map +1 -1
- package/dist/cli.mjs +1 -1
- package/dist/index.d.mts +59 -3
- package/dist/index.d.ts +59 -3
- package/dist/index.js +1014 -147
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +2 -2
- package/dist/chunk-EKGWFKU2.mjs.map +0 -1
package/dist/cli.js
CHANGED
|
@@ -634,17 +634,83 @@ async function autoRigFromLayers(input) {
|
|
|
634
634
|
|
|
635
635
|
// src/compose.ts
|
|
636
636
|
var import_node_fs3 = __toESM(require("fs"));
|
|
637
|
-
var
|
|
638
|
-
var
|
|
639
|
-
var
|
|
637
|
+
var import_node_path7 = __toESM(require("path"));
|
|
638
|
+
var import_sharp4 = __toESM(require("sharp"));
|
|
639
|
+
var import_editor11 = require("@ikijs/editor");
|
|
640
|
+
|
|
641
|
+
// src/trim.ts
|
|
642
|
+
var import_editor3 = require("@ikijs/editor");
|
|
643
|
+
var TRIM_THRESHOLD = 12;
|
|
644
|
+
var STRAY_SPECK_FRACTION = 0.01;
|
|
645
|
+
function luma(rgba, i) {
|
|
646
|
+
return 0.299 * rgba[i] + 0.587 * rgba[i + 1] + 0.114 * rgba[i + 2];
|
|
647
|
+
}
|
|
648
|
+
function boxWithoutStraySpecks(rgba, W, H) {
|
|
649
|
+
const n = W * H;
|
|
650
|
+
let kept = 0;
|
|
651
|
+
for (let p = 0; p < n; p++) if (rgba[p * 4 + 3] > TRIM_THRESHOLD) kept++;
|
|
652
|
+
const visited = new Uint8Array(n);
|
|
653
|
+
let stack = new Int32Array(1024);
|
|
654
|
+
const drawing = [W, H, -1, -1];
|
|
655
|
+
const strays = [W, H, -1, -1];
|
|
656
|
+
let anyOpaque = false;
|
|
657
|
+
for (let start = 0; start < n; start++) {
|
|
658
|
+
if (visited[start] || rgba[start * 4 + 3] <= TRIM_THRESHOLD) continue;
|
|
659
|
+
let setLeft = W;
|
|
660
|
+
let setTop = H;
|
|
661
|
+
let setRight = -1;
|
|
662
|
+
let setBottom = -1;
|
|
663
|
+
let size = 0;
|
|
664
|
+
let opaque = false;
|
|
665
|
+
let depth = 0;
|
|
666
|
+
visited[start] = 1;
|
|
667
|
+
stack[depth++] = start;
|
|
668
|
+
while (depth > 0) {
|
|
669
|
+
const p = stack[--depth];
|
|
670
|
+
size++;
|
|
671
|
+
if (rgba[p * 4 + 3] >= import_editor3.ALPHA_OPAQUE) opaque = true;
|
|
672
|
+
const px2 = p % W;
|
|
673
|
+
const py = (p - px2) / W;
|
|
674
|
+
setLeft = Math.min(setLeft, px2);
|
|
675
|
+
setTop = Math.min(setTop, py);
|
|
676
|
+
setRight = Math.max(setRight, px2);
|
|
677
|
+
setBottom = Math.max(setBottom, py);
|
|
678
|
+
for (let y = Math.max(0, py - 1); y <= Math.min(H - 1, py + 1); y++) {
|
|
679
|
+
for (let x = Math.max(0, px2 - 1); x <= Math.min(W - 1, px2 + 1); x++) {
|
|
680
|
+
const q = y * W + x;
|
|
681
|
+
if (!visited[q] && rgba[q * 4 + 3] > TRIM_THRESHOLD) {
|
|
682
|
+
visited[q] = 1;
|
|
683
|
+
if (depth === stack.length) {
|
|
684
|
+
const grown = new Int32Array(Math.min(n, stack.length * 2));
|
|
685
|
+
grown.set(stack);
|
|
686
|
+
stack = grown;
|
|
687
|
+
}
|
|
688
|
+
stack[depth++] = q;
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
anyOpaque ||= opaque;
|
|
694
|
+
const into = !opaque && size < kept * STRAY_SPECK_FRACTION ? strays : drawing;
|
|
695
|
+
into[0] = Math.min(into[0], setLeft);
|
|
696
|
+
into[1] = Math.min(into[1], setTop);
|
|
697
|
+
into[2] = Math.max(into[2], setRight);
|
|
698
|
+
into[3] = Math.max(into[3], setBottom);
|
|
699
|
+
}
|
|
700
|
+
if (!anyOpaque || strays[2] < 0) return null;
|
|
701
|
+
const [left, top, right, bottom] = drawing;
|
|
702
|
+
if (strays[0] >= left && strays[1] >= top && strays[2] <= right && strays[3] <= bottom)
|
|
703
|
+
return null;
|
|
704
|
+
return { left, top, width: right - left + 1, height: bottom - top + 1 };
|
|
705
|
+
}
|
|
640
706
|
|
|
641
707
|
// src/measure.ts
|
|
642
708
|
var import_node_fs2 = __toESM(require("fs"));
|
|
643
|
-
var
|
|
709
|
+
var import_node_path6 = __toESM(require("path"));
|
|
644
710
|
|
|
645
711
|
// src/measure-arms.ts
|
|
646
712
|
var import_node_path3 = __toESM(require("path"));
|
|
647
|
-
var
|
|
713
|
+
var import_editor4 = require("@ikijs/editor");
|
|
648
714
|
var ARM_ROLES = ["arm_L", "arm_R"];
|
|
649
715
|
var CAP_OVERLAP = 0.5;
|
|
650
716
|
var px = (v) => `${Number(v.toFixed(1))}`;
|
|
@@ -673,7 +739,7 @@ async function armWarnings(absDir, layers) {
|
|
|
673
739
|
continue;
|
|
674
740
|
}
|
|
675
741
|
if (set === void 0) {
|
|
676
|
-
const measurer = (0,
|
|
742
|
+
const measurer = (0, import_editor4.createLayerSetMeasurer)({
|
|
677
743
|
width: body.canvasW,
|
|
678
744
|
height: body.canvasH
|
|
679
745
|
});
|
|
@@ -681,12 +747,12 @@ async function armWarnings(absDir, layers) {
|
|
|
681
747
|
}
|
|
682
748
|
let g;
|
|
683
749
|
try {
|
|
684
|
-
g = (0,
|
|
750
|
+
g = (0, import_editor4.armGeometry)(
|
|
685
751
|
await measured(set.measurer, absDir, arm),
|
|
686
752
|
set.body.bbox.x + set.body.bbox.w / 2 - body.canvasW / 2
|
|
687
753
|
);
|
|
688
754
|
} catch (err) {
|
|
689
|
-
if (err instanceof
|
|
755
|
+
if (err instanceof import_editor4.LayerGeometryError) {
|
|
690
756
|
warnings.push(
|
|
691
757
|
`${arm}: ${err.message} \u2014 auto_rig_from_layers refuses it. Regenerate arm.png drawn hanging, shoulder at the top. Billed.`
|
|
692
758
|
);
|
|
@@ -714,7 +780,7 @@ function capWarning(arm, g, body) {
|
|
|
714
780
|
}
|
|
715
781
|
|
|
716
782
|
// src/measure-forearm-pose.ts
|
|
717
|
-
var
|
|
783
|
+
var import_editor5 = require("@ikijs/editor");
|
|
718
784
|
var POSE_ROLES = ["forearm_pose_L", "forearm_pose_R"];
|
|
719
785
|
var ARM_OF = {
|
|
720
786
|
forearm_pose_L: "arm_L",
|
|
@@ -748,7 +814,7 @@ async function forearmPoseWarnings(absDir, layers) {
|
|
|
748
814
|
continue;
|
|
749
815
|
}
|
|
750
816
|
if (layers.body === void 0) continue;
|
|
751
|
-
const measurer = (0,
|
|
817
|
+
const measurer = (0, import_editor5.createLayerSetMeasurer)({
|
|
752
818
|
width: stats.canvasW,
|
|
753
819
|
height: stats.canvasH
|
|
754
820
|
});
|
|
@@ -756,12 +822,12 @@ async function forearmPoseWarnings(absDir, layers) {
|
|
|
756
822
|
let armLayer;
|
|
757
823
|
try {
|
|
758
824
|
armLayer = await measured(measurer, absDir, arm);
|
|
759
|
-
a = (0,
|
|
825
|
+
a = (0, import_editor5.armGeometry)(armLayer, arm === "arm_R" ? Infinity : -Infinity);
|
|
760
826
|
} catch (err) {
|
|
761
|
-
if (err instanceof
|
|
827
|
+
if (err instanceof import_editor5.LayerGeometryError) continue;
|
|
762
828
|
throw err;
|
|
763
829
|
}
|
|
764
|
-
const b = (0,
|
|
830
|
+
const b = (0, import_editor5.forearmPoseGeometry)(await measured(measurer, absDir, pose));
|
|
765
831
|
warnings.push(...jointWarnings(pose, arm, stats, a, armLayer, b));
|
|
766
832
|
}
|
|
767
833
|
return warnings;
|
|
@@ -799,10 +865,721 @@ function jointWarnings(pose, arm, stats, a, armLayer, b) {
|
|
|
799
865
|
return warnings;
|
|
800
866
|
}
|
|
801
867
|
|
|
802
|
-
// src/measure-
|
|
868
|
+
// src/measure-lips.ts
|
|
803
869
|
var import_node_path4 = __toESM(require("path"));
|
|
804
|
-
var
|
|
870
|
+
var import_editor7 = require("@ikijs/editor");
|
|
871
|
+
|
|
872
|
+
// src/compose-lips.ts
|
|
873
|
+
var import_sharp2 = __toESM(require("sharp"));
|
|
805
874
|
var import_editor6 = require("@ikijs/editor");
|
|
875
|
+
var KEYED_SRC = "mouth_keyed.png";
|
|
876
|
+
var INTERIOR_SRC = "mouth_interior.png";
|
|
877
|
+
var KEY_CANDIDATE = 0.5;
|
|
878
|
+
var KEY_FILL_DIST = 20;
|
|
879
|
+
var KEY_REACH = 8;
|
|
880
|
+
var KEY_COLLINEAR = 12;
|
|
881
|
+
var KEY_MIN_SHARE = 0.05;
|
|
882
|
+
var STRAY_KEY_FRACTION = 0.02;
|
|
883
|
+
var MIN_OPENING_WIDTH = 0.3;
|
|
884
|
+
var SECOND_HOLE_FRACTION = 0.05;
|
|
885
|
+
var BAND_LUMA = 150;
|
|
886
|
+
var BAND_CAP = 3;
|
|
887
|
+
var CAVITY_LUMA = 100;
|
|
888
|
+
var SKIN_MIN_R = 225;
|
|
889
|
+
var SKIN_MIN_G = 160;
|
|
890
|
+
var SKIN_MIN_B = 135;
|
|
891
|
+
var TEETH_MIN_G = 215;
|
|
892
|
+
var TEETH_MIN_B = 205;
|
|
893
|
+
var isSkin = (r, g, b) => r > SKIN_MIN_R && g > SKIN_MIN_G && b > SKIN_MIN_B && !(g > TEETH_MIN_G && b > TEETH_MIN_B);
|
|
894
|
+
function components(mask, W, H) {
|
|
895
|
+
const n = W * H;
|
|
896
|
+
const label = new Int32Array(n).fill(-1);
|
|
897
|
+
const stack = new Int32Array(n);
|
|
898
|
+
const sizes = [];
|
|
899
|
+
const boxes = [];
|
|
900
|
+
for (let start = 0; start < n; start++) {
|
|
901
|
+
if (!mask[start] || label[start] >= 0) continue;
|
|
902
|
+
const id = sizes.length;
|
|
903
|
+
const box = { x0: W, y0: H, x1: -1, y1: -1 };
|
|
904
|
+
let size = 0;
|
|
905
|
+
let depth = 0;
|
|
906
|
+
label[start] = id;
|
|
907
|
+
stack[depth++] = start;
|
|
908
|
+
while (depth > 0) {
|
|
909
|
+
const p = stack[--depth];
|
|
910
|
+
size++;
|
|
911
|
+
const px2 = p % W;
|
|
912
|
+
const py = (p - px2) / W;
|
|
913
|
+
box.x0 = Math.min(box.x0, px2);
|
|
914
|
+
box.x1 = Math.max(box.x1, px2);
|
|
915
|
+
box.y0 = Math.min(box.y0, py);
|
|
916
|
+
box.y1 = Math.max(box.y1, py);
|
|
917
|
+
for (let y = Math.max(0, py - 1); y <= Math.min(H - 1, py + 1); y++) {
|
|
918
|
+
for (let x = Math.max(0, px2 - 1); x <= Math.min(W - 1, px2 + 1); x++) {
|
|
919
|
+
if (x !== px2 && y !== py) continue;
|
|
920
|
+
const q = y * W + x;
|
|
921
|
+
if (mask[q] && label[q] < 0) {
|
|
922
|
+
label[q] = id;
|
|
923
|
+
stack[depth++] = q;
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
}
|
|
928
|
+
sizes.push(size);
|
|
929
|
+
boxes.push(box);
|
|
930
|
+
}
|
|
931
|
+
return { label, sizes, boxes };
|
|
932
|
+
}
|
|
933
|
+
var largest = (sizes) => sizes.reduce((best, s, i) => best < 0 || s > sizes[best] ? i : best, -1);
|
|
934
|
+
function dilate(mask, W, H, reach) {
|
|
935
|
+
const rows = new Uint8Array(W * H);
|
|
936
|
+
const prefix = new Int32Array(Math.max(W, H) + 1);
|
|
937
|
+
for (let y = 0; y < H; y++) {
|
|
938
|
+
for (let x = 0; x < W; x++) prefix[x + 1] = prefix[x] + mask[y * W + x];
|
|
939
|
+
for (let x = 0; x < W; x++) {
|
|
940
|
+
rows[y * W + x] = prefix[Math.min(W, x + reach + 1)] - prefix[Math.max(0, x - reach)] > 0 ? 1 : 0;
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
const out = new Uint8Array(W * H);
|
|
944
|
+
for (let x = 0; x < W; x++) {
|
|
945
|
+
for (let y = 0; y < H; y++) prefix[y + 1] = prefix[y] + rows[y * W + x];
|
|
946
|
+
for (let y = 0; y < H; y++) {
|
|
947
|
+
out[y * W + x] = prefix[Math.min(H, y + reach + 1)] - prefix[Math.max(0, y - reach)] > 0 ? 1 : 0;
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
return out;
|
|
951
|
+
}
|
|
952
|
+
function shareOf(rgba, i, j, g) {
|
|
953
|
+
const dr = g[0] - rgba[j];
|
|
954
|
+
const dg = g[1] - rgba[j + 1];
|
|
955
|
+
const db = g[2] - rgba[j + 2];
|
|
956
|
+
const t = ((rgba[i] - rgba[j]) * dr + (rgba[i + 1] - rgba[j + 1]) * dg + (rgba[i + 2] - rgba[j + 2]) * db) / (dr * dr + dg * dg + db * db);
|
|
957
|
+
return Math.min(1, Math.max(0, t));
|
|
958
|
+
}
|
|
959
|
+
function keyGreen(rgba, W, H) {
|
|
960
|
+
const n = W * H;
|
|
961
|
+
const noGreen = new AutoRigInputError(
|
|
962
|
+
`${KEYED_SRC}: no green opening (#00FF00) to key \u2014 regenerate it with the inside of the open mouth painted ONE flat pure green`
|
|
963
|
+
);
|
|
964
|
+
const hist = [
|
|
965
|
+
new Uint32Array(256),
|
|
966
|
+
new Uint32Array(256),
|
|
967
|
+
new Uint32Array(256)
|
|
968
|
+
];
|
|
969
|
+
let candidates = 0;
|
|
970
|
+
for (let p = 0; p < n; p++) {
|
|
971
|
+
const i = p * 4;
|
|
972
|
+
if (rgba[i + 3] < import_editor6.ALPHA_OPAQUE) continue;
|
|
973
|
+
const excess = (rgba[i + 1] - Math.max(rgba[i], rgba[i + 2])) / 255;
|
|
974
|
+
if (excess < KEY_CANDIDATE) continue;
|
|
975
|
+
candidates++;
|
|
976
|
+
for (let c = 0; c < 3; c++) hist[c][rgba[i + c]]++;
|
|
977
|
+
}
|
|
978
|
+
if (candidates === 0) throw noGreen;
|
|
979
|
+
const median = (h) => {
|
|
980
|
+
let seen = 0;
|
|
981
|
+
for (let v = 0; v < 256; v++) {
|
|
982
|
+
seen += h[v];
|
|
983
|
+
if (seen * 2 >= candidates) return v;
|
|
984
|
+
}
|
|
985
|
+
return 255;
|
|
986
|
+
};
|
|
987
|
+
const G = [median(hist[0]), median(hist[1]), median(hist[2])];
|
|
988
|
+
const out = Buffer.from(rgba);
|
|
989
|
+
const fill = new Uint8Array(n);
|
|
990
|
+
const ref = new Uint8Array(n);
|
|
991
|
+
let fillCount = 0;
|
|
992
|
+
for (let p = 0; p < n; p++) {
|
|
993
|
+
const i = p * 4;
|
|
994
|
+
if (rgba[i + 3] === 0) continue;
|
|
995
|
+
const dr = rgba[i] - G[0];
|
|
996
|
+
const dg = rgba[i + 1] - G[1];
|
|
997
|
+
const db = rgba[i + 2] - G[2];
|
|
998
|
+
const green = dr * dr + dg * dg + db * db <= KEY_FILL_DIST * KEY_FILL_DIST;
|
|
999
|
+
if (rgba[i + 3] < import_editor6.ALPHA_OPAQUE) {
|
|
1000
|
+
if (green) out[i + 3] = 0;
|
|
1001
|
+
} else if (green) {
|
|
1002
|
+
fill[p] = 1;
|
|
1003
|
+
fillCount++;
|
|
1004
|
+
} else {
|
|
1005
|
+
ref[p] = 1;
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
if (fillCount === 0) throw noGreen;
|
|
1009
|
+
const reach = dilate(fill, W, H, KEY_REACH);
|
|
1010
|
+
const collinear = KEY_COLLINEAR * KEY_COLLINEAR;
|
|
1011
|
+
for (let y = 0; y < H; y++) {
|
|
1012
|
+
for (let x = 0; x < W; x++) {
|
|
1013
|
+
const p = y * W + x;
|
|
1014
|
+
if (!reach[p] || fill[p] || out[p * 4 + 3] === 0) continue;
|
|
1015
|
+
const i = p * 4;
|
|
1016
|
+
let best = i;
|
|
1017
|
+
let bestD2 = (rgba[i] - G[0]) ** 2 + (rgba[i + 1] - G[1]) ** 2 + (rgba[i + 2] - G[2]) ** 2;
|
|
1018
|
+
const y1 = Math.min(H - 1, y + KEY_REACH);
|
|
1019
|
+
const x1 = Math.min(W - 1, x + KEY_REACH);
|
|
1020
|
+
for (let qy = Math.max(0, y - KEY_REACH); qy <= y1; qy++) {
|
|
1021
|
+
for (let qx = Math.max(0, x - KEY_REACH); qx <= x1; qx++) {
|
|
1022
|
+
const q = qy * W + qx;
|
|
1023
|
+
if (q === p || !ref[q]) continue;
|
|
1024
|
+
const j = q * 4;
|
|
1025
|
+
const d2 = (rgba[j] - G[0]) ** 2 + (rgba[j + 1] - G[1]) ** 2 + (rgba[j + 2] - G[2]) ** 2;
|
|
1026
|
+
if (d2 <= bestD2) continue;
|
|
1027
|
+
const t = shareOf(rgba, i, j, G);
|
|
1028
|
+
const er = rgba[i] - rgba[j] - t * (G[0] - rgba[j]);
|
|
1029
|
+
const eg = rgba[i + 1] - rgba[j + 1] - t * (G[1] - rgba[j + 1]);
|
|
1030
|
+
const eb = rgba[i + 2] - rgba[j + 2] - t * (G[2] - rgba[j + 2]);
|
|
1031
|
+
if (er * er + eg * eg + eb * eb > collinear) continue;
|
|
1032
|
+
bestD2 = d2;
|
|
1033
|
+
best = j;
|
|
1034
|
+
}
|
|
1035
|
+
}
|
|
1036
|
+
const k = shareOf(rgba, i, best, G);
|
|
1037
|
+
if (k < KEY_MIN_SHARE) continue;
|
|
1038
|
+
rgba.copy(out, i, best, best + 3);
|
|
1039
|
+
out[i + 3] = Math.round(rgba[i + 3] * (1 - k));
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
for (let p = 0; p < n; p++) if (fill[p]) out[p * 4 + 3] = 0;
|
|
1043
|
+
const { sizes } = components(fill, W, H);
|
|
1044
|
+
const main2 = largest(sizes);
|
|
1045
|
+
const strays = sizes.filter((_, i) => i !== main2);
|
|
1046
|
+
const strayPx = strays.reduce((a, b) => a + b, 0);
|
|
1047
|
+
if (strayPx > STRAY_KEY_FRACTION * sizes[main2]) {
|
|
1048
|
+
throw new AutoRigInputError(
|
|
1049
|
+
`${KEYED_SRC}: green outside the opening (${strayPx} px in ${strays.length} regions beside the opening's) \u2014 the key must be one flat region inside the outline; regenerate with no green elsewhere`
|
|
1050
|
+
);
|
|
1051
|
+
}
|
|
1052
|
+
return out;
|
|
1053
|
+
}
|
|
1054
|
+
var GROUND_TOLERANCE = 4;
|
|
1055
|
+
var GROUND_LIGHT = 200;
|
|
1056
|
+
var GROUND_MIN = 238;
|
|
1057
|
+
var GROUND_BORDER_SHARE = 0.65;
|
|
1058
|
+
function keyBorderWhite(rgba, W, H) {
|
|
1059
|
+
const border = [[], [], []];
|
|
1060
|
+
const edge = (p) => {
|
|
1061
|
+
for (let c = 0; c < 3; c++) border[c].push(rgba[p * 4 + c]);
|
|
1062
|
+
};
|
|
1063
|
+
for (let x = 0; x < W; x++) {
|
|
1064
|
+
edge(x);
|
|
1065
|
+
edge((H - 1) * W + x);
|
|
1066
|
+
}
|
|
1067
|
+
for (let y = 1; y < H - 1; y++) {
|
|
1068
|
+
edge(y * W);
|
|
1069
|
+
edge(y * W + W - 1);
|
|
1070
|
+
}
|
|
1071
|
+
const ground = border.map((v) => v.sort((a, b) => a - b)[v.length >> 1]);
|
|
1072
|
+
const light = ground.every((v) => v >= GROUND_LIGHT);
|
|
1073
|
+
if (light && ground.some((v) => v < GROUND_MIN)) {
|
|
1074
|
+
throw new AutoRigInputError(
|
|
1075
|
+
`${INTERIOR_SRC} came back opaque on a non-white ground \u2014 regenerate it with a transparent background. Billed.`
|
|
1076
|
+
);
|
|
1077
|
+
}
|
|
1078
|
+
if (!light) ground.fill(255);
|
|
1079
|
+
const out = Buffer.from(rgba);
|
|
1080
|
+
const seen = new Uint8Array(W * H);
|
|
1081
|
+
const stack = new Int32Array(W * H);
|
|
1082
|
+
let depth = 0;
|
|
1083
|
+
const seed = (p) => {
|
|
1084
|
+
if (seen[p]) return;
|
|
1085
|
+
for (let c = 0; c < 3; c++) {
|
|
1086
|
+
if (Math.abs(rgba[p * 4 + c] - ground[c]) > GROUND_TOLERANCE) return;
|
|
1087
|
+
}
|
|
1088
|
+
seen[p] = 1;
|
|
1089
|
+
stack[depth++] = p;
|
|
1090
|
+
};
|
|
1091
|
+
for (let x = 0; x < W; x++) {
|
|
1092
|
+
seed(x);
|
|
1093
|
+
seed((H - 1) * W + x);
|
|
1094
|
+
}
|
|
1095
|
+
for (let y = 0; y < H; y++) {
|
|
1096
|
+
seed(y * W);
|
|
1097
|
+
seed(y * W + W - 1);
|
|
1098
|
+
}
|
|
1099
|
+
if (light && depth < GROUND_BORDER_SHARE * border[0].length) {
|
|
1100
|
+
throw new AutoRigInputError(
|
|
1101
|
+
`${INTERIOR_SRC} came back opaque on a white ground too noisy or shaded to key cleanly \u2014 regenerate it with a transparent background. Billed.`
|
|
1102
|
+
);
|
|
1103
|
+
}
|
|
1104
|
+
while (depth > 0) {
|
|
1105
|
+
const p = stack[--depth];
|
|
1106
|
+
out[p * 4 + 3] = 0;
|
|
1107
|
+
const x = p % W;
|
|
1108
|
+
if (x > 0) seed(p - 1);
|
|
1109
|
+
if (x < W - 1) seed(p + 1);
|
|
1110
|
+
if (p >= W) seed(p - W);
|
|
1111
|
+
if (p < W * H - W) seed(p + W);
|
|
1112
|
+
}
|
|
1113
|
+
return out;
|
|
1114
|
+
}
|
|
1115
|
+
function drawingBox(rgba, W, H) {
|
|
1116
|
+
const speckless = boxWithoutStraySpecks(rgba, W, H);
|
|
1117
|
+
if (speckless !== null) {
|
|
1118
|
+
return {
|
|
1119
|
+
x: speckless.left,
|
|
1120
|
+
y: speckless.top,
|
|
1121
|
+
w: speckless.width,
|
|
1122
|
+
h: speckless.height
|
|
1123
|
+
};
|
|
1124
|
+
}
|
|
1125
|
+
let x0 = W;
|
|
1126
|
+
let y0 = H;
|
|
1127
|
+
let x1 = -1;
|
|
1128
|
+
let y1 = -1;
|
|
1129
|
+
for (let y = 0; y < H; y++) {
|
|
1130
|
+
for (let x = 0; x < W; x++) {
|
|
1131
|
+
if (rgba[(y * W + x) * 4 + 3] <= TRIM_THRESHOLD) continue;
|
|
1132
|
+
x0 = Math.min(x0, x);
|
|
1133
|
+
x1 = Math.max(x1, x);
|
|
1134
|
+
y0 = Math.min(y0, y);
|
|
1135
|
+
y1 = Math.max(y1, y);
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
return x1 < 0 ? null : { x: x0, y: y0, w: x1 - x0 + 1, h: y1 - y0 + 1 };
|
|
1139
|
+
}
|
|
1140
|
+
async function prepInterior(rgba, W, H) {
|
|
1141
|
+
const empty = new AutoRigInputError(
|
|
1142
|
+
`${INTERIOR_SRC}: nothing but lips/skin after trimming \u2014 regenerate it with the cavity, the teeth and the tongue only, NO lips`
|
|
1143
|
+
);
|
|
1144
|
+
const first = drawingBox(rgba, W, H);
|
|
1145
|
+
if (first === null) throw empty;
|
|
1146
|
+
const cw = first.w;
|
|
1147
|
+
const ch = first.h;
|
|
1148
|
+
const crop = Buffer.alloc(cw * ch * 4);
|
|
1149
|
+
for (let y = 0; y < ch; y++) {
|
|
1150
|
+
const from2 = ((first.y + y) * W + first.x) * 4;
|
|
1151
|
+
rgba.copy(crop, y * cw * 4, from2, from2 + cw * 4);
|
|
1152
|
+
}
|
|
1153
|
+
const skin = new Uint8Array(cw * ch);
|
|
1154
|
+
for (let p = 0; p < cw * ch; p++) {
|
|
1155
|
+
const i = p * 4;
|
|
1156
|
+
if (crop[i + 3] > TRIM_THRESHOLD && isSkin(crop[i], crop[i + 1], crop[i + 2]))
|
|
1157
|
+
skin[p] = 1;
|
|
1158
|
+
}
|
|
1159
|
+
const { label, boxes } = components(skin, cw, ch);
|
|
1160
|
+
const rim = boxes.map(
|
|
1161
|
+
(b) => b.x0 === 0 || b.y0 === 0 || b.x1 === cw - 1 || b.y1 === ch - 1
|
|
1162
|
+
);
|
|
1163
|
+
for (let p = 0; p < cw * ch; p++) {
|
|
1164
|
+
if (label[p] >= 0 && rim[label[p]]) crop[p * 4 + 3] = 0;
|
|
1165
|
+
}
|
|
1166
|
+
const box = drawingBox(crop, cw, ch);
|
|
1167
|
+
if (box === null) throw empty;
|
|
1168
|
+
const sum = [0, 0, 0];
|
|
1169
|
+
const all = [0, 0, 0];
|
|
1170
|
+
let dark = 0;
|
|
1171
|
+
let count = 0;
|
|
1172
|
+
for (let y = box.y; y < box.y + box.h; y++) {
|
|
1173
|
+
for (let x = box.x; x < box.x + box.w; x++) {
|
|
1174
|
+
const i = (y * cw + x) * 4;
|
|
1175
|
+
if (crop[i + 3] < import_editor6.ALPHA_OPAQUE) continue;
|
|
1176
|
+
count++;
|
|
1177
|
+
for (let c = 0; c < 3; c++) all[c] += crop[i + c];
|
|
1178
|
+
if (luma(crop, i) < CAVITY_LUMA) {
|
|
1179
|
+
dark++;
|
|
1180
|
+
for (let c = 0; c < 3; c++) sum[c] += crop[i + c];
|
|
1181
|
+
}
|
|
1182
|
+
}
|
|
1183
|
+
}
|
|
1184
|
+
if (count === 0) throw empty;
|
|
1185
|
+
const [from, over] = dark > 0 ? [sum, dark] : [all, count];
|
|
1186
|
+
const cavity = from.map((v) => Math.round(v / over));
|
|
1187
|
+
return { png: await cropToBuffer(crop, cw, ch, box), cavity };
|
|
1188
|
+
}
|
|
1189
|
+
var rawOf = async (png) => (0, import_sharp2.default)(png).ensureAlpha().raw().toBuffer({ resolveWithObject: true });
|
|
1190
|
+
var encode = (rgba, w, h) => (0, import_sharp2.default)(rgba, { raw: { width: w, height: h, channels: 4 } }).png().toBuffer();
|
|
1191
|
+
function enclosedHole(frame, w, h) {
|
|
1192
|
+
const n = w * h;
|
|
1193
|
+
const alpha = (p) => frame[p * 4 + 3];
|
|
1194
|
+
const outside = new Uint8Array(n);
|
|
1195
|
+
const stack = new Int32Array(n);
|
|
1196
|
+
let depth = 0;
|
|
1197
|
+
const seed = (p) => {
|
|
1198
|
+
if (!outside[p] && alpha(p) < import_editor6.ALPHA_OPAQUE) {
|
|
1199
|
+
outside[p] = 1;
|
|
1200
|
+
stack[depth++] = p;
|
|
1201
|
+
}
|
|
1202
|
+
};
|
|
1203
|
+
for (let x = 0; x < w; x++) {
|
|
1204
|
+
seed(x);
|
|
1205
|
+
seed((h - 1) * w + x);
|
|
1206
|
+
}
|
|
1207
|
+
for (let y = 0; y < h; y++) {
|
|
1208
|
+
seed(y * w);
|
|
1209
|
+
seed(y * w + w - 1);
|
|
1210
|
+
}
|
|
1211
|
+
while (depth > 0) {
|
|
1212
|
+
const p = stack[--depth];
|
|
1213
|
+
const x = p % w;
|
|
1214
|
+
if (x > 0) seed(p - 1);
|
|
1215
|
+
if (x < w - 1) seed(p + 1);
|
|
1216
|
+
if (p >= w) seed(p - w);
|
|
1217
|
+
if (p < n - w) seed(p + w);
|
|
1218
|
+
}
|
|
1219
|
+
const mask = new Uint8Array(n);
|
|
1220
|
+
for (let p = 0; p < n; p++) {
|
|
1221
|
+
if (alpha(p) < import_editor6.ALPHA_OPAQUE && !outside[p]) mask[p] = 1;
|
|
1222
|
+
}
|
|
1223
|
+
const { label, sizes, boxes } = components(mask, w, h);
|
|
1224
|
+
const main2 = largest(sizes);
|
|
1225
|
+
if (main2 < 0) {
|
|
1226
|
+
throw new AutoRigInputError(
|
|
1227
|
+
`${KEYED_SRC}: the outline is not closed round the opening (the key reached the outside, or the line is broken or too thin at this size) \u2014 regenerate with ONE closed, bolder outline`
|
|
1228
|
+
);
|
|
1229
|
+
}
|
|
1230
|
+
const box = boxes[main2];
|
|
1231
|
+
const holeW = box.x1 - box.x0 + 1;
|
|
1232
|
+
if (holeW < MIN_OPENING_WIDTH * w) {
|
|
1233
|
+
throw new AutoRigInputError(
|
|
1234
|
+
`${KEYED_SRC}: the opening is only ${holeW} px wide of the mouth's ${w} \u2014 too small to fold; regenerate with the mouth open wider, or, if its outline is broken (only a small pocket of the opening was enclosed), with ONE closed, bolder outline`
|
|
1235
|
+
);
|
|
1236
|
+
}
|
|
1237
|
+
const others = sizes.filter(
|
|
1238
|
+
(s, i) => i !== main2 && s >= SECOND_HOLE_FRACTION * sizes[main2]
|
|
1239
|
+
);
|
|
1240
|
+
if (others.length > 0) {
|
|
1241
|
+
throw new AutoRigInputError(
|
|
1242
|
+
`${KEYED_SRC}: the opening is not one connected region (${others.length + 1} holes: teeth or tongue drawn into the green?) \u2014 regenerate with nothing drawn inside the green`
|
|
1243
|
+
);
|
|
1244
|
+
}
|
|
1245
|
+
return { label, main: main2, box };
|
|
1246
|
+
}
|
|
1247
|
+
function measureLips(layers, w, h) {
|
|
1248
|
+
const measurer = (0, import_editor6.createLayerSetMeasurer)({ width: w, height: h });
|
|
1249
|
+
return new Map(
|
|
1250
|
+
Object.entries(layers).map(([role, rgba]) => [
|
|
1251
|
+
role,
|
|
1252
|
+
measurer.add({ role, fileName: `${role}.png`, rgba })
|
|
1253
|
+
])
|
|
1254
|
+
);
|
|
1255
|
+
}
|
|
1256
|
+
function assertFoldContract(layers, w, h) {
|
|
1257
|
+
const byRole = /* @__PURE__ */ new Map();
|
|
1258
|
+
for (const [role, layer] of measureLips(layers, w, h)) {
|
|
1259
|
+
if (layer === null && role === "lip_lower") {
|
|
1260
|
+
throw new AutoRigInputError(
|
|
1261
|
+
`${KEYED_SRC}: no lip_lower after the split \u2014 there is no skin under the opening; draw the lower lip under the outline`
|
|
1262
|
+
);
|
|
1263
|
+
}
|
|
1264
|
+
byRole.set(role, layer);
|
|
1265
|
+
}
|
|
1266
|
+
const opening = (0, import_editor6.mouthOpening)(byRole);
|
|
1267
|
+
const runs = (0, import_editor6.columnRuns)(byRole.get("mouth_inner"));
|
|
1268
|
+
for (let col = opening.x0; col <= opening.x1; col++) {
|
|
1269
|
+
const c = opening.at(col);
|
|
1270
|
+
const why = !runs.has(col) ? "has no interior run" : c.Tu > c.T ? `has a gap between the line and the interior (Tu ${c.Tu} > T ${c.T})` : c.T > c.lineTop || c.lineH <= 0 ? `has no line above the interior (T ${c.T}, line top ${c.lineTop})` : c.Bl < c.Bb ? `has a gap between the interior and the skin (Bl ${c.Bl} < Bb ${c.Bb})` : null;
|
|
1271
|
+
if (why !== null) throw new Error(`splitLipSet: column ${col} ${why}`);
|
|
1272
|
+
}
|
|
1273
|
+
return opening;
|
|
1274
|
+
}
|
|
1275
|
+
async function splitLipSet(keyedPng, w, h, interior) {
|
|
1276
|
+
const { data: frame, info } = await rawOf(keyedPng);
|
|
1277
|
+
if (info.width !== w || info.height !== h) {
|
|
1278
|
+
throw new Error(
|
|
1279
|
+
`splitLipSet: the frame is ${info.width}x${info.height}, not ${w}x${h}`
|
|
1280
|
+
);
|
|
1281
|
+
}
|
|
1282
|
+
const n = w * h;
|
|
1283
|
+
const alpha = (p) => frame[p * 4 + 3];
|
|
1284
|
+
const { label, main: main2, box: hb } = enclosedHole(frame, w, h);
|
|
1285
|
+
const holeW = hb.x1 - hb.x0 + 1;
|
|
1286
|
+
const top = new Int32Array(w).fill(-1);
|
|
1287
|
+
const bot = new Int32Array(w).fill(-1);
|
|
1288
|
+
for (let y = 0; y < h; y++) {
|
|
1289
|
+
for (let x = hb.x0; x <= hb.x1; x++) {
|
|
1290
|
+
if (label[y * w + x] !== main2) continue;
|
|
1291
|
+
if (top[x] < 0) top[x] = y;
|
|
1292
|
+
bot[x] = y + 1;
|
|
1293
|
+
}
|
|
1294
|
+
}
|
|
1295
|
+
const centre = Math.round((hb.x0 + hb.x1) / 2);
|
|
1296
|
+
let stroke = 0;
|
|
1297
|
+
while (top[centre] - 1 - stroke >= 0 && alpha((top[centre] - 1 - stroke) * w + centre) >= import_editor6.ALPHA_OPAQUE)
|
|
1298
|
+
stroke++;
|
|
1299
|
+
const bandCap = BAND_CAP * Math.max(1, stroke);
|
|
1300
|
+
const skinRow = new Int32Array(w).fill(-1);
|
|
1301
|
+
const inM = new Uint8Array(n);
|
|
1302
|
+
for (let x = hb.x0; x <= hb.x1; x++) {
|
|
1303
|
+
let y = bot[x];
|
|
1304
|
+
while (y < h && y - bot[x] < bandCap && alpha(y * w + x) >= import_editor6.ALPHA_OPAQUE && luma(frame, (y * w + x) * 4) < BAND_LUMA)
|
|
1305
|
+
y++;
|
|
1306
|
+
for (let k = top[x]; k < y; k++) inM[k * w + x] = 1;
|
|
1307
|
+
let r = y;
|
|
1308
|
+
while (r < h && alpha(r * w + x) < import_editor6.ALPHA_OPAQUE) r++;
|
|
1309
|
+
if (r < h) {
|
|
1310
|
+
skinRow[x] = r;
|
|
1311
|
+
inM[r * w + x] = 1;
|
|
1312
|
+
}
|
|
1313
|
+
}
|
|
1314
|
+
const grown = Uint8Array.from(inM);
|
|
1315
|
+
for (let y = 0; y < h; y++) {
|
|
1316
|
+
for (let x = 0; x < w; x++) {
|
|
1317
|
+
const p = y * w + x;
|
|
1318
|
+
if (inM[p] || alpha(p) < import_editor6.ALPHA_OPAQUE) continue;
|
|
1319
|
+
if (top[x] >= 0 && y < top[x]) continue;
|
|
1320
|
+
if (x > 0 && inM[p - 1] || x < w - 1 && inM[p + 1]) grown[p] = 1;
|
|
1321
|
+
}
|
|
1322
|
+
}
|
|
1323
|
+
const wall = new Uint8Array(w);
|
|
1324
|
+
for (const x of [hb.x0 - 1, hb.x1 + 1]) {
|
|
1325
|
+
if (x < 0 || x >= w) continue;
|
|
1326
|
+
let first = 0;
|
|
1327
|
+
while (first < h && !(grown[first * w + x] && !inM[first * w + x])) first++;
|
|
1328
|
+
if (first > 0 && first < h && alpha((first - 1) * w + x) >= import_editor6.ALPHA_OPAQUE)
|
|
1329
|
+
wall[x] = 1;
|
|
1330
|
+
}
|
|
1331
|
+
const gx = hb.x0 - 1;
|
|
1332
|
+
const gy = hb.y0 - 1;
|
|
1333
|
+
const gw = holeW + 2;
|
|
1334
|
+
const gh = hb.y1 - hb.y0 + 3;
|
|
1335
|
+
const { data: fitted } = await (0, import_sharp2.default)(interior.png).resize(gw, gh, { fit: "fill" }).ensureAlpha().raw().toBuffer({ resolveWithObject: true });
|
|
1336
|
+
const faintUpper = (p, x, y) => y < bot[x] && label[p] === main2 && 2 * y < top[x] + bot[x];
|
|
1337
|
+
const upper = Buffer.alloc(n * 4);
|
|
1338
|
+
const lower = Buffer.alloc(n * 4);
|
|
1339
|
+
const inner = Buffer.alloc(n * 4);
|
|
1340
|
+
const copy = (to, p) => frame.copy(to, p * 4, p * 4, p * 4 + 4);
|
|
1341
|
+
for (let y = 0; y < h; y++) {
|
|
1342
|
+
for (let x = 0; x < w; x++) {
|
|
1343
|
+
const p = y * w + x;
|
|
1344
|
+
const inOpening = top[x] >= 0;
|
|
1345
|
+
if ((!inOpening || y < top[x] || faintUpper(p, x, y)) && !(wall[x] && grown[p]))
|
|
1346
|
+
copy(upper, p);
|
|
1347
|
+
if (inOpening && skinRow[x] >= 0 && y >= skinRow[x]) copy(lower, p);
|
|
1348
|
+
if (!grown[p]) continue;
|
|
1349
|
+
const out = [...interior.cavity];
|
|
1350
|
+
const ix = x - gx;
|
|
1351
|
+
const iy = y - gy;
|
|
1352
|
+
if (ix >= 0 && ix < gw && iy >= 0 && iy < gh) {
|
|
1353
|
+
const i = (iy * gw + ix) * 4;
|
|
1354
|
+
const a = fitted[i + 3] / 255;
|
|
1355
|
+
for (let c = 0; c < 3; c++) out[c] += (fitted[i + c] - out[c]) * a;
|
|
1356
|
+
}
|
|
1357
|
+
if ((inM[p] || wall[x]) && !faintUpper(p, x, y)) {
|
|
1358
|
+
const a = alpha(p) / 255;
|
|
1359
|
+
for (let c = 0; c < 3; c++) out[c] += (frame[p * 4 + c] - out[c]) * a;
|
|
1360
|
+
}
|
|
1361
|
+
inner[p * 4] = Math.round(out[0]);
|
|
1362
|
+
inner[p * 4 + 1] = Math.round(out[1]);
|
|
1363
|
+
inner[p * 4 + 2] = Math.round(out[2]);
|
|
1364
|
+
inner[p * 4 + 3] = 255;
|
|
1365
|
+
}
|
|
1366
|
+
}
|
|
1367
|
+
const opening = assertFoldContract(
|
|
1368
|
+
{ mouth_inner: inner, lip_lower: lower, lip_upper: upper },
|
|
1369
|
+
w,
|
|
1370
|
+
h
|
|
1371
|
+
);
|
|
1372
|
+
const [innerPng, lowerPng, upperPng] = await Promise.all([
|
|
1373
|
+
encode(inner, w, h),
|
|
1374
|
+
encode(lower, w, h),
|
|
1375
|
+
encode(upper, w, h)
|
|
1376
|
+
]);
|
|
1377
|
+
return { inner: innerPng, lower: lowerPng, upper: upperPng, opening };
|
|
1378
|
+
}
|
|
1379
|
+
async function restShiftOf(layers, width, height) {
|
|
1380
|
+
const [mouth_inner, lip_lower, lip_upper] = await Promise.all(
|
|
1381
|
+
[layers.mouth_inner, layers.lip_lower, layers.lip_upper].map(
|
|
1382
|
+
async (png) => (await rawOf(png)).data
|
|
1383
|
+
)
|
|
1384
|
+
);
|
|
1385
|
+
const byRole = /* @__PURE__ */ new Map();
|
|
1386
|
+
for (const [role, layer] of measureLips(
|
|
1387
|
+
{ mouth_inner, lip_lower, lip_upper },
|
|
1388
|
+
width,
|
|
1389
|
+
height
|
|
1390
|
+
)) {
|
|
1391
|
+
if (layer === null) {
|
|
1392
|
+
throw new AutoRigInputError(
|
|
1393
|
+
`layout.mouth_inner places the lip set so that ${role} falls off the canvas (nothing of it is left) \u2014 move it back on the canvas`
|
|
1394
|
+
);
|
|
1395
|
+
}
|
|
1396
|
+
byRole.set(role, layer);
|
|
1397
|
+
}
|
|
1398
|
+
return (0, import_editor6.mouthRestShift)(byRole);
|
|
1399
|
+
}
|
|
1400
|
+
async function closedLips(upper, lower, shift, left) {
|
|
1401
|
+
const shut = async (png, role) => {
|
|
1402
|
+
const { data, info } = await rawOf(png);
|
|
1403
|
+
const { width: w, height: h } = info;
|
|
1404
|
+
const out = Buffer.alloc(w * h * 4);
|
|
1405
|
+
for (let x = 0; x < w; x++) {
|
|
1406
|
+
const dy = shift(role, left + x);
|
|
1407
|
+
const f = Math.floor(dy);
|
|
1408
|
+
const t = dy - f;
|
|
1409
|
+
const px2 = (row, c) => row < 0 || row >= h ? 0 : data[(row * w + x) * 4 + c];
|
|
1410
|
+
for (let to = 0; to < h; to++) {
|
|
1411
|
+
const a0 = px2(to + f, 3);
|
|
1412
|
+
const a1 = px2(to + f + 1, 3);
|
|
1413
|
+
const a = (1 - t) * a0 + t * a1;
|
|
1414
|
+
if (a === 0) continue;
|
|
1415
|
+
const o = (to * w + x) * 4;
|
|
1416
|
+
for (let c = 0; c < 3; c++)
|
|
1417
|
+
out[o + c] = Math.round(
|
|
1418
|
+
((1 - t) * a0 * px2(to + f, c) + t * a1 * px2(to + f + 1, c)) / a
|
|
1419
|
+
);
|
|
1420
|
+
out[o + 3] = Math.round(a);
|
|
1421
|
+
}
|
|
1422
|
+
}
|
|
1423
|
+
return encode(out, w, h);
|
|
1424
|
+
};
|
|
1425
|
+
const [u, l] = await Promise.all([
|
|
1426
|
+
shut(upper, "lip_upper"),
|
|
1427
|
+
shut(lower, "lip_lower")
|
|
1428
|
+
]);
|
|
1429
|
+
return { upper: u, lower: l };
|
|
1430
|
+
}
|
|
1431
|
+
|
|
1432
|
+
// src/measure-lips.ts
|
|
1433
|
+
var isLipRole = (role) => import_editor7.LIP_ROLES.includes(role);
|
|
1434
|
+
var MIN_OPENING_ROWS = 8;
|
|
1435
|
+
var MIN_LINE_ROWS = 2;
|
|
1436
|
+
var GREEN_EXCESS = 40;
|
|
1437
|
+
var INTERIOR_GAP_FRACTION = 0.05;
|
|
1438
|
+
var columns = (cols) => `${cols.length} column${cols.length === 1 ? "" : "s"} (first at x=${cols[0]})`;
|
|
1439
|
+
async function lipWarnings(absDir, layers) {
|
|
1440
|
+
const warnings = [];
|
|
1441
|
+
const present = import_editor7.LIP_ROLES.filter((role) => layers[role] !== void 0);
|
|
1442
|
+
if (present.length === 0) return { warnings };
|
|
1443
|
+
const legacy = ["mouth", "mouth_open"].filter(
|
|
1444
|
+
(role) => layers[role] !== void 0
|
|
1445
|
+
);
|
|
1446
|
+
if (legacy.length > 0) {
|
|
1447
|
+
warnings.push(
|
|
1448
|
+
`${present.join("/")}: the lip set cannot be mixed with ${legacy.join("/")} \u2014 auto_rig_from_layers refuses it. Remove one route: compose again with only mouth_keyed.png + mouth_interior.png, or without them.`
|
|
1449
|
+
);
|
|
1450
|
+
}
|
|
1451
|
+
if (present.length < import_editor7.LIP_ROLES.length) {
|
|
1452
|
+
const missing = import_editor7.LIP_ROLES.filter((role) => layers[role] === void 0);
|
|
1453
|
+
warnings.push(
|
|
1454
|
+
`${present.join("/")}: partial lip set, missing ${missing.join("/")} \u2014 auto_rig_from_layers refuses it. Compose mouth_keyed.png + mouth_interior.png together, which cuts all three.`
|
|
1455
|
+
);
|
|
1456
|
+
return { warnings };
|
|
1457
|
+
}
|
|
1458
|
+
const { mouth_inner: inner, lip_upper: upper } = layers;
|
|
1459
|
+
for (const role of ["lip_lower", "lip_upper"]) {
|
|
1460
|
+
const stats = layers[role];
|
|
1461
|
+
if (stats.canvasW !== inner.canvasW || stats.canvasH !== inner.canvasH) {
|
|
1462
|
+
warnings.push(
|
|
1463
|
+
`${role}: its canvas ${stats.canvasW}x${stats.canvasH} differs from mouth_inner's ${inner.canvasW}x${inner.canvasH} \u2014 auto_rig_from_layers refuses layers of different sizes; recompose them together.`
|
|
1464
|
+
);
|
|
1465
|
+
return { warnings };
|
|
1466
|
+
}
|
|
1467
|
+
}
|
|
1468
|
+
const measurer = (0, import_editor7.createLayerSetMeasurer)({
|
|
1469
|
+
width: inner.canvasW,
|
|
1470
|
+
height: inner.canvasH
|
|
1471
|
+
});
|
|
1472
|
+
const set = /* @__PURE__ */ new Map();
|
|
1473
|
+
const green = [];
|
|
1474
|
+
for (const role of import_editor7.LIP_ROLES) {
|
|
1475
|
+
const fileName = `${role}.png`;
|
|
1476
|
+
const { rgba } = await decodePng(import_node_path4.default.join(absDir, fileName));
|
|
1477
|
+
set.set(role, measurer.add({ role, fileName, rgba }));
|
|
1478
|
+
for (let i = 0; i < rgba.length; i += 4) {
|
|
1479
|
+
if (rgba[i + 3] >= import_editor7.ALPHA_OPAQUE && rgba[i + 1] - Math.max(rgba[i], rgba[i + 2]) > GREEN_EXCESS) {
|
|
1480
|
+
green.push(role);
|
|
1481
|
+
break;
|
|
1482
|
+
}
|
|
1483
|
+
}
|
|
1484
|
+
}
|
|
1485
|
+
let opening;
|
|
1486
|
+
try {
|
|
1487
|
+
opening = (0, import_editor7.mouthOpening)(set);
|
|
1488
|
+
} catch (err) {
|
|
1489
|
+
if (err instanceof import_editor7.LayerGeometryError) {
|
|
1490
|
+
warnings.push(err.message);
|
|
1491
|
+
return { warnings };
|
|
1492
|
+
}
|
|
1493
|
+
throw err;
|
|
1494
|
+
}
|
|
1495
|
+
const innerRuns = (0, import_editor7.columnRuns)(set.get("mouth_inner"));
|
|
1496
|
+
const noInterior = [];
|
|
1497
|
+
const gap = [];
|
|
1498
|
+
const noLine = [];
|
|
1499
|
+
const skinGap = [];
|
|
1500
|
+
let area = 0;
|
|
1501
|
+
let hollow = 0;
|
|
1502
|
+
for (let x = opening.x0; x <= opening.x1; x++) {
|
|
1503
|
+
const runs = innerRuns.get(x);
|
|
1504
|
+
if (runs === void 0) {
|
|
1505
|
+
noInterior.push(x);
|
|
1506
|
+
continue;
|
|
1507
|
+
}
|
|
1508
|
+
const span = runs[runs.length - 1][1] - runs[0][0];
|
|
1509
|
+
area += span;
|
|
1510
|
+
hollow += span - runs.reduce((n, r) => n + r[1] - r[0], 0);
|
|
1511
|
+
const c = opening.at(x);
|
|
1512
|
+
if (c.lineH <= 0) noLine.push(x);
|
|
1513
|
+
else if (c.Tu > c.T) gap.push(x);
|
|
1514
|
+
if (c.Bl < c.Bb) skinGap.push(x);
|
|
1515
|
+
}
|
|
1516
|
+
const recompose = (file, ask) => `Recompose from mouth_keyed.png + mouth_interior.png (free); if it persists, regenerate ${file} ${ask}. Billed.`;
|
|
1517
|
+
const regenKeyed = recompose(
|
|
1518
|
+
"mouth_keyed.png",
|
|
1519
|
+
"with a closed outline and solid lips under it"
|
|
1520
|
+
);
|
|
1521
|
+
if (noInterior.length > 0) {
|
|
1522
|
+
warnings.push(
|
|
1523
|
+
`mouth_inner: no interior in ${columns(noInterior)} of the opening \u2014 the fold has nothing to scale there. ${recompose("mouth_interior.png", "as one solid cavity")}`
|
|
1524
|
+
);
|
|
1525
|
+
}
|
|
1526
|
+
if (gap.length > 0) {
|
|
1527
|
+
warnings.push(
|
|
1528
|
+
`lip_upper: a gap between the line and the interior in ${columns(gap)} \u2014 the face shows through it at rest (the interior's top must reach the line's ink). ${regenKeyed}`
|
|
1529
|
+
);
|
|
1530
|
+
}
|
|
1531
|
+
if (noLine.length > 0) {
|
|
1532
|
+
warnings.push(
|
|
1533
|
+
`lip_upper: no line above the interior in ${columns(noLine)} \u2014 nothing closes the mouth there. ${regenKeyed}`
|
|
1534
|
+
);
|
|
1535
|
+
}
|
|
1536
|
+
if (skinGap.length > 0) {
|
|
1537
|
+
warnings.push(
|
|
1538
|
+
`lip_lower: a gap between the interior and the skin in ${columns(skinGap)} \u2014 the face shows through it once the mouth opens (the skin must reach the interior's bottom). ${regenKeyed}`
|
|
1539
|
+
);
|
|
1540
|
+
}
|
|
1541
|
+
const width = opening.x1 - opening.x0 + 1;
|
|
1542
|
+
const centre = opening.at(opening.centre);
|
|
1543
|
+
if (width < MIN_OPENING_WIDTH * upper.w) {
|
|
1544
|
+
warnings.push(
|
|
1545
|
+
`mouth_inner: the opening is ${width} px wide, ${Math.round(100 * width / upper.w)}% of lip_upper's ${upper.w} px (under ${Math.round(100 * MIN_OPENING_WIDTH)}%) \u2014 too narrow to fold; the slit stays a hairline. Regenerate mouth_keyed.png with the mouth open wider. Billed.`
|
|
1546
|
+
);
|
|
1547
|
+
}
|
|
1548
|
+
if (centre.H < MIN_OPENING_ROWS) {
|
|
1549
|
+
const w = Math.ceil(upper.w * MIN_OPENING_ROWS / centre.H);
|
|
1550
|
+
const retune = w <= inner.canvasW ? w : null;
|
|
1551
|
+
warnings.push(
|
|
1552
|
+
`mouth_inner: the opening is ${centre.H} px tall at its centre (under ${MIN_OPENING_ROWS}) \u2014 too small to fold; the slit stays shut until MouthOpen is nearly 1. ` + (retune === null ? "" : `If the mouth is smaller than the reference's, set layout.mouth_inner.w to ${retune} (free; the whole set scales with it, so scale layout.mouth_inner.h too if it is set). Otherwise `) + `${retune === null ? "R" : "r"}egenerate mouth_keyed.png with the mouth open taller. Billed.`
|
|
1553
|
+
);
|
|
1554
|
+
}
|
|
1555
|
+
if (opening.w < MIN_LINE_ROWS) {
|
|
1556
|
+
warnings.push(
|
|
1557
|
+
`lip_upper: the upper line is ${opening.w} px at the opening's centre (under ${MIN_LINE_ROWS}) \u2014 a hairline closes to nothing. Regenerate mouth_keyed.png with a bolder upper line, never thinner than the reference's mouth line. Billed.`
|
|
1558
|
+
);
|
|
1559
|
+
}
|
|
1560
|
+
if (green.length > 0) {
|
|
1561
|
+
warnings.push(
|
|
1562
|
+
`${green.join("/")}: opaque green left (key green, g exceeds r and b by over ${GREEN_EXCESS}) \u2014 it shows as a green fringe on the lips. Regenerate mouth_keyed.png with the green only inside the opening, or mouth_interior.png without green in it. Billed.`
|
|
1563
|
+
);
|
|
1564
|
+
}
|
|
1565
|
+
if (area > 0 && hollow > INTERIOR_GAP_FRACTION * area) {
|
|
1566
|
+
warnings.push(
|
|
1567
|
+
`mouth_inner: ${Math.round(100 * hollow / area)}% of the opening is transparent inside the interior's own span (over ${Math.round(100 * INTERIOR_GAP_FRACTION)}%) \u2014 the face shows through it. Regenerate mouth_interior.png as one solid cavity. Billed.`
|
|
1568
|
+
);
|
|
1569
|
+
}
|
|
1570
|
+
return {
|
|
1571
|
+
warnings,
|
|
1572
|
+
lips: {
|
|
1573
|
+
opening: { width, height: centre.H, line: opening.w },
|
|
1574
|
+
deadZone: centre.overlap / (centre.H + centre.overlap)
|
|
1575
|
+
}
|
|
1576
|
+
};
|
|
1577
|
+
}
|
|
1578
|
+
|
|
1579
|
+
// src/measure-turn.ts
|
|
1580
|
+
var import_node_path5 = __toESM(require("path"));
|
|
1581
|
+
var import_editor8 = require("@ikijs/editor");
|
|
1582
|
+
var import_editor9 = require("@ikijs/editor");
|
|
806
1583
|
var DEFAULT_IRIS = { hueMin: 230, hueMax: 300, satMin: 0.22 };
|
|
807
1584
|
var IRIS_V_MIN = 0.18;
|
|
808
1585
|
var IRIS_V_MAX = 0.95;
|
|
@@ -839,7 +1616,7 @@ function rgbToHsv(r, g, b) {
|
|
|
839
1616
|
}
|
|
840
1617
|
return [h, max === 0 ? 0 : d / max, max];
|
|
841
1618
|
}
|
|
842
|
-
function
|
|
1619
|
+
function dilate2(mask, width, height, r) {
|
|
843
1620
|
const out = new Uint8Array(width * height);
|
|
844
1621
|
for (let y = 0; y < height; y++) {
|
|
845
1622
|
for (let x = 0; x < width; x++) {
|
|
@@ -878,9 +1655,9 @@ function erode(mask, width, height, r) {
|
|
|
878
1655
|
return out;
|
|
879
1656
|
}
|
|
880
1657
|
function closeMask(mask, width, height, r) {
|
|
881
|
-
return erode(
|
|
1658
|
+
return erode(dilate2(mask, width, height, r), width, height, r);
|
|
882
1659
|
}
|
|
883
|
-
function
|
|
1660
|
+
function components2(mask, width, height, minArea) {
|
|
884
1661
|
const seen = new Uint8Array(width * height);
|
|
885
1662
|
const out = [];
|
|
886
1663
|
for (let i = 0; i < width * height; i++) {
|
|
@@ -952,7 +1729,7 @@ async function measureTurnImage(filePath, iris) {
|
|
|
952
1729
|
let transparent = false;
|
|
953
1730
|
if (hasAlpha) {
|
|
954
1731
|
for (let i = 0; i < n; i++) {
|
|
955
|
-
if (rgba[i * 4 + 3] <
|
|
1732
|
+
if (rgba[i * 4 + 3] < import_editor8.ALPHA_OPAQUE) {
|
|
956
1733
|
transparent = true;
|
|
957
1734
|
break;
|
|
958
1735
|
}
|
|
@@ -963,7 +1740,7 @@ async function measureTurnImage(filePath, iris) {
|
|
|
963
1740
|
const fg = new Uint8Array(n);
|
|
964
1741
|
const value = new Float32Array(n);
|
|
965
1742
|
for (let i = 0; i < n; i++) {
|
|
966
|
-
if (rgba[i * 4 + 3] <
|
|
1743
|
+
if (rgba[i * 4 + 3] < import_editor8.ALPHA_OPAQUE) continue;
|
|
967
1744
|
const [hue, s, v] = rgbToHsv(rgba[i * 4], rgba[i * 4 + 1], rgba[i * 4 + 2]);
|
|
968
1745
|
if (maskMode === "alpha") {
|
|
969
1746
|
fg[i] = 1;
|
|
@@ -999,7 +1776,7 @@ async function measureTurnImage(filePath, iris) {
|
|
|
999
1776
|
}
|
|
1000
1777
|
return count ? total / count : 0;
|
|
1001
1778
|
};
|
|
1002
|
-
const candidates =
|
|
1779
|
+
const candidates = components2(
|
|
1003
1780
|
closeMask(irisMask, width, height, closeRadius),
|
|
1004
1781
|
width,
|
|
1005
1782
|
height,
|
|
@@ -1029,12 +1806,12 @@ async function measureTurnImage(filePath, iris) {
|
|
|
1029
1806
|
}
|
|
1030
1807
|
const [irisL, irisR] = best.pair;
|
|
1031
1808
|
const eyeRow = Math.round((irisL.cy + irisR.cy) / 2);
|
|
1032
|
-
const { left, right } = (0,
|
|
1809
|
+
const { left, right } = (0, import_editor8.foregroundSpan)(
|
|
1033
1810
|
fg,
|
|
1034
1811
|
width,
|
|
1035
1812
|
height,
|
|
1036
|
-
eyeRow -
|
|
1037
|
-
eyeRow +
|
|
1813
|
+
eyeRow - import_editor8.HEAD_BAND,
|
|
1814
|
+
eyeRow + import_editor8.HEAD_BAND
|
|
1038
1815
|
);
|
|
1039
1816
|
if (right <= left) {
|
|
1040
1817
|
throw new AutoRigInputError(
|
|
@@ -1058,7 +1835,7 @@ async function measureTurnImage(filePath, iris) {
|
|
|
1058
1835
|
left,
|
|
1059
1836
|
right,
|
|
1060
1837
|
cx: (left + right) / 2,
|
|
1061
|
-
half: (0,
|
|
1838
|
+
half: (0, import_editor8.headHalfOf)({ left, right })
|
|
1062
1839
|
},
|
|
1063
1840
|
pairCx: (irisL.cx + irisR.cx) / 2,
|
|
1064
1841
|
eyeGap: irisR.cx - irisL.cx
|
|
@@ -1073,9 +1850,9 @@ function overlaySvg(m) {
|
|
|
1073
1850
|
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>";
|
|
1074
1851
|
}
|
|
1075
1852
|
async function writeOverlay(debugDir, label, m) {
|
|
1076
|
-
const outPath =
|
|
1853
|
+
const outPath = import_node_path5.default.join(
|
|
1077
1854
|
debugDir,
|
|
1078
|
-
`${label}-${
|
|
1855
|
+
`${label}-${import_node_path5.default.basename(m.file, ".png")}.debug.png`
|
|
1079
1856
|
);
|
|
1080
1857
|
writeFileAtomic(outPath, await encodeOverlayPng(m.file, overlaySvg(m)));
|
|
1081
1858
|
return outPath;
|
|
@@ -1249,7 +2026,7 @@ function noseSpeckWarning({ core, part }, source) {
|
|
|
1249
2026
|
`the layer's dense core is ${core.w}x${core.h} in its ${part.w}x${part.h} crop`,
|
|
1250
2027
|
`auto_rig_from_layers turns and tilts the nose about its whole crop instead`
|
|
1251
2028
|
];
|
|
1252
|
-
return `nose: ${found} \u2014 under ${
|
|
2029
|
+
return `nose: ${found} \u2014 under ${import_editor9.SPECK_CORE_FRACTION} of its width or height, a speck: a lone nostril mark or highlight at alpha >= 128, not the drawing, so ${consequence}. Regenerate the nose with its shading painted denser, so the drawing itself reaches alpha 128. Billed.`;
|
|
1253
2030
|
}
|
|
1254
2031
|
async function lashOffSclera(eyePath, lashPath) {
|
|
1255
2032
|
const eye = await decodePng(eyePath);
|
|
@@ -1279,8 +2056,8 @@ async function measureDir(absDir, noseSpeck) {
|
|
|
1279
2056
|
const layers = {};
|
|
1280
2057
|
const empty = [];
|
|
1281
2058
|
for (const f of files) {
|
|
1282
|
-
const role =
|
|
1283
|
-
const m = await layerStats(
|
|
2059
|
+
const role = import_node_path6.default.basename(f, ".png");
|
|
2060
|
+
const m = await layerStats(import_node_path6.default.join(absDir, f));
|
|
1284
2061
|
if (!m) {
|
|
1285
2062
|
empty.push(role);
|
|
1286
2063
|
continue;
|
|
@@ -1289,6 +2066,7 @@ async function measureDir(absDir, noseSpeck) {
|
|
|
1289
2066
|
}
|
|
1290
2067
|
const warnings = [];
|
|
1291
2068
|
for (const [role, m] of Object.entries(layers)) {
|
|
2069
|
+
if (isLipRole(role)) continue;
|
|
1292
2070
|
const edges = [
|
|
1293
2071
|
["top", m.edgeTop, m.w, m.marginTop],
|
|
1294
2072
|
["left", m.edgeLeft, m.h, m.marginLeft],
|
|
@@ -1318,7 +2096,7 @@ async function measureDir(absDir, noseSpeck) {
|
|
|
1318
2096
|
}
|
|
1319
2097
|
}
|
|
1320
2098
|
for (const [role, m] of Object.entries(layers)) {
|
|
1321
|
-
if (role === "body") continue;
|
|
2099
|
+
if (role === "body" || isLipRole(role)) continue;
|
|
1322
2100
|
if (m.flatCutRun > FLAT_CUT_MAX_FRAC * m.w && m.flatCutRun >= FLAT_CUT_MIN_PX) {
|
|
1323
2101
|
warnings.push(
|
|
1324
2102
|
`${role}: ${m.flatCutRun}px of straight flat edge at y=${m.flatCutY} (${pct(m.flatCutRun / m.w)} of its width) \u2014 the source art is cut through there. ` + (isArm(role) ? `It hides at rest and opens into a seam once the arm raises. ` : `It hides while the head faces front and opens into a seam on turn. `) + `Regenerate this part with the whole subject inside the frame.`
|
|
@@ -1327,6 +2105,8 @@ async function measureDir(absDir, noseSpeck) {
|
|
|
1327
2105
|
}
|
|
1328
2106
|
warnings.push(...await armWarnings(absDir, layers));
|
|
1329
2107
|
warnings.push(...await forearmPoseWarnings(absDir, layers));
|
|
2108
|
+
const lip = await lipWarnings(absDir, layers);
|
|
2109
|
+
warnings.push(...lip.warnings);
|
|
1330
2110
|
for (const side of ["L", "R"]) {
|
|
1331
2111
|
const eye = layers[`eye_${side}`];
|
|
1332
2112
|
const iris = layers[`iris_${side}`];
|
|
@@ -1355,8 +2135,8 @@ async function measureDir(absDir, noseSpeck) {
|
|
|
1355
2135
|
}
|
|
1356
2136
|
if (lash) {
|
|
1357
2137
|
const { opaque, off } = await lashOffSclera(
|
|
1358
|
-
|
|
1359
|
-
|
|
2138
|
+
import_node_path6.default.join(absDir, `eye_${side}.png`),
|
|
2139
|
+
import_node_path6.default.join(absDir, `lash_${side}.png`)
|
|
1360
2140
|
);
|
|
1361
2141
|
const dTop = lash.marginTop - eye.marginTop;
|
|
1362
2142
|
const faults = [
|
|
@@ -1382,11 +2162,11 @@ async function measureDir(absDir, noseSpeck) {
|
|
|
1382
2162
|
if (noseSpeck !== void 0) {
|
|
1383
2163
|
warnings.push(noseSpeckWarning(noseSpeck, "compose"));
|
|
1384
2164
|
} else if (layers.nose !== void 0) {
|
|
1385
|
-
const nose = await decodePng(
|
|
1386
|
-
const core = (0,
|
|
2165
|
+
const nose = await decodePng(import_node_path6.default.join(absDir, "nose.png"));
|
|
2166
|
+
const core = (0, import_editor9.denseCoreOf)(nose.rgba, nose.width, nose.height);
|
|
1387
2167
|
if (core !== null) {
|
|
1388
2168
|
const crop = detectAlphaBbox(nose.rgba, nose.width, nose.height);
|
|
1389
|
-
if ((0,
|
|
2169
|
+
if ((0, import_editor9.isSpeckCore)(core, crop))
|
|
1390
2170
|
warnings.push(noseSpeckWarning({ core, part: crop }, "layer"));
|
|
1391
2171
|
}
|
|
1392
2172
|
}
|
|
@@ -1400,7 +2180,12 @@ async function measureDir(absDir, noseSpeck) {
|
|
|
1400
2180
|
]) {
|
|
1401
2181
|
if (layers[role] === void 0) warnings.push(`${role}: missing \u2014 ${why}.`);
|
|
1402
2182
|
}
|
|
1403
|
-
return {
|
|
2183
|
+
return {
|
|
2184
|
+
layers,
|
|
2185
|
+
empty,
|
|
2186
|
+
warnings,
|
|
2187
|
+
...lip.lips === void 0 ? {} : { lips: lip.lips }
|
|
2188
|
+
};
|
|
1404
2189
|
}
|
|
1405
2190
|
async function measureLayers(input) {
|
|
1406
2191
|
try {
|
|
@@ -1434,6 +2219,13 @@ function formatMeasureReport(result) {
|
|
|
1434
2219
|
`${role.padEnd(13)} ${`${m.w}x${m.h}`.padEnd(11)} ${`${m.bboxCx.toFixed(0)},${m.bboxCy.toFixed(0)}`.padEnd(14)} ${`${m.massCx.toFixed(0)},${m.massCy.toFixed(0)}`.padEnd(13)} ${m.marginTop}/${m.marginBottom}/${m.marginLeft}/${m.marginRight}`
|
|
1435
2220
|
);
|
|
1436
2221
|
}
|
|
2222
|
+
if (result.lips !== void 0) {
|
|
2223
|
+
const { opening, deadZone } = result.lips;
|
|
2224
|
+
lines.push(
|
|
2225
|
+
"",
|
|
2226
|
+
`lips: opening ${opening.width} px wide, ${opening.height} px tall at its centre under a ${opening.line} px line \u2014 no slit below MouthOpen ${deadZone.toFixed(2)}`
|
|
2227
|
+
);
|
|
2228
|
+
}
|
|
1437
2229
|
lines.push("", "# checks", "");
|
|
1438
2230
|
if (result.warnings.length === 0) {
|
|
1439
2231
|
lines.push("all geometry checks passed");
|
|
@@ -1444,13 +2236,13 @@ function formatMeasureReport(result) {
|
|
|
1444
2236
|
}
|
|
1445
2237
|
|
|
1446
2238
|
// src/compose-arms.ts
|
|
1447
|
-
var
|
|
1448
|
-
var
|
|
2239
|
+
var import_sharp3 = __toESM(require("sharp"));
|
|
2240
|
+
var import_editor10 = require("@ikijs/editor");
|
|
1449
2241
|
var ARM_WIDTH = 0.4;
|
|
1450
2242
|
var SHOULDER_DROP = 0.12;
|
|
1451
2243
|
async function armPlacement(role, cfg, part, body) {
|
|
1452
|
-
const { data } = await (0,
|
|
1453
|
-
const layer = (0,
|
|
2244
|
+
const { data } = await (0, import_sharp3.default)(part.buf).ensureAlpha().raw().toBuffer({ resolveWithObject: true });
|
|
2245
|
+
const layer = (0, import_editor10.createLayerSetMeasurer)({ width: part.w, height: part.h }).add({
|
|
1454
2246
|
role,
|
|
1455
2247
|
fileName: "arm.png",
|
|
1456
2248
|
rgba: data
|
|
@@ -1462,9 +2254,9 @@ async function armPlacement(role, cfg, part, body) {
|
|
|
1462
2254
|
}
|
|
1463
2255
|
let g;
|
|
1464
2256
|
try {
|
|
1465
|
-
g = (0,
|
|
2257
|
+
g = (0, import_editor10.armGeometry)(layer, role === "arm_R" ? Infinity : -Infinity);
|
|
1466
2258
|
} catch (err) {
|
|
1467
|
-
if (err instanceof
|
|
2259
|
+
if (err instanceof import_editor10.LayerGeometryError) {
|
|
1468
2260
|
throw new AutoRigInputError(`layout.${role}: ${err.message}`);
|
|
1469
2261
|
}
|
|
1470
2262
|
throw err;
|
|
@@ -1490,8 +2282,8 @@ async function armPlacement(role, cfg, part, body) {
|
|
|
1490
2282
|
};
|
|
1491
2283
|
}
|
|
1492
2284
|
async function poseGeometryOf(role, png, size, when) {
|
|
1493
|
-
const { data } = await (0,
|
|
1494
|
-
const layer = (0,
|
|
2285
|
+
const { data } = await (0, import_sharp3.default)(png).ensureAlpha().raw().toBuffer({ resolveWithObject: true });
|
|
2286
|
+
const layer = (0, import_editor10.createLayerSetMeasurer)({
|
|
1495
2287
|
width: size.w,
|
|
1496
2288
|
height: size.h
|
|
1497
2289
|
}).add({ role, fileName: "forearm_pose.png", rgba: data });
|
|
@@ -1500,7 +2292,7 @@ async function poseGeometryOf(role, png, size, when) {
|
|
|
1500
2292
|
`layout.${role}: forearm_pose.png is empty after alpha threshold${when}`
|
|
1501
2293
|
);
|
|
1502
2294
|
}
|
|
1503
|
-
return (0,
|
|
2295
|
+
return (0, import_editor10.forearmPoseGeometry)(layer);
|
|
1504
2296
|
}
|
|
1505
2297
|
async function forearmPoseWidth(role, trimmed, elbowRun) {
|
|
1506
2298
|
const g = await poseGeometryOf(role, trimmed.data, trimmed, "");
|
|
@@ -1530,8 +2322,6 @@ var IRIS_W = 50;
|
|
|
1530
2322
|
var EYEWHITE_SRC = "eyewhite.png";
|
|
1531
2323
|
var EYE_LASH_LUMA = 120;
|
|
1532
2324
|
var LASH_KEEP_FRACTION = 0.5;
|
|
1533
|
-
var TRIM_THRESHOLD = 12;
|
|
1534
|
-
var STRAY_SPECK_FRACTION = 0.01;
|
|
1535
2325
|
var NOSE_TIP_AT = 0.66;
|
|
1536
2326
|
var DEFAULT_LAYOUT = {
|
|
1537
2327
|
// Back hair and body sit behind the face. Both are OPTIONAL: a parts dir
|
|
@@ -1582,6 +2372,18 @@ var DEFAULT_LAYOUT = {
|
|
|
1582
2372
|
w: 76,
|
|
1583
2373
|
optional: true
|
|
1584
2374
|
},
|
|
2375
|
+
// The lip set's ONE frame, mouth_open's place: the keyed mouth is split into
|
|
2376
|
+
// mouth_inner / lip_lower / lip_upper and all three land here, so a retune
|
|
2377
|
+
// moves them together. lip_lower and lip_upper have no key of their own
|
|
2378
|
+
// (LIP_FRAME). OPTIONAL and opt-in: with mouth_keyed.png + mouth_interior.png
|
|
2379
|
+
// it stands in for mouth and mouth_open.
|
|
2380
|
+
mouth_inner: {
|
|
2381
|
+
src: KEYED_SRC,
|
|
2382
|
+
cx: 550,
|
|
2383
|
+
cy: 596,
|
|
2384
|
+
w: 76,
|
|
2385
|
+
optional: true
|
|
2386
|
+
},
|
|
1585
2387
|
// eye_* (sclera) and lash_* share the eyewhite's cropped frame via noTrim (so
|
|
1586
2388
|
// they are NOT re-bboxed independently): the upper lash stays anchored ABOVE
|
|
1587
2389
|
// the sclera center, so on blink it folds DOWN over the eye like the sample
|
|
@@ -1628,6 +2430,9 @@ var ORDER = [
|
|
|
1628
2430
|
"nose",
|
|
1629
2431
|
"mouth",
|
|
1630
2432
|
"mouth_open",
|
|
2433
|
+
"mouth_inner",
|
|
2434
|
+
"lip_lower",
|
|
2435
|
+
"lip_upper",
|
|
1631
2436
|
"eye_L",
|
|
1632
2437
|
"eye_R",
|
|
1633
2438
|
"iris_L",
|
|
@@ -1646,13 +2451,15 @@ var PART_FILES = [
|
|
|
1646
2451
|
...new Set(
|
|
1647
2452
|
Object.values(DEFAULT_LAYOUT).map((cfg) => cfg.src).filter((src) => !src.startsWith("eyewhite_"))
|
|
1648
2453
|
),
|
|
1649
|
-
EYEWHITE_SRC
|
|
2454
|
+
EYEWHITE_SRC,
|
|
2455
|
+
INTERIOR_SRC
|
|
1650
2456
|
];
|
|
1651
2457
|
var FACE_FEATURES = [
|
|
1652
2458
|
"blush_L",
|
|
1653
2459
|
"blush_R",
|
|
1654
2460
|
"mouth",
|
|
1655
2461
|
"mouth_open",
|
|
2462
|
+
"mouth_inner",
|
|
1656
2463
|
"eye_L",
|
|
1657
2464
|
"eye_R",
|
|
1658
2465
|
"iris_L",
|
|
@@ -1756,20 +2563,20 @@ function keyWhiteToAlpha(rgba) {
|
|
|
1756
2563
|
return keyed;
|
|
1757
2564
|
}
|
|
1758
2565
|
async function coreAndBoundsOf(png) {
|
|
1759
|
-
const { data, info } = await (0,
|
|
2566
|
+
const { data, info } = await (0, import_sharp4.default)(png).ensureAlpha().raw().toBuffer({ resolveWithObject: true });
|
|
1760
2567
|
return {
|
|
1761
|
-
core: (0,
|
|
2568
|
+
core: (0, import_editor9.denseCoreOf)(data, info.width, info.height),
|
|
1762
2569
|
bounds: { x: 0, y: 0, w: info.width, h: info.height }
|
|
1763
2570
|
};
|
|
1764
2571
|
}
|
|
1765
2572
|
async function hasCoverage(png) {
|
|
1766
|
-
const { data, info } = await (0,
|
|
1767
|
-
return (0,
|
|
2573
|
+
const { data, info } = await (0, import_sharp4.default)(png).ensureAlpha().raw().toBuffer({ resolveWithObject: true });
|
|
2574
|
+
return (0, import_editor11.detectAlphaBbox)(data, info.width, info.height) !== null;
|
|
1768
2575
|
}
|
|
1769
2576
|
async function noseSizingOf(png) {
|
|
1770
2577
|
const { core, bounds } = await coreAndBoundsOf(png);
|
|
1771
2578
|
if (core === null) return { box: bounds, whole: true };
|
|
1772
|
-
if ((0,
|
|
2579
|
+
if ((0, import_editor9.isSpeckCore)(core, bounds)) {
|
|
1773
2580
|
return {
|
|
1774
2581
|
box: bounds,
|
|
1775
2582
|
whole: true,
|
|
@@ -1784,24 +2591,24 @@ async function noseSizingOf(png) {
|
|
|
1784
2591
|
async function trimmedPart(role, cfg, partsDir, inMemory, flipSource) {
|
|
1785
2592
|
let img;
|
|
1786
2593
|
if (inMemory !== void 0) {
|
|
1787
|
-
img = (0,
|
|
2594
|
+
img = (0, import_sharp4.default)(inMemory);
|
|
1788
2595
|
} else {
|
|
1789
|
-
const srcPath =
|
|
2596
|
+
const srcPath = import_node_path7.default.join(partsDir, cfg.src);
|
|
1790
2597
|
if (!import_node_fs3.default.existsSync(srcPath)) {
|
|
1791
2598
|
if (cfg.optional) return null;
|
|
1792
2599
|
throw new AutoRigInputError(
|
|
1793
|
-
`missing part source for role "${role}": ${srcPath}`
|
|
2600
|
+
`missing part source for role "${role}": ${srcPath}` + (role === "mouth" ? " \u2014 draw mouth.png (with mouth_open.png), or the lip set's mouth_keyed.png + mouth_interior.png" : "")
|
|
1794
2601
|
);
|
|
1795
2602
|
}
|
|
1796
2603
|
const png = await decodePng(srcPath);
|
|
1797
|
-
img = (0,
|
|
2604
|
+
img = (0, import_sharp4.default)(png.hasAlpha ? png.rgba : keyWhiteToAlpha(png.rgba), {
|
|
1798
2605
|
raw: { width: png.width, height: png.height, channels: 4 }
|
|
1799
2606
|
});
|
|
1800
2607
|
}
|
|
1801
2608
|
if (!cfg.noTrim) {
|
|
1802
2609
|
const kept = await img.trim({ threshold: TRIM_THRESHOLD }).ensureAlpha().raw().toBuffer({ resolveWithObject: true });
|
|
1803
2610
|
const { width: keptW, height: keptH } = kept.info;
|
|
1804
|
-
img = (0,
|
|
2611
|
+
img = (0, import_sharp4.default)(kept.data, {
|
|
1805
2612
|
raw: { width: keptW, height: keptH, channels: 4 }
|
|
1806
2613
|
});
|
|
1807
2614
|
const box = boxWithoutStraySpecks(kept.data, keptW, keptH);
|
|
@@ -1827,7 +2634,7 @@ async function sizedPart(role, cfg, trimmed, canvas) {
|
|
|
1827
2634
|
`layout.${role}.${field}: resized part ${width}x${height} exceeds the ${canvasName(canvas)} canvas`
|
|
1828
2635
|
);
|
|
1829
2636
|
}
|
|
1830
|
-
const resized = await (0,
|
|
2637
|
+
const resized = await (0, import_sharp4.default)(trimmed.data).resize(cfg.h === void 0 ? { width } : { width, height, fit: "fill" }).png().toBuffer({ resolveWithObject: true });
|
|
1831
2638
|
return {
|
|
1832
2639
|
buf: resized.data,
|
|
1833
2640
|
w: resized.info.width,
|
|
@@ -1852,7 +2659,7 @@ function placement(role, cfg, w, h, canvas) {
|
|
|
1852
2659
|
assertOnCanvas(`layout.${role}.cx/cy`, w, h, left, top, canvas);
|
|
1853
2660
|
return { left, top };
|
|
1854
2661
|
}
|
|
1855
|
-
function nosePlacement(layout, box, w, h, canvas) {
|
|
2662
|
+
function nosePlacement(layout, mouthKey, box, w, h, canvas) {
|
|
1856
2663
|
const { cx, cy } = layout.nose;
|
|
1857
2664
|
const left = Math.round(cx - (box.x + box.w / 2));
|
|
1858
2665
|
let top;
|
|
@@ -1861,12 +2668,12 @@ function nosePlacement(layout, box, w, h, canvas) {
|
|
|
1861
2668
|
} else {
|
|
1862
2669
|
const eyeRow = (layout.eye_L.cy + layout.eye_R.cy) / 2;
|
|
1863
2670
|
const tipRow = Math.round(
|
|
1864
|
-
eyeRow + NOSE_TIP_AT * (layout.
|
|
2671
|
+
eyeRow + NOSE_TIP_AT * (layout[mouthKey].cy - eyeRow)
|
|
1865
2672
|
);
|
|
1866
2673
|
top = tipRow - (box.y + box.h - 1);
|
|
1867
2674
|
}
|
|
1868
2675
|
assertOnCanvas(
|
|
1869
|
-
cy !== void 0 ? "layout.nose.cx/cy" :
|
|
2676
|
+
cy !== void 0 ? "layout.nose.cx/cy" : `layout.nose.cx/cy (cy unset: its tip row comes from layout.eye_L/eye_R/${mouthKey}.cy)`,
|
|
1870
2677
|
w,
|
|
1871
2678
|
h,
|
|
1872
2679
|
left,
|
|
@@ -1876,7 +2683,7 @@ function nosePlacement(layout, box, w, h, canvas) {
|
|
|
1876
2683
|
return { left, top };
|
|
1877
2684
|
}
|
|
1878
2685
|
function blankCanvas(canvas) {
|
|
1879
|
-
return (0,
|
|
2686
|
+
return (0, import_sharp4.default)({
|
|
1880
2687
|
create: {
|
|
1881
2688
|
width: canvas.width,
|
|
1882
2689
|
height: canvas.height,
|
|
@@ -1885,74 +2692,13 @@ function blankCanvas(canvas) {
|
|
|
1885
2692
|
}
|
|
1886
2693
|
});
|
|
1887
2694
|
}
|
|
1888
|
-
function luma(rgba, i) {
|
|
1889
|
-
return 0.299 * rgba[i] + 0.587 * rgba[i + 1] + 0.114 * rgba[i + 2];
|
|
1890
|
-
}
|
|
1891
|
-
function boxWithoutStraySpecks(rgba, W, H) {
|
|
1892
|
-
const n = W * H;
|
|
1893
|
-
let kept = 0;
|
|
1894
|
-
for (let p = 0; p < n; p++) if (rgba[p * 4 + 3] > TRIM_THRESHOLD) kept++;
|
|
1895
|
-
const visited = new Uint8Array(n);
|
|
1896
|
-
let stack = new Int32Array(1024);
|
|
1897
|
-
const drawing = [W, H, -1, -1];
|
|
1898
|
-
const strays = [W, H, -1, -1];
|
|
1899
|
-
let anyOpaque = false;
|
|
1900
|
-
for (let start = 0; start < n; start++) {
|
|
1901
|
-
if (visited[start] || rgba[start * 4 + 3] <= TRIM_THRESHOLD) continue;
|
|
1902
|
-
let setLeft = W;
|
|
1903
|
-
let setTop = H;
|
|
1904
|
-
let setRight = -1;
|
|
1905
|
-
let setBottom = -1;
|
|
1906
|
-
let size = 0;
|
|
1907
|
-
let opaque = false;
|
|
1908
|
-
let depth = 0;
|
|
1909
|
-
visited[start] = 1;
|
|
1910
|
-
stack[depth++] = start;
|
|
1911
|
-
while (depth > 0) {
|
|
1912
|
-
const p = stack[--depth];
|
|
1913
|
-
size++;
|
|
1914
|
-
if (rgba[p * 4 + 3] >= import_editor8.ALPHA_OPAQUE) opaque = true;
|
|
1915
|
-
const px2 = p % W;
|
|
1916
|
-
const py = (p - px2) / W;
|
|
1917
|
-
setLeft = Math.min(setLeft, px2);
|
|
1918
|
-
setTop = Math.min(setTop, py);
|
|
1919
|
-
setRight = Math.max(setRight, px2);
|
|
1920
|
-
setBottom = Math.max(setBottom, py);
|
|
1921
|
-
for (let y = Math.max(0, py - 1); y <= Math.min(H - 1, py + 1); y++) {
|
|
1922
|
-
for (let x = Math.max(0, px2 - 1); x <= Math.min(W - 1, px2 + 1); x++) {
|
|
1923
|
-
const q = y * W + x;
|
|
1924
|
-
if (!visited[q] && rgba[q * 4 + 3] > TRIM_THRESHOLD) {
|
|
1925
|
-
visited[q] = 1;
|
|
1926
|
-
if (depth === stack.length) {
|
|
1927
|
-
const grown = new Int32Array(Math.min(n, stack.length * 2));
|
|
1928
|
-
grown.set(stack);
|
|
1929
|
-
stack = grown;
|
|
1930
|
-
}
|
|
1931
|
-
stack[depth++] = q;
|
|
1932
|
-
}
|
|
1933
|
-
}
|
|
1934
|
-
}
|
|
1935
|
-
}
|
|
1936
|
-
anyOpaque ||= opaque;
|
|
1937
|
-
const into = !opaque && size < kept * STRAY_SPECK_FRACTION ? strays : drawing;
|
|
1938
|
-
into[0] = Math.min(into[0], setLeft);
|
|
1939
|
-
into[1] = Math.min(into[1], setTop);
|
|
1940
|
-
into[2] = Math.max(into[2], setRight);
|
|
1941
|
-
into[3] = Math.max(into[3], setBottom);
|
|
1942
|
-
}
|
|
1943
|
-
if (!anyOpaque || strays[2] < 0) return null;
|
|
1944
|
-
const [left, top, right, bottom] = drawing;
|
|
1945
|
-
if (strays[0] >= left && strays[1] >= top && strays[2] <= right && strays[3] <= bottom)
|
|
1946
|
-
return null;
|
|
1947
|
-
return { left, top, width: right - left + 1, height: bottom - top + 1 };
|
|
1948
|
-
}
|
|
1949
2695
|
function detachedAboveWhite(rgba, W, H) {
|
|
1950
2696
|
const n = W * H;
|
|
1951
2697
|
const ink = new Uint8Array(n);
|
|
1952
2698
|
for (let p = 0; p < n; p++) {
|
|
1953
2699
|
const alpha = rgba[p * 4 + 3];
|
|
1954
2700
|
if (alpha > 0 && luma(rgba, p * 4) >= EYE_LASH_LUMA)
|
|
1955
|
-
ink[p] = alpha >=
|
|
2701
|
+
ink[p] = alpha >= import_editor11.ALPHA_OPAQUE ? 2 : 1;
|
|
1956
2702
|
}
|
|
1957
2703
|
const label = new Int32Array(n).fill(-1);
|
|
1958
2704
|
const stack = new Int32Array(n);
|
|
@@ -2032,7 +2778,7 @@ function detachedAboveWhite(rgba, W, H) {
|
|
|
2032
2778
|
return detached;
|
|
2033
2779
|
}
|
|
2034
2780
|
async function prepEyeSplit(partsDir) {
|
|
2035
|
-
const srcPath =
|
|
2781
|
+
const srcPath = import_node_path7.default.join(partsDir, EYEWHITE_SRC);
|
|
2036
2782
|
if (!import_node_fs3.default.existsSync(srcPath)) {
|
|
2037
2783
|
throw new AutoRigInputError(`missing eyewhite source: ${srcPath}`);
|
|
2038
2784
|
}
|
|
@@ -2084,6 +2830,50 @@ async function prepEyeSplit(partsDir) {
|
|
|
2084
2830
|
lashLower: lowerInk ? await cropToBuffer(lashLower, W, H, region) : null
|
|
2085
2831
|
};
|
|
2086
2832
|
}
|
|
2833
|
+
function hasTransparency(rgba) {
|
|
2834
|
+
for (let p = 3; p < rgba.length; p += 4) if (rgba[p] < 255) return true;
|
|
2835
|
+
return false;
|
|
2836
|
+
}
|
|
2837
|
+
async function prepLipSources(partsDir, mirrored) {
|
|
2838
|
+
const has = (name) => import_node_fs3.default.existsSync(import_node_path7.default.join(partsDir, name));
|
|
2839
|
+
const keyed = has(KEYED_SRC);
|
|
2840
|
+
const interior = has(INTERIOR_SRC);
|
|
2841
|
+
if (keyed && !interior) {
|
|
2842
|
+
throw new AutoRigInputError(
|
|
2843
|
+
`${KEYED_SRC} needs ${INTERIOR_SRC}: the composer fits the drawn interior into the keyed opening \u2014 draw it (billed)`
|
|
2844
|
+
);
|
|
2845
|
+
}
|
|
2846
|
+
if (interior && !keyed) {
|
|
2847
|
+
throw new AutoRigInputError(
|
|
2848
|
+
`${INTERIOR_SRC} needs ${KEYED_SRC}: the composer cuts the lip set from the keyed open mouth and fits the interior into its opening \u2014 draw it (billed)`
|
|
2849
|
+
);
|
|
2850
|
+
}
|
|
2851
|
+
if (!keyed) return void 0;
|
|
2852
|
+
const legacy = ["mouth.png", "mouth_open.png"].filter(has);
|
|
2853
|
+
if (legacy.length > 0) {
|
|
2854
|
+
throw new AutoRigInputError(
|
|
2855
|
+
`${KEYED_SRC}/${INTERIOR_SRC} cannot be composed beside mouth.png/mouth_open.png (the lip set replaces the mouth drawings, and auto_rig_from_layers refuses the mix) \u2014 remove ${legacy.join(" and ")}`
|
|
2856
|
+
);
|
|
2857
|
+
}
|
|
2858
|
+
const k = await decodePng(import_node_path7.default.join(partsDir, KEYED_SRC));
|
|
2859
|
+
const keyedRgba = keyGreen(
|
|
2860
|
+
hasTransparency(k.rgba) ? k.rgba : keyWhiteToAlpha(k.rgba),
|
|
2861
|
+
k.width,
|
|
2862
|
+
k.height
|
|
2863
|
+
);
|
|
2864
|
+
const i = await decodePng(import_node_path7.default.join(partsDir, INTERIOR_SRC));
|
|
2865
|
+
const prepared = await prepInterior(
|
|
2866
|
+
hasTransparency(i.rgba) ? i.rgba : keyBorderWhite(i.rgba, i.width, i.height),
|
|
2867
|
+
i.width,
|
|
2868
|
+
i.height
|
|
2869
|
+
);
|
|
2870
|
+
return {
|
|
2871
|
+
keyedPng: await (0, import_sharp4.default)(keyedRgba, {
|
|
2872
|
+
raw: { width: k.width, height: k.height, channels: 4 }
|
|
2873
|
+
}).png().toBuffer(),
|
|
2874
|
+
interior: mirrored.has(INTERIOR_SRC) ? { ...prepared, png: await (0, import_sharp4.default)(prepared.png).flop().png().toBuffer() } : prepared
|
|
2875
|
+
};
|
|
2876
|
+
}
|
|
2087
2877
|
async function composeLayersFromParts(input) {
|
|
2088
2878
|
try {
|
|
2089
2879
|
const outDir = resolveOutputDir(input.outDir);
|
|
@@ -2107,10 +2897,49 @@ async function composeLayersFromParts(input) {
|
|
|
2107
2897
|
if (split.lashLower !== null) {
|
|
2108
2898
|
splitSources.set(LOWER_LASH_SRC, split.lashLower);
|
|
2109
2899
|
}
|
|
2900
|
+
const lips = await prepLipSources(partsDir, mirrored);
|
|
2901
|
+
let lipSplit;
|
|
2110
2902
|
const placed = [];
|
|
2111
2903
|
const skipped = [];
|
|
2112
2904
|
let noseSpeck;
|
|
2113
2905
|
for (const role of ORDER) {
|
|
2906
|
+
if (lips !== void 0 && (role === "mouth" || role === "mouth_open")) {
|
|
2907
|
+
continue;
|
|
2908
|
+
}
|
|
2909
|
+
if (role === "mouth_inner") {
|
|
2910
|
+
if (lips === void 0) continue;
|
|
2911
|
+
const cfg2 = layout.mouth_inner;
|
|
2912
|
+
const part2 = await partBuffer(
|
|
2913
|
+
role,
|
|
2914
|
+
cfg2,
|
|
2915
|
+
partsDir,
|
|
2916
|
+
lips.keyedPng,
|
|
2917
|
+
mirrored.has(KEYED_SRC),
|
|
2918
|
+
canvas
|
|
2919
|
+
);
|
|
2920
|
+
const split2 = await splitLipSet(
|
|
2921
|
+
part2.buf,
|
|
2922
|
+
part2.w,
|
|
2923
|
+
part2.h,
|
|
2924
|
+
lips.interior
|
|
2925
|
+
);
|
|
2926
|
+
const { left: left2, top: top2 } = placement(role, cfg2, part2.w, part2.h, canvas);
|
|
2927
|
+
lipSplit = { split: split2, w: part2.w, h: part2.h, left: left2, top: top2 };
|
|
2928
|
+
placed.push({
|
|
2929
|
+
role,
|
|
2930
|
+
part: { buf: split2.inner, w: part2.w, h: part2.h },
|
|
2931
|
+
left: left2,
|
|
2932
|
+
top: top2
|
|
2933
|
+
});
|
|
2934
|
+
continue;
|
|
2935
|
+
}
|
|
2936
|
+
if (role === "lip_lower" || role === "lip_upper") {
|
|
2937
|
+
if (lipSplit === void 0) continue;
|
|
2938
|
+
const { split: split2, w, h, left: left2, top: top2 } = lipSplit;
|
|
2939
|
+
const buf = role === "lip_lower" ? split2.lower : split2.upper;
|
|
2940
|
+
placed.push({ role, part: { buf, w, h }, left: left2, top: top2 });
|
|
2941
|
+
continue;
|
|
2942
|
+
}
|
|
2114
2943
|
const lower = role === "lash_lower_L" || role === "lash_lower_R";
|
|
2115
2944
|
const key = lower ? LOWER_LASH[role] : role;
|
|
2116
2945
|
if (lower && split.lashLower === null) {
|
|
@@ -2119,7 +2948,7 @@ async function composeLayersFromParts(input) {
|
|
|
2119
2948
|
}
|
|
2120
2949
|
if (key === "arm_L" || key === "arm_R") {
|
|
2121
2950
|
const arm = layout[key];
|
|
2122
|
-
if (!import_node_fs3.default.existsSync(
|
|
2951
|
+
if (!import_node_fs3.default.existsSync(import_node_path7.default.join(partsDir, arm.src))) continue;
|
|
2123
2952
|
const body = placed.find((p) => p.role === "body");
|
|
2124
2953
|
if (body === void 0) {
|
|
2125
2954
|
throw new AutoRigInputError(
|
|
@@ -2162,7 +2991,7 @@ async function composeLayersFromParts(input) {
|
|
|
2162
2991
|
}
|
|
2163
2992
|
if (key === "forearm_pose_L" || key === "forearm_pose_R") {
|
|
2164
2993
|
const pose = layout[key];
|
|
2165
|
-
if (!import_node_fs3.default.existsSync(
|
|
2994
|
+
if (!import_node_fs3.default.existsSync(import_node_path7.default.join(partsDir, pose.src))) continue;
|
|
2166
2995
|
const armRole = key === "forearm_pose_L" ? "arm_L" : "arm_R";
|
|
2167
2996
|
const arm = placed.find((p) => p.role === armRole);
|
|
2168
2997
|
if (arm?.elbow === void 0 || arm.elbowRun === void 0) {
|
|
@@ -2233,7 +3062,14 @@ async function composeLayersFromParts(input) {
|
|
|
2233
3062
|
noseSpeck = part.nose?.speck;
|
|
2234
3063
|
const { core, bounds } = await coreAndBoundsOf(part.buf);
|
|
2235
3064
|
const box = part.nose?.whole ? bounds : core ?? bounds;
|
|
2236
|
-
({ left, top } = nosePlacement(
|
|
3065
|
+
({ left, top } = nosePlacement(
|
|
3066
|
+
layout,
|
|
3067
|
+
lips === void 0 ? "mouth" : "mouth_inner",
|
|
3068
|
+
box,
|
|
3069
|
+
part.w,
|
|
3070
|
+
part.h,
|
|
3071
|
+
canvas
|
|
3072
|
+
));
|
|
2237
3073
|
} else {
|
|
2238
3074
|
({ left, top } = placement(key, layout[key], part.w, part.h, canvas));
|
|
2239
3075
|
}
|
|
@@ -2248,20 +3084,39 @@ async function composeLayersFromParts(input) {
|
|
|
2248
3084
|
}
|
|
2249
3085
|
placed.push({ role, part, left, top });
|
|
2250
3086
|
}
|
|
3087
|
+
const built = /* @__PURE__ */ new Map();
|
|
3088
|
+
for (const { role, part, left, top } of placed) {
|
|
3089
|
+
built.set(
|
|
3090
|
+
role,
|
|
3091
|
+
await blankCanvas(canvas).composite([{ input: part.buf, left, top }]).png().toBuffer()
|
|
3092
|
+
);
|
|
3093
|
+
}
|
|
3094
|
+
const closed = lipSplit === void 0 ? void 0 : await closedLips(
|
|
3095
|
+
lipSplit.split.upper,
|
|
3096
|
+
lipSplit.split.lower,
|
|
3097
|
+
await restShiftOf(
|
|
3098
|
+
{
|
|
3099
|
+
mouth_inner: built.get("mouth_inner"),
|
|
3100
|
+
lip_lower: built.get("lip_lower"),
|
|
3101
|
+
lip_upper: built.get("lip_upper")
|
|
3102
|
+
},
|
|
3103
|
+
canvas.width,
|
|
3104
|
+
canvas.height
|
|
3105
|
+
),
|
|
3106
|
+
lipSplit.left
|
|
3107
|
+
);
|
|
2251
3108
|
for (const role of ORDER) {
|
|
2252
3109
|
if (placed.some((p) => p.role === role)) continue;
|
|
2253
|
-
import_node_fs3.default.rmSync(
|
|
3110
|
+
import_node_fs3.default.rmSync(import_node_path7.default.join(outDir, `${role}.png`), { force: true });
|
|
2254
3111
|
}
|
|
2255
3112
|
const posed = (role) => role === "forearm_pose_L" || role === "forearm_pose_R";
|
|
2256
3113
|
if (!placed.some((p) => posed(p.role))) {
|
|
2257
|
-
import_node_fs3.default.rmSync(
|
|
3114
|
+
import_node_fs3.default.rmSync(import_node_path7.default.join(outDir, "preview-pose.png"), { force: true });
|
|
2258
3115
|
}
|
|
2259
3116
|
const layers = [];
|
|
2260
|
-
const preview = [];
|
|
2261
3117
|
for (const { role, part, left, top } of placed) {
|
|
2262
|
-
const
|
|
2263
|
-
|
|
2264
|
-
writeFileAtomic(outPath, layer);
|
|
3118
|
+
const outPath = import_node_path7.default.join(outDir, `${role}.png`);
|
|
3119
|
+
writeFileAtomic(outPath, built.get(role));
|
|
2265
3120
|
layers.push({
|
|
2266
3121
|
role,
|
|
2267
3122
|
path: outPath,
|
|
@@ -2270,9 +3125,20 @@ async function composeLayersFromParts(input) {
|
|
|
2270
3125
|
left,
|
|
2271
3126
|
top
|
|
2272
3127
|
});
|
|
2273
|
-
if (!posed(role)) preview.push({ input: part.buf, left, top });
|
|
2274
3128
|
}
|
|
2275
|
-
const
|
|
3129
|
+
const restInput = (role, buf) => {
|
|
3130
|
+
if (role === "mouth_inner") return null;
|
|
3131
|
+
if (closed === void 0) return buf;
|
|
3132
|
+
if (role === "lip_upper") return closed.upper;
|
|
3133
|
+
if (role === "lip_lower") return closed.lower;
|
|
3134
|
+
return buf;
|
|
3135
|
+
};
|
|
3136
|
+
const preview = [];
|
|
3137
|
+
for (const { role, part, left, top } of placed) {
|
|
3138
|
+
const input2 = restInput(role, part.buf);
|
|
3139
|
+
if (!posed(role) && input2 !== null) preview.push({ input: input2, left, top });
|
|
3140
|
+
}
|
|
3141
|
+
const previewPng = await (0, import_sharp4.default)({
|
|
2276
3142
|
create: {
|
|
2277
3143
|
width: canvas.width,
|
|
2278
3144
|
height: canvas.height,
|
|
@@ -2280,7 +3146,7 @@ async function composeLayersFromParts(input) {
|
|
|
2280
3146
|
background: { r: 245, g: 245, b: 248, alpha: 1 }
|
|
2281
3147
|
}
|
|
2282
3148
|
}).composite(preview).png().toBuffer();
|
|
2283
|
-
const previewPath =
|
|
3149
|
+
const previewPath = import_node_path7.default.join(outDir, "preview.png");
|
|
2284
3150
|
writeFileAtomic(previewPath, previewPng);
|
|
2285
3151
|
let previewPosePath;
|
|
2286
3152
|
if (placed.some((p) => posed(p.role))) {
|
|
@@ -2290,17 +3156,18 @@ async function composeLayersFromParts(input) {
|
|
|
2290
3156
|
(p) => p.role === (role === "arm_L" ? "forearm_pose_L" : "forearm_pose_R")
|
|
2291
3157
|
);
|
|
2292
3158
|
if (!armPosed) {
|
|
2293
|
-
|
|
3159
|
+
const input2 = restInput(role, part.buf);
|
|
3160
|
+
if (input2 !== null) pose.push({ input: input2, left, top });
|
|
2294
3161
|
continue;
|
|
2295
3162
|
}
|
|
2296
3163
|
const rows = Math.min(part.h, Math.max(1, Math.round(elbow.y - top)));
|
|
2297
3164
|
pose.push({
|
|
2298
|
-
input: await (0,
|
|
3165
|
+
input: await (0, import_sharp4.default)(part.buf).extract({ left: 0, top: 0, width: part.w, height: rows }).png().toBuffer(),
|
|
2299
3166
|
left,
|
|
2300
3167
|
top
|
|
2301
3168
|
});
|
|
2302
3169
|
}
|
|
2303
|
-
const posePng = await (0,
|
|
3170
|
+
const posePng = await (0, import_sharp4.default)({
|
|
2304
3171
|
create: {
|
|
2305
3172
|
width: canvas.width,
|
|
2306
3173
|
height: canvas.height,
|
|
@@ -2308,7 +3175,7 @@ async function composeLayersFromParts(input) {
|
|
|
2308
3175
|
background: { r: 245, g: 245, b: 248, alpha: 1 }
|
|
2309
3176
|
}
|
|
2310
3177
|
}).composite(pose).png().toBuffer();
|
|
2311
|
-
previewPosePath =
|
|
3178
|
+
previewPosePath = import_node_path7.default.join(outDir, "preview-pose.png");
|
|
2312
3179
|
writeFileAtomic(previewPosePath, posePng);
|
|
2313
3180
|
}
|
|
2314
3181
|
return {
|
|
@@ -2329,7 +3196,7 @@ async function composeLayersFromParts(input) {
|
|
|
2329
3196
|
|
|
2330
3197
|
// src/server.ts
|
|
2331
3198
|
function createIkiMcpServer() {
|
|
2332
|
-
const server = new import_mcp.McpServer({ name: "iki", version: "0.
|
|
3199
|
+
const server = new import_mcp.McpServer({ name: "iki", version: "0.22.0" });
|
|
2333
3200
|
server.registerTool(
|
|
2334
3201
|
"validate_iki",
|
|
2335
3202
|
{
|
|
@@ -2449,7 +3316,7 @@ function createIkiMcpServer() {
|
|
|
2449
3316
|
server.registerTool(
|
|
2450
3317
|
"compose_layers_from_parts",
|
|
2451
3318
|
{
|
|
2452
|
-
description: "Composes AI-generated part PNGs into canvas-aligned, role-named PNG layers on disk, ready for auto_rig_from_layers (the eyewhite split, alpha-trim/white-key, and placement pipeline the character-generation skill needs), with the same geometry report measure_layers returns standalone included inline. face,
|
|
3319
|
+
description: "Composes AI-generated part PNGs into canvas-aligned, role-named PNG layers on disk, ready for auto_rig_from_layers (the eyewhite split, alpha-trim/white-key, and placement pipeline the character-generation skill needs), with the same geometry report measure_layers returns standalone included inline. face, eyewhite, iris, brow, hair_front are required, with mouth.png \u2014 or the lip set's mouth_keyed.png (the open mouth, its inside painted flat #00FF00) + mouth_interior.png (the cavity, teeth and tongue alone), which the composer keys and splits into mouth_inner / lip_lower / lip_upper on one frame so auto_rig_from_layers folds the mouth open like the eyelid; the two routes cannot be mixed, one keyed file without the other is refused, and a key that fails (no green, green outside the opening, an outline not closed round it, more than one opening, no skin under the opening, an interior that is only lips, or one drawn opaque on a non-white or noisy ground) is refused naming the regeneration; with the lip set neither mouth nor mouth_open is written and preview.png shows the mouth closed, as the rig draws the rest pose. hair_back, body, mouth_open (legacy route only), blush (one blush.png makes the blush_L/blush_R pair) and nose are optional \u2014 without a nose layer the head still turns on the rig's default profile, but it has no nose to lead the turn, and `auto_rig_from_layers` fits no turn to the art (`turnTargets` is inert, no `turn` report). One arm.png \u2014 the arm on the screen LEFT, drawn hanging with its shoulder at the top \u2014 makes both arms of a full body: arm_R takes it as drawn and arm_L mirrored. The arms are opt-in: without arm.png the compose is a bust's and `skipped` never lists them. arm.png needs body.png, and each arm is hung on the body's shoulder by default (see `layout`). One forearm_pose.png \u2014 the arm's forearm raised, on the screen LEFT, standing up from its elbow with the hand at the top \u2014 makes both pose forearms (forearm_pose_R as drawn, forearm_pose_L mirrored): each is scaled so its elbow end is as wide as its arm's elbow run and pinned on that arm's elbow (see `layout`), and it needs arm.png. With one, preview-pose.png shows the switch at 1 as the rig draws it (the hanging forearms hidden below the elbow, the pose forearms over everything) while preview.png stays the rest pose; without one it is removed. The inline report carries the full-body checks too: a body whose feet a tall canvas's bottom cuts off, an arm without a body, an arm whose shoulder cap does not reach under the torso's edge, and a pose forearm without its arm or off its arm's elbow in pivot, width or length. A nose whose dense core is a speck \u2014 under a quarter of its trimmed part's width or height, such as a lone nostril mark or highlight \u2014 is sized and placed whole, and the inline report warns of it. A light mark drawn detached above the eye white, such as a double-eyelid crease, is dropped from the sclera (the iris's clip); dark strokes there stay lash ink. The eyewhite's dark lower lid \u2014 its line and lashes \u2014 becomes lash_lower_L/lash_lower_R, drawn over the iris; a white with none writes none and lists them in `skipped`.",
|
|
2453
3320
|
inputSchema: {
|
|
2454
3321
|
partsDir: import_zod.z.string().describe(
|
|
2455
3322
|
"Directory of the source part PNGs (resolved against cwd)."
|
|
@@ -2466,7 +3333,7 @@ function createIkiMcpServer() {
|
|
|
2466
3333
|
h: import_zod.z.number()
|
|
2467
3334
|
}).partial()
|
|
2468
3335
|
).optional().describe(
|
|
2469
|
-
"Per-role override merged over the built-in defaults, keyed by role \u2014 hair_back, body, arm_L, arm_R, face, blush_L, blush_R, nose, mouth, mouth_open, eye_L, eye_R, iris_L, iris_R, lash_L, lash_R, brow_L, brow_R, hair_front, forearm_pose_L, forearm_pose_R \u2014 e.g. `{ eye_L: { cx: 660 } }`. `w` is an integer in 1..1100 (the canvas width) and `h` in 1..canvasHeight; omit `h` to keep the part's own aspect, set it to stretch. The defaults of the face's features \u2014 blush, nose, mouth, mouth_open, eyes, irises, lashes, brows \u2014 are proportions of the face: an override of face.cx, face.cy or face.w carries them with it (scaled by the width; face.h stretches the face alone), while a feature's own override names canvas px. The nose is sized and placed by its dense core (its pixels at alpha >= 128, the drawing inside a soft nose's feather), not its whole part: `w`/`h` size the core, `cx` centres it, and a set `cy` centres its rows. A nose whose core is a speck \u2014 under a quarter of the trimmed part's width or height, such as a lone nostril mark or highlight \u2014 is sized and placed whole instead, and the report warns of it. Its `cy` defaults to unset, which lands the core's bottom row (the tip) 0.66 of the way from the eye row down to the mouth's, so the nose follows a retuned eye or mouth row. eye_L/lash_L and eye_R/lash_R are cut from one eyewhite into one frame, so each pair must land on the same frame \u2014 a pair set to land apart is rejected. lash_lower_L/lash_lower_R are cut from it too and have no key: they land on their eye's frame. arm_L/arm_R have no cx/cy/w defaults: an unset `w` is 0.4 of the placed body's width, and an unset `cx` or `cy` hangs the arm's shoulder pivot \u2014 the one auto_rig_from_layers turns it about \u2014 on the body box's shoulder corner on that axis: its left edge for arm_R, its right edge for arm_L, 0.12 of its height under its top. A set one centres the arm as for every other role. forearm_pose_L/forearm_pose_R have no cx/cy/w defaults either: an unset `w` scales the forearm so its elbow end (the shoulder rule run from its bottom) is as wide as the placed arm's elbow run, and an unset `cx` or `cy` pins that elbow end's pivot on the arm's elbow \u2014 the one auto_rig_from_layers hangs it from."
|
|
3336
|
+
"Per-role override merged over the built-in defaults, keyed by role \u2014 hair_back, body, arm_L, arm_R, face, blush_L, blush_R, nose, mouth, mouth_open, mouth_inner, eye_L, eye_R, iris_L, iris_R, lash_L, lash_R, brow_L, brow_R, hair_front, forearm_pose_L, forearm_pose_R \u2014 e.g. `{ eye_L: { cx: 660 } }`. `w` is an integer in 1..1100 (the canvas width) and `h` in 1..canvasHeight; omit `h` to keep the part's own aspect, set it to stretch. The defaults of the face's features \u2014 blush, nose, mouth, mouth_open, mouth_inner, eyes, irises, lashes, brows \u2014 are proportions of the face: an override of face.cx, face.cy or face.w carries them with it (scaled by the width; face.h stretches the face alone), while a feature's own override names canvas px. The nose is sized and placed by its dense core (its pixels at alpha >= 128, the drawing inside a soft nose's feather), not its whole part: `w`/`h` size the core, `cx` centres it, and a set `cy` centres its rows. A nose whose core is a speck \u2014 under a quarter of the trimmed part's width or height, such as a lone nostril mark or highlight \u2014 is sized and placed whole instead, and the report warns of it. Its `cy` defaults to unset, which lands the core's bottom row (the tip) 0.66 of the way from the eye row down to the mouth's, so the nose follows a retuned eye or mouth row. eye_L/lash_L and eye_R/lash_R are cut from one eyewhite into one frame, so each pair must land on the same frame \u2014 a pair set to land apart is rejected. lash_lower_L/lash_lower_R are cut from it too and have no key: they land on their eye's frame. mouth_inner is the lip set's one frame (mouth_open's place by default; lip_lower and lip_upper have no key and land on it, so a retune moves the three together), and with the lip set the nose's tip rule reads layout.mouth_inner.cy. arm_L/arm_R have no cx/cy/w defaults: an unset `w` is 0.4 of the placed body's width, and an unset `cx` or `cy` hangs the arm's shoulder pivot \u2014 the one auto_rig_from_layers turns it about \u2014 on the body box's shoulder corner on that axis: its left edge for arm_R, its right edge for arm_L, 0.12 of its height under its top. A set one centres the arm as for every other role. forearm_pose_L/forearm_pose_R have no cx/cy/w defaults either: an unset `w` scales the forearm so its elbow end (the shoulder rule run from its bottom) is as wide as the placed arm's elbow run, and an unset `cx` or `cy` pins that elbow end's pivot on the arm's elbow \u2014 the one auto_rig_from_layers hangs it from."
|
|
2470
3337
|
),
|
|
2471
3338
|
mirrorParts: import_zod.z.array(import_zod.z.string()).optional().describe(
|
|
2472
3339
|
"Part files to flip left-right as they are read, e.g. `[\"eyewhite.png\"]`. The composer reads eyewhite.png as the eye on the screen LEFT (lash wing at the image's left end, lash-free tear duct at its right), brow.png as the brow on the screen RIGHT (thick head at the image's left end, tail at its right), blush.png as the blush on the screen LEFT (outer end, toward the face's edge, at the image's left end, inner end at its right), arm.png as the arm on the screen LEFT (the character's right, hanging, its shoulder at the image's top), and forearm_pose.png as the raised forearm on the screen LEFT (standing up from its elbow, the hand at the image's top, the thumb toward the body at the image's right); a part drawn the other way round is fixed here for free. It flips the source, so every role cut from it flips together and an eye's sclera and lash stay in one frame."
|
|
@@ -2497,7 +3364,7 @@ function createIkiMcpServer() {
|
|
|
2497
3364
|
server.registerTool(
|
|
2498
3365
|
"measure_layers",
|
|
2499
3366
|
{
|
|
2500
|
-
description: "Reports read-only geometry checks over an already-composed layers directory \u2014 the same checks compose_layers_from_parts returns inline (iris ratio/offset, eye aspect, cropped/cut edges, a nose whose dense core is a speck of its crop, missing optional roles, a body whose feet a tall canvas's bottom cuts off, an arm without a body, an arm whose shoulder cap does not reach under the torso's edge on its shoulder pivot's row, a pose forearm without its arm or whose elbow end is off its arm's elbow in pivot, width or length). Every preview*.png is skipped. Use to re-check a layers dir without recomposing. Its nose check reads the composed file against its crop, which is what auto_rig_from_layers acts on. Compose's inline report does the same unless the composer found the source part's core a speck, which it reports instead \u2014 so a speck that resampling erased from the layer shows there and not here.",
|
|
3367
|
+
description: "Reports read-only geometry checks over an already-composed layers directory \u2014 the same checks compose_layers_from_parts returns inline (iris ratio/offset, eye aspect, cropped/cut edges, a nose whose dense core is a speck of its crop, missing optional roles, a body whose feet a tall canvas's bottom cuts off, an arm without a body, an arm whose shoulder cap does not reach under the torso's edge on its shoulder pivot's row, a pose forearm without its arm or whose elbow end is off its arm's elbow in pivot, width or length, and the lip set: a partial one or one beside mouth/mouth_open, the fold's contract per column of the opening, an opening too narrow or short, a line too thin, green left, an interior with holes \u2014 with the opening's size and the MouthOpen below which no slit shows). Every preview*.png is skipped. Use to re-check a layers dir without recomposing. Its nose check reads the composed file against its crop, which is what auto_rig_from_layers acts on. Compose's inline report does the same unless the composer found the source part's core a speck, which it reports instead \u2014 so a speck that resampling erased from the layer shows there and not here.",
|
|
2501
3368
|
inputSchema: {
|
|
2502
3369
|
layersDir: import_zod.z.string().describe(
|
|
2503
3370
|
"Directory of role-named layer PNGs (resolved against cwd)."
|