spine-parts 2.27.1 → 2.27.2

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/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. `--checkpoint-sha256` (64 hex characters) is required: the box
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,11 +5376,27 @@ 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` 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).
5394
+
5377
5395
  | rule | means | change |
5378
5396
  | --- | --- | --- |
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 than the painting; a `figure_rig.png` holding other than opaque white and black; a mask with no figure pixel | run `comfy seethrough` on the painting, or `assemble` with `config.scene`, at the painting's own size |
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` to regenerate `parts.json` and `painting_in_rig.png` together |
5397
+ | <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 |
5398
+ | <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 |
5399
+ | <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
5400
  | <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
5401
  | <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
5402
  | <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 |
package/docs/STABILITY.md CHANGED
@@ -562,6 +562,34 @@ Painting generation metadata includes `upscale: {model, then}` (issue #698),
562
562
  read from the same `UPSCALE_MODEL` and `UPSCALE_THEN` constants as the graph.
563
563
  This adds provenance; it does not change model selection.
564
564
 
565
+ See-through run metadata (`meta.json`, written by `comfy seethrough`) includes
566
+ `image_sha256` (the SHA-256 of the original `--image` PNG bytes) and `layers`
567
+ (each written layer PNG's `{path, sha256}`, `path` relative to the run
568
+ directory) (issue #1188, additive under *What "stable" means here*). `comfy
569
+ plate` with `--mask-from seethrough:<dir>` binds every layer file read and
570
+ `--painting` against those hashes and refuses a legacy `meta.json` that lacks
571
+ them.
572
+
573
+ Non-square inputs are prepared by parts before upload (issue #1197).
574
+ Their additive `input_transform` records `source: [width,height]`,
575
+ `padded: [side,side]`, `pad: [left,top]` in padded pixels, `scale` (target
576
+ side divided by padded side), `resample: "parts-lanczos3-rgba"`,
577
+ `pad_fill: "rgb-black-opaque"`, `target: [resolution,resolution]` and `inverse: "nearest-pixel-centres"`.
578
+ The enclosing `image_sha256` is the single source-image hash. Preparation
579
+ zero-fills the square, places the source at floor-centred offsets, and runs
580
+ 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`.
586
+ For each source pixel `(x,y)`, the inverse samples the layer union at
587
+ `floor((x+left+0.5)*scale), floor((y+top+0.5)*scale)`.
588
+ Exactness holds for representable masks, and detail below the See-through
589
+ grid's resolution is not recovered. Missing records on another grid and
590
+ invalid records refuse `PLATE_MASK_GRID`, while mismatched source bytes
591
+ refuse `PLATE_MASK_PAINTING`. Existing runs are never backfilled.
592
+
565
593
  Revision ancestry is additive (issue #531): a previous root `remesh.json` or
566
594
  `retune.json` is preserved at `provenance/<command>-<sha256>.json`, and optional
567
595
  `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.27.1",
3
+ "version": "2.27.2",
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.4.1"
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) {
@@ -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');
@@ -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 { decodePngBytes } from '../raster/png.ts';
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);
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(bytes, uploadName);
96
- say(` uploaded ${run.image} (${img.width}x${img.height}) as input/${ref}`);
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
- for (const l of layers) writeFileSync(join(stage, 'parts', `${l.name as string}.png`), await client.view(l.filename as string));
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/plate.ts CHANGED
@@ -26,13 +26,14 @@
26
26
  * of every file it names; `verifyPlateReceipt` reads it back against the bytes
27
27
  * on disk. Every path in it is relative to the receipt's own directory.
28
28
  */
29
+ import { isSeeThroughTransform, inverseSeeThroughMask } from './seethrough-transform.ts';
29
30
  import { createHash } from 'node:crypto';
30
- import { existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmdirSync, rmSync, statSync, writeFileSync } from 'node:fs';
31
+ import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, renameSync, rmdirSync, rmSync, statSync, writeFileSync } from 'node:fs';
31
32
  import { basename, dirname, join, relative, resolve } from 'node:path';
32
33
  import { sourceInRig } from './assemble.ts';
33
34
  import { PartsError, type Problem } from './errors.ts';
34
35
  import type { Graph } from './graphs.ts';
35
- import { OPAQUE_ALPHA_ABOVE, readWrapperLayers } from './layers.ts';
36
+ import { type LayerSet, OPAQUE_ALPHA_ABOVE, readWrapperLayers } from './layers.ts';
36
37
  import { FIGURE_MASK_FILE, PAINTING_IN_RIG_FILE } from './parts.ts';
37
38
  import { decodePngBytes, encodePngBytes } from './raster/png.ts';
38
39
  import { newRaster, type Raster } from './raster/types.ts';
@@ -105,15 +106,62 @@ export interface FigureMask {
105
106
  inside: Uint8Array;
106
107
  }
107
108
 
108
- function maskRecord(path: string, code: string): { record: Record<string, unknown>; bytes: Uint8Array } {
109
+ function maskRecord(path: string, code: string, remedy = 're-run assemble'): { record: Record<string, unknown>; bytes: Uint8Array } {
109
110
  try {
110
111
  const bytes = new Uint8Array(readFileSync(path));
111
112
  const value: unknown = JSON.parse(new TextDecoder().decode(bytes));
112
113
  if (typeof value === 'object' && value !== null && !Array.isArray(value)) return { record: value as Record<string, unknown>, bytes };
113
114
  } catch (err) {
114
- fail(code, path, `cannot read a JSON object (${(err as Error).message}); re-run assemble`);
115
+ fail(code, path, `cannot read a JSON object (${(err as Error).message}); ${remedy}`);
115
116
  }
116
- return fail(code, path, 'requires a JSON object; re-run assemble');
117
+ return fail(code, path, `requires a JSON object; ${remedy}`);
118
+ }
119
+
120
+ /**
121
+ * comfy seethrough writes meta.json with image_sha256 and per-layer {path, sha256}.
122
+ * A missing or legacy meta without hashes is refused by name, every layer file
123
+ * readWrapperLayers read must match its recorded entry in meta.json, and the
124
+ * supplied --painting bytes must match image_sha256.
125
+ */
126
+ function bindSeeThroughSource(dir: string, set: LayerSet, paintingBytes: Uint8Array | undefined): Record<string, unknown> {
127
+ const root = resolve(dir);
128
+ const metaPath = join(root, 'meta.json');
129
+ const { record: meta } = maskRecord(metaPath, 'PLATE_MASK_RUN', 're-run comfy seethrough');
130
+ if (typeof meta.image_sha256 !== 'string' || !/^[0-9a-f]{64}$/.test(meta.image_sha256) || !Array.isArray(meta.layers) || meta.layers.length === 0) {
131
+ 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');
132
+ }
133
+ const entries = meta.layers as unknown[];
134
+ for (let i = 0; i < entries.length; i++) {
135
+ const e = entries[i];
136
+ if (typeof e !== 'object' || e === null || !('path' in e) || typeof e.path !== 'string' || e.path === '' ||
137
+ !('sha256' in e) || typeof e.sha256 !== 'string' || !/^[0-9a-f]{64}$/.test(e.sha256)) {
138
+ fail('PLATE_MASK_RUN', metaPath, `layers[${i}] requires a relative path and lowercase 64-hex sha256; re-run comfy seethrough`);
139
+ }
140
+ }
141
+ const typedEntries = entries as Array<{ path: string; sha256: string }>;
142
+ const readFiles = [...set.layers, ...set.hidden];
143
+ for (const layer of readFiles) {
144
+ if (layer.file === null) {
145
+ fail('PLATE_MASK_RUN', metaPath, `layer "${layer.name}" has no file on disk; re-run comfy seethrough`);
146
+ }
147
+ const key = relative(root, resolve(layer.file)).split('\\').join('/');
148
+ const matches = typedEntries.filter((e) => e.path === key);
149
+ const actual = sha256(new Uint8Array(readFileSync(layer.file)));
150
+ if (matches.length !== 1 || matches[0].sha256 !== actual) {
151
+ 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`);
152
+ }
153
+ }
154
+ if (typedEntries.length !== readFiles.length) {
155
+ fail('PLATE_MASK_RUN', metaPath, `records ${typedEntries.length} layer hash(es), while layers.json has ${readFiles.length}; re-run comfy seethrough`);
156
+ }
157
+ if (paintingBytes === undefined) {
158
+ fail('PLATE_MASK_PAINTING', metaPath, 'the supplied painting bytes are required to compare with meta.json image_sha256');
159
+ }
160
+ const actualPainting = sha256(paintingBytes);
161
+ if (actualPainting !== meta.image_sha256) {
162
+ 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`);
163
+ }
164
+ return meta;
117
165
  }
118
166
 
119
167
  /**
@@ -174,29 +222,36 @@ function bindFigureSource(dir: string, parts: Record<string, unknown>, partsByte
174
222
  * `width`x`height`, a `figure_rig.png` that is not the two values assemble
175
223
  * writes, and a mask with no figure pixel.
176
224
  */
177
- export function readSourceMask(source: MaskSource, dir: string, width: number, height: number, painting?: Raster): FigureMask {
225
+ export function readSourceMask(source: MaskSource, dir: string, width: number, height: number, painting?: Raster, paintingBytes?: Uint8Array): FigureMask {
178
226
  if (!existsSync(dir) || !statSync(dir).isDirectory()) {
179
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`);
180
228
  }
181
- const inside = new Uint8Array(width * height);
229
+ let inside: Uint8Array = new Uint8Array(width * height);
182
230
  if (source === 'seethrough') {
183
231
  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
232
  const set = readWrapperLayers(dir);
185
- if (set.canvas.w !== width || set.canvas.h !== height) {
186
- fail('PLATE_MASK_GRID', `${source}:${dir}`, `is a ${set.canvas.w}x${set.canvas.h} canvas; the painting's ${width}x${height} is required — the mask is never fitted, so run See-through on the painting at its own size`);
233
+ const meta = bindSeeThroughSource(dir, set, paintingBytes);
234
+ const transform = meta.input_transform;
235
+ const mapped = transform !== undefined && isSeeThroughTransform(transform, width, height, set.canvas.w, set.canvas.h);
236
+ if ((transform !== undefined && !mapped) || (!mapped && (set.canvas.w !== width || set.canvas.h !== height))) {
237
+ 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
238
  }
239
+ const maskWidth = set.canvas.w;
240
+ const maskHeight = set.canvas.h;
241
+ inside = new Uint8Array(maskWidth * maskHeight);
188
242
  for (const l of set.layers) {
189
243
  const w = l.right - l.left;
190
244
  for (let y = 0; y < l.bottom - l.top; y++) {
191
245
  const cy = l.top + y;
192
- if (cy < 0 || cy >= height) continue;
246
+ if (cy < 0 || cy >= maskHeight) continue;
193
247
  for (let x = 0; x < w; x++) {
194
248
  const cx = l.left + x;
195
- if (cx < 0 || cx >= width) continue;
196
- if (l.pixels.data[(y * w + x) * 4 + 3] > OPAQUE_ALPHA_ABOVE) inside[cy * width + cx] = 1;
249
+ if (cx < 0 || cx >= maskWidth) continue;
250
+ if (l.pixels.data[(y * w + x) * 4 + 3] > OPAQUE_ALPHA_ABOVE) inside[cy * maskWidth + cx] = 1;
197
251
  }
198
252
  }
199
253
  }
254
+ if (mapped) inside = inverseSeeThroughMask(inside, transform);
200
255
  } else {
201
256
  const partsPath = join(dir, 'parts.json');
202
257
  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 +495,8 @@ export interface PlateWrite {
440
495
  gpu: string | null;
441
496
  }
442
497
 
498
+ let foregroundStageSeq = 0;
499
+
443
500
  /**
444
501
  * Write `mask.png`, `plate.png` and `receipt.json` into a staging directory
445
502
  * beside `out`, read the receipt back against the bytes on disk, and only
@@ -449,8 +506,7 @@ export interface PlateWrite {
449
506
  export function publishPlate(w: PlateWrite): PlateReceipt {
450
507
  const parent = dirname(resolve(w.out));
451
508
  mkdirSync(parent, { recursive: true });
452
- const stage = join(parent, `.${basename(resolve(w.out))}.partial-${w.stageTag}`);
453
- mkdirSync(stage, { recursive: true });
509
+ const stage = mkdtempSync(join(parent, `.${basename(resolve(w.out))}.partial-${w.stageTag}-`));
454
510
  try {
455
511
  const mask = pngWithHash(maskImage(w.mask));
456
512
  const plate = pngWithHash(w.plate);
@@ -470,8 +526,17 @@ export function publishPlate(w: PlateWrite): PlateReceipt {
470
526
  writeFileSync(join(stage, PLATE_RECEIPT_FILE), receiptText(receipt));
471
527
  const problems = verifyPlateReceipt(join(stage, PLATE_RECEIPT_FILE));
472
528
  if (problems.length > 0) throw new PartsError(problems);
473
- if (existsSync(w.out)) rmdirSync(w.out);
474
- renameSync(stage, w.out);
529
+ assertPlateOutFree(w.out);
530
+ try {
531
+ if (existsSync(w.out)) rmdirSync(w.out);
532
+ renameSync(stage, w.out);
533
+ } catch (err) {
534
+ const code = (err as NodeJS.ErrnoException).code;
535
+ if (code === 'ENOTEMPTY' || code === 'EEXIST' || code === 'ENOTDIR' || code === 'EISDIR') {
536
+ fail('PLATE_OUT_EMPTY', w.out, 'is a file or a directory that holds entries; an absent or empty directory is required');
537
+ }
538
+ throw err;
539
+ }
475
540
  return receipt;
476
541
  } finally {
477
542
  if (existsSync(stage)) rmSync(stage, { recursive: true, force: true });
@@ -501,11 +566,11 @@ export interface ForegroundRun {
501
566
  export function runForeground(run: ForegroundRun): PlateReceipt {
502
567
  const from = parseMaskFrom(run.maskFrom);
503
568
  const painting = readPlatePainting(run.painting);
504
- const mask = readSourceMask(from.source, from.dir, painting.raster.width, painting.raster.height, painting.raster);
569
+ const mask = readSourceMask(from.source, from.dir, painting.raster.width, painting.raster.height, painting.raster, painting.bytes);
505
570
  assertPlateOutFree(run.out);
506
571
  return publishPlate({
507
572
  out: run.out,
508
- stageTag: 'foreground',
573
+ stageTag: `foreground-${process.pid}-${++foregroundStageSeq}`,
509
574
  mode: 'foreground',
510
575
  paintingPath: run.painting,
511
576
  paintingBytes: painting.bytes,
@@ -0,0 +1,62 @@
1
+ import { newRaster, type Raster } from './raster/types.ts';
2
+ import { resize } from './raster/resize.ts';
3
+
4
+ /** image_sha256 in the enclosing run record binds the source PNG bytes. */
5
+ export interface SeeThroughTransform {
6
+ source: [number, number];
7
+ padded: [number, number];
8
+ pad: [number, number];
9
+ scale: number;
10
+ resample: 'parts-lanczos3-rgba';
11
+ pad_fill: 'rgb-black-opaque';
12
+ target: [number, number];
13
+ inverse: 'nearest-pixel-centres';
14
+ }
15
+
16
+ /** Prepare only non-square images. Square inputs keep their original upload bytes. */
17
+ export function prepareSeeThroughInput(source: Raster, resolution: number): { image: Raster; transform?: SeeThroughTransform } {
18
+ if (source.width === source.height) return { image: source };
19
+ const side = Math.max(source.width, source.height);
20
+ const left = Math.floor((side - source.width) / 2);
21
+ const top = Math.floor((side - source.height) / 2);
22
+ const padded = newRaster(side, side);
23
+ for (let y = 0; y < source.height; y++) {
24
+ padded.data.set(source.data.subarray(y * source.width * 4, (y + 1) * source.width * 4), ((y + top) * side + left) * 4);
25
+ }
26
+ return {
27
+ image: resize(padded, resolution, resolution, 'lanczos3'),
28
+ transform: {
29
+ 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',
31
+ },
32
+ };
33
+ }
34
+
35
+ /** Validate all geometry before allocating or indexing a mapped mask. Offsets are read, never inferred. */
36
+ export function isSeeThroughTransform(value: unknown, width: number, height: number, targetWidth: number, targetHeight: number): value is SeeThroughTransform {
37
+ if (typeof value !== 'object' || value === null) return false;
38
+ const v = value as Record<string, unknown>;
39
+ const pair = (p: unknown): p is [number, number] => Array.isArray(p) && p.length === 2 && p.every(n => Number.isSafeInteger(n));
40
+ if (!pair(v.source) || !pair(v.padded) || !pair(v.pad) || !pair(v.target)) return false;
41
+ const side = v.padded[0];
42
+ return v.source[0] === width && v.source[1] === height && width !== height &&
43
+ side === Math.max(width, height) && v.padded[1] === side &&
44
+ v.pad[0] >= 0 && v.pad[1] >= 0 && v.pad[0] + width <= side && v.pad[1] + height <= side &&
45
+ 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';
47
+ }
48
+
49
+ /** Sample the union at transformed source pixel centres. Sub-grid detail cannot be recovered. */
50
+ export function inverseSeeThroughMask(mask: Uint8Array, transform: SeeThroughTransform): Uint8Array {
51
+ const [width, height] = transform.source;
52
+ const [targetWidth, targetHeight] = transform.target;
53
+ const out = new Uint8Array(width * height);
54
+ for (let y = 0; y < height; y++) {
55
+ const ty = Math.min(targetHeight - 1, Math.floor((y + transform.pad[1] + 0.5) * transform.scale));
56
+ for (let x = 0; x < width; x++) {
57
+ const tx = Math.min(targetWidth - 1, Math.floor((x + transform.pad[0] + 0.5) * transform.scale));
58
+ out[y * width + x] = mask[ty * targetWidth + tx];
59
+ }
60
+ }
61
+ return out;
62
+ }