spine-parts 2.27.2 → 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 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 plate; got ${sub === undefined ? 'nothing' : `"${sub}"`}`);
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
@@ -5379,18 +5379,21 @@ plate pass from this command is a recorded source step, not a repair.
5379
5379
  ### See-through mask grid mapping
5380
5380
 
5381
5381
  For non-square paintings, `comfy seethrough` prepares the configured square
5382
- before upload and records `input_transform` in `meta.json`, bound to the
5383
- original PNG by `image_sha256`. `seethrough:` samples the union of layer
5384
- alpha masks at transformed painting pixel centres with nearest sampling,
5385
- using the recorded offsets and scale. Exactness holds for representable
5386
- masks, and detail below the See-through grid's resolution is not recovered.
5387
- The model receives the padded area as opaque black RGB because LoadImage
5388
- separates alpha and GenerateLayers adds opaque alpha. The old wrapper-internal
5389
- pad was transparent, so non-square model inputs differ from before.
5390
- Square uploads omit the transform and retain the existing path. A legacy
5391
- run on another grid still refuses `PLATE_MASK_GRID`; regenerate it through
5392
- `comfy seethrough` to obtain a recorded transform. The record fields and
5393
- sampling formula are specified in [STABILITY](STABILITY.md).
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).
5394
5397
 
5395
5398
  | rule | means | change |
5396
5399
  | --- | --- | --- |
@@ -5401,6 +5404,11 @@ sampling formula are specified in [STABILITY](STABILITY.md).
5401
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` |
5402
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 |
5403
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` |
5404
5412
 
5405
5413
  ### assemble
5406
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` | `plate`: `--mode` (`background` or `foreground`), `--painting`, `--mask-from` (`seethrough:<dir>` or `figure-mask:<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) |
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"` or `"figure-mask"`, `from` the source directory, `path` the mask actually used (`mask.png`, opaque white inside) |
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,
@@ -570,19 +608,26 @@ plate` with `--mask-from seethrough:<dir>` binds every layer file read and
570
608
  `--painting` against those hashes and refuses a legacy `meta.json` that lacks
571
609
  them.
572
610
 
573
- Non-square inputs are prepared by parts before upload (issue #1197).
611
+ Non-square inputs are prepared by parts before upload (issues #1197, #1205).
574
612
  Their additive `input_transform` records `source: [width,height]`,
575
613
  `padded: [side,side]`, `pad: [left,top]` in padded pixels, `scale` (target
576
614
  side divided by padded side), `resample: "parts-lanczos3-rgba"`,
577
- `pad_fill: "rgb-black-opaque"`, `target: [resolution,resolution]` and `inverse: "nearest-pixel-centres"`.
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"`.
578
618
  The enclosing `image_sha256` is the single source-image hash. Preparation
579
619
  zero-fills the square, places the source at floor-centred offsets, and runs
580
620
  parts' RGBA Lanczos3 resampler. The wrapper receives the configured square.
581
- LoadImage separates the PNG's alpha from RGB, and GenerateLayers adds opaque
582
- alpha to that RGB. The model therefore receives the padded area as opaque
583
- black RGB. The old wrapper-internal pad was transparent, so non-square model
584
- inputs differ from before.
585
- Square inputs keep the existing upload bytes and omit `input_transform`.
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`.
586
631
  For each source pixel `(x,y)`, the inverse samples the layer union at
587
632
  `floor((x+left+0.5)*scale), floor((y+top+0.5)*scale)`.
588
633
  Exactness holds for representable masks, and detail below the See-through
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "spine-parts",
3
- "version": "2.27.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": {
@@ -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();
@@ -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
+ }
@@ -84,7 +84,7 @@ export async function runSeeThrough(client: ComfyClient, run: SeeThroughRun, say
84
84
  const bytes = new Uint8Array(readFileSync(run.image));
85
85
  const img = decodePngBytes(bytes, run.image);
86
86
  assertOutFree(run.out);
87
- const prepared = prepareSeeThroughInput(img, run.resolution);
87
+ const prepared = prepareSeeThroughInput(img, run.resolution, 'rgba-zero');
88
88
  const uploadBytes = prepared.transform === undefined ? bytes : encodePngBytes(prepared.image);
89
89
 
90
90
  await client.systemStats();
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.** Two sources are
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,6 +28,7 @@
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';
29
32
  import { isSeeThroughTransform, inverseSeeThroughMask } from './seethrough-transform.ts';
30
33
  import { createHash } from 'node:crypto';
31
34
  import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, renameSync, rmdirSync, rmSync, statSync, writeFileSync } from 'node:fs';
@@ -48,7 +51,7 @@ export const PLATE_RECEIPT_KEYS = ['spec', 'mode', 'painting', 'mask', 'model',
48
51
 
49
52
  export type PlateMode = 'background' | 'foreground';
50
53
  export type PlateGraph = 'lama' | 'core';
51
- export type MaskSource = 'seethrough' | 'figure-mask';
54
+ export type MaskSource = 'seethrough' | 'figure-mask' | 'matte';
52
55
 
53
56
  /** The graph ids a receipt names: one per background graph, and the fixed local id of the foreground derivation. */
54
57
  export const PLATE_GRAPH_IDS = {
@@ -92,10 +95,10 @@ export function declaredChecksum(value: string | undefined): string {
92
95
  /** `--mask-from <source>:<dir>`, parsed; an absent or unknown source is refused by name. */
93
96
  export function parseMaskFrom(value: string | undefined): { source: MaskSource; dir: string } {
94
97
  if (value === undefined) {
95
- 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');
96
99
  }
97
- const m = /^(seethrough|figure-mask):(.+)$/.exec(value);
98
- if (m === null) fail('PLATE_MASK_SOURCE', '--mask-from', `is ${JSON.stringify(value)}; seethrough:<dir> or figure-mask:<dir> is required`);
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`);
99
102
  return { source: m[1] as MaskSource, dir: m[2] };
100
103
  }
101
104
 
@@ -224,7 +227,11 @@ function bindFigureSource(dir: string, parts: Record<string, unknown>, partsByte
224
227
  */
225
228
  export function readSourceMask(source: MaskSource, dir: string, width: number, height: number, painting?: Raster, paintingBytes?: Uint8Array): FigureMask {
226
229
  if (!existsSync(dir) || !statSync(dir).isDirectory()) {
227
- 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`);
231
+ }
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) };
228
235
  }
229
236
  let inside: Uint8Array = new Uint8Array(width * height);
230
237
  if (source === 'seethrough') {
@@ -1,6 +1,8 @@
1
1
  import { newRaster, type Raster } from './raster/types.ts';
2
2
  import { resize } from './raster/resize.ts';
3
3
 
4
+ export type SeeThroughPadFill = 'rgba-zero' | 'rgb-black-opaque';
5
+
4
6
  /** image_sha256 in the enclosing run record binds the source PNG bytes. */
5
7
  export interface SeeThroughTransform {
6
8
  source: [number, number];
@@ -8,13 +10,17 @@ export interface SeeThroughTransform {
8
10
  pad: [number, number];
9
11
  scale: number;
10
12
  resample: 'parts-lanczos3-rgba';
11
- pad_fill: 'rgb-black-opaque';
13
+ pad_fill: SeeThroughPadFill;
12
14
  target: [number, number];
13
15
  inverse: 'nearest-pixel-centres';
14
16
  }
15
17
 
16
18
  /** Prepare only non-square images. Square inputs keep their original upload bytes. */
17
- export function prepareSeeThroughInput(source: Raster, resolution: number): { image: Raster; transform?: SeeThroughTransform } {
19
+ export function prepareSeeThroughInput(
20
+ source: Raster,
21
+ resolution: number,
22
+ padFill: SeeThroughPadFill,
23
+ ): { image: Raster; transform?: SeeThroughTransform } {
18
24
  if (source.width === source.height) return { image: source };
19
25
  const side = Math.max(source.width, source.height);
20
26
  const left = Math.floor((side - source.width) / 2);
@@ -27,7 +33,7 @@ export function prepareSeeThroughInput(source: Raster, resolution: number): { im
27
33
  image: resize(padded, resolution, resolution, 'lanczos3'),
28
34
  transform: {
29
35
  source: [source.width, source.height], padded: [side, side], pad: [left, top],
30
- scale: resolution / side, resample: 'parts-lanczos3-rgba', pad_fill: 'rgb-black-opaque', target: [resolution, resolution], inverse: 'nearest-pixel-centres',
36
+ scale: resolution / side, resample: 'parts-lanczos3-rgba', pad_fill: padFill, target: [resolution, resolution], inverse: 'nearest-pixel-centres',
31
37
  },
32
38
  };
33
39
  }
@@ -43,7 +49,9 @@ export function isSeeThroughTransform(value: unknown, width: number, height: num
43
49
  side === Math.max(width, height) && v.padded[1] === side &&
44
50
  v.pad[0] >= 0 && v.pad[1] >= 0 && v.pad[0] + width <= side && v.pad[1] + height <= side &&
45
51
  v.target[0] === targetWidth && v.target[1] === targetHeight && targetWidth > 0 && targetWidth === targetHeight &&
46
- v.scale === targetWidth / side && v.resample === 'parts-lanczos3-rgba' && v.pad_fill === 'rgb-black-opaque' && v.inverse === 'nearest-pixel-centres';
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';
47
55
  }
48
56
 
49
57
  /** Sample the union at transformed source pixel centres. Sub-grid detail cannot be recovered. */
@@ -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": ["1", 0],
26
+ "image": ["10", 0],
23
27
  "layerdiff_model": ["2", 0],
24
28
  "seed": "%SEED%",
25
29
  "resolution": "%RESOLUTION%",