spine-parts 2.27.1 → 2.28.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/cli.ts +36 -4
- package/docs/AUTHORING.md +30 -4
- package/docs/STABILITY.md +75 -2
- package/package.json +2 -2
- package/src/build.ts +9 -2
- package/src/comfy/client.ts +9 -0
- package/src/comfy/index.ts +2 -0
- package/src/comfy/matte.ts +51 -0
- package/src/comfy/plate.ts +1 -1
- package/src/comfy/seethrough.ts +20 -4
- package/src/matte.ts +171 -0
- package/src/plate.ts +97 -25
- package/src/seethrough-transform.ts +70 -0
- package/workflows/seethrough.json +6 -2
package/cli.ts
CHANGED
|
@@ -24,7 +24,7 @@ import { proposeNeck } from './tools/neck-propose.ts';
|
|
|
24
24
|
import { parseAltArg } from './tools/alternate-cli.ts';
|
|
25
25
|
import { type AlternateCliInput, assembleStage, build, checkStage, ERROR_MAP_FILE, loopStage, readRuns, readSource, rigStage, suppliedCheckFlag } from './src/build.ts';
|
|
26
26
|
import { ACCEPTABLE_BARS, DEFAULT_PACK_SHAPE, DEFAULT_PAGE_EDGES, findRigc, PACK_SHAPES, rigcRunnerAt, type PackShape, PAGE_EDGES, type PageEdges, PARTS_HOME_SENTENCE, RIGC_GEOMETRY_VERSION, rigcRanClause, type RigcRunner, NO_PARTS_SENTENCE, SEAM_MEAN_BAR, SOURCE_SENTENCE, SEAM_PX_BAR, SEAM_PX_LEVEL, SPINEBOY_YARDSTICK, TEXTURE_STRETCH_CEILING } from './src/check.ts';
|
|
27
|
-
import { ComfyClient, resolveHost, runBackground, runPainting, runSeeThrough } from './src/comfy/index.ts';
|
|
27
|
+
import { ComfyClient, resolveHost, runBackground, runMatte, runPainting, runSeeThrough } from './src/comfy/index.ts';
|
|
28
28
|
import { DEFAULT_GROW_PX, runForeground } from './src/plate.ts';
|
|
29
29
|
import { type CharacterConfig, loadClothConfig, loadConfig, loadEarlyConfig, loadPaintConfig } from './src/config.ts';
|
|
30
30
|
import { configWriteLines, readConfigWriteTarget, separateConfigWriteTarget, stageConfigWrite, type StagedConfigWrite } from './tools/configwrite.ts';
|
|
@@ -868,8 +868,17 @@ usage:
|
|
|
868
868
|
is set. The pose words are generation.pose, or the control skeleton's own.
|
|
869
869
|
The config needs only key and generation here; the rest comes later.
|
|
870
870
|
|
|
871
|
+
rig-parts comfy matte --painting <png> --out <dir>
|
|
872
|
+
--node BiRefNetRMBG --model BiRefNet-HR-matting --checkpoint-sha256 <hex>
|
|
873
|
+
[--resolution 1024] [--threshold 10] [--host <url>]
|
|
874
|
+
[--wait 1800] [--timeout 3600] [--poll 3]
|
|
875
|
+
A receipted model source: raw model-mask.png, painting-grid mask.png and
|
|
876
|
+
receipt.json. No blur, dilation or cleanup. Threshold is grayscale > value;
|
|
877
|
+
non-square inputs use the recorded square transform, square uploads stay
|
|
878
|
+
unchanged. A selector alone is not installation: model files are checked.
|
|
879
|
+
|
|
871
880
|
rig-parts comfy plate --mode background|foreground --painting <png>
|
|
872
|
-
--mask-from seethrough:<dir>|figure-mask:<dir> --out <dir>
|
|
881
|
+
--mask-from seethrough:<dir>|figure-mask:<dir>|matte:<dir> --out <dir>
|
|
873
882
|
[--graph lama|core] [--model <name>] [--checkpoint-sha256 <hex>]
|
|
874
883
|
[--seed 0] [--grow 8] [--steps 30] [--prompt <text>] [--negative <text>]
|
|
875
884
|
[--host <url>] [--wait 1800] [--timeout 3600] [--poll 3]
|
|
@@ -1171,6 +1180,7 @@ const TAKES = {
|
|
|
1171
1180
|
assemble: { valued: ['--source', '--full', '--head', '--config', '--out', '--seam', '--project', '--alt', '--write-config', '--requirements'], switches: ['--propose-plan'] },
|
|
1172
1181
|
'comfy seethrough': { valued: ['--image', '--out', '--resolution', '--steps', '--seed', '--prefix', '--host', '--wait', '--timeout', '--poll', '--config', '--depth-run'], switches: ['--offload', '--no-offload', '--lama', '--nf4'] },
|
|
1173
1182
|
'comfy paint': { valued: ['--config', '--out', '--seeds', '--seed0', '--host', '--wait', '--timeout', '--poll'], switches: [] },
|
|
1183
|
+
'comfy matte': { valued: ['--painting', '--out', '--node', '--model', '--checkpoint-sha256', '--resolution', '--threshold', '--host', '--wait', '--timeout', '--poll'], switches: [] },
|
|
1174
1184
|
'comfy plate': { valued: ['--mode', '--painting', '--mask-from', '--out', '--graph', '--model', '--checkpoint-sha256', '--seed', '--grow', '--steps', '--prompt', '--negative', '--host', '--wait', '--timeout', '--poll'], switches: [] },
|
|
1175
1185
|
inputs: { valued: ['--source', '--config', '--out'], switches: [] },
|
|
1176
1186
|
build: { valued: ['--config', '--source', '--full', '--head', '--out', '--seam', '--project', '--page-edges', '--pack-shape', '--requirements', '--idle-keys', '--lead-in', '--alt', '--set', '--environment', '--wind-band'], switches: ['--loop', '--seam-pairs', '--keep-stills', '--all-sets', '--no-extremes'] },
|
|
@@ -1922,7 +1932,7 @@ function cmdAssemble(args: string[]): number {
|
|
|
1922
1932
|
}
|
|
1923
1933
|
|
|
1924
1934
|
/** Parse `--flag value` pairs and bare `--switch`es, as the command's {@link TAKES} row names them; a string is a usage error. */
|
|
1925
|
-
function comfyFlags(args: string[], command: 'comfy seethrough' | 'comfy paint' | 'comfy plate'): { v: Map<string, string>; on: Set<string> } | string {
|
|
1935
|
+
function comfyFlags(args: string[], command: 'comfy seethrough' | 'comfy paint' | 'comfy plate' | 'comfy matte'): { v: Map<string, string>; on: Set<string> } | string {
|
|
1926
1936
|
const switches: readonly string[] = TAKES[command].switches;
|
|
1927
1937
|
const v = new Map<string, string>();
|
|
1928
1938
|
const on = new Set<string>();
|
|
@@ -2003,12 +2013,34 @@ async function cmdComfyPlate(v: Map<string, string>): Promise<number> {
|
|
|
2003
2013
|
}
|
|
2004
2014
|
}
|
|
2005
2015
|
|
|
2016
|
+
async function cmdComfyMatte(v: Map<string, string>): Promise<number> {
|
|
2017
|
+
const painting = v.get('--painting');
|
|
2018
|
+
const out = v.get('--out');
|
|
2019
|
+
if (painting === undefined || out === undefined) return usage('comfy matte needs --painting <png> and --out <dir>');
|
|
2020
|
+
const resolution = intFlag(v, '--resolution', 1024, 64);
|
|
2021
|
+
const threshold = intFlag(v, '--threshold', 10, 0);
|
|
2022
|
+
const wait = secondsFlag(v, '--wait', 1800);
|
|
2023
|
+
const timeout = secondsFlag(v, '--timeout', 3600);
|
|
2024
|
+
const poll = secondsFlag(v, '--poll', 3);
|
|
2025
|
+
for (const n of [resolution, threshold, wait, timeout, poll]) if (typeof n === 'string') return usage(n);
|
|
2026
|
+
try {
|
|
2027
|
+
const receipt = await runMatte(
|
|
2028
|
+
() => new ComfyClient(resolveHost(v.get('--host'), process.env.COMFY_HOST), { poll: poll as number, request: 30 }),
|
|
2029
|
+
{ painting, out, node: v.get('--node'), model: v.get('--model'), checkpointSha256: v.get('--checkpoint-sha256'), resolution: resolution as number, threshold: threshold as number, wait: wait as number, timeout: timeout as number },
|
|
2030
|
+
line => console.log(line),
|
|
2031
|
+
);
|
|
2032
|
+
console.log(` wrote model-mask.png, mask.png and receipt.json; mask sha256 ${receipt.output.sha256}; checkpoint sha256 declared`);
|
|
2033
|
+
return EXIT_OK;
|
|
2034
|
+
} catch (err) { return printRefusal(err); }
|
|
2035
|
+
}
|
|
2036
|
+
|
|
2006
2037
|
async function cmdComfy(args: string[]): Promise<number> {
|
|
2007
2038
|
const [sub, ...rest] = args;
|
|
2008
|
-
if (sub !== 'seethrough' && sub !== 'paint' && sub !== 'plate') return usage(`comfy takes seethrough, paint or
|
|
2039
|
+
if (sub !== 'seethrough' && sub !== 'paint' && sub !== 'plate' && sub !== 'matte') return usage(`comfy takes seethrough, paint, plate or matte; got ${sub === undefined ? 'nothing' : `"${sub}"`}`);
|
|
2009
2040
|
const f = comfyFlags(rest, `comfy ${sub}`);
|
|
2010
2041
|
if (typeof f === 'string') return usage(f);
|
|
2011
2042
|
if (sub === 'plate') return cmdComfyPlate(f.v);
|
|
2043
|
+
if (sub === 'matte') return cmdComfyMatte(f.v);
|
|
2012
2044
|
const { v, on } = f;
|
|
2013
2045
|
if (on.has('--offload') && on.has('--no-offload')) return usage('--offload and --no-offload cannot be combined');
|
|
2014
2046
|
const wait = secondsFlag(v, '--wait', 1800);
|
package/docs/AUTHORING.md
CHANGED
|
@@ -5363,7 +5363,9 @@ or a joint.
|
|
|
5363
5363
|
nothing cancelled. A job still pending after `--wait` seconds is refused with
|
|
5364
5364
|
its place in the queue and left there. The box's image is pasted into the
|
|
5365
5365
|
painting inside the mask only, so every pixel outside it is the painting's,
|
|
5366
|
-
byte for byte
|
|
5366
|
+
byte for byte (for 16-bit or EXIF-oriented paintings, the byte-exact claim
|
|
5367
|
+
holds against the decoded 8-bit raster, and ComfyUI's `LoadImage` applies the
|
|
5368
|
+
EXIF transpose). `--checkpoint-sha256` (64 hex characters) is required: the box
|
|
5367
5369
|
does not report a hash, so the receipt records the value **as declared, not
|
|
5368
5370
|
measured**.
|
|
5369
5371
|
- **foreground** runs no box and no model. The plate is the painting's pixels
|
|
@@ -5374,15 +5376,39 @@ inside, opaque black outside) and `receipt.json`, spec
|
|
|
5374
5376
|
`rig-parts-plate-receipt/1` ([STABILITY](STABILITY.md#plate-receipt)). A receipted
|
|
5375
5377
|
plate pass from this command is a recorded source step, not a repair.
|
|
5376
5378
|
|
|
5379
|
+
### See-through mask grid mapping
|
|
5380
|
+
|
|
5381
|
+
For non-square paintings, `comfy seethrough` prepares the configured square
|
|
5382
|
+
before upload and records `input_transform` (`pad_fill: "rgba-zero"`) in
|
|
5383
|
+
`meta.json`, bound to the original PNG by `image_sha256` (readers also accept
|
|
5384
|
+
`pad_fill: "rgb-black-opaque"` from 2.27.2 records). `seethrough:` samples the
|
|
5385
|
+
union of layer alpha masks at transformed painting pixel centres with nearest
|
|
5386
|
+
sampling, using the recorded offsets and scale. Exactness holds for
|
|
5387
|
+
representable masks, and detail below the See-through grid's resolution is not
|
|
5388
|
+
recovered. `workflows/seethrough.json` rejoins `LoadImage`'s `IMAGE` and `MASK`
|
|
5389
|
+
through `JoinImageWithAlpha` before `SeeThrough_GenerateLayers`, so the padded
|
|
5390
|
+
area reaches the model with transparent alpha (`alpha = 0`). Square uploads
|
|
5391
|
+
omit the transform and retain the existing upload bytes (an opaque square
|
|
5392
|
+
reaches the model unchanged; a square carrying transparent pixels now forwards
|
|
5393
|
+
its alpha through `JoinImageWithAlpha`). A legacy run on another grid still
|
|
5394
|
+
refuses `PLATE_MASK_GRID`; regenerate it through `comfy seethrough` to obtain a
|
|
5395
|
+
recorded transform. The record fields and sampling formula are specified in
|
|
5396
|
+
[STABILITY](STABILITY.md).
|
|
5397
|
+
|
|
5377
5398
|
| rule | means | change |
|
|
5378
5399
|
| --- | --- | --- |
|
|
5379
|
-
| <a id="PLATE_MASK_SOURCE"></a><a id="PLATE_MASK_GRID"></a><a id="PLATE_MASK_VALUES"></a><a id="PLATE_MASK_EMPTY"></a>`PLATE_MASK_SOURCE`, `PLATE_MASK_GRID`, `PLATE_MASK_VALUES`, `PLATE_MASK_EMPTY` | no `--mask-from`, or a source directory holding no See-through run or no scene figure mask; a source on another grid
|
|
5380
|
-
| <a id="PLATE_MASK_RUN"></a>`PLATE_MASK_RUN` | the figure source has no readable `rig-run/1` assemble `run.json`, or its `figure_rig.png`, `parts.json` or declared `painting_in_rig.png` lacks exactly one matching output hash | re-run `assemble` with the source config to regenerate `run.json` and its `rig/` files together |
|
|
5381
|
-
| <a id="PLATE_MASK_PAINTING"></a>`PLATE_MASK_PAINTING` | `parts.json` has an invalid `painting_in_rig` declaration, its recorded painting is missing, undecodable or differs from its declared hash, or the supplied painting differs from the recorded opaque RGB | use the original `--painting` file for this source, or re-run `assemble`
|
|
5400
|
+
| <a id="PLATE_MASK_SOURCE"></a><a id="PLATE_MASK_GRID"></a><a id="PLATE_MASK_VALUES"></a><a id="PLATE_MASK_EMPTY"></a>`PLATE_MASK_SOURCE`, `PLATE_MASK_GRID`, `PLATE_MASK_VALUES`, `PLATE_MASK_EMPTY` | no `--mask-from`, or a source directory holding no See-through run or no scene figure mask; a source on another grid without a valid recorded `input_transform`; a `figure_rig.png` holding other than opaque white and black; a mask with no figure pixel | run `comfy seethrough` on the original painting to record its input transform, or `assemble` with `config.scene` at the painting grid |
|
|
5401
|
+
| <a id="PLATE_MASK_RUN"></a>`PLATE_MASK_RUN` | the figure source has no readable `rig-run/1` assemble `run.json`, or its `figure_rig.png`, `parts.json` or declared `painting_in_rig.png` lacks exactly one matching output hash; or the See-through source has no readable `meta.json` with `image_sha256` and `layers` hashes, or a layer PNG on disk lacks a matching `layers` entry | re-run `assemble` with the source config to regenerate `run.json` and its `rig/` files together, or re-run `comfy seethrough` on the painting to regenerate `meta.json` and `parts/` together |
|
|
5402
|
+
| <a id="PLATE_MASK_PAINTING"></a>`PLATE_MASK_PAINTING` | `parts.json` has an invalid `painting_in_rig` declaration, its recorded painting is missing, undecodable or differs from its declared hash, or the supplied painting differs from the recorded opaque RGB; or the supplied painting's SHA-256 differs from a See-through source's `meta.json` `image_sha256` | use the original `--painting` file for this source, or re-run `assemble` or `comfy seethrough` on this painting |
|
|
5382
5403
|
| <a id="PLATE_CHECKPOINT_SHA256"></a><a id="PLATE_MODEL_GIVEN"></a><a id="PLATE_CORE_GRID"></a>`PLATE_CHECKPOINT_SHA256`, `PLATE_MODEL_GIVEN`, `PLATE_CORE_GRID` | `--checkpoint-sha256` absent or not 64 hex characters; `--graph core` without `--model`; a painting off the multiple-of-8 grid under `--graph core` | the flag, or `--graph lama` |
|
|
5383
5404
|
| <a id="PLATE_PAINTING_PRESENT"></a><a id="PLATE_OUT_EMPTY"></a>`PLATE_PAINTING_PRESENT`, `PLATE_OUT_EMPTY` | no `--painting` file, or an `--out` that already holds entries | `--painting`, `--out` |
|
|
5384
5405
|
| <a id="COMFY_GPU_NAMED"></a><a id="COMFY_PLATE_SIZE"></a>`COMFY_GPU_NAMED`, `COMFY_PLATE_SIZE` | `/system_stats` does not name exactly one device; the box's image is not the painting's size | the box |
|
|
5385
5406
|
| <a id="PLATE_RECEIPT_READ"></a><a id="PLATE_RECEIPT_SPEC"></a><a id="PLATE_RECEIPT_KEYS"></a><a id="PLATE_RECEIPT_FILE"></a><a id="PLATE_RECEIPT_HASH"></a>`PLATE_RECEIPT_READ`, `PLATE_RECEIPT_SPEC`, `PLATE_RECEIPT_KEYS`, `PLATE_RECEIPT_FILE`, `PLATE_RECEIPT_HASH` | the receipt read back before it is published is not JSON, not this spec, not in its key order, names a file that is gone, or records a sha256 that is not the file's | nothing to change by hand: the run is refused and `<out>` is not written |
|
|
5407
|
+
| <a id="MATTE_MODEL"></a><a id="MATTE_MODEL_INSTALLED"></a>`MATTE_MODEL`, `MATTE_MODEL_INSTALLED` | absent or unsupported explicit node/model, or the supported weight file is absent from the server model listing | use `--node BiRefNetRMBG --model BiRefNet-HR-matting` with installed weights |
|
|
5408
|
+
| <a id="MATTE_PARAMS"></a>`MATTE_PARAMS` | resolution is outside [64,2048] or not a multiple of 8, or threshold is outside integer [0,254] | set `--resolution` and `--threshold` within these bounds |
|
|
5409
|
+
| <a id="MATTE_RECEIPT"></a><a id="MATTE_PAINTING"></a><a id="MATTE_HASH"></a>`MATTE_RECEIPT`, `MATTE_PAINTING`, `MATTE_HASH` | `matte:<dir>` lacks a complete ordered receipt, names another painting, or raw/final mask bytes differ from their recorded hashes | re-run `comfy matte` on the supplied painting and keep its receipt and both masks together |
|
|
5410
|
+
| <a id="MATTE_GRID"></a><a id="MATTE_VALUES"></a><a id="MATTE_EMPTY"></a>`MATTE_GRID`, `MATTE_VALUES`, `MATTE_EMPTY` | mask geometry, prepared input hash or recorded transform is inconsistent, model output is not opaque grayscale, final mask differs from its recorded derivation, or mapped foreground is empty | re-run the model on the painting. The #1197 `input_transform.pad_fill` describes the model's input after `LoadImage` IMAGE separates RGB from alpha. |
|
|
5411
|
+
| <a id="MATTE_OUT_EMPTY"></a>`MATTE_OUT_EMPTY` | matte output is a file or a nonempty directory | choose an absent or empty `--out` |
|
|
5386
5412
|
|
|
5387
5413
|
### assemble
|
|
5388
5414
|
|
package/docs/STABILITY.md
CHANGED
|
@@ -213,7 +213,8 @@ defaults are the help's too, and none is guessed from the art.
|
|
|
213
213
|
| `sheet` | `--source`, `--layers` (repeatable), `--out`; optional `--cell`, `--cols` |
|
|
214
214
|
| `inputs` | `--source`, `--config`, `--out` |
|
|
215
215
|
| `comfy` | `paint`: `--config`, `--out`; optional `--seeds`, `--seed0`, `--host`, `--wait`, `--timeout`, `--poll` |
|
|
216
|
-
| `comfy` | `
|
|
216
|
+
| `comfy` | `matte`: `--painting`, `--out`, `--node` (`BiRefNetRMBG`), `--model` (`BiRefNet-HR-matting`), `--checkpoint-sha256` (declared, required); optional `--resolution`, `--threshold`, `--host`, `--wait`, `--timeout`, `--poll` (issue #1204) |
|
|
217
|
+
| `comfy` | `plate`: `--mode` (`background` or `foreground`), `--painting`, `--mask-from` (`seethrough:<dir>`, `figure-mask:<dir>` or `matte:<dir>`), `--out`; with `--mode background` also `--checkpoint-sha256` (required) and optional `--graph` (`lama` or `core`), `--model` (required with `core`), `--seed`, `--grow`, `--steps`, `--prompt`, `--negative` (the last three with `core` only), `--host`, `--wait`, `--timeout`, `--poll` (issue #1183, additive) |
|
|
217
218
|
| `comfy` | `seethrough`: `--image`, `--out`; optional `--resolution`, `--steps`, `--seed`, `--prefix`, `--offload`, `--no-offload`, `--lama`, `--nf4`, `--host`, `--wait`, `--timeout`, `--poll`, `--config` (See-through settings and optional depth block), `--depth-run` (`full` or `head`, required with the block) |
|
|
218
219
|
| `assemble` | `--propose-plan` with `--source`, `--full`, `--config`, optional `--head`, `--write-config` (must name `--config` itself, issue #356); or `--source`, `--full`, `--config`, `--out`, optional `--head` (without it, one run: issue #359), `--seam` (near-white, silhouette), `--project` (core, visible), `--alt` (repeatable `<name>=<source>:<full>[:<head>]`, issue #454), `--requirements` (source-bound cut-material evidence only when the file declares `cut_material`, issue #888) |
|
|
219
220
|
| `head-crop-preflight` | `--full`, `--canvas`, `--head-box` (JSON coordinate array); read-only advice before the head run, original alpha > 8 samples (issue #1082) |
|
|
@@ -445,7 +446,7 @@ new spec version. Every path in it is relative to the receipt's directory.
|
|
|
445
446
|
| `spec` | `"rig-parts-plate-receipt/1"` |
|
|
446
447
|
| `mode` | `"background"` or `"foreground"` |
|
|
447
448
|
| `painting` | `{ path, sha256 }` of the painting read |
|
|
448
|
-
| `mask` | `{ source, from, path, sha256 }`: `source` is `"seethrough"
|
|
449
|
+
| `mask` | `{ source, from, path, sha256 }`: `source` is `"seethrough"`, `"figure-mask"` or `"matte"`, `from` the source directory, `path` the mask actually used (`mask.png`, opaque white inside) |
|
|
449
450
|
| `model` | `{ name, checkpoint_sha256, checkpoint_sha256_from }`, or null for `foreground`. `checkpoint_sha256` is the `--checkpoint-sha256` value, lower-cased; `checkpoint_sha256_from` is `"declared"`: the box reports no hash, so this field is **declared, not measured** |
|
|
450
451
|
| `params` | every parameter of the run, the seed included, in a fixed order per graph: `lama` — `seed`, `grow_px`, `mask_alpha_above`; `core` — `seed`, `grow_px`, `steps`, `cfg`, `sampler`, `scheduler`, `denoise`, `prompt`, `negative`, `mask_alpha_above`; `foreground` — `mask_alpha_above` |
|
|
451
452
|
| `graph` | `"rig-parts/plate-background-lama/1"`, `"rig-parts/plate-background-core/1"`, or the fixed local id `"rig-parts/plate-foreground/1"` |
|
|
@@ -455,6 +456,43 @@ new spec version. Every path in it is relative to the receipt's directory.
|
|
|
455
456
|
The receipt is read back against the bytes on disk before `<out>` is published;
|
|
456
457
|
a hash that is not its file's refuses the run.
|
|
457
458
|
|
|
459
|
+
### Matte source receipt
|
|
460
|
+
|
|
461
|
+
`comfy matte` writes `model-mask.png` (the exact downloaded grayscale model
|
|
462
|
+
output), `mask.png` (opaque binary mask on the painting grid) and `receipt.json`,
|
|
463
|
+
spec `rig-parts-matte-receipt/1`. Node and model are explicit flags. The supported
|
|
464
|
+
pair is `BiRefNetRMBG` / `BiRefNet-HR-matting`. The model file must also appear in
|
|
465
|
+
`/models/RMBG` before any upload. Queue submission appends behind existing jobs.
|
|
466
|
+
The node has blur and offset zero, inversion and foreground refinement disabled.
|
|
467
|
+
|
|
468
|
+
The ordered receipt keys are `spec`, `painting`, `model`, `params`, `graph`,
|
|
469
|
+
`gpu`, `input_transform`, `input_sha256`, `raw_output`, `output`. `painting`
|
|
470
|
+
records its relative path and the SHA-256 of the source file bytes. `model`
|
|
471
|
+
records node, name, `checkpoint_sha256` and `checkpoint_sha256_from: "declared"`.
|
|
472
|
+
`params` records every node option, resolution, threshold and
|
|
473
|
+
`mask_rule: "red-above-threshold"`. `graph` records
|
|
474
|
+
`id: "rig-parts/matte-BiRefNetRMBG/1"`, the submitted API graph and its SHA-256
|
|
475
|
+
(over UTF-8 `JSON.stringify(api)`). `gpu` is the device the server names.
|
|
476
|
+
`raw_output` and `output` each record a fixed relative path, SHA-256, width and
|
|
477
|
+
height. Paths to output masks cannot redirect the reader to another file.
|
|
478
|
+
|
|
479
|
+
For a non-square painting, `input_transform` uses the #1197 shape documented
|
|
480
|
+
below. Its `pad_fill` describes the model's input after `LoadImage` IMAGE
|
|
481
|
+
separates alpha from RGB. The matte graph feeds that IMAGE output to the model.
|
|
482
|
+
`input_sha256` binds the prepared PNG upload. Square paintings retain their
|
|
483
|
+
original upload bytes and record a null transform. Resolution defaults to 1024
|
|
484
|
+
and applies to the non-square prepared square. It must be a multiple of 8 in
|
|
485
|
+
[64,2048]. The threshold defaults to 10 and must be an integer in [0,254].
|
|
486
|
+
Each grayscale byte strictly above the threshold becomes foreground. The recorded
|
|
487
|
+
nearest-pixel-centre inverse maps that classification to the painting grid.
|
|
488
|
+
|
|
489
|
+
`--mask-from matte:<dir>` binds the supplied painting's exact bytes, both mask
|
|
490
|
+
file hashes, the prepared input, the complete graph, and the final mask's exact
|
|
491
|
+
reproduction from the raw mask and recorded parameters. Missing or legacy
|
|
492
|
+
receipts, another painting, tampering, inconsistent geometry and empty masks
|
|
493
|
+
are named refusals before any plate output or network request. A matte source is
|
|
494
|
+
available to both plate modes. Staged output is read back before publication.
|
|
495
|
+
|
|
458
496
|
### Head-crop preflight stdout
|
|
459
497
|
|
|
460
498
|
`head-crop-preflight` writes one JSON object followed by a newline on success,
|
|
@@ -562,6 +600,41 @@ Painting generation metadata includes `upscale: {model, then}` (issue #698),
|
|
|
562
600
|
read from the same `UPSCALE_MODEL` and `UPSCALE_THEN` constants as the graph.
|
|
563
601
|
This adds provenance; it does not change model selection.
|
|
564
602
|
|
|
603
|
+
See-through run metadata (`meta.json`, written by `comfy seethrough`) includes
|
|
604
|
+
`image_sha256` (the SHA-256 of the original `--image` PNG bytes) and `layers`
|
|
605
|
+
(each written layer PNG's `{path, sha256}`, `path` relative to the run
|
|
606
|
+
directory) (issue #1188, additive under *What "stable" means here*). `comfy
|
|
607
|
+
plate` with `--mask-from seethrough:<dir>` binds every layer file read and
|
|
608
|
+
`--painting` against those hashes and refuses a legacy `meta.json` that lacks
|
|
609
|
+
them.
|
|
610
|
+
|
|
611
|
+
Non-square inputs are prepared by parts before upload (issues #1197, #1205).
|
|
612
|
+
Their additive `input_transform` records `source: [width,height]`,
|
|
613
|
+
`padded: [side,side]`, `pad: [left,top]` in padded pixels, `scale` (target
|
|
614
|
+
side divided by padded side), `resample: "parts-lanczos3-rgba"`,
|
|
615
|
+
`pad_fill: "rgba-zero"` (`"rgb-black-opaque"` for a graph that reads `LoadImage`
|
|
616
|
+
IMAGE only, and on 2.27.2 `seethrough` records, which readers continue to accept),
|
|
617
|
+
`target: [resolution,resolution]` and `inverse: "nearest-pixel-centres"`.
|
|
618
|
+
The enclosing `image_sha256` is the single source-image hash. Preparation
|
|
619
|
+
zero-fills the square, places the source at floor-centred offsets, and runs
|
|
620
|
+
parts' RGBA Lanczos3 resampler. The wrapper receives the configured square.
|
|
621
|
+
In `workflows/seethrough.json`, `LoadImage`'s `IMAGE` (output 0) and `MASK`
|
|
622
|
+
(output 1) rejoin through ComfyUI's core `JoinImageWithAlpha` node before
|
|
623
|
+
`SeeThrough_GenerateLayers`, so the 4-channel RGBA image reaches the model
|
|
624
|
+
with `alpha = 0` on the padded area and `page_alpha` masks layer alpha to zero
|
|
625
|
+
outside the source rectangle, matching the wrapper's internal pad semantics.
|
|
626
|
+
Square inputs keep the existing upload bytes, omit `input_transform`, and for
|
|
627
|
+
opaque square inputs (whether RGBA or RGB-only PNG) reach the model with the
|
|
628
|
+
same `alpha = 255` tensor as before; a square input that itself carries
|
|
629
|
+
transparent pixels now has that transparency forwarded to the model instead of
|
|
630
|
+
dropped at `LoadImage`.
|
|
631
|
+
For each source pixel `(x,y)`, the inverse samples the layer union at
|
|
632
|
+
`floor((x+left+0.5)*scale), floor((y+top+0.5)*scale)`.
|
|
633
|
+
Exactness holds for representable masks, and detail below the See-through
|
|
634
|
+
grid's resolution is not recovered. Missing records on another grid and
|
|
635
|
+
invalid records refuse `PLATE_MASK_GRID`, while mismatched source bytes
|
|
636
|
+
refuse `PLATE_MASK_PAINTING`. Existing runs are never backfilled.
|
|
637
|
+
|
|
565
638
|
Revision ancestry is additive (issue #531): a previous root `remesh.json` or
|
|
566
639
|
`retune.json` is preserved at `provenance/<command>-<sha256>.json`, and optional
|
|
567
640
|
`input.provenance` rows name its source, path, bytes and SHA-256. The chain survives
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "spine-parts",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.28.0",
|
|
4
4
|
"description": "AI-authored 2D character parts from one anime painting and its See-through layer decomposition: measured parts, weighted-mesh rig specs, verified through rig-c before anything is written. A CLI for agents that cannot see the image.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -68,7 +68,7 @@
|
|
|
68
68
|
},
|
|
69
69
|
"dependencies": {
|
|
70
70
|
"ag-psd": "^31.0.2",
|
|
71
|
-
"@rig-c/core": "3.
|
|
71
|
+
"@rig-c/core": "3.5.0"
|
|
72
72
|
},
|
|
73
73
|
"devDependencies": {
|
|
74
74
|
"@esotericsoftware/spine-core": "4.3.13",
|
package/src/build.ts
CHANGED
|
@@ -2595,13 +2595,20 @@ export interface BuildResult {
|
|
|
2595
2595
|
*/
|
|
2596
2596
|
export const RIGC_MODEL_DOCUMENT = 'skeleton.model.json';
|
|
2597
2597
|
|
|
2598
|
+
/**
|
|
2599
|
+
* rigc's run stamp (`rigc-run/1`), which `build` writes beside the artifact
|
|
2600
|
+
* since rig-c 3.5.0: a record of the run, not a Spine file, so it is not
|
|
2601
|
+
* counted as a second skeleton JSON either.
|
|
2602
|
+
*/
|
|
2603
|
+
export const RIGC_RUN_STAMP = 'run.json';
|
|
2604
|
+
|
|
2598
2605
|
/** The three artifact files of a packed build, each refused by name when absent. */
|
|
2599
2606
|
export function artifactPaths(buildDir: string, pack: readonly PackLine[]): string[] {
|
|
2600
2607
|
const problems: Problem[] = [];
|
|
2601
2608
|
const names = existsSync(buildDir) ? readdirSync(buildDir).sort() : [];
|
|
2602
|
-
const json = names.filter((n) => n.endsWith('.json') && n !== RIGC_MODEL_DOCUMENT);
|
|
2609
|
+
const json = names.filter((n) => n.endsWith('.json') && n !== RIGC_MODEL_DOCUMENT && n !== RIGC_RUN_STAMP);
|
|
2603
2610
|
const atlas = names.filter((n) => n.endsWith('.atlas'));
|
|
2604
|
-
if (json.length !== 1) problems.push({ code: 'BUILD_ARTIFACT_PRESENT', object: `${buildDir} skeleton JSON`, detail: `holds ${json.length} .json file(s) [${json.join(', ')}]; the packed build writes exactly one besides rigc's ${RIGC_MODEL_DOCUMENT}` });
|
|
2611
|
+
if (json.length !== 1) problems.push({ code: 'BUILD_ARTIFACT_PRESENT', object: `${buildDir} skeleton JSON`, detail: `holds ${json.length} .json file(s) [${json.join(', ')}]; the packed build writes exactly one besides rigc's ${RIGC_MODEL_DOCUMENT} and ${RIGC_RUN_STAMP}` });
|
|
2605
2612
|
if (atlas.length !== 1) problems.push({ code: 'BUILD_ARTIFACT_PRESENT', object: `${buildDir} atlas`, detail: `holds ${atlas.length} .atlas file(s) [${atlas.join(', ')}]; the packed build writes exactly one` });
|
|
2606
2613
|
if (pack.length === 0) problems.push({ code: 'BUILD_ARTIFACT_PRESENT', object: `${buildDir} packed page`, detail: 'rigc printed no pack line, so no packed page is named; the build runs with --pack and must print one' });
|
|
2607
2614
|
for (const p of pack) {
|
package/src/comfy/client.ts
CHANGED
|
@@ -100,6 +100,15 @@ export class ComfyClient {
|
|
|
100
100
|
return s;
|
|
101
101
|
}
|
|
102
102
|
|
|
103
|
+
/** Listed model files, not the node selector's advertised vocabulary. */
|
|
104
|
+
async modelFiles(folder: string): Promise<string[]> {
|
|
105
|
+
const files = await this.json('GET', `/models/${encodeURIComponent(folder)}`);
|
|
106
|
+
if (!Array.isArray(files) || !files.every((f): f is string => typeof f === 'string')) {
|
|
107
|
+
refuse('MATTE_MODEL_INSTALLED', folder, 'the model listing is not an array of file names; installed weights must be established before upload');
|
|
108
|
+
}
|
|
109
|
+
return files;
|
|
110
|
+
}
|
|
111
|
+
|
|
103
112
|
/** Free VRAM per device in GB (two decimals), keyed by the device name's first word; empty when the box does not say. */
|
|
104
113
|
async vramFree(): Promise<Record<string, number>> {
|
|
105
114
|
const s = await this.systemStats();
|
package/src/comfy/index.ts
CHANGED
|
@@ -18,3 +18,5 @@ export { ComfyClient, type ClientTiming, resolveHost } from './client.ts';
|
|
|
18
18
|
export { runPainting, type PaintRun, type Painted } from './painting.ts';
|
|
19
19
|
export { runBackground, type BackgroundRun } from './plate.ts';
|
|
20
20
|
export { runSeeThrough, type SeeThroughResult, type SeeThroughRun } from './seethrough.ts';
|
|
21
|
+
|
|
22
|
+
export { runMatte, type MatteRun } from './matte.ts';
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/** Optional GPU matte adapter. Local validation precedes host resolution. */
|
|
2
|
+
import { refuseIfAny } from '../errors.ts';
|
|
3
|
+
import { checkGraph } from '../graphs.ts';
|
|
4
|
+
import { assertMatteOutFree, matteGraph, matteGraphHash, matteParams, matteSelection, MATTE_GRAPH_ID, MATTE_MODEL, MATTE_MODEL_FILE, MATTE_NODE, publishMatte, type MatteReceipt } from '../matte.ts';
|
|
5
|
+
import { declaredChecksum, readPlatePainting, sha256 } from '../plate.ts';
|
|
6
|
+
import { encodePngBytes } from '../raster/png.ts';
|
|
7
|
+
import { prepareSeeThroughInput } from '../seethrough-transform.ts';
|
|
8
|
+
import { ComfyClient, historyImages, refuse } from './client.ts';
|
|
9
|
+
import { gpuName } from './plate.ts';
|
|
10
|
+
|
|
11
|
+
export interface MatteRun {
|
|
12
|
+
painting: string;
|
|
13
|
+
out: string;
|
|
14
|
+
node: string | undefined;
|
|
15
|
+
model: string | undefined;
|
|
16
|
+
checkpointSha256: string | undefined;
|
|
17
|
+
resolution: number;
|
|
18
|
+
threshold: number;
|
|
19
|
+
wait: number;
|
|
20
|
+
timeout: number;
|
|
21
|
+
}
|
|
22
|
+
export async function runMatte(connect: () => ComfyClient, run: MatteRun, say: (line: string) => void): Promise<MatteReceipt> {
|
|
23
|
+
matteSelection(run.node, run.model);
|
|
24
|
+
const params = matteParams(run.resolution, run.threshold);
|
|
25
|
+
const checkpoint = declaredChecksum(run.checkpointSha256);
|
|
26
|
+
const painting = readPlatePainting(run.painting);
|
|
27
|
+
assertMatteOutFree(run.out);
|
|
28
|
+
const prepared = prepareSeeThroughInput(painting.raster, run.resolution, 'rgb-black-opaque');
|
|
29
|
+
const input = prepared.transform === undefined ? painting.bytes : encodePngBytes(prepared.image);
|
|
30
|
+
const client = connect();
|
|
31
|
+
const gpu = gpuName(await client.systemStats(), client.host);
|
|
32
|
+
const prefix = `rig_parts_matte_${crypto.randomUUID().slice(0, 8)}`;
|
|
33
|
+
const info = await client.objectInfo();
|
|
34
|
+
refuseIfAny(checkGraph(matteGraph(`${prefix}.png`, prefix), info, client.host, new Set(['1.image'])));
|
|
35
|
+
const files = await client.modelFiles('RMBG');
|
|
36
|
+
if (!files.some(f => f.replaceAll('\\', '/') === MATTE_MODEL_FILE)) refuse('MATTE_MODEL_INSTALLED', MATTE_MODEL_FILE, 'is absent from /models/RMBG; a selector entry alone does not establish installed weights, and automatic download is not permitted');
|
|
37
|
+
const image = await client.uploadImage(input, `${prefix}.png`);
|
|
38
|
+
const graph = matteGraph(image, prefix);
|
|
39
|
+
const id = await client.submit(graph, 'rig-parts');
|
|
40
|
+
say(` appended matte prompt ${id} behind all queued jobs`);
|
|
41
|
+
const { entry } = await client.waitBehind(id, run.wait, run.timeout, say);
|
|
42
|
+
const outputs = historyImages(entry);
|
|
43
|
+
if (outputs.length !== 1) refuse('COMFY_HISTORY_OUTPUTS', `matte prompt ${id}`, `lists ${outputs.length} images; exactly one saved model mask is required`);
|
|
44
|
+
const output = outputs[0];
|
|
45
|
+
const raw = await client.view(output.filename, output.subfolder, output.type);
|
|
46
|
+
return publishMatte(run.out, run.painting, painting.bytes, raw, {
|
|
47
|
+
model: { node: MATTE_NODE, name: MATTE_MODEL, checkpoint_sha256: checkpoint, checkpoint_sha256_from: 'declared' },
|
|
48
|
+
params, graph: { id: MATTE_GRAPH_ID, api: graph, sha256: matteGraphHash(graph) }, gpu,
|
|
49
|
+
input_transform: prepared.transform ?? null, input_sha256: sha256(input),
|
|
50
|
+
});
|
|
51
|
+
}
|
package/src/comfy/plate.ts
CHANGED
|
@@ -81,7 +81,7 @@ export async function runBackground(connect: () => ComfyClient, run: BackgroundR
|
|
|
81
81
|
const from = parseMaskFrom(run.maskFrom);
|
|
82
82
|
const painting = readPlatePainting(run.painting);
|
|
83
83
|
const { width: W, height: H } = painting.raster;
|
|
84
|
-
const source: FigureMask = readSourceMask(from.source, from.dir, W, H, painting.raster);
|
|
84
|
+
const source: FigureMask = readSourceMask(from.source, from.dir, W, H, painting.raster, painting.bytes);
|
|
85
85
|
const checkpoint = declaredChecksum(run.checkpointSha256);
|
|
86
86
|
if (run.graph === 'core') {
|
|
87
87
|
if (run.model === undefined) refuse('PLATE_MODEL_GIVEN', '--model', 'is absent; the core graph needs the checkpoint the box loads, by its name on the box — there is no default checkpoint');
|
package/src/comfy/seethrough.ts
CHANGED
|
@@ -33,7 +33,9 @@ import { refuseIfAny } from '../errors.ts';
|
|
|
33
33
|
import { checkGraph, fillSeeThrough, type SeeThroughParams } from '../graphs.ts';
|
|
34
34
|
import { readWrapperLayers } from '../layers.ts';
|
|
35
35
|
import type { DepthBinding } from '../depthsheets.ts';
|
|
36
|
-
import {
|
|
36
|
+
import { sha256 } from '../plate.ts';
|
|
37
|
+
import { prepareSeeThroughInput } from '../seethrough-transform.ts';
|
|
38
|
+
import { decodePngBytes, encodePngBytes } from '../raster/png.ts';
|
|
37
39
|
import { ComfyClient, historyImages, refuse } from './client.ts';
|
|
38
40
|
|
|
39
41
|
export const SEETHROUGH_WORKFLOW = new URL('../../workflows/seethrough.json', import.meta.url);
|
|
@@ -82,6 +84,8 @@ export async function runSeeThrough(client: ComfyClient, run: SeeThroughRun, say
|
|
|
82
84
|
const bytes = new Uint8Array(readFileSync(run.image));
|
|
83
85
|
const img = decodePngBytes(bytes, run.image);
|
|
84
86
|
assertOutFree(run.out);
|
|
87
|
+
const prepared = prepareSeeThroughInput(img, run.resolution, 'rgba-zero');
|
|
88
|
+
const uploadBytes = prepared.transform === undefined ? bytes : encodePngBytes(prepared.image);
|
|
85
89
|
|
|
86
90
|
await client.systemStats();
|
|
87
91
|
const prefix = `${run.prefix}_${crypto.randomUUID().slice(0, 8)}`;
|
|
@@ -92,8 +96,8 @@ export async function runSeeThrough(client: ComfyClient, run: SeeThroughRun, say
|
|
|
92
96
|
refuseIfAny(checkGraph(fillSeeThrough(template, params), info, client.host, new Set(['1.image'])));
|
|
93
97
|
say(` box: every node and input of workflows/seethrough.json is on its /object_info`);
|
|
94
98
|
|
|
95
|
-
const ref = await client.uploadImage(
|
|
96
|
-
say(` uploaded ${run.image} (${
|
|
99
|
+
const ref = await client.uploadImage(uploadBytes, uploadName);
|
|
100
|
+
say(` uploaded ${run.image} (${prepared.image.width}x${prepared.image.height}) as input/${ref}`);
|
|
97
101
|
const graph = fillSeeThrough(template, { ...params, image: ref });
|
|
98
102
|
const vramBefore = await client.vramFree();
|
|
99
103
|
const id = await client.submit(graph, 'rig-parts');
|
|
@@ -137,13 +141,23 @@ export async function runSeeThrough(client: ComfyClient, run: SeeThroughRun, say
|
|
|
137
141
|
mkdirSync(join(stage, 'previews'), { recursive: true });
|
|
138
142
|
try {
|
|
139
143
|
for (const p of previews) writeFileSync(join(stage, 'previews', basename(p.filename)), await client.view(p.filename, p.subfolder, p.type));
|
|
140
|
-
|
|
144
|
+
const layerHashes: Array<{ path: string; sha256: string }> = [];
|
|
145
|
+
for (const l of layers) {
|
|
146
|
+
const rel = `parts/${l.name as string}.png`;
|
|
147
|
+
const layerBytes = await client.view(l.filename as string);
|
|
148
|
+
writeFileSync(join(stage, rel), layerBytes);
|
|
149
|
+
layerHashes.push({ path: rel, sha256: sha256(layerBytes) });
|
|
150
|
+
}
|
|
141
151
|
if (depth !== undefined) for (const l of layers) if ('depth_filename' in l) writeFileSync(join(stage, 'parts', `${l.name as string}_depth.png`), await client.view(l.depth_filename as string));
|
|
142
152
|
writeFileSync(join(stage, 'layers.json'), manifestBytes);
|
|
143
153
|
const set = readWrapperLayers(stage, depth);
|
|
154
|
+
if (prepared.transform !== undefined && (set.canvas.w !== run.resolution || set.canvas.h !== run.resolution)) {
|
|
155
|
+
refuse('COMFY_MANIFEST_NAMED', `output/${infoLog}`, `canvas is ${set.canvas.w}x${set.canvas.h}; the prepared ${run.resolution}x${run.resolution} input grid is required`);
|
|
156
|
+
}
|
|
144
157
|
const meta = {
|
|
145
158
|
prompt_id: id,
|
|
146
159
|
image: run.image,
|
|
160
|
+
image_sha256: sha256(bytes),
|
|
147
161
|
elapsed_s: Math.round(elapsed * 10) / 10,
|
|
148
162
|
vram_free_before_gb: vramBefore,
|
|
149
163
|
vram_free_low_gb: vramLow,
|
|
@@ -156,7 +170,9 @@ export async function runSeeThrough(client: ComfyClient, run: SeeThroughRun, say
|
|
|
156
170
|
lama: run.lama,
|
|
157
171
|
layer_count: layers.length,
|
|
158
172
|
layer_names: layers.map((l) => l.name as string),
|
|
173
|
+
layers: layerHashes,
|
|
159
174
|
canvas: [set.canvas.w, set.canvas.h],
|
|
175
|
+
...(prepared.transform === undefined ? {} : { input_transform: prepared.transform }),
|
|
160
176
|
};
|
|
161
177
|
writeFileSync(join(stage, 'meta.json'), `${JSON.stringify(meta, null, 2)}\n`);
|
|
162
178
|
if (existsSync(run.out)) rmdirSync(run.out);
|
package/src/matte.ts
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
/** Receipted model masks, bound to the exact painting bytes; no pixel repair. */
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
3
|
+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, renameSync, rmdirSync, rmSync, statSync, writeFileSync } from 'node:fs';
|
|
4
|
+
import { basename, dirname, join, relative, resolve } from 'node:path';
|
|
5
|
+
import { PartsError } from './errors.ts';
|
|
6
|
+
import type { Graph } from './graphs.ts';
|
|
7
|
+
import { decodePngBytes, encodePngBytes } from './raster/png.ts';
|
|
8
|
+
import { newRaster, type Raster } from './raster/types.ts';
|
|
9
|
+
import { inverseSeeThroughMask, isSeeThroughTransform, prepareSeeThroughInput as prepareMatteInput, type SeeThroughTransform } from './seethrough-transform.ts';
|
|
10
|
+
|
|
11
|
+
export const MATTE_SPEC = 'rig-parts-matte-receipt/1';
|
|
12
|
+
export const MATTE_KEYS = ['spec', 'painting', 'model', 'params', 'graph', 'gpu', 'input_transform', 'input_sha256', 'raw_output', 'output'] as const;
|
|
13
|
+
export const MATTE_NODE = 'BiRefNetRMBG';
|
|
14
|
+
export const MATTE_MODEL = 'BiRefNet-HR-matting';
|
|
15
|
+
export const MATTE_MODEL_FILE = 'BiRefNet/BiRefNet-HR-matting.safetensors';
|
|
16
|
+
export const MATTE_GRAPH_ID = 'rig-parts/matte-BiRefNetRMBG/1';
|
|
17
|
+
const NODE_PARAMS = { mask_blur: 0, mask_offset: 0, invert_output: false, refine_foreground: false, background: 'Alpha', background_color: '#222222' } as const;
|
|
18
|
+
interface Artifact {
|
|
19
|
+
path: string;
|
|
20
|
+
sha256: string;
|
|
21
|
+
width: number;
|
|
22
|
+
height: number;
|
|
23
|
+
}
|
|
24
|
+
export interface MatteReceipt {
|
|
25
|
+
spec: typeof MATTE_SPEC;
|
|
26
|
+
painting: { path: string; sha256: string };
|
|
27
|
+
model: { node: string; name: string; checkpoint_sha256: string; checkpoint_sha256_from: 'declared' };
|
|
28
|
+
params: typeof NODE_PARAMS & { resolution: number; threshold: number; mask_rule: 'red-above-threshold'; };
|
|
29
|
+
graph: { id: typeof MATTE_GRAPH_ID; api: Graph; sha256: string };
|
|
30
|
+
gpu: string;
|
|
31
|
+
input_transform: SeeThroughTransform | null;
|
|
32
|
+
input_sha256: string;
|
|
33
|
+
raw_output: Artifact;
|
|
34
|
+
output: Artifact;
|
|
35
|
+
}
|
|
36
|
+
const hash = (bytes: Uint8Array): string => createHash('sha256').update(bytes).digest('hex');
|
|
37
|
+
function fail(code: string, object: string, detail: string): never {
|
|
38
|
+
throw new PartsError([{ code, object, detail }]);
|
|
39
|
+
}
|
|
40
|
+
const record = (v: unknown): v is Record<string, unknown> => typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
41
|
+
const hex = (v: unknown): v is string => typeof v === 'string' && /^[0-9a-f]{64}$/.test(v);
|
|
42
|
+
|
|
43
|
+
/** Only the isolated-probe winner is admitted; an enum is not a runnable-node receipt. */
|
|
44
|
+
export function matteSelection(node: string | undefined, model: string | undefined): void {
|
|
45
|
+
if (node !== MATTE_NODE || model !== MATTE_MODEL) fail('MATTE_MODEL', '--node / --model', `got ${JSON.stringify(node)} / ${JSON.stringify(model)}; explicitly name ${MATTE_NODE} / ${MATTE_MODEL}, the supported graph; no model is guessed`);
|
|
46
|
+
}
|
|
47
|
+
export function matteParams(resolution: number, threshold: number): MatteReceipt['params'] {
|
|
48
|
+
if (!Number.isSafeInteger(resolution) || resolution < 64 || resolution > 2048 || resolution % 8 !== 0 || !Number.isInteger(threshold) || threshold < 0 || threshold > 254) {
|
|
49
|
+
fail('MATTE_PARAMS', '--resolution / --threshold', `got ${resolution} / ${threshold}; resolution must be a multiple of 8 in [64,2048], threshold an integer in [0,254]`);
|
|
50
|
+
}
|
|
51
|
+
return { ...NODE_PARAMS, resolution, threshold, mask_rule: 'red-above-threshold' };
|
|
52
|
+
}
|
|
53
|
+
export function matteGraph(image: string, prefix: string): Graph {
|
|
54
|
+
return {
|
|
55
|
+
'1': { class_type: 'LoadImage', inputs: { image } },
|
|
56
|
+
'2': { class_type: MATTE_NODE, inputs: { image: ['1', 0], model: MATTE_MODEL, ...NODE_PARAMS } },
|
|
57
|
+
'3': { class_type: 'MaskToImage', inputs: { mask: ['2', 1] } },
|
|
58
|
+
'4': { class_type: 'SaveImage', inputs: { images: ['3', 0], filename_prefix: prefix } },
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
export function matteGraphHash(graph: Graph): string { return hash(new TextEncoder().encode(JSON.stringify(graph))); }
|
|
62
|
+
|
|
63
|
+
/** Model output is grayscale and opaque. Thresholding is the sole classification. */
|
|
64
|
+
export function matteMask(raw: Raster, width: number, height: number, threshold: number, transform: SeeThroughTransform | null): Raster {
|
|
65
|
+
if ((transform === null && (raw.width !== width || raw.height !== height)) || (transform !== null && !isSeeThroughTransform(transform, width, height, raw.width, raw.height))) {
|
|
66
|
+
fail('MATTE_GRID', 'model mask / input_transform', `model grid ${raw.width}x${raw.height}, painting ${width}x${height}; an identical grid or valid recorded square transform is required`);
|
|
67
|
+
}
|
|
68
|
+
let inside: Uint8Array = new Uint8Array(raw.width * raw.height);
|
|
69
|
+
for (let p = 0; p < inside.length; p++) {
|
|
70
|
+
const i = p * 4;
|
|
71
|
+
if (raw.data[i] !== raw.data[i + 1] || raw.data[i] !== raw.data[i + 2] || raw.data[i + 3] !== 255) fail('MATTE_VALUES', 'model-mask.png', `pixel ${p} is not opaque grayscale; MaskToImage output is required`);
|
|
72
|
+
inside[p] = raw.data[i] > threshold ? 1 : 0;
|
|
73
|
+
}
|
|
74
|
+
if (transform !== null) inside = inverseSeeThroughMask(inside, transform);
|
|
75
|
+
if (!inside.some(v => v === 1)) fail('MATTE_EMPTY', 'mask.png', 'has no foreground pixel after threshold and inverse mapping; a nonempty model mask is required');
|
|
76
|
+
const out = newRaster(width, height);
|
|
77
|
+
for (let p = 0; p < inside.length; p++) out.data.set([inside[p] * 255, inside[p] * 255, inside[p] * 255, 255], p * 4);
|
|
78
|
+
return out;
|
|
79
|
+
}
|
|
80
|
+
export function assertMatteOutFree(out: string): void {
|
|
81
|
+
if (existsSync(out) && (!statSync(out).isDirectory() || readdirSync(out).length !== 0)) fail('MATTE_OUT_EMPTY', out, 'is a file or nonempty directory; an absent or empty output is required');
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Validate the complete receipt before trusting any cast or output file. */
|
|
85
|
+
function parseReceipt(value: unknown, file: string): MatteReceipt {
|
|
86
|
+
if (!record(value) || Object.keys(value).join(',') !== MATTE_KEYS.join(',') || value.spec !== MATTE_SPEC ||
|
|
87
|
+
!record(value.painting) || typeof value.painting.path !== 'string' || value.painting.path === '' || !hex(value.painting.sha256) ||
|
|
88
|
+
!record(value.model) || value.model.node !== MATTE_NODE || value.model.name !== MATTE_MODEL || !hex(value.model.checkpoint_sha256) || value.model.checkpoint_sha256_from !== 'declared' ||
|
|
89
|
+
!record(value.params) || !record(value.graph) || value.graph.id !== MATTE_GRAPH_ID || !record(value.graph.api) || !hex(value.graph.sha256) ||
|
|
90
|
+
typeof value.gpu !== 'string' || value.gpu === '' || !hex(value.input_sha256)) {
|
|
91
|
+
fail('MATTE_RECEIPT', file, `requires the complete ordered ${MATTE_SPEC} receipt, painting, model, declared checkpoint hash, params, graph and GPU; legacy or unbound masks are refused`);
|
|
92
|
+
}
|
|
93
|
+
const params = matteParams(value.params.resolution as number, value.params.threshold as number);
|
|
94
|
+
if (JSON.stringify(value.params) !== JSON.stringify(params)) fail('MATTE_RECEIPT', file, 'params differ from the fixed node graph and recorded threshold rule; re-run comfy matte');
|
|
95
|
+
for (const [field, expected] of [['raw_output', 'model-mask.png'], ['output', 'mask.png']] as const) {
|
|
96
|
+
const a = value[field];
|
|
97
|
+
if (!record(a) || a.path !== expected || !hex(a.sha256) || !Number.isSafeInteger(a.width) || !Number.isSafeInteger(a.height) || (a.width as number) <= 0 || (a.height as number) <= 0) fail('MATTE_RECEIPT', file, `${field} requires fixed path ${expected}, lowercase sha256 and positive integer dimensions`);
|
|
98
|
+
}
|
|
99
|
+
const api = value.graph.api;
|
|
100
|
+
if (!record(api['1']) || !record(api['1'].inputs) || typeof api['1'].inputs.image !== 'string' ||
|
|
101
|
+
!record(api['4']) || !record(api['4'].inputs) || typeof api['4'].inputs.filename_prefix !== 'string' ||
|
|
102
|
+
JSON.stringify(api) !== JSON.stringify(matteGraph(api['1'].inputs.image, api['4'].inputs.filename_prefix)) ||
|
|
103
|
+
value.graph.sha256 !== matteGraphHash(api as Graph)) fail('MATTE_RECEIPT', file, 'submitted graph or its sha256 differs from the versioned matte graph');
|
|
104
|
+
return value as unknown as MatteReceipt;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Bind supplied painting bytes, both saved masks, geometry and the exact derivation. */
|
|
108
|
+
export function readMatteSource(dir: string, paintingBytes: Uint8Array | undefined, width: number, height: number): Raster {
|
|
109
|
+
const file = join(dir, 'receipt.json');
|
|
110
|
+
let value: unknown;
|
|
111
|
+
try { value = JSON.parse(readFileSync(file, 'utf8')); }
|
|
112
|
+
catch { return fail('MATTE_RECEIPT', file, 'cannot read a JSON receipt; re-run comfy matte on the painting'); }
|
|
113
|
+
const r = parseReceipt(value, file);
|
|
114
|
+
if (paintingBytes === undefined || hash(paintingBytes) !== r.painting.sha256) fail('MATTE_PAINTING', file, `supplied painting sha256 ${paintingBytes === undefined ? 'absent' : hash(paintingBytes)} differs from recorded ${r.painting.sha256}; use the painting this matte run read`);
|
|
115
|
+
const source = decodePngBytes(paintingBytes, 'painting');
|
|
116
|
+
if (source.width !== width || source.height !== height) fail('MATTE_GRID', file, `supplied painting decodes as ${source.width}x${source.height}, requested ${width}x${height}; identical dimensions are required`);
|
|
117
|
+
const artifacts: Raster[] = [];
|
|
118
|
+
for (const a of [r.raw_output, r.output]) {
|
|
119
|
+
let bytes: Uint8Array;
|
|
120
|
+
try { bytes = new Uint8Array(readFileSync(join(dir, a.path))); }
|
|
121
|
+
catch { return fail('MATTE_HASH', a.path, 'recorded mask file is absent; re-run comfy matte'); }
|
|
122
|
+
if (hash(bytes) !== a.sha256) fail('MATTE_HASH', a.path, `file sha256 ${hash(bytes)} differs from recorded ${a.sha256}; re-run comfy matte`);
|
|
123
|
+
const raster = decodePngBytes(bytes, a.path);
|
|
124
|
+
if (raster.width !== a.width || raster.height !== a.height) fail('MATTE_GRID', a.path, `decoded ${raster.width}x${raster.height} differs from recorded ${a.width}x${a.height}`);
|
|
125
|
+
artifacts.push(raster);
|
|
126
|
+
}
|
|
127
|
+
// Recreate preparation, including its square fast path, using the recorded resolution.
|
|
128
|
+
const prepared = prepareMatteInput(source, r.params.resolution, 'rgb-black-opaque');
|
|
129
|
+
const inputBytes = prepared.transform === undefined ? paintingBytes : encodePngBytes(prepared.image);
|
|
130
|
+
if (hash(inputBytes) !== r.input_sha256 || JSON.stringify(prepared.transform ?? null) !== JSON.stringify(r.input_transform)) fail('MATTE_GRID', file, 'recorded input hash or transform differs from preparation of the bound painting; re-run comfy matte');
|
|
131
|
+
if (artifacts[0].width !== prepared.image.width || artifacts[0].height !== prepared.image.height) fail('MATTE_GRID', 'model-mask.png', `requires prepared input grid ${prepared.image.width}x${prepared.image.height}; got ${artifacts[0].width}x${artifacts[0].height}`);
|
|
132
|
+
const expected = matteMask(artifacts[0], width, height, r.params.threshold, r.input_transform);
|
|
133
|
+
const final = artifacts[1];
|
|
134
|
+
if (final.width !== width || final.height !== height || !expected.data.every((v, i) => v === final.data[i])) fail('MATTE_VALUES', 'mask.png', 'differs from the recorded raw-mask threshold and inverse mapping; no pixel repair is accepted');
|
|
135
|
+
return final;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Stage, verify against frozen input bytes, then publish without replacing prior output. */
|
|
139
|
+
export function publishMatte(out: string, paintingPath: string, paintingBytes: Uint8Array, rawBytes: Uint8Array, receipt: Omit<MatteReceipt, 'spec' | 'painting' | 'raw_output' | 'output'>): MatteReceipt {
|
|
140
|
+
assertMatteOutFree(out);
|
|
141
|
+
const painting = decodePngBytes(paintingBytes, paintingPath);
|
|
142
|
+
const raw = decodePngBytes(rawBytes, 'model-mask.png');
|
|
143
|
+
const mask = matteMask(raw, painting.width, painting.height, receipt.params.threshold, receipt.input_transform);
|
|
144
|
+
const maskBytes = encodePngBytes(mask);
|
|
145
|
+
const parent = dirname(resolve(out));
|
|
146
|
+
mkdirSync(parent, { recursive: true });
|
|
147
|
+
const stage = mkdtempSync(join(parent, `.${basename(resolve(out))}.partial-`));
|
|
148
|
+
try {
|
|
149
|
+
const r: MatteReceipt = {
|
|
150
|
+
spec: MATTE_SPEC, painting: { path: relative(resolve(out), resolve(paintingPath)).split('\\').join('/'), sha256: hash(paintingBytes) },
|
|
151
|
+
...receipt,
|
|
152
|
+
raw_output: { path: 'model-mask.png', sha256: hash(rawBytes), width: raw.width, height: raw.height },
|
|
153
|
+
output: { path: 'mask.png', sha256: hash(maskBytes), width: mask.width, height: mask.height },
|
|
154
|
+
};
|
|
155
|
+
writeFileSync(join(stage, 'model-mask.png'), rawBytes);
|
|
156
|
+
writeFileSync(join(stage, 'mask.png'), maskBytes);
|
|
157
|
+
writeFileSync(join(stage, 'receipt.json'), `${JSON.stringify(r, null, 2)}\n`);
|
|
158
|
+
readMatteSource(stage, paintingBytes, painting.width, painting.height);
|
|
159
|
+
assertMatteOutFree(out);
|
|
160
|
+
try {
|
|
161
|
+
if (existsSync(out)) rmdirSync(out);
|
|
162
|
+
renameSync(stage, out);
|
|
163
|
+
} catch (error) {
|
|
164
|
+
if (['ENOTEMPTY', 'EEXIST', 'ENOTDIR', 'EISDIR'].includes((error as NodeJS.ErrnoException).code ?? '')) {
|
|
165
|
+
fail('MATTE_OUT_EMPTY', out, 'became a file or nonempty directory before publication; an absent or empty output is required');
|
|
166
|
+
}
|
|
167
|
+
throw error;
|
|
168
|
+
}
|
|
169
|
+
return r;
|
|
170
|
+
} finally { if (existsSync(stage)) rmSync(stage, { recursive: true, force: true }); }
|
|
171
|
+
}
|
package/src/plate.ts
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
* inpainting run, lives in `src/comfy/plate.ts` and carries the graphs this
|
|
8
8
|
* file builds.
|
|
9
9
|
*
|
|
10
|
-
* 🔒 **The mask comes from a source, never a hand polygon.**
|
|
10
|
+
* 🔒 **The mask comes from a source, never a hand polygon.** Three sources are
|
|
11
11
|
* read, and nothing else:
|
|
12
12
|
*
|
|
13
13
|
* - `seethrough:<dir>`: a See-through run in the wrapper form (`layers.json`
|
|
@@ -15,7 +15,9 @@
|
|
|
15
15
|
* `OPAQUE_ALPHA_ABOVE`, on the run's own canvas;
|
|
16
16
|
* - `figure-mask:<dir>`: an assemble output whose `parts.json` declares a
|
|
17
17
|
* `scene`, the figure being the `figure_rig.png` assemble wrote beside it
|
|
18
|
-
* (opaque white inside the figure, opaque black outside)
|
|
18
|
+
* (opaque white inside the figure, opaque black outside);
|
|
19
|
+
* - `matte:<dir>`: a model mask whose receipt binds both raw and final masks
|
|
20
|
+
* to the exact painting bytes and the recorded threshold/transform.
|
|
19
21
|
*
|
|
20
22
|
* The mask is taken on its own grid and never fitted: a source whose grid is
|
|
21
23
|
* not the painting's size is refused by name. With no source the command
|
|
@@ -26,13 +28,15 @@
|
|
|
26
28
|
* of every file it names; `verifyPlateReceipt` reads it back against the bytes
|
|
27
29
|
* on disk. Every path in it is relative to the receipt's own directory.
|
|
28
30
|
*/
|
|
31
|
+
import { readMatteSource } from './matte.ts';
|
|
32
|
+
import { isSeeThroughTransform, inverseSeeThroughMask } from './seethrough-transform.ts';
|
|
29
33
|
import { createHash } from 'node:crypto';
|
|
30
|
-
import { existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmdirSync, rmSync, statSync, writeFileSync } from 'node:fs';
|
|
34
|
+
import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, renameSync, rmdirSync, rmSync, statSync, writeFileSync } from 'node:fs';
|
|
31
35
|
import { basename, dirname, join, relative, resolve } from 'node:path';
|
|
32
36
|
import { sourceInRig } from './assemble.ts';
|
|
33
37
|
import { PartsError, type Problem } from './errors.ts';
|
|
34
38
|
import type { Graph } from './graphs.ts';
|
|
35
|
-
import { OPAQUE_ALPHA_ABOVE, readWrapperLayers } from './layers.ts';
|
|
39
|
+
import { type LayerSet, OPAQUE_ALPHA_ABOVE, readWrapperLayers } from './layers.ts';
|
|
36
40
|
import { FIGURE_MASK_FILE, PAINTING_IN_RIG_FILE } from './parts.ts';
|
|
37
41
|
import { decodePngBytes, encodePngBytes } from './raster/png.ts';
|
|
38
42
|
import { newRaster, type Raster } from './raster/types.ts';
|
|
@@ -47,7 +51,7 @@ export const PLATE_RECEIPT_KEYS = ['spec', 'mode', 'painting', 'mask', 'model',
|
|
|
47
51
|
|
|
48
52
|
export type PlateMode = 'background' | 'foreground';
|
|
49
53
|
export type PlateGraph = 'lama' | 'core';
|
|
50
|
-
export type MaskSource = 'seethrough' | 'figure-mask';
|
|
54
|
+
export type MaskSource = 'seethrough' | 'figure-mask' | 'matte';
|
|
51
55
|
|
|
52
56
|
/** The graph ids a receipt names: one per background graph, and the fixed local id of the foreground derivation. */
|
|
53
57
|
export const PLATE_GRAPH_IDS = {
|
|
@@ -91,10 +95,10 @@ export function declaredChecksum(value: string | undefined): string {
|
|
|
91
95
|
/** `--mask-from <source>:<dir>`, parsed; an absent or unknown source is refused by name. */
|
|
92
96
|
export function parseMaskFrom(value: string | undefined): { source: MaskSource; dir: string } {
|
|
93
97
|
if (value === undefined) {
|
|
94
|
-
fail('PLATE_MASK_SOURCE', '--mask-from', 'is absent; a source mask is required — seethrough:<run dir> (run `rig-parts comfy seethrough` on the painting) or figure-mask:<assemble out dir> (run `assemble` with config.scene); a hand-drawn mask is not taken');
|
|
98
|
+
fail('PLATE_MASK_SOURCE', '--mask-from', 'is absent; a source mask is required — seethrough:<run dir> (run `rig-parts comfy seethrough` on the painting) or figure-mask:<assemble out dir> (run `assemble` with config.scene), or matte:<matte out dir> (run `rig-parts comfy matte`); a hand-drawn mask is not taken');
|
|
95
99
|
}
|
|
96
|
-
const m = /^(seethrough|figure-mask):(.+)$/.exec(value);
|
|
97
|
-
if (m === null) fail('PLATE_MASK_SOURCE', '--mask-from', `is ${JSON.stringify(value)}; seethrough:<dir
|
|
100
|
+
const m = /^(seethrough|figure-mask|matte):(.+)$/.exec(value);
|
|
101
|
+
if (m === null) fail('PLATE_MASK_SOURCE', '--mask-from', `is ${JSON.stringify(value)}; seethrough:<dir>, figure-mask:<dir> or matte:<dir> is required`);
|
|
98
102
|
return { source: m[1] as MaskSource, dir: m[2] };
|
|
99
103
|
}
|
|
100
104
|
|
|
@@ -105,15 +109,62 @@ export interface FigureMask {
|
|
|
105
109
|
inside: Uint8Array;
|
|
106
110
|
}
|
|
107
111
|
|
|
108
|
-
function maskRecord(path: string, code: string): { record: Record<string, unknown>; bytes: Uint8Array } {
|
|
112
|
+
function maskRecord(path: string, code: string, remedy = 're-run assemble'): { record: Record<string, unknown>; bytes: Uint8Array } {
|
|
109
113
|
try {
|
|
110
114
|
const bytes = new Uint8Array(readFileSync(path));
|
|
111
115
|
const value: unknown = JSON.parse(new TextDecoder().decode(bytes));
|
|
112
116
|
if (typeof value === 'object' && value !== null && !Array.isArray(value)) return { record: value as Record<string, unknown>, bytes };
|
|
113
117
|
} catch (err) {
|
|
114
|
-
fail(code, path, `cannot read a JSON object (${(err as Error).message});
|
|
118
|
+
fail(code, path, `cannot read a JSON object (${(err as Error).message}); ${remedy}`);
|
|
115
119
|
}
|
|
116
|
-
return fail(code, path,
|
|
120
|
+
return fail(code, path, `requires a JSON object; ${remedy}`);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* comfy seethrough writes meta.json with image_sha256 and per-layer {path, sha256}.
|
|
125
|
+
* A missing or legacy meta without hashes is refused by name, every layer file
|
|
126
|
+
* readWrapperLayers read must match its recorded entry in meta.json, and the
|
|
127
|
+
* supplied --painting bytes must match image_sha256.
|
|
128
|
+
*/
|
|
129
|
+
function bindSeeThroughSource(dir: string, set: LayerSet, paintingBytes: Uint8Array | undefined): Record<string, unknown> {
|
|
130
|
+
const root = resolve(dir);
|
|
131
|
+
const metaPath = join(root, 'meta.json');
|
|
132
|
+
const { record: meta } = maskRecord(metaPath, 'PLATE_MASK_RUN', 're-run comfy seethrough');
|
|
133
|
+
if (typeof meta.image_sha256 !== 'string' || !/^[0-9a-f]{64}$/.test(meta.image_sha256) || !Array.isArray(meta.layers) || meta.layers.length === 0) {
|
|
134
|
+
fail('PLATE_MASK_RUN', metaPath, 'requires lowercase 64-hex image_sha256 and non-empty layers [{path, sha256}] from comfy seethrough; legacy or unstamped runs are refused — re-run comfy seethrough');
|
|
135
|
+
}
|
|
136
|
+
const entries = meta.layers as unknown[];
|
|
137
|
+
for (let i = 0; i < entries.length; i++) {
|
|
138
|
+
const e = entries[i];
|
|
139
|
+
if (typeof e !== 'object' || e === null || !('path' in e) || typeof e.path !== 'string' || e.path === '' ||
|
|
140
|
+
!('sha256' in e) || typeof e.sha256 !== 'string' || !/^[0-9a-f]{64}$/.test(e.sha256)) {
|
|
141
|
+
fail('PLATE_MASK_RUN', metaPath, `layers[${i}] requires a relative path and lowercase 64-hex sha256; re-run comfy seethrough`);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
const typedEntries = entries as Array<{ path: string; sha256: string }>;
|
|
145
|
+
const readFiles = [...set.layers, ...set.hidden];
|
|
146
|
+
for (const layer of readFiles) {
|
|
147
|
+
if (layer.file === null) {
|
|
148
|
+
fail('PLATE_MASK_RUN', metaPath, `layer "${layer.name}" has no file on disk; re-run comfy seethrough`);
|
|
149
|
+
}
|
|
150
|
+
const key = relative(root, resolve(layer.file)).split('\\').join('/');
|
|
151
|
+
const matches = typedEntries.filter((e) => e.path === key);
|
|
152
|
+
const actual = sha256(new Uint8Array(readFileSync(layer.file)));
|
|
153
|
+
if (matches.length !== 1 || matches[0].sha256 !== actual) {
|
|
154
|
+
fail('PLATE_MASK_RUN', layer.file, `sha256 is ${actual}; ${metaPath} must record exactly one layers entry for ${JSON.stringify(key)} with that hash (found ${matches.length}, hashes ${JSON.stringify(matches.map((e) => e.sha256))}); re-run comfy seethrough`);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
if (typedEntries.length !== readFiles.length) {
|
|
158
|
+
fail('PLATE_MASK_RUN', metaPath, `records ${typedEntries.length} layer hash(es), while layers.json has ${readFiles.length}; re-run comfy seethrough`);
|
|
159
|
+
}
|
|
160
|
+
if (paintingBytes === undefined) {
|
|
161
|
+
fail('PLATE_MASK_PAINTING', metaPath, 'the supplied painting bytes are required to compare with meta.json image_sha256');
|
|
162
|
+
}
|
|
163
|
+
const actualPainting = sha256(paintingBytes);
|
|
164
|
+
if (actualPainting !== meta.image_sha256) {
|
|
165
|
+
fail('PLATE_MASK_PAINTING', metaPath, `records image_sha256 ${meta.image_sha256}; the supplied painting has sha256 ${actualPainting} — use the painting this comfy seethrough run read`);
|
|
166
|
+
}
|
|
167
|
+
return meta;
|
|
117
168
|
}
|
|
118
169
|
|
|
119
170
|
/**
|
|
@@ -174,29 +225,40 @@ function bindFigureSource(dir: string, parts: Record<string, unknown>, partsByte
|
|
|
174
225
|
* `width`x`height`, a `figure_rig.png` that is not the two values assemble
|
|
175
226
|
* writes, and a mask with no figure pixel.
|
|
176
227
|
*/
|
|
177
|
-
export function readSourceMask(source: MaskSource, dir: string, width: number, height: number, painting?: Raster): FigureMask {
|
|
228
|
+
export function readSourceMask(source: MaskSource, dir: string, width: number, height: number, painting?: Raster, paintingBytes?: Uint8Array): FigureMask {
|
|
178
229
|
if (!existsSync(dir) || !statSync(dir).isDirectory()) {
|
|
179
|
-
fail('PLATE_MASK_SOURCE', `${source}:${dir}`, `is not a directory; ${source === 'seethrough' ? 'a See-through run directory (from `rig-parts comfy seethrough`)' : 'an assemble output directory with parts.json and figure_rig.png'} is required`);
|
|
230
|
+
fail('PLATE_MASK_SOURCE', `${source}:${dir}`, `is not a directory; ${source === 'seethrough' ? 'a See-through run directory (from `rig-parts comfy seethrough`)' : source === 'matte' ? 'a comfy matte directory with receipt.json, model-mask.png and mask.png' : 'an assemble output directory with parts.json and figure_rig.png'} is required`);
|
|
180
231
|
}
|
|
181
|
-
|
|
232
|
+
if (source === 'matte') {
|
|
233
|
+
const mask = readMatteSource(dir, paintingBytes, width, height);
|
|
234
|
+
return { width, height, inside: Uint8Array.from({ length: width * height }, (_, p) => mask.data[p * 4] === 255 ? 1 : 0) };
|
|
235
|
+
}
|
|
236
|
+
let inside: Uint8Array = new Uint8Array(width * height);
|
|
182
237
|
if (source === 'seethrough') {
|
|
183
238
|
if (!existsSync(join(dir, 'layers.json'))) fail('PLATE_MASK_SOURCE', `${source}:${dir}`, 'holds no layers.json; the figure layers of a See-through run are required — run `rig-parts comfy seethrough` on the painting first');
|
|
184
239
|
const set = readWrapperLayers(dir);
|
|
185
|
-
|
|
186
|
-
|
|
240
|
+
const meta = bindSeeThroughSource(dir, set, paintingBytes);
|
|
241
|
+
const transform = meta.input_transform;
|
|
242
|
+
const mapped = transform !== undefined && isSeeThroughTransform(transform, width, height, set.canvas.w, set.canvas.h);
|
|
243
|
+
if ((transform !== undefined && !mapped) || (!mapped && (set.canvas.w !== width || set.canvas.h !== height))) {
|
|
244
|
+
fail('PLATE_MASK_GRID', `${source}:${dir}`, `is a ${set.canvas.w}x${set.canvas.h} canvas; the painting's ${width}x${height} is required — a valid recorded input_transform is required for another grid; re-run comfy seethrough on the painting`);
|
|
187
245
|
}
|
|
246
|
+
const maskWidth = set.canvas.w;
|
|
247
|
+
const maskHeight = set.canvas.h;
|
|
248
|
+
inside = new Uint8Array(maskWidth * maskHeight);
|
|
188
249
|
for (const l of set.layers) {
|
|
189
250
|
const w = l.right - l.left;
|
|
190
251
|
for (let y = 0; y < l.bottom - l.top; y++) {
|
|
191
252
|
const cy = l.top + y;
|
|
192
|
-
if (cy < 0 || cy >=
|
|
253
|
+
if (cy < 0 || cy >= maskHeight) continue;
|
|
193
254
|
for (let x = 0; x < w; x++) {
|
|
194
255
|
const cx = l.left + x;
|
|
195
|
-
if (cx < 0 || cx >=
|
|
196
|
-
if (l.pixels.data[(y * w + x) * 4 + 3] > OPAQUE_ALPHA_ABOVE) inside[cy *
|
|
256
|
+
if (cx < 0 || cx >= maskWidth) continue;
|
|
257
|
+
if (l.pixels.data[(y * w + x) * 4 + 3] > OPAQUE_ALPHA_ABOVE) inside[cy * maskWidth + cx] = 1;
|
|
197
258
|
}
|
|
198
259
|
}
|
|
199
260
|
}
|
|
261
|
+
if (mapped) inside = inverseSeeThroughMask(inside, transform);
|
|
200
262
|
} else {
|
|
201
263
|
const partsPath = join(dir, 'parts.json');
|
|
202
264
|
if (!existsSync(partsPath)) fail('PLATE_MASK_SOURCE', `${source}:${dir}`, 'holds no parts.json; an assemble output with config.scene is required — run `assemble` on the painting first');
|
|
@@ -440,6 +502,8 @@ export interface PlateWrite {
|
|
|
440
502
|
gpu: string | null;
|
|
441
503
|
}
|
|
442
504
|
|
|
505
|
+
let foregroundStageSeq = 0;
|
|
506
|
+
|
|
443
507
|
/**
|
|
444
508
|
* Write `mask.png`, `plate.png` and `receipt.json` into a staging directory
|
|
445
509
|
* beside `out`, read the receipt back against the bytes on disk, and only
|
|
@@ -449,8 +513,7 @@ export interface PlateWrite {
|
|
|
449
513
|
export function publishPlate(w: PlateWrite): PlateReceipt {
|
|
450
514
|
const parent = dirname(resolve(w.out));
|
|
451
515
|
mkdirSync(parent, { recursive: true });
|
|
452
|
-
const stage = join(parent, `.${basename(resolve(w.out))}.partial-${w.stageTag}
|
|
453
|
-
mkdirSync(stage, { recursive: true });
|
|
516
|
+
const stage = mkdtempSync(join(parent, `.${basename(resolve(w.out))}.partial-${w.stageTag}-`));
|
|
454
517
|
try {
|
|
455
518
|
const mask = pngWithHash(maskImage(w.mask));
|
|
456
519
|
const plate = pngWithHash(w.plate);
|
|
@@ -470,8 +533,17 @@ export function publishPlate(w: PlateWrite): PlateReceipt {
|
|
|
470
533
|
writeFileSync(join(stage, PLATE_RECEIPT_FILE), receiptText(receipt));
|
|
471
534
|
const problems = verifyPlateReceipt(join(stage, PLATE_RECEIPT_FILE));
|
|
472
535
|
if (problems.length > 0) throw new PartsError(problems);
|
|
473
|
-
|
|
474
|
-
|
|
536
|
+
assertPlateOutFree(w.out);
|
|
537
|
+
try {
|
|
538
|
+
if (existsSync(w.out)) rmdirSync(w.out);
|
|
539
|
+
renameSync(stage, w.out);
|
|
540
|
+
} catch (err) {
|
|
541
|
+
const code = (err as NodeJS.ErrnoException).code;
|
|
542
|
+
if (code === 'ENOTEMPTY' || code === 'EEXIST' || code === 'ENOTDIR' || code === 'EISDIR') {
|
|
543
|
+
fail('PLATE_OUT_EMPTY', w.out, 'is a file or a directory that holds entries; an absent or empty directory is required');
|
|
544
|
+
}
|
|
545
|
+
throw err;
|
|
546
|
+
}
|
|
475
547
|
return receipt;
|
|
476
548
|
} finally {
|
|
477
549
|
if (existsSync(stage)) rmSync(stage, { recursive: true, force: true });
|
|
@@ -501,11 +573,11 @@ export interface ForegroundRun {
|
|
|
501
573
|
export function runForeground(run: ForegroundRun): PlateReceipt {
|
|
502
574
|
const from = parseMaskFrom(run.maskFrom);
|
|
503
575
|
const painting = readPlatePainting(run.painting);
|
|
504
|
-
const mask = readSourceMask(from.source, from.dir, painting.raster.width, painting.raster.height, painting.raster);
|
|
576
|
+
const mask = readSourceMask(from.source, from.dir, painting.raster.width, painting.raster.height, painting.raster, painting.bytes);
|
|
505
577
|
assertPlateOutFree(run.out);
|
|
506
578
|
return publishPlate({
|
|
507
579
|
out: run.out,
|
|
508
|
-
stageTag:
|
|
580
|
+
stageTag: `foreground-${process.pid}-${++foregroundStageSeq}`,
|
|
509
581
|
mode: 'foreground',
|
|
510
582
|
paintingPath: run.painting,
|
|
511
583
|
paintingBytes: painting.bytes,
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { newRaster, type Raster } from './raster/types.ts';
|
|
2
|
+
import { resize } from './raster/resize.ts';
|
|
3
|
+
|
|
4
|
+
export type SeeThroughPadFill = 'rgba-zero' | 'rgb-black-opaque';
|
|
5
|
+
|
|
6
|
+
/** image_sha256 in the enclosing run record binds the source PNG bytes. */
|
|
7
|
+
export interface SeeThroughTransform {
|
|
8
|
+
source: [number, number];
|
|
9
|
+
padded: [number, number];
|
|
10
|
+
pad: [number, number];
|
|
11
|
+
scale: number;
|
|
12
|
+
resample: 'parts-lanczos3-rgba';
|
|
13
|
+
pad_fill: SeeThroughPadFill;
|
|
14
|
+
target: [number, number];
|
|
15
|
+
inverse: 'nearest-pixel-centres';
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Prepare only non-square images. Square inputs keep their original upload bytes. */
|
|
19
|
+
export function prepareSeeThroughInput(
|
|
20
|
+
source: Raster,
|
|
21
|
+
resolution: number,
|
|
22
|
+
padFill: SeeThroughPadFill,
|
|
23
|
+
): { image: Raster; transform?: SeeThroughTransform } {
|
|
24
|
+
if (source.width === source.height) return { image: source };
|
|
25
|
+
const side = Math.max(source.width, source.height);
|
|
26
|
+
const left = Math.floor((side - source.width) / 2);
|
|
27
|
+
const top = Math.floor((side - source.height) / 2);
|
|
28
|
+
const padded = newRaster(side, side);
|
|
29
|
+
for (let y = 0; y < source.height; y++) {
|
|
30
|
+
padded.data.set(source.data.subarray(y * source.width * 4, (y + 1) * source.width * 4), ((y + top) * side + left) * 4);
|
|
31
|
+
}
|
|
32
|
+
return {
|
|
33
|
+
image: resize(padded, resolution, resolution, 'lanczos3'),
|
|
34
|
+
transform: {
|
|
35
|
+
source: [source.width, source.height], padded: [side, side], pad: [left, top],
|
|
36
|
+
scale: resolution / side, resample: 'parts-lanczos3-rgba', pad_fill: padFill, target: [resolution, resolution], inverse: 'nearest-pixel-centres',
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Validate all geometry before allocating or indexing a mapped mask. Offsets are read, never inferred. */
|
|
42
|
+
export function isSeeThroughTransform(value: unknown, width: number, height: number, targetWidth: number, targetHeight: number): value is SeeThroughTransform {
|
|
43
|
+
if (typeof value !== 'object' || value === null) return false;
|
|
44
|
+
const v = value as Record<string, unknown>;
|
|
45
|
+
const pair = (p: unknown): p is [number, number] => Array.isArray(p) && p.length === 2 && p.every(n => Number.isSafeInteger(n));
|
|
46
|
+
if (!pair(v.source) || !pair(v.padded) || !pair(v.pad) || !pair(v.target)) return false;
|
|
47
|
+
const side = v.padded[0];
|
|
48
|
+
return v.source[0] === width && v.source[1] === height && width !== height &&
|
|
49
|
+
side === Math.max(width, height) && v.padded[1] === side &&
|
|
50
|
+
v.pad[0] >= 0 && v.pad[1] >= 0 && v.pad[0] + width <= side && v.pad[1] + height <= side &&
|
|
51
|
+
v.target[0] === targetWidth && v.target[1] === targetHeight && targetWidth > 0 && targetWidth === targetHeight &&
|
|
52
|
+
v.scale === targetWidth / side && v.resample === 'parts-lanczos3-rgba' &&
|
|
53
|
+
(v.pad_fill === 'rgba-zero' || v.pad_fill === 'rgb-black-opaque') &&
|
|
54
|
+
v.inverse === 'nearest-pixel-centres';
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Sample the union at transformed source pixel centres. Sub-grid detail cannot be recovered. */
|
|
58
|
+
export function inverseSeeThroughMask(mask: Uint8Array, transform: SeeThroughTransform): Uint8Array {
|
|
59
|
+
const [width, height] = transform.source;
|
|
60
|
+
const [targetWidth, targetHeight] = transform.target;
|
|
61
|
+
const out = new Uint8Array(width * height);
|
|
62
|
+
for (let y = 0; y < height; y++) {
|
|
63
|
+
const ty = Math.min(targetHeight - 1, Math.floor((y + transform.pad[1] + 0.5) * transform.scale));
|
|
64
|
+
for (let x = 0; x < width; x++) {
|
|
65
|
+
const tx = Math.min(targetWidth - 1, Math.floor((x + transform.pad[0] + 0.5) * transform.scale));
|
|
66
|
+
out[y * width + x] = mask[ty * targetWidth + tx];
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return out;
|
|
70
|
+
}
|
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
{
|
|
2
|
-
"_comment": "API-format graph for jtydhr88/ComfyUI-See-through (its node classes and inputs as a box running the wrapper lists them on /object_info; the adapter checks every node and input against the box's own /object_info before it submits). Every %NAME% string is filled by src/graphs.ts fillSeeThrough, and a placeholder nothing fills is refused before submission. The LoadImage 'upload' widget of the UI format is left out: it is not an input /object_info lists. Top-level keys starting with _ are annotations and are dropped before /prompt. SeeThrough_SavePSD writes one PNG per layer and a <prefix>_<timestamp>_layers.json manifest into the box's output directory, and records that manifest's file name in output/seethrough_psd_info.log; the two SaveImage nodes are previews and are what /history lists.",
|
|
2
|
+
"_comment": "API-format graph for jtydhr88/ComfyUI-See-through (its node classes and inputs as a box running the wrapper lists them on /object_info; the adapter checks every node and input against the box's own /object_info before it submits). Every %NAME% string is filled by src/graphs.ts fillSeeThrough, and a placeholder nothing fills is refused before submission. The LoadImage 'upload' widget of the UI format is left out: it is not an input /object_info lists. LoadImage's IMAGE (output 0) and MASK (output 1) rejoin through JoinImageWithAlpha so a 4-channel RGBA image reaches SeeThrough_GenerateLayers. Top-level keys starting with _ are annotations and are dropped before /prompt. SeeThrough_SavePSD writes one PNG per layer and a <prefix>_<timestamp>_layers.json manifest into the box's output directory, and records that manifest's file name in output/seethrough_psd_info.log; the two SaveImage nodes are previews and are what /history lists.",
|
|
3
3
|
"1": {
|
|
4
4
|
"class_type": "LoadImage",
|
|
5
5
|
"inputs": { "image": "%IMAGE%" }
|
|
6
6
|
},
|
|
7
|
+
"10": {
|
|
8
|
+
"class_type": "JoinImageWithAlpha",
|
|
9
|
+
"inputs": { "image": ["1", 0], "alpha": ["1", 1] }
|
|
10
|
+
},
|
|
7
11
|
"2": {
|
|
8
12
|
"class_type": "SeeThrough_LoadLayerDiffModel",
|
|
9
13
|
"inputs": {
|
|
@@ -19,7 +23,7 @@
|
|
|
19
23
|
"3": {
|
|
20
24
|
"class_type": "SeeThrough_GenerateLayers",
|
|
21
25
|
"inputs": {
|
|
22
|
-
"image": ["
|
|
26
|
+
"image": ["10", 0],
|
|
23
27
|
"layerdiff_model": ["2", 0],
|
|
24
28
|
"seed": "%SEED%",
|
|
25
29
|
"resolution": "%RESOLUTION%",
|