@bb-studio/exr 0.1.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/LICENSE +21 -0
- package/README.md +63 -0
- package/THIRD_PARTY_LICENSES.md +32 -0
- package/dist/browser/dwaDecodeWorker.js +1184 -0
- package/dist/browser/dwaDecodeWorker.js.map +1 -0
- package/dist/browser/index.d.ts +86 -0
- package/dist/browser/index.js +3170 -0
- package/dist/browser/index.js.map +1 -0
- package/dist/browser/zipDecodeWorker.js +57 -0
- package/dist/browser/zipDecodeWorker.js.map +1 -0
- package/dist/index.cjs +4627 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +121 -0
- package/dist/index.d.ts +121 -0
- package/dist/index.js +4592 -0
- package/dist/index.js.map +1 -0
- package/package.json +63 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,4627 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
EXR_EVENT_CODES: () => EXR_EVENT_CODES,
|
|
24
|
+
ExrError: () => ExrError,
|
|
25
|
+
decodeExrPart: () => decodeExrPart,
|
|
26
|
+
isExrError: () => isExrError,
|
|
27
|
+
parseExr: () => parseExr,
|
|
28
|
+
parseExrStructure: () => parseExrStructure,
|
|
29
|
+
readExr: () => readExr,
|
|
30
|
+
toArrayBuffer: () => toArrayBuffer,
|
|
31
|
+
writeExr: () => writeExr
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(index_exports);
|
|
34
|
+
|
|
35
|
+
// src/shared/events.ts
|
|
36
|
+
var EXR_EVENT_CODES = [
|
|
37
|
+
"parse.magic.invalid",
|
|
38
|
+
"parse.magic.ok",
|
|
39
|
+
"parse.version.ok",
|
|
40
|
+
"parse.part.ok",
|
|
41
|
+
"parse.complete",
|
|
42
|
+
"decode.setup",
|
|
43
|
+
"decode.offsets.read",
|
|
44
|
+
"decode.chunk.part_mismatch",
|
|
45
|
+
"decode.chunk.trailing_bytes",
|
|
46
|
+
"decode.complete",
|
|
47
|
+
"encode.setup",
|
|
48
|
+
"encode.part.complete",
|
|
49
|
+
"encode.complete"
|
|
50
|
+
];
|
|
51
|
+
|
|
52
|
+
// src/shared/errors.ts
|
|
53
|
+
var ExrError = class extends Error {
|
|
54
|
+
code;
|
|
55
|
+
details;
|
|
56
|
+
constructor(code, message, details) {
|
|
57
|
+
super(message);
|
|
58
|
+
this.name = "ExrError";
|
|
59
|
+
this.code = code;
|
|
60
|
+
this.details = details;
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
function isExrError(value) {
|
|
64
|
+
return value instanceof ExrError;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// src/shared/binary.ts
|
|
68
|
+
function toArrayBuffer(input) {
|
|
69
|
+
if (input instanceof ArrayBuffer) {
|
|
70
|
+
return input;
|
|
71
|
+
}
|
|
72
|
+
const output = new Uint8Array(input.byteLength);
|
|
73
|
+
output.set(input);
|
|
74
|
+
return output.buffer;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// src/shared/constants.ts
|
|
78
|
+
var EXR_MAGIC = 20000630;
|
|
79
|
+
var COMPRESSION_NAMES = {
|
|
80
|
+
0: "NO_COMPRESSION",
|
|
81
|
+
1: "RLE_COMPRESSION",
|
|
82
|
+
2: "ZIPS_COMPRESSION",
|
|
83
|
+
3: "ZIP_COMPRESSION",
|
|
84
|
+
4: "PIZ_COMPRESSION",
|
|
85
|
+
5: "PXR24_COMPRESSION",
|
|
86
|
+
6: "B44_COMPRESSION",
|
|
87
|
+
7: "B44A_COMPRESSION",
|
|
88
|
+
8: "DWAA_COMPRESSION",
|
|
89
|
+
9: "DWAB_COMPRESSION"
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
// src/reader/parseExrStructure.ts
|
|
93
|
+
function nowMs() {
|
|
94
|
+
return typeof performance !== "undefined" ? performance.now() : Date.now();
|
|
95
|
+
}
|
|
96
|
+
var BinaryReader = class {
|
|
97
|
+
constructor(buffer) {
|
|
98
|
+
this.buffer = buffer;
|
|
99
|
+
this.view = new DataView(buffer);
|
|
100
|
+
}
|
|
101
|
+
view;
|
|
102
|
+
offset = 0;
|
|
103
|
+
get byteLength() {
|
|
104
|
+
return this.view.byteLength;
|
|
105
|
+
}
|
|
106
|
+
ensureAvailable(count, code, context) {
|
|
107
|
+
if (count < 0 || this.offset + count > this.view.byteLength) {
|
|
108
|
+
throw new ExrError("TRUNCATED_FILE", `Unexpected EOF while reading ${context}.`, {
|
|
109
|
+
code,
|
|
110
|
+
offset: this.offset,
|
|
111
|
+
requested: count,
|
|
112
|
+
size: this.view.byteLength
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
peekUint8() {
|
|
117
|
+
this.ensureAvailable(1, "peek_u8", "byte");
|
|
118
|
+
return this.view.getUint8(this.offset);
|
|
119
|
+
}
|
|
120
|
+
readUint8() {
|
|
121
|
+
this.ensureAvailable(1, "u8", "byte");
|
|
122
|
+
const value = this.view.getUint8(this.offset);
|
|
123
|
+
this.offset += 1;
|
|
124
|
+
return value;
|
|
125
|
+
}
|
|
126
|
+
readInt32() {
|
|
127
|
+
this.ensureAvailable(4, "i32", "int32");
|
|
128
|
+
const value = this.view.getInt32(this.offset, true);
|
|
129
|
+
this.offset += 4;
|
|
130
|
+
return value;
|
|
131
|
+
}
|
|
132
|
+
readUint32() {
|
|
133
|
+
this.ensureAvailable(4, "u32", "uint32");
|
|
134
|
+
const value = this.view.getUint32(this.offset, true);
|
|
135
|
+
this.offset += 4;
|
|
136
|
+
return value;
|
|
137
|
+
}
|
|
138
|
+
readCString(context) {
|
|
139
|
+
let value = "";
|
|
140
|
+
while (this.offset < this.view.byteLength) {
|
|
141
|
+
const ch = this.readUint8();
|
|
142
|
+
if (ch === 0) return value;
|
|
143
|
+
value += String.fromCharCode(ch);
|
|
144
|
+
}
|
|
145
|
+
throw new ExrError("TRUNCATED_FILE", `Unterminated string while reading ${context}.`, {
|
|
146
|
+
offset: this.offset
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
readCStringWithin(limit, context) {
|
|
150
|
+
if (limit < this.offset || limit > this.view.byteLength) {
|
|
151
|
+
throw new ExrError("MALFORMED_HEADER", `Invalid bounded read while parsing ${context}.`, {
|
|
152
|
+
offset: this.offset,
|
|
153
|
+
limit
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
let value = "";
|
|
157
|
+
while (this.offset < limit) {
|
|
158
|
+
const ch = this.readUint8();
|
|
159
|
+
if (ch === 0) return value;
|
|
160
|
+
value += String.fromCharCode(ch);
|
|
161
|
+
}
|
|
162
|
+
throw new ExrError("MALFORMED_HEADER", `Unterminated string while reading ${context}.`, {
|
|
163
|
+
offset: this.offset,
|
|
164
|
+
limit
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
skip(count, context) {
|
|
168
|
+
this.ensureAvailable(count, "skip", context);
|
|
169
|
+
this.offset += count;
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
function emit(onEvent, event) {
|
|
173
|
+
onEvent?.(event);
|
|
174
|
+
}
|
|
175
|
+
function ensureExactAttributeSize(name, type, size, expected) {
|
|
176
|
+
if (size !== expected) {
|
|
177
|
+
throw new ExrError("MALFORMED_HEADER", `Invalid ${type} attribute size for ${name}.`, {
|
|
178
|
+
attribute: name,
|
|
179
|
+
expected,
|
|
180
|
+
actual: size
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
function parsePartHeader(reader, index) {
|
|
185
|
+
if (reader.peekUint8() === 0) {
|
|
186
|
+
return null;
|
|
187
|
+
}
|
|
188
|
+
const attributes = {};
|
|
189
|
+
const channels = [];
|
|
190
|
+
while (reader.offset < reader.byteLength) {
|
|
191
|
+
const name = reader.readCString("attribute name");
|
|
192
|
+
if (name === "") {
|
|
193
|
+
break;
|
|
194
|
+
}
|
|
195
|
+
const type = reader.readCString(`attribute type for ${name}`);
|
|
196
|
+
const size = reader.readInt32();
|
|
197
|
+
if (size < 0) {
|
|
198
|
+
throw new ExrError("MALFORMED_HEADER", `Negative attribute size for ${name}.`, {
|
|
199
|
+
attribute: name,
|
|
200
|
+
size
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
const valueStart = reader.offset;
|
|
204
|
+
reader.ensureAvailable(size, "attribute_size", `attribute payload for ${name}`);
|
|
205
|
+
let value;
|
|
206
|
+
if (type === "chlist") {
|
|
207
|
+
const channelEnd = valueStart + size;
|
|
208
|
+
let sawChannelListTerminator = false;
|
|
209
|
+
while (reader.offset < channelEnd) {
|
|
210
|
+
const channelName = reader.readCStringWithin(channelEnd, "channel name");
|
|
211
|
+
if (channelName === "") {
|
|
212
|
+
sawChannelListTerminator = true;
|
|
213
|
+
break;
|
|
214
|
+
}
|
|
215
|
+
if (reader.offset + 16 > channelEnd) {
|
|
216
|
+
throw new ExrError("MALFORMED_HEADER", `Truncated channel record for ${channelName}.`, {
|
|
217
|
+
attribute: name,
|
|
218
|
+
channel: channelName,
|
|
219
|
+
offset: reader.offset,
|
|
220
|
+
channelEnd
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
const pixelType = reader.readInt32();
|
|
224
|
+
const pLinear = reader.readUint8();
|
|
225
|
+
reader.skip(3, "channel reserved bytes");
|
|
226
|
+
const xSampling = reader.readInt32();
|
|
227
|
+
const ySampling = reader.readInt32();
|
|
228
|
+
channels.push({
|
|
229
|
+
name: channelName,
|
|
230
|
+
pixelType,
|
|
231
|
+
pLinear,
|
|
232
|
+
xSampling,
|
|
233
|
+
ySampling
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
if (!sawChannelListTerminator) {
|
|
237
|
+
throw new ExrError("MALFORMED_HEADER", "Channel list payload is missing terminator.", {
|
|
238
|
+
attribute: name,
|
|
239
|
+
size
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
if (reader.offset !== channelEnd) {
|
|
243
|
+
throw new ExrError("MALFORMED_HEADER", "Channel list payload had trailing bytes.", {
|
|
244
|
+
attribute: name,
|
|
245
|
+
offset: reader.offset,
|
|
246
|
+
expectedEnd: channelEnd
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
value = `(${channels.length} channels)`;
|
|
250
|
+
} else if (type === "compression") {
|
|
251
|
+
ensureExactAttributeSize(name, type, size, 1);
|
|
252
|
+
value = reader.readUint8();
|
|
253
|
+
} else if (type === "box2i") {
|
|
254
|
+
ensureExactAttributeSize(name, type, size, 16);
|
|
255
|
+
value = {
|
|
256
|
+
xMin: reader.readInt32(),
|
|
257
|
+
yMin: reader.readInt32(),
|
|
258
|
+
xMax: reader.readInt32(),
|
|
259
|
+
yMax: reader.readInt32()
|
|
260
|
+
};
|
|
261
|
+
} else if (type === "string") {
|
|
262
|
+
let stringValue = "";
|
|
263
|
+
for (let i = 0; i < size; i++) {
|
|
264
|
+
const charCode = reader.readUint8();
|
|
265
|
+
if (charCode === 0) break;
|
|
266
|
+
stringValue += String.fromCharCode(charCode);
|
|
267
|
+
}
|
|
268
|
+
value = stringValue;
|
|
269
|
+
} else if (type === "int") {
|
|
270
|
+
ensureExactAttributeSize(name, type, size, 4);
|
|
271
|
+
value = reader.readInt32();
|
|
272
|
+
} else if (type === "float") {
|
|
273
|
+
ensureExactAttributeSize(name, type, size, 4);
|
|
274
|
+
const asInt = reader.readUint32();
|
|
275
|
+
const scratch = new DataView(new ArrayBuffer(4));
|
|
276
|
+
scratch.setUint32(0, asInt, true);
|
|
277
|
+
value = scratch.getFloat32(0, true);
|
|
278
|
+
} else {
|
|
279
|
+
reader.skip(size, `unknown attribute ${name}`);
|
|
280
|
+
value = `<${type} data>`;
|
|
281
|
+
}
|
|
282
|
+
reader.offset = valueStart + size;
|
|
283
|
+
attributes[name] = value;
|
|
284
|
+
}
|
|
285
|
+
return {
|
|
286
|
+
id: index,
|
|
287
|
+
attributes,
|
|
288
|
+
channels,
|
|
289
|
+
dataWindow: attributes.dataWindow,
|
|
290
|
+
displayWindow: attributes.displayWindow,
|
|
291
|
+
compression: attributes.compression,
|
|
292
|
+
type: attributes.type
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
function parseExrStructure(bufferInput, options = {}) {
|
|
296
|
+
const onEvent = options.onEvent;
|
|
297
|
+
const t0 = nowMs();
|
|
298
|
+
const buffer = toArrayBuffer(bufferInput);
|
|
299
|
+
if (buffer.byteLength < 8) {
|
|
300
|
+
throw new ExrError("BUFFER_TOO_SMALL", "Buffer is too small to be an EXR file.", {
|
|
301
|
+
size: buffer.byteLength
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
const reader = new BinaryReader(buffer);
|
|
305
|
+
const tMagic = nowMs();
|
|
306
|
+
const magic = reader.readUint32();
|
|
307
|
+
if (magic !== EXR_MAGIC) {
|
|
308
|
+
emit(onEvent, {
|
|
309
|
+
phase: "parse",
|
|
310
|
+
level: "error",
|
|
311
|
+
code: "parse.magic.invalid",
|
|
312
|
+
message: "Invalid EXR magic number.",
|
|
313
|
+
metrics: {
|
|
314
|
+
expected: `0x${EXR_MAGIC.toString(16)}`,
|
|
315
|
+
actual: `0x${magic.toString(16)}`
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
throw new ExrError("INVALID_MAGIC", "Invalid EXR magic number.", {
|
|
319
|
+
expected: EXR_MAGIC,
|
|
320
|
+
actual: magic
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
emit(onEvent, {
|
|
324
|
+
phase: "parse",
|
|
325
|
+
level: "info",
|
|
326
|
+
code: "parse.magic.ok",
|
|
327
|
+
message: "Parsed EXR magic number.",
|
|
328
|
+
metrics: {
|
|
329
|
+
ms: (nowMs() - tMagic).toFixed(3),
|
|
330
|
+
magic: `0x${EXR_MAGIC.toString(16)}`
|
|
331
|
+
}
|
|
332
|
+
});
|
|
333
|
+
const tVersion = nowMs();
|
|
334
|
+
const versionField = reader.readUint32();
|
|
335
|
+
const version = versionField & 255;
|
|
336
|
+
const flags = versionField >> 8;
|
|
337
|
+
const isMultipart = (flags & 16) !== 0;
|
|
338
|
+
emit(onEvent, {
|
|
339
|
+
phase: "parse",
|
|
340
|
+
level: "info",
|
|
341
|
+
code: "parse.version.ok",
|
|
342
|
+
message: "Parsed EXR version and flags.",
|
|
343
|
+
metrics: {
|
|
344
|
+
ms: (nowMs() - tVersion).toFixed(3),
|
|
345
|
+
version,
|
|
346
|
+
flags,
|
|
347
|
+
multipart: isMultipart ? 1 : 0
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
const parts = [];
|
|
351
|
+
let partIndex = 0;
|
|
352
|
+
while (reader.offset < reader.byteLength) {
|
|
353
|
+
const tHeader = nowMs();
|
|
354
|
+
const part = parsePartHeader(reader, partIndex);
|
|
355
|
+
if (!part) {
|
|
356
|
+
if (isMultipart) {
|
|
357
|
+
reader.skip(1, "multipart header terminator");
|
|
358
|
+
}
|
|
359
|
+
break;
|
|
360
|
+
}
|
|
361
|
+
parts.push(part);
|
|
362
|
+
emit(onEvent, {
|
|
363
|
+
phase: "parse",
|
|
364
|
+
level: "info",
|
|
365
|
+
code: "parse.part.ok",
|
|
366
|
+
message: `Parsed part ${part.id} header.`,
|
|
367
|
+
metrics: {
|
|
368
|
+
ms: (nowMs() - tHeader).toFixed(3),
|
|
369
|
+
partId: part.id,
|
|
370
|
+
channels: part.channels.length,
|
|
371
|
+
compression: COMPRESSION_NAMES[part.compression ?? -1] || "UNKNOWN"
|
|
372
|
+
}
|
|
373
|
+
});
|
|
374
|
+
partIndex += 1;
|
|
375
|
+
if (!isMultipart) {
|
|
376
|
+
break;
|
|
377
|
+
}
|
|
378
|
+
if (reader.offset >= reader.byteLength) {
|
|
379
|
+
throw new ExrError("TRUNCATED_FILE", "Unexpected EOF while reading multipart headers.", {
|
|
380
|
+
offset: reader.offset
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
if (reader.peekUint8() === 0) {
|
|
384
|
+
reader.skip(1, "multipart headers terminator");
|
|
385
|
+
break;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
if (parts.length === 0) {
|
|
389
|
+
throw new ExrError("MALFORMED_HEADER", "No EXR headers were parsed.", {
|
|
390
|
+
offset: reader.offset
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
const structure = {
|
|
394
|
+
magic,
|
|
395
|
+
version,
|
|
396
|
+
flags,
|
|
397
|
+
isMultipart,
|
|
398
|
+
parts,
|
|
399
|
+
headerEndOffset: reader.offset
|
|
400
|
+
};
|
|
401
|
+
emit(onEvent, {
|
|
402
|
+
phase: "parse",
|
|
403
|
+
level: "info",
|
|
404
|
+
code: "parse.complete",
|
|
405
|
+
message: "EXR structure parsed.",
|
|
406
|
+
metrics: {
|
|
407
|
+
ms: (nowMs() - t0).toFixed(3),
|
|
408
|
+
parts: parts.length,
|
|
409
|
+
headerEndOffset: structure.headerEndOffset
|
|
410
|
+
}
|
|
411
|
+
});
|
|
412
|
+
return structure;
|
|
413
|
+
}
|
|
414
|
+
var parseExr = parseExrStructure;
|
|
415
|
+
|
|
416
|
+
// src/shared/half.ts
|
|
417
|
+
function createHalfToFloatTable() {
|
|
418
|
+
const exponentTable = new Uint32Array(64);
|
|
419
|
+
const offsetTable = new Uint32Array(64);
|
|
420
|
+
const mantissaTable = new Uint32Array(2048);
|
|
421
|
+
const table = new Float32Array(65536);
|
|
422
|
+
exponentTable[0] = 0;
|
|
423
|
+
for (let i = 1; i < 31; i++) {
|
|
424
|
+
exponentTable[i] = i << 23;
|
|
425
|
+
}
|
|
426
|
+
exponentTable[31] = 1199570944;
|
|
427
|
+
exponentTable[32] = 2147483648;
|
|
428
|
+
for (let i = 33; i < 63; i++) {
|
|
429
|
+
exponentTable[i] = 2147483648 + (i - 32 << 23);
|
|
430
|
+
}
|
|
431
|
+
exponentTable[63] = 3347054592;
|
|
432
|
+
offsetTable[0] = 0;
|
|
433
|
+
for (let i = 1; i < 64; i++) {
|
|
434
|
+
offsetTable[i] = i === 32 ? 0 : 1024;
|
|
435
|
+
}
|
|
436
|
+
mantissaTable[0] = 0;
|
|
437
|
+
for (let i = 1; i < 1024; i++) {
|
|
438
|
+
let m = i << 13;
|
|
439
|
+
let e = 0;
|
|
440
|
+
while ((m & 8388608) === 0) {
|
|
441
|
+
e -= 8388608;
|
|
442
|
+
m <<= 1;
|
|
443
|
+
}
|
|
444
|
+
m &= ~8388608;
|
|
445
|
+
e += 947912704;
|
|
446
|
+
mantissaTable[i] = m | e;
|
|
447
|
+
}
|
|
448
|
+
for (let i = 1024; i < 2048; i++) {
|
|
449
|
+
mantissaTable[i] = 939524096 + (i - 1024 << 13);
|
|
450
|
+
}
|
|
451
|
+
const scratch = new ArrayBuffer(4);
|
|
452
|
+
const floatView2 = new Float32Array(scratch);
|
|
453
|
+
const uintView2 = new Uint32Array(scratch);
|
|
454
|
+
for (let h = 0; h < 65536; h++) {
|
|
455
|
+
const offset = offsetTable[h >> 10];
|
|
456
|
+
const mantissa = mantissaTable[offset + (h & 1023)];
|
|
457
|
+
const exponent = exponentTable[h >> 10];
|
|
458
|
+
uintView2[0] = mantissa + exponent;
|
|
459
|
+
table[h] = floatView2[0];
|
|
460
|
+
}
|
|
461
|
+
return table;
|
|
462
|
+
}
|
|
463
|
+
var HALF_TO_FLOAT_TABLE = createHalfToFloatTable();
|
|
464
|
+
function float16ToFloat32(h) {
|
|
465
|
+
return HALF_TO_FLOAT_TABLE[h & 65535];
|
|
466
|
+
}
|
|
467
|
+
function float32ToFloat16(value) {
|
|
468
|
+
if (Object.is(value, 0)) return 0;
|
|
469
|
+
const floatView2 = new Float32Array(1);
|
|
470
|
+
const intView = new Uint32Array(floatView2.buffer);
|
|
471
|
+
floatView2[0] = value;
|
|
472
|
+
const bits = intView[0];
|
|
473
|
+
const sign = bits >>> 16 & 32768;
|
|
474
|
+
const exponent = (bits >>> 23 & 255) - 127 + 15;
|
|
475
|
+
const mantissa = bits & 8388607;
|
|
476
|
+
if (exponent <= 0) {
|
|
477
|
+
if (exponent < -10) return sign;
|
|
478
|
+
const shiftedMantissa = (mantissa | 8388608) >> 1 - exponent;
|
|
479
|
+
return sign | shiftedMantissa + 4096 >> 13;
|
|
480
|
+
}
|
|
481
|
+
if (exponent >= 31) {
|
|
482
|
+
return sign | 31744;
|
|
483
|
+
}
|
|
484
|
+
return sign | exponent << 10 | mantissa + 4096 >> 13;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
// src/reader/compression/b44.ts
|
|
488
|
+
var HALF_VALUES_PER_BLOCK = 16;
|
|
489
|
+
var B44_BLOCK_SIZE = 14;
|
|
490
|
+
var B44A_BLOCK_SIZE = 3;
|
|
491
|
+
var B44A_MARKER_THRESHOLD = 13 << 2;
|
|
492
|
+
var floatScratch = new ArrayBuffer(4);
|
|
493
|
+
var floatView = new Float32Array(floatScratch);
|
|
494
|
+
var uintView = new Uint32Array(floatScratch);
|
|
495
|
+
var b44ToLinearTable = null;
|
|
496
|
+
function modulo(value, base) {
|
|
497
|
+
const result = value % base;
|
|
498
|
+
return result < 0 ? result + base : result;
|
|
499
|
+
}
|
|
500
|
+
function firstSampleCoordinate(min, sampling) {
|
|
501
|
+
if (sampling <= 1) return min;
|
|
502
|
+
const remainder = modulo(min, sampling);
|
|
503
|
+
return remainder === 0 ? min : min + (sampling - remainder);
|
|
504
|
+
}
|
|
505
|
+
function countSamplesInRange(min, max, sampling) {
|
|
506
|
+
if (sampling <= 0 || max < min) return 0;
|
|
507
|
+
const first = firstSampleCoordinate(min, sampling);
|
|
508
|
+
if (first > max) return 0;
|
|
509
|
+
return Math.floor((max - first) / sampling) + 1;
|
|
510
|
+
}
|
|
511
|
+
function isSampledCoordinate(value, firstSample, sampling) {
|
|
512
|
+
if (sampling <= 1) return true;
|
|
513
|
+
if (value < firstSample) return false;
|
|
514
|
+
return (value - firstSample) % sampling === 0;
|
|
515
|
+
}
|
|
516
|
+
function firstSampleInRange(min, max, sampleOrigin, sampling) {
|
|
517
|
+
if (max < min) return null;
|
|
518
|
+
if (sampling <= 1) return min;
|
|
519
|
+
const delta = min - sampleOrigin;
|
|
520
|
+
const remainder = modulo(delta, sampling);
|
|
521
|
+
const first = remainder === 0 ? min : min + (sampling - remainder);
|
|
522
|
+
return first <= max ? first : null;
|
|
523
|
+
}
|
|
524
|
+
function float32ToFloat162(value) {
|
|
525
|
+
if (Object.is(value, -0)) return 32768;
|
|
526
|
+
if (Object.is(value, 0)) return 0;
|
|
527
|
+
if (!Number.isFinite(value)) {
|
|
528
|
+
if (Number.isNaN(value)) return 32256;
|
|
529
|
+
return value < 0 ? 64512 : 31744;
|
|
530
|
+
}
|
|
531
|
+
floatView[0] = value;
|
|
532
|
+
const bits = uintView[0];
|
|
533
|
+
const sign = bits >>> 16 & 32768;
|
|
534
|
+
const exponent = (bits >>> 23 & 255) - 127 + 15;
|
|
535
|
+
const mantissa = bits & 8388607;
|
|
536
|
+
if (exponent <= 0) {
|
|
537
|
+
if (exponent < -10) return sign;
|
|
538
|
+
const m = (mantissa | 8388608) >> 1 - exponent;
|
|
539
|
+
return sign | m + 4096 >> 13;
|
|
540
|
+
}
|
|
541
|
+
if (exponent >= 31) {
|
|
542
|
+
return sign | 31744;
|
|
543
|
+
}
|
|
544
|
+
return sign | exponent << 10 | mantissa + 4096 >> 13;
|
|
545
|
+
}
|
|
546
|
+
function convertB44ToLinear(value) {
|
|
547
|
+
if ((value & 31744) === 31744) return 0;
|
|
548
|
+
if (value > 32768) return 0;
|
|
549
|
+
const floatValue = float16ToFloat32(value);
|
|
550
|
+
const converted = 8 * Math.log(floatValue);
|
|
551
|
+
return float32ToFloat162(converted);
|
|
552
|
+
}
|
|
553
|
+
function getB44ToLinearTable() {
|
|
554
|
+
if (b44ToLinearTable) {
|
|
555
|
+
return b44ToLinearTable;
|
|
556
|
+
}
|
|
557
|
+
const table = new Uint16Array(1 << 16);
|
|
558
|
+
for (let i = 0; i < table.length; i++) {
|
|
559
|
+
table[i] = convertB44ToLinear(i);
|
|
560
|
+
}
|
|
561
|
+
b44ToLinearTable = table;
|
|
562
|
+
return table;
|
|
563
|
+
}
|
|
564
|
+
function unpack14(source, offset, out) {
|
|
565
|
+
const shift = source[offset + 2] >> 2;
|
|
566
|
+
const bias = 32 << shift;
|
|
567
|
+
out[0] = source[offset] << 8 | source[offset + 1];
|
|
568
|
+
out[4] = out[0] + ((((source[offset + 2] << 4 | source[offset + 3] >> 4) & 63) << shift) - bias) & 65535;
|
|
569
|
+
out[8] = out[4] + ((((source[offset + 3] << 2 | source[offset + 4] >> 6) & 63) << shift) - bias) & 65535;
|
|
570
|
+
out[12] = out[8] + (((source[offset + 4] & 63) << shift) - bias) & 65535;
|
|
571
|
+
out[1] = out[0] + ((source[offset + 5] >> 2 << shift) - bias) & 65535;
|
|
572
|
+
out[5] = out[4] + ((((source[offset + 5] << 4 | source[offset + 6] >> 4) & 63) << shift) - bias) & 65535;
|
|
573
|
+
out[9] = out[8] + ((((source[offset + 6] << 2 | source[offset + 7] >> 6) & 63) << shift) - bias) & 65535;
|
|
574
|
+
out[13] = out[12] + (((source[offset + 7] & 63) << shift) - bias) & 65535;
|
|
575
|
+
out[2] = out[1] + ((source[offset + 8] >> 2 << shift) - bias) & 65535;
|
|
576
|
+
out[6] = out[5] + ((((source[offset + 8] << 4 | source[offset + 9] >> 4) & 63) << shift) - bias) & 65535;
|
|
577
|
+
out[10] = out[9] + ((((source[offset + 9] << 2 | source[offset + 10] >> 6) & 63) << shift) - bias) & 65535;
|
|
578
|
+
out[14] = out[13] + (((source[offset + 10] & 63) << shift) - bias) & 65535;
|
|
579
|
+
out[3] = out[2] + ((source[offset + 11] >> 2 << shift) - bias) & 65535;
|
|
580
|
+
out[7] = out[6] + ((((source[offset + 11] << 4 | source[offset + 12] >> 4) & 63) << shift) - bias) & 65535;
|
|
581
|
+
out[11] = out[10] + ((((source[offset + 12] << 2 | source[offset + 13] >> 6) & 63) << shift) - bias) & 65535;
|
|
582
|
+
out[15] = out[14] + (((source[offset + 13] & 63) << shift) - bias) & 65535;
|
|
583
|
+
for (let i = 0; i < HALF_VALUES_PER_BLOCK; i++) {
|
|
584
|
+
const value = out[i];
|
|
585
|
+
out[i] = (value & 32768) !== 0 ? value & 32767 : ~value & 65535;
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
function unpack3(source, offset, out) {
|
|
589
|
+
let value = source[offset] << 8 | source[offset + 1];
|
|
590
|
+
value = (value & 32768) !== 0 ? value & 32767 : ~value & 65535;
|
|
591
|
+
out.fill(value);
|
|
592
|
+
}
|
|
593
|
+
function buildChannelLayouts(context) {
|
|
594
|
+
const part = context.part;
|
|
595
|
+
const dataWindow = part.dataWindow;
|
|
596
|
+
if (!dataWindow) {
|
|
597
|
+
throw new Error("B44 decode requires part dataWindow.");
|
|
598
|
+
}
|
|
599
|
+
const chunkStart = context.chunkY;
|
|
600
|
+
const chunkEnd = context.chunkY + context.linesInChunk - 1;
|
|
601
|
+
const layouts = [];
|
|
602
|
+
for (const channel of part.channels) {
|
|
603
|
+
const xSampling = channel.xSampling > 0 ? channel.xSampling : 1;
|
|
604
|
+
const ySampling = channel.ySampling > 0 ? channel.ySampling : 1;
|
|
605
|
+
const sampleOriginY = firstSampleCoordinate(dataWindow.yMin, ySampling);
|
|
606
|
+
const sampledWidth = countSamplesInRange(dataWindow.xMin, dataWindow.xMax, xSampling);
|
|
607
|
+
const bytesPerSample = channel.pixelType === 1 ? 2 : 4;
|
|
608
|
+
const visibleStart = Math.max(chunkStart, dataWindow.yMin);
|
|
609
|
+
const visibleEnd = Math.min(chunkEnd, dataWindow.yMax);
|
|
610
|
+
let sampledRows = 0;
|
|
611
|
+
let firstChunkSampleY = -1;
|
|
612
|
+
if (visibleEnd >= visibleStart) {
|
|
613
|
+
const first = firstSampleInRange(visibleStart, visibleEnd, sampleOriginY, ySampling);
|
|
614
|
+
if (first !== null) {
|
|
615
|
+
firstChunkSampleY = first;
|
|
616
|
+
sampledRows = Math.floor((visibleEnd - first) / ySampling) + 1;
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
const planeSize = sampledWidth * sampledRows * bytesPerSample;
|
|
620
|
+
layouts.push({
|
|
621
|
+
channel,
|
|
622
|
+
bytesPerSample,
|
|
623
|
+
sampledWidth,
|
|
624
|
+
sampledRows,
|
|
625
|
+
sampleOriginY,
|
|
626
|
+
ySampling,
|
|
627
|
+
firstChunkSampleY,
|
|
628
|
+
plane: new Uint8Array(planeSize)
|
|
629
|
+
});
|
|
630
|
+
}
|
|
631
|
+
return layouts;
|
|
632
|
+
}
|
|
633
|
+
function decodeB44Block(context) {
|
|
634
|
+
try {
|
|
635
|
+
const part = context.part;
|
|
636
|
+
const dataWindow = part.dataWindow;
|
|
637
|
+
if (!dataWindow) {
|
|
638
|
+
throw new Error("B44 decode requires part dataWindow.");
|
|
639
|
+
}
|
|
640
|
+
const source = new Uint8Array(context.buffer, context.dataPtr, context.dataSize);
|
|
641
|
+
const layouts = buildChannelLayouts(context);
|
|
642
|
+
const halfBlock = new Uint16Array(HALF_VALUES_PER_BLOCK);
|
|
643
|
+
const chunkStart = context.chunkY;
|
|
644
|
+
const chunkEnd = context.chunkY + context.linesInChunk - 1;
|
|
645
|
+
let sourceCursor = 0;
|
|
646
|
+
for (const layout of layouts) {
|
|
647
|
+
if (layout.plane.length === 0) {
|
|
648
|
+
continue;
|
|
649
|
+
}
|
|
650
|
+
if (layout.channel.pixelType !== 1) {
|
|
651
|
+
if (sourceCursor + layout.plane.length > source.length) {
|
|
652
|
+
throw new Error("Truncated B44 payload for non-HALF channel data.");
|
|
653
|
+
}
|
|
654
|
+
layout.plane.set(source.subarray(sourceCursor, sourceCursor + layout.plane.length));
|
|
655
|
+
sourceCursor += layout.plane.length;
|
|
656
|
+
continue;
|
|
657
|
+
}
|
|
658
|
+
const blocksY = Math.ceil(layout.sampledRows / 4);
|
|
659
|
+
const blocksX = Math.ceil(layout.sampledWidth / 4);
|
|
660
|
+
const applyPLinear = layout.channel.pLinear !== 0;
|
|
661
|
+
const toLinearTable = applyPLinear ? getB44ToLinearTable() : null;
|
|
662
|
+
for (let blockY = 0; blockY < blocksY; blockY++) {
|
|
663
|
+
const rowBase = blockY * 4;
|
|
664
|
+
const rowsInBlock = Math.min(4, layout.sampledRows - rowBase);
|
|
665
|
+
for (let blockX = 0; blockX < blocksX; blockX++) {
|
|
666
|
+
const colBase = blockX * 4;
|
|
667
|
+
const colsInBlock = Math.min(4, layout.sampledWidth - colBase);
|
|
668
|
+
if (sourceCursor + B44A_BLOCK_SIZE > source.length) {
|
|
669
|
+
throw new Error("Truncated B44 payload while reading block header.");
|
|
670
|
+
}
|
|
671
|
+
if (source[sourceCursor + 2] >= B44A_MARKER_THRESHOLD) {
|
|
672
|
+
unpack3(source, sourceCursor, halfBlock);
|
|
673
|
+
sourceCursor += B44A_BLOCK_SIZE;
|
|
674
|
+
} else {
|
|
675
|
+
if (sourceCursor + B44_BLOCK_SIZE > source.length) {
|
|
676
|
+
throw new Error("Truncated B44 payload while reading 14-byte block.");
|
|
677
|
+
}
|
|
678
|
+
unpack14(source, sourceCursor, halfBlock);
|
|
679
|
+
sourceCursor += B44_BLOCK_SIZE;
|
|
680
|
+
}
|
|
681
|
+
if (toLinearTable) {
|
|
682
|
+
for (let i = 0; i < HALF_VALUES_PER_BLOCK; i++) {
|
|
683
|
+
halfBlock[i] = toLinearTable[halfBlock[i]];
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
for (let row = 0; row < rowsInBlock; row++) {
|
|
687
|
+
const rowIndex = rowBase + row;
|
|
688
|
+
const rowOffset = rowIndex * layout.sampledWidth;
|
|
689
|
+
const srcRowOffset = row * 4;
|
|
690
|
+
for (let col = 0; col < colsInBlock; col++) {
|
|
691
|
+
const dstIndex = (rowOffset + colBase + col) * 2;
|
|
692
|
+
const value = halfBlock[srcRowOffset + col];
|
|
693
|
+
layout.plane[dstIndex] = value & 255;
|
|
694
|
+
layout.plane[dstIndex + 1] = value >> 8;
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
const outputSize = layouts.reduce((total, layout) => total + layout.plane.length, 0);
|
|
701
|
+
const output = new Uint8Array(outputSize);
|
|
702
|
+
let outputCursor = 0;
|
|
703
|
+
for (let y = chunkStart; y <= chunkEnd; y++) {
|
|
704
|
+
if (y < dataWindow.yMin || y > dataWindow.yMax) {
|
|
705
|
+
continue;
|
|
706
|
+
}
|
|
707
|
+
for (const layout of layouts) {
|
|
708
|
+
if (layout.sampledRows === 0 || layout.sampledWidth === 0) {
|
|
709
|
+
continue;
|
|
710
|
+
}
|
|
711
|
+
if (!isSampledCoordinate(y, layout.sampleOriginY, layout.ySampling)) {
|
|
712
|
+
continue;
|
|
713
|
+
}
|
|
714
|
+
if (layout.firstChunkSampleY < 0 || y < layout.firstChunkSampleY) {
|
|
715
|
+
continue;
|
|
716
|
+
}
|
|
717
|
+
const sampledRow = Math.floor((y - layout.firstChunkSampleY) / layout.ySampling);
|
|
718
|
+
if (sampledRow < 0 || sampledRow >= layout.sampledRows) {
|
|
719
|
+
continue;
|
|
720
|
+
}
|
|
721
|
+
const rowByteLength = layout.sampledWidth * layout.bytesPerSample;
|
|
722
|
+
const rowOffset = sampledRow * rowByteLength;
|
|
723
|
+
output.set(layout.plane.subarray(rowOffset, rowOffset + rowByteLength), outputCursor);
|
|
724
|
+
outputCursor += rowByteLength;
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
if (outputCursor !== output.length) {
|
|
728
|
+
throw new Error("B44 row packing produced an unexpected output size.");
|
|
729
|
+
}
|
|
730
|
+
return output;
|
|
731
|
+
} catch {
|
|
732
|
+
throw new ExrError("DECOMPRESSION_FAILED", "Failed to decompress B44/B44A chunk.", {
|
|
733
|
+
partId: context.partId,
|
|
734
|
+
chunkIndex: context.chunkIndex,
|
|
735
|
+
size: context.dataSize
|
|
736
|
+
});
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
// src/reader/compression/dwa.ts
|
|
741
|
+
var import_fflate = require("fflate");
|
|
742
|
+
var HUF_ENCBITS = 16;
|
|
743
|
+
var HUF_DECBITS = 14;
|
|
744
|
+
var HUF_ENCSIZE = (1 << HUF_ENCBITS) + 1;
|
|
745
|
+
var HUF_DECSIZE = 1 << HUF_DECBITS;
|
|
746
|
+
var HUF_DECMASK = HUF_DECSIZE - 1;
|
|
747
|
+
var SHORT_ZEROCODE_RUN = 59;
|
|
748
|
+
var LONG_ZEROCODE_RUN = 63;
|
|
749
|
+
var SHORTEST_LONG_RUN = 2 + LONG_ZEROCODE_RUN - SHORT_ZEROCODE_RUN;
|
|
750
|
+
var INT16_SIZE = 2;
|
|
751
|
+
var STATIC_HUFFMAN = 0;
|
|
752
|
+
var DEFLATE = 1;
|
|
753
|
+
var UNKNOWN = 0;
|
|
754
|
+
var LOSSY_DCT = 1;
|
|
755
|
+
var RLE = 2;
|
|
756
|
+
var LOG_BASE = Math.pow(2.7182818, 2.2);
|
|
757
|
+
var TEXT_DECODER = new TextDecoder();
|
|
758
|
+
var DCT_A = 0.5 * Math.cos(Math.PI / 4);
|
|
759
|
+
var DCT_B = 0.5 * Math.cos(Math.PI / 16);
|
|
760
|
+
var DCT_C = 0.5 * Math.cos(Math.PI / 8);
|
|
761
|
+
var DCT_D = 0.5 * Math.cos(3 * Math.PI / 16);
|
|
762
|
+
var DCT_E = 0.5 * Math.cos(5 * Math.PI / 16);
|
|
763
|
+
var DCT_F = 0.5 * Math.cos(3 * Math.PI / 8);
|
|
764
|
+
var DCT_G = 0.5 * Math.cos(7 * Math.PI / 16);
|
|
765
|
+
var FLOAT32_TO_FLOAT16_SCRATCH = new ArrayBuffer(4);
|
|
766
|
+
var FLOAT32_TO_FLOAT16_FLOAT_VIEW = new Float32Array(FLOAT32_TO_FLOAT16_SCRATCH);
|
|
767
|
+
var FLOAT32_TO_FLOAT16_INT_VIEW = new Uint32Array(FLOAT32_TO_FLOAT16_SCRATCH);
|
|
768
|
+
var LEGACY_DWA_RULES = [
|
|
769
|
+
{ name: "r", compression: LOSSY_DCT, pixelType: 1, index: 0, caseInsensitive: true },
|
|
770
|
+
{ name: "r", compression: LOSSY_DCT, pixelType: 2, index: 0, caseInsensitive: true },
|
|
771
|
+
{ name: "red", compression: LOSSY_DCT, pixelType: 1, index: 0, caseInsensitive: true },
|
|
772
|
+
{ name: "red", compression: LOSSY_DCT, pixelType: 2, index: 0, caseInsensitive: true },
|
|
773
|
+
{ name: "g", compression: LOSSY_DCT, pixelType: 1, index: 1, caseInsensitive: true },
|
|
774
|
+
{ name: "g", compression: LOSSY_DCT, pixelType: 2, index: 1, caseInsensitive: true },
|
|
775
|
+
{ name: "grn", compression: LOSSY_DCT, pixelType: 1, index: 1, caseInsensitive: true },
|
|
776
|
+
{ name: "grn", compression: LOSSY_DCT, pixelType: 2, index: 1, caseInsensitive: true },
|
|
777
|
+
{ name: "green", compression: LOSSY_DCT, pixelType: 1, index: 1, caseInsensitive: true },
|
|
778
|
+
{ name: "green", compression: LOSSY_DCT, pixelType: 2, index: 1, caseInsensitive: true },
|
|
779
|
+
{ name: "b", compression: LOSSY_DCT, pixelType: 1, index: 2, caseInsensitive: true },
|
|
780
|
+
{ name: "b", compression: LOSSY_DCT, pixelType: 2, index: 2, caseInsensitive: true },
|
|
781
|
+
{ name: "blu", compression: LOSSY_DCT, pixelType: 1, index: 2, caseInsensitive: true },
|
|
782
|
+
{ name: "blu", compression: LOSSY_DCT, pixelType: 2, index: 2, caseInsensitive: true },
|
|
783
|
+
{ name: "blue", compression: LOSSY_DCT, pixelType: 1, index: 2, caseInsensitive: true },
|
|
784
|
+
{ name: "blue", compression: LOSSY_DCT, pixelType: 2, index: 2, caseInsensitive: true },
|
|
785
|
+
{ name: "y", compression: LOSSY_DCT, pixelType: 1, index: -1, caseInsensitive: true },
|
|
786
|
+
{ name: "y", compression: LOSSY_DCT, pixelType: 2, index: -1, caseInsensitive: true },
|
|
787
|
+
{ name: "by", compression: LOSSY_DCT, pixelType: 1, index: -1, caseInsensitive: true },
|
|
788
|
+
{ name: "by", compression: LOSSY_DCT, pixelType: 2, index: -1, caseInsensitive: true },
|
|
789
|
+
{ name: "ry", compression: LOSSY_DCT, pixelType: 1, index: -1, caseInsensitive: true },
|
|
790
|
+
{ name: "ry", compression: LOSSY_DCT, pixelType: 2, index: -1, caseInsensitive: true },
|
|
791
|
+
{ name: "a", compression: RLE, pixelType: 0, index: -1, caseInsensitive: true },
|
|
792
|
+
{ name: "a", compression: RLE, pixelType: 1, index: -1, caseInsensitive: true },
|
|
793
|
+
{ name: "a", compression: RLE, pixelType: 2, index: -1, caseInsensitive: true }
|
|
794
|
+
];
|
|
795
|
+
function ensureAvailable(totalLength, cursor, size, context) {
|
|
796
|
+
if (size < 0 || cursor.value < 0 || cursor.value + size > totalLength) {
|
|
797
|
+
throw new Error(`Truncated DWA payload while reading ${context}.`);
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
function ensureSafeInteger(value, context) {
|
|
801
|
+
const max = BigInt(Number.MAX_SAFE_INTEGER);
|
|
802
|
+
const min = BigInt(Number.MIN_SAFE_INTEGER);
|
|
803
|
+
if (value > max || value < min) {
|
|
804
|
+
throw new Error(`DWA value exceeded safe integer range while reading ${context}.`);
|
|
805
|
+
}
|
|
806
|
+
return Number(value);
|
|
807
|
+
}
|
|
808
|
+
function readUint8(view, cursor) {
|
|
809
|
+
ensureAvailable(view.byteLength, cursor, 1, "uint8");
|
|
810
|
+
const value = view.getUint8(cursor.value);
|
|
811
|
+
cursor.value += 1;
|
|
812
|
+
return value;
|
|
813
|
+
}
|
|
814
|
+
function readUint16(view, cursor) {
|
|
815
|
+
ensureAvailable(view.byteLength, cursor, 2, "uint16");
|
|
816
|
+
const value = view.getUint16(cursor.value, true);
|
|
817
|
+
cursor.value += 2;
|
|
818
|
+
return value;
|
|
819
|
+
}
|
|
820
|
+
function readUint32(view, cursor) {
|
|
821
|
+
ensureAvailable(view.byteLength, cursor, 4, "uint32");
|
|
822
|
+
const value = view.getUint32(cursor.value, true);
|
|
823
|
+
cursor.value += 4;
|
|
824
|
+
return value;
|
|
825
|
+
}
|
|
826
|
+
function readInt64(view, cursor) {
|
|
827
|
+
ensureAvailable(view.byteLength, cursor, 8, "int64");
|
|
828
|
+
let value;
|
|
829
|
+
if ("getBigInt64" in DataView.prototype) {
|
|
830
|
+
value = ensureSafeInteger(view.getBigInt64(cursor.value, true), "int64");
|
|
831
|
+
} else {
|
|
832
|
+
const low = BigInt(view.getUint32(cursor.value, true));
|
|
833
|
+
const high = BigInt(view.getInt32(cursor.value + 4, true));
|
|
834
|
+
value = ensureSafeInteger((high << 32n) + low, "int64");
|
|
835
|
+
}
|
|
836
|
+
cursor.value += 8;
|
|
837
|
+
return value;
|
|
838
|
+
}
|
|
839
|
+
function readUint8FromArray(input, cursor) {
|
|
840
|
+
if (cursor.value < 0 || cursor.value >= input.length) {
|
|
841
|
+
throw new Error("Truncated DWA Huffman payload.");
|
|
842
|
+
}
|
|
843
|
+
const value = input[cursor.value];
|
|
844
|
+
cursor.value += 1;
|
|
845
|
+
return value;
|
|
846
|
+
}
|
|
847
|
+
function readCStringWithin(input, cursor, limit, context) {
|
|
848
|
+
if (limit < cursor.value || limit > input.length) {
|
|
849
|
+
throw new Error(`Invalid DWA bounded read while parsing ${context}.`);
|
|
850
|
+
}
|
|
851
|
+
const start = cursor.value;
|
|
852
|
+
while (cursor.value < limit) {
|
|
853
|
+
if (input[cursor.value] === 0) {
|
|
854
|
+
const value = TEXT_DECODER.decode(input.subarray(start, cursor.value));
|
|
855
|
+
cursor.value += 1;
|
|
856
|
+
return value;
|
|
857
|
+
}
|
|
858
|
+
cursor.value += 1;
|
|
859
|
+
}
|
|
860
|
+
throw new Error(`Unterminated DWA string while parsing ${context}.`);
|
|
861
|
+
}
|
|
862
|
+
function clearHuffmanDecodingTable(table) {
|
|
863
|
+
for (let i = 0; i < HUF_DECSIZE; i++) {
|
|
864
|
+
table[i] = { len: 0, lit: 0, p: null };
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
function getBits(nBits, state, input, cursor) {
|
|
868
|
+
while (state.lc < nBits) {
|
|
869
|
+
state.c = (state.c << 8 | readUint8FromArray(input, cursor)) >>> 0;
|
|
870
|
+
state.lc += 8;
|
|
871
|
+
}
|
|
872
|
+
state.lc -= nBits;
|
|
873
|
+
return state.c >>> state.lc & (1 << nBits) - 1;
|
|
874
|
+
}
|
|
875
|
+
var HUF_TABLE_BUFFER = new Int32Array(59);
|
|
876
|
+
function hufLength(code) {
|
|
877
|
+
return code & 63;
|
|
878
|
+
}
|
|
879
|
+
function hufCode(code) {
|
|
880
|
+
return code >>> 6;
|
|
881
|
+
}
|
|
882
|
+
function hufCanonicalCodeTable(hcode) {
|
|
883
|
+
HUF_TABLE_BUFFER.fill(0);
|
|
884
|
+
for (let i = 0; i < HUF_ENCSIZE; i++) {
|
|
885
|
+
HUF_TABLE_BUFFER[hcode[i]] += 1;
|
|
886
|
+
}
|
|
887
|
+
let c = 0;
|
|
888
|
+
for (let i = 58; i > 0; i--) {
|
|
889
|
+
const next = c + HUF_TABLE_BUFFER[i] >> 1;
|
|
890
|
+
HUF_TABLE_BUFFER[i] = c;
|
|
891
|
+
c = next;
|
|
892
|
+
}
|
|
893
|
+
for (let i = 0; i < HUF_ENCSIZE; i++) {
|
|
894
|
+
const length = hcode[i];
|
|
895
|
+
if (length > 0) {
|
|
896
|
+
hcode[i] = length | HUF_TABLE_BUFFER[length]++ << 6;
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
function hufUnpackEncTable(input, cursor, maxTableBytes, minSymbol, maxSymbol, hcode) {
|
|
901
|
+
const start = cursor.value;
|
|
902
|
+
const state = { c: 0, lc: 0 };
|
|
903
|
+
for (let symbol = minSymbol; symbol <= maxSymbol; symbol++) {
|
|
904
|
+
if (cursor.value - start > maxTableBytes) {
|
|
905
|
+
throw new Error("Invalid DWA Huffman table payload.");
|
|
906
|
+
}
|
|
907
|
+
const length = getBits(6, state, input, cursor);
|
|
908
|
+
hcode[symbol] = length;
|
|
909
|
+
if (length === LONG_ZEROCODE_RUN) {
|
|
910
|
+
if (cursor.value - start > maxTableBytes) {
|
|
911
|
+
throw new Error("Invalid DWA Huffman table payload.");
|
|
912
|
+
}
|
|
913
|
+
let zeroRun = getBits(8, state, input, cursor) + SHORTEST_LONG_RUN;
|
|
914
|
+
if (symbol + zeroRun > maxSymbol + 1) {
|
|
915
|
+
throw new Error("Invalid DWA Huffman zero-run.");
|
|
916
|
+
}
|
|
917
|
+
while (zeroRun-- > 0) {
|
|
918
|
+
hcode[symbol++] = 0;
|
|
919
|
+
}
|
|
920
|
+
symbol--;
|
|
921
|
+
} else if (length >= SHORT_ZEROCODE_RUN) {
|
|
922
|
+
let zeroRun = length - SHORT_ZEROCODE_RUN + 2;
|
|
923
|
+
if (symbol + zeroRun > maxSymbol + 1) {
|
|
924
|
+
throw new Error("Invalid DWA Huffman zero-run.");
|
|
925
|
+
}
|
|
926
|
+
while (zeroRun-- > 0) {
|
|
927
|
+
hcode[symbol++] = 0;
|
|
928
|
+
}
|
|
929
|
+
symbol--;
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
hufCanonicalCodeTable(hcode);
|
|
933
|
+
}
|
|
934
|
+
function hufBuildDecTable(hcode, minSymbol, maxSymbol, table) {
|
|
935
|
+
for (let symbol = minSymbol; symbol <= maxSymbol; symbol++) {
|
|
936
|
+
const code = hufCode(hcode[symbol]);
|
|
937
|
+
const length = hufLength(hcode[symbol]);
|
|
938
|
+
if (code >>> length) {
|
|
939
|
+
throw new Error("Invalid DWA Huffman code.");
|
|
940
|
+
}
|
|
941
|
+
if (length > HUF_DECBITS) {
|
|
942
|
+
const entry = table[code >>> length - HUF_DECBITS];
|
|
943
|
+
if (entry.len) {
|
|
944
|
+
throw new Error("Invalid DWA Huffman decode table entry.");
|
|
945
|
+
}
|
|
946
|
+
entry.lit += 1;
|
|
947
|
+
if (entry.p) {
|
|
948
|
+
entry.p.push(symbol);
|
|
949
|
+
} else {
|
|
950
|
+
entry.p = [symbol];
|
|
951
|
+
}
|
|
952
|
+
} else if (length) {
|
|
953
|
+
const base = code << HUF_DECBITS - length;
|
|
954
|
+
const count = 1 << HUF_DECBITS - length;
|
|
955
|
+
for (let i = 0; i < count; i++) {
|
|
956
|
+
const entry = table[base + i];
|
|
957
|
+
if (entry.len || entry.p) {
|
|
958
|
+
throw new Error("Invalid DWA Huffman decode table entry.");
|
|
959
|
+
}
|
|
960
|
+
entry.len = length;
|
|
961
|
+
entry.lit = symbol;
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
function getChar(state, input, cursor) {
|
|
967
|
+
state.c = (state.c << 8 | readUint8FromArray(input, cursor)) >>> 0;
|
|
968
|
+
state.lc += 8;
|
|
969
|
+
}
|
|
970
|
+
function getCode(symbol, runLengthCode, state, input, cursor, outBuffer, outOffset, outEnd) {
|
|
971
|
+
if (symbol === runLengthCode) {
|
|
972
|
+
if (state.lc < 8) {
|
|
973
|
+
getChar(state, input, cursor);
|
|
974
|
+
}
|
|
975
|
+
state.lc -= 8;
|
|
976
|
+
let repeatCount = state.c >>> state.lc & 255;
|
|
977
|
+
if (outOffset.value === 0 || outOffset.value + repeatCount > outEnd) {
|
|
978
|
+
throw new Error("Invalid DWA run-length code.");
|
|
979
|
+
}
|
|
980
|
+
const last = outBuffer[outOffset.value - 1];
|
|
981
|
+
while (repeatCount-- > 0) {
|
|
982
|
+
outBuffer[outOffset.value++] = last;
|
|
983
|
+
}
|
|
984
|
+
} else if (outOffset.value < outEnd) {
|
|
985
|
+
outBuffer[outOffset.value++] = symbol;
|
|
986
|
+
} else {
|
|
987
|
+
throw new Error("DWA Huffman decode overflow.");
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
function hufDecode(encodingTable, decodingTable, input, cursor, nBits, runLengthCode, outSize, outBuffer, outOffset) {
|
|
991
|
+
const state = { c: 0, lc: 0 };
|
|
992
|
+
const inOffsetEnd = Math.trunc(cursor.value + (nBits + 7) / 8);
|
|
993
|
+
while (cursor.value < inOffsetEnd) {
|
|
994
|
+
getChar(state, input, cursor);
|
|
995
|
+
while (state.lc >= HUF_DECBITS) {
|
|
996
|
+
const index = state.c >>> state.lc - HUF_DECBITS & HUF_DECMASK;
|
|
997
|
+
const entry = decodingTable[index];
|
|
998
|
+
if (entry.len) {
|
|
999
|
+
state.lc -= entry.len;
|
|
1000
|
+
getCode(entry.lit, runLengthCode, state, input, cursor, outBuffer, outOffset, outSize);
|
|
1001
|
+
} else {
|
|
1002
|
+
if (!entry.p) {
|
|
1003
|
+
throw new Error("DWA Huffman decode failed.");
|
|
1004
|
+
}
|
|
1005
|
+
let matched = false;
|
|
1006
|
+
for (let i = 0; i < entry.lit; i++) {
|
|
1007
|
+
const symbol = entry.p[i];
|
|
1008
|
+
const length = hufLength(encodingTable[symbol]);
|
|
1009
|
+
while (state.lc < length && cursor.value < inOffsetEnd) {
|
|
1010
|
+
getChar(state, input, cursor);
|
|
1011
|
+
}
|
|
1012
|
+
if (state.lc >= length) {
|
|
1013
|
+
const code = hufCode(encodingTable[symbol]);
|
|
1014
|
+
const candidate = state.c >>> state.lc - length & (1 << length) - 1;
|
|
1015
|
+
if (code === candidate) {
|
|
1016
|
+
state.lc -= length;
|
|
1017
|
+
getCode(symbol, runLengthCode, state, input, cursor, outBuffer, outOffset, outSize);
|
|
1018
|
+
matched = true;
|
|
1019
|
+
break;
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
if (!matched) {
|
|
1024
|
+
throw new Error("DWA Huffman decode failed.");
|
|
1025
|
+
}
|
|
1026
|
+
}
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
1029
|
+
const pad = 8 - nBits & 7;
|
|
1030
|
+
state.c >>>= pad;
|
|
1031
|
+
state.lc -= pad;
|
|
1032
|
+
while (state.lc > 0) {
|
|
1033
|
+
const entry = decodingTable[state.c << HUF_DECBITS - state.lc & HUF_DECMASK];
|
|
1034
|
+
if (!entry.len) {
|
|
1035
|
+
throw new Error("DWA Huffman decode failed.");
|
|
1036
|
+
}
|
|
1037
|
+
state.lc -= entry.len;
|
|
1038
|
+
getCode(entry.lit, runLengthCode, state, input, cursor, outBuffer, outOffset, outSize);
|
|
1039
|
+
}
|
|
1040
|
+
}
|
|
1041
|
+
function hufUncompress(input, view, cursor, nCompressed, out, nRaw) {
|
|
1042
|
+
const outOffset = { value: 0 };
|
|
1043
|
+
const initialOffset = cursor.value;
|
|
1044
|
+
const minSymbol = readUint32(view, cursor);
|
|
1045
|
+
const maxSymbol = readUint32(view, cursor);
|
|
1046
|
+
readUint32(view, cursor);
|
|
1047
|
+
const nBits = readUint32(view, cursor);
|
|
1048
|
+
readUint32(view, cursor);
|
|
1049
|
+
if (minSymbol < 0 || minSymbol >= HUF_ENCSIZE || maxSymbol < 0 || maxSymbol >= HUF_ENCSIZE || minSymbol > maxSymbol) {
|
|
1050
|
+
throw new Error("Invalid DWA Huffman symbol bounds.");
|
|
1051
|
+
}
|
|
1052
|
+
const encodingTable = new Int32Array(HUF_ENCSIZE);
|
|
1053
|
+
const decodingTable = new Array(HUF_DECSIZE);
|
|
1054
|
+
clearHuffmanDecodingTable(decodingTable);
|
|
1055
|
+
const tableBytes = nCompressed - (cursor.value - initialOffset);
|
|
1056
|
+
if (tableBytes < 0) {
|
|
1057
|
+
throw new Error("Invalid DWA Huffman payload size.");
|
|
1058
|
+
}
|
|
1059
|
+
hufUnpackEncTable(input, cursor, tableBytes, minSymbol, maxSymbol, encodingTable);
|
|
1060
|
+
if (nBits > 8 * (nCompressed - (cursor.value - initialOffset))) {
|
|
1061
|
+
throw new Error("Invalid DWA Huffman bit count.");
|
|
1062
|
+
}
|
|
1063
|
+
hufBuildDecTable(encodingTable, minSymbol, maxSymbol, decodingTable);
|
|
1064
|
+
hufDecode(encodingTable, decodingTable, input, cursor, nBits, maxSymbol, nRaw, out, outOffset);
|
|
1065
|
+
if (outOffset.value !== nRaw) {
|
|
1066
|
+
throw new Error("DWA Huffman output size mismatch.");
|
|
1067
|
+
}
|
|
1068
|
+
}
|
|
1069
|
+
var DWA_DC_SCRATCH = {
|
|
1070
|
+
inflate: new Uint8Array(0),
|
|
1071
|
+
output: new Uint8Array(0)
|
|
1072
|
+
};
|
|
1073
|
+
function undoZipPredictorAndInterleave(data, output) {
|
|
1074
|
+
const length = data.length;
|
|
1075
|
+
if (length === 0) {
|
|
1076
|
+
return data;
|
|
1077
|
+
}
|
|
1078
|
+
const half = length + 1 >> 1;
|
|
1079
|
+
let predicted = data[0];
|
|
1080
|
+
output[0] = predicted;
|
|
1081
|
+
for (let i = 1; i < half; i++) {
|
|
1082
|
+
predicted = predicted + data[i] - 128 & 255;
|
|
1083
|
+
output[i << 1] = predicted;
|
|
1084
|
+
}
|
|
1085
|
+
for (let i = half; i < length; i++) {
|
|
1086
|
+
predicted = predicted + data[i] - 128 & 255;
|
|
1087
|
+
output[(i - half << 1) + 1] = predicted;
|
|
1088
|
+
}
|
|
1089
|
+
return output;
|
|
1090
|
+
}
|
|
1091
|
+
function ensureDwaScratchBuffer(buffer, size) {
|
|
1092
|
+
return buffer.byteLength >= size ? buffer.subarray(0, size) : new Uint8Array(size);
|
|
1093
|
+
}
|
|
1094
|
+
function uncompressZipStyle(input, offset, size, expectedUncompressedSize, scratch) {
|
|
1095
|
+
if (size < 0 || offset < 0 || offset + size > input.length) {
|
|
1096
|
+
throw new Error("Invalid DWA ZIP payload bounds.");
|
|
1097
|
+
}
|
|
1098
|
+
const compressed = input.subarray(offset, offset + size);
|
|
1099
|
+
const inflateOut = expectedUncompressedSize > 0 ? ensureDwaScratchBuffer(scratch.inflate, expectedUncompressedSize) : scratch.inflate;
|
|
1100
|
+
if (expectedUncompressedSize > 0 && inflateOut !== scratch.inflate) {
|
|
1101
|
+
scratch.inflate = inflateOut;
|
|
1102
|
+
}
|
|
1103
|
+
const raw = expectedUncompressedSize > 0 ? (0, import_fflate.unzlibSync)(compressed, { out: inflateOut }) : (0, import_fflate.unzlibSync)(compressed);
|
|
1104
|
+
const outputBuffer = ensureDwaScratchBuffer(scratch.output, raw.byteLength);
|
|
1105
|
+
if (outputBuffer !== scratch.output) {
|
|
1106
|
+
scratch.output = outputBuffer;
|
|
1107
|
+
}
|
|
1108
|
+
return undoZipPredictorAndInterleave(raw, outputBuffer);
|
|
1109
|
+
}
|
|
1110
|
+
function decodeRunLength(input, expectedSize) {
|
|
1111
|
+
if (expectedSize < 0) {
|
|
1112
|
+
throw new Error("Invalid DWA RLE expected size.");
|
|
1113
|
+
}
|
|
1114
|
+
const out = new Uint8Array(expectedSize);
|
|
1115
|
+
let ptr = 0;
|
|
1116
|
+
let outPtr = 0;
|
|
1117
|
+
while (ptr < input.byteLength) {
|
|
1118
|
+
const length = input[ptr] << 24 >> 24;
|
|
1119
|
+
ptr += 1;
|
|
1120
|
+
if (length < 0) {
|
|
1121
|
+
const count = -length;
|
|
1122
|
+
if (ptr + count > input.byteLength || outPtr + count > out.byteLength) {
|
|
1123
|
+
throw new Error("Invalid DWA RLE payload bounds.");
|
|
1124
|
+
}
|
|
1125
|
+
out.set(input.subarray(ptr, ptr + count), outPtr);
|
|
1126
|
+
ptr += count;
|
|
1127
|
+
outPtr += count;
|
|
1128
|
+
} else {
|
|
1129
|
+
const count = length + 1;
|
|
1130
|
+
if (ptr >= input.byteLength || outPtr + count > out.byteLength) {
|
|
1131
|
+
throw new Error("Invalid DWA RLE payload bounds.");
|
|
1132
|
+
}
|
|
1133
|
+
const value = input[ptr++];
|
|
1134
|
+
out.fill(value, outPtr, outPtr + count);
|
|
1135
|
+
outPtr += count;
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
if (outPtr !== out.byteLength) {
|
|
1139
|
+
throw new Error("Invalid DWA RLE payload length.");
|
|
1140
|
+
}
|
|
1141
|
+
return out;
|
|
1142
|
+
}
|
|
1143
|
+
function unRleAC(currAc, acBuffer, halfZigBlock) {
|
|
1144
|
+
let dctComp = 1;
|
|
1145
|
+
while (dctComp < 64) {
|
|
1146
|
+
if (currAc.value >= acBuffer.length) {
|
|
1147
|
+
throw new Error("Invalid DWA AC coefficient bounds.");
|
|
1148
|
+
}
|
|
1149
|
+
const acValue = acBuffer[currAc.value++];
|
|
1150
|
+
if (acValue === 65280) {
|
|
1151
|
+
dctComp = 64;
|
|
1152
|
+
} else if (acValue >> 8 === 255) {
|
|
1153
|
+
dctComp += acValue & 255;
|
|
1154
|
+
} else {
|
|
1155
|
+
halfZigBlock[dctComp++] = acValue;
|
|
1156
|
+
}
|
|
1157
|
+
}
|
|
1158
|
+
}
|
|
1159
|
+
function unZigZag(src, dst) {
|
|
1160
|
+
dst[0] = float16ToFloat32(src[0]);
|
|
1161
|
+
dst[1] = float16ToFloat32(src[1]);
|
|
1162
|
+
dst[2] = float16ToFloat32(src[5]);
|
|
1163
|
+
dst[3] = float16ToFloat32(src[6]);
|
|
1164
|
+
dst[4] = float16ToFloat32(src[14]);
|
|
1165
|
+
dst[5] = float16ToFloat32(src[15]);
|
|
1166
|
+
dst[6] = float16ToFloat32(src[27]);
|
|
1167
|
+
dst[7] = float16ToFloat32(src[28]);
|
|
1168
|
+
dst[8] = float16ToFloat32(src[2]);
|
|
1169
|
+
dst[9] = float16ToFloat32(src[4]);
|
|
1170
|
+
dst[10] = float16ToFloat32(src[7]);
|
|
1171
|
+
dst[11] = float16ToFloat32(src[13]);
|
|
1172
|
+
dst[12] = float16ToFloat32(src[16]);
|
|
1173
|
+
dst[13] = float16ToFloat32(src[26]);
|
|
1174
|
+
dst[14] = float16ToFloat32(src[29]);
|
|
1175
|
+
dst[15] = float16ToFloat32(src[42]);
|
|
1176
|
+
dst[16] = float16ToFloat32(src[3]);
|
|
1177
|
+
dst[17] = float16ToFloat32(src[8]);
|
|
1178
|
+
dst[18] = float16ToFloat32(src[12]);
|
|
1179
|
+
dst[19] = float16ToFloat32(src[17]);
|
|
1180
|
+
dst[20] = float16ToFloat32(src[25]);
|
|
1181
|
+
dst[21] = float16ToFloat32(src[30]);
|
|
1182
|
+
dst[22] = float16ToFloat32(src[41]);
|
|
1183
|
+
dst[23] = float16ToFloat32(src[43]);
|
|
1184
|
+
dst[24] = float16ToFloat32(src[9]);
|
|
1185
|
+
dst[25] = float16ToFloat32(src[11]);
|
|
1186
|
+
dst[26] = float16ToFloat32(src[18]);
|
|
1187
|
+
dst[27] = float16ToFloat32(src[24]);
|
|
1188
|
+
dst[28] = float16ToFloat32(src[31]);
|
|
1189
|
+
dst[29] = float16ToFloat32(src[40]);
|
|
1190
|
+
dst[30] = float16ToFloat32(src[44]);
|
|
1191
|
+
dst[31] = float16ToFloat32(src[53]);
|
|
1192
|
+
dst[32] = float16ToFloat32(src[10]);
|
|
1193
|
+
dst[33] = float16ToFloat32(src[19]);
|
|
1194
|
+
dst[34] = float16ToFloat32(src[23]);
|
|
1195
|
+
dst[35] = float16ToFloat32(src[32]);
|
|
1196
|
+
dst[36] = float16ToFloat32(src[39]);
|
|
1197
|
+
dst[37] = float16ToFloat32(src[45]);
|
|
1198
|
+
dst[38] = float16ToFloat32(src[52]);
|
|
1199
|
+
dst[39] = float16ToFloat32(src[54]);
|
|
1200
|
+
dst[40] = float16ToFloat32(src[20]);
|
|
1201
|
+
dst[41] = float16ToFloat32(src[22]);
|
|
1202
|
+
dst[42] = float16ToFloat32(src[33]);
|
|
1203
|
+
dst[43] = float16ToFloat32(src[38]);
|
|
1204
|
+
dst[44] = float16ToFloat32(src[46]);
|
|
1205
|
+
dst[45] = float16ToFloat32(src[51]);
|
|
1206
|
+
dst[46] = float16ToFloat32(src[55]);
|
|
1207
|
+
dst[47] = float16ToFloat32(src[60]);
|
|
1208
|
+
dst[48] = float16ToFloat32(src[21]);
|
|
1209
|
+
dst[49] = float16ToFloat32(src[34]);
|
|
1210
|
+
dst[50] = float16ToFloat32(src[37]);
|
|
1211
|
+
dst[51] = float16ToFloat32(src[47]);
|
|
1212
|
+
dst[52] = float16ToFloat32(src[50]);
|
|
1213
|
+
dst[53] = float16ToFloat32(src[56]);
|
|
1214
|
+
dst[54] = float16ToFloat32(src[59]);
|
|
1215
|
+
dst[55] = float16ToFloat32(src[61]);
|
|
1216
|
+
dst[56] = float16ToFloat32(src[35]);
|
|
1217
|
+
dst[57] = float16ToFloat32(src[36]);
|
|
1218
|
+
dst[58] = float16ToFloat32(src[48]);
|
|
1219
|
+
dst[59] = float16ToFloat32(src[49]);
|
|
1220
|
+
dst[60] = float16ToFloat32(src[57]);
|
|
1221
|
+
dst[61] = float16ToFloat32(src[58]);
|
|
1222
|
+
dst[62] = float16ToFloat32(src[62]);
|
|
1223
|
+
dst[63] = float16ToFloat32(src[63]);
|
|
1224
|
+
}
|
|
1225
|
+
function dctInverse(data) {
|
|
1226
|
+
for (let row = 0; row < 8; row++) {
|
|
1227
|
+
const rowPtr = row * 8;
|
|
1228
|
+
const x0 = data[rowPtr + 0];
|
|
1229
|
+
const x1 = data[rowPtr + 1];
|
|
1230
|
+
const x2 = data[rowPtr + 2];
|
|
1231
|
+
const x3 = data[rowPtr + 3];
|
|
1232
|
+
const x4 = data[rowPtr + 4];
|
|
1233
|
+
const x5 = data[rowPtr + 5];
|
|
1234
|
+
const x6 = data[rowPtr + 6];
|
|
1235
|
+
const x7 = data[rowPtr + 7];
|
|
1236
|
+
const alpha0 = DCT_C * x2;
|
|
1237
|
+
const alpha1 = DCT_F * x2;
|
|
1238
|
+
const alpha2 = DCT_C * x6;
|
|
1239
|
+
const alpha3 = DCT_F * x6;
|
|
1240
|
+
const beta0 = DCT_B * x1 + DCT_D * x3 + DCT_E * x5 + DCT_G * x7;
|
|
1241
|
+
const beta1 = DCT_D * x1 - DCT_G * x3 - DCT_B * x5 - DCT_E * x7;
|
|
1242
|
+
const beta2 = DCT_E * x1 - DCT_B * x3 + DCT_G * x5 + DCT_D * x7;
|
|
1243
|
+
const beta3 = DCT_G * x1 - DCT_E * x3 + DCT_D * x5 - DCT_B * x7;
|
|
1244
|
+
const theta0 = DCT_A * (x0 + x4);
|
|
1245
|
+
const theta1 = alpha0 + alpha3;
|
|
1246
|
+
const theta2 = alpha1 - alpha2;
|
|
1247
|
+
const theta3 = DCT_A * (x0 - x4);
|
|
1248
|
+
const gamma0 = theta0 + theta1;
|
|
1249
|
+
const gamma1 = theta3 + theta2;
|
|
1250
|
+
const gamma2 = theta3 - theta2;
|
|
1251
|
+
const gamma3 = theta0 - theta1;
|
|
1252
|
+
data[rowPtr + 0] = gamma0 + beta0;
|
|
1253
|
+
data[rowPtr + 1] = gamma1 + beta1;
|
|
1254
|
+
data[rowPtr + 2] = gamma2 + beta2;
|
|
1255
|
+
data[rowPtr + 3] = gamma3 + beta3;
|
|
1256
|
+
data[rowPtr + 4] = gamma3 - beta3;
|
|
1257
|
+
data[rowPtr + 5] = gamma2 - beta2;
|
|
1258
|
+
data[rowPtr + 6] = gamma1 - beta1;
|
|
1259
|
+
data[rowPtr + 7] = gamma0 - beta0;
|
|
1260
|
+
}
|
|
1261
|
+
for (let column = 0; column < 8; column++) {
|
|
1262
|
+
const x0 = data[column];
|
|
1263
|
+
const x1 = data[8 + column];
|
|
1264
|
+
const x2 = data[16 + column];
|
|
1265
|
+
const x3 = data[24 + column];
|
|
1266
|
+
const x4 = data[32 + column];
|
|
1267
|
+
const x5 = data[40 + column];
|
|
1268
|
+
const x6 = data[48 + column];
|
|
1269
|
+
const x7 = data[56 + column];
|
|
1270
|
+
const alpha0 = DCT_C * x2;
|
|
1271
|
+
const alpha1 = DCT_F * x2;
|
|
1272
|
+
const alpha2 = DCT_C * x6;
|
|
1273
|
+
const alpha3 = DCT_F * x6;
|
|
1274
|
+
const beta0 = DCT_B * x1 + DCT_D * x3 + DCT_E * x5 + DCT_G * x7;
|
|
1275
|
+
const beta1 = DCT_D * x1 - DCT_G * x3 - DCT_B * x5 - DCT_E * x7;
|
|
1276
|
+
const beta2 = DCT_E * x1 - DCT_B * x3 + DCT_G * x5 + DCT_D * x7;
|
|
1277
|
+
const beta3 = DCT_G * x1 - DCT_E * x3 + DCT_D * x5 - DCT_B * x7;
|
|
1278
|
+
const theta0 = DCT_A * (x0 + x4);
|
|
1279
|
+
const theta1 = alpha0 + alpha3;
|
|
1280
|
+
const theta2 = alpha1 - alpha2;
|
|
1281
|
+
const theta3 = DCT_A * (x0 - x4);
|
|
1282
|
+
const gamma0 = theta0 + theta1;
|
|
1283
|
+
const gamma1 = theta3 + theta2;
|
|
1284
|
+
const gamma2 = theta3 - theta2;
|
|
1285
|
+
const gamma3 = theta0 - theta1;
|
|
1286
|
+
data[column] = gamma0 + beta0;
|
|
1287
|
+
data[8 + column] = gamma1 + beta1;
|
|
1288
|
+
data[16 + column] = gamma2 + beta2;
|
|
1289
|
+
data[24 + column] = gamma3 + beta3;
|
|
1290
|
+
data[32 + column] = gamma3 - beta3;
|
|
1291
|
+
data[40 + column] = gamma2 - beta2;
|
|
1292
|
+
data[48 + column] = gamma1 - beta1;
|
|
1293
|
+
data[56 + column] = gamma0 - beta0;
|
|
1294
|
+
}
|
|
1295
|
+
}
|
|
1296
|
+
function csc709Inverse(data) {
|
|
1297
|
+
for (let i = 0; i < 64; i++) {
|
|
1298
|
+
const y = data[0][i];
|
|
1299
|
+
const cb = data[1][i];
|
|
1300
|
+
const cr = data[2][i];
|
|
1301
|
+
data[0][i] = y + 1.5747 * cr;
|
|
1302
|
+
data[1][i] = y - 0.1873 * cb - 0.4682 * cr;
|
|
1303
|
+
data[2][i] = y + 1.8556 * cb;
|
|
1304
|
+
}
|
|
1305
|
+
}
|
|
1306
|
+
function float32ToFloat163(value) {
|
|
1307
|
+
if (Object.is(value, 0)) return 0;
|
|
1308
|
+
FLOAT32_TO_FLOAT16_FLOAT_VIEW[0] = value;
|
|
1309
|
+
const bits = FLOAT32_TO_FLOAT16_INT_VIEW[0];
|
|
1310
|
+
const sign = bits >>> 16 & 32768;
|
|
1311
|
+
const exponent = (bits >>> 23 & 255) - 127 + 15;
|
|
1312
|
+
const mantissa = bits & 8388607;
|
|
1313
|
+
if (exponent <= 0) {
|
|
1314
|
+
if (exponent < -10) return sign;
|
|
1315
|
+
const m = (mantissa | 8388608) >> 1 - exponent;
|
|
1316
|
+
return sign | m + 4096 >> 13;
|
|
1317
|
+
}
|
|
1318
|
+
if (exponent >= 31) {
|
|
1319
|
+
return sign | 31744;
|
|
1320
|
+
}
|
|
1321
|
+
return sign | exponent << 10 | mantissa + 4096 >> 13;
|
|
1322
|
+
}
|
|
1323
|
+
function toLinear(value) {
|
|
1324
|
+
const abs = Math.abs(value);
|
|
1325
|
+
if (abs <= 1) {
|
|
1326
|
+
const linear2 = Math.pow(abs, 2.2);
|
|
1327
|
+
return value < 0 ? -linear2 : linear2;
|
|
1328
|
+
}
|
|
1329
|
+
const linear = Math.pow(LOG_BASE, abs - 1);
|
|
1330
|
+
return value < 0 ? -linear : linear;
|
|
1331
|
+
}
|
|
1332
|
+
function convertToHalf(src, dst, idx) {
|
|
1333
|
+
for (let i = 0; i < 64; i++) {
|
|
1334
|
+
dst[idx + i] = float32ToFloat163(toLinear(src[i]));
|
|
1335
|
+
}
|
|
1336
|
+
}
|
|
1337
|
+
function lossyDctDecode(cscSet, rowPtrs, channelData, acBuffer, dcBuffer, outBuffer, acOffset, dcOffset) {
|
|
1338
|
+
const c0 = cscSet.idx[0];
|
|
1339
|
+
const c1 = cscSet.idx[1];
|
|
1340
|
+
const c2 = cscSet.idx[2];
|
|
1341
|
+
if (c0 === void 0 || c1 === void 0 || c2 === void 0) {
|
|
1342
|
+
return { acConsumed: 0, dcConsumed: 0 };
|
|
1343
|
+
}
|
|
1344
|
+
const cscIndices = [c0, c1, c2];
|
|
1345
|
+
const dataView = new DataView(outBuffer.buffer, outBuffer.byteOffset, outBuffer.byteLength);
|
|
1346
|
+
const outWords = new Uint16Array(
|
|
1347
|
+
outBuffer.buffer,
|
|
1348
|
+
outBuffer.byteOffset,
|
|
1349
|
+
outBuffer.byteLength >> 1
|
|
1350
|
+
);
|
|
1351
|
+
const width = channelData[c0].width;
|
|
1352
|
+
const height = channelData[c0].height;
|
|
1353
|
+
const numComp = 3;
|
|
1354
|
+
const numFullBlocksX = Math.floor(width / 8);
|
|
1355
|
+
const numBlocksX = Math.ceil(width / 8);
|
|
1356
|
+
const numBlocksY = Math.ceil(height / 8);
|
|
1357
|
+
const leftoverX = width - (numBlocksX - 1) * 8;
|
|
1358
|
+
const leftoverY = height - (numBlocksY - 1) * 8;
|
|
1359
|
+
const currAcComp = { value: acOffset };
|
|
1360
|
+
const currDcComp = new Array(numComp);
|
|
1361
|
+
const dctData = new Array(numComp);
|
|
1362
|
+
const halfZigBlock = new Array(numComp);
|
|
1363
|
+
const rowBlock = new Array(numComp);
|
|
1364
|
+
const rowOffsets = new Array(numComp);
|
|
1365
|
+
for (let comp = 0; comp < numComp; comp++) {
|
|
1366
|
+
rowOffsets[comp] = rowPtrs[cscIndices[comp]];
|
|
1367
|
+
currDcComp[comp] = comp < 1 ? dcOffset : currDcComp[comp - 1] + numBlocksX * numBlocksY;
|
|
1368
|
+
dctData[comp] = new Float32Array(64);
|
|
1369
|
+
halfZigBlock[comp] = new Uint16Array(64);
|
|
1370
|
+
rowBlock[comp] = new Uint16Array(numBlocksX * 64);
|
|
1371
|
+
}
|
|
1372
|
+
for (let blockY = 0; blockY < numBlocksY; blockY++) {
|
|
1373
|
+
let maxY = 8;
|
|
1374
|
+
if (blockY === numBlocksY - 1) maxY = leftoverY;
|
|
1375
|
+
let maxX = 8;
|
|
1376
|
+
for (let blockX = 0; blockX < numBlocksX; blockX++) {
|
|
1377
|
+
if (blockX === numBlocksX - 1) maxX = leftoverX;
|
|
1378
|
+
for (let comp = 0; comp < numComp; comp++) {
|
|
1379
|
+
halfZigBlock[comp].fill(0);
|
|
1380
|
+
if (currDcComp[comp] >= dcBuffer.length) {
|
|
1381
|
+
throw new Error("Invalid DWA DC coefficient bounds.");
|
|
1382
|
+
}
|
|
1383
|
+
halfZigBlock[comp][0] = dcBuffer[currDcComp[comp]++];
|
|
1384
|
+
unRleAC(currAcComp, acBuffer, halfZigBlock[comp]);
|
|
1385
|
+
unZigZag(halfZigBlock[comp], dctData[comp]);
|
|
1386
|
+
dctInverse(dctData[comp]);
|
|
1387
|
+
}
|
|
1388
|
+
csc709Inverse(dctData);
|
|
1389
|
+
for (let comp = 0; comp < numComp; comp++) {
|
|
1390
|
+
convertToHalf(dctData[comp], rowBlock[comp], blockX * 64);
|
|
1391
|
+
}
|
|
1392
|
+
}
|
|
1393
|
+
for (let comp = 0; comp < numComp; comp++) {
|
|
1394
|
+
const channelIndex = cscIndices[comp];
|
|
1395
|
+
const sampleWords = channelData[channelIndex].sampleWords;
|
|
1396
|
+
for (let y = 8 * blockY; y < 8 * blockY + maxY; y++) {
|
|
1397
|
+
let offsetWords = rowOffsets[comp][y] >> 1;
|
|
1398
|
+
for (let blockX = 0; blockX < numFullBlocksX; blockX++) {
|
|
1399
|
+
const src = blockX * 64 + (y & 7) * 8;
|
|
1400
|
+
outWords[offsetWords + 0 * sampleWords] = rowBlock[comp][src + 0];
|
|
1401
|
+
outWords[offsetWords + 1 * sampleWords] = rowBlock[comp][src + 1];
|
|
1402
|
+
outWords[offsetWords + 2 * sampleWords] = rowBlock[comp][src + 2];
|
|
1403
|
+
outWords[offsetWords + 3 * sampleWords] = rowBlock[comp][src + 3];
|
|
1404
|
+
outWords[offsetWords + 4 * sampleWords] = rowBlock[comp][src + 4];
|
|
1405
|
+
outWords[offsetWords + 5 * sampleWords] = rowBlock[comp][src + 5];
|
|
1406
|
+
outWords[offsetWords + 6 * sampleWords] = rowBlock[comp][src + 6];
|
|
1407
|
+
outWords[offsetWords + 7 * sampleWords] = rowBlock[comp][src + 7];
|
|
1408
|
+
offsetWords += 8 * sampleWords;
|
|
1409
|
+
}
|
|
1410
|
+
}
|
|
1411
|
+
if (numFullBlocksX !== numBlocksX) {
|
|
1412
|
+
for (let y = 8 * blockY; y < 8 * blockY + maxY; y++) {
|
|
1413
|
+
const offsetWords = (rowOffsets[comp][y] >> 1) + 8 * numFullBlocksX * sampleWords;
|
|
1414
|
+
const src = numFullBlocksX * 64 + (y & 7) * 8;
|
|
1415
|
+
for (let x = 0; x < maxX; x++) {
|
|
1416
|
+
outWords[offsetWords + x * sampleWords] = rowBlock[comp][src + x];
|
|
1417
|
+
}
|
|
1418
|
+
}
|
|
1419
|
+
}
|
|
1420
|
+
}
|
|
1421
|
+
}
|
|
1422
|
+
for (let comp = 0; comp < numComp; comp++) {
|
|
1423
|
+
const channelIndex = cscIndices[comp];
|
|
1424
|
+
const channel = channelData[channelIndex];
|
|
1425
|
+
channel.decoded = true;
|
|
1426
|
+
if (channel.pixelType !== 2) {
|
|
1427
|
+
continue;
|
|
1428
|
+
}
|
|
1429
|
+
const sampleWords = channel.sampleWords;
|
|
1430
|
+
for (let y = 0; y < height; y++) {
|
|
1431
|
+
const offset = rowOffsets[comp][y];
|
|
1432
|
+
let srcWords = offset >> 1;
|
|
1433
|
+
for (let x = 0; x < width; x++) {
|
|
1434
|
+
dataView.setFloat32(
|
|
1435
|
+
offset + x * INT16_SIZE * sampleWords,
|
|
1436
|
+
float16ToFloat32(outWords[srcWords]),
|
|
1437
|
+
true
|
|
1438
|
+
);
|
|
1439
|
+
srcWords += sampleWords;
|
|
1440
|
+
}
|
|
1441
|
+
}
|
|
1442
|
+
}
|
|
1443
|
+
return {
|
|
1444
|
+
acConsumed: currAcComp.value - acOffset,
|
|
1445
|
+
dcConsumed: currDcComp[numComp - 1] - dcOffset
|
|
1446
|
+
};
|
|
1447
|
+
}
|
|
1448
|
+
function lossyDctChannelDecode(channelIndex, rowPtrs, channelData, acBuffer, dcBuffer, outBuffer, acOffset, dcOffset) {
|
|
1449
|
+
const dataView = new DataView(outBuffer.buffer, outBuffer.byteOffset, outBuffer.byteLength);
|
|
1450
|
+
const outWords = new Uint16Array(
|
|
1451
|
+
outBuffer.buffer,
|
|
1452
|
+
outBuffer.byteOffset,
|
|
1453
|
+
outBuffer.byteLength >> 1
|
|
1454
|
+
);
|
|
1455
|
+
const channel = channelData[channelIndex];
|
|
1456
|
+
const width = channel.width;
|
|
1457
|
+
const height = channel.height;
|
|
1458
|
+
const numBlocksX = Math.ceil(width / 8);
|
|
1459
|
+
const numBlocksY = Math.ceil(height / 8);
|
|
1460
|
+
const numFullBlocksX = Math.floor(width / 8);
|
|
1461
|
+
const leftoverX = width - (numBlocksX - 1) * 8;
|
|
1462
|
+
const leftoverY = height - (numBlocksY - 1) * 8;
|
|
1463
|
+
const currAcComp = { value: acOffset };
|
|
1464
|
+
let currDcComp = dcOffset;
|
|
1465
|
+
const dctData = new Float32Array(64);
|
|
1466
|
+
const halfZigBlock = new Uint16Array(64);
|
|
1467
|
+
const rowBlock = new Uint16Array(numBlocksX * 64);
|
|
1468
|
+
for (let blockY = 0; blockY < numBlocksY; blockY++) {
|
|
1469
|
+
let maxY = 8;
|
|
1470
|
+
if (blockY === numBlocksY - 1) maxY = leftoverY;
|
|
1471
|
+
for (let blockX = 0; blockX < numBlocksX; blockX++) {
|
|
1472
|
+
halfZigBlock.fill(0);
|
|
1473
|
+
if (currDcComp >= dcBuffer.length) {
|
|
1474
|
+
throw new Error("Invalid DWA DC coefficient bounds.");
|
|
1475
|
+
}
|
|
1476
|
+
halfZigBlock[0] = dcBuffer[currDcComp++];
|
|
1477
|
+
unRleAC(currAcComp, acBuffer, halfZigBlock);
|
|
1478
|
+
unZigZag(halfZigBlock, dctData);
|
|
1479
|
+
dctInverse(dctData);
|
|
1480
|
+
convertToHalf(dctData, rowBlock, blockX * 64);
|
|
1481
|
+
}
|
|
1482
|
+
for (let y = 8 * blockY; y < 8 * blockY + maxY; y++) {
|
|
1483
|
+
let offsetWords = rowPtrs[channelIndex][y] >> 1;
|
|
1484
|
+
for (let blockX = 0; blockX < numFullBlocksX; blockX++) {
|
|
1485
|
+
const src = blockX * 64 + (y & 7) * 8;
|
|
1486
|
+
for (let x = 0; x < 8; x++) {
|
|
1487
|
+
outWords[offsetWords + x * channel.sampleWords] = rowBlock[src + x];
|
|
1488
|
+
}
|
|
1489
|
+
offsetWords += 8 * channel.sampleWords;
|
|
1490
|
+
}
|
|
1491
|
+
if (numBlocksX !== numFullBlocksX) {
|
|
1492
|
+
const src = numFullBlocksX * 64 + (y & 7) * 8;
|
|
1493
|
+
for (let x = 0; x < leftoverX; x++) {
|
|
1494
|
+
outWords[offsetWords + x * channel.sampleWords] = rowBlock[src + x];
|
|
1495
|
+
}
|
|
1496
|
+
}
|
|
1497
|
+
}
|
|
1498
|
+
}
|
|
1499
|
+
if (channel.pixelType === 2) {
|
|
1500
|
+
for (let y = 0; y < height; y++) {
|
|
1501
|
+
const offset = rowPtrs[channelIndex][y];
|
|
1502
|
+
let srcWords = offset >> 1;
|
|
1503
|
+
for (let x = 0; x < width; x++) {
|
|
1504
|
+
dataView.setFloat32(
|
|
1505
|
+
offset + x * INT16_SIZE * channel.sampleWords,
|
|
1506
|
+
float16ToFloat32(outWords[srcWords]),
|
|
1507
|
+
true
|
|
1508
|
+
);
|
|
1509
|
+
srcWords += channel.sampleWords;
|
|
1510
|
+
}
|
|
1511
|
+
}
|
|
1512
|
+
}
|
|
1513
|
+
channel.decoded = true;
|
|
1514
|
+
return {
|
|
1515
|
+
acConsumed: currAcComp.value - acOffset,
|
|
1516
|
+
dcConsumed: currDcComp - dcOffset
|
|
1517
|
+
};
|
|
1518
|
+
}
|
|
1519
|
+
function suffixForChannel(name) {
|
|
1520
|
+
const index = name.lastIndexOf(".");
|
|
1521
|
+
return index >= 0 ? name.slice(index + 1) : name;
|
|
1522
|
+
}
|
|
1523
|
+
function matchesRule(channelName, rule) {
|
|
1524
|
+
if (rule.caseInsensitive) {
|
|
1525
|
+
return channelName.localeCompare(rule.name, void 0, { sensitivity: "accent" }) === 0;
|
|
1526
|
+
}
|
|
1527
|
+
return channelName === rule.name;
|
|
1528
|
+
}
|
|
1529
|
+
function parseDwaHeader(view, cursor) {
|
|
1530
|
+
return {
|
|
1531
|
+
version: readInt64(view, cursor),
|
|
1532
|
+
unknownUncompressedSize: readInt64(view, cursor),
|
|
1533
|
+
unknownCompressedSize: readInt64(view, cursor),
|
|
1534
|
+
acCompressedSize: readInt64(view, cursor),
|
|
1535
|
+
dcCompressedSize: readInt64(view, cursor),
|
|
1536
|
+
rleCompressedSize: readInt64(view, cursor),
|
|
1537
|
+
rleUncompressedSize: readInt64(view, cursor),
|
|
1538
|
+
rleRawSize: readInt64(view, cursor),
|
|
1539
|
+
totalAcUncompressedCount: readInt64(view, cursor),
|
|
1540
|
+
totalDcUncompressedCount: readInt64(view, cursor),
|
|
1541
|
+
acCompression: readInt64(view, cursor)
|
|
1542
|
+
};
|
|
1543
|
+
}
|
|
1544
|
+
function parseDwaRules(bytes, view, cursor) {
|
|
1545
|
+
const rulesSize = readUint16(view, cursor);
|
|
1546
|
+
if (rulesSize < 2) {
|
|
1547
|
+
throw new Error("Invalid DWA channel rules payload size.");
|
|
1548
|
+
}
|
|
1549
|
+
const rulesEnd = cursor.value + (rulesSize - 2);
|
|
1550
|
+
if (rulesEnd > bytes.length) {
|
|
1551
|
+
throw new Error("Truncated DWA channel rules payload.");
|
|
1552
|
+
}
|
|
1553
|
+
const rules = [];
|
|
1554
|
+
while (cursor.value < rulesEnd) {
|
|
1555
|
+
const name = readCStringWithin(bytes, cursor, rulesEnd, "DWA channel rule name");
|
|
1556
|
+
const value = readUint8(view, cursor);
|
|
1557
|
+
const compression = value >> 2 & 3;
|
|
1558
|
+
const index = (value >> 4) - 1;
|
|
1559
|
+
const caseInsensitive = (value & 1) !== 0;
|
|
1560
|
+
const pixelType = readUint8(view, cursor);
|
|
1561
|
+
rules.push({
|
|
1562
|
+
name,
|
|
1563
|
+
compression,
|
|
1564
|
+
index,
|
|
1565
|
+
pixelType,
|
|
1566
|
+
caseInsensitive
|
|
1567
|
+
});
|
|
1568
|
+
}
|
|
1569
|
+
if (cursor.value !== rulesEnd) {
|
|
1570
|
+
throw new Error("Invalid DWA channel rules alignment.");
|
|
1571
|
+
}
|
|
1572
|
+
return rules;
|
|
1573
|
+
}
|
|
1574
|
+
function decodeDwaChunk(context) {
|
|
1575
|
+
if (!context.part.dataWindow) {
|
|
1576
|
+
throw new Error("Missing DWA part data window.");
|
|
1577
|
+
}
|
|
1578
|
+
if (context.linesInChunk <= 0) {
|
|
1579
|
+
return new Uint8Array(0);
|
|
1580
|
+
}
|
|
1581
|
+
const xMin = context.part.dataWindow.xMin;
|
|
1582
|
+
const xMax = context.part.dataWindow.xMax;
|
|
1583
|
+
const columns = xMax - xMin + 1;
|
|
1584
|
+
const lines = context.linesInChunk;
|
|
1585
|
+
for (const channel of context.part.channels) {
|
|
1586
|
+
const xSampling = channel.xSampling > 0 ? channel.xSampling : 1;
|
|
1587
|
+
const ySampling = channel.ySampling > 0 ? channel.ySampling : 1;
|
|
1588
|
+
if (xSampling !== 1 || ySampling !== 1) {
|
|
1589
|
+
throw new Error(
|
|
1590
|
+
"DWAA/DWAB decoding currently requires xSampling=1 and ySampling=1 for all channels."
|
|
1591
|
+
);
|
|
1592
|
+
}
|
|
1593
|
+
}
|
|
1594
|
+
const bytes = new Uint8Array(context.buffer, context.dataPtr, context.dataSize);
|
|
1595
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
1596
|
+
const cursor = { value: 0 };
|
|
1597
|
+
const header = parseDwaHeader(view, cursor);
|
|
1598
|
+
if (header.version < 1 || header.version > 2) {
|
|
1599
|
+
throw new Error(`Unsupported DWA version ${header.version}.`);
|
|
1600
|
+
}
|
|
1601
|
+
const rules = header.version >= 2 ? parseDwaRules(bytes, view, cursor) : LEGACY_DWA_RULES;
|
|
1602
|
+
if (header.unknownCompressedSize > 0 || header.unknownUncompressedSize > 0) {
|
|
1603
|
+
throw new Error("DWAA/DWAB UNKNOWN channel blocks are not supported.");
|
|
1604
|
+
}
|
|
1605
|
+
const channelData = context.part.channels.map((channel) => ({
|
|
1606
|
+
name: channel.name,
|
|
1607
|
+
compression: UNKNOWN,
|
|
1608
|
+
decoded: false,
|
|
1609
|
+
pixelType: channel.pixelType,
|
|
1610
|
+
sampleWords: channel.pixelType === 1 ? 1 : 2,
|
|
1611
|
+
pLinear: channel.pLinear,
|
|
1612
|
+
width: columns,
|
|
1613
|
+
height: lines
|
|
1614
|
+
}));
|
|
1615
|
+
const cscSet = { idx: new Array(3) };
|
|
1616
|
+
for (let offset = 0; offset < channelData.length; offset++) {
|
|
1617
|
+
const channel = channelData[offset];
|
|
1618
|
+
const suffix = suffixForChannel(channel.name);
|
|
1619
|
+
for (const rule of rules) {
|
|
1620
|
+
if (rule.pixelType !== channel.pixelType) continue;
|
|
1621
|
+
if (!matchesRule(suffix, rule)) continue;
|
|
1622
|
+
channel.compression = rule.compression;
|
|
1623
|
+
if (rule.index >= 0) {
|
|
1624
|
+
cscSet.idx[rule.index] = offset;
|
|
1625
|
+
}
|
|
1626
|
+
break;
|
|
1627
|
+
}
|
|
1628
|
+
}
|
|
1629
|
+
for (const channel of channelData) {
|
|
1630
|
+
if (channel.compression !== LOSSY_DCT && channel.compression !== RLE) {
|
|
1631
|
+
throw new Error(`Unsupported DWA channel compression for channel ${channel.name}.`);
|
|
1632
|
+
}
|
|
1633
|
+
if (channel.compression === LOSSY_DCT && channel.pixelType !== 1 && channel.pixelType !== 2) {
|
|
1634
|
+
throw new Error(`Unsupported DWA lossy channel pixel type for channel ${channel.name}.`);
|
|
1635
|
+
}
|
|
1636
|
+
}
|
|
1637
|
+
let acBuffer = new Uint16Array(0);
|
|
1638
|
+
let dcBuffer = new Uint16Array(0);
|
|
1639
|
+
let rleBuffer = new Uint8Array(0);
|
|
1640
|
+
if (header.acCompressedSize > 0) {
|
|
1641
|
+
if (cursor.value + header.acCompressedSize > bytes.length) {
|
|
1642
|
+
throw new Error("Truncated DWA AC payload.");
|
|
1643
|
+
}
|
|
1644
|
+
switch (header.acCompression) {
|
|
1645
|
+
case STATIC_HUFFMAN: {
|
|
1646
|
+
acBuffer = new Uint16Array(header.totalAcUncompressedCount);
|
|
1647
|
+
const acStart = cursor.value;
|
|
1648
|
+
hufUncompress(
|
|
1649
|
+
bytes,
|
|
1650
|
+
view,
|
|
1651
|
+
cursor,
|
|
1652
|
+
header.acCompressedSize,
|
|
1653
|
+
acBuffer,
|
|
1654
|
+
header.totalAcUncompressedCount
|
|
1655
|
+
);
|
|
1656
|
+
cursor.value = acStart + header.acCompressedSize;
|
|
1657
|
+
break;
|
|
1658
|
+
}
|
|
1659
|
+
case DEFLATE: {
|
|
1660
|
+
const compressed = bytes.subarray(cursor.value, cursor.value + header.acCompressedSize);
|
|
1661
|
+
const decoded = (0, import_fflate.unzlibSync)(compressed, {
|
|
1662
|
+
out: new Uint8Array(header.totalAcUncompressedCount * INT16_SIZE)
|
|
1663
|
+
});
|
|
1664
|
+
if (decoded.byteLength % 2 !== 0) {
|
|
1665
|
+
throw new Error("Invalid DWA AC DEFLATE payload size.");
|
|
1666
|
+
}
|
|
1667
|
+
const decodedWords = decoded.byteLength / 2;
|
|
1668
|
+
if (decodedWords !== header.totalAcUncompressedCount) {
|
|
1669
|
+
throw new Error("Invalid DWA AC DEFLATE payload length.");
|
|
1670
|
+
}
|
|
1671
|
+
const decodedView = new Uint16Array(decoded.buffer, decoded.byteOffset, decodedWords);
|
|
1672
|
+
acBuffer = new Uint16Array(decodedWords);
|
|
1673
|
+
acBuffer.set(decodedView);
|
|
1674
|
+
cursor.value += header.acCompressedSize;
|
|
1675
|
+
break;
|
|
1676
|
+
}
|
|
1677
|
+
default:
|
|
1678
|
+
throw new Error(`Unsupported DWA AC compression mode ${header.acCompression}.`);
|
|
1679
|
+
}
|
|
1680
|
+
} else if (header.totalAcUncompressedCount !== 0) {
|
|
1681
|
+
throw new Error("Invalid DWA AC payload metadata.");
|
|
1682
|
+
}
|
|
1683
|
+
if (header.dcCompressedSize > 0) {
|
|
1684
|
+
const dcBytes = uncompressZipStyle(
|
|
1685
|
+
bytes,
|
|
1686
|
+
cursor.value,
|
|
1687
|
+
header.dcCompressedSize,
|
|
1688
|
+
header.totalDcUncompressedCount * INT16_SIZE,
|
|
1689
|
+
DWA_DC_SCRATCH
|
|
1690
|
+
);
|
|
1691
|
+
if (dcBytes.byteLength % 2 !== 0) {
|
|
1692
|
+
throw new Error("Invalid DWA DC payload size.");
|
|
1693
|
+
}
|
|
1694
|
+
const dcWords = dcBytes.byteLength / 2;
|
|
1695
|
+
if (dcWords !== header.totalDcUncompressedCount) {
|
|
1696
|
+
throw new Error("Invalid DWA DC payload length.");
|
|
1697
|
+
}
|
|
1698
|
+
const dcView = new Uint16Array(dcBytes.buffer, dcBytes.byteOffset, dcWords);
|
|
1699
|
+
dcBuffer = new Uint16Array(dcWords);
|
|
1700
|
+
dcBuffer.set(dcView);
|
|
1701
|
+
cursor.value += header.dcCompressedSize;
|
|
1702
|
+
} else if (header.totalDcUncompressedCount !== 0) {
|
|
1703
|
+
throw new Error("Invalid DWA DC payload metadata.");
|
|
1704
|
+
}
|
|
1705
|
+
if (header.rleRawSize > 0) {
|
|
1706
|
+
if (cursor.value + header.rleCompressedSize > bytes.length) {
|
|
1707
|
+
throw new Error("Truncated DWA RLE payload.");
|
|
1708
|
+
}
|
|
1709
|
+
const compressed = bytes.subarray(cursor.value, cursor.value + header.rleCompressedSize);
|
|
1710
|
+
const decoded = (0, import_fflate.unzlibSync)(compressed, {
|
|
1711
|
+
out: new Uint8Array(header.rleUncompressedSize)
|
|
1712
|
+
});
|
|
1713
|
+
if (decoded.byteLength !== header.rleUncompressedSize) {
|
|
1714
|
+
throw new Error("Invalid DWA RLE intermediate payload length.");
|
|
1715
|
+
}
|
|
1716
|
+
const decodedRle = decodeRunLength(decoded, header.rleRawSize);
|
|
1717
|
+
rleBuffer = new Uint8Array(decodedRle.byteLength);
|
|
1718
|
+
rleBuffer.set(decodedRle);
|
|
1719
|
+
cursor.value += header.rleCompressedSize;
|
|
1720
|
+
} else if (header.rleCompressedSize !== 0 || header.rleUncompressedSize !== 0) {
|
|
1721
|
+
throw new Error("Invalid DWA RLE payload metadata.");
|
|
1722
|
+
}
|
|
1723
|
+
let outputSize = 0;
|
|
1724
|
+
const rowOffsets = new Array(channelData.length);
|
|
1725
|
+
for (let i = 0; i < channelData.length; i++) {
|
|
1726
|
+
rowOffsets[i] = new Int32Array(lines);
|
|
1727
|
+
}
|
|
1728
|
+
for (let y = 0; y < lines; y++) {
|
|
1729
|
+
for (let chan = 0; chan < channelData.length; chan++) {
|
|
1730
|
+
rowOffsets[chan][y] = outputSize;
|
|
1731
|
+
outputSize += channelData[chan].width * channelData[chan].sampleWords * INT16_SIZE;
|
|
1732
|
+
}
|
|
1733
|
+
}
|
|
1734
|
+
const outBuffer = new Uint8Array(outputSize);
|
|
1735
|
+
let acOffset = 0;
|
|
1736
|
+
let dcOffset = 0;
|
|
1737
|
+
if (cscSet.idx[0] !== void 0 && cscSet.idx[1] !== void 0 && cscSet.idx[2] !== void 0) {
|
|
1738
|
+
const usage = lossyDctDecode(
|
|
1739
|
+
cscSet,
|
|
1740
|
+
rowOffsets,
|
|
1741
|
+
channelData,
|
|
1742
|
+
acBuffer,
|
|
1743
|
+
dcBuffer,
|
|
1744
|
+
outBuffer,
|
|
1745
|
+
acOffset,
|
|
1746
|
+
dcOffset
|
|
1747
|
+
);
|
|
1748
|
+
acOffset += usage.acConsumed;
|
|
1749
|
+
dcOffset += usage.dcConsumed;
|
|
1750
|
+
}
|
|
1751
|
+
let rleOffset = 0;
|
|
1752
|
+
for (let i = 0; i < channelData.length; i++) {
|
|
1753
|
+
const channel = channelData[i];
|
|
1754
|
+
if (channel.decoded) continue;
|
|
1755
|
+
switch (channel.compression) {
|
|
1756
|
+
case RLE: {
|
|
1757
|
+
for (let y = 0; y < lines; y++) {
|
|
1758
|
+
let rowOffsetBytes = rowOffsets[i][y];
|
|
1759
|
+
for (let x = 0; x < channel.width; x++) {
|
|
1760
|
+
for (let byte = 0; byte < channel.sampleWords * INT16_SIZE; byte++) {
|
|
1761
|
+
const src = rleOffset + byte * channel.width * channel.height;
|
|
1762
|
+
if (src < 0 || src >= rleBuffer.length) {
|
|
1763
|
+
throw new Error("Invalid DWA RLE planar bounds.");
|
|
1764
|
+
}
|
|
1765
|
+
outBuffer[rowOffsetBytes++] = rleBuffer[src];
|
|
1766
|
+
}
|
|
1767
|
+
rleOffset++;
|
|
1768
|
+
}
|
|
1769
|
+
}
|
|
1770
|
+
channel.decoded = true;
|
|
1771
|
+
break;
|
|
1772
|
+
}
|
|
1773
|
+
case LOSSY_DCT: {
|
|
1774
|
+
const usage = lossyDctChannelDecode(
|
|
1775
|
+
i,
|
|
1776
|
+
rowOffsets,
|
|
1777
|
+
channelData,
|
|
1778
|
+
acBuffer,
|
|
1779
|
+
dcBuffer,
|
|
1780
|
+
outBuffer,
|
|
1781
|
+
acOffset,
|
|
1782
|
+
dcOffset
|
|
1783
|
+
);
|
|
1784
|
+
acOffset += usage.acConsumed;
|
|
1785
|
+
dcOffset += usage.dcConsumed;
|
|
1786
|
+
break;
|
|
1787
|
+
}
|
|
1788
|
+
default:
|
|
1789
|
+
throw new Error(`Unsupported DWA channel compression for channel ${channel.name}.`);
|
|
1790
|
+
}
|
|
1791
|
+
}
|
|
1792
|
+
return outBuffer;
|
|
1793
|
+
}
|
|
1794
|
+
function decodeDwaBlock(context) {
|
|
1795
|
+
try {
|
|
1796
|
+
return decodeDwaChunk(context);
|
|
1797
|
+
} catch (error) {
|
|
1798
|
+
if (error instanceof ExrError) {
|
|
1799
|
+
throw error;
|
|
1800
|
+
}
|
|
1801
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
1802
|
+
throw new ExrError("DECOMPRESSION_FAILED", "Failed to decompress DWAA/DWAB chunk.", {
|
|
1803
|
+
partId: context.partId,
|
|
1804
|
+
chunkIndex: context.chunkIndex,
|
|
1805
|
+
size: context.dataSize,
|
|
1806
|
+
reason
|
|
1807
|
+
});
|
|
1808
|
+
}
|
|
1809
|
+
}
|
|
1810
|
+
|
|
1811
|
+
// src/reader/compression/piz.ts
|
|
1812
|
+
var USHORT_RANGE = 1 << 16;
|
|
1813
|
+
var BITMAP_SIZE = USHORT_RANGE >> 3;
|
|
1814
|
+
var HUF_ENCBITS2 = 16;
|
|
1815
|
+
var HUF_DECBITS2 = 14;
|
|
1816
|
+
var HUF_ENCSIZE2 = (1 << HUF_ENCBITS2) + 1;
|
|
1817
|
+
var HUF_DECSIZE2 = 1 << HUF_DECBITS2;
|
|
1818
|
+
var HUF_DECMASK2 = HUF_DECSIZE2 - 1;
|
|
1819
|
+
var SHORT_ZEROCODE_RUN2 = 59;
|
|
1820
|
+
var LONG_ZEROCODE_RUN2 = 63;
|
|
1821
|
+
var SHORTEST_LONG_RUN2 = 2 + LONG_ZEROCODE_RUN2 - SHORT_ZEROCODE_RUN2;
|
|
1822
|
+
var NBITS = 16;
|
|
1823
|
+
var A_OFFSET = 1 << NBITS - 1;
|
|
1824
|
+
var MOD_MASK = (1 << NBITS) - 1;
|
|
1825
|
+
function modulo2(value, base) {
|
|
1826
|
+
const result = value % base;
|
|
1827
|
+
return result < 0 ? result + base : result;
|
|
1828
|
+
}
|
|
1829
|
+
function firstSampleCoordinate2(min, sampling) {
|
|
1830
|
+
if (sampling <= 1) return min;
|
|
1831
|
+
const remainder = modulo2(min, sampling);
|
|
1832
|
+
return remainder === 0 ? min : min + (sampling - remainder);
|
|
1833
|
+
}
|
|
1834
|
+
function countSamplesInRange2(min, max, sampling) {
|
|
1835
|
+
if (sampling <= 0 || max < min) return 0;
|
|
1836
|
+
const first = firstSampleCoordinate2(min, sampling);
|
|
1837
|
+
if (first > max) return 0;
|
|
1838
|
+
return Math.floor((max - first) / sampling) + 1;
|
|
1839
|
+
}
|
|
1840
|
+
function isSampledCoordinate2(value, firstSample, sampling) {
|
|
1841
|
+
if (sampling <= 1) return true;
|
|
1842
|
+
if (value < firstSample) return false;
|
|
1843
|
+
return (value - firstSample) % sampling === 0;
|
|
1844
|
+
}
|
|
1845
|
+
function countSampledRowsInRange(start, end, sampleOrigin, sampling) {
|
|
1846
|
+
if (end < start) return 0;
|
|
1847
|
+
if (sampling <= 1) return end - start + 1;
|
|
1848
|
+
let count = 0;
|
|
1849
|
+
for (let y = start; y <= end; y++) {
|
|
1850
|
+
if (isSampledCoordinate2(y, sampleOrigin, sampling)) {
|
|
1851
|
+
count++;
|
|
1852
|
+
}
|
|
1853
|
+
}
|
|
1854
|
+
return count;
|
|
1855
|
+
}
|
|
1856
|
+
function readUint162(view, cursor) {
|
|
1857
|
+
if (cursor.value + 2 > view.byteLength) {
|
|
1858
|
+
throw new Error("Truncated PIZ payload.");
|
|
1859
|
+
}
|
|
1860
|
+
const value = view.getUint16(cursor.value, true);
|
|
1861
|
+
cursor.value += 2;
|
|
1862
|
+
return value;
|
|
1863
|
+
}
|
|
1864
|
+
function readUint322(view, cursor) {
|
|
1865
|
+
if (cursor.value + 4 > view.byteLength) {
|
|
1866
|
+
throw new Error("Truncated PIZ payload.");
|
|
1867
|
+
}
|
|
1868
|
+
const value = view.getUint32(cursor.value, true);
|
|
1869
|
+
cursor.value += 4;
|
|
1870
|
+
return value;
|
|
1871
|
+
}
|
|
1872
|
+
function reverseLutFromBitmap(bitmap, lut) {
|
|
1873
|
+
let k = 0;
|
|
1874
|
+
for (let i = 0; i < USHORT_RANGE; i++) {
|
|
1875
|
+
if (i === 0 || bitmap[i >> 3] & 1 << (i & 7)) {
|
|
1876
|
+
lut[k++] = i;
|
|
1877
|
+
}
|
|
1878
|
+
}
|
|
1879
|
+
const n = k - 1;
|
|
1880
|
+
while (k < USHORT_RANGE) {
|
|
1881
|
+
lut[k++] = 0;
|
|
1882
|
+
}
|
|
1883
|
+
return n;
|
|
1884
|
+
}
|
|
1885
|
+
function clearHuffmanDecodingTable2(table) {
|
|
1886
|
+
for (let i = 0; i < HUF_DECSIZE2; i++) {
|
|
1887
|
+
table[i] = { len: 0, lit: 0, p: null };
|
|
1888
|
+
}
|
|
1889
|
+
}
|
|
1890
|
+
function getBits2(nBits, state, input, cursor) {
|
|
1891
|
+
while (state.lc < nBits) {
|
|
1892
|
+
state.c = (state.c << 8 | readUint8FromArray2(input, cursor)) >>> 0;
|
|
1893
|
+
state.lc += 8;
|
|
1894
|
+
}
|
|
1895
|
+
state.lc -= nBits;
|
|
1896
|
+
return state.c >>> state.lc & (1 << nBits) - 1;
|
|
1897
|
+
}
|
|
1898
|
+
var HUF_TABLE_BUFFER2 = new Int32Array(59);
|
|
1899
|
+
function hufCanonicalCodeTable2(hcode) {
|
|
1900
|
+
HUF_TABLE_BUFFER2.fill(0);
|
|
1901
|
+
for (let i = 0; i < HUF_ENCSIZE2; i++) {
|
|
1902
|
+
HUF_TABLE_BUFFER2[hcode[i]] += 1;
|
|
1903
|
+
}
|
|
1904
|
+
let c = 0;
|
|
1905
|
+
for (let i = 58; i > 0; i--) {
|
|
1906
|
+
const nextCode = c + HUF_TABLE_BUFFER2[i] >> 1;
|
|
1907
|
+
HUF_TABLE_BUFFER2[i] = c;
|
|
1908
|
+
c = nextCode;
|
|
1909
|
+
}
|
|
1910
|
+
for (let i = 0; i < HUF_ENCSIZE2; i++) {
|
|
1911
|
+
const length = hcode[i];
|
|
1912
|
+
if (length > 0) {
|
|
1913
|
+
hcode[i] = length | HUF_TABLE_BUFFER2[length]++ << 6;
|
|
1914
|
+
}
|
|
1915
|
+
}
|
|
1916
|
+
}
|
|
1917
|
+
function hufUnpackEncTable2(input, cursor, maxTableBytes, minSymbol, maxSymbol, hcode) {
|
|
1918
|
+
const start = cursor.value;
|
|
1919
|
+
const state = { c: 0, lc: 0 };
|
|
1920
|
+
for (let symbol = minSymbol; symbol <= maxSymbol; symbol++) {
|
|
1921
|
+
if (cursor.value - start > maxTableBytes) {
|
|
1922
|
+
throw new Error("Invalid PIZ Huffman table payload.");
|
|
1923
|
+
}
|
|
1924
|
+
const length = getBits2(6, state, input, cursor);
|
|
1925
|
+
hcode[symbol] = length;
|
|
1926
|
+
if (length === LONG_ZEROCODE_RUN2) {
|
|
1927
|
+
if (cursor.value - start > maxTableBytes) {
|
|
1928
|
+
throw new Error("Invalid PIZ Huffman table payload.");
|
|
1929
|
+
}
|
|
1930
|
+
let zeroRun = getBits2(8, state, input, cursor) + SHORTEST_LONG_RUN2;
|
|
1931
|
+
if (symbol + zeroRun > maxSymbol + 1) {
|
|
1932
|
+
throw new Error("Invalid PIZ Huffman zero-run.");
|
|
1933
|
+
}
|
|
1934
|
+
while (zeroRun-- > 0) {
|
|
1935
|
+
hcode[symbol++] = 0;
|
|
1936
|
+
}
|
|
1937
|
+
symbol--;
|
|
1938
|
+
} else if (length >= SHORT_ZEROCODE_RUN2) {
|
|
1939
|
+
let zeroRun = length - SHORT_ZEROCODE_RUN2 + 2;
|
|
1940
|
+
if (symbol + zeroRun > maxSymbol + 1) {
|
|
1941
|
+
throw new Error("Invalid PIZ Huffman zero-run.");
|
|
1942
|
+
}
|
|
1943
|
+
while (zeroRun-- > 0) {
|
|
1944
|
+
hcode[symbol++] = 0;
|
|
1945
|
+
}
|
|
1946
|
+
symbol--;
|
|
1947
|
+
}
|
|
1948
|
+
}
|
|
1949
|
+
hufCanonicalCodeTable2(hcode);
|
|
1950
|
+
}
|
|
1951
|
+
function hufLength2(code) {
|
|
1952
|
+
return code & 63;
|
|
1953
|
+
}
|
|
1954
|
+
function hufCode2(code) {
|
|
1955
|
+
return code >>> 6;
|
|
1956
|
+
}
|
|
1957
|
+
function hufBuildDecTable2(hcode, minSymbol, maxSymbol, table) {
|
|
1958
|
+
for (let symbol = minSymbol; symbol <= maxSymbol; symbol++) {
|
|
1959
|
+
const code = hufCode2(hcode[symbol]);
|
|
1960
|
+
const length = hufLength2(hcode[symbol]);
|
|
1961
|
+
if (code >>> length) {
|
|
1962
|
+
throw new Error("Invalid PIZ Huffman code.");
|
|
1963
|
+
}
|
|
1964
|
+
if (length > HUF_DECBITS2) {
|
|
1965
|
+
const entry = table[code >>> length - HUF_DECBITS2];
|
|
1966
|
+
if (entry.len) {
|
|
1967
|
+
throw new Error("Invalid PIZ Huffman decode table entry.");
|
|
1968
|
+
}
|
|
1969
|
+
entry.lit += 1;
|
|
1970
|
+
if (entry.p) {
|
|
1971
|
+
entry.p.push(symbol);
|
|
1972
|
+
} else {
|
|
1973
|
+
entry.p = [symbol];
|
|
1974
|
+
}
|
|
1975
|
+
} else if (length) {
|
|
1976
|
+
const base = code << HUF_DECBITS2 - length;
|
|
1977
|
+
const count = 1 << HUF_DECBITS2 - length;
|
|
1978
|
+
for (let i = 0; i < count; i++) {
|
|
1979
|
+
const entry = table[base + i];
|
|
1980
|
+
if (entry.len || entry.p) {
|
|
1981
|
+
throw new Error("Invalid PIZ Huffman decode table entry.");
|
|
1982
|
+
}
|
|
1983
|
+
entry.len = length;
|
|
1984
|
+
entry.lit = symbol;
|
|
1985
|
+
}
|
|
1986
|
+
}
|
|
1987
|
+
}
|
|
1988
|
+
}
|
|
1989
|
+
function readUint8FromArray2(input, cursor) {
|
|
1990
|
+
if (cursor.value >= input.length) {
|
|
1991
|
+
throw new Error("Truncated PIZ Huffman payload.");
|
|
1992
|
+
}
|
|
1993
|
+
const value = input[cursor.value];
|
|
1994
|
+
cursor.value += 1;
|
|
1995
|
+
return value;
|
|
1996
|
+
}
|
|
1997
|
+
function getChar2(state, input, cursor) {
|
|
1998
|
+
state.c = (state.c << 8 | readUint8FromArray2(input, cursor)) >>> 0;
|
|
1999
|
+
state.lc += 8;
|
|
2000
|
+
}
|
|
2001
|
+
function getCode2(symbol, runLengthCode, state, input, cursor, outBuffer, outOffset, outEnd) {
|
|
2002
|
+
if (symbol === runLengthCode) {
|
|
2003
|
+
if (state.lc < 8) {
|
|
2004
|
+
getChar2(state, input, cursor);
|
|
2005
|
+
}
|
|
2006
|
+
state.lc -= 8;
|
|
2007
|
+
let repeatCount = state.c >>> state.lc & 255;
|
|
2008
|
+
if (outOffset.value === 0 || outOffset.value + repeatCount > outEnd) {
|
|
2009
|
+
throw new Error("Invalid PIZ run-length code.");
|
|
2010
|
+
}
|
|
2011
|
+
const last = outBuffer[outOffset.value - 1];
|
|
2012
|
+
while (repeatCount-- > 0) {
|
|
2013
|
+
outBuffer[outOffset.value++] = last;
|
|
2014
|
+
}
|
|
2015
|
+
} else if (outOffset.value < outEnd) {
|
|
2016
|
+
outBuffer[outOffset.value++] = symbol;
|
|
2017
|
+
} else {
|
|
2018
|
+
throw new Error("PIZ Huffman decode overflow.");
|
|
2019
|
+
}
|
|
2020
|
+
}
|
|
2021
|
+
function hufDecode2(encodingTable, decodingTable, input, cursor, nBits, runLengthCode, outSize, outBuffer, outOffset) {
|
|
2022
|
+
const state = { c: 0, lc: 0 };
|
|
2023
|
+
const inOffsetEnd = Math.trunc(cursor.value + (nBits + 7) / 8);
|
|
2024
|
+
while (cursor.value < inOffsetEnd) {
|
|
2025
|
+
getChar2(state, input, cursor);
|
|
2026
|
+
while (state.lc >= HUF_DECBITS2) {
|
|
2027
|
+
const index = state.c >>> state.lc - HUF_DECBITS2 & HUF_DECMASK2;
|
|
2028
|
+
const entry = decodingTable[index];
|
|
2029
|
+
if (entry.len) {
|
|
2030
|
+
state.lc -= entry.len;
|
|
2031
|
+
getCode2(entry.lit, runLengthCode, state, input, cursor, outBuffer, outOffset, outSize);
|
|
2032
|
+
} else {
|
|
2033
|
+
if (!entry.p) {
|
|
2034
|
+
throw new Error("PIZ Huffman decode failed.");
|
|
2035
|
+
}
|
|
2036
|
+
let matched = false;
|
|
2037
|
+
for (let i = 0; i < entry.lit; i++) {
|
|
2038
|
+
const symbol = entry.p[i];
|
|
2039
|
+
const length = hufLength2(encodingTable[symbol]);
|
|
2040
|
+
while (state.lc < length && cursor.value < inOffsetEnd) {
|
|
2041
|
+
getChar2(state, input, cursor);
|
|
2042
|
+
}
|
|
2043
|
+
if (state.lc >= length) {
|
|
2044
|
+
const code = hufCode2(encodingTable[symbol]);
|
|
2045
|
+
const candidate = state.c >>> state.lc - length & (1 << length) - 1;
|
|
2046
|
+
if (code === candidate) {
|
|
2047
|
+
state.lc -= length;
|
|
2048
|
+
getCode2(symbol, runLengthCode, state, input, cursor, outBuffer, outOffset, outSize);
|
|
2049
|
+
matched = true;
|
|
2050
|
+
break;
|
|
2051
|
+
}
|
|
2052
|
+
}
|
|
2053
|
+
}
|
|
2054
|
+
if (!matched) {
|
|
2055
|
+
throw new Error("PIZ Huffman decode failed.");
|
|
2056
|
+
}
|
|
2057
|
+
}
|
|
2058
|
+
}
|
|
2059
|
+
}
|
|
2060
|
+
const pad = 8 - nBits & 7;
|
|
2061
|
+
state.c >>>= pad;
|
|
2062
|
+
state.lc -= pad;
|
|
2063
|
+
while (state.lc > 0) {
|
|
2064
|
+
const entry = decodingTable[state.c << HUF_DECBITS2 - state.lc & HUF_DECMASK2];
|
|
2065
|
+
if (!entry.len) {
|
|
2066
|
+
throw new Error("PIZ Huffman decode failed.");
|
|
2067
|
+
}
|
|
2068
|
+
state.lc -= entry.len;
|
|
2069
|
+
getCode2(entry.lit, runLengthCode, state, input, cursor, outBuffer, outOffset, outSize);
|
|
2070
|
+
}
|
|
2071
|
+
}
|
|
2072
|
+
function hufUncompress2(input, view, cursor, nCompressed, out, nRaw) {
|
|
2073
|
+
const outOffset = { value: 0 };
|
|
2074
|
+
const initialOffset = cursor.value;
|
|
2075
|
+
const minSymbol = readUint322(view, cursor);
|
|
2076
|
+
const maxSymbol = readUint322(view, cursor);
|
|
2077
|
+
readUint322(view, cursor);
|
|
2078
|
+
const nBits = readUint322(view, cursor);
|
|
2079
|
+
readUint322(view, cursor);
|
|
2080
|
+
if (minSymbol < 0 || minSymbol >= HUF_ENCSIZE2 || maxSymbol < 0 || maxSymbol >= HUF_ENCSIZE2 || minSymbol > maxSymbol) {
|
|
2081
|
+
throw new Error("Invalid PIZ Huffman symbol bounds.");
|
|
2082
|
+
}
|
|
2083
|
+
const encodingTable = new Int32Array(HUF_ENCSIZE2);
|
|
2084
|
+
const decodingTable = new Array(HUF_DECSIZE2);
|
|
2085
|
+
clearHuffmanDecodingTable2(decodingTable);
|
|
2086
|
+
const tableBytes = nCompressed - (cursor.value - initialOffset);
|
|
2087
|
+
if (tableBytes < 0) {
|
|
2088
|
+
throw new Error("Invalid PIZ Huffman payload size.");
|
|
2089
|
+
}
|
|
2090
|
+
hufUnpackEncTable2(input, cursor, tableBytes, minSymbol, maxSymbol, encodingTable);
|
|
2091
|
+
if (nBits > 8 * (nCompressed - (cursor.value - initialOffset))) {
|
|
2092
|
+
throw new Error("Invalid PIZ Huffman bit count.");
|
|
2093
|
+
}
|
|
2094
|
+
hufBuildDecTable2(encodingTable, minSymbol, maxSymbol, decodingTable);
|
|
2095
|
+
hufDecode2(encodingTable, decodingTable, input, cursor, nBits, maxSymbol, nRaw, out, outOffset);
|
|
2096
|
+
if (outOffset.value !== nRaw) {
|
|
2097
|
+
throw new Error("PIZ Huffman output size mismatch.");
|
|
2098
|
+
}
|
|
2099
|
+
}
|
|
2100
|
+
function uint16(value) {
|
|
2101
|
+
return value & 65535;
|
|
2102
|
+
}
|
|
2103
|
+
function int16(value) {
|
|
2104
|
+
const clamped = uint16(value);
|
|
2105
|
+
return clamped > 32767 ? clamped - 65536 : clamped;
|
|
2106
|
+
}
|
|
2107
|
+
var waveletPair = { a: 0, b: 0 };
|
|
2108
|
+
function wdec14(low, high) {
|
|
2109
|
+
const ls = int16(low);
|
|
2110
|
+
const hs = int16(high);
|
|
2111
|
+
const ai = ls + (hs & 1) + (hs >> 1);
|
|
2112
|
+
const bi = ai - hs;
|
|
2113
|
+
waveletPair.a = ai;
|
|
2114
|
+
waveletPair.b = bi;
|
|
2115
|
+
}
|
|
2116
|
+
function wdec16(low, high) {
|
|
2117
|
+
const m = uint16(low);
|
|
2118
|
+
const d = uint16(high);
|
|
2119
|
+
const b = m - (d >> 1) & MOD_MASK;
|
|
2120
|
+
const a = d + b - A_OFFSET & MOD_MASK;
|
|
2121
|
+
waveletPair.a = a;
|
|
2122
|
+
waveletPair.b = b;
|
|
2123
|
+
}
|
|
2124
|
+
function wav2Decode(buffer, channelOffset, nx, ox, ny, oy, maxValue) {
|
|
2125
|
+
if (nx <= 0 || ny <= 0) {
|
|
2126
|
+
return;
|
|
2127
|
+
}
|
|
2128
|
+
const wavelet14 = maxValue < 1 << 14;
|
|
2129
|
+
const minDim = nx > ny ? ny : nx;
|
|
2130
|
+
let p = 1;
|
|
2131
|
+
while (p <= minDim) {
|
|
2132
|
+
p <<= 1;
|
|
2133
|
+
}
|
|
2134
|
+
let p2 = p >> 1;
|
|
2135
|
+
p >>= 2;
|
|
2136
|
+
while (p >= 1) {
|
|
2137
|
+
let py = 0;
|
|
2138
|
+
const ey = oy * (ny - p2);
|
|
2139
|
+
const oy1 = oy * p;
|
|
2140
|
+
const oy2 = oy * p2;
|
|
2141
|
+
const ox1 = ox * p;
|
|
2142
|
+
const ox2 = ox * p2;
|
|
2143
|
+
for (; py <= ey; py += oy2) {
|
|
2144
|
+
let px = py;
|
|
2145
|
+
const ex = py + ox * (nx - p2);
|
|
2146
|
+
for (; px <= ex; px += ox2) {
|
|
2147
|
+
const p01 = px + ox1;
|
|
2148
|
+
const p10 = px + oy1;
|
|
2149
|
+
const p11 = p10 + ox1;
|
|
2150
|
+
if (wavelet14) {
|
|
2151
|
+
wdec14(buffer[px + channelOffset], buffer[p10 + channelOffset]);
|
|
2152
|
+
const i00 = waveletPair.a;
|
|
2153
|
+
const i10 = waveletPair.b;
|
|
2154
|
+
wdec14(buffer[p01 + channelOffset], buffer[p11 + channelOffset]);
|
|
2155
|
+
const i01 = waveletPair.a;
|
|
2156
|
+
const i11 = waveletPair.b;
|
|
2157
|
+
wdec14(i00, i01);
|
|
2158
|
+
buffer[px + channelOffset] = waveletPair.a;
|
|
2159
|
+
buffer[p01 + channelOffset] = waveletPair.b;
|
|
2160
|
+
wdec14(i10, i11);
|
|
2161
|
+
buffer[p10 + channelOffset] = waveletPair.a;
|
|
2162
|
+
buffer[p11 + channelOffset] = waveletPair.b;
|
|
2163
|
+
} else {
|
|
2164
|
+
wdec16(buffer[px + channelOffset], buffer[p10 + channelOffset]);
|
|
2165
|
+
const i00 = waveletPair.a;
|
|
2166
|
+
const i10 = waveletPair.b;
|
|
2167
|
+
wdec16(buffer[p01 + channelOffset], buffer[p11 + channelOffset]);
|
|
2168
|
+
const i01 = waveletPair.a;
|
|
2169
|
+
const i11 = waveletPair.b;
|
|
2170
|
+
wdec16(i00, i01);
|
|
2171
|
+
buffer[px + channelOffset] = waveletPair.a;
|
|
2172
|
+
buffer[p01 + channelOffset] = waveletPair.b;
|
|
2173
|
+
wdec16(i10, i11);
|
|
2174
|
+
buffer[p10 + channelOffset] = waveletPair.a;
|
|
2175
|
+
buffer[p11 + channelOffset] = waveletPair.b;
|
|
2176
|
+
}
|
|
2177
|
+
}
|
|
2178
|
+
if (nx & p) {
|
|
2179
|
+
const p10 = px + oy1;
|
|
2180
|
+
if (wavelet14) {
|
|
2181
|
+
wdec14(buffer[px + channelOffset], buffer[p10 + channelOffset]);
|
|
2182
|
+
} else {
|
|
2183
|
+
wdec16(buffer[px + channelOffset], buffer[p10 + channelOffset]);
|
|
2184
|
+
}
|
|
2185
|
+
buffer[px + channelOffset] = waveletPair.a;
|
|
2186
|
+
buffer[p10 + channelOffset] = waveletPair.b;
|
|
2187
|
+
}
|
|
2188
|
+
}
|
|
2189
|
+
if (ny & p) {
|
|
2190
|
+
let px = py;
|
|
2191
|
+
const ex = py + ox * (nx - p2);
|
|
2192
|
+
for (; px <= ex; px += ox2) {
|
|
2193
|
+
const p01 = px + ox1;
|
|
2194
|
+
if (wavelet14) {
|
|
2195
|
+
wdec14(buffer[px + channelOffset], buffer[p01 + channelOffset]);
|
|
2196
|
+
} else {
|
|
2197
|
+
wdec16(buffer[px + channelOffset], buffer[p01 + channelOffset]);
|
|
2198
|
+
}
|
|
2199
|
+
buffer[px + channelOffset] = waveletPair.a;
|
|
2200
|
+
buffer[p01 + channelOffset] = waveletPair.b;
|
|
2201
|
+
}
|
|
2202
|
+
}
|
|
2203
|
+
p2 = p;
|
|
2204
|
+
p >>= 1;
|
|
2205
|
+
}
|
|
2206
|
+
}
|
|
2207
|
+
function applyLut(lut, data, count) {
|
|
2208
|
+
for (let i = 0; i < count; i++) {
|
|
2209
|
+
data[i] = lut[data[i]];
|
|
2210
|
+
}
|
|
2211
|
+
}
|
|
2212
|
+
function buildChannelLayout(part, chunkY, linesInChunk) {
|
|
2213
|
+
if (!part.dataWindow) {
|
|
2214
|
+
return { channels: [], totalWords: 0 };
|
|
2215
|
+
}
|
|
2216
|
+
const { xMin, xMax, yMin, yMax } = part.dataWindow;
|
|
2217
|
+
const chunkStart = Math.max(chunkY, yMin);
|
|
2218
|
+
const chunkEnd = Math.min(chunkY + linesInChunk - 1, yMax);
|
|
2219
|
+
const channels = [];
|
|
2220
|
+
let totalWords = 0;
|
|
2221
|
+
for (const channel of part.channels) {
|
|
2222
|
+
const xSampling = channel.xSampling > 0 ? channel.xSampling : 1;
|
|
2223
|
+
const ySampling = channel.ySampling > 0 ? channel.ySampling : 1;
|
|
2224
|
+
const sampleOriginY = firstSampleCoordinate2(yMin, ySampling);
|
|
2225
|
+
const sampledWidth = countSamplesInRange2(xMin, xMax, xSampling);
|
|
2226
|
+
const sampledRows = countSampledRowsInRange(chunkStart, chunkEnd, sampleOriginY, ySampling);
|
|
2227
|
+
const wordsPerSample = channel.pixelType === 1 ? 1 : 2;
|
|
2228
|
+
const wordsPerRow = sampledWidth * wordsPerSample;
|
|
2229
|
+
const wordCount = wordsPerRow * sampledRows;
|
|
2230
|
+
channels.push({
|
|
2231
|
+
sampledWidth,
|
|
2232
|
+
sampledRows,
|
|
2233
|
+
ySampling,
|
|
2234
|
+
sampleOriginY,
|
|
2235
|
+
wordsPerSample,
|
|
2236
|
+
wordsPerRow,
|
|
2237
|
+
wordStart: totalWords,
|
|
2238
|
+
wordEnd: totalWords + wordCount,
|
|
2239
|
+
wordCursor: totalWords
|
|
2240
|
+
});
|
|
2241
|
+
totalWords += wordCount;
|
|
2242
|
+
}
|
|
2243
|
+
return { channels, totalWords };
|
|
2244
|
+
}
|
|
2245
|
+
function decodePizChunk(context) {
|
|
2246
|
+
if (!context.part.dataWindow) {
|
|
2247
|
+
throw new Error("Part is missing dataWindow.");
|
|
2248
|
+
}
|
|
2249
|
+
const { channels, totalWords } = buildChannelLayout(
|
|
2250
|
+
context.part,
|
|
2251
|
+
context.chunkY,
|
|
2252
|
+
context.linesInChunk
|
|
2253
|
+
);
|
|
2254
|
+
const { yMin, yMax } = context.part.dataWindow;
|
|
2255
|
+
const compressedView = new DataView(context.buffer, context.dataPtr, context.dataSize);
|
|
2256
|
+
const compressedBytes = new Uint8Array(context.buffer, context.dataPtr, context.dataSize);
|
|
2257
|
+
const cursor = { value: 0 };
|
|
2258
|
+
const bitmap = new Uint8Array(BITMAP_SIZE);
|
|
2259
|
+
const minNonZero = readUint162(compressedView, cursor);
|
|
2260
|
+
const maxNonZero = readUint162(compressedView, cursor);
|
|
2261
|
+
if (maxNonZero >= BITMAP_SIZE) {
|
|
2262
|
+
throw new Error("Invalid PIZ bitmap range.");
|
|
2263
|
+
}
|
|
2264
|
+
if (minNonZero <= maxNonZero) {
|
|
2265
|
+
const bitmapLength = maxNonZero - minNonZero + 1;
|
|
2266
|
+
if (cursor.value + bitmapLength > compressedView.byteLength) {
|
|
2267
|
+
throw new Error("Truncated PIZ bitmap payload.");
|
|
2268
|
+
}
|
|
2269
|
+
bitmap.set(compressedBytes.subarray(cursor.value, cursor.value + bitmapLength), minNonZero);
|
|
2270
|
+
cursor.value += bitmapLength;
|
|
2271
|
+
}
|
|
2272
|
+
const lut = new Uint16Array(USHORT_RANGE);
|
|
2273
|
+
const maxValue = reverseLutFromBitmap(bitmap, lut);
|
|
2274
|
+
const huffmanLength = readUint322(compressedView, cursor);
|
|
2275
|
+
if (huffmanLength > compressedView.byteLength - cursor.value) {
|
|
2276
|
+
throw new Error("Invalid PIZ Huffman payload length.");
|
|
2277
|
+
}
|
|
2278
|
+
const outBuffer = new Uint16Array(totalWords);
|
|
2279
|
+
hufUncompress2(compressedBytes, compressedView, cursor, huffmanLength, outBuffer, totalWords);
|
|
2280
|
+
for (const channel of channels) {
|
|
2281
|
+
for (let component = 0; component < channel.wordsPerSample; component++) {
|
|
2282
|
+
wav2Decode(
|
|
2283
|
+
outBuffer,
|
|
2284
|
+
channel.wordStart + component,
|
|
2285
|
+
channel.sampledWidth,
|
|
2286
|
+
channel.wordsPerSample,
|
|
2287
|
+
channel.sampledRows,
|
|
2288
|
+
channel.sampledWidth * channel.wordsPerSample,
|
|
2289
|
+
maxValue
|
|
2290
|
+
);
|
|
2291
|
+
}
|
|
2292
|
+
}
|
|
2293
|
+
applyLut(lut, outBuffer, totalWords);
|
|
2294
|
+
const output = new Uint8Array(totalWords * 2);
|
|
2295
|
+
let outputOffset = 0;
|
|
2296
|
+
for (let dy = 0; dy < context.linesInChunk; dy++) {
|
|
2297
|
+
const y = context.chunkY + dy;
|
|
2298
|
+
if (y > yMax) {
|
|
2299
|
+
break;
|
|
2300
|
+
}
|
|
2301
|
+
if (y < yMin) {
|
|
2302
|
+
continue;
|
|
2303
|
+
}
|
|
2304
|
+
for (const channel of channels) {
|
|
2305
|
+
if (channel.wordsPerRow === 0 || channel.sampledRows === 0) {
|
|
2306
|
+
continue;
|
|
2307
|
+
}
|
|
2308
|
+
if (!isSampledCoordinate2(y, channel.sampleOriginY, channel.ySampling)) {
|
|
2309
|
+
continue;
|
|
2310
|
+
}
|
|
2311
|
+
if (channel.wordCursor + channel.wordsPerRow > channel.wordEnd) {
|
|
2312
|
+
throw new Error("Invalid PIZ channel plane bounds.");
|
|
2313
|
+
}
|
|
2314
|
+
for (let i = 0; i < channel.wordsPerRow; i++) {
|
|
2315
|
+
const value = outBuffer[channel.wordCursor + i];
|
|
2316
|
+
output[outputOffset++] = value & 255;
|
|
2317
|
+
output[outputOffset++] = value >>> 8 & 255;
|
|
2318
|
+
}
|
|
2319
|
+
channel.wordCursor += channel.wordsPerRow;
|
|
2320
|
+
}
|
|
2321
|
+
}
|
|
2322
|
+
if (outputOffset !== output.byteLength) {
|
|
2323
|
+
throw new Error("Invalid PIZ output layout.");
|
|
2324
|
+
}
|
|
2325
|
+
return output;
|
|
2326
|
+
}
|
|
2327
|
+
function decodePizBlock(context) {
|
|
2328
|
+
try {
|
|
2329
|
+
return decodePizChunk(context);
|
|
2330
|
+
} catch (error) {
|
|
2331
|
+
if (error instanceof ExrError) {
|
|
2332
|
+
throw error;
|
|
2333
|
+
}
|
|
2334
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
2335
|
+
throw new ExrError("DECOMPRESSION_FAILED", "Failed to decompress PIZ chunk.", {
|
|
2336
|
+
partId: context.partId,
|
|
2337
|
+
chunkIndex: context.chunkIndex,
|
|
2338
|
+
size: context.dataSize,
|
|
2339
|
+
reason
|
|
2340
|
+
});
|
|
2341
|
+
}
|
|
2342
|
+
}
|
|
2343
|
+
|
|
2344
|
+
// src/reader/compression/pxr24.ts
|
|
2345
|
+
var import_fflate2 = require("fflate");
|
|
2346
|
+
function modulo3(value, base) {
|
|
2347
|
+
const result = value % base;
|
|
2348
|
+
return result < 0 ? result + base : result;
|
|
2349
|
+
}
|
|
2350
|
+
function firstSampleCoordinate3(min, sampling) {
|
|
2351
|
+
if (sampling <= 1) return min;
|
|
2352
|
+
const remainder = modulo3(min, sampling);
|
|
2353
|
+
return remainder === 0 ? min : min + (sampling - remainder);
|
|
2354
|
+
}
|
|
2355
|
+
function countSamplesInRange3(min, max, sampling) {
|
|
2356
|
+
if (sampling <= 0 || max < min) return 0;
|
|
2357
|
+
const first = firstSampleCoordinate3(min, sampling);
|
|
2358
|
+
if (first > max) return 0;
|
|
2359
|
+
return Math.floor((max - first) / sampling) + 1;
|
|
2360
|
+
}
|
|
2361
|
+
function isSampledCoordinate3(value, firstSample, sampling) {
|
|
2362
|
+
if (sampling <= 1) return true;
|
|
2363
|
+
if (value < firstSample) return false;
|
|
2364
|
+
return (value - firstSample) % sampling === 0;
|
|
2365
|
+
}
|
|
2366
|
+
function buildRowPlan(context) {
|
|
2367
|
+
const dataWindow = context.part.dataWindow;
|
|
2368
|
+
if (!dataWindow) {
|
|
2369
|
+
throw new Error("PXR24 decode requires part dataWindow.");
|
|
2370
|
+
}
|
|
2371
|
+
const channelLayouts = context.part.channels.map((channel) => {
|
|
2372
|
+
const xSampling = channel.xSampling > 0 ? channel.xSampling : 1;
|
|
2373
|
+
const ySampling = channel.ySampling > 0 ? channel.ySampling : 1;
|
|
2374
|
+
const sampleOriginY = firstSampleCoordinate3(dataWindow.yMin, ySampling);
|
|
2375
|
+
const sampledWidth = countSamplesInRange3(dataWindow.xMin, dataWindow.xMax, xSampling);
|
|
2376
|
+
const bytesPerSample = channel.pixelType === 1 ? 2 : 4;
|
|
2377
|
+
return {
|
|
2378
|
+
pixelType: channel.pixelType,
|
|
2379
|
+
sampledWidth,
|
|
2380
|
+
sampleOriginY,
|
|
2381
|
+
ySampling,
|
|
2382
|
+
bytesPerSample
|
|
2383
|
+
};
|
|
2384
|
+
});
|
|
2385
|
+
const rows = [];
|
|
2386
|
+
let encodedSize = 0;
|
|
2387
|
+
let decodedSize = 0;
|
|
2388
|
+
for (let dy = 0; dy < context.linesInChunk; dy++) {
|
|
2389
|
+
const y = context.chunkY + dy;
|
|
2390
|
+
if (y < dataWindow.yMin || y > dataWindow.yMax) {
|
|
2391
|
+
continue;
|
|
2392
|
+
}
|
|
2393
|
+
for (const layout of channelLayouts) {
|
|
2394
|
+
if (layout.sampledWidth === 0) {
|
|
2395
|
+
continue;
|
|
2396
|
+
}
|
|
2397
|
+
if (!isSampledCoordinate3(y, layout.sampleOriginY, layout.ySampling)) {
|
|
2398
|
+
continue;
|
|
2399
|
+
}
|
|
2400
|
+
rows.push({
|
|
2401
|
+
pixelType: layout.pixelType,
|
|
2402
|
+
sampledWidth: layout.sampledWidth,
|
|
2403
|
+
bytesPerSample: layout.bytesPerSample
|
|
2404
|
+
});
|
|
2405
|
+
const encodedBytesPerSample = layout.pixelType === 2 ? 3 : layout.bytesPerSample;
|
|
2406
|
+
encodedSize += layout.sampledWidth * encodedBytesPerSample;
|
|
2407
|
+
decodedSize += layout.sampledWidth * layout.bytesPerSample;
|
|
2408
|
+
}
|
|
2409
|
+
}
|
|
2410
|
+
return { rows, encodedSize, decodedSize };
|
|
2411
|
+
}
|
|
2412
|
+
function decodePxr24Block(context) {
|
|
2413
|
+
try {
|
|
2414
|
+
const compressed = new Uint8Array(context.buffer, context.dataPtr, context.dataSize);
|
|
2415
|
+
const unpacked = (0, import_fflate2.unzlibSync)(compressed);
|
|
2416
|
+
const plan = buildRowPlan(context);
|
|
2417
|
+
if (unpacked.byteLength !== plan.encodedSize) {
|
|
2418
|
+
throw new Error("Invalid PXR24 intermediate payload length.");
|
|
2419
|
+
}
|
|
2420
|
+
if (plan.decodedSize !== context.expectedUncompressedSize) {
|
|
2421
|
+
throw new Error("Invalid PXR24 expected output length.");
|
|
2422
|
+
}
|
|
2423
|
+
const out = new Uint8Array(plan.decodedSize);
|
|
2424
|
+
let unpackedPtr = 0;
|
|
2425
|
+
let outPtr = 0;
|
|
2426
|
+
for (const row of plan.rows) {
|
|
2427
|
+
const width = row.sampledWidth;
|
|
2428
|
+
if (row.pixelType === 1) {
|
|
2429
|
+
const needed = width * 2;
|
|
2430
|
+
if (unpackedPtr + needed > unpacked.length) {
|
|
2431
|
+
throw new Error("Truncated PXR24 HALF payload.");
|
|
2432
|
+
}
|
|
2433
|
+
let p0 = unpackedPtr;
|
|
2434
|
+
let p1 = p0 + width;
|
|
2435
|
+
unpackedPtr += needed;
|
|
2436
|
+
let pixel = 0;
|
|
2437
|
+
for (let x = 0; x < width; x++) {
|
|
2438
|
+
const diff = (unpacked[p0++] << 8 | unpacked[p1++]) >>> 0;
|
|
2439
|
+
pixel = pixel + diff >>> 0;
|
|
2440
|
+
out[outPtr++] = pixel & 255;
|
|
2441
|
+
out[outPtr++] = pixel >>> 8 & 255;
|
|
2442
|
+
}
|
|
2443
|
+
continue;
|
|
2444
|
+
}
|
|
2445
|
+
if (row.pixelType === 0) {
|
|
2446
|
+
const needed = width * 4;
|
|
2447
|
+
if (unpackedPtr + needed > unpacked.length) {
|
|
2448
|
+
throw new Error("Truncated PXR24 UINT payload.");
|
|
2449
|
+
}
|
|
2450
|
+
let p0 = unpackedPtr;
|
|
2451
|
+
let p1 = p0 + width;
|
|
2452
|
+
let p2 = p1 + width;
|
|
2453
|
+
let p3 = p2 + width;
|
|
2454
|
+
unpackedPtr += needed;
|
|
2455
|
+
let pixel = 0;
|
|
2456
|
+
for (let x = 0; x < width; x++) {
|
|
2457
|
+
const diff = (unpacked[p0++] << 24 >>> 0 | unpacked[p1++] << 16 >>> 0 | unpacked[p2++] << 8 >>> 0 | unpacked[p3++] >>> 0) >>> 0;
|
|
2458
|
+
pixel = pixel + diff >>> 0;
|
|
2459
|
+
out[outPtr++] = pixel & 255;
|
|
2460
|
+
out[outPtr++] = pixel >>> 8 & 255;
|
|
2461
|
+
out[outPtr++] = pixel >>> 16 & 255;
|
|
2462
|
+
out[outPtr++] = pixel >>> 24 & 255;
|
|
2463
|
+
}
|
|
2464
|
+
continue;
|
|
2465
|
+
}
|
|
2466
|
+
if (row.pixelType === 2) {
|
|
2467
|
+
const needed = width * 3;
|
|
2468
|
+
if (unpackedPtr + needed > unpacked.length) {
|
|
2469
|
+
throw new Error("Truncated PXR24 FLOAT payload.");
|
|
2470
|
+
}
|
|
2471
|
+
let p0 = unpackedPtr;
|
|
2472
|
+
let p1 = p0 + width;
|
|
2473
|
+
let p2 = p1 + width;
|
|
2474
|
+
unpackedPtr += needed;
|
|
2475
|
+
let pixel = 0;
|
|
2476
|
+
for (let x = 0; x < width; x++) {
|
|
2477
|
+
const diff = (unpacked[p0++] << 24 >>> 0 | unpacked[p1++] << 16 >>> 0 | unpacked[p2++] << 8 >>> 0) >>> 0;
|
|
2478
|
+
pixel = pixel + diff >>> 0;
|
|
2479
|
+
out[outPtr++] = pixel & 255;
|
|
2480
|
+
out[outPtr++] = pixel >>> 8 & 255;
|
|
2481
|
+
out[outPtr++] = pixel >>> 16 & 255;
|
|
2482
|
+
out[outPtr++] = pixel >>> 24 & 255;
|
|
2483
|
+
}
|
|
2484
|
+
continue;
|
|
2485
|
+
}
|
|
2486
|
+
throw new Error(`Unsupported PXR24 pixel type ${row.pixelType}.`);
|
|
2487
|
+
}
|
|
2488
|
+
if (unpackedPtr !== unpacked.length || outPtr !== out.length) {
|
|
2489
|
+
throw new Error("PXR24 payload decode bounds mismatch.");
|
|
2490
|
+
}
|
|
2491
|
+
return out;
|
|
2492
|
+
} catch {
|
|
2493
|
+
throw new ExrError("DECOMPRESSION_FAILED", "Failed to decompress PXR24 chunk.", {
|
|
2494
|
+
partId: context.partId,
|
|
2495
|
+
chunkIndex: context.chunkIndex,
|
|
2496
|
+
size: context.dataSize,
|
|
2497
|
+
expected: context.expectedUncompressedSize
|
|
2498
|
+
});
|
|
2499
|
+
}
|
|
2500
|
+
}
|
|
2501
|
+
|
|
2502
|
+
// src/reader/compression/rle.ts
|
|
2503
|
+
function undoPredictor(data) {
|
|
2504
|
+
for (let i = 1; i < data.length; i++) {
|
|
2505
|
+
data[i] = data[i - 1] + data[i] - 128 & 255;
|
|
2506
|
+
}
|
|
2507
|
+
}
|
|
2508
|
+
function undoInterleave(data) {
|
|
2509
|
+
const length = data.length;
|
|
2510
|
+
const output = new Uint8Array(length);
|
|
2511
|
+
const half = Math.floor((length + 1) / 2);
|
|
2512
|
+
const first = data.subarray(0, half);
|
|
2513
|
+
const second = data.subarray(half);
|
|
2514
|
+
for (let i = 0; i < half; i++) {
|
|
2515
|
+
output[i * 2] = first[i];
|
|
2516
|
+
if (i * 2 + 1 < length) {
|
|
2517
|
+
output[i * 2 + 1] = second[i];
|
|
2518
|
+
}
|
|
2519
|
+
}
|
|
2520
|
+
return output;
|
|
2521
|
+
}
|
|
2522
|
+
function decodeRunLength2(input, expectedSize) {
|
|
2523
|
+
if (expectedSize < 0) {
|
|
2524
|
+
throw new Error("Invalid expected RLE output size.");
|
|
2525
|
+
}
|
|
2526
|
+
const output = new Uint8Array(expectedSize);
|
|
2527
|
+
let sourcePtr = 0;
|
|
2528
|
+
let outputPtr = 0;
|
|
2529
|
+
while (sourcePtr < input.length) {
|
|
2530
|
+
const length = input[sourcePtr] << 24 >> 24;
|
|
2531
|
+
sourcePtr += 1;
|
|
2532
|
+
if (length < 0) {
|
|
2533
|
+
const count2 = -length;
|
|
2534
|
+
if (sourcePtr + count2 > input.length) {
|
|
2535
|
+
throw new Error("Truncated RLE literal run.");
|
|
2536
|
+
}
|
|
2537
|
+
if (outputPtr + count2 > output.length) {
|
|
2538
|
+
throw new Error("RLE literal run exceeded expected output size.");
|
|
2539
|
+
}
|
|
2540
|
+
output.set(input.subarray(sourcePtr, sourcePtr + count2), outputPtr);
|
|
2541
|
+
sourcePtr += count2;
|
|
2542
|
+
outputPtr += count2;
|
|
2543
|
+
continue;
|
|
2544
|
+
}
|
|
2545
|
+
const count = length + 1;
|
|
2546
|
+
if (sourcePtr >= input.length) {
|
|
2547
|
+
throw new Error("Truncated RLE repeat run.");
|
|
2548
|
+
}
|
|
2549
|
+
if (outputPtr + count > output.length) {
|
|
2550
|
+
throw new Error("RLE repeat run exceeded expected output size.");
|
|
2551
|
+
}
|
|
2552
|
+
output.fill(input[sourcePtr], outputPtr, outputPtr + count);
|
|
2553
|
+
sourcePtr += 1;
|
|
2554
|
+
outputPtr += count;
|
|
2555
|
+
}
|
|
2556
|
+
if (outputPtr !== output.length) {
|
|
2557
|
+
throw new Error("RLE output size mismatch.");
|
|
2558
|
+
}
|
|
2559
|
+
return output;
|
|
2560
|
+
}
|
|
2561
|
+
function decodeRleBlock(context) {
|
|
2562
|
+
try {
|
|
2563
|
+
const compressed = new Uint8Array(context.buffer, context.dataPtr, context.dataSize);
|
|
2564
|
+
const unpacked = decodeRunLength2(compressed, context.expectedUncompressedSize);
|
|
2565
|
+
undoPredictor(unpacked);
|
|
2566
|
+
return undoInterleave(unpacked);
|
|
2567
|
+
} catch {
|
|
2568
|
+
throw new ExrError("DECOMPRESSION_FAILED", "Failed to decompress RLE chunk.", {
|
|
2569
|
+
partId: context.partId,
|
|
2570
|
+
chunkIndex: context.chunkIndex,
|
|
2571
|
+
size: context.dataSize,
|
|
2572
|
+
expected: context.expectedUncompressedSize
|
|
2573
|
+
});
|
|
2574
|
+
}
|
|
2575
|
+
}
|
|
2576
|
+
|
|
2577
|
+
// src/reader/compression/scanline.ts
|
|
2578
|
+
function getScanlineLinesPerBlock(compression) {
|
|
2579
|
+
switch (compression) {
|
|
2580
|
+
case 0:
|
|
2581
|
+
case 1:
|
|
2582
|
+
case 2:
|
|
2583
|
+
return 1;
|
|
2584
|
+
case 3:
|
|
2585
|
+
case 5:
|
|
2586
|
+
return 16;
|
|
2587
|
+
case 4:
|
|
2588
|
+
case 6:
|
|
2589
|
+
case 7:
|
|
2590
|
+
case 8:
|
|
2591
|
+
return 32;
|
|
2592
|
+
case 9:
|
|
2593
|
+
return 256;
|
|
2594
|
+
default:
|
|
2595
|
+
return 1;
|
|
2596
|
+
}
|
|
2597
|
+
}
|
|
2598
|
+
|
|
2599
|
+
// src/reader/compression/zip.ts
|
|
2600
|
+
var import_fflate3 = require("fflate");
|
|
2601
|
+
function undoZipPredictorAndInterleave2(data, output) {
|
|
2602
|
+
const length = data.length;
|
|
2603
|
+
if (length === 0) return data;
|
|
2604
|
+
const half = length + 1 >> 1;
|
|
2605
|
+
let predicted = data[0];
|
|
2606
|
+
output[0] = predicted;
|
|
2607
|
+
for (let i = 1; i < half; i++) {
|
|
2608
|
+
predicted = predicted + data[i] - 128 & 255;
|
|
2609
|
+
output[i << 1] = predicted;
|
|
2610
|
+
}
|
|
2611
|
+
for (let i = half; i < length; i++) {
|
|
2612
|
+
predicted = predicted + data[i] - 128 & 255;
|
|
2613
|
+
output[(i - half << 1) + 1] = predicted;
|
|
2614
|
+
}
|
|
2615
|
+
return output;
|
|
2616
|
+
}
|
|
2617
|
+
function decodeZipStyleBlock(context) {
|
|
2618
|
+
try {
|
|
2619
|
+
const compressed = new Uint8Array(context.buffer, context.dataPtr, context.dataSize);
|
|
2620
|
+
let raw;
|
|
2621
|
+
if (context.expectedUncompressedSize > 0 && context.zipScratch) {
|
|
2622
|
+
if (context.zipScratch.inflate.byteLength < context.expectedUncompressedSize) {
|
|
2623
|
+
context.zipScratch.inflate = new Uint8Array(context.expectedUncompressedSize);
|
|
2624
|
+
}
|
|
2625
|
+
const inflateOut = context.zipScratch.inflate.subarray(0, context.expectedUncompressedSize);
|
|
2626
|
+
raw = (0, import_fflate3.unzlibSync)(compressed, { out: inflateOut });
|
|
2627
|
+
} else if (context.expectedUncompressedSize > 0) {
|
|
2628
|
+
raw = (0, import_fflate3.unzlibSync)(compressed, { out: new Uint8Array(context.expectedUncompressedSize) });
|
|
2629
|
+
} else {
|
|
2630
|
+
raw = (0, import_fflate3.unzlibSync)(compressed);
|
|
2631
|
+
}
|
|
2632
|
+
if (context.zipScratch) {
|
|
2633
|
+
if (context.zipScratch.output.byteLength < raw.byteLength) {
|
|
2634
|
+
context.zipScratch.output = new Uint8Array(raw.byteLength);
|
|
2635
|
+
}
|
|
2636
|
+
const output = context.zipScratch.output.subarray(0, raw.byteLength);
|
|
2637
|
+
return undoZipPredictorAndInterleave2(raw, output);
|
|
2638
|
+
}
|
|
2639
|
+
return undoZipPredictorAndInterleave2(raw, new Uint8Array(raw.byteLength));
|
|
2640
|
+
} catch {
|
|
2641
|
+
throw new ExrError("DECOMPRESSION_FAILED", "Failed to decompress ZIP/ZIPS chunk.", {
|
|
2642
|
+
partId: context.partId,
|
|
2643
|
+
chunkIndex: context.chunkIndex,
|
|
2644
|
+
size: context.dataSize
|
|
2645
|
+
});
|
|
2646
|
+
}
|
|
2647
|
+
}
|
|
2648
|
+
|
|
2649
|
+
// src/reader/compression/handlers.ts
|
|
2650
|
+
var READ_COMPRESSION_HANDLERS = /* @__PURE__ */ new Map([
|
|
2651
|
+
[
|
|
2652
|
+
0,
|
|
2653
|
+
{
|
|
2654
|
+
compressionId: 0,
|
|
2655
|
+
name: COMPRESSION_NAMES[0],
|
|
2656
|
+
linesPerBlock: getScanlineLinesPerBlock(0),
|
|
2657
|
+
decodeBlock: ({ buffer, dataPtr, dataSize }) => new Uint8Array(buffer, dataPtr, dataSize)
|
|
2658
|
+
}
|
|
2659
|
+
],
|
|
2660
|
+
[
|
|
2661
|
+
1,
|
|
2662
|
+
{
|
|
2663
|
+
compressionId: 1,
|
|
2664
|
+
name: COMPRESSION_NAMES[1],
|
|
2665
|
+
linesPerBlock: getScanlineLinesPerBlock(1),
|
|
2666
|
+
decodeBlock: decodeRleBlock
|
|
2667
|
+
}
|
|
2668
|
+
],
|
|
2669
|
+
[
|
|
2670
|
+
2,
|
|
2671
|
+
{
|
|
2672
|
+
compressionId: 2,
|
|
2673
|
+
name: COMPRESSION_NAMES[2],
|
|
2674
|
+
linesPerBlock: getScanlineLinesPerBlock(2),
|
|
2675
|
+
decodeBlock: decodeZipStyleBlock
|
|
2676
|
+
}
|
|
2677
|
+
],
|
|
2678
|
+
[
|
|
2679
|
+
3,
|
|
2680
|
+
{
|
|
2681
|
+
compressionId: 3,
|
|
2682
|
+
name: COMPRESSION_NAMES[3],
|
|
2683
|
+
linesPerBlock: getScanlineLinesPerBlock(3),
|
|
2684
|
+
decodeBlock: decodeZipStyleBlock
|
|
2685
|
+
}
|
|
2686
|
+
],
|
|
2687
|
+
[
|
|
2688
|
+
4,
|
|
2689
|
+
{
|
|
2690
|
+
compressionId: 4,
|
|
2691
|
+
name: COMPRESSION_NAMES[4],
|
|
2692
|
+
linesPerBlock: getScanlineLinesPerBlock(4),
|
|
2693
|
+
decodeBlock: decodePizBlock
|
|
2694
|
+
}
|
|
2695
|
+
],
|
|
2696
|
+
[
|
|
2697
|
+
5,
|
|
2698
|
+
{
|
|
2699
|
+
compressionId: 5,
|
|
2700
|
+
name: COMPRESSION_NAMES[5],
|
|
2701
|
+
linesPerBlock: getScanlineLinesPerBlock(5),
|
|
2702
|
+
decodeBlock: decodePxr24Block
|
|
2703
|
+
}
|
|
2704
|
+
],
|
|
2705
|
+
[
|
|
2706
|
+
6,
|
|
2707
|
+
{
|
|
2708
|
+
compressionId: 6,
|
|
2709
|
+
name: COMPRESSION_NAMES[6],
|
|
2710
|
+
linesPerBlock: getScanlineLinesPerBlock(6),
|
|
2711
|
+
decodeBlock: decodeB44Block
|
|
2712
|
+
}
|
|
2713
|
+
],
|
|
2714
|
+
[
|
|
2715
|
+
7,
|
|
2716
|
+
{
|
|
2717
|
+
compressionId: 7,
|
|
2718
|
+
name: COMPRESSION_NAMES[7],
|
|
2719
|
+
linesPerBlock: getScanlineLinesPerBlock(7),
|
|
2720
|
+
decodeBlock: decodeB44Block
|
|
2721
|
+
}
|
|
2722
|
+
],
|
|
2723
|
+
[
|
|
2724
|
+
8,
|
|
2725
|
+
{
|
|
2726
|
+
compressionId: 8,
|
|
2727
|
+
name: COMPRESSION_NAMES[8],
|
|
2728
|
+
linesPerBlock: getScanlineLinesPerBlock(8),
|
|
2729
|
+
decodeBlock: decodeDwaBlock
|
|
2730
|
+
}
|
|
2731
|
+
],
|
|
2732
|
+
[
|
|
2733
|
+
9,
|
|
2734
|
+
{
|
|
2735
|
+
compressionId: 9,
|
|
2736
|
+
name: COMPRESSION_NAMES[9],
|
|
2737
|
+
linesPerBlock: getScanlineLinesPerBlock(9),
|
|
2738
|
+
decodeBlock: decodeDwaBlock
|
|
2739
|
+
}
|
|
2740
|
+
]
|
|
2741
|
+
]);
|
|
2742
|
+
function getReadCompressionHandler(compression) {
|
|
2743
|
+
return READ_COMPRESSION_HANDLERS.get(compression);
|
|
2744
|
+
}
|
|
2745
|
+
function getSupportedReadCompressionText() {
|
|
2746
|
+
return Array.from(READ_COMPRESSION_HANDLERS.values()).map((handler) => handler.name).join(", ");
|
|
2747
|
+
}
|
|
2748
|
+
|
|
2749
|
+
// src/reader/decodeExrPart.ts
|
|
2750
|
+
var UINT32_MAX = 4294967295;
|
|
2751
|
+
var INV_UINT32_MAX = 1 / UINT32_MAX;
|
|
2752
|
+
function nowMs2() {
|
|
2753
|
+
return typeof performance !== "undefined" ? performance.now() : Date.now();
|
|
2754
|
+
}
|
|
2755
|
+
function emit2(onEvent, event) {
|
|
2756
|
+
onEvent?.(event);
|
|
2757
|
+
}
|
|
2758
|
+
function modulo4(value, base) {
|
|
2759
|
+
const result = value % base;
|
|
2760
|
+
return result < 0 ? result + base : result;
|
|
2761
|
+
}
|
|
2762
|
+
function firstSampleCoordinate4(min, sampling) {
|
|
2763
|
+
if (sampling <= 1) return min;
|
|
2764
|
+
const remainder = modulo4(min, sampling);
|
|
2765
|
+
return remainder === 0 ? min : min + (sampling - remainder);
|
|
2766
|
+
}
|
|
2767
|
+
function countSamplesInRange4(min, max, sampling) {
|
|
2768
|
+
if (sampling <= 0 || max < min) return 0;
|
|
2769
|
+
const first = firstSampleCoordinate4(min, sampling);
|
|
2770
|
+
if (first > max) return 0;
|
|
2771
|
+
return Math.floor((max - first) / sampling) + 1;
|
|
2772
|
+
}
|
|
2773
|
+
function isSampledCoordinate4(value, firstSample, sampling) {
|
|
2774
|
+
if (sampling <= 1) return true;
|
|
2775
|
+
if (value < firstSample) return false;
|
|
2776
|
+
return (value - firstSample) % sampling === 0;
|
|
2777
|
+
}
|
|
2778
|
+
function toNumberOffset(low, high) {
|
|
2779
|
+
if (high <= 2097151) {
|
|
2780
|
+
return high * 4294967296 + low;
|
|
2781
|
+
}
|
|
2782
|
+
const combined = BigInt(low) + (BigInt(high) << 32n);
|
|
2783
|
+
if (combined > BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
2784
|
+
throw new ExrError("MALFORMED_OFFSET_TABLE", "Chunk offset exceeds safe integer range.", {
|
|
2785
|
+
offset: combined.toString()
|
|
2786
|
+
});
|
|
2787
|
+
}
|
|
2788
|
+
return Number(combined);
|
|
2789
|
+
}
|
|
2790
|
+
function buildChannelMeta(part) {
|
|
2791
|
+
if (!part.dataWindow) return [];
|
|
2792
|
+
const { xMin, xMax, yMin, yMax } = part.dataWindow;
|
|
2793
|
+
return part.channels.map((channel) => {
|
|
2794
|
+
const xSampling = channel.xSampling > 0 ? channel.xSampling : 1;
|
|
2795
|
+
const ySampling = channel.ySampling > 0 ? channel.ySampling : 1;
|
|
2796
|
+
const sampleOriginX = firstSampleCoordinate4(xMin, xSampling);
|
|
2797
|
+
const sampleOriginY = firstSampleCoordinate4(yMin, ySampling);
|
|
2798
|
+
const sampledWidth = countSamplesInRange4(xMin, xMax, xSampling);
|
|
2799
|
+
const sampledHeight = countSamplesInRange4(yMin, yMax, ySampling);
|
|
2800
|
+
const bytesPerSample = channel.pixelType === 1 ? 2 : 4;
|
|
2801
|
+
return {
|
|
2802
|
+
channel,
|
|
2803
|
+
pixelType: channel.pixelType,
|
|
2804
|
+
ySampling,
|
|
2805
|
+
bytesPerSample,
|
|
2806
|
+
rowByteLength: sampledWidth * bytesPerSample,
|
|
2807
|
+
sampledWidth,
|
|
2808
|
+
sampledHeight,
|
|
2809
|
+
sampleOriginX,
|
|
2810
|
+
sampleOriginY,
|
|
2811
|
+
data: new Float32Array(sampledWidth * sampledHeight)
|
|
2812
|
+
};
|
|
2813
|
+
});
|
|
2814
|
+
}
|
|
2815
|
+
function getExpectedUncompressedChunkSize(part, channelMeta, chunkY, linesInChunk) {
|
|
2816
|
+
if (!part.dataWindow) return 0;
|
|
2817
|
+
let expected = 0;
|
|
2818
|
+
for (let dy = 0; dy < linesInChunk; dy++) {
|
|
2819
|
+
const y = chunkY + dy;
|
|
2820
|
+
if (y < part.dataWindow.yMin) {
|
|
2821
|
+
continue;
|
|
2822
|
+
}
|
|
2823
|
+
for (const meta of channelMeta) {
|
|
2824
|
+
if (meta.sampledWidth === 0 || meta.sampledHeight === 0) {
|
|
2825
|
+
continue;
|
|
2826
|
+
}
|
|
2827
|
+
if (!isSampledCoordinate4(y, meta.sampleOriginY, meta.ySampling)) {
|
|
2828
|
+
continue;
|
|
2829
|
+
}
|
|
2830
|
+
expected += meta.rowByteLength;
|
|
2831
|
+
}
|
|
2832
|
+
}
|
|
2833
|
+
return expected;
|
|
2834
|
+
}
|
|
2835
|
+
function decodeRowIntoChannel(meta, blockData, blockPointer, rowOffset, blockView) {
|
|
2836
|
+
const destination = meta.data;
|
|
2837
|
+
const sampleCount = meta.sampledWidth;
|
|
2838
|
+
const sourceByteOffset = blockData.byteOffset + blockPointer;
|
|
2839
|
+
if (meta.pixelType === 1) {
|
|
2840
|
+
if ((sourceByteOffset & 1) === 0) {
|
|
2841
|
+
const source = new Uint16Array(blockData.buffer, sourceByteOffset, sampleCount);
|
|
2842
|
+
for (let i = 0; i < sampleCount; i++) {
|
|
2843
|
+
destination[rowOffset + i] = float16ToFloat32(source[i]);
|
|
2844
|
+
}
|
|
2845
|
+
return;
|
|
2846
|
+
}
|
|
2847
|
+
for (let i = 0; i < sampleCount; i++) {
|
|
2848
|
+
destination[rowOffset + i] = float16ToFloat32(
|
|
2849
|
+
blockView.getUint16(blockPointer + (i << 1), true)
|
|
2850
|
+
);
|
|
2851
|
+
}
|
|
2852
|
+
return;
|
|
2853
|
+
}
|
|
2854
|
+
if (meta.pixelType === 2) {
|
|
2855
|
+
if ((sourceByteOffset & 3) === 0) {
|
|
2856
|
+
const source = new Float32Array(blockData.buffer, sourceByteOffset, sampleCount);
|
|
2857
|
+
destination.set(source, rowOffset);
|
|
2858
|
+
return;
|
|
2859
|
+
}
|
|
2860
|
+
for (let i = 0; i < sampleCount; i++) {
|
|
2861
|
+
destination[rowOffset + i] = blockView.getFloat32(blockPointer + (i << 2), true);
|
|
2862
|
+
}
|
|
2863
|
+
return;
|
|
2864
|
+
}
|
|
2865
|
+
if ((sourceByteOffset & 3) === 0) {
|
|
2866
|
+
const source = new Uint32Array(blockData.buffer, sourceByteOffset, sampleCount);
|
|
2867
|
+
for (let i = 0; i < sampleCount; i++) {
|
|
2868
|
+
destination[rowOffset + i] = source[i] * INV_UINT32_MAX;
|
|
2869
|
+
}
|
|
2870
|
+
return;
|
|
2871
|
+
}
|
|
2872
|
+
for (let i = 0; i < sampleCount; i++) {
|
|
2873
|
+
destination[rowOffset + i] = blockView.getUint32(blockPointer + (i << 2), true) * INV_UINT32_MAX;
|
|
2874
|
+
}
|
|
2875
|
+
}
|
|
2876
|
+
function decodeExrPart(bufferInput, structure, options) {
|
|
2877
|
+
const buffer = toArrayBuffer(bufferInput);
|
|
2878
|
+
const onEvent = options.onEvent;
|
|
2879
|
+
const t0 = nowMs2();
|
|
2880
|
+
const partIndex = structure.parts.findIndex((part2) => part2.id === options.partId);
|
|
2881
|
+
if (partIndex < 0) {
|
|
2882
|
+
throw new ExrError("PART_NOT_FOUND", `Part ${options.partId} was not found in EXR structure.`, {
|
|
2883
|
+
partId: options.partId
|
|
2884
|
+
});
|
|
2885
|
+
}
|
|
2886
|
+
const part = structure.parts[partIndex];
|
|
2887
|
+
if (part.type === "tiledimage") {
|
|
2888
|
+
throw new ExrError("UNSUPPORTED_PART_TYPE", "Tiled EXR parts are not supported.", {
|
|
2889
|
+
partId: options.partId,
|
|
2890
|
+
type: part.type
|
|
2891
|
+
});
|
|
2892
|
+
}
|
|
2893
|
+
const compression = part.compression ?? 0;
|
|
2894
|
+
const compressionHandler = getReadCompressionHandler(compression);
|
|
2895
|
+
if (!compressionHandler) {
|
|
2896
|
+
throw new ExrError(
|
|
2897
|
+
"UNSUPPORTED_COMPRESSION",
|
|
2898
|
+
`Unsupported compression. Supported: ${getSupportedReadCompressionText()}.`,
|
|
2899
|
+
{
|
|
2900
|
+
partId: options.partId,
|
|
2901
|
+
compression
|
|
2902
|
+
}
|
|
2903
|
+
);
|
|
2904
|
+
}
|
|
2905
|
+
if (!part.dataWindow) {
|
|
2906
|
+
throw new ExrError("MISSING_DATA_WINDOW", "EXR part is missing required dataWindow.", {
|
|
2907
|
+
partId: options.partId
|
|
2908
|
+
});
|
|
2909
|
+
}
|
|
2910
|
+
const view = new DataView(buffer);
|
|
2911
|
+
const width = part.dataWindow.xMax - part.dataWindow.xMin + 1;
|
|
2912
|
+
const height = part.dataWindow.yMax - part.dataWindow.yMin + 1;
|
|
2913
|
+
const linesPerBlock = compressionHandler.linesPerBlock;
|
|
2914
|
+
const chunkCount = Math.ceil(height / linesPerBlock);
|
|
2915
|
+
emit2(onEvent, {
|
|
2916
|
+
phase: "decode",
|
|
2917
|
+
level: "info",
|
|
2918
|
+
code: "decode.setup",
|
|
2919
|
+
message: `Decoder setup for part ${part.id}.`,
|
|
2920
|
+
metrics: {
|
|
2921
|
+
partId: part.id,
|
|
2922
|
+
width,
|
|
2923
|
+
height,
|
|
2924
|
+
channels: part.channels.length,
|
|
2925
|
+
compression: compressionHandler.name || COMPRESSION_NAMES[compression] || "UNKNOWN",
|
|
2926
|
+
chunks: chunkCount
|
|
2927
|
+
}
|
|
2928
|
+
});
|
|
2929
|
+
let offsetTablePtr = structure.headerEndOffset;
|
|
2930
|
+
for (let i = 0; i < partIndex; i++) {
|
|
2931
|
+
const prior = structure.parts[i];
|
|
2932
|
+
if (!prior.dataWindow) continue;
|
|
2933
|
+
const priorHeight = prior.dataWindow.yMax - prior.dataWindow.yMin + 1;
|
|
2934
|
+
const priorChunks = Math.ceil(priorHeight / getScanlineLinesPerBlock(prior.compression ?? 0));
|
|
2935
|
+
offsetTablePtr += priorChunks * 8;
|
|
2936
|
+
}
|
|
2937
|
+
if (offsetTablePtr < 0 || offsetTablePtr + chunkCount * 8 > view.byteLength) {
|
|
2938
|
+
throw new ExrError("MALFORMED_OFFSET_TABLE", "Offset table is truncated or invalid.", {
|
|
2939
|
+
partId: options.partId,
|
|
2940
|
+
offsetTablePtr,
|
|
2941
|
+
chunkCount,
|
|
2942
|
+
size: view.byteLength
|
|
2943
|
+
});
|
|
2944
|
+
}
|
|
2945
|
+
const tOffsets = nowMs2();
|
|
2946
|
+
const chunkOffsets = new Array(chunkCount);
|
|
2947
|
+
for (let i = 0; i < chunkCount; i++) {
|
|
2948
|
+
const low = view.getUint32(offsetTablePtr + i * 8, true);
|
|
2949
|
+
const high = view.getUint32(offsetTablePtr + i * 8 + 4, true);
|
|
2950
|
+
chunkOffsets[i] = toNumberOffset(low, high);
|
|
2951
|
+
}
|
|
2952
|
+
emit2(onEvent, {
|
|
2953
|
+
phase: "decode",
|
|
2954
|
+
level: "info",
|
|
2955
|
+
code: "decode.offsets.read",
|
|
2956
|
+
message: "Read chunk offsets.",
|
|
2957
|
+
metrics: {
|
|
2958
|
+
partId: part.id,
|
|
2959
|
+
count: chunkOffsets.length,
|
|
2960
|
+
ms: (nowMs2() - tOffsets).toFixed(3)
|
|
2961
|
+
}
|
|
2962
|
+
});
|
|
2963
|
+
const channelMeta = buildChannelMeta(part);
|
|
2964
|
+
const zipScratch = {
|
|
2965
|
+
inflate: new Uint8Array(0),
|
|
2966
|
+
output: new Uint8Array(0)
|
|
2967
|
+
};
|
|
2968
|
+
const tDecode = nowMs2();
|
|
2969
|
+
let bytesRead = 0;
|
|
2970
|
+
for (let chunkIndex = 0; chunkIndex < chunkOffsets.length; chunkIndex++) {
|
|
2971
|
+
const chunkOffset = chunkOffsets[chunkIndex];
|
|
2972
|
+
const chunkHeaderSize = structure.isMultipart ? 12 : 8;
|
|
2973
|
+
if (chunkOffset < 0 || chunkOffset + chunkHeaderSize > view.byteLength) {
|
|
2974
|
+
throw new ExrError("MALFORMED_CHUNK", "Chunk header is outside file bounds.", {
|
|
2975
|
+
partId: part.id,
|
|
2976
|
+
chunkIndex,
|
|
2977
|
+
chunkOffset
|
|
2978
|
+
});
|
|
2979
|
+
}
|
|
2980
|
+
let chunkY = 0;
|
|
2981
|
+
let dataSize = 0;
|
|
2982
|
+
let dataPtr = chunkOffset;
|
|
2983
|
+
if (structure.isMultipart) {
|
|
2984
|
+
const partNumber = view.getInt32(chunkOffset, true);
|
|
2985
|
+
chunkY = view.getInt32(chunkOffset + 4, true);
|
|
2986
|
+
dataSize = view.getInt32(chunkOffset + 8, true);
|
|
2987
|
+
dataPtr = chunkOffset + 12;
|
|
2988
|
+
if (partNumber !== part.id) {
|
|
2989
|
+
emit2(onEvent, {
|
|
2990
|
+
phase: "decode",
|
|
2991
|
+
level: "warn",
|
|
2992
|
+
code: "decode.chunk.part_mismatch",
|
|
2993
|
+
message: "Skipped chunk whose part number did not match requested part.",
|
|
2994
|
+
metrics: {
|
|
2995
|
+
chunkIndex,
|
|
2996
|
+
expectedPart: part.id,
|
|
2997
|
+
actualPart: partNumber
|
|
2998
|
+
}
|
|
2999
|
+
});
|
|
3000
|
+
continue;
|
|
3001
|
+
}
|
|
3002
|
+
} else {
|
|
3003
|
+
chunkY = view.getInt32(chunkOffset, true);
|
|
3004
|
+
dataSize = view.getInt32(chunkOffset + 4, true);
|
|
3005
|
+
dataPtr = chunkOffset + 8;
|
|
3006
|
+
}
|
|
3007
|
+
if (dataSize < 0 || dataPtr < 0 || dataPtr + dataSize > view.byteLength) {
|
|
3008
|
+
throw new ExrError("MALFORMED_CHUNK", "Chunk payload is outside file bounds.", {
|
|
3009
|
+
partId: part.id,
|
|
3010
|
+
chunkIndex,
|
|
3011
|
+
chunkY,
|
|
3012
|
+
dataPtr,
|
|
3013
|
+
dataSize
|
|
3014
|
+
});
|
|
3015
|
+
}
|
|
3016
|
+
const linesInChunk = Math.max(0, Math.min(linesPerBlock, part.dataWindow.yMax - chunkY + 1));
|
|
3017
|
+
const expectedUncompressedSize = getExpectedUncompressedChunkSize(
|
|
3018
|
+
part,
|
|
3019
|
+
channelMeta,
|
|
3020
|
+
chunkY,
|
|
3021
|
+
linesInChunk
|
|
3022
|
+
);
|
|
3023
|
+
const predecodedZipBlock = options.predecodedZipBlocks?.get(chunkIndex);
|
|
3024
|
+
const predecodedDwaBlock = options.predecodedDwaBlocks?.get(chunkIndex);
|
|
3025
|
+
const predecodedBlock = predecodedZipBlock ?? predecodedDwaBlock;
|
|
3026
|
+
const blockData = predecodedBlock ? predecodedBlock : dataSize === expectedUncompressedSize ? new Uint8Array(buffer, dataPtr, dataSize) : compressionHandler.decodeBlock({
|
|
3027
|
+
buffer,
|
|
3028
|
+
dataPtr,
|
|
3029
|
+
dataSize,
|
|
3030
|
+
expectedUncompressedSize,
|
|
3031
|
+
part,
|
|
3032
|
+
partId: part.id,
|
|
3033
|
+
chunkIndex,
|
|
3034
|
+
chunkY,
|
|
3035
|
+
linesInChunk,
|
|
3036
|
+
zipScratch: compression === 2 || compression === 3 ? zipScratch : void 0
|
|
3037
|
+
});
|
|
3038
|
+
bytesRead += dataSize;
|
|
3039
|
+
let blockPointer = 0;
|
|
3040
|
+
const blockView = new DataView(blockData.buffer, blockData.byteOffset, blockData.byteLength);
|
|
3041
|
+
for (let dy = 0; dy < linesInChunk; dy++) {
|
|
3042
|
+
const y = chunkY + dy;
|
|
3043
|
+
if (y < part.dataWindow.yMin) {
|
|
3044
|
+
continue;
|
|
3045
|
+
}
|
|
3046
|
+
for (const meta of channelMeta) {
|
|
3047
|
+
if (meta.sampledWidth === 0 || meta.sampledHeight === 0) {
|
|
3048
|
+
continue;
|
|
3049
|
+
}
|
|
3050
|
+
if (!isSampledCoordinate4(y, meta.sampleOriginY, meta.ySampling)) {
|
|
3051
|
+
continue;
|
|
3052
|
+
}
|
|
3053
|
+
const byteLength = meta.rowByteLength;
|
|
3054
|
+
if (blockPointer + byteLength > blockData.byteLength) {
|
|
3055
|
+
throw new ExrError("MALFORMED_CHUNK", "Chunk row data exceeded block payload bounds.", {
|
|
3056
|
+
partId: part.id,
|
|
3057
|
+
chunkIndex,
|
|
3058
|
+
row: y,
|
|
3059
|
+
channel: meta.channel.name,
|
|
3060
|
+
blockPointer,
|
|
3061
|
+
byteLength,
|
|
3062
|
+
blockSize: blockData.byteLength
|
|
3063
|
+
});
|
|
3064
|
+
}
|
|
3065
|
+
const sampledRow = Math.floor((y - meta.sampleOriginY) / meta.ySampling);
|
|
3066
|
+
if (sampledRow < 0 || sampledRow >= meta.sampledHeight) {
|
|
3067
|
+
blockPointer += byteLength;
|
|
3068
|
+
continue;
|
|
3069
|
+
}
|
|
3070
|
+
const rowOffset = sampledRow * meta.sampledWidth;
|
|
3071
|
+
decodeRowIntoChannel(meta, blockData, blockPointer, rowOffset, blockView);
|
|
3072
|
+
blockPointer += byteLength;
|
|
3073
|
+
}
|
|
3074
|
+
}
|
|
3075
|
+
if (blockPointer < blockData.byteLength) {
|
|
3076
|
+
emit2(onEvent, {
|
|
3077
|
+
phase: "decode",
|
|
3078
|
+
level: "warn",
|
|
3079
|
+
code: "decode.chunk.trailing_bytes",
|
|
3080
|
+
message: "Chunk had trailing bytes after row decode.",
|
|
3081
|
+
metrics: {
|
|
3082
|
+
partId: part.id,
|
|
3083
|
+
chunkIndex,
|
|
3084
|
+
trailing: blockData.byteLength - blockPointer
|
|
3085
|
+
}
|
|
3086
|
+
});
|
|
3087
|
+
}
|
|
3088
|
+
}
|
|
3089
|
+
const decodedChannels = {};
|
|
3090
|
+
for (const meta of channelMeta) {
|
|
3091
|
+
decodedChannels[meta.channel.name] = {
|
|
3092
|
+
pixelType: meta.channel.pixelType,
|
|
3093
|
+
xSampling: meta.channel.xSampling,
|
|
3094
|
+
ySampling: meta.channel.ySampling,
|
|
3095
|
+
sampledWidth: meta.sampledWidth,
|
|
3096
|
+
sampledHeight: meta.sampledHeight,
|
|
3097
|
+
sampleOriginX: meta.sampleOriginX,
|
|
3098
|
+
sampleOriginY: meta.sampleOriginY,
|
|
3099
|
+
data: meta.data
|
|
3100
|
+
};
|
|
3101
|
+
}
|
|
3102
|
+
emit2(onEvent, {
|
|
3103
|
+
phase: "decode",
|
|
3104
|
+
level: "info",
|
|
3105
|
+
code: "decode.complete",
|
|
3106
|
+
message: `Decoded part ${part.id}.`,
|
|
3107
|
+
metrics: {
|
|
3108
|
+
partId: part.id,
|
|
3109
|
+
channels: Object.keys(decodedChannels).length,
|
|
3110
|
+
readKB: (bytesRead / 1024).toFixed(1),
|
|
3111
|
+
ms: (nowMs2() - tDecode).toFixed(3),
|
|
3112
|
+
totalMs: (nowMs2() - t0).toFixed(3)
|
|
3113
|
+
}
|
|
3114
|
+
});
|
|
3115
|
+
return {
|
|
3116
|
+
width,
|
|
3117
|
+
height,
|
|
3118
|
+
channels: decodedChannels
|
|
3119
|
+
};
|
|
3120
|
+
}
|
|
3121
|
+
|
|
3122
|
+
// src/reader/readExr.ts
|
|
3123
|
+
function readExr(buffer, options = {}) {
|
|
3124
|
+
const structure = parseExrStructure(buffer, { onEvent: options.onEvent });
|
|
3125
|
+
const partId = options.partId ?? structure.parts[0]?.id ?? 0;
|
|
3126
|
+
const part = decodeExrPart(buffer, structure, {
|
|
3127
|
+
partId,
|
|
3128
|
+
predecodedZipBlocks: options.decode?.predecodedZipBlocks,
|
|
3129
|
+
predecodedDwaBlocks: options.decode?.predecodedDwaBlocks,
|
|
3130
|
+
onEvent: options.onEvent
|
|
3131
|
+
});
|
|
3132
|
+
return {
|
|
3133
|
+
structure,
|
|
3134
|
+
part,
|
|
3135
|
+
partId
|
|
3136
|
+
};
|
|
3137
|
+
}
|
|
3138
|
+
|
|
3139
|
+
// src/writer/sampling.ts
|
|
3140
|
+
function modulo5(value, base) {
|
|
3141
|
+
const result = value % base;
|
|
3142
|
+
return result < 0 ? result + base : result;
|
|
3143
|
+
}
|
|
3144
|
+
function firstSampleCoordinate5(min, sampling) {
|
|
3145
|
+
if (sampling <= 1) return min;
|
|
3146
|
+
const remainder = modulo5(min, sampling);
|
|
3147
|
+
return remainder === 0 ? min : min + (sampling - remainder);
|
|
3148
|
+
}
|
|
3149
|
+
function countSamplesInRange5(min, max, sampling) {
|
|
3150
|
+
if (sampling <= 0 || max < min) return 0;
|
|
3151
|
+
const first = firstSampleCoordinate5(min, sampling);
|
|
3152
|
+
if (first > max) return 0;
|
|
3153
|
+
return Math.floor((max - first) / sampling) + 1;
|
|
3154
|
+
}
|
|
3155
|
+
function isSampledCoordinate5(value, firstSample, sampling) {
|
|
3156
|
+
if (sampling <= 1) return true;
|
|
3157
|
+
if (value < firstSample) return false;
|
|
3158
|
+
return (value - firstSample) % sampling === 0;
|
|
3159
|
+
}
|
|
3160
|
+
|
|
3161
|
+
// src/writer/compression/b44.ts
|
|
3162
|
+
var B44_BLOCK_SIZE2 = 14;
|
|
3163
|
+
var B44A_BLOCK_MARKER = 252;
|
|
3164
|
+
var B44A_COMPRESSION = 7;
|
|
3165
|
+
function countSampledRowsInChunk(channel, chunkY, linesInChunk) {
|
|
3166
|
+
let rows = 0;
|
|
3167
|
+
for (let dy = 0; dy < linesInChunk; dy++) {
|
|
3168
|
+
const y = chunkY + dy;
|
|
3169
|
+
if (isSampledCoordinate5(y, channel.sampleOriginY, channel.ySampling)) {
|
|
3170
|
+
rows++;
|
|
3171
|
+
}
|
|
3172
|
+
}
|
|
3173
|
+
return rows;
|
|
3174
|
+
}
|
|
3175
|
+
function buildChunkChannelPlanes(raw, part, chunkY, linesInChunk) {
|
|
3176
|
+
const planes = part.channels.map((channel) => {
|
|
3177
|
+
const sampledRows = countSampledRowsInChunk(channel, chunkY, linesInChunk);
|
|
3178
|
+
return {
|
|
3179
|
+
channel,
|
|
3180
|
+
sampledRows,
|
|
3181
|
+
plane: new Uint8Array(channel.rowByteLength * sampledRows),
|
|
3182
|
+
writePtr: 0
|
|
3183
|
+
};
|
|
3184
|
+
});
|
|
3185
|
+
let rawPtr = 0;
|
|
3186
|
+
for (let dy = 0; dy < linesInChunk; dy++) {
|
|
3187
|
+
const y = chunkY + dy;
|
|
3188
|
+
for (let channelIndex = 0; channelIndex < part.channels.length; channelIndex++) {
|
|
3189
|
+
const plane = planes[channelIndex];
|
|
3190
|
+
const channel = plane.channel;
|
|
3191
|
+
if (!isSampledCoordinate5(y, channel.sampleOriginY, channel.ySampling)) {
|
|
3192
|
+
continue;
|
|
3193
|
+
}
|
|
3194
|
+
const rowSize = channel.rowByteLength;
|
|
3195
|
+
if (rawPtr + rowSize > raw.byteLength) {
|
|
3196
|
+
throw new ExrError("ENCODING_FAILED", "Failed to split raw chunk into channel planes.", {
|
|
3197
|
+
consumed: rawPtr,
|
|
3198
|
+
expected: raw.byteLength,
|
|
3199
|
+
rowSize
|
|
3200
|
+
});
|
|
3201
|
+
}
|
|
3202
|
+
plane.plane.set(raw.subarray(rawPtr, rawPtr + rowSize), plane.writePtr);
|
|
3203
|
+
plane.writePtr += rowSize;
|
|
3204
|
+
rawPtr += rowSize;
|
|
3205
|
+
}
|
|
3206
|
+
}
|
|
3207
|
+
if (rawPtr !== raw.byteLength) {
|
|
3208
|
+
throw new ExrError("ENCODING_FAILED", "Raw chunk split consumed an unexpected number of bytes.", {
|
|
3209
|
+
consumed: rawPtr,
|
|
3210
|
+
expected: raw.byteLength
|
|
3211
|
+
});
|
|
3212
|
+
}
|
|
3213
|
+
return planes.map((plane) => ({
|
|
3214
|
+
channel: plane.channel,
|
|
3215
|
+
sampledRows: plane.sampledRows,
|
|
3216
|
+
plane: plane.plane
|
|
3217
|
+
}));
|
|
3218
|
+
}
|
|
3219
|
+
function transformHalfForB44(halfValue) {
|
|
3220
|
+
if ((halfValue & 31744) === 31744) {
|
|
3221
|
+
return 32768;
|
|
3222
|
+
}
|
|
3223
|
+
if ((halfValue & 32768) !== 0) {
|
|
3224
|
+
return ~halfValue & 65535;
|
|
3225
|
+
}
|
|
3226
|
+
return halfValue | 32768;
|
|
3227
|
+
}
|
|
3228
|
+
function convertLinearToB44Half(halfValue) {
|
|
3229
|
+
if ((halfValue & 31744) === 31744) {
|
|
3230
|
+
return 0;
|
|
3231
|
+
}
|
|
3232
|
+
if (halfValue >= 21900 && halfValue < 32768) {
|
|
3233
|
+
return 31743;
|
|
3234
|
+
}
|
|
3235
|
+
const linearValue = float16ToFloat32(halfValue);
|
|
3236
|
+
if (!Number.isFinite(linearValue)) {
|
|
3237
|
+
return 0;
|
|
3238
|
+
}
|
|
3239
|
+
const mapped = Math.exp(linearValue / 8);
|
|
3240
|
+
return float32ToFloat16(mapped);
|
|
3241
|
+
}
|
|
3242
|
+
function signedDelta16(current, previous) {
|
|
3243
|
+
const diff = current - previous & 65535;
|
|
3244
|
+
return diff >= 32768 ? diff - 65536 : diff;
|
|
3245
|
+
}
|
|
3246
|
+
function clampUint6(value) {
|
|
3247
|
+
if (value < 0) return 0;
|
|
3248
|
+
if (value > 63) return 63;
|
|
3249
|
+
return value | 0;
|
|
3250
|
+
}
|
|
3251
|
+
function packB44Block14(transformed) {
|
|
3252
|
+
const deltas = [
|
|
3253
|
+
signedDelta16(transformed[4], transformed[0]),
|
|
3254
|
+
signedDelta16(transformed[8], transformed[4]),
|
|
3255
|
+
signedDelta16(transformed[12], transformed[8]),
|
|
3256
|
+
signedDelta16(transformed[1], transformed[0]),
|
|
3257
|
+
signedDelta16(transformed[5], transformed[4]),
|
|
3258
|
+
signedDelta16(transformed[9], transformed[8]),
|
|
3259
|
+
signedDelta16(transformed[13], transformed[12]),
|
|
3260
|
+
signedDelta16(transformed[2], transformed[1]),
|
|
3261
|
+
signedDelta16(transformed[6], transformed[5]),
|
|
3262
|
+
signedDelta16(transformed[10], transformed[9]),
|
|
3263
|
+
signedDelta16(transformed[14], transformed[13]),
|
|
3264
|
+
signedDelta16(transformed[3], transformed[2]),
|
|
3265
|
+
signedDelta16(transformed[7], transformed[6]),
|
|
3266
|
+
signedDelta16(transformed[11], transformed[10]),
|
|
3267
|
+
signedDelta16(transformed[15], transformed[14])
|
|
3268
|
+
];
|
|
3269
|
+
let shift = 0;
|
|
3270
|
+
for (; shift < 15; shift++) {
|
|
3271
|
+
const step2 = 1 << shift;
|
|
3272
|
+
const minDelta = -(32 * step2);
|
|
3273
|
+
const maxDelta = 31 * step2;
|
|
3274
|
+
const fits = deltas.every((delta) => delta >= minDelta && delta <= maxDelta);
|
|
3275
|
+
if (fits) break;
|
|
3276
|
+
}
|
|
3277
|
+
const step = 1 << shift;
|
|
3278
|
+
const bias = 32 * step;
|
|
3279
|
+
const q = deltas.map((delta) => clampUint6(Math.round((delta + bias) / step)));
|
|
3280
|
+
const out = new Uint8Array(B44_BLOCK_SIZE2);
|
|
3281
|
+
out[0] = transformed[0] >> 8 & 255;
|
|
3282
|
+
out[1] = transformed[0] & 255;
|
|
3283
|
+
out[2] = (shift & 63) << 2 | q[0] >> 4 & 3;
|
|
3284
|
+
out[3] = (q[0] & 15) << 4 | q[1] >> 2 & 15;
|
|
3285
|
+
out[4] = (q[1] & 3) << 6 | q[2] & 63;
|
|
3286
|
+
out[5] = (q[3] & 63) << 2 | q[4] >> 4 & 3;
|
|
3287
|
+
out[6] = (q[4] & 15) << 4 | q[5] >> 2 & 15;
|
|
3288
|
+
out[7] = (q[5] & 3) << 6 | q[6] & 63;
|
|
3289
|
+
out[8] = (q[7] & 63) << 2 | q[8] >> 4 & 3;
|
|
3290
|
+
out[9] = (q[8] & 15) << 4 | q[9] >> 2 & 15;
|
|
3291
|
+
out[10] = (q[9] & 3) << 6 | q[10] & 63;
|
|
3292
|
+
out[11] = (q[11] & 63) << 2 | q[12] >> 4 & 3;
|
|
3293
|
+
out[12] = (q[12] & 15) << 4 | q[13] >> 2 & 15;
|
|
3294
|
+
out[13] = (q[13] & 3) << 6 | q[14] & 63;
|
|
3295
|
+
return out;
|
|
3296
|
+
}
|
|
3297
|
+
function encodeHalfPlaneB44(plane, sampledWidth, sampledRows, compression, applyPLinear) {
|
|
3298
|
+
if (sampledWidth <= 0 || sampledRows <= 0) {
|
|
3299
|
+
return new Uint8Array(0);
|
|
3300
|
+
}
|
|
3301
|
+
const view = new DataView(plane.buffer, plane.byteOffset, plane.byteLength);
|
|
3302
|
+
const blocksX = Math.ceil(sampledWidth / 4);
|
|
3303
|
+
const blocksY = Math.ceil(sampledRows / 4);
|
|
3304
|
+
const out = [];
|
|
3305
|
+
const transformed = new Uint16Array(16);
|
|
3306
|
+
for (let blockY = 0; blockY < blocksY; blockY++) {
|
|
3307
|
+
const startY = blockY * 4;
|
|
3308
|
+
for (let blockX = 0; blockX < blocksX; blockX++) {
|
|
3309
|
+
const startX = blockX * 4;
|
|
3310
|
+
for (let row = 0; row < 4; row++) {
|
|
3311
|
+
const sourceY = Math.min(startY + row, sampledRows - 1);
|
|
3312
|
+
for (let col = 0; col < 4; col++) {
|
|
3313
|
+
const sourceX = Math.min(startX + col, sampledWidth - 1);
|
|
3314
|
+
const sampleIndex = sourceY * sampledWidth + sourceX;
|
|
3315
|
+
const sourceHalf = view.getUint16(sampleIndex * 2, true);
|
|
3316
|
+
const half = applyPLinear ? convertLinearToB44Half(sourceHalf) : sourceHalf;
|
|
3317
|
+
transformed[row * 4 + col] = transformHalfForB44(half);
|
|
3318
|
+
}
|
|
3319
|
+
}
|
|
3320
|
+
const first = transformed[0];
|
|
3321
|
+
let isFlat = true;
|
|
3322
|
+
for (let i = 1; i < transformed.length; i++) {
|
|
3323
|
+
if (transformed[i] !== first) {
|
|
3324
|
+
isFlat = false;
|
|
3325
|
+
break;
|
|
3326
|
+
}
|
|
3327
|
+
}
|
|
3328
|
+
if (compression === B44A_COMPRESSION && isFlat) {
|
|
3329
|
+
out.push(first >> 8 & 255, first & 255, B44A_BLOCK_MARKER);
|
|
3330
|
+
continue;
|
|
3331
|
+
}
|
|
3332
|
+
const block = packB44Block14(transformed);
|
|
3333
|
+
out.push(...block);
|
|
3334
|
+
}
|
|
3335
|
+
}
|
|
3336
|
+
return Uint8Array.from(out);
|
|
3337
|
+
}
|
|
3338
|
+
function encodeB44Chunk(raw, part, chunkY) {
|
|
3339
|
+
const linesInChunk = Math.max(0, Math.min(part.linesPerBlock, part.dataWindow.yMax - chunkY + 1));
|
|
3340
|
+
const channelPlanes = buildChunkChannelPlanes(raw, part, chunkY, linesInChunk);
|
|
3341
|
+
const out = [];
|
|
3342
|
+
for (const channelPlane of channelPlanes) {
|
|
3343
|
+
if (channelPlane.channel.pixelType === 1) {
|
|
3344
|
+
const encodedHalfPlane = encodeHalfPlaneB44(
|
|
3345
|
+
channelPlane.plane,
|
|
3346
|
+
channelPlane.channel.sampledWidth,
|
|
3347
|
+
channelPlane.sampledRows,
|
|
3348
|
+
part.compression,
|
|
3349
|
+
channelPlane.channel.pLinear !== 0
|
|
3350
|
+
);
|
|
3351
|
+
out.push(...encodedHalfPlane);
|
|
3352
|
+
continue;
|
|
3353
|
+
}
|
|
3354
|
+
out.push(...channelPlane.plane);
|
|
3355
|
+
}
|
|
3356
|
+
const encoded = Uint8Array.from(out);
|
|
3357
|
+
return encoded.byteLength >= raw.byteLength ? raw : encoded;
|
|
3358
|
+
}
|
|
3359
|
+
|
|
3360
|
+
// src/writer/compression/piz.ts
|
|
3361
|
+
var USHORT_RANGE2 = 1 << 16;
|
|
3362
|
+
var BITMAP_SIZE2 = USHORT_RANGE2 >> 3;
|
|
3363
|
+
var HUF_ENCBITS3 = 16;
|
|
3364
|
+
var HUF_ENCSIZE3 = (1 << HUF_ENCBITS3) + 1;
|
|
3365
|
+
var SHORT_ZEROCODE_RUN3 = 59;
|
|
3366
|
+
var LONG_ZEROCODE_RUN3 = 63;
|
|
3367
|
+
var SHORTEST_LONG_RUN3 = 2 + LONG_ZEROCODE_RUN3 - SHORT_ZEROCODE_RUN3;
|
|
3368
|
+
var LONGEST_LONG_RUN = 255 + SHORTEST_LONG_RUN3;
|
|
3369
|
+
var NBITS2 = 16;
|
|
3370
|
+
var A_OFFSET2 = 1 << NBITS2 - 1;
|
|
3371
|
+
var M_OFFSET = 1 << NBITS2 - 1;
|
|
3372
|
+
var MOD_MASK2 = (1 << NBITS2) - 1;
|
|
3373
|
+
var MAX_SAFE_HUF_CODE_LENGTH = 30;
|
|
3374
|
+
var BitWriter = class {
|
|
3375
|
+
bytes = [];
|
|
3376
|
+
bitBuffer = 0;
|
|
3377
|
+
bitCount = 0;
|
|
3378
|
+
bitLength = 0;
|
|
3379
|
+
writeBits(nBits, bits) {
|
|
3380
|
+
if (nBits <= 0) {
|
|
3381
|
+
return;
|
|
3382
|
+
}
|
|
3383
|
+
const maskedBits = bits & (1 << nBits) - 1;
|
|
3384
|
+
this.bitBuffer = this.bitBuffer * 2 ** nBits + maskedBits;
|
|
3385
|
+
this.bitCount += nBits;
|
|
3386
|
+
this.bitLength += nBits;
|
|
3387
|
+
while (this.bitCount >= 8) {
|
|
3388
|
+
this.bitCount -= 8;
|
|
3389
|
+
this.bytes.push(Math.floor(this.bitBuffer / 2 ** this.bitCount) & 255);
|
|
3390
|
+
}
|
|
3391
|
+
if (this.bitCount === 0) {
|
|
3392
|
+
this.bitBuffer = 0;
|
|
3393
|
+
} else {
|
|
3394
|
+
this.bitBuffer &= (1 << this.bitCount) - 1;
|
|
3395
|
+
}
|
|
3396
|
+
}
|
|
3397
|
+
finish() {
|
|
3398
|
+
if (this.bitCount > 0) {
|
|
3399
|
+
this.bytes.push(this.bitBuffer << 8 - this.bitCount & 255);
|
|
3400
|
+
this.bitCount = 0;
|
|
3401
|
+
this.bitBuffer = 0;
|
|
3402
|
+
}
|
|
3403
|
+
return {
|
|
3404
|
+
bytes: Uint8Array.from(this.bytes),
|
|
3405
|
+
bitLength: this.bitLength
|
|
3406
|
+
};
|
|
3407
|
+
}
|
|
3408
|
+
};
|
|
3409
|
+
function uint162(value) {
|
|
3410
|
+
return value & 65535;
|
|
3411
|
+
}
|
|
3412
|
+
function int162(value) {
|
|
3413
|
+
const clamped = uint162(value);
|
|
3414
|
+
return clamped > 32767 ? clamped - 65536 : clamped;
|
|
3415
|
+
}
|
|
3416
|
+
function wenc14(a, b) {
|
|
3417
|
+
const as = int162(a);
|
|
3418
|
+
const bs = int162(b);
|
|
3419
|
+
const ms = as + bs >> 1;
|
|
3420
|
+
const ds = as - bs;
|
|
3421
|
+
return {
|
|
3422
|
+
l: uint162(ms),
|
|
3423
|
+
h: uint162(ds)
|
|
3424
|
+
};
|
|
3425
|
+
}
|
|
3426
|
+
function wenc16(a, b) {
|
|
3427
|
+
const ao = uint162(a) + A_OFFSET2 & MOD_MASK2;
|
|
3428
|
+
let m = ao + uint162(b) >> 1;
|
|
3429
|
+
let d = ao - uint162(b);
|
|
3430
|
+
if (d < 0) {
|
|
3431
|
+
m = m + M_OFFSET & MOD_MASK2;
|
|
3432
|
+
}
|
|
3433
|
+
d &= MOD_MASK2;
|
|
3434
|
+
return {
|
|
3435
|
+
l: uint162(m),
|
|
3436
|
+
h: uint162(d)
|
|
3437
|
+
};
|
|
3438
|
+
}
|
|
3439
|
+
function wav2Encode(buffer, channelOffset, nx, ox, ny, oy, maxValue) {
|
|
3440
|
+
if (nx <= 0 || ny <= 0) {
|
|
3441
|
+
return;
|
|
3442
|
+
}
|
|
3443
|
+
const wavelet14 = maxValue < 1 << 14;
|
|
3444
|
+
const minDim = nx > ny ? ny : nx;
|
|
3445
|
+
let p = 1;
|
|
3446
|
+
let p2 = 2;
|
|
3447
|
+
while (p2 <= minDim) {
|
|
3448
|
+
let py = 0;
|
|
3449
|
+
const ey = oy * (ny - p2);
|
|
3450
|
+
const oy1 = oy * p;
|
|
3451
|
+
const oy2 = oy * p2;
|
|
3452
|
+
const ox1 = ox * p;
|
|
3453
|
+
const ox2 = ox * p2;
|
|
3454
|
+
for (; py <= ey; py += oy2) {
|
|
3455
|
+
let px = py;
|
|
3456
|
+
const ex = py + ox * (nx - p2);
|
|
3457
|
+
for (; px <= ex; px += ox2) {
|
|
3458
|
+
const p01 = px + ox1;
|
|
3459
|
+
const p10 = px + oy1;
|
|
3460
|
+
const p11 = p10 + ox1;
|
|
3461
|
+
if (wavelet14) {
|
|
3462
|
+
const r00 = wenc14(buffer[channelOffset + px], buffer[channelOffset + p01]);
|
|
3463
|
+
const r10 = wenc14(buffer[channelOffset + p10], buffer[channelOffset + p11]);
|
|
3464
|
+
const top = wenc14(r00.l, r10.l);
|
|
3465
|
+
const bottom = wenc14(r00.h, r10.h);
|
|
3466
|
+
buffer[channelOffset + px] = top.l;
|
|
3467
|
+
buffer[channelOffset + p10] = top.h;
|
|
3468
|
+
buffer[channelOffset + p01] = bottom.l;
|
|
3469
|
+
buffer[channelOffset + p11] = bottom.h;
|
|
3470
|
+
} else {
|
|
3471
|
+
const r00 = wenc16(buffer[channelOffset + px], buffer[channelOffset + p01]);
|
|
3472
|
+
const r10 = wenc16(buffer[channelOffset + p10], buffer[channelOffset + p11]);
|
|
3473
|
+
const top = wenc16(r00.l, r10.l);
|
|
3474
|
+
const bottom = wenc16(r00.h, r10.h);
|
|
3475
|
+
buffer[channelOffset + px] = top.l;
|
|
3476
|
+
buffer[channelOffset + p10] = top.h;
|
|
3477
|
+
buffer[channelOffset + p01] = bottom.l;
|
|
3478
|
+
buffer[channelOffset + p11] = bottom.h;
|
|
3479
|
+
}
|
|
3480
|
+
}
|
|
3481
|
+
if (nx & p) {
|
|
3482
|
+
const p10 = px + oy1;
|
|
3483
|
+
const result = wavelet14 ? wenc14(buffer[channelOffset + px], buffer[channelOffset + p10]) : wenc16(buffer[channelOffset + px], buffer[channelOffset + p10]);
|
|
3484
|
+
buffer[channelOffset + px] = result.l;
|
|
3485
|
+
buffer[channelOffset + p10] = result.h;
|
|
3486
|
+
}
|
|
3487
|
+
}
|
|
3488
|
+
if (ny & p) {
|
|
3489
|
+
let px = py;
|
|
3490
|
+
const ex = py + ox * (nx - p2);
|
|
3491
|
+
for (; px <= ex; px += ox2) {
|
|
3492
|
+
const p01 = px + ox1;
|
|
3493
|
+
const result = wavelet14 ? wenc14(buffer[channelOffset + px], buffer[channelOffset + p01]) : wenc16(buffer[channelOffset + px], buffer[channelOffset + p01]);
|
|
3494
|
+
buffer[channelOffset + px] = result.l;
|
|
3495
|
+
buffer[channelOffset + p01] = result.h;
|
|
3496
|
+
}
|
|
3497
|
+
}
|
|
3498
|
+
p = p2;
|
|
3499
|
+
p2 <<= 1;
|
|
3500
|
+
}
|
|
3501
|
+
}
|
|
3502
|
+
function bitmapFromData(data) {
|
|
3503
|
+
const bitmap = new Uint8Array(BITMAP_SIZE2);
|
|
3504
|
+
let minNonZero = BITMAP_SIZE2 - 1;
|
|
3505
|
+
let maxNonZero = 0;
|
|
3506
|
+
for (let i = 0; i < data.length; i++) {
|
|
3507
|
+
const value = data[i];
|
|
3508
|
+
bitmap[value >> 3] |= 1 << (value & 7);
|
|
3509
|
+
}
|
|
3510
|
+
bitmap[0] &= ~1;
|
|
3511
|
+
for (let i = 0; i < BITMAP_SIZE2; i++) {
|
|
3512
|
+
if (bitmap[i] !== 0) {
|
|
3513
|
+
if (i < minNonZero) minNonZero = i;
|
|
3514
|
+
if (i > maxNonZero) maxNonZero = i;
|
|
3515
|
+
}
|
|
3516
|
+
}
|
|
3517
|
+
return { bitmap, minNonZero, maxNonZero };
|
|
3518
|
+
}
|
|
3519
|
+
function forwardLutFromBitmap(bitmap) {
|
|
3520
|
+
const lut = new Uint16Array(USHORT_RANGE2);
|
|
3521
|
+
let k = 0;
|
|
3522
|
+
for (let i = 0; i < USHORT_RANGE2; i++) {
|
|
3523
|
+
if (i === 0 || (bitmap[i >> 3] & 1 << (i & 7)) !== 0) {
|
|
3524
|
+
lut[i] = k++;
|
|
3525
|
+
} else {
|
|
3526
|
+
lut[i] = 0;
|
|
3527
|
+
}
|
|
3528
|
+
}
|
|
3529
|
+
return { lut, maxValue: k - 1 };
|
|
3530
|
+
}
|
|
3531
|
+
function applyLut2(lut, data) {
|
|
3532
|
+
for (let i = 0; i < data.length; i++) {
|
|
3533
|
+
data[i] = lut[data[i]];
|
|
3534
|
+
}
|
|
3535
|
+
}
|
|
3536
|
+
function countSampledRowsInChunk2(channel, chunkY, linesInChunk) {
|
|
3537
|
+
let rows = 0;
|
|
3538
|
+
for (let dy = 0; dy < linesInChunk; dy++) {
|
|
3539
|
+
const y = chunkY + dy;
|
|
3540
|
+
if (isSampledCoordinate5(y, channel.sampleOriginY, channel.ySampling)) {
|
|
3541
|
+
rows++;
|
|
3542
|
+
}
|
|
3543
|
+
}
|
|
3544
|
+
return rows;
|
|
3545
|
+
}
|
|
3546
|
+
function buildChannelLayouts2(part, chunkY, linesInChunk) {
|
|
3547
|
+
const layouts = [];
|
|
3548
|
+
let totalWords = 0;
|
|
3549
|
+
for (const channel of part.channels) {
|
|
3550
|
+
const sampledRows = countSampledRowsInChunk2(channel, chunkY, linesInChunk);
|
|
3551
|
+
const wordsPerSample = channel.pixelType === 1 ? 1 : 2;
|
|
3552
|
+
const wordsPerRow = channel.sampledWidth * wordsPerSample;
|
|
3553
|
+
const wordCount = wordsPerRow * sampledRows;
|
|
3554
|
+
layouts.push({
|
|
3555
|
+
channel,
|
|
3556
|
+
sampledWidth: channel.sampledWidth,
|
|
3557
|
+
sampledRows,
|
|
3558
|
+
wordsPerSample,
|
|
3559
|
+
wordsPerRow,
|
|
3560
|
+
wordStart: totalWords,
|
|
3561
|
+
wordEnd: totalWords + wordCount,
|
|
3562
|
+
wordCursor: totalWords
|
|
3563
|
+
});
|
|
3564
|
+
totalWords += wordCount;
|
|
3565
|
+
}
|
|
3566
|
+
return { layouts, totalWords };
|
|
3567
|
+
}
|
|
3568
|
+
function rawToWordPlanes(raw, part, chunkY, linesInChunk, layouts, totalWords) {
|
|
3569
|
+
const out = new Uint16Array(totalWords);
|
|
3570
|
+
const rawView = new DataView(raw.buffer, raw.byteOffset, raw.byteLength);
|
|
3571
|
+
let rawPtr = 0;
|
|
3572
|
+
for (let dy = 0; dy < linesInChunk; dy++) {
|
|
3573
|
+
const y = chunkY + dy;
|
|
3574
|
+
for (const layout of layouts) {
|
|
3575
|
+
const channel = layout.channel;
|
|
3576
|
+
if (!isSampledCoordinate5(y, channel.sampleOriginY, channel.ySampling)) {
|
|
3577
|
+
continue;
|
|
3578
|
+
}
|
|
3579
|
+
const rowByteLength = channel.rowByteLength;
|
|
3580
|
+
if (rawPtr + rowByteLength > raw.byteLength) {
|
|
3581
|
+
throw new ExrError(
|
|
3582
|
+
"ENCODING_FAILED",
|
|
3583
|
+
"Failed to split raw PIZ chunk into channel word planes.",
|
|
3584
|
+
{
|
|
3585
|
+
consumed: rawPtr,
|
|
3586
|
+
expected: raw.byteLength,
|
|
3587
|
+
rowByteLength
|
|
3588
|
+
}
|
|
3589
|
+
);
|
|
3590
|
+
}
|
|
3591
|
+
const rowWordLength = layout.wordsPerRow;
|
|
3592
|
+
if (layout.wordCursor + rowWordLength > layout.wordEnd) {
|
|
3593
|
+
throw new ExrError("ENCODING_FAILED", "PIZ channel plane cursor exceeded expected bounds.", {
|
|
3594
|
+
channel: channel.name,
|
|
3595
|
+
cursor: layout.wordCursor,
|
|
3596
|
+
rowWordLength,
|
|
3597
|
+
wordEnd: layout.wordEnd
|
|
3598
|
+
});
|
|
3599
|
+
}
|
|
3600
|
+
for (let i = 0; i < rowWordLength; i++) {
|
|
3601
|
+
out[layout.wordCursor + i] = rawView.getUint16(rawPtr + i * 2, true);
|
|
3602
|
+
}
|
|
3603
|
+
layout.wordCursor += rowWordLength;
|
|
3604
|
+
rawPtr += rowByteLength;
|
|
3605
|
+
}
|
|
3606
|
+
}
|
|
3607
|
+
if (rawPtr !== raw.byteLength) {
|
|
3608
|
+
throw new ExrError("ENCODING_FAILED", "PIZ raw split consumed an unexpected number of bytes.", {
|
|
3609
|
+
consumed: rawPtr,
|
|
3610
|
+
expected: raw.byteLength
|
|
3611
|
+
});
|
|
3612
|
+
}
|
|
3613
|
+
for (const layout of layouts) {
|
|
3614
|
+
if (layout.wordCursor !== layout.wordEnd) {
|
|
3615
|
+
throw new ExrError("ENCODING_FAILED", "PIZ channel plane fill did not reach expected bounds.", {
|
|
3616
|
+
channel: layout.channel.name,
|
|
3617
|
+
cursor: layout.wordCursor,
|
|
3618
|
+
wordEnd: layout.wordEnd
|
|
3619
|
+
});
|
|
3620
|
+
}
|
|
3621
|
+
}
|
|
3622
|
+
return out;
|
|
3623
|
+
}
|
|
3624
|
+
function swapHeapEntries(heap, i, j) {
|
|
3625
|
+
const temp = heap[i];
|
|
3626
|
+
heap[i] = heap[j];
|
|
3627
|
+
heap[j] = temp;
|
|
3628
|
+
}
|
|
3629
|
+
function compareNodes(a, b) {
|
|
3630
|
+
if (a.frequency !== b.frequency) {
|
|
3631
|
+
return a.frequency - b.frequency;
|
|
3632
|
+
}
|
|
3633
|
+
return a.tieBreaker - b.tieBreaker;
|
|
3634
|
+
}
|
|
3635
|
+
function heapPush(heap, nodes, nodeIndex) {
|
|
3636
|
+
heap.push(nodeIndex);
|
|
3637
|
+
let i = heap.length - 1;
|
|
3638
|
+
while (i > 0) {
|
|
3639
|
+
const parent = i - 1 >> 1;
|
|
3640
|
+
if (compareNodes(nodes[heap[parent]], nodes[heap[i]]) <= 0) {
|
|
3641
|
+
break;
|
|
3642
|
+
}
|
|
3643
|
+
swapHeapEntries(heap, parent, i);
|
|
3644
|
+
i = parent;
|
|
3645
|
+
}
|
|
3646
|
+
}
|
|
3647
|
+
function heapPop(heap, nodes) {
|
|
3648
|
+
const root = heap[0];
|
|
3649
|
+
const last = heap.pop();
|
|
3650
|
+
if (heap.length === 0 || last === void 0) {
|
|
3651
|
+
return root;
|
|
3652
|
+
}
|
|
3653
|
+
heap[0] = last;
|
|
3654
|
+
let i = 0;
|
|
3655
|
+
let siftDown = true;
|
|
3656
|
+
while (siftDown) {
|
|
3657
|
+
const left = i * 2 + 1;
|
|
3658
|
+
const right = left + 1;
|
|
3659
|
+
let smallest = i;
|
|
3660
|
+
if (left < heap.length && compareNodes(nodes[heap[left]], nodes[heap[smallest]]) < 0) {
|
|
3661
|
+
smallest = left;
|
|
3662
|
+
}
|
|
3663
|
+
if (right < heap.length && compareNodes(nodes[heap[right]], nodes[heap[smallest]]) < 0) {
|
|
3664
|
+
smallest = right;
|
|
3665
|
+
}
|
|
3666
|
+
if (smallest === i) {
|
|
3667
|
+
siftDown = false;
|
|
3668
|
+
continue;
|
|
3669
|
+
}
|
|
3670
|
+
swapHeapEntries(heap, i, smallest);
|
|
3671
|
+
i = smallest;
|
|
3672
|
+
}
|
|
3673
|
+
return root;
|
|
3674
|
+
}
|
|
3675
|
+
function buildHuffmanCodeLengths(frequencies, minSymbol, maxSymbol) {
|
|
3676
|
+
const nodes = [];
|
|
3677
|
+
const heap = [];
|
|
3678
|
+
let nextNodeId = 0;
|
|
3679
|
+
for (let symbol = minSymbol; symbol <= maxSymbol; symbol++) {
|
|
3680
|
+
const frequency = frequencies[symbol];
|
|
3681
|
+
if (frequency === 0) {
|
|
3682
|
+
continue;
|
|
3683
|
+
}
|
|
3684
|
+
const node = {
|
|
3685
|
+
index: nodes.length,
|
|
3686
|
+
frequency,
|
|
3687
|
+
symbol,
|
|
3688
|
+
left: -1,
|
|
3689
|
+
right: -1,
|
|
3690
|
+
tieBreaker: nextNodeId++
|
|
3691
|
+
};
|
|
3692
|
+
nodes.push(node);
|
|
3693
|
+
heapPush(heap, nodes, node.index);
|
|
3694
|
+
}
|
|
3695
|
+
if (heap.length === 0) {
|
|
3696
|
+
return null;
|
|
3697
|
+
}
|
|
3698
|
+
if (heap.length === 1) {
|
|
3699
|
+
const lengths2 = new Uint8Array(HUF_ENCSIZE3);
|
|
3700
|
+
lengths2[nodes[heap[0]].symbol] = 1;
|
|
3701
|
+
return lengths2;
|
|
3702
|
+
}
|
|
3703
|
+
while (heap.length > 1) {
|
|
3704
|
+
const leftIndex = heapPop(heap, nodes);
|
|
3705
|
+
const rightIndex = heapPop(heap, nodes);
|
|
3706
|
+
const left = nodes[leftIndex];
|
|
3707
|
+
const right = nodes[rightIndex];
|
|
3708
|
+
const parent = {
|
|
3709
|
+
index: nodes.length,
|
|
3710
|
+
frequency: left.frequency + right.frequency,
|
|
3711
|
+
symbol: -1,
|
|
3712
|
+
left: leftIndex,
|
|
3713
|
+
right: rightIndex,
|
|
3714
|
+
tieBreaker: nextNodeId++
|
|
3715
|
+
};
|
|
3716
|
+
nodes.push(parent);
|
|
3717
|
+
heapPush(heap, nodes, parent.index);
|
|
3718
|
+
}
|
|
3719
|
+
const root = heap[0];
|
|
3720
|
+
const lengths = new Uint8Array(HUF_ENCSIZE3);
|
|
3721
|
+
const stack = [{ nodeIndex: root, depth: 0 }];
|
|
3722
|
+
while (stack.length > 0) {
|
|
3723
|
+
const current = stack.pop();
|
|
3724
|
+
if (!current) {
|
|
3725
|
+
continue;
|
|
3726
|
+
}
|
|
3727
|
+
const node = nodes[current.nodeIndex];
|
|
3728
|
+
if (node.symbol >= 0) {
|
|
3729
|
+
lengths[node.symbol] = current.depth === 0 ? 1 : current.depth;
|
|
3730
|
+
continue;
|
|
3731
|
+
}
|
|
3732
|
+
if (node.right >= 0) {
|
|
3733
|
+
stack.push({ nodeIndex: node.right, depth: current.depth + 1 });
|
|
3734
|
+
}
|
|
3735
|
+
if (node.left >= 0) {
|
|
3736
|
+
stack.push({ nodeIndex: node.left, depth: current.depth + 1 });
|
|
3737
|
+
}
|
|
3738
|
+
}
|
|
3739
|
+
let maxLength = 0;
|
|
3740
|
+
for (let symbol = minSymbol; symbol <= maxSymbol; symbol++) {
|
|
3741
|
+
if (lengths[symbol] > maxLength) {
|
|
3742
|
+
maxLength = lengths[symbol];
|
|
3743
|
+
}
|
|
3744
|
+
}
|
|
3745
|
+
if (maxLength <= 0 || maxLength > 58 || maxLength > MAX_SAFE_HUF_CODE_LENGTH) {
|
|
3746
|
+
return null;
|
|
3747
|
+
}
|
|
3748
|
+
return lengths;
|
|
3749
|
+
}
|
|
3750
|
+
function buildCanonicalCodes(lengths, minSymbol, maxSymbol) {
|
|
3751
|
+
const counts = new Uint32Array(59);
|
|
3752
|
+
for (let symbol = minSymbol; symbol <= maxSymbol; symbol++) {
|
|
3753
|
+
const length = lengths[symbol];
|
|
3754
|
+
if (length >= counts.length) {
|
|
3755
|
+
return null;
|
|
3756
|
+
}
|
|
3757
|
+
counts[length] += 1;
|
|
3758
|
+
}
|
|
3759
|
+
let code = 0;
|
|
3760
|
+
for (let i = counts.length - 1; i > 0; i--) {
|
|
3761
|
+
const nextCode = code + counts[i] >> 1;
|
|
3762
|
+
counts[i] = code;
|
|
3763
|
+
code = nextCode;
|
|
3764
|
+
}
|
|
3765
|
+
const codes = new Uint32Array(HUF_ENCSIZE3);
|
|
3766
|
+
for (let symbol = minSymbol; symbol <= maxSymbol; symbol++) {
|
|
3767
|
+
const length = lengths[symbol];
|
|
3768
|
+
if (length > 0) {
|
|
3769
|
+
codes[symbol] = counts[length]++;
|
|
3770
|
+
}
|
|
3771
|
+
}
|
|
3772
|
+
return codes;
|
|
3773
|
+
}
|
|
3774
|
+
function hufPackEncTable(lengths, minSymbol, maxSymbol) {
|
|
3775
|
+
const writer = new BitWriter();
|
|
3776
|
+
for (let symbol = minSymbol; symbol <= maxSymbol; symbol++) {
|
|
3777
|
+
const length = lengths[symbol];
|
|
3778
|
+
if (length === 0) {
|
|
3779
|
+
let zeroRun = 1;
|
|
3780
|
+
while (symbol < maxSymbol && zeroRun < LONGEST_LONG_RUN && lengths[symbol + 1] === 0) {
|
|
3781
|
+
symbol++;
|
|
3782
|
+
zeroRun++;
|
|
3783
|
+
}
|
|
3784
|
+
if (zeroRun >= 2) {
|
|
3785
|
+
if (zeroRun >= SHORTEST_LONG_RUN3) {
|
|
3786
|
+
writer.writeBits(6, LONG_ZEROCODE_RUN3);
|
|
3787
|
+
writer.writeBits(8, zeroRun - SHORTEST_LONG_RUN3);
|
|
3788
|
+
} else {
|
|
3789
|
+
writer.writeBits(6, SHORT_ZEROCODE_RUN3 + zeroRun - 2);
|
|
3790
|
+
}
|
|
3791
|
+
continue;
|
|
3792
|
+
}
|
|
3793
|
+
}
|
|
3794
|
+
writer.writeBits(6, length);
|
|
3795
|
+
}
|
|
3796
|
+
return writer.finish().bytes;
|
|
3797
|
+
}
|
|
3798
|
+
function hufEncodeData(values, lengths, codes, runLengthSymbol) {
|
|
3799
|
+
if (values.length === 0) {
|
|
3800
|
+
return { bytes: new Uint8Array(0), nBits: 0 };
|
|
3801
|
+
}
|
|
3802
|
+
const runLengthCodeLength = lengths[runLengthSymbol];
|
|
3803
|
+
const runLengthCode = codes[runLengthSymbol];
|
|
3804
|
+
if (runLengthCodeLength === 0) {
|
|
3805
|
+
return null;
|
|
3806
|
+
}
|
|
3807
|
+
const writer = new BitWriter();
|
|
3808
|
+
function emitSymbolRun(symbol2, runCount2) {
|
|
3809
|
+
const symbolCodeLength = lengths[symbol2];
|
|
3810
|
+
const symbolCode = codes[symbol2];
|
|
3811
|
+
if (symbolCodeLength === 0) {
|
|
3812
|
+
return false;
|
|
3813
|
+
}
|
|
3814
|
+
if (symbolCodeLength + runLengthCodeLength + 8 < symbolCodeLength * runCount2) {
|
|
3815
|
+
writer.writeBits(symbolCodeLength, symbolCode);
|
|
3816
|
+
writer.writeBits(runLengthCodeLength, runLengthCode);
|
|
3817
|
+
writer.writeBits(8, runCount2);
|
|
3818
|
+
return true;
|
|
3819
|
+
}
|
|
3820
|
+
for (let count = runCount2; count >= 0; count--) {
|
|
3821
|
+
writer.writeBits(symbolCodeLength, symbolCode);
|
|
3822
|
+
}
|
|
3823
|
+
return true;
|
|
3824
|
+
}
|
|
3825
|
+
let symbol = values[0];
|
|
3826
|
+
let runCount = 0;
|
|
3827
|
+
for (let i = 1; i < values.length; i++) {
|
|
3828
|
+
const next = values[i];
|
|
3829
|
+
if (runCount === 255 || symbol !== next) {
|
|
3830
|
+
if (!emitSymbolRun(symbol, runCount)) {
|
|
3831
|
+
return null;
|
|
3832
|
+
}
|
|
3833
|
+
symbol = next;
|
|
3834
|
+
runCount = 0;
|
|
3835
|
+
} else {
|
|
3836
|
+
runCount++;
|
|
3837
|
+
}
|
|
3838
|
+
}
|
|
3839
|
+
if (!emitSymbolRun(symbol, runCount)) {
|
|
3840
|
+
return null;
|
|
3841
|
+
}
|
|
3842
|
+
const result = writer.finish();
|
|
3843
|
+
return { bytes: result.bytes, nBits: result.bitLength };
|
|
3844
|
+
}
|
|
3845
|
+
function encodeHuffman(values) {
|
|
3846
|
+
if (values.length === 0) {
|
|
3847
|
+
return {
|
|
3848
|
+
minSymbol: 0,
|
|
3849
|
+
maxSymbol: 0,
|
|
3850
|
+
tableBytes: new Uint8Array(0),
|
|
3851
|
+
nBits: 0,
|
|
3852
|
+
dataBytes: new Uint8Array(0)
|
|
3853
|
+
};
|
|
3854
|
+
}
|
|
3855
|
+
const frequencies = new Uint32Array(HUF_ENCSIZE3);
|
|
3856
|
+
let minSymbol = HUF_ENCSIZE3;
|
|
3857
|
+
let maxDataSymbol = 0;
|
|
3858
|
+
for (let i = 0; i < values.length; i++) {
|
|
3859
|
+
const symbol = values[i];
|
|
3860
|
+
frequencies[symbol] += 1;
|
|
3861
|
+
if (symbol < minSymbol) minSymbol = symbol;
|
|
3862
|
+
if (symbol > maxDataSymbol) maxDataSymbol = symbol;
|
|
3863
|
+
}
|
|
3864
|
+
const runLengthSymbol = maxDataSymbol + 1;
|
|
3865
|
+
if (runLengthSymbol >= HUF_ENCSIZE3) {
|
|
3866
|
+
return null;
|
|
3867
|
+
}
|
|
3868
|
+
frequencies[runLengthSymbol] = 1;
|
|
3869
|
+
if (runLengthSymbol < minSymbol) {
|
|
3870
|
+
minSymbol = runLengthSymbol;
|
|
3871
|
+
}
|
|
3872
|
+
const maxSymbol = runLengthSymbol;
|
|
3873
|
+
const lengths = buildHuffmanCodeLengths(frequencies, minSymbol, maxSymbol);
|
|
3874
|
+
if (!lengths) {
|
|
3875
|
+
return null;
|
|
3876
|
+
}
|
|
3877
|
+
const codes = buildCanonicalCodes(lengths, minSymbol, maxSymbol);
|
|
3878
|
+
if (!codes) {
|
|
3879
|
+
return null;
|
|
3880
|
+
}
|
|
3881
|
+
const tableBytes = hufPackEncTable(lengths, minSymbol, maxSymbol);
|
|
3882
|
+
const encodedData = hufEncodeData(values, lengths, codes, runLengthSymbol);
|
|
3883
|
+
if (!encodedData) {
|
|
3884
|
+
return null;
|
|
3885
|
+
}
|
|
3886
|
+
return {
|
|
3887
|
+
minSymbol,
|
|
3888
|
+
maxSymbol,
|
|
3889
|
+
tableBytes,
|
|
3890
|
+
nBits: encodedData.nBits,
|
|
3891
|
+
dataBytes: encodedData.bytes
|
|
3892
|
+
};
|
|
3893
|
+
}
|
|
3894
|
+
function buildHuffmanPayload(values) {
|
|
3895
|
+
const huffman = encodeHuffman(values);
|
|
3896
|
+
if (!huffman) {
|
|
3897
|
+
return null;
|
|
3898
|
+
}
|
|
3899
|
+
const totalLength = 20 + huffman.tableBytes.byteLength + huffman.dataBytes.byteLength;
|
|
3900
|
+
const out = new Uint8Array(totalLength);
|
|
3901
|
+
const view = new DataView(out.buffer);
|
|
3902
|
+
view.setUint32(0, huffman.minSymbol, true);
|
|
3903
|
+
view.setUint32(4, huffman.maxSymbol, true);
|
|
3904
|
+
view.setUint32(8, huffman.tableBytes.byteLength, true);
|
|
3905
|
+
view.setUint32(12, huffman.nBits, true);
|
|
3906
|
+
view.setUint32(16, 0, true);
|
|
3907
|
+
out.set(huffman.tableBytes, 20);
|
|
3908
|
+
out.set(huffman.dataBytes, 20 + huffman.tableBytes.byteLength);
|
|
3909
|
+
return out;
|
|
3910
|
+
}
|
|
3911
|
+
function buildPizPayload(raw, part, chunkY) {
|
|
3912
|
+
const linesInChunk = Math.max(0, Math.min(part.linesPerBlock, part.dataWindow.yMax - chunkY + 1));
|
|
3913
|
+
const { layouts, totalWords } = buildChannelLayouts2(part, chunkY, linesInChunk);
|
|
3914
|
+
if (totalWords === 0) {
|
|
3915
|
+
return new Uint8Array(0);
|
|
3916
|
+
}
|
|
3917
|
+
const words = rawToWordPlanes(raw, part, chunkY, linesInChunk, layouts, totalWords);
|
|
3918
|
+
const { bitmap, minNonZero, maxNonZero } = bitmapFromData(words);
|
|
3919
|
+
const { lut, maxValue } = forwardLutFromBitmap(bitmap);
|
|
3920
|
+
applyLut2(lut, words);
|
|
3921
|
+
for (const layout of layouts) {
|
|
3922
|
+
for (let component = 0; component < layout.wordsPerSample; component++) {
|
|
3923
|
+
wav2Encode(
|
|
3924
|
+
words,
|
|
3925
|
+
layout.wordStart + component,
|
|
3926
|
+
layout.sampledWidth,
|
|
3927
|
+
layout.wordsPerSample,
|
|
3928
|
+
layout.sampledRows,
|
|
3929
|
+
layout.sampledWidth * layout.wordsPerSample,
|
|
3930
|
+
maxValue
|
|
3931
|
+
);
|
|
3932
|
+
}
|
|
3933
|
+
}
|
|
3934
|
+
const huffmanPayload = buildHuffmanPayload(words);
|
|
3935
|
+
if (!huffmanPayload) {
|
|
3936
|
+
return null;
|
|
3937
|
+
}
|
|
3938
|
+
const bitmapLength = minNonZero <= maxNonZero ? maxNonZero - minNonZero + 1 : 0;
|
|
3939
|
+
const payloadLength = 4 + bitmapLength + 4 + huffmanPayload.byteLength;
|
|
3940
|
+
const payload = new Uint8Array(payloadLength);
|
|
3941
|
+
const view = new DataView(payload.buffer);
|
|
3942
|
+
view.setUint16(0, minNonZero, true);
|
|
3943
|
+
view.setUint16(2, maxNonZero, true);
|
|
3944
|
+
let offset = 4;
|
|
3945
|
+
if (bitmapLength > 0) {
|
|
3946
|
+
payload.set(bitmap.subarray(minNonZero, maxNonZero + 1), offset);
|
|
3947
|
+
offset += bitmapLength;
|
|
3948
|
+
}
|
|
3949
|
+
view.setUint32(offset, huffmanPayload.byteLength, true);
|
|
3950
|
+
offset += 4;
|
|
3951
|
+
payload.set(huffmanPayload, offset);
|
|
3952
|
+
return payload;
|
|
3953
|
+
}
|
|
3954
|
+
function encodePizChunk(raw, part, chunkY) {
|
|
3955
|
+
if (raw.byteLength === 0) {
|
|
3956
|
+
return raw;
|
|
3957
|
+
}
|
|
3958
|
+
const encoded = buildPizPayload(raw, part, chunkY);
|
|
3959
|
+
if (!encoded || encoded.byteLength >= raw.byteLength) {
|
|
3960
|
+
return raw;
|
|
3961
|
+
}
|
|
3962
|
+
return encoded;
|
|
3963
|
+
}
|
|
3964
|
+
|
|
3965
|
+
// src/writer/compression/pxr24.ts
|
|
3966
|
+
var import_fflate4 = require("fflate");
|
|
3967
|
+
function floatBitsToFloat24Bits(bits) {
|
|
3968
|
+
const sign = bits & 2147483648;
|
|
3969
|
+
const exponent = bits & 2139095040;
|
|
3970
|
+
const mantissa = bits & 8388607;
|
|
3971
|
+
let reduced = 0;
|
|
3972
|
+
if (exponent === 2139095040) {
|
|
3973
|
+
if (mantissa !== 0) {
|
|
3974
|
+
const shrunkMantissa = mantissa >>> 8;
|
|
3975
|
+
reduced = exponent >>> 8 | shrunkMantissa | (shrunkMantissa === 0 ? 1 : 0);
|
|
3976
|
+
} else {
|
|
3977
|
+
reduced = exponent >>> 8;
|
|
3978
|
+
}
|
|
3979
|
+
} else {
|
|
3980
|
+
reduced = (exponent | mantissa) + (mantissa & 128) >>> 8;
|
|
3981
|
+
if (reduced >= 8355840) {
|
|
3982
|
+
reduced = (exponent | mantissa) >>> 8;
|
|
3983
|
+
}
|
|
3984
|
+
}
|
|
3985
|
+
return (sign >>> 8 | reduced) >>> 0;
|
|
3986
|
+
}
|
|
3987
|
+
function encodePxr24Chunk(raw, part, chunkY) {
|
|
3988
|
+
const out = [];
|
|
3989
|
+
const view = new DataView(raw.buffer, raw.byteOffset, raw.byteLength);
|
|
3990
|
+
let rawPtr = 0;
|
|
3991
|
+
for (let dy = 0; dy < part.linesPerBlock; dy++) {
|
|
3992
|
+
const y = chunkY + dy;
|
|
3993
|
+
if (y > part.dataWindow.yMax) break;
|
|
3994
|
+
for (const channel of part.channels) {
|
|
3995
|
+
if (!isSampledCoordinate5(y, channel.sampleOriginY, channel.ySampling)) {
|
|
3996
|
+
continue;
|
|
3997
|
+
}
|
|
3998
|
+
const count = channel.sampledWidth;
|
|
3999
|
+
if (channel.pixelType === 1) {
|
|
4000
|
+
const p02 = new Uint8Array(count);
|
|
4001
|
+
const p12 = new Uint8Array(count);
|
|
4002
|
+
let prev2 = 0;
|
|
4003
|
+
for (let i = 0; i < count; i++) {
|
|
4004
|
+
const pixel = view.getUint16(rawPtr, true);
|
|
4005
|
+
rawPtr += 2;
|
|
4006
|
+
const diff = pixel - prev2 >>> 0;
|
|
4007
|
+
prev2 = pixel;
|
|
4008
|
+
p02[i] = diff >>> 8 & 255;
|
|
4009
|
+
p12[i] = diff & 255;
|
|
4010
|
+
}
|
|
4011
|
+
out.push(...p02, ...p12);
|
|
4012
|
+
continue;
|
|
4013
|
+
}
|
|
4014
|
+
if (channel.pixelType === 0) {
|
|
4015
|
+
const p02 = new Uint8Array(count);
|
|
4016
|
+
const p12 = new Uint8Array(count);
|
|
4017
|
+
const p22 = new Uint8Array(count);
|
|
4018
|
+
const p3 = new Uint8Array(count);
|
|
4019
|
+
let prev2 = 0;
|
|
4020
|
+
for (let i = 0; i < count; i++) {
|
|
4021
|
+
const pixel = view.getUint32(rawPtr, true);
|
|
4022
|
+
rawPtr += 4;
|
|
4023
|
+
const diff = pixel - prev2 >>> 0;
|
|
4024
|
+
prev2 = pixel;
|
|
4025
|
+
p02[i] = diff >>> 24 & 255;
|
|
4026
|
+
p12[i] = diff >>> 16 & 255;
|
|
4027
|
+
p22[i] = diff >>> 8 & 255;
|
|
4028
|
+
p3[i] = diff & 255;
|
|
4029
|
+
}
|
|
4030
|
+
out.push(...p02, ...p12, ...p22, ...p3);
|
|
4031
|
+
continue;
|
|
4032
|
+
}
|
|
4033
|
+
const p0 = new Uint8Array(count);
|
|
4034
|
+
const p1 = new Uint8Array(count);
|
|
4035
|
+
const p2 = new Uint8Array(count);
|
|
4036
|
+
let prev = 0;
|
|
4037
|
+
for (let i = 0; i < count; i++) {
|
|
4038
|
+
const bits = view.getUint32(rawPtr, true);
|
|
4039
|
+
rawPtr += 4;
|
|
4040
|
+
const pixel24 = floatBitsToFloat24Bits(bits);
|
|
4041
|
+
const diff = pixel24 - prev >>> 0;
|
|
4042
|
+
prev = pixel24;
|
|
4043
|
+
p0[i] = diff >>> 16 & 255;
|
|
4044
|
+
p1[i] = diff >>> 8 & 255;
|
|
4045
|
+
p2[i] = diff & 255;
|
|
4046
|
+
}
|
|
4047
|
+
out.push(...p0, ...p1, ...p2);
|
|
4048
|
+
}
|
|
4049
|
+
}
|
|
4050
|
+
if (rawPtr !== raw.byteLength) {
|
|
4051
|
+
throw new ExrError("ENCODING_FAILED", "PXR24 encoder consumed an unexpected raw block length.", {
|
|
4052
|
+
consumed: rawPtr,
|
|
4053
|
+
expected: raw.byteLength
|
|
4054
|
+
});
|
|
4055
|
+
}
|
|
4056
|
+
const packed = Uint8Array.from(out);
|
|
4057
|
+
const encoded = (0, import_fflate4.zlibSync)(packed);
|
|
4058
|
+
return encoded.byteLength >= raw.byteLength ? raw : encoded;
|
|
4059
|
+
}
|
|
4060
|
+
|
|
4061
|
+
// src/writer/compression/zipPredictor.ts
|
|
4062
|
+
function interleave(data) {
|
|
4063
|
+
const length = data.length;
|
|
4064
|
+
const half = Math.floor((length + 1) / 2);
|
|
4065
|
+
const out = new Uint8Array(length);
|
|
4066
|
+
let even = 0;
|
|
4067
|
+
let odd = half;
|
|
4068
|
+
for (let i = 0; i < length; i++) {
|
|
4069
|
+
if ((i & 1) === 0) {
|
|
4070
|
+
out[even++] = data[i];
|
|
4071
|
+
} else {
|
|
4072
|
+
out[odd++] = data[i];
|
|
4073
|
+
}
|
|
4074
|
+
}
|
|
4075
|
+
return out;
|
|
4076
|
+
}
|
|
4077
|
+
function applyPredictor(data) {
|
|
4078
|
+
if (data.length === 0) return data;
|
|
4079
|
+
const out = new Uint8Array(data.length);
|
|
4080
|
+
out[0] = data[0];
|
|
4081
|
+
for (let i = 1; i < data.length; i++) {
|
|
4082
|
+
out[i] = data[i] - data[i - 1] + 128 & 255;
|
|
4083
|
+
}
|
|
4084
|
+
return out;
|
|
4085
|
+
}
|
|
4086
|
+
|
|
4087
|
+
// src/writer/compression/rle.ts
|
|
4088
|
+
function rleCompress(data) {
|
|
4089
|
+
const out = [];
|
|
4090
|
+
const end = data.length;
|
|
4091
|
+
let runs = 0;
|
|
4092
|
+
let rune = runs + 1;
|
|
4093
|
+
while (runs < end) {
|
|
4094
|
+
let count = 0;
|
|
4095
|
+
while (rune < end && data[runs] === data[rune] && count < 127) {
|
|
4096
|
+
rune++;
|
|
4097
|
+
count++;
|
|
4098
|
+
}
|
|
4099
|
+
if (count >= 2) {
|
|
4100
|
+
out.push(count & 255);
|
|
4101
|
+
out.push(data[runs]);
|
|
4102
|
+
runs = rune;
|
|
4103
|
+
} else {
|
|
4104
|
+
count++;
|
|
4105
|
+
while (rune < end && (rune + 1 >= end || data[rune] !== data[rune + 1] || rune + 2 >= end || data[rune + 1] !== data[rune + 2]) && count < 127) {
|
|
4106
|
+
count++;
|
|
4107
|
+
rune++;
|
|
4108
|
+
}
|
|
4109
|
+
out.push(-count & 255);
|
|
4110
|
+
while (runs < rune) {
|
|
4111
|
+
out.push(data[runs++]);
|
|
4112
|
+
}
|
|
4113
|
+
}
|
|
4114
|
+
rune++;
|
|
4115
|
+
}
|
|
4116
|
+
return Uint8Array.from(out);
|
|
4117
|
+
}
|
|
4118
|
+
function encodeRleChunk(raw) {
|
|
4119
|
+
const encoded = rleCompress(applyPredictor(interleave(raw)));
|
|
4120
|
+
return encoded.byteLength >= raw.byteLength ? raw : encoded;
|
|
4121
|
+
}
|
|
4122
|
+
|
|
4123
|
+
// src/writer/compression/zip.ts
|
|
4124
|
+
var import_fflate5 = require("fflate");
|
|
4125
|
+
function encodeZipChunk(raw) {
|
|
4126
|
+
const encoded = (0, import_fflate5.zlibSync)(applyPredictor(interleave(raw)));
|
|
4127
|
+
return encoded.byteLength >= raw.byteLength ? raw : encoded;
|
|
4128
|
+
}
|
|
4129
|
+
|
|
4130
|
+
// src/writer/compression/handlers.ts
|
|
4131
|
+
var NO_COMPRESSION = 0;
|
|
4132
|
+
var RLE_COMPRESSION = 1;
|
|
4133
|
+
var ZIPS_COMPRESSION = 2;
|
|
4134
|
+
var ZIP_COMPRESSION = 3;
|
|
4135
|
+
var PIZ_COMPRESSION = 4;
|
|
4136
|
+
var PXR24_COMPRESSION = 5;
|
|
4137
|
+
var B44_COMPRESSION = 6;
|
|
4138
|
+
var B44A_COMPRESSION2 = 7;
|
|
4139
|
+
var SUPPORTED_WRITE_COMPRESSION_TEXT = "NO/RLE/ZIPS/ZIP/PIZ/PXR24/B44/B44A";
|
|
4140
|
+
var WRITE_COMPRESSION_HANDLERS = /* @__PURE__ */ new Map([
|
|
4141
|
+
[
|
|
4142
|
+
NO_COMPRESSION,
|
|
4143
|
+
{
|
|
4144
|
+
compressionId: NO_COMPRESSION,
|
|
4145
|
+
name: "NO_COMPRESSION",
|
|
4146
|
+
linesPerBlock: 1,
|
|
4147
|
+
encodeChunk: (raw) => raw
|
|
4148
|
+
}
|
|
4149
|
+
],
|
|
4150
|
+
[
|
|
4151
|
+
RLE_COMPRESSION,
|
|
4152
|
+
{
|
|
4153
|
+
compressionId: RLE_COMPRESSION,
|
|
4154
|
+
name: "RLE_COMPRESSION",
|
|
4155
|
+
linesPerBlock: 1,
|
|
4156
|
+
encodeChunk: (raw) => encodeRleChunk(raw)
|
|
4157
|
+
}
|
|
4158
|
+
],
|
|
4159
|
+
[
|
|
4160
|
+
ZIPS_COMPRESSION,
|
|
4161
|
+
{
|
|
4162
|
+
compressionId: ZIPS_COMPRESSION,
|
|
4163
|
+
name: "ZIPS_COMPRESSION",
|
|
4164
|
+
linesPerBlock: 1,
|
|
4165
|
+
encodeChunk: (raw) => encodeZipChunk(raw)
|
|
4166
|
+
}
|
|
4167
|
+
],
|
|
4168
|
+
[
|
|
4169
|
+
ZIP_COMPRESSION,
|
|
4170
|
+
{
|
|
4171
|
+
compressionId: ZIP_COMPRESSION,
|
|
4172
|
+
name: "ZIP_COMPRESSION",
|
|
4173
|
+
linesPerBlock: 16,
|
|
4174
|
+
encodeChunk: (raw) => encodeZipChunk(raw)
|
|
4175
|
+
}
|
|
4176
|
+
],
|
|
4177
|
+
[
|
|
4178
|
+
PIZ_COMPRESSION,
|
|
4179
|
+
{
|
|
4180
|
+
compressionId: PIZ_COMPRESSION,
|
|
4181
|
+
name: "PIZ_COMPRESSION",
|
|
4182
|
+
linesPerBlock: 32,
|
|
4183
|
+
encodeChunk: (raw, part, chunkY) => encodePizChunk(raw, part, chunkY)
|
|
4184
|
+
}
|
|
4185
|
+
],
|
|
4186
|
+
[
|
|
4187
|
+
PXR24_COMPRESSION,
|
|
4188
|
+
{
|
|
4189
|
+
compressionId: PXR24_COMPRESSION,
|
|
4190
|
+
name: "PXR24_COMPRESSION",
|
|
4191
|
+
linesPerBlock: 16,
|
|
4192
|
+
encodeChunk: (raw, part, chunkY) => encodePxr24Chunk(raw, part, chunkY)
|
|
4193
|
+
}
|
|
4194
|
+
],
|
|
4195
|
+
[
|
|
4196
|
+
B44_COMPRESSION,
|
|
4197
|
+
{
|
|
4198
|
+
compressionId: B44_COMPRESSION,
|
|
4199
|
+
name: "B44_COMPRESSION",
|
|
4200
|
+
linesPerBlock: 32,
|
|
4201
|
+
encodeChunk: (raw, part, chunkY) => encodeB44Chunk(raw, part, chunkY)
|
|
4202
|
+
}
|
|
4203
|
+
],
|
|
4204
|
+
[
|
|
4205
|
+
B44A_COMPRESSION2,
|
|
4206
|
+
{
|
|
4207
|
+
compressionId: B44A_COMPRESSION2,
|
|
4208
|
+
name: "B44A_COMPRESSION",
|
|
4209
|
+
linesPerBlock: 32,
|
|
4210
|
+
encodeChunk: (raw, part, chunkY) => encodeB44Chunk(raw, part, chunkY)
|
|
4211
|
+
}
|
|
4212
|
+
]
|
|
4213
|
+
]);
|
|
4214
|
+
function getWriteCompressionHandler(compression) {
|
|
4215
|
+
return WRITE_COMPRESSION_HANDLERS.get(compression);
|
|
4216
|
+
}
|
|
4217
|
+
|
|
4218
|
+
// src/writer/writeExr.ts
|
|
4219
|
+
var EXR_VERSION = 2;
|
|
4220
|
+
var EXR_MULTIPART_FLAG = 16;
|
|
4221
|
+
var UINT32_MAX2 = 4294967295;
|
|
4222
|
+
var UTF8_ENCODER = new TextEncoder();
|
|
4223
|
+
var ByteWriter = class {
|
|
4224
|
+
bytes = [];
|
|
4225
|
+
get length() {
|
|
4226
|
+
return this.bytes.length;
|
|
4227
|
+
}
|
|
4228
|
+
writeUint8(value) {
|
|
4229
|
+
this.bytes.push(value & 255);
|
|
4230
|
+
}
|
|
4231
|
+
writeInt32(value) {
|
|
4232
|
+
const v = value | 0;
|
|
4233
|
+
this.bytes.push(v & 255, v >> 8 & 255, v >> 16 & 255, v >> 24 & 255);
|
|
4234
|
+
}
|
|
4235
|
+
writeUint32(value) {
|
|
4236
|
+
const v = value >>> 0;
|
|
4237
|
+
this.bytes.push(v & 255, v >> 8 & 255, v >> 16 & 255, v >> 24 & 255);
|
|
4238
|
+
}
|
|
4239
|
+
writeBytes(bytes) {
|
|
4240
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
4241
|
+
this.bytes.push(bytes[i]);
|
|
4242
|
+
}
|
|
4243
|
+
}
|
|
4244
|
+
writeCString(text) {
|
|
4245
|
+
const encoded = UTF8_ENCODER.encode(text);
|
|
4246
|
+
this.writeBytes(encoded);
|
|
4247
|
+
this.writeUint8(0);
|
|
4248
|
+
}
|
|
4249
|
+
patchUint64LE(offset, value) {
|
|
4250
|
+
if (!Number.isFinite(value) || value < 0) {
|
|
4251
|
+
throw new ExrError("ENCODING_FAILED", "Attempted to patch an invalid chunk offset.", {
|
|
4252
|
+
offset
|
|
4253
|
+
});
|
|
4254
|
+
}
|
|
4255
|
+
const low = value >>> 0;
|
|
4256
|
+
const high = Math.floor(value / 4294967296) >>> 0;
|
|
4257
|
+
this.patchUint32LE(offset, low);
|
|
4258
|
+
this.patchUint32LE(offset + 4, high);
|
|
4259
|
+
}
|
|
4260
|
+
toUint8Array() {
|
|
4261
|
+
return Uint8Array.from(this.bytes);
|
|
4262
|
+
}
|
|
4263
|
+
patchUint32LE(offset, value) {
|
|
4264
|
+
if (offset < 0 || offset + 4 > this.bytes.length) {
|
|
4265
|
+
throw new ExrError("ENCODING_FAILED", "Failed to patch bytes outside output bounds.", {
|
|
4266
|
+
offset,
|
|
4267
|
+
size: this.bytes.length
|
|
4268
|
+
});
|
|
4269
|
+
}
|
|
4270
|
+
const v = value >>> 0;
|
|
4271
|
+
this.bytes[offset + 0] = v & 255;
|
|
4272
|
+
this.bytes[offset + 1] = v >> 8 & 255;
|
|
4273
|
+
this.bytes[offset + 2] = v >> 16 & 255;
|
|
4274
|
+
this.bytes[offset + 3] = v >> 24 & 255;
|
|
4275
|
+
}
|
|
4276
|
+
};
|
|
4277
|
+
function nowMs3() {
|
|
4278
|
+
return typeof performance !== "undefined" ? performance.now() : Date.now();
|
|
4279
|
+
}
|
|
4280
|
+
function emit3(onEvent, event) {
|
|
4281
|
+
onEvent?.(event);
|
|
4282
|
+
}
|
|
4283
|
+
function ensureFiniteInteger(value, field, details) {
|
|
4284
|
+
if (!Number.isFinite(value) || !Number.isInteger(value)) {
|
|
4285
|
+
throw new ExrError("INVALID_WRITE_INPUT", `Expected integer value for ${field}.`, details);
|
|
4286
|
+
}
|
|
4287
|
+
}
|
|
4288
|
+
function ensureWindow(window, field, partId) {
|
|
4289
|
+
ensureFiniteInteger(window.xMin, `${field}.xMin`, { partId });
|
|
4290
|
+
ensureFiniteInteger(window.yMin, `${field}.yMin`, { partId });
|
|
4291
|
+
ensureFiniteInteger(window.xMax, `${field}.xMax`, { partId });
|
|
4292
|
+
ensureFiniteInteger(window.yMax, `${field}.yMax`, { partId });
|
|
4293
|
+
if (window.xMax < window.xMin || window.yMax < window.yMin) {
|
|
4294
|
+
throw new ExrError("INVALID_WRITE_INPUT", `${field} has invalid extents.`, {
|
|
4295
|
+
partId,
|
|
4296
|
+
xMin: window.xMin,
|
|
4297
|
+
yMin: window.yMin,
|
|
4298
|
+
xMax: window.xMax,
|
|
4299
|
+
yMax: window.yMax
|
|
4300
|
+
});
|
|
4301
|
+
}
|
|
4302
|
+
return window;
|
|
4303
|
+
}
|
|
4304
|
+
function unsupportedCompressionError(compression) {
|
|
4305
|
+
return new ExrError(
|
|
4306
|
+
"UNSUPPORTED_WRITE_COMPRESSION",
|
|
4307
|
+
`Writer currently supports ${SUPPORTED_WRITE_COMPRESSION_TEXT} compression only.`,
|
|
4308
|
+
{ compression }
|
|
4309
|
+
);
|
|
4310
|
+
}
|
|
4311
|
+
function normalizeChannel(channel, partId, dataWindow) {
|
|
4312
|
+
if (!channel.name) {
|
|
4313
|
+
throw new ExrError("INVALID_WRITE_INPUT", "Channel name must be non-empty.", { partId });
|
|
4314
|
+
}
|
|
4315
|
+
const pixelType = channel.pixelType;
|
|
4316
|
+
if (pixelType !== 0 && pixelType !== 1 && pixelType !== 2) {
|
|
4317
|
+
throw new ExrError("INVALID_WRITE_INPUT", `Unsupported channel pixel type for ${channel.name}.`, {
|
|
4318
|
+
partId
|
|
4319
|
+
});
|
|
4320
|
+
}
|
|
4321
|
+
const xSampling = channel.xSampling ?? 1;
|
|
4322
|
+
const ySampling = channel.ySampling ?? 1;
|
|
4323
|
+
ensureFiniteInteger(xSampling, `${channel.name}.xSampling`, { partId });
|
|
4324
|
+
ensureFiniteInteger(ySampling, `${channel.name}.ySampling`, { partId });
|
|
4325
|
+
if (xSampling <= 0 || ySampling <= 0) {
|
|
4326
|
+
throw new ExrError("INVALID_WRITE_INPUT", `Sampling must be positive for ${channel.name}.`, {
|
|
4327
|
+
partId
|
|
4328
|
+
});
|
|
4329
|
+
}
|
|
4330
|
+
const pLinear = channel.pLinear ?? 0;
|
|
4331
|
+
ensureFiniteInteger(pLinear, `${channel.name}.pLinear`, { partId });
|
|
4332
|
+
const sampleOriginX = firstSampleCoordinate5(dataWindow.xMin, xSampling);
|
|
4333
|
+
const sampleOriginY = firstSampleCoordinate5(dataWindow.yMin, ySampling);
|
|
4334
|
+
const sampledWidth = countSamplesInRange5(dataWindow.xMin, dataWindow.xMax, xSampling);
|
|
4335
|
+
const sampledHeight = countSamplesInRange5(dataWindow.yMin, dataWindow.yMax, ySampling);
|
|
4336
|
+
const expectedLength = sampledWidth * sampledHeight;
|
|
4337
|
+
if (!(channel.data instanceof Float32Array)) {
|
|
4338
|
+
throw new ExrError("INVALID_WRITE_INPUT", `Channel ${channel.name} data must be Float32Array.`, {
|
|
4339
|
+
partId
|
|
4340
|
+
});
|
|
4341
|
+
}
|
|
4342
|
+
if (channel.data.length !== expectedLength) {
|
|
4343
|
+
throw new ExrError("INVALID_WRITE_INPUT", `Channel ${channel.name} has invalid data length.`, {
|
|
4344
|
+
partId,
|
|
4345
|
+
expected: expectedLength,
|
|
4346
|
+
actual: channel.data.length
|
|
4347
|
+
});
|
|
4348
|
+
}
|
|
4349
|
+
const bytesPerSample = pixelType === 1 ? 2 : 4;
|
|
4350
|
+
return {
|
|
4351
|
+
name: channel.name,
|
|
4352
|
+
pixelType,
|
|
4353
|
+
pLinear,
|
|
4354
|
+
xSampling,
|
|
4355
|
+
ySampling,
|
|
4356
|
+
sampleOriginX,
|
|
4357
|
+
sampleOriginY,
|
|
4358
|
+
sampledWidth,
|
|
4359
|
+
sampledHeight,
|
|
4360
|
+
rowByteLength: sampledWidth * bytesPerSample,
|
|
4361
|
+
data: channel.data
|
|
4362
|
+
};
|
|
4363
|
+
}
|
|
4364
|
+
function normalizeParts(input) {
|
|
4365
|
+
if (!input || !Array.isArray(input.parts) || input.parts.length === 0) {
|
|
4366
|
+
throw new ExrError("INVALID_WRITE_INPUT", "writeExr requires at least one part.", {});
|
|
4367
|
+
}
|
|
4368
|
+
const isMultipart = input.parts.length > 1;
|
|
4369
|
+
return input.parts.map((part, index) => {
|
|
4370
|
+
if (!part || !Array.isArray(part.channels) || part.channels.length === 0) {
|
|
4371
|
+
throw new ExrError("INVALID_WRITE_INPUT", "Each part must contain at least one channel.", {
|
|
4372
|
+
partId: index
|
|
4373
|
+
});
|
|
4374
|
+
}
|
|
4375
|
+
const compression = part.compression;
|
|
4376
|
+
const compressionHandler = getWriteCompressionHandler(compression);
|
|
4377
|
+
if (!compressionHandler) {
|
|
4378
|
+
throw unsupportedCompressionError(compression);
|
|
4379
|
+
}
|
|
4380
|
+
const linesPerBlock = compressionHandler.linesPerBlock;
|
|
4381
|
+
const dataWindow = ensureWindow(part.dataWindow, "dataWindow", index);
|
|
4382
|
+
const displayWindow = ensureWindow(part.displayWindow ?? dataWindow, "displayWindow", index);
|
|
4383
|
+
const channels = part.channels.map((channel) => normalizeChannel(channel, index, dataWindow));
|
|
4384
|
+
const height = dataWindow.yMax - dataWindow.yMin + 1;
|
|
4385
|
+
const chunkCount = Math.ceil(height / linesPerBlock);
|
|
4386
|
+
const includeNameAttribute = isMultipart || typeof part.name === "string";
|
|
4387
|
+
const includeTypeAttribute = isMultipart || typeof part.type === "string";
|
|
4388
|
+
return {
|
|
4389
|
+
id: index,
|
|
4390
|
+
compression,
|
|
4391
|
+
dataWindow,
|
|
4392
|
+
displayWindow,
|
|
4393
|
+
name: part.name ?? `part_${index}`,
|
|
4394
|
+
type: part.type ?? "scanlineimage",
|
|
4395
|
+
channels,
|
|
4396
|
+
linesPerBlock,
|
|
4397
|
+
chunkCount,
|
|
4398
|
+
includeNameAttribute,
|
|
4399
|
+
includeTypeAttribute
|
|
4400
|
+
};
|
|
4401
|
+
});
|
|
4402
|
+
}
|
|
4403
|
+
function writeAttribute(writer, name, type, payload) {
|
|
4404
|
+
writer.writeCString(name);
|
|
4405
|
+
writer.writeCString(type);
|
|
4406
|
+
writer.writeInt32(payload.byteLength);
|
|
4407
|
+
writer.writeBytes(payload);
|
|
4408
|
+
}
|
|
4409
|
+
function writeInt32Payload(values) {
|
|
4410
|
+
const out = new Uint8Array(values.length * 4);
|
|
4411
|
+
const view = new DataView(out.buffer);
|
|
4412
|
+
for (let i = 0; i < values.length; i++) {
|
|
4413
|
+
view.setInt32(i * 4, values[i], true);
|
|
4414
|
+
}
|
|
4415
|
+
return out;
|
|
4416
|
+
}
|
|
4417
|
+
function writeCompressionPayload(compression) {
|
|
4418
|
+
return Uint8Array.of(compression & 255);
|
|
4419
|
+
}
|
|
4420
|
+
function writeStringPayload(value) {
|
|
4421
|
+
return UTF8_ENCODER.encode(value);
|
|
4422
|
+
}
|
|
4423
|
+
function writeChannelsPayload(channels) {
|
|
4424
|
+
const writer = new ByteWriter();
|
|
4425
|
+
for (const channel of channels) {
|
|
4426
|
+
writer.writeCString(channel.name);
|
|
4427
|
+
writer.writeInt32(channel.pixelType);
|
|
4428
|
+
writer.writeUint8(channel.pLinear);
|
|
4429
|
+
writer.writeUint8(0);
|
|
4430
|
+
writer.writeUint8(0);
|
|
4431
|
+
writer.writeUint8(0);
|
|
4432
|
+
writer.writeInt32(channel.xSampling);
|
|
4433
|
+
writer.writeInt32(channel.ySampling);
|
|
4434
|
+
}
|
|
4435
|
+
writer.writeUint8(0);
|
|
4436
|
+
return writer.toUint8Array();
|
|
4437
|
+
}
|
|
4438
|
+
function writePartHeader(writer, part) {
|
|
4439
|
+
writeAttribute(writer, "channels", "chlist", writeChannelsPayload(part.channels));
|
|
4440
|
+
writeAttribute(writer, "compression", "compression", writeCompressionPayload(part.compression));
|
|
4441
|
+
writeAttribute(
|
|
4442
|
+
writer,
|
|
4443
|
+
"dataWindow",
|
|
4444
|
+
"box2i",
|
|
4445
|
+
writeInt32Payload([
|
|
4446
|
+
part.dataWindow.xMin,
|
|
4447
|
+
part.dataWindow.yMin,
|
|
4448
|
+
part.dataWindow.xMax,
|
|
4449
|
+
part.dataWindow.yMax
|
|
4450
|
+
])
|
|
4451
|
+
);
|
|
4452
|
+
writeAttribute(
|
|
4453
|
+
writer,
|
|
4454
|
+
"displayWindow",
|
|
4455
|
+
"box2i",
|
|
4456
|
+
writeInt32Payload([
|
|
4457
|
+
part.displayWindow.xMin,
|
|
4458
|
+
part.displayWindow.yMin,
|
|
4459
|
+
part.displayWindow.xMax,
|
|
4460
|
+
part.displayWindow.yMax
|
|
4461
|
+
])
|
|
4462
|
+
);
|
|
4463
|
+
writeAttribute(writer, "lineOrder", "lineOrder", Uint8Array.of(0));
|
|
4464
|
+
if (part.includeNameAttribute) {
|
|
4465
|
+
writeAttribute(writer, "name", "string", writeStringPayload(part.name));
|
|
4466
|
+
}
|
|
4467
|
+
if (part.includeTypeAttribute) {
|
|
4468
|
+
writeAttribute(writer, "type", "string", writeStringPayload(part.type));
|
|
4469
|
+
}
|
|
4470
|
+
writer.writeUint8(0);
|
|
4471
|
+
}
|
|
4472
|
+
function getRawChunkSize(part, chunkY, linesInChunk) {
|
|
4473
|
+
let expected = 0;
|
|
4474
|
+
for (let dy = 0; dy < linesInChunk; dy++) {
|
|
4475
|
+
const y = chunkY + dy;
|
|
4476
|
+
for (const channel of part.channels) {
|
|
4477
|
+
if (!isSampledCoordinate5(y, channel.sampleOriginY, channel.ySampling)) {
|
|
4478
|
+
continue;
|
|
4479
|
+
}
|
|
4480
|
+
expected += channel.rowByteLength;
|
|
4481
|
+
}
|
|
4482
|
+
}
|
|
4483
|
+
return expected;
|
|
4484
|
+
}
|
|
4485
|
+
function clamp01(value) {
|
|
4486
|
+
if (!Number.isFinite(value)) return 0;
|
|
4487
|
+
if (value < 0) return 0;
|
|
4488
|
+
if (value > 1) return 1;
|
|
4489
|
+
return value;
|
|
4490
|
+
}
|
|
4491
|
+
function buildRawChunk(part, chunkY) {
|
|
4492
|
+
const linesInChunk = Math.max(0, Math.min(part.linesPerBlock, part.dataWindow.yMax - chunkY + 1));
|
|
4493
|
+
const raw = new Uint8Array(getRawChunkSize(part, chunkY, linesInChunk));
|
|
4494
|
+
const view = new DataView(raw.buffer);
|
|
4495
|
+
let ptr = 0;
|
|
4496
|
+
for (let dy = 0; dy < linesInChunk; dy++) {
|
|
4497
|
+
const y = chunkY + dy;
|
|
4498
|
+
for (const channel of part.channels) {
|
|
4499
|
+
if (!isSampledCoordinate5(y, channel.sampleOriginY, channel.ySampling)) {
|
|
4500
|
+
continue;
|
|
4501
|
+
}
|
|
4502
|
+
const sampledRow = Math.floor((y - channel.sampleOriginY) / channel.ySampling);
|
|
4503
|
+
const rowOffset = sampledRow * channel.sampledWidth;
|
|
4504
|
+
for (let x = 0; x < channel.sampledWidth; x++) {
|
|
4505
|
+
const value = channel.data[rowOffset + x];
|
|
4506
|
+
if (channel.pixelType === 1) {
|
|
4507
|
+
view.setUint16(ptr, float32ToFloat16(value), true);
|
|
4508
|
+
ptr += 2;
|
|
4509
|
+
continue;
|
|
4510
|
+
}
|
|
4511
|
+
if (channel.pixelType === 2) {
|
|
4512
|
+
view.setFloat32(ptr, value, true);
|
|
4513
|
+
ptr += 4;
|
|
4514
|
+
continue;
|
|
4515
|
+
}
|
|
4516
|
+
const quantized = Math.round(clamp01(value) * UINT32_MAX2) >>> 0;
|
|
4517
|
+
view.setUint32(ptr, quantized, true);
|
|
4518
|
+
ptr += 4;
|
|
4519
|
+
}
|
|
4520
|
+
}
|
|
4521
|
+
}
|
|
4522
|
+
return raw;
|
|
4523
|
+
}
|
|
4524
|
+
function encodeChunk(raw, part, chunkY, chunkIndex) {
|
|
4525
|
+
const compressionHandler = getWriteCompressionHandler(part.compression);
|
|
4526
|
+
if (!compressionHandler) {
|
|
4527
|
+
throw unsupportedCompressionError(part.compression);
|
|
4528
|
+
}
|
|
4529
|
+
try {
|
|
4530
|
+
return compressionHandler.encodeChunk(raw, part, chunkY);
|
|
4531
|
+
} catch {
|
|
4532
|
+
throw new ExrError("ENCODING_FAILED", "Failed to compress EXR chunk payload.", {
|
|
4533
|
+
partId: part.id,
|
|
4534
|
+
chunkIndex,
|
|
4535
|
+
compression: part.compression
|
|
4536
|
+
});
|
|
4537
|
+
}
|
|
4538
|
+
}
|
|
4539
|
+
function writeExr(input, options = {}) {
|
|
4540
|
+
const onEvent = options.onEvent;
|
|
4541
|
+
const t0 = nowMs3();
|
|
4542
|
+
const parts = normalizeParts(input);
|
|
4543
|
+
const isMultipart = parts.length > 1;
|
|
4544
|
+
emit3(onEvent, {
|
|
4545
|
+
phase: "encode",
|
|
4546
|
+
level: "info",
|
|
4547
|
+
code: "encode.setup",
|
|
4548
|
+
message: "Writer setup complete.",
|
|
4549
|
+
metrics: {
|
|
4550
|
+
parts: parts.length,
|
|
4551
|
+
multipart: isMultipart ? 1 : 0
|
|
4552
|
+
}
|
|
4553
|
+
});
|
|
4554
|
+
const writer = new ByteWriter();
|
|
4555
|
+
writer.writeUint32(EXR_MAGIC);
|
|
4556
|
+
const versionField = EXR_VERSION | (isMultipart ? EXR_MULTIPART_FLAG : 0) << 8;
|
|
4557
|
+
writer.writeUint32(versionField);
|
|
4558
|
+
for (const part of parts) {
|
|
4559
|
+
writePartHeader(writer, part);
|
|
4560
|
+
}
|
|
4561
|
+
if (isMultipart) {
|
|
4562
|
+
writer.writeUint8(0);
|
|
4563
|
+
}
|
|
4564
|
+
const offsetSlots = parts.map((part) => {
|
|
4565
|
+
const offsets = [];
|
|
4566
|
+
for (let chunk = 0; chunk < part.chunkCount; chunk++) {
|
|
4567
|
+
offsets.push(writer.length);
|
|
4568
|
+
writer.writeUint32(0);
|
|
4569
|
+
writer.writeUint32(0);
|
|
4570
|
+
}
|
|
4571
|
+
return offsets;
|
|
4572
|
+
});
|
|
4573
|
+
for (const part of parts) {
|
|
4574
|
+
const tPart = nowMs3();
|
|
4575
|
+
for (let chunkIndex = 0; chunkIndex < part.chunkCount; chunkIndex++) {
|
|
4576
|
+
const chunkOffset = writer.length;
|
|
4577
|
+
writer.patchUint64LE(offsetSlots[part.id][chunkIndex], chunkOffset);
|
|
4578
|
+
const chunkY = part.dataWindow.yMin + chunkIndex * part.linesPerBlock;
|
|
4579
|
+
const raw = buildRawChunk(part, chunkY);
|
|
4580
|
+
const encoded = encodeChunk(raw, part, chunkY, chunkIndex);
|
|
4581
|
+
if (isMultipart) {
|
|
4582
|
+
writer.writeInt32(part.id);
|
|
4583
|
+
}
|
|
4584
|
+
writer.writeInt32(chunkY);
|
|
4585
|
+
writer.writeInt32(encoded.byteLength);
|
|
4586
|
+
writer.writeBytes(encoded);
|
|
4587
|
+
}
|
|
4588
|
+
emit3(onEvent, {
|
|
4589
|
+
phase: "encode",
|
|
4590
|
+
level: "info",
|
|
4591
|
+
code: "encode.part.complete",
|
|
4592
|
+
message: `Encoded part ${part.id}.`,
|
|
4593
|
+
metrics: {
|
|
4594
|
+
partId: part.id,
|
|
4595
|
+
chunks: part.chunkCount,
|
|
4596
|
+
compression: part.compression,
|
|
4597
|
+
ms: (nowMs3() - tPart).toFixed(3)
|
|
4598
|
+
}
|
|
4599
|
+
});
|
|
4600
|
+
}
|
|
4601
|
+
const out = writer.toUint8Array();
|
|
4602
|
+
emit3(onEvent, {
|
|
4603
|
+
phase: "encode",
|
|
4604
|
+
level: "info",
|
|
4605
|
+
code: "encode.complete",
|
|
4606
|
+
message: "EXR encode complete.",
|
|
4607
|
+
metrics: {
|
|
4608
|
+
parts: parts.length,
|
|
4609
|
+
bytes: out.byteLength,
|
|
4610
|
+
totalMs: (nowMs3() - t0).toFixed(3)
|
|
4611
|
+
}
|
|
4612
|
+
});
|
|
4613
|
+
return out;
|
|
4614
|
+
}
|
|
4615
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
4616
|
+
0 && (module.exports = {
|
|
4617
|
+
EXR_EVENT_CODES,
|
|
4618
|
+
ExrError,
|
|
4619
|
+
decodeExrPart,
|
|
4620
|
+
isExrError,
|
|
4621
|
+
parseExr,
|
|
4622
|
+
parseExrStructure,
|
|
4623
|
+
readExr,
|
|
4624
|
+
toArrayBuffer,
|
|
4625
|
+
writeExr
|
|
4626
|
+
});
|
|
4627
|
+
//# sourceMappingURL=index.cjs.map
|