@jimp/jpeg-js 0.0.0-stage → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +129 -0
- package/LICENSE-APACHE-2.0 +202 -0
- package/NOTICE +27 -0
- package/README.ijg +260 -0
- package/README.md +261 -2
- package/dist/browser/compat.d.ts +44 -0
- package/dist/browser/compat.d.ts.map +1 -0
- package/dist/browser/compat.js +42 -0
- package/dist/browser/compat.js.map +1 -0
- package/dist/browser/decode.d.ts +73 -0
- package/dist/browser/decode.d.ts.map +1 -0
- package/dist/browser/decode.js +82 -0
- package/dist/browser/decode.js.map +1 -0
- package/dist/browser/decoder/arithmetic.d.ts +66 -0
- package/dist/browser/decoder/arithmetic.d.ts.map +1 -0
- package/dist/browser/decoder/arithmetic.js +374 -0
- package/dist/browser/decoder/arithmetic.js.map +1 -0
- package/dist/browser/decoder/decoder.d.ts +87 -0
- package/dist/browser/decoder/decoder.d.ts.map +1 -0
- package/dist/browser/decoder/decoder.js +869 -0
- package/dist/browser/decoder/decoder.js.map +1 -0
- package/dist/browser/decoder/huffman.d.ts +64 -0
- package/dist/browser/decoder/huffman.d.ts.map +1 -0
- package/dist/browser/decoder/huffman.js +260 -0
- package/dist/browser/decoder/huffman.js.map +1 -0
- package/dist/browser/decoder/idct.d.ts +20 -0
- package/dist/browser/decoder/idct.d.ts.map +1 -0
- package/dist/browser/decoder/idct.js +222 -0
- package/dist/browser/decoder/idct.js.map +1 -0
- package/dist/browser/decoder/output.d.ts +17 -0
- package/dist/browser/decoder/output.d.ts.map +1 -0
- package/dist/browser/decoder/output.js +351 -0
- package/dist/browser/decoder/output.js.map +1 -0
- package/dist/browser/decoder/scan-tables.d.ts +7 -0
- package/dist/browser/decoder/scan-tables.d.ts.map +1 -0
- package/dist/browser/decoder/scan-tables.js +13 -0
- package/dist/browser/decoder/scan-tables.js.map +1 -0
- package/dist/browser/decoder/scan.d.ts +57 -0
- package/dist/browser/decoder/scan.d.ts.map +1 -0
- package/dist/browser/decoder/scan.js +381 -0
- package/dist/browser/decoder/scan.js.map +1 -0
- package/dist/browser/decoder/smoothing.d.ts +28 -0
- package/dist/browser/decoder/smoothing.d.ts.map +1 -0
- package/dist/browser/decoder/smoothing.js +345 -0
- package/dist/browser/decoder/smoothing.js.map +1 -0
- package/dist/browser/decoder/standard-tables.d.ts +19 -0
- package/dist/browser/decoder/standard-tables.d.ts.map +1 -0
- package/dist/browser/decoder/standard-tables.js +50 -0
- package/dist/browser/decoder/standard-tables.js.map +1 -0
- package/dist/browser/encode.d.ts +57 -0
- package/dist/browser/encode.d.ts.map +1 -0
- package/dist/browser/encode.js +57 -0
- package/dist/browser/encode.js.map +1 -0
- package/dist/browser/encoder/encoder.d.ts +65 -0
- package/dist/browser/encoder/encoder.d.ts.map +1 -0
- package/dist/browser/encoder/encoder.js +713 -0
- package/dist/browser/encoder/encoder.js.map +1 -0
- package/dist/browser/encoder/huffman.d.ts +19 -0
- package/dist/browser/encoder/huffman.d.ts.map +1 -0
- package/dist/browser/encoder/huffman.js +135 -0
- package/dist/browser/encoder/huffman.js.map +1 -0
- package/dist/browser/encoder/progressive.d.ts +20 -0
- package/dist/browser/encoder/progressive.d.ts.map +1 -0
- package/dist/browser/encoder/progressive.js +315 -0
- package/dist/browser/encoder/progressive.js.map +1 -0
- package/dist/browser/encoder/writer.d.ts +18 -0
- package/dist/browser/encoder/writer.d.ts.map +1 -0
- package/dist/browser/encoder/writer.js +71 -0
- package/dist/browser/encoder/writer.js.map +1 -0
- package/dist/browser/errors.d.ts +27 -0
- package/dist/browser/errors.d.ts.map +1 -0
- package/dist/browser/errors.js +28 -0
- package/dist/browser/errors.js.map +1 -0
- package/dist/browser/index.d.ts +12 -0
- package/dist/browser/index.d.ts.map +1 -0
- package/dist/browser/index.js +8 -0
- package/dist/browser/index.js.map +1 -0
- package/dist/browser/package.json +3 -0
- package/dist/commonjs/compat.d.ts +44 -0
- package/dist/commonjs/compat.d.ts.map +1 -0
- package/dist/commonjs/compat.js +46 -0
- package/dist/commonjs/compat.js.map +1 -0
- package/dist/commonjs/decode.d.ts +73 -0
- package/dist/commonjs/decode.d.ts.map +1 -0
- package/dist/commonjs/decode.js +86 -0
- package/dist/commonjs/decode.js.map +1 -0
- package/dist/commonjs/decoder/arithmetic.d.ts +66 -0
- package/dist/commonjs/decoder/arithmetic.d.ts.map +1 -0
- package/dist/commonjs/decoder/arithmetic.js +380 -0
- package/dist/commonjs/decoder/arithmetic.js.map +1 -0
- package/dist/commonjs/decoder/decoder.d.ts +87 -0
- package/dist/commonjs/decoder/decoder.d.ts.map +1 -0
- package/dist/commonjs/decoder/decoder.js +873 -0
- package/dist/commonjs/decoder/decoder.js.map +1 -0
- package/dist/commonjs/decoder/huffman.d.ts +64 -0
- package/dist/commonjs/decoder/huffman.d.ts.map +1 -0
- package/dist/commonjs/decoder/huffman.js +265 -0
- package/dist/commonjs/decoder/huffman.js.map +1 -0
- package/dist/commonjs/decoder/idct.d.ts +20 -0
- package/dist/commonjs/decoder/idct.d.ts.map +1 -0
- package/dist/commonjs/decoder/idct.js +226 -0
- package/dist/commonjs/decoder/idct.js.map +1 -0
- package/dist/commonjs/decoder/output.d.ts +17 -0
- package/dist/commonjs/decoder/output.d.ts.map +1 -0
- package/dist/commonjs/decoder/output.js +354 -0
- package/dist/commonjs/decoder/output.js.map +1 -0
- package/dist/commonjs/decoder/scan-tables.d.ts +7 -0
- package/dist/commonjs/decoder/scan-tables.d.ts.map +1 -0
- package/dist/commonjs/decoder/scan-tables.js +16 -0
- package/dist/commonjs/decoder/scan-tables.js.map +1 -0
- package/dist/commonjs/decoder/scan.d.ts +57 -0
- package/dist/commonjs/decoder/scan.d.ts.map +1 -0
- package/dist/commonjs/decoder/scan.js +385 -0
- package/dist/commonjs/decoder/scan.js.map +1 -0
- package/dist/commonjs/decoder/smoothing.d.ts +28 -0
- package/dist/commonjs/decoder/smoothing.d.ts.map +1 -0
- package/dist/commonjs/decoder/smoothing.js +349 -0
- package/dist/commonjs/decoder/smoothing.js.map +1 -0
- package/dist/commonjs/decoder/standard-tables.d.ts +19 -0
- package/dist/commonjs/decoder/standard-tables.d.ts.map +1 -0
- package/dist/commonjs/decoder/standard-tables.js +53 -0
- package/dist/commonjs/decoder/standard-tables.js.map +1 -0
- package/dist/commonjs/encode.d.ts +57 -0
- package/dist/commonjs/encode.d.ts.map +1 -0
- package/dist/commonjs/encode.js +60 -0
- package/dist/commonjs/encode.js.map +1 -0
- package/dist/commonjs/encoder/encoder.d.ts +65 -0
- package/dist/commonjs/encoder/encoder.d.ts.map +1 -0
- package/dist/commonjs/encoder/encoder.js +720 -0
- package/dist/commonjs/encoder/encoder.js.map +1 -0
- package/dist/commonjs/encoder/huffman.d.ts +19 -0
- package/dist/commonjs/encoder/huffman.d.ts.map +1 -0
- package/dist/commonjs/encoder/huffman.js +139 -0
- package/dist/commonjs/encoder/huffman.js.map +1 -0
- package/dist/commonjs/encoder/progressive.d.ts +20 -0
- package/dist/commonjs/encoder/progressive.d.ts.map +1 -0
- package/dist/commonjs/encoder/progressive.js +318 -0
- package/dist/commonjs/encoder/progressive.js.map +1 -0
- package/dist/commonjs/encoder/writer.d.ts +18 -0
- package/dist/commonjs/encoder/writer.d.ts.map +1 -0
- package/dist/commonjs/encoder/writer.js +75 -0
- package/dist/commonjs/encoder/writer.js.map +1 -0
- package/dist/commonjs/errors.d.ts +27 -0
- package/dist/commonjs/errors.d.ts.map +1 -0
- package/dist/commonjs/errors.js +35 -0
- package/dist/commonjs/errors.js.map +1 -0
- package/dist/commonjs/index.d.ts +12 -0
- package/dist/commonjs/index.d.ts.map +1 -0
- package/dist/commonjs/index.js +19 -0
- package/dist/commonjs/index.js.map +1 -0
- package/dist/commonjs/package.json +3 -0
- package/dist/esm/compat.d.ts +44 -0
- package/dist/esm/compat.d.ts.map +1 -0
- package/dist/esm/compat.js +42 -0
- package/dist/esm/compat.js.map +1 -0
- package/dist/esm/decode.d.ts +73 -0
- package/dist/esm/decode.d.ts.map +1 -0
- package/dist/esm/decode.js +82 -0
- package/dist/esm/decode.js.map +1 -0
- package/dist/esm/decoder/arithmetic.d.ts +66 -0
- package/dist/esm/decoder/arithmetic.d.ts.map +1 -0
- package/dist/esm/decoder/arithmetic.js +374 -0
- package/dist/esm/decoder/arithmetic.js.map +1 -0
- package/dist/esm/decoder/decoder.d.ts +87 -0
- package/dist/esm/decoder/decoder.d.ts.map +1 -0
- package/dist/esm/decoder/decoder.js +869 -0
- package/dist/esm/decoder/decoder.js.map +1 -0
- package/dist/esm/decoder/huffman.d.ts +64 -0
- package/dist/esm/decoder/huffman.d.ts.map +1 -0
- package/dist/esm/decoder/huffman.js +260 -0
- package/dist/esm/decoder/huffman.js.map +1 -0
- package/dist/esm/decoder/idct.d.ts +20 -0
- package/dist/esm/decoder/idct.d.ts.map +1 -0
- package/dist/esm/decoder/idct.js +222 -0
- package/dist/esm/decoder/idct.js.map +1 -0
- package/dist/esm/decoder/output.d.ts +17 -0
- package/dist/esm/decoder/output.d.ts.map +1 -0
- package/dist/esm/decoder/output.js +351 -0
- package/dist/esm/decoder/output.js.map +1 -0
- package/dist/esm/decoder/scan-tables.d.ts +7 -0
- package/dist/esm/decoder/scan-tables.d.ts.map +1 -0
- package/dist/esm/decoder/scan-tables.js +13 -0
- package/dist/esm/decoder/scan-tables.js.map +1 -0
- package/dist/esm/decoder/scan.d.ts +57 -0
- package/dist/esm/decoder/scan.d.ts.map +1 -0
- package/dist/esm/decoder/scan.js +381 -0
- package/dist/esm/decoder/scan.js.map +1 -0
- package/dist/esm/decoder/smoothing.d.ts +28 -0
- package/dist/esm/decoder/smoothing.d.ts.map +1 -0
- package/dist/esm/decoder/smoothing.js +345 -0
- package/dist/esm/decoder/smoothing.js.map +1 -0
- package/dist/esm/decoder/standard-tables.d.ts +19 -0
- package/dist/esm/decoder/standard-tables.d.ts.map +1 -0
- package/dist/esm/decoder/standard-tables.js +50 -0
- package/dist/esm/decoder/standard-tables.js.map +1 -0
- package/dist/esm/encode.d.ts +57 -0
- package/dist/esm/encode.d.ts.map +1 -0
- package/dist/esm/encode.js +57 -0
- package/dist/esm/encode.js.map +1 -0
- package/dist/esm/encoder/encoder.d.ts +65 -0
- package/dist/esm/encoder/encoder.d.ts.map +1 -0
- package/dist/esm/encoder/encoder.js +713 -0
- package/dist/esm/encoder/encoder.js.map +1 -0
- package/dist/esm/encoder/huffman.d.ts +19 -0
- package/dist/esm/encoder/huffman.d.ts.map +1 -0
- package/dist/esm/encoder/huffman.js +135 -0
- package/dist/esm/encoder/huffman.js.map +1 -0
- package/dist/esm/encoder/progressive.d.ts +20 -0
- package/dist/esm/encoder/progressive.d.ts.map +1 -0
- package/dist/esm/encoder/progressive.js +315 -0
- package/dist/esm/encoder/progressive.js.map +1 -0
- package/dist/esm/encoder/writer.d.ts +18 -0
- package/dist/esm/encoder/writer.d.ts.map +1 -0
- package/dist/esm/encoder/writer.js +71 -0
- package/dist/esm/encoder/writer.js.map +1 -0
- package/dist/esm/errors.d.ts +27 -0
- package/dist/esm/errors.d.ts.map +1 -0
- package/dist/esm/errors.js +28 -0
- package/dist/esm/errors.js.map +1 -0
- package/dist/esm/index.d.ts +12 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +8 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/package.json +3 -0
- package/package.json +99 -4
|
@@ -0,0 +1,873 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* JPEG decoder: marker parsing, frame setup and orchestration.
|
|
4
|
+
*
|
|
5
|
+
* Copyright 2011 notmasteryet
|
|
6
|
+
* Copyright 2014 Mozilla Foundation
|
|
7
|
+
*
|
|
8
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
9
|
+
* you may not use this file except in compliance with the License.
|
|
10
|
+
* You may obtain a copy of the License at
|
|
11
|
+
*
|
|
12
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
13
|
+
*
|
|
14
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
15
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
16
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
17
|
+
* See the License for the specific language governing permissions and
|
|
18
|
+
* limitations under the License.
|
|
19
|
+
*
|
|
20
|
+
* This code was forked from https://github.com/notmasteryet/jpgjs, through
|
|
21
|
+
* jpeg-js (https://github.com/jpeg-js/jpeg-js) and pdf.js
|
|
22
|
+
* (https://github.com/mozilla/pdf.js, src/core/jpg.js and jpeg_utils.js).
|
|
23
|
+
*
|
|
24
|
+
* - The JPEG specification can be found in the ITU CCITT Recommendation T.81
|
|
25
|
+
* (www.w3.org/Graphics/JPEG/itu-t81.pdf)
|
|
26
|
+
* - The JFIF specification can be found in the JPEG File Interchange Format
|
|
27
|
+
* (www.w3.org/Graphics/JPEG/jfif3.pdf)
|
|
28
|
+
* - The Adobe Application-Specific JPEG markers in the
|
|
29
|
+
* Supporting the DCT Filters in PostScript Level 2, Technical Note #5116
|
|
30
|
+
* (partners.adobe.com/public/developer/en/ps/sdk/5116.DCT_Filter.pdf)
|
|
31
|
+
*
|
|
32
|
+
* Modified by Jimp contributors (2026):
|
|
33
|
+
* - translated to TypeScript, with a JpegError subclass for every failure
|
|
34
|
+
* instead of TypeErrors on malformed input;
|
|
35
|
+
* - every segment is bounds-checked; SOS before SOF, unknown component IDs,
|
|
36
|
+
* missing tables, bad sampling factors and bad progression parameters are
|
|
37
|
+
* rejected;
|
|
38
|
+
* - recovery from junk between segments, a lost 0xFF before a marker, segment
|
|
39
|
+
* lengths that are off and a missing EOI, merged from jpeg-js and Jimp;
|
|
40
|
+
* - DNL support only for frames whose SOF height is 0, found by scanning ahead
|
|
41
|
+
* instead of re-parsing;
|
|
42
|
+
* - entropy decoding, IDCT, upsampling and colour conversion are replaced by
|
|
43
|
+
* ports of libjpeg-turbo's (scan.ts, idct.ts, output.ts), and arithmetic
|
|
44
|
+
* coding is supported (arithmetic.ts);
|
|
45
|
+
* - sequential images are decoded one MCU row at a time, straight into sample
|
|
46
|
+
* planes, and colour conversion picks the colour space like libjpeg;
|
|
47
|
+
* - resolution and memory limits from jpeg-js, plus external tables (JPEG
|
|
48
|
+
* abbreviated format).
|
|
49
|
+
*/
|
|
50
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
51
|
+
exports.JpegDecoder = void 0;
|
|
52
|
+
const errors_js_1 = require("../errors.js");
|
|
53
|
+
const arithmetic_js_1 = require("./arithmetic.js");
|
|
54
|
+
const huffman_js_1 = require("./huffman.js");
|
|
55
|
+
const idct_js_1 = require("./idct.js");
|
|
56
|
+
const output_js_1 = require("./output.js");
|
|
57
|
+
const scan_js_1 = require("./scan.js");
|
|
58
|
+
const smoothing_js_1 = require("./smoothing.js");
|
|
59
|
+
const standard_tables_js_1 = require("./standard-tables.js");
|
|
60
|
+
/** Maximum number of blocks in an MCU, as in libjpeg (D_MAX_BLOCKS_IN_MCU). */
|
|
61
|
+
const MAX_BLOCKS_IN_MCU = 10;
|
|
62
|
+
/** Defends against files with an absurd number of (empty) scans. */
|
|
63
|
+
const MAX_SCANS = 1000;
|
|
64
|
+
const SOF_NAMES = {
|
|
65
|
+
0xc3: "lossless",
|
|
66
|
+
0xc5: "differential sequential (hierarchical)",
|
|
67
|
+
0xc6: "differential progressive (hierarchical)",
|
|
68
|
+
0xc7: "differential lossless (hierarchical)",
|
|
69
|
+
0xcb: "arithmetic-coded lossless",
|
|
70
|
+
0xcd: "arithmetic-coded differential sequential (hierarchical)",
|
|
71
|
+
0xce: "arithmetic-coded differential progressive (hierarchical)",
|
|
72
|
+
0xcf: "arithmetic-coded differential lossless (hierarchical)",
|
|
73
|
+
};
|
|
74
|
+
function hex(value) {
|
|
75
|
+
return value.toString(16).toUpperCase().padStart(2, "0");
|
|
76
|
+
}
|
|
77
|
+
function isMarkerCode(code) {
|
|
78
|
+
return code !== undefined && code >= 0xc0 && code <= 0xfe;
|
|
79
|
+
}
|
|
80
|
+
/** Markers that are followed by a length field. */
|
|
81
|
+
function hasLength(code) {
|
|
82
|
+
return !(code >= 0xd0 && code <= 0xd9) && code !== 0x01;
|
|
83
|
+
}
|
|
84
|
+
class JpegDecoder {
|
|
85
|
+
options;
|
|
86
|
+
width = 0;
|
|
87
|
+
height = 0;
|
|
88
|
+
jfif = false;
|
|
89
|
+
adobe = null;
|
|
90
|
+
exif;
|
|
91
|
+
comments = [];
|
|
92
|
+
frame = null;
|
|
93
|
+
quantTables = [];
|
|
94
|
+
dcTables = [];
|
|
95
|
+
acTables = [];
|
|
96
|
+
restartInterval = 0;
|
|
97
|
+
conditioning = (0, arithmetic_js_1.defaultConditioning)();
|
|
98
|
+
/** Sequential image whose single scan is decoded one MCU row at a time. */
|
|
99
|
+
streaming = false;
|
|
100
|
+
scans = 0;
|
|
101
|
+
idct = null;
|
|
102
|
+
memoryUsed = 0;
|
|
103
|
+
/** See ScanResult.lastGoodRow. */
|
|
104
|
+
lastGoodRow = 0;
|
|
105
|
+
constructor(options) {
|
|
106
|
+
this.options = options;
|
|
107
|
+
}
|
|
108
|
+
get tolerant() {
|
|
109
|
+
return this.options.tolerantDecoding;
|
|
110
|
+
}
|
|
111
|
+
corrupt(message) {
|
|
112
|
+
if (!this.tolerant) {
|
|
113
|
+
throw new errors_js_1.JpegFormatError(message);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/** Tracks memory use against maxMemoryUsageInMB, like jpeg-js. */
|
|
117
|
+
requestMemory(bytes) {
|
|
118
|
+
const max = this.options.maxMemoryUsageInMB * 1024 * 1024;
|
|
119
|
+
this.memoryUsed += bytes;
|
|
120
|
+
if (this.memoryUsed > max) {
|
|
121
|
+
const exceeded = Math.ceil((this.memoryUsed - max) / 1024 / 1024);
|
|
122
|
+
throw new errors_js_1.JpegLimitError(`maxMemoryUsageInMB limit exceeded by at least ${exceeded}MB`);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
parse(data) {
|
|
126
|
+
if (this.options.tables) {
|
|
127
|
+
this.parseMarkers(this.options.tables, true);
|
|
128
|
+
}
|
|
129
|
+
this.parseMarkers(data, false);
|
|
130
|
+
const frame = this.frame;
|
|
131
|
+
if (!frame) {
|
|
132
|
+
throw new errors_js_1.JpegFormatError("JPEG data contains no image (no SOF marker)");
|
|
133
|
+
}
|
|
134
|
+
if (this.scans === 0) {
|
|
135
|
+
throw new errors_js_1.JpegFormatError("JPEG data contains no image data (no SOS marker)");
|
|
136
|
+
}
|
|
137
|
+
if (!this.streaming) {
|
|
138
|
+
this.finishCoefficients(frame);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
parseMarkers(data, tablesOnly) {
|
|
142
|
+
const length = data.length;
|
|
143
|
+
if (length < 2 || data[0] !== 0xff || data[1] !== 0xd8) {
|
|
144
|
+
throw new errors_js_1.JpegFormatError(tablesOnly
|
|
145
|
+
? "JPEG tables don't start with an SOI marker"
|
|
146
|
+
: "SOI not found: the data is not a JPEG");
|
|
147
|
+
}
|
|
148
|
+
let offset = 2;
|
|
149
|
+
while (offset < length) {
|
|
150
|
+
if (data[offset] !== 0xff || !isMarkerCode(data[offset + 1])) {
|
|
151
|
+
// Fill bytes before a marker
|
|
152
|
+
if (data[offset] === 0xff && data[offset + 1] === 0xff) {
|
|
153
|
+
offset++;
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
const next = this.recoverMarker(data, offset);
|
|
157
|
+
if (next < 0) {
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
// Unless the marker only lost its 0xFF, parse from where it is
|
|
161
|
+
if (next !== offset) {
|
|
162
|
+
offset = next;
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
const code = data[offset + 1];
|
|
167
|
+
let segmentEnd = offset + 2;
|
|
168
|
+
if (hasLength(code)) {
|
|
169
|
+
if (offset + 4 > length) {
|
|
170
|
+
this.corrupt("JPEG data ends inside a marker segment");
|
|
171
|
+
break;
|
|
172
|
+
}
|
|
173
|
+
const segmentLength = (data[offset + 2] << 8) | data[offset + 3];
|
|
174
|
+
segmentEnd = offset + 2 + segmentLength;
|
|
175
|
+
if (segmentLength < 2 || segmentEnd > length) {
|
|
176
|
+
// Only metadata can be cut short and leave a usable image
|
|
177
|
+
if (code >= 0xe0 || code === 0xfe) {
|
|
178
|
+
this.corrupt(`Bad length of ${this.describe(code)}`);
|
|
179
|
+
break;
|
|
180
|
+
}
|
|
181
|
+
throw new errors_js_1.JpegFormatError(`Bad length of ${this.describe(code)}`);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
const start = offset + 4;
|
|
185
|
+
if (code === 0xd9) {
|
|
186
|
+
break;
|
|
187
|
+
}
|
|
188
|
+
if (tablesOnly &&
|
|
189
|
+
code !== 0xdb &&
|
|
190
|
+
code !== 0xc4 &&
|
|
191
|
+
code !== 0xdd &&
|
|
192
|
+
code !== 0xcc &&
|
|
193
|
+
!(code >= 0xe0 && code <= 0xef) &&
|
|
194
|
+
code !== 0xfe) {
|
|
195
|
+
throw new errors_js_1.JpegFormatError(`JPEG tables can only contain DQT, DHT and DRI markers, not ${this.describe(code)}`);
|
|
196
|
+
}
|
|
197
|
+
switch (code) {
|
|
198
|
+
case 0xe0:
|
|
199
|
+
case 0xe1:
|
|
200
|
+
case 0xe2:
|
|
201
|
+
case 0xe3:
|
|
202
|
+
case 0xe4:
|
|
203
|
+
case 0xe5:
|
|
204
|
+
case 0xe6:
|
|
205
|
+
case 0xe7:
|
|
206
|
+
case 0xe8:
|
|
207
|
+
case 0xe9:
|
|
208
|
+
case 0xea:
|
|
209
|
+
case 0xeb:
|
|
210
|
+
case 0xec:
|
|
211
|
+
case 0xed:
|
|
212
|
+
case 0xee:
|
|
213
|
+
case 0xef:
|
|
214
|
+
case 0xfe:
|
|
215
|
+
segmentEnd = this.fixSegmentEnd(data, start, segmentEnd);
|
|
216
|
+
if (!tablesOnly) {
|
|
217
|
+
this.readAppSegment(code, data.subarray(start, segmentEnd));
|
|
218
|
+
}
|
|
219
|
+
offset = segmentEnd;
|
|
220
|
+
break;
|
|
221
|
+
case 0xdb:
|
|
222
|
+
this.readQuantTables(data, start, segmentEnd);
|
|
223
|
+
offset = segmentEnd;
|
|
224
|
+
break;
|
|
225
|
+
case 0xc4:
|
|
226
|
+
this.readHuffmanTables(data, start, segmentEnd);
|
|
227
|
+
offset = segmentEnd;
|
|
228
|
+
break;
|
|
229
|
+
case 0xdd:
|
|
230
|
+
if (segmentEnd - start < 2) {
|
|
231
|
+
throw new errors_js_1.JpegFormatError("Bad length of DRI marker");
|
|
232
|
+
}
|
|
233
|
+
this.restartInterval = (data[start] << 8) | data[start + 1];
|
|
234
|
+
offset = segmentEnd;
|
|
235
|
+
break;
|
|
236
|
+
case 0xc0:
|
|
237
|
+
case 0xc1:
|
|
238
|
+
case 0xc2:
|
|
239
|
+
case 0xc9:
|
|
240
|
+
case 0xca:
|
|
241
|
+
this.readFrame(code, data, start, segmentEnd);
|
|
242
|
+
offset = segmentEnd;
|
|
243
|
+
break;
|
|
244
|
+
case 0xc3:
|
|
245
|
+
case 0xc5:
|
|
246
|
+
case 0xc6:
|
|
247
|
+
case 0xc7:
|
|
248
|
+
case 0xcb:
|
|
249
|
+
case 0xcd:
|
|
250
|
+
case 0xce:
|
|
251
|
+
case 0xcf:
|
|
252
|
+
throw new errors_js_1.JpegUnsupportedError(`Unsupported JPEG process: ${SOF_NAMES[code]} (SOF${code - 0xc0}) JPEGs are not supported`);
|
|
253
|
+
case 0xda:
|
|
254
|
+
offset = this.readScan(data, start, segmentEnd);
|
|
255
|
+
break;
|
|
256
|
+
case 0xd8: // SOI
|
|
257
|
+
case 0xd0: // RSTn outside a scan, e.g. extra ones after it
|
|
258
|
+
case 0xd1:
|
|
259
|
+
case 0xd2:
|
|
260
|
+
case 0xd3:
|
|
261
|
+
case 0xd4:
|
|
262
|
+
case 0xd5:
|
|
263
|
+
case 0xd6:
|
|
264
|
+
case 0xd7:
|
|
265
|
+
offset += 2;
|
|
266
|
+
break;
|
|
267
|
+
case 0xcc:
|
|
268
|
+
this.readConditioning(data, start, segmentEnd);
|
|
269
|
+
offset = segmentEnd;
|
|
270
|
+
break;
|
|
271
|
+
case 0xdc: // DNL: read ahead in readFrame() when it's needed
|
|
272
|
+
offset = segmentEnd;
|
|
273
|
+
break;
|
|
274
|
+
default:
|
|
275
|
+
// JPGn, DHP, EXP, ... Skip them like APPn if their length is sane
|
|
276
|
+
this.corrupt(`Unsupported JPEG marker ${this.describe(code)}`);
|
|
277
|
+
offset = this.fixSegmentEnd(data, start, segmentEnd);
|
|
278
|
+
break;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
describe(code) {
|
|
283
|
+
return `marker 0xFF${hex(code)}`;
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Called when a marker was expected at `offset` but isn't there. Finds where
|
|
287
|
+
* parsing should go on, or returns -1 to stop.
|
|
288
|
+
*/
|
|
289
|
+
recoverMarker(data, offset) {
|
|
290
|
+
const length = data.length;
|
|
291
|
+
this.corrupt(`Corrupt JPEG data: expected a marker at offset ${offset}, found 0x${hex(data[offset])}`);
|
|
292
|
+
// The 0xFF of the marker was lost (seen from Pixel phones, jpeg-js #84):
|
|
293
|
+
// parse it from here
|
|
294
|
+
const code = data[offset + 1];
|
|
295
|
+
if (isMarkerCode(code) &&
|
|
296
|
+
hasLength(code) &&
|
|
297
|
+
offset + 4 <= length &&
|
|
298
|
+
this.segmentEndsWell(data, offset + 2 + ((data[offset + 2] << 8) | data[offset + 3]))) {
|
|
299
|
+
return offset;
|
|
300
|
+
}
|
|
301
|
+
// Otherwise skip to the next marker, like libjpeg does (and Jimp did
|
|
302
|
+
// with skipJunkBeforeMarkers)
|
|
303
|
+
for (let i = offset + 1; i + 1 < length; i++) {
|
|
304
|
+
if (data[i] !== 0xff) {
|
|
305
|
+
continue;
|
|
306
|
+
}
|
|
307
|
+
const next = data[i + 1];
|
|
308
|
+
if (!isMarkerCode(next) || next === 0xff) {
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
if (!hasLength(next) ||
|
|
312
|
+
next === 0xda ||
|
|
313
|
+
(i + 4 <= length &&
|
|
314
|
+
this.segmentEndsWell(data, i + 2 + ((data[i + 2] << 8) | data[i + 3])))) {
|
|
315
|
+
return i;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
return -1;
|
|
319
|
+
}
|
|
320
|
+
/** Whether a segment ending at `end` is followed by a marker or the end. */
|
|
321
|
+
segmentEndsWell(data, end) {
|
|
322
|
+
return end === data.length || (end < data.length && data[end] === 0xff);
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* Fixes the end of a segment whose length doesn't lead to a marker, by
|
|
326
|
+
* searching for one in the data (pdf.js readDataBlock).
|
|
327
|
+
*/
|
|
328
|
+
fixSegmentEnd(data, start, end) {
|
|
329
|
+
if (this.segmentEndsWell(data, end)) {
|
|
330
|
+
return end;
|
|
331
|
+
}
|
|
332
|
+
this.corrupt("Corrupt JPEG data: bad marker segment length");
|
|
333
|
+
for (let i = start; i + 1 < data.length; i++) {
|
|
334
|
+
if (data[i] === 0xff &&
|
|
335
|
+
isMarkerCode(data[i + 1]) &&
|
|
336
|
+
data[i + 1] !== 0xff) {
|
|
337
|
+
return i;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
return data.length;
|
|
341
|
+
}
|
|
342
|
+
readAppSegment(code, segment) {
|
|
343
|
+
const is = (...bytes) => bytes.every((byte, index) => segment[index] === byte);
|
|
344
|
+
if (code === 0xfe) {
|
|
345
|
+
let comment = "";
|
|
346
|
+
for (let i = 0; i < segment.length; i += 0x8000) {
|
|
347
|
+
comment += String.fromCharCode(...segment.subarray(i, i + 0x8000));
|
|
348
|
+
}
|
|
349
|
+
this.comments.push(comment);
|
|
350
|
+
}
|
|
351
|
+
else if (code === 0xe0 && is(0x4a, 0x46, 0x49, 0x46, 0)) {
|
|
352
|
+
// JFIF\0
|
|
353
|
+
this.jfif = true;
|
|
354
|
+
}
|
|
355
|
+
else if (code === 0xe1 && is(0x45, 0x78, 0x69, 0x66, 0)) {
|
|
356
|
+
// Exif\0. Like jpeg-js, everything after "Exif\0", so starting with the
|
|
357
|
+
// second padding byte
|
|
358
|
+
this.exif = segment.slice(5);
|
|
359
|
+
}
|
|
360
|
+
else if (code === 0xee && is(0x41, 0x64, 0x6f, 0x62, 0x65)) {
|
|
361
|
+
// Adobe
|
|
362
|
+
this.adobe = { transformCode: segment[11] ?? 0 };
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
readQuantTables(data, offset, end) {
|
|
366
|
+
while (offset < end) {
|
|
367
|
+
const spec = data[offset++];
|
|
368
|
+
const precision = spec >> 4;
|
|
369
|
+
if (precision > 1) {
|
|
370
|
+
throw new errors_js_1.JpegFormatError("Bogus DQT: invalid table precision");
|
|
371
|
+
}
|
|
372
|
+
if (offset + 64 * (precision + 1) > end) {
|
|
373
|
+
throw new errors_js_1.JpegFormatError("Bad length of DQT marker");
|
|
374
|
+
}
|
|
375
|
+
const table = new Uint16Array(64);
|
|
376
|
+
for (let i = 0; i < 64; i++) {
|
|
377
|
+
if (precision === 0) {
|
|
378
|
+
table[scan_js_1.NATURAL_ORDER[i]] = data[offset++];
|
|
379
|
+
}
|
|
380
|
+
else {
|
|
381
|
+
table[scan_js_1.NATURAL_ORDER[i]] = (data[offset] << 8) | data[offset + 1];
|
|
382
|
+
offset += 2;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
this.quantTables[spec & 15] = table;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
readHuffmanTables(data, offset, end) {
|
|
389
|
+
while (offset < end) {
|
|
390
|
+
const spec = data[offset++];
|
|
391
|
+
const tableClass = spec >> 4;
|
|
392
|
+
if (tableClass > 1 || offset + 16 > end) {
|
|
393
|
+
throw new errors_js_1.JpegFormatError("Bogus DHT: invalid table");
|
|
394
|
+
}
|
|
395
|
+
const counts = data.subarray(offset, offset + 16);
|
|
396
|
+
let count = 0;
|
|
397
|
+
for (let i = 0; i < 16; i++) {
|
|
398
|
+
count += counts[i];
|
|
399
|
+
}
|
|
400
|
+
offset += 16;
|
|
401
|
+
if (count > 256 || offset + count > end) {
|
|
402
|
+
throw new errors_js_1.JpegFormatError("Bogus DHT: invalid table");
|
|
403
|
+
}
|
|
404
|
+
const table = (0, huffman_js_1.buildHuffmanTable)(counts, data.subarray(offset, offset + count), tableClass === 0);
|
|
405
|
+
offset += count;
|
|
406
|
+
(tableClass === 0 ? this.dcTables : this.acTables)[spec & 15] = table;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
/** DAC: arithmetic coding conditioning, like libjpeg's get_dac(). */
|
|
410
|
+
readConditioning(data, offset, end) {
|
|
411
|
+
if ((end - offset) % 2 !== 0) {
|
|
412
|
+
throw new errors_js_1.JpegFormatError("Bad length of DAC marker");
|
|
413
|
+
}
|
|
414
|
+
for (; offset < end; offset += 2) {
|
|
415
|
+
const index = data[offset];
|
|
416
|
+
const value = data[offset + 1];
|
|
417
|
+
const table = index & 15;
|
|
418
|
+
if (index >= 32 || table > 3) {
|
|
419
|
+
throw new errors_js_1.JpegFormatError(`Bogus DAC index ${index}`);
|
|
420
|
+
}
|
|
421
|
+
if (index >= 16) {
|
|
422
|
+
this.conditioning.acK[table] = value;
|
|
423
|
+
}
|
|
424
|
+
else {
|
|
425
|
+
this.conditioning.dcLower[table] = value & 15;
|
|
426
|
+
this.conditioning.dcUpper[table] = value >> 4;
|
|
427
|
+
if ((value & 15) > value >> 4) {
|
|
428
|
+
throw new errors_js_1.JpegFormatError(`Bogus DAC value 0x${hex(value)}`);
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
readFrame(code, data, offset, end) {
|
|
434
|
+
if (this.frame) {
|
|
435
|
+
throw new errors_js_1.JpegFormatError("JPEG data has more than one frame (SOF marker)");
|
|
436
|
+
}
|
|
437
|
+
if (end - offset < 6) {
|
|
438
|
+
throw new errors_js_1.JpegFormatError("Bad length of SOF marker");
|
|
439
|
+
}
|
|
440
|
+
const precision = data[offset];
|
|
441
|
+
let height = (data[offset + 1] << 8) | data[offset + 2];
|
|
442
|
+
const width = (data[offset + 3] << 8) | data[offset + 4];
|
|
443
|
+
const count = data[offset + 5];
|
|
444
|
+
if (precision !== 8 && precision !== 12) {
|
|
445
|
+
throw new errors_js_1.JpegUnsupportedError(`Unsupported JPEG data precision: ${precision} bits (only 8 and 12 are supported)`);
|
|
446
|
+
}
|
|
447
|
+
if (count === 0 || end - offset < 6 + count * 3) {
|
|
448
|
+
throw new errors_js_1.JpegFormatError("Bad length of SOF marker");
|
|
449
|
+
}
|
|
450
|
+
if (count !== 1 && count !== 3 && count !== 4) {
|
|
451
|
+
throw new errors_js_1.JpegUnsupportedError(`Unsupported JPEG colour format: ${count} components`);
|
|
452
|
+
}
|
|
453
|
+
if (width === 0) {
|
|
454
|
+
throw new errors_js_1.JpegFormatError("Empty JPEG image (width is 0)");
|
|
455
|
+
}
|
|
456
|
+
if (height === 0) {
|
|
457
|
+
// The height is in a DNL marker after the first scan
|
|
458
|
+
height = this.findDnlHeight(data, end);
|
|
459
|
+
}
|
|
460
|
+
const megapixels = (width * height) / 1e6;
|
|
461
|
+
if (megapixels > this.options.maxResolutionInMP) {
|
|
462
|
+
const exceeded = Math.ceil(megapixels - this.options.maxResolutionInMP);
|
|
463
|
+
throw new errors_js_1.JpegLimitError(`maxResolutionInMP limit exceeded by ${exceeded}MP`);
|
|
464
|
+
}
|
|
465
|
+
const components = [];
|
|
466
|
+
let maxH = 1;
|
|
467
|
+
let maxV = 1;
|
|
468
|
+
for (let i = 0; i < count; i++) {
|
|
469
|
+
const p = offset + 6 + i * 3;
|
|
470
|
+
const id = data[p];
|
|
471
|
+
const h = data[p + 1] >> 4;
|
|
472
|
+
const v = data[p + 1] & 15;
|
|
473
|
+
if (h < 1 || h > 4 || v < 1 || v > 4) {
|
|
474
|
+
throw new errors_js_1.JpegFormatError("Invalid sampling factor, expected values above 0 and up to 4");
|
|
475
|
+
}
|
|
476
|
+
maxH = Math.max(maxH, h);
|
|
477
|
+
maxV = Math.max(maxV, v);
|
|
478
|
+
components.push({
|
|
479
|
+
id,
|
|
480
|
+
h,
|
|
481
|
+
v,
|
|
482
|
+
quantId: data[p + 2],
|
|
483
|
+
quantTable: null,
|
|
484
|
+
blocksPerLine: 0,
|
|
485
|
+
blocksPerColumn: 0,
|
|
486
|
+
blocksPerLineMcu: 0,
|
|
487
|
+
blocksPerColumnMcu: 0,
|
|
488
|
+
width: 0,
|
|
489
|
+
height: 0,
|
|
490
|
+
coefficients: new Int16Array(0),
|
|
491
|
+
plane: null,
|
|
492
|
+
dcTable: null,
|
|
493
|
+
acTable: null,
|
|
494
|
+
pred: 0,
|
|
495
|
+
dcArithmetic: 0,
|
|
496
|
+
acArithmetic: 0,
|
|
497
|
+
dcContext: 0,
|
|
498
|
+
scanned: false,
|
|
499
|
+
coefBits: new Int8Array(64).fill(-1),
|
|
500
|
+
previousCoefBits: new Int8Array(64),
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
const mcusPerLine = Math.ceil(width / (8 * maxH));
|
|
504
|
+
const mcusPerColumn = Math.ceil(height / (8 * maxV));
|
|
505
|
+
const sampleBytes = precision > 8 ? 2 : 1;
|
|
506
|
+
let planeBytes = 0;
|
|
507
|
+
let coefficientBytes = 0;
|
|
508
|
+
for (const component of components) {
|
|
509
|
+
component.width = Math.ceil((width * component.h) / maxH);
|
|
510
|
+
component.height = Math.ceil((height * component.v) / maxV);
|
|
511
|
+
component.blocksPerLine = Math.ceil(component.width / 8);
|
|
512
|
+
component.blocksPerColumn = Math.ceil(component.height / 8);
|
|
513
|
+
component.blocksPerLineMcu = mcusPerLine * component.h;
|
|
514
|
+
component.blocksPerColumnMcu = mcusPerColumn * component.v;
|
|
515
|
+
planeBytes +=
|
|
516
|
+
component.blocksPerLine * component.blocksPerColumn * 64 * sampleBytes;
|
|
517
|
+
coefficientBytes +=
|
|
518
|
+
component.blocksPerLineMcu * component.blocksPerColumnMcu * 128;
|
|
519
|
+
}
|
|
520
|
+
this.width = width;
|
|
521
|
+
this.height = height;
|
|
522
|
+
const progressive = code === 0xc2 || code === 0xca;
|
|
523
|
+
this.frame = {
|
|
524
|
+
progressive,
|
|
525
|
+
arithmetic: code === 0xc9 || code === 0xca,
|
|
526
|
+
precision,
|
|
527
|
+
width,
|
|
528
|
+
height,
|
|
529
|
+
components,
|
|
530
|
+
maxH,
|
|
531
|
+
maxV,
|
|
532
|
+
mcusPerLine,
|
|
533
|
+
mcusPerColumn,
|
|
534
|
+
};
|
|
535
|
+
// The planes and the output, plus the coefficients of a progressive JPEG
|
|
536
|
+
this.requestMemory(planeBytes + width * height * 4);
|
|
537
|
+
if (progressive) {
|
|
538
|
+
this.requestMemory(coefficientBytes);
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
/** Finds the DNL marker that gives the height of a frame. */
|
|
542
|
+
findDnlHeight(data, offset) {
|
|
543
|
+
for (let i = offset; i + 5 < data.length; i++) {
|
|
544
|
+
if (data[i] === 0xff && data[i + 1] === 0xdc) {
|
|
545
|
+
const height = (data[i + 4] << 8) | data[i + 5];
|
|
546
|
+
if (height > 0) {
|
|
547
|
+
return height;
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
throw new errors_js_1.JpegFormatError("Empty JPEG image (height is 0 and there is no DNL marker)");
|
|
552
|
+
}
|
|
553
|
+
readScan(data, offset, end) {
|
|
554
|
+
const frame = this.frame;
|
|
555
|
+
if (!frame) {
|
|
556
|
+
throw new errors_js_1.JpegFormatError("Corrupt JPEG data: SOS marker before SOF marker");
|
|
557
|
+
}
|
|
558
|
+
if (++this.scans > MAX_SCANS) {
|
|
559
|
+
throw new errors_js_1.JpegLimitError(`JPEG has more than ${MAX_SCANS} scans`);
|
|
560
|
+
}
|
|
561
|
+
const count = data[offset++];
|
|
562
|
+
if (count < 1 || count > 4 || end - offset !== count * 2 + 3) {
|
|
563
|
+
throw new errors_js_1.JpegFormatError("Bad length of SOS marker");
|
|
564
|
+
}
|
|
565
|
+
const components = [];
|
|
566
|
+
for (let i = 0; i < count; i++) {
|
|
567
|
+
const id = data[offset++];
|
|
568
|
+
const tables = data[offset++];
|
|
569
|
+
const component = frame.components.find((c) => c.id === id);
|
|
570
|
+
if (!component || components.includes(component)) {
|
|
571
|
+
throw new errors_js_1.JpegFormatError(`Invalid component ID ${id} in SOS marker`);
|
|
572
|
+
}
|
|
573
|
+
if (frame.arithmetic) {
|
|
574
|
+
if (tables >> 4 > 3 || (tables & 15) > 3) {
|
|
575
|
+
throw new errors_js_1.JpegFormatError("Invalid arithmetic table in SOS marker");
|
|
576
|
+
}
|
|
577
|
+
component.dcArithmetic = tables >> 4;
|
|
578
|
+
component.acArithmetic = tables & 15;
|
|
579
|
+
}
|
|
580
|
+
else {
|
|
581
|
+
component.dcTable = this.huffmanTable(this.dcTables, tables >> 4, true, frame.progressive);
|
|
582
|
+
component.acTable = this.huffmanTable(this.acTables, tables & 15, false, frame.progressive);
|
|
583
|
+
}
|
|
584
|
+
components.push(component);
|
|
585
|
+
}
|
|
586
|
+
const spectralStart = data[offset++];
|
|
587
|
+
const spectralEnd = data[offset++];
|
|
588
|
+
const approxHigh = data[offset] >> 4;
|
|
589
|
+
const approxLow = data[offset] & 15;
|
|
590
|
+
if (frame.progressive) {
|
|
591
|
+
const dc = spectralStart === 0;
|
|
592
|
+
if ((dc
|
|
593
|
+
? spectralEnd !== 0
|
|
594
|
+
: spectralStart > spectralEnd || spectralEnd > 63 || count !== 1) ||
|
|
595
|
+
(approxHigh !== 0 && approxLow !== approxHigh - 1) ||
|
|
596
|
+
approxLow > 13) {
|
|
597
|
+
throw new errors_js_1.JpegFormatError(`Invalid progressive parameters Ss=${spectralStart} Se=${spectralEnd} Ah=${approxHigh} Al=${approxLow}`);
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
// Check the tables each kind of scan needs
|
|
601
|
+
const needsDC = !frame.progressive || (spectralStart === 0 && approxHigh === 0);
|
|
602
|
+
const needsAC = !frame.progressive || spectralStart > 0;
|
|
603
|
+
for (const component of components) {
|
|
604
|
+
if (frame.arithmetic) {
|
|
605
|
+
break;
|
|
606
|
+
}
|
|
607
|
+
if ((needsDC && !component.dcTable) || (needsAC && !component.acTable)) {
|
|
608
|
+
throw new errors_js_1.JpegFormatError("Huffman table missing for SOS component");
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
if (count > 1) {
|
|
612
|
+
let blocks = 0;
|
|
613
|
+
for (const component of components) {
|
|
614
|
+
blocks += component.h * component.v;
|
|
615
|
+
}
|
|
616
|
+
if (blocks > MAX_BLOCKS_IN_MCU) {
|
|
617
|
+
throw new errors_js_1.JpegFormatError("Sampling factors too large for interleaved scan");
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
const scanStart = end;
|
|
621
|
+
if (!frame.progressive && this.streaming) {
|
|
622
|
+
// The image is complete, ignore further scans
|
|
623
|
+
return this.skipScan(data, scanStart);
|
|
624
|
+
}
|
|
625
|
+
for (const component of components) {
|
|
626
|
+
if (!component.quantTable) {
|
|
627
|
+
component.quantTable = this.quantTables[component.quantId] ?? null;
|
|
628
|
+
if (!component.quantTable &&
|
|
629
|
+
!frame.progressive &&
|
|
630
|
+
count === frame.components.length) {
|
|
631
|
+
throw new errors_js_1.JpegFormatError(`Quantization table ${component.quantId} is missing`);
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
this.checkEnoughData(frame, data, scanStart, components, needsDC ? (frame.progressive ? 1 : 2) : 0);
|
|
636
|
+
const firstScan = this.scans === 1;
|
|
637
|
+
if (firstScan) {
|
|
638
|
+
this.idct = (0, idct_js_1.createIdctContext)(frame.precision);
|
|
639
|
+
this.streaming = !frame.progressive && count === frame.components.length;
|
|
640
|
+
}
|
|
641
|
+
if (this.streaming) {
|
|
642
|
+
return this.decodeStreamingScan(frame, data, scanStart, components);
|
|
643
|
+
}
|
|
644
|
+
if (frame.progressive) {
|
|
645
|
+
// Track the progression for block smoothing, like jdphuff.c
|
|
646
|
+
for (const { coefBits, previousCoefBits } of components) {
|
|
647
|
+
const from = Math.min(spectralStart, 1);
|
|
648
|
+
const to = Math.max(spectralEnd, 9);
|
|
649
|
+
for (let i = from; i <= to; i++) {
|
|
650
|
+
previousCoefBits[i] = this.scans > 1 ? coefBits[i] : 0;
|
|
651
|
+
}
|
|
652
|
+
for (let i = spectralStart; i <= spectralEnd; i++) {
|
|
653
|
+
coefBits[i] = approxLow;
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
for (const component of components) {
|
|
658
|
+
if (component.coefficients.length === 0) {
|
|
659
|
+
if (!frame.progressive) {
|
|
660
|
+
this.requestMemory(component.blocksPerLineMcu * component.blocksPerColumnMcu * 128);
|
|
661
|
+
}
|
|
662
|
+
component.coefficients = this.allocate(() => new Int16Array(component.blocksPerLineMcu * component.blocksPerColumnMcu * 64));
|
|
663
|
+
}
|
|
664
|
+
component.scanned = true;
|
|
665
|
+
}
|
|
666
|
+
const result = (0, scan_js_1.decodeScan)(data, scanStart, {
|
|
667
|
+
components,
|
|
668
|
+
progressive: frame.progressive,
|
|
669
|
+
spectralStart,
|
|
670
|
+
spectralEnd,
|
|
671
|
+
approxHigh,
|
|
672
|
+
approxLow,
|
|
673
|
+
mcusPerLine: frame.mcusPerLine,
|
|
674
|
+
mcusPerColumn: frame.mcusPerColumn,
|
|
675
|
+
restartInterval: this.restartInterval,
|
|
676
|
+
arithmetic: frame.arithmetic ? this.conditioning : undefined,
|
|
677
|
+
}, this.scanOptions());
|
|
678
|
+
this.lastGoodRow = result.lastGoodRow;
|
|
679
|
+
return result.offset;
|
|
680
|
+
}
|
|
681
|
+
scanOptions() {
|
|
682
|
+
return {
|
|
683
|
+
tolerant: this.tolerant,
|
|
684
|
+
allowTruncated: this.options.allowTruncated,
|
|
685
|
+
};
|
|
686
|
+
}
|
|
687
|
+
huffmanTable(tables, id, isDC, progressive) {
|
|
688
|
+
let table = tables[id];
|
|
689
|
+
// Like libjpeg-turbo, fall back on the tables of the JPEG standard
|
|
690
|
+
// (Annex K) in sequential JPEGs: Motion JPEG frames leave them out
|
|
691
|
+
if (!table && !progressive && id < 2) {
|
|
692
|
+
const standard = standard_tables_js_1.STANDARD_TABLES[isDC ? "dc" : "ac"][id];
|
|
693
|
+
table = (0, huffman_js_1.buildHuffmanTable)(standard.counts, standard.symbols, isDC);
|
|
694
|
+
tables[id] = table;
|
|
695
|
+
}
|
|
696
|
+
return table ?? null;
|
|
697
|
+
}
|
|
698
|
+
/**
|
|
699
|
+
* Rejects a frame that is too big for the data left before decoding it, so
|
|
700
|
+
* a small file can't make the decoder allocate and decode a huge image.
|
|
701
|
+
* Every block needs at least one bit per scan for its DC coefficient, two
|
|
702
|
+
* for a sequential scan (with the end-of-block code).
|
|
703
|
+
*/
|
|
704
|
+
checkEnoughData(frame, data, offset, components, bitsPerBlock) {
|
|
705
|
+
// Arithmetic coding can take less than a bit per block
|
|
706
|
+
if (this.options.allowTruncated || bitsPerBlock === 0 || frame.arithmetic) {
|
|
707
|
+
return;
|
|
708
|
+
}
|
|
709
|
+
let blocks = 0;
|
|
710
|
+
for (const component of components) {
|
|
711
|
+
blocks +=
|
|
712
|
+
components.length === 1
|
|
713
|
+
? component.blocksPerLine * component.blocksPerColumn
|
|
714
|
+
: frame.mcusPerLine * frame.mcusPerColumn * component.h * component.v;
|
|
715
|
+
}
|
|
716
|
+
// Leave a row of MCUs, which may be missing (see checkTruncated())
|
|
717
|
+
const rowBlocks = frame.mcusPerLine * frame.maxH * frame.maxV * components.length;
|
|
718
|
+
if (((blocks - rowBlocks) * bitsPerBlock) / 8 > data.length - offset) {
|
|
719
|
+
throw new errors_js_1.JpegFormatError("JPEG data is truncated: the image data ends before the image is complete");
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
allocate(create) {
|
|
723
|
+
try {
|
|
724
|
+
return create();
|
|
725
|
+
}
|
|
726
|
+
catch (error) {
|
|
727
|
+
if (error instanceof RangeError) {
|
|
728
|
+
throw new errors_js_1.JpegLimitError("Could not allocate enough memory for the image");
|
|
729
|
+
}
|
|
730
|
+
throw error;
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
createPlane(frame, component) {
|
|
734
|
+
const size = component.blocksPerLine * component.blocksPerColumn * 64;
|
|
735
|
+
component.plane = this.allocate(() => frame.precision > 8 ? new Uint16Array(size) : new Uint8Array(size));
|
|
736
|
+
}
|
|
737
|
+
decodeStreamingScan(frame, data, offset, components) {
|
|
738
|
+
const single = components.length === 1;
|
|
739
|
+
const idct = this.idct;
|
|
740
|
+
for (const component of components) {
|
|
741
|
+
const rows = single ? 1 : component.v;
|
|
742
|
+
component.coefficients = new Int16Array(component.blocksPerLineMcu * rows * 64);
|
|
743
|
+
this.createPlane(frame, component);
|
|
744
|
+
component.scanned = true;
|
|
745
|
+
}
|
|
746
|
+
const onRow = (mcuRow) => {
|
|
747
|
+
for (const component of components) {
|
|
748
|
+
const rows = single ? 1 : component.v;
|
|
749
|
+
const { blocksPerLine, blocksPerColumn, blocksPerLineMcu, coefficients, } = component;
|
|
750
|
+
const plane = component.plane;
|
|
751
|
+
const stride = blocksPerLine * 8;
|
|
752
|
+
const quantTable = component.quantTable;
|
|
753
|
+
for (let j = 0; j < rows; j++) {
|
|
754
|
+
const blockRow = mcuRow * rows + j;
|
|
755
|
+
if (blockRow >= blocksPerColumn) {
|
|
756
|
+
break;
|
|
757
|
+
}
|
|
758
|
+
for (let col = 0; col < blocksPerLine; col++) {
|
|
759
|
+
(0, idct_js_1.idctBlock)(idct, coefficients, (j * blocksPerLineMcu + col) * 64, quantTable, plane, blockRow * 8 * stride + col * 8, stride);
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
coefficients.fill(0);
|
|
763
|
+
}
|
|
764
|
+
};
|
|
765
|
+
return (0, scan_js_1.decodeScan)(data, offset, {
|
|
766
|
+
components,
|
|
767
|
+
progressive: false,
|
|
768
|
+
spectralStart: 0,
|
|
769
|
+
spectralEnd: 63,
|
|
770
|
+
approxHigh: 0,
|
|
771
|
+
approxLow: 0,
|
|
772
|
+
mcusPerLine: frame.mcusPerLine,
|
|
773
|
+
mcusPerColumn: frame.mcusPerColumn,
|
|
774
|
+
restartInterval: this.restartInterval,
|
|
775
|
+
arithmetic: frame.arithmetic ? this.conditioning : undefined,
|
|
776
|
+
onRow,
|
|
777
|
+
}, this.scanOptions()).offset;
|
|
778
|
+
}
|
|
779
|
+
/** Skips the data of a scan that isn't decoded. */
|
|
780
|
+
skipScan(data, offset) {
|
|
781
|
+
for (let i = offset; i + 1 < data.length; i++) {
|
|
782
|
+
const next = data[i + 1];
|
|
783
|
+
if (data[i] === 0xff &&
|
|
784
|
+
next !== 0 &&
|
|
785
|
+
next !== 0xff &&
|
|
786
|
+
!(next >= 0xd0 && next <= 0xd7)) {
|
|
787
|
+
return i;
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
return data.length;
|
|
791
|
+
}
|
|
792
|
+
/** IDCT of the stored coefficients of a progressive or multi-scan JPEG. */
|
|
793
|
+
finishCoefficients(frame) {
|
|
794
|
+
const idct = this.idct;
|
|
795
|
+
const latched = frame.components.every((c) => c.quantTable);
|
|
796
|
+
const smoothing = frame.progressive &&
|
|
797
|
+
latched &&
|
|
798
|
+
(0, smoothing_js_1.smoothingUseful)(frame.components.map((c) => ({ ...c, quantTable: c.quantTable })));
|
|
799
|
+
for (const component of frame.components) {
|
|
800
|
+
if (!component.scanned) {
|
|
801
|
+
this.corrupt("JPEG data is missing a colour component");
|
|
802
|
+
}
|
|
803
|
+
const quantTable = component.quantTable ?? this.quantTables[component.quantId];
|
|
804
|
+
if (!quantTable) {
|
|
805
|
+
throw new errors_js_1.JpegFormatError(`Quantization table ${component.quantId} is missing`);
|
|
806
|
+
}
|
|
807
|
+
this.createPlane(frame, component);
|
|
808
|
+
if (!component.scanned) {
|
|
809
|
+
continue;
|
|
810
|
+
}
|
|
811
|
+
if (smoothing) {
|
|
812
|
+
(0, smoothing_js_1.smoothAndTransform)(idct, { ...component, quantTable }, component.plane, this.scans, this.lastGoodRow, frame.mcusPerColumn);
|
|
813
|
+
component.coefficients = new Int16Array(0);
|
|
814
|
+
continue;
|
|
815
|
+
}
|
|
816
|
+
const { blocksPerLine, blocksPerColumn, blocksPerLineMcu, coefficients } = component;
|
|
817
|
+
const plane = component.plane;
|
|
818
|
+
const stride = blocksPerLine * 8;
|
|
819
|
+
for (let row = 0; row < blocksPerColumn; row++) {
|
|
820
|
+
for (let col = 0; col < blocksPerLine; col++) {
|
|
821
|
+
(0, idct_js_1.idctBlock)(idct, coefficients, (row * blocksPerLineMcu + col) * 64, quantTable, plane, row * 8 * stride + col * 8, stride);
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
// Free the coefficients before the output is allocated
|
|
825
|
+
component.coefficients = new Int16Array(0);
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
/**
|
|
829
|
+
* The colour space of the data, picked like libjpeg does unless
|
|
830
|
+
* `colorTransform` says whether to transform from YCbCr (or YCCK).
|
|
831
|
+
*/
|
|
832
|
+
get colorSpace() {
|
|
833
|
+
const frame = this.frame;
|
|
834
|
+
const count = frame.components.length;
|
|
835
|
+
const transform = this.options.colorTransform;
|
|
836
|
+
if (count === 1) {
|
|
837
|
+
return "grayscale";
|
|
838
|
+
}
|
|
839
|
+
if (count === 3) {
|
|
840
|
+
if (transform !== undefined) {
|
|
841
|
+
return transform ? "ycbcr" : "rgb";
|
|
842
|
+
}
|
|
843
|
+
if (this.jfif) {
|
|
844
|
+
return "ycbcr";
|
|
845
|
+
}
|
|
846
|
+
if (this.adobe) {
|
|
847
|
+
return this.adobe.transformCode === 0 ? "rgb" : "ycbcr";
|
|
848
|
+
}
|
|
849
|
+
const [a, b, c] = frame.components;
|
|
850
|
+
// ASCII 'R', 'G', 'B'
|
|
851
|
+
return a.id === 0x52 && b.id === 0x47 && c.id === 0x42 ? "rgb" : "ycbcr";
|
|
852
|
+
}
|
|
853
|
+
if (transform !== undefined) {
|
|
854
|
+
return transform ? "ycck" : "cmyk";
|
|
855
|
+
}
|
|
856
|
+
return this.adobe && this.adobe.transformCode !== 0 ? "ycck" : "cmyk";
|
|
857
|
+
}
|
|
858
|
+
/** Converts the decoded image to RGBA (`channels` 4) or RGB. */
|
|
859
|
+
getPixels(output, channels) {
|
|
860
|
+
const frame = this.frame;
|
|
861
|
+
const components = frame.components.map((c) => ({
|
|
862
|
+
plane: c.plane,
|
|
863
|
+
stride: c.blocksPerLine * 8,
|
|
864
|
+
width: c.width,
|
|
865
|
+
height: c.height,
|
|
866
|
+
h: c.h,
|
|
867
|
+
v: c.v,
|
|
868
|
+
}));
|
|
869
|
+
(0, output_js_1.convertToRgb)(components, this.colorSpace, frame.width, frame.height, frame.precision, channels, output);
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
exports.JpegDecoder = JpegDecoder;
|
|
873
|
+
//# sourceMappingURL=decoder.js.map
|