rig-parts 2.52.0 → 2.53.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 +6 -4
- package/docs/AUTHORING.md +321 -55
- package/docs/STABILITY.md +108 -12
- package/package.json +1 -1
- package/src/assemble.ts +155 -63
- package/src/build.ts +62 -15
- package/src/check.ts +67 -32
- package/src/checkrow.ts +2 -0
- package/src/comfy/plate.ts +6 -0
- package/src/comfy/seethrough.ts +59 -5
- package/src/editedparts.ts +3 -0
- package/src/layers.ts +7 -3
- package/src/motion.ts +1 -0
- package/src/paintingedges.ts +56 -24
- package/src/parts.ts +196 -4
- package/src/partscheck.ts +50 -9
- package/src/partvisibility.ts +380 -0
- package/src/plate.ts +93 -14
- package/src/platecomponents.ts +269 -0
- package/src/raster/warp.ts +21 -4
- package/src/requirements.ts +6 -2
- package/src/runstamp.ts +2 -2
- package/src/scene.ts +70 -9
- package/src/sourceexposure.ts +309 -0
- package/src/sourcestates.ts +126 -0
- package/src/takelayers.ts +6 -1
- package/src/takeregion.ts +11 -5
- package/src/visibility.ts +384 -0
package/src/editedparts.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
/** Rig-space whole-file replacements, applied after the last normal raster pass. */
|
|
2
|
+
import type { PartVisibility } from './partvisibility.ts';
|
|
2
3
|
import { createHash } from 'node:crypto';
|
|
3
4
|
import { readFileSync } from 'node:fs';
|
|
4
5
|
import { dirname, resolve } from 'node:path';
|
|
@@ -156,6 +157,7 @@ export function replaceEditedParts(
|
|
|
156
157
|
painting: Raster,
|
|
157
158
|
owner: PaintingOwner,
|
|
158
159
|
errorLimit: number,
|
|
160
|
+
visibility?: PartVisibility,
|
|
159
161
|
): EditedPartFrontColourReading[] {
|
|
160
162
|
const problems: Problem[] = [], seen = new Set<string>();
|
|
161
163
|
for (const edit of edits) {
|
|
@@ -206,6 +208,7 @@ export function replaceEditedParts(
|
|
|
206
208
|
const offset = p * 4, prior = previous(), priorSeam = preSeam();
|
|
207
209
|
const changed = !rgbaEqual(old.data, offset, edit.image.data, offset);
|
|
208
210
|
const counted = edit.image.data[offset + 3] > OPAQUE_ALPHA_ABOVE;
|
|
211
|
+
if (!changed && counted) visibility?.copy(edit.image, p, old);
|
|
209
212
|
if (changed) {
|
|
210
213
|
metadata.changed_px++;
|
|
211
214
|
if (counted) metadata.changed_counted_px++; else metadata.changed_excluded_px++;
|
package/src/layers.ts
CHANGED
|
@@ -224,7 +224,11 @@ const DEPTH_RULE = 'a finite number (See-through\'s depth, larger = further back
|
|
|
224
224
|
* order, a scale) that silently ignoring would get wrong — so the reader says
|
|
225
225
|
* which key it does not know and stops.
|
|
226
226
|
*/
|
|
227
|
-
export function readWrapperLayers(
|
|
227
|
+
export function readWrapperLayers(
|
|
228
|
+
path: string, binding?: DepthBinding,
|
|
229
|
+
sealedFiles?: ReadonlyMap<string, Buffer>,
|
|
230
|
+
): LayerSet {
|
|
231
|
+
const bytesOf = (file: string): Buffer => sealedFiles?.get(file) ?? readFileSync(file);
|
|
228
232
|
const manifestPath = existsSync(path) && statSync(path).isDirectory() ? join(path, 'layers.json') : path;
|
|
229
233
|
const dir = dirname(manifestPath);
|
|
230
234
|
const problems: Problem[] = [];
|
|
@@ -237,7 +241,7 @@ export function readWrapperLayers(path: string, binding?: DepthBinding): LayerSe
|
|
|
237
241
|
}
|
|
238
242
|
let manifest: unknown;
|
|
239
243
|
try {
|
|
240
|
-
manifest = JSON.parse(
|
|
244
|
+
manifest = JSON.parse(bytesOf(manifestPath).toString('utf8'));
|
|
241
245
|
} catch (err) {
|
|
242
246
|
refuse('LAYERS_MANIFEST_IS_JSON', manifestPath, `does not parse as JSON: ${(err as Error).message}`);
|
|
243
247
|
refuseIfAny(problems);
|
|
@@ -355,7 +359,7 @@ export function readWrapperLayers(path: string, binding?: DepthBinding): LayerSe
|
|
|
355
359
|
refuse('LAYERS_PNG_PRESENT', `layer "${name}"`, `neither ${flat} nor ${byTag} exists; one of them is required`);
|
|
356
360
|
return;
|
|
357
361
|
}
|
|
358
|
-
const bytes = present.map(
|
|
362
|
+
const bytes = present.map(p => new Uint8Array(bytesOf(p)));
|
|
359
363
|
if (bytes.length === 2 && Buffer.compare(Buffer.from(bytes[0]), Buffer.from(bytes[1])) !== 0) {
|
|
360
364
|
refuse('LAYERS_PNG_UNAMBIGUOUS', `layer "${name}"`, `${flat} and ${byTag} both exist with different bytes; exactly one source is required`);
|
|
361
365
|
return;
|
package/src/motion.ts
CHANGED
package/src/paintingedges.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { foreignPaintingReceiver, paintingReceiverAt, type PaintingReceivers } from './paintingowner.ts';
|
|
2
|
+
import type { PartVisibility } from './partvisibility.ts';
|
|
2
3
|
import { OPAQUE_ALPHA_ABOVE } from './layers.ts';
|
|
3
4
|
import { connectedComponents } from './raster/components.ts';
|
|
4
5
|
import { type Ground } from './projectrule.ts';
|
|
@@ -230,6 +231,7 @@ export interface PaintingEdgeOptions {
|
|
|
230
231
|
movingPair?: ((a: number, b: number) => boolean) | null;
|
|
231
232
|
/** Called once per part and pixel whose RGBA changed. */
|
|
232
233
|
written?: (part: number, pixel: number) => void;
|
|
234
|
+
visibility?: PartVisibility;
|
|
233
235
|
/** Issue #996: the painting's ground; absent, `plain` (see the invariant above {@link EDGE_RADIUS}). */
|
|
234
236
|
ground?: Ground;
|
|
235
237
|
}
|
|
@@ -298,7 +300,7 @@ function applyPaintingEdges(
|
|
|
298
300
|
existing?: { parts: Raster[]; defects: Uint8Array },
|
|
299
301
|
readDecision?: (q: Decision, alpha: number) => void,
|
|
300
302
|
): PaintingEdgeResult {
|
|
301
|
-
const allocation = options
|
|
303
|
+
const { owner: allocation, visibility } = options;
|
|
302
304
|
const events: Array<{ p: number; front: number; behind: number; target: number; affected: number[]; status: string; reason: string }> = [];
|
|
303
305
|
const reading: PaintingAllocationReading | undefined = allocation === undefined ? undefined
|
|
304
306
|
: { supplier: allocation.supplier, refused: {}, lineage: allocation.faults, fallbackHeld: 0, coverageTransfers: [] };
|
|
@@ -515,11 +517,12 @@ function applyPaintingEdges(
|
|
|
515
517
|
const at = new Int32Array(W * H).fill(-1);
|
|
516
518
|
decisions.forEach((q, k) => { at[q.p] = k; });
|
|
517
519
|
const alphaOf = new Int16Array(decisions.length);
|
|
520
|
+
const alphaMaterials = visibility === undefined ? undefined : new Uint32Array(decisions.length);
|
|
518
521
|
const stage = (q: Decision): number => (q.inside ? 2 * q.ring : 2 * q.ring - 1);
|
|
519
522
|
const order = decisions.map((_, k) => k).sort((u, v) => stage(decisions[u]) - stage(decisions[v]) || u - v);
|
|
520
523
|
/** The greatest coverage already resolved for the same FRONT among p's 8 neighbours on the given side and ring. */
|
|
521
|
-
const neighbours = (q: Decision, inside: boolean, ring: number): number => {
|
|
522
|
-
let best = 0;
|
|
524
|
+
const neighbours = (q: Decision, inside: boolean, ring: number): [number, number] => {
|
|
525
|
+
let best = 0, material = 0;
|
|
523
526
|
const x = q.p % W;
|
|
524
527
|
const y = (q.p - x) / W;
|
|
525
528
|
for (let dy = -1; dy <= 1; dy++) {
|
|
@@ -528,27 +531,48 @@ function applyPaintingEdges(
|
|
|
528
531
|
const ny = y + dy;
|
|
529
532
|
if ((dx === 0 && dy === 0) || nx < 0 || ny < 0 || nx >= W || ny >= H) continue;
|
|
530
533
|
const j = at[ny * W + nx];
|
|
531
|
-
if (j
|
|
534
|
+
if (j < 0 || decisions[j].front !== q.front || decisions[j].inside !== inside || decisions[j].ring !== ring) continue;
|
|
535
|
+
if (alphaOf[j] > best) {
|
|
536
|
+
best = alphaOf[j];
|
|
537
|
+
material = alphaMaterials?.[j] ?? 0;
|
|
538
|
+
} else if (alphaOf[j] === best && alphaMaterials !== undefined && best > 0) {
|
|
539
|
+
material = visibility!.union(material, alphaMaterials[j]);
|
|
540
|
+
}
|
|
532
541
|
}
|
|
533
542
|
}
|
|
534
|
-
return best;
|
|
543
|
+
return [best, material];
|
|
535
544
|
};
|
|
536
545
|
for (const k of order) {
|
|
537
546
|
const q = decisions[k];
|
|
538
|
-
let alpha = q.matte;
|
|
539
|
-
|
|
547
|
+
let alpha = q.matte, material = 0;
|
|
548
|
+
const take = (other: number, from: number, prefer: boolean): void => {
|
|
549
|
+
if (prefer) { alpha = other; material = from; }
|
|
550
|
+
else if (other === alpha && visibility !== undefined) material = visibility.union(material, from);
|
|
551
|
+
};
|
|
552
|
+
if (q.inside && q.ring > 1) take(q.own, visibility?.sample(parts[q.front], q.p)[1] ?? 0, q.own > alpha);
|
|
540
553
|
else if (q.inside) {
|
|
541
|
-
|
|
542
|
-
|
|
554
|
+
const [adjacent, from] = neighbours(q, false, 1);
|
|
555
|
+
take(adjacent, from, adjacent > alpha);
|
|
556
|
+
if (busy && q.behind === -1) take(q.own, visibility?.sample(parts[q.front], q.p)[1] ?? 0, q.own > alpha);
|
|
557
|
+
} else if (q.ring > 1) {
|
|
558
|
+
const [adjacent, from] = neighbours(q, false, q.ring - 1);
|
|
559
|
+
take(adjacent, from, adjacent < alpha);
|
|
543
560
|
}
|
|
544
|
-
else if (q.ring > 1) alpha = Math.min(alpha, neighbours(q, false, q.ring - 1));
|
|
545
561
|
if (alpha <= OPAQUE_ALPHA_ABOVE) alpha = 0;
|
|
546
562
|
else if (alpha >= 255 - OPAQUE_ALPHA_ABOVE) alpha = 255;
|
|
547
563
|
alphaOf[k] = alpha;
|
|
564
|
+
if (alphaMaterials !== undefined) alphaMaterials[k] = material;
|
|
548
565
|
}
|
|
549
566
|
// Every write is planned first, per part, so a part's pieces can be held before anything lands.
|
|
550
|
-
|
|
551
|
-
|
|
567
|
+
type RGBA = readonly [number, number, number, number];
|
|
568
|
+
type Material = readonly [number, number];
|
|
569
|
+
const planned: Array<Map<number, RGBA>> = parts.map(() => new Map());
|
|
570
|
+
const materials: Array<Map<number, Material>> | undefined = visibility === undefined ? undefined : parts.map(() => new Map());
|
|
571
|
+
const write = (i: number, p: number, rgba: RGBA, material: Material = [0, 0]): void => {
|
|
572
|
+
planned[i].set(p, rgba);
|
|
573
|
+
materials?.[i].set(p, material);
|
|
574
|
+
};
|
|
575
|
+
const plan = (i: number, p: number, r: number, g: number, bl: number, al: number, material?: Material): void => {
|
|
552
576
|
const d = parts[i].data;
|
|
553
577
|
const s = p * 4;
|
|
554
578
|
if (d[s] === r && d[s + 1] === g && d[s + 2] === bl && d[s + 3] === al) return;
|
|
@@ -556,7 +580,7 @@ function applyPaintingEdges(
|
|
|
556
580
|
skipped.support++;
|
|
557
581
|
return;
|
|
558
582
|
}
|
|
559
|
-
|
|
583
|
+
write(i, p, [r, g, bl, al], material);
|
|
560
584
|
};
|
|
561
585
|
/** Per part, the pixels it fills as BEHIND, with the FRONT part over them. */
|
|
562
586
|
const fills: Array<Map<number, number>> = parts.map(() => new Map());
|
|
@@ -565,9 +589,9 @@ function applyPaintingEdges(
|
|
|
565
589
|
const P = [srcr.data[s], srcr.data[s + 1], srcr.data[s + 2]];
|
|
566
590
|
// An existing BEHIND surface keeps its own colour. Unmix FRONT against
|
|
567
591
|
// that same surface so the pair still reconstructs the painting.
|
|
568
|
-
const
|
|
569
|
-
|
|
570
|
-
|
|
592
|
+
const behind = q.behind >= 0 && parts[q.behind].data[s + 3] >= EDGE_OWNER_ALPHA ? parts[q.behind] : undefined;
|
|
593
|
+
const cb = behind === undefined ? q.cb : [behind.data[s], behind.data[s + 1], behind.data[s + 2]];
|
|
594
|
+
const behindRgb = behind === undefined ? 0 : visibility?.sample(behind, q.p)[0] ?? 0;
|
|
571
595
|
const unmixAt = (c: number, a: number): number => Math.min(255, Math.max(0, Math.round(cb[c] + ((P[c] - cb[c]) * 255) / a)));
|
|
572
596
|
// FRONT's colour is clamped to 0..255, so a pixel the two colours cannot mix at that coverage (a painted line darker or
|
|
573
597
|
// lighter than both) would miss the painting: FRONT's coverage rises to the least that brings the composite within half
|
|
@@ -612,10 +636,11 @@ function applyPaintingEdges(
|
|
|
612
636
|
// FRONT keeps its coverage: nothing is planned for it here.
|
|
613
637
|
} else if (alpha === 0) plan(q.front, q.p, 0, 0, 0, 0);
|
|
614
638
|
else if (alpha === 255) plan(q.front, q.p, P[0], P[1], P[2], 255);
|
|
615
|
-
else plan(q.front, q.p, unmix(0), unmix(1), unmix(2), alpha
|
|
639
|
+
else plan(q.front, q.p, unmix(0), unmix(1), unmix(2), alpha,
|
|
640
|
+
visibility === undefined ? undefined : [visibility.union(0, behindRgb), alphaMaterials![k]]);
|
|
616
641
|
if (q.behind >= 0 && alpha < 255) {
|
|
617
642
|
if (alpha === 0) plan(q.behind, q.p, P[0], P[1], P[2], 255);
|
|
618
|
-
else plan(q.behind, q.p, cb[0], cb[1], cb[2], 255);
|
|
643
|
+
else plan(q.behind, q.p, cb[0], cb[1], cb[2], 255, [behindRgb, 0]);
|
|
619
644
|
fills[q.behind].set(q.p, q.front);
|
|
620
645
|
}
|
|
621
646
|
});
|
|
@@ -629,7 +654,7 @@ function applyPaintingEdges(
|
|
|
629
654
|
const pre = parts[i].data.subarray(event.p * 4, event.p * 4 + 4);
|
|
630
655
|
if (pre.every((v, c) => v === rgba[c])) planned[i].delete(event.p);
|
|
631
656
|
else {
|
|
632
|
-
|
|
657
|
+
write(i, event.p, [rgba[0], rgba[1], rgba[2], rgba[3]], visibility?.sample(existing.parts[i], event.p));
|
|
633
658
|
fallback.push({ p: event.p, part: i, desired: Array.from(rgba) });
|
|
634
659
|
}
|
|
635
660
|
}
|
|
@@ -700,7 +725,7 @@ function applyPaintingEdges(
|
|
|
700
725
|
// original pixel, including clean fallbacks and silhouettes.
|
|
701
726
|
const offset = p * 4;
|
|
702
727
|
const original = parts[i].data;
|
|
703
|
-
|
|
728
|
+
write(i, p, [original[offset], original[offset + 1], original[offset + 2], OPAQUE_ALPHA_ABOVE + 1], [visibility?.sample(parts[i], p)[0] ?? 0, 0]);
|
|
704
729
|
}
|
|
705
730
|
skipped.pieces++;
|
|
706
731
|
}
|
|
@@ -720,7 +745,7 @@ function applyPaintingEdges(
|
|
|
720
745
|
const front = fills[i].get(p);
|
|
721
746
|
if (front !== undefined) {
|
|
722
747
|
if (busy && !supports(front, p)) skipped.support++;
|
|
723
|
-
else
|
|
748
|
+
else write(front, p, [srcr.data[p * 4], srcr.data[p * 4 + 1], srcr.data[p * 4 + 2], 255]);
|
|
724
749
|
}
|
|
725
750
|
}
|
|
726
751
|
}
|
|
@@ -746,7 +771,8 @@ function applyPaintingEdges(
|
|
|
746
771
|
// Keep the original FRONT colour at the least coverage that preserves
|
|
747
772
|
// the guarded art bridge. BEHIND already carries its accepted own P.
|
|
748
773
|
const front = parts[event.front].data;
|
|
749
|
-
|
|
774
|
+
write(event.front, event.p, [front[offset], front[offset + 1], front[offset + 2], OPAQUE_ALPHA_ABOVE + 1],
|
|
775
|
+
[visibility?.sample(parts[event.front], event.p)[0] ?? 0, 0]);
|
|
750
776
|
event.status = 'retained-front';
|
|
751
777
|
} else {
|
|
752
778
|
for (const i of event.affected) planned[i].delete(event.p);
|
|
@@ -769,6 +795,7 @@ function applyPaintingEdges(
|
|
|
769
795
|
const d = parts[i].data;
|
|
770
796
|
for (const [p, v] of writes) {
|
|
771
797
|
d.set(v, p * 4);
|
|
798
|
+
if (visibility !== undefined) visibility.write(parts[i], p, materials![i].get(p)!);
|
|
772
799
|
changed[i].data[p] = 1;
|
|
773
800
|
}
|
|
774
801
|
});
|
|
@@ -866,6 +893,7 @@ function applyPaintingEdges(
|
|
|
866
893
|
for (const [p, front] of restore) {
|
|
867
894
|
const s = p * 4;
|
|
868
895
|
parts[front].data.set([srcr.data[s], srcr.data[s + 1], srcr.data[s + 2], 255], s);
|
|
896
|
+
visibility?.painting(parts[front], p, 'both');
|
|
869
897
|
changed[front].data[p] = 1;
|
|
870
898
|
}
|
|
871
899
|
planned.forEach((writes, i) => {
|
|
@@ -901,9 +929,13 @@ function applyPaintingEdges(
|
|
|
901
929
|
|
|
902
930
|
/** Apply the owner-aware plan after evaluating clean fallback on scratch art. */
|
|
903
931
|
export function paintingEdges(parts: Raster[], srcr: Raster, options: PaintingEdgeOptions): PaintingEdgeResult {
|
|
904
|
-
const allocation = options
|
|
932
|
+
const { owner: allocation, visibility } = options;
|
|
905
933
|
if (allocation === undefined) return applyPaintingEdges(parts, srcr, options);
|
|
906
|
-
const scratch = parts.map(r =>
|
|
934
|
+
const scratch = parts.map(r => {
|
|
935
|
+
const copy = { ...r, data: new Uint8ClampedArray(r.data) };
|
|
936
|
+
visibility?.clone(r, copy);
|
|
937
|
+
return copy;
|
|
938
|
+
});
|
|
907
939
|
const decisions: Array<{ p: number; front: number; behind: number; alpha: number }> = [];
|
|
908
940
|
const old = applyPaintingEdges(scratch, srcr, { ...options, owner: undefined, written: undefined }, undefined,
|
|
909
941
|
(q, alpha) => decisions.push({ p: q.p, front: q.front, behind: q.behind, alpha }));
|
package/src/parts.ts
CHANGED
|
@@ -103,6 +103,36 @@ import { readLayerTag } from './tags.ts';
|
|
|
103
103
|
import { edgeLength } from './length.ts';
|
|
104
104
|
import { recordedFocusProblems } from './setfocus.ts';
|
|
105
105
|
|
|
106
|
+
export const SOURCE_UNDECIDED_REASONS = ['partial-top', 'depth-tie', 'mixed-source-states'] as const;
|
|
107
|
+
|
|
108
|
+
export interface PartSourceVisibility {
|
|
109
|
+
source_layers: string[];
|
|
110
|
+
visible_px: number;
|
|
111
|
+
occluded_px: number;
|
|
112
|
+
undecided_px: Record<typeof SOURCE_UNDECIDED_REASONS[number], number>;
|
|
113
|
+
unrecorded_px: number;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** The per-texel state map of a part with `source_visibility` (#1473): `parts/<image>`, bound by its SHA-256. */
|
|
117
|
+
export interface PartSourceStateMap {
|
|
118
|
+
image: string;
|
|
119
|
+
sha256: string;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** One part drawn in front of the carrying part, over pixels both parts make opaque (issue #1474). */
|
|
123
|
+
export interface PartSourceOverlap {
|
|
124
|
+
front: string;
|
|
125
|
+
source_layers: string[];
|
|
126
|
+
overlap_px: number;
|
|
127
|
+
inverted_px: number;
|
|
128
|
+
agreeing_px: number;
|
|
129
|
+
unordered_px: number;
|
|
130
|
+
undecided_px: Record<typeof SOURCE_UNDECIDED_REASONS[number], number>;
|
|
131
|
+
unrecorded_px: number;
|
|
132
|
+
/** Directly attributable overlap and inversions on the registered full run's native grid. */
|
|
133
|
+
native_samples?: { overlap_px: number; inverted_px: number };
|
|
134
|
+
}
|
|
135
|
+
|
|
106
136
|
export interface PartRecord {
|
|
107
137
|
/** Verified donor sources, including sources of cuts, limbs and extensions. */
|
|
108
138
|
taken_layers?: TakenLayerSource[];
|
|
@@ -125,6 +155,10 @@ export interface PartRecord {
|
|
|
125
155
|
w: number;
|
|
126
156
|
h: number;
|
|
127
157
|
opaque_px: number;
|
|
158
|
+
source_visibility?: PartSourceVisibility;
|
|
159
|
+
source_state_map?: PartSourceStateMap;
|
|
160
|
+
/** With `source_visibility` only: every later part this one overlaps, in draw order (issue #1474). */
|
|
161
|
+
source_overlaps?: PartSourceOverlap[];
|
|
128
162
|
/** Absent in a `parts.json` the reference wrote; `assemble` always writes it. */
|
|
129
163
|
visible_px?: number;
|
|
130
164
|
/** Absent in a `parts.json` the reference wrote; `assemble` always writes it. */
|
|
@@ -509,6 +543,9 @@ export const PART_KEYS = [
|
|
|
509
543
|
'w',
|
|
510
544
|
'h',
|
|
511
545
|
'opaque_px',
|
|
546
|
+
'source_visibility',
|
|
547
|
+
'source_state_map',
|
|
548
|
+
'source_overlaps',
|
|
512
549
|
'provenance',
|
|
513
550
|
'painting_extension_px',
|
|
514
551
|
'visible_px',
|
|
@@ -529,10 +566,10 @@ const FRINGE_KEY = 'fringe_pushed_back';
|
|
|
529
566
|
const CUT_FROM_KEY = 'cut_from';
|
|
530
567
|
/** The one part field that is a record of its own: a limb segment's provenance (issue #349), written only on one. */
|
|
531
568
|
const LIMB_KEY = 'limb';
|
|
532
|
-
const COUNT_KEYS = PART_KEYS.slice(2).filter((k) => k !== FRINGE_KEY && k !== CUT_FROM_KEY && k !== LIMB_KEY && k !== 'depth' && k !== 'provenance' && k !== 'taken_layers') as ReadonlyArray<(typeof PART_KEYS)[number]>;
|
|
569
|
+
const COUNT_KEYS = PART_KEYS.slice(2).filter((k) => k !== FRINGE_KEY && k !== CUT_FROM_KEY && k !== LIMB_KEY && k !== 'depth' && k !== 'provenance' && k !== 'taken_layers' && k !== 'source_visibility' && k !== 'source_state_map' && k !== 'source_overlaps') as ReadonlyArray<(typeof PART_KEYS)[number]>;
|
|
533
570
|
/** The port's own counts: all three or none (none = a record the reference wrote). */
|
|
534
571
|
export const VISIBILITY_KEYS = ['visible_px', 'occluded_px', 'visible_not_projected_px'] as const;
|
|
535
|
-
const REQUIRED_KEYS = PART_KEYS.filter((k) => k !== 'painting_extension_px' && !(VISIBILITY_KEYS as readonly string[]).includes(k) && k !== FRINGE_KEY && k !== CUT_FROM_KEY && k !== LIMB_KEY && k !== 'depth' && k !== 'provenance' && k !== 'taken_layers');
|
|
572
|
+
const REQUIRED_KEYS = PART_KEYS.filter((k) => k !== 'painting_extension_px' && !(VISIBILITY_KEYS as readonly string[]).includes(k) && k !== FRINGE_KEY && k !== CUT_FROM_KEY && k !== LIMB_KEY && k !== 'depth' && k !== 'provenance' && k !== 'taken_layers' && k !== 'source_visibility' && k !== 'source_state_map' && k !== 'source_overlaps');
|
|
536
573
|
|
|
537
574
|
/** Where a part came from: a See-through run and a v3 tag or an author-made layer's `author:<label>` (issue #256), or the painting itself for a patch. */
|
|
538
575
|
export type From = { run: 'full' | 'head'; tag: string } | { run: 'painting'; patch: string };
|
|
@@ -673,9 +710,43 @@ function orderedTakenSource(source: TakenLayerSource): TakenLayerSource {
|
|
|
673
710
|
};
|
|
674
711
|
}
|
|
675
712
|
|
|
713
|
+
export function orderedSourceVisibility(value: PartSourceVisibility): PartSourceVisibility {
|
|
714
|
+
return {
|
|
715
|
+
source_layers: [...value.source_layers].sort(),
|
|
716
|
+
visible_px: value.visible_px,
|
|
717
|
+
occluded_px: value.occluded_px,
|
|
718
|
+
undecided_px: {
|
|
719
|
+
'partial-top': value.undecided_px['partial-top'],
|
|
720
|
+
'depth-tie': value.undecided_px['depth-tie'],
|
|
721
|
+
'mixed-source-states': value.undecided_px['mixed-source-states'],
|
|
722
|
+
},
|
|
723
|
+
unrecorded_px: value.unrecorded_px,
|
|
724
|
+
};
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
export function orderedSourceOverlap(value: PartSourceOverlap): PartSourceOverlap {
|
|
728
|
+
return {
|
|
729
|
+
front: value.front,
|
|
730
|
+
source_layers: [...value.source_layers].sort(),
|
|
731
|
+
overlap_px: value.overlap_px,
|
|
732
|
+
inverted_px: value.inverted_px,
|
|
733
|
+
agreeing_px: value.agreeing_px,
|
|
734
|
+
unordered_px: value.unordered_px,
|
|
735
|
+
undecided_px: {
|
|
736
|
+
'partial-top': value.undecided_px['partial-top'],
|
|
737
|
+
'depth-tie': value.undecided_px['depth-tie'],
|
|
738
|
+
'mixed-source-states': value.undecided_px['mixed-source-states'],
|
|
739
|
+
},
|
|
740
|
+
unrecorded_px: value.unrecorded_px,
|
|
741
|
+
...(value.native_samples === undefined ? {} : { native_samples: {
|
|
742
|
+
overlap_px: value.native_samples.overlap_px, inverted_px: value.native_samples.inverted_px,
|
|
743
|
+
} }),
|
|
744
|
+
};
|
|
745
|
+
}
|
|
746
|
+
|
|
676
747
|
function serializePartList(parts: readonly PartRecord[]): Array<Record<string, unknown>> {
|
|
677
748
|
return parts.map((p) =>
|
|
678
|
-
Object.fromEntries(PART_KEYS.filter((k) => p[k] !== undefined).map((k) => [k, k === FRINGE_KEY ? (p.fringe_pushed_back ?? []).map((q) => ({ part: q.part, px: q.px })) : k === LIMB_KEY ? orderedLimb(p.limb as LimbRecord) : k === 'taken_layers' ? p.taken_layers?.map(orderedTakenSource) : k === 'provenance' ? orderedProvenance(p.provenance as PixelProvenance) : p[k]])),
|
|
749
|
+
Object.fromEntries(PART_KEYS.filter((k) => p[k] !== undefined).map((k) => [k, k === FRINGE_KEY ? (p.fringe_pushed_back ?? []).map((q) => ({ part: q.part, px: q.px })) : k === LIMB_KEY ? orderedLimb(p.limb as LimbRecord) : k === 'taken_layers' ? p.taken_layers?.map(orderedTakenSource) : k === 'provenance' ? orderedProvenance(p.provenance as PixelProvenance) : k === 'source_visibility' ? orderedSourceVisibility(p.source_visibility!) : k === 'source_overlaps' ? p.source_overlaps!.map(orderedSourceOverlap) : k === 'source_state_map' ? { image: p.source_state_map!.image, sha256: p.source_state_map!.sha256 } : p[k]])),
|
|
679
750
|
);
|
|
680
751
|
}
|
|
681
752
|
|
|
@@ -744,7 +815,7 @@ export function writeParts(path: string, file: PartsFile): void {
|
|
|
744
815
|
/** A declared PNG path relative to the directory holding parts.json. */
|
|
745
816
|
export interface PartsFileReference {
|
|
746
817
|
path: string;
|
|
747
|
-
role: 'part' | 'alternate' | 'depth' | 'painting' | 'figure_mask';
|
|
818
|
+
role: 'part' | 'alternate' | 'depth' | 'painting' | 'figure_mask' | 'source_state_map';
|
|
748
819
|
}
|
|
749
820
|
|
|
750
821
|
/** Shared with the PNG readers, including their alternate attachment names. */
|
|
@@ -756,6 +827,10 @@ export function depthImageReference(depth: PartDepthReference): PartsFileReferen
|
|
|
756
827
|
return { path: `parts/${depth.image}`, role: 'depth' };
|
|
757
828
|
}
|
|
758
829
|
|
|
830
|
+
export function sourceStateMapReference(map: PartSourceStateMap): PartsFileReference {
|
|
831
|
+
return { path: `parts/${map.image}`, role: 'source_state_map' };
|
|
832
|
+
}
|
|
833
|
+
|
|
759
834
|
function paintingImageReference(painting: PaintingInRig): PartsFileReference {
|
|
760
835
|
return { path: painting.path, role: 'painting' };
|
|
761
836
|
}
|
|
@@ -772,6 +847,7 @@ export function partsFileReferences(dir: string): PartsFileReference[] {
|
|
|
772
847
|
const add = (part: PartRecord, set?: string): void => {
|
|
773
848
|
references.push(partImageReference(part, set));
|
|
774
849
|
if (part.depth !== undefined) references.push(depthImageReference(part.depth));
|
|
850
|
+
if (part.source_state_map !== undefined) references.push(sourceStateMapReference(part.source_state_map));
|
|
775
851
|
};
|
|
776
852
|
for (const part of file.parts) add(part);
|
|
777
853
|
for (const alternate of file.alternates ?? []) {
|
|
@@ -820,7 +896,115 @@ export function readParts(path: string): PartsFile {
|
|
|
820
896
|
return file;
|
|
821
897
|
}
|
|
822
898
|
|
|
899
|
+
/**
|
|
900
|
+
* Pairwise source visibility over overlapping opaque pixels (issue #1474); names resolve within the carrying list.
|
|
901
|
+
* The primary list is the draw order, so a front part comes later in it. An alternate set draws a front-slot part
|
|
902
|
+
* after a target parts.json does not record (`pairs` is given): there a front is another record of the set, and
|
|
903
|
+
* each pair of records is reported once.
|
|
904
|
+
*/
|
|
905
|
+
function checkSourceOverlaps(raw: unknown, r: Record<string, unknown>, names: ReadonlyArray<unknown>, index: number, label: string, fail: (code: string, object: string, detail: string) => void, pairs?: Set<string>): void {
|
|
906
|
+
const at = label + ' source_overlaps';
|
|
907
|
+
if (!('source_visibility' in r)) fail('PARTS_FIELD_PRESENT', `${label} field "source_visibility"`, 'is absent; source_overlaps is written only beside it');
|
|
908
|
+
if (!Array.isArray(raw)) {
|
|
909
|
+
fail('PARTS_FIELD_TYPE', at, `is ${show(raw)}; an array of overlap rows is required`);
|
|
910
|
+
return;
|
|
911
|
+
}
|
|
912
|
+
const count = (n: unknown): n is number => Number.isSafeInteger(n) && Number(n) >= 0;
|
|
913
|
+
const fields = ['front', 'source_layers', 'overlap_px', 'inverted_px', 'agreeing_px', 'unordered_px', 'undecided_px', 'unrecorded_px'];
|
|
914
|
+
let previous = index;
|
|
915
|
+
raw.forEach((row: unknown, i) => {
|
|
916
|
+
const rowAt = `${at}[${i}]`;
|
|
917
|
+
const value = row as Record<string, unknown> | null;
|
|
918
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value) ||
|
|
919
|
+
!fields.every(k => Object.hasOwn(value, k)) || Object.keys(value).some(k => k !== 'native_samples' && !fields.includes(k))) {
|
|
920
|
+
fail('PARTS_FIELD_TYPE', rowAt, `is ${show(row)}; requires ${fields.join(', ')} and accepts optional native_samples`);
|
|
921
|
+
return;
|
|
922
|
+
}
|
|
923
|
+
const front = names.indexOf(value.front);
|
|
924
|
+
if (pairs !== undefined) {
|
|
925
|
+
const key = JSON.stringify([String(r.name), String(value.front)].sort());
|
|
926
|
+
if (typeof value.front !== 'string' || front < 0 || front === index || pairs.has(key)) {
|
|
927
|
+
fail('PARTS_FIELD_TYPE', `${rowAt} front`, `is ${show(value.front)}; another record of the set, each pair of records once, is required`);
|
|
928
|
+
} else pairs.add(key);
|
|
929
|
+
} else if (typeof value.front !== 'string' || front <= previous) {
|
|
930
|
+
const later = names.slice(index + 1).filter(n => typeof n === 'string').map(n => JSON.stringify(n)).join(', ') || 'none';
|
|
931
|
+
fail('PARTS_FIELD_TYPE', `${rowAt} front`, `is ${show(value.front)}; a part drawn later than this one, once each in draw order, is required (later parts: ${later})`);
|
|
932
|
+
} else previous = front;
|
|
933
|
+
const layers = value.source_layers;
|
|
934
|
+
if (!Array.isArray(layers) || layers.some(k => typeof k !== 'string' || !/^(full|head):.+$/.test(k)) ||
|
|
935
|
+
new Set(layers).size !== layers.length) {
|
|
936
|
+
fail('PARTS_FIELD_TYPE', `${rowAt} source_layers`, `is ${show(layers)}; requires unique full: or head: layer names`);
|
|
937
|
+
}
|
|
938
|
+
const undecided = value.undecided_px as Record<string, unknown> | null;
|
|
939
|
+
const counts = ['overlap_px', 'inverted_px', 'agreeing_px', 'unordered_px', 'unrecorded_px'];
|
|
940
|
+
if (undecided === null || typeof undecided !== 'object' || Array.isArray(undecided) ||
|
|
941
|
+
Object.keys(undecided).length !== SOURCE_UNDECIDED_REASONS.length || !SOURCE_UNDECIDED_REASONS.every(k => count(undecided[k])) ||
|
|
942
|
+
!counts.every(k => count(value[k]))) {
|
|
943
|
+
fail('PARTS_FIELD_TYPE', rowAt, `is ${show(row)}; overlap counts must be non-negative safe integers for each class and undecided reason`);
|
|
944
|
+
return;
|
|
945
|
+
}
|
|
946
|
+
const [overlap, inverted, agreeing, unordered, unrecorded] = counts.map(k => value[k] as number);
|
|
947
|
+
const total = inverted + agreeing + unordered + unrecorded + SOURCE_UNDECIDED_REASONS.reduce((n, k) => n + Number(undecided[k]), 0);
|
|
948
|
+
if (overlap === 0 || total !== overlap) fail('PARTS_COUNTS_ADD_UP', rowAt, `classes sum to ${total}; a positive overlap_px equal to that sum is required, found ${overlap}`);
|
|
949
|
+
if (count(r.opaque_px) && overlap > r.opaque_px) fail('PARTS_COUNTS_ADD_UP', rowAt, `overlap_px ${overlap} is above opaque_px ${r.opaque_px}; at most opaque_px is required`);
|
|
950
|
+
if (Array.isArray(layers) && (layers.length === 0) !== (inverted === 0)) {
|
|
951
|
+
fail('PARTS_COUNTS_ADD_UP', `${rowAt} source_layers`, `lists ${layers.length} layer(s) for inverted_px ${inverted}; the layers of the inverted pixels, none exactly when there are none, are required`);
|
|
952
|
+
}
|
|
953
|
+
if (Object.hasOwn(value, 'native_samples')) {
|
|
954
|
+
const native = value.native_samples as Record<string, unknown> | null;
|
|
955
|
+
const nativeAt = `${rowAt} native_samples`;
|
|
956
|
+
if (native === null || typeof native !== 'object' || Array.isArray(native) ||
|
|
957
|
+
Object.keys(native).length !== 2 || !count(native.overlap_px) || !count(native.inverted_px)) {
|
|
958
|
+
fail('PARTS_FIELD_TYPE', nativeAt, `is ${show(native)}; requires exactly overlap_px and inverted_px as non-negative safe integers`);
|
|
959
|
+
} else if (native.overlap_px === 0 || native.inverted_px > native.overlap_px) {
|
|
960
|
+
fail('PARTS_COUNTS_ADD_UP', nativeAt,
|
|
961
|
+
`has inverted_px ${native.inverted_px} and overlap_px ${native.overlap_px}; a positive overlap_px at least as large as inverted_px is required`);
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
});
|
|
965
|
+
}
|
|
966
|
+
|
|
823
967
|
/** Additive ownership census; not another term in the value partition. */
|
|
968
|
+
function checkSourceVisibility(raw: unknown, opaque: unknown, label: string, fail: (code: string, object: string, detail: string) => void): void {
|
|
969
|
+
const value = raw as Record<string, unknown> | null;
|
|
970
|
+
const count = (n: unknown): n is number => Number.isSafeInteger(n) && Number(n) >= 0;
|
|
971
|
+
const fields = ['source_layers', 'visible_px', 'occluded_px', 'undecided_px', 'unrecorded_px'];
|
|
972
|
+
const at = label + ' source_visibility';
|
|
973
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value) ||
|
|
974
|
+
Object.keys(value).length !== fields.length || !fields.every(k => Object.hasOwn(value, k))) {
|
|
975
|
+
fail('PARTS_FIELD_TYPE', at, `is ${show(raw)}; requires exactly ${fields.join(', ')}`);
|
|
976
|
+
return;
|
|
977
|
+
}
|
|
978
|
+
const layers = value.source_layers;
|
|
979
|
+
if (!Array.isArray(layers) || layers.some(k => typeof k !== 'string' || !/^(full|head):.+$/.test(k)) ||
|
|
980
|
+
new Set(layers).size !== layers.length) {
|
|
981
|
+
fail('PARTS_FIELD_TYPE', at, `source_layers is ${show(layers)}; requires unique full: or head: layer names`);
|
|
982
|
+
}
|
|
983
|
+
const undecided = value.undecided_px as Record<string, unknown> | null;
|
|
984
|
+
if (undecided === null || typeof undecided !== 'object' || Array.isArray(undecided) ||
|
|
985
|
+
Object.keys(undecided).length !== SOURCE_UNDECIDED_REASONS.length || !SOURCE_UNDECIDED_REASONS.every(k => count(undecided[k])) ||
|
|
986
|
+
!count(value.visible_px) || !count(value.occluded_px) || !count(value.unrecorded_px)) {
|
|
987
|
+
fail('PARTS_FIELD_TYPE', at, `is ${show(raw)}; visibility counts must be non-negative safe integers for each state and undecided reason`);
|
|
988
|
+
return;
|
|
989
|
+
}
|
|
990
|
+
const total = value.visible_px + value.occluded_px + value.unrecorded_px +
|
|
991
|
+
SOURCE_UNDECIDED_REASONS.reduce((n, k) => n + Number(undecided[k]), 0);
|
|
992
|
+
if (!count(opaque)) fail('PARTS_FIELD_TYPE', at, `opaque_px is ${show(opaque)}; a non-negative safe integer is required`);
|
|
993
|
+
else if (total !== opaque) fail('PARTS_COUNTS_ADD_UP', at, `state counts sum to ${total}; opaque_px ${opaque} is required`);
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
/** The map's reference only; `readSourceStateMap` (src/sourcestates.ts) reads the file it names. */
|
|
997
|
+
function checkSourceStateMap(r: Record<string, unknown>, label: string, fail: (code: string, object: string, detail: string) => void): void {
|
|
998
|
+
const at = label + ' source_state_map';
|
|
999
|
+
const v = r.source_state_map as Record<string, unknown> | null;
|
|
1000
|
+
if (!('source_visibility' in r)) fail('PARTS_FIELD_TYPE', at, 'is present without source_visibility; the map locates source_visibility\'s counts, and a record without them has nothing for it to locate');
|
|
1001
|
+
if (v === null || typeof v !== 'object' || Array.isArray(v) || Object.keys(v).sort().join(',') !== 'image,sha256' ||
|
|
1002
|
+
typeof v.image !== 'string' || !/^[^/\\]+\.png$/.test(v.image) || v.image.startsWith('.') ||
|
|
1003
|
+
typeof v.sha256 !== 'string' || !/^[a-f0-9]{64}$/.test(v.sha256)) {
|
|
1004
|
+
fail('PARTS_FIELD_TYPE', at, `is ${show(v)}; exactly image (one PNG filename in parts/) and sha256 (lowercase 64-hex) are required`);
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
|
|
824
1008
|
function checkPaintingExtensionCount(r: Record<string, unknown>, label: string, fail: (code: string, object: string, detail: string) => void): void {
|
|
825
1009
|
if (!('painting_extension_px' in r)) return;
|
|
826
1010
|
const n = r.painting_extension_px;
|
|
@@ -957,6 +1141,7 @@ function checkParts(raw: unknown, path: string): PartsFile {
|
|
|
957
1141
|
if ('parts' in o && !(Array.isArray(o.parts) && o.parts.length > 0)) fail('PARTS_FIELD_TYPE', `${path} field "parts"`, `is ${show(o.parts)}; a non-empty array is required`);
|
|
958
1142
|
if (Array.isArray(o.parts)) {
|
|
959
1143
|
const seen = new Map<string, { index: number; name: string }>();
|
|
1144
|
+
const partNames = o.parts.map(p => (p as Record<string, unknown> | null)?.name);
|
|
960
1145
|
o.parts.forEach((p, i) => {
|
|
961
1146
|
const at = `${path} parts[${i}]`;
|
|
962
1147
|
if (typeof p !== 'object' || p === null || Array.isArray(p)) {
|
|
@@ -988,6 +1173,9 @@ function checkParts(raw: unknown, path: string): PartsFile {
|
|
|
988
1173
|
const count = (k: string): number | null => (Number.isInteger(r[k]) && (r[k] as number) >= 0 ? (r[k] as number) : null);
|
|
989
1174
|
if ('provenance' in r) checkProvenance(r.provenance, r.opaque_px, label + ' provenance', fail, 'depth' in r, { name: r.name, w: r.w, h: r.h });
|
|
990
1175
|
checkPaintingExtensionCount(r, label, fail);
|
|
1176
|
+
if ('source_visibility' in r) checkSourceVisibility(r.source_visibility, r.opaque_px, label, fail);
|
|
1177
|
+
if ('source_state_map' in r) checkSourceStateMap(r, label, fail);
|
|
1178
|
+
if ('source_overlaps' in r) checkSourceOverlaps(r.source_overlaps, r, partNames, i, label, fail);
|
|
991
1179
|
const [op, vis, occ, vnp] = ['opaque_px', 'visible_px', 'occluded_px', 'visible_not_projected_px'].map(count);
|
|
992
1180
|
if (held.length === VISIBILITY_KEYS.length && op !== null && vis !== null && occ !== null && vnp !== null) {
|
|
993
1181
|
if (vis + occ !== op) fail('PARTS_COUNTS_ADD_UP', `${label}`, `visible_px ${vis} + occluded_px ${occ} = ${vis + occ}; opaque_px ${op} is required`);
|
|
@@ -1212,6 +1400,7 @@ function checkParts(raw: unknown, path: string): PartsFile {
|
|
|
1212
1400
|
}
|
|
1213
1401
|
if (!Array.isArray(set.records)) return;
|
|
1214
1402
|
if (set.records.some((p) => typeof p === 'object' && p !== null && !Array.isArray(p) && typeof p.limb === 'object' && p.limb !== null && ('hybrid' in p.limb || 'band' in p.limb))) checkLimbRecords(set.records, `${at} `, fail, false);
|
|
1403
|
+
const setPairs = new Set<string>();
|
|
1215
1404
|
set.records.forEach((p, j) => {
|
|
1216
1405
|
if (typeof p !== 'object' || p === null || Array.isArray(p)) {
|
|
1217
1406
|
fail('PARTS_FIELD_TYPE', `${at} records[${j}]`, `is ${show(p)}; a part object is required`);
|
|
@@ -1224,6 +1413,9 @@ function checkParts(raw: unknown, path: string): PartsFile {
|
|
|
1224
1413
|
const part817 = typeof r.name === 'string' ? `part "${r.name}"` : `records[${j}]`;
|
|
1225
1414
|
if ('provenance' in r) checkProvenance(r.provenance, r.opaque_px, `${at} ${part817} provenance`, fail, 'depth' in r, { name: r.name, w: r.w, h: r.h });
|
|
1226
1415
|
checkPaintingExtensionCount(r, `${at} ${part817}`, fail);
|
|
1416
|
+
if ('source_visibility' in r) checkSourceVisibility(r.source_visibility, r.opaque_px, `${at} ${part817}`, fail);
|
|
1417
|
+
if ('source_state_map' in r) checkSourceStateMap(r, `${at} ${part817}`, fail);
|
|
1418
|
+
if ('source_overlaps' in r) checkSourceOverlaps(r.source_overlaps, r, (set.records as unknown[]).map(q => (q as Record<string, unknown> | null)?.name), j, `${at} ${part817}`, fail, setPairs);
|
|
1227
1419
|
const l = r[LIMB_KEY];
|
|
1228
1420
|
if (typeof l !== 'object' || l === null || Array.isArray(l) || !('cap' in l)) return;
|
|
1229
1421
|
const part = typeof r.name === 'string' ? `part "${r.name}"` : `records[${j}]`;
|