@rig-c/parts 2.44.0 → 2.46.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/README.md +2 -0
- package/cli.ts +142 -33
- package/docs/AUTHORING.md +441 -100
- package/docs/DEPTH.md +33 -6
- package/docs/MIGRATION.md +2 -0
- package/docs/STABILITY.md +405 -51
- package/package.json +5 -1
- package/src/assemble.ts +7 -2
- package/src/assembly-reading.ts +267 -0
- package/src/boundarydepth.ts +113 -0
- package/src/build.ts +27 -13
- package/src/check.ts +305 -81
- package/src/cloth.ts +14 -3
- package/src/config.ts +78 -11
- package/src/configrelocation.ts +48 -2
- package/src/configwrite.ts +9 -7
- package/src/diffdrafts.ts +593 -0
- package/src/editedparts.ts +11 -2
- package/src/externalreceipt.ts +130 -0
- package/src/maskvalues.ts +21 -0
- package/src/outputpaths.ts +40 -0
- package/src/parts.ts +48 -11
- package/src/plate.ts +2 -6
- package/src/propose.ts +169 -12
- package/src/receiptproducer.ts +41 -0
- package/src/remesh-api.ts +33 -0
- package/src/remesh.ts +197 -19
- package/src/remeshdelta.ts +143 -30
- package/src/rig.ts +95 -21
- package/src/scene.ts +45 -9
- package/src/sceneclips.ts +60 -1
- package/src/turn.ts +24 -3
- package/tools/configwrite.ts +22 -14
- package/tools/edited-part-cli.ts +28 -8
- package/tools/neck-propose.ts +2 -0
|
@@ -0,0 +1,593 @@
|
|
|
1
|
+
/** Draft-to-draft difference report between two assemble or build outputs (issue #1369). */
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
3
|
+
import { existsSync, lstatSync, readFileSync, realpathSync, statSync, writeFileSync } from 'node:fs';
|
|
4
|
+
import { dirname, isAbsolute, join, relative, resolve, sep } from 'node:path';
|
|
5
|
+
import { PartsError, type Problem } from './errors.ts';
|
|
6
|
+
import { partImageReference, readParts, type PartRecord, type PartsFile } from './parts.ts';
|
|
7
|
+
import { decodePngBytes } from './raster/png.ts';
|
|
8
|
+
import type { Raster } from './raster/types.ts';
|
|
9
|
+
|
|
10
|
+
export const DIFF_DRAFTS_SPEC = 'rig-parts-draft-diff/1';
|
|
11
|
+
|
|
12
|
+
export interface DiffDraftSideSummary {
|
|
13
|
+
parts_sha256: string;
|
|
14
|
+
part_count: number;
|
|
15
|
+
composite_sha256: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface DiffDraftBox {
|
|
19
|
+
x: number;
|
|
20
|
+
y: number;
|
|
21
|
+
w: number;
|
|
22
|
+
h: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface DiffDraftBounds {
|
|
26
|
+
x: number;
|
|
27
|
+
y: number;
|
|
28
|
+
w: number;
|
|
29
|
+
h: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface DiffDraftPixels {
|
|
33
|
+
changed_px: number;
|
|
34
|
+
colour_px: number;
|
|
35
|
+
alpha_px: number;
|
|
36
|
+
bounds: DiffDraftBounds | null;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface DiffDraftPartSide {
|
|
40
|
+
sha256: string;
|
|
41
|
+
box: DiffDraftBox;
|
|
42
|
+
index: number;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export type DiffDraftPartStatus = 'same' | 'changed' | 'added' | 'removed';
|
|
46
|
+
|
|
47
|
+
export interface DiffDraftPart {
|
|
48
|
+
name: string;
|
|
49
|
+
status: DiffDraftPartStatus;
|
|
50
|
+
left?: DiffDraftPartSide;
|
|
51
|
+
right?: DiffDraftPartSide;
|
|
52
|
+
pixels_equal?: boolean;
|
|
53
|
+
pixels?: DiffDraftPixels;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface DiffDraftsSummary {
|
|
57
|
+
same: number;
|
|
58
|
+
changed: number;
|
|
59
|
+
added: number;
|
|
60
|
+
removed: number;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface DiffDraftsReport {
|
|
64
|
+
spec: typeof DIFF_DRAFTS_SPEC;
|
|
65
|
+
left: DiffDraftSideSummary;
|
|
66
|
+
right: DiffDraftSideSummary;
|
|
67
|
+
canvas: { width: number; height: number };
|
|
68
|
+
order: 'same' | 'changed';
|
|
69
|
+
parts: DiffDraftPart[];
|
|
70
|
+
composite: DiffDraftPixels;
|
|
71
|
+
summary: DiffDraftsSummary;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export interface DiffDraftPartInput {
|
|
75
|
+
record: Pick<PartRecord, 'name' | 'x' | 'y' | 'w' | 'h'>;
|
|
76
|
+
sha256: string;
|
|
77
|
+
image: Raster;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface DiffDraftSideInput {
|
|
81
|
+
partsSha256: string;
|
|
82
|
+
compositeSha256: string;
|
|
83
|
+
rigSize: readonly [number, number];
|
|
84
|
+
parts: readonly DiffDraftPartInput[];
|
|
85
|
+
recomposite: Raster;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const sha256Hex = (bytes: Uint8Array): string => createHash('sha256').update(bytes).digest('hex');
|
|
89
|
+
|
|
90
|
+
function physicalPath(path: string): string {
|
|
91
|
+
const absolute = resolve(path);
|
|
92
|
+
if (existsSync(absolute)) return realpathSync(absolute);
|
|
93
|
+
const parent = dirname(absolute);
|
|
94
|
+
return parent === absolute ? absolute : join(physicalPath(parent), relative(parent, absolute));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function withinPath(parent: string, target: string): boolean {
|
|
98
|
+
const rel = relative(parent, target);
|
|
99
|
+
return rel === '' || (!isAbsolute(rel) && rel !== '..' && !rel.startsWith(`..${sep}`));
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function pathExistsOrLink(path: string): boolean {
|
|
103
|
+
if (existsSync(path)) return true;
|
|
104
|
+
try {
|
|
105
|
+
lstatSync(path);
|
|
106
|
+
return true;
|
|
107
|
+
} catch {
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function isRegularFile(path: string): boolean {
|
|
113
|
+
try {
|
|
114
|
+
return statSync(path).isFile();
|
|
115
|
+
} catch {
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function isDirectory(path: string): boolean {
|
|
121
|
+
try {
|
|
122
|
+
return statSync(path).isDirectory();
|
|
123
|
+
} catch {
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** True when the two decoded part crops have identical dimensions and identical RGBA bytes at every position. */
|
|
129
|
+
export function cropPixelsEqual(a: Raster, b: Raster): boolean {
|
|
130
|
+
if (a.width !== b.width || a.height !== b.height || a.data.length !== b.data.length) return false;
|
|
131
|
+
for (let i = 0; i < a.data.length; i++) {
|
|
132
|
+
if (a.data[i] !== b.data[i]) return false;
|
|
133
|
+
}
|
|
134
|
+
return true;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Compare two rasters in rig-pixel space over the union of their boxes; positions outside a box count as RGBA 0,0,0,0. */
|
|
138
|
+
export function compareRigPixels(leftBox: DiffDraftBox, leftImage: Raster, rightBox: DiffDraftBox, rightImage: Raster): DiffDraftPixels {
|
|
139
|
+
const x0 = Math.min(leftBox.x, rightBox.x);
|
|
140
|
+
const y0 = Math.min(leftBox.y, rightBox.y);
|
|
141
|
+
const x1 = Math.max(leftBox.x + leftBox.w, rightBox.x + rightBox.w);
|
|
142
|
+
const y1 = Math.max(leftBox.y + leftBox.h, rightBox.y + rightBox.h);
|
|
143
|
+
let changedPx = 0;
|
|
144
|
+
let colourPx = 0;
|
|
145
|
+
let alphaPx = 0;
|
|
146
|
+
let minX = x1;
|
|
147
|
+
let minY = y1;
|
|
148
|
+
let maxX = x0 - 1;
|
|
149
|
+
let maxY = y0 - 1;
|
|
150
|
+
for (let y = y0; y < y1; y++) {
|
|
151
|
+
const inLeftY = y >= leftBox.y && y < leftBox.y + leftBox.h;
|
|
152
|
+
const inRightY = y >= rightBox.y && y < rightBox.y + rightBox.h;
|
|
153
|
+
for (let x = x0; x < x1; x++) {
|
|
154
|
+
let lR = 0, lG = 0, lB = 0, lA = 0;
|
|
155
|
+
if (inLeftY && x >= leftBox.x && x < leftBox.x + leftBox.w) {
|
|
156
|
+
const li = ((y - leftBox.y) * leftBox.w + (x - leftBox.x)) * 4;
|
|
157
|
+
lR = leftImage.data[li];
|
|
158
|
+
lG = leftImage.data[li + 1];
|
|
159
|
+
lB = leftImage.data[li + 2];
|
|
160
|
+
lA = leftImage.data[li + 3];
|
|
161
|
+
}
|
|
162
|
+
let rR = 0, rG = 0, rB = 0, rA = 0;
|
|
163
|
+
if (inRightY && x >= rightBox.x && x < rightBox.x + rightBox.w) {
|
|
164
|
+
const ri = ((y - rightBox.y) * rightBox.w + (x - rightBox.x)) * 4;
|
|
165
|
+
rR = rightImage.data[ri];
|
|
166
|
+
rG = rightImage.data[ri + 1];
|
|
167
|
+
rB = rightImage.data[ri + 2];
|
|
168
|
+
rA = rightImage.data[ri + 3];
|
|
169
|
+
}
|
|
170
|
+
const colourDiff = lR !== rR || lG !== rG || lB !== rB;
|
|
171
|
+
const alphaDiff = lA !== rA;
|
|
172
|
+
if (colourDiff) colourPx++;
|
|
173
|
+
if (alphaDiff) alphaPx++;
|
|
174
|
+
if (colourDiff || alphaDiff) {
|
|
175
|
+
changedPx++;
|
|
176
|
+
if (x < minX) minX = x;
|
|
177
|
+
if (y < minY) minY = y;
|
|
178
|
+
if (x > maxX) maxX = x;
|
|
179
|
+
if (y > maxY) maxY = y;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return {
|
|
184
|
+
changed_px: changedPx,
|
|
185
|
+
colour_px: colourPx,
|
|
186
|
+
alpha_px: alphaPx,
|
|
187
|
+
bounds: changedPx === 0 ? null : { x: minX, y: minY, w: maxX - minX + 1, h: maxY - minY + 1 },
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function validateSideInput(side: DiffDraftSideInput, label: 'left' | 'right', problems: Problem[]): void {
|
|
192
|
+
const [W, H] = side.rigSize;
|
|
193
|
+
if (!Number.isSafeInteger(W) || !Number.isSafeInteger(H) || W < 1 || H < 1) {
|
|
194
|
+
problems.push({
|
|
195
|
+
code: 'DIFF_DRAFTS_INPUT',
|
|
196
|
+
object: `${label}.rig_size`,
|
|
197
|
+
detail: `is ${JSON.stringify(side.rigSize)}; a pair of positive integer canvas dimensions [width, height] is required`,
|
|
198
|
+
});
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
if (!/^[a-f0-9]{64}$/.test(side.partsSha256)) {
|
|
202
|
+
problems.push({
|
|
203
|
+
code: 'DIFF_DRAFTS_INPUT',
|
|
204
|
+
object: `${label}.parts_sha256`,
|
|
205
|
+
detail: `is ${JSON.stringify(side.partsSha256)}; 64 lowercase hex characters are required`,
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
if (!/^[a-f0-9]{64}$/.test(side.compositeSha256)) {
|
|
209
|
+
problems.push({
|
|
210
|
+
code: 'DIFF_DRAFTS_INPUT',
|
|
211
|
+
object: `${label}.composite_sha256`,
|
|
212
|
+
detail: `is ${JSON.stringify(side.compositeSha256)}; 64 lowercase hex characters are required`,
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
if (side.recomposite.width !== W || side.recomposite.height !== H || side.recomposite.data.length < W * H * 4) {
|
|
216
|
+
problems.push({
|
|
217
|
+
code: 'DIFF_DRAFTS_INPUT',
|
|
218
|
+
object: `${label} recomposite_rig.png`,
|
|
219
|
+
detail: `is ${side.recomposite.width}x${side.recomposite.height} with ${side.recomposite.data.length} RGBA bytes; ${W}x${H} matching rig_size is required`,
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
const seen = new Set<string>();
|
|
223
|
+
for (const entry of side.parts) {
|
|
224
|
+
const p = entry.record;
|
|
225
|
+
const dup = seen.has(p.name);
|
|
226
|
+
if (
|
|
227
|
+
!p.name ||
|
|
228
|
+
dup ||
|
|
229
|
+
!/^[a-f0-9]{64}$/.test(entry.sha256) ||
|
|
230
|
+
![p.x, p.y, p.w, p.h].every(Number.isSafeInteger) ||
|
|
231
|
+
p.x < 0 ||
|
|
232
|
+
p.y < 0 ||
|
|
233
|
+
p.w < 1 ||
|
|
234
|
+
p.h < 1 ||
|
|
235
|
+
p.x + p.w > W ||
|
|
236
|
+
p.y + p.h > H ||
|
|
237
|
+
entry.image.width !== p.w ||
|
|
238
|
+
entry.image.height !== p.h ||
|
|
239
|
+
entry.image.data.length < p.w * p.h * 4
|
|
240
|
+
) {
|
|
241
|
+
problems.push({
|
|
242
|
+
code: 'DIFF_DRAFTS_INPUT',
|
|
243
|
+
object: `${label} part "${p.name}"`,
|
|
244
|
+
detail: `box ${p.x},${p.y} ${p.w}x${p.h}, image ${entry.image.width}x${entry.image.height} with ${entry.image.data.length} RGBA bytes, sha256 ${JSON.stringify(entry.sha256)}, duplicate name ${dup}; a unique nonempty name, 64-hex sha256 and an in-canvas matching box on ${W}x${H} are required`,
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
seen.add(p.name);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** Pure measurement over two already-read assemble outputs. */
|
|
252
|
+
export function measureDiffDrafts(left: DiffDraftSideInput, right: DiffDraftSideInput): DiffDraftsReport {
|
|
253
|
+
const problems: Problem[] = [];
|
|
254
|
+
validateSideInput(left, 'left', problems);
|
|
255
|
+
validateSideInput(right, 'right', problems);
|
|
256
|
+
if (
|
|
257
|
+
Number.isSafeInteger(left.rigSize[0]) &&
|
|
258
|
+
Number.isSafeInteger(left.rigSize[1]) &&
|
|
259
|
+
Number.isSafeInteger(right.rigSize[0]) &&
|
|
260
|
+
Number.isSafeInteger(right.rigSize[1]) &&
|
|
261
|
+
(left.rigSize[0] !== right.rigSize[0] || left.rigSize[1] !== right.rigSize[1])
|
|
262
|
+
) {
|
|
263
|
+
problems.push({
|
|
264
|
+
code: 'DIFF_DRAFTS_CANVAS',
|
|
265
|
+
object: 'canvas',
|
|
266
|
+
detail: `left is ${left.rigSize[0]}x${left.rigSize[1]} and right is ${right.rigSize[0]}x${right.rigSize[1]}; identical rig_size dimensions are required because the two outputs are not drafts of one rig`,
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
if (problems.length > 0) throw new PartsError(problems);
|
|
270
|
+
|
|
271
|
+
const [W, H] = left.rigSize;
|
|
272
|
+
const order: 'same' | 'changed' =
|
|
273
|
+
left.parts.length === right.parts.length && left.parts.every((p, i) => p.record.name === right.parts[i].record.name)
|
|
274
|
+
? 'same'
|
|
275
|
+
: 'changed';
|
|
276
|
+
|
|
277
|
+
const leftByName = new Map<string, { entry: DiffDraftPartInput; side: DiffDraftPartSide }>();
|
|
278
|
+
left.parts.forEach((entry, index) => {
|
|
279
|
+
const { x, y, w, h } = entry.record;
|
|
280
|
+
leftByName.set(entry.record.name, { entry, side: { sha256: entry.sha256, box: { x, y, w, h }, index } });
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
const rightByName = new Map<string, { entry: DiffDraftPartInput; side: DiffDraftPartSide }>();
|
|
284
|
+
right.parts.forEach((entry, index) => {
|
|
285
|
+
const { x, y, w, h } = entry.record;
|
|
286
|
+
rightByName.set(entry.record.name, { entry, side: { sha256: entry.sha256, box: { x, y, w, h }, index } });
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
const names = [...new Set([...leftByName.keys(), ...rightByName.keys()])].sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
|
|
290
|
+
const summary: DiffDraftsSummary = { same: 0, changed: 0, added: 0, removed: 0 };
|
|
291
|
+
const parts: DiffDraftPart[] = [];
|
|
292
|
+
|
|
293
|
+
for (const name of names) {
|
|
294
|
+
const l = leftByName.get(name);
|
|
295
|
+
const r = rightByName.get(name);
|
|
296
|
+
if (l === undefined && r !== undefined) {
|
|
297
|
+
summary.added++;
|
|
298
|
+
parts.push({ name, status: 'added', right: r.side });
|
|
299
|
+
continue;
|
|
300
|
+
}
|
|
301
|
+
if (l !== undefined && r === undefined) {
|
|
302
|
+
summary.removed++;
|
|
303
|
+
parts.push({ name, status: 'removed', left: l.side });
|
|
304
|
+
continue;
|
|
305
|
+
}
|
|
306
|
+
const leftSide = l!.side;
|
|
307
|
+
const rightSide = r!.side;
|
|
308
|
+
const sameBox =
|
|
309
|
+
leftSide.box.x === rightSide.box.x &&
|
|
310
|
+
leftSide.box.y === rightSide.box.y &&
|
|
311
|
+
leftSide.box.w === rightSide.box.w &&
|
|
312
|
+
leftSide.box.h === rightSide.box.h;
|
|
313
|
+
if (leftSide.sha256 === rightSide.sha256 && sameBox) {
|
|
314
|
+
summary.same++;
|
|
315
|
+
parts.push({ name, status: 'same', left: leftSide, right: rightSide });
|
|
316
|
+
} else {
|
|
317
|
+
summary.changed++;
|
|
318
|
+
parts.push({
|
|
319
|
+
name,
|
|
320
|
+
status: 'changed',
|
|
321
|
+
left: leftSide,
|
|
322
|
+
right: rightSide,
|
|
323
|
+
pixels_equal: cropPixelsEqual(l!.entry.image, r!.entry.image),
|
|
324
|
+
pixels: compareRigPixels(leftSide.box, l!.entry.image, rightSide.box, r!.entry.image),
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
const canvasBox: DiffDraftBox = { x: 0, y: 0, w: W, h: H };
|
|
330
|
+
const composite = compareRigPixels(canvasBox, left.recomposite, canvasBox, right.recomposite);
|
|
331
|
+
|
|
332
|
+
return {
|
|
333
|
+
spec: DIFF_DRAFTS_SPEC,
|
|
334
|
+
left: { parts_sha256: left.partsSha256, part_count: left.parts.length, composite_sha256: left.compositeSha256 },
|
|
335
|
+
right: { parts_sha256: right.partsSha256, part_count: right.parts.length, composite_sha256: right.compositeSha256 },
|
|
336
|
+
canvas: { width: W, height: H },
|
|
337
|
+
order,
|
|
338
|
+
parts,
|
|
339
|
+
composite,
|
|
340
|
+
summary,
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
export function serializeDiffDrafts(report: DiffDraftsReport): string {
|
|
345
|
+
return `${JSON.stringify(report, null, 2)}\n`;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
interface ResolvedDraftLayout {
|
|
349
|
+
partsJsonPath: string;
|
|
350
|
+
partsBaseDir: string;
|
|
351
|
+
recompositePath: string;
|
|
352
|
+
recompositeLabel: string;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
function resolveDraftLayout(dir: string, flag: '--left' | '--right', problems: Problem[]): ResolvedDraftLayout | null {
|
|
356
|
+
if (!isDirectory(dir)) {
|
|
357
|
+
problems.push({
|
|
358
|
+
code: 'DIFF_DRAFTS_INPUT',
|
|
359
|
+
object: flag,
|
|
360
|
+
detail: `${dir} is not a directory; an assemble or build output directory holding parts.json or rig/parts.json is required`,
|
|
361
|
+
});
|
|
362
|
+
return null;
|
|
363
|
+
}
|
|
364
|
+
const buildManifest = join(dir, 'parts.json');
|
|
365
|
+
const assembleManifest = join(dir, 'rig', 'parts.json');
|
|
366
|
+
const hasBuild = isRegularFile(buildManifest);
|
|
367
|
+
const hasAssemble = isRegularFile(assembleManifest);
|
|
368
|
+
if (!hasBuild && !hasAssemble) {
|
|
369
|
+
problems.push({
|
|
370
|
+
code: 'DIFF_DRAFTS_INPUT',
|
|
371
|
+
object: flag,
|
|
372
|
+
detail: `${dir} holds neither parts.json (build layout) nor rig/parts.json (assemble layout); an assemble or build output directory is required`,
|
|
373
|
+
});
|
|
374
|
+
return null;
|
|
375
|
+
}
|
|
376
|
+
if (hasBuild && hasAssemble) {
|
|
377
|
+
problems.push({
|
|
378
|
+
code: 'DIFF_DRAFTS_INPUT',
|
|
379
|
+
object: flag,
|
|
380
|
+
detail: `${dir} holds both parts.json and rig/parts.json; a single assemble or build output layout is required`,
|
|
381
|
+
});
|
|
382
|
+
return null;
|
|
383
|
+
}
|
|
384
|
+
return hasBuild
|
|
385
|
+
? {
|
|
386
|
+
partsJsonPath: buildManifest,
|
|
387
|
+
partsBaseDir: dir,
|
|
388
|
+
recompositePath: join(dir, 'recomposite_rig.png'),
|
|
389
|
+
recompositeLabel: 'recomposite_rig.png',
|
|
390
|
+
}
|
|
391
|
+
: {
|
|
392
|
+
partsJsonPath: assembleManifest,
|
|
393
|
+
partsBaseDir: join(dir, 'rig'),
|
|
394
|
+
recompositePath: join(dir, 'render', 'recomposite_rig.png'),
|
|
395
|
+
recompositeLabel: 'render/recomposite_rig.png',
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
function readSideDirectory(dir: string, flag: '--left' | '--right', problems: Problem[]): { partsFile: PartsFile | null; input: DiffDraftSideInput | null } {
|
|
400
|
+
const layout = resolveDraftLayout(dir, flag, problems);
|
|
401
|
+
if (layout === null) return { partsFile: null, input: null };
|
|
402
|
+
|
|
403
|
+
let partsBytes: Uint8Array | null = null;
|
|
404
|
+
try {
|
|
405
|
+
partsBytes = new Uint8Array(readFileSync(layout.partsJsonPath));
|
|
406
|
+
} catch (err) {
|
|
407
|
+
problems.push({
|
|
408
|
+
code: 'DIFF_DRAFTS_INPUT',
|
|
409
|
+
object: `${flag} parts.json`,
|
|
410
|
+
detail: `cannot read ${layout.partsJsonPath}: ${JSON.stringify(err instanceof Error ? err.message : err)}; readable parts.json bytes are required`,
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
let partsFile: PartsFile | null = null;
|
|
415
|
+
try {
|
|
416
|
+
partsFile = readParts(layout.partsJsonPath);
|
|
417
|
+
} catch (err) {
|
|
418
|
+
if (err instanceof PartsError) problems.push(...err.problems);
|
|
419
|
+
else {
|
|
420
|
+
problems.push({
|
|
421
|
+
code: 'DIFF_DRAFTS_INPUT',
|
|
422
|
+
object: `${flag} parts.json`,
|
|
423
|
+
detail: `cannot parse ${layout.partsJsonPath}: ${JSON.stringify(err instanceof Error ? err.message : err)}; a valid parts.json is required`,
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
let recomposite: Raster | null = null;
|
|
429
|
+
let compositeBytes: Uint8Array | null = null;
|
|
430
|
+
if (!isRegularFile(layout.recompositePath)) {
|
|
431
|
+
problems.push({
|
|
432
|
+
code: 'DIFF_DRAFTS_INPUT',
|
|
433
|
+
object: `${flag} ${layout.recompositeLabel}`,
|
|
434
|
+
detail: `${layout.recompositePath} is missing; ${layout.recompositeLabel} from the assemble or build output is required`,
|
|
435
|
+
});
|
|
436
|
+
} else {
|
|
437
|
+
try {
|
|
438
|
+
const bytes = new Uint8Array(readFileSync(layout.recompositePath));
|
|
439
|
+
compositeBytes = bytes;
|
|
440
|
+
recomposite = decodePngBytes(bytes, layout.recompositePath);
|
|
441
|
+
} catch (err) {
|
|
442
|
+
problems.push({
|
|
443
|
+
code: 'DIFF_DRAFTS_INPUT',
|
|
444
|
+
object: `${flag} ${layout.recompositeLabel}`,
|
|
445
|
+
detail: `cannot decode PNG ${layout.recompositePath}: ${JSON.stringify(err instanceof Error ? err.message : err)}; valid PNG bytes are required`,
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
if (partsFile !== null && recomposite !== null) {
|
|
451
|
+
const [W, H] = partsFile.rig_size;
|
|
452
|
+
if (recomposite.width !== W || recomposite.height !== H) {
|
|
453
|
+
problems.push({
|
|
454
|
+
code: 'DIFF_DRAFTS_INPUT',
|
|
455
|
+
object: `${flag} ${layout.recompositeLabel}`,
|
|
456
|
+
detail: `is ${recomposite.width}x${recomposite.height}; parts.json rig_size ${W}x${H} is required`,
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
const partInputs: DiffDraftPartInput[] = [];
|
|
462
|
+
if (partsFile !== null) {
|
|
463
|
+
for (const record of partsFile.parts) {
|
|
464
|
+
const rel = partImageReference(record).path;
|
|
465
|
+
const fullPath = join(layout.partsBaseDir, rel);
|
|
466
|
+
if (!isRegularFile(fullPath)) {
|
|
467
|
+
problems.push({
|
|
468
|
+
code: 'DIFF_DRAFTS_INPUT',
|
|
469
|
+
object: `${flag} part "${record.name}"`,
|
|
470
|
+
detail: `${fullPath} is missing; ${rel} matching parts.json is required`,
|
|
471
|
+
});
|
|
472
|
+
continue;
|
|
473
|
+
}
|
|
474
|
+
try {
|
|
475
|
+
const bytes = new Uint8Array(readFileSync(fullPath));
|
|
476
|
+
const image = decodePngBytes(bytes, fullPath);
|
|
477
|
+
if (image.width !== record.w || image.height !== record.h) {
|
|
478
|
+
problems.push({
|
|
479
|
+
code: 'DIFF_DRAFTS_INPUT',
|
|
480
|
+
object: `${flag} part "${record.name}"`,
|
|
481
|
+
detail: `${rel} is ${image.width}x${image.height}; parts.json box ${record.w}x${record.h} is required`,
|
|
482
|
+
});
|
|
483
|
+
continue;
|
|
484
|
+
}
|
|
485
|
+
partInputs.push({ record, sha256: sha256Hex(bytes), image });
|
|
486
|
+
} catch (err) {
|
|
487
|
+
problems.push({
|
|
488
|
+
code: 'DIFF_DRAFTS_INPUT',
|
|
489
|
+
object: `${flag} part "${record.name}"`,
|
|
490
|
+
detail: `cannot decode PNG ${fullPath}: ${JSON.stringify(err instanceof Error ? err.message : err)}; valid PNG bytes are required`,
|
|
491
|
+
});
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
if (partsBytes === null || partsFile === null || compositeBytes === null || recomposite === null || partInputs.length !== partsFile.parts.length) {
|
|
497
|
+
return { partsFile, input: null };
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
return {
|
|
501
|
+
partsFile,
|
|
502
|
+
input: {
|
|
503
|
+
partsSha256: sha256Hex(partsBytes),
|
|
504
|
+
compositeSha256: sha256Hex(compositeBytes),
|
|
505
|
+
rigSize: partsFile.rig_size,
|
|
506
|
+
parts: partInputs,
|
|
507
|
+
recomposite,
|
|
508
|
+
},
|
|
509
|
+
};
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
/** CLI adapter: read two assemble or build outputs, refuse any invalid input or output collision once, and write rig-parts-draft-diff/1. */
|
|
513
|
+
export function diffDraftsCommand(args: string[]): DiffDraftsReport {
|
|
514
|
+
const allowed = ['--left', '--right', '--out'];
|
|
515
|
+
const flags = new Map<string, string>();
|
|
516
|
+
for (let i = 0; i < args.length; i += 2) {
|
|
517
|
+
const key = args[i];
|
|
518
|
+
const value = args[i + 1];
|
|
519
|
+
if (!allowed.includes(key) || flags.has(key) || value === undefined || value.startsWith('--')) {
|
|
520
|
+
throw new PartsError([{ code: 'DIFF_DRAFTS_INPUT', object: 'arguments', detail: `requires unique ${allowed.join(', ')} flags with values; found ${key}` }]);
|
|
521
|
+
}
|
|
522
|
+
if (value.trim().length === 0) {
|
|
523
|
+
throw new PartsError([{ code: 'DIFF_DRAFTS_INPUT', object: key, detail: `is ${JSON.stringify(value)}; a nonempty path is required` }]);
|
|
524
|
+
}
|
|
525
|
+
flags.set(key, value);
|
|
526
|
+
}
|
|
527
|
+
const missing: Problem[] = [];
|
|
528
|
+
for (const key of allowed) {
|
|
529
|
+
if (!flags.has(key)) missing.push({ code: 'DIFF_DRAFTS_INPUT', object: key, detail: 'is missing; an explicit path is required' });
|
|
530
|
+
}
|
|
531
|
+
if (missing.length > 0) throw new PartsError(missing);
|
|
532
|
+
|
|
533
|
+
const leftDir = flags.get('--left')!;
|
|
534
|
+
const rightDir = flags.get('--right')!;
|
|
535
|
+
const outPath = flags.get('--out')!;
|
|
536
|
+
|
|
537
|
+
const problems: Problem[] = [];
|
|
538
|
+
if (pathExistsOrLink(outPath)) {
|
|
539
|
+
problems.push({
|
|
540
|
+
code: 'DIFF_DRAFTS_OUT',
|
|
541
|
+
object: '--out',
|
|
542
|
+
detail: `${outPath} already exists; a fresh output file path that does not exist is required`,
|
|
543
|
+
});
|
|
544
|
+
}
|
|
545
|
+
const realOut = physicalPath(outPath);
|
|
546
|
+
const realLeft = physicalPath(leftDir);
|
|
547
|
+
const realRight = physicalPath(rightDir);
|
|
548
|
+
if (withinPath(realLeft, realOut)) {
|
|
549
|
+
problems.push({
|
|
550
|
+
code: 'DIFF_DRAFTS_OUT',
|
|
551
|
+
object: '--out',
|
|
552
|
+
detail: `${outPath} is inside --left (${leftDir}); an output file path outside both input directories is required`,
|
|
553
|
+
});
|
|
554
|
+
}
|
|
555
|
+
if (withinPath(realRight, realOut)) {
|
|
556
|
+
problems.push({
|
|
557
|
+
code: 'DIFF_DRAFTS_OUT',
|
|
558
|
+
object: '--out',
|
|
559
|
+
detail: `${outPath} is inside --right (${rightDir}); an output file path outside both input directories is required`,
|
|
560
|
+
});
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
const leftLoaded = readSideDirectory(leftDir, '--left', problems);
|
|
564
|
+
const rightLoaded = readSideDirectory(rightDir, '--right', problems);
|
|
565
|
+
|
|
566
|
+
if (leftLoaded.partsFile !== null && rightLoaded.partsFile !== null) {
|
|
567
|
+
const [lW, lH] = leftLoaded.partsFile.rig_size;
|
|
568
|
+
const [rW, rH] = rightLoaded.partsFile.rig_size;
|
|
569
|
+
if (lW !== rW || lH !== rH) {
|
|
570
|
+
problems.push({
|
|
571
|
+
code: 'DIFF_DRAFTS_CANVAS',
|
|
572
|
+
object: 'canvas',
|
|
573
|
+
detail: `left is ${lW}x${lH} and right is ${rW}x${rH}; identical rig_size dimensions are required because the two outputs are not drafts of one rig`,
|
|
574
|
+
});
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
if (problems.length > 0) throw new PartsError(problems);
|
|
579
|
+
|
|
580
|
+
const report = measureDiffDrafts(leftLoaded.input!, rightLoaded.input!);
|
|
581
|
+
try {
|
|
582
|
+
writeFileSync(outPath, serializeDiffDrafts(report), { flag: 'wx' });
|
|
583
|
+
} catch (err) {
|
|
584
|
+
throw new PartsError([
|
|
585
|
+
{
|
|
586
|
+
code: 'DIFF_DRAFTS_OUT',
|
|
587
|
+
object: '--out',
|
|
588
|
+
detail: `cannot write ${outPath}: ${JSON.stringify(err instanceof Error ? err.message : err)}; a fresh writable output file path is required`,
|
|
589
|
+
},
|
|
590
|
+
]);
|
|
591
|
+
}
|
|
592
|
+
return report;
|
|
593
|
+
}
|
package/src/editedparts.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { createHash } from 'node:crypto';
|
|
|
3
3
|
import { readFileSync } from 'node:fs';
|
|
4
4
|
import { dirname, resolve } from 'node:path';
|
|
5
5
|
import type { EditorReceipt } from './editreceipt.ts';
|
|
6
|
+
import { parseReceiptProducer, type ReceiptProducer } from './receiptproducer.ts';
|
|
6
7
|
import { refuseIfAny, type Problem } from './errors.ts';
|
|
7
8
|
import { OPAQUE_ALPHA_ABOVE } from './layers.ts';
|
|
8
9
|
import { AUTOMATIC_WRITERS, EXCLUDED_PIXEL, NOT_OVERRIDDEN_PIXEL, rgbaEqual, type AutomaticWriter, type PartStageMap, type PixelProvenance, type StageMap, type ValueBaseline } from './provenance.ts';
|
|
@@ -12,7 +13,15 @@ import type { PlacedPart } from './assemble.ts';
|
|
|
12
13
|
import type { PartDepthReference } from './depthsheets.ts';
|
|
13
14
|
import type { PaintingOwner } from './paintingowner.ts';
|
|
14
15
|
|
|
15
|
-
export
|
|
16
|
+
export type EditedPartReceipt = Omit<EditorReceipt, 'editor'> & { part: string } &
|
|
17
|
+
({ editor: string; producer?: never } | { producer: ReceiptProducer; editor?: never });
|
|
18
|
+
|
|
19
|
+
export function orderedEditedPartReceipt(receipt: EditedPartReceipt): EditedPartReceipt {
|
|
20
|
+
const { part, file, sha256, from_sha256, editor, producer } = receipt;
|
|
21
|
+
const fields = { part, file, sha256, from_sha256 };
|
|
22
|
+
if (editor !== undefined) return { ...fields, editor };
|
|
23
|
+
return { ...fields, producer: parseReceiptProducer(producer, 'edited part producer') };
|
|
24
|
+
}
|
|
16
25
|
export interface EditedParts { dir: string; receipts: EditedPartReceipt[] }
|
|
17
26
|
export interface VerifiedEditedPart {
|
|
18
27
|
index: number;
|
|
@@ -135,7 +144,7 @@ export function replaceEditedParts(
|
|
|
135
144
|
let id = map.edits.indexOf(name) + 1;
|
|
136
145
|
if (id === 0) { map.edits.push(name); id = map.edits.length; }
|
|
137
146
|
const metadata: EditedPartProvenance = { edit: name, dir: edit.dir,
|
|
138
|
-
receipt:
|
|
147
|
+
receipt: orderedEditedPartReceipt(edit.receipt),
|
|
139
148
|
changed_px: 0, changed_counted_px: 0, changed_excluded_px: 0, removed_opaque_px: 0 };
|
|
140
149
|
const next: PartStageMap = { name: stage.name, x: stage.x, y: stage.y, w: stage.w, h: stage.h,
|
|
141
150
|
excluded_px: 0, runs: [], pre_seam_writer_runs: [], edited_part: metadata };
|