rapier-jxl 1.1.21 → 2.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.
- package/AGENTS.md +14 -10
- package/ENCODER-COMPARISON.md +6 -6
- package/README.md +75 -38
- package/admit.mjs +56 -0
- package/bits.mjs +30 -10
- package/effort.d.mts +11 -0
- package/effort.min.mjs +2 -0
- package/effort.mjs +217 -0
- package/entropy.mjs +16 -1
- package/frame.mjs +16 -9
- package/index.d.mts +13 -17
- package/index.mjs +30 -83
- package/jfif.mjs +407 -0
- package/jpeg.d.mts +14 -2
- package/jpeg.min.mjs +2 -2
- package/jpeg.mjs +34 -377
- package/lossless.mjs +33 -21
- package/lossy.mjs +41 -16
- package/package.json +9 -8
- package/photo.d.mts +6 -2
- package/photo.min.mjs +2 -2
- package/photo.mjs +73 -42
- package/public/examples/browser.html +4 -2
- package/public/examples/worker.mjs +34 -9
- package/rapier-jxl.min.mjs +2 -2
- package/sizes.json +179 -78
- package/vardct.mjs +30 -10
- package/weighted.mjs +79 -0
- package/writer.mjs +17 -0
- package/lossless.d.mts +0 -3
- package/lossless.min.mjs +0 -2
package/jfif.mjs
ADDED
|
@@ -0,0 +1,407 @@
|
|
|
1
|
+
// Rapier's JPEG XL encoder: reading a JPEG's coefficients. MIT (LICENSE).
|
|
2
|
+
// A JPEG is not decoded to pixels here: its quantised DCT coefficients are recovered from the Huffman-coded scans
|
|
3
|
+
// (baseline, extended sequential and progressive, 8-bit, grey or three components) so they can be carried into a
|
|
4
|
+
// JPEG XL frame as they are. Anything else (arithmetic coding, 12-bit, lossless, hierarchical, four components, a
|
|
5
|
+
// height left to a DNL marker, a colour profile other than sRGB or Display P3) is refused with JXL_JPEG and the
|
|
6
|
+
// picture takes the ordinary path instead. So is a JPEG cut short, a scan with bytes missing or left over, a restart
|
|
7
|
+
// marker out of order, or a progressive file whose scans do not finish every coefficient: nothing here invents pixels
|
|
8
|
+
// a file lost.
|
|
9
|
+
// The frame's dimensions are admitted against the carrier's limits (JPEG_LIMITS) before a coefficient plane is
|
|
10
|
+
// allocated, so a header naming a picture the module would never carry costs nothing.
|
|
11
|
+
import {JPEG_LIMITS} from './bits.mjs';
|
|
12
|
+
|
|
13
|
+
export const ZIGZAG = [0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28,
|
|
14
|
+
35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51, 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63];
|
|
15
|
+
|
|
16
|
+
export function jpegError(message, code = 'JXL_JPEG') { return Object.assign(new Error('JPEG: ' + message), {code}); }
|
|
17
|
+
|
|
18
|
+
// A 16-bit lookup: entry = (code length << 8) | symbol, 0 for no code.
|
|
19
|
+
function buildHuffman(counts, symbols) {
|
|
20
|
+
const lookup = new Uint16Array(65536);
|
|
21
|
+
let code = 0, k = 0;
|
|
22
|
+
for (let length = 1; length <= 16; length++) {
|
|
23
|
+
for (let i = 0; i < counts[length - 1]; i++) {
|
|
24
|
+
const symbol = symbols[k++], start = code << (16 - length), n = 1 << (16 - length);
|
|
25
|
+
// An all-ones code would turn the required scan padding into a symbol.
|
|
26
|
+
if (start + n >= 65536) throw jpegError('bad Huffman table');
|
|
27
|
+
lookup.fill((length << 8) | symbol, start, start + n);
|
|
28
|
+
code++;
|
|
29
|
+
}
|
|
30
|
+
code <<= 1;
|
|
31
|
+
}
|
|
32
|
+
return lookup;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// The colour space an ICC profile gives a JPEG's samples, read from what the profile does, never from what it says:
|
|
36
|
+
// RGB into the XYZ connection space through three colorants within 0.004 of sRGB's or Display P3's (D50, Bradford) and
|
|
37
|
+
// the sRGB curve on every channel, or grey on the sRGB curve (the same in both spaces). Lookup tables, another gamut or
|
|
38
|
+
// another curve name no space this module declares: null, and the JPEG is refused.
|
|
39
|
+
const SRGB = [0.436, 0.2225, 0.0139, 0.3851, 0.7169, 0.0971, 0.143, 0.0606, 0.7139];
|
|
40
|
+
const P3 = [0.5151, 0.2412, -0.001, 0.292, 0.6922, 0.0419, 0.1571, 0.0666, 0.7841];
|
|
41
|
+
// The sRGB curve at sixteenths, and its parameters (g, a, b, c, d) as the ICC's parametric curve 3 writes them.
|
|
42
|
+
const CURVE = [0, 0.00516, 0.01435, 0.02934, 0.05088, 0.07958, 0.11602, 0.16068, 0.21404, 0.27652, 0.34851, 0.43039,
|
|
43
|
+
0.52252, 0.62523, 0.73884, 0.86367, 1];
|
|
44
|
+
const PARAMETERS = [2.4, 1 / 1.055, 0.055 / 1.055, 1 / 12.92, 0.04045];
|
|
45
|
+
export function profileSpace(profile) {
|
|
46
|
+
const size = profile.length;
|
|
47
|
+
const u16 = at => (profile[at] << 8) | profile[at + 1];
|
|
48
|
+
const u32 = at => at + 4 <= size ? ((profile[at] << 24) | (profile[at + 1] << 16) | (profile[at + 2] << 8) | profile[at + 3]) >>> 0 : 0;
|
|
49
|
+
const sig = at => at + 4 <= size ? String.fromCharCode(profile[at], profile[at + 1], profile[at + 2], profile[at + 3]) : '';
|
|
50
|
+
const s15 = at => (u32(at) | 0) / 65536, tags = u32(128);
|
|
51
|
+
if (size < 132 || 132 + tags * 12 > size || sig(36) !== 'acsp' || sig(20) !== 'XYZ ') return null;
|
|
52
|
+
const tag = name => {
|
|
53
|
+
for (let i = 0; i < tags; i++) { const at = 132 + i * 12, offset = u32(at + 4); if (sig(at) === name) return offset + u32(at + 8) <= size ? offset : -1; }
|
|
54
|
+
return -1;
|
|
55
|
+
};
|
|
56
|
+
const srgbCurve = name => {
|
|
57
|
+
const at = tag(name);
|
|
58
|
+
if (at < 0) return false;
|
|
59
|
+
if (sig(at) === 'para') {
|
|
60
|
+
const type = u16(at + 8), count = [1, 3, 4, 5, 7][type];
|
|
61
|
+
if (type !== 3 && type !== 4) return false;
|
|
62
|
+
for (let i = 0; i < count; i++) if (Math.abs(s15(at + 12 + 4 * i) - (i < 5 ? PARAMETERS[i] : 0)) > (i ? 0.002 : 0.01)) return false;
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
const n = sig(at) === 'curv' ? u32(at + 8) : 0;
|
|
66
|
+
if (n < 2 || at + 12 + 2 * n > size) return false;
|
|
67
|
+
return CURVE.every((want, k) => {
|
|
68
|
+
const p = k * (n - 1) / 16, i = Math.floor(p), j = Math.min(n - 1, i + 1), f = p - i;
|
|
69
|
+
return Math.abs((u16(at + 12 + 2 * i) * (1 - f) + u16(at + 12 + 2 * j) * f) / 65535 - want) <= 0.001;
|
|
70
|
+
});
|
|
71
|
+
};
|
|
72
|
+
if (['A2B0', 'A2B1', 'A2B2'].some(name => tag(name) >= 0)) return null;
|
|
73
|
+
if (sig(16) === 'GRAY') return srgbCurve('kTRC') ? 'srgb' : null;
|
|
74
|
+
if (sig(16) !== 'RGB ' || !['rTRC', 'gTRC', 'bTRC'].every(srgbCurve)) return null;
|
|
75
|
+
const colorants = ['rXYZ', 'gXYZ', 'bXYZ'].flatMap(name => { const at = tag(name); return at >= 0 && sig(at) === 'XYZ ' ? [s15(at + 8), s15(at + 12), s15(at + 16)] : [9, 9, 9]; });
|
|
76
|
+
const near = reference => reference.every((v, i) => Math.abs(colorants[i] - v) <= 0.004);
|
|
77
|
+
return near(SRGB) ? 'srgb' : near(P3) ? 'display-p3' : null;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function parseJPEG(bytes) {
|
|
81
|
+
if (!(bytes.length > 4) || bytes[0] !== 0xFF || bytes[1] !== 0xD8) throw jpegError('not a JPEG');
|
|
82
|
+
const quant = [], huffman = [[], []], badDC = new Set(), icc = {count: 0, parts: []};
|
|
83
|
+
let frame = null, restartInterval = 0, adobeTransform = -1, jfif = false, orientation = 1, pos = 2;
|
|
84
|
+
const u16 = at => (bytes[at] << 8) | bytes[at + 1];
|
|
85
|
+
const signatureAt = (at, signature) => {
|
|
86
|
+
for (let i = 0; i < signature.length; i++) if (bytes[at + i] !== signature.charCodeAt(i)) return false;
|
|
87
|
+
return true;
|
|
88
|
+
};
|
|
89
|
+
for (;;) {
|
|
90
|
+
if (pos >= bytes.length) { if (frame && frame.scans) break; throw jpegError('truncated'); }
|
|
91
|
+
if (bytes[pos] !== 0xFF) throw jpegError('marker expected');
|
|
92
|
+
while (bytes[pos] === 0xFF) pos++;
|
|
93
|
+
const marker = bytes[pos++];
|
|
94
|
+
if (marker === 0xD9) break;
|
|
95
|
+
if (marker === 0xD8) throw jpegError('a second start');
|
|
96
|
+
if (marker >= 0xD0 && marker <= 0xD7) throw jpegError('a restart marker outside a scan');
|
|
97
|
+
if (marker === 0x01) continue;
|
|
98
|
+
if (pos + 2 > bytes.length) throw jpegError('truncated');
|
|
99
|
+
const length = u16(pos), segment = pos + 2, end = pos + length;
|
|
100
|
+
if (length < 2 || end > bytes.length) throw jpegError('bad segment');
|
|
101
|
+
pos = end;
|
|
102
|
+
if (marker === 0xDB) {
|
|
103
|
+
for (let at = segment; at < end;) {
|
|
104
|
+
const pq = bytes[at] >> 4, tq = bytes[at] & 15;
|
|
105
|
+
if (pq > 1 || tq > 3 || at + 1 + (pq ? 128 : 64) > end) throw jpegError('bad quantisation table');
|
|
106
|
+
at++;
|
|
107
|
+
const table = new Int32Array(64);
|
|
108
|
+
for (let k = 0; k < 64; k++) { table[ZIGZAG[k]] = pq ? u16(at) : bytes[at]; at += pq ? 2 : 1; if (!table[ZIGZAG[k]]) throw jpegError('a zero quantisation step'); }
|
|
109
|
+
if (frame?.progressive && frame.components.some(c => c.tq === tq && c.quant && c.coverage.some(v => v !== 0) && c.quant.some((v, k) => v !== table[k]))) throw jpegError('a quantisation table changed between progressive scans');
|
|
110
|
+
quant[tq] = table;
|
|
111
|
+
}
|
|
112
|
+
} else if (marker === 0xC4) {
|
|
113
|
+
for (let at = segment; at < end;) {
|
|
114
|
+
const tc = bytes[at] >> 4, th = bytes[at] & 15;
|
|
115
|
+
if (tc > 1 || th > 3 || at + 17 > end) throw jpegError('bad Huffman table');
|
|
116
|
+
at++;
|
|
117
|
+
const counts = bytes.subarray(at, at + 16);
|
|
118
|
+
let total = 0;
|
|
119
|
+
for (const c of counts) total += c;
|
|
120
|
+
if (total > 256 || at + 16 + total > end) throw jpegError('bad Huffman table');
|
|
121
|
+
const symbols = bytes.subarray(at + 16, at + 16 + total);
|
|
122
|
+
huffman[tc][th] = buildHuffman(counts, symbols);
|
|
123
|
+
// libjpeg refuses a DC table naming a symbol above 15 when a scan first reads DC through it, used or not.
|
|
124
|
+
if (tc === 0) { if (symbols.some(s => s > 15)) badDC.add(th); else badDC.delete(th); }
|
|
125
|
+
at += 16 + total;
|
|
126
|
+
}
|
|
127
|
+
} else if (marker === 0xC0 || marker === 0xC1 || marker === 0xC2) {
|
|
128
|
+
if (frame) throw jpegError('two frames');
|
|
129
|
+
if (end - segment < 6) throw jpegError('bad frame header');
|
|
130
|
+
if (bytes[segment] !== 8) throw jpegError('only 8-bit samples');
|
|
131
|
+
const height = u16(segment + 1), width = u16(segment + 3), count = bytes[segment + 5];
|
|
132
|
+
if (!width || !height) throw jpegError('a height left to a DNL marker');
|
|
133
|
+
if (count !== 1 && count !== 3) throw jpegError('only grey or three components');
|
|
134
|
+
if (end - segment < 6 + count * 3) throw jpegError('bad frame header');
|
|
135
|
+
// Admitted before a plane is allocated: the limits are the module's, the same the checked API keeps.
|
|
136
|
+
if (width > JPEG_LIMITS.edge || height > JPEG_LIMITS.edge) throw jpegError('a picture past ' + JPEG_LIMITS.edge + ' pixels on a side', 'JXL_DIMENSIONS');
|
|
137
|
+
if (width * height > JPEG_LIMITS.pixels) throw jpegError('a picture past ' + JPEG_LIMITS.pixels + ' pixels', 'JXL_DIMENSIONS');
|
|
138
|
+
const components = [];
|
|
139
|
+
let blocksPerMcu = 0;
|
|
140
|
+
for (let i = 0, at = segment + 6; i < count; i++, at += 3) {
|
|
141
|
+
const h = bytes[at + 1] >> 4, v = bytes[at + 1] & 15, tq = bytes[at + 2];
|
|
142
|
+
if (h < 1 || h > 4 || v < 1 || v > 4) throw jpegError('bad sampling');
|
|
143
|
+
if (tq > 3) throw jpegError('bad quantisation table');
|
|
144
|
+
if (components.some(c => c.id === bytes[at])) throw jpegError('two components with one id');
|
|
145
|
+
blocksPerMcu += h * v;
|
|
146
|
+
components.push({id: bytes[at], h, v, tq, pred: 0, coverage: new Int8Array(64).fill(-1)});
|
|
147
|
+
}
|
|
148
|
+
if (blocksPerMcu > 10) throw jpegError('bad sampling');
|
|
149
|
+
const hmax = Math.max(...components.map(c => c.h)), vmax = Math.max(...components.map(c => c.v));
|
|
150
|
+
const mcusX = Math.ceil(width / (8 * hmax)), mcusY = Math.ceil(height / (8 * vmax));
|
|
151
|
+
for (const c of components) {
|
|
152
|
+
c.blocksW = Math.ceil(Math.ceil(width * c.h / hmax) / 8); c.blocksH = Math.ceil(Math.ceil(height * c.v / vmax) / 8);
|
|
153
|
+
c.stride = mcusX * c.h; c.rows = mcusY * c.v;
|
|
154
|
+
c.coeffs = new Int16Array(c.stride * c.rows * 64);
|
|
155
|
+
}
|
|
156
|
+
frame = {progressive: marker === 0xC2, width, height, components, hmax, vmax, mcusX, mcusY, scans: 0};
|
|
157
|
+
} else if (marker === 0xC3 || (marker >= 0xC5 && marker <= 0xC7) || (marker >= 0xC9 && marker <= 0xCB) || (marker >= 0xCD && marker <= 0xCF)) {
|
|
158
|
+
throw jpegError('lossless, hierarchical or arithmetic coding');
|
|
159
|
+
} else if (marker === 0xDD) { if (length < 4) throw jpegError('bad segment'); restartInterval = u16(segment); }
|
|
160
|
+
else if (marker === 0xDC) throw jpegError('a DNL marker');
|
|
161
|
+
else if (marker === 0xDA) {
|
|
162
|
+
if (!frame) throw jpegError('a scan before the frame');
|
|
163
|
+
const n = bytes[segment], scan = [];
|
|
164
|
+
if (n < 1 || n > 4 || segment + 1 + n * 2 + 3 > end) throw jpegError('bad scan header');
|
|
165
|
+
let at = segment + 1;
|
|
166
|
+
for (let i = 0; i < n; i++, at += 2) {
|
|
167
|
+
const component = frame.components.find(c => c.id === bytes[at]);
|
|
168
|
+
if (!component) throw jpegError('a scan of an unknown component');
|
|
169
|
+
if (scan.some(s => s.component === component)) throw jpegError('a component twice in one scan');
|
|
170
|
+
// A table may be reused for another component after this one's scans. Keep the table that actually
|
|
171
|
+
// decoded each component, not whatever happens to occupy its slot at the end of the file.
|
|
172
|
+
if (!component.quant) component.quant = quant[component.tq];
|
|
173
|
+
if (!component.quant) throw jpegError('a scan without its quantisation table');
|
|
174
|
+
scan.push({component, dc: huffman[0][bytes[at + 1] >> 4], ac: huffman[1][bytes[at + 1] & 15], dcSlot: bytes[at + 1] >> 4});
|
|
175
|
+
}
|
|
176
|
+
const ss = bytes[at], se = bytes[at + 1], ah = bytes[at + 2] >> 4, al = bytes[at + 2] & 15;
|
|
177
|
+
if ((!frame.progressive || (ss === 0 && ah === 0)) && scan.some(s => badDC.has(s.dcSlot))) throw jpegError('bad Huffman table');
|
|
178
|
+
pos = decodeScan(bytes, end, frame, scan, ss, se, ah, al, restartInterval);
|
|
179
|
+
frame.scans++;
|
|
180
|
+
} else if (marker === 0xE0) {
|
|
181
|
+
if (signatureAt(segment, 'JFIF')) jfif = true;
|
|
182
|
+
} else if (marker === 0xE1) {
|
|
183
|
+
if (orientation === 1 && signatureAt(segment, 'Exif')) orientation = exifOrientation(bytes, segment + 6, end);
|
|
184
|
+
} else if (marker === 0xE2) {
|
|
185
|
+
if (length >= 16 && signatureAt(segment, 'ICC_PROFILE\0')) {
|
|
186
|
+
const seq = bytes[segment + 12], count = bytes[segment + 13];
|
|
187
|
+
if (!seq || !count || seq > count || (icc.count && icc.count !== count) || icc.parts[seq]) throw jpegError('a colour profile cut short');
|
|
188
|
+
icc.count = count; icc.parts[seq] = bytes.subarray(segment + 14, end);
|
|
189
|
+
}
|
|
190
|
+
} else if (marker === 0xEE) {
|
|
191
|
+
if (length >= 14 && signatureAt(segment, 'Adobe')) adobeTransform = bytes[segment + 11];
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
if (!frame || !frame.scans) throw jpegError('no picture');
|
|
195
|
+
for (const c of frame.components) {
|
|
196
|
+
for (let k = 0; k < 64; k++) if (c.coverage[k] !== 0) throw jpegError(frame.progressive ? 'a progressive JPEG whose scans do not finish every coefficient' : 'a component without its scan');
|
|
197
|
+
}
|
|
198
|
+
let colorSpace = 'srgb';
|
|
199
|
+
if (icc.count) {
|
|
200
|
+
const parts = [];
|
|
201
|
+
for (let i = 1; i <= icc.count; i++) { if (!icc.parts[i]) throw jpegError('a colour profile cut short'); parts.push(icc.parts[i]); }
|
|
202
|
+
const profile = new Uint8Array(parts.reduce((n, p) => n + p.length, 0));
|
|
203
|
+
for (let i = 0, at = 0; i < parts.length; at += parts[i].length, i++) profile.set(parts[i], at);
|
|
204
|
+
colorSpace = profileSpace(profile);
|
|
205
|
+
if (!colorSpace) throw jpegError('a colour profile other than sRGB or Display P3 (decode the picture and encode its pixels)');
|
|
206
|
+
}
|
|
207
|
+
// Colour as libjxl reads it: JFIF means YCbCr; else an Adobe marker decides; else component ids spelling RGB.
|
|
208
|
+
let rgb = false;
|
|
209
|
+
if (frame.components.length === 3 && !jfif) {
|
|
210
|
+
if (adobeTransform >= 0) rgb = adobeTransform === 0;
|
|
211
|
+
else rgb = frame.components[0].id === 0x52 && frame.components[1].id === 0x47 && frame.components[2].id === 0x42;
|
|
212
|
+
}
|
|
213
|
+
return {width: frame.width, height: frame.height, components: frame.components, progressive: frame.progressive, ycbcr: !rgb, orientation, colorSpace};
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// The Exif orientation tag (0x0112) of the first image directory, 1 when absent or unreadable.
|
|
217
|
+
function exifOrientation(bytes, start, end) {
|
|
218
|
+
const big = bytes[start] === 0x4D && bytes[start + 1] === 0x4D;
|
|
219
|
+
if (!big && !(bytes[start] === 0x49 && bytes[start + 1] === 0x49)) return 1;
|
|
220
|
+
const u16 = at => at + 2 > end ? 0 : big ? (bytes[at] << 8) | bytes[at + 1] : bytes[at] | (bytes[at + 1] << 8);
|
|
221
|
+
const u32 = at => at + 4 > end ? 0 : big ? ((bytes[at] << 24) | (bytes[at + 1] << 16) | (bytes[at + 2] << 8) | bytes[at + 3]) >>> 0 : (bytes[at] | (bytes[at + 1] << 8) | (bytes[at + 2] << 16) | (bytes[at + 3] << 24)) >>> 0;
|
|
222
|
+
if (u16(start + 2) !== 42) return 1;
|
|
223
|
+
const ifd = start + u32(start + 4), entries = u16(ifd);
|
|
224
|
+
for (let i = 0, at = ifd + 2; i < entries && at + 12 <= end; i++, at += 12) {
|
|
225
|
+
if (u16(at) !== 0x0112) continue;
|
|
226
|
+
const value = u16(at + 8);
|
|
227
|
+
return value >= 1 && value <= 8 ? value : 1;
|
|
228
|
+
}
|
|
229
|
+
return 1;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// What a scan may be, and what it leaves coded: sequential scans code a component whole at once; progressive scans
|
|
233
|
+
// code the DC first, then AC bands, each bit plane once, from the top down. `coverage[k]` is the lowest bit plane
|
|
234
|
+
// coefficient k has been coded to (-1 never), and the picture is complete only when every one reaches 0.
|
|
235
|
+
function admitScan(frame, scan, ss, se, ah, al) {
|
|
236
|
+
const progressive = frame.progressive;
|
|
237
|
+
if (!progressive) {
|
|
238
|
+
if (ss !== 0 || se !== 63 || ah !== 0 || al !== 0) throw jpegError('a bad spectral selection');
|
|
239
|
+
for (const s of scan) { if (s.component.coverage.some(v => v !== -1)) throw jpegError('a component coded twice'); s.component.coverage.fill(0); }
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
if (al > 13 || (ah && ah !== al + 1)) throw jpegError('a bad successive approximation');
|
|
243
|
+
if (ss === 0) {
|
|
244
|
+
if (se !== 0) throw jpegError('a bad spectral selection');
|
|
245
|
+
for (const {component} of scan) {
|
|
246
|
+
if (component.coverage[0] !== (ah ? ah : -1)) throw jpegError(ah ? 'a refinement scan out of order' : 'a DC scan twice');
|
|
247
|
+
component.coverage[0] = al;
|
|
248
|
+
}
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
if (scan.length !== 1) throw jpegError('an interleaved AC scan');
|
|
252
|
+
if (se > 63 || ss > se) throw jpegError('a bad spectral selection');
|
|
253
|
+
const {coverage} = scan[0].component;
|
|
254
|
+
if (coverage[0] === -1) throw jpegError('an AC scan before the DC scan');
|
|
255
|
+
for (let k = ss; k <= se; k++) {
|
|
256
|
+
if (coverage[k] !== (ah ? ah : -1)) throw jpegError(ah ? 'a refinement scan out of order' : 'a band coded twice');
|
|
257
|
+
coverage[k] = al;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function decodeScan(bytes, start, frame, scan, ss, se, ah, al, restartInterval) {
|
|
262
|
+
admitScan(frame, scan, ss, se, ah, al);
|
|
263
|
+
// `pad` counts the bits in `buffer` that are not the file's: zero bytes fed once a marker or the end is reached, so
|
|
264
|
+
// the lookahead always holds 24 bits. A code or a value that would take them is a scan cut short.
|
|
265
|
+
let pos = start, buffer = 0, count = 0, pad = 0, marker = false;
|
|
266
|
+
const fill = () => {
|
|
267
|
+
while (count <= 24) {
|
|
268
|
+
let byte = 0;
|
|
269
|
+
if (marker || pos >= bytes.length) { marker = true; pad += 8; }
|
|
270
|
+
else {
|
|
271
|
+
byte = bytes[pos];
|
|
272
|
+
if (byte === 0xFF) {
|
|
273
|
+
const next = bytes[pos + 1];
|
|
274
|
+
if (next === 0) pos += 2;
|
|
275
|
+
else if (next === 0xFF) { pos++; continue; }
|
|
276
|
+
else { marker = true; byte = 0; pad += 8; }
|
|
277
|
+
} else pos++;
|
|
278
|
+
}
|
|
279
|
+
buffer = ((buffer << 8) | byte) >>> 0; count += 8;
|
|
280
|
+
}
|
|
281
|
+
};
|
|
282
|
+
const short = () => jpegError('a scan cut short');
|
|
283
|
+
const receive = n => { if (!n) return 0; fill(); if (n > count - pad) throw short(); const value = (buffer >>> (count - n)) & ((1 << n) - 1); count -= n; return value; };
|
|
284
|
+
const extend = (value, n) => value < (1 << (n - 1)) ? value - (1 << n) + 1 : value;
|
|
285
|
+
const decode = table => {
|
|
286
|
+
if (!table) throw jpegError('a scan without its Huffman table');
|
|
287
|
+
fill();
|
|
288
|
+
const entry = table[(buffer >>> (count - 16)) & 0xFFFF];
|
|
289
|
+
if (!entry) throw jpegError('bad Huffman code');
|
|
290
|
+
if ((entry >> 8) > count - pad) throw short();
|
|
291
|
+
count -= entry >> 8;
|
|
292
|
+
return entry & 255;
|
|
293
|
+
};
|
|
294
|
+
// The bytes of an interval are consumed whole but for the last one's padding bits; then its marker, in order.
|
|
295
|
+
const atMarker = () => {
|
|
296
|
+
const remaining = count - pad;
|
|
297
|
+
if (remaining >= 8) throw jpegError('bytes left over in a scan');
|
|
298
|
+
if (eobrun) throw jpegError('an end-of-band run past its interval');
|
|
299
|
+
if (remaining && ((buffer >>> pad) & ((1 << remaining) - 1)) !== (1 << remaining) - 1) throw jpegError('bad scan padding');
|
|
300
|
+
while (pos + 1 < bytes.length && bytes[pos] === 0xFF && bytes[pos + 1] === 0xFF) pos++;
|
|
301
|
+
};
|
|
302
|
+
const restart = index => {
|
|
303
|
+
atMarker();
|
|
304
|
+
if (!(bytes[pos] === 0xFF && bytes[pos + 1] === 0xD0 + (index & 7))) throw jpegError('a restart marker missing or out of order');
|
|
305
|
+
pos += 2; buffer = 0; count = 0; pad = 0; marker = false;
|
|
306
|
+
for (const s of scan) s.component.pred = 0;
|
|
307
|
+
eobrun = 0;
|
|
308
|
+
};
|
|
309
|
+
const dcValue = (s, t) => {
|
|
310
|
+
if (t > 11) throw jpegError('a DC value out of range');
|
|
311
|
+
const diff = t ? extend(receive(t), t) : 0;
|
|
312
|
+
s.component.pred += diff;
|
|
313
|
+
if (s.component.pred > 2047 || s.component.pred < -2048) throw jpegError('a DC value out of range');
|
|
314
|
+
return s.component.pred;
|
|
315
|
+
};
|
|
316
|
+
const acValue = (size, shift) => {
|
|
317
|
+
if (size > 10) throw jpegError('an AC value out of range');
|
|
318
|
+
const value = extend(receive(size), size) * (1 << shift);
|
|
319
|
+
if (value > 1023 || value < -1023) throw jpegError('an AC value out of range');
|
|
320
|
+
return value;
|
|
321
|
+
};
|
|
322
|
+
let eobrun = 0;
|
|
323
|
+
const single = scan.length === 1, progressive = frame.progressive;
|
|
324
|
+
const baseline = (s, coeffs, at) => {
|
|
325
|
+
coeffs[at] = dcValue(s, decode(s.dc));
|
|
326
|
+
acFirst(s, coeffs, at);
|
|
327
|
+
};
|
|
328
|
+
const dcFirst = (s, coeffs, at) => {
|
|
329
|
+
const value = dcValue(s, decode(s.dc)) * (1 << al);
|
|
330
|
+
if (value > 2047 || value < -2048) throw jpegError('a DC value out of range');
|
|
331
|
+
coeffs[at] = value;
|
|
332
|
+
};
|
|
333
|
+
const dcRefine = (s, coeffs, at) => { if (receive(1)) coeffs[at] |= 1 << al; };
|
|
334
|
+
const pastBand = () => jpegError('coefficients past the ' + (progressive ? 'band' : 'block'));
|
|
335
|
+
// Sequential scans use this same first AC pass after their DC: admission fixes ss=0, se=63, al=0. Only a
|
|
336
|
+
// progressive scan may carry an end-of-band run across blocks; a sequential nonzero run still refuses here.
|
|
337
|
+
const acFirst = (s, coeffs, at) => {
|
|
338
|
+
if (eobrun > 0) { eobrun--; return; }
|
|
339
|
+
for (let k = ss || 1; k <= se;) {
|
|
340
|
+
const rs = decode(s.ac), r = rs >> 4, size = rs & 15;
|
|
341
|
+
if (!size) {
|
|
342
|
+
if (r < 15) {
|
|
343
|
+
if (r && !progressive) throw jpegError('an end-of-band run in a sequential scan');
|
|
344
|
+
eobrun = (1 << r) - 1 + (r ? receive(r) : 0); break;
|
|
345
|
+
}
|
|
346
|
+
k += 16; if (k > se + 1) throw pastBand(); continue;
|
|
347
|
+
}
|
|
348
|
+
k += r;
|
|
349
|
+
if (k > se) throw pastBand();
|
|
350
|
+
coeffs[at + ZIGZAG[k]] = acValue(size, al);
|
|
351
|
+
k++;
|
|
352
|
+
}
|
|
353
|
+
};
|
|
354
|
+
const acRefine = (s, coeffs, at) => {
|
|
355
|
+
const p1 = 1 << al, m1 = -1 << al;
|
|
356
|
+
let k = ss;
|
|
357
|
+
if (eobrun <= 0) {
|
|
358
|
+
for (; k <= se; k++) {
|
|
359
|
+
const rs = decode(s.ac);
|
|
360
|
+
let r = rs >> 4, value = 0;
|
|
361
|
+
const size = rs & 15;
|
|
362
|
+
if (size) { if (size !== 1) throw jpegError('a bad refinement code'); value = receive(1) ? p1 : m1; }
|
|
363
|
+
else if (r !== 15) { eobrun = 1 << r; if (r) eobrun += receive(r); break; }
|
|
364
|
+
for (; k <= se; k++) {
|
|
365
|
+
const z = at + ZIGZAG[k];
|
|
366
|
+
if (coeffs[z] !== 0) { if (receive(1) && (coeffs[z] & p1) === 0) coeffs[z] += coeffs[z] >= 0 ? p1 : m1; }
|
|
367
|
+
else if (--r < 0) break;
|
|
368
|
+
}
|
|
369
|
+
if (r >= 0 || (value && k > se)) throw jpegError('coefficients past the band');
|
|
370
|
+
if (value) coeffs[at + ZIGZAG[k]] = value;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
if (eobrun > 0) {
|
|
374
|
+
for (; k <= se; k++) {
|
|
375
|
+
const z = at + ZIGZAG[k];
|
|
376
|
+
if (coeffs[z] !== 0 && receive(1) && (coeffs[z] & p1) === 0) coeffs[z] += coeffs[z] >= 0 ? p1 : m1;
|
|
377
|
+
}
|
|
378
|
+
eobrun--;
|
|
379
|
+
}
|
|
380
|
+
};
|
|
381
|
+
const block = !progressive ? baseline : ss === 0 ? (ah === 0 ? dcFirst : dcRefine) : (ah === 0 ? acFirst : acRefine);
|
|
382
|
+
for (const s of scan) s.component.pred = 0;
|
|
383
|
+
const first = scan[0].component;
|
|
384
|
+
const total = single ? first.blocksW * first.blocksH : frame.mcusX * frame.mcusY;
|
|
385
|
+
for (let unit = 0; unit < total; unit++) {
|
|
386
|
+
if (restartInterval && unit > 0 && unit % restartInterval === 0) restart(unit / restartInterval - 1);
|
|
387
|
+
if (single) {
|
|
388
|
+
const by = (unit / first.blocksW) | 0, bx = unit - by * first.blocksW;
|
|
389
|
+
block(scan[0], first.coeffs, (by * first.stride + bx) * 64);
|
|
390
|
+
} else {
|
|
391
|
+
const my = (unit / frame.mcusX) | 0, mx = unit - my * frame.mcusX;
|
|
392
|
+
for (const s of scan) {
|
|
393
|
+
const c = s.component;
|
|
394
|
+
for (let v = 0; v < c.v; v++) for (let h = 0; h < c.h; h++) block(s, c.coeffs, ((my * c.v + v) * c.stride + mx * c.h + h) * 64);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
// The scan ends at a marker or EOF. A final complete interval may carry its next restart marker, once.
|
|
399
|
+
atMarker();
|
|
400
|
+
if (bytes[pos] === 0xFF && bytes[pos + 1] >= 0xD0 && bytes[pos + 1] <= 0xD7) {
|
|
401
|
+
if (!restartInterval || total % restartInterval || bytes[pos + 1] !== 0xD0 + ((total / restartInterval - 1) & 7)) throw jpegError('a restart marker out of order');
|
|
402
|
+
pos += 2;
|
|
403
|
+
while (pos + 1 < bytes.length && bytes[pos] === 0xFF && bytes[pos + 1] === 0xFF) pos++;
|
|
404
|
+
}
|
|
405
|
+
if (pos < bytes.length && !(bytes[pos] === 0xFF && pos + 1 < bytes.length && bytes[pos + 1] !== 0 && bytes[pos + 1] !== 0xFF)) throw jpegError('bytes left over after a scan');
|
|
406
|
+
return pos;
|
|
407
|
+
}
|
package/jpeg.d.mts
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
1
|
// SPDX-License-Identifier: MIT
|
|
2
|
-
|
|
3
|
-
export type {
|
|
2
|
+
import type {Job, Limits} from './index.mjs';
|
|
3
|
+
export type {Limits, ErrorCode, EncoderError, Job} from './index.mjs';
|
|
4
|
+
/** This door's limits, by its memory: the 16 MiB codestream and JPEG, 64 million pixels, 16,384 on a side. */
|
|
5
|
+
export const LIMITS: Limits;
|
|
6
|
+
export interface Transcoded {
|
|
7
|
+
/** A bare JPEG XL codestream, the .jxl file's bytes. */ bytes: Uint8Array;
|
|
8
|
+
/** The picture's size as shown, swapped when the Exif orientation turns it. */ width: number; height: number;
|
|
9
|
+
/** The Exif orientation, 1 to 8, kept in the JPEG XL header. */ orientation: number;
|
|
10
|
+
}
|
|
11
|
+
/** A JPEG's bytes to JPEG XL, its coefficients carried; a JPEG this path does not take is refused as JXL_JPEG. */
|
|
12
|
+
export function transcode(jpeg: Uint8Array): Transcoded;
|
|
13
|
+
/** transcode's work as steps; the first step reads the scans and sets the picture's size as shown and orientation. */
|
|
14
|
+
export interface TranscodeJob extends Job { width?: number; height?: number; orientation?: number; }
|
|
15
|
+
export function transcodeSteps(jpeg: Uint8Array): TranscodeJob;
|
package/jpeg.min.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
/*! Rapier JXL
|
|
2
|
-
function t(t){return t>=0?2*t:2*-t-1}function o(t){return 31-Math.clz32(t)}function n(t){return t<=1?0:32-Math.clz32(t-1)}function r(t){if(0===t)return 0;const o=t<0?32768:0;if(!((t=Math.abs(t))<65520))throw new Error("half float out of range: "+t);let n=Math.floor(Math.log2(t)),r=t/2**n-1;n<-14&&(r=t/2**-14,n=-15);let e=Math.round(1024*r);return 1024===e&&(e=0,n++),o|n+15<<10|e}function e(t,o){const n=new Uint8Array(t.length),r=[];for(let o=0;o<t.length;o++)t[o]>0&&r.push(o);if(!r.length)return n;if(1===r.length)return n[r[0]]=1,n;for(let e=1;;e*=2){const i=r.map(o=>({w:Math.max(t[o],e),t:o,l:null,r:null})).sort((t,o)=>t.w-o.w||t.t-o.t),s=[];let f=0,c=0;const a=()=>f<i.length&&(c>=s.length||i[f].w<=s[c].w)?i[f++]:s[c++];for(;i.length-f+(s.length-c)>1;){const t=a(),o=a();s.push({w:t.w+o.w,t:-1,l:t,r:o})}const h=[[a(),0]];let l=0;for(;h.length;){const[t,o]=h.pop();t.t>=0?(n[t.t]=o,o>l&&(l=o)):h.push([t.l,o+1],[t.r,o+1])}if(l<=o)return n}}function i(t,o,n,r,e,i){const s=[];for(;s.push(r&e),r>>=i;)r--;for(let r=s.length-1;r>=0;r--)t.push(n),o.push(s[r])}function s(t,o){let n=0;for(let r=0;r<o;r++)n=n<<1|1&t,t>>=1;return n}function f(t){const o=new Uint32Array(17),n=new Uint32Array(17),r=new Uint16Array(t.length);for(const n of t)o[n]++;o[0]=0;for(let t=1,r=0;t<=16;t++)r=r+o[t-1]<<1,n[t]=r;for(let o=0;o<t.length;o++)t[o]&&(r[o]=s(n[t[o]]++,t[o]));return r}function c(t){const o=[];for(let n=0;n<t.length;n++)t[n]>0&&o.push(n);const n=o.length,r=o[n-1]+1||1,i=n<=4?new Uint8Array(r):e(t,15).subarray(0,r);let s=0;if(n<=4){o.sort((o,n)=>t[n]-t[o]||o-n),s=4===n&&t[o[0]]>t[o[2]]+t[o[3]]?1:0;const r=n<2?[0]:2===n?[1,1]:3===n?[1,2,2]:s?[1,2,3,3]:[2,2,2,2];o.forEach((t,o)=>{i[t]=r[o]}),o.sort((t,o)=>i[t]-i[o]||t-o)}return{o:r,i:i,u:f(i),p:n&&n<=4?o:null,m:s}}function a(t,n){if(n.o<=1)return;if(n.p){const r=o(n.o-1)+1;t.write(2,1),t.write(2,n.p.length-1);for(const o of n.p)t.write(r,o);return void(4===n.p.length&&t.write(1,n.m))}const r=n.i,s=[],c=[];let a=r.length-1;for(;a>0&&!r[a];)a--;let h=8;for(let t=0;t<=a;){const o=r[t];let n=1;for(;t+n<=a&&r[t+n]===o;)n++;let e=n;if(o&&o!==h&&(s.push(o),c.push(0),e--,h=o),e<3)for(let t=0;t<e;t++)s.push(o),c.push(0);else i(s,c,o?16:17,e-3,o?3:7,o?2:3);t+=n}const l=new Uint32Array(18);for(const t of s)l[t]++;let u=0;for(const t of l)t&&u++;1===u&&(l[s[0]?0:1]=l[s[0]]);const w=e(l,5),d=f(w);let p=18;for(;p>0&&!w[O[p-1]];)p--;t.write(2,0);for(let o=0;o<p;o++){const n=w[O[o]];t.write(D[n],G[n])}for(let o=0;o<s.length;o++){const n=s[o];t.write(w[n],d[n]),16===n?t.write(2,c[o]):17===n&&t.write(3,c[o])}}function h(t,o=0,n=0){return{split:t,M:o,A:n,U:1<<t}}function l(t,o,r=15){t.write(n(r+1),o.split),o.split!==r&&(t.write(n(o.split+1),o.M),t.write(n(o.split-o.M+1),o.A))}function u(t,n,r){if(n<t.U)return r[0]=n,r[1]=0,void(r[2]=0);const e=o(n),i=n-(1<<e),s=e-t.M-t.A;r[0]=t.U+(e-t.split<<t.M+t.A|i>>e-t.M<<t.A|i&(1<<t.A)-1),r[1]=s,r[2]=n>>t.A&(1<<s)-1}function w(t,o,n,r=0){u(t,o,X),n[r+X[0]]++}function d(t,o,n,r,e=0){u(n,r,X);const i=e+X[0];t.write(o.i[i],o.u[i]),X[1]&&t.write(X[1],X[2])}function p(t,o){let r=0;for(const t of o)r=Math.max(r,t);const e=r?n(r+1):0;if(e<=3){if(t.write(1,1),t.write(2,e),e)for(const n of o)t.write(e,n);return}t.write(1,0),t.write(1,0);const i=h(8),s=h(4),f=o.length>=64,a=new Uint32Array(f?257:r+1),l=[];for(let t=0;t<o.length;){let n=1;for(;f&&t+n<o.length&&o[t+n]===o[t];)n++;if(n>=8)l.push([o[t],n-1]),a[o[t]]++,w(s,n-1-7,a,224),t+=n;else{for(let r=0;r<n;r++)l.push([o[t],0]),a[o[t]]++;t+=n}}const u=c(a);if(!f){m(t,{I:new Uint8Array(1),S:[{config:i,code:u}]});for(const[o]of l)d(t,u,i,o);return}const p=new Uint32Array(1);p[0]=1,m(t,{J:{C:224,minLength:7,k:s},I:new Uint8Array([1,0]),S:[{config:h(0),code:c(p)},{config:i,code:u}]});for(const[o,n]of l)d(t,u,i,o),n&&d(t,u,s,n-7,224)}function m(t,{J:n=null,I:r,S:e}){t.write(1,n?1:0),n&&(t.L([[0,224],[0,512],[0,4096],[15,8]],n.C),t.L([[0,3],[0,4],[2,5],[8,9]],n.minLength),l(t,n.k,8)),r.length>1&&p(t,r),t.write(1,1);for(const o of e)l(t,o.config,15);for(const n of e){const r=n.code.o-1;if(r){const n=o(r);t.write(1,1),t.write(4,n),t.write(n,r-(1<<n))}else t.write(1,0)}for(const o of e)a(t,o.code)}function b(t,o){const n=[[3,0],[6,8],[10,72],[13,1096]];if("rct"===o.type)t.write(2,0),t.L(n,o.N),t.L([[0,6],[2,0],[4,2],[6,10]],o.O??6);else if("palette"===o.type)t.write(2,1),t.L(n,o.N),t.L([[0,1],[0,3],[0,4],[13,1]],o.D),t.L([[8,0],[10,256],[12,1280],[16,5376]],o.G),t.L([[0,0],[8,1],[10,257],[16,1281]],o.X??0),t.write(4,o._??0);else{if("squeeze"!==o.type)throw new Error("unknown transform "+o.type);{t.write(2,2);const r=o.P??[];t.L([[0,0],[4,1],[6,9],[8,41]],r.length);for(const o of r)t.write(1,o.q?1:0),t.write(1,o.T?1:0),t.L(n,o.N),t.L([[0,1],[0,2],[0,3],[4,4]],o.D)}}}function y(o,n,r,e,i,s){const f=s._,c=s.offset,a=s.B,h=q,l=_;let u=0;const p=(t,r=h,e=0)=>{o?d(o,n,r,t,e):w(r,t,n,e)},m=()=>{if(u){if(u>=8)p(0),p(u-7-1,l,224);else for(let t=0;t<u;t++)p(0);u=0}},b=(o,n)=>{const e=r[o];let i;1===a?i=e-n-c:(i=Math.round((e-n-c)/a),r[o]=n+c+i*a),0!==i?(m(),p(t(i))):u++};for(let t=0,o=0;t<i;t++)for(let n=0;n<e;n++,o++){let i;if(0===f)i=0;else{const s=n?r[o-1]:t?r[o-e]:0,c=t?r[o-e]:s;if(1===f)i=s;else if(2===f)i=c;else if(3===f)i=(s+c)/2|0;else{const a=s+c-(n&&t?r[o-e-1]:s);if(5===f){const t=s<c?s:c,o=s<c?c:s;i=a<t?t:a>o?o:a}else{if(4!==f)throw new Error("predictor "+f+" is not coded here");i=Math.abs(a-s)<Math.abs(a-c)?s:c}}}b(o,i)}m()}function M(t,o){t.L([[9,1],[13,1],[18,1],[30,1]],o)}function g(t,o="JXL_JPEG"){return Object.assign(new Error("JPEG: "+t),{code:o})}function A(t,o){const n=new Uint16Array(65536);let r=0,e=0;for(let i=1;i<=16;i++){for(let s=0;s<t[i-1];s++){const t=o[e++],s=r<<16-i,f=1<<16-i;if(s+f>=65536)throw g("bad Huffman table");n.fill(i<<8|t,s,s+f),r++}r<<=1}return n}function U(t){if(!(t.length>4)||255!==t[0]||216!==t[1])throw g("not a JPEG");const o=[],n=[[],[]],r=new Set,e={count:0,H:[]};let i=null,s=0,f=-1,c=!1,a=1,h=2;const l=o=>t[o]<<8|t[o+1],u=(o,n)=>{for(let r=0;r<n.length;r++)if(t[o+r]!==n.charCodeAt(r))return!1;return!0};for(;;){if(h>=t.length){if(i&&i.scans)break;throw g("truncated")}if(255!==t[h])throw g("marker expected");for(;255===t[h];)h++;const w=t[h++];if(217===w)break;if(216===w)throw g("a second start");if(w>=208&&w<=215)throw g("a restart marker outside a scan");if(1===w)continue;if(h+2>t.length)throw g("truncated");const d=l(h),p=h+2,m=h+d;if(d<2||m>t.length)throw g("bad segment");if(h=m,219===w)for(let n=p;n<m;){const r=t[n]>>4,e=15&t[n];if(r>1||e>3||n+1+(r?128:64)>m)throw g("bad quantisation table");n++;const s=new Int32Array(64);for(let o=0;o<64;o++)if(s[T[o]]=r?l(n):t[n],n+=r?2:1,!s[T[o]])throw g("a zero quantisation step");if(i?.progressive&&i.components.some(t=>t.tq===e&&t.quant&&t.coverage.some(t=>0!==t)&&t.quant.some((t,o)=>t!==s[o])))throw g("a quantisation table changed between progressive scans");o[e]=s}else if(196===w)for(let o=p;o<m;){const e=t[o]>>4,i=15&t[o];if(e>1||i>3||o+17>m)throw g("bad Huffman table");o++;const s=t.subarray(o,o+16);let f=0;for(const t of s)f+=t;if(f>256||o+16+f>m)throw g("bad Huffman table");const c=t.subarray(o+16,o+16+f);n[e][i]=A(s,c),0===e&&(c.some(t=>t>15)?r.add(i):r.delete(i)),o+=16+f}else if(192===w||193===w||194===w){if(i)throw g("two frames");if(m-p<6)throw g("bad frame header");if(8!==t[p])throw g("only 8-bit samples");const o=l(p+1),n=l(p+3),r=t[p+5];if(!n||!o)throw g("a height left to a DNL marker");if(1!==r&&3!==r)throw g("only grey or three components");if(m-p<6+3*r)throw g("bad frame header");if(n>k.edge||o>k.edge)throw g("a picture past "+k.edge+" pixels on a side","JXL_DIMENSIONS");if(n*o>k.pixels)throw g("a picture past "+k.pixels+" pixels","JXL_DIMENSIONS");const e=[];let s=0;for(let o=0,n=p+6;o<r;o++,n+=3){const o=t[n+1]>>4,r=15&t[n+1],i=t[n+2];if(o<1||o>4||r<1||r>4)throw g("bad sampling");if(i>3)throw g("bad quantisation table");if(e.some(o=>o.id===t[n]))throw g("two components with one id");s+=o*r,e.push({id:t[n],h:o,v:r,tq:i,pred:0,coverage:new Int8Array(64).fill(-1)})}if(s>10)throw g("bad sampling");const f=Math.max(...e.map(t=>t.h)),c=Math.max(...e.map(t=>t.v)),a=Math.ceil(n/(8*f)),h=Math.ceil(o/(8*c));for(const t of e)t.blocksW=Math.ceil(Math.ceil(n*t.h/f)/8),t.blocksH=Math.ceil(Math.ceil(o*t.v/c)/8),t.stride=a*t.h,t.rows=h*t.v,t.coeffs=new Int16Array(t.stride*t.rows*64);i={progressive:194===w,width:n,height:o,components:e,hmax:f,vmax:c,mcusX:a,mcusY:h,scans:0}}else{if(195===w||w>=197&&w<=199||w>=201&&w<=203||w>=205&&w<=207)throw g("lossless, hierarchical or arithmetic coding");if(221===w){if(d<4)throw g("bad segment");s=l(p)}else{if(220===w)throw g("a DNL marker");if(218===w){if(!i)throw g("a scan before the frame");const e=t[p],f=[];if(e<1||e>4||p+1+2*e+3>m)throw g("bad scan header");let c=p+1;for(let r=0;r<e;r++,c+=2){const r=i.components.find(o=>o.id===t[c]);if(!r)throw g("a scan of an unknown component");if(f.some(t=>t.component===r))throw g("a component twice in one scan");if(r.quant||(r.quant=o[r.tq]),!r.quant)throw g("a scan without its quantisation table");f.push({component:r,dc:n[0][t[c+1]>>4],ac:n[1][15&t[c+1]],dcSlot:t[c+1]>>4})}const a=t[c],l=t[c+1],u=t[c+2]>>4,w=15&t[c+2];if((!i.progressive||0===a&&0===u)&&f.some(t=>r.has(t.dcSlot)))throw g("bad Huffman table");h=I(t,m,i,f,a,l,u,w,s),i.scans++}else if(224===w)u(p,"JFIF")&&(c=!0);else if(225===w)1===a&&u(p,"Exif")&&(a=x(t,p+6,m));else if(226===w){if(d>=16&&u(p,"ICC_PROFILE\0")){const o=t[p+12],n=t[p+13];if(!o||!n||o>n||e.count&&e.count!==n||e.H[o])throw g("a colour profile cut short");e.count=n,e.H[o]=t.subarray(p+14,m)}}else 238===w&&d>=14&&u(p,"Adobe")&&(f=t[p+11])}}}if(!i||!i.scans)throw g("no picture");for(const t of i.components)for(let o=0;o<64;o++)if(0!==t.coverage[o])throw g(i.progressive?"a progressive JPEG whose scans do not finish every coefficient":"a component without its scan");if(e.count&&!function(t){const o=[];for(let n=1;n<=t.count;n++){if(!t.H[n])throw g("a colour profile cut short");o.push(t.H[n])}const n=o.reduce((t,o)=>t+o.length,0),r=new Uint8Array(n);for(let t=0,n=0;t<o.length;n+=o[t].length,t++)r.set(o[t],n);const e=t=>t+4<=n?(r[t]<<24|r[t+1]<<16|r[t+2]<<8|r[t+3])>>>0:0,i=t=>t+4<=n?String.fromCharCode(r[t],r[t+1],r[t+2],r[t+3]):"",s=e(128);if(n<132||132+12*s>n)return!1;for(let t=0;t<s;t++){const o=132+12*t;if("desc"!==i(o))continue;const s=e(o+4),f=e(o+8);if(s+f>n||f<16)return!1;let c="";if("desc"===i(s)){const t=Math.min(e(s+8),f-12);for(let o=0;o<t;o++)c+=String.fromCharCode(r[s+12+o])}else{if("mluc"!==i(s))return!1;{if(!e(s+8)||f<28)return!1;const t=e(s+20),o=s+e(s+24);if(o+t>n)return!1;for(let n=0;n+1<t;n+=2)c+=String.fromCharCode(r[o+n]<<8|r[o+n+1])}}return/sRGB|sGray|IEC ?61966/i.test(c)}return!1}(e))throw g("a colour profile other than sRGB (decode the picture and encode its pixels)");let w=!1;return 3!==i.components.length||c||(w=f>=0?0===f:82===i.components[0].id&&71===i.components[1].id&&66===i.components[2].id),{width:i.width,height:i.height,components:i.components,progressive:i.progressive,ycbcr:!w,orientation:a}}function x(t,o,n){const r=77===t[o]&&77===t[o+1];if(!r&&(73!==t[o]||73!==t[o+1]))return 1;const e=o=>o+2>n?0:r?t[o]<<8|t[o+1]:t[o]|t[o+1]<<8;if(42!==e(o+2))return 1;const i=o+((f=o+4)+4>n?0:r?(t[f]<<24|t[f+1]<<16|t[f+2]<<8|t[f+3])>>>0:(t[f]|t[f+1]<<8|t[f+2]<<16|t[f+3]<<24)>>>0),s=e(i);var f;for(let t=0,o=i+2;t<s&&o+12<=n;t++,o+=12){if(274!==e(o))continue;const t=e(o+8);return t>=1&&t<=8?t:1}return 1}function I(t,o,n,r,e,i,s,f,c){!function(t,o,n,r,e,i){if(!t.progressive){if(0!==n||63!==r||0!==e||0!==i)throw g("a bad spectral selection");for(const t of o){if(t.component.coverage.some(t=>-1!==t))throw g("a component coded twice");t.component.coverage.fill(0)}return}if(i>13||e&&e!==i+1)throw g("a bad successive approximation");if(0===n){if(0!==r)throw g("a bad spectral selection");for(const{component:t}of o){if(t.coverage[0]!==(e||-1))throw g(e?"a refinement scan out of order":"a DC scan twice");t.coverage[0]=i}return}if(1!==o.length)throw g("an interleaved AC scan");if(r>63||n>r)throw g("a bad spectral selection");const{coverage:s}=o[0].component;if(-1===s[0])throw g("an AC scan before the DC scan");for(let t=n;t<=r;t++){if(s[t]!==(e||-1))throw g(e?"a refinement scan out of order":"a band coded twice");s[t]=i}}(n,r,e,i,s,f);let a=o,h=0,l=0,u=0,w=!1;const d=()=>{for(;l<=24;){let o=0;if(w||a>=t.length)w=!0,u+=8;else if(o=t[a],255===o){const n=t[a+1];if(0===n)a+=2;else{if(255===n){a++;continue}w=!0,o=0,u+=8}}else a++;h=(h<<8|o)>>>0,l+=8}},p=()=>g("a scan cut short"),m=t=>{if(!t)return 0;if(d(),t>l-u)throw p();const o=h>>>l-t&(1<<t)-1;return l-=t,o},b=(t,o)=>t<1<<o-1?t-(1<<o)+1:t,y=t=>{if(!t)throw g("a scan without its Huffman table");d();const o=t[h>>>l-16&65535];if(!o)throw g("bad Huffman code");if(o>>8>l-u)throw p();return l-=o>>8,255&o},M=()=>{const o=l-u;if(o>=8)throw g("bytes left over in a scan");if(I)throw g("an end-of-band run past its interval");if(o&&(h>>>u&(1<<o)-1)!=(1<<o)-1)throw g("bad scan padding");for(;a+1<t.length&&255===t[a]&&255===t[a+1];)a++},A=o=>{if(M(),255!==t[a]||t[a+1]!==208+(7&o))throw g("a restart marker missing or out of order");a+=2,h=0,l=0,u=0,w=!1;for(const t of r)t.component.pred=0;I=0},U=(t,o)=>{if(o>11)throw g("a DC value out of range");const n=o?b(m(o),o):0;if(t.component.pred+=n,t.component.pred>2047||t.component.pred<-2048)throw g("a DC value out of range");return t.component.pred},x=(t,o)=>{if(t>10)throw g("an AC value out of range");const n=b(m(t),t)*(1<<o);if(n>1023||n<-1023)throw g("an AC value out of range");return n};let I=0;const E=1===r.length,v=n.progressive,S=()=>g("coefficients past the "+(v?"band":"block")),J=(t,o,n)=>{if(I>0)I--;else for(let r=e||1;r<=i;){const e=y(t.ac),s=e>>4,c=15&e;if(c){if(r+=s,r>i)throw S();o[n+T[r]]=x(c,f),r++}else{if(s<15){if(s&&!v)throw g("an end-of-band run in a sequential scan");I=(1<<s)-1+(s?m(s):0);break}if(r+=16,r>i+1)throw S()}}},C=v?0===e?0===s?(t,o,n)=>{const r=U(t,y(t.dc))*(1<<f);if(r>2047||r<-2048)throw g("a DC value out of range");o[n]=r}:(t,o,n)=>{m(1)&&(o[n]|=1<<f)}:0===s?J:(t,o,n)=>{const r=1<<f,s=-1<<f;let c=e;if(I<=0)for(;c<=i;c++){const e=y(t.ac);let f=e>>4,a=0;const h=15&e;if(h){if(1!==h)throw g("a bad refinement code");a=m(1)?r:s}else if(15!==f){I=1<<f,f&&(I+=m(f));break}for(;c<=i;c++){const t=n+T[c];if(0!==o[t])m(1)&&0===(o[t]&r)&&(o[t]+=o[t]>=0?r:s);else if(--f<0)break}if(f>=0||a&&c>i)throw g("coefficients past the band");a&&(o[n+T[c]]=a)}if(I>0){for(;c<=i;c++){const t=n+T[c];0!==o[t]&&m(1)&&0===(o[t]&r)&&(o[t]+=o[t]>=0?r:s)}I--}}:(t,o,n)=>{o[n]=U(t,y(t.dc)),J(t,o,n)};for(const t of r)t.component.pred=0;const k=r[0].component,L=E?k.blocksW*k.blocksH:n.mcusX*n.mcusY;for(let t=0;t<L;t++)if(c&&t>0&&t%c===0&&A(t/c-1),E){const o=t/k.blocksW|0,n=t-o*k.blocksW;C(r[0],k.coeffs,64*(o*k.stride+n))}else{const o=t/n.mcusX|0,e=t-o*n.mcusX;for(const t of r){const n=t.component;for(let r=0;r<n.v;r++)for(let i=0;i<n.h;i++)C(t,n.coeffs,64*((o*n.v+r)*n.stride+e*n.h+i))}}if(M(),255===t[a]&&t[a+1]>=208&&t[a+1]<=215){if(!c||L%c||t[a+1]!==208+(L/c-1&7))throw g("a restart marker out of order");for(a+=2;a+1<t.length&&255===t[a]&&255===t[a+1];)a++}if(a<t.length&&!(255===t[a]&&a+1<t.length&&0!==t[a+1]&&255!==t[a+1]))throw g("bytes left over after a scan");return a}function E(t,o){return u(t,o,j),j[0]}function v(t){let o=0,n=0;for(const n of t)o+=n;for(const r of t)r&&(n+=r*Math.log2(o/r));return n}function S(t,o,n){const r=[];if(o>0){const n=new Uint32Array(2048);let e=0;for(const o of t)n[Math.min(2047,Math.max(0,o+1024))]++,e++;let i=0,s=e/(o+1);for(let t=0;t<2048&&r.length<o;t++)i+=n[t],i>s&&(r.push(t-1025),s=e*(r.length+1)/(o+1))}const e=r.length+1,i=new Uint8Array(39*e);for(let t=0;t<e;t++)for(let o=0;o<F;o++)i[(0+o)*e+t]=t,i[(13+o)*e+t]=n?e:e+(t>>1),i[(26+o)*e+t]=n?e:e+(e-1>>1)+1+(t>>1);let s=0;for(const t of i)s=Math.max(s,t+1);return{thresholds:r,R:e,map:i,j:s,F:t=>{let o=0;for(const n of r)t>n&&o++;return o},Z:(t,o)=>i[(t<2?1^t:2)*F*e+o],Y:(t,o)=>(Math.min(t,64)<8?Math.min(t,64):4+(Math.min(t,64)>>1))*s+o,K:t=>37*s+458*t,V:495*s}}function J(o,n,r){const e=n.map(()=>function(t,o=0,n=1){return{_:t,offset:o,B:n,context:-1}}(r)),i=e.map(()=>new Uint32Array(257));n.forEach((t,o)=>y(null,i[o],t.data,t.w,t.h,e[o])),function(t,{W:o=!0,$:n=[]}={}){t.write(1,o?1:0),t.write(1,1),t.L([[0,0],[0,1],[4,2],[8,18]],n.length);for(const o of n)b(t,o)}(o,{W:!1,$:[]});const s=function(o,n){const r=[n],e=[],i=[];for(const o of r){if(o.left){e.push(o.tt+1,t(o.ot)),r.push(o.left,o.right);continue}o.context=i.length,i.push(o);const n=1===o.B?0:31-Math.clz32(o.B&-o.B);e.push(0,o._,t(o.offset),n,(o.B>>n)-1)}const s=new Uint32Array(64);for(const t of e)w(q,t,s);const f=c(s);m(o,{I:new Uint8Array(6),S:[{config:q,code:f}]});for(const t of e)d(o,f,q,t);return i}(o,function(t){return function(t,o){const n=(r,e)=>{if(r===e)return o?t[r].nt:t[r];const i=r+e>>1;return function(t,o,n,r){return{tt:t,ot:o,left:n,right:r}}(o,o?t[i].rt:i,n(i+1,e),n(r,i))};return n(0,t.length-1)}(t,0)}(e)),f=function(t,o,n,r=t=>t.context){const e=new Uint8Array(o.length+1);for(const t of o)e[t.context]=r(t)+1;const i=new Uint32Array(2);i[1]=1;const s=[{config:q,code:c(i)}],f=Math.max(...e);for(let t=0;t<f;t++)s.push({config:q,code:c(n[t])});return m(t,{J:P,I:e,S:s}),s}(o,s,i,t=>e.indexOf(t));n.forEach((t,n)=>y(o,f[n+1].code,t.data,t.w,t.h,e[n]))}function C(o){if(!(o instanceof Uint8Array&&o.length))throw V("JXL_INPUT","A JPEG is a non-empty Uint8Array.");if(o.length>k.bytes)throw V("JXL_SIZE","A JPEG is at most 16 MiB.");return function(t){try{return t()}catch(t){if(t instanceof RangeError&&!t.code)throw V("JXL_MEMORY","Not enough memory for this picture.");throw t}}(()=>{const e=U(o);!function(t,o){if(!Number.isInteger(t)||!Number.isInteger(o)||t<1||o<1)throw V("JXL_INPUT","A picture is at least one pixel wide and high, in whole pixels.");if(t>k.edge||o>k.edge)throw V("JXL_DIMENSIONS","A picture is at most "+k.edge+" pixels on a side.");if(t*o>k.pixels)throw V("JXL_DIMENSIONS","A picture is at most "+k.pixels.toLocaleString("en-US")+" pixels.")}(e.width,e.height);const i=function(t){if(t.length>k.bytes)throw V("JXL_SIZE","The encoded picture exceeds 16 MiB.");return t}(function(o,e=U(o)){return function(o){const{width:e,height:i,components:s,alpha:f}=o,a=1===s.length,h=o.quantScale||1,l=o.ycbcr||a,u=(a?[0,0,0]:l?[1,0,2]:[0,1,2]).map(t=>s[t]),w=t=>1===t?0:2===t?1:-1,b=u.map(t=>w(t.h)),y=u.map(t=>w(t.v));if(b.includes(-1)||y.includes(-1))throw g("sampling factors other than one or two");const A=Math.max(...b),U=Math.max(...y),x=t=>A-b[t],I=t=>U-y[t];if(!l&&(A||U))throw g("an RGB JPEG with subsampling");const E=Math.ceil(e/(8<<A))<<A,C=Math.ceil(i/(8<<U))<<U,k=Math.ceil(e/z),L=Math.ceil(i/z),O=k*L,D=Math.ceil(E/256),G=D*Math.ceil(C/256),X=1===O,_=(t,o,n)=>{const r=u[t];return a&&1!==t?-1:64*(n*r.stride+o)},P=t=>{let o=1024;for(;t%o;)o>>=1;return o},q=u.map(t=>l?1:t.quant[0]/P(t.quant[0])),T=u.map(t=>l?0:1024/P(t.quant[0])),j=new Int32Array(E*C);for(let t=0;t<C;t++)for(let o=0;o<E;o++)j[t*E+o]=u[1].coeffs[_(1,o,t)]*q[1]+T[1];const F=u[1].quant,Z=(F[8]+F[16]+F[24]+F[32]+F[40])/h,Y=E*C,V=Y<256?0:Math.max(1,Math.min(7,n(Y)-n(Z)-7));let W=S(j,V,a);const $=new N(256);if(function(t,o,n,r,e,{xyb:i=!1,orientation:s=1}={}){t.write(16,2815),t.write(1,0),M(t,n),t.write(3,0),M(t,o),t.write(1,0);const f=1!==s;t.write(1,f?1:0),f&&t.write(6,s-1),t.write(4,8),e?t.write(3,5):t.write(2,0),t.write(1,i?1:0),3===r?t.write(1,1):t.write(15,14100),f&&t.write(1,1),t.write(2,0),t.write(1,1),t.et()}($,e,i,a?1:3,!!f,{orientation:o.orientation}),$.write(1,0),$.write(2,0),$.write(1,0),$.write(2,2),$.write(8,111),$.write(1,l?1:0),l)for(let t=0;t<3;t++)$.write(2,0===b[t]&&0===y[t]?0:1===b[t]&&1===y[t]?1:1===b[t]?2:3);$.write(2,0),f&&$.write(2,0),function(t,o){t.write(o?7:5,0),t.write(11,1)}($,f);const tt=[0,1,2].map(()=>new Int32Array(1024)),ot=(o,n)=>{const r=o%k*32,e=32*(o/k|0),i=Math.min(32,E-r),s=Math.min(32,C-e);for(let o=0;o<s;o++)for(let s=0;s<i;s++)for(const i of[1,0,2]){const f=x(i),c=I(i),a=s>>f,h=o>>c;if(a<<f!==s||h<<c!==o)continue;const l=_(i,(r>>f)+a,(e>>c)+h),w=u[i].coeffs;let d=0;if(l>=0)for(let t=1;t<64;t++)w[l+t]&&d++;const p=tt[i],m=0===a?0===h?32:p[32*(h-1)]:0===h?p[32*h+a-1]:p[32*(h-1)+a]+p[32*h+a-1]+1>>1;p[32*h+a]=d;const b=W.Z(i,W.F(j[(e+o)*E+r+s]));n(W.Y(m,b),d);const y=W.K(b);let M=d>4?0:1,g=d;for(let o=1;o<64&&g;o++){const r=w[l+K[o]];n(y+Q(g,o,M),t(r)),M=r?1:0,g-=M}}},nt=t=>{W=t;const o=new R(t.V);for(let t=0;t<O;t++)ot(t,(t,n)=>o.add(t,n));return function(t,{maxClusters:o=48,newClusterCost:n=320}={}){const r=[];for(let o=0;o<t.V;o++)t.it[o]>0&&r.push(o);r.sort((o,n)=>t.it[n]-t.it[o]);const e=[];for(const i of r){const r=t.st([i],H),s=v(r);let f=-1,c=1/0;for(let t=0;t<e.length;t++){const o=e[t],n=new Uint32Array(64);for(let t=0;t<64;t++)n[t]=o.ft[t]+r[t];const i=v(n)-o.ct-s;i<c&&(c=i,f=t)}if(f<0||e.length<o&&c>n)e.push({V:[i],ft:r,ct:s});else{const t=e[f];t.V.push(i);for(let o=0;o<64;o++)t.ft[o]+=r[o];t.ct=v(t.ft)}}e.length||e.push({V:[],ft:new Uint32Array(64),ct:0});const i=new Uint8Array(t.V);let s=2*t.V;const f=e.map((o,n)=>{for(const t of o.V)i[t]=n;let r=B[0],e=1/0;for(const n of B){const i=t.ht(o.V,n);i<e&&(e=i,r=n)}const f=c(t.st(o.V,r,128));return s+=e+3*f.o+40,{config:r,code:f}});return{I:i,S:f,write:(t,o,n)=>{const r=f[i[o]];d(t,r.code,r.config,n)},ct:s}}(o)};let rt=nt(W);if(V>0){const t=W,o=nt(S(j,0,a));o.ct<=rt.ct?rt=o:W=t}const et=Array.from({length:X?1:2+G+O},()=>null),it=t=>et[X?0:t]||(et[X?0:t]=new N(4096)),st=it(0);st.write(1,0);const ft=(t,o)=>{const n=t*z,r=o*z,s=Math.min(z,e-n),c=Math.min(z,i-r),a=new Int32Array(s*c);for(let t=0;t<c;t++)for(let o=0;o<s;o++)a[t*s+o]=f[4*((r+t)*e+n+o)+3];return{w:s,h:c,data:a}};for(let t=0;t<3;t++)st.write(16,r(u[t].quant[0]/q[t]/(2040*h)*128));st.write(2,3),st.write(16,57343),st.write(2,1),st.write(5,0),st.write(1,0),st.write(4,0),st.write(4,W.thresholds.length);for(const o of W.thresholds)st.L([[4,0],[8,16],[16,272],[32,65808]],t(o));st.write(4,0),st.write(4,0),p(st,W.map),st.write(1,0),st.write(2,0),st.write(16,0),st.write(16,0),st.write(8,128),st.write(8,128),st.write(1,0),f&&J(st,[X?ft(0,0):{w:0,h:0,data:new Int32Array(0)}],5);for(let t=0;t<G;t++){const o=it(1+t),r=t%D*256,e=256*(t/D|0),i=Math.min(256,E-r),s=Math.min(256,C-e);o.write(2,0),J(o,[1,0,2].map(t=>{const o=i>>x(t),n=s>>I(t),f=new Int32Array(o*n),c=q[t],a=T[t];for(let i=0;i<n;i++)for(let n=0;n<o;n++){const s=_(t,(r>>x(t))+n,(e>>I(t))+i);f[i*o+n]=s<0?0:u[t].coeffs[s]*c+a}return{w:o,h:n,data:f}}),5);const f=i*s,c=n(f);c&&o.write(c,f-1);const a=i+7>>3,h=s+7>>3;J(o,[{w:a,h:h,data:new Int32Array(a*h)},{w:a,h:h,data:new Int32Array(a*h)},{w:f,h:2,data:new Int32Array(2*f)},{w:i,h:s,data:new Int32Array(f)}],0)}const ct=it(1+G);ct.write(1,0),ct.write(3,7),ct.write(16,r(1/(2040*h))),J(ct,[0,1,2].map(t=>{const o=new Int32Array(64),n=u[t].quant;for(let t=0;t<8;t++)for(let r=0;r<8;r++)o[8*r+t]=n[8*t+r];return{w:8,h:8,data:o}}),5);for(let t=1;t<17;t++)ct.write(3,0);O>1&&ct.write(n(O),0),ct.write(2,2),m(ct,{I:rt.I,S:rt.S});for(let t=0;t<O;t++){const o=it(2+G+t);ot(t,(t,n)=>rt.write(o,t,n)),f&&!X&&J(o,[ft(t%k,Math.floor(t/k))],5)}return function(t,o){const n=o.map(t=>t.length);!function(t,o){t.write(1,0),t.et();for(const n of o)t.L([[10,0],[14,1024],[22,17408],[30,4211712]],n);t.et()}(t,n);const r=t.finish(),e=new Uint8Array(r.length+n.reduce((t,o)=>t+o,0));e.set(r);let i=r.length;for(const t of o)e.set(t,i),i+=t.length;return e}($,function(t){return t.map(t=>t?t.finish():new Uint8Array(0))}(et))}(e)}(o,e)),s=e.orientation>=5;return{bytes:i,width:s?e.height:e.width,height:s?e.width:e.height,orientation:e.orientation}})}const k=Object.freeze({bytes:16777216,pixels:24e6,edge:16384}),L=Array.from({length:33},(t,o)=>2**o);class N{constructor(t=4096){this.bytes=new Uint8Array(t),this.at=0,this.lt=0,this.pending=0}write(t,o){if(t>32||o<0||o>=(L[t]??2**t))throw new Error("bit write out of range: "+t+" bits, "+o);let n=this.lt+o*(1<<this.pending),r=this.pending+t;this.at+5>=this.bytes.length&&this.grow();const e=this.bytes;for(;r>=8;)e[this.at++]=255&n,n=Math.floor(n/256),r-=8;this.lt=n,this.pending=r}L(t,o){for(let n=0;n<4;n++){const[r,e]=t[n];if(o>=e&&o-e<2**r)return this.write(2,n),void(r&&this.write(r,o-e))}throw new Error("U32 value out of range: "+o)}et(){this.pending&&(this.at+1>=this.bytes.length&&this.grow(),this.bytes[this.at++]=this.lt,this.lt=0,this.pending=0)}get ut(){return 8*this.at+this.pending}grow(t=0){const o=k.bytes+64;if(this.at+t+16>o)throw Object.assign(new Error("The encoded picture exceeds 16 MiB."),{code:"JXL_SIZE"});const n=new Uint8Array(Math.min(o,Math.max(2*this.bytes.length,this.at+t+16)));n.set(this.bytes.subarray(0,this.at)),this.bytes=n}append(t){if(this.at+t.at+8>=this.bytes.length&&this.grow(t.at+8),this.pending)for(let o=0;o<t.at;o++)this.write(8,t.bytes[o]);else this.bytes.set(t.bytes.subarray(0,t.at),this.at),this.at+=t.at;t.pending&&this.write(t.pending,t.lt)}finish(){const t=this.at+(this.pending?1:0),o=this.bytes.slice(0,t);return this.pending&&(o[this.at]=this.lt),o}}const O=[1,2,3,4,0,5,17,6,16,7,8,9,10,11,12,13,14,15],D=[2,4,3,2,2,4],G=[0,7,3,2,1,15],X=[0,0,0],_=h(4),P=Object.freeze({C:224,minLength:7,k:_}),q=h(0),z=256,T=[0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63],B=[h(0),h(1),h(2),h(3),h(4),h(4,1,1),h(5,1,1),h(2,0,1),h(3,0,1)],H=h(4,1,1);class R{constructor(t){this.V=t,this.small=new Uint32Array(64*t),this.wt=Array.from({length:t},()=>null),this.it=new Float64Array(t)}add(t,o){if(this.it[t]++,o<64)return void this.small[64*t+o]++;const n=this.wt[t]||(this.wt[t]=new Map);n.set(o,(n.get(o)||0)+1)}st(t,o,n=64){const r=new Uint32Array(n);for(const n of t){const t=64*n;for(let n=0;n<64;n++)this.small[t+n]&&(r[E(o,n)]+=this.small[t+n]);const e=this.wt[n];if(e)for(const[t,n]of e)r[E(o,t)]+=n}return r}ht(t,n){let r=v(this.st(t,n));for(const e of t){const t=64*e;for(let e=n.U;e<64;e++)this.small[t+e]&&(r+=this.small[t+e]*(o(e)-n.M-n.A));const i=this.wt[e];if(i)for(const[t,e]of i)r+=e*(o(t)-n.M-n.A)}return r}}const j=[0,0,0],F=13,Z=[0,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,15,16,16,17,17,18,18,19,19,20,20,21,21,22,22,23,23,23,23,24,24,24,24,25,25,25,25,26,26,26,26,27,27,27,27,28,28,28,28,29,29,29,29,30,30,30,30],Y=[0,0,31,62,62,93,93,93,93,123,123,123,123,152,152,152,152,152,152,152,152,180,180,180,180,180,180,180,180,180,180,180,180,206,206,206,206,206,206,206,206,206,206,206,206,206,206,206,206,206,206,206,206,206,206,206,206,206,206,206,206,206,206,206],K=T.map(t=>(7&t)<<3|t>>3),Q=(t,o,n)=>2*(Y[t]+Z[o])+n,V=(t,o)=>Object.assign(new Error(o),{code:t});export{C as transcode,k as LIMITS};
|
|
1
|
+
/*! Rapier JXL 2.0.0 | MIT License Copyright (c) 2026 rapier.website Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
2
|
+
const t=Object.freeze({bytes:16777216,pixels:24e6,edge:16384}),o=Object.freeze({bytes:16777216,pixels:64e6,edge:16384}),n=[1];for(let t=1;t<=32;t++)n.push(2*n[t-1]);class r{constructor(t=4096){this.bytes=new Uint8Array(t),this.at=0,this.t=0,this.pending=0}write(t,o){if(!(o>=0&&o<n[t]))throw new Error("bit write out of range: "+t+" bits, "+o);let r=this.t+o*(1<<this.pending),e=this.pending+t;this.at+5>=this.bytes.length&&this.grow();const s=this.bytes;for(;e>=8;)s[this.at++]=255&r,r=Math.floor(r/256),e-=8;this.t=r,this.pending=e}o(t,o){for(let r=0;r<4;r++){const[e,s]=t[r];if(o>=s&&o-s<n[e])return this.write(2,r),void(e&&this.write(e,o-s))}throw new Error("U32 value out of range: "+o)}i(){this.pending&&(this.at+1>=this.bytes.length&&this.grow(),this.bytes[this.at++]=this.t,this.t=0,this.pending=0)}get u(){return 8*this.at+this.pending}grow(o=0){const n=t.bytes+64;if(this.at+o+16>n)throw Object.assign(new Error("The encoded picture exceeds 16 MiB."),{code:"JXL_SIZE"});const r=new Uint8Array(Math.min(n,Math.max(2*this.bytes.length,this.at+o+16)));r.set(this.bytes.subarray(0,this.at)),this.bytes=r}append(t){if(this.at+t.at+8>=this.bytes.length&&this.grow(t.at+8),this.pending)for(let o=0;o<t.at;o++)this.write(8,t.bytes[o]);else this.bytes.set(t.bytes.subarray(0,t.at),this.at),this.at+=t.at;t.pending&&this.write(t.pending,t.t)}finish(){const t=this.at+(this.pending?1:0),o=this.bytes.slice(0,t);return this.pending&&(o[this.at]=this.t),o}}function e(t){return t>=0?2*t:2*-t-1}function s(t){return 31-Math.clz32(t)}function i(t){return t<=1?0:32-Math.clz32(t-1)}function f(t){if(0===t)return 0;const o=t<0?32768:0;if(!((t=Math.abs(t))<65520))throw new Error("half float out of range: "+t);let n=0,r=t;for(;r>=2;)r/=2,n++;for(;r<1;)r*=2,n--;n<-14?(r=16384*t,n=-15):r-=1;let e=Math.round(1024*r);return 1024===e&&(e=0,n++),o|n+15<<10|e}const c=[0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63];function a(t,o="JXL_JPEG"){return Object.assign(new Error("JPEG: "+t),{code:o})}function h(t,o){const n=new Uint16Array(65536);let r=0,e=0;for(let s=1;s<=16;s++){for(let i=0;i<t[s-1];i++){const t=o[e++],i=r<<16-s,f=1<<16-s;if(i+f>=65536)throw a("bad Huffman table");n.fill(s<<8|t,i,i+f),r++}r<<=1}return n}const l=[.436,.2225,.0139,.3851,.7169,.0971,.143,.0606,.7139],u=[.5151,.2412,-.001,.292,.6922,.0419,.1571,.0666,.7841],w=[0,.00516,.01435,.02934,.05088,.07958,.11602,.16068,.21404,.27652,.34851,.43039,.52252,.62523,.73884,.86367,1],d=[2.4,1/1.055,.055/1.055,1/12.92,.04045];function p(t,o,n){const r=77===t[o]&&77===t[o+1];if(!r&&(73!==t[o]||73!==t[o+1]))return 1;const e=o=>o+2>n?0:r?t[o]<<8|t[o+1]:t[o]|t[o+1]<<8;if(42!==e(o+2))return 1;const s=o+(o=>o+4>n?0:r?(t[o]<<24|t[o+1]<<16|t[o+2]<<8|t[o+3])>>>0:(t[o]|t[o+1]<<8|t[o+2]<<16|t[o+3]<<24)>>>0)(o+4),i=e(s);for(let t=0,o=s+2;t<i&&o+12<=n;t++,o+=12){if(274!==e(o))continue;const t=e(o+8);return t>=1&&t<=8?t:1}return 1}function b(t,o,n,r,e,s,i,f,h){!function(t,o,n,r,e,s){if(!t.progressive){if(0!==n||63!==r||0!==e||0!==s)throw a("a bad spectral selection");for(const t of o){if(t.component.coverage.some(t=>-1!==t))throw a("a component coded twice");t.component.coverage.fill(0)}return}if(s>13||e&&e!==s+1)throw a("a bad successive approximation");if(0===n){if(0!==r)throw a("a bad spectral selection");for(const{component:t}of o){if(t.coverage[0]!==(e||-1))throw a(e?"a refinement scan out of order":"a DC scan twice");t.coverage[0]=s}return}if(1!==o.length)throw a("an interleaved AC scan");if(r>63||n>r)throw a("a bad spectral selection");const{coverage:i}=o[0].component;if(-1===i[0])throw a("an AC scan before the DC scan");for(let t=n;t<=r;t++){if(i[t]!==(e||-1))throw a(e?"a refinement scan out of order":"a band coded twice");i[t]=s}}(n,r,e,s,i,f);let l=o,u=0,w=0,d=0,p=!1;const b=()=>{for(;w<=24;){let o=0;if(p||l>=t.length)p=!0,d+=8;else if(o=t[l],255===o){const n=t[l+1];if(0===n)l+=2;else{if(255===n){l++;continue}p=!0,o=0,d+=8}}else l++;u=(u<<8|o)>>>0,w+=8}},y=()=>a("a scan cut short"),m=t=>{if(!t)return 0;if(b(),t>w-d)throw y();const o=u>>>w-t&(1<<t)-1;return w-=t,o},g=(t,o)=>t<1<<o-1?t-(1<<o)+1:t,M=t=>{if(!t)throw a("a scan without its Huffman table");b();const o=t[u>>>w-16&65535];if(!o)throw a("bad Huffman code");if(o>>8>w-d)throw y();return w-=o>>8,255&o},A=()=>{const o=w-d;if(o>=8)throw a("bytes left over in a scan");if(v)throw a("an end-of-band run past its interval");if(o&&(u>>>d&(1<<o)-1)!=(1<<o)-1)throw a("bad scan padding");for(;l+1<t.length&&255===t[l]&&255===t[l+1];)l++},U=o=>{if(A(),255!==t[l]||t[l+1]!==208+(7&o))throw a("a restart marker missing or out of order");l+=2,u=0,w=0,d=0,p=!1;for(const t of r)t.component.pred=0;v=0},x=(t,o)=>{if(o>11)throw a("a DC value out of range");const n=o?g(m(o),o):0;if(t.component.pred+=n,t.component.pred>2047||t.component.pred<-2048)throw a("a DC value out of range");return t.component.pred},I=(t,o)=>{if(t>10)throw a("an AC value out of range");const n=g(m(t),t)*(1<<o);if(n>1023||n<-1023)throw a("an AC value out of range");return n};let v=0;const E=1===r.length,S=n.progressive,C=()=>a("coefficients past the "+(S?"band":"block")),J=(t,o,n)=>{if(v>0)v--;else for(let r=e||1;r<=s;){const e=M(t.ac),i=e>>4,h=15&e;if(h){if(r+=i,r>s)throw C();o[n+c[r]]=I(h,f),r++}else{if(i<15){if(i&&!S)throw a("an end-of-band run in a sequential scan");v=(1<<i)-1+(i?m(i):0);break}if(r+=16,r>s+1)throw C()}}},k=S?0===e?0===i?(t,o,n)=>{const r=x(t,M(t.dc))*(1<<f);if(r>2047||r<-2048)throw a("a DC value out of range");o[n]=r}:(t,o,n)=>{m(1)&&(o[n]|=1<<f)}:0===i?J:(t,o,n)=>{const r=1<<f,i=-1<<f;let h=e;if(v<=0)for(;h<=s;h++){const e=M(t.ac);let f=e>>4,l=0;const u=15&e;if(u){if(1!==u)throw a("a bad refinement code");l=m(1)?r:i}else if(15!==f){v=1<<f,f&&(v+=m(f));break}for(;h<=s;h++){const t=n+c[h];if(0!==o[t])m(1)&&0===(o[t]&r)&&(o[t]+=o[t]>=0?r:i);else if(--f<0)break}if(f>=0||l&&h>s)throw a("coefficients past the band");l&&(o[n+c[h]]=l)}if(v>0){for(;h<=s;h++){const t=n+c[h];0!==o[t]&&m(1)&&0===(o[t]&r)&&(o[t]+=o[t]>=0?r:i)}v--}}:(t,o,n)=>{o[n]=x(t,M(t.dc)),J(t,o,n)};for(const t of r)t.component.pred=0;const L=r[0].component,X=E?L.blocksW*L.blocksH:n.mcusX*n.mcusY;for(let t=0;t<X;t++)if(h&&t>0&&t%h===0&&U(t/h-1),E){const o=t/L.blocksW|0,n=t-o*L.blocksW;k(r[0],L.coeffs,64*(o*L.stride+n))}else{const o=t/n.mcusX|0,e=t-o*n.mcusX;for(const t of r){const n=t.component;for(let r=0;r<n.v;r++)for(let s=0;s<n.h;s++)k(t,n.coeffs,64*((o*n.v+r)*n.stride+e*n.h+s))}}if(A(),255===t[l]&&t[l+1]>=208&&t[l+1]<=215){if(!h||X%h||t[l+1]!==208+(X/h-1&7))throw a("a restart marker out of order");for(l+=2;l+1<t.length&&255===t[l]&&255===t[l+1];)l++}if(l<t.length&&!(255===t[l]&&l+1<t.length&&0!==t[l+1]&&255!==t[l+1]))throw a("bytes left over after a scan");return l}const y=256;function m(t,o){t.o([[9,1],[13,1],[18,1],[30,1]],o)}const g=[1,2,3,4,0,5,17,6,16,7,8,9,10,11,12,13,14,15],M=[2,4,3,2,2,4],A=[0,7,3,2,1,15];function U(t,o){const n=new Uint8Array(t.length),r=[];for(let o=0;o<t.length;o++)t[o]>0&&r.push(o);if(!r.length)return n;if(1===r.length)return n[r[0]]=1,n;for(let e=1;;e*=2){const s=r.map(o=>({w:Math.max(t[o],e),p:o,l:null,r:null})).sort((t,o)=>t.w-o.w||t.p-o.p),i=[];let f=0,c=0;const a=()=>f<s.length&&(c>=i.length||s[f].w<=i[c].w)?s[f++]:i[c++];for(;s.length-f+(i.length-c)>1;){const t=a(),o=a();i.push({w:t.w+o.w,p:-1,l:t,r:o})}const h=[[a(),0]];let l=0;for(;h.length;){const[t,o]=h.pop();t.p>=0?(n[t.p]=o,o>l&&(l=o)):h.push([t.l,o+1],[t.r,o+1])}if(l<=o)return n}}function x(t,o,n,r,e,s){const i=[];for(;i.push(r&e),r>>=s;)r--;for(let r=i.length-1;r>=0;r--)t.push(n),o.push(i[r])}function I(t,o){let n=0;for(let r=0;r<o;r++)n=n<<1|1&t,t>>=1;return n}function v(t){const o=new Uint32Array(17),n=new Uint32Array(17),r=new Uint16Array(t.length);for(const n of t)o[n]++;o[0]=0;for(let t=1,r=0;t<=16;t++)r=r+o[t-1]<<1,n[t]=r;for(let o=0;o<t.length;o++)t[o]&&(r[o]=I(n[t[o]]++,t[o]));return r}function E(t){const o=[];for(let n=0;n<t.length;n++)t[n]>0&&o.push(n);const n=o.length,r=o[n-1]+1||1,e=n<=4?new Uint8Array(r):U(t,15).subarray(0,r);let s=0;if(n<=4){o.sort((o,n)=>t[n]-t[o]||o-n),s=4===n&&t[o[0]]>t[o[2]]+t[o[3]]?1:0;const r=n<2?[0]:2===n?[1,1]:3===n?[1,2,2]:s?[1,2,3,3]:[2,2,2,2];o.forEach((t,o)=>{e[t]=r[o]}),o.sort((t,o)=>e[t]-e[o]||t-o)}return{m:r,M:e,A:v(e),U:n&&n<=4?o:null,I:s}}function S(t,o){if(o.m<=1)return;if(o.U){const n=s(o.m-1)+1;t.write(2,1),t.write(2,o.U.length-1);for(const r of o.U)t.write(n,r);return void(4===o.U.length&&t.write(1,o.I))}const n=o.M,r=[],e=[];let i=n.length-1;for(;i>0&&!n[i];)i--;let f=8;for(let t=0;t<=i;){const o=n[t];let s=1;for(;t+s<=i&&n[t+s]===o;)s++;let c=s;if(o&&o!==f&&(r.push(o),e.push(0),c--,f=o),c<3)for(let t=0;t<c;t++)r.push(o),e.push(0);else x(r,e,o?16:17,c-3,o?3:7,o?2:3);t+=s}const c=new Uint32Array(18);for(const t of r)c[t]++;let a=0;for(const t of c)t&&a++;1===a&&(c[r[0]?0:1]=c[r[0]]);const h=U(c,5),l=v(h);let u=18;for(;u>0&&!h[g[u-1]];)u--;t.write(2,0);for(let o=0;o<u;o++){const n=h[g[o]];t.write(M[n],A[n])}for(let o=0;o<r.length;o++){const n=r[o];t.write(h[n],l[n]),16===n?t.write(2,e[o]):17===n&&t.write(3,e[o])}}function C(t,o=0,n=0){return{split:t,S:o,C:n,J:1<<t}}function J(t,o,n=15){t.write(i(n+1),o.split),o.split!==n&&(t.write(i(o.split+1),o.S),t.write(i(o.split-o.S+1),o.C))}function k(t,o,n){if(o<t.J)return n[0]=o,n[1]=0,void(n[2]=0);const r=s(o),e=o-(1<<r),i=r-t.S-t.C;n[0]=t.J+(r-t.split<<t.S+t.C|e>>r-t.S<<t.C|e&(1<<t.C)-1),n[1]=i,n[2]=o>>t.C&(1<<i)-1}function L(t,o,n,r=0){k(t,o,X),n[r+X[0]]++}const X=[0,0,0];function O(t,o,n,r,e=0){k(n,r,X);const s=e+X[0];t.write(o.M[s],o.A[s]),X[1]&&t.write(X[1],X[2])}function N(t,o){let n=0;for(const t of o)n=Math.max(n,t);const r=n?i(n+1):0;if(r<=3){if(t.write(1,1),t.write(2,r),r)for(const n of o)t.write(r,n);return}t.write(1,0),t.write(1,0);const e=C(8),s=C(4),f=o.length>=64,c=new Uint32Array(f?257:n+1),a=[];for(let t=0;t<o.length;){let n=1;for(;f&&t+n<o.length&&o[t+n]===o[t];)n++;if(n>=8)a.push([o[t],n-1]),c[o[t]]++,L(s,n-1-7,c,224),t+=n;else{for(let r=0;r<n;r++)a.push([o[t],0]),c[o[t]]++;t+=n}}const h=E(c);if(!f){D(t,{k:new Uint8Array(1),L:[{config:e,code:h}]});for(const[o]of a)O(t,h,e,o);return}const l=new Uint32Array(1);l[0]=1,D(t,{X:{O:224,minLength:7,N:s},k:new Uint8Array([1,0]),L:[{config:C(0),code:E(l)},{config:e,code:h}]});for(const[o,n]of a)O(t,h,e,o),n&&O(t,h,s,n-7,224)}function D(t,{X:o=null,k:n,L:r}){t.write(1,o?1:0),o&&(t.o([[0,224],[0,512],[0,4096],[15,8]],o.O),t.o([[0,3],[0,4],[2,5],[8,9]],o.minLength),J(t,o.N,8)),n.length>1&&N(t,n),t.write(1,1);for(const o of r)J(t,o.config,15);for(const o of r){const n=o.code.m-1;if(n){const o=s(n);t.write(1,1),t.write(4,o),t.write(o,n-(1<<o))}else t.write(1,0)}for(const o of r)S(t,o.code)}const G=C(4),T=Object.freeze({O:224,minLength:7,N:G}),P=C(0);function _(t,o){const n=[[3,0],[6,8],[10,72],[13,1096]];if("rct"===o.type)t.write(2,0),t.o(n,o.D),t.o([[0,6],[2,0],[4,2],[6,10]],o.G??6);else if("palette"===o.type)t.write(2,1),t.o(n,o.D),t.o([[0,1],[0,3],[0,4],[13,1]],o.T),t.o([[8,0],[10,256],[12,1280],[16,5376]],o.P),t.o([[0,0],[8,1],[10,257],[16,1281]],o._??0),t.write(4,o.R??0);else{if("squeeze"!==o.type)throw new Error("unknown transform "+o.type);{t.write(2,2);const r=o.q??[];t.o([[0,0],[4,1],[6,9],[8,41]],r.length);for(const o of r)t.write(1,o.B?1:0),t.write(1,o.Z?1:0),t.o(n,o.D),t.o([[0,1],[0,2],[0,3],[4,4]],o.T)}}}function R(t,o,n,r,s,i){const f=i.R,c=i.offset,a=i.Y,h=P,l=G;let u=0;const w=(n,r=h,e=0)=>{t?O(t,o,r,n,e):L(r,n,o,e)},d=()=>{if(u){if(u>=8)w(0),w(u-7-1,l,224);else for(let t=0;t<u;t++)w(0);u=0}},p=(t,o)=>{const r=n[t];let s;1===a?s=r-o-c:(s=Math.round((r-o-c)/a),n[t]=o+c+s*a),0!==s?(d(),w(e(s))):u++};for(let t=0,o=0;t<s;t++)for(let e=0;e<r;e++,o++){let s;if(0===f)s=0;else{const i=e?n[o-1]:t?n[o-r]:0,c=t?n[o-r]:i;if(1===f)s=i;else if(2===f)s=c;else if(3===f)s=(i+c)/2|0;else{const a=i+c-(e&&t?n[o-r-1]:i);if(5===f){const t=i<c?i:c,o=i<c?c:i;s=a<t?t:a>o?o:a}else{if(4!==f)throw new Error("predictor "+f+" is not coded here");s=Math.abs(a-i)<Math.abs(a-c)?i:c}}}p(o,s)}d()}const q=[C(0),C(1),C(2),C(3),C(4),C(4,1,1),C(5,1,1),C(2,0,1),C(3,0,1)],B=C(4,1,1);class Z{constructor(t){this.j=t,this.small=new Uint32Array(64*t),this.H=Array.from({length:t},()=>null),this.F=new Float64Array(t)}add(t,o){if(this.F[t]++,o<64)return void this.small[64*t+o]++;const n=this.H[t]||(this.H[t]=new Map);n.set(o,(n.get(o)||0)+1)}K(t,o,n=64){const r=new Uint32Array(n);for(const n of t){const t=64*n;for(let n=0;n<64;n++)this.small[t+n]&&(r[Y(o,n)]+=this.small[t+n]);const e=this.H[n];if(e)for(const[t,n]of e)r[Y(o,t)]+=n}return r}V(t,o){let n=F(this.K(t,o));for(const r of t){const t=64*r;for(let r=o.J;r<64;r++)this.small[t+r]&&(n+=this.small[t+r]*(s(r)-o.S-o.C));const e=this.H[r];if(e)for(const[t,r]of e)n+=r*(s(t)-o.S-o.C)}return n}}const z=[0,0,0];function Y(t,o){return k(t,o,z),z[0]}const j=[1];for(let t=1;t<32;t++)j.push(j[t-1]/2);function H(t){let o=0;for(t>=2&&t<4294967296&&(o=31-Math.clz32(t),t*=j[o]);t>=1.4142135623730951;)t/=2,o++;for(;t<.7071067811865476;)t*=2,o--;const n=(t-1)/(t+1),r=n*n;let e=n,s=n;for(let t=3;t<30;t+=2)s+=(e*=r)/t;return o+2.885390081777927*s}function F(t){let o=0,n=0;for(const n of t)o+=n;for(const r of t)r&&(n+=r*H(o/r));return n}const K=[0,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,15,16,16,17,17,18,18,19,19,20,20,21,21,22,22,23,23,23,23,24,24,24,24,25,25,25,25,26,26,26,26,27,27,27,27,28,28,28,28,29,29,29,29,30,30,30,30],Q=[0,0,31,62,62,93,93,93,93,123,123,123,123,152,152,152,152,152,152,152,152,180,180,180,180,180,180,180,180,180,180,180,180,206,206,206,206,206,206,206,206,206,206,206,206,206,206,206,206,206,206,206,206,206,206,206,206,206,206,206,206,206,206,206],V=c.map(t=>(7&t)<<3|t>>3),W=(t,o,n)=>2*(Q[t]+K[o])+n;function $(t,o,n){const r=[];if(o>0){const n=new Uint32Array(2048);let e=0;for(const o of t)n[Math.min(2047,Math.max(0,o+1024))]++,e++;let s=0,i=e/(o+1);for(let t=0;t<2048&&r.length<o;t++)s+=n[t],s>i&&(r.push(t-1025),i=e*(r.length+1)/(o+1))}const e=r.length+1,s=new Uint8Array(39*e);for(let t=0;t<e;t++)for(let o=0;o<13;o++)s[(0+o)*e+t]=t,s[(13+o)*e+t]=n?e:e+(t>>1),s[(26+o)*e+t]=n?e:e+(e-1>>1)+1+(t>>1);let i=0;for(const t of s)i=Math.max(i,t+1);return{thresholds:r,W:e,map:s,$:i,tt:t=>{let o=0;for(const n of r)t>n&&o++;return o},ot:(t,o)=>s[13*(t<2?1^t:2)*e+o],nt:(t,o)=>(Math.min(t,64)<8?Math.min(t,64):4+(Math.min(t,64)>>1))*i+o,rt:t=>37*i+458*t,j:495*i}}function tt(t,o,n){const r=o.map(()=>function(t,o=0,n=1){return{R:t,offset:o,Y:n,context:-1}}(n)),s=r.map(()=>new Uint32Array(257));o.forEach((t,o)=>R(null,s[o],t.data,t.w,t.h,r[o])),function(t,{et:o=!0,st:n=[]}={}){t.write(1,o?1:0),t.write(1,1),t.o([[0,0],[0,1],[4,2],[8,18]],n.length);for(const o of n)_(t,o)}(t,{et:!1,st:[]});const i=function(t,o){const n=[o],r=[],s=[];for(const t of n){if(t.left){r.push(t.it+1,e(t.ft)),n.push(t.left,t.right);continue}t.context=s.length,s.push(t);const o=1===t.Y?0:31-Math.clz32(t.Y&-t.Y);r.push(0,t.R,e(t.offset),o,(t.Y>>o)-1)}const i=new Uint32Array(64);for(const t of r)L(P,t,i);const f=E(i);D(t,{k:new Uint8Array(6),L:[{config:P,code:f}]});for(const o of r)O(t,f,P,o);return s}(t,function(t){return function(t,o){const n=(r,e)=>{if(r===e)return o?t[r].ct:t[r];const s=r+e>>1;return function(t,o,n,r){return{it:t,ft:o,left:n,right:r}}(o,o?t[s].ht:s,n(s+1,e),n(r,s))};return n(0,t.length-1)}(t,0)}(r)),f=function(t,o,n,r=t=>t.context){const e=new Uint8Array(o.length+1);for(const t of o)e[t.context]=r(t)+1;const s=new Uint32Array(2);s[1]=1;const i=[{config:P,code:E(s)}],f=Math.max(...e);for(let t=0;t<f;t++)i.push({config:P,code:E(n[t])});return D(t,{X:T,k:e,L:i}),i}(t,i,s,t=>r.indexOf(t));o.forEach((o,n)=>R(t,f[n+1].code,o.data,o.w,o.h,r[n]))}const ot=(t,o)=>Object.assign(new Error(o),{code:t});function nt(t){try{return t()}catch(t){if(t instanceof RangeError&&!t.code)throw ot("JXL_MEMORY","Not enough memory for this picture.");throw t}}function rt(t){const o=et(t);return function(t){let o;for(;!(o=t.next()).done;);o.value}(o),{bytes:o.bytes,width:o.width,height:o.height,orientation:o.orientation}}function et(n){if(!(n instanceof Uint8Array&&n.length))throw ot("JXL_INPUT","A JPEG is a non-empty Uint8Array.");if(n.length>o.bytes)throw ot("JXL_SIZE","A JPEG is at most 16 MiB.");const s=function(o){const n=function*(){let r,e;for(;!(r=nt(()=>o.next(n.hurry))).done;)yield e=r.value;return e<1&&(yield 1),n.bytes=function(o){if(o.length>t.bytes)throw ot("JXL_SIZE","The encoded picture exceeds 16 MiB.");return o}(r.value)}();return n.bytes=null,n.hurry=!1,n}(function*(n){const g=function(t){if(!(t.length>4)||255!==t[0]||216!==t[1])throw a("not a JPEG");const n=[],r=[[],[]],e=new Set,s={count:0,lt:[]};let i=null,f=0,y=-1,m=!1,g=1,M=2;const A=o=>t[o]<<8|t[o+1],U=(o,n)=>{for(let r=0;r<n.length;r++)if(t[o+r]!==n.charCodeAt(r))return!1;return!0};for(;;){if(M>=t.length){if(i&&i.scans)break;throw a("truncated")}if(255!==t[M])throw a("marker expected");for(;255===t[M];)M++;const l=t[M++];if(217===l)break;if(216===l)throw a("a second start");if(l>=208&&l<=215)throw a("a restart marker outside a scan");if(1===l)continue;if(M+2>t.length)throw a("truncated");const u=A(M),w=M+2,d=M+u;if(u<2||d>t.length)throw a("bad segment");if(M=d,219===l)for(let o=w;o<d;){const r=t[o]>>4,e=15&t[o];if(r>1||e>3||o+1+(r?128:64)>d)throw a("bad quantisation table");o++;const s=new Int32Array(64);for(let n=0;n<64;n++)if(s[c[n]]=r?A(o):t[o],o+=r?2:1,!s[c[n]])throw a("a zero quantisation step");if(i?.progressive&&i.components.some(t=>t.tq===e&&t.quant&&t.coverage.some(t=>0!==t)&&t.quant.some((t,o)=>t!==s[o])))throw a("a quantisation table changed between progressive scans");n[e]=s}else if(196===l)for(let o=w;o<d;){const n=t[o]>>4,s=15&t[o];if(n>1||s>3||o+17>d)throw a("bad Huffman table");o++;const i=t.subarray(o,o+16);let f=0;for(const t of i)f+=t;if(f>256||o+16+f>d)throw a("bad Huffman table");const c=t.subarray(o+16,o+16+f);r[n][s]=h(i,c),0===n&&(c.some(t=>t>15)?e.add(s):e.delete(s)),o+=16+f}else if(192===l||193===l||194===l){if(i)throw a("two frames");if(d-w<6)throw a("bad frame header");if(8!==t[w])throw a("only 8-bit samples");const n=A(w+1),r=A(w+3),e=t[w+5];if(!r||!n)throw a("a height left to a DNL marker");if(1!==e&&3!==e)throw a("only grey or three components");if(d-w<6+3*e)throw a("bad frame header");if(r>o.edge||n>o.edge)throw a("a picture past "+o.edge+" pixels on a side","JXL_DIMENSIONS");if(r*n>o.pixels)throw a("a picture past "+o.pixels+" pixels","JXL_DIMENSIONS");const s=[];let f=0;for(let o=0,n=w+6;o<e;o++,n+=3){const o=t[n+1]>>4,r=15&t[n+1],e=t[n+2];if(o<1||o>4||r<1||r>4)throw a("bad sampling");if(e>3)throw a("bad quantisation table");if(s.some(o=>o.id===t[n]))throw a("two components with one id");f+=o*r,s.push({id:t[n],h:o,v:r,tq:e,pred:0,coverage:new Int8Array(64).fill(-1)})}if(f>10)throw a("bad sampling");const c=Math.max(...s.map(t=>t.h)),h=Math.max(...s.map(t=>t.v)),u=Math.ceil(r/(8*c)),p=Math.ceil(n/(8*h));for(const t of s)t.blocksW=Math.ceil(Math.ceil(r*t.h/c)/8),t.blocksH=Math.ceil(Math.ceil(n*t.v/h)/8),t.stride=u*t.h,t.rows=p*t.v,t.coeffs=new Int16Array(t.stride*t.rows*64);i={progressive:194===l,width:r,height:n,components:s,hmax:c,vmax:h,mcusX:u,mcusY:p,scans:0}}else{if(195===l||l>=197&&l<=199||l>=201&&l<=203||l>=205&&l<=207)throw a("lossless, hierarchical or arithmetic coding");if(221===l){if(u<4)throw a("bad segment");f=A(w)}else{if(220===l)throw a("a DNL marker");if(218===l){if(!i)throw a("a scan before the frame");const o=t[w],s=[];if(o<1||o>4||w+1+2*o+3>d)throw a("bad scan header");let c=w+1;for(let e=0;e<o;e++,c+=2){const o=i.components.find(o=>o.id===t[c]);if(!o)throw a("a scan of an unknown component");if(s.some(t=>t.component===o))throw a("a component twice in one scan");if(o.quant||(o.quant=n[o.tq]),!o.quant)throw a("a scan without its quantisation table");s.push({component:o,dc:r[0][t[c+1]>>4],ac:r[1][15&t[c+1]],dcSlot:t[c+1]>>4})}const h=t[c],l=t[c+1],u=t[c+2]>>4,p=15&t[c+2];if((!i.progressive||0===h&&0===u)&&s.some(t=>e.has(t.dcSlot)))throw a("bad Huffman table");M=b(t,d,i,s,h,l,u,p,f),i.scans++}else if(224===l)U(w,"JFIF")&&(m=!0);else if(225===l)1===g&&U(w,"Exif")&&(g=p(t,w+6,d));else if(226===l){if(u>=16&&U(w,"ICC_PROFILE\0")){const o=t[w+12],n=t[w+13];if(!o||!n||o>n||s.count&&s.count!==n||s.lt[o])throw a("a colour profile cut short");s.count=n,s.lt[o]=t.subarray(w+14,d)}}else 238===l&&u>=14&&U(w,"Adobe")&&(y=t[w+11])}}}if(!i||!i.scans)throw a("no picture");for(const t of i.components)for(let o=0;o<64;o++)if(0!==t.coverage[o])throw a(i.progressive?"a progressive JPEG whose scans do not finish every coefficient":"a component without its scan");let x="srgb";if(s.count){const t=[];for(let o=1;o<=s.count;o++){if(!s.lt[o])throw a("a colour profile cut short");t.push(s.lt[o])}const o=new Uint8Array(t.reduce((t,o)=>t+o.length,0));for(let n=0,r=0;n<t.length;r+=t[n].length,n++)o.set(t[n],r);if(x=function(t){const o=t.length,n=o=>t[o]<<8|t[o+1],r=n=>n+4<=o?(t[n]<<24|t[n+1]<<16|t[n+2]<<8|t[n+3])>>>0:0,e=n=>n+4<=o?String.fromCharCode(t[n],t[n+1],t[n+2],t[n+3]):"",s=t=>(0|r(t))/65536,i=r(128);if(o<132||132+12*i>o||"acsp"!==e(36)||"XYZ "!==e(20))return null;const f=t=>{for(let n=0;n<i;n++){const s=132+12*n,i=r(s+4);if(e(s)===t)return i+r(s+8)<=o?i:-1}return-1},c=t=>{const i=f(t);if(i<0)return!1;if("para"===e(i)){const t=n(i+8),o=[1,3,4,5,7][t];if(3!==t&&4!==t)return!1;for(let t=0;t<o;t++)if(Math.abs(s(i+12+4*t)-(t<5?d[t]:0))>(t?.002:.01))return!1;return!0}const c="curv"===e(i)?r(i+8):0;return!(c<2||i+12+2*c>o)&&w.every((t,o)=>{const r=o*(c-1)/16,e=Math.floor(r),s=Math.min(c-1,e+1),f=r-e;return Math.abs((n(i+12+2*e)*(1-f)+n(i+12+2*s)*f)/65535-t)<=.001})};if(["A2B0","A2B1","A2B2"].some(t=>f(t)>=0))return null;if("GRAY"===e(16))return c("kTRC")?"srgb":null;if("RGB "!==e(16)||!["rTRC","gTRC","bTRC"].every(c))return null;const a=["rXYZ","gXYZ","bXYZ"].flatMap(t=>{const o=f(t);return o>=0&&"XYZ "===e(o)?[s(o+8),s(o+12),s(o+16)]:[9,9,9]}),h=t=>t.every((t,o)=>Math.abs(a[o]-t)<=.004);return h(l)?"srgb":h(u)?"display-p3":null}(o),!x)throw a("a colour profile other than sRGB or Display P3 (decode the picture and encode its pixels)")}let I=!1;return 3!==i.components.length||m||(I=y>=0?0===y:82===i.components[0].id&&71===i.components[1].id&&66===i.components[2].id),{width:i.width,height:i.height,components:i.components,progressive:i.progressive,ycbcr:!I,orientation:g,colorSpace:x}}(n);!function(o,n,r=t){if(!Number.isInteger(o)||!Number.isInteger(n)||o<1||n<1)throw ot("JXL_INPUT","A picture is at least one pixel wide and high, in whole pixels.");if(o>r.edge||n>r.edge)throw ot("JXL_DIMENSIONS","A picture is at most "+r.edge+" pixels on a side.");if(o*n>r.pixels)throw ot("JXL_DIMENSIONS","A picture is at most "+r.pixels.toLocaleString("en-US")+" pixels.")}(g.width,g.height,o);const M=g.orientation>=5;return(t=>{Object.assign(s,t)})({width:M?g.height:g.width,height:M?g.width:g.height,orientation:g.orientation}),yield*function*(t){const{width:o,height:n,components:s,alpha:c}=t,h=1===s.length,l=t.quantScale||1;let u=1;for(;u/(2040*l)<1/16384;)u*=2;const w=t.ycbcr||h,d=(h?[0,0,0]:w?[1,0,2]:[0,1,2]).map(t=>s[t]),p=t=>1===t?0:2===t?1:-1,b=d.map(t=>p(t.h)),g=d.map(t=>p(t.v));if(b.includes(-1)||g.includes(-1))throw a("sampling factors other than one or two");const M=Math.max(...b),A=Math.max(...g),U=t=>M-b[t],x=t=>A-g[t];if(!w&&(M||A))throw a("an RGB JPEG with subsampling");const I=Math.ceil(o/(8<<M))<<M,v=Math.ceil(n/(8<<A))<<A,S=Math.ceil(o/y),C=Math.ceil(n/y),J=S*C,k=Math.ceil(I/256),L=k*Math.ceil(v/256),X=1===J,G=(t,o,n)=>{const r=d[t];return h&&1!==t?-1:64*(n*r.stride+o)},T=t=>{let o=1024;for(;t%o;)o>>=1;return o},P=d.map(t=>w?1:t.quant[0]/T(t.quant[0])),_=d.map(t=>w?0:1024/T(t.quant[0])),R=new Int32Array(I*v);for(let t=0;t<v;t++)for(let o=0;o<I;o++)R[t*I+o]=d[1].coeffs[G(1,o,t)]*P[1]+_[1];const z=d[1].quant,Y=(z[8]+z[16]+z[24]+z[32]+z[40])/l,j=I*v,H=j<256?0:Math.max(1,Math.min(7,i(j)-i(Y)-7));let K=$(R,H,h);const Q=new r(256);if(function(t,o,n,r,e,{xyb:s=!1,orientation:i=1,colorSpace:f}={}){t.write(16,2815),t.write(1,0),m(t,n),t.write(3,0),m(t,o),t.write(1,0);const c=1!==i;t.write(1,c?1:0),c&&t.write(6,i-1),t.write(4,8),e?t.write(3,5):t.write(2,0),t.write(1,s?1:0),3===r&&"display-p3"===f?t.write(21,903568):3===r?t.write(1,1):t.write(15,14100),c&&t.write(1,1),t.write(2,0),t.write(1,1),t.i()}(Q,o,n,h?1:3,!!c,{orientation:t.orientation,colorSpace:t.colorSpace}),Q.write(1,0),Q.write(2,0),Q.write(1,0),Q.write(2,2),Q.write(8,111),Q.write(1,w?1:0),w)for(let t=0;t<3;t++)Q.write(2,0===b[t]&&0===g[t]?0:1===b[t]&&1===g[t]?1:1===b[t]?2:3);Q.write(2,0),c&&Q.write(2,0),function(t,o){t.write(o?7:5,0),t.write(11,1)}(Q,c);const ot=[0,1,2].map(()=>new Int32Array(1024)),nt=(t,o)=>{const n=t%S*32,r=32*(t/S|0),s=Math.min(32,I-n),i=Math.min(32,v-r);for(let t=0;t<i;t++)for(let i=0;i<s;i++)for(const s of[1,0,2]){const f=U(s),c=x(s),a=i>>f,h=t>>c;if(a<<f!==i||h<<c!==t)continue;const l=G(s,(n>>f)+a,(r>>c)+h),u=d[s].coeffs;let w=0;if(l>=0)for(let t=1;t<64;t++)u[l+t]&&w++;const p=ot[s],b=0===a?0===h?32:p[32*(h-1)]:0===h?p[32*h+a-1]:p[32*(h-1)+a]+p[32*h+a-1]+1>>1;p[32*h+a]=w;const y=K.ot(s,K.tt(R[(r+t)*I+n+i]));o(K.nt(b,y),w);const m=K.rt(y);let g=w>4?0:1,M=w;for(let t=1;t<64&&M;t++){const n=u[l+V[t]];o(m+W(M,t,g),e(n)),g=n?1:0,M-=g}}},rt=(H>0?3:2)*J+L;let et=0;const st=function*(t){K=t;const o=new Z(t.j);for(let t=0;t<J;t++)nt(t,(t,n)=>o.add(t,n)),yield++et/rt;return function(t,{maxClusters:o=48,newClusterCost:n=320}={}){const r=[];for(let o=0;o<t.j;o++)t.F[o]>0&&r.push(o);r.sort((o,n)=>t.F[n]-t.F[o]);const e=[];for(const s of r){const r=t.K([s],B),i=F(r);let f=-1,c=1/0;for(let t=0;t<e.length;t++){const o=e[t],n=new Uint32Array(64);for(let t=0;t<64;t++)n[t]=o.ut[t]+r[t];const s=F(n)-o.wt-i;s<c&&(c=s,f=t)}if(f<0||e.length<o&&c>n)e.push({j:[s],ut:r,wt:i});else{const t=e[f];t.j.push(s);for(let o=0;o<64;o++)t.ut[o]+=r[o];t.wt=F(t.ut)}}e.length||e.push({j:[],ut:new Uint32Array(64),wt:0});const s=new Uint8Array(t.j);let i=2*t.j;const f=e.map((o,n)=>{for(const t of o.j)s[t]=n;let r=q[0],e=1/0;for(const n of q){const s=t.V(o.j,n);s<e&&(e=s,r=n)}const f=E(t.K(o.j,r,128));return i+=e+3*f.m+40,{config:r,code:f}});return{k:s,L:f,write:(t,o,n)=>{const r=f[s[o]];O(t,r.code,r.config,n)},wt:i}}(o)};let it=yield*st(K);if(H>0){const t=K,o=$(R,0,h),n=yield*st(o);n.wt<=it.wt?it=n:K=t}const ft=Array.from({length:X?1:2+L+J},()=>null),ct=t=>ft[X?0:t]||(ft[X?0:t]=new r(4096)),at=ct(0);at.write(1,0);const ht=(t,r)=>{const e=t*y,s=r*y,i=Math.min(y,o-e),f=Math.min(y,n-s),a=new Int32Array(i*f);for(let t=0;t<f;t++)for(let n=0;n<i;n++)a[t*i+n]=c[4*((s+t)*o+e+n)+3];return{w:i,h:f,data:a}};for(let t=0;t<3;t++)at.write(16,f(d[t].quant[0]/P[t]/(2040*l)*128));at.write(2,3),at.write(16,57343),at.write(2,1),at.write(5,0),at.write(1,0),at.write(4,0),at.write(4,K.thresholds.length);for(const t of K.thresholds)at.o([[4,0],[8,16],[16,272],[32,65808]],e(t));at.write(4,0),at.write(4,0),N(at,K.map),at.write(1,0),at.write(2,0),at.write(16,0),at.write(16,0),at.write(8,128),at.write(8,128),at.write(1,0),c&&tt(at,[X?ht(0,0):{w:0,h:0,data:new Int32Array(0)}],5);for(let t=0;t<L;t++){const o=ct(1+t),n=t%k*256,r=256*(t/k|0),e=Math.min(256,I-n),s=Math.min(256,v-r);o.write(2,0),tt(o,[1,0,2].map(t=>{const o=e>>U(t),i=s>>x(t),f=new Int32Array(o*i),c=P[t],a=_[t];for(let e=0;e<i;e++)for(let s=0;s<o;s++){const i=G(t,(n>>U(t))+s,(r>>x(t))+e);f[e*o+s]=i<0?0:d[t].coeffs[i]*c+a}return{w:o,h:i,data:f}}),5);const f=e*s,c=i(f);c&&o.write(c,f-1);const a=e+7>>3,h=s+7>>3,l=new Int32Array(2*f).fill(u-1,f);tt(o,[{w:a,h:h,data:new Int32Array(a*h)},{w:a,h:h,data:new Int32Array(a*h)},{w:f,h:2,data:l},{w:e,h:s,data:new Int32Array(f)}],u>1?5:0),yield++et/rt}const lt=ct(1+L);lt.write(1,0),lt.write(3,7),lt.write(16,f(u/(2040*l))),tt(lt,[0,1,2].map(t=>{const o=new Int32Array(64),n=d[t].quant;for(let t=0;t<8;t++)for(let r=0;r<8;r++)o[8*r+t]=n[8*t+r];return{w:8,h:8,data:o}}),5);for(let t=1;t<17;t++)lt.write(3,0);J>1&<.write(i(J),0),lt.write(2,2),D(lt,{k:it.k,L:it.L});for(let t=0;t<J;t++){const o=ct(2+L+t);nt(t,(t,n)=>it.write(o,t,n)),c&&!X&&tt(o,[ht(t%S,Math.floor(t/S))],5),yield++et/rt}return function(t,o){const n=o.map(t=>t.length);!function(t,o){t.write(1,0),t.i();for(const n of o)t.o([[10,0],[14,1024],[22,17408],[30,4211712]],n);t.i()}(t,n);const r=t.finish(),e=new Uint8Array(r.length+n.reduce((t,o)=>t+o,0));e.set(r);let s=r.length;for(const t of o)e.set(t,s),s+=t.length;return e}(Q,function(t){return t.map(t=>t?t.finish():new Uint8Array(0))}(ft))}(g)}(n));return s}export{o as LIMITS,rt as transcode,et as transcodeSteps};
|