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/partscheck.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { createHash } from 'node:crypto';
|
|
|
3
3
|
import { existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, realpathSync, renameSync, rmSync, writeFileSync } from 'node:fs';
|
|
4
4
|
import { dirname, isAbsolute, join, relative, resolve, sep } from 'node:path';
|
|
5
5
|
import { failLine, PartsError, type Problem } from './errors.ts';
|
|
6
|
-
import { FIGURE_MASK_FILE, partImageReference, partsFileReferences, readParts, type PartsFile } from './parts.ts';
|
|
6
|
+
import { FIGURE_MASK_FILE, orderedSourceOverlap, orderedSourceVisibility, partImageReference, partsFileReferences, readParts, type PartsFile, type PartSourceOverlap, type PartSourceVisibility } from './parts.ts';
|
|
7
7
|
import { readPng } from './raster/png.ts';
|
|
8
8
|
import { RasterError } from './raster/types.ts';
|
|
9
9
|
import { NotAPngError } from '@rig-c/core/png';
|
|
@@ -29,6 +29,7 @@ export const PARTS_CHECK_CODES: readonly string[] = [
|
|
|
29
29
|
'PARTS_COUNTS_ADD_UP',
|
|
30
30
|
'PARTS_CUT_FROM_KNOWN',
|
|
31
31
|
'PARTS_DEPTH_REFERENCE',
|
|
32
|
+
'PARTS_DRAW_ORDER_INVERTED',
|
|
32
33
|
'PARTS_FIELD_PRESENT',
|
|
33
34
|
'PARTS_FIELD_TYPE',
|
|
34
35
|
'PARTS_FILE_PRESENT',
|
|
@@ -76,14 +77,22 @@ export interface PartsCheckInput {
|
|
|
76
77
|
}
|
|
77
78
|
interface Refusal extends Problem { found: unknown; required: unknown }
|
|
78
79
|
interface Hole { code: string; object: string; reason: string; line: string }
|
|
79
|
-
interface CoverRow { code:
|
|
80
|
+
interface CoverRow { code: 'PARTS_UNPLANNED_SOURCE_COVER'; set: string | null; part: string; source: string; count: number; spans: unknown[] }
|
|
81
|
+
interface MaterialRow {
|
|
82
|
+
code: 'PARTS_INVENTED_MATERIAL';
|
|
83
|
+
set: string | null;
|
|
84
|
+
part: string;
|
|
85
|
+
occluded_px: number;
|
|
86
|
+
undecided_px: PartSourceVisibility['undecided_px'];
|
|
87
|
+
}
|
|
88
|
+
type OverlapRow = { code: 'PARTS_DRAW_ORDER_INVERTED'; set: string | null; part: string } & PartSourceOverlap;
|
|
80
89
|
export interface PartsCheckReport {
|
|
81
90
|
spec: typeof PARTS_CHECK_SPEC;
|
|
82
91
|
stage: 'parts';
|
|
83
92
|
inputs: PartsCheckInput[];
|
|
84
93
|
parts: { primary: string[]; alternates: unknown[]; source_cover: unknown; painting_allocation: unknown[] };
|
|
85
94
|
refusals: Refusal[];
|
|
86
|
-
reported: CoverRow[];
|
|
95
|
+
reported: (CoverRow | MaterialRow | OverlapRow)[];
|
|
87
96
|
holes: Hole[];
|
|
88
97
|
exit_code: number;
|
|
89
98
|
}
|
|
@@ -237,12 +246,30 @@ export function checkPartsStage(input: string, output: string, log: (line: strin
|
|
|
237
246
|
before.sort((a, b) => a.path < b.path ? -1 : a.path > b.path ? 1 : 0);
|
|
238
247
|
const holes: Hole[] = [];
|
|
239
248
|
const hole = (code: string, at: string, reason: string): void => { holes.push({ code, object: at, reason, line: `${code}: HOLE — ${reason}` }); };
|
|
240
|
-
|
|
249
|
+
const partRows = file === undefined ? [] : [
|
|
250
|
+
...file.parts.map(part => ({ part, set: null, path: partImageReference(part).path })),
|
|
251
|
+
...(file.alternates ?? []).flatMap(set => set.records.map(part => ({ part, set: set.name, path: partImageReference(part, set.name).path }))),
|
|
252
|
+
];
|
|
253
|
+
const materialRows: MaterialRow[] = partRows.flatMap(({ part, set }) => {
|
|
254
|
+
if (part.source_visibility === undefined) return [];
|
|
255
|
+
const { occluded_px, undecided_px } = orderedSourceVisibility(part.source_visibility);
|
|
256
|
+
return [{ code: 'PARTS_INVENTED_MATERIAL', set, part: part.name, occluded_px, undecided_px }];
|
|
257
|
+
});
|
|
258
|
+
const overlapRows: OverlapRow[] = partRows.flatMap(({ part, set }) => (part.source_overlaps ?? [])
|
|
259
|
+
.map(row => ({ code: 'PARTS_DRAW_ORDER_INVERTED' as const, set, part: part.name, ...orderedSourceOverlap(row) })));
|
|
260
|
+
for (const [code, reason] of PARTS_QUALITY_HOLES) {
|
|
261
|
+
if (code !== 'PARTS_INVENTED_MATERIAL' || materialRows.length === 0) {
|
|
262
|
+
hole(code, 'parts', reason);
|
|
263
|
+
} else if (materialRows.length < partRows.length) {
|
|
264
|
+
const unmeasured = partRows.filter(({ part }) => part.source_visibility === undefined).map(({ part, set }) => {
|
|
265
|
+
const location = set === null ? 'primary set' : `alternate set ${JSON.stringify(set)}`;
|
|
266
|
+
return `part ${JSON.stringify(part.name)} in ${location}`;
|
|
267
|
+
});
|
|
268
|
+
const noun = unmeasured.length === 1 ? 'part lacks' : 'parts lack';
|
|
269
|
+
hole(code, 'parts', `${unmeasured.length} ${noun} validated source_visibility: ${unmeasured.join(', ')}`);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
241
272
|
if (file !== undefined) {
|
|
242
|
-
const partRows = [
|
|
243
|
-
...file.parts.map(part => ({ part, path: partImageReference(part).path })),
|
|
244
|
-
...(file.alternates ?? []).flatMap(set => set.records.map(part => ({ part, path: partImageReference(part, set.name).path }))),
|
|
245
|
-
];
|
|
246
273
|
for (const { part, path } of partRows) {
|
|
247
274
|
try {
|
|
248
275
|
const image = readPng(join(home, path));
|
|
@@ -395,7 +422,7 @@ export function checkPartsStage(input: string, output: string, log: (line: strin
|
|
|
395
422
|
spec: PARTS_CHECK_SPEC, stage: 'parts', inputs: before,
|
|
396
423
|
parts: { primary: file?.parts.map(part => part.name) ?? [], alternates: canonical(file?.alternates ?? []) as unknown[],
|
|
397
424
|
source_cover: canonical(cover), painting_allocation: allocations },
|
|
398
|
-
refusals, reported, holes, exit_code: refusals.length === 0 ? 0 : 1,
|
|
425
|
+
refusals, reported: [...reported, ...materialRows, ...overlapRows], holes, exit_code: refusals.length === 0 ? 0 : 1,
|
|
399
426
|
};
|
|
400
427
|
if (JSON.stringify(before) !== JSON.stringify(inventory(false))) {
|
|
401
428
|
fatal('PARTS_CHECK_INPUT_CHANGED', '--parts', 'input inventory changed during the read; no report was published');
|
|
@@ -416,6 +443,20 @@ export function checkPartsStage(input: string, output: string, log: (line: strin
|
|
|
416
443
|
}
|
|
417
444
|
for (const refusal of refusals) log(failLine(refusal));
|
|
418
445
|
for (const row of reported) log(`${row.code}: REPORTED part "${row.part}" — ${row.source}, ${row.count} px`);
|
|
446
|
+
for (const row of materialRows) {
|
|
447
|
+
const set = row.set === null ? '' : ` in set "${row.set}"`;
|
|
448
|
+
const undecided = Object.entries(row.undecided_px).map(([reason, px]) => `${reason} ${px}`).join(', ');
|
|
449
|
+
log(`${row.code}: REPORTED part "${row.part}"${set} — occluded ${row.occluded_px} px, undecided (${undecided})`);
|
|
450
|
+
}
|
|
451
|
+
for (const row of overlapRows) {
|
|
452
|
+
const set = row.set === null ? '' : ` in set "${row.set}"`;
|
|
453
|
+
const undecided = Object.entries(row.undecided_px).map(([reason, px]) => `${reason} ${px}`).join(', ');
|
|
454
|
+
const layers = row.source_layers.length === 0 ? '' : ` (${row.source_layers.join(', ')})`;
|
|
455
|
+
const native = row.native_samples === undefined ? '' :
|
|
456
|
+
`; native sample witnesses ${row.native_samples.inverted_px} of ${row.native_samples.overlap_px} attributable overlapping px`;
|
|
457
|
+
log(`${row.code}: REPORTED part "${row.part}" behind part "${row.front}"${set} — inverted ${row.inverted_px} of ${row.overlap_px} overlapping px${layers}; ` +
|
|
458
|
+
`agreeing ${row.agreeing_px}, unordered ${row.unordered_px}, undecided (${undecided}), unrecorded ${row.unrecorded_px}${native}`);
|
|
459
|
+
}
|
|
419
460
|
for (const row of holes) log(row.line);
|
|
420
461
|
return report;
|
|
421
462
|
}
|
|
@@ -0,0 +1,380 @@
|
|
|
1
|
+
/** Contributor identities and state sets, carried only while assembling a recorded run. */
|
|
2
|
+
import { OPAQUE_ALPHA_ABOVE } from './layers.ts';
|
|
3
|
+
import type { PartSourceOverlap, PartSourceVisibility } from './parts.ts';
|
|
4
|
+
import { visitWarpSupport, type ScaleTranslate, type WarpFilter } from './raster/warp.ts';
|
|
5
|
+
import type { Raster } from './raster/types.ts';
|
|
6
|
+
import { VISIBILITY_STATE } from './visibility.ts';
|
|
7
|
+
import { SOURCE_STATE, sourceStateCounts } from './sourcestates.ts';
|
|
8
|
+
|
|
9
|
+
const UNRECORDED = 1 << VISIBILITY_STATE.unrecorded;
|
|
10
|
+
const VISIBLE = 1 << VISIBILITY_STATE.visible;
|
|
11
|
+
const OCCLUDED = 1 << VISIBILITY_STATE.occluded;
|
|
12
|
+
const PARTIAL_TOP = 1 << VISIBILITY_STATE.partialTop;
|
|
13
|
+
const DEPTH_TIE = 1 << VISIBILITY_STATE.depthTie;
|
|
14
|
+
const decided = (states: number): boolean => states === VISIBLE || states === OCCLUDED;
|
|
15
|
+
|
|
16
|
+
interface Material { states: number; layers: readonly string[] }
|
|
17
|
+
interface Box { x0: number; y0: number; x1: number; y1: number }
|
|
18
|
+
interface NativeSource extends Box { states: Uint8Array }
|
|
19
|
+
interface Registration {
|
|
20
|
+
map: ScaleTranslate;
|
|
21
|
+
sourceWidth: number;
|
|
22
|
+
sourceHeight: number;
|
|
23
|
+
width: number;
|
|
24
|
+
height: number;
|
|
25
|
+
area: number;
|
|
26
|
+
xs: Uint32Array;
|
|
27
|
+
ys: Uint32Array;
|
|
28
|
+
sources: NativeSource[];
|
|
29
|
+
}
|
|
30
|
+
interface DirectPixels { registration: Registration; refs: Uint32Array }
|
|
31
|
+
interface Pixels {
|
|
32
|
+
rgb: Uint32Array;
|
|
33
|
+
alpha: Uint32Array;
|
|
34
|
+
origin?: Box;
|
|
35
|
+
direct?: DirectPixels;
|
|
36
|
+
}
|
|
37
|
+
type NativeSamples = NonNullable<PartSourceOverlap['native_samples']>;
|
|
38
|
+
|
|
39
|
+
/** Boundaries of the native integer sample origins in each half-open rig cell. */
|
|
40
|
+
function cellStarts(sourceSize: number, size: number, scale: number, translate: number): Uint32Array {
|
|
41
|
+
const starts = new Uint32Array(size + 1);
|
|
42
|
+
let sample = 0;
|
|
43
|
+
for (let boundary = 0; boundary <= size; boundary++) {
|
|
44
|
+
while (sample < sourceSize && scale * sample + translate < boundary) sample++;
|
|
45
|
+
starts[boundary] = sample;
|
|
46
|
+
}
|
|
47
|
+
return starts;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function rgba(image: Raster, p: number): number {
|
|
51
|
+
const i = p * 4, d = image.data;
|
|
52
|
+
return d[i] | (d[i + 1] << 8) | (d[i + 2] << 16) | (d[i + 3] << 24);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function countNative(counts: NativeSamples, back: DirectPixels, b: number, front: DirectPixels, f: number, x: number, y: number): void {
|
|
56
|
+
const { width, height, area, sources, xs, ys } = back.registration;
|
|
57
|
+
const br = back.refs[b] - 1, fr = front.refs[f] - 1;
|
|
58
|
+
if (br < 0 || fr < 0 || x < 0 || y < 0 || x >= width || y >= height) return;
|
|
59
|
+
const p = y * width + x;
|
|
60
|
+
if (br % area !== p || fr % area !== p) return;
|
|
61
|
+
const rear = sources[Math.floor(br / area)], ahead = sources[Math.floor(fr / area)];
|
|
62
|
+
// Each native coordinate belongs to one rig cell, so scanning each accepted
|
|
63
|
+
// cell once also deduplicates physical coordinates within this part pair.
|
|
64
|
+
const x0 = Math.max(xs[x], rear.x0, ahead.x0), x1 = Math.min(xs[x + 1], rear.x1, ahead.x1);
|
|
65
|
+
const y0 = Math.max(ys[y], rear.y0, ahead.y0), y1 = Math.min(ys[y + 1], rear.y1, ahead.y1);
|
|
66
|
+
for (let sy = y0; sy < y1; sy++) for (let sx = x0; sx < x1; sx++) {
|
|
67
|
+
const rs = rear.states[(sy - rear.y0) * (rear.x1 - rear.x0) + sx - rear.x0];
|
|
68
|
+
const fs = ahead.states[(sy - ahead.y0) * (ahead.x1 - ahead.x0) + sx - ahead.x0];
|
|
69
|
+
if (rs === 0 || fs === 0) continue;
|
|
70
|
+
counts.overlap_px++;
|
|
71
|
+
if (rs === VISIBLE && fs === OCCLUDED) counts.inverted_px++;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** A placed part in draw order: its rig-space box and its output image. */
|
|
76
|
+
export interface PlacedImage { name: string; x: number; y: number; image: Raster }
|
|
77
|
+
|
|
78
|
+
/** ID zero denotes material outside the record, with no known layer. */
|
|
79
|
+
export class PartVisibility {
|
|
80
|
+
private readonly materials: Material[] = [{ states: UNRECORDED, layers: [] }];
|
|
81
|
+
private readonly ids = new Map([[JSON.stringify([UNRECORDED, []]), 0]]);
|
|
82
|
+
private readonly combinations = new Map<number, Map<number, number>>();
|
|
83
|
+
private readonly images = new WeakMap<Raster, Pixels>();
|
|
84
|
+
private readonly registrations = new WeakMap<object, Registration>();
|
|
85
|
+
private readonly directSamples = new WeakMap<readonly [number, number], { registration: Registration; ref: number; rgba: number }>();
|
|
86
|
+
|
|
87
|
+
private id(states: number, layers: readonly string[]): number {
|
|
88
|
+
const key = JSON.stringify([states, layers]);
|
|
89
|
+
const previous = this.ids.get(key);
|
|
90
|
+
if (previous !== undefined) return previous;
|
|
91
|
+
const id = this.materials.length;
|
|
92
|
+
this.materials.push({ states, layers });
|
|
93
|
+
this.ids.set(key, id);
|
|
94
|
+
return id;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
union(a: number, b: number): number {
|
|
98
|
+
if (a === b) return a;
|
|
99
|
+
const first = Math.min(a, b), second = Math.max(a, b);
|
|
100
|
+
let row = this.combinations.get(first);
|
|
101
|
+
if (row === undefined) this.combinations.set(first, row = new Map());
|
|
102
|
+
const previous = row.get(second);
|
|
103
|
+
if (previous !== undefined) return previous;
|
|
104
|
+
const am = this.materials[a], bm = this.materials[b];
|
|
105
|
+
const combined = this.id(am.states | bm.states, [...new Set([...am.layers, ...bm.layers])].sort());
|
|
106
|
+
row.set(second, combined);
|
|
107
|
+
return combined;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
private plane(image: Raster): Pixels {
|
|
111
|
+
let pixels = this.images.get(image);
|
|
112
|
+
if (pixels === undefined) {
|
|
113
|
+
const size = image.width * image.height;
|
|
114
|
+
pixels = { rgb: new Uint32Array(size), alpha: new Uint32Array(size) };
|
|
115
|
+
this.images.set(image, pixels);
|
|
116
|
+
}
|
|
117
|
+
return pixels;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
layer(image: Raster, name: string, states?: Uint8Array): void {
|
|
121
|
+
const pixels = this.plane(image);
|
|
122
|
+
this.forgetNative(image);
|
|
123
|
+
if (states?.length === pixels.alpha.length) pixels.origin = { x0: 0, y0: 0, x1: image.width, y1: image.height };
|
|
124
|
+
const ids = Object.values(VISIBILITY_STATE).map(state => this.id(1 << state, [name]));
|
|
125
|
+
if (states === undefined) {
|
|
126
|
+
pixels.rgb.fill(ids[0]);
|
|
127
|
+
pixels.alpha.fill(ids[0]);
|
|
128
|
+
} else for (let p = 0; p < states.length; p++) {
|
|
129
|
+
pixels.rgb[p] = pixels.alpha[p] = ids[states[p]];
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
clone(from: Raster, to: Raster): void {
|
|
134
|
+
const source = this.images.get(from);
|
|
135
|
+
if (source === undefined) return;
|
|
136
|
+
const target: Pixels = { rgb: source.rgb.slice(), alpha: source.alpha.slice() };
|
|
137
|
+
if (from.width === to.width && from.height === to.height) {
|
|
138
|
+
if (source.origin !== undefined) target.origin = source.origin;
|
|
139
|
+
const direct = source.direct;
|
|
140
|
+
if (direct !== undefined) target.direct = { registration: direct.registration, refs: direct.refs.slice() };
|
|
141
|
+
}
|
|
142
|
+
this.images.set(to, target);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** Drop native evidence for edits that leave the existing material tally intact. */
|
|
146
|
+
forgetNative(image: Raster, p?: number): void {
|
|
147
|
+
const target = this.images.get(image);
|
|
148
|
+
if (target === undefined) return;
|
|
149
|
+
delete target.origin;
|
|
150
|
+
if (p === undefined) delete target.direct;
|
|
151
|
+
else if (target.direct !== undefined) target.direct.refs[p] = 0;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
private directPlane(image: Raster, target: Pixels, registration: Registration): DirectPixels {
|
|
155
|
+
return target.direct ??= { registration, refs: new Uint32Array(image.width * image.height) };
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
private writeDirect(image: Raster, target: Pixels, p: number, registration?: Registration, ref = 0): void {
|
|
159
|
+
delete target.origin;
|
|
160
|
+
if (registration !== undefined && ref !== 0) this.directPlane(image, target, registration);
|
|
161
|
+
if (target.direct !== undefined) target.direct.refs[p] = target.direct.registration === registration ? ref : 0;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
copy(to: Raster, p: number, from: Raster, q = p, channels: 'both' | 'rgb' | 'alpha' = 'both'): void {
|
|
165
|
+
const source = this.images.get(from);
|
|
166
|
+
const target = this.plane(to);
|
|
167
|
+
if (channels !== 'alpha') target.rgb[p] = source?.rgb[q] ?? 0;
|
|
168
|
+
if (channels !== 'rgb') target.alpha[p] = source?.alpha[q] ?? 0;
|
|
169
|
+
const direct = channels === 'both' ? source?.direct : undefined;
|
|
170
|
+
this.writeDirect(to, target, p, direct?.registration, direct?.refs[q]);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
rows(to: Raster, from: Raster, x: number, y: number, width: number, height: number, dx = 0, dy = 0): void {
|
|
174
|
+
const source = this.images.get(from);
|
|
175
|
+
const fresh = !this.images.has(to);
|
|
176
|
+
if (source === undefined) {
|
|
177
|
+
const target = this.images.get(to);
|
|
178
|
+
if (target !== undefined) {
|
|
179
|
+
delete target.origin;
|
|
180
|
+
for (let row = 0; row < height; row++) {
|
|
181
|
+
const at = (row + dy) * to.width + dx;
|
|
182
|
+
target.direct?.refs.fill(0, at, at + width);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
const target = this.plane(to);
|
|
188
|
+
const origin = source.origin;
|
|
189
|
+
delete target.origin;
|
|
190
|
+
if (fresh && origin !== undefined) {
|
|
191
|
+
const box = {
|
|
192
|
+
x0: Math.max(x, origin.x0) - x + dx, y0: Math.max(y, origin.y0) - y + dy,
|
|
193
|
+
x1: Math.min(x + width, origin.x1) - x + dx, y1: Math.min(y + height, origin.y1) - y + dy,
|
|
194
|
+
};
|
|
195
|
+
if (box.x1 > box.x0 && box.y1 > box.y0) target.origin = box;
|
|
196
|
+
}
|
|
197
|
+
const direct = source.direct;
|
|
198
|
+
if (direct !== undefined) this.directPlane(to, target, direct.registration);
|
|
199
|
+
const dest = target.direct;
|
|
200
|
+
for (let row = 0; row < height; row++) {
|
|
201
|
+
const start = (row + y) * from.width + x;
|
|
202
|
+
const at = (row + dy) * to.width + dx;
|
|
203
|
+
target.rgb.set(source.rgb.subarray(start, start + width), at);
|
|
204
|
+
target.alpha.set(source.alpha.subarray(start, start + width), at);
|
|
205
|
+
if (dest === undefined) continue;
|
|
206
|
+
if (direct?.registration === dest.registration) dest.refs.set(direct.refs.subarray(start, start + width), at);
|
|
207
|
+
else dest.refs.fill(0, at, at + width);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
sample(image: Raster, p: number): readonly [number, number] {
|
|
212
|
+
const pixels = this.images.get(image);
|
|
213
|
+
const sample = [pixels?.rgb[p] ?? 0, pixels?.alpha[p] ?? 0] as const;
|
|
214
|
+
const direct = pixels?.direct, ref = direct?.refs[p] ?? 0;
|
|
215
|
+
if (direct !== undefined && ref !== 0) this.directSamples.set(sample, { registration: direct.registration, ref, rgba: rgba(image, p) });
|
|
216
|
+
return sample;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
write(image: Raster, p: number, material: readonly [number, number]): void {
|
|
220
|
+
const pixels = this.plane(image);
|
|
221
|
+
pixels.rgb[p] = material[0];
|
|
222
|
+
pixels.alpha[p] = material[1];
|
|
223
|
+
const direct = this.directSamples.get(material);
|
|
224
|
+
if (direct !== undefined && direct.rgba === rgba(image, p)) this.writeDirect(image, pixels, p, direct.registration, direct.ref);
|
|
225
|
+
else this.forgetNative(image, p);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
painting(to: Raster, p: number, channels: 'both' | 'rgb', blend = false): void {
|
|
229
|
+
const target = this.images.get(to);
|
|
230
|
+
if (target === undefined) return;
|
|
231
|
+
target.rgb[p] = blend ? this.union(target.rgb[p], 0) : 0;
|
|
232
|
+
if (channels === 'both') target.alpha[p] = 0;
|
|
233
|
+
this.forgetNative(to, p);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
underfill(to: Raster, p: number, head: Raster, full: Raster, headAlpha: number, fullAlpha: number): void {
|
|
237
|
+
const h = this.images.get(head), f = this.images.get(full), target = this.plane(to);
|
|
238
|
+
const hr = h?.rgb[p] ?? 0, fr = f?.rgb[p] ?? 0;
|
|
239
|
+
const fullColor = fullAlpha > 0 || fr !== 0;
|
|
240
|
+
target.rgb[p] = headAlpha === 255 || !fullColor ? hr : headAlpha === 0 ? fr : this.union(hr, fr);
|
|
241
|
+
const ha = h?.alpha[p] ?? 0, fa = f?.alpha[p] ?? 0;
|
|
242
|
+
target.alpha[p] = headAlpha === 255 || headAlpha > fullAlpha ? ha : fullAlpha > headAlpha ? fa : this.union(ha, fa);
|
|
243
|
+
this.forgetNative(to, p);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
private nativeWarp(from: Raster, to: Raster, source: Pixels, target: Pixels, map: ScaleTranslate, fullFrame: object): void {
|
|
247
|
+
const box = source.origin;
|
|
248
|
+
if (box === undefined || map.sx <= 0 || map.sy <= 0 || (map.sx === 1 && map.sy === 1)) return;
|
|
249
|
+
let registration = this.registrations.get(fullFrame);
|
|
250
|
+
if (registration === undefined) {
|
|
251
|
+
registration = {
|
|
252
|
+
map, sourceWidth: from.width, sourceHeight: from.height, width: to.width, height: to.height, area: to.width * to.height,
|
|
253
|
+
xs: cellStarts(from.width, to.width, map.sx, map.tx), ys: cellStarts(from.height, to.height, map.sy, map.ty), sources: [],
|
|
254
|
+
};
|
|
255
|
+
this.registrations.set(fullFrame, registration);
|
|
256
|
+
}
|
|
257
|
+
const { sourceWidth, sourceHeight, width, height, area, sources } = registration;
|
|
258
|
+
const m = registration.map;
|
|
259
|
+
if (sourceWidth !== from.width || sourceHeight !== from.height || width !== to.width || height !== to.height ||
|
|
260
|
+
m.sx !== map.sx || m.sy !== map.sy || m.tx !== map.tx || m.ty !== map.ty) return;
|
|
261
|
+
// Zero is untracked. Each other code identifies one source plane and the
|
|
262
|
+
// raw rig pixel, even after that pixel is cropped or copied elsewhere.
|
|
263
|
+
const base = sources.length * area + 1;
|
|
264
|
+
if (base + area - 1 > 0xffffffff) return;
|
|
265
|
+
const stride = box.x1 - box.x0;
|
|
266
|
+
const states = new Uint8Array(stride * (box.y1 - box.y0));
|
|
267
|
+
for (let y = box.y0; y < box.y1; y++) for (let x = box.x0; x < box.x1; x++) {
|
|
268
|
+
const p = y * sourceWidth + x;
|
|
269
|
+
if (from.data[p * 4 + 3] > OPAQUE_ALPHA_ABOVE) {
|
|
270
|
+
states[(y - box.y0) * stride + x - box.x0] = this.materials[source.alpha[p]].states;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
sources.push({ ...box, states });
|
|
274
|
+
const { refs } = this.directPlane(to, target, registration);
|
|
275
|
+
for (let p = 0; p < area; p++) if (to.data[p * 4 + 3] > OPAQUE_ALPHA_ABOVE) refs[p] = base + p;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
warp(from: Raster, to: Raster, map: ScaleTranslate, filter: WarpFilter, hasColor?: (pixel: number) => boolean, fullFrame?: object): void {
|
|
279
|
+
const source = this.images.get(from);
|
|
280
|
+
this.forgetNative(to);
|
|
281
|
+
if (source === undefined) return;
|
|
282
|
+
const target = this.plane(to);
|
|
283
|
+
visitWarpSupport(from, map, to.width, to.height, filter, (p, taps) => {
|
|
284
|
+
if (to.data[p * 4 + 3] === 0 && hasColor?.(p) !== true) return;
|
|
285
|
+
let rgb: number | undefined;
|
|
286
|
+
let alpha: number | undefined;
|
|
287
|
+
for (const q of taps) {
|
|
288
|
+
if (from.data[q * 4 + 3] === 0) continue;
|
|
289
|
+
const a = source.alpha[q];
|
|
290
|
+
const premultiplied = this.union(source.rgb[q], a);
|
|
291
|
+
rgb = rgb === undefined ? premultiplied : this.union(rgb, premultiplied);
|
|
292
|
+
alpha = alpha === undefined ? a : this.union(alpha, a);
|
|
293
|
+
}
|
|
294
|
+
target.rgb[p] = rgb ?? 0;
|
|
295
|
+
target.alpha[p] = alpha ?? 0;
|
|
296
|
+
});
|
|
297
|
+
if (fullFrame !== undefined) this.nativeWarp(from, to, source, target, map, fullFrame);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/** The material of an output pixel: the union of its colour and alpha contributors. */
|
|
301
|
+
private material(image: Raster, p: number): number {
|
|
302
|
+
const pixels = this.images.get(image);
|
|
303
|
+
return this.union(pixels?.rgb[p] ?? 0, pixels?.alpha[p] ?? 0);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Issue #1474: for each part, every later part (drawn in front of it) whose
|
|
308
|
+
* opaque output pixels (alpha above 8, as `opaque_px`) meet its own. A pixel
|
|
309
|
+
* is inverted when every contributor of the part drawn behind is visible in
|
|
310
|
+
* the record and every contributor of the part drawn in front is occluded:
|
|
311
|
+
* the painting shows the back part there and the draw order shows the front.
|
|
312
|
+
*/
|
|
313
|
+
overlaps(parts: readonly PlacedImage[]): PartSourceOverlap[][] {
|
|
314
|
+
return parts.map((back, i) => parts.slice(i + 1).flatMap((front): PartSourceOverlap[] => {
|
|
315
|
+
const x0 = Math.max(back.x, front.x), y0 = Math.max(back.y, front.y);
|
|
316
|
+
const x1 = Math.min(back.x + back.image.width, front.x + front.image.width);
|
|
317
|
+
const y1 = Math.min(back.y + back.image.height, front.y + front.image.height);
|
|
318
|
+
const row: PartSourceOverlap = {
|
|
319
|
+
front: front.name, source_layers: [], overlap_px: 0, inverted_px: 0, agreeing_px: 0, unordered_px: 0,
|
|
320
|
+
undecided_px: { 'partial-top': 0, 'depth-tie': 0, 'mixed-source-states': 0 }, unrecorded_px: 0,
|
|
321
|
+
};
|
|
322
|
+
const layers = new Set<string>();
|
|
323
|
+
const bd = this.images.get(back.image)?.direct, fd = this.images.get(front.image)?.direct;
|
|
324
|
+
const native = bd !== undefined && fd !== undefined && bd.registration === fd.registration
|
|
325
|
+
? { back: bd, front: fd, counts: { overlap_px: 0, inverted_px: 0 } } : undefined;
|
|
326
|
+
for (let y = y0; y < y1; y++) for (let x = x0; x < x1; x++) {
|
|
327
|
+
const b = (y - back.y) * back.image.width + x - back.x;
|
|
328
|
+
const f = (y - front.y) * front.image.width + x - front.x;
|
|
329
|
+
if (back.image.data[b * 4 + 3] <= OPAQUE_ALPHA_ABOVE || front.image.data[f * 4 + 3] <= OPAQUE_ALPHA_ABOVE) continue;
|
|
330
|
+
row.overlap_px++;
|
|
331
|
+
if (native !== undefined) countNative(native.counts, native.back, b, native.front, f, x, y);
|
|
332
|
+
const bm = this.materials[this.material(back.image, b)], fm = this.materials[this.material(front.image, f)];
|
|
333
|
+
const bs = bm.states, fs = fm.states;
|
|
334
|
+
if (((bs | fs) & UNRECORDED) !== 0) row.unrecorded_px++;
|
|
335
|
+
else if (bs === VISIBLE && fs === OCCLUDED) {
|
|
336
|
+
row.inverted_px++;
|
|
337
|
+
for (const layer of [...bm.layers, ...fm.layers]) layers.add(layer);
|
|
338
|
+
} else if (bs === OCCLUDED && fs === VISIBLE) row.agreeing_px++;
|
|
339
|
+
else if (decided(bs) && decided(fs)) row.unordered_px++;
|
|
340
|
+
else if (bs === PARTIAL_TOP && fs === PARTIAL_TOP) row.undecided_px['partial-top']++;
|
|
341
|
+
else if (bs === DEPTH_TIE && fs === DEPTH_TIE) row.undecided_px['depth-tie']++;
|
|
342
|
+
else row.undecided_px['mixed-source-states']++;
|
|
343
|
+
}
|
|
344
|
+
row.source_layers = [...layers].sort();
|
|
345
|
+
if (native !== undefined && native.counts.overlap_px > 0) row.native_samples = native.counts;
|
|
346
|
+
return row.overlap_px === 0 ? [] : [row];
|
|
347
|
+
}));
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Each texel's source state (#1473): `SOURCE_STATE.none` at alpha 8 or less,
|
|
352
|
+
* otherwise the one state its material's contributors share — unrecorded
|
|
353
|
+
* when any is outside the record, `mixed-source-states` when they differ.
|
|
354
|
+
*/
|
|
355
|
+
states(image: Raster): { codes: Uint8Array; layers: string[] } {
|
|
356
|
+
const codes = new Uint8Array(image.width * image.height);
|
|
357
|
+
const used = new Set<number>();
|
|
358
|
+
for (let p = 0; p < codes.length; p++) {
|
|
359
|
+
if (image.data[p * 4 + 3] <= OPAQUE_ALPHA_ABOVE) continue;
|
|
360
|
+
const id = this.material(image, p);
|
|
361
|
+
used.add(id);
|
|
362
|
+
const { states } = this.materials[id];
|
|
363
|
+
codes[p] = (states & UNRECORDED) !== 0 ? SOURCE_STATE.unrecorded
|
|
364
|
+
: states === VISIBLE ? SOURCE_STATE.visible
|
|
365
|
+
: states === OCCLUDED ? SOURCE_STATE.occluded
|
|
366
|
+
: states === PARTIAL_TOP ? SOURCE_STATE['partial-top']
|
|
367
|
+
: states === DEPTH_TIE ? SOURCE_STATE['depth-tie']
|
|
368
|
+
: SOURCE_STATE['mixed-source-states'];
|
|
369
|
+
}
|
|
370
|
+
const layers = new Set<string>();
|
|
371
|
+
for (const id of used) for (const layer of this.materials[id].layers) layers.add(layer);
|
|
372
|
+
return { codes, layers: [...layers].sort() };
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/** The counts are read off {@link states}, so the map assemble writes beside them holds them exactly. */
|
|
376
|
+
summarize(image: Raster): PartSourceVisibility {
|
|
377
|
+
const { codes, layers } = this.states(image);
|
|
378
|
+
return { source_layers: layers, ...sourceStateCounts(codes) };
|
|
379
|
+
}
|
|
380
|
+
}
|