@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
|
@@ -0,0 +1,3170 @@
|
|
|
1
|
+
// src/shared/constants.ts
|
|
2
|
+
var COMPRESSION_NAMES = {
|
|
3
|
+
0: "NO_COMPRESSION",
|
|
4
|
+
1: "RLE_COMPRESSION",
|
|
5
|
+
2: "ZIPS_COMPRESSION",
|
|
6
|
+
3: "ZIP_COMPRESSION",
|
|
7
|
+
4: "PIZ_COMPRESSION",
|
|
8
|
+
5: "PXR24_COMPRESSION",
|
|
9
|
+
6: "B44_COMPRESSION",
|
|
10
|
+
7: "B44A_COMPRESSION",
|
|
11
|
+
8: "DWAA_COMPRESSION",
|
|
12
|
+
9: "DWAB_COMPRESSION"
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
// src/shared/errors.ts
|
|
16
|
+
var ExrError = class extends Error {
|
|
17
|
+
code;
|
|
18
|
+
details;
|
|
19
|
+
constructor(code, message, details) {
|
|
20
|
+
super(message);
|
|
21
|
+
this.name = "ExrError";
|
|
22
|
+
this.code = code;
|
|
23
|
+
this.details = details;
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
// src/shared/half.ts
|
|
28
|
+
function createHalfToFloatTable() {
|
|
29
|
+
const exponentTable = new Uint32Array(64);
|
|
30
|
+
const offsetTable = new Uint32Array(64);
|
|
31
|
+
const mantissaTable = new Uint32Array(2048);
|
|
32
|
+
const table = new Float32Array(65536);
|
|
33
|
+
exponentTable[0] = 0;
|
|
34
|
+
for (let i = 1; i < 31; i++) {
|
|
35
|
+
exponentTable[i] = i << 23;
|
|
36
|
+
}
|
|
37
|
+
exponentTable[31] = 1199570944;
|
|
38
|
+
exponentTable[32] = 2147483648;
|
|
39
|
+
for (let i = 33; i < 63; i++) {
|
|
40
|
+
exponentTable[i] = 2147483648 + (i - 32 << 23);
|
|
41
|
+
}
|
|
42
|
+
exponentTable[63] = 3347054592;
|
|
43
|
+
offsetTable[0] = 0;
|
|
44
|
+
for (let i = 1; i < 64; i++) {
|
|
45
|
+
offsetTable[i] = i === 32 ? 0 : 1024;
|
|
46
|
+
}
|
|
47
|
+
mantissaTable[0] = 0;
|
|
48
|
+
for (let i = 1; i < 1024; i++) {
|
|
49
|
+
let m = i << 13;
|
|
50
|
+
let e = 0;
|
|
51
|
+
while ((m & 8388608) === 0) {
|
|
52
|
+
e -= 8388608;
|
|
53
|
+
m <<= 1;
|
|
54
|
+
}
|
|
55
|
+
m &= ~8388608;
|
|
56
|
+
e += 947912704;
|
|
57
|
+
mantissaTable[i] = m | e;
|
|
58
|
+
}
|
|
59
|
+
for (let i = 1024; i < 2048; i++) {
|
|
60
|
+
mantissaTable[i] = 939524096 + (i - 1024 << 13);
|
|
61
|
+
}
|
|
62
|
+
const scratch = new ArrayBuffer(4);
|
|
63
|
+
const floatView2 = new Float32Array(scratch);
|
|
64
|
+
const uintView2 = new Uint32Array(scratch);
|
|
65
|
+
for (let h = 0; h < 65536; h++) {
|
|
66
|
+
const offset = offsetTable[h >> 10];
|
|
67
|
+
const mantissa = mantissaTable[offset + (h & 1023)];
|
|
68
|
+
const exponent = exponentTable[h >> 10];
|
|
69
|
+
uintView2[0] = mantissa + exponent;
|
|
70
|
+
table[h] = floatView2[0];
|
|
71
|
+
}
|
|
72
|
+
return table;
|
|
73
|
+
}
|
|
74
|
+
var HALF_TO_FLOAT_TABLE = createHalfToFloatTable();
|
|
75
|
+
function float16ToFloat32(h) {
|
|
76
|
+
return HALF_TO_FLOAT_TABLE[h & 65535];
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// src/reader/compression/b44.ts
|
|
80
|
+
var HALF_VALUES_PER_BLOCK = 16;
|
|
81
|
+
var B44_BLOCK_SIZE = 14;
|
|
82
|
+
var B44A_BLOCK_SIZE = 3;
|
|
83
|
+
var B44A_MARKER_THRESHOLD = 13 << 2;
|
|
84
|
+
var floatScratch = new ArrayBuffer(4);
|
|
85
|
+
var floatView = new Float32Array(floatScratch);
|
|
86
|
+
var uintView = new Uint32Array(floatScratch);
|
|
87
|
+
var b44ToLinearTable = null;
|
|
88
|
+
function modulo(value, base) {
|
|
89
|
+
const result = value % base;
|
|
90
|
+
return result < 0 ? result + base : result;
|
|
91
|
+
}
|
|
92
|
+
function firstSampleCoordinate(min, sampling) {
|
|
93
|
+
if (sampling <= 1) return min;
|
|
94
|
+
const remainder = modulo(min, sampling);
|
|
95
|
+
return remainder === 0 ? min : min + (sampling - remainder);
|
|
96
|
+
}
|
|
97
|
+
function countSamplesInRange(min, max, sampling) {
|
|
98
|
+
if (sampling <= 0 || max < min) return 0;
|
|
99
|
+
const first = firstSampleCoordinate(min, sampling);
|
|
100
|
+
if (first > max) return 0;
|
|
101
|
+
return Math.floor((max - first) / sampling) + 1;
|
|
102
|
+
}
|
|
103
|
+
function isSampledCoordinate(value, firstSample, sampling) {
|
|
104
|
+
if (sampling <= 1) return true;
|
|
105
|
+
if (value < firstSample) return false;
|
|
106
|
+
return (value - firstSample) % sampling === 0;
|
|
107
|
+
}
|
|
108
|
+
function firstSampleInRange(min, max, sampleOrigin, sampling) {
|
|
109
|
+
if (max < min) return null;
|
|
110
|
+
if (sampling <= 1) return min;
|
|
111
|
+
const delta = min - sampleOrigin;
|
|
112
|
+
const remainder = modulo(delta, sampling);
|
|
113
|
+
const first = remainder === 0 ? min : min + (sampling - remainder);
|
|
114
|
+
return first <= max ? first : null;
|
|
115
|
+
}
|
|
116
|
+
function float32ToFloat16(value) {
|
|
117
|
+
if (Object.is(value, -0)) return 32768;
|
|
118
|
+
if (Object.is(value, 0)) return 0;
|
|
119
|
+
if (!Number.isFinite(value)) {
|
|
120
|
+
if (Number.isNaN(value)) return 32256;
|
|
121
|
+
return value < 0 ? 64512 : 31744;
|
|
122
|
+
}
|
|
123
|
+
floatView[0] = value;
|
|
124
|
+
const bits = uintView[0];
|
|
125
|
+
const sign = bits >>> 16 & 32768;
|
|
126
|
+
const exponent = (bits >>> 23 & 255) - 127 + 15;
|
|
127
|
+
const mantissa = bits & 8388607;
|
|
128
|
+
if (exponent <= 0) {
|
|
129
|
+
if (exponent < -10) return sign;
|
|
130
|
+
const m = (mantissa | 8388608) >> 1 - exponent;
|
|
131
|
+
return sign | m + 4096 >> 13;
|
|
132
|
+
}
|
|
133
|
+
if (exponent >= 31) {
|
|
134
|
+
return sign | 31744;
|
|
135
|
+
}
|
|
136
|
+
return sign | exponent << 10 | mantissa + 4096 >> 13;
|
|
137
|
+
}
|
|
138
|
+
function convertB44ToLinear(value) {
|
|
139
|
+
if ((value & 31744) === 31744) return 0;
|
|
140
|
+
if (value > 32768) return 0;
|
|
141
|
+
const floatValue = float16ToFloat32(value);
|
|
142
|
+
const converted = 8 * Math.log(floatValue);
|
|
143
|
+
return float32ToFloat16(converted);
|
|
144
|
+
}
|
|
145
|
+
function getB44ToLinearTable() {
|
|
146
|
+
if (b44ToLinearTable) {
|
|
147
|
+
return b44ToLinearTable;
|
|
148
|
+
}
|
|
149
|
+
const table = new Uint16Array(1 << 16);
|
|
150
|
+
for (let i = 0; i < table.length; i++) {
|
|
151
|
+
table[i] = convertB44ToLinear(i);
|
|
152
|
+
}
|
|
153
|
+
b44ToLinearTable = table;
|
|
154
|
+
return table;
|
|
155
|
+
}
|
|
156
|
+
function unpack14(source, offset, out) {
|
|
157
|
+
const shift = source[offset + 2] >> 2;
|
|
158
|
+
const bias = 32 << shift;
|
|
159
|
+
out[0] = source[offset] << 8 | source[offset + 1];
|
|
160
|
+
out[4] = out[0] + ((((source[offset + 2] << 4 | source[offset + 3] >> 4) & 63) << shift) - bias) & 65535;
|
|
161
|
+
out[8] = out[4] + ((((source[offset + 3] << 2 | source[offset + 4] >> 6) & 63) << shift) - bias) & 65535;
|
|
162
|
+
out[12] = out[8] + (((source[offset + 4] & 63) << shift) - bias) & 65535;
|
|
163
|
+
out[1] = out[0] + ((source[offset + 5] >> 2 << shift) - bias) & 65535;
|
|
164
|
+
out[5] = out[4] + ((((source[offset + 5] << 4 | source[offset + 6] >> 4) & 63) << shift) - bias) & 65535;
|
|
165
|
+
out[9] = out[8] + ((((source[offset + 6] << 2 | source[offset + 7] >> 6) & 63) << shift) - bias) & 65535;
|
|
166
|
+
out[13] = out[12] + (((source[offset + 7] & 63) << shift) - bias) & 65535;
|
|
167
|
+
out[2] = out[1] + ((source[offset + 8] >> 2 << shift) - bias) & 65535;
|
|
168
|
+
out[6] = out[5] + ((((source[offset + 8] << 4 | source[offset + 9] >> 4) & 63) << shift) - bias) & 65535;
|
|
169
|
+
out[10] = out[9] + ((((source[offset + 9] << 2 | source[offset + 10] >> 6) & 63) << shift) - bias) & 65535;
|
|
170
|
+
out[14] = out[13] + (((source[offset + 10] & 63) << shift) - bias) & 65535;
|
|
171
|
+
out[3] = out[2] + ((source[offset + 11] >> 2 << shift) - bias) & 65535;
|
|
172
|
+
out[7] = out[6] + ((((source[offset + 11] << 4 | source[offset + 12] >> 4) & 63) << shift) - bias) & 65535;
|
|
173
|
+
out[11] = out[10] + ((((source[offset + 12] << 2 | source[offset + 13] >> 6) & 63) << shift) - bias) & 65535;
|
|
174
|
+
out[15] = out[14] + (((source[offset + 13] & 63) << shift) - bias) & 65535;
|
|
175
|
+
for (let i = 0; i < HALF_VALUES_PER_BLOCK; i++) {
|
|
176
|
+
const value = out[i];
|
|
177
|
+
out[i] = (value & 32768) !== 0 ? value & 32767 : ~value & 65535;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
function unpack3(source, offset, out) {
|
|
181
|
+
let value = source[offset] << 8 | source[offset + 1];
|
|
182
|
+
value = (value & 32768) !== 0 ? value & 32767 : ~value & 65535;
|
|
183
|
+
out.fill(value);
|
|
184
|
+
}
|
|
185
|
+
function buildChannelLayouts(context) {
|
|
186
|
+
const part = context.part;
|
|
187
|
+
const dataWindow = part.dataWindow;
|
|
188
|
+
if (!dataWindow) {
|
|
189
|
+
throw new Error("B44 decode requires part dataWindow.");
|
|
190
|
+
}
|
|
191
|
+
const chunkStart = context.chunkY;
|
|
192
|
+
const chunkEnd = context.chunkY + context.linesInChunk - 1;
|
|
193
|
+
const layouts = [];
|
|
194
|
+
for (const channel of part.channels) {
|
|
195
|
+
const xSampling = channel.xSampling > 0 ? channel.xSampling : 1;
|
|
196
|
+
const ySampling = channel.ySampling > 0 ? channel.ySampling : 1;
|
|
197
|
+
const sampleOriginY = firstSampleCoordinate(dataWindow.yMin, ySampling);
|
|
198
|
+
const sampledWidth = countSamplesInRange(dataWindow.xMin, dataWindow.xMax, xSampling);
|
|
199
|
+
const bytesPerSample = channel.pixelType === 1 ? 2 : 4;
|
|
200
|
+
const visibleStart = Math.max(chunkStart, dataWindow.yMin);
|
|
201
|
+
const visibleEnd = Math.min(chunkEnd, dataWindow.yMax);
|
|
202
|
+
let sampledRows = 0;
|
|
203
|
+
let firstChunkSampleY = -1;
|
|
204
|
+
if (visibleEnd >= visibleStart) {
|
|
205
|
+
const first = firstSampleInRange(visibleStart, visibleEnd, sampleOriginY, ySampling);
|
|
206
|
+
if (first !== null) {
|
|
207
|
+
firstChunkSampleY = first;
|
|
208
|
+
sampledRows = Math.floor((visibleEnd - first) / ySampling) + 1;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
const planeSize = sampledWidth * sampledRows * bytesPerSample;
|
|
212
|
+
layouts.push({
|
|
213
|
+
channel,
|
|
214
|
+
bytesPerSample,
|
|
215
|
+
sampledWidth,
|
|
216
|
+
sampledRows,
|
|
217
|
+
sampleOriginY,
|
|
218
|
+
ySampling,
|
|
219
|
+
firstChunkSampleY,
|
|
220
|
+
plane: new Uint8Array(planeSize)
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
return layouts;
|
|
224
|
+
}
|
|
225
|
+
function decodeB44Block(context) {
|
|
226
|
+
try {
|
|
227
|
+
const part = context.part;
|
|
228
|
+
const dataWindow = part.dataWindow;
|
|
229
|
+
if (!dataWindow) {
|
|
230
|
+
throw new Error("B44 decode requires part dataWindow.");
|
|
231
|
+
}
|
|
232
|
+
const source = new Uint8Array(context.buffer, context.dataPtr, context.dataSize);
|
|
233
|
+
const layouts = buildChannelLayouts(context);
|
|
234
|
+
const halfBlock = new Uint16Array(HALF_VALUES_PER_BLOCK);
|
|
235
|
+
const chunkStart = context.chunkY;
|
|
236
|
+
const chunkEnd = context.chunkY + context.linesInChunk - 1;
|
|
237
|
+
let sourceCursor = 0;
|
|
238
|
+
for (const layout of layouts) {
|
|
239
|
+
if (layout.plane.length === 0) {
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
if (layout.channel.pixelType !== 1) {
|
|
243
|
+
if (sourceCursor + layout.plane.length > source.length) {
|
|
244
|
+
throw new Error("Truncated B44 payload for non-HALF channel data.");
|
|
245
|
+
}
|
|
246
|
+
layout.plane.set(source.subarray(sourceCursor, sourceCursor + layout.plane.length));
|
|
247
|
+
sourceCursor += layout.plane.length;
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
const blocksY = Math.ceil(layout.sampledRows / 4);
|
|
251
|
+
const blocksX = Math.ceil(layout.sampledWidth / 4);
|
|
252
|
+
const applyPLinear = layout.channel.pLinear !== 0;
|
|
253
|
+
const toLinearTable = applyPLinear ? getB44ToLinearTable() : null;
|
|
254
|
+
for (let blockY = 0; blockY < blocksY; blockY++) {
|
|
255
|
+
const rowBase = blockY * 4;
|
|
256
|
+
const rowsInBlock = Math.min(4, layout.sampledRows - rowBase);
|
|
257
|
+
for (let blockX = 0; blockX < blocksX; blockX++) {
|
|
258
|
+
const colBase = blockX * 4;
|
|
259
|
+
const colsInBlock = Math.min(4, layout.sampledWidth - colBase);
|
|
260
|
+
if (sourceCursor + B44A_BLOCK_SIZE > source.length) {
|
|
261
|
+
throw new Error("Truncated B44 payload while reading block header.");
|
|
262
|
+
}
|
|
263
|
+
if (source[sourceCursor + 2] >= B44A_MARKER_THRESHOLD) {
|
|
264
|
+
unpack3(source, sourceCursor, halfBlock);
|
|
265
|
+
sourceCursor += B44A_BLOCK_SIZE;
|
|
266
|
+
} else {
|
|
267
|
+
if (sourceCursor + B44_BLOCK_SIZE > source.length) {
|
|
268
|
+
throw new Error("Truncated B44 payload while reading 14-byte block.");
|
|
269
|
+
}
|
|
270
|
+
unpack14(source, sourceCursor, halfBlock);
|
|
271
|
+
sourceCursor += B44_BLOCK_SIZE;
|
|
272
|
+
}
|
|
273
|
+
if (toLinearTable) {
|
|
274
|
+
for (let i = 0; i < HALF_VALUES_PER_BLOCK; i++) {
|
|
275
|
+
halfBlock[i] = toLinearTable[halfBlock[i]];
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
for (let row = 0; row < rowsInBlock; row++) {
|
|
279
|
+
const rowIndex = rowBase + row;
|
|
280
|
+
const rowOffset = rowIndex * layout.sampledWidth;
|
|
281
|
+
const srcRowOffset = row * 4;
|
|
282
|
+
for (let col = 0; col < colsInBlock; col++) {
|
|
283
|
+
const dstIndex = (rowOffset + colBase + col) * 2;
|
|
284
|
+
const value = halfBlock[srcRowOffset + col];
|
|
285
|
+
layout.plane[dstIndex] = value & 255;
|
|
286
|
+
layout.plane[dstIndex + 1] = value >> 8;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
const outputSize = layouts.reduce((total, layout) => total + layout.plane.length, 0);
|
|
293
|
+
const output = new Uint8Array(outputSize);
|
|
294
|
+
let outputCursor = 0;
|
|
295
|
+
for (let y = chunkStart; y <= chunkEnd; y++) {
|
|
296
|
+
if (y < dataWindow.yMin || y > dataWindow.yMax) {
|
|
297
|
+
continue;
|
|
298
|
+
}
|
|
299
|
+
for (const layout of layouts) {
|
|
300
|
+
if (layout.sampledRows === 0 || layout.sampledWidth === 0) {
|
|
301
|
+
continue;
|
|
302
|
+
}
|
|
303
|
+
if (!isSampledCoordinate(y, layout.sampleOriginY, layout.ySampling)) {
|
|
304
|
+
continue;
|
|
305
|
+
}
|
|
306
|
+
if (layout.firstChunkSampleY < 0 || y < layout.firstChunkSampleY) {
|
|
307
|
+
continue;
|
|
308
|
+
}
|
|
309
|
+
const sampledRow = Math.floor((y - layout.firstChunkSampleY) / layout.ySampling);
|
|
310
|
+
if (sampledRow < 0 || sampledRow >= layout.sampledRows) {
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
const rowByteLength = layout.sampledWidth * layout.bytesPerSample;
|
|
314
|
+
const rowOffset = sampledRow * rowByteLength;
|
|
315
|
+
output.set(layout.plane.subarray(rowOffset, rowOffset + rowByteLength), outputCursor);
|
|
316
|
+
outputCursor += rowByteLength;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
if (outputCursor !== output.length) {
|
|
320
|
+
throw new Error("B44 row packing produced an unexpected output size.");
|
|
321
|
+
}
|
|
322
|
+
return output;
|
|
323
|
+
} catch {
|
|
324
|
+
throw new ExrError("DECOMPRESSION_FAILED", "Failed to decompress B44/B44A chunk.", {
|
|
325
|
+
partId: context.partId,
|
|
326
|
+
chunkIndex: context.chunkIndex,
|
|
327
|
+
size: context.dataSize
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// src/reader/compression/dwa.ts
|
|
333
|
+
import { unzlibSync } from "fflate";
|
|
334
|
+
var HUF_ENCBITS = 16;
|
|
335
|
+
var HUF_DECBITS = 14;
|
|
336
|
+
var HUF_ENCSIZE = (1 << HUF_ENCBITS) + 1;
|
|
337
|
+
var HUF_DECSIZE = 1 << HUF_DECBITS;
|
|
338
|
+
var HUF_DECMASK = HUF_DECSIZE - 1;
|
|
339
|
+
var SHORT_ZEROCODE_RUN = 59;
|
|
340
|
+
var LONG_ZEROCODE_RUN = 63;
|
|
341
|
+
var SHORTEST_LONG_RUN = 2 + LONG_ZEROCODE_RUN - SHORT_ZEROCODE_RUN;
|
|
342
|
+
var INT16_SIZE = 2;
|
|
343
|
+
var STATIC_HUFFMAN = 0;
|
|
344
|
+
var DEFLATE = 1;
|
|
345
|
+
var UNKNOWN = 0;
|
|
346
|
+
var LOSSY_DCT = 1;
|
|
347
|
+
var RLE = 2;
|
|
348
|
+
var LOG_BASE = Math.pow(2.7182818, 2.2);
|
|
349
|
+
var TEXT_DECODER = new TextDecoder();
|
|
350
|
+
var DCT_A = 0.5 * Math.cos(Math.PI / 4);
|
|
351
|
+
var DCT_B = 0.5 * Math.cos(Math.PI / 16);
|
|
352
|
+
var DCT_C = 0.5 * Math.cos(Math.PI / 8);
|
|
353
|
+
var DCT_D = 0.5 * Math.cos(3 * Math.PI / 16);
|
|
354
|
+
var DCT_E = 0.5 * Math.cos(5 * Math.PI / 16);
|
|
355
|
+
var DCT_F = 0.5 * Math.cos(3 * Math.PI / 8);
|
|
356
|
+
var DCT_G = 0.5 * Math.cos(7 * Math.PI / 16);
|
|
357
|
+
var FLOAT32_TO_FLOAT16_SCRATCH = new ArrayBuffer(4);
|
|
358
|
+
var FLOAT32_TO_FLOAT16_FLOAT_VIEW = new Float32Array(FLOAT32_TO_FLOAT16_SCRATCH);
|
|
359
|
+
var FLOAT32_TO_FLOAT16_INT_VIEW = new Uint32Array(FLOAT32_TO_FLOAT16_SCRATCH);
|
|
360
|
+
var LEGACY_DWA_RULES = [
|
|
361
|
+
{ name: "r", compression: LOSSY_DCT, pixelType: 1, index: 0, caseInsensitive: true },
|
|
362
|
+
{ name: "r", compression: LOSSY_DCT, pixelType: 2, index: 0, caseInsensitive: true },
|
|
363
|
+
{ name: "red", compression: LOSSY_DCT, pixelType: 1, index: 0, caseInsensitive: true },
|
|
364
|
+
{ name: "red", compression: LOSSY_DCT, pixelType: 2, index: 0, caseInsensitive: true },
|
|
365
|
+
{ name: "g", compression: LOSSY_DCT, pixelType: 1, index: 1, caseInsensitive: true },
|
|
366
|
+
{ name: "g", compression: LOSSY_DCT, pixelType: 2, index: 1, caseInsensitive: true },
|
|
367
|
+
{ name: "grn", compression: LOSSY_DCT, pixelType: 1, index: 1, caseInsensitive: true },
|
|
368
|
+
{ name: "grn", compression: LOSSY_DCT, pixelType: 2, index: 1, caseInsensitive: true },
|
|
369
|
+
{ name: "green", compression: LOSSY_DCT, pixelType: 1, index: 1, caseInsensitive: true },
|
|
370
|
+
{ name: "green", compression: LOSSY_DCT, pixelType: 2, index: 1, caseInsensitive: true },
|
|
371
|
+
{ name: "b", compression: LOSSY_DCT, pixelType: 1, index: 2, caseInsensitive: true },
|
|
372
|
+
{ name: "b", compression: LOSSY_DCT, pixelType: 2, index: 2, caseInsensitive: true },
|
|
373
|
+
{ name: "blu", compression: LOSSY_DCT, pixelType: 1, index: 2, caseInsensitive: true },
|
|
374
|
+
{ name: "blu", compression: LOSSY_DCT, pixelType: 2, index: 2, caseInsensitive: true },
|
|
375
|
+
{ name: "blue", compression: LOSSY_DCT, pixelType: 1, index: 2, caseInsensitive: true },
|
|
376
|
+
{ name: "blue", compression: LOSSY_DCT, pixelType: 2, index: 2, caseInsensitive: true },
|
|
377
|
+
{ name: "y", compression: LOSSY_DCT, pixelType: 1, index: -1, caseInsensitive: true },
|
|
378
|
+
{ name: "y", compression: LOSSY_DCT, pixelType: 2, index: -1, caseInsensitive: true },
|
|
379
|
+
{ name: "by", compression: LOSSY_DCT, pixelType: 1, index: -1, caseInsensitive: true },
|
|
380
|
+
{ name: "by", compression: LOSSY_DCT, pixelType: 2, index: -1, caseInsensitive: true },
|
|
381
|
+
{ name: "ry", compression: LOSSY_DCT, pixelType: 1, index: -1, caseInsensitive: true },
|
|
382
|
+
{ name: "ry", compression: LOSSY_DCT, pixelType: 2, index: -1, caseInsensitive: true },
|
|
383
|
+
{ name: "a", compression: RLE, pixelType: 0, index: -1, caseInsensitive: true },
|
|
384
|
+
{ name: "a", compression: RLE, pixelType: 1, index: -1, caseInsensitive: true },
|
|
385
|
+
{ name: "a", compression: RLE, pixelType: 2, index: -1, caseInsensitive: true }
|
|
386
|
+
];
|
|
387
|
+
function ensureAvailable(totalLength, cursor, size, context) {
|
|
388
|
+
if (size < 0 || cursor.value < 0 || cursor.value + size > totalLength) {
|
|
389
|
+
throw new Error(`Truncated DWA payload while reading ${context}.`);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
function ensureSafeInteger(value, context) {
|
|
393
|
+
const max = BigInt(Number.MAX_SAFE_INTEGER);
|
|
394
|
+
const min = BigInt(Number.MIN_SAFE_INTEGER);
|
|
395
|
+
if (value > max || value < min) {
|
|
396
|
+
throw new Error(`DWA value exceeded safe integer range while reading ${context}.`);
|
|
397
|
+
}
|
|
398
|
+
return Number(value);
|
|
399
|
+
}
|
|
400
|
+
function readUint8(view, cursor) {
|
|
401
|
+
ensureAvailable(view.byteLength, cursor, 1, "uint8");
|
|
402
|
+
const value = view.getUint8(cursor.value);
|
|
403
|
+
cursor.value += 1;
|
|
404
|
+
return value;
|
|
405
|
+
}
|
|
406
|
+
function readUint16(view, cursor) {
|
|
407
|
+
ensureAvailable(view.byteLength, cursor, 2, "uint16");
|
|
408
|
+
const value = view.getUint16(cursor.value, true);
|
|
409
|
+
cursor.value += 2;
|
|
410
|
+
return value;
|
|
411
|
+
}
|
|
412
|
+
function readUint32(view, cursor) {
|
|
413
|
+
ensureAvailable(view.byteLength, cursor, 4, "uint32");
|
|
414
|
+
const value = view.getUint32(cursor.value, true);
|
|
415
|
+
cursor.value += 4;
|
|
416
|
+
return value;
|
|
417
|
+
}
|
|
418
|
+
function readInt64(view, cursor) {
|
|
419
|
+
ensureAvailable(view.byteLength, cursor, 8, "int64");
|
|
420
|
+
let value;
|
|
421
|
+
if ("getBigInt64" in DataView.prototype) {
|
|
422
|
+
value = ensureSafeInteger(view.getBigInt64(cursor.value, true), "int64");
|
|
423
|
+
} else {
|
|
424
|
+
const low = BigInt(view.getUint32(cursor.value, true));
|
|
425
|
+
const high = BigInt(view.getInt32(cursor.value + 4, true));
|
|
426
|
+
value = ensureSafeInteger((high << 32n) + low, "int64");
|
|
427
|
+
}
|
|
428
|
+
cursor.value += 8;
|
|
429
|
+
return value;
|
|
430
|
+
}
|
|
431
|
+
function readUint8FromArray(input, cursor) {
|
|
432
|
+
if (cursor.value < 0 || cursor.value >= input.length) {
|
|
433
|
+
throw new Error("Truncated DWA Huffman payload.");
|
|
434
|
+
}
|
|
435
|
+
const value = input[cursor.value];
|
|
436
|
+
cursor.value += 1;
|
|
437
|
+
return value;
|
|
438
|
+
}
|
|
439
|
+
function readCStringWithin(input, cursor, limit, context) {
|
|
440
|
+
if (limit < cursor.value || limit > input.length) {
|
|
441
|
+
throw new Error(`Invalid DWA bounded read while parsing ${context}.`);
|
|
442
|
+
}
|
|
443
|
+
const start = cursor.value;
|
|
444
|
+
while (cursor.value < limit) {
|
|
445
|
+
if (input[cursor.value] === 0) {
|
|
446
|
+
const value = TEXT_DECODER.decode(input.subarray(start, cursor.value));
|
|
447
|
+
cursor.value += 1;
|
|
448
|
+
return value;
|
|
449
|
+
}
|
|
450
|
+
cursor.value += 1;
|
|
451
|
+
}
|
|
452
|
+
throw new Error(`Unterminated DWA string while parsing ${context}.`);
|
|
453
|
+
}
|
|
454
|
+
function clearHuffmanDecodingTable(table) {
|
|
455
|
+
for (let i = 0; i < HUF_DECSIZE; i++) {
|
|
456
|
+
table[i] = { len: 0, lit: 0, p: null };
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
function getBits(nBits, state, input, cursor) {
|
|
460
|
+
while (state.lc < nBits) {
|
|
461
|
+
state.c = (state.c << 8 | readUint8FromArray(input, cursor)) >>> 0;
|
|
462
|
+
state.lc += 8;
|
|
463
|
+
}
|
|
464
|
+
state.lc -= nBits;
|
|
465
|
+
return state.c >>> state.lc & (1 << nBits) - 1;
|
|
466
|
+
}
|
|
467
|
+
var HUF_TABLE_BUFFER = new Int32Array(59);
|
|
468
|
+
function hufLength(code) {
|
|
469
|
+
return code & 63;
|
|
470
|
+
}
|
|
471
|
+
function hufCode(code) {
|
|
472
|
+
return code >>> 6;
|
|
473
|
+
}
|
|
474
|
+
function hufCanonicalCodeTable(hcode) {
|
|
475
|
+
HUF_TABLE_BUFFER.fill(0);
|
|
476
|
+
for (let i = 0; i < HUF_ENCSIZE; i++) {
|
|
477
|
+
HUF_TABLE_BUFFER[hcode[i]] += 1;
|
|
478
|
+
}
|
|
479
|
+
let c = 0;
|
|
480
|
+
for (let i = 58; i > 0; i--) {
|
|
481
|
+
const next = c + HUF_TABLE_BUFFER[i] >> 1;
|
|
482
|
+
HUF_TABLE_BUFFER[i] = c;
|
|
483
|
+
c = next;
|
|
484
|
+
}
|
|
485
|
+
for (let i = 0; i < HUF_ENCSIZE; i++) {
|
|
486
|
+
const length = hcode[i];
|
|
487
|
+
if (length > 0) {
|
|
488
|
+
hcode[i] = length | HUF_TABLE_BUFFER[length]++ << 6;
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
function hufUnpackEncTable(input, cursor, maxTableBytes, minSymbol, maxSymbol, hcode) {
|
|
493
|
+
const start = cursor.value;
|
|
494
|
+
const state = { c: 0, lc: 0 };
|
|
495
|
+
for (let symbol = minSymbol; symbol <= maxSymbol; symbol++) {
|
|
496
|
+
if (cursor.value - start > maxTableBytes) {
|
|
497
|
+
throw new Error("Invalid DWA Huffman table payload.");
|
|
498
|
+
}
|
|
499
|
+
const length = getBits(6, state, input, cursor);
|
|
500
|
+
hcode[symbol] = length;
|
|
501
|
+
if (length === LONG_ZEROCODE_RUN) {
|
|
502
|
+
if (cursor.value - start > maxTableBytes) {
|
|
503
|
+
throw new Error("Invalid DWA Huffman table payload.");
|
|
504
|
+
}
|
|
505
|
+
let zeroRun = getBits(8, state, input, cursor) + SHORTEST_LONG_RUN;
|
|
506
|
+
if (symbol + zeroRun > maxSymbol + 1) {
|
|
507
|
+
throw new Error("Invalid DWA Huffman zero-run.");
|
|
508
|
+
}
|
|
509
|
+
while (zeroRun-- > 0) {
|
|
510
|
+
hcode[symbol++] = 0;
|
|
511
|
+
}
|
|
512
|
+
symbol--;
|
|
513
|
+
} else if (length >= SHORT_ZEROCODE_RUN) {
|
|
514
|
+
let zeroRun = length - SHORT_ZEROCODE_RUN + 2;
|
|
515
|
+
if (symbol + zeroRun > maxSymbol + 1) {
|
|
516
|
+
throw new Error("Invalid DWA Huffman zero-run.");
|
|
517
|
+
}
|
|
518
|
+
while (zeroRun-- > 0) {
|
|
519
|
+
hcode[symbol++] = 0;
|
|
520
|
+
}
|
|
521
|
+
symbol--;
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
hufCanonicalCodeTable(hcode);
|
|
525
|
+
}
|
|
526
|
+
function hufBuildDecTable(hcode, minSymbol, maxSymbol, table) {
|
|
527
|
+
for (let symbol = minSymbol; symbol <= maxSymbol; symbol++) {
|
|
528
|
+
const code = hufCode(hcode[symbol]);
|
|
529
|
+
const length = hufLength(hcode[symbol]);
|
|
530
|
+
if (code >>> length) {
|
|
531
|
+
throw new Error("Invalid DWA Huffman code.");
|
|
532
|
+
}
|
|
533
|
+
if (length > HUF_DECBITS) {
|
|
534
|
+
const entry = table[code >>> length - HUF_DECBITS];
|
|
535
|
+
if (entry.len) {
|
|
536
|
+
throw new Error("Invalid DWA Huffman decode table entry.");
|
|
537
|
+
}
|
|
538
|
+
entry.lit += 1;
|
|
539
|
+
if (entry.p) {
|
|
540
|
+
entry.p.push(symbol);
|
|
541
|
+
} else {
|
|
542
|
+
entry.p = [symbol];
|
|
543
|
+
}
|
|
544
|
+
} else if (length) {
|
|
545
|
+
const base = code << HUF_DECBITS - length;
|
|
546
|
+
const count = 1 << HUF_DECBITS - length;
|
|
547
|
+
for (let i = 0; i < count; i++) {
|
|
548
|
+
const entry = table[base + i];
|
|
549
|
+
if (entry.len || entry.p) {
|
|
550
|
+
throw new Error("Invalid DWA Huffman decode table entry.");
|
|
551
|
+
}
|
|
552
|
+
entry.len = length;
|
|
553
|
+
entry.lit = symbol;
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
function getChar(state, input, cursor) {
|
|
559
|
+
state.c = (state.c << 8 | readUint8FromArray(input, cursor)) >>> 0;
|
|
560
|
+
state.lc += 8;
|
|
561
|
+
}
|
|
562
|
+
function getCode(symbol, runLengthCode, state, input, cursor, outBuffer, outOffset, outEnd) {
|
|
563
|
+
if (symbol === runLengthCode) {
|
|
564
|
+
if (state.lc < 8) {
|
|
565
|
+
getChar(state, input, cursor);
|
|
566
|
+
}
|
|
567
|
+
state.lc -= 8;
|
|
568
|
+
let repeatCount = state.c >>> state.lc & 255;
|
|
569
|
+
if (outOffset.value === 0 || outOffset.value + repeatCount > outEnd) {
|
|
570
|
+
throw new Error("Invalid DWA run-length code.");
|
|
571
|
+
}
|
|
572
|
+
const last = outBuffer[outOffset.value - 1];
|
|
573
|
+
while (repeatCount-- > 0) {
|
|
574
|
+
outBuffer[outOffset.value++] = last;
|
|
575
|
+
}
|
|
576
|
+
} else if (outOffset.value < outEnd) {
|
|
577
|
+
outBuffer[outOffset.value++] = symbol;
|
|
578
|
+
} else {
|
|
579
|
+
throw new Error("DWA Huffman decode overflow.");
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
function hufDecode(encodingTable, decodingTable, input, cursor, nBits, runLengthCode, outSize, outBuffer, outOffset) {
|
|
583
|
+
const state = { c: 0, lc: 0 };
|
|
584
|
+
const inOffsetEnd = Math.trunc(cursor.value + (nBits + 7) / 8);
|
|
585
|
+
while (cursor.value < inOffsetEnd) {
|
|
586
|
+
getChar(state, input, cursor);
|
|
587
|
+
while (state.lc >= HUF_DECBITS) {
|
|
588
|
+
const index = state.c >>> state.lc - HUF_DECBITS & HUF_DECMASK;
|
|
589
|
+
const entry = decodingTable[index];
|
|
590
|
+
if (entry.len) {
|
|
591
|
+
state.lc -= entry.len;
|
|
592
|
+
getCode(entry.lit, runLengthCode, state, input, cursor, outBuffer, outOffset, outSize);
|
|
593
|
+
} else {
|
|
594
|
+
if (!entry.p) {
|
|
595
|
+
throw new Error("DWA Huffman decode failed.");
|
|
596
|
+
}
|
|
597
|
+
let matched = false;
|
|
598
|
+
for (let i = 0; i < entry.lit; i++) {
|
|
599
|
+
const symbol = entry.p[i];
|
|
600
|
+
const length = hufLength(encodingTable[symbol]);
|
|
601
|
+
while (state.lc < length && cursor.value < inOffsetEnd) {
|
|
602
|
+
getChar(state, input, cursor);
|
|
603
|
+
}
|
|
604
|
+
if (state.lc >= length) {
|
|
605
|
+
const code = hufCode(encodingTable[symbol]);
|
|
606
|
+
const candidate = state.c >>> state.lc - length & (1 << length) - 1;
|
|
607
|
+
if (code === candidate) {
|
|
608
|
+
state.lc -= length;
|
|
609
|
+
getCode(symbol, runLengthCode, state, input, cursor, outBuffer, outOffset, outSize);
|
|
610
|
+
matched = true;
|
|
611
|
+
break;
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
if (!matched) {
|
|
616
|
+
throw new Error("DWA Huffman decode failed.");
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
const pad = 8 - nBits & 7;
|
|
622
|
+
state.c >>>= pad;
|
|
623
|
+
state.lc -= pad;
|
|
624
|
+
while (state.lc > 0) {
|
|
625
|
+
const entry = decodingTable[state.c << HUF_DECBITS - state.lc & HUF_DECMASK];
|
|
626
|
+
if (!entry.len) {
|
|
627
|
+
throw new Error("DWA Huffman decode failed.");
|
|
628
|
+
}
|
|
629
|
+
state.lc -= entry.len;
|
|
630
|
+
getCode(entry.lit, runLengthCode, state, input, cursor, outBuffer, outOffset, outSize);
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
function hufUncompress(input, view, cursor, nCompressed, out, nRaw) {
|
|
634
|
+
const outOffset = { value: 0 };
|
|
635
|
+
const initialOffset = cursor.value;
|
|
636
|
+
const minSymbol = readUint32(view, cursor);
|
|
637
|
+
const maxSymbol = readUint32(view, cursor);
|
|
638
|
+
readUint32(view, cursor);
|
|
639
|
+
const nBits = readUint32(view, cursor);
|
|
640
|
+
readUint32(view, cursor);
|
|
641
|
+
if (minSymbol < 0 || minSymbol >= HUF_ENCSIZE || maxSymbol < 0 || maxSymbol >= HUF_ENCSIZE || minSymbol > maxSymbol) {
|
|
642
|
+
throw new Error("Invalid DWA Huffman symbol bounds.");
|
|
643
|
+
}
|
|
644
|
+
const encodingTable = new Int32Array(HUF_ENCSIZE);
|
|
645
|
+
const decodingTable = new Array(HUF_DECSIZE);
|
|
646
|
+
clearHuffmanDecodingTable(decodingTable);
|
|
647
|
+
const tableBytes = nCompressed - (cursor.value - initialOffset);
|
|
648
|
+
if (tableBytes < 0) {
|
|
649
|
+
throw new Error("Invalid DWA Huffman payload size.");
|
|
650
|
+
}
|
|
651
|
+
hufUnpackEncTable(input, cursor, tableBytes, minSymbol, maxSymbol, encodingTable);
|
|
652
|
+
if (nBits > 8 * (nCompressed - (cursor.value - initialOffset))) {
|
|
653
|
+
throw new Error("Invalid DWA Huffman bit count.");
|
|
654
|
+
}
|
|
655
|
+
hufBuildDecTable(encodingTable, minSymbol, maxSymbol, decodingTable);
|
|
656
|
+
hufDecode(encodingTable, decodingTable, input, cursor, nBits, maxSymbol, nRaw, out, outOffset);
|
|
657
|
+
if (outOffset.value !== nRaw) {
|
|
658
|
+
throw new Error("DWA Huffman output size mismatch.");
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
var DWA_DC_SCRATCH = {
|
|
662
|
+
inflate: new Uint8Array(0),
|
|
663
|
+
output: new Uint8Array(0)
|
|
664
|
+
};
|
|
665
|
+
function undoZipPredictorAndInterleave(data, output) {
|
|
666
|
+
const length = data.length;
|
|
667
|
+
if (length === 0) {
|
|
668
|
+
return data;
|
|
669
|
+
}
|
|
670
|
+
const half = length + 1 >> 1;
|
|
671
|
+
let predicted = data[0];
|
|
672
|
+
output[0] = predicted;
|
|
673
|
+
for (let i = 1; i < half; i++) {
|
|
674
|
+
predicted = predicted + data[i] - 128 & 255;
|
|
675
|
+
output[i << 1] = predicted;
|
|
676
|
+
}
|
|
677
|
+
for (let i = half; i < length; i++) {
|
|
678
|
+
predicted = predicted + data[i] - 128 & 255;
|
|
679
|
+
output[(i - half << 1) + 1] = predicted;
|
|
680
|
+
}
|
|
681
|
+
return output;
|
|
682
|
+
}
|
|
683
|
+
function ensureDwaScratchBuffer(buffer, size) {
|
|
684
|
+
return buffer.byteLength >= size ? buffer.subarray(0, size) : new Uint8Array(size);
|
|
685
|
+
}
|
|
686
|
+
function uncompressZipStyle(input, offset, size, expectedUncompressedSize, scratch) {
|
|
687
|
+
if (size < 0 || offset < 0 || offset + size > input.length) {
|
|
688
|
+
throw new Error("Invalid DWA ZIP payload bounds.");
|
|
689
|
+
}
|
|
690
|
+
const compressed = input.subarray(offset, offset + size);
|
|
691
|
+
const inflateOut = expectedUncompressedSize > 0 ? ensureDwaScratchBuffer(scratch.inflate, expectedUncompressedSize) : scratch.inflate;
|
|
692
|
+
if (expectedUncompressedSize > 0 && inflateOut !== scratch.inflate) {
|
|
693
|
+
scratch.inflate = inflateOut;
|
|
694
|
+
}
|
|
695
|
+
const raw = expectedUncompressedSize > 0 ? unzlibSync(compressed, { out: inflateOut }) : unzlibSync(compressed);
|
|
696
|
+
const outputBuffer = ensureDwaScratchBuffer(scratch.output, raw.byteLength);
|
|
697
|
+
if (outputBuffer !== scratch.output) {
|
|
698
|
+
scratch.output = outputBuffer;
|
|
699
|
+
}
|
|
700
|
+
return undoZipPredictorAndInterleave(raw, outputBuffer);
|
|
701
|
+
}
|
|
702
|
+
function decodeRunLength(input, expectedSize) {
|
|
703
|
+
if (expectedSize < 0) {
|
|
704
|
+
throw new Error("Invalid DWA RLE expected size.");
|
|
705
|
+
}
|
|
706
|
+
const out = new Uint8Array(expectedSize);
|
|
707
|
+
let ptr = 0;
|
|
708
|
+
let outPtr = 0;
|
|
709
|
+
while (ptr < input.byteLength) {
|
|
710
|
+
const length = input[ptr] << 24 >> 24;
|
|
711
|
+
ptr += 1;
|
|
712
|
+
if (length < 0) {
|
|
713
|
+
const count = -length;
|
|
714
|
+
if (ptr + count > input.byteLength || outPtr + count > out.byteLength) {
|
|
715
|
+
throw new Error("Invalid DWA RLE payload bounds.");
|
|
716
|
+
}
|
|
717
|
+
out.set(input.subarray(ptr, ptr + count), outPtr);
|
|
718
|
+
ptr += count;
|
|
719
|
+
outPtr += count;
|
|
720
|
+
} else {
|
|
721
|
+
const count = length + 1;
|
|
722
|
+
if (ptr >= input.byteLength || outPtr + count > out.byteLength) {
|
|
723
|
+
throw new Error("Invalid DWA RLE payload bounds.");
|
|
724
|
+
}
|
|
725
|
+
const value = input[ptr++];
|
|
726
|
+
out.fill(value, outPtr, outPtr + count);
|
|
727
|
+
outPtr += count;
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
if (outPtr !== out.byteLength) {
|
|
731
|
+
throw new Error("Invalid DWA RLE payload length.");
|
|
732
|
+
}
|
|
733
|
+
return out;
|
|
734
|
+
}
|
|
735
|
+
function unRleAC(currAc, acBuffer, halfZigBlock) {
|
|
736
|
+
let dctComp = 1;
|
|
737
|
+
while (dctComp < 64) {
|
|
738
|
+
if (currAc.value >= acBuffer.length) {
|
|
739
|
+
throw new Error("Invalid DWA AC coefficient bounds.");
|
|
740
|
+
}
|
|
741
|
+
const acValue = acBuffer[currAc.value++];
|
|
742
|
+
if (acValue === 65280) {
|
|
743
|
+
dctComp = 64;
|
|
744
|
+
} else if (acValue >> 8 === 255) {
|
|
745
|
+
dctComp += acValue & 255;
|
|
746
|
+
} else {
|
|
747
|
+
halfZigBlock[dctComp++] = acValue;
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
function unZigZag(src, dst) {
|
|
752
|
+
dst[0] = float16ToFloat32(src[0]);
|
|
753
|
+
dst[1] = float16ToFloat32(src[1]);
|
|
754
|
+
dst[2] = float16ToFloat32(src[5]);
|
|
755
|
+
dst[3] = float16ToFloat32(src[6]);
|
|
756
|
+
dst[4] = float16ToFloat32(src[14]);
|
|
757
|
+
dst[5] = float16ToFloat32(src[15]);
|
|
758
|
+
dst[6] = float16ToFloat32(src[27]);
|
|
759
|
+
dst[7] = float16ToFloat32(src[28]);
|
|
760
|
+
dst[8] = float16ToFloat32(src[2]);
|
|
761
|
+
dst[9] = float16ToFloat32(src[4]);
|
|
762
|
+
dst[10] = float16ToFloat32(src[7]);
|
|
763
|
+
dst[11] = float16ToFloat32(src[13]);
|
|
764
|
+
dst[12] = float16ToFloat32(src[16]);
|
|
765
|
+
dst[13] = float16ToFloat32(src[26]);
|
|
766
|
+
dst[14] = float16ToFloat32(src[29]);
|
|
767
|
+
dst[15] = float16ToFloat32(src[42]);
|
|
768
|
+
dst[16] = float16ToFloat32(src[3]);
|
|
769
|
+
dst[17] = float16ToFloat32(src[8]);
|
|
770
|
+
dst[18] = float16ToFloat32(src[12]);
|
|
771
|
+
dst[19] = float16ToFloat32(src[17]);
|
|
772
|
+
dst[20] = float16ToFloat32(src[25]);
|
|
773
|
+
dst[21] = float16ToFloat32(src[30]);
|
|
774
|
+
dst[22] = float16ToFloat32(src[41]);
|
|
775
|
+
dst[23] = float16ToFloat32(src[43]);
|
|
776
|
+
dst[24] = float16ToFloat32(src[9]);
|
|
777
|
+
dst[25] = float16ToFloat32(src[11]);
|
|
778
|
+
dst[26] = float16ToFloat32(src[18]);
|
|
779
|
+
dst[27] = float16ToFloat32(src[24]);
|
|
780
|
+
dst[28] = float16ToFloat32(src[31]);
|
|
781
|
+
dst[29] = float16ToFloat32(src[40]);
|
|
782
|
+
dst[30] = float16ToFloat32(src[44]);
|
|
783
|
+
dst[31] = float16ToFloat32(src[53]);
|
|
784
|
+
dst[32] = float16ToFloat32(src[10]);
|
|
785
|
+
dst[33] = float16ToFloat32(src[19]);
|
|
786
|
+
dst[34] = float16ToFloat32(src[23]);
|
|
787
|
+
dst[35] = float16ToFloat32(src[32]);
|
|
788
|
+
dst[36] = float16ToFloat32(src[39]);
|
|
789
|
+
dst[37] = float16ToFloat32(src[45]);
|
|
790
|
+
dst[38] = float16ToFloat32(src[52]);
|
|
791
|
+
dst[39] = float16ToFloat32(src[54]);
|
|
792
|
+
dst[40] = float16ToFloat32(src[20]);
|
|
793
|
+
dst[41] = float16ToFloat32(src[22]);
|
|
794
|
+
dst[42] = float16ToFloat32(src[33]);
|
|
795
|
+
dst[43] = float16ToFloat32(src[38]);
|
|
796
|
+
dst[44] = float16ToFloat32(src[46]);
|
|
797
|
+
dst[45] = float16ToFloat32(src[51]);
|
|
798
|
+
dst[46] = float16ToFloat32(src[55]);
|
|
799
|
+
dst[47] = float16ToFloat32(src[60]);
|
|
800
|
+
dst[48] = float16ToFloat32(src[21]);
|
|
801
|
+
dst[49] = float16ToFloat32(src[34]);
|
|
802
|
+
dst[50] = float16ToFloat32(src[37]);
|
|
803
|
+
dst[51] = float16ToFloat32(src[47]);
|
|
804
|
+
dst[52] = float16ToFloat32(src[50]);
|
|
805
|
+
dst[53] = float16ToFloat32(src[56]);
|
|
806
|
+
dst[54] = float16ToFloat32(src[59]);
|
|
807
|
+
dst[55] = float16ToFloat32(src[61]);
|
|
808
|
+
dst[56] = float16ToFloat32(src[35]);
|
|
809
|
+
dst[57] = float16ToFloat32(src[36]);
|
|
810
|
+
dst[58] = float16ToFloat32(src[48]);
|
|
811
|
+
dst[59] = float16ToFloat32(src[49]);
|
|
812
|
+
dst[60] = float16ToFloat32(src[57]);
|
|
813
|
+
dst[61] = float16ToFloat32(src[58]);
|
|
814
|
+
dst[62] = float16ToFloat32(src[62]);
|
|
815
|
+
dst[63] = float16ToFloat32(src[63]);
|
|
816
|
+
}
|
|
817
|
+
function dctInverse(data) {
|
|
818
|
+
for (let row = 0; row < 8; row++) {
|
|
819
|
+
const rowPtr = row * 8;
|
|
820
|
+
const x0 = data[rowPtr + 0];
|
|
821
|
+
const x1 = data[rowPtr + 1];
|
|
822
|
+
const x2 = data[rowPtr + 2];
|
|
823
|
+
const x3 = data[rowPtr + 3];
|
|
824
|
+
const x4 = data[rowPtr + 4];
|
|
825
|
+
const x5 = data[rowPtr + 5];
|
|
826
|
+
const x6 = data[rowPtr + 6];
|
|
827
|
+
const x7 = data[rowPtr + 7];
|
|
828
|
+
const alpha0 = DCT_C * x2;
|
|
829
|
+
const alpha1 = DCT_F * x2;
|
|
830
|
+
const alpha2 = DCT_C * x6;
|
|
831
|
+
const alpha3 = DCT_F * x6;
|
|
832
|
+
const beta0 = DCT_B * x1 + DCT_D * x3 + DCT_E * x5 + DCT_G * x7;
|
|
833
|
+
const beta1 = DCT_D * x1 - DCT_G * x3 - DCT_B * x5 - DCT_E * x7;
|
|
834
|
+
const beta2 = DCT_E * x1 - DCT_B * x3 + DCT_G * x5 + DCT_D * x7;
|
|
835
|
+
const beta3 = DCT_G * x1 - DCT_E * x3 + DCT_D * x5 - DCT_B * x7;
|
|
836
|
+
const theta0 = DCT_A * (x0 + x4);
|
|
837
|
+
const theta1 = alpha0 + alpha3;
|
|
838
|
+
const theta2 = alpha1 - alpha2;
|
|
839
|
+
const theta3 = DCT_A * (x0 - x4);
|
|
840
|
+
const gamma0 = theta0 + theta1;
|
|
841
|
+
const gamma1 = theta3 + theta2;
|
|
842
|
+
const gamma2 = theta3 - theta2;
|
|
843
|
+
const gamma3 = theta0 - theta1;
|
|
844
|
+
data[rowPtr + 0] = gamma0 + beta0;
|
|
845
|
+
data[rowPtr + 1] = gamma1 + beta1;
|
|
846
|
+
data[rowPtr + 2] = gamma2 + beta2;
|
|
847
|
+
data[rowPtr + 3] = gamma3 + beta3;
|
|
848
|
+
data[rowPtr + 4] = gamma3 - beta3;
|
|
849
|
+
data[rowPtr + 5] = gamma2 - beta2;
|
|
850
|
+
data[rowPtr + 6] = gamma1 - beta1;
|
|
851
|
+
data[rowPtr + 7] = gamma0 - beta0;
|
|
852
|
+
}
|
|
853
|
+
for (let column = 0; column < 8; column++) {
|
|
854
|
+
const x0 = data[column];
|
|
855
|
+
const x1 = data[8 + column];
|
|
856
|
+
const x2 = data[16 + column];
|
|
857
|
+
const x3 = data[24 + column];
|
|
858
|
+
const x4 = data[32 + column];
|
|
859
|
+
const x5 = data[40 + column];
|
|
860
|
+
const x6 = data[48 + column];
|
|
861
|
+
const x7 = data[56 + column];
|
|
862
|
+
const alpha0 = DCT_C * x2;
|
|
863
|
+
const alpha1 = DCT_F * x2;
|
|
864
|
+
const alpha2 = DCT_C * x6;
|
|
865
|
+
const alpha3 = DCT_F * x6;
|
|
866
|
+
const beta0 = DCT_B * x1 + DCT_D * x3 + DCT_E * x5 + DCT_G * x7;
|
|
867
|
+
const beta1 = DCT_D * x1 - DCT_G * x3 - DCT_B * x5 - DCT_E * x7;
|
|
868
|
+
const beta2 = DCT_E * x1 - DCT_B * x3 + DCT_G * x5 + DCT_D * x7;
|
|
869
|
+
const beta3 = DCT_G * x1 - DCT_E * x3 + DCT_D * x5 - DCT_B * x7;
|
|
870
|
+
const theta0 = DCT_A * (x0 + x4);
|
|
871
|
+
const theta1 = alpha0 + alpha3;
|
|
872
|
+
const theta2 = alpha1 - alpha2;
|
|
873
|
+
const theta3 = DCT_A * (x0 - x4);
|
|
874
|
+
const gamma0 = theta0 + theta1;
|
|
875
|
+
const gamma1 = theta3 + theta2;
|
|
876
|
+
const gamma2 = theta3 - theta2;
|
|
877
|
+
const gamma3 = theta0 - theta1;
|
|
878
|
+
data[column] = gamma0 + beta0;
|
|
879
|
+
data[8 + column] = gamma1 + beta1;
|
|
880
|
+
data[16 + column] = gamma2 + beta2;
|
|
881
|
+
data[24 + column] = gamma3 + beta3;
|
|
882
|
+
data[32 + column] = gamma3 - beta3;
|
|
883
|
+
data[40 + column] = gamma2 - beta2;
|
|
884
|
+
data[48 + column] = gamma1 - beta1;
|
|
885
|
+
data[56 + column] = gamma0 - beta0;
|
|
886
|
+
}
|
|
887
|
+
}
|
|
888
|
+
function csc709Inverse(data) {
|
|
889
|
+
for (let i = 0; i < 64; i++) {
|
|
890
|
+
const y = data[0][i];
|
|
891
|
+
const cb = data[1][i];
|
|
892
|
+
const cr = data[2][i];
|
|
893
|
+
data[0][i] = y + 1.5747 * cr;
|
|
894
|
+
data[1][i] = y - 0.1873 * cb - 0.4682 * cr;
|
|
895
|
+
data[2][i] = y + 1.8556 * cb;
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
function float32ToFloat162(value) {
|
|
899
|
+
if (Object.is(value, 0)) return 0;
|
|
900
|
+
FLOAT32_TO_FLOAT16_FLOAT_VIEW[0] = value;
|
|
901
|
+
const bits = FLOAT32_TO_FLOAT16_INT_VIEW[0];
|
|
902
|
+
const sign = bits >>> 16 & 32768;
|
|
903
|
+
const exponent = (bits >>> 23 & 255) - 127 + 15;
|
|
904
|
+
const mantissa = bits & 8388607;
|
|
905
|
+
if (exponent <= 0) {
|
|
906
|
+
if (exponent < -10) return sign;
|
|
907
|
+
const m = (mantissa | 8388608) >> 1 - exponent;
|
|
908
|
+
return sign | m + 4096 >> 13;
|
|
909
|
+
}
|
|
910
|
+
if (exponent >= 31) {
|
|
911
|
+
return sign | 31744;
|
|
912
|
+
}
|
|
913
|
+
return sign | exponent << 10 | mantissa + 4096 >> 13;
|
|
914
|
+
}
|
|
915
|
+
function toLinear(value) {
|
|
916
|
+
const abs = Math.abs(value);
|
|
917
|
+
if (abs <= 1) {
|
|
918
|
+
const linear2 = Math.pow(abs, 2.2);
|
|
919
|
+
return value < 0 ? -linear2 : linear2;
|
|
920
|
+
}
|
|
921
|
+
const linear = Math.pow(LOG_BASE, abs - 1);
|
|
922
|
+
return value < 0 ? -linear : linear;
|
|
923
|
+
}
|
|
924
|
+
function convertToHalf(src, dst, idx) {
|
|
925
|
+
for (let i = 0; i < 64; i++) {
|
|
926
|
+
dst[idx + i] = float32ToFloat162(toLinear(src[i]));
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
function lossyDctDecode(cscSet, rowPtrs, channelData, acBuffer, dcBuffer, outBuffer, acOffset, dcOffset) {
|
|
930
|
+
const c0 = cscSet.idx[0];
|
|
931
|
+
const c1 = cscSet.idx[1];
|
|
932
|
+
const c2 = cscSet.idx[2];
|
|
933
|
+
if (c0 === void 0 || c1 === void 0 || c2 === void 0) {
|
|
934
|
+
return { acConsumed: 0, dcConsumed: 0 };
|
|
935
|
+
}
|
|
936
|
+
const cscIndices = [c0, c1, c2];
|
|
937
|
+
const dataView = new DataView(outBuffer.buffer, outBuffer.byteOffset, outBuffer.byteLength);
|
|
938
|
+
const outWords = new Uint16Array(
|
|
939
|
+
outBuffer.buffer,
|
|
940
|
+
outBuffer.byteOffset,
|
|
941
|
+
outBuffer.byteLength >> 1
|
|
942
|
+
);
|
|
943
|
+
const width = channelData[c0].width;
|
|
944
|
+
const height = channelData[c0].height;
|
|
945
|
+
const numComp = 3;
|
|
946
|
+
const numFullBlocksX = Math.floor(width / 8);
|
|
947
|
+
const numBlocksX = Math.ceil(width / 8);
|
|
948
|
+
const numBlocksY = Math.ceil(height / 8);
|
|
949
|
+
const leftoverX = width - (numBlocksX - 1) * 8;
|
|
950
|
+
const leftoverY = height - (numBlocksY - 1) * 8;
|
|
951
|
+
const currAcComp = { value: acOffset };
|
|
952
|
+
const currDcComp = new Array(numComp);
|
|
953
|
+
const dctData = new Array(numComp);
|
|
954
|
+
const halfZigBlock = new Array(numComp);
|
|
955
|
+
const rowBlock = new Array(numComp);
|
|
956
|
+
const rowOffsets = new Array(numComp);
|
|
957
|
+
for (let comp = 0; comp < numComp; comp++) {
|
|
958
|
+
rowOffsets[comp] = rowPtrs[cscIndices[comp]];
|
|
959
|
+
currDcComp[comp] = comp < 1 ? dcOffset : currDcComp[comp - 1] + numBlocksX * numBlocksY;
|
|
960
|
+
dctData[comp] = new Float32Array(64);
|
|
961
|
+
halfZigBlock[comp] = new Uint16Array(64);
|
|
962
|
+
rowBlock[comp] = new Uint16Array(numBlocksX * 64);
|
|
963
|
+
}
|
|
964
|
+
for (let blockY = 0; blockY < numBlocksY; blockY++) {
|
|
965
|
+
let maxY = 8;
|
|
966
|
+
if (blockY === numBlocksY - 1) maxY = leftoverY;
|
|
967
|
+
let maxX = 8;
|
|
968
|
+
for (let blockX = 0; blockX < numBlocksX; blockX++) {
|
|
969
|
+
if (blockX === numBlocksX - 1) maxX = leftoverX;
|
|
970
|
+
for (let comp = 0; comp < numComp; comp++) {
|
|
971
|
+
halfZigBlock[comp].fill(0);
|
|
972
|
+
if (currDcComp[comp] >= dcBuffer.length) {
|
|
973
|
+
throw new Error("Invalid DWA DC coefficient bounds.");
|
|
974
|
+
}
|
|
975
|
+
halfZigBlock[comp][0] = dcBuffer[currDcComp[comp]++];
|
|
976
|
+
unRleAC(currAcComp, acBuffer, halfZigBlock[comp]);
|
|
977
|
+
unZigZag(halfZigBlock[comp], dctData[comp]);
|
|
978
|
+
dctInverse(dctData[comp]);
|
|
979
|
+
}
|
|
980
|
+
csc709Inverse(dctData);
|
|
981
|
+
for (let comp = 0; comp < numComp; comp++) {
|
|
982
|
+
convertToHalf(dctData[comp], rowBlock[comp], blockX * 64);
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
for (let comp = 0; comp < numComp; comp++) {
|
|
986
|
+
const channelIndex = cscIndices[comp];
|
|
987
|
+
const sampleWords = channelData[channelIndex].sampleWords;
|
|
988
|
+
for (let y = 8 * blockY; y < 8 * blockY + maxY; y++) {
|
|
989
|
+
let offsetWords = rowOffsets[comp][y] >> 1;
|
|
990
|
+
for (let blockX = 0; blockX < numFullBlocksX; blockX++) {
|
|
991
|
+
const src = blockX * 64 + (y & 7) * 8;
|
|
992
|
+
outWords[offsetWords + 0 * sampleWords] = rowBlock[comp][src + 0];
|
|
993
|
+
outWords[offsetWords + 1 * sampleWords] = rowBlock[comp][src + 1];
|
|
994
|
+
outWords[offsetWords + 2 * sampleWords] = rowBlock[comp][src + 2];
|
|
995
|
+
outWords[offsetWords + 3 * sampleWords] = rowBlock[comp][src + 3];
|
|
996
|
+
outWords[offsetWords + 4 * sampleWords] = rowBlock[comp][src + 4];
|
|
997
|
+
outWords[offsetWords + 5 * sampleWords] = rowBlock[comp][src + 5];
|
|
998
|
+
outWords[offsetWords + 6 * sampleWords] = rowBlock[comp][src + 6];
|
|
999
|
+
outWords[offsetWords + 7 * sampleWords] = rowBlock[comp][src + 7];
|
|
1000
|
+
offsetWords += 8 * sampleWords;
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
if (numFullBlocksX !== numBlocksX) {
|
|
1004
|
+
for (let y = 8 * blockY; y < 8 * blockY + maxY; y++) {
|
|
1005
|
+
const offsetWords = (rowOffsets[comp][y] >> 1) + 8 * numFullBlocksX * sampleWords;
|
|
1006
|
+
const src = numFullBlocksX * 64 + (y & 7) * 8;
|
|
1007
|
+
for (let x = 0; x < maxX; x++) {
|
|
1008
|
+
outWords[offsetWords + x * sampleWords] = rowBlock[comp][src + x];
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1013
|
+
}
|
|
1014
|
+
for (let comp = 0; comp < numComp; comp++) {
|
|
1015
|
+
const channelIndex = cscIndices[comp];
|
|
1016
|
+
const channel = channelData[channelIndex];
|
|
1017
|
+
channel.decoded = true;
|
|
1018
|
+
if (channel.pixelType !== 2) {
|
|
1019
|
+
continue;
|
|
1020
|
+
}
|
|
1021
|
+
const sampleWords = channel.sampleWords;
|
|
1022
|
+
for (let y = 0; y < height; y++) {
|
|
1023
|
+
const offset = rowOffsets[comp][y];
|
|
1024
|
+
let srcWords = offset >> 1;
|
|
1025
|
+
for (let x = 0; x < width; x++) {
|
|
1026
|
+
dataView.setFloat32(
|
|
1027
|
+
offset + x * INT16_SIZE * sampleWords,
|
|
1028
|
+
float16ToFloat32(outWords[srcWords]),
|
|
1029
|
+
true
|
|
1030
|
+
);
|
|
1031
|
+
srcWords += sampleWords;
|
|
1032
|
+
}
|
|
1033
|
+
}
|
|
1034
|
+
}
|
|
1035
|
+
return {
|
|
1036
|
+
acConsumed: currAcComp.value - acOffset,
|
|
1037
|
+
dcConsumed: currDcComp[numComp - 1] - dcOffset
|
|
1038
|
+
};
|
|
1039
|
+
}
|
|
1040
|
+
function lossyDctChannelDecode(channelIndex, rowPtrs, channelData, acBuffer, dcBuffer, outBuffer, acOffset, dcOffset) {
|
|
1041
|
+
const dataView = new DataView(outBuffer.buffer, outBuffer.byteOffset, outBuffer.byteLength);
|
|
1042
|
+
const outWords = new Uint16Array(
|
|
1043
|
+
outBuffer.buffer,
|
|
1044
|
+
outBuffer.byteOffset,
|
|
1045
|
+
outBuffer.byteLength >> 1
|
|
1046
|
+
);
|
|
1047
|
+
const channel = channelData[channelIndex];
|
|
1048
|
+
const width = channel.width;
|
|
1049
|
+
const height = channel.height;
|
|
1050
|
+
const numBlocksX = Math.ceil(width / 8);
|
|
1051
|
+
const numBlocksY = Math.ceil(height / 8);
|
|
1052
|
+
const numFullBlocksX = Math.floor(width / 8);
|
|
1053
|
+
const leftoverX = width - (numBlocksX - 1) * 8;
|
|
1054
|
+
const leftoverY = height - (numBlocksY - 1) * 8;
|
|
1055
|
+
const currAcComp = { value: acOffset };
|
|
1056
|
+
let currDcComp = dcOffset;
|
|
1057
|
+
const dctData = new Float32Array(64);
|
|
1058
|
+
const halfZigBlock = new Uint16Array(64);
|
|
1059
|
+
const rowBlock = new Uint16Array(numBlocksX * 64);
|
|
1060
|
+
for (let blockY = 0; blockY < numBlocksY; blockY++) {
|
|
1061
|
+
let maxY = 8;
|
|
1062
|
+
if (blockY === numBlocksY - 1) maxY = leftoverY;
|
|
1063
|
+
for (let blockX = 0; blockX < numBlocksX; blockX++) {
|
|
1064
|
+
halfZigBlock.fill(0);
|
|
1065
|
+
if (currDcComp >= dcBuffer.length) {
|
|
1066
|
+
throw new Error("Invalid DWA DC coefficient bounds.");
|
|
1067
|
+
}
|
|
1068
|
+
halfZigBlock[0] = dcBuffer[currDcComp++];
|
|
1069
|
+
unRleAC(currAcComp, acBuffer, halfZigBlock);
|
|
1070
|
+
unZigZag(halfZigBlock, dctData);
|
|
1071
|
+
dctInverse(dctData);
|
|
1072
|
+
convertToHalf(dctData, rowBlock, blockX * 64);
|
|
1073
|
+
}
|
|
1074
|
+
for (let y = 8 * blockY; y < 8 * blockY + maxY; y++) {
|
|
1075
|
+
let offsetWords = rowPtrs[channelIndex][y] >> 1;
|
|
1076
|
+
for (let blockX = 0; blockX < numFullBlocksX; blockX++) {
|
|
1077
|
+
const src = blockX * 64 + (y & 7) * 8;
|
|
1078
|
+
for (let x = 0; x < 8; x++) {
|
|
1079
|
+
outWords[offsetWords + x * channel.sampleWords] = rowBlock[src + x];
|
|
1080
|
+
}
|
|
1081
|
+
offsetWords += 8 * channel.sampleWords;
|
|
1082
|
+
}
|
|
1083
|
+
if (numBlocksX !== numFullBlocksX) {
|
|
1084
|
+
const src = numFullBlocksX * 64 + (y & 7) * 8;
|
|
1085
|
+
for (let x = 0; x < leftoverX; x++) {
|
|
1086
|
+
outWords[offsetWords + x * channel.sampleWords] = rowBlock[src + x];
|
|
1087
|
+
}
|
|
1088
|
+
}
|
|
1089
|
+
}
|
|
1090
|
+
}
|
|
1091
|
+
if (channel.pixelType === 2) {
|
|
1092
|
+
for (let y = 0; y < height; y++) {
|
|
1093
|
+
const offset = rowPtrs[channelIndex][y];
|
|
1094
|
+
let srcWords = offset >> 1;
|
|
1095
|
+
for (let x = 0; x < width; x++) {
|
|
1096
|
+
dataView.setFloat32(
|
|
1097
|
+
offset + x * INT16_SIZE * channel.sampleWords,
|
|
1098
|
+
float16ToFloat32(outWords[srcWords]),
|
|
1099
|
+
true
|
|
1100
|
+
);
|
|
1101
|
+
srcWords += channel.sampleWords;
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
channel.decoded = true;
|
|
1106
|
+
return {
|
|
1107
|
+
acConsumed: currAcComp.value - acOffset,
|
|
1108
|
+
dcConsumed: currDcComp - dcOffset
|
|
1109
|
+
};
|
|
1110
|
+
}
|
|
1111
|
+
function suffixForChannel(name) {
|
|
1112
|
+
const index = name.lastIndexOf(".");
|
|
1113
|
+
return index >= 0 ? name.slice(index + 1) : name;
|
|
1114
|
+
}
|
|
1115
|
+
function matchesRule(channelName, rule) {
|
|
1116
|
+
if (rule.caseInsensitive) {
|
|
1117
|
+
return channelName.localeCompare(rule.name, void 0, { sensitivity: "accent" }) === 0;
|
|
1118
|
+
}
|
|
1119
|
+
return channelName === rule.name;
|
|
1120
|
+
}
|
|
1121
|
+
function parseDwaHeader(view, cursor) {
|
|
1122
|
+
return {
|
|
1123
|
+
version: readInt64(view, cursor),
|
|
1124
|
+
unknownUncompressedSize: readInt64(view, cursor),
|
|
1125
|
+
unknownCompressedSize: readInt64(view, cursor),
|
|
1126
|
+
acCompressedSize: readInt64(view, cursor),
|
|
1127
|
+
dcCompressedSize: readInt64(view, cursor),
|
|
1128
|
+
rleCompressedSize: readInt64(view, cursor),
|
|
1129
|
+
rleUncompressedSize: readInt64(view, cursor),
|
|
1130
|
+
rleRawSize: readInt64(view, cursor),
|
|
1131
|
+
totalAcUncompressedCount: readInt64(view, cursor),
|
|
1132
|
+
totalDcUncompressedCount: readInt64(view, cursor),
|
|
1133
|
+
acCompression: readInt64(view, cursor)
|
|
1134
|
+
};
|
|
1135
|
+
}
|
|
1136
|
+
function parseDwaRules(bytes, view, cursor) {
|
|
1137
|
+
const rulesSize = readUint16(view, cursor);
|
|
1138
|
+
if (rulesSize < 2) {
|
|
1139
|
+
throw new Error("Invalid DWA channel rules payload size.");
|
|
1140
|
+
}
|
|
1141
|
+
const rulesEnd = cursor.value + (rulesSize - 2);
|
|
1142
|
+
if (rulesEnd > bytes.length) {
|
|
1143
|
+
throw new Error("Truncated DWA channel rules payload.");
|
|
1144
|
+
}
|
|
1145
|
+
const rules = [];
|
|
1146
|
+
while (cursor.value < rulesEnd) {
|
|
1147
|
+
const name = readCStringWithin(bytes, cursor, rulesEnd, "DWA channel rule name");
|
|
1148
|
+
const value = readUint8(view, cursor);
|
|
1149
|
+
const compression = value >> 2 & 3;
|
|
1150
|
+
const index = (value >> 4) - 1;
|
|
1151
|
+
const caseInsensitive = (value & 1) !== 0;
|
|
1152
|
+
const pixelType = readUint8(view, cursor);
|
|
1153
|
+
rules.push({
|
|
1154
|
+
name,
|
|
1155
|
+
compression,
|
|
1156
|
+
index,
|
|
1157
|
+
pixelType,
|
|
1158
|
+
caseInsensitive
|
|
1159
|
+
});
|
|
1160
|
+
}
|
|
1161
|
+
if (cursor.value !== rulesEnd) {
|
|
1162
|
+
throw new Error("Invalid DWA channel rules alignment.");
|
|
1163
|
+
}
|
|
1164
|
+
return rules;
|
|
1165
|
+
}
|
|
1166
|
+
function decodeDwaChunk(context) {
|
|
1167
|
+
if (!context.part.dataWindow) {
|
|
1168
|
+
throw new Error("Missing DWA part data window.");
|
|
1169
|
+
}
|
|
1170
|
+
if (context.linesInChunk <= 0) {
|
|
1171
|
+
return new Uint8Array(0);
|
|
1172
|
+
}
|
|
1173
|
+
const xMin = context.part.dataWindow.xMin;
|
|
1174
|
+
const xMax = context.part.dataWindow.xMax;
|
|
1175
|
+
const columns = xMax - xMin + 1;
|
|
1176
|
+
const lines = context.linesInChunk;
|
|
1177
|
+
for (const channel of context.part.channels) {
|
|
1178
|
+
const xSampling = channel.xSampling > 0 ? channel.xSampling : 1;
|
|
1179
|
+
const ySampling = channel.ySampling > 0 ? channel.ySampling : 1;
|
|
1180
|
+
if (xSampling !== 1 || ySampling !== 1) {
|
|
1181
|
+
throw new Error(
|
|
1182
|
+
"DWAA/DWAB decoding currently requires xSampling=1 and ySampling=1 for all channels."
|
|
1183
|
+
);
|
|
1184
|
+
}
|
|
1185
|
+
}
|
|
1186
|
+
const bytes = new Uint8Array(context.buffer, context.dataPtr, context.dataSize);
|
|
1187
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
1188
|
+
const cursor = { value: 0 };
|
|
1189
|
+
const header = parseDwaHeader(view, cursor);
|
|
1190
|
+
if (header.version < 1 || header.version > 2) {
|
|
1191
|
+
throw new Error(`Unsupported DWA version ${header.version}.`);
|
|
1192
|
+
}
|
|
1193
|
+
const rules = header.version >= 2 ? parseDwaRules(bytes, view, cursor) : LEGACY_DWA_RULES;
|
|
1194
|
+
if (header.unknownCompressedSize > 0 || header.unknownUncompressedSize > 0) {
|
|
1195
|
+
throw new Error("DWAA/DWAB UNKNOWN channel blocks are not supported.");
|
|
1196
|
+
}
|
|
1197
|
+
const channelData = context.part.channels.map((channel) => ({
|
|
1198
|
+
name: channel.name,
|
|
1199
|
+
compression: UNKNOWN,
|
|
1200
|
+
decoded: false,
|
|
1201
|
+
pixelType: channel.pixelType,
|
|
1202
|
+
sampleWords: channel.pixelType === 1 ? 1 : 2,
|
|
1203
|
+
pLinear: channel.pLinear,
|
|
1204
|
+
width: columns,
|
|
1205
|
+
height: lines
|
|
1206
|
+
}));
|
|
1207
|
+
const cscSet = { idx: new Array(3) };
|
|
1208
|
+
for (let offset = 0; offset < channelData.length; offset++) {
|
|
1209
|
+
const channel = channelData[offset];
|
|
1210
|
+
const suffix = suffixForChannel(channel.name);
|
|
1211
|
+
for (const rule of rules) {
|
|
1212
|
+
if (rule.pixelType !== channel.pixelType) continue;
|
|
1213
|
+
if (!matchesRule(suffix, rule)) continue;
|
|
1214
|
+
channel.compression = rule.compression;
|
|
1215
|
+
if (rule.index >= 0) {
|
|
1216
|
+
cscSet.idx[rule.index] = offset;
|
|
1217
|
+
}
|
|
1218
|
+
break;
|
|
1219
|
+
}
|
|
1220
|
+
}
|
|
1221
|
+
for (const channel of channelData) {
|
|
1222
|
+
if (channel.compression !== LOSSY_DCT && channel.compression !== RLE) {
|
|
1223
|
+
throw new Error(`Unsupported DWA channel compression for channel ${channel.name}.`);
|
|
1224
|
+
}
|
|
1225
|
+
if (channel.compression === LOSSY_DCT && channel.pixelType !== 1 && channel.pixelType !== 2) {
|
|
1226
|
+
throw new Error(`Unsupported DWA lossy channel pixel type for channel ${channel.name}.`);
|
|
1227
|
+
}
|
|
1228
|
+
}
|
|
1229
|
+
let acBuffer = new Uint16Array(0);
|
|
1230
|
+
let dcBuffer = new Uint16Array(0);
|
|
1231
|
+
let rleBuffer = new Uint8Array(0);
|
|
1232
|
+
if (header.acCompressedSize > 0) {
|
|
1233
|
+
if (cursor.value + header.acCompressedSize > bytes.length) {
|
|
1234
|
+
throw new Error("Truncated DWA AC payload.");
|
|
1235
|
+
}
|
|
1236
|
+
switch (header.acCompression) {
|
|
1237
|
+
case STATIC_HUFFMAN: {
|
|
1238
|
+
acBuffer = new Uint16Array(header.totalAcUncompressedCount);
|
|
1239
|
+
const acStart = cursor.value;
|
|
1240
|
+
hufUncompress(
|
|
1241
|
+
bytes,
|
|
1242
|
+
view,
|
|
1243
|
+
cursor,
|
|
1244
|
+
header.acCompressedSize,
|
|
1245
|
+
acBuffer,
|
|
1246
|
+
header.totalAcUncompressedCount
|
|
1247
|
+
);
|
|
1248
|
+
cursor.value = acStart + header.acCompressedSize;
|
|
1249
|
+
break;
|
|
1250
|
+
}
|
|
1251
|
+
case DEFLATE: {
|
|
1252
|
+
const compressed = bytes.subarray(cursor.value, cursor.value + header.acCompressedSize);
|
|
1253
|
+
const decoded = unzlibSync(compressed, {
|
|
1254
|
+
out: new Uint8Array(header.totalAcUncompressedCount * INT16_SIZE)
|
|
1255
|
+
});
|
|
1256
|
+
if (decoded.byteLength % 2 !== 0) {
|
|
1257
|
+
throw new Error("Invalid DWA AC DEFLATE payload size.");
|
|
1258
|
+
}
|
|
1259
|
+
const decodedWords = decoded.byteLength / 2;
|
|
1260
|
+
if (decodedWords !== header.totalAcUncompressedCount) {
|
|
1261
|
+
throw new Error("Invalid DWA AC DEFLATE payload length.");
|
|
1262
|
+
}
|
|
1263
|
+
const decodedView = new Uint16Array(decoded.buffer, decoded.byteOffset, decodedWords);
|
|
1264
|
+
acBuffer = new Uint16Array(decodedWords);
|
|
1265
|
+
acBuffer.set(decodedView);
|
|
1266
|
+
cursor.value += header.acCompressedSize;
|
|
1267
|
+
break;
|
|
1268
|
+
}
|
|
1269
|
+
default:
|
|
1270
|
+
throw new Error(`Unsupported DWA AC compression mode ${header.acCompression}.`);
|
|
1271
|
+
}
|
|
1272
|
+
} else if (header.totalAcUncompressedCount !== 0) {
|
|
1273
|
+
throw new Error("Invalid DWA AC payload metadata.");
|
|
1274
|
+
}
|
|
1275
|
+
if (header.dcCompressedSize > 0) {
|
|
1276
|
+
const dcBytes = uncompressZipStyle(
|
|
1277
|
+
bytes,
|
|
1278
|
+
cursor.value,
|
|
1279
|
+
header.dcCompressedSize,
|
|
1280
|
+
header.totalDcUncompressedCount * INT16_SIZE,
|
|
1281
|
+
DWA_DC_SCRATCH
|
|
1282
|
+
);
|
|
1283
|
+
if (dcBytes.byteLength % 2 !== 0) {
|
|
1284
|
+
throw new Error("Invalid DWA DC payload size.");
|
|
1285
|
+
}
|
|
1286
|
+
const dcWords = dcBytes.byteLength / 2;
|
|
1287
|
+
if (dcWords !== header.totalDcUncompressedCount) {
|
|
1288
|
+
throw new Error("Invalid DWA DC payload length.");
|
|
1289
|
+
}
|
|
1290
|
+
const dcView = new Uint16Array(dcBytes.buffer, dcBytes.byteOffset, dcWords);
|
|
1291
|
+
dcBuffer = new Uint16Array(dcWords);
|
|
1292
|
+
dcBuffer.set(dcView);
|
|
1293
|
+
cursor.value += header.dcCompressedSize;
|
|
1294
|
+
} else if (header.totalDcUncompressedCount !== 0) {
|
|
1295
|
+
throw new Error("Invalid DWA DC payload metadata.");
|
|
1296
|
+
}
|
|
1297
|
+
if (header.rleRawSize > 0) {
|
|
1298
|
+
if (cursor.value + header.rleCompressedSize > bytes.length) {
|
|
1299
|
+
throw new Error("Truncated DWA RLE payload.");
|
|
1300
|
+
}
|
|
1301
|
+
const compressed = bytes.subarray(cursor.value, cursor.value + header.rleCompressedSize);
|
|
1302
|
+
const decoded = unzlibSync(compressed, {
|
|
1303
|
+
out: new Uint8Array(header.rleUncompressedSize)
|
|
1304
|
+
});
|
|
1305
|
+
if (decoded.byteLength !== header.rleUncompressedSize) {
|
|
1306
|
+
throw new Error("Invalid DWA RLE intermediate payload length.");
|
|
1307
|
+
}
|
|
1308
|
+
const decodedRle = decodeRunLength(decoded, header.rleRawSize);
|
|
1309
|
+
rleBuffer = new Uint8Array(decodedRle.byteLength);
|
|
1310
|
+
rleBuffer.set(decodedRle);
|
|
1311
|
+
cursor.value += header.rleCompressedSize;
|
|
1312
|
+
} else if (header.rleCompressedSize !== 0 || header.rleUncompressedSize !== 0) {
|
|
1313
|
+
throw new Error("Invalid DWA RLE payload metadata.");
|
|
1314
|
+
}
|
|
1315
|
+
let outputSize = 0;
|
|
1316
|
+
const rowOffsets = new Array(channelData.length);
|
|
1317
|
+
for (let i = 0; i < channelData.length; i++) {
|
|
1318
|
+
rowOffsets[i] = new Int32Array(lines);
|
|
1319
|
+
}
|
|
1320
|
+
for (let y = 0; y < lines; y++) {
|
|
1321
|
+
for (let chan = 0; chan < channelData.length; chan++) {
|
|
1322
|
+
rowOffsets[chan][y] = outputSize;
|
|
1323
|
+
outputSize += channelData[chan].width * channelData[chan].sampleWords * INT16_SIZE;
|
|
1324
|
+
}
|
|
1325
|
+
}
|
|
1326
|
+
const outBuffer = new Uint8Array(outputSize);
|
|
1327
|
+
let acOffset = 0;
|
|
1328
|
+
let dcOffset = 0;
|
|
1329
|
+
if (cscSet.idx[0] !== void 0 && cscSet.idx[1] !== void 0 && cscSet.idx[2] !== void 0) {
|
|
1330
|
+
const usage = lossyDctDecode(
|
|
1331
|
+
cscSet,
|
|
1332
|
+
rowOffsets,
|
|
1333
|
+
channelData,
|
|
1334
|
+
acBuffer,
|
|
1335
|
+
dcBuffer,
|
|
1336
|
+
outBuffer,
|
|
1337
|
+
acOffset,
|
|
1338
|
+
dcOffset
|
|
1339
|
+
);
|
|
1340
|
+
acOffset += usage.acConsumed;
|
|
1341
|
+
dcOffset += usage.dcConsumed;
|
|
1342
|
+
}
|
|
1343
|
+
let rleOffset = 0;
|
|
1344
|
+
for (let i = 0; i < channelData.length; i++) {
|
|
1345
|
+
const channel = channelData[i];
|
|
1346
|
+
if (channel.decoded) continue;
|
|
1347
|
+
switch (channel.compression) {
|
|
1348
|
+
case RLE: {
|
|
1349
|
+
for (let y = 0; y < lines; y++) {
|
|
1350
|
+
let rowOffsetBytes = rowOffsets[i][y];
|
|
1351
|
+
for (let x = 0; x < channel.width; x++) {
|
|
1352
|
+
for (let byte = 0; byte < channel.sampleWords * INT16_SIZE; byte++) {
|
|
1353
|
+
const src = rleOffset + byte * channel.width * channel.height;
|
|
1354
|
+
if (src < 0 || src >= rleBuffer.length) {
|
|
1355
|
+
throw new Error("Invalid DWA RLE planar bounds.");
|
|
1356
|
+
}
|
|
1357
|
+
outBuffer[rowOffsetBytes++] = rleBuffer[src];
|
|
1358
|
+
}
|
|
1359
|
+
rleOffset++;
|
|
1360
|
+
}
|
|
1361
|
+
}
|
|
1362
|
+
channel.decoded = true;
|
|
1363
|
+
break;
|
|
1364
|
+
}
|
|
1365
|
+
case LOSSY_DCT: {
|
|
1366
|
+
const usage = lossyDctChannelDecode(
|
|
1367
|
+
i,
|
|
1368
|
+
rowOffsets,
|
|
1369
|
+
channelData,
|
|
1370
|
+
acBuffer,
|
|
1371
|
+
dcBuffer,
|
|
1372
|
+
outBuffer,
|
|
1373
|
+
acOffset,
|
|
1374
|
+
dcOffset
|
|
1375
|
+
);
|
|
1376
|
+
acOffset += usage.acConsumed;
|
|
1377
|
+
dcOffset += usage.dcConsumed;
|
|
1378
|
+
break;
|
|
1379
|
+
}
|
|
1380
|
+
default:
|
|
1381
|
+
throw new Error(`Unsupported DWA channel compression for channel ${channel.name}.`);
|
|
1382
|
+
}
|
|
1383
|
+
}
|
|
1384
|
+
return outBuffer;
|
|
1385
|
+
}
|
|
1386
|
+
function decodeDwaBlock(context) {
|
|
1387
|
+
try {
|
|
1388
|
+
return decodeDwaChunk(context);
|
|
1389
|
+
} catch (error) {
|
|
1390
|
+
if (error instanceof ExrError) {
|
|
1391
|
+
throw error;
|
|
1392
|
+
}
|
|
1393
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
1394
|
+
throw new ExrError("DECOMPRESSION_FAILED", "Failed to decompress DWAA/DWAB chunk.", {
|
|
1395
|
+
partId: context.partId,
|
|
1396
|
+
chunkIndex: context.chunkIndex,
|
|
1397
|
+
size: context.dataSize,
|
|
1398
|
+
reason
|
|
1399
|
+
});
|
|
1400
|
+
}
|
|
1401
|
+
}
|
|
1402
|
+
|
|
1403
|
+
// src/reader/compression/piz.ts
|
|
1404
|
+
var USHORT_RANGE = 1 << 16;
|
|
1405
|
+
var BITMAP_SIZE = USHORT_RANGE >> 3;
|
|
1406
|
+
var HUF_ENCBITS2 = 16;
|
|
1407
|
+
var HUF_DECBITS2 = 14;
|
|
1408
|
+
var HUF_ENCSIZE2 = (1 << HUF_ENCBITS2) + 1;
|
|
1409
|
+
var HUF_DECSIZE2 = 1 << HUF_DECBITS2;
|
|
1410
|
+
var HUF_DECMASK2 = HUF_DECSIZE2 - 1;
|
|
1411
|
+
var SHORT_ZEROCODE_RUN2 = 59;
|
|
1412
|
+
var LONG_ZEROCODE_RUN2 = 63;
|
|
1413
|
+
var SHORTEST_LONG_RUN2 = 2 + LONG_ZEROCODE_RUN2 - SHORT_ZEROCODE_RUN2;
|
|
1414
|
+
var NBITS = 16;
|
|
1415
|
+
var A_OFFSET = 1 << NBITS - 1;
|
|
1416
|
+
var MOD_MASK = (1 << NBITS) - 1;
|
|
1417
|
+
function modulo2(value, base) {
|
|
1418
|
+
const result = value % base;
|
|
1419
|
+
return result < 0 ? result + base : result;
|
|
1420
|
+
}
|
|
1421
|
+
function firstSampleCoordinate2(min, sampling) {
|
|
1422
|
+
if (sampling <= 1) return min;
|
|
1423
|
+
const remainder = modulo2(min, sampling);
|
|
1424
|
+
return remainder === 0 ? min : min + (sampling - remainder);
|
|
1425
|
+
}
|
|
1426
|
+
function countSamplesInRange2(min, max, sampling) {
|
|
1427
|
+
if (sampling <= 0 || max < min) return 0;
|
|
1428
|
+
const first = firstSampleCoordinate2(min, sampling);
|
|
1429
|
+
if (first > max) return 0;
|
|
1430
|
+
return Math.floor((max - first) / sampling) + 1;
|
|
1431
|
+
}
|
|
1432
|
+
function isSampledCoordinate2(value, firstSample, sampling) {
|
|
1433
|
+
if (sampling <= 1) return true;
|
|
1434
|
+
if (value < firstSample) return false;
|
|
1435
|
+
return (value - firstSample) % sampling === 0;
|
|
1436
|
+
}
|
|
1437
|
+
function countSampledRowsInRange(start, end, sampleOrigin, sampling) {
|
|
1438
|
+
if (end < start) return 0;
|
|
1439
|
+
if (sampling <= 1) return end - start + 1;
|
|
1440
|
+
let count = 0;
|
|
1441
|
+
for (let y = start; y <= end; y++) {
|
|
1442
|
+
if (isSampledCoordinate2(y, sampleOrigin, sampling)) {
|
|
1443
|
+
count++;
|
|
1444
|
+
}
|
|
1445
|
+
}
|
|
1446
|
+
return count;
|
|
1447
|
+
}
|
|
1448
|
+
function readUint162(view, cursor) {
|
|
1449
|
+
if (cursor.value + 2 > view.byteLength) {
|
|
1450
|
+
throw new Error("Truncated PIZ payload.");
|
|
1451
|
+
}
|
|
1452
|
+
const value = view.getUint16(cursor.value, true);
|
|
1453
|
+
cursor.value += 2;
|
|
1454
|
+
return value;
|
|
1455
|
+
}
|
|
1456
|
+
function readUint322(view, cursor) {
|
|
1457
|
+
if (cursor.value + 4 > view.byteLength) {
|
|
1458
|
+
throw new Error("Truncated PIZ payload.");
|
|
1459
|
+
}
|
|
1460
|
+
const value = view.getUint32(cursor.value, true);
|
|
1461
|
+
cursor.value += 4;
|
|
1462
|
+
return value;
|
|
1463
|
+
}
|
|
1464
|
+
function reverseLutFromBitmap(bitmap, lut) {
|
|
1465
|
+
let k = 0;
|
|
1466
|
+
for (let i = 0; i < USHORT_RANGE; i++) {
|
|
1467
|
+
if (i === 0 || bitmap[i >> 3] & 1 << (i & 7)) {
|
|
1468
|
+
lut[k++] = i;
|
|
1469
|
+
}
|
|
1470
|
+
}
|
|
1471
|
+
const n = k - 1;
|
|
1472
|
+
while (k < USHORT_RANGE) {
|
|
1473
|
+
lut[k++] = 0;
|
|
1474
|
+
}
|
|
1475
|
+
return n;
|
|
1476
|
+
}
|
|
1477
|
+
function clearHuffmanDecodingTable2(table) {
|
|
1478
|
+
for (let i = 0; i < HUF_DECSIZE2; i++) {
|
|
1479
|
+
table[i] = { len: 0, lit: 0, p: null };
|
|
1480
|
+
}
|
|
1481
|
+
}
|
|
1482
|
+
function getBits2(nBits, state, input, cursor) {
|
|
1483
|
+
while (state.lc < nBits) {
|
|
1484
|
+
state.c = (state.c << 8 | readUint8FromArray2(input, cursor)) >>> 0;
|
|
1485
|
+
state.lc += 8;
|
|
1486
|
+
}
|
|
1487
|
+
state.lc -= nBits;
|
|
1488
|
+
return state.c >>> state.lc & (1 << nBits) - 1;
|
|
1489
|
+
}
|
|
1490
|
+
var HUF_TABLE_BUFFER2 = new Int32Array(59);
|
|
1491
|
+
function hufCanonicalCodeTable2(hcode) {
|
|
1492
|
+
HUF_TABLE_BUFFER2.fill(0);
|
|
1493
|
+
for (let i = 0; i < HUF_ENCSIZE2; i++) {
|
|
1494
|
+
HUF_TABLE_BUFFER2[hcode[i]] += 1;
|
|
1495
|
+
}
|
|
1496
|
+
let c = 0;
|
|
1497
|
+
for (let i = 58; i > 0; i--) {
|
|
1498
|
+
const nextCode = c + HUF_TABLE_BUFFER2[i] >> 1;
|
|
1499
|
+
HUF_TABLE_BUFFER2[i] = c;
|
|
1500
|
+
c = nextCode;
|
|
1501
|
+
}
|
|
1502
|
+
for (let i = 0; i < HUF_ENCSIZE2; i++) {
|
|
1503
|
+
const length = hcode[i];
|
|
1504
|
+
if (length > 0) {
|
|
1505
|
+
hcode[i] = length | HUF_TABLE_BUFFER2[length]++ << 6;
|
|
1506
|
+
}
|
|
1507
|
+
}
|
|
1508
|
+
}
|
|
1509
|
+
function hufUnpackEncTable2(input, cursor, maxTableBytes, minSymbol, maxSymbol, hcode) {
|
|
1510
|
+
const start = cursor.value;
|
|
1511
|
+
const state = { c: 0, lc: 0 };
|
|
1512
|
+
for (let symbol = minSymbol; symbol <= maxSymbol; symbol++) {
|
|
1513
|
+
if (cursor.value - start > maxTableBytes) {
|
|
1514
|
+
throw new Error("Invalid PIZ Huffman table payload.");
|
|
1515
|
+
}
|
|
1516
|
+
const length = getBits2(6, state, input, cursor);
|
|
1517
|
+
hcode[symbol] = length;
|
|
1518
|
+
if (length === LONG_ZEROCODE_RUN2) {
|
|
1519
|
+
if (cursor.value - start > maxTableBytes) {
|
|
1520
|
+
throw new Error("Invalid PIZ Huffman table payload.");
|
|
1521
|
+
}
|
|
1522
|
+
let zeroRun = getBits2(8, state, input, cursor) + SHORTEST_LONG_RUN2;
|
|
1523
|
+
if (symbol + zeroRun > maxSymbol + 1) {
|
|
1524
|
+
throw new Error("Invalid PIZ Huffman zero-run.");
|
|
1525
|
+
}
|
|
1526
|
+
while (zeroRun-- > 0) {
|
|
1527
|
+
hcode[symbol++] = 0;
|
|
1528
|
+
}
|
|
1529
|
+
symbol--;
|
|
1530
|
+
} else if (length >= SHORT_ZEROCODE_RUN2) {
|
|
1531
|
+
let zeroRun = length - SHORT_ZEROCODE_RUN2 + 2;
|
|
1532
|
+
if (symbol + zeroRun > maxSymbol + 1) {
|
|
1533
|
+
throw new Error("Invalid PIZ Huffman zero-run.");
|
|
1534
|
+
}
|
|
1535
|
+
while (zeroRun-- > 0) {
|
|
1536
|
+
hcode[symbol++] = 0;
|
|
1537
|
+
}
|
|
1538
|
+
symbol--;
|
|
1539
|
+
}
|
|
1540
|
+
}
|
|
1541
|
+
hufCanonicalCodeTable2(hcode);
|
|
1542
|
+
}
|
|
1543
|
+
function hufLength2(code) {
|
|
1544
|
+
return code & 63;
|
|
1545
|
+
}
|
|
1546
|
+
function hufCode2(code) {
|
|
1547
|
+
return code >>> 6;
|
|
1548
|
+
}
|
|
1549
|
+
function hufBuildDecTable2(hcode, minSymbol, maxSymbol, table) {
|
|
1550
|
+
for (let symbol = minSymbol; symbol <= maxSymbol; symbol++) {
|
|
1551
|
+
const code = hufCode2(hcode[symbol]);
|
|
1552
|
+
const length = hufLength2(hcode[symbol]);
|
|
1553
|
+
if (code >>> length) {
|
|
1554
|
+
throw new Error("Invalid PIZ Huffman code.");
|
|
1555
|
+
}
|
|
1556
|
+
if (length > HUF_DECBITS2) {
|
|
1557
|
+
const entry = table[code >>> length - HUF_DECBITS2];
|
|
1558
|
+
if (entry.len) {
|
|
1559
|
+
throw new Error("Invalid PIZ Huffman decode table entry.");
|
|
1560
|
+
}
|
|
1561
|
+
entry.lit += 1;
|
|
1562
|
+
if (entry.p) {
|
|
1563
|
+
entry.p.push(symbol);
|
|
1564
|
+
} else {
|
|
1565
|
+
entry.p = [symbol];
|
|
1566
|
+
}
|
|
1567
|
+
} else if (length) {
|
|
1568
|
+
const base = code << HUF_DECBITS2 - length;
|
|
1569
|
+
const count = 1 << HUF_DECBITS2 - length;
|
|
1570
|
+
for (let i = 0; i < count; i++) {
|
|
1571
|
+
const entry = table[base + i];
|
|
1572
|
+
if (entry.len || entry.p) {
|
|
1573
|
+
throw new Error("Invalid PIZ Huffman decode table entry.");
|
|
1574
|
+
}
|
|
1575
|
+
entry.len = length;
|
|
1576
|
+
entry.lit = symbol;
|
|
1577
|
+
}
|
|
1578
|
+
}
|
|
1579
|
+
}
|
|
1580
|
+
}
|
|
1581
|
+
function readUint8FromArray2(input, cursor) {
|
|
1582
|
+
if (cursor.value >= input.length) {
|
|
1583
|
+
throw new Error("Truncated PIZ Huffman payload.");
|
|
1584
|
+
}
|
|
1585
|
+
const value = input[cursor.value];
|
|
1586
|
+
cursor.value += 1;
|
|
1587
|
+
return value;
|
|
1588
|
+
}
|
|
1589
|
+
function getChar2(state, input, cursor) {
|
|
1590
|
+
state.c = (state.c << 8 | readUint8FromArray2(input, cursor)) >>> 0;
|
|
1591
|
+
state.lc += 8;
|
|
1592
|
+
}
|
|
1593
|
+
function getCode2(symbol, runLengthCode, state, input, cursor, outBuffer, outOffset, outEnd) {
|
|
1594
|
+
if (symbol === runLengthCode) {
|
|
1595
|
+
if (state.lc < 8) {
|
|
1596
|
+
getChar2(state, input, cursor);
|
|
1597
|
+
}
|
|
1598
|
+
state.lc -= 8;
|
|
1599
|
+
let repeatCount = state.c >>> state.lc & 255;
|
|
1600
|
+
if (outOffset.value === 0 || outOffset.value + repeatCount > outEnd) {
|
|
1601
|
+
throw new Error("Invalid PIZ run-length code.");
|
|
1602
|
+
}
|
|
1603
|
+
const last = outBuffer[outOffset.value - 1];
|
|
1604
|
+
while (repeatCount-- > 0) {
|
|
1605
|
+
outBuffer[outOffset.value++] = last;
|
|
1606
|
+
}
|
|
1607
|
+
} else if (outOffset.value < outEnd) {
|
|
1608
|
+
outBuffer[outOffset.value++] = symbol;
|
|
1609
|
+
} else {
|
|
1610
|
+
throw new Error("PIZ Huffman decode overflow.");
|
|
1611
|
+
}
|
|
1612
|
+
}
|
|
1613
|
+
function hufDecode2(encodingTable, decodingTable, input, cursor, nBits, runLengthCode, outSize, outBuffer, outOffset) {
|
|
1614
|
+
const state = { c: 0, lc: 0 };
|
|
1615
|
+
const inOffsetEnd = Math.trunc(cursor.value + (nBits + 7) / 8);
|
|
1616
|
+
while (cursor.value < inOffsetEnd) {
|
|
1617
|
+
getChar2(state, input, cursor);
|
|
1618
|
+
while (state.lc >= HUF_DECBITS2) {
|
|
1619
|
+
const index = state.c >>> state.lc - HUF_DECBITS2 & HUF_DECMASK2;
|
|
1620
|
+
const entry = decodingTable[index];
|
|
1621
|
+
if (entry.len) {
|
|
1622
|
+
state.lc -= entry.len;
|
|
1623
|
+
getCode2(entry.lit, runLengthCode, state, input, cursor, outBuffer, outOffset, outSize);
|
|
1624
|
+
} else {
|
|
1625
|
+
if (!entry.p) {
|
|
1626
|
+
throw new Error("PIZ Huffman decode failed.");
|
|
1627
|
+
}
|
|
1628
|
+
let matched = false;
|
|
1629
|
+
for (let i = 0; i < entry.lit; i++) {
|
|
1630
|
+
const symbol = entry.p[i];
|
|
1631
|
+
const length = hufLength2(encodingTable[symbol]);
|
|
1632
|
+
while (state.lc < length && cursor.value < inOffsetEnd) {
|
|
1633
|
+
getChar2(state, input, cursor);
|
|
1634
|
+
}
|
|
1635
|
+
if (state.lc >= length) {
|
|
1636
|
+
const code = hufCode2(encodingTable[symbol]);
|
|
1637
|
+
const candidate = state.c >>> state.lc - length & (1 << length) - 1;
|
|
1638
|
+
if (code === candidate) {
|
|
1639
|
+
state.lc -= length;
|
|
1640
|
+
getCode2(symbol, runLengthCode, state, input, cursor, outBuffer, outOffset, outSize);
|
|
1641
|
+
matched = true;
|
|
1642
|
+
break;
|
|
1643
|
+
}
|
|
1644
|
+
}
|
|
1645
|
+
}
|
|
1646
|
+
if (!matched) {
|
|
1647
|
+
throw new Error("PIZ Huffman decode failed.");
|
|
1648
|
+
}
|
|
1649
|
+
}
|
|
1650
|
+
}
|
|
1651
|
+
}
|
|
1652
|
+
const pad = 8 - nBits & 7;
|
|
1653
|
+
state.c >>>= pad;
|
|
1654
|
+
state.lc -= pad;
|
|
1655
|
+
while (state.lc > 0) {
|
|
1656
|
+
const entry = decodingTable[state.c << HUF_DECBITS2 - state.lc & HUF_DECMASK2];
|
|
1657
|
+
if (!entry.len) {
|
|
1658
|
+
throw new Error("PIZ Huffman decode failed.");
|
|
1659
|
+
}
|
|
1660
|
+
state.lc -= entry.len;
|
|
1661
|
+
getCode2(entry.lit, runLengthCode, state, input, cursor, outBuffer, outOffset, outSize);
|
|
1662
|
+
}
|
|
1663
|
+
}
|
|
1664
|
+
function hufUncompress2(input, view, cursor, nCompressed, out, nRaw) {
|
|
1665
|
+
const outOffset = { value: 0 };
|
|
1666
|
+
const initialOffset = cursor.value;
|
|
1667
|
+
const minSymbol = readUint322(view, cursor);
|
|
1668
|
+
const maxSymbol = readUint322(view, cursor);
|
|
1669
|
+
readUint322(view, cursor);
|
|
1670
|
+
const nBits = readUint322(view, cursor);
|
|
1671
|
+
readUint322(view, cursor);
|
|
1672
|
+
if (minSymbol < 0 || minSymbol >= HUF_ENCSIZE2 || maxSymbol < 0 || maxSymbol >= HUF_ENCSIZE2 || minSymbol > maxSymbol) {
|
|
1673
|
+
throw new Error("Invalid PIZ Huffman symbol bounds.");
|
|
1674
|
+
}
|
|
1675
|
+
const encodingTable = new Int32Array(HUF_ENCSIZE2);
|
|
1676
|
+
const decodingTable = new Array(HUF_DECSIZE2);
|
|
1677
|
+
clearHuffmanDecodingTable2(decodingTable);
|
|
1678
|
+
const tableBytes = nCompressed - (cursor.value - initialOffset);
|
|
1679
|
+
if (tableBytes < 0) {
|
|
1680
|
+
throw new Error("Invalid PIZ Huffman payload size.");
|
|
1681
|
+
}
|
|
1682
|
+
hufUnpackEncTable2(input, cursor, tableBytes, minSymbol, maxSymbol, encodingTable);
|
|
1683
|
+
if (nBits > 8 * (nCompressed - (cursor.value - initialOffset))) {
|
|
1684
|
+
throw new Error("Invalid PIZ Huffman bit count.");
|
|
1685
|
+
}
|
|
1686
|
+
hufBuildDecTable2(encodingTable, minSymbol, maxSymbol, decodingTable);
|
|
1687
|
+
hufDecode2(encodingTable, decodingTable, input, cursor, nBits, maxSymbol, nRaw, out, outOffset);
|
|
1688
|
+
if (outOffset.value !== nRaw) {
|
|
1689
|
+
throw new Error("PIZ Huffman output size mismatch.");
|
|
1690
|
+
}
|
|
1691
|
+
}
|
|
1692
|
+
function uint16(value) {
|
|
1693
|
+
return value & 65535;
|
|
1694
|
+
}
|
|
1695
|
+
function int16(value) {
|
|
1696
|
+
const clamped = uint16(value);
|
|
1697
|
+
return clamped > 32767 ? clamped - 65536 : clamped;
|
|
1698
|
+
}
|
|
1699
|
+
var waveletPair = { a: 0, b: 0 };
|
|
1700
|
+
function wdec14(low, high) {
|
|
1701
|
+
const ls = int16(low);
|
|
1702
|
+
const hs = int16(high);
|
|
1703
|
+
const ai = ls + (hs & 1) + (hs >> 1);
|
|
1704
|
+
const bi = ai - hs;
|
|
1705
|
+
waveletPair.a = ai;
|
|
1706
|
+
waveletPair.b = bi;
|
|
1707
|
+
}
|
|
1708
|
+
function wdec16(low, high) {
|
|
1709
|
+
const m = uint16(low);
|
|
1710
|
+
const d = uint16(high);
|
|
1711
|
+
const b = m - (d >> 1) & MOD_MASK;
|
|
1712
|
+
const a = d + b - A_OFFSET & MOD_MASK;
|
|
1713
|
+
waveletPair.a = a;
|
|
1714
|
+
waveletPair.b = b;
|
|
1715
|
+
}
|
|
1716
|
+
function wav2Decode(buffer, channelOffset, nx, ox, ny, oy, maxValue) {
|
|
1717
|
+
if (nx <= 0 || ny <= 0) {
|
|
1718
|
+
return;
|
|
1719
|
+
}
|
|
1720
|
+
const wavelet14 = maxValue < 1 << 14;
|
|
1721
|
+
const minDim = nx > ny ? ny : nx;
|
|
1722
|
+
let p = 1;
|
|
1723
|
+
while (p <= minDim) {
|
|
1724
|
+
p <<= 1;
|
|
1725
|
+
}
|
|
1726
|
+
let p2 = p >> 1;
|
|
1727
|
+
p >>= 2;
|
|
1728
|
+
while (p >= 1) {
|
|
1729
|
+
let py = 0;
|
|
1730
|
+
const ey = oy * (ny - p2);
|
|
1731
|
+
const oy1 = oy * p;
|
|
1732
|
+
const oy2 = oy * p2;
|
|
1733
|
+
const ox1 = ox * p;
|
|
1734
|
+
const ox2 = ox * p2;
|
|
1735
|
+
for (; py <= ey; py += oy2) {
|
|
1736
|
+
let px = py;
|
|
1737
|
+
const ex = py + ox * (nx - p2);
|
|
1738
|
+
for (; px <= ex; px += ox2) {
|
|
1739
|
+
const p01 = px + ox1;
|
|
1740
|
+
const p10 = px + oy1;
|
|
1741
|
+
const p11 = p10 + ox1;
|
|
1742
|
+
if (wavelet14) {
|
|
1743
|
+
wdec14(buffer[px + channelOffset], buffer[p10 + channelOffset]);
|
|
1744
|
+
const i00 = waveletPair.a;
|
|
1745
|
+
const i10 = waveletPair.b;
|
|
1746
|
+
wdec14(buffer[p01 + channelOffset], buffer[p11 + channelOffset]);
|
|
1747
|
+
const i01 = waveletPair.a;
|
|
1748
|
+
const i11 = waveletPair.b;
|
|
1749
|
+
wdec14(i00, i01);
|
|
1750
|
+
buffer[px + channelOffset] = waveletPair.a;
|
|
1751
|
+
buffer[p01 + channelOffset] = waveletPair.b;
|
|
1752
|
+
wdec14(i10, i11);
|
|
1753
|
+
buffer[p10 + channelOffset] = waveletPair.a;
|
|
1754
|
+
buffer[p11 + channelOffset] = waveletPair.b;
|
|
1755
|
+
} else {
|
|
1756
|
+
wdec16(buffer[px + channelOffset], buffer[p10 + channelOffset]);
|
|
1757
|
+
const i00 = waveletPair.a;
|
|
1758
|
+
const i10 = waveletPair.b;
|
|
1759
|
+
wdec16(buffer[p01 + channelOffset], buffer[p11 + channelOffset]);
|
|
1760
|
+
const i01 = waveletPair.a;
|
|
1761
|
+
const i11 = waveletPair.b;
|
|
1762
|
+
wdec16(i00, i01);
|
|
1763
|
+
buffer[px + channelOffset] = waveletPair.a;
|
|
1764
|
+
buffer[p01 + channelOffset] = waveletPair.b;
|
|
1765
|
+
wdec16(i10, i11);
|
|
1766
|
+
buffer[p10 + channelOffset] = waveletPair.a;
|
|
1767
|
+
buffer[p11 + channelOffset] = waveletPair.b;
|
|
1768
|
+
}
|
|
1769
|
+
}
|
|
1770
|
+
if (nx & p) {
|
|
1771
|
+
const p10 = px + oy1;
|
|
1772
|
+
if (wavelet14) {
|
|
1773
|
+
wdec14(buffer[px + channelOffset], buffer[p10 + channelOffset]);
|
|
1774
|
+
} else {
|
|
1775
|
+
wdec16(buffer[px + channelOffset], buffer[p10 + channelOffset]);
|
|
1776
|
+
}
|
|
1777
|
+
buffer[px + channelOffset] = waveletPair.a;
|
|
1778
|
+
buffer[p10 + channelOffset] = waveletPair.b;
|
|
1779
|
+
}
|
|
1780
|
+
}
|
|
1781
|
+
if (ny & p) {
|
|
1782
|
+
let px = py;
|
|
1783
|
+
const ex = py + ox * (nx - p2);
|
|
1784
|
+
for (; px <= ex; px += ox2) {
|
|
1785
|
+
const p01 = px + ox1;
|
|
1786
|
+
if (wavelet14) {
|
|
1787
|
+
wdec14(buffer[px + channelOffset], buffer[p01 + channelOffset]);
|
|
1788
|
+
} else {
|
|
1789
|
+
wdec16(buffer[px + channelOffset], buffer[p01 + channelOffset]);
|
|
1790
|
+
}
|
|
1791
|
+
buffer[px + channelOffset] = waveletPair.a;
|
|
1792
|
+
buffer[p01 + channelOffset] = waveletPair.b;
|
|
1793
|
+
}
|
|
1794
|
+
}
|
|
1795
|
+
p2 = p;
|
|
1796
|
+
p >>= 1;
|
|
1797
|
+
}
|
|
1798
|
+
}
|
|
1799
|
+
function applyLut(lut, data, count) {
|
|
1800
|
+
for (let i = 0; i < count; i++) {
|
|
1801
|
+
data[i] = lut[data[i]];
|
|
1802
|
+
}
|
|
1803
|
+
}
|
|
1804
|
+
function buildChannelLayout(part, chunkY, linesInChunk) {
|
|
1805
|
+
if (!part.dataWindow) {
|
|
1806
|
+
return { channels: [], totalWords: 0 };
|
|
1807
|
+
}
|
|
1808
|
+
const { xMin, xMax, yMin, yMax } = part.dataWindow;
|
|
1809
|
+
const chunkStart = Math.max(chunkY, yMin);
|
|
1810
|
+
const chunkEnd = Math.min(chunkY + linesInChunk - 1, yMax);
|
|
1811
|
+
const channels = [];
|
|
1812
|
+
let totalWords = 0;
|
|
1813
|
+
for (const channel of part.channels) {
|
|
1814
|
+
const xSampling = channel.xSampling > 0 ? channel.xSampling : 1;
|
|
1815
|
+
const ySampling = channel.ySampling > 0 ? channel.ySampling : 1;
|
|
1816
|
+
const sampleOriginY = firstSampleCoordinate2(yMin, ySampling);
|
|
1817
|
+
const sampledWidth = countSamplesInRange2(xMin, xMax, xSampling);
|
|
1818
|
+
const sampledRows = countSampledRowsInRange(chunkStart, chunkEnd, sampleOriginY, ySampling);
|
|
1819
|
+
const wordsPerSample = channel.pixelType === 1 ? 1 : 2;
|
|
1820
|
+
const wordsPerRow = sampledWidth * wordsPerSample;
|
|
1821
|
+
const wordCount = wordsPerRow * sampledRows;
|
|
1822
|
+
channels.push({
|
|
1823
|
+
sampledWidth,
|
|
1824
|
+
sampledRows,
|
|
1825
|
+
ySampling,
|
|
1826
|
+
sampleOriginY,
|
|
1827
|
+
wordsPerSample,
|
|
1828
|
+
wordsPerRow,
|
|
1829
|
+
wordStart: totalWords,
|
|
1830
|
+
wordEnd: totalWords + wordCount,
|
|
1831
|
+
wordCursor: totalWords
|
|
1832
|
+
});
|
|
1833
|
+
totalWords += wordCount;
|
|
1834
|
+
}
|
|
1835
|
+
return { channels, totalWords };
|
|
1836
|
+
}
|
|
1837
|
+
function decodePizChunk(context) {
|
|
1838
|
+
if (!context.part.dataWindow) {
|
|
1839
|
+
throw new Error("Part is missing dataWindow.");
|
|
1840
|
+
}
|
|
1841
|
+
const { channels, totalWords } = buildChannelLayout(
|
|
1842
|
+
context.part,
|
|
1843
|
+
context.chunkY,
|
|
1844
|
+
context.linesInChunk
|
|
1845
|
+
);
|
|
1846
|
+
const { yMin, yMax } = context.part.dataWindow;
|
|
1847
|
+
const compressedView = new DataView(context.buffer, context.dataPtr, context.dataSize);
|
|
1848
|
+
const compressedBytes = new Uint8Array(context.buffer, context.dataPtr, context.dataSize);
|
|
1849
|
+
const cursor = { value: 0 };
|
|
1850
|
+
const bitmap = new Uint8Array(BITMAP_SIZE);
|
|
1851
|
+
const minNonZero = readUint162(compressedView, cursor);
|
|
1852
|
+
const maxNonZero = readUint162(compressedView, cursor);
|
|
1853
|
+
if (maxNonZero >= BITMAP_SIZE) {
|
|
1854
|
+
throw new Error("Invalid PIZ bitmap range.");
|
|
1855
|
+
}
|
|
1856
|
+
if (minNonZero <= maxNonZero) {
|
|
1857
|
+
const bitmapLength = maxNonZero - minNonZero + 1;
|
|
1858
|
+
if (cursor.value + bitmapLength > compressedView.byteLength) {
|
|
1859
|
+
throw new Error("Truncated PIZ bitmap payload.");
|
|
1860
|
+
}
|
|
1861
|
+
bitmap.set(compressedBytes.subarray(cursor.value, cursor.value + bitmapLength), minNonZero);
|
|
1862
|
+
cursor.value += bitmapLength;
|
|
1863
|
+
}
|
|
1864
|
+
const lut = new Uint16Array(USHORT_RANGE);
|
|
1865
|
+
const maxValue = reverseLutFromBitmap(bitmap, lut);
|
|
1866
|
+
const huffmanLength = readUint322(compressedView, cursor);
|
|
1867
|
+
if (huffmanLength > compressedView.byteLength - cursor.value) {
|
|
1868
|
+
throw new Error("Invalid PIZ Huffman payload length.");
|
|
1869
|
+
}
|
|
1870
|
+
const outBuffer = new Uint16Array(totalWords);
|
|
1871
|
+
hufUncompress2(compressedBytes, compressedView, cursor, huffmanLength, outBuffer, totalWords);
|
|
1872
|
+
for (const channel of channels) {
|
|
1873
|
+
for (let component = 0; component < channel.wordsPerSample; component++) {
|
|
1874
|
+
wav2Decode(
|
|
1875
|
+
outBuffer,
|
|
1876
|
+
channel.wordStart + component,
|
|
1877
|
+
channel.sampledWidth,
|
|
1878
|
+
channel.wordsPerSample,
|
|
1879
|
+
channel.sampledRows,
|
|
1880
|
+
channel.sampledWidth * channel.wordsPerSample,
|
|
1881
|
+
maxValue
|
|
1882
|
+
);
|
|
1883
|
+
}
|
|
1884
|
+
}
|
|
1885
|
+
applyLut(lut, outBuffer, totalWords);
|
|
1886
|
+
const output = new Uint8Array(totalWords * 2);
|
|
1887
|
+
let outputOffset = 0;
|
|
1888
|
+
for (let dy = 0; dy < context.linesInChunk; dy++) {
|
|
1889
|
+
const y = context.chunkY + dy;
|
|
1890
|
+
if (y > yMax) {
|
|
1891
|
+
break;
|
|
1892
|
+
}
|
|
1893
|
+
if (y < yMin) {
|
|
1894
|
+
continue;
|
|
1895
|
+
}
|
|
1896
|
+
for (const channel of channels) {
|
|
1897
|
+
if (channel.wordsPerRow === 0 || channel.sampledRows === 0) {
|
|
1898
|
+
continue;
|
|
1899
|
+
}
|
|
1900
|
+
if (!isSampledCoordinate2(y, channel.sampleOriginY, channel.ySampling)) {
|
|
1901
|
+
continue;
|
|
1902
|
+
}
|
|
1903
|
+
if (channel.wordCursor + channel.wordsPerRow > channel.wordEnd) {
|
|
1904
|
+
throw new Error("Invalid PIZ channel plane bounds.");
|
|
1905
|
+
}
|
|
1906
|
+
for (let i = 0; i < channel.wordsPerRow; i++) {
|
|
1907
|
+
const value = outBuffer[channel.wordCursor + i];
|
|
1908
|
+
output[outputOffset++] = value & 255;
|
|
1909
|
+
output[outputOffset++] = value >>> 8 & 255;
|
|
1910
|
+
}
|
|
1911
|
+
channel.wordCursor += channel.wordsPerRow;
|
|
1912
|
+
}
|
|
1913
|
+
}
|
|
1914
|
+
if (outputOffset !== output.byteLength) {
|
|
1915
|
+
throw new Error("Invalid PIZ output layout.");
|
|
1916
|
+
}
|
|
1917
|
+
return output;
|
|
1918
|
+
}
|
|
1919
|
+
function decodePizBlock(context) {
|
|
1920
|
+
try {
|
|
1921
|
+
return decodePizChunk(context);
|
|
1922
|
+
} catch (error) {
|
|
1923
|
+
if (error instanceof ExrError) {
|
|
1924
|
+
throw error;
|
|
1925
|
+
}
|
|
1926
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
1927
|
+
throw new ExrError("DECOMPRESSION_FAILED", "Failed to decompress PIZ chunk.", {
|
|
1928
|
+
partId: context.partId,
|
|
1929
|
+
chunkIndex: context.chunkIndex,
|
|
1930
|
+
size: context.dataSize,
|
|
1931
|
+
reason
|
|
1932
|
+
});
|
|
1933
|
+
}
|
|
1934
|
+
}
|
|
1935
|
+
|
|
1936
|
+
// src/reader/compression/pxr24.ts
|
|
1937
|
+
import { unzlibSync as unzlibSync2 } from "fflate";
|
|
1938
|
+
function modulo3(value, base) {
|
|
1939
|
+
const result = value % base;
|
|
1940
|
+
return result < 0 ? result + base : result;
|
|
1941
|
+
}
|
|
1942
|
+
function firstSampleCoordinate3(min, sampling) {
|
|
1943
|
+
if (sampling <= 1) return min;
|
|
1944
|
+
const remainder = modulo3(min, sampling);
|
|
1945
|
+
return remainder === 0 ? min : min + (sampling - remainder);
|
|
1946
|
+
}
|
|
1947
|
+
function countSamplesInRange3(min, max, sampling) {
|
|
1948
|
+
if (sampling <= 0 || max < min) return 0;
|
|
1949
|
+
const first = firstSampleCoordinate3(min, sampling);
|
|
1950
|
+
if (first > max) return 0;
|
|
1951
|
+
return Math.floor((max - first) / sampling) + 1;
|
|
1952
|
+
}
|
|
1953
|
+
function isSampledCoordinate3(value, firstSample, sampling) {
|
|
1954
|
+
if (sampling <= 1) return true;
|
|
1955
|
+
if (value < firstSample) return false;
|
|
1956
|
+
return (value - firstSample) % sampling === 0;
|
|
1957
|
+
}
|
|
1958
|
+
function buildRowPlan(context) {
|
|
1959
|
+
const dataWindow = context.part.dataWindow;
|
|
1960
|
+
if (!dataWindow) {
|
|
1961
|
+
throw new Error("PXR24 decode requires part dataWindow.");
|
|
1962
|
+
}
|
|
1963
|
+
const channelLayouts = context.part.channels.map((channel) => {
|
|
1964
|
+
const xSampling = channel.xSampling > 0 ? channel.xSampling : 1;
|
|
1965
|
+
const ySampling = channel.ySampling > 0 ? channel.ySampling : 1;
|
|
1966
|
+
const sampleOriginY = firstSampleCoordinate3(dataWindow.yMin, ySampling);
|
|
1967
|
+
const sampledWidth = countSamplesInRange3(dataWindow.xMin, dataWindow.xMax, xSampling);
|
|
1968
|
+
const bytesPerSample = channel.pixelType === 1 ? 2 : 4;
|
|
1969
|
+
return {
|
|
1970
|
+
pixelType: channel.pixelType,
|
|
1971
|
+
sampledWidth,
|
|
1972
|
+
sampleOriginY,
|
|
1973
|
+
ySampling,
|
|
1974
|
+
bytesPerSample
|
|
1975
|
+
};
|
|
1976
|
+
});
|
|
1977
|
+
const rows = [];
|
|
1978
|
+
let encodedSize = 0;
|
|
1979
|
+
let decodedSize = 0;
|
|
1980
|
+
for (let dy = 0; dy < context.linesInChunk; dy++) {
|
|
1981
|
+
const y = context.chunkY + dy;
|
|
1982
|
+
if (y < dataWindow.yMin || y > dataWindow.yMax) {
|
|
1983
|
+
continue;
|
|
1984
|
+
}
|
|
1985
|
+
for (const layout of channelLayouts) {
|
|
1986
|
+
if (layout.sampledWidth === 0) {
|
|
1987
|
+
continue;
|
|
1988
|
+
}
|
|
1989
|
+
if (!isSampledCoordinate3(y, layout.sampleOriginY, layout.ySampling)) {
|
|
1990
|
+
continue;
|
|
1991
|
+
}
|
|
1992
|
+
rows.push({
|
|
1993
|
+
pixelType: layout.pixelType,
|
|
1994
|
+
sampledWidth: layout.sampledWidth,
|
|
1995
|
+
bytesPerSample: layout.bytesPerSample
|
|
1996
|
+
});
|
|
1997
|
+
const encodedBytesPerSample = layout.pixelType === 2 ? 3 : layout.bytesPerSample;
|
|
1998
|
+
encodedSize += layout.sampledWidth * encodedBytesPerSample;
|
|
1999
|
+
decodedSize += layout.sampledWidth * layout.bytesPerSample;
|
|
2000
|
+
}
|
|
2001
|
+
}
|
|
2002
|
+
return { rows, encodedSize, decodedSize };
|
|
2003
|
+
}
|
|
2004
|
+
function decodePxr24Block(context) {
|
|
2005
|
+
try {
|
|
2006
|
+
const compressed = new Uint8Array(context.buffer, context.dataPtr, context.dataSize);
|
|
2007
|
+
const unpacked = unzlibSync2(compressed);
|
|
2008
|
+
const plan = buildRowPlan(context);
|
|
2009
|
+
if (unpacked.byteLength !== plan.encodedSize) {
|
|
2010
|
+
throw new Error("Invalid PXR24 intermediate payload length.");
|
|
2011
|
+
}
|
|
2012
|
+
if (plan.decodedSize !== context.expectedUncompressedSize) {
|
|
2013
|
+
throw new Error("Invalid PXR24 expected output length.");
|
|
2014
|
+
}
|
|
2015
|
+
const out = new Uint8Array(plan.decodedSize);
|
|
2016
|
+
let unpackedPtr = 0;
|
|
2017
|
+
let outPtr = 0;
|
|
2018
|
+
for (const row of plan.rows) {
|
|
2019
|
+
const width = row.sampledWidth;
|
|
2020
|
+
if (row.pixelType === 1) {
|
|
2021
|
+
const needed = width * 2;
|
|
2022
|
+
if (unpackedPtr + needed > unpacked.length) {
|
|
2023
|
+
throw new Error("Truncated PXR24 HALF payload.");
|
|
2024
|
+
}
|
|
2025
|
+
let p0 = unpackedPtr;
|
|
2026
|
+
let p1 = p0 + width;
|
|
2027
|
+
unpackedPtr += needed;
|
|
2028
|
+
let pixel = 0;
|
|
2029
|
+
for (let x = 0; x < width; x++) {
|
|
2030
|
+
const diff = (unpacked[p0++] << 8 | unpacked[p1++]) >>> 0;
|
|
2031
|
+
pixel = pixel + diff >>> 0;
|
|
2032
|
+
out[outPtr++] = pixel & 255;
|
|
2033
|
+
out[outPtr++] = pixel >>> 8 & 255;
|
|
2034
|
+
}
|
|
2035
|
+
continue;
|
|
2036
|
+
}
|
|
2037
|
+
if (row.pixelType === 0) {
|
|
2038
|
+
const needed = width * 4;
|
|
2039
|
+
if (unpackedPtr + needed > unpacked.length) {
|
|
2040
|
+
throw new Error("Truncated PXR24 UINT payload.");
|
|
2041
|
+
}
|
|
2042
|
+
let p0 = unpackedPtr;
|
|
2043
|
+
let p1 = p0 + width;
|
|
2044
|
+
let p2 = p1 + width;
|
|
2045
|
+
let p3 = p2 + width;
|
|
2046
|
+
unpackedPtr += needed;
|
|
2047
|
+
let pixel = 0;
|
|
2048
|
+
for (let x = 0; x < width; x++) {
|
|
2049
|
+
const diff = (unpacked[p0++] << 24 >>> 0 | unpacked[p1++] << 16 >>> 0 | unpacked[p2++] << 8 >>> 0 | unpacked[p3++] >>> 0) >>> 0;
|
|
2050
|
+
pixel = pixel + diff >>> 0;
|
|
2051
|
+
out[outPtr++] = pixel & 255;
|
|
2052
|
+
out[outPtr++] = pixel >>> 8 & 255;
|
|
2053
|
+
out[outPtr++] = pixel >>> 16 & 255;
|
|
2054
|
+
out[outPtr++] = pixel >>> 24 & 255;
|
|
2055
|
+
}
|
|
2056
|
+
continue;
|
|
2057
|
+
}
|
|
2058
|
+
if (row.pixelType === 2) {
|
|
2059
|
+
const needed = width * 3;
|
|
2060
|
+
if (unpackedPtr + needed > unpacked.length) {
|
|
2061
|
+
throw new Error("Truncated PXR24 FLOAT payload.");
|
|
2062
|
+
}
|
|
2063
|
+
let p0 = unpackedPtr;
|
|
2064
|
+
let p1 = p0 + width;
|
|
2065
|
+
let p2 = p1 + width;
|
|
2066
|
+
unpackedPtr += needed;
|
|
2067
|
+
let pixel = 0;
|
|
2068
|
+
for (let x = 0; x < width; x++) {
|
|
2069
|
+
const diff = (unpacked[p0++] << 24 >>> 0 | unpacked[p1++] << 16 >>> 0 | unpacked[p2++] << 8 >>> 0) >>> 0;
|
|
2070
|
+
pixel = pixel + diff >>> 0;
|
|
2071
|
+
out[outPtr++] = pixel & 255;
|
|
2072
|
+
out[outPtr++] = pixel >>> 8 & 255;
|
|
2073
|
+
out[outPtr++] = pixel >>> 16 & 255;
|
|
2074
|
+
out[outPtr++] = pixel >>> 24 & 255;
|
|
2075
|
+
}
|
|
2076
|
+
continue;
|
|
2077
|
+
}
|
|
2078
|
+
throw new Error(`Unsupported PXR24 pixel type ${row.pixelType}.`);
|
|
2079
|
+
}
|
|
2080
|
+
if (unpackedPtr !== unpacked.length || outPtr !== out.length) {
|
|
2081
|
+
throw new Error("PXR24 payload decode bounds mismatch.");
|
|
2082
|
+
}
|
|
2083
|
+
return out;
|
|
2084
|
+
} catch {
|
|
2085
|
+
throw new ExrError("DECOMPRESSION_FAILED", "Failed to decompress PXR24 chunk.", {
|
|
2086
|
+
partId: context.partId,
|
|
2087
|
+
chunkIndex: context.chunkIndex,
|
|
2088
|
+
size: context.dataSize,
|
|
2089
|
+
expected: context.expectedUncompressedSize
|
|
2090
|
+
});
|
|
2091
|
+
}
|
|
2092
|
+
}
|
|
2093
|
+
|
|
2094
|
+
// src/reader/compression/rle.ts
|
|
2095
|
+
function undoPredictor(data) {
|
|
2096
|
+
for (let i = 1; i < data.length; i++) {
|
|
2097
|
+
data[i] = data[i - 1] + data[i] - 128 & 255;
|
|
2098
|
+
}
|
|
2099
|
+
}
|
|
2100
|
+
function undoInterleave(data) {
|
|
2101
|
+
const length = data.length;
|
|
2102
|
+
const output = new Uint8Array(length);
|
|
2103
|
+
const half = Math.floor((length + 1) / 2);
|
|
2104
|
+
const first = data.subarray(0, half);
|
|
2105
|
+
const second = data.subarray(half);
|
|
2106
|
+
for (let i = 0; i < half; i++) {
|
|
2107
|
+
output[i * 2] = first[i];
|
|
2108
|
+
if (i * 2 + 1 < length) {
|
|
2109
|
+
output[i * 2 + 1] = second[i];
|
|
2110
|
+
}
|
|
2111
|
+
}
|
|
2112
|
+
return output;
|
|
2113
|
+
}
|
|
2114
|
+
function decodeRunLength2(input, expectedSize) {
|
|
2115
|
+
if (expectedSize < 0) {
|
|
2116
|
+
throw new Error("Invalid expected RLE output size.");
|
|
2117
|
+
}
|
|
2118
|
+
const output = new Uint8Array(expectedSize);
|
|
2119
|
+
let sourcePtr = 0;
|
|
2120
|
+
let outputPtr = 0;
|
|
2121
|
+
while (sourcePtr < input.length) {
|
|
2122
|
+
const length = input[sourcePtr] << 24 >> 24;
|
|
2123
|
+
sourcePtr += 1;
|
|
2124
|
+
if (length < 0) {
|
|
2125
|
+
const count2 = -length;
|
|
2126
|
+
if (sourcePtr + count2 > input.length) {
|
|
2127
|
+
throw new Error("Truncated RLE literal run.");
|
|
2128
|
+
}
|
|
2129
|
+
if (outputPtr + count2 > output.length) {
|
|
2130
|
+
throw new Error("RLE literal run exceeded expected output size.");
|
|
2131
|
+
}
|
|
2132
|
+
output.set(input.subarray(sourcePtr, sourcePtr + count2), outputPtr);
|
|
2133
|
+
sourcePtr += count2;
|
|
2134
|
+
outputPtr += count2;
|
|
2135
|
+
continue;
|
|
2136
|
+
}
|
|
2137
|
+
const count = length + 1;
|
|
2138
|
+
if (sourcePtr >= input.length) {
|
|
2139
|
+
throw new Error("Truncated RLE repeat run.");
|
|
2140
|
+
}
|
|
2141
|
+
if (outputPtr + count > output.length) {
|
|
2142
|
+
throw new Error("RLE repeat run exceeded expected output size.");
|
|
2143
|
+
}
|
|
2144
|
+
output.fill(input[sourcePtr], outputPtr, outputPtr + count);
|
|
2145
|
+
sourcePtr += 1;
|
|
2146
|
+
outputPtr += count;
|
|
2147
|
+
}
|
|
2148
|
+
if (outputPtr !== output.length) {
|
|
2149
|
+
throw new Error("RLE output size mismatch.");
|
|
2150
|
+
}
|
|
2151
|
+
return output;
|
|
2152
|
+
}
|
|
2153
|
+
function decodeRleBlock(context) {
|
|
2154
|
+
try {
|
|
2155
|
+
const compressed = new Uint8Array(context.buffer, context.dataPtr, context.dataSize);
|
|
2156
|
+
const unpacked = decodeRunLength2(compressed, context.expectedUncompressedSize);
|
|
2157
|
+
undoPredictor(unpacked);
|
|
2158
|
+
return undoInterleave(unpacked);
|
|
2159
|
+
} catch {
|
|
2160
|
+
throw new ExrError("DECOMPRESSION_FAILED", "Failed to decompress RLE chunk.", {
|
|
2161
|
+
partId: context.partId,
|
|
2162
|
+
chunkIndex: context.chunkIndex,
|
|
2163
|
+
size: context.dataSize,
|
|
2164
|
+
expected: context.expectedUncompressedSize
|
|
2165
|
+
});
|
|
2166
|
+
}
|
|
2167
|
+
}
|
|
2168
|
+
|
|
2169
|
+
// src/reader/compression/scanline.ts
|
|
2170
|
+
function getScanlineLinesPerBlock(compression) {
|
|
2171
|
+
switch (compression) {
|
|
2172
|
+
case 0:
|
|
2173
|
+
case 1:
|
|
2174
|
+
case 2:
|
|
2175
|
+
return 1;
|
|
2176
|
+
case 3:
|
|
2177
|
+
case 5:
|
|
2178
|
+
return 16;
|
|
2179
|
+
case 4:
|
|
2180
|
+
case 6:
|
|
2181
|
+
case 7:
|
|
2182
|
+
case 8:
|
|
2183
|
+
return 32;
|
|
2184
|
+
case 9:
|
|
2185
|
+
return 256;
|
|
2186
|
+
default:
|
|
2187
|
+
return 1;
|
|
2188
|
+
}
|
|
2189
|
+
}
|
|
2190
|
+
|
|
2191
|
+
// src/reader/compression/zip.ts
|
|
2192
|
+
import { unzlibSync as unzlibSync3 } from "fflate";
|
|
2193
|
+
function undoZipPredictorAndInterleave2(data, output) {
|
|
2194
|
+
const length = data.length;
|
|
2195
|
+
if (length === 0) return data;
|
|
2196
|
+
const half = length + 1 >> 1;
|
|
2197
|
+
let predicted = data[0];
|
|
2198
|
+
output[0] = predicted;
|
|
2199
|
+
for (let i = 1; i < half; i++) {
|
|
2200
|
+
predicted = predicted + data[i] - 128 & 255;
|
|
2201
|
+
output[i << 1] = predicted;
|
|
2202
|
+
}
|
|
2203
|
+
for (let i = half; i < length; i++) {
|
|
2204
|
+
predicted = predicted + data[i] - 128 & 255;
|
|
2205
|
+
output[(i - half << 1) + 1] = predicted;
|
|
2206
|
+
}
|
|
2207
|
+
return output;
|
|
2208
|
+
}
|
|
2209
|
+
function decodeZipStyleBlock(context) {
|
|
2210
|
+
try {
|
|
2211
|
+
const compressed = new Uint8Array(context.buffer, context.dataPtr, context.dataSize);
|
|
2212
|
+
let raw;
|
|
2213
|
+
if (context.expectedUncompressedSize > 0 && context.zipScratch) {
|
|
2214
|
+
if (context.zipScratch.inflate.byteLength < context.expectedUncompressedSize) {
|
|
2215
|
+
context.zipScratch.inflate = new Uint8Array(context.expectedUncompressedSize);
|
|
2216
|
+
}
|
|
2217
|
+
const inflateOut = context.zipScratch.inflate.subarray(0, context.expectedUncompressedSize);
|
|
2218
|
+
raw = unzlibSync3(compressed, { out: inflateOut });
|
|
2219
|
+
} else if (context.expectedUncompressedSize > 0) {
|
|
2220
|
+
raw = unzlibSync3(compressed, { out: new Uint8Array(context.expectedUncompressedSize) });
|
|
2221
|
+
} else {
|
|
2222
|
+
raw = unzlibSync3(compressed);
|
|
2223
|
+
}
|
|
2224
|
+
if (context.zipScratch) {
|
|
2225
|
+
if (context.zipScratch.output.byteLength < raw.byteLength) {
|
|
2226
|
+
context.zipScratch.output = new Uint8Array(raw.byteLength);
|
|
2227
|
+
}
|
|
2228
|
+
const output = context.zipScratch.output.subarray(0, raw.byteLength);
|
|
2229
|
+
return undoZipPredictorAndInterleave2(raw, output);
|
|
2230
|
+
}
|
|
2231
|
+
return undoZipPredictorAndInterleave2(raw, new Uint8Array(raw.byteLength));
|
|
2232
|
+
} catch {
|
|
2233
|
+
throw new ExrError("DECOMPRESSION_FAILED", "Failed to decompress ZIP/ZIPS chunk.", {
|
|
2234
|
+
partId: context.partId,
|
|
2235
|
+
chunkIndex: context.chunkIndex,
|
|
2236
|
+
size: context.dataSize
|
|
2237
|
+
});
|
|
2238
|
+
}
|
|
2239
|
+
}
|
|
2240
|
+
|
|
2241
|
+
// src/reader/compression/handlers.ts
|
|
2242
|
+
var READ_COMPRESSION_HANDLERS = /* @__PURE__ */ new Map([
|
|
2243
|
+
[
|
|
2244
|
+
0,
|
|
2245
|
+
{
|
|
2246
|
+
compressionId: 0,
|
|
2247
|
+
name: COMPRESSION_NAMES[0],
|
|
2248
|
+
linesPerBlock: getScanlineLinesPerBlock(0),
|
|
2249
|
+
decodeBlock: ({ buffer, dataPtr, dataSize }) => new Uint8Array(buffer, dataPtr, dataSize)
|
|
2250
|
+
}
|
|
2251
|
+
],
|
|
2252
|
+
[
|
|
2253
|
+
1,
|
|
2254
|
+
{
|
|
2255
|
+
compressionId: 1,
|
|
2256
|
+
name: COMPRESSION_NAMES[1],
|
|
2257
|
+
linesPerBlock: getScanlineLinesPerBlock(1),
|
|
2258
|
+
decodeBlock: decodeRleBlock
|
|
2259
|
+
}
|
|
2260
|
+
],
|
|
2261
|
+
[
|
|
2262
|
+
2,
|
|
2263
|
+
{
|
|
2264
|
+
compressionId: 2,
|
|
2265
|
+
name: COMPRESSION_NAMES[2],
|
|
2266
|
+
linesPerBlock: getScanlineLinesPerBlock(2),
|
|
2267
|
+
decodeBlock: decodeZipStyleBlock
|
|
2268
|
+
}
|
|
2269
|
+
],
|
|
2270
|
+
[
|
|
2271
|
+
3,
|
|
2272
|
+
{
|
|
2273
|
+
compressionId: 3,
|
|
2274
|
+
name: COMPRESSION_NAMES[3],
|
|
2275
|
+
linesPerBlock: getScanlineLinesPerBlock(3),
|
|
2276
|
+
decodeBlock: decodeZipStyleBlock
|
|
2277
|
+
}
|
|
2278
|
+
],
|
|
2279
|
+
[
|
|
2280
|
+
4,
|
|
2281
|
+
{
|
|
2282
|
+
compressionId: 4,
|
|
2283
|
+
name: COMPRESSION_NAMES[4],
|
|
2284
|
+
linesPerBlock: getScanlineLinesPerBlock(4),
|
|
2285
|
+
decodeBlock: decodePizBlock
|
|
2286
|
+
}
|
|
2287
|
+
],
|
|
2288
|
+
[
|
|
2289
|
+
5,
|
|
2290
|
+
{
|
|
2291
|
+
compressionId: 5,
|
|
2292
|
+
name: COMPRESSION_NAMES[5],
|
|
2293
|
+
linesPerBlock: getScanlineLinesPerBlock(5),
|
|
2294
|
+
decodeBlock: decodePxr24Block
|
|
2295
|
+
}
|
|
2296
|
+
],
|
|
2297
|
+
[
|
|
2298
|
+
6,
|
|
2299
|
+
{
|
|
2300
|
+
compressionId: 6,
|
|
2301
|
+
name: COMPRESSION_NAMES[6],
|
|
2302
|
+
linesPerBlock: getScanlineLinesPerBlock(6),
|
|
2303
|
+
decodeBlock: decodeB44Block
|
|
2304
|
+
}
|
|
2305
|
+
],
|
|
2306
|
+
[
|
|
2307
|
+
7,
|
|
2308
|
+
{
|
|
2309
|
+
compressionId: 7,
|
|
2310
|
+
name: COMPRESSION_NAMES[7],
|
|
2311
|
+
linesPerBlock: getScanlineLinesPerBlock(7),
|
|
2312
|
+
decodeBlock: decodeB44Block
|
|
2313
|
+
}
|
|
2314
|
+
],
|
|
2315
|
+
[
|
|
2316
|
+
8,
|
|
2317
|
+
{
|
|
2318
|
+
compressionId: 8,
|
|
2319
|
+
name: COMPRESSION_NAMES[8],
|
|
2320
|
+
linesPerBlock: getScanlineLinesPerBlock(8),
|
|
2321
|
+
decodeBlock: decodeDwaBlock
|
|
2322
|
+
}
|
|
2323
|
+
],
|
|
2324
|
+
[
|
|
2325
|
+
9,
|
|
2326
|
+
{
|
|
2327
|
+
compressionId: 9,
|
|
2328
|
+
name: COMPRESSION_NAMES[9],
|
|
2329
|
+
linesPerBlock: getScanlineLinesPerBlock(9),
|
|
2330
|
+
decodeBlock: decodeDwaBlock
|
|
2331
|
+
}
|
|
2332
|
+
]
|
|
2333
|
+
]);
|
|
2334
|
+
function getReadCompressionHandler(compression) {
|
|
2335
|
+
return READ_COMPRESSION_HANDLERS.get(compression);
|
|
2336
|
+
}
|
|
2337
|
+
function getSupportedReadCompressionText() {
|
|
2338
|
+
return Array.from(READ_COMPRESSION_HANDLERS.values()).map((handler) => handler.name).join(", ");
|
|
2339
|
+
}
|
|
2340
|
+
|
|
2341
|
+
// src/shared/binary.ts
|
|
2342
|
+
function toArrayBuffer(input) {
|
|
2343
|
+
if (input instanceof ArrayBuffer) {
|
|
2344
|
+
return input;
|
|
2345
|
+
}
|
|
2346
|
+
const output = new Uint8Array(input.byteLength);
|
|
2347
|
+
output.set(input);
|
|
2348
|
+
return output.buffer;
|
|
2349
|
+
}
|
|
2350
|
+
|
|
2351
|
+
// src/reader/decodeExrPart.ts
|
|
2352
|
+
var UINT32_MAX = 4294967295;
|
|
2353
|
+
var INV_UINT32_MAX = 1 / UINT32_MAX;
|
|
2354
|
+
function nowMs() {
|
|
2355
|
+
return typeof performance !== "undefined" ? performance.now() : Date.now();
|
|
2356
|
+
}
|
|
2357
|
+
function emit(onEvent, event) {
|
|
2358
|
+
onEvent?.(event);
|
|
2359
|
+
}
|
|
2360
|
+
function modulo4(value, base) {
|
|
2361
|
+
const result = value % base;
|
|
2362
|
+
return result < 0 ? result + base : result;
|
|
2363
|
+
}
|
|
2364
|
+
function firstSampleCoordinate4(min, sampling) {
|
|
2365
|
+
if (sampling <= 1) return min;
|
|
2366
|
+
const remainder = modulo4(min, sampling);
|
|
2367
|
+
return remainder === 0 ? min : min + (sampling - remainder);
|
|
2368
|
+
}
|
|
2369
|
+
function countSamplesInRange4(min, max, sampling) {
|
|
2370
|
+
if (sampling <= 0 || max < min) return 0;
|
|
2371
|
+
const first = firstSampleCoordinate4(min, sampling);
|
|
2372
|
+
if (first > max) return 0;
|
|
2373
|
+
return Math.floor((max - first) / sampling) + 1;
|
|
2374
|
+
}
|
|
2375
|
+
function isSampledCoordinate4(value, firstSample, sampling) {
|
|
2376
|
+
if (sampling <= 1) return true;
|
|
2377
|
+
if (value < firstSample) return false;
|
|
2378
|
+
return (value - firstSample) % sampling === 0;
|
|
2379
|
+
}
|
|
2380
|
+
function toNumberOffset(low, high) {
|
|
2381
|
+
if (high <= 2097151) {
|
|
2382
|
+
return high * 4294967296 + low;
|
|
2383
|
+
}
|
|
2384
|
+
const combined = BigInt(low) + (BigInt(high) << 32n);
|
|
2385
|
+
if (combined > BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
2386
|
+
throw new ExrError("MALFORMED_OFFSET_TABLE", "Chunk offset exceeds safe integer range.", {
|
|
2387
|
+
offset: combined.toString()
|
|
2388
|
+
});
|
|
2389
|
+
}
|
|
2390
|
+
return Number(combined);
|
|
2391
|
+
}
|
|
2392
|
+
function buildChannelMeta(part) {
|
|
2393
|
+
if (!part.dataWindow) return [];
|
|
2394
|
+
const { xMin, xMax, yMin, yMax } = part.dataWindow;
|
|
2395
|
+
return part.channels.map((channel) => {
|
|
2396
|
+
const xSampling = channel.xSampling > 0 ? channel.xSampling : 1;
|
|
2397
|
+
const ySampling = channel.ySampling > 0 ? channel.ySampling : 1;
|
|
2398
|
+
const sampleOriginX = firstSampleCoordinate4(xMin, xSampling);
|
|
2399
|
+
const sampleOriginY = firstSampleCoordinate4(yMin, ySampling);
|
|
2400
|
+
const sampledWidth = countSamplesInRange4(xMin, xMax, xSampling);
|
|
2401
|
+
const sampledHeight = countSamplesInRange4(yMin, yMax, ySampling);
|
|
2402
|
+
const bytesPerSample = channel.pixelType === 1 ? 2 : 4;
|
|
2403
|
+
return {
|
|
2404
|
+
channel,
|
|
2405
|
+
pixelType: channel.pixelType,
|
|
2406
|
+
ySampling,
|
|
2407
|
+
bytesPerSample,
|
|
2408
|
+
rowByteLength: sampledWidth * bytesPerSample,
|
|
2409
|
+
sampledWidth,
|
|
2410
|
+
sampledHeight,
|
|
2411
|
+
sampleOriginX,
|
|
2412
|
+
sampleOriginY,
|
|
2413
|
+
data: new Float32Array(sampledWidth * sampledHeight)
|
|
2414
|
+
};
|
|
2415
|
+
});
|
|
2416
|
+
}
|
|
2417
|
+
function getExpectedUncompressedChunkSize(part, channelMeta, chunkY, linesInChunk) {
|
|
2418
|
+
if (!part.dataWindow) return 0;
|
|
2419
|
+
let expected = 0;
|
|
2420
|
+
for (let dy = 0; dy < linesInChunk; dy++) {
|
|
2421
|
+
const y = chunkY + dy;
|
|
2422
|
+
if (y < part.dataWindow.yMin) {
|
|
2423
|
+
continue;
|
|
2424
|
+
}
|
|
2425
|
+
for (const meta of channelMeta) {
|
|
2426
|
+
if (meta.sampledWidth === 0 || meta.sampledHeight === 0) {
|
|
2427
|
+
continue;
|
|
2428
|
+
}
|
|
2429
|
+
if (!isSampledCoordinate4(y, meta.sampleOriginY, meta.ySampling)) {
|
|
2430
|
+
continue;
|
|
2431
|
+
}
|
|
2432
|
+
expected += meta.rowByteLength;
|
|
2433
|
+
}
|
|
2434
|
+
}
|
|
2435
|
+
return expected;
|
|
2436
|
+
}
|
|
2437
|
+
function decodeRowIntoChannel(meta, blockData, blockPointer, rowOffset, blockView) {
|
|
2438
|
+
const destination = meta.data;
|
|
2439
|
+
const sampleCount = meta.sampledWidth;
|
|
2440
|
+
const sourceByteOffset = blockData.byteOffset + blockPointer;
|
|
2441
|
+
if (meta.pixelType === 1) {
|
|
2442
|
+
if ((sourceByteOffset & 1) === 0) {
|
|
2443
|
+
const source = new Uint16Array(blockData.buffer, sourceByteOffset, sampleCount);
|
|
2444
|
+
for (let i = 0; i < sampleCount; i++) {
|
|
2445
|
+
destination[rowOffset + i] = float16ToFloat32(source[i]);
|
|
2446
|
+
}
|
|
2447
|
+
return;
|
|
2448
|
+
}
|
|
2449
|
+
for (let i = 0; i < sampleCount; i++) {
|
|
2450
|
+
destination[rowOffset + i] = float16ToFloat32(
|
|
2451
|
+
blockView.getUint16(blockPointer + (i << 1), true)
|
|
2452
|
+
);
|
|
2453
|
+
}
|
|
2454
|
+
return;
|
|
2455
|
+
}
|
|
2456
|
+
if (meta.pixelType === 2) {
|
|
2457
|
+
if ((sourceByteOffset & 3) === 0) {
|
|
2458
|
+
const source = new Float32Array(blockData.buffer, sourceByteOffset, sampleCount);
|
|
2459
|
+
destination.set(source, rowOffset);
|
|
2460
|
+
return;
|
|
2461
|
+
}
|
|
2462
|
+
for (let i = 0; i < sampleCount; i++) {
|
|
2463
|
+
destination[rowOffset + i] = blockView.getFloat32(blockPointer + (i << 2), true);
|
|
2464
|
+
}
|
|
2465
|
+
return;
|
|
2466
|
+
}
|
|
2467
|
+
if ((sourceByteOffset & 3) === 0) {
|
|
2468
|
+
const source = new Uint32Array(blockData.buffer, sourceByteOffset, sampleCount);
|
|
2469
|
+
for (let i = 0; i < sampleCount; i++) {
|
|
2470
|
+
destination[rowOffset + i] = source[i] * INV_UINT32_MAX;
|
|
2471
|
+
}
|
|
2472
|
+
return;
|
|
2473
|
+
}
|
|
2474
|
+
for (let i = 0; i < sampleCount; i++) {
|
|
2475
|
+
destination[rowOffset + i] = blockView.getUint32(blockPointer + (i << 2), true) * INV_UINT32_MAX;
|
|
2476
|
+
}
|
|
2477
|
+
}
|
|
2478
|
+
function decodeExrPart(bufferInput, structure, options) {
|
|
2479
|
+
const buffer = toArrayBuffer(bufferInput);
|
|
2480
|
+
const onEvent = options.onEvent;
|
|
2481
|
+
const t0 = nowMs();
|
|
2482
|
+
const partIndex = structure.parts.findIndex((part2) => part2.id === options.partId);
|
|
2483
|
+
if (partIndex < 0) {
|
|
2484
|
+
throw new ExrError("PART_NOT_FOUND", `Part ${options.partId} was not found in EXR structure.`, {
|
|
2485
|
+
partId: options.partId
|
|
2486
|
+
});
|
|
2487
|
+
}
|
|
2488
|
+
const part = structure.parts[partIndex];
|
|
2489
|
+
if (part.type === "tiledimage") {
|
|
2490
|
+
throw new ExrError("UNSUPPORTED_PART_TYPE", "Tiled EXR parts are not supported.", {
|
|
2491
|
+
partId: options.partId,
|
|
2492
|
+
type: part.type
|
|
2493
|
+
});
|
|
2494
|
+
}
|
|
2495
|
+
const compression = part.compression ?? 0;
|
|
2496
|
+
const compressionHandler = getReadCompressionHandler(compression);
|
|
2497
|
+
if (!compressionHandler) {
|
|
2498
|
+
throw new ExrError(
|
|
2499
|
+
"UNSUPPORTED_COMPRESSION",
|
|
2500
|
+
`Unsupported compression. Supported: ${getSupportedReadCompressionText()}.`,
|
|
2501
|
+
{
|
|
2502
|
+
partId: options.partId,
|
|
2503
|
+
compression
|
|
2504
|
+
}
|
|
2505
|
+
);
|
|
2506
|
+
}
|
|
2507
|
+
if (!part.dataWindow) {
|
|
2508
|
+
throw new ExrError("MISSING_DATA_WINDOW", "EXR part is missing required dataWindow.", {
|
|
2509
|
+
partId: options.partId
|
|
2510
|
+
});
|
|
2511
|
+
}
|
|
2512
|
+
const view = new DataView(buffer);
|
|
2513
|
+
const width = part.dataWindow.xMax - part.dataWindow.xMin + 1;
|
|
2514
|
+
const height = part.dataWindow.yMax - part.dataWindow.yMin + 1;
|
|
2515
|
+
const linesPerBlock = compressionHandler.linesPerBlock;
|
|
2516
|
+
const chunkCount = Math.ceil(height / linesPerBlock);
|
|
2517
|
+
emit(onEvent, {
|
|
2518
|
+
phase: "decode",
|
|
2519
|
+
level: "info",
|
|
2520
|
+
code: "decode.setup",
|
|
2521
|
+
message: `Decoder setup for part ${part.id}.`,
|
|
2522
|
+
metrics: {
|
|
2523
|
+
partId: part.id,
|
|
2524
|
+
width,
|
|
2525
|
+
height,
|
|
2526
|
+
channels: part.channels.length,
|
|
2527
|
+
compression: compressionHandler.name || COMPRESSION_NAMES[compression] || "UNKNOWN",
|
|
2528
|
+
chunks: chunkCount
|
|
2529
|
+
}
|
|
2530
|
+
});
|
|
2531
|
+
let offsetTablePtr = structure.headerEndOffset;
|
|
2532
|
+
for (let i = 0; i < partIndex; i++) {
|
|
2533
|
+
const prior = structure.parts[i];
|
|
2534
|
+
if (!prior.dataWindow) continue;
|
|
2535
|
+
const priorHeight = prior.dataWindow.yMax - prior.dataWindow.yMin + 1;
|
|
2536
|
+
const priorChunks = Math.ceil(priorHeight / getScanlineLinesPerBlock(prior.compression ?? 0));
|
|
2537
|
+
offsetTablePtr += priorChunks * 8;
|
|
2538
|
+
}
|
|
2539
|
+
if (offsetTablePtr < 0 || offsetTablePtr + chunkCount * 8 > view.byteLength) {
|
|
2540
|
+
throw new ExrError("MALFORMED_OFFSET_TABLE", "Offset table is truncated or invalid.", {
|
|
2541
|
+
partId: options.partId,
|
|
2542
|
+
offsetTablePtr,
|
|
2543
|
+
chunkCount,
|
|
2544
|
+
size: view.byteLength
|
|
2545
|
+
});
|
|
2546
|
+
}
|
|
2547
|
+
const tOffsets = nowMs();
|
|
2548
|
+
const chunkOffsets = new Array(chunkCount);
|
|
2549
|
+
for (let i = 0; i < chunkCount; i++) {
|
|
2550
|
+
const low = view.getUint32(offsetTablePtr + i * 8, true);
|
|
2551
|
+
const high = view.getUint32(offsetTablePtr + i * 8 + 4, true);
|
|
2552
|
+
chunkOffsets[i] = toNumberOffset(low, high);
|
|
2553
|
+
}
|
|
2554
|
+
emit(onEvent, {
|
|
2555
|
+
phase: "decode",
|
|
2556
|
+
level: "info",
|
|
2557
|
+
code: "decode.offsets.read",
|
|
2558
|
+
message: "Read chunk offsets.",
|
|
2559
|
+
metrics: {
|
|
2560
|
+
partId: part.id,
|
|
2561
|
+
count: chunkOffsets.length,
|
|
2562
|
+
ms: (nowMs() - tOffsets).toFixed(3)
|
|
2563
|
+
}
|
|
2564
|
+
});
|
|
2565
|
+
const channelMeta = buildChannelMeta(part);
|
|
2566
|
+
const zipScratch = {
|
|
2567
|
+
inflate: new Uint8Array(0),
|
|
2568
|
+
output: new Uint8Array(0)
|
|
2569
|
+
};
|
|
2570
|
+
const tDecode = nowMs();
|
|
2571
|
+
let bytesRead = 0;
|
|
2572
|
+
for (let chunkIndex = 0; chunkIndex < chunkOffsets.length; chunkIndex++) {
|
|
2573
|
+
const chunkOffset = chunkOffsets[chunkIndex];
|
|
2574
|
+
const chunkHeaderSize = structure.isMultipart ? 12 : 8;
|
|
2575
|
+
if (chunkOffset < 0 || chunkOffset + chunkHeaderSize > view.byteLength) {
|
|
2576
|
+
throw new ExrError("MALFORMED_CHUNK", "Chunk header is outside file bounds.", {
|
|
2577
|
+
partId: part.id,
|
|
2578
|
+
chunkIndex,
|
|
2579
|
+
chunkOffset
|
|
2580
|
+
});
|
|
2581
|
+
}
|
|
2582
|
+
let chunkY = 0;
|
|
2583
|
+
let dataSize = 0;
|
|
2584
|
+
let dataPtr = chunkOffset;
|
|
2585
|
+
if (structure.isMultipart) {
|
|
2586
|
+
const partNumber = view.getInt32(chunkOffset, true);
|
|
2587
|
+
chunkY = view.getInt32(chunkOffset + 4, true);
|
|
2588
|
+
dataSize = view.getInt32(chunkOffset + 8, true);
|
|
2589
|
+
dataPtr = chunkOffset + 12;
|
|
2590
|
+
if (partNumber !== part.id) {
|
|
2591
|
+
emit(onEvent, {
|
|
2592
|
+
phase: "decode",
|
|
2593
|
+
level: "warn",
|
|
2594
|
+
code: "decode.chunk.part_mismatch",
|
|
2595
|
+
message: "Skipped chunk whose part number did not match requested part.",
|
|
2596
|
+
metrics: {
|
|
2597
|
+
chunkIndex,
|
|
2598
|
+
expectedPart: part.id,
|
|
2599
|
+
actualPart: partNumber
|
|
2600
|
+
}
|
|
2601
|
+
});
|
|
2602
|
+
continue;
|
|
2603
|
+
}
|
|
2604
|
+
} else {
|
|
2605
|
+
chunkY = view.getInt32(chunkOffset, true);
|
|
2606
|
+
dataSize = view.getInt32(chunkOffset + 4, true);
|
|
2607
|
+
dataPtr = chunkOffset + 8;
|
|
2608
|
+
}
|
|
2609
|
+
if (dataSize < 0 || dataPtr < 0 || dataPtr + dataSize > view.byteLength) {
|
|
2610
|
+
throw new ExrError("MALFORMED_CHUNK", "Chunk payload is outside file bounds.", {
|
|
2611
|
+
partId: part.id,
|
|
2612
|
+
chunkIndex,
|
|
2613
|
+
chunkY,
|
|
2614
|
+
dataPtr,
|
|
2615
|
+
dataSize
|
|
2616
|
+
});
|
|
2617
|
+
}
|
|
2618
|
+
const linesInChunk = Math.max(0, Math.min(linesPerBlock, part.dataWindow.yMax - chunkY + 1));
|
|
2619
|
+
const expectedUncompressedSize = getExpectedUncompressedChunkSize(
|
|
2620
|
+
part,
|
|
2621
|
+
channelMeta,
|
|
2622
|
+
chunkY,
|
|
2623
|
+
linesInChunk
|
|
2624
|
+
);
|
|
2625
|
+
const predecodedZipBlock = options.predecodedZipBlocks?.get(chunkIndex);
|
|
2626
|
+
const predecodedDwaBlock = options.predecodedDwaBlocks?.get(chunkIndex);
|
|
2627
|
+
const predecodedBlock = predecodedZipBlock ?? predecodedDwaBlock;
|
|
2628
|
+
const blockData = predecodedBlock ? predecodedBlock : dataSize === expectedUncompressedSize ? new Uint8Array(buffer, dataPtr, dataSize) : compressionHandler.decodeBlock({
|
|
2629
|
+
buffer,
|
|
2630
|
+
dataPtr,
|
|
2631
|
+
dataSize,
|
|
2632
|
+
expectedUncompressedSize,
|
|
2633
|
+
part,
|
|
2634
|
+
partId: part.id,
|
|
2635
|
+
chunkIndex,
|
|
2636
|
+
chunkY,
|
|
2637
|
+
linesInChunk,
|
|
2638
|
+
zipScratch: compression === 2 || compression === 3 ? zipScratch : void 0
|
|
2639
|
+
});
|
|
2640
|
+
bytesRead += dataSize;
|
|
2641
|
+
let blockPointer = 0;
|
|
2642
|
+
const blockView = new DataView(blockData.buffer, blockData.byteOffset, blockData.byteLength);
|
|
2643
|
+
for (let dy = 0; dy < linesInChunk; dy++) {
|
|
2644
|
+
const y = chunkY + dy;
|
|
2645
|
+
if (y < part.dataWindow.yMin) {
|
|
2646
|
+
continue;
|
|
2647
|
+
}
|
|
2648
|
+
for (const meta of channelMeta) {
|
|
2649
|
+
if (meta.sampledWidth === 0 || meta.sampledHeight === 0) {
|
|
2650
|
+
continue;
|
|
2651
|
+
}
|
|
2652
|
+
if (!isSampledCoordinate4(y, meta.sampleOriginY, meta.ySampling)) {
|
|
2653
|
+
continue;
|
|
2654
|
+
}
|
|
2655
|
+
const byteLength = meta.rowByteLength;
|
|
2656
|
+
if (blockPointer + byteLength > blockData.byteLength) {
|
|
2657
|
+
throw new ExrError("MALFORMED_CHUNK", "Chunk row data exceeded block payload bounds.", {
|
|
2658
|
+
partId: part.id,
|
|
2659
|
+
chunkIndex,
|
|
2660
|
+
row: y,
|
|
2661
|
+
channel: meta.channel.name,
|
|
2662
|
+
blockPointer,
|
|
2663
|
+
byteLength,
|
|
2664
|
+
blockSize: blockData.byteLength
|
|
2665
|
+
});
|
|
2666
|
+
}
|
|
2667
|
+
const sampledRow = Math.floor((y - meta.sampleOriginY) / meta.ySampling);
|
|
2668
|
+
if (sampledRow < 0 || sampledRow >= meta.sampledHeight) {
|
|
2669
|
+
blockPointer += byteLength;
|
|
2670
|
+
continue;
|
|
2671
|
+
}
|
|
2672
|
+
const rowOffset = sampledRow * meta.sampledWidth;
|
|
2673
|
+
decodeRowIntoChannel(meta, blockData, blockPointer, rowOffset, blockView);
|
|
2674
|
+
blockPointer += byteLength;
|
|
2675
|
+
}
|
|
2676
|
+
}
|
|
2677
|
+
if (blockPointer < blockData.byteLength) {
|
|
2678
|
+
emit(onEvent, {
|
|
2679
|
+
phase: "decode",
|
|
2680
|
+
level: "warn",
|
|
2681
|
+
code: "decode.chunk.trailing_bytes",
|
|
2682
|
+
message: "Chunk had trailing bytes after row decode.",
|
|
2683
|
+
metrics: {
|
|
2684
|
+
partId: part.id,
|
|
2685
|
+
chunkIndex,
|
|
2686
|
+
trailing: blockData.byteLength - blockPointer
|
|
2687
|
+
}
|
|
2688
|
+
});
|
|
2689
|
+
}
|
|
2690
|
+
}
|
|
2691
|
+
const decodedChannels = {};
|
|
2692
|
+
for (const meta of channelMeta) {
|
|
2693
|
+
decodedChannels[meta.channel.name] = {
|
|
2694
|
+
pixelType: meta.channel.pixelType,
|
|
2695
|
+
xSampling: meta.channel.xSampling,
|
|
2696
|
+
ySampling: meta.channel.ySampling,
|
|
2697
|
+
sampledWidth: meta.sampledWidth,
|
|
2698
|
+
sampledHeight: meta.sampledHeight,
|
|
2699
|
+
sampleOriginX: meta.sampleOriginX,
|
|
2700
|
+
sampleOriginY: meta.sampleOriginY,
|
|
2701
|
+
data: meta.data
|
|
2702
|
+
};
|
|
2703
|
+
}
|
|
2704
|
+
emit(onEvent, {
|
|
2705
|
+
phase: "decode",
|
|
2706
|
+
level: "info",
|
|
2707
|
+
code: "decode.complete",
|
|
2708
|
+
message: `Decoded part ${part.id}.`,
|
|
2709
|
+
metrics: {
|
|
2710
|
+
partId: part.id,
|
|
2711
|
+
channels: Object.keys(decodedChannels).length,
|
|
2712
|
+
readKB: (bytesRead / 1024).toFixed(1),
|
|
2713
|
+
ms: (nowMs() - tDecode).toFixed(3),
|
|
2714
|
+
totalMs: (nowMs() - t0).toFixed(3)
|
|
2715
|
+
}
|
|
2716
|
+
});
|
|
2717
|
+
return {
|
|
2718
|
+
width,
|
|
2719
|
+
height,
|
|
2720
|
+
channels: decodedChannels
|
|
2721
|
+
};
|
|
2722
|
+
}
|
|
2723
|
+
|
|
2724
|
+
// src/browser/decodeExrPartWithWorkers.ts
|
|
2725
|
+
var ZIPS_COMPRESSION = 2;
|
|
2726
|
+
var ZIP_COMPRESSION = 3;
|
|
2727
|
+
var DWAA_COMPRESSION = 8;
|
|
2728
|
+
var DWAB_COMPRESSION = 9;
|
|
2729
|
+
var DEFAULT_MIN_ZIP_CHUNKS = 8;
|
|
2730
|
+
var DEFAULT_MIN_DWA_CHUNKS = 2;
|
|
2731
|
+
var DEFAULT_MAX_WORKERS = 8;
|
|
2732
|
+
function clamp(value, min, max) {
|
|
2733
|
+
if (value < min) return min;
|
|
2734
|
+
if (value > max) return max;
|
|
2735
|
+
return value;
|
|
2736
|
+
}
|
|
2737
|
+
function modulo5(value, base) {
|
|
2738
|
+
const result = value % base;
|
|
2739
|
+
return result < 0 ? result + base : result;
|
|
2740
|
+
}
|
|
2741
|
+
function firstSampleCoordinate5(min, sampling) {
|
|
2742
|
+
if (sampling <= 1) return min;
|
|
2743
|
+
const remainder = modulo5(min, sampling);
|
|
2744
|
+
return remainder === 0 ? min : min + (sampling - remainder);
|
|
2745
|
+
}
|
|
2746
|
+
function countSamplesInRange5(min, max, sampling) {
|
|
2747
|
+
if (sampling <= 0 || max < min) return 0;
|
|
2748
|
+
const first = firstSampleCoordinate5(min, sampling);
|
|
2749
|
+
if (first > max) return 0;
|
|
2750
|
+
return Math.floor((max - first) / sampling) + 1;
|
|
2751
|
+
}
|
|
2752
|
+
function isSampledCoordinate5(value, firstSample, sampling) {
|
|
2753
|
+
if (sampling <= 1) return true;
|
|
2754
|
+
if (value < firstSample) return false;
|
|
2755
|
+
return (value - firstSample) % sampling === 0;
|
|
2756
|
+
}
|
|
2757
|
+
function toNumberOffset2(low, high) {
|
|
2758
|
+
if (high <= 2097151) {
|
|
2759
|
+
return high * 4294967296 + low;
|
|
2760
|
+
}
|
|
2761
|
+
const combined = BigInt(low) + (BigInt(high) << 32n);
|
|
2762
|
+
if (combined > BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
2763
|
+
throw new Error("Chunk offset exceeds safe integer range.");
|
|
2764
|
+
}
|
|
2765
|
+
return Number(combined);
|
|
2766
|
+
}
|
|
2767
|
+
function buildZipChannelMeta(part) {
|
|
2768
|
+
if (!part.dataWindow) return [];
|
|
2769
|
+
const { xMin, xMax, yMin, yMax } = part.dataWindow;
|
|
2770
|
+
return part.channels.map((channel) => {
|
|
2771
|
+
const xSampling = channel.xSampling > 0 ? channel.xSampling : 1;
|
|
2772
|
+
const ySampling = channel.ySampling > 0 ? channel.ySampling : 1;
|
|
2773
|
+
const sampledWidth = countSamplesInRange5(xMin, xMax, xSampling);
|
|
2774
|
+
const sampledHeight = countSamplesInRange5(yMin, yMax, ySampling);
|
|
2775
|
+
const bytesPerSample = channel.pixelType === 1 ? 2 : 4;
|
|
2776
|
+
return {
|
|
2777
|
+
ySampling,
|
|
2778
|
+
sampleOriginY: firstSampleCoordinate5(yMin, ySampling),
|
|
2779
|
+
sampledWidth,
|
|
2780
|
+
sampledHeight,
|
|
2781
|
+
rowByteLength: sampledWidth * bytesPerSample
|
|
2782
|
+
};
|
|
2783
|
+
});
|
|
2784
|
+
}
|
|
2785
|
+
function getExpectedUncompressedChunkSize2(part, channelMeta, chunkY, linesInChunk) {
|
|
2786
|
+
if (!part.dataWindow) return 0;
|
|
2787
|
+
let expected = 0;
|
|
2788
|
+
for (let dy = 0; dy < linesInChunk; dy++) {
|
|
2789
|
+
const y = chunkY + dy;
|
|
2790
|
+
if (y < part.dataWindow.yMin) {
|
|
2791
|
+
continue;
|
|
2792
|
+
}
|
|
2793
|
+
for (const meta of channelMeta) {
|
|
2794
|
+
if (meta.sampledWidth === 0 || meta.sampledHeight === 0) {
|
|
2795
|
+
continue;
|
|
2796
|
+
}
|
|
2797
|
+
if (!isSampledCoordinate5(y, meta.sampleOriginY, meta.ySampling)) {
|
|
2798
|
+
continue;
|
|
2799
|
+
}
|
|
2800
|
+
expected += meta.rowByteLength;
|
|
2801
|
+
}
|
|
2802
|
+
}
|
|
2803
|
+
return expected;
|
|
2804
|
+
}
|
|
2805
|
+
function getParallelWorkerCount(taskCount, maxWorkers) {
|
|
2806
|
+
const hardware = typeof navigator !== "undefined" && typeof navigator.hardwareConcurrency === "number" ? navigator.hardwareConcurrency : 4;
|
|
2807
|
+
const target = hardware > 1 ? hardware - 1 : 1;
|
|
2808
|
+
return Math.max(1, Math.min(taskCount, target, maxWorkers));
|
|
2809
|
+
}
|
|
2810
|
+
function collectCompressedChunkTasks(buffer, structure, part, partIndex) {
|
|
2811
|
+
if (!part.dataWindow) return [];
|
|
2812
|
+
const compression = part.compression ?? 0;
|
|
2813
|
+
if (compression !== ZIPS_COMPRESSION && compression !== ZIP_COMPRESSION && compression !== DWAA_COMPRESSION && compression !== DWAB_COMPRESSION) {
|
|
2814
|
+
return [];
|
|
2815
|
+
}
|
|
2816
|
+
const view = new DataView(buffer);
|
|
2817
|
+
const linesPerBlock = getScanlineLinesPerBlock(compression);
|
|
2818
|
+
const height = part.dataWindow.yMax - part.dataWindow.yMin + 1;
|
|
2819
|
+
const chunkCount = Math.ceil(height / linesPerBlock);
|
|
2820
|
+
let offsetTablePtr = structure.headerEndOffset;
|
|
2821
|
+
for (let i = 0; i < partIndex; i++) {
|
|
2822
|
+
const prior = structure.parts[i];
|
|
2823
|
+
if (!prior.dataWindow) continue;
|
|
2824
|
+
const priorHeight = prior.dataWindow.yMax - prior.dataWindow.yMin + 1;
|
|
2825
|
+
const priorChunks = Math.ceil(priorHeight / getScanlineLinesPerBlock(prior.compression ?? 0));
|
|
2826
|
+
offsetTablePtr += priorChunks * 8;
|
|
2827
|
+
}
|
|
2828
|
+
if (offsetTablePtr < 0 || offsetTablePtr + chunkCount * 8 > view.byteLength) {
|
|
2829
|
+
throw new Error("Offset table is truncated or invalid.");
|
|
2830
|
+
}
|
|
2831
|
+
const chunkOffsets = new Array(chunkCount);
|
|
2832
|
+
for (let i = 0; i < chunkCount; i++) {
|
|
2833
|
+
const low = view.getUint32(offsetTablePtr + i * 8, true);
|
|
2834
|
+
const high = view.getUint32(offsetTablePtr + i * 8 + 4, true);
|
|
2835
|
+
chunkOffsets[i] = toNumberOffset2(low, high);
|
|
2836
|
+
}
|
|
2837
|
+
const channelMeta = buildZipChannelMeta(part);
|
|
2838
|
+
const tasks = [];
|
|
2839
|
+
for (let chunkIndex = 0; chunkIndex < chunkOffsets.length; chunkIndex++) {
|
|
2840
|
+
const chunkOffset = chunkOffsets[chunkIndex];
|
|
2841
|
+
const chunkHeaderSize = structure.isMultipart ? 12 : 8;
|
|
2842
|
+
if (chunkOffset < 0 || chunkOffset + chunkHeaderSize > view.byteLength) {
|
|
2843
|
+
throw new Error(`Chunk ${chunkIndex} header is outside file bounds.`);
|
|
2844
|
+
}
|
|
2845
|
+
let chunkY = 0;
|
|
2846
|
+
let dataSize = 0;
|
|
2847
|
+
let dataPtr = chunkOffset;
|
|
2848
|
+
if (structure.isMultipart) {
|
|
2849
|
+
const partNumber = view.getInt32(chunkOffset, true);
|
|
2850
|
+
chunkY = view.getInt32(chunkOffset + 4, true);
|
|
2851
|
+
dataSize = view.getInt32(chunkOffset + 8, true);
|
|
2852
|
+
dataPtr = chunkOffset + 12;
|
|
2853
|
+
if (partNumber !== part.id) {
|
|
2854
|
+
continue;
|
|
2855
|
+
}
|
|
2856
|
+
} else {
|
|
2857
|
+
chunkY = view.getInt32(chunkOffset, true);
|
|
2858
|
+
dataSize = view.getInt32(chunkOffset + 4, true);
|
|
2859
|
+
dataPtr = chunkOffset + 8;
|
|
2860
|
+
}
|
|
2861
|
+
if (dataSize < 0 || dataPtr < 0 || dataPtr + dataSize > view.byteLength) {
|
|
2862
|
+
throw new Error(`Chunk ${chunkIndex} payload is outside file bounds.`);
|
|
2863
|
+
}
|
|
2864
|
+
const linesInChunk = Math.max(0, Math.min(linesPerBlock, part.dataWindow.yMax - chunkY + 1));
|
|
2865
|
+
const expectedUncompressedSize = getExpectedUncompressedChunkSize2(
|
|
2866
|
+
part,
|
|
2867
|
+
channelMeta,
|
|
2868
|
+
chunkY,
|
|
2869
|
+
linesInChunk
|
|
2870
|
+
);
|
|
2871
|
+
if (dataSize !== expectedUncompressedSize) {
|
|
2872
|
+
tasks.push({
|
|
2873
|
+
chunkIndex,
|
|
2874
|
+
dataPtr,
|
|
2875
|
+
dataSize,
|
|
2876
|
+
expectedUncompressedSize,
|
|
2877
|
+
chunkY,
|
|
2878
|
+
linesInChunk
|
|
2879
|
+
});
|
|
2880
|
+
}
|
|
2881
|
+
}
|
|
2882
|
+
return tasks;
|
|
2883
|
+
}
|
|
2884
|
+
function getDefaultWorkerFactory() {
|
|
2885
|
+
return {
|
|
2886
|
+
createZipWorker: () => new Worker(new URL("./zipDecodeWorker.js", import.meta.url), { type: "module" }),
|
|
2887
|
+
createDwaWorker: () => new Worker(new URL("./dwaDecodeWorker.js", import.meta.url), { type: "module" })
|
|
2888
|
+
};
|
|
2889
|
+
}
|
|
2890
|
+
async function decodeZipTasksInWorkers(buffer, tasks, maxWorkers, workerFactory) {
|
|
2891
|
+
if (tasks.length === 0) {
|
|
2892
|
+
return /* @__PURE__ */ new Map();
|
|
2893
|
+
}
|
|
2894
|
+
const workerCount = getParallelWorkerCount(tasks.length, maxWorkers);
|
|
2895
|
+
const workers = [];
|
|
2896
|
+
const taskByMessageId = /* @__PURE__ */ new Map();
|
|
2897
|
+
const blocks = /* @__PURE__ */ new Map();
|
|
2898
|
+
return new Promise((resolve, reject) => {
|
|
2899
|
+
let completed = 0;
|
|
2900
|
+
let nextTaskIndex = 0;
|
|
2901
|
+
let nextMessageId = 1;
|
|
2902
|
+
let settled = false;
|
|
2903
|
+
const cleanup = () => {
|
|
2904
|
+
for (const worker of workers) {
|
|
2905
|
+
worker.terminate();
|
|
2906
|
+
}
|
|
2907
|
+
};
|
|
2908
|
+
const fail = (error) => {
|
|
2909
|
+
if (settled) return;
|
|
2910
|
+
settled = true;
|
|
2911
|
+
cleanup();
|
|
2912
|
+
reject(error instanceof Error ? error : new Error(String(error)));
|
|
2913
|
+
};
|
|
2914
|
+
const finishIfDone = () => {
|
|
2915
|
+
if (settled || completed !== tasks.length) return;
|
|
2916
|
+
settled = true;
|
|
2917
|
+
cleanup();
|
|
2918
|
+
resolve(blocks);
|
|
2919
|
+
};
|
|
2920
|
+
const dispatchNextTask = (worker) => {
|
|
2921
|
+
if (settled || nextTaskIndex >= tasks.length) return;
|
|
2922
|
+
const task = tasks[nextTaskIndex++];
|
|
2923
|
+
const messageId = nextMessageId++;
|
|
2924
|
+
taskByMessageId.set(messageId, task);
|
|
2925
|
+
const compressed = new Uint8Array(task.dataSize);
|
|
2926
|
+
compressed.set(new Uint8Array(buffer, task.dataPtr, task.dataSize));
|
|
2927
|
+
const request = {
|
|
2928
|
+
id: messageId,
|
|
2929
|
+
compressed: compressed.buffer,
|
|
2930
|
+
expectedUncompressedSize: task.expectedUncompressedSize
|
|
2931
|
+
};
|
|
2932
|
+
worker.postMessage(request, [request.compressed]);
|
|
2933
|
+
};
|
|
2934
|
+
for (let i = 0; i < workerCount; i++) {
|
|
2935
|
+
const worker = workerFactory.createZipWorker();
|
|
2936
|
+
workers.push(worker);
|
|
2937
|
+
worker.onmessage = (event) => {
|
|
2938
|
+
if (settled) return;
|
|
2939
|
+
const response = event.data;
|
|
2940
|
+
const task = taskByMessageId.get(response.id);
|
|
2941
|
+
if (!task) {
|
|
2942
|
+
fail(new Error("Received worker response for unknown ZIP task."));
|
|
2943
|
+
return;
|
|
2944
|
+
}
|
|
2945
|
+
taskByMessageId.delete(response.id);
|
|
2946
|
+
if (response.ok === false) {
|
|
2947
|
+
fail(new Error(response.error || "ZIP worker failed."));
|
|
2948
|
+
return;
|
|
2949
|
+
}
|
|
2950
|
+
blocks.set(task.chunkIndex, new Uint8Array(response.decoded));
|
|
2951
|
+
completed += 1;
|
|
2952
|
+
dispatchNextTask(worker);
|
|
2953
|
+
finishIfDone();
|
|
2954
|
+
};
|
|
2955
|
+
worker.onerror = (event) => {
|
|
2956
|
+
fail(event.error || new Error(event.message || "ZIP worker error."));
|
|
2957
|
+
};
|
|
2958
|
+
dispatchNextTask(worker);
|
|
2959
|
+
}
|
|
2960
|
+
});
|
|
2961
|
+
}
|
|
2962
|
+
async function decodeDwaTasksInWorkers(buffer, tasks, part, maxWorkers, workerFactory) {
|
|
2963
|
+
if (tasks.length === 0) {
|
|
2964
|
+
return /* @__PURE__ */ new Map();
|
|
2965
|
+
}
|
|
2966
|
+
const workerCount = getParallelWorkerCount(tasks.length, maxWorkers);
|
|
2967
|
+
const workers = [];
|
|
2968
|
+
const taskByMessageId = /* @__PURE__ */ new Map();
|
|
2969
|
+
const blocks = /* @__PURE__ */ new Map();
|
|
2970
|
+
return new Promise((resolve, reject) => {
|
|
2971
|
+
let completed = 0;
|
|
2972
|
+
let nextTaskIndex = 0;
|
|
2973
|
+
let nextMessageId = 1;
|
|
2974
|
+
let settled = false;
|
|
2975
|
+
const cleanup = () => {
|
|
2976
|
+
for (const worker of workers) {
|
|
2977
|
+
worker.terminate();
|
|
2978
|
+
}
|
|
2979
|
+
};
|
|
2980
|
+
const fail = (error) => {
|
|
2981
|
+
if (settled) return;
|
|
2982
|
+
settled = true;
|
|
2983
|
+
cleanup();
|
|
2984
|
+
reject(error instanceof Error ? error : new Error(String(error)));
|
|
2985
|
+
};
|
|
2986
|
+
const finishIfDone = () => {
|
|
2987
|
+
if (settled || completed !== tasks.length) return;
|
|
2988
|
+
settled = true;
|
|
2989
|
+
cleanup();
|
|
2990
|
+
resolve(blocks);
|
|
2991
|
+
};
|
|
2992
|
+
const dispatchNextTask = (worker) => {
|
|
2993
|
+
if (settled || nextTaskIndex >= tasks.length) return;
|
|
2994
|
+
const task = tasks[nextTaskIndex++];
|
|
2995
|
+
const messageId = nextMessageId++;
|
|
2996
|
+
taskByMessageId.set(messageId, task);
|
|
2997
|
+
const compressed = new Uint8Array(task.dataSize);
|
|
2998
|
+
compressed.set(new Uint8Array(buffer, task.dataPtr, task.dataSize));
|
|
2999
|
+
const request = {
|
|
3000
|
+
type: "decode",
|
|
3001
|
+
id: messageId,
|
|
3002
|
+
compressed: compressed.buffer,
|
|
3003
|
+
chunkIndex: task.chunkIndex,
|
|
3004
|
+
chunkY: task.chunkY,
|
|
3005
|
+
linesInChunk: task.linesInChunk
|
|
3006
|
+
};
|
|
3007
|
+
worker.postMessage(request, [request.compressed]);
|
|
3008
|
+
};
|
|
3009
|
+
for (let i = 0; i < workerCount; i++) {
|
|
3010
|
+
const worker = workerFactory.createDwaWorker();
|
|
3011
|
+
workers.push(worker);
|
|
3012
|
+
const initMessage = {
|
|
3013
|
+
type: "init",
|
|
3014
|
+
part,
|
|
3015
|
+
partId: part.id
|
|
3016
|
+
};
|
|
3017
|
+
worker.postMessage(initMessage);
|
|
3018
|
+
worker.onmessage = (event) => {
|
|
3019
|
+
if (settled) return;
|
|
3020
|
+
const response = event.data;
|
|
3021
|
+
const task = taskByMessageId.get(response.id);
|
|
3022
|
+
if (!task) {
|
|
3023
|
+
fail(new Error("Received worker response for unknown DWA task."));
|
|
3024
|
+
return;
|
|
3025
|
+
}
|
|
3026
|
+
taskByMessageId.delete(response.id);
|
|
3027
|
+
if (response.ok === false) {
|
|
3028
|
+
fail(new Error(response.error || "DWA worker failed."));
|
|
3029
|
+
return;
|
|
3030
|
+
}
|
|
3031
|
+
blocks.set(task.chunkIndex, new Uint8Array(response.decoded));
|
|
3032
|
+
completed += 1;
|
|
3033
|
+
dispatchNextTask(worker);
|
|
3034
|
+
finishIfDone();
|
|
3035
|
+
};
|
|
3036
|
+
worker.onerror = (event) => {
|
|
3037
|
+
fail(event.error || new Error(event.message || "DWA worker error."));
|
|
3038
|
+
};
|
|
3039
|
+
dispatchNextTask(worker);
|
|
3040
|
+
}
|
|
3041
|
+
});
|
|
3042
|
+
}
|
|
3043
|
+
function canUseParallelChunkDecode() {
|
|
3044
|
+
return typeof Worker !== "undefined";
|
|
3045
|
+
}
|
|
3046
|
+
function mergePredecodedMaps(base, patch) {
|
|
3047
|
+
if (!base && !patch) return void 0;
|
|
3048
|
+
const merged = /* @__PURE__ */ new Map();
|
|
3049
|
+
for (const [k, v] of base ?? []) merged.set(k, v);
|
|
3050
|
+
for (const [k, v] of patch ?? []) merged.set(k, v);
|
|
3051
|
+
return merged;
|
|
3052
|
+
}
|
|
3053
|
+
async function decodeExrPartWithWorkers(bufferInput, structure, options) {
|
|
3054
|
+
const buffer = toArrayBuffer(bufferInput);
|
|
3055
|
+
const partIndex = structure.parts.findIndex((candidate) => candidate.id === options.partId);
|
|
3056
|
+
const part = partIndex >= 0 ? structure.parts[partIndex] : null;
|
|
3057
|
+
if (!part?.dataWindow) {
|
|
3058
|
+
return decodeExrPart(buffer, structure, options);
|
|
3059
|
+
}
|
|
3060
|
+
const maxWorkers = clamp(options.maxWorkers ?? DEFAULT_MAX_WORKERS, 1, DEFAULT_MAX_WORKERS);
|
|
3061
|
+
const minZipChunks = Math.max(1, options.minZipChunks ?? DEFAULT_MIN_ZIP_CHUNKS);
|
|
3062
|
+
const minDwaChunks = Math.max(1, options.minDwaChunks ?? DEFAULT_MIN_DWA_CHUNKS);
|
|
3063
|
+
let predecodedZipBlocks;
|
|
3064
|
+
let predecodedDwaBlocks;
|
|
3065
|
+
const compression = part.compression ?? 0;
|
|
3066
|
+
const workerFactory = options.workerFactory ?? getDefaultWorkerFactory();
|
|
3067
|
+
if (canUseParallelChunkDecode()) {
|
|
3068
|
+
if (compression === ZIPS_COMPRESSION || compression === ZIP_COMPRESSION) {
|
|
3069
|
+
const tasks = collectCompressedChunkTasks(buffer, structure, part, partIndex);
|
|
3070
|
+
if (tasks.length >= minZipChunks) {
|
|
3071
|
+
predecodedZipBlocks = await decodeZipTasksInWorkers(
|
|
3072
|
+
buffer,
|
|
3073
|
+
tasks,
|
|
3074
|
+
maxWorkers,
|
|
3075
|
+
workerFactory
|
|
3076
|
+
);
|
|
3077
|
+
}
|
|
3078
|
+
}
|
|
3079
|
+
if (compression === DWAA_COMPRESSION || compression === DWAB_COMPRESSION) {
|
|
3080
|
+
const tasks = collectCompressedChunkTasks(buffer, structure, part, partIndex);
|
|
3081
|
+
if (tasks.length >= minDwaChunks) {
|
|
3082
|
+
predecodedDwaBlocks = await decodeDwaTasksInWorkers(
|
|
3083
|
+
buffer,
|
|
3084
|
+
tasks,
|
|
3085
|
+
part,
|
|
3086
|
+
maxWorkers,
|
|
3087
|
+
workerFactory
|
|
3088
|
+
);
|
|
3089
|
+
}
|
|
3090
|
+
}
|
|
3091
|
+
}
|
|
3092
|
+
const mergedZipBlocks = mergePredecodedMaps(options.predecodedZipBlocks, predecodedZipBlocks);
|
|
3093
|
+
const mergedDwaBlocks = mergePredecodedMaps(options.predecodedDwaBlocks, predecodedDwaBlocks);
|
|
3094
|
+
return decodeExrPart(buffer, structure, {
|
|
3095
|
+
partId: options.partId,
|
|
3096
|
+
onEvent: options.onEvent,
|
|
3097
|
+
predecodedZipBlocks: mergedZipBlocks,
|
|
3098
|
+
predecodedDwaBlocks: mergedDwaBlocks
|
|
3099
|
+
});
|
|
3100
|
+
}
|
|
3101
|
+
|
|
3102
|
+
// src/browser/expandSampledChannels.ts
|
|
3103
|
+
function clamp2(value, min, max) {
|
|
3104
|
+
if (value < min) return min;
|
|
3105
|
+
if (value > max) return max;
|
|
3106
|
+
return value;
|
|
3107
|
+
}
|
|
3108
|
+
function buildNearestSampleMap(length, worldMin, sampleOrigin, sampling, sampledLength) {
|
|
3109
|
+
const output = new Int32Array(length);
|
|
3110
|
+
if (sampledLength <= 0) return output;
|
|
3111
|
+
for (let i = 0; i < length; i++) {
|
|
3112
|
+
const world = worldMin + i;
|
|
3113
|
+
const sample = Math.round((world - sampleOrigin) / sampling);
|
|
3114
|
+
output[i] = clamp2(sample, 0, sampledLength - 1);
|
|
3115
|
+
}
|
|
3116
|
+
return output;
|
|
3117
|
+
}
|
|
3118
|
+
function expandSampledChannel(width, height, xMin, yMin, channel) {
|
|
3119
|
+
if (channel.sampledWidth === 0 || channel.sampledHeight === 0 || channel.data.length === 0) {
|
|
3120
|
+
return new Float32Array(width * height);
|
|
3121
|
+
}
|
|
3122
|
+
const xSampling = channel.xSampling > 0 ? channel.xSampling : 1;
|
|
3123
|
+
const ySampling = channel.ySampling > 0 ? channel.ySampling : 1;
|
|
3124
|
+
if (xSampling === 1 && ySampling === 1 && channel.sampledWidth === width && channel.sampledHeight === height && channel.sampleOriginX === xMin && channel.sampleOriginY === yMin) {
|
|
3125
|
+
return channel.data;
|
|
3126
|
+
}
|
|
3127
|
+
const output = new Float32Array(width * height);
|
|
3128
|
+
const xSampleMap = buildNearestSampleMap(
|
|
3129
|
+
width,
|
|
3130
|
+
xMin,
|
|
3131
|
+
channel.sampleOriginX,
|
|
3132
|
+
xSampling,
|
|
3133
|
+
channel.sampledWidth
|
|
3134
|
+
);
|
|
3135
|
+
const ySampleMap = buildNearestSampleMap(
|
|
3136
|
+
height,
|
|
3137
|
+
yMin,
|
|
3138
|
+
channel.sampleOriginY,
|
|
3139
|
+
ySampling,
|
|
3140
|
+
channel.sampledHeight
|
|
3141
|
+
);
|
|
3142
|
+
for (let y = 0; y < height; y++) {
|
|
3143
|
+
const sampleY = ySampleMap[y];
|
|
3144
|
+
const targetRow = y * width;
|
|
3145
|
+
const sourceRow = sampleY * channel.sampledWidth;
|
|
3146
|
+
for (let x = 0; x < width; x++) {
|
|
3147
|
+
output[targetRow + x] = channel.data[sourceRow + xSampleMap[x]];
|
|
3148
|
+
}
|
|
3149
|
+
}
|
|
3150
|
+
return output;
|
|
3151
|
+
}
|
|
3152
|
+
function expandDecodedPartChannels(decoded, dataWindow) {
|
|
3153
|
+
const output = {};
|
|
3154
|
+
for (const [name, channel] of Object.entries(decoded.channels)) {
|
|
3155
|
+
output[name] = expandSampledChannel(
|
|
3156
|
+
decoded.width,
|
|
3157
|
+
decoded.height,
|
|
3158
|
+
dataWindow.xMin,
|
|
3159
|
+
dataWindow.yMin,
|
|
3160
|
+
channel
|
|
3161
|
+
);
|
|
3162
|
+
}
|
|
3163
|
+
return output;
|
|
3164
|
+
}
|
|
3165
|
+
export {
|
|
3166
|
+
decodeExrPartWithWorkers,
|
|
3167
|
+
expandDecodedPartChannels,
|
|
3168
|
+
expandSampledChannel
|
|
3169
|
+
};
|
|
3170
|
+
//# sourceMappingURL=index.js.map
|