@rig-c/parts 2.32.0 → 2.34.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/docs/AUTHORING.md +64 -2
- package/docs/STABILITY.md +3 -3
- package/package.json +1 -1
- package/src/assemble.ts +247 -32
- package/src/build.ts +8 -3
- package/src/config.ts +97 -22
- package/src/occluders.ts +231 -10
- package/src/occluderstage.ts +104 -14
- package/src/paintingowner.ts +7 -4
- package/src/parts.ts +48 -2
- package/src/takelayers.ts +130 -7
- package/src/takeregion.ts +16 -3
- package/src/takesource.ts +20 -1
package/src/paintingowner.ts
CHANGED
|
@@ -15,11 +15,14 @@ export interface PaintingReceivers extends PaintingOwner {
|
|
|
15
15
|
faults: Array<{ receiver: string; reason: string }>;
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
/** Owner (a), the last raw plan layer at alpha >=128 on the rig canvas. */
|
|
19
|
-
export function rawPaintingOwner(raw: ReadonlyArray<{ name: string; raster: Raster }>, pixels: number): PaintingOwner {
|
|
18
|
+
/** Owner (a), the last raw plan layer at alpha >=128 on the rig canvas (cleared where an occluder keep mask holds). */
|
|
19
|
+
export function rawPaintingOwner(raw: ReadonlyArray<{ name: string; raster: Raster; keep?: Mask | null }>, pixels: number): PaintingOwner {
|
|
20
20
|
const owner = new Int32Array(pixels).fill(-1);
|
|
21
|
-
raw.forEach(({ raster }, i) => {
|
|
22
|
-
for (let p = 0; p < pixels; p++)
|
|
21
|
+
raw.forEach(({ raster, keep }, i) => {
|
|
22
|
+
for (let p = 0; p < pixels; p++) {
|
|
23
|
+
if (raster.data[p * 4 + 3] < 128) continue;
|
|
24
|
+
owner[p] = keep?.data[p] === 1 ? -1 : i;
|
|
25
|
+
}
|
|
23
26
|
});
|
|
24
27
|
return { supplier: 'owner-a/raw-plan-alpha>=128', names: raw.map(q => q.name), pixels: owner };
|
|
25
28
|
}
|
package/src/parts.ts
CHANGED
|
@@ -377,6 +377,21 @@ export interface PaintingInRig {
|
|
|
377
377
|
resample: 'lanczos3-rgb-opaque';
|
|
378
378
|
}
|
|
379
379
|
|
|
380
|
+
export interface OccluderPlateRecord {
|
|
381
|
+
/** The plate receipt's path as declared (`<plate>/receipt.json`, POSIX separators). */
|
|
382
|
+
receipt: string;
|
|
383
|
+
/** SHA-256 of `receipt.json` bytes on disk. */
|
|
384
|
+
receipt_sha256: string;
|
|
385
|
+
/** `receipt.occluder.sha256` (bound to `config.assemble.occluders[i].sha256`). */
|
|
386
|
+
occluder_sha256: string;
|
|
387
|
+
/** `receipt.painting.sha256` (bound to `--source` painting bytes). */
|
|
388
|
+
painting_sha256: string;
|
|
389
|
+
/** `receipt.output.sha256` (bound to the base `--full` See-through run's `meta.json` `image_sha256`). */
|
|
390
|
+
output_sha256: string;
|
|
391
|
+
/** `receipt.mask.sha256` (from the verified plate receipt). */
|
|
392
|
+
mask_sha256: string;
|
|
393
|
+
}
|
|
394
|
+
|
|
380
395
|
export interface PartsFile {
|
|
381
396
|
/** Optional on legacy packages; binds the opaque RGB raster assemble actually read. */
|
|
382
397
|
painting_in_rig?: PaintingInRig;
|
|
@@ -394,6 +409,8 @@ export interface PartsFile {
|
|
|
394
409
|
scene?: SceneRecord;
|
|
395
410
|
/** Written only for one run whose file is not the resolution square (issue #366): where the file sat on the run's square; absent otherwise. */
|
|
396
411
|
single_run_pad?: SingleRunPad;
|
|
412
|
+
/** Written only when an `assemble.occluders` entry declares `plate` (issue #1237); absent otherwise. */
|
|
413
|
+
occluder_plates?: OccluderPlateRecord[];
|
|
397
414
|
/** Written only when alternate painting sets were assembled (issue #454); absent otherwise. */
|
|
398
415
|
alternates?: AlternatePartsRecord[];
|
|
399
416
|
}
|
|
@@ -430,8 +447,9 @@ export interface SceneRecord {
|
|
|
430
447
|
figure_mask: string;
|
|
431
448
|
}
|
|
432
449
|
|
|
433
|
-
const TOP_KEYS = ['painting_in_rig', 'rig_size', 'scale_rig_per_source', 'parts', 'ghost_px', 'recomposite', 'scene', 'single_run_pad', 'alternates', 'motion_seams', 'cut_ownership'] as const;
|
|
434
|
-
const REQUIRED_TOP_KEYS = TOP_KEYS.filter((k) => k !== 'painting_in_rig' && k !== 'recomposite' && k !== 'scene' && k !== 'single_run_pad' && k !== 'alternates' && k !== 'motion_seams' && k !== 'cut_ownership');
|
|
450
|
+
const TOP_KEYS = ['painting_in_rig', 'rig_size', 'scale_rig_per_source', 'parts', 'ghost_px', 'recomposite', 'scene', 'occluder_plates', 'single_run_pad', 'alternates', 'motion_seams', 'cut_ownership'] as const;
|
|
451
|
+
const REQUIRED_TOP_KEYS = TOP_KEYS.filter((k) => k !== 'painting_in_rig' && k !== 'recomposite' && k !== 'scene' && k !== 'single_run_pad' && k !== 'occluder_plates' && k !== 'alternates' && k !== 'motion_seams' && k !== 'cut_ownership');
|
|
452
|
+
export const OCCLUDER_PLATE_KEYS = ['receipt', 'receipt_sha256', 'occluder_sha256', 'painting_sha256', 'output_sha256', 'mask_sha256'] as const;
|
|
435
453
|
export const SINGLE_RUN_PAD_KEYS = ['file_size', 'side', 'left', 'top'] as const;
|
|
436
454
|
export const SCENE_KEYS = ['figure_alpha_above', 'figure_px', 'figure_mask'] as const;
|
|
437
455
|
export const RECOMPOSITE_KEYS = ['mean_abs', 'within_limit', 'within_share', 'error_limit', 'error_px', 'covered_alpha', 'uncovered_error_px', 'hole_count', 'holes_listed', 'holes'] as const;
|
|
@@ -522,6 +540,7 @@ function orderedProvenance(p: PixelProvenance): PixelProvenance {
|
|
|
522
540
|
|
|
523
541
|
function orderedTakenSource(source: TakenLayerSource): TakenLayerSource {
|
|
524
542
|
const transport = source.transport;
|
|
543
|
+
const split = source.split;
|
|
525
544
|
return {
|
|
526
545
|
run: source.run, tag: source.tag, run_dir: source.run_dir, st_input: source.st_input,
|
|
527
546
|
prompt_id: source.prompt_id, seed: source.seed, support_px: source.support_px,
|
|
@@ -534,6 +553,10 @@ function orderedTakenSource(source: TakenLayerSource): TakenLayerSource {
|
|
|
534
553
|
before_rgba_sha256: transport.before_rgba_sha256,
|
|
535
554
|
after_rgba_sha256: transport.after_rgba_sha256,
|
|
536
555
|
} }),
|
|
556
|
+
...(split === undefined ? {} : { split: {
|
|
557
|
+
occluder: split.occluder, image: split.image, sha256: split.sha256,
|
|
558
|
+
covered_px: split.covered_px, visible_px: split.visible_px,
|
|
559
|
+
} }),
|
|
537
560
|
};
|
|
538
561
|
}
|
|
539
562
|
|
|
@@ -563,6 +586,7 @@ export function serializeParts(file: PartsFile): string {
|
|
|
563
586
|
ghost_px: Object.fromEntries(Object.keys(file.ghost_px).sort().map((k) => [k, file.ghost_px[k]])),
|
|
564
587
|
...(file.recomposite === undefined ? {} : { recomposite: serializeRecomposite(file.recomposite) }),
|
|
565
588
|
...(file.scene === undefined ? {} : { scene: Object.fromEntries(SCENE_KEYS.map((k) => [k, (file.scene as SceneRecord)[k]])) }),
|
|
589
|
+
...(file.occluder_plates === undefined ? {} : { occluder_plates: file.occluder_plates.map((p) => Object.fromEntries(OCCLUDER_PLATE_KEYS.map((k) => [k, p[k]]))) }),
|
|
566
590
|
...(file.single_run_pad === undefined ? {} : { single_run_pad: Object.fromEntries(SINGLE_RUN_PAD_KEYS.map((k) => [k, (file.single_run_pad as SingleRunPad)[k]])) }),
|
|
567
591
|
...(file.alternates === undefined
|
|
568
592
|
? {}
|
|
@@ -902,6 +926,28 @@ function checkParts(raw: unknown, path: string): PartsFile {
|
|
|
902
926
|
}
|
|
903
927
|
if ('cut_ownership' in o) checkOwnership(o.cut_ownership, `${path} cut_ownership`, fail);
|
|
904
928
|
if ('single_run_pad' in o) checkSingleRunPad(o.single_run_pad, path, sizeOk ? (size as [number, number]) : null, typeof scale === 'number' ? scale : null, fail);
|
|
929
|
+
if ('occluder_plates' in o) {
|
|
930
|
+
if (!Array.isArray(o.occluder_plates) || o.occluder_plates.length === 0) {
|
|
931
|
+
fail('PARTS_FIELD_TYPE', `${path} field "occluder_plates"`, `is ${show(o.occluder_plates)}; a non-empty array of occluder plate records is required`);
|
|
932
|
+
} else {
|
|
933
|
+
o.occluder_plates.forEach((entry, i) => {
|
|
934
|
+
const at = `${path} occluder_plates[${i}]`;
|
|
935
|
+
if (typeof entry !== 'object' || entry === null || Array.isArray(entry)) {
|
|
936
|
+
fail('PARTS_FIELD_TYPE', at, `is ${show(entry)}; an object is required`);
|
|
937
|
+
return;
|
|
938
|
+
}
|
|
939
|
+
const r = entry as Record<string, unknown>;
|
|
940
|
+
for (const k of Object.keys(r)) if (!(OCCLUDER_PLATE_KEYS as readonly string[]).includes(k)) fail('PARTS_KEY_KNOWN', `${at} key "${k}"`, `is not an occluder_plates field; known: ${OCCLUDER_PLATE_KEYS.join(', ')}`);
|
|
941
|
+
for (const k of OCCLUDER_PLATE_KEYS) if (!(k in r)) fail('PARTS_FIELD_PRESENT', `${at} field "${k}"`, 'is absent and required');
|
|
942
|
+
if ('receipt' in r && (typeof r.receipt !== 'string' || r.receipt.length === 0)) fail('PARTS_FIELD_TYPE', `${at} field "receipt"`, `is ${show(r.receipt)}; a non-empty string path is required`);
|
|
943
|
+
for (const k of ['receipt_sha256', 'occluder_sha256', 'painting_sha256', 'output_sha256', 'mask_sha256'] as const) {
|
|
944
|
+
if (k in r && (typeof r[k] !== 'string' || !/^[a-f0-9]{64}$/.test(r[k] as string))) {
|
|
945
|
+
fail('PARTS_FIELD_TYPE', `${at} field "${k}"`, `is ${show(r[k])}; 64 lowercase hex characters are required`);
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
});
|
|
949
|
+
}
|
|
950
|
+
}
|
|
905
951
|
if ('recomposite' in o && sizeOk) {
|
|
906
952
|
const names = Array.isArray(o.parts) ? new Set(o.parts.map((p) => (typeof p === 'object' && p !== null ? (p as Record<string, unknown>).name : undefined)).filter((n): n is string => typeof n === 'string')) : new Set<string>();
|
|
907
953
|
checkRecomposite(o.recomposite, path, size as [number, number], names, fail);
|
package/src/takelayers.ts
CHANGED
|
@@ -7,14 +7,18 @@ import { CORE_KERNEL, fullPlanBand } from './assemble.ts';
|
|
|
7
7
|
import { makeInputs, squarePad } from './inputs.ts';
|
|
8
8
|
import { OPAQUE_ALPHA_ABOVE, readLayers, type Layer, type LayerSet } from './layers.ts';
|
|
9
9
|
import { PartsError, refuseIfAny, type Problem } from './errors.ts';
|
|
10
|
+
import { COPIED_ALPHA_ABOVE, declaredOccluderPair, occluderDrawCycleProblems, occluderDrawOrderProblems, occluderOverTakenProblems, type OccluderDeclaration } from './occluders.ts';
|
|
10
11
|
import { dilate } from './raster/morph.ts';
|
|
11
12
|
import { newMask, newRaster, newFloatImage, type Raster } from './raster/types.ts';
|
|
12
13
|
import { readPng } from './raster/png.ts';
|
|
13
|
-
import type { TakeDeclaration } from './takeregion.ts';
|
|
14
|
+
import type { TakeDeclaration, TakeOccluderSplit } from './takeregion.ts';
|
|
14
15
|
import { crop, pad } from './raster/composite.ts';
|
|
15
16
|
import { warpAffine, type ScaleTranslate } from './raster/warp.ts';
|
|
16
17
|
import { takenTransportProblems, type TakenLayerSource, type TakenLayerTransport } from './takesource.ts';
|
|
17
18
|
|
|
19
|
+
export const TAKE_SPLIT_OCCLUDER = 'ASSEMBLE_TAKE_SPLIT_OCCLUDER';
|
|
20
|
+
export const TAKE_SPLIT_EMPTY = 'ASSEMBLE_TAKE_SPLIT_EMPTY';
|
|
21
|
+
|
|
18
22
|
export interface TakeLayers {
|
|
19
23
|
run: Run;
|
|
20
24
|
run_dir: string;
|
|
@@ -141,7 +145,10 @@ export function transportLayer(layer: Layer, donor: LayerSet, set: LayerSet, ima
|
|
|
141
145
|
}
|
|
142
146
|
|
|
143
147
|
export function takeLayerNote(source: TakenLayerSource): string {
|
|
144
|
-
|
|
148
|
+
const splitClause = source.split === undefined
|
|
149
|
+
? ''
|
|
150
|
+
: `; split occluder[${source.split.occluder}] ${source.split.image} (${source.split.sha256.slice(0, 12)}) covered ${source.split.covered_px} run px, visible ${source.split.visible_px} run px`;
|
|
151
|
+
return `take ${source.run}:${source.tag} from ${source.run_dir}; prompt_id ${source.prompt_id}; seed ${source.seed}; st_input ${source.st_input}; support ${source.support_px} run px; checked ${source.checked_px} input px; differing ${source.differing_px}; alpha > ${OPAQUE_ALPHA_ABOVE}, ${CORE_KERNEL}x${CORE_KERNEL} margin${splitClause}`;
|
|
145
152
|
}
|
|
146
153
|
/** Intersection of original alpha supports in the common target-run canvas; no bbox or dilation proxy. */
|
|
147
154
|
function supportOverlap(a: Layer, b: Layer): number {
|
|
@@ -154,8 +161,42 @@ function supportOverlap(a: Layer, b: Layer): number {
|
|
|
154
161
|
return px;
|
|
155
162
|
}
|
|
156
163
|
/** Apply every declaration together, so replacing all tied layers from one run can retain its known order. */
|
|
157
|
-
export function applyTakenLayers(runs: { full: LayerSet; head: LayerSet; single: boolean }, painting: Raster, cfg: Pick<EarlyConfig, 'seethrough' | 'depth'> & { assemble: { take?: TakeDeclaration[]; plan?: PlanEntry[] } }, configPath: string): { full: LayerSet; head: LayerSet; single: boolean; notes: string[] } {
|
|
158
|
-
const
|
|
164
|
+
export function applyTakenLayers(runs: { full: LayerSet; head: LayerSet; single: boolean }, painting: Raster, cfg: Pick<EarlyConfig, 'seethrough' | 'depth'> & { assemble: { take?: TakeDeclaration[]; plan?: PlanEntry[]; occluders?: OccluderDeclaration[] } }, configPath: string): { full: LayerSet; head: LayerSet; single: boolean; notes: string[] } {
|
|
165
|
+
const wholeDeclarations = (cfg.assemble.take ?? []).filter((item): item is TakeLayers => 'tags' in item);
|
|
166
|
+
const declarations = (cfg.assemble.take ?? []).filter((item): item is TakeLayers | TakeOccluderSplit => 'tags' in item || 'occluder' in item);
|
|
167
|
+
const occluders = cfg.assemble.occluders ?? [];
|
|
168
|
+
for (const d of declarations) {
|
|
169
|
+
if ('occluder' in d) {
|
|
170
|
+
const partAt = `config.assemble.take run "${d.run_dir}" (${d.run}) part "${d.part}"`;
|
|
171
|
+
const occEntry = Number.isSafeInteger(d.occluder) && d.occluder >= 0 ? occluders[d.occluder] : undefined;
|
|
172
|
+
if (occEntry === undefined) {
|
|
173
|
+
return refuse(TAKE_SPLIT_OCCLUDER, partAt, `occluder index ${d.occluder} does not exist in config.assemble.occluders (${occluders.length} declared); a valid occluder index is required`);
|
|
174
|
+
}
|
|
175
|
+
if ((occEntry.over ?? []).includes(d.part)) {
|
|
176
|
+
return refuse(TAKE_SPLIT_OCCLUDER, partAt, `part "${d.part}" is in config.assemble.occluders[${d.occluder}].over; a split take is for a part under the occluder, not in its over`);
|
|
177
|
+
}
|
|
178
|
+
if (!occEntry.under.includes(d.part)) {
|
|
179
|
+
return refuse(TAKE_SPLIT_OCCLUDER, partAt, `part "${d.part}" is not in config.assemble.occluders[${d.occluder}].under; a part declared under that occluder is required`);
|
|
180
|
+
}
|
|
181
|
+
if (cfg.assemble.plan !== undefined) {
|
|
182
|
+
const row = cfg.assemble.plan.find(([part]) => part === d.part);
|
|
183
|
+
if (row === undefined) return refuse('CONFIG_NAME_RESOLVES', partAt, `names "${d.part}"; a part named in assemble.plan is required`);
|
|
184
|
+
if (row[1] !== d.run || row[2] !== d.tag) return refuse('CONFIG_NAME_RESOLVES', partAt, `declares ${d.run}:${d.tag} for part "${d.part}"; assemble.plan declares ["${row[0]}", "${row[1]}", "${row[2]}"]`);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
if (occluders.length > 0) {
|
|
189
|
+
const cycles = occluderDrawCycleProblems(occluders);
|
|
190
|
+
refuseIfAny([
|
|
191
|
+
...cycles,
|
|
192
|
+
...(cfg.assemble.plan === undefined
|
|
193
|
+
? []
|
|
194
|
+
: [
|
|
195
|
+
...occluderOverTakenProblems(occluders, cfg.assemble.plan, wholeDeclarations),
|
|
196
|
+
...(cycles.length > 0 ? [] : occluderDrawOrderProblems(occluders, cfg.assemble.plan)),
|
|
197
|
+
]),
|
|
198
|
+
]);
|
|
199
|
+
}
|
|
159
200
|
if (declarations.length === 0) return { ...runs, notes: [] };
|
|
160
201
|
if (cfg.depth !== undefined || [...runs.full.layers, ...runs.head.layers].some(l => l.depthSheet !== undefined)) return refuse('ASSEMBLE_TAKE_DEPTH_SHEET', 'config.assemble.take', 'active scalar depth sheets have no mixed-run binding; disable the depth opt-in or assemble a separately bound run before taking layers (numeric depth_median ordering is retained)');
|
|
161
202
|
const inputs = makeInputs(painting, cfg, 'take target painting');
|
|
@@ -176,6 +217,85 @@ export function applyTakenLayers(runs: { full: LayerSet; head: LayerSet; single:
|
|
|
176
217
|
if (target === null) return refuse('ASSEMBLE_TAKE_CROP', at, 'a target input is required for a declared crop');
|
|
177
218
|
const geometry = d.crop === undefined ? undefined : cropGeometry(d, painting, target, donor, set, inputs, at);
|
|
178
219
|
if (geometry !== undefined && (image.width !== geometry.expected.width || image.height !== geometry.expected.height)) return refuse('ASSEMBLE_TAKE_CROP', at, `donor st_input ${image.width}x${image.height}; declared square-padded crop requires ${geometry.expected.width}x${geometry.expected.height}`);
|
|
220
|
+
if ('occluder' in d) {
|
|
221
|
+
const partAt = `${at} part "${d.part}"`;
|
|
222
|
+
const tagAt = `${at} tag "${d.tag}"`;
|
|
223
|
+
const key = `${d.run}:${d.tag}`;
|
|
224
|
+
if (seen.has(key)) return refuse('ASSEMBLE_TAKE_UNIQUE', at, `tag "${d.tag}" replaces ${key} twice; declare one donor per target tag`);
|
|
225
|
+
seen.add(key);
|
|
226
|
+
const baseLayer = lists[d.run].find(l => l.name === d.tag);
|
|
227
|
+
if (baseLayer === undefined) return refuse('ASSEMBLE_TAKE_TAG_IN_RUN', at, `tag "${d.tag}" is absent from the base ${d.run} run; both base and donor must hold the split tag`);
|
|
228
|
+
const donorLayer = donor.layers.find(l => l.name === d.tag);
|
|
229
|
+
if (donorLayer === undefined) return refuse('ASSEMBLE_TAKE_TAG_IN_RUN', at, `tag "${d.tag}" is absent; a named See-through tag held by that donor is required`);
|
|
230
|
+
const occEntry = occluders[d.occluder]!;
|
|
231
|
+
let occPng: Raster;
|
|
232
|
+
try { occPng = readPng(resolve(base, occEntry.image)); } catch { return refuse('CONFIG_FILE_PRESENT', `config.assemble.occluders[${d.occluder}].image`, `names "${occEntry.image}", which is not a readable PNG`); }
|
|
233
|
+
if (occPng.width !== painting.width || occPng.height !== painting.height) {
|
|
234
|
+
return refuse('ASSEMBLE_OCCLUDER_GRID', `config.assemble.occluders[${d.occluder}].image`, `is ${occPng.width}x${occPng.height}; the painting's dimensions ${painting.width}x${painting.height} are required`);
|
|
235
|
+
}
|
|
236
|
+
const occTarget = d.run === 'head'
|
|
237
|
+
? crop(occPng, inputs.headBox![0], inputs.headBox![1], inputs.headBox![2] - inputs.headBox![0], inputs.headBox![3] - inputs.headBox![1])
|
|
238
|
+
: target === painting
|
|
239
|
+
? occPng
|
|
240
|
+
: pad(occPng, inputs.padLeft, inputs.padTop, target.width - painting.width - inputs.padLeft, target.height - painting.height - inputs.padTop, [0, 0, 0, 0]);
|
|
241
|
+
const isCovered = (x: number, y: number): boolean => {
|
|
242
|
+
const ox = Math.min(occTarget.width - 1, Math.max(0, Math.floor((x + 0.5) * occTarget.width / set.canvas.w)));
|
|
243
|
+
const oy = Math.min(occTarget.height - 1, Math.max(0, Math.floor((y + 0.5) * occTarget.height / set.canvas.h)));
|
|
244
|
+
return occTarget.data[(oy * occTarget.width + ox) * 4 + 3] > COPIED_ALPHA_ABOVE;
|
|
245
|
+
};
|
|
246
|
+
const transported = geometry === undefined ? undefined : transportLayer(donorLayer, donor, set, image, d, geometry.map, geometry.exact, tagAt);
|
|
247
|
+
const donorOnSet = transported?.layer ?? donorLayer;
|
|
248
|
+
const merged = newRaster(set.canvas.w, set.canvas.h);
|
|
249
|
+
let coveredPx = 0, visiblePx = 0, opaquePx = 0;
|
|
250
|
+
for (let y = 0; y < set.canvas.h; y++) for (let x = 0; x < set.canvas.w; x++) {
|
|
251
|
+
const p = (y * set.canvas.w + x) * 4;
|
|
252
|
+
if (isCovered(x, y)) {
|
|
253
|
+
if (x >= baseLayer.left && x < baseLayer.right && y >= baseLayer.top && y < baseLayer.bottom) {
|
|
254
|
+
const bp = ((y - baseLayer.top) * baseLayer.pixels.width + (x - baseLayer.left)) * 4;
|
|
255
|
+
merged.data.set(baseLayer.pixels.data.subarray(bp, bp + 4), p);
|
|
256
|
+
if (baseLayer.pixels.data[bp + 3] > OPAQUE_ALPHA_ABOVE) { coveredPx++; opaquePx++; }
|
|
257
|
+
}
|
|
258
|
+
} else {
|
|
259
|
+
if (x >= donorOnSet.left && x < donorOnSet.right && y >= donorOnSet.top && y < donorOnSet.bottom) {
|
|
260
|
+
const dp = ((y - donorOnSet.top) * donorOnSet.pixels.width + (x - donorOnSet.left)) * 4;
|
|
261
|
+
merged.data.set(donorOnSet.pixels.data.subarray(dp, dp + 4), p);
|
|
262
|
+
if (donorOnSet.pixels.data[dp + 3] > OPAQUE_ALPHA_ABOVE) { visiblePx++; opaquePx++; }
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
if (coveredPx === 0 || visiblePx === 0) {
|
|
267
|
+
return refuse('ASSEMBLE_TAKE_SPLIT_EMPTY', partAt, `covered_px is ${coveredPx} (base ${d.run}:${d.tag} under occluder[${d.occluder}] alpha > ${COPIED_ALPHA_ABOVE}) and visible_px is ${visiblePx} (donor ${d.run}:${d.tag} outside occluder[${d.occluder}]); both regions require at least 1 pixel of alpha > ${OPAQUE_ALPHA_ABOVE}`);
|
|
268
|
+
}
|
|
269
|
+
const donorVisiblePixels = newRaster(donorLayer.pixels.width, donorLayer.pixels.height);
|
|
270
|
+
let donorVisibleOpaque = 0;
|
|
271
|
+
for (let y = 0; y < donorLayer.pixels.height; y++) for (let x = 0; x < donorLayer.pixels.width; x++) {
|
|
272
|
+
const dx = donorLayer.left + x, dy = donorLayer.top + y;
|
|
273
|
+
const tx = geometry === undefined ? dx : Math.floor(geometry.map.sx * dx + geometry.map.tx);
|
|
274
|
+
const ty = geometry === undefined ? dy : Math.floor(geometry.map.sy * dy + geometry.map.ty);
|
|
275
|
+
if (tx >= 0 && tx < set.canvas.w && ty >= 0 && ty < set.canvas.h && !isCovered(tx, ty)) {
|
|
276
|
+
const dp = (y * donorLayer.pixels.width + x) * 4;
|
|
277
|
+
donorVisiblePixels.data.set(donorLayer.pixels.data.subarray(dp, dp + 4), dp);
|
|
278
|
+
if (donorLayer.pixels.data[dp + 3] > OPAQUE_ALPHA_ABOVE) donorVisibleOpaque++;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
const donorVisible: Layer = { ...donorLayer, pixels: donorVisiblePixels, opaquePx: donorVisibleOpaque };
|
|
282
|
+
const checked = identity(donorVisible, donor.canvas, image, geometry?.expected ?? target, tagAt);
|
|
283
|
+
const source: TakenLayerSource = {
|
|
284
|
+
run: d.run, tag: d.tag, run_dir: d.run_dir, st_input: d.st_input,
|
|
285
|
+
prompt_id: meta.prompt_id, seed: meta.seed,
|
|
286
|
+
support_px: checked.support,
|
|
287
|
+
checked_px: checked.checked.reduce((a, b) => a + b, 0),
|
|
288
|
+
differing_px: 0,
|
|
289
|
+
...(transported === undefined ? {} : { transport: transported.transport }),
|
|
290
|
+
split: { occluder: d.occluder, image: occEntry.image, sha256: occEntry.sha256, covered_px: coveredPx, visible_px: visiblePx },
|
|
291
|
+
};
|
|
292
|
+
const replacement: Layer = { ...baseLayer, left: 0, top: 0, right: set.canvas.w, bottom: set.canvas.h, pixels: merged, opaquePx, takenLayers: [source] };
|
|
293
|
+
lists[d.run] = [...lists[d.run].filter(l => l.name !== d.tag), replacement];
|
|
294
|
+
origins.set(replacement, resolve(base, d.run_dir));
|
|
295
|
+
notes.push(takeLayerNote(source));
|
|
296
|
+
notes.push(`take union ${d.run} from ${d.run_dir}: checked ${checked.checked.reduce((a, b) => a + b, 0)} input px; differing 0`);
|
|
297
|
+
continue;
|
|
298
|
+
}
|
|
179
299
|
const union = new Uint8Array(image.width * image.height);
|
|
180
300
|
for (const tag of d.tags) {
|
|
181
301
|
const key = `${d.run}:${tag}`;
|
|
@@ -206,7 +326,9 @@ export function applyTakenLayers(runs: { full: LayerSet; head: LayerSet; single:
|
|
|
206
326
|
const selected = (cfg.assemble.plan ?? []).filter(p => p[1] === run).map(p => ({ part: p[0], layer: list.find(l => l.name === p[2]) }));
|
|
207
327
|
for (let i = 0; i < selected.length; i++) for (let j = i + 1; j < selected.length; j++) {
|
|
208
328
|
const a = selected[i], b = selected[j];
|
|
209
|
-
if (a.layer === undefined || b.layer === undefined
|
|
329
|
+
if (a.layer === undefined || b.layer === undefined) continue;
|
|
330
|
+
if (declaredOccluderPair(occluders, a.part, b.part) !== null) continue;
|
|
331
|
+
if (origin(a.layer) === origin(b.layer)) continue;
|
|
210
332
|
const overlap = supportOverlap(a.layer, b.layer);
|
|
211
333
|
if (overlap === 0) continue;
|
|
212
334
|
// Only full rows in a two-run plan have the proposer's fallback roles.
|
|
@@ -216,8 +338,9 @@ export function applyTakenLayers(runs: { full: LayerSet; head: LayerSet; single:
|
|
|
216
338
|
if (reversed) problems.push({ code: 'ASSEMBLE_TAKE_PLAN_ORDER', object: `${run} plan parts "${a.part}" / "${b.part}"`, detail: `plan order reverses ${band === 0 ? `mixed-run depths ${a.layer.depth} / ${b.layer.depth}; put the larger depth behind` : `two-run full-layer roles "${a.layer.name}" / "${b.layer.name}"; order back hair, body, headwear/earwear, face, head details, front hair`}; supports overlap at ${overlap} run px (alpha > ${OPAQUE_ALPHA_ABOVE}); use assemble --propose-plan` });
|
|
217
339
|
}
|
|
218
340
|
refuseIfAny(problems);
|
|
219
|
-
const
|
|
220
|
-
|
|
341
|
+
const tagsOf = (d: TakeLayers | TakeOccluderSplit): readonly string[] => 'tags' in d ? d.tags : [d.tag];
|
|
342
|
+
const replaced = new Set(declarations.filter(d => d.run === run).flatMap(tagsOf));
|
|
343
|
+
if (runs[run].hidden.some(l => replaced.has(l.of)) || declarations.some(d => d.run === run && readLayers(resolve(base, d.run_dir)).hidden.some(l => tagsOf(d).includes(l.of)))) return refuse('ASSEMBLE_TAKE_HIDDEN', run, 'a replaced layer has hidden companion art; separate hidden provenance is not declared, so the combination is refused');
|
|
221
344
|
}
|
|
222
345
|
const full = { ...runs.full, layers: lists.full }, head = runs.single ? full : { ...runs.head, layers: lists.head };
|
|
223
346
|
return { full, head, single: runs.single, notes };
|
package/src/takeregion.ts
CHANGED
|
@@ -24,7 +24,20 @@ export interface TakeRegion {
|
|
|
24
24
|
/** PNG bytes, explicitly bound to the stage source or a separate painting. */
|
|
25
25
|
reference?: { source: true; sha256: string } | { painting: string; sha256: string };
|
|
26
26
|
}
|
|
27
|
-
export
|
|
27
|
+
export interface TakeOccluderSplit {
|
|
28
|
+
run: Run;
|
|
29
|
+
run_dir: string;
|
|
30
|
+
st_input: string;
|
|
31
|
+
/** Same painting-space crop and transport as a whole-layer take. */
|
|
32
|
+
crop?: TakeLayers['crop'];
|
|
33
|
+
/** Plan part on `run`; must be in `occluders[occluder].under`, not in `over`. */
|
|
34
|
+
part: string;
|
|
35
|
+
/** The plan row's See-through tag for `part` on `run`. */
|
|
36
|
+
tag: string;
|
|
37
|
+
/** Zero-based index into `config.assemble.occluders`; its image is the split mask. */
|
|
38
|
+
occluder: number;
|
|
39
|
+
}
|
|
40
|
+
export type TakeDeclaration = TakeLayers | TakeRegion | TakeOccluderSplit;
|
|
28
41
|
export interface RegionReading {
|
|
29
42
|
part: string;
|
|
30
43
|
run: Run;
|
|
@@ -106,7 +119,7 @@ export function deriveTakenRegion(base: Layer, donor: Layer, painting: Raster, s
|
|
|
106
119
|
|
|
107
120
|
/** Called beside whole-layer take, before assembly or any output emission. */
|
|
108
121
|
export function applyTakenRegions(runs: Runs, painting: Raster, cfg: AssembleConfig, configPath: string, setup: SetupPainting | undefined, receipts: ReceiptLedger, sourcePath?: string): Runs {
|
|
109
|
-
const declarations = (cfg.assemble.take ?? []).filter((item): item is TakeRegion => '
|
|
122
|
+
const declarations = (cfg.assemble.take ?? []).filter((item): item is TakeRegion => 'seed' in item);
|
|
110
123
|
if (declarations.length === 0) return runs;
|
|
111
124
|
const tolerance = setup?.rgb_tolerance;
|
|
112
125
|
if (typeof tolerance !== 'number' || !Number.isSafeInteger(tolerance) || tolerance < 0 || tolerance > 255) {
|
|
@@ -126,7 +139,7 @@ export function applyTakenRegions(runs: Runs, painting: Raster, cfg: AssembleCon
|
|
|
126
139
|
const plan = cfg.assemble.plan.find(entry => entry[0] === declaration.part && entry[1] === declaration.run);
|
|
127
140
|
if (plan === undefined) return refuse('ASSEMBLE_REGION_TAKE_PART', at, `requires a plan part named "${declaration.part}" in target run "${declaration.run}"`);
|
|
128
141
|
const key = `${plan[1]}:${plan[2]}`;
|
|
129
|
-
if (seen.has(key) || (cfg.assemble.take ?? []).some(item => 'tags' in item && item.run === plan[1] && item.tags.includes(plan[2]))) {
|
|
142
|
+
if (seen.has(key) || (cfg.assemble.take ?? []).some(item => ('tags' in item && item.run === plan[1] && item.tags.includes(plan[2])) || ('occluder' in item && item.run === plan[1] && (item.tag === plan[2] || item.part === declaration.part)))) {
|
|
130
143
|
return refuse('ASSEMBLE_REGION_TAKE_PART', at, `target ${key} is taken more than once; one region donor or whole-layer donor per target is required`);
|
|
131
144
|
}
|
|
132
145
|
seen.add(key);
|
package/src/takesource.ts
CHANGED
|
@@ -23,6 +23,13 @@ export function takenTransportProblems(transport: TakenLayerTransport, image: Ra
|
|
|
23
23
|
}
|
|
24
24
|
return [];
|
|
25
25
|
}
|
|
26
|
+
export interface TakenLayerSplit {
|
|
27
|
+
occluder: number;
|
|
28
|
+
image: string;
|
|
29
|
+
sha256: string;
|
|
30
|
+
covered_px: number;
|
|
31
|
+
visible_px: number;
|
|
32
|
+
}
|
|
26
33
|
export interface TakenLayerSource {
|
|
27
34
|
run: 'full' | 'head';
|
|
28
35
|
tag: string;
|
|
@@ -34,6 +41,7 @@ export interface TakenLayerSource {
|
|
|
34
41
|
checked_px: number;
|
|
35
42
|
differing_px: 0;
|
|
36
43
|
transport?: TakenLayerTransport;
|
|
44
|
+
split?: TakenLayerSplit;
|
|
37
45
|
}
|
|
38
46
|
export function unionTakenSources(lists: readonly (readonly TakenLayerSource[] | undefined)[]): TakenLayerSource[] {
|
|
39
47
|
const out: TakenLayerSource[] = [];
|
|
@@ -51,7 +59,7 @@ export function takenSourceProblems(value: unknown, at: string): Problem[] {
|
|
|
51
59
|
if (typeof item !== 'object' || item === null || Array.isArray(item)) { fail('each source must be a complete identity receipt'); continue; }
|
|
52
60
|
const o = item as Record<string, unknown>;
|
|
53
61
|
const tag = typeof o.tag === 'string' ? readLayerTag(o.tag) : null;
|
|
54
|
-
if (Object.keys(o).some(k => !keys.includes(k) && k !== 'transport') || keys.some(k => !(k in o)) ||
|
|
62
|
+
if (Object.keys(o).some(k => !keys.includes(k) && k !== 'transport' && k !== 'split') || keys.some(k => !(k in o)) ||
|
|
55
63
|
(o.run !== 'full' && o.run !== 'head') || tag === null || tag.group === 'author' ||
|
|
56
64
|
['run_dir', 'st_input', 'prompt_id'].some(k => typeof o[k] !== 'string' || !(o[k] as string).trim()) ||
|
|
57
65
|
!Number.isSafeInteger(o.seed) || (o.seed as number) < 0 ||
|
|
@@ -72,6 +80,17 @@ export function takenSourceProblems(value: unknown, at: string): Problem[] {
|
|
|
72
80
|
fail('transport filter exact requires unit scale and integer translation; every other map requires bilinear');
|
|
73
81
|
}
|
|
74
82
|
}
|
|
83
|
+
if (o.split !== undefined) {
|
|
84
|
+
const s = o.split as Record<string, unknown> | null;
|
|
85
|
+
const splitFields = ['occluder', 'image', 'sha256', 'covered_px', 'visible_px'];
|
|
86
|
+
if (typeof s !== 'object' || s === null || Array.isArray(s) || Object.keys(s).length !== splitFields.length || splitFields.some(k => !(k in s)) ||
|
|
87
|
+
!Number.isSafeInteger(s.occluder) || (s.occluder as number) < 0 ||
|
|
88
|
+
typeof s.image !== 'string' || !s.image.trim() ||
|
|
89
|
+
typeof s.sha256 !== 'string' || !/^[0-9a-f]{64}$/.test(s.sha256) ||
|
|
90
|
+
['covered_px', 'visible_px'].some(k => !Number.isSafeInteger(s[k]) || (s[k] as number) <= 0)) {
|
|
91
|
+
fail('split requires exactly nonnegative safe integer occluder, nonempty image, lowercase SHA-256 sha256, and positive safe integers covered_px and visible_px');
|
|
92
|
+
}
|
|
93
|
+
}
|
|
75
94
|
const key = JSON.stringify([o.run, o.tag, o.run_dir]);
|
|
76
95
|
if (seen.has(key)) fail(`duplicate source ${key}; each source must appear once`);
|
|
77
96
|
seen.add(key);
|