@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,1184 @@
|
|
|
1
|
+
// src/reader/compression/dwa.ts
|
|
2
|
+
import { unzlibSync } from "fflate";
|
|
3
|
+
|
|
4
|
+
// src/shared/errors.ts
|
|
5
|
+
var ExrError = class extends Error {
|
|
6
|
+
code;
|
|
7
|
+
details;
|
|
8
|
+
constructor(code, message, details) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = "ExrError";
|
|
11
|
+
this.code = code;
|
|
12
|
+
this.details = details;
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
// src/shared/half.ts
|
|
17
|
+
function createHalfToFloatTable() {
|
|
18
|
+
const exponentTable = new Uint32Array(64);
|
|
19
|
+
const offsetTable = new Uint32Array(64);
|
|
20
|
+
const mantissaTable = new Uint32Array(2048);
|
|
21
|
+
const table = new Float32Array(65536);
|
|
22
|
+
exponentTable[0] = 0;
|
|
23
|
+
for (let i = 1; i < 31; i++) {
|
|
24
|
+
exponentTable[i] = i << 23;
|
|
25
|
+
}
|
|
26
|
+
exponentTable[31] = 1199570944;
|
|
27
|
+
exponentTable[32] = 2147483648;
|
|
28
|
+
for (let i = 33; i < 63; i++) {
|
|
29
|
+
exponentTable[i] = 2147483648 + (i - 32 << 23);
|
|
30
|
+
}
|
|
31
|
+
exponentTable[63] = 3347054592;
|
|
32
|
+
offsetTable[0] = 0;
|
|
33
|
+
for (let i = 1; i < 64; i++) {
|
|
34
|
+
offsetTable[i] = i === 32 ? 0 : 1024;
|
|
35
|
+
}
|
|
36
|
+
mantissaTable[0] = 0;
|
|
37
|
+
for (let i = 1; i < 1024; i++) {
|
|
38
|
+
let m = i << 13;
|
|
39
|
+
let e = 0;
|
|
40
|
+
while ((m & 8388608) === 0) {
|
|
41
|
+
e -= 8388608;
|
|
42
|
+
m <<= 1;
|
|
43
|
+
}
|
|
44
|
+
m &= ~8388608;
|
|
45
|
+
e += 947912704;
|
|
46
|
+
mantissaTable[i] = m | e;
|
|
47
|
+
}
|
|
48
|
+
for (let i = 1024; i < 2048; i++) {
|
|
49
|
+
mantissaTable[i] = 939524096 + (i - 1024 << 13);
|
|
50
|
+
}
|
|
51
|
+
const scratch = new ArrayBuffer(4);
|
|
52
|
+
const floatView = new Float32Array(scratch);
|
|
53
|
+
const uintView = new Uint32Array(scratch);
|
|
54
|
+
for (let h = 0; h < 65536; h++) {
|
|
55
|
+
const offset = offsetTable[h >> 10];
|
|
56
|
+
const mantissa = mantissaTable[offset + (h & 1023)];
|
|
57
|
+
const exponent = exponentTable[h >> 10];
|
|
58
|
+
uintView[0] = mantissa + exponent;
|
|
59
|
+
table[h] = floatView[0];
|
|
60
|
+
}
|
|
61
|
+
return table;
|
|
62
|
+
}
|
|
63
|
+
var HALF_TO_FLOAT_TABLE = createHalfToFloatTable();
|
|
64
|
+
function float16ToFloat32(h) {
|
|
65
|
+
return HALF_TO_FLOAT_TABLE[h & 65535];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// src/reader/compression/dwa.ts
|
|
69
|
+
var HUF_ENCBITS = 16;
|
|
70
|
+
var HUF_DECBITS = 14;
|
|
71
|
+
var HUF_ENCSIZE = (1 << HUF_ENCBITS) + 1;
|
|
72
|
+
var HUF_DECSIZE = 1 << HUF_DECBITS;
|
|
73
|
+
var HUF_DECMASK = HUF_DECSIZE - 1;
|
|
74
|
+
var SHORT_ZEROCODE_RUN = 59;
|
|
75
|
+
var LONG_ZEROCODE_RUN = 63;
|
|
76
|
+
var SHORTEST_LONG_RUN = 2 + LONG_ZEROCODE_RUN - SHORT_ZEROCODE_RUN;
|
|
77
|
+
var INT16_SIZE = 2;
|
|
78
|
+
var STATIC_HUFFMAN = 0;
|
|
79
|
+
var DEFLATE = 1;
|
|
80
|
+
var UNKNOWN = 0;
|
|
81
|
+
var LOSSY_DCT = 1;
|
|
82
|
+
var RLE = 2;
|
|
83
|
+
var LOG_BASE = Math.pow(2.7182818, 2.2);
|
|
84
|
+
var TEXT_DECODER = new TextDecoder();
|
|
85
|
+
var DCT_A = 0.5 * Math.cos(Math.PI / 4);
|
|
86
|
+
var DCT_B = 0.5 * Math.cos(Math.PI / 16);
|
|
87
|
+
var DCT_C = 0.5 * Math.cos(Math.PI / 8);
|
|
88
|
+
var DCT_D = 0.5 * Math.cos(3 * Math.PI / 16);
|
|
89
|
+
var DCT_E = 0.5 * Math.cos(5 * Math.PI / 16);
|
|
90
|
+
var DCT_F = 0.5 * Math.cos(3 * Math.PI / 8);
|
|
91
|
+
var DCT_G = 0.5 * Math.cos(7 * Math.PI / 16);
|
|
92
|
+
var FLOAT32_TO_FLOAT16_SCRATCH = new ArrayBuffer(4);
|
|
93
|
+
var FLOAT32_TO_FLOAT16_FLOAT_VIEW = new Float32Array(FLOAT32_TO_FLOAT16_SCRATCH);
|
|
94
|
+
var FLOAT32_TO_FLOAT16_INT_VIEW = new Uint32Array(FLOAT32_TO_FLOAT16_SCRATCH);
|
|
95
|
+
var LEGACY_DWA_RULES = [
|
|
96
|
+
{ name: "r", compression: LOSSY_DCT, pixelType: 1, index: 0, caseInsensitive: true },
|
|
97
|
+
{ name: "r", compression: LOSSY_DCT, pixelType: 2, index: 0, caseInsensitive: true },
|
|
98
|
+
{ name: "red", compression: LOSSY_DCT, pixelType: 1, index: 0, caseInsensitive: true },
|
|
99
|
+
{ name: "red", compression: LOSSY_DCT, pixelType: 2, index: 0, caseInsensitive: true },
|
|
100
|
+
{ name: "g", compression: LOSSY_DCT, pixelType: 1, index: 1, caseInsensitive: true },
|
|
101
|
+
{ name: "g", compression: LOSSY_DCT, pixelType: 2, index: 1, caseInsensitive: true },
|
|
102
|
+
{ name: "grn", compression: LOSSY_DCT, pixelType: 1, index: 1, caseInsensitive: true },
|
|
103
|
+
{ name: "grn", compression: LOSSY_DCT, pixelType: 2, index: 1, caseInsensitive: true },
|
|
104
|
+
{ name: "green", compression: LOSSY_DCT, pixelType: 1, index: 1, caseInsensitive: true },
|
|
105
|
+
{ name: "green", compression: LOSSY_DCT, pixelType: 2, index: 1, caseInsensitive: true },
|
|
106
|
+
{ name: "b", compression: LOSSY_DCT, pixelType: 1, index: 2, caseInsensitive: true },
|
|
107
|
+
{ name: "b", compression: LOSSY_DCT, pixelType: 2, index: 2, caseInsensitive: true },
|
|
108
|
+
{ name: "blu", compression: LOSSY_DCT, pixelType: 1, index: 2, caseInsensitive: true },
|
|
109
|
+
{ name: "blu", compression: LOSSY_DCT, pixelType: 2, index: 2, caseInsensitive: true },
|
|
110
|
+
{ name: "blue", compression: LOSSY_DCT, pixelType: 1, index: 2, caseInsensitive: true },
|
|
111
|
+
{ name: "blue", compression: LOSSY_DCT, pixelType: 2, index: 2, caseInsensitive: true },
|
|
112
|
+
{ name: "y", compression: LOSSY_DCT, pixelType: 1, index: -1, caseInsensitive: true },
|
|
113
|
+
{ name: "y", compression: LOSSY_DCT, pixelType: 2, index: -1, caseInsensitive: true },
|
|
114
|
+
{ name: "by", compression: LOSSY_DCT, pixelType: 1, index: -1, caseInsensitive: true },
|
|
115
|
+
{ name: "by", compression: LOSSY_DCT, pixelType: 2, index: -1, caseInsensitive: true },
|
|
116
|
+
{ name: "ry", compression: LOSSY_DCT, pixelType: 1, index: -1, caseInsensitive: true },
|
|
117
|
+
{ name: "ry", compression: LOSSY_DCT, pixelType: 2, index: -1, caseInsensitive: true },
|
|
118
|
+
{ name: "a", compression: RLE, pixelType: 0, index: -1, caseInsensitive: true },
|
|
119
|
+
{ name: "a", compression: RLE, pixelType: 1, index: -1, caseInsensitive: true },
|
|
120
|
+
{ name: "a", compression: RLE, pixelType: 2, index: -1, caseInsensitive: true }
|
|
121
|
+
];
|
|
122
|
+
function ensureAvailable(totalLength, cursor, size, context) {
|
|
123
|
+
if (size < 0 || cursor.value < 0 || cursor.value + size > totalLength) {
|
|
124
|
+
throw new Error(`Truncated DWA payload while reading ${context}.`);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
function ensureSafeInteger(value, context) {
|
|
128
|
+
const max = BigInt(Number.MAX_SAFE_INTEGER);
|
|
129
|
+
const min = BigInt(Number.MIN_SAFE_INTEGER);
|
|
130
|
+
if (value > max || value < min) {
|
|
131
|
+
throw new Error(`DWA value exceeded safe integer range while reading ${context}.`);
|
|
132
|
+
}
|
|
133
|
+
return Number(value);
|
|
134
|
+
}
|
|
135
|
+
function readUint8(view, cursor) {
|
|
136
|
+
ensureAvailable(view.byteLength, cursor, 1, "uint8");
|
|
137
|
+
const value = view.getUint8(cursor.value);
|
|
138
|
+
cursor.value += 1;
|
|
139
|
+
return value;
|
|
140
|
+
}
|
|
141
|
+
function readUint16(view, cursor) {
|
|
142
|
+
ensureAvailable(view.byteLength, cursor, 2, "uint16");
|
|
143
|
+
const value = view.getUint16(cursor.value, true);
|
|
144
|
+
cursor.value += 2;
|
|
145
|
+
return value;
|
|
146
|
+
}
|
|
147
|
+
function readUint32(view, cursor) {
|
|
148
|
+
ensureAvailable(view.byteLength, cursor, 4, "uint32");
|
|
149
|
+
const value = view.getUint32(cursor.value, true);
|
|
150
|
+
cursor.value += 4;
|
|
151
|
+
return value;
|
|
152
|
+
}
|
|
153
|
+
function readInt64(view, cursor) {
|
|
154
|
+
ensureAvailable(view.byteLength, cursor, 8, "int64");
|
|
155
|
+
let value;
|
|
156
|
+
if ("getBigInt64" in DataView.prototype) {
|
|
157
|
+
value = ensureSafeInteger(view.getBigInt64(cursor.value, true), "int64");
|
|
158
|
+
} else {
|
|
159
|
+
const low = BigInt(view.getUint32(cursor.value, true));
|
|
160
|
+
const high = BigInt(view.getInt32(cursor.value + 4, true));
|
|
161
|
+
value = ensureSafeInteger((high << 32n) + low, "int64");
|
|
162
|
+
}
|
|
163
|
+
cursor.value += 8;
|
|
164
|
+
return value;
|
|
165
|
+
}
|
|
166
|
+
function readUint8FromArray(input, cursor) {
|
|
167
|
+
if (cursor.value < 0 || cursor.value >= input.length) {
|
|
168
|
+
throw new Error("Truncated DWA Huffman payload.");
|
|
169
|
+
}
|
|
170
|
+
const value = input[cursor.value];
|
|
171
|
+
cursor.value += 1;
|
|
172
|
+
return value;
|
|
173
|
+
}
|
|
174
|
+
function readCStringWithin(input, cursor, limit, context) {
|
|
175
|
+
if (limit < cursor.value || limit > input.length) {
|
|
176
|
+
throw new Error(`Invalid DWA bounded read while parsing ${context}.`);
|
|
177
|
+
}
|
|
178
|
+
const start = cursor.value;
|
|
179
|
+
while (cursor.value < limit) {
|
|
180
|
+
if (input[cursor.value] === 0) {
|
|
181
|
+
const value = TEXT_DECODER.decode(input.subarray(start, cursor.value));
|
|
182
|
+
cursor.value += 1;
|
|
183
|
+
return value;
|
|
184
|
+
}
|
|
185
|
+
cursor.value += 1;
|
|
186
|
+
}
|
|
187
|
+
throw new Error(`Unterminated DWA string while parsing ${context}.`);
|
|
188
|
+
}
|
|
189
|
+
function clearHuffmanDecodingTable(table) {
|
|
190
|
+
for (let i = 0; i < HUF_DECSIZE; i++) {
|
|
191
|
+
table[i] = { len: 0, lit: 0, p: null };
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
function getBits(nBits, state, input, cursor) {
|
|
195
|
+
while (state.lc < nBits) {
|
|
196
|
+
state.c = (state.c << 8 | readUint8FromArray(input, cursor)) >>> 0;
|
|
197
|
+
state.lc += 8;
|
|
198
|
+
}
|
|
199
|
+
state.lc -= nBits;
|
|
200
|
+
return state.c >>> state.lc & (1 << nBits) - 1;
|
|
201
|
+
}
|
|
202
|
+
var HUF_TABLE_BUFFER = new Int32Array(59);
|
|
203
|
+
function hufLength(code) {
|
|
204
|
+
return code & 63;
|
|
205
|
+
}
|
|
206
|
+
function hufCode(code) {
|
|
207
|
+
return code >>> 6;
|
|
208
|
+
}
|
|
209
|
+
function hufCanonicalCodeTable(hcode) {
|
|
210
|
+
HUF_TABLE_BUFFER.fill(0);
|
|
211
|
+
for (let i = 0; i < HUF_ENCSIZE; i++) {
|
|
212
|
+
HUF_TABLE_BUFFER[hcode[i]] += 1;
|
|
213
|
+
}
|
|
214
|
+
let c = 0;
|
|
215
|
+
for (let i = 58; i > 0; i--) {
|
|
216
|
+
const next = c + HUF_TABLE_BUFFER[i] >> 1;
|
|
217
|
+
HUF_TABLE_BUFFER[i] = c;
|
|
218
|
+
c = next;
|
|
219
|
+
}
|
|
220
|
+
for (let i = 0; i < HUF_ENCSIZE; i++) {
|
|
221
|
+
const length = hcode[i];
|
|
222
|
+
if (length > 0) {
|
|
223
|
+
hcode[i] = length | HUF_TABLE_BUFFER[length]++ << 6;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
function hufUnpackEncTable(input, cursor, maxTableBytes, minSymbol, maxSymbol, hcode) {
|
|
228
|
+
const start = cursor.value;
|
|
229
|
+
const state = { c: 0, lc: 0 };
|
|
230
|
+
for (let symbol = minSymbol; symbol <= maxSymbol; symbol++) {
|
|
231
|
+
if (cursor.value - start > maxTableBytes) {
|
|
232
|
+
throw new Error("Invalid DWA Huffman table payload.");
|
|
233
|
+
}
|
|
234
|
+
const length = getBits(6, state, input, cursor);
|
|
235
|
+
hcode[symbol] = length;
|
|
236
|
+
if (length === LONG_ZEROCODE_RUN) {
|
|
237
|
+
if (cursor.value - start > maxTableBytes) {
|
|
238
|
+
throw new Error("Invalid DWA Huffman table payload.");
|
|
239
|
+
}
|
|
240
|
+
let zeroRun = getBits(8, state, input, cursor) + SHORTEST_LONG_RUN;
|
|
241
|
+
if (symbol + zeroRun > maxSymbol + 1) {
|
|
242
|
+
throw new Error("Invalid DWA Huffman zero-run.");
|
|
243
|
+
}
|
|
244
|
+
while (zeroRun-- > 0) {
|
|
245
|
+
hcode[symbol++] = 0;
|
|
246
|
+
}
|
|
247
|
+
symbol--;
|
|
248
|
+
} else if (length >= SHORT_ZEROCODE_RUN) {
|
|
249
|
+
let zeroRun = length - SHORT_ZEROCODE_RUN + 2;
|
|
250
|
+
if (symbol + zeroRun > maxSymbol + 1) {
|
|
251
|
+
throw new Error("Invalid DWA Huffman zero-run.");
|
|
252
|
+
}
|
|
253
|
+
while (zeroRun-- > 0) {
|
|
254
|
+
hcode[symbol++] = 0;
|
|
255
|
+
}
|
|
256
|
+
symbol--;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
hufCanonicalCodeTable(hcode);
|
|
260
|
+
}
|
|
261
|
+
function hufBuildDecTable(hcode, minSymbol, maxSymbol, table) {
|
|
262
|
+
for (let symbol = minSymbol; symbol <= maxSymbol; symbol++) {
|
|
263
|
+
const code = hufCode(hcode[symbol]);
|
|
264
|
+
const length = hufLength(hcode[symbol]);
|
|
265
|
+
if (code >>> length) {
|
|
266
|
+
throw new Error("Invalid DWA Huffman code.");
|
|
267
|
+
}
|
|
268
|
+
if (length > HUF_DECBITS) {
|
|
269
|
+
const entry = table[code >>> length - HUF_DECBITS];
|
|
270
|
+
if (entry.len) {
|
|
271
|
+
throw new Error("Invalid DWA Huffman decode table entry.");
|
|
272
|
+
}
|
|
273
|
+
entry.lit += 1;
|
|
274
|
+
if (entry.p) {
|
|
275
|
+
entry.p.push(symbol);
|
|
276
|
+
} else {
|
|
277
|
+
entry.p = [symbol];
|
|
278
|
+
}
|
|
279
|
+
} else if (length) {
|
|
280
|
+
const base = code << HUF_DECBITS - length;
|
|
281
|
+
const count = 1 << HUF_DECBITS - length;
|
|
282
|
+
for (let i = 0; i < count; i++) {
|
|
283
|
+
const entry = table[base + i];
|
|
284
|
+
if (entry.len || entry.p) {
|
|
285
|
+
throw new Error("Invalid DWA Huffman decode table entry.");
|
|
286
|
+
}
|
|
287
|
+
entry.len = length;
|
|
288
|
+
entry.lit = symbol;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
function getChar(state, input, cursor) {
|
|
294
|
+
state.c = (state.c << 8 | readUint8FromArray(input, cursor)) >>> 0;
|
|
295
|
+
state.lc += 8;
|
|
296
|
+
}
|
|
297
|
+
function getCode(symbol, runLengthCode, state, input, cursor, outBuffer, outOffset, outEnd) {
|
|
298
|
+
if (symbol === runLengthCode) {
|
|
299
|
+
if (state.lc < 8) {
|
|
300
|
+
getChar(state, input, cursor);
|
|
301
|
+
}
|
|
302
|
+
state.lc -= 8;
|
|
303
|
+
let repeatCount = state.c >>> state.lc & 255;
|
|
304
|
+
if (outOffset.value === 0 || outOffset.value + repeatCount > outEnd) {
|
|
305
|
+
throw new Error("Invalid DWA run-length code.");
|
|
306
|
+
}
|
|
307
|
+
const last = outBuffer[outOffset.value - 1];
|
|
308
|
+
while (repeatCount-- > 0) {
|
|
309
|
+
outBuffer[outOffset.value++] = last;
|
|
310
|
+
}
|
|
311
|
+
} else if (outOffset.value < outEnd) {
|
|
312
|
+
outBuffer[outOffset.value++] = symbol;
|
|
313
|
+
} else {
|
|
314
|
+
throw new Error("DWA Huffman decode overflow.");
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
function hufDecode(encodingTable, decodingTable, input, cursor, nBits, runLengthCode, outSize, outBuffer, outOffset) {
|
|
318
|
+
const state = { c: 0, lc: 0 };
|
|
319
|
+
const inOffsetEnd = Math.trunc(cursor.value + (nBits + 7) / 8);
|
|
320
|
+
while (cursor.value < inOffsetEnd) {
|
|
321
|
+
getChar(state, input, cursor);
|
|
322
|
+
while (state.lc >= HUF_DECBITS) {
|
|
323
|
+
const index = state.c >>> state.lc - HUF_DECBITS & HUF_DECMASK;
|
|
324
|
+
const entry = decodingTable[index];
|
|
325
|
+
if (entry.len) {
|
|
326
|
+
state.lc -= entry.len;
|
|
327
|
+
getCode(entry.lit, runLengthCode, state, input, cursor, outBuffer, outOffset, outSize);
|
|
328
|
+
} else {
|
|
329
|
+
if (!entry.p) {
|
|
330
|
+
throw new Error("DWA Huffman decode failed.");
|
|
331
|
+
}
|
|
332
|
+
let matched = false;
|
|
333
|
+
for (let i = 0; i < entry.lit; i++) {
|
|
334
|
+
const symbol = entry.p[i];
|
|
335
|
+
const length = hufLength(encodingTable[symbol]);
|
|
336
|
+
while (state.lc < length && cursor.value < inOffsetEnd) {
|
|
337
|
+
getChar(state, input, cursor);
|
|
338
|
+
}
|
|
339
|
+
if (state.lc >= length) {
|
|
340
|
+
const code = hufCode(encodingTable[symbol]);
|
|
341
|
+
const candidate = state.c >>> state.lc - length & (1 << length) - 1;
|
|
342
|
+
if (code === candidate) {
|
|
343
|
+
state.lc -= length;
|
|
344
|
+
getCode(symbol, runLengthCode, state, input, cursor, outBuffer, outOffset, outSize);
|
|
345
|
+
matched = true;
|
|
346
|
+
break;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
if (!matched) {
|
|
351
|
+
throw new Error("DWA Huffman decode failed.");
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
const pad = 8 - nBits & 7;
|
|
357
|
+
state.c >>>= pad;
|
|
358
|
+
state.lc -= pad;
|
|
359
|
+
while (state.lc > 0) {
|
|
360
|
+
const entry = decodingTable[state.c << HUF_DECBITS - state.lc & HUF_DECMASK];
|
|
361
|
+
if (!entry.len) {
|
|
362
|
+
throw new Error("DWA Huffman decode failed.");
|
|
363
|
+
}
|
|
364
|
+
state.lc -= entry.len;
|
|
365
|
+
getCode(entry.lit, runLengthCode, state, input, cursor, outBuffer, outOffset, outSize);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
function hufUncompress(input, view, cursor, nCompressed, out, nRaw) {
|
|
369
|
+
const outOffset = { value: 0 };
|
|
370
|
+
const initialOffset = cursor.value;
|
|
371
|
+
const minSymbol = readUint32(view, cursor);
|
|
372
|
+
const maxSymbol = readUint32(view, cursor);
|
|
373
|
+
readUint32(view, cursor);
|
|
374
|
+
const nBits = readUint32(view, cursor);
|
|
375
|
+
readUint32(view, cursor);
|
|
376
|
+
if (minSymbol < 0 || minSymbol >= HUF_ENCSIZE || maxSymbol < 0 || maxSymbol >= HUF_ENCSIZE || minSymbol > maxSymbol) {
|
|
377
|
+
throw new Error("Invalid DWA Huffman symbol bounds.");
|
|
378
|
+
}
|
|
379
|
+
const encodingTable = new Int32Array(HUF_ENCSIZE);
|
|
380
|
+
const decodingTable = new Array(HUF_DECSIZE);
|
|
381
|
+
clearHuffmanDecodingTable(decodingTable);
|
|
382
|
+
const tableBytes = nCompressed - (cursor.value - initialOffset);
|
|
383
|
+
if (tableBytes < 0) {
|
|
384
|
+
throw new Error("Invalid DWA Huffman payload size.");
|
|
385
|
+
}
|
|
386
|
+
hufUnpackEncTable(input, cursor, tableBytes, minSymbol, maxSymbol, encodingTable);
|
|
387
|
+
if (nBits > 8 * (nCompressed - (cursor.value - initialOffset))) {
|
|
388
|
+
throw new Error("Invalid DWA Huffman bit count.");
|
|
389
|
+
}
|
|
390
|
+
hufBuildDecTable(encodingTable, minSymbol, maxSymbol, decodingTable);
|
|
391
|
+
hufDecode(encodingTable, decodingTable, input, cursor, nBits, maxSymbol, nRaw, out, outOffset);
|
|
392
|
+
if (outOffset.value !== nRaw) {
|
|
393
|
+
throw new Error("DWA Huffman output size mismatch.");
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
var DWA_DC_SCRATCH = {
|
|
397
|
+
inflate: new Uint8Array(0),
|
|
398
|
+
output: new Uint8Array(0)
|
|
399
|
+
};
|
|
400
|
+
function undoZipPredictorAndInterleave(data, output) {
|
|
401
|
+
const length = data.length;
|
|
402
|
+
if (length === 0) {
|
|
403
|
+
return data;
|
|
404
|
+
}
|
|
405
|
+
const half = length + 1 >> 1;
|
|
406
|
+
let predicted = data[0];
|
|
407
|
+
output[0] = predicted;
|
|
408
|
+
for (let i = 1; i < half; i++) {
|
|
409
|
+
predicted = predicted + data[i] - 128 & 255;
|
|
410
|
+
output[i << 1] = predicted;
|
|
411
|
+
}
|
|
412
|
+
for (let i = half; i < length; i++) {
|
|
413
|
+
predicted = predicted + data[i] - 128 & 255;
|
|
414
|
+
output[(i - half << 1) + 1] = predicted;
|
|
415
|
+
}
|
|
416
|
+
return output;
|
|
417
|
+
}
|
|
418
|
+
function ensureDwaScratchBuffer(buffer, size) {
|
|
419
|
+
return buffer.byteLength >= size ? buffer.subarray(0, size) : new Uint8Array(size);
|
|
420
|
+
}
|
|
421
|
+
function uncompressZipStyle(input, offset, size, expectedUncompressedSize, scratch) {
|
|
422
|
+
if (size < 0 || offset < 0 || offset + size > input.length) {
|
|
423
|
+
throw new Error("Invalid DWA ZIP payload bounds.");
|
|
424
|
+
}
|
|
425
|
+
const compressed = input.subarray(offset, offset + size);
|
|
426
|
+
const inflateOut = expectedUncompressedSize > 0 ? ensureDwaScratchBuffer(scratch.inflate, expectedUncompressedSize) : scratch.inflate;
|
|
427
|
+
if (expectedUncompressedSize > 0 && inflateOut !== scratch.inflate) {
|
|
428
|
+
scratch.inflate = inflateOut;
|
|
429
|
+
}
|
|
430
|
+
const raw = expectedUncompressedSize > 0 ? unzlibSync(compressed, { out: inflateOut }) : unzlibSync(compressed);
|
|
431
|
+
const outputBuffer = ensureDwaScratchBuffer(scratch.output, raw.byteLength);
|
|
432
|
+
if (outputBuffer !== scratch.output) {
|
|
433
|
+
scratch.output = outputBuffer;
|
|
434
|
+
}
|
|
435
|
+
return undoZipPredictorAndInterleave(raw, outputBuffer);
|
|
436
|
+
}
|
|
437
|
+
function decodeRunLength(input, expectedSize) {
|
|
438
|
+
if (expectedSize < 0) {
|
|
439
|
+
throw new Error("Invalid DWA RLE expected size.");
|
|
440
|
+
}
|
|
441
|
+
const out = new Uint8Array(expectedSize);
|
|
442
|
+
let ptr = 0;
|
|
443
|
+
let outPtr = 0;
|
|
444
|
+
while (ptr < input.byteLength) {
|
|
445
|
+
const length = input[ptr] << 24 >> 24;
|
|
446
|
+
ptr += 1;
|
|
447
|
+
if (length < 0) {
|
|
448
|
+
const count = -length;
|
|
449
|
+
if (ptr + count > input.byteLength || outPtr + count > out.byteLength) {
|
|
450
|
+
throw new Error("Invalid DWA RLE payload bounds.");
|
|
451
|
+
}
|
|
452
|
+
out.set(input.subarray(ptr, ptr + count), outPtr);
|
|
453
|
+
ptr += count;
|
|
454
|
+
outPtr += count;
|
|
455
|
+
} else {
|
|
456
|
+
const count = length + 1;
|
|
457
|
+
if (ptr >= input.byteLength || outPtr + count > out.byteLength) {
|
|
458
|
+
throw new Error("Invalid DWA RLE payload bounds.");
|
|
459
|
+
}
|
|
460
|
+
const value = input[ptr++];
|
|
461
|
+
out.fill(value, outPtr, outPtr + count);
|
|
462
|
+
outPtr += count;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
if (outPtr !== out.byteLength) {
|
|
466
|
+
throw new Error("Invalid DWA RLE payload length.");
|
|
467
|
+
}
|
|
468
|
+
return out;
|
|
469
|
+
}
|
|
470
|
+
function unRleAC(currAc, acBuffer, halfZigBlock) {
|
|
471
|
+
let dctComp = 1;
|
|
472
|
+
while (dctComp < 64) {
|
|
473
|
+
if (currAc.value >= acBuffer.length) {
|
|
474
|
+
throw new Error("Invalid DWA AC coefficient bounds.");
|
|
475
|
+
}
|
|
476
|
+
const acValue = acBuffer[currAc.value++];
|
|
477
|
+
if (acValue === 65280) {
|
|
478
|
+
dctComp = 64;
|
|
479
|
+
} else if (acValue >> 8 === 255) {
|
|
480
|
+
dctComp += acValue & 255;
|
|
481
|
+
} else {
|
|
482
|
+
halfZigBlock[dctComp++] = acValue;
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
function unZigZag(src, dst) {
|
|
487
|
+
dst[0] = float16ToFloat32(src[0]);
|
|
488
|
+
dst[1] = float16ToFloat32(src[1]);
|
|
489
|
+
dst[2] = float16ToFloat32(src[5]);
|
|
490
|
+
dst[3] = float16ToFloat32(src[6]);
|
|
491
|
+
dst[4] = float16ToFloat32(src[14]);
|
|
492
|
+
dst[5] = float16ToFloat32(src[15]);
|
|
493
|
+
dst[6] = float16ToFloat32(src[27]);
|
|
494
|
+
dst[7] = float16ToFloat32(src[28]);
|
|
495
|
+
dst[8] = float16ToFloat32(src[2]);
|
|
496
|
+
dst[9] = float16ToFloat32(src[4]);
|
|
497
|
+
dst[10] = float16ToFloat32(src[7]);
|
|
498
|
+
dst[11] = float16ToFloat32(src[13]);
|
|
499
|
+
dst[12] = float16ToFloat32(src[16]);
|
|
500
|
+
dst[13] = float16ToFloat32(src[26]);
|
|
501
|
+
dst[14] = float16ToFloat32(src[29]);
|
|
502
|
+
dst[15] = float16ToFloat32(src[42]);
|
|
503
|
+
dst[16] = float16ToFloat32(src[3]);
|
|
504
|
+
dst[17] = float16ToFloat32(src[8]);
|
|
505
|
+
dst[18] = float16ToFloat32(src[12]);
|
|
506
|
+
dst[19] = float16ToFloat32(src[17]);
|
|
507
|
+
dst[20] = float16ToFloat32(src[25]);
|
|
508
|
+
dst[21] = float16ToFloat32(src[30]);
|
|
509
|
+
dst[22] = float16ToFloat32(src[41]);
|
|
510
|
+
dst[23] = float16ToFloat32(src[43]);
|
|
511
|
+
dst[24] = float16ToFloat32(src[9]);
|
|
512
|
+
dst[25] = float16ToFloat32(src[11]);
|
|
513
|
+
dst[26] = float16ToFloat32(src[18]);
|
|
514
|
+
dst[27] = float16ToFloat32(src[24]);
|
|
515
|
+
dst[28] = float16ToFloat32(src[31]);
|
|
516
|
+
dst[29] = float16ToFloat32(src[40]);
|
|
517
|
+
dst[30] = float16ToFloat32(src[44]);
|
|
518
|
+
dst[31] = float16ToFloat32(src[53]);
|
|
519
|
+
dst[32] = float16ToFloat32(src[10]);
|
|
520
|
+
dst[33] = float16ToFloat32(src[19]);
|
|
521
|
+
dst[34] = float16ToFloat32(src[23]);
|
|
522
|
+
dst[35] = float16ToFloat32(src[32]);
|
|
523
|
+
dst[36] = float16ToFloat32(src[39]);
|
|
524
|
+
dst[37] = float16ToFloat32(src[45]);
|
|
525
|
+
dst[38] = float16ToFloat32(src[52]);
|
|
526
|
+
dst[39] = float16ToFloat32(src[54]);
|
|
527
|
+
dst[40] = float16ToFloat32(src[20]);
|
|
528
|
+
dst[41] = float16ToFloat32(src[22]);
|
|
529
|
+
dst[42] = float16ToFloat32(src[33]);
|
|
530
|
+
dst[43] = float16ToFloat32(src[38]);
|
|
531
|
+
dst[44] = float16ToFloat32(src[46]);
|
|
532
|
+
dst[45] = float16ToFloat32(src[51]);
|
|
533
|
+
dst[46] = float16ToFloat32(src[55]);
|
|
534
|
+
dst[47] = float16ToFloat32(src[60]);
|
|
535
|
+
dst[48] = float16ToFloat32(src[21]);
|
|
536
|
+
dst[49] = float16ToFloat32(src[34]);
|
|
537
|
+
dst[50] = float16ToFloat32(src[37]);
|
|
538
|
+
dst[51] = float16ToFloat32(src[47]);
|
|
539
|
+
dst[52] = float16ToFloat32(src[50]);
|
|
540
|
+
dst[53] = float16ToFloat32(src[56]);
|
|
541
|
+
dst[54] = float16ToFloat32(src[59]);
|
|
542
|
+
dst[55] = float16ToFloat32(src[61]);
|
|
543
|
+
dst[56] = float16ToFloat32(src[35]);
|
|
544
|
+
dst[57] = float16ToFloat32(src[36]);
|
|
545
|
+
dst[58] = float16ToFloat32(src[48]);
|
|
546
|
+
dst[59] = float16ToFloat32(src[49]);
|
|
547
|
+
dst[60] = float16ToFloat32(src[57]);
|
|
548
|
+
dst[61] = float16ToFloat32(src[58]);
|
|
549
|
+
dst[62] = float16ToFloat32(src[62]);
|
|
550
|
+
dst[63] = float16ToFloat32(src[63]);
|
|
551
|
+
}
|
|
552
|
+
function dctInverse(data) {
|
|
553
|
+
for (let row = 0; row < 8; row++) {
|
|
554
|
+
const rowPtr = row * 8;
|
|
555
|
+
const x0 = data[rowPtr + 0];
|
|
556
|
+
const x1 = data[rowPtr + 1];
|
|
557
|
+
const x2 = data[rowPtr + 2];
|
|
558
|
+
const x3 = data[rowPtr + 3];
|
|
559
|
+
const x4 = data[rowPtr + 4];
|
|
560
|
+
const x5 = data[rowPtr + 5];
|
|
561
|
+
const x6 = data[rowPtr + 6];
|
|
562
|
+
const x7 = data[rowPtr + 7];
|
|
563
|
+
const alpha0 = DCT_C * x2;
|
|
564
|
+
const alpha1 = DCT_F * x2;
|
|
565
|
+
const alpha2 = DCT_C * x6;
|
|
566
|
+
const alpha3 = DCT_F * x6;
|
|
567
|
+
const beta0 = DCT_B * x1 + DCT_D * x3 + DCT_E * x5 + DCT_G * x7;
|
|
568
|
+
const beta1 = DCT_D * x1 - DCT_G * x3 - DCT_B * x5 - DCT_E * x7;
|
|
569
|
+
const beta2 = DCT_E * x1 - DCT_B * x3 + DCT_G * x5 + DCT_D * x7;
|
|
570
|
+
const beta3 = DCT_G * x1 - DCT_E * x3 + DCT_D * x5 - DCT_B * x7;
|
|
571
|
+
const theta0 = DCT_A * (x0 + x4);
|
|
572
|
+
const theta1 = alpha0 + alpha3;
|
|
573
|
+
const theta2 = alpha1 - alpha2;
|
|
574
|
+
const theta3 = DCT_A * (x0 - x4);
|
|
575
|
+
const gamma0 = theta0 + theta1;
|
|
576
|
+
const gamma1 = theta3 + theta2;
|
|
577
|
+
const gamma2 = theta3 - theta2;
|
|
578
|
+
const gamma3 = theta0 - theta1;
|
|
579
|
+
data[rowPtr + 0] = gamma0 + beta0;
|
|
580
|
+
data[rowPtr + 1] = gamma1 + beta1;
|
|
581
|
+
data[rowPtr + 2] = gamma2 + beta2;
|
|
582
|
+
data[rowPtr + 3] = gamma3 + beta3;
|
|
583
|
+
data[rowPtr + 4] = gamma3 - beta3;
|
|
584
|
+
data[rowPtr + 5] = gamma2 - beta2;
|
|
585
|
+
data[rowPtr + 6] = gamma1 - beta1;
|
|
586
|
+
data[rowPtr + 7] = gamma0 - beta0;
|
|
587
|
+
}
|
|
588
|
+
for (let column = 0; column < 8; column++) {
|
|
589
|
+
const x0 = data[column];
|
|
590
|
+
const x1 = data[8 + column];
|
|
591
|
+
const x2 = data[16 + column];
|
|
592
|
+
const x3 = data[24 + column];
|
|
593
|
+
const x4 = data[32 + column];
|
|
594
|
+
const x5 = data[40 + column];
|
|
595
|
+
const x6 = data[48 + column];
|
|
596
|
+
const x7 = data[56 + column];
|
|
597
|
+
const alpha0 = DCT_C * x2;
|
|
598
|
+
const alpha1 = DCT_F * x2;
|
|
599
|
+
const alpha2 = DCT_C * x6;
|
|
600
|
+
const alpha3 = DCT_F * x6;
|
|
601
|
+
const beta0 = DCT_B * x1 + DCT_D * x3 + DCT_E * x5 + DCT_G * x7;
|
|
602
|
+
const beta1 = DCT_D * x1 - DCT_G * x3 - DCT_B * x5 - DCT_E * x7;
|
|
603
|
+
const beta2 = DCT_E * x1 - DCT_B * x3 + DCT_G * x5 + DCT_D * x7;
|
|
604
|
+
const beta3 = DCT_G * x1 - DCT_E * x3 + DCT_D * x5 - DCT_B * x7;
|
|
605
|
+
const theta0 = DCT_A * (x0 + x4);
|
|
606
|
+
const theta1 = alpha0 + alpha3;
|
|
607
|
+
const theta2 = alpha1 - alpha2;
|
|
608
|
+
const theta3 = DCT_A * (x0 - x4);
|
|
609
|
+
const gamma0 = theta0 + theta1;
|
|
610
|
+
const gamma1 = theta3 + theta2;
|
|
611
|
+
const gamma2 = theta3 - theta2;
|
|
612
|
+
const gamma3 = theta0 - theta1;
|
|
613
|
+
data[column] = gamma0 + beta0;
|
|
614
|
+
data[8 + column] = gamma1 + beta1;
|
|
615
|
+
data[16 + column] = gamma2 + beta2;
|
|
616
|
+
data[24 + column] = gamma3 + beta3;
|
|
617
|
+
data[32 + column] = gamma3 - beta3;
|
|
618
|
+
data[40 + column] = gamma2 - beta2;
|
|
619
|
+
data[48 + column] = gamma1 - beta1;
|
|
620
|
+
data[56 + column] = gamma0 - beta0;
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
function csc709Inverse(data) {
|
|
624
|
+
for (let i = 0; i < 64; i++) {
|
|
625
|
+
const y = data[0][i];
|
|
626
|
+
const cb = data[1][i];
|
|
627
|
+
const cr = data[2][i];
|
|
628
|
+
data[0][i] = y + 1.5747 * cr;
|
|
629
|
+
data[1][i] = y - 0.1873 * cb - 0.4682 * cr;
|
|
630
|
+
data[2][i] = y + 1.8556 * cb;
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
function float32ToFloat16(value) {
|
|
634
|
+
if (Object.is(value, 0)) return 0;
|
|
635
|
+
FLOAT32_TO_FLOAT16_FLOAT_VIEW[0] = value;
|
|
636
|
+
const bits = FLOAT32_TO_FLOAT16_INT_VIEW[0];
|
|
637
|
+
const sign = bits >>> 16 & 32768;
|
|
638
|
+
const exponent = (bits >>> 23 & 255) - 127 + 15;
|
|
639
|
+
const mantissa = bits & 8388607;
|
|
640
|
+
if (exponent <= 0) {
|
|
641
|
+
if (exponent < -10) return sign;
|
|
642
|
+
const m = (mantissa | 8388608) >> 1 - exponent;
|
|
643
|
+
return sign | m + 4096 >> 13;
|
|
644
|
+
}
|
|
645
|
+
if (exponent >= 31) {
|
|
646
|
+
return sign | 31744;
|
|
647
|
+
}
|
|
648
|
+
return sign | exponent << 10 | mantissa + 4096 >> 13;
|
|
649
|
+
}
|
|
650
|
+
function toLinear(value) {
|
|
651
|
+
const abs = Math.abs(value);
|
|
652
|
+
if (abs <= 1) {
|
|
653
|
+
const linear2 = Math.pow(abs, 2.2);
|
|
654
|
+
return value < 0 ? -linear2 : linear2;
|
|
655
|
+
}
|
|
656
|
+
const linear = Math.pow(LOG_BASE, abs - 1);
|
|
657
|
+
return value < 0 ? -linear : linear;
|
|
658
|
+
}
|
|
659
|
+
function convertToHalf(src, dst, idx) {
|
|
660
|
+
for (let i = 0; i < 64; i++) {
|
|
661
|
+
dst[idx + i] = float32ToFloat16(toLinear(src[i]));
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
function lossyDctDecode(cscSet, rowPtrs, channelData, acBuffer, dcBuffer, outBuffer, acOffset, dcOffset) {
|
|
665
|
+
const c0 = cscSet.idx[0];
|
|
666
|
+
const c1 = cscSet.idx[1];
|
|
667
|
+
const c2 = cscSet.idx[2];
|
|
668
|
+
if (c0 === void 0 || c1 === void 0 || c2 === void 0) {
|
|
669
|
+
return { acConsumed: 0, dcConsumed: 0 };
|
|
670
|
+
}
|
|
671
|
+
const cscIndices = [c0, c1, c2];
|
|
672
|
+
const dataView = new DataView(outBuffer.buffer, outBuffer.byteOffset, outBuffer.byteLength);
|
|
673
|
+
const outWords = new Uint16Array(
|
|
674
|
+
outBuffer.buffer,
|
|
675
|
+
outBuffer.byteOffset,
|
|
676
|
+
outBuffer.byteLength >> 1
|
|
677
|
+
);
|
|
678
|
+
const width = channelData[c0].width;
|
|
679
|
+
const height = channelData[c0].height;
|
|
680
|
+
const numComp = 3;
|
|
681
|
+
const numFullBlocksX = Math.floor(width / 8);
|
|
682
|
+
const numBlocksX = Math.ceil(width / 8);
|
|
683
|
+
const numBlocksY = Math.ceil(height / 8);
|
|
684
|
+
const leftoverX = width - (numBlocksX - 1) * 8;
|
|
685
|
+
const leftoverY = height - (numBlocksY - 1) * 8;
|
|
686
|
+
const currAcComp = { value: acOffset };
|
|
687
|
+
const currDcComp = new Array(numComp);
|
|
688
|
+
const dctData = new Array(numComp);
|
|
689
|
+
const halfZigBlock = new Array(numComp);
|
|
690
|
+
const rowBlock = new Array(numComp);
|
|
691
|
+
const rowOffsets = new Array(numComp);
|
|
692
|
+
for (let comp = 0; comp < numComp; comp++) {
|
|
693
|
+
rowOffsets[comp] = rowPtrs[cscIndices[comp]];
|
|
694
|
+
currDcComp[comp] = comp < 1 ? dcOffset : currDcComp[comp - 1] + numBlocksX * numBlocksY;
|
|
695
|
+
dctData[comp] = new Float32Array(64);
|
|
696
|
+
halfZigBlock[comp] = new Uint16Array(64);
|
|
697
|
+
rowBlock[comp] = new Uint16Array(numBlocksX * 64);
|
|
698
|
+
}
|
|
699
|
+
for (let blockY = 0; blockY < numBlocksY; blockY++) {
|
|
700
|
+
let maxY = 8;
|
|
701
|
+
if (blockY === numBlocksY - 1) maxY = leftoverY;
|
|
702
|
+
let maxX = 8;
|
|
703
|
+
for (let blockX = 0; blockX < numBlocksX; blockX++) {
|
|
704
|
+
if (blockX === numBlocksX - 1) maxX = leftoverX;
|
|
705
|
+
for (let comp = 0; comp < numComp; comp++) {
|
|
706
|
+
halfZigBlock[comp].fill(0);
|
|
707
|
+
if (currDcComp[comp] >= dcBuffer.length) {
|
|
708
|
+
throw new Error("Invalid DWA DC coefficient bounds.");
|
|
709
|
+
}
|
|
710
|
+
halfZigBlock[comp][0] = dcBuffer[currDcComp[comp]++];
|
|
711
|
+
unRleAC(currAcComp, acBuffer, halfZigBlock[comp]);
|
|
712
|
+
unZigZag(halfZigBlock[comp], dctData[comp]);
|
|
713
|
+
dctInverse(dctData[comp]);
|
|
714
|
+
}
|
|
715
|
+
csc709Inverse(dctData);
|
|
716
|
+
for (let comp = 0; comp < numComp; comp++) {
|
|
717
|
+
convertToHalf(dctData[comp], rowBlock[comp], blockX * 64);
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
for (let comp = 0; comp < numComp; comp++) {
|
|
721
|
+
const channelIndex = cscIndices[comp];
|
|
722
|
+
const sampleWords = channelData[channelIndex].sampleWords;
|
|
723
|
+
for (let y = 8 * blockY; y < 8 * blockY + maxY; y++) {
|
|
724
|
+
let offsetWords = rowOffsets[comp][y] >> 1;
|
|
725
|
+
for (let blockX = 0; blockX < numFullBlocksX; blockX++) {
|
|
726
|
+
const src = blockX * 64 + (y & 7) * 8;
|
|
727
|
+
outWords[offsetWords + 0 * sampleWords] = rowBlock[comp][src + 0];
|
|
728
|
+
outWords[offsetWords + 1 * sampleWords] = rowBlock[comp][src + 1];
|
|
729
|
+
outWords[offsetWords + 2 * sampleWords] = rowBlock[comp][src + 2];
|
|
730
|
+
outWords[offsetWords + 3 * sampleWords] = rowBlock[comp][src + 3];
|
|
731
|
+
outWords[offsetWords + 4 * sampleWords] = rowBlock[comp][src + 4];
|
|
732
|
+
outWords[offsetWords + 5 * sampleWords] = rowBlock[comp][src + 5];
|
|
733
|
+
outWords[offsetWords + 6 * sampleWords] = rowBlock[comp][src + 6];
|
|
734
|
+
outWords[offsetWords + 7 * sampleWords] = rowBlock[comp][src + 7];
|
|
735
|
+
offsetWords += 8 * sampleWords;
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
if (numFullBlocksX !== numBlocksX) {
|
|
739
|
+
for (let y = 8 * blockY; y < 8 * blockY + maxY; y++) {
|
|
740
|
+
const offsetWords = (rowOffsets[comp][y] >> 1) + 8 * numFullBlocksX * sampleWords;
|
|
741
|
+
const src = numFullBlocksX * 64 + (y & 7) * 8;
|
|
742
|
+
for (let x = 0; x < maxX; x++) {
|
|
743
|
+
outWords[offsetWords + x * sampleWords] = rowBlock[comp][src + x];
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
for (let comp = 0; comp < numComp; comp++) {
|
|
750
|
+
const channelIndex = cscIndices[comp];
|
|
751
|
+
const channel = channelData[channelIndex];
|
|
752
|
+
channel.decoded = true;
|
|
753
|
+
if (channel.pixelType !== 2) {
|
|
754
|
+
continue;
|
|
755
|
+
}
|
|
756
|
+
const sampleWords = channel.sampleWords;
|
|
757
|
+
for (let y = 0; y < height; y++) {
|
|
758
|
+
const offset = rowOffsets[comp][y];
|
|
759
|
+
let srcWords = offset >> 1;
|
|
760
|
+
for (let x = 0; x < width; x++) {
|
|
761
|
+
dataView.setFloat32(
|
|
762
|
+
offset + x * INT16_SIZE * sampleWords,
|
|
763
|
+
float16ToFloat32(outWords[srcWords]),
|
|
764
|
+
true
|
|
765
|
+
);
|
|
766
|
+
srcWords += sampleWords;
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
return {
|
|
771
|
+
acConsumed: currAcComp.value - acOffset,
|
|
772
|
+
dcConsumed: currDcComp[numComp - 1] - dcOffset
|
|
773
|
+
};
|
|
774
|
+
}
|
|
775
|
+
function lossyDctChannelDecode(channelIndex, rowPtrs, channelData, acBuffer, dcBuffer, outBuffer, acOffset, dcOffset) {
|
|
776
|
+
const dataView = new DataView(outBuffer.buffer, outBuffer.byteOffset, outBuffer.byteLength);
|
|
777
|
+
const outWords = new Uint16Array(
|
|
778
|
+
outBuffer.buffer,
|
|
779
|
+
outBuffer.byteOffset,
|
|
780
|
+
outBuffer.byteLength >> 1
|
|
781
|
+
);
|
|
782
|
+
const channel = channelData[channelIndex];
|
|
783
|
+
const width = channel.width;
|
|
784
|
+
const height = channel.height;
|
|
785
|
+
const numBlocksX = Math.ceil(width / 8);
|
|
786
|
+
const numBlocksY = Math.ceil(height / 8);
|
|
787
|
+
const numFullBlocksX = Math.floor(width / 8);
|
|
788
|
+
const leftoverX = width - (numBlocksX - 1) * 8;
|
|
789
|
+
const leftoverY = height - (numBlocksY - 1) * 8;
|
|
790
|
+
const currAcComp = { value: acOffset };
|
|
791
|
+
let currDcComp = dcOffset;
|
|
792
|
+
const dctData = new Float32Array(64);
|
|
793
|
+
const halfZigBlock = new Uint16Array(64);
|
|
794
|
+
const rowBlock = new Uint16Array(numBlocksX * 64);
|
|
795
|
+
for (let blockY = 0; blockY < numBlocksY; blockY++) {
|
|
796
|
+
let maxY = 8;
|
|
797
|
+
if (blockY === numBlocksY - 1) maxY = leftoverY;
|
|
798
|
+
for (let blockX = 0; blockX < numBlocksX; blockX++) {
|
|
799
|
+
halfZigBlock.fill(0);
|
|
800
|
+
if (currDcComp >= dcBuffer.length) {
|
|
801
|
+
throw new Error("Invalid DWA DC coefficient bounds.");
|
|
802
|
+
}
|
|
803
|
+
halfZigBlock[0] = dcBuffer[currDcComp++];
|
|
804
|
+
unRleAC(currAcComp, acBuffer, halfZigBlock);
|
|
805
|
+
unZigZag(halfZigBlock, dctData);
|
|
806
|
+
dctInverse(dctData);
|
|
807
|
+
convertToHalf(dctData, rowBlock, blockX * 64);
|
|
808
|
+
}
|
|
809
|
+
for (let y = 8 * blockY; y < 8 * blockY + maxY; y++) {
|
|
810
|
+
let offsetWords = rowPtrs[channelIndex][y] >> 1;
|
|
811
|
+
for (let blockX = 0; blockX < numFullBlocksX; blockX++) {
|
|
812
|
+
const src = blockX * 64 + (y & 7) * 8;
|
|
813
|
+
for (let x = 0; x < 8; x++) {
|
|
814
|
+
outWords[offsetWords + x * channel.sampleWords] = rowBlock[src + x];
|
|
815
|
+
}
|
|
816
|
+
offsetWords += 8 * channel.sampleWords;
|
|
817
|
+
}
|
|
818
|
+
if (numBlocksX !== numFullBlocksX) {
|
|
819
|
+
const src = numFullBlocksX * 64 + (y & 7) * 8;
|
|
820
|
+
for (let x = 0; x < leftoverX; x++) {
|
|
821
|
+
outWords[offsetWords + x * channel.sampleWords] = rowBlock[src + x];
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
if (channel.pixelType === 2) {
|
|
827
|
+
for (let y = 0; y < height; y++) {
|
|
828
|
+
const offset = rowPtrs[channelIndex][y];
|
|
829
|
+
let srcWords = offset >> 1;
|
|
830
|
+
for (let x = 0; x < width; x++) {
|
|
831
|
+
dataView.setFloat32(
|
|
832
|
+
offset + x * INT16_SIZE * channel.sampleWords,
|
|
833
|
+
float16ToFloat32(outWords[srcWords]),
|
|
834
|
+
true
|
|
835
|
+
);
|
|
836
|
+
srcWords += channel.sampleWords;
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
channel.decoded = true;
|
|
841
|
+
return {
|
|
842
|
+
acConsumed: currAcComp.value - acOffset,
|
|
843
|
+
dcConsumed: currDcComp - dcOffset
|
|
844
|
+
};
|
|
845
|
+
}
|
|
846
|
+
function suffixForChannel(name) {
|
|
847
|
+
const index = name.lastIndexOf(".");
|
|
848
|
+
return index >= 0 ? name.slice(index + 1) : name;
|
|
849
|
+
}
|
|
850
|
+
function matchesRule(channelName, rule) {
|
|
851
|
+
if (rule.caseInsensitive) {
|
|
852
|
+
return channelName.localeCompare(rule.name, void 0, { sensitivity: "accent" }) === 0;
|
|
853
|
+
}
|
|
854
|
+
return channelName === rule.name;
|
|
855
|
+
}
|
|
856
|
+
function parseDwaHeader(view, cursor) {
|
|
857
|
+
return {
|
|
858
|
+
version: readInt64(view, cursor),
|
|
859
|
+
unknownUncompressedSize: readInt64(view, cursor),
|
|
860
|
+
unknownCompressedSize: readInt64(view, cursor),
|
|
861
|
+
acCompressedSize: readInt64(view, cursor),
|
|
862
|
+
dcCompressedSize: readInt64(view, cursor),
|
|
863
|
+
rleCompressedSize: readInt64(view, cursor),
|
|
864
|
+
rleUncompressedSize: readInt64(view, cursor),
|
|
865
|
+
rleRawSize: readInt64(view, cursor),
|
|
866
|
+
totalAcUncompressedCount: readInt64(view, cursor),
|
|
867
|
+
totalDcUncompressedCount: readInt64(view, cursor),
|
|
868
|
+
acCompression: readInt64(view, cursor)
|
|
869
|
+
};
|
|
870
|
+
}
|
|
871
|
+
function parseDwaRules(bytes, view, cursor) {
|
|
872
|
+
const rulesSize = readUint16(view, cursor);
|
|
873
|
+
if (rulesSize < 2) {
|
|
874
|
+
throw new Error("Invalid DWA channel rules payload size.");
|
|
875
|
+
}
|
|
876
|
+
const rulesEnd = cursor.value + (rulesSize - 2);
|
|
877
|
+
if (rulesEnd > bytes.length) {
|
|
878
|
+
throw new Error("Truncated DWA channel rules payload.");
|
|
879
|
+
}
|
|
880
|
+
const rules = [];
|
|
881
|
+
while (cursor.value < rulesEnd) {
|
|
882
|
+
const name = readCStringWithin(bytes, cursor, rulesEnd, "DWA channel rule name");
|
|
883
|
+
const value = readUint8(view, cursor);
|
|
884
|
+
const compression = value >> 2 & 3;
|
|
885
|
+
const index = (value >> 4) - 1;
|
|
886
|
+
const caseInsensitive = (value & 1) !== 0;
|
|
887
|
+
const pixelType = readUint8(view, cursor);
|
|
888
|
+
rules.push({
|
|
889
|
+
name,
|
|
890
|
+
compression,
|
|
891
|
+
index,
|
|
892
|
+
pixelType,
|
|
893
|
+
caseInsensitive
|
|
894
|
+
});
|
|
895
|
+
}
|
|
896
|
+
if (cursor.value !== rulesEnd) {
|
|
897
|
+
throw new Error("Invalid DWA channel rules alignment.");
|
|
898
|
+
}
|
|
899
|
+
return rules;
|
|
900
|
+
}
|
|
901
|
+
function decodeDwaChunk(context) {
|
|
902
|
+
if (!context.part.dataWindow) {
|
|
903
|
+
throw new Error("Missing DWA part data window.");
|
|
904
|
+
}
|
|
905
|
+
if (context.linesInChunk <= 0) {
|
|
906
|
+
return new Uint8Array(0);
|
|
907
|
+
}
|
|
908
|
+
const xMin = context.part.dataWindow.xMin;
|
|
909
|
+
const xMax = context.part.dataWindow.xMax;
|
|
910
|
+
const columns = xMax - xMin + 1;
|
|
911
|
+
const lines = context.linesInChunk;
|
|
912
|
+
for (const channel of context.part.channels) {
|
|
913
|
+
const xSampling = channel.xSampling > 0 ? channel.xSampling : 1;
|
|
914
|
+
const ySampling = channel.ySampling > 0 ? channel.ySampling : 1;
|
|
915
|
+
if (xSampling !== 1 || ySampling !== 1) {
|
|
916
|
+
throw new Error(
|
|
917
|
+
"DWAA/DWAB decoding currently requires xSampling=1 and ySampling=1 for all channels."
|
|
918
|
+
);
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
const bytes = new Uint8Array(context.buffer, context.dataPtr, context.dataSize);
|
|
922
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
923
|
+
const cursor = { value: 0 };
|
|
924
|
+
const header = parseDwaHeader(view, cursor);
|
|
925
|
+
if (header.version < 1 || header.version > 2) {
|
|
926
|
+
throw new Error(`Unsupported DWA version ${header.version}.`);
|
|
927
|
+
}
|
|
928
|
+
const rules = header.version >= 2 ? parseDwaRules(bytes, view, cursor) : LEGACY_DWA_RULES;
|
|
929
|
+
if (header.unknownCompressedSize > 0 || header.unknownUncompressedSize > 0) {
|
|
930
|
+
throw new Error("DWAA/DWAB UNKNOWN channel blocks are not supported.");
|
|
931
|
+
}
|
|
932
|
+
const channelData = context.part.channels.map((channel) => ({
|
|
933
|
+
name: channel.name,
|
|
934
|
+
compression: UNKNOWN,
|
|
935
|
+
decoded: false,
|
|
936
|
+
pixelType: channel.pixelType,
|
|
937
|
+
sampleWords: channel.pixelType === 1 ? 1 : 2,
|
|
938
|
+
pLinear: channel.pLinear,
|
|
939
|
+
width: columns,
|
|
940
|
+
height: lines
|
|
941
|
+
}));
|
|
942
|
+
const cscSet = { idx: new Array(3) };
|
|
943
|
+
for (let offset = 0; offset < channelData.length; offset++) {
|
|
944
|
+
const channel = channelData[offset];
|
|
945
|
+
const suffix = suffixForChannel(channel.name);
|
|
946
|
+
for (const rule of rules) {
|
|
947
|
+
if (rule.pixelType !== channel.pixelType) continue;
|
|
948
|
+
if (!matchesRule(suffix, rule)) continue;
|
|
949
|
+
channel.compression = rule.compression;
|
|
950
|
+
if (rule.index >= 0) {
|
|
951
|
+
cscSet.idx[rule.index] = offset;
|
|
952
|
+
}
|
|
953
|
+
break;
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
for (const channel of channelData) {
|
|
957
|
+
if (channel.compression !== LOSSY_DCT && channel.compression !== RLE) {
|
|
958
|
+
throw new Error(`Unsupported DWA channel compression for channel ${channel.name}.`);
|
|
959
|
+
}
|
|
960
|
+
if (channel.compression === LOSSY_DCT && channel.pixelType !== 1 && channel.pixelType !== 2) {
|
|
961
|
+
throw new Error(`Unsupported DWA lossy channel pixel type for channel ${channel.name}.`);
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
let acBuffer = new Uint16Array(0);
|
|
965
|
+
let dcBuffer = new Uint16Array(0);
|
|
966
|
+
let rleBuffer = new Uint8Array(0);
|
|
967
|
+
if (header.acCompressedSize > 0) {
|
|
968
|
+
if (cursor.value + header.acCompressedSize > bytes.length) {
|
|
969
|
+
throw new Error("Truncated DWA AC payload.");
|
|
970
|
+
}
|
|
971
|
+
switch (header.acCompression) {
|
|
972
|
+
case STATIC_HUFFMAN: {
|
|
973
|
+
acBuffer = new Uint16Array(header.totalAcUncompressedCount);
|
|
974
|
+
const acStart = cursor.value;
|
|
975
|
+
hufUncompress(
|
|
976
|
+
bytes,
|
|
977
|
+
view,
|
|
978
|
+
cursor,
|
|
979
|
+
header.acCompressedSize,
|
|
980
|
+
acBuffer,
|
|
981
|
+
header.totalAcUncompressedCount
|
|
982
|
+
);
|
|
983
|
+
cursor.value = acStart + header.acCompressedSize;
|
|
984
|
+
break;
|
|
985
|
+
}
|
|
986
|
+
case DEFLATE: {
|
|
987
|
+
const compressed = bytes.subarray(cursor.value, cursor.value + header.acCompressedSize);
|
|
988
|
+
const decoded = unzlibSync(compressed, {
|
|
989
|
+
out: new Uint8Array(header.totalAcUncompressedCount * INT16_SIZE)
|
|
990
|
+
});
|
|
991
|
+
if (decoded.byteLength % 2 !== 0) {
|
|
992
|
+
throw new Error("Invalid DWA AC DEFLATE payload size.");
|
|
993
|
+
}
|
|
994
|
+
const decodedWords = decoded.byteLength / 2;
|
|
995
|
+
if (decodedWords !== header.totalAcUncompressedCount) {
|
|
996
|
+
throw new Error("Invalid DWA AC DEFLATE payload length.");
|
|
997
|
+
}
|
|
998
|
+
const decodedView = new Uint16Array(decoded.buffer, decoded.byteOffset, decodedWords);
|
|
999
|
+
acBuffer = new Uint16Array(decodedWords);
|
|
1000
|
+
acBuffer.set(decodedView);
|
|
1001
|
+
cursor.value += header.acCompressedSize;
|
|
1002
|
+
break;
|
|
1003
|
+
}
|
|
1004
|
+
default:
|
|
1005
|
+
throw new Error(`Unsupported DWA AC compression mode ${header.acCompression}.`);
|
|
1006
|
+
}
|
|
1007
|
+
} else if (header.totalAcUncompressedCount !== 0) {
|
|
1008
|
+
throw new Error("Invalid DWA AC payload metadata.");
|
|
1009
|
+
}
|
|
1010
|
+
if (header.dcCompressedSize > 0) {
|
|
1011
|
+
const dcBytes = uncompressZipStyle(
|
|
1012
|
+
bytes,
|
|
1013
|
+
cursor.value,
|
|
1014
|
+
header.dcCompressedSize,
|
|
1015
|
+
header.totalDcUncompressedCount * INT16_SIZE,
|
|
1016
|
+
DWA_DC_SCRATCH
|
|
1017
|
+
);
|
|
1018
|
+
if (dcBytes.byteLength % 2 !== 0) {
|
|
1019
|
+
throw new Error("Invalid DWA DC payload size.");
|
|
1020
|
+
}
|
|
1021
|
+
const dcWords = dcBytes.byteLength / 2;
|
|
1022
|
+
if (dcWords !== header.totalDcUncompressedCount) {
|
|
1023
|
+
throw new Error("Invalid DWA DC payload length.");
|
|
1024
|
+
}
|
|
1025
|
+
const dcView = new Uint16Array(dcBytes.buffer, dcBytes.byteOffset, dcWords);
|
|
1026
|
+
dcBuffer = new Uint16Array(dcWords);
|
|
1027
|
+
dcBuffer.set(dcView);
|
|
1028
|
+
cursor.value += header.dcCompressedSize;
|
|
1029
|
+
} else if (header.totalDcUncompressedCount !== 0) {
|
|
1030
|
+
throw new Error("Invalid DWA DC payload metadata.");
|
|
1031
|
+
}
|
|
1032
|
+
if (header.rleRawSize > 0) {
|
|
1033
|
+
if (cursor.value + header.rleCompressedSize > bytes.length) {
|
|
1034
|
+
throw new Error("Truncated DWA RLE payload.");
|
|
1035
|
+
}
|
|
1036
|
+
const compressed = bytes.subarray(cursor.value, cursor.value + header.rleCompressedSize);
|
|
1037
|
+
const decoded = unzlibSync(compressed, {
|
|
1038
|
+
out: new Uint8Array(header.rleUncompressedSize)
|
|
1039
|
+
});
|
|
1040
|
+
if (decoded.byteLength !== header.rleUncompressedSize) {
|
|
1041
|
+
throw new Error("Invalid DWA RLE intermediate payload length.");
|
|
1042
|
+
}
|
|
1043
|
+
const decodedRle = decodeRunLength(decoded, header.rleRawSize);
|
|
1044
|
+
rleBuffer = new Uint8Array(decodedRle.byteLength);
|
|
1045
|
+
rleBuffer.set(decodedRle);
|
|
1046
|
+
cursor.value += header.rleCompressedSize;
|
|
1047
|
+
} else if (header.rleCompressedSize !== 0 || header.rleUncompressedSize !== 0) {
|
|
1048
|
+
throw new Error("Invalid DWA RLE payload metadata.");
|
|
1049
|
+
}
|
|
1050
|
+
let outputSize = 0;
|
|
1051
|
+
const rowOffsets = new Array(channelData.length);
|
|
1052
|
+
for (let i = 0; i < channelData.length; i++) {
|
|
1053
|
+
rowOffsets[i] = new Int32Array(lines);
|
|
1054
|
+
}
|
|
1055
|
+
for (let y = 0; y < lines; y++) {
|
|
1056
|
+
for (let chan = 0; chan < channelData.length; chan++) {
|
|
1057
|
+
rowOffsets[chan][y] = outputSize;
|
|
1058
|
+
outputSize += channelData[chan].width * channelData[chan].sampleWords * INT16_SIZE;
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
1061
|
+
const outBuffer = new Uint8Array(outputSize);
|
|
1062
|
+
let acOffset = 0;
|
|
1063
|
+
let dcOffset = 0;
|
|
1064
|
+
if (cscSet.idx[0] !== void 0 && cscSet.idx[1] !== void 0 && cscSet.idx[2] !== void 0) {
|
|
1065
|
+
const usage = lossyDctDecode(
|
|
1066
|
+
cscSet,
|
|
1067
|
+
rowOffsets,
|
|
1068
|
+
channelData,
|
|
1069
|
+
acBuffer,
|
|
1070
|
+
dcBuffer,
|
|
1071
|
+
outBuffer,
|
|
1072
|
+
acOffset,
|
|
1073
|
+
dcOffset
|
|
1074
|
+
);
|
|
1075
|
+
acOffset += usage.acConsumed;
|
|
1076
|
+
dcOffset += usage.dcConsumed;
|
|
1077
|
+
}
|
|
1078
|
+
let rleOffset = 0;
|
|
1079
|
+
for (let i = 0; i < channelData.length; i++) {
|
|
1080
|
+
const channel = channelData[i];
|
|
1081
|
+
if (channel.decoded) continue;
|
|
1082
|
+
switch (channel.compression) {
|
|
1083
|
+
case RLE: {
|
|
1084
|
+
for (let y = 0; y < lines; y++) {
|
|
1085
|
+
let rowOffsetBytes = rowOffsets[i][y];
|
|
1086
|
+
for (let x = 0; x < channel.width; x++) {
|
|
1087
|
+
for (let byte = 0; byte < channel.sampleWords * INT16_SIZE; byte++) {
|
|
1088
|
+
const src = rleOffset + byte * channel.width * channel.height;
|
|
1089
|
+
if (src < 0 || src >= rleBuffer.length) {
|
|
1090
|
+
throw new Error("Invalid DWA RLE planar bounds.");
|
|
1091
|
+
}
|
|
1092
|
+
outBuffer[rowOffsetBytes++] = rleBuffer[src];
|
|
1093
|
+
}
|
|
1094
|
+
rleOffset++;
|
|
1095
|
+
}
|
|
1096
|
+
}
|
|
1097
|
+
channel.decoded = true;
|
|
1098
|
+
break;
|
|
1099
|
+
}
|
|
1100
|
+
case LOSSY_DCT: {
|
|
1101
|
+
const usage = lossyDctChannelDecode(
|
|
1102
|
+
i,
|
|
1103
|
+
rowOffsets,
|
|
1104
|
+
channelData,
|
|
1105
|
+
acBuffer,
|
|
1106
|
+
dcBuffer,
|
|
1107
|
+
outBuffer,
|
|
1108
|
+
acOffset,
|
|
1109
|
+
dcOffset
|
|
1110
|
+
);
|
|
1111
|
+
acOffset += usage.acConsumed;
|
|
1112
|
+
dcOffset += usage.dcConsumed;
|
|
1113
|
+
break;
|
|
1114
|
+
}
|
|
1115
|
+
default:
|
|
1116
|
+
throw new Error(`Unsupported DWA channel compression for channel ${channel.name}.`);
|
|
1117
|
+
}
|
|
1118
|
+
}
|
|
1119
|
+
return outBuffer;
|
|
1120
|
+
}
|
|
1121
|
+
function decodeDwaBlock(context) {
|
|
1122
|
+
try {
|
|
1123
|
+
return decodeDwaChunk(context);
|
|
1124
|
+
} catch (error) {
|
|
1125
|
+
if (error instanceof ExrError) {
|
|
1126
|
+
throw error;
|
|
1127
|
+
}
|
|
1128
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
1129
|
+
throw new ExrError("DECOMPRESSION_FAILED", "Failed to decompress DWAA/DWAB chunk.", {
|
|
1130
|
+
partId: context.partId,
|
|
1131
|
+
chunkIndex: context.chunkIndex,
|
|
1132
|
+
size: context.dataSize,
|
|
1133
|
+
reason
|
|
1134
|
+
});
|
|
1135
|
+
}
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
// src/browser/dwaDecodeWorker.ts
|
|
1139
|
+
var scope = self;
|
|
1140
|
+
var part = null;
|
|
1141
|
+
var partId = 0;
|
|
1142
|
+
function toTransferBuffer(view) {
|
|
1143
|
+
const output = new Uint8Array(view.byteLength);
|
|
1144
|
+
output.set(view);
|
|
1145
|
+
return output.buffer;
|
|
1146
|
+
}
|
|
1147
|
+
scope.onmessage = (event) => {
|
|
1148
|
+
const message = event.data;
|
|
1149
|
+
if (message.type === "init") {
|
|
1150
|
+
part = message.part;
|
|
1151
|
+
partId = message.partId;
|
|
1152
|
+
return;
|
|
1153
|
+
}
|
|
1154
|
+
try {
|
|
1155
|
+
if (!part) {
|
|
1156
|
+
throw new Error("DWA worker is not initialized.");
|
|
1157
|
+
}
|
|
1158
|
+
const decoded = decodeDwaBlock({
|
|
1159
|
+
buffer: message.compressed,
|
|
1160
|
+
dataPtr: 0,
|
|
1161
|
+
dataSize: message.compressed.byteLength,
|
|
1162
|
+
part,
|
|
1163
|
+
partId,
|
|
1164
|
+
chunkIndex: message.chunkIndex,
|
|
1165
|
+
chunkY: message.chunkY,
|
|
1166
|
+
linesInChunk: message.linesInChunk
|
|
1167
|
+
});
|
|
1168
|
+
const transfer = toTransferBuffer(decoded);
|
|
1169
|
+
const response = {
|
|
1170
|
+
id: message.id,
|
|
1171
|
+
ok: true,
|
|
1172
|
+
decoded: transfer
|
|
1173
|
+
};
|
|
1174
|
+
scope.postMessage(response, [transfer]);
|
|
1175
|
+
} catch (error) {
|
|
1176
|
+
const response = {
|
|
1177
|
+
id: message.id,
|
|
1178
|
+
ok: false,
|
|
1179
|
+
error: error instanceof Error ? error.message : String(error)
|
|
1180
|
+
};
|
|
1181
|
+
scope.postMessage(response);
|
|
1182
|
+
}
|
|
1183
|
+
};
|
|
1184
|
+
//# sourceMappingURL=dwaDecodeWorker.js.map
|