@vaadin-component-factory/vcf-pdf-viewer 0.9.0 → 1.0.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.
Files changed (172) hide show
  1. package/README.md +1 -1
  2. package/package.json +42 -26
  3. package/{src/display → pdfjs/dist}/display_utils.js +344 -139
  4. package/{src/display → pdfjs/dist}/fetch_stream.js +115 -97
  5. package/pdfjs/dist/l10n_utils.js +140 -0
  6. package/{src/shared → pdfjs/dist}/message_handler.js +243 -259
  7. package/{src/display → pdfjs/dist}/network.js +149 -87
  8. package/{src/display/content_disposition.js → pdfjs/dist/network_utils.js} +167 -55
  9. package/{src/display → pdfjs/dist}/node_stream.js +133 -98
  10. package/pdfjs/dist/pdf.js +12778 -0
  11. package/pdfjs/dist/pdf_link_service.js +638 -0
  12. package/pdfjs/dist/pdf_rendering_queue.js +199 -0
  13. package/pdfjs/dist/pdf_thumbnail_viewer.js +819 -0
  14. package/pdfjs/dist/pdf_viewer.js +3598 -0
  15. package/pdfjs/dist/ui_utils.js +1033 -0
  16. package/{src/shared → pdfjs/dist}/util.js +301 -287
  17. package/pdfjs/dist/worker.js +62813 -0
  18. package/src/vcf-pdf-viewer.js +98 -46
  19. package/theme/lumo/vcf-pdf-viewer-styles.js +4 -4
  20. package/theme/material/vcf-pdf-viewer-styles.js +4 -4
  21. package/theme/material/vcf-pdf-viewer.js +2 -2
  22. package/src/core/.eslintrc +0 -13
  23. package/src/core/annotation.js +0 -2948
  24. package/src/core/arithmetic_decoder.js +0 -182
  25. package/src/core/ascii_85_stream.js +0 -98
  26. package/src/core/ascii_hex_stream.js +0 -79
  27. package/src/core/base_stream.js +0 -110
  28. package/src/core/bidi.js +0 -438
  29. package/src/core/calibri_factors.js +0 -308
  30. package/src/core/catalog.js +0 -1459
  31. package/src/core/ccitt.js +0 -1062
  32. package/src/core/ccitt_stream.js +0 -60
  33. package/src/core/cff_font.js +0 -116
  34. package/src/core/cff_parser.js +0 -1949
  35. package/src/core/charsets.js +0 -119
  36. package/src/core/chunked_stream.js +0 -557
  37. package/src/core/cmap.js +0 -1039
  38. package/src/core/colorspace.js +0 -1533
  39. package/src/core/core_utils.js +0 -464
  40. package/src/core/crypto.js +0 -1900
  41. package/src/core/decode_stream.js +0 -170
  42. package/src/core/decrypt_stream.js +0 -59
  43. package/src/core/default_appearance.js +0 -99
  44. package/src/core/document.js +0 -1456
  45. package/src/core/encodings.js +0 -301
  46. package/src/core/evaluator.js +0 -4601
  47. package/src/core/file_spec.js +0 -108
  48. package/src/core/flate_stream.js +0 -402
  49. package/src/core/font_renderer.js +0 -882
  50. package/src/core/fonts.js +0 -3260
  51. package/src/core/fonts_utils.js +0 -221
  52. package/src/core/function.js +0 -1257
  53. package/src/core/glyf.js +0 -706
  54. package/src/core/glyphlist.js +0 -4558
  55. package/src/core/helvetica_factors.js +0 -353
  56. package/src/core/image.js +0 -802
  57. package/src/core/image_utils.js +0 -291
  58. package/src/core/jbig2.js +0 -2572
  59. package/src/core/jbig2_stream.js +0 -73
  60. package/src/core/jpeg_stream.js +0 -105
  61. package/src/core/jpg.js +0 -1416
  62. package/src/core/jpx.js +0 -2343
  63. package/src/core/jpx_stream.js +0 -87
  64. package/src/core/liberationsans_widths.js +0 -221
  65. package/src/core/lzw_stream.js +0 -150
  66. package/src/core/metadata_parser.js +0 -146
  67. package/src/core/metrics.js +0 -2970
  68. package/src/core/murmurhash3.js +0 -139
  69. package/src/core/myriadpro_factors.js +0 -290
  70. package/src/core/name_number_tree.js +0 -153
  71. package/src/core/object_loader.js +0 -149
  72. package/src/core/opentype_file_builder.js +0 -154
  73. package/src/core/operator_list.js +0 -734
  74. package/src/core/parser.js +0 -1416
  75. package/src/core/pattern.js +0 -985
  76. package/src/core/pdf_manager.js +0 -217
  77. package/src/core/predictor_stream.js +0 -238
  78. package/src/core/primitives.js +0 -402
  79. package/src/core/ps_parser.js +0 -272
  80. package/src/core/run_length_stream.js +0 -61
  81. package/src/core/segoeui_factors.js +0 -308
  82. package/src/core/standard_fonts.js +0 -817
  83. package/src/core/stream.js +0 -103
  84. package/src/core/struct_tree.js +0 -335
  85. package/src/core/to_unicode_map.js +0 -103
  86. package/src/core/type1_font.js +0 -421
  87. package/src/core/type1_parser.js +0 -776
  88. package/src/core/unicode.js +0 -1649
  89. package/src/core/worker.js +0 -848
  90. package/src/core/worker_stream.js +0 -135
  91. package/src/core/writer.js +0 -278
  92. package/src/core/xfa/bind.js +0 -652
  93. package/src/core/xfa/builder.js +0 -207
  94. package/src/core/xfa/config.js +0 -1926
  95. package/src/core/xfa/connection_set.js +0 -202
  96. package/src/core/xfa/data.js +0 -82
  97. package/src/core/xfa/datasets.js +0 -76
  98. package/src/core/xfa/factory.js +0 -111
  99. package/src/core/xfa/fonts.js +0 -181
  100. package/src/core/xfa/formcalc_lexer.js +0 -385
  101. package/src/core/xfa/formcalc_parser.js +0 -1340
  102. package/src/core/xfa/html_utils.js +0 -639
  103. package/src/core/xfa/layout.js +0 -383
  104. package/src/core/xfa/locale_set.js +0 -345
  105. package/src/core/xfa/namespaces.js +0 -81
  106. package/src/core/xfa/parser.js +0 -184
  107. package/src/core/xfa/setup.js +0 -38
  108. package/src/core/xfa/signature.js +0 -40
  109. package/src/core/xfa/som.js +0 -338
  110. package/src/core/xfa/stylesheet.js +0 -40
  111. package/src/core/xfa/template.js +0 -6260
  112. package/src/core/xfa/text.js +0 -290
  113. package/src/core/xfa/unknown.js +0 -29
  114. package/src/core/xfa/utils.js +0 -217
  115. package/src/core/xfa/xdp.js +0 -59
  116. package/src/core/xfa/xfa_object.js +0 -1130
  117. package/src/core/xfa/xhtml.js +0 -543
  118. package/src/core/xfa_fonts.js +0 -208
  119. package/src/core/xml_parser.js +0 -507
  120. package/src/core/xref.js +0 -899
  121. package/src/display/annotation_layer.js +0 -2107
  122. package/src/display/annotation_storage.js +0 -113
  123. package/src/display/api.js +0 -3292
  124. package/src/display/base_factory.js +0 -180
  125. package/src/display/canvas.js +0 -2828
  126. package/src/display/font_loader.js +0 -484
  127. package/src/display/metadata.js +0 -41
  128. package/src/display/network_utils.js +0 -100
  129. package/src/display/node_utils.js +0 -83
  130. package/src/display/optional_content_config.js +0 -189
  131. package/src/display/pattern_helper.js +0 -659
  132. package/src/display/svg.js +0 -1709
  133. package/src/display/text_layer.js +0 -847
  134. package/src/display/transport_stream.js +0 -303
  135. package/src/display/worker_options.js +0 -40
  136. package/src/display/xfa_layer.js +0 -204
  137. package/src/doc_helper.js +0 -25
  138. package/src/images/logo.svg +0 -41
  139. package/src/interfaces.js +0 -169
  140. package/src/license_header.js +0 -14
  141. package/src/license_header_libre.js +0 -21
  142. package/src/pdf.image_decoders.js +0 -46
  143. package/src/pdf.js +0 -146
  144. package/src/pdf.sandbox.external.js +0 -181
  145. package/src/pdf.sandbox.js +0 -151
  146. package/src/pdf.scripting.js +0 -25
  147. package/src/pdf.worker.entry.js +0 -19
  148. package/src/pdf.worker.js +0 -23
  149. package/src/scripting_api/aform.js +0 -608
  150. package/src/scripting_api/app.js +0 -621
  151. package/src/scripting_api/color.js +0 -129
  152. package/src/scripting_api/common.js +0 -58
  153. package/src/scripting_api/console.js +0 -38
  154. package/src/scripting_api/constants.js +0 -208
  155. package/src/scripting_api/doc.js +0 -1195
  156. package/src/scripting_api/error.js +0 -23
  157. package/src/scripting_api/event.js +0 -232
  158. package/src/scripting_api/field.js +0 -620
  159. package/src/scripting_api/fullscreen.js +0 -145
  160. package/src/scripting_api/initialization.js +0 -223
  161. package/src/scripting_api/pdf_object.js +0 -24
  162. package/src/scripting_api/print_params.js +0 -146
  163. package/src/scripting_api/proxy.js +0 -139
  164. package/src/scripting_api/thermometer.js +0 -69
  165. package/src/scripting_api/util.js +0 -581
  166. package/src/shared/.eslintrc +0 -13
  167. package/src/shared/cffStandardStrings.js +0 -311
  168. package/src/shared/compatibility.js +0 -114
  169. package/src/shared/fonts_utils.js +0 -429
  170. package/src/shared/is_node.js +0 -27
  171. package/src/shared/scripting_utils.js +0 -85
  172. package/src/worker_loader.js +0 -32
package/src/core/jpg.js DELETED
@@ -1,1416 +0,0 @@
1
- /* Copyright 2014 Mozilla Foundation
2
- *
3
- * Licensed under the Apache License, Version 2.0 (the 'License');
4
- * you may not use this file except in compliance with the License.
5
- * You may obtain a copy of the License at
6
- *
7
- * http://www.apache.org/licenses/LICENSE-2.0
8
- *
9
- * Unless required by applicable law or agreed to in writing, software
10
- * distributed under the License is distributed on an 'AS IS' BASIS,
11
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- * See the License for the specific language governing permissions and
13
- * limitations under the License.
14
- */
15
-
16
- import { assert, BaseException, warn } from "../shared/util.js";
17
- import { readUint16 } from "./core_utils.js";
18
-
19
- class JpegError extends BaseException {
20
- constructor(msg) {
21
- super(`JPEG error: ${msg}`);
22
- }
23
- }
24
-
25
- class DNLMarkerError extends BaseException {
26
- constructor(message, scanLines) {
27
- super(message);
28
- this.scanLines = scanLines;
29
- }
30
- }
31
-
32
- class EOIMarkerError extends BaseException {}
33
-
34
- /**
35
- * This code was forked from https://github.com/notmasteryet/jpgjs.
36
- * The original version was created by GitHub user notmasteryet.
37
- *
38
- * - The JPEG specification can be found in the ITU CCITT Recommendation T.81
39
- * (www.w3.org/Graphics/JPEG/itu-t81.pdf)
40
- * - The JFIF specification can be found in the JPEG File Interchange Format
41
- * (www.w3.org/Graphics/JPEG/jfif3.pdf)
42
- * - The Adobe Application-Specific JPEG markers in the
43
- * Supporting the DCT Filters in PostScript Level 2, Technical Note #5116
44
- * (partners.adobe.com/public/developer/en/ps/sdk/5116.DCT_Filter.pdf)
45
- */
46
-
47
- // prettier-ignore
48
- const dctZigZag = new Uint8Array([
49
- 0,
50
- 1, 8,
51
- 16, 9, 2,
52
- 3, 10, 17, 24,
53
- 32, 25, 18, 11, 4,
54
- 5, 12, 19, 26, 33, 40,
55
- 48, 41, 34, 27, 20, 13, 6,
56
- 7, 14, 21, 28, 35, 42, 49, 56,
57
- 57, 50, 43, 36, 29, 22, 15,
58
- 23, 30, 37, 44, 51, 58,
59
- 59, 52, 45, 38, 31,
60
- 39, 46, 53, 60,
61
- 61, 54, 47,
62
- 55, 62,
63
- 63
64
- ]);
65
-
66
- const dctCos1 = 4017; // cos(pi/16)
67
- const dctSin1 = 799; // sin(pi/16)
68
- const dctCos3 = 3406; // cos(3*pi/16)
69
- const dctSin3 = 2276; // sin(3*pi/16)
70
- const dctCos6 = 1567; // cos(6*pi/16)
71
- const dctSin6 = 3784; // sin(6*pi/16)
72
- const dctSqrt2 = 5793; // sqrt(2)
73
- const dctSqrt1d2 = 2896; // sqrt(2) / 2
74
-
75
- function buildHuffmanTable(codeLengths, values) {
76
- let k = 0,
77
- i,
78
- j,
79
- length = 16;
80
- while (length > 0 && !codeLengths[length - 1]) {
81
- length--;
82
- }
83
- const code = [{ children: [], index: 0 }];
84
- let p = code[0],
85
- q;
86
- for (i = 0; i < length; i++) {
87
- for (j = 0; j < codeLengths[i]; j++) {
88
- p = code.pop();
89
- p.children[p.index] = values[k];
90
- while (p.index > 0) {
91
- p = code.pop();
92
- }
93
- p.index++;
94
- code.push(p);
95
- while (code.length <= i) {
96
- code.push((q = { children: [], index: 0 }));
97
- p.children[p.index] = q.children;
98
- p = q;
99
- }
100
- k++;
101
- }
102
- if (i + 1 < length) {
103
- // p here points to last code
104
- code.push((q = { children: [], index: 0 }));
105
- p.children[p.index] = q.children;
106
- p = q;
107
- }
108
- }
109
- return code[0].children;
110
- }
111
-
112
- function getBlockBufferOffset(component, row, col) {
113
- return 64 * ((component.blocksPerLine + 1) * row + col);
114
- }
115
-
116
- function decodeScan(
117
- data,
118
- offset,
119
- frame,
120
- components,
121
- resetInterval,
122
- spectralStart,
123
- spectralEnd,
124
- successivePrev,
125
- successive,
126
- parseDNLMarker = false
127
- ) {
128
- const mcusPerLine = frame.mcusPerLine;
129
- const progressive = frame.progressive;
130
-
131
- const startOffset = offset;
132
- let bitsData = 0,
133
- bitsCount = 0;
134
-
135
- function readBit() {
136
- if (bitsCount > 0) {
137
- bitsCount--;
138
- return (bitsData >> bitsCount) & 1;
139
- }
140
- bitsData = data[offset++];
141
- if (bitsData === 0xff) {
142
- const nextByte = data[offset++];
143
- if (nextByte) {
144
- if (nextByte === /* DNL = */ 0xdc && parseDNLMarker) {
145
- offset += 2; // Skip marker length.
146
-
147
- const scanLines = readUint16(data, offset);
148
- offset += 2;
149
- if (scanLines > 0 && scanLines !== frame.scanLines) {
150
- throw new DNLMarkerError(
151
- "Found DNL marker (0xFFDC) while parsing scan data",
152
- scanLines
153
- );
154
- }
155
- } else if (nextByte === /* EOI = */ 0xd9) {
156
- if (parseDNLMarker) {
157
- // NOTE: only 8-bit JPEG images are supported in this decoder.
158
- const maybeScanLines = blockRow * (frame.precision === 8 ? 8 : 0);
159
- // Heuristic to attempt to handle corrupt JPEG images with too
160
- // large `scanLines` parameter, by falling back to the currently
161
- // parsed number of scanLines when it's at least (approximately)
162
- // one order of magnitude smaller than expected (fixes
163
- // issue10880.pdf and issue10989.pdf).
164
- if (
165
- maybeScanLines > 0 &&
166
- Math.round(frame.scanLines / maybeScanLines) >= 10
167
- ) {
168
- throw new DNLMarkerError(
169
- "Found EOI marker (0xFFD9) while parsing scan data, " +
170
- "possibly caused by incorrect `scanLines` parameter",
171
- maybeScanLines
172
- );
173
- }
174
- }
175
- throw new EOIMarkerError(
176
- "Found EOI marker (0xFFD9) while parsing scan data"
177
- );
178
- }
179
- throw new JpegError(
180
- `unexpected marker ${((bitsData << 8) | nextByte).toString(16)}`
181
- );
182
- }
183
- // unstuff 0
184
- }
185
- bitsCount = 7;
186
- return bitsData >>> 7;
187
- }
188
-
189
- function decodeHuffman(tree) {
190
- let node = tree;
191
- while (true) {
192
- node = node[readBit()];
193
- switch (typeof node) {
194
- case "number":
195
- return node;
196
- case "object":
197
- continue;
198
- }
199
- throw new JpegError("invalid huffman sequence");
200
- }
201
- }
202
-
203
- function receive(length) {
204
- let n = 0;
205
- while (length > 0) {
206
- n = (n << 1) | readBit();
207
- length--;
208
- }
209
- return n;
210
- }
211
-
212
- function receiveAndExtend(length) {
213
- if (length === 1) {
214
- return readBit() === 1 ? 1 : -1;
215
- }
216
- const n = receive(length);
217
- if (n >= 1 << (length - 1)) {
218
- return n;
219
- }
220
- return n + (-1 << length) + 1;
221
- }
222
-
223
- function decodeBaseline(component, blockOffset) {
224
- const t = decodeHuffman(component.huffmanTableDC);
225
- const diff = t === 0 ? 0 : receiveAndExtend(t);
226
- component.blockData[blockOffset] = component.pred += diff;
227
- let k = 1;
228
- while (k < 64) {
229
- const rs = decodeHuffman(component.huffmanTableAC);
230
- const s = rs & 15,
231
- r = rs >> 4;
232
- if (s === 0) {
233
- if (r < 15) {
234
- break;
235
- }
236
- k += 16;
237
- continue;
238
- }
239
- k += r;
240
- const z = dctZigZag[k];
241
- component.blockData[blockOffset + z] = receiveAndExtend(s);
242
- k++;
243
- }
244
- }
245
-
246
- function decodeDCFirst(component, blockOffset) {
247
- const t = decodeHuffman(component.huffmanTableDC);
248
- const diff = t === 0 ? 0 : receiveAndExtend(t) << successive;
249
- component.blockData[blockOffset] = component.pred += diff;
250
- }
251
-
252
- function decodeDCSuccessive(component, blockOffset) {
253
- component.blockData[blockOffset] |= readBit() << successive;
254
- }
255
-
256
- let eobrun = 0;
257
- function decodeACFirst(component, blockOffset) {
258
- if (eobrun > 0) {
259
- eobrun--;
260
- return;
261
- }
262
- let k = spectralStart;
263
- const e = spectralEnd;
264
- while (k <= e) {
265
- const rs = decodeHuffman(component.huffmanTableAC);
266
- const s = rs & 15,
267
- r = rs >> 4;
268
- if (s === 0) {
269
- if (r < 15) {
270
- eobrun = receive(r) + (1 << r) - 1;
271
- break;
272
- }
273
- k += 16;
274
- continue;
275
- }
276
- k += r;
277
- const z = dctZigZag[k];
278
- component.blockData[blockOffset + z] =
279
- receiveAndExtend(s) * (1 << successive);
280
- k++;
281
- }
282
- }
283
-
284
- let successiveACState = 0,
285
- successiveACNextValue;
286
- function decodeACSuccessive(component, blockOffset) {
287
- let k = spectralStart;
288
- const e = spectralEnd;
289
- let r = 0;
290
- let s;
291
- let rs;
292
- while (k <= e) {
293
- const offsetZ = blockOffset + dctZigZag[k];
294
- const sign = component.blockData[offsetZ] < 0 ? -1 : 1;
295
- switch (successiveACState) {
296
- case 0: // initial state
297
- rs = decodeHuffman(component.huffmanTableAC);
298
- s = rs & 15;
299
- r = rs >> 4;
300
- if (s === 0) {
301
- if (r < 15) {
302
- eobrun = receive(r) + (1 << r);
303
- successiveACState = 4;
304
- } else {
305
- r = 16;
306
- successiveACState = 1;
307
- }
308
- } else {
309
- if (s !== 1) {
310
- throw new JpegError("invalid ACn encoding");
311
- }
312
- successiveACNextValue = receiveAndExtend(s);
313
- successiveACState = r ? 2 : 3;
314
- }
315
- continue;
316
- case 1: // skipping r zero items
317
- case 2:
318
- if (component.blockData[offsetZ]) {
319
- component.blockData[offsetZ] += sign * (readBit() << successive);
320
- } else {
321
- r--;
322
- if (r === 0) {
323
- successiveACState = successiveACState === 2 ? 3 : 0;
324
- }
325
- }
326
- break;
327
- case 3: // set value for a zero item
328
- if (component.blockData[offsetZ]) {
329
- component.blockData[offsetZ] += sign * (readBit() << successive);
330
- } else {
331
- component.blockData[offsetZ] = successiveACNextValue << successive;
332
- successiveACState = 0;
333
- }
334
- break;
335
- case 4: // eob
336
- if (component.blockData[offsetZ]) {
337
- component.blockData[offsetZ] += sign * (readBit() << successive);
338
- }
339
- break;
340
- }
341
- k++;
342
- }
343
- if (successiveACState === 4) {
344
- eobrun--;
345
- if (eobrun === 0) {
346
- successiveACState = 0;
347
- }
348
- }
349
- }
350
-
351
- let blockRow = 0;
352
- function decodeMcu(component, decode, mcu, row, col) {
353
- const mcuRow = (mcu / mcusPerLine) | 0;
354
- const mcuCol = mcu % mcusPerLine;
355
- blockRow = mcuRow * component.v + row;
356
- const blockCol = mcuCol * component.h + col;
357
- const blockOffset = getBlockBufferOffset(component, blockRow, blockCol);
358
- decode(component, blockOffset);
359
- }
360
-
361
- function decodeBlock(component, decode, mcu) {
362
- blockRow = (mcu / component.blocksPerLine) | 0;
363
- const blockCol = mcu % component.blocksPerLine;
364
- const blockOffset = getBlockBufferOffset(component, blockRow, blockCol);
365
- decode(component, blockOffset);
366
- }
367
-
368
- const componentsLength = components.length;
369
- let component, i, j, k, n;
370
- let decodeFn;
371
- if (progressive) {
372
- if (spectralStart === 0) {
373
- decodeFn = successivePrev === 0 ? decodeDCFirst : decodeDCSuccessive;
374
- } else {
375
- decodeFn = successivePrev === 0 ? decodeACFirst : decodeACSuccessive;
376
- }
377
- } else {
378
- decodeFn = decodeBaseline;
379
- }
380
-
381
- let mcu = 0,
382
- fileMarker;
383
- let mcuExpected;
384
- if (componentsLength === 1) {
385
- mcuExpected = components[0].blocksPerLine * components[0].blocksPerColumn;
386
- } else {
387
- mcuExpected = mcusPerLine * frame.mcusPerColumn;
388
- }
389
-
390
- let h, v;
391
- while (mcu <= mcuExpected) {
392
- // reset interval stuff
393
- const mcuToRead = resetInterval
394
- ? Math.min(mcuExpected - mcu, resetInterval)
395
- : mcuExpected;
396
-
397
- // The `mcuToRead === 0` case should only occur when all of the expected
398
- // MCU data has been already parsed, i.e. when `mcu === mcuExpected`, but
399
- // some corrupt JPEG images contain more data than intended and we thus
400
- // want to skip over any extra RSTx markers below (fixes issue11794.pdf).
401
- if (mcuToRead > 0) {
402
- for (i = 0; i < componentsLength; i++) {
403
- components[i].pred = 0;
404
- }
405
- eobrun = 0;
406
-
407
- if (componentsLength === 1) {
408
- component = components[0];
409
- for (n = 0; n < mcuToRead; n++) {
410
- decodeBlock(component, decodeFn, mcu);
411
- mcu++;
412
- }
413
- } else {
414
- for (n = 0; n < mcuToRead; n++) {
415
- for (i = 0; i < componentsLength; i++) {
416
- component = components[i];
417
- h = component.h;
418
- v = component.v;
419
- for (j = 0; j < v; j++) {
420
- for (k = 0; k < h; k++) {
421
- decodeMcu(component, decodeFn, mcu, j, k);
422
- }
423
- }
424
- }
425
- mcu++;
426
- }
427
- }
428
- }
429
-
430
- // find marker
431
- bitsCount = 0;
432
- fileMarker = findNextFileMarker(data, offset);
433
- if (!fileMarker) {
434
- break; // Reached the end of the image data without finding any marker.
435
- }
436
- if (fileMarker.invalid) {
437
- // Some bad images seem to pad Scan blocks with e.g. zero bytes, skip
438
- // past those to attempt to find a valid marker (fixes issue4090.pdf).
439
- const partialMsg = mcuToRead > 0 ? "unexpected" : "excessive";
440
- warn(
441
- `decodeScan - ${partialMsg} MCU data, current marker is: ${fileMarker.invalid}`
442
- );
443
- offset = fileMarker.offset;
444
- }
445
- if (fileMarker.marker >= 0xffd0 && fileMarker.marker <= 0xffd7) {
446
- // RSTx
447
- offset += 2;
448
- } else {
449
- break;
450
- }
451
- }
452
-
453
- return offset - startOffset;
454
- }
455
-
456
- // A port of poppler's IDCT method which in turn is taken from:
457
- // Christoph Loeffler, Adriaan Ligtenberg, George S. Moschytz,
458
- // 'Practical Fast 1-D DCT Algorithms with 11 Multiplications',
459
- // IEEE Intl. Conf. on Acoustics, Speech & Signal Processing, 1989,
460
- // 988-991.
461
- function quantizeAndInverse(component, blockBufferOffset, p) {
462
- const qt = component.quantizationTable,
463
- blockData = component.blockData;
464
- let v0, v1, v2, v3, v4, v5, v6, v7;
465
- let p0, p1, p2, p3, p4, p5, p6, p7;
466
- let t;
467
-
468
- if (!qt) {
469
- throw new JpegError("missing required Quantization Table.");
470
- }
471
-
472
- // inverse DCT on rows
473
- for (let row = 0; row < 64; row += 8) {
474
- // gather block data
475
- p0 = blockData[blockBufferOffset + row];
476
- p1 = blockData[blockBufferOffset + row + 1];
477
- p2 = blockData[blockBufferOffset + row + 2];
478
- p3 = blockData[blockBufferOffset + row + 3];
479
- p4 = blockData[blockBufferOffset + row + 4];
480
- p5 = blockData[blockBufferOffset + row + 5];
481
- p6 = blockData[blockBufferOffset + row + 6];
482
- p7 = blockData[blockBufferOffset + row + 7];
483
-
484
- // dequant p0
485
- p0 *= qt[row];
486
-
487
- // check for all-zero AC coefficients
488
- if ((p1 | p2 | p3 | p4 | p5 | p6 | p7) === 0) {
489
- t = (dctSqrt2 * p0 + 512) >> 10;
490
- p[row] = t;
491
- p[row + 1] = t;
492
- p[row + 2] = t;
493
- p[row + 3] = t;
494
- p[row + 4] = t;
495
- p[row + 5] = t;
496
- p[row + 6] = t;
497
- p[row + 7] = t;
498
- continue;
499
- }
500
- // dequant p1 ... p7
501
- p1 *= qt[row + 1];
502
- p2 *= qt[row + 2];
503
- p3 *= qt[row + 3];
504
- p4 *= qt[row + 4];
505
- p5 *= qt[row + 5];
506
- p6 *= qt[row + 6];
507
- p7 *= qt[row + 7];
508
-
509
- // stage 4
510
- v0 = (dctSqrt2 * p0 + 128) >> 8;
511
- v1 = (dctSqrt2 * p4 + 128) >> 8;
512
- v2 = p2;
513
- v3 = p6;
514
- v4 = (dctSqrt1d2 * (p1 - p7) + 128) >> 8;
515
- v7 = (dctSqrt1d2 * (p1 + p7) + 128) >> 8;
516
- v5 = p3 << 4;
517
- v6 = p5 << 4;
518
-
519
- // stage 3
520
- v0 = (v0 + v1 + 1) >> 1;
521
- v1 = v0 - v1;
522
- t = (v2 * dctSin6 + v3 * dctCos6 + 128) >> 8;
523
- v2 = (v2 * dctCos6 - v3 * dctSin6 + 128) >> 8;
524
- v3 = t;
525
- v4 = (v4 + v6 + 1) >> 1;
526
- v6 = v4 - v6;
527
- v7 = (v7 + v5 + 1) >> 1;
528
- v5 = v7 - v5;
529
-
530
- // stage 2
531
- v0 = (v0 + v3 + 1) >> 1;
532
- v3 = v0 - v3;
533
- v1 = (v1 + v2 + 1) >> 1;
534
- v2 = v1 - v2;
535
- t = (v4 * dctSin3 + v7 * dctCos3 + 2048) >> 12;
536
- v4 = (v4 * dctCos3 - v7 * dctSin3 + 2048) >> 12;
537
- v7 = t;
538
- t = (v5 * dctSin1 + v6 * dctCos1 + 2048) >> 12;
539
- v5 = (v5 * dctCos1 - v6 * dctSin1 + 2048) >> 12;
540
- v6 = t;
541
-
542
- // stage 1
543
- p[row] = v0 + v7;
544
- p[row + 7] = v0 - v7;
545
- p[row + 1] = v1 + v6;
546
- p[row + 6] = v1 - v6;
547
- p[row + 2] = v2 + v5;
548
- p[row + 5] = v2 - v5;
549
- p[row + 3] = v3 + v4;
550
- p[row + 4] = v3 - v4;
551
- }
552
-
553
- // inverse DCT on columns
554
- for (let col = 0; col < 8; ++col) {
555
- p0 = p[col];
556
- p1 = p[col + 8];
557
- p2 = p[col + 16];
558
- p3 = p[col + 24];
559
- p4 = p[col + 32];
560
- p5 = p[col + 40];
561
- p6 = p[col + 48];
562
- p7 = p[col + 56];
563
-
564
- // check for all-zero AC coefficients
565
- if ((p1 | p2 | p3 | p4 | p5 | p6 | p7) === 0) {
566
- t = (dctSqrt2 * p0 + 8192) >> 14;
567
- // Convert to 8-bit.
568
- if (t < -2040) {
569
- t = 0;
570
- } else if (t >= 2024) {
571
- t = 255;
572
- } else {
573
- t = (t + 2056) >> 4;
574
- }
575
- blockData[blockBufferOffset + col] = t;
576
- blockData[blockBufferOffset + col + 8] = t;
577
- blockData[blockBufferOffset + col + 16] = t;
578
- blockData[blockBufferOffset + col + 24] = t;
579
- blockData[blockBufferOffset + col + 32] = t;
580
- blockData[blockBufferOffset + col + 40] = t;
581
- blockData[blockBufferOffset + col + 48] = t;
582
- blockData[blockBufferOffset + col + 56] = t;
583
- continue;
584
- }
585
-
586
- // stage 4
587
- v0 = (dctSqrt2 * p0 + 2048) >> 12;
588
- v1 = (dctSqrt2 * p4 + 2048) >> 12;
589
- v2 = p2;
590
- v3 = p6;
591
- v4 = (dctSqrt1d2 * (p1 - p7) + 2048) >> 12;
592
- v7 = (dctSqrt1d2 * (p1 + p7) + 2048) >> 12;
593
- v5 = p3;
594
- v6 = p5;
595
-
596
- // stage 3
597
- // Shift v0 by 128.5 << 5 here, so we don't need to shift p0...p7 when
598
- // converting to UInt8 range later.
599
- v0 = ((v0 + v1 + 1) >> 1) + 4112;
600
- v1 = v0 - v1;
601
- t = (v2 * dctSin6 + v3 * dctCos6 + 2048) >> 12;
602
- v2 = (v2 * dctCos6 - v3 * dctSin6 + 2048) >> 12;
603
- v3 = t;
604
- v4 = (v4 + v6 + 1) >> 1;
605
- v6 = v4 - v6;
606
- v7 = (v7 + v5 + 1) >> 1;
607
- v5 = v7 - v5;
608
-
609
- // stage 2
610
- v0 = (v0 + v3 + 1) >> 1;
611
- v3 = v0 - v3;
612
- v1 = (v1 + v2 + 1) >> 1;
613
- v2 = v1 - v2;
614
- t = (v4 * dctSin3 + v7 * dctCos3 + 2048) >> 12;
615
- v4 = (v4 * dctCos3 - v7 * dctSin3 + 2048) >> 12;
616
- v7 = t;
617
- t = (v5 * dctSin1 + v6 * dctCos1 + 2048) >> 12;
618
- v5 = (v5 * dctCos1 - v6 * dctSin1 + 2048) >> 12;
619
- v6 = t;
620
-
621
- // stage 1
622
- p0 = v0 + v7;
623
- p7 = v0 - v7;
624
- p1 = v1 + v6;
625
- p6 = v1 - v6;
626
- p2 = v2 + v5;
627
- p5 = v2 - v5;
628
- p3 = v3 + v4;
629
- p4 = v3 - v4;
630
-
631
- // Convert to 8-bit integers.
632
- if (p0 < 16) {
633
- p0 = 0;
634
- } else if (p0 >= 4080) {
635
- p0 = 255;
636
- } else {
637
- p0 >>= 4;
638
- }
639
- if (p1 < 16) {
640
- p1 = 0;
641
- } else if (p1 >= 4080) {
642
- p1 = 255;
643
- } else {
644
- p1 >>= 4;
645
- }
646
- if (p2 < 16) {
647
- p2 = 0;
648
- } else if (p2 >= 4080) {
649
- p2 = 255;
650
- } else {
651
- p2 >>= 4;
652
- }
653
- if (p3 < 16) {
654
- p3 = 0;
655
- } else if (p3 >= 4080) {
656
- p3 = 255;
657
- } else {
658
- p3 >>= 4;
659
- }
660
- if (p4 < 16) {
661
- p4 = 0;
662
- } else if (p4 >= 4080) {
663
- p4 = 255;
664
- } else {
665
- p4 >>= 4;
666
- }
667
- if (p5 < 16) {
668
- p5 = 0;
669
- } else if (p5 >= 4080) {
670
- p5 = 255;
671
- } else {
672
- p5 >>= 4;
673
- }
674
- if (p6 < 16) {
675
- p6 = 0;
676
- } else if (p6 >= 4080) {
677
- p6 = 255;
678
- } else {
679
- p6 >>= 4;
680
- }
681
- if (p7 < 16) {
682
- p7 = 0;
683
- } else if (p7 >= 4080) {
684
- p7 = 255;
685
- } else {
686
- p7 >>= 4;
687
- }
688
-
689
- // store block data
690
- blockData[blockBufferOffset + col] = p0;
691
- blockData[blockBufferOffset + col + 8] = p1;
692
- blockData[blockBufferOffset + col + 16] = p2;
693
- blockData[blockBufferOffset + col + 24] = p3;
694
- blockData[blockBufferOffset + col + 32] = p4;
695
- blockData[blockBufferOffset + col + 40] = p5;
696
- blockData[blockBufferOffset + col + 48] = p6;
697
- blockData[blockBufferOffset + col + 56] = p7;
698
- }
699
- }
700
-
701
- function buildComponentData(frame, component) {
702
- const blocksPerLine = component.blocksPerLine;
703
- const blocksPerColumn = component.blocksPerColumn;
704
- const computationBuffer = new Int16Array(64);
705
-
706
- for (let blockRow = 0; blockRow < blocksPerColumn; blockRow++) {
707
- for (let blockCol = 0; blockCol < blocksPerLine; blockCol++) {
708
- const offset = getBlockBufferOffset(component, blockRow, blockCol);
709
- quantizeAndInverse(component, offset, computationBuffer);
710
- }
711
- }
712
- return component.blockData;
713
- }
714
-
715
- function findNextFileMarker(data, currentPos, startPos = currentPos) {
716
- const maxPos = data.length - 1;
717
- let newPos = startPos < currentPos ? startPos : currentPos;
718
-
719
- if (currentPos >= maxPos) {
720
- return null; // Don't attempt to read non-existent data and just return.
721
- }
722
- const currentMarker = readUint16(data, currentPos);
723
- if (currentMarker >= 0xffc0 && currentMarker <= 0xfffe) {
724
- return {
725
- invalid: null,
726
- marker: currentMarker,
727
- offset: currentPos,
728
- };
729
- }
730
- let newMarker = readUint16(data, newPos);
731
- while (!(newMarker >= 0xffc0 && newMarker <= 0xfffe)) {
732
- if (++newPos >= maxPos) {
733
- return null; // Don't attempt to read non-existent data and just return.
734
- }
735
- newMarker = readUint16(data, newPos);
736
- }
737
- return {
738
- invalid: currentMarker.toString(16),
739
- marker: newMarker,
740
- offset: newPos,
741
- };
742
- }
743
-
744
- class JpegImage {
745
- constructor({ decodeTransform = null, colorTransform = -1 } = {}) {
746
- this._decodeTransform = decodeTransform;
747
- this._colorTransform = colorTransform;
748
- }
749
-
750
- parse(data, { dnlScanLines = null } = {}) {
751
- function readDataBlock() {
752
- const length = readUint16(data, offset);
753
- offset += 2;
754
- let endOffset = offset + length - 2;
755
-
756
- const fileMarker = findNextFileMarker(data, endOffset, offset);
757
- if (fileMarker && fileMarker.invalid) {
758
- warn(
759
- "readDataBlock - incorrect length, current marker is: " +
760
- fileMarker.invalid
761
- );
762
- endOffset = fileMarker.offset;
763
- }
764
-
765
- const array = data.subarray(offset, endOffset);
766
- offset += array.length;
767
- return array;
768
- }
769
-
770
- function prepareComponents(frame) {
771
- const mcusPerLine = Math.ceil(frame.samplesPerLine / 8 / frame.maxH);
772
- const mcusPerColumn = Math.ceil(frame.scanLines / 8 / frame.maxV);
773
- for (let i = 0, ii = frame.components.length; i < ii; i++) {
774
- const component = frame.components[i];
775
- const blocksPerLine = Math.ceil(
776
- (Math.ceil(frame.samplesPerLine / 8) * component.h) / frame.maxH
777
- );
778
- const blocksPerColumn = Math.ceil(
779
- (Math.ceil(frame.scanLines / 8) * component.v) / frame.maxV
780
- );
781
- const blocksPerLineForMcu = mcusPerLine * component.h;
782
- const blocksPerColumnForMcu = mcusPerColumn * component.v;
783
-
784
- const blocksBufferSize =
785
- 64 * blocksPerColumnForMcu * (blocksPerLineForMcu + 1);
786
- component.blockData = new Int16Array(blocksBufferSize);
787
- component.blocksPerLine = blocksPerLine;
788
- component.blocksPerColumn = blocksPerColumn;
789
- }
790
- frame.mcusPerLine = mcusPerLine;
791
- frame.mcusPerColumn = mcusPerColumn;
792
- }
793
-
794
- let offset = 0;
795
- let jfif = null;
796
- let adobe = null;
797
- let frame, resetInterval;
798
- let numSOSMarkers = 0;
799
- const quantizationTables = [];
800
- const huffmanTablesAC = [],
801
- huffmanTablesDC = [];
802
-
803
- let fileMarker = readUint16(data, offset);
804
- offset += 2;
805
- if (fileMarker !== /* SOI (Start of Image) = */ 0xffd8) {
806
- throw new JpegError("SOI not found");
807
- }
808
- fileMarker = readUint16(data, offset);
809
- offset += 2;
810
-
811
- markerLoop: while (fileMarker !== /* EOI (End of Image) = */ 0xffd9) {
812
- let i, j, l;
813
- switch (fileMarker) {
814
- case 0xffe0: // APP0 (Application Specific)
815
- case 0xffe1: // APP1
816
- case 0xffe2: // APP2
817
- case 0xffe3: // APP3
818
- case 0xffe4: // APP4
819
- case 0xffe5: // APP5
820
- case 0xffe6: // APP6
821
- case 0xffe7: // APP7
822
- case 0xffe8: // APP8
823
- case 0xffe9: // APP9
824
- case 0xffea: // APP10
825
- case 0xffeb: // APP11
826
- case 0xffec: // APP12
827
- case 0xffed: // APP13
828
- case 0xffee: // APP14
829
- case 0xffef: // APP15
830
- case 0xfffe: // COM (Comment)
831
- const appData = readDataBlock();
832
-
833
- if (fileMarker === 0xffe0) {
834
- // 'JFIF\x00'
835
- if (
836
- appData[0] === 0x4a &&
837
- appData[1] === 0x46 &&
838
- appData[2] === 0x49 &&
839
- appData[3] === 0x46 &&
840
- appData[4] === 0
841
- ) {
842
- jfif = {
843
- version: { major: appData[5], minor: appData[6] },
844
- densityUnits: appData[7],
845
- xDensity: (appData[8] << 8) | appData[9],
846
- yDensity: (appData[10] << 8) | appData[11],
847
- thumbWidth: appData[12],
848
- thumbHeight: appData[13],
849
- thumbData: appData.subarray(
850
- 14,
851
- 14 + 3 * appData[12] * appData[13]
852
- ),
853
- };
854
- }
855
- }
856
- // TODO APP1 - Exif
857
- if (fileMarker === 0xffee) {
858
- // 'Adobe'
859
- if (
860
- appData[0] === 0x41 &&
861
- appData[1] === 0x64 &&
862
- appData[2] === 0x6f &&
863
- appData[3] === 0x62 &&
864
- appData[4] === 0x65
865
- ) {
866
- adobe = {
867
- version: (appData[5] << 8) | appData[6],
868
- flags0: (appData[7] << 8) | appData[8],
869
- flags1: (appData[9] << 8) | appData[10],
870
- transformCode: appData[11],
871
- };
872
- }
873
- }
874
- break;
875
-
876
- case 0xffdb: // DQT (Define Quantization Tables)
877
- const quantizationTablesLength = readUint16(data, offset);
878
- offset += 2;
879
- const quantizationTablesEnd = quantizationTablesLength + offset - 2;
880
- let z;
881
- while (offset < quantizationTablesEnd) {
882
- const quantizationTableSpec = data[offset++];
883
- const tableData = new Uint16Array(64);
884
- if (quantizationTableSpec >> 4 === 0) {
885
- // 8 bit values
886
- for (j = 0; j < 64; j++) {
887
- z = dctZigZag[j];
888
- tableData[z] = data[offset++];
889
- }
890
- } else if (quantizationTableSpec >> 4 === 1) {
891
- // 16 bit values
892
- for (j = 0; j < 64; j++) {
893
- z = dctZigZag[j];
894
- tableData[z] = readUint16(data, offset);
895
- offset += 2;
896
- }
897
- } else {
898
- throw new JpegError("DQT - invalid table spec");
899
- }
900
- quantizationTables[quantizationTableSpec & 15] = tableData;
901
- }
902
- break;
903
-
904
- case 0xffc0: // SOF0 (Start of Frame, Baseline DCT)
905
- case 0xffc1: // SOF1 (Start of Frame, Extended DCT)
906
- case 0xffc2: // SOF2 (Start of Frame, Progressive DCT)
907
- if (frame) {
908
- throw new JpegError("Only single frame JPEGs supported");
909
- }
910
- offset += 2; // Skip marker length.
911
-
912
- frame = {};
913
- frame.extended = fileMarker === 0xffc1;
914
- frame.progressive = fileMarker === 0xffc2;
915
- frame.precision = data[offset++];
916
- const sofScanLines = readUint16(data, offset);
917
- offset += 2;
918
- frame.scanLines = dnlScanLines || sofScanLines;
919
- frame.samplesPerLine = readUint16(data, offset);
920
- offset += 2;
921
- frame.components = [];
922
- frame.componentIds = {};
923
- const componentsCount = data[offset++];
924
- let maxH = 0,
925
- maxV = 0;
926
- for (i = 0; i < componentsCount; i++) {
927
- const componentId = data[offset];
928
- const h = data[offset + 1] >> 4;
929
- const v = data[offset + 1] & 15;
930
- if (maxH < h) {
931
- maxH = h;
932
- }
933
- if (maxV < v) {
934
- maxV = v;
935
- }
936
- const qId = data[offset + 2];
937
- l = frame.components.push({
938
- h,
939
- v,
940
- quantizationId: qId,
941
- quantizationTable: null, // See comment below.
942
- });
943
- frame.componentIds[componentId] = l - 1;
944
- offset += 3;
945
- }
946
- frame.maxH = maxH;
947
- frame.maxV = maxV;
948
- prepareComponents(frame);
949
- break;
950
-
951
- case 0xffc4: // DHT (Define Huffman Tables)
952
- const huffmanLength = readUint16(data, offset);
953
- offset += 2;
954
- for (i = 2; i < huffmanLength; ) {
955
- const huffmanTableSpec = data[offset++];
956
- const codeLengths = new Uint8Array(16);
957
- let codeLengthSum = 0;
958
- for (j = 0; j < 16; j++, offset++) {
959
- codeLengthSum += codeLengths[j] = data[offset];
960
- }
961
- const huffmanValues = new Uint8Array(codeLengthSum);
962
- for (j = 0; j < codeLengthSum; j++, offset++) {
963
- huffmanValues[j] = data[offset];
964
- }
965
- i += 17 + codeLengthSum;
966
-
967
- (huffmanTableSpec >> 4 === 0 ? huffmanTablesDC : huffmanTablesAC)[
968
- huffmanTableSpec & 15
969
- ] = buildHuffmanTable(codeLengths, huffmanValues);
970
- }
971
- break;
972
-
973
- case 0xffdd: // DRI (Define Restart Interval)
974
- offset += 2; // Skip marker length.
975
-
976
- resetInterval = readUint16(data, offset);
977
- offset += 2;
978
- break;
979
-
980
- case 0xffda: // SOS (Start of Scan)
981
- // A DNL marker (0xFFDC), if it exists, is only allowed at the end
982
- // of the first scan segment and may only occur once in an image.
983
- // Furthermore, to prevent an infinite loop, do *not* attempt to
984
- // parse DNL markers during re-parsing of the JPEG scan data.
985
- const parseDNLMarker = ++numSOSMarkers === 1 && !dnlScanLines;
986
-
987
- offset += 2; // Skip marker length.
988
-
989
- const selectorsCount = data[offset++],
990
- components = [];
991
- for (i = 0; i < selectorsCount; i++) {
992
- const index = data[offset++];
993
- const componentIndex = frame.componentIds[index];
994
- const component = frame.components[componentIndex];
995
- component.index = index;
996
- const tableSpec = data[offset++];
997
- component.huffmanTableDC = huffmanTablesDC[tableSpec >> 4];
998
- component.huffmanTableAC = huffmanTablesAC[tableSpec & 15];
999
- components.push(component);
1000
- }
1001
- const spectralStart = data[offset++],
1002
- spectralEnd = data[offset++],
1003
- successiveApproximation = data[offset++];
1004
- try {
1005
- const processed = decodeScan(
1006
- data,
1007
- offset,
1008
- frame,
1009
- components,
1010
- resetInterval,
1011
- spectralStart,
1012
- spectralEnd,
1013
- successiveApproximation >> 4,
1014
- successiveApproximation & 15,
1015
- parseDNLMarker
1016
- );
1017
- offset += processed;
1018
- } catch (ex) {
1019
- if (ex instanceof DNLMarkerError) {
1020
- warn(`${ex.message} -- attempting to re-parse the JPEG image.`);
1021
- return this.parse(data, { dnlScanLines: ex.scanLines });
1022
- } else if (ex instanceof EOIMarkerError) {
1023
- warn(`${ex.message} -- ignoring the rest of the image data.`);
1024
- break markerLoop;
1025
- }
1026
- throw ex;
1027
- }
1028
- break;
1029
-
1030
- case 0xffdc: // DNL (Define Number of Lines)
1031
- // Ignore the marker, since it's being handled in `decodeScan`.
1032
- offset += 4;
1033
- break;
1034
-
1035
- case 0xffff: // Fill bytes
1036
- if (data[offset] !== 0xff) {
1037
- // Avoid skipping a valid marker.
1038
- offset--;
1039
- }
1040
- break;
1041
-
1042
- default:
1043
- // Could be incorrect encoding -- the last 0xFF byte of the previous
1044
- // block could have been eaten by the encoder, hence we fallback to
1045
- // `startPos = offset - 3` when looking for the next valid marker.
1046
- const nextFileMarker = findNextFileMarker(
1047
- data,
1048
- /* currentPos = */ offset - 2,
1049
- /* startPos = */ offset - 3
1050
- );
1051
- if (nextFileMarker && nextFileMarker.invalid) {
1052
- warn(
1053
- "JpegImage.parse - unexpected data, current marker is: " +
1054
- nextFileMarker.invalid
1055
- );
1056
- offset = nextFileMarker.offset;
1057
- break;
1058
- }
1059
- if (!nextFileMarker || offset >= data.length - 1) {
1060
- warn(
1061
- "JpegImage.parse - reached the end of the image data " +
1062
- "without finding an EOI marker (0xFFD9)."
1063
- );
1064
- break markerLoop;
1065
- }
1066
- throw new JpegError(
1067
- "JpegImage.parse - unknown marker: " + fileMarker.toString(16)
1068
- );
1069
- }
1070
- fileMarker = readUint16(data, offset);
1071
- offset += 2;
1072
- }
1073
-
1074
- this.width = frame.samplesPerLine;
1075
- this.height = frame.scanLines;
1076
- this.jfif = jfif;
1077
- this.adobe = adobe;
1078
- this.components = [];
1079
- for (let i = 0, ii = frame.components.length; i < ii; i++) {
1080
- const component = frame.components[i];
1081
-
1082
- // Prevent errors when DQT markers are placed after SOF{n} markers,
1083
- // by assigning the `quantizationTable` entry after the entire image
1084
- // has been parsed (fixes issue7406.pdf).
1085
- const quantizationTable = quantizationTables[component.quantizationId];
1086
- if (quantizationTable) {
1087
- component.quantizationTable = quantizationTable;
1088
- }
1089
-
1090
- this.components.push({
1091
- index: component.index,
1092
- output: buildComponentData(frame, component),
1093
- scaleX: component.h / frame.maxH,
1094
- scaleY: component.v / frame.maxV,
1095
- blocksPerLine: component.blocksPerLine,
1096
- blocksPerColumn: component.blocksPerColumn,
1097
- });
1098
- }
1099
- this.numComponents = this.components.length;
1100
- return undefined;
1101
- }
1102
-
1103
- _getLinearizedBlockData(width, height, isSourcePDF = false) {
1104
- const scaleX = this.width / width,
1105
- scaleY = this.height / height;
1106
-
1107
- let component, componentScaleX, componentScaleY, blocksPerScanline;
1108
- let x, y, i, j, k;
1109
- let index;
1110
- let offset = 0;
1111
- let output;
1112
- const numComponents = this.components.length;
1113
- const dataLength = width * height * numComponents;
1114
- const data = new Uint8ClampedArray(dataLength);
1115
- const xScaleBlockOffset = new Uint32Array(width);
1116
- const mask3LSB = 0xfffffff8; // used to clear the 3 LSBs
1117
- let lastComponentScaleX;
1118
-
1119
- for (i = 0; i < numComponents; i++) {
1120
- component = this.components[i];
1121
- componentScaleX = component.scaleX * scaleX;
1122
- componentScaleY = component.scaleY * scaleY;
1123
- offset = i;
1124
- output = component.output;
1125
- blocksPerScanline = (component.blocksPerLine + 1) << 3;
1126
- // Precalculate the `xScaleBlockOffset`. Since it doesn't depend on the
1127
- // component data, that's only necessary when `componentScaleX` changes.
1128
- if (componentScaleX !== lastComponentScaleX) {
1129
- for (x = 0; x < width; x++) {
1130
- j = 0 | (x * componentScaleX);
1131
- xScaleBlockOffset[x] = ((j & mask3LSB) << 3) | (j & 7);
1132
- }
1133
- lastComponentScaleX = componentScaleX;
1134
- }
1135
- // linearize the blocks of the component
1136
- for (y = 0; y < height; y++) {
1137
- j = 0 | (y * componentScaleY);
1138
- index = (blocksPerScanline * (j & mask3LSB)) | ((j & 7) << 3);
1139
- for (x = 0; x < width; x++) {
1140
- data[offset] = output[index + xScaleBlockOffset[x]];
1141
- offset += numComponents;
1142
- }
1143
- }
1144
- }
1145
-
1146
- // decodeTransform contains pairs of multiplier (-256..256) and additive
1147
- let transform = this._decodeTransform;
1148
-
1149
- // In PDF files, JPEG images with CMYK colour spaces are usually inverted
1150
- // (this can be observed by extracting the raw image data).
1151
- // Since the conversion algorithms (see below) were written primarily for
1152
- // the PDF use-cases, attempting to use `JpegImage` to parse standalone
1153
- // JPEG (CMYK) images may thus result in inverted images (see issue 9513).
1154
- //
1155
- // Unfortunately it's not (always) possible to tell, from the image data
1156
- // alone, if it needs to be inverted. Thus in an attempt to provide better
1157
- // out-of-box behaviour when `JpegImage` is used standalone, default to
1158
- // inverting JPEG (CMYK) images if and only if the image data does *not*
1159
- // come from a PDF file and no `decodeTransform` was passed by the user.
1160
- if (!isSourcePDF && numComponents === 4 && !transform) {
1161
- transform = new Int32Array([-256, 255, -256, 255, -256, 255, -256, 255]);
1162
- }
1163
-
1164
- if (transform) {
1165
- for (i = 0; i < dataLength; ) {
1166
- for (j = 0, k = 0; j < numComponents; j++, i++, k += 2) {
1167
- data[i] = ((data[i] * transform[k]) >> 8) + transform[k + 1];
1168
- }
1169
- }
1170
- }
1171
- return data;
1172
- }
1173
-
1174
- get _isColorConversionNeeded() {
1175
- if (this.adobe) {
1176
- // The adobe transform marker overrides any previous setting.
1177
- return !!this.adobe.transformCode;
1178
- }
1179
- if (this.numComponents === 3) {
1180
- if (this._colorTransform === 0) {
1181
- // If the Adobe transform marker is not present and the image
1182
- // dictionary has a 'ColorTransform' entry, explicitly set to `0`,
1183
- // then the colours should *not* be transformed.
1184
- return false;
1185
- } else if (
1186
- this.components[0].index === /* "R" = */ 0x52 &&
1187
- this.components[1].index === /* "G" = */ 0x47 &&
1188
- this.components[2].index === /* "B" = */ 0x42
1189
- ) {
1190
- // If the three components are indexed as RGB in ASCII
1191
- // then the colours should *not* be transformed.
1192
- return false;
1193
- }
1194
- return true;
1195
- }
1196
- // `this.numComponents !== 3`
1197
- if (this._colorTransform === 1) {
1198
- // If the Adobe transform marker is not present and the image
1199
- // dictionary has a 'ColorTransform' entry, explicitly set to `1`,
1200
- // then the colours should be transformed.
1201
- return true;
1202
- }
1203
- return false;
1204
- }
1205
-
1206
- _convertYccToRgb(data) {
1207
- let Y, Cb, Cr;
1208
- for (let i = 0, length = data.length; i < length; i += 3) {
1209
- Y = data[i];
1210
- Cb = data[i + 1];
1211
- Cr = data[i + 2];
1212
- data[i] = Y - 179.456 + 1.402 * Cr;
1213
- data[i + 1] = Y + 135.459 - 0.344 * Cb - 0.714 * Cr;
1214
- data[i + 2] = Y - 226.816 + 1.772 * Cb;
1215
- }
1216
- return data;
1217
- }
1218
-
1219
- _convertYcckToRgb(data) {
1220
- let Y, Cb, Cr, k;
1221
- let offset = 0;
1222
- for (let i = 0, length = data.length; i < length; i += 4) {
1223
- Y = data[i];
1224
- Cb = data[i + 1];
1225
- Cr = data[i + 2];
1226
- k = data[i + 3];
1227
-
1228
- data[offset++] =
1229
- -122.67195406894 +
1230
- Cb *
1231
- (-6.60635669420364e-5 * Cb +
1232
- 0.000437130475926232 * Cr -
1233
- 5.4080610064599e-5 * Y +
1234
- 0.00048449797120281 * k -
1235
- 0.154362151871126) +
1236
- Cr *
1237
- (-0.000957964378445773 * Cr +
1238
- 0.000817076911346625 * Y -
1239
- 0.00477271405408747 * k +
1240
- 1.53380253221734) +
1241
- Y *
1242
- (0.000961250184130688 * Y -
1243
- 0.00266257332283933 * k +
1244
- 0.48357088451265) +
1245
- k * (-0.000336197177618394 * k + 0.484791561490776);
1246
-
1247
- data[offset++] =
1248
- 107.268039397724 +
1249
- Cb *
1250
- (2.19927104525741e-5 * Cb -
1251
- 0.000640992018297945 * Cr +
1252
- 0.000659397001245577 * Y +
1253
- 0.000426105652938837 * k -
1254
- 0.176491792462875) +
1255
- Cr *
1256
- (-0.000778269941513683 * Cr +
1257
- 0.00130872261408275 * Y +
1258
- 0.000770482631801132 * k -
1259
- 0.151051492775562) +
1260
- Y *
1261
- (0.00126935368114843 * Y -
1262
- 0.00265090189010898 * k +
1263
- 0.25802910206845) +
1264
- k * (-0.000318913117588328 * k - 0.213742400323665);
1265
-
1266
- data[offset++] =
1267
- -20.810012546947 +
1268
- Cb *
1269
- (-0.000570115196973677 * Cb -
1270
- 2.63409051004589e-5 * Cr +
1271
- 0.0020741088115012 * Y -
1272
- 0.00288260236853442 * k +
1273
- 0.814272968359295) +
1274
- Cr *
1275
- (-1.53496057440975e-5 * Cr -
1276
- 0.000132689043961446 * Y +
1277
- 0.000560833691242812 * k -
1278
- 0.195152027534049) +
1279
- Y *
1280
- (0.00174418132927582 * Y -
1281
- 0.00255243321439347 * k +
1282
- 0.116935020465145) +
1283
- k * (-0.000343531996510555 * k + 0.24165260232407);
1284
- }
1285
- // Ensure that only the converted RGB data is returned.
1286
- return data.subarray(0, offset);
1287
- }
1288
-
1289
- _convertYcckToCmyk(data) {
1290
- let Y, Cb, Cr;
1291
- for (let i = 0, length = data.length; i < length; i += 4) {
1292
- Y = data[i];
1293
- Cb = data[i + 1];
1294
- Cr = data[i + 2];
1295
- data[i] = 434.456 - Y - 1.402 * Cr;
1296
- data[i + 1] = 119.541 - Y + 0.344 * Cb + 0.714 * Cr;
1297
- data[i + 2] = 481.816 - Y - 1.772 * Cb;
1298
- // K in data[i + 3] is unchanged
1299
- }
1300
- return data;
1301
- }
1302
-
1303
- _convertCmykToRgb(data) {
1304
- let c, m, y, k;
1305
- let offset = 0;
1306
- for (let i = 0, length = data.length; i < length; i += 4) {
1307
- c = data[i];
1308
- m = data[i + 1];
1309
- y = data[i + 2];
1310
- k = data[i + 3];
1311
-
1312
- data[offset++] =
1313
- 255 +
1314
- c *
1315
- (-0.00006747147073602441 * c +
1316
- 0.0008379262121013727 * m +
1317
- 0.0002894718188643294 * y +
1318
- 0.003264231057537806 * k -
1319
- 1.1185611867203937) +
1320
- m *
1321
- (0.000026374107616089405 * m -
1322
- 0.00008626949158638572 * y -
1323
- 0.0002748769067499491 * k -
1324
- 0.02155688794978967) +
1325
- y *
1326
- (-0.00003878099212869363 * y -
1327
- 0.0003267808279485286 * k +
1328
- 0.0686742238595345) -
1329
- k * (0.0003361971776183937 * k + 0.7430659151342254);
1330
-
1331
- data[offset++] =
1332
- 255 +
1333
- c *
1334
- (0.00013596372813588848 * c +
1335
- 0.000924537132573585 * m +
1336
- 0.00010567359618683593 * y +
1337
- 0.0004791864687436512 * k -
1338
- 0.3109689587515875) +
1339
- m *
1340
- (-0.00023545346108370344 * m +
1341
- 0.0002702845253534714 * y +
1342
- 0.0020200308977307156 * k -
1343
- 0.7488052167015494) +
1344
- y *
1345
- (0.00006834815998235662 * y +
1346
- 0.00015168452363460973 * k -
1347
- 0.09751927774728933) -
1348
- k * (0.00031891311758832814 * k + 0.7364883807733168);
1349
-
1350
- data[offset++] =
1351
- 255 +
1352
- c *
1353
- (0.000013598650411385307 * c +
1354
- 0.00012423956175490851 * m +
1355
- 0.0004751985097583589 * y -
1356
- 0.0000036729317476630422 * k -
1357
- 0.05562186980264034) +
1358
- m *
1359
- (0.00016141380598724676 * m +
1360
- 0.0009692239130725186 * y +
1361
- 0.0007782692450036253 * k -
1362
- 0.44015232367526463) +
1363
- y *
1364
- (5.068882914068769e-7 * y +
1365
- 0.0017778369011375071 * k -
1366
- 0.7591454649749609) -
1367
- k * (0.0003435319965105553 * k + 0.7063770186160144);
1368
- }
1369
- // Ensure that only the converted RGB data is returned.
1370
- return data.subarray(0, offset);
1371
- }
1372
-
1373
- getData({ width, height, forceRGB = false, isSourcePDF = false }) {
1374
- if (
1375
- typeof PDFJSDev === "undefined" ||
1376
- PDFJSDev.test("!PRODUCTION || TESTING")
1377
- ) {
1378
- assert(
1379
- isSourcePDF === true,
1380
- 'JpegImage.getData: Unexpected "isSourcePDF" value for PDF files.'
1381
- );
1382
- }
1383
- if (this.numComponents > 4) {
1384
- throw new JpegError("Unsupported color mode");
1385
- }
1386
- // Type of data: Uint8ClampedArray(width * height * numComponents)
1387
- const data = this._getLinearizedBlockData(width, height, isSourcePDF);
1388
-
1389
- if (this.numComponents === 1 && forceRGB) {
1390
- const dataLength = data.length;
1391
- const rgbData = new Uint8ClampedArray(dataLength * 3);
1392
- let offset = 0;
1393
- for (let i = 0; i < dataLength; i++) {
1394
- const grayColor = data[i];
1395
- rgbData[offset++] = grayColor;
1396
- rgbData[offset++] = grayColor;
1397
- rgbData[offset++] = grayColor;
1398
- }
1399
- return rgbData;
1400
- } else if (this.numComponents === 3 && this._isColorConversionNeeded) {
1401
- return this._convertYccToRgb(data);
1402
- } else if (this.numComponents === 4) {
1403
- if (this._isColorConversionNeeded) {
1404
- if (forceRGB) {
1405
- return this._convertYcckToRgb(data);
1406
- }
1407
- return this._convertYcckToCmyk(data);
1408
- } else if (forceRGB) {
1409
- return this._convertCmykToRgb(data);
1410
- }
1411
- }
1412
- return data;
1413
- }
1414
- }
1415
-
1416
- export { JpegImage };