@hiroosy/baileys-lite 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +25 -0
- package/WAProto/index.d.ts +15932 -0
- package/WAProto/index.js +115856 -0
- package/engine-requirements.js +11 -0
- package/package.json +27 -0
- package/src/binary/wa-binary-constants.js +1300 -0
- package/src/binary/wa-binary.js +649 -0
- package/src/constants.js +143 -0
- package/src/defaults.js +102 -0
- package/src/foundation/audio-duration.js +530 -0
- package/src/foundation/audio-samples.js +268 -0
- package/src/foundation/boom.js +46 -0
- package/src/foundation/cache.js +142 -0
- package/src/foundation/concurrency.js +87 -0
- package/src/foundation/curve25519.js +221 -0
- package/src/foundation/link-preview.js +320 -0
- package/src/foundation/logger.js +69 -0
- package/src/foundation/long.js +140 -0
- package/src/foundation/protobuf-minimal.js +619 -0
- package/src/foundation/wa-crypto.js +76 -0
- package/src/foundation/zip.js +124 -0
- package/src/index.js +48 -0
- package/src/signal/libsignal.js +1051 -0
- package/src/signal/signal-group.js +487 -0
- package/src/signal/signal-repository.js +646 -0
- package/src/socket/business-communities.js +830 -0
- package/src/socket/chats.js +674 -0
- package/src/socket/groups-newsletter.js +342 -0
- package/src/socket/messages-recv.js +1835 -0
- package/src/socket/messages-send.js +1202 -0
- package/src/socket/socket-core.js +833 -0
- package/src/socket/usync.js +291 -0
- package/src/socket-client/websocket-client.js +124 -0
- package/src/utils/auth-state-core.js +764 -0
- package/src/utils/auth-state-storage.js +316 -0
- package/src/utils/chat-sync.js +1045 -0
- package/src/utils/media.js +841 -0
- package/src/utils/message-compose.js +1609 -0
- package/src/utils/message-processing.js +958 -0
- package/src/utils/wa-protocol-core.js +1283 -0
- package/src/vendor/jpeg-js/LICENSE +25 -0
- package/src/vendor/jpeg-js/decoder.js +1152 -0
- package/src/vendor/jpeg-js/encoder.js +824 -0
- package/src/vendor/pngjs/LICENSE +20 -0
- package/src/vendor/pngjs/bitmapper.js +273 -0
- package/src/vendor/pngjs/bitpacker.js +167 -0
- package/src/vendor/pngjs/constants.js +38 -0
- package/src/vendor/pngjs/crc.js +47 -0
- package/src/vendor/pngjs/filter-pack.js +179 -0
- package/src/vendor/pngjs/filter-parse-sync.js +27 -0
- package/src/vendor/pngjs/filter-parse.js +185 -0
- package/src/vendor/pngjs/format-normaliser.js +101 -0
- package/src/vendor/pngjs/interlace.js +101 -0
- package/src/vendor/pngjs/packer-sync.js +65 -0
- package/src/vendor/pngjs/packer.js +138 -0
- package/src/vendor/pngjs/paeth-predictor.js +22 -0
- package/src/vendor/pngjs/parser-sync.js +121 -0
- package/src/vendor/pngjs/parser.js +299 -0
- package/src/vendor/pngjs/png-sync.js +19 -0
- package/src/vendor/pngjs/sync-inflate.js +174 -0
- package/src/vendor/pngjs/sync-reader.js +54 -0
- package/src/wam/wam-constants.js +22853 -0
- package/src/wam/wam.js +151 -0
|
@@ -0,0 +1,1152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure-JS JPEG decoder — vendored verbatim from `jpeg-js` 0.4.4 (BSD-3-Clause, Copyright 2011 notmasteryet),
|
|
3
|
+
* only the CommonJS export at the bottom was converted to `export default`. See ./LICENSE.
|
|
4
|
+
* Algorithm body is untouched (checked in against upstream md5 03d367b0053de962384f06f9007e23b9).
|
|
5
|
+
* https://github.com/eugeneware/jpeg-js
|
|
6
|
+
*/
|
|
7
|
+
/* -*- tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
|
|
8
|
+
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
|
|
9
|
+
/*
|
|
10
|
+
Copyright 2011 notmasteryet
|
|
11
|
+
|
|
12
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
13
|
+
you may not use this file except in compliance with the License.
|
|
14
|
+
You may obtain a copy of the License at
|
|
15
|
+
|
|
16
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
17
|
+
|
|
18
|
+
Unless required by applicable law or agreed to in writing, software
|
|
19
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
20
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
21
|
+
See the License for the specific language governing permissions and
|
|
22
|
+
limitations under the License.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
// - The JPEG specification can be found in the ITU CCITT Recommendation T.81
|
|
26
|
+
// (www.w3.org/Graphics/JPEG/itu-t81.pdf)
|
|
27
|
+
// - The JFIF specification can be found in the JPEG File Interchange Format
|
|
28
|
+
// (www.w3.org/Graphics/JPEG/jfif3.pdf)
|
|
29
|
+
// - The Adobe Application-Specific JPEG markers in the Supporting the DCT Filters
|
|
30
|
+
// in PostScript Level 2, Technical Note #5116
|
|
31
|
+
// (partners.adobe.com/public/developer/en/ps/sdk/5116.DCT_Filter.pdf)
|
|
32
|
+
|
|
33
|
+
var JpegImage = (function jpegImage() {
|
|
34
|
+
"use strict";
|
|
35
|
+
var dctZigZag = new Int32Array([
|
|
36
|
+
0,
|
|
37
|
+
1, 8,
|
|
38
|
+
16, 9, 2,
|
|
39
|
+
3, 10, 17, 24,
|
|
40
|
+
32, 25, 18, 11, 4,
|
|
41
|
+
5, 12, 19, 26, 33, 40,
|
|
42
|
+
48, 41, 34, 27, 20, 13, 6,
|
|
43
|
+
7, 14, 21, 28, 35, 42, 49, 56,
|
|
44
|
+
57, 50, 43, 36, 29, 22, 15,
|
|
45
|
+
23, 30, 37, 44, 51, 58,
|
|
46
|
+
59, 52, 45, 38, 31,
|
|
47
|
+
39, 46, 53, 60,
|
|
48
|
+
61, 54, 47,
|
|
49
|
+
55, 62,
|
|
50
|
+
63
|
|
51
|
+
]);
|
|
52
|
+
|
|
53
|
+
var dctCos1 = 4017 // cos(pi/16)
|
|
54
|
+
var dctSin1 = 799 // sin(pi/16)
|
|
55
|
+
var dctCos3 = 3406 // cos(3*pi/16)
|
|
56
|
+
var dctSin3 = 2276 // sin(3*pi/16)
|
|
57
|
+
var dctCos6 = 1567 // cos(6*pi/16)
|
|
58
|
+
var dctSin6 = 3784 // sin(6*pi/16)
|
|
59
|
+
var dctSqrt2 = 5793 // sqrt(2)
|
|
60
|
+
var dctSqrt1d2 = 2896 // sqrt(2) / 2
|
|
61
|
+
|
|
62
|
+
function constructor() {
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function buildHuffmanTable(codeLengths, values) {
|
|
66
|
+
var k = 0, code = [], i, j, length = 16;
|
|
67
|
+
while (length > 0 && !codeLengths[length - 1])
|
|
68
|
+
length--;
|
|
69
|
+
code.push({children: [], index: 0});
|
|
70
|
+
var p = code[0], q;
|
|
71
|
+
for (i = 0; i < length; i++) {
|
|
72
|
+
for (j = 0; j < codeLengths[i]; j++) {
|
|
73
|
+
p = code.pop();
|
|
74
|
+
p.children[p.index] = values[k];
|
|
75
|
+
while (p.index > 0) {
|
|
76
|
+
if (code.length === 0)
|
|
77
|
+
throw new Error('Could not recreate Huffman Table');
|
|
78
|
+
p = code.pop();
|
|
79
|
+
}
|
|
80
|
+
p.index++;
|
|
81
|
+
code.push(p);
|
|
82
|
+
while (code.length <= i) {
|
|
83
|
+
code.push(q = {children: [], index: 0});
|
|
84
|
+
p.children[p.index] = q.children;
|
|
85
|
+
p = q;
|
|
86
|
+
}
|
|
87
|
+
k++;
|
|
88
|
+
}
|
|
89
|
+
if (i + 1 < length) {
|
|
90
|
+
// p here points to last code
|
|
91
|
+
code.push(q = {children: [], index: 0});
|
|
92
|
+
p.children[p.index] = q.children;
|
|
93
|
+
p = q;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return code[0].children;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function decodeScan(data, offset,
|
|
100
|
+
frame, components, resetInterval,
|
|
101
|
+
spectralStart, spectralEnd,
|
|
102
|
+
successivePrev, successive, opts) {
|
|
103
|
+
var precision = frame.precision;
|
|
104
|
+
var samplesPerLine = frame.samplesPerLine;
|
|
105
|
+
var scanLines = frame.scanLines;
|
|
106
|
+
var mcusPerLine = frame.mcusPerLine;
|
|
107
|
+
var progressive = frame.progressive;
|
|
108
|
+
var maxH = frame.maxH, maxV = frame.maxV;
|
|
109
|
+
|
|
110
|
+
var startOffset = offset, bitsData = 0, bitsCount = 0;
|
|
111
|
+
function readBit() {
|
|
112
|
+
if (bitsCount > 0) {
|
|
113
|
+
bitsCount--;
|
|
114
|
+
return (bitsData >> bitsCount) & 1;
|
|
115
|
+
}
|
|
116
|
+
bitsData = data[offset++];
|
|
117
|
+
if (bitsData == 0xFF) {
|
|
118
|
+
var nextByte = data[offset++];
|
|
119
|
+
if (nextByte) {
|
|
120
|
+
throw new Error("unexpected marker: " + ((bitsData << 8) | nextByte).toString(16));
|
|
121
|
+
}
|
|
122
|
+
// unstuff 0
|
|
123
|
+
}
|
|
124
|
+
bitsCount = 7;
|
|
125
|
+
return bitsData >>> 7;
|
|
126
|
+
}
|
|
127
|
+
function decodeHuffman(tree) {
|
|
128
|
+
var node = tree, bit;
|
|
129
|
+
while ((bit = readBit()) !== null) {
|
|
130
|
+
node = node[bit];
|
|
131
|
+
if (typeof node === 'number')
|
|
132
|
+
return node;
|
|
133
|
+
if (typeof node !== 'object')
|
|
134
|
+
throw new Error("invalid huffman sequence");
|
|
135
|
+
}
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
function receive(length) {
|
|
139
|
+
var n = 0;
|
|
140
|
+
while (length > 0) {
|
|
141
|
+
var bit = readBit();
|
|
142
|
+
if (bit === null) return;
|
|
143
|
+
n = (n << 1) | bit;
|
|
144
|
+
length--;
|
|
145
|
+
}
|
|
146
|
+
return n;
|
|
147
|
+
}
|
|
148
|
+
function receiveAndExtend(length) {
|
|
149
|
+
var n = receive(length);
|
|
150
|
+
if (n >= 1 << (length - 1))
|
|
151
|
+
return n;
|
|
152
|
+
return n + (-1 << length) + 1;
|
|
153
|
+
}
|
|
154
|
+
function decodeBaseline(component, zz) {
|
|
155
|
+
var t = decodeHuffman(component.huffmanTableDC);
|
|
156
|
+
var diff = t === 0 ? 0 : receiveAndExtend(t);
|
|
157
|
+
zz[0]= (component.pred += diff);
|
|
158
|
+
var k = 1;
|
|
159
|
+
while (k < 64) {
|
|
160
|
+
var rs = decodeHuffman(component.huffmanTableAC);
|
|
161
|
+
var s = rs & 15, r = rs >> 4;
|
|
162
|
+
if (s === 0) {
|
|
163
|
+
if (r < 15)
|
|
164
|
+
break;
|
|
165
|
+
k += 16;
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
k += r;
|
|
169
|
+
var z = dctZigZag[k];
|
|
170
|
+
zz[z] = receiveAndExtend(s);
|
|
171
|
+
k++;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
function decodeDCFirst(component, zz) {
|
|
175
|
+
var t = decodeHuffman(component.huffmanTableDC);
|
|
176
|
+
var diff = t === 0 ? 0 : (receiveAndExtend(t) << successive);
|
|
177
|
+
zz[0] = (component.pred += diff);
|
|
178
|
+
}
|
|
179
|
+
function decodeDCSuccessive(component, zz) {
|
|
180
|
+
zz[0] |= readBit() << successive;
|
|
181
|
+
}
|
|
182
|
+
var eobrun = 0;
|
|
183
|
+
function decodeACFirst(component, zz) {
|
|
184
|
+
if (eobrun > 0) {
|
|
185
|
+
eobrun--;
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
var k = spectralStart, e = spectralEnd;
|
|
189
|
+
while (k <= e) {
|
|
190
|
+
var rs = decodeHuffman(component.huffmanTableAC);
|
|
191
|
+
var s = rs & 15, r = rs >> 4;
|
|
192
|
+
if (s === 0) {
|
|
193
|
+
if (r < 15) {
|
|
194
|
+
eobrun = receive(r) + (1 << r) - 1;
|
|
195
|
+
break;
|
|
196
|
+
}
|
|
197
|
+
k += 16;
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
k += r;
|
|
201
|
+
var z = dctZigZag[k];
|
|
202
|
+
zz[z] = receiveAndExtend(s) * (1 << successive);
|
|
203
|
+
k++;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
var successiveACState = 0, successiveACNextValue;
|
|
207
|
+
function decodeACSuccessive(component, zz) {
|
|
208
|
+
var k = spectralStart, e = spectralEnd, r = 0;
|
|
209
|
+
while (k <= e) {
|
|
210
|
+
var z = dctZigZag[k];
|
|
211
|
+
var direction = zz[z] < 0 ? -1 : 1;
|
|
212
|
+
switch (successiveACState) {
|
|
213
|
+
case 0: // initial state
|
|
214
|
+
var rs = decodeHuffman(component.huffmanTableAC);
|
|
215
|
+
var s = rs & 15, r = rs >> 4;
|
|
216
|
+
if (s === 0) {
|
|
217
|
+
if (r < 15) {
|
|
218
|
+
eobrun = receive(r) + (1 << r);
|
|
219
|
+
successiveACState = 4;
|
|
220
|
+
} else {
|
|
221
|
+
r = 16;
|
|
222
|
+
successiveACState = 1;
|
|
223
|
+
}
|
|
224
|
+
} else {
|
|
225
|
+
if (s !== 1)
|
|
226
|
+
throw new Error("invalid ACn encoding");
|
|
227
|
+
successiveACNextValue = receiveAndExtend(s);
|
|
228
|
+
successiveACState = r ? 2 : 3;
|
|
229
|
+
}
|
|
230
|
+
continue;
|
|
231
|
+
case 1: // skipping r zero items
|
|
232
|
+
case 2:
|
|
233
|
+
if (zz[z])
|
|
234
|
+
zz[z] += (readBit() << successive) * direction;
|
|
235
|
+
else {
|
|
236
|
+
r--;
|
|
237
|
+
if (r === 0)
|
|
238
|
+
successiveACState = successiveACState == 2 ? 3 : 0;
|
|
239
|
+
}
|
|
240
|
+
break;
|
|
241
|
+
case 3: // set value for a zero item
|
|
242
|
+
if (zz[z])
|
|
243
|
+
zz[z] += (readBit() << successive) * direction;
|
|
244
|
+
else {
|
|
245
|
+
zz[z] = successiveACNextValue << successive;
|
|
246
|
+
successiveACState = 0;
|
|
247
|
+
}
|
|
248
|
+
break;
|
|
249
|
+
case 4: // eob
|
|
250
|
+
if (zz[z])
|
|
251
|
+
zz[z] += (readBit() << successive) * direction;
|
|
252
|
+
break;
|
|
253
|
+
}
|
|
254
|
+
k++;
|
|
255
|
+
}
|
|
256
|
+
if (successiveACState === 4) {
|
|
257
|
+
eobrun--;
|
|
258
|
+
if (eobrun === 0)
|
|
259
|
+
successiveACState = 0;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
function decodeMcu(component, decode, mcu, row, col) {
|
|
263
|
+
var mcuRow = (mcu / mcusPerLine) | 0;
|
|
264
|
+
var mcuCol = mcu % mcusPerLine;
|
|
265
|
+
var blockRow = mcuRow * component.v + row;
|
|
266
|
+
var blockCol = mcuCol * component.h + col;
|
|
267
|
+
// If the block is missing and we're in tolerant mode, just skip it.
|
|
268
|
+
if (component.blocks[blockRow] === undefined && opts.tolerantDecoding)
|
|
269
|
+
return;
|
|
270
|
+
decode(component, component.blocks[blockRow][blockCol]);
|
|
271
|
+
}
|
|
272
|
+
function decodeBlock(component, decode, mcu) {
|
|
273
|
+
var blockRow = (mcu / component.blocksPerLine) | 0;
|
|
274
|
+
var blockCol = mcu % component.blocksPerLine;
|
|
275
|
+
// If the block is missing and we're in tolerant mode, just skip it.
|
|
276
|
+
if (component.blocks[blockRow] === undefined && opts.tolerantDecoding)
|
|
277
|
+
return;
|
|
278
|
+
decode(component, component.blocks[blockRow][blockCol]);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
var componentsLength = components.length;
|
|
282
|
+
var component, i, j, k, n;
|
|
283
|
+
var decodeFn;
|
|
284
|
+
if (progressive) {
|
|
285
|
+
if (spectralStart === 0)
|
|
286
|
+
decodeFn = successivePrev === 0 ? decodeDCFirst : decodeDCSuccessive;
|
|
287
|
+
else
|
|
288
|
+
decodeFn = successivePrev === 0 ? decodeACFirst : decodeACSuccessive;
|
|
289
|
+
} else {
|
|
290
|
+
decodeFn = decodeBaseline;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
var mcu = 0, marker;
|
|
294
|
+
var mcuExpected;
|
|
295
|
+
if (componentsLength == 1) {
|
|
296
|
+
mcuExpected = components[0].blocksPerLine * components[0].blocksPerColumn;
|
|
297
|
+
} else {
|
|
298
|
+
mcuExpected = mcusPerLine * frame.mcusPerColumn;
|
|
299
|
+
}
|
|
300
|
+
if (!resetInterval) resetInterval = mcuExpected;
|
|
301
|
+
|
|
302
|
+
var h, v;
|
|
303
|
+
while (mcu < mcuExpected) {
|
|
304
|
+
// reset interval stuff
|
|
305
|
+
for (i = 0; i < componentsLength; i++)
|
|
306
|
+
components[i].pred = 0;
|
|
307
|
+
eobrun = 0;
|
|
308
|
+
|
|
309
|
+
if (componentsLength == 1) {
|
|
310
|
+
component = components[0];
|
|
311
|
+
for (n = 0; n < resetInterval; n++) {
|
|
312
|
+
decodeBlock(component, decodeFn, mcu);
|
|
313
|
+
mcu++;
|
|
314
|
+
}
|
|
315
|
+
} else {
|
|
316
|
+
for (n = 0; n < resetInterval; n++) {
|
|
317
|
+
for (i = 0; i < componentsLength; i++) {
|
|
318
|
+
component = components[i];
|
|
319
|
+
h = component.h;
|
|
320
|
+
v = component.v;
|
|
321
|
+
for (j = 0; j < v; j++) {
|
|
322
|
+
for (k = 0; k < h; k++) {
|
|
323
|
+
decodeMcu(component, decodeFn, mcu, j, k);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
mcu++;
|
|
328
|
+
|
|
329
|
+
// If we've reached our expected MCU's, stop decoding
|
|
330
|
+
if (mcu === mcuExpected) break;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
if (mcu === mcuExpected) {
|
|
335
|
+
// Skip trailing bytes at the end of the scan - until we reach the next marker
|
|
336
|
+
do {
|
|
337
|
+
if (data[offset] === 0xFF) {
|
|
338
|
+
if (data[offset + 1] !== 0x00) {
|
|
339
|
+
break;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
offset += 1;
|
|
343
|
+
} while (offset < data.length - 2);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
// find marker
|
|
347
|
+
bitsCount = 0;
|
|
348
|
+
marker = (data[offset] << 8) | data[offset + 1];
|
|
349
|
+
if (marker < 0xFF00) {
|
|
350
|
+
throw new Error("marker was not found");
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
if (marker >= 0xFFD0 && marker <= 0xFFD7) { // RSTx
|
|
354
|
+
offset += 2;
|
|
355
|
+
}
|
|
356
|
+
else
|
|
357
|
+
break;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
return offset - startOffset;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
function buildComponentData(frame, component) {
|
|
364
|
+
var lines = [];
|
|
365
|
+
var blocksPerLine = component.blocksPerLine;
|
|
366
|
+
var blocksPerColumn = component.blocksPerColumn;
|
|
367
|
+
var samplesPerLine = blocksPerLine << 3;
|
|
368
|
+
// Only 1 used per invocation of this function and garbage collected after invocation, so no need to account for its memory footprint.
|
|
369
|
+
var R = new Int32Array(64), r = new Uint8Array(64);
|
|
370
|
+
|
|
371
|
+
// A port of poppler's IDCT method which in turn is taken from:
|
|
372
|
+
// Christoph Loeffler, Adriaan Ligtenberg, George S. Moschytz,
|
|
373
|
+
// "Practical Fast 1-D DCT Algorithms with 11 Multiplications",
|
|
374
|
+
// IEEE Intl. Conf. on Acoustics, Speech & Signal Processing, 1989,
|
|
375
|
+
// 988-991.
|
|
376
|
+
function quantizeAndInverse(zz, dataOut, dataIn) {
|
|
377
|
+
var qt = component.quantizationTable;
|
|
378
|
+
var v0, v1, v2, v3, v4, v5, v6, v7, t;
|
|
379
|
+
var p = dataIn;
|
|
380
|
+
var i;
|
|
381
|
+
|
|
382
|
+
// dequant
|
|
383
|
+
for (i = 0; i < 64; i++)
|
|
384
|
+
p[i] = zz[i] * qt[i];
|
|
385
|
+
|
|
386
|
+
// inverse DCT on rows
|
|
387
|
+
for (i = 0; i < 8; ++i) {
|
|
388
|
+
var row = 8 * i;
|
|
389
|
+
|
|
390
|
+
// check for all-zero AC coefficients
|
|
391
|
+
if (p[1 + row] == 0 && p[2 + row] == 0 && p[3 + row] == 0 &&
|
|
392
|
+
p[4 + row] == 0 && p[5 + row] == 0 && p[6 + row] == 0 &&
|
|
393
|
+
p[7 + row] == 0) {
|
|
394
|
+
t = (dctSqrt2 * p[0 + row] + 512) >> 10;
|
|
395
|
+
p[0 + row] = t;
|
|
396
|
+
p[1 + row] = t;
|
|
397
|
+
p[2 + row] = t;
|
|
398
|
+
p[3 + row] = t;
|
|
399
|
+
p[4 + row] = t;
|
|
400
|
+
p[5 + row] = t;
|
|
401
|
+
p[6 + row] = t;
|
|
402
|
+
p[7 + row] = t;
|
|
403
|
+
continue;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
// stage 4
|
|
407
|
+
v0 = (dctSqrt2 * p[0 + row] + 128) >> 8;
|
|
408
|
+
v1 = (dctSqrt2 * p[4 + row] + 128) >> 8;
|
|
409
|
+
v2 = p[2 + row];
|
|
410
|
+
v3 = p[6 + row];
|
|
411
|
+
v4 = (dctSqrt1d2 * (p[1 + row] - p[7 + row]) + 128) >> 8;
|
|
412
|
+
v7 = (dctSqrt1d2 * (p[1 + row] + p[7 + row]) + 128) >> 8;
|
|
413
|
+
v5 = p[3 + row] << 4;
|
|
414
|
+
v6 = p[5 + row] << 4;
|
|
415
|
+
|
|
416
|
+
// stage 3
|
|
417
|
+
t = (v0 - v1+ 1) >> 1;
|
|
418
|
+
v0 = (v0 + v1 + 1) >> 1;
|
|
419
|
+
v1 = t;
|
|
420
|
+
t = (v2 * dctSin6 + v3 * dctCos6 + 128) >> 8;
|
|
421
|
+
v2 = (v2 * dctCos6 - v3 * dctSin6 + 128) >> 8;
|
|
422
|
+
v3 = t;
|
|
423
|
+
t = (v4 - v6 + 1) >> 1;
|
|
424
|
+
v4 = (v4 + v6 + 1) >> 1;
|
|
425
|
+
v6 = t;
|
|
426
|
+
t = (v7 + v5 + 1) >> 1;
|
|
427
|
+
v5 = (v7 - v5 + 1) >> 1;
|
|
428
|
+
v7 = t;
|
|
429
|
+
|
|
430
|
+
// stage 2
|
|
431
|
+
t = (v0 - v3 + 1) >> 1;
|
|
432
|
+
v0 = (v0 + v3 + 1) >> 1;
|
|
433
|
+
v3 = t;
|
|
434
|
+
t = (v1 - v2 + 1) >> 1;
|
|
435
|
+
v1 = (v1 + v2 + 1) >> 1;
|
|
436
|
+
v2 = t;
|
|
437
|
+
t = (v4 * dctSin3 + v7 * dctCos3 + 2048) >> 12;
|
|
438
|
+
v4 = (v4 * dctCos3 - v7 * dctSin3 + 2048) >> 12;
|
|
439
|
+
v7 = t;
|
|
440
|
+
t = (v5 * dctSin1 + v6 * dctCos1 + 2048) >> 12;
|
|
441
|
+
v5 = (v5 * dctCos1 - v6 * dctSin1 + 2048) >> 12;
|
|
442
|
+
v6 = t;
|
|
443
|
+
|
|
444
|
+
// stage 1
|
|
445
|
+
p[0 + row] = v0 + v7;
|
|
446
|
+
p[7 + row] = v0 - v7;
|
|
447
|
+
p[1 + row] = v1 + v6;
|
|
448
|
+
p[6 + row] = v1 - v6;
|
|
449
|
+
p[2 + row] = v2 + v5;
|
|
450
|
+
p[5 + row] = v2 - v5;
|
|
451
|
+
p[3 + row] = v3 + v4;
|
|
452
|
+
p[4 + row] = v3 - v4;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
// inverse DCT on columns
|
|
456
|
+
for (i = 0; i < 8; ++i) {
|
|
457
|
+
var col = i;
|
|
458
|
+
|
|
459
|
+
// check for all-zero AC coefficients
|
|
460
|
+
if (p[1*8 + col] == 0 && p[2*8 + col] == 0 && p[3*8 + col] == 0 &&
|
|
461
|
+
p[4*8 + col] == 0 && p[5*8 + col] == 0 && p[6*8 + col] == 0 &&
|
|
462
|
+
p[7*8 + col] == 0) {
|
|
463
|
+
t = (dctSqrt2 * dataIn[i+0] + 8192) >> 14;
|
|
464
|
+
p[0*8 + col] = t;
|
|
465
|
+
p[1*8 + col] = t;
|
|
466
|
+
p[2*8 + col] = t;
|
|
467
|
+
p[3*8 + col] = t;
|
|
468
|
+
p[4*8 + col] = t;
|
|
469
|
+
p[5*8 + col] = t;
|
|
470
|
+
p[6*8 + col] = t;
|
|
471
|
+
p[7*8 + col] = t;
|
|
472
|
+
continue;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
// stage 4
|
|
476
|
+
v0 = (dctSqrt2 * p[0*8 + col] + 2048) >> 12;
|
|
477
|
+
v1 = (dctSqrt2 * p[4*8 + col] + 2048) >> 12;
|
|
478
|
+
v2 = p[2*8 + col];
|
|
479
|
+
v3 = p[6*8 + col];
|
|
480
|
+
v4 = (dctSqrt1d2 * (p[1*8 + col] - p[7*8 + col]) + 2048) >> 12;
|
|
481
|
+
v7 = (dctSqrt1d2 * (p[1*8 + col] + p[7*8 + col]) + 2048) >> 12;
|
|
482
|
+
v5 = p[3*8 + col];
|
|
483
|
+
v6 = p[5*8 + col];
|
|
484
|
+
|
|
485
|
+
// stage 3
|
|
486
|
+
t = (v0 - v1 + 1) >> 1;
|
|
487
|
+
v0 = (v0 + v1 + 1) >> 1;
|
|
488
|
+
v1 = t;
|
|
489
|
+
t = (v2 * dctSin6 + v3 * dctCos6 + 2048) >> 12;
|
|
490
|
+
v2 = (v2 * dctCos6 - v3 * dctSin6 + 2048) >> 12;
|
|
491
|
+
v3 = t;
|
|
492
|
+
t = (v4 - v6 + 1) >> 1;
|
|
493
|
+
v4 = (v4 + v6 + 1) >> 1;
|
|
494
|
+
v6 = t;
|
|
495
|
+
t = (v7 + v5 + 1) >> 1;
|
|
496
|
+
v5 = (v7 - v5 + 1) >> 1;
|
|
497
|
+
v7 = t;
|
|
498
|
+
|
|
499
|
+
// stage 2
|
|
500
|
+
t = (v0 - v3 + 1) >> 1;
|
|
501
|
+
v0 = (v0 + v3 + 1) >> 1;
|
|
502
|
+
v3 = t;
|
|
503
|
+
t = (v1 - v2 + 1) >> 1;
|
|
504
|
+
v1 = (v1 + v2 + 1) >> 1;
|
|
505
|
+
v2 = t;
|
|
506
|
+
t = (v4 * dctSin3 + v7 * dctCos3 + 2048) >> 12;
|
|
507
|
+
v4 = (v4 * dctCos3 - v7 * dctSin3 + 2048) >> 12;
|
|
508
|
+
v7 = t;
|
|
509
|
+
t = (v5 * dctSin1 + v6 * dctCos1 + 2048) >> 12;
|
|
510
|
+
v5 = (v5 * dctCos1 - v6 * dctSin1 + 2048) >> 12;
|
|
511
|
+
v6 = t;
|
|
512
|
+
|
|
513
|
+
// stage 1
|
|
514
|
+
p[0*8 + col] = v0 + v7;
|
|
515
|
+
p[7*8 + col] = v0 - v7;
|
|
516
|
+
p[1*8 + col] = v1 + v6;
|
|
517
|
+
p[6*8 + col] = v1 - v6;
|
|
518
|
+
p[2*8 + col] = v2 + v5;
|
|
519
|
+
p[5*8 + col] = v2 - v5;
|
|
520
|
+
p[3*8 + col] = v3 + v4;
|
|
521
|
+
p[4*8 + col] = v3 - v4;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
// convert to 8-bit integers
|
|
525
|
+
for (i = 0; i < 64; ++i) {
|
|
526
|
+
var sample = 128 + ((p[i] + 8) >> 4);
|
|
527
|
+
dataOut[i] = sample < 0 ? 0 : sample > 0xFF ? 0xFF : sample;
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
requestMemoryAllocation(samplesPerLine * blocksPerColumn * 8);
|
|
532
|
+
|
|
533
|
+
var i, j;
|
|
534
|
+
for (var blockRow = 0; blockRow < blocksPerColumn; blockRow++) {
|
|
535
|
+
var scanLine = blockRow << 3;
|
|
536
|
+
for (i = 0; i < 8; i++)
|
|
537
|
+
lines.push(new Uint8Array(samplesPerLine));
|
|
538
|
+
for (var blockCol = 0; blockCol < blocksPerLine; blockCol++) {
|
|
539
|
+
quantizeAndInverse(component.blocks[blockRow][blockCol], r, R);
|
|
540
|
+
|
|
541
|
+
var offset = 0, sample = blockCol << 3;
|
|
542
|
+
for (j = 0; j < 8; j++) {
|
|
543
|
+
var line = lines[scanLine + j];
|
|
544
|
+
for (i = 0; i < 8; i++)
|
|
545
|
+
line[sample + i] = r[offset++];
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
return lines;
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
function clampTo8bit(a) {
|
|
553
|
+
return a < 0 ? 0 : a > 255 ? 255 : a;
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
constructor.prototype = {
|
|
557
|
+
load: function load(path) {
|
|
558
|
+
var xhr = new XMLHttpRequest();
|
|
559
|
+
xhr.open("GET", path, true);
|
|
560
|
+
xhr.responseType = "arraybuffer";
|
|
561
|
+
xhr.onload = (function() {
|
|
562
|
+
// TODO catch parse error
|
|
563
|
+
var data = new Uint8Array(xhr.response || xhr.mozResponseArrayBuffer);
|
|
564
|
+
this.parse(data);
|
|
565
|
+
if (this.onload)
|
|
566
|
+
this.onload();
|
|
567
|
+
}).bind(this);
|
|
568
|
+
xhr.send(null);
|
|
569
|
+
},
|
|
570
|
+
parse: function parse(data) {
|
|
571
|
+
var maxResolutionInPixels = this.opts.maxResolutionInMP * 1000 * 1000;
|
|
572
|
+
var offset = 0, length = data.length;
|
|
573
|
+
function readUint16() {
|
|
574
|
+
var value = (data[offset] << 8) | data[offset + 1];
|
|
575
|
+
offset += 2;
|
|
576
|
+
return value;
|
|
577
|
+
}
|
|
578
|
+
function readDataBlock() {
|
|
579
|
+
var length = readUint16();
|
|
580
|
+
var array = data.subarray(offset, offset + length - 2);
|
|
581
|
+
offset += array.length;
|
|
582
|
+
return array;
|
|
583
|
+
}
|
|
584
|
+
function prepareComponents(frame) {
|
|
585
|
+
// According to the JPEG standard, the sampling factor must be between 1 and 4
|
|
586
|
+
// See https://github.com/libjpeg-turbo/libjpeg-turbo/blob/9abeff46d87bd201a952e276f3e4339556a403a3/libjpeg.txt#L1138-L1146
|
|
587
|
+
var maxH = 1, maxV = 1;
|
|
588
|
+
var component, componentId;
|
|
589
|
+
for (componentId in frame.components) {
|
|
590
|
+
if (frame.components.hasOwnProperty(componentId)) {
|
|
591
|
+
component = frame.components[componentId];
|
|
592
|
+
if (maxH < component.h) maxH = component.h;
|
|
593
|
+
if (maxV < component.v) maxV = component.v;
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
var mcusPerLine = Math.ceil(frame.samplesPerLine / 8 / maxH);
|
|
597
|
+
var mcusPerColumn = Math.ceil(frame.scanLines / 8 / maxV);
|
|
598
|
+
for (componentId in frame.components) {
|
|
599
|
+
if (frame.components.hasOwnProperty(componentId)) {
|
|
600
|
+
component = frame.components[componentId];
|
|
601
|
+
var blocksPerLine = Math.ceil(Math.ceil(frame.samplesPerLine / 8) * component.h / maxH);
|
|
602
|
+
var blocksPerColumn = Math.ceil(Math.ceil(frame.scanLines / 8) * component.v / maxV);
|
|
603
|
+
var blocksPerLineForMcu = mcusPerLine * component.h;
|
|
604
|
+
var blocksPerColumnForMcu = mcusPerColumn * component.v;
|
|
605
|
+
var blocksToAllocate = blocksPerColumnForMcu * blocksPerLineForMcu;
|
|
606
|
+
var blocks = [];
|
|
607
|
+
|
|
608
|
+
// Each block is a Int32Array of length 64 (4 x 64 = 256 bytes)
|
|
609
|
+
requestMemoryAllocation(blocksToAllocate * 256);
|
|
610
|
+
|
|
611
|
+
for (var i = 0; i < blocksPerColumnForMcu; i++) {
|
|
612
|
+
var row = [];
|
|
613
|
+
for (var j = 0; j < blocksPerLineForMcu; j++)
|
|
614
|
+
row.push(new Int32Array(64));
|
|
615
|
+
blocks.push(row);
|
|
616
|
+
}
|
|
617
|
+
component.blocksPerLine = blocksPerLine;
|
|
618
|
+
component.blocksPerColumn = blocksPerColumn;
|
|
619
|
+
component.blocks = blocks;
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
frame.maxH = maxH;
|
|
623
|
+
frame.maxV = maxV;
|
|
624
|
+
frame.mcusPerLine = mcusPerLine;
|
|
625
|
+
frame.mcusPerColumn = mcusPerColumn;
|
|
626
|
+
}
|
|
627
|
+
var jfif = null;
|
|
628
|
+
var adobe = null;
|
|
629
|
+
var pixels = null;
|
|
630
|
+
var frame, resetInterval;
|
|
631
|
+
var quantizationTables = [], frames = [];
|
|
632
|
+
var huffmanTablesAC = [], huffmanTablesDC = [];
|
|
633
|
+
var fileMarker = readUint16();
|
|
634
|
+
var malformedDataOffset = -1;
|
|
635
|
+
this.comments = [];
|
|
636
|
+
if (fileMarker != 0xFFD8) { // SOI (Start of Image)
|
|
637
|
+
throw new Error("SOI not found");
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
fileMarker = readUint16();
|
|
641
|
+
while (fileMarker != 0xFFD9) { // EOI (End of image)
|
|
642
|
+
var i, j, l;
|
|
643
|
+
switch(fileMarker) {
|
|
644
|
+
case 0xFF00: break;
|
|
645
|
+
case 0xFFE0: // APP0 (Application Specific)
|
|
646
|
+
case 0xFFE1: // APP1
|
|
647
|
+
case 0xFFE2: // APP2
|
|
648
|
+
case 0xFFE3: // APP3
|
|
649
|
+
case 0xFFE4: // APP4
|
|
650
|
+
case 0xFFE5: // APP5
|
|
651
|
+
case 0xFFE6: // APP6
|
|
652
|
+
case 0xFFE7: // APP7
|
|
653
|
+
case 0xFFE8: // APP8
|
|
654
|
+
case 0xFFE9: // APP9
|
|
655
|
+
case 0xFFEA: // APP10
|
|
656
|
+
case 0xFFEB: // APP11
|
|
657
|
+
case 0xFFEC: // APP12
|
|
658
|
+
case 0xFFED: // APP13
|
|
659
|
+
case 0xFFEE: // APP14
|
|
660
|
+
case 0xFFEF: // APP15
|
|
661
|
+
case 0xFFFE: // COM (Comment)
|
|
662
|
+
var appData = readDataBlock();
|
|
663
|
+
|
|
664
|
+
if (fileMarker === 0xFFFE) {
|
|
665
|
+
var comment = String.fromCharCode.apply(null, appData);
|
|
666
|
+
this.comments.push(comment);
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
if (fileMarker === 0xFFE0) {
|
|
670
|
+
if (appData[0] === 0x4A && appData[1] === 0x46 && appData[2] === 0x49 &&
|
|
671
|
+
appData[3] === 0x46 && appData[4] === 0) { // 'JFIF\x00'
|
|
672
|
+
jfif = {
|
|
673
|
+
version: { major: appData[5], minor: appData[6] },
|
|
674
|
+
densityUnits: appData[7],
|
|
675
|
+
xDensity: (appData[8] << 8) | appData[9],
|
|
676
|
+
yDensity: (appData[10] << 8) | appData[11],
|
|
677
|
+
thumbWidth: appData[12],
|
|
678
|
+
thumbHeight: appData[13],
|
|
679
|
+
thumbData: appData.subarray(14, 14 + 3 * appData[12] * appData[13])
|
|
680
|
+
};
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
// TODO APP1 - Exif
|
|
684
|
+
if (fileMarker === 0xFFE1) {
|
|
685
|
+
if (appData[0] === 0x45 &&
|
|
686
|
+
appData[1] === 0x78 &&
|
|
687
|
+
appData[2] === 0x69 &&
|
|
688
|
+
appData[3] === 0x66 &&
|
|
689
|
+
appData[4] === 0) { // 'EXIF\x00'
|
|
690
|
+
this.exifBuffer = appData.subarray(5, appData.length);
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
if (fileMarker === 0xFFEE) {
|
|
695
|
+
if (appData[0] === 0x41 && appData[1] === 0x64 && appData[2] === 0x6F &&
|
|
696
|
+
appData[3] === 0x62 && appData[4] === 0x65 && appData[5] === 0) { // 'Adobe\x00'
|
|
697
|
+
adobe = {
|
|
698
|
+
version: appData[6],
|
|
699
|
+
flags0: (appData[7] << 8) | appData[8],
|
|
700
|
+
flags1: (appData[9] << 8) | appData[10],
|
|
701
|
+
transformCode: appData[11]
|
|
702
|
+
};
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
break;
|
|
706
|
+
|
|
707
|
+
case 0xFFDB: // DQT (Define Quantization Tables)
|
|
708
|
+
var quantizationTablesLength = readUint16();
|
|
709
|
+
var quantizationTablesEnd = quantizationTablesLength + offset - 2;
|
|
710
|
+
while (offset < quantizationTablesEnd) {
|
|
711
|
+
var quantizationTableSpec = data[offset++];
|
|
712
|
+
requestMemoryAllocation(64 * 4);
|
|
713
|
+
var tableData = new Int32Array(64);
|
|
714
|
+
if ((quantizationTableSpec >> 4) === 0) { // 8 bit values
|
|
715
|
+
for (j = 0; j < 64; j++) {
|
|
716
|
+
var z = dctZigZag[j];
|
|
717
|
+
tableData[z] = data[offset++];
|
|
718
|
+
}
|
|
719
|
+
} else if ((quantizationTableSpec >> 4) === 1) { //16 bit
|
|
720
|
+
for (j = 0; j < 64; j++) {
|
|
721
|
+
var z = dctZigZag[j];
|
|
722
|
+
tableData[z] = readUint16();
|
|
723
|
+
}
|
|
724
|
+
} else
|
|
725
|
+
throw new Error("DQT: invalid table spec");
|
|
726
|
+
quantizationTables[quantizationTableSpec & 15] = tableData;
|
|
727
|
+
}
|
|
728
|
+
break;
|
|
729
|
+
|
|
730
|
+
case 0xFFC0: // SOF0 (Start of Frame, Baseline DCT)
|
|
731
|
+
case 0xFFC1: // SOF1 (Start of Frame, Extended DCT)
|
|
732
|
+
case 0xFFC2: // SOF2 (Start of Frame, Progressive DCT)
|
|
733
|
+
readUint16(); // skip data length
|
|
734
|
+
frame = {};
|
|
735
|
+
frame.extended = (fileMarker === 0xFFC1);
|
|
736
|
+
frame.progressive = (fileMarker === 0xFFC2);
|
|
737
|
+
frame.precision = data[offset++];
|
|
738
|
+
frame.scanLines = readUint16();
|
|
739
|
+
frame.samplesPerLine = readUint16();
|
|
740
|
+
frame.components = {};
|
|
741
|
+
frame.componentsOrder = [];
|
|
742
|
+
|
|
743
|
+
var pixelsInFrame = frame.scanLines * frame.samplesPerLine;
|
|
744
|
+
if (pixelsInFrame > maxResolutionInPixels) {
|
|
745
|
+
var exceededAmount = Math.ceil((pixelsInFrame - maxResolutionInPixels) / 1e6);
|
|
746
|
+
throw new Error(`maxResolutionInMP limit exceeded by ${exceededAmount}MP`);
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
var componentsCount = data[offset++], componentId;
|
|
750
|
+
var maxH = 0, maxV = 0;
|
|
751
|
+
for (i = 0; i < componentsCount; i++) {
|
|
752
|
+
componentId = data[offset];
|
|
753
|
+
var h = data[offset + 1] >> 4;
|
|
754
|
+
var v = data[offset + 1] & 15;
|
|
755
|
+
var qId = data[offset + 2];
|
|
756
|
+
|
|
757
|
+
if ( h <= 0 || v <= 0 ) {
|
|
758
|
+
throw new Error('Invalid sampling factor, expected values above 0');
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
frame.componentsOrder.push(componentId);
|
|
762
|
+
frame.components[componentId] = {
|
|
763
|
+
h: h,
|
|
764
|
+
v: v,
|
|
765
|
+
quantizationIdx: qId
|
|
766
|
+
};
|
|
767
|
+
offset += 3;
|
|
768
|
+
}
|
|
769
|
+
prepareComponents(frame);
|
|
770
|
+
frames.push(frame);
|
|
771
|
+
break;
|
|
772
|
+
|
|
773
|
+
case 0xFFC4: // DHT (Define Huffman Tables)
|
|
774
|
+
var huffmanLength = readUint16();
|
|
775
|
+
for (i = 2; i < huffmanLength;) {
|
|
776
|
+
var huffmanTableSpec = data[offset++];
|
|
777
|
+
var codeLengths = new Uint8Array(16);
|
|
778
|
+
var codeLengthSum = 0;
|
|
779
|
+
for (j = 0; j < 16; j++, offset++) {
|
|
780
|
+
codeLengthSum += (codeLengths[j] = data[offset]);
|
|
781
|
+
}
|
|
782
|
+
requestMemoryAllocation(16 + codeLengthSum);
|
|
783
|
+
var huffmanValues = new Uint8Array(codeLengthSum);
|
|
784
|
+
for (j = 0; j < codeLengthSum; j++, offset++)
|
|
785
|
+
huffmanValues[j] = data[offset];
|
|
786
|
+
i += 17 + codeLengthSum;
|
|
787
|
+
|
|
788
|
+
((huffmanTableSpec >> 4) === 0 ?
|
|
789
|
+
huffmanTablesDC : huffmanTablesAC)[huffmanTableSpec & 15] =
|
|
790
|
+
buildHuffmanTable(codeLengths, huffmanValues);
|
|
791
|
+
}
|
|
792
|
+
break;
|
|
793
|
+
|
|
794
|
+
case 0xFFDD: // DRI (Define Restart Interval)
|
|
795
|
+
readUint16(); // skip data length
|
|
796
|
+
resetInterval = readUint16();
|
|
797
|
+
break;
|
|
798
|
+
|
|
799
|
+
case 0xFFDC: // Number of Lines marker
|
|
800
|
+
readUint16() // skip data length
|
|
801
|
+
readUint16() // Ignore this data since it represents the image height
|
|
802
|
+
break;
|
|
803
|
+
|
|
804
|
+
case 0xFFDA: // SOS (Start of Scan)
|
|
805
|
+
var scanLength = readUint16();
|
|
806
|
+
var selectorsCount = data[offset++];
|
|
807
|
+
var components = [], component;
|
|
808
|
+
for (i = 0; i < selectorsCount; i++) {
|
|
809
|
+
component = frame.components[data[offset++]];
|
|
810
|
+
var tableSpec = data[offset++];
|
|
811
|
+
component.huffmanTableDC = huffmanTablesDC[tableSpec >> 4];
|
|
812
|
+
component.huffmanTableAC = huffmanTablesAC[tableSpec & 15];
|
|
813
|
+
components.push(component);
|
|
814
|
+
}
|
|
815
|
+
var spectralStart = data[offset++];
|
|
816
|
+
var spectralEnd = data[offset++];
|
|
817
|
+
var successiveApproximation = data[offset++];
|
|
818
|
+
var processed = decodeScan(data, offset,
|
|
819
|
+
frame, components, resetInterval,
|
|
820
|
+
spectralStart, spectralEnd,
|
|
821
|
+
successiveApproximation >> 4, successiveApproximation & 15, this.opts);
|
|
822
|
+
offset += processed;
|
|
823
|
+
break;
|
|
824
|
+
|
|
825
|
+
case 0xFFFF: // Fill bytes
|
|
826
|
+
if (data[offset] !== 0xFF) { // Avoid skipping a valid marker.
|
|
827
|
+
offset--;
|
|
828
|
+
}
|
|
829
|
+
break;
|
|
830
|
+
default:
|
|
831
|
+
if (data[offset - 3] == 0xFF &&
|
|
832
|
+
data[offset - 2] >= 0xC0 && data[offset - 2] <= 0xFE) {
|
|
833
|
+
// could be incorrect encoding -- last 0xFF byte of the previous
|
|
834
|
+
// block was eaten by the encoder
|
|
835
|
+
offset -= 3;
|
|
836
|
+
break;
|
|
837
|
+
}
|
|
838
|
+
else if (fileMarker === 0xE0 || fileMarker == 0xE1) {
|
|
839
|
+
// Recover from malformed APP1 markers popular in some phone models.
|
|
840
|
+
// See https://github.com/eugeneware/jpeg-js/issues/82
|
|
841
|
+
if (malformedDataOffset !== -1) {
|
|
842
|
+
throw new Error(`first unknown JPEG marker at offset ${malformedDataOffset.toString(16)}, second unknown JPEG marker ${fileMarker.toString(16)} at offset ${(offset - 1).toString(16)}`);
|
|
843
|
+
}
|
|
844
|
+
malformedDataOffset = offset - 1;
|
|
845
|
+
const nextOffset = readUint16();
|
|
846
|
+
if (data[offset + nextOffset - 2] === 0xFF) {
|
|
847
|
+
offset += nextOffset - 2;
|
|
848
|
+
break;
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
throw new Error("unknown JPEG marker " + fileMarker.toString(16));
|
|
852
|
+
}
|
|
853
|
+
fileMarker = readUint16();
|
|
854
|
+
}
|
|
855
|
+
if (frames.length != 1)
|
|
856
|
+
throw new Error("only single frame JPEGs supported");
|
|
857
|
+
|
|
858
|
+
// set each frame's components quantization table
|
|
859
|
+
for (var i = 0; i < frames.length; i++) {
|
|
860
|
+
var cp = frames[i].components;
|
|
861
|
+
for (var j in cp) {
|
|
862
|
+
cp[j].quantizationTable = quantizationTables[cp[j].quantizationIdx];
|
|
863
|
+
delete cp[j].quantizationIdx;
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
this.width = frame.samplesPerLine;
|
|
868
|
+
this.height = frame.scanLines;
|
|
869
|
+
this.jfif = jfif;
|
|
870
|
+
this.adobe = adobe;
|
|
871
|
+
this.components = [];
|
|
872
|
+
for (var i = 0; i < frame.componentsOrder.length; i++) {
|
|
873
|
+
var component = frame.components[frame.componentsOrder[i]];
|
|
874
|
+
this.components.push({
|
|
875
|
+
lines: buildComponentData(frame, component),
|
|
876
|
+
scaleX: component.h / frame.maxH,
|
|
877
|
+
scaleY: component.v / frame.maxV
|
|
878
|
+
});
|
|
879
|
+
}
|
|
880
|
+
},
|
|
881
|
+
getData: function getData(width, height) {
|
|
882
|
+
var scaleX = this.width / width, scaleY = this.height / height;
|
|
883
|
+
|
|
884
|
+
var component1, component2, component3, component4;
|
|
885
|
+
var component1Line, component2Line, component3Line, component4Line;
|
|
886
|
+
var x, y;
|
|
887
|
+
var offset = 0;
|
|
888
|
+
var Y, Cb, Cr, K, C, M, Ye, R, G, B;
|
|
889
|
+
var colorTransform;
|
|
890
|
+
var dataLength = width * height * this.components.length;
|
|
891
|
+
requestMemoryAllocation(dataLength);
|
|
892
|
+
var data = new Uint8Array(dataLength);
|
|
893
|
+
switch (this.components.length) {
|
|
894
|
+
case 1:
|
|
895
|
+
component1 = this.components[0];
|
|
896
|
+
for (y = 0; y < height; y++) {
|
|
897
|
+
component1Line = component1.lines[0 | (y * component1.scaleY * scaleY)];
|
|
898
|
+
for (x = 0; x < width; x++) {
|
|
899
|
+
Y = component1Line[0 | (x * component1.scaleX * scaleX)];
|
|
900
|
+
|
|
901
|
+
data[offset++] = Y;
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
break;
|
|
905
|
+
case 2:
|
|
906
|
+
// PDF might compress two component data in custom colorspace
|
|
907
|
+
component1 = this.components[0];
|
|
908
|
+
component2 = this.components[1];
|
|
909
|
+
for (y = 0; y < height; y++) {
|
|
910
|
+
component1Line = component1.lines[0 | (y * component1.scaleY * scaleY)];
|
|
911
|
+
component2Line = component2.lines[0 | (y * component2.scaleY * scaleY)];
|
|
912
|
+
for (x = 0; x < width; x++) {
|
|
913
|
+
Y = component1Line[0 | (x * component1.scaleX * scaleX)];
|
|
914
|
+
data[offset++] = Y;
|
|
915
|
+
Y = component2Line[0 | (x * component2.scaleX * scaleX)];
|
|
916
|
+
data[offset++] = Y;
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
break;
|
|
920
|
+
case 3:
|
|
921
|
+
// The default transform for three components is true
|
|
922
|
+
colorTransform = true;
|
|
923
|
+
// The adobe transform marker overrides any previous setting
|
|
924
|
+
if (this.adobe && this.adobe.transformCode)
|
|
925
|
+
colorTransform = true;
|
|
926
|
+
else if (typeof this.opts.colorTransform !== 'undefined')
|
|
927
|
+
colorTransform = !!this.opts.colorTransform;
|
|
928
|
+
|
|
929
|
+
component1 = this.components[0];
|
|
930
|
+
component2 = this.components[1];
|
|
931
|
+
component3 = this.components[2];
|
|
932
|
+
for (y = 0; y < height; y++) {
|
|
933
|
+
component1Line = component1.lines[0 | (y * component1.scaleY * scaleY)];
|
|
934
|
+
component2Line = component2.lines[0 | (y * component2.scaleY * scaleY)];
|
|
935
|
+
component3Line = component3.lines[0 | (y * component3.scaleY * scaleY)];
|
|
936
|
+
for (x = 0; x < width; x++) {
|
|
937
|
+
if (!colorTransform) {
|
|
938
|
+
R = component1Line[0 | (x * component1.scaleX * scaleX)];
|
|
939
|
+
G = component2Line[0 | (x * component2.scaleX * scaleX)];
|
|
940
|
+
B = component3Line[0 | (x * component3.scaleX * scaleX)];
|
|
941
|
+
} else {
|
|
942
|
+
Y = component1Line[0 | (x * component1.scaleX * scaleX)];
|
|
943
|
+
Cb = component2Line[0 | (x * component2.scaleX * scaleX)];
|
|
944
|
+
Cr = component3Line[0 | (x * component3.scaleX * scaleX)];
|
|
945
|
+
|
|
946
|
+
R = clampTo8bit(Y + 1.402 * (Cr - 128));
|
|
947
|
+
G = clampTo8bit(Y - 0.3441363 * (Cb - 128) - 0.71413636 * (Cr - 128));
|
|
948
|
+
B = clampTo8bit(Y + 1.772 * (Cb - 128));
|
|
949
|
+
}
|
|
950
|
+
|
|
951
|
+
data[offset++] = R;
|
|
952
|
+
data[offset++] = G;
|
|
953
|
+
data[offset++] = B;
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
break;
|
|
957
|
+
case 4:
|
|
958
|
+
if (!this.adobe)
|
|
959
|
+
throw new Error('Unsupported color mode (4 components)');
|
|
960
|
+
// The default transform for four components is false
|
|
961
|
+
colorTransform = false;
|
|
962
|
+
// The adobe transform marker overrides any previous setting
|
|
963
|
+
if (this.adobe && this.adobe.transformCode)
|
|
964
|
+
colorTransform = true;
|
|
965
|
+
else if (typeof this.opts.colorTransform !== 'undefined')
|
|
966
|
+
colorTransform = !!this.opts.colorTransform;
|
|
967
|
+
|
|
968
|
+
component1 = this.components[0];
|
|
969
|
+
component2 = this.components[1];
|
|
970
|
+
component3 = this.components[2];
|
|
971
|
+
component4 = this.components[3];
|
|
972
|
+
for (y = 0; y < height; y++) {
|
|
973
|
+
component1Line = component1.lines[0 | (y * component1.scaleY * scaleY)];
|
|
974
|
+
component2Line = component2.lines[0 | (y * component2.scaleY * scaleY)];
|
|
975
|
+
component3Line = component3.lines[0 | (y * component3.scaleY * scaleY)];
|
|
976
|
+
component4Line = component4.lines[0 | (y * component4.scaleY * scaleY)];
|
|
977
|
+
for (x = 0; x < width; x++) {
|
|
978
|
+
if (!colorTransform) {
|
|
979
|
+
C = component1Line[0 | (x * component1.scaleX * scaleX)];
|
|
980
|
+
M = component2Line[0 | (x * component2.scaleX * scaleX)];
|
|
981
|
+
Ye = component3Line[0 | (x * component3.scaleX * scaleX)];
|
|
982
|
+
K = component4Line[0 | (x * component4.scaleX * scaleX)];
|
|
983
|
+
} else {
|
|
984
|
+
Y = component1Line[0 | (x * component1.scaleX * scaleX)];
|
|
985
|
+
Cb = component2Line[0 | (x * component2.scaleX * scaleX)];
|
|
986
|
+
Cr = component3Line[0 | (x * component3.scaleX * scaleX)];
|
|
987
|
+
K = component4Line[0 | (x * component4.scaleX * scaleX)];
|
|
988
|
+
|
|
989
|
+
C = 255 - clampTo8bit(Y + 1.402 * (Cr - 128));
|
|
990
|
+
M = 255 - clampTo8bit(Y - 0.3441363 * (Cb - 128) - 0.71413636 * (Cr - 128));
|
|
991
|
+
Ye = 255 - clampTo8bit(Y + 1.772 * (Cb - 128));
|
|
992
|
+
}
|
|
993
|
+
data[offset++] = 255-C;
|
|
994
|
+
data[offset++] = 255-M;
|
|
995
|
+
data[offset++] = 255-Ye;
|
|
996
|
+
data[offset++] = 255-K;
|
|
997
|
+
}
|
|
998
|
+
}
|
|
999
|
+
break;
|
|
1000
|
+
default:
|
|
1001
|
+
throw new Error('Unsupported color mode');
|
|
1002
|
+
}
|
|
1003
|
+
return data;
|
|
1004
|
+
},
|
|
1005
|
+
copyToImageData: function copyToImageData(imageData, formatAsRGBA) {
|
|
1006
|
+
var width = imageData.width, height = imageData.height;
|
|
1007
|
+
var imageDataArray = imageData.data;
|
|
1008
|
+
var data = this.getData(width, height);
|
|
1009
|
+
var i = 0, j = 0, x, y;
|
|
1010
|
+
var Y, K, C, M, R, G, B;
|
|
1011
|
+
switch (this.components.length) {
|
|
1012
|
+
case 1:
|
|
1013
|
+
for (y = 0; y < height; y++) {
|
|
1014
|
+
for (x = 0; x < width; x++) {
|
|
1015
|
+
Y = data[i++];
|
|
1016
|
+
|
|
1017
|
+
imageDataArray[j++] = Y;
|
|
1018
|
+
imageDataArray[j++] = Y;
|
|
1019
|
+
imageDataArray[j++] = Y;
|
|
1020
|
+
if (formatAsRGBA) {
|
|
1021
|
+
imageDataArray[j++] = 255;
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
break;
|
|
1026
|
+
case 3:
|
|
1027
|
+
for (y = 0; y < height; y++) {
|
|
1028
|
+
for (x = 0; x < width; x++) {
|
|
1029
|
+
R = data[i++];
|
|
1030
|
+
G = data[i++];
|
|
1031
|
+
B = data[i++];
|
|
1032
|
+
|
|
1033
|
+
imageDataArray[j++] = R;
|
|
1034
|
+
imageDataArray[j++] = G;
|
|
1035
|
+
imageDataArray[j++] = B;
|
|
1036
|
+
if (formatAsRGBA) {
|
|
1037
|
+
imageDataArray[j++] = 255;
|
|
1038
|
+
}
|
|
1039
|
+
}
|
|
1040
|
+
}
|
|
1041
|
+
break;
|
|
1042
|
+
case 4:
|
|
1043
|
+
for (y = 0; y < height; y++) {
|
|
1044
|
+
for (x = 0; x < width; x++) {
|
|
1045
|
+
C = data[i++];
|
|
1046
|
+
M = data[i++];
|
|
1047
|
+
Y = data[i++];
|
|
1048
|
+
K = data[i++];
|
|
1049
|
+
|
|
1050
|
+
R = 255 - clampTo8bit(C * (1 - K / 255) + K);
|
|
1051
|
+
G = 255 - clampTo8bit(M * (1 - K / 255) + K);
|
|
1052
|
+
B = 255 - clampTo8bit(Y * (1 - K / 255) + K);
|
|
1053
|
+
|
|
1054
|
+
imageDataArray[j++] = R;
|
|
1055
|
+
imageDataArray[j++] = G;
|
|
1056
|
+
imageDataArray[j++] = B;
|
|
1057
|
+
if (formatAsRGBA) {
|
|
1058
|
+
imageDataArray[j++] = 255;
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
1061
|
+
}
|
|
1062
|
+
break;
|
|
1063
|
+
default:
|
|
1064
|
+
throw new Error('Unsupported color mode');
|
|
1065
|
+
}
|
|
1066
|
+
}
|
|
1067
|
+
};
|
|
1068
|
+
|
|
1069
|
+
|
|
1070
|
+
// We cap the amount of memory used by jpeg-js to avoid unexpected OOMs from untrusted content.
|
|
1071
|
+
var totalBytesAllocated = 0;
|
|
1072
|
+
var maxMemoryUsageBytes = 0;
|
|
1073
|
+
function requestMemoryAllocation(increaseAmount = 0) {
|
|
1074
|
+
var totalMemoryImpactBytes = totalBytesAllocated + increaseAmount;
|
|
1075
|
+
if (totalMemoryImpactBytes > maxMemoryUsageBytes) {
|
|
1076
|
+
var exceededAmount = Math.ceil((totalMemoryImpactBytes - maxMemoryUsageBytes) / 1024 / 1024);
|
|
1077
|
+
throw new Error(`maxMemoryUsageInMB limit exceeded by at least ${exceededAmount}MB`);
|
|
1078
|
+
}
|
|
1079
|
+
|
|
1080
|
+
totalBytesAllocated = totalMemoryImpactBytes;
|
|
1081
|
+
}
|
|
1082
|
+
|
|
1083
|
+
constructor.resetMaxMemoryUsage = function (maxMemoryUsageBytes_) {
|
|
1084
|
+
totalBytesAllocated = 0;
|
|
1085
|
+
maxMemoryUsageBytes = maxMemoryUsageBytes_;
|
|
1086
|
+
};
|
|
1087
|
+
|
|
1088
|
+
constructor.getBytesAllocated = function () {
|
|
1089
|
+
return totalBytesAllocated;
|
|
1090
|
+
};
|
|
1091
|
+
|
|
1092
|
+
constructor.requestMemoryAllocation = requestMemoryAllocation;
|
|
1093
|
+
|
|
1094
|
+
return constructor;
|
|
1095
|
+
})();
|
|
1096
|
+
|
|
1097
|
+
export default decode;
|
|
1098
|
+
|
|
1099
|
+
function decode(jpegData, userOpts = {}) {
|
|
1100
|
+
var defaultOpts = {
|
|
1101
|
+
// "undefined" means "Choose whether to transform colors based on the image’s color model."
|
|
1102
|
+
colorTransform: undefined,
|
|
1103
|
+
useTArray: false,
|
|
1104
|
+
formatAsRGBA: true,
|
|
1105
|
+
tolerantDecoding: true,
|
|
1106
|
+
maxResolutionInMP: 100, // Don't decode more than 100 megapixels
|
|
1107
|
+
maxMemoryUsageInMB: 512, // Don't decode if memory footprint is more than 512MB
|
|
1108
|
+
};
|
|
1109
|
+
|
|
1110
|
+
var opts = {...defaultOpts, ...userOpts};
|
|
1111
|
+
var arr = new Uint8Array(jpegData);
|
|
1112
|
+
var decoder = new JpegImage();
|
|
1113
|
+
decoder.opts = opts;
|
|
1114
|
+
// If this constructor ever supports async decoding this will need to be done differently.
|
|
1115
|
+
// Until then, treating as singleton limit is fine.
|
|
1116
|
+
JpegImage.resetMaxMemoryUsage(opts.maxMemoryUsageInMB * 1024 * 1024);
|
|
1117
|
+
decoder.parse(arr);
|
|
1118
|
+
|
|
1119
|
+
var channels = (opts.formatAsRGBA) ? 4 : 3;
|
|
1120
|
+
var bytesNeeded = decoder.width * decoder.height * channels;
|
|
1121
|
+
try {
|
|
1122
|
+
JpegImage.requestMemoryAllocation(bytesNeeded);
|
|
1123
|
+
var image = {
|
|
1124
|
+
width: decoder.width,
|
|
1125
|
+
height: decoder.height,
|
|
1126
|
+
exifBuffer: decoder.exifBuffer,
|
|
1127
|
+
data: opts.useTArray ?
|
|
1128
|
+
new Uint8Array(bytesNeeded) :
|
|
1129
|
+
Buffer.alloc(bytesNeeded)
|
|
1130
|
+
};
|
|
1131
|
+
if(decoder.comments.length > 0) {
|
|
1132
|
+
image["comments"] = decoder.comments;
|
|
1133
|
+
}
|
|
1134
|
+
} catch (err) {
|
|
1135
|
+
if (err instanceof RangeError) {
|
|
1136
|
+
throw new Error("Could not allocate enough memory for the image. " +
|
|
1137
|
+
"Required: " + bytesNeeded);
|
|
1138
|
+
}
|
|
1139
|
+
|
|
1140
|
+
if (err instanceof ReferenceError) {
|
|
1141
|
+
if (err.message === "Buffer is not defined") {
|
|
1142
|
+
throw new Error("Buffer is not globally defined in this environment. " +
|
|
1143
|
+
"Consider setting useTArray to true");
|
|
1144
|
+
}
|
|
1145
|
+
}
|
|
1146
|
+
throw err;
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
decoder.copyToImageData(image, opts.formatAsRGBA);
|
|
1150
|
+
|
|
1151
|
+
return image;
|
|
1152
|
+
}
|