imagescript 3.6.0 → 3.7.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.
@@ -0,0 +1,397 @@
1
+ import { BitWriter } from "./bit-writer.js";
2
+ import { imageHeader, u32 } from "./headers-enc.js";
3
+ import { EntropyCode, Tokens, packSigned } from "./entropy-enc.js";
4
+ import { MATree, PRED_GRADIENT, PRED_LEFT, PRED_WEIGHTED, PROP_CHANNEL, PROP_STREAM, PROP_WP_ERROR, channelTokens, fixedTree, streamHeader } from "./modular-enc.js";
5
+ import { forwardTransform } from "./transforms-enc.js";
6
+ import { initialQuantField } from "./aq-enc.js";
7
+ import { chooseVarblocks } from "./acs-enc.js";
8
+ import { defaultDequant, MATRIX_INDEX } from "./quant.js";
9
+ import { naturalOrder, DEFAULT_CTX_MAP, ORDER_ID, SIZES, single, transposed } from "./vardct.js";
10
+ const OPSIN = [
11
+ 0.30, 0.622, 0.078,
12
+ 0.23, 0.692, 0.078,
13
+ 0.2434226892454782, 0.2047674442449682, 0.5518098665095535
14
+ ];
15
+ const BIAS = 0.0037930732552754493, CBRT_BIAS = Math.cbrt(BIAS);
16
+ const LINEAR = Float64Array.from({ length: 256 }, (_, i) => {
17
+ const v = i / 255;
18
+ return v <= 0.04045 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4;
19
+ });
20
+ function toXYB(rgba, width, height, pw, ph) {
21
+ const x = new Float32Array(pw * ph), y = new Float32Array(pw * ph), b = new Float32Array(pw * ph);
22
+ for (let py = 0; py < ph; py++) {
23
+ const sy = Math.min(py, height - 1);
24
+ for (let px = 0; px < pw; px++) {
25
+ const s = 4 * (sy * width + Math.min(px, width - 1));
26
+ const r = LINEAR[rgba[s]], g = LINEAR[rgba[s + 1]], bl = LINEAR[rgba[s + 2]];
27
+ const l = Math.cbrt(Math.max(OPSIN[0] * r + OPSIN[1] * g + OPSIN[2] * bl + BIAS, 0)) - CBRT_BIAS;
28
+ const m = Math.cbrt(Math.max(OPSIN[3] * r + OPSIN[4] * g + OPSIN[5] * bl + BIAS, 0)) - CBRT_BIAS;
29
+ const s3 = Math.cbrt(Math.max(OPSIN[6] * r + OPSIN[7] * g + OPSIN[8] * bl + BIAS, 0)) - CBRT_BIAS;
30
+ const i = py * pw + px;
31
+ x[i] = 0.5 * (l - m);
32
+ y[i] = 0.5 * (l + m);
33
+ b[i] = s3;
34
+ }
35
+ }
36
+ return [x, y, b];
37
+ }
38
+ function quantizer(distance) {
39
+ const dcDistance = Math.max(0.5 * distance, Math.min(distance, 0.3 * (distance / 0.3) ** 0.83));
40
+ const quantDc = Math.fround(Math.min(1.095924047623553 / dcDistance, 50));
41
+ const q = Math.fround(0.79 / distance);
42
+ let globalScale = Math.trunc(Math.min(Math.max(65536 * q / 5, 1), 1 << 15));
43
+ const scaledQuantDc = Math.trunc(quantDc * 4096 * 1.6);
44
+ if (globalScale > scaledQuantDc)
45
+ globalScale = Math.max(scaledQuantDc, 1);
46
+ const inv = 65536 / globalScale;
47
+ const quantLf = Math.trunc(Math.min(quantDc * inv + 0.5, 1 << 16));
48
+ const mul = Math.trunc(Math.max(1, Math.min(q * inv + 0.5, 256)));
49
+ return [globalScale, quantLf, mul];
50
+ }
51
+ const LF_DEQUANT = [1 / 32, 1 / 4, 1 / 2];
52
+ const QUANT_BIAS = [1 - 0.05465007330715401, 1 - 0.07005449891748593, 1 - 0.049935103337343655], QUANT_BIAS_NUMERATOR = 0.145;
53
+ const NUM_BLOCK_CTX = 15, MAX_COEFFS = 64 * 64;
54
+ const FREQ_CONTEXT = Uint8Array.of(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);
55
+ const NUM_NONZERO_CONTEXT = Uint8Array.of(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);
56
+ const ceilLog2 = (x) => x <= 1 ? 0 : 32 - Math.clz32(x - 1);
57
+ const LF_CUTOFFS = [
58
+ -500, -392, -255, -191, -127, -95, -63, -47, -31, -23, -15, -11, -7, -4, -3, -1, 0, 1, 3, 5, 7, 11, 15, 23, 31, 47, 63, 95,
59
+ 127, 191, 255, 392, 500
60
+ ];
61
+ function globalTree(numLfGroups, numPixels) {
62
+ const lf = () => fixedTree(PROP_WP_ERROR, LF_CUTOFFS, PRED_WEIGHTED, numPixels);
63
+ const byChannel = (n, leaf) => {
64
+ let node = leaf();
65
+ for (let c = n - 2; c >= 0; c--)
66
+ node = { property: PROP_CHANNEL, split: c, above: node, below: leaf() };
67
+ return node;
68
+ };
69
+ const hf = byChannel(4, () => ({ predictor: PRED_LEFT })), alpha = () => ({ predictor: PRED_GRADIENT });
70
+ const lfOrAlpha = { property: PROP_STREAM, split: 0, above: byChannel(3, lf), below: alpha() };
71
+ return new MATree({ property: PROP_STREAM, split: 3 * numLfGroups + 16, above: alpha(), below: { property: PROP_STREAM, split: numLfGroups, above: hf, below: lfOrAlpha } });
72
+ }
73
+ const inverses = new Map();
74
+ function inverseMatrices(type) {
75
+ let inv = inverses.get(type);
76
+ if (!inv)
77
+ inverses.set(type, inv = defaultDequant(MATRIX_INDEX[type])[transposed(type) ? 1 : 0].map(m => Float32Array.from(m, v => 1 / v)));
78
+ return inv;
79
+ }
80
+ function chromaFromLuma(raw, types, muls, pw, bw, bh, globalScale, xFromY, bFromY) {
81
+ const tilesPerRow = Math.ceil(bw / 8);
82
+ for (let ty = 0; ty < bh; ty += 8) {
83
+ for (let tx = 0; tx < bw; tx += 8) {
84
+ const sums = [0, 0, 0, 0];
85
+ let num = 0;
86
+ for (let by = ty; by < Math.min(ty + 8, bh); by++) {
87
+ for (let bx = tx; bx < Math.min(tx + 8, bw); bx++) {
88
+ const type = types[by * bw + bx];
89
+ if (type < 0)
90
+ continue;
91
+ const w8 = SIZES[2 * type], h8 = SIZES[2 * type + 1], w = w8 * 8, h = h8 * 8, o = by * 8 * pw + bx * 8, one = single(type);
92
+ const inv = inverseMatrices(type), q = globalScale / 65536 * 128 * muls[by * bw + bx];
93
+ num += w * h;
94
+ for (let y = 0; y < h; y++) {
95
+ for (let x = 0; x < w; x++) {
96
+ if (one ? x === 0 && y === 0 : x < w8 && y < h8)
97
+ continue;
98
+ const k = y * w + x, yv = raw[1][o + y * pw + x];
99
+ for (let c = 0; c < 2; c++) {
100
+ const ch = c ? 2 : 0, qm = q * inv[ch][k], base = c;
101
+ const a = yv * qm / 84, b = base * yv * qm - raw[ch][o + y * pw + x] * qm;
102
+ sums[2 * c] += a * a;
103
+ sums[2 * c + 1] += a * b;
104
+ }
105
+ }
106
+ }
107
+ }
108
+ }
109
+ const factor = (aa, ab) => {
110
+ if (!num)
111
+ return 0;
112
+ let x = -ab / (aa + num * 1e-9 * 0.5);
113
+ x = x >= 2.6 ? x - 2.6 : x <= -2.6 ? x + 2.6 : 0;
114
+ return Math.max(-128, Math.min(127, Math.round(x)));
115
+ };
116
+ const t = (ty >> 3) * tilesPerRow + (tx >> 3);
117
+ xFromY[t] = factor(sums[0], sums[1]);
118
+ bFromY[t] = factor(sums[2], sums[3]);
119
+ }
120
+ }
121
+ }
122
+ export function encodeVarDCT(rgba, width, height, { distance, effort }) {
123
+ if (width < 1 || height < 1)
124
+ throw new RangeError('JPEG XL: dimensions must be at least 1');
125
+ const bw = Math.ceil(width / 8), bh = Math.ceil(height / 8), pw = bw * 8;
126
+ const planes = toXYB(rgba, width, height, pw, bh * 8);
127
+ let alpha = null;
128
+ for (let i = 3; i < rgba.length; i += 4) {
129
+ if (rgba[i] === 255)
130
+ continue;
131
+ alpha = new Int32Array(width * height);
132
+ for (let k = 0; k < alpha.length; k++)
133
+ alpha[k] = rgba[4 * k + 3];
134
+ break;
135
+ }
136
+ const [globalScale, quantLf, mul] = quantizer(distance);
137
+ const xqmScale = 3 + [2.5, 5.5, 9.5].filter(t => distance > t).length, bqmScale = 2;
138
+ const qmMul = [1.25 ** (xqmScale - 2), 1, 1.25 ** (bqmScale - 2)];
139
+ const epfIters = [0.7, 1.5, 4].filter(t => distance >= t).length;
140
+ const lfStep = LF_DEQUANT.map(m => m * 512 / (globalScale * quantLf));
141
+ const types = effort ? chooseVarblocks(planes, pw, bw, bh, initialQuantField(distance * 0.62, planes, pw, bh * 8, 0.725 / (distance * 0.62)), distance, effort === 1 ? 5 : 4) : new Int8Array(bw * bh);
142
+ const muls = new Int32Array(bw * bh).fill(mul);
143
+ const lfValues = [0, 1, 2].map(() => new Float32Array(bw * bh));
144
+ const raw = [0, 1, 2].map(() => new Float32Array(pw * bh * 8));
145
+ const coeffs = new Float32Array(MAX_COEFFS), lfBlock = new Float32Array(MAX_COEFFS / 64);
146
+ for (let by = 0; by < bh; by++) {
147
+ for (let bx = 0; bx < bw; bx++) {
148
+ const type = types[by * bw + bx];
149
+ if (type < 0)
150
+ continue;
151
+ const w8 = SIZES[2 * type], h8 = SIZES[2 * type + 1], w = w8 * 8, h = h8 * 8, o = by * 8 * pw + bx * 8;
152
+ for (let c = 0; c < 3; c++) {
153
+ forwardTransform(type, planes[c], o, pw, coeffs, lfBlock);
154
+ for (let y = 0; y < h; y++)
155
+ raw[c].set(coeffs.subarray(y * w, (y + 1) * w), o + y * pw);
156
+ for (let y = 0; y < h8; y++)
157
+ for (let x = 0; x < w8; x++)
158
+ lfValues[c][(by + y) * bw + bx + x] = lfBlock[y * w8 + x];
159
+ }
160
+ }
161
+ }
162
+ const tilesPerRow = Math.ceil(bw / 8), tileRows = Math.ceil(bh / 8);
163
+ const xFromY = new Int32Array(tilesPerRow * tileRows), bFromY = new Int32Array(tilesPerRow * tileRows);
164
+ if (effort)
165
+ chromaFromLuma(raw, types, muls, pw, bw, bh, globalScale, xFromY, bFromY);
166
+ const hf = [0, 1, 2].map(() => new Int32Array(pw * bh * 8));
167
+ const yDequant = new Float32Array(MAX_COEFFS);
168
+ for (let by = 0; by < bh; by++) {
169
+ for (let bx = 0; bx < bw; bx++) {
170
+ const bi = by * bw + bx, type = types[bi];
171
+ if (type < 0)
172
+ continue;
173
+ const w8 = SIZES[2 * type], h8 = SIZES[2 * type + 1], w = w8 * 8, h = h8 * 8, o = by * 8 * pw + bx * 8;
174
+ const tile = (by >> 3) * tilesPerRow + (bx >> 3), cfl = [xFromY[tile] / 84, 0, 1 + bFromY[tile] / 84];
175
+ const [m0, m1] = defaultDequant(MATRIX_INDEX[type]), matrices = transposed(type) ? m1 : m0;
176
+ const hfStep = 65536 / (globalScale * muls[bi]), one = single(type);
177
+ for (const c of [1, 0, 2]) {
178
+ const src = raw[c], m = matrices[c], out = hf[c], k0 = cfl[c];
179
+ const thresholds = c === 1 ? [0.56, 0.62, 0.62, 0.62] : [0.58, 0.62, 0.62, 0.62];
180
+ if (c !== 1 && w8 * h8 >= 4)
181
+ for (let i = 0; i < 4; i++)
182
+ thresholds[i] = Math.max(0.5, thresholds[i] - 0.00744 * w8 * h8);
183
+ const scale = qmMul[c] / hfStep;
184
+ for (let y = 0; y < h; y++) {
185
+ for (let x = 0; x < w; x++) {
186
+ const k = y * w + x;
187
+ if (one ? k === 0 : x < w8 && y < h8)
188
+ continue;
189
+ const v = (c === 1 ? src[o + y * pw + x] : src[o + y * pw + x] - k0 * yDequant[k]) / m[k] * scale;
190
+ const q = Math.abs(v) >= thresholds[(y >= h / 2 ? 2 : 0) + (x >= w / 2 ? 1 : 0)] ? Math.round(v) : 0;
191
+ out[o + y * pw + x] = q;
192
+ if (c === 1) {
193
+ const d = q === 0 ? 0 : Math.abs(q) <= 1 ? q * QUANT_BIAS[1] : q - QUANT_BIAS_NUMERATOR / q;
194
+ yDequant[k] = d * m[k] * hfStep;
195
+ }
196
+ }
197
+ }
198
+ }
199
+ }
200
+ }
201
+ const lf = [0, 1, 2].map(() => new Int32Array(bw * bh));
202
+ for (let i = 0; i < bw * bh; i++) {
203
+ const ly = Math.round(lfValues[1][i] / lfStep[1]);
204
+ lf[1][i] = ly;
205
+ lf[0][i] = Math.round(lfValues[0][i] / lfStep[0]);
206
+ lf[2][i] = Math.round((lfValues[2][i] - ly * lfStep[1]) / lfStep[2]);
207
+ }
208
+ const groupsPerRow = Math.ceil(width / 256), numGroups = groupsPerRow * Math.ceil(height / 256);
209
+ const lfGroupsPerRow = Math.ceil(width / 2048), numLfGroups = lfGroupsPerRow * Math.ceil(height / 2048);
210
+ const tree = globalTree(numLfGroups, 3 * bw * bh);
211
+ const lfTokens = [], numVarblocks = [];
212
+ const lfGroupBlocks = (g) => {
213
+ const x0 = (g % lfGroupsPerRow) * 256, y0 = Math.floor(g / lfGroupsPerRow) * 256;
214
+ return [x0, y0, Math.min(256, bw - x0), Math.min(256, bh - y0)];
215
+ };
216
+ const crop = (src, x0, y0, w, h, stride) => {
217
+ const out = new Int32Array(w * h);
218
+ for (let y = 0; y < h; y++)
219
+ out.set(src.subarray((y0 + y) * stride + x0, (y0 + y) * stride + x0 + w), y * w);
220
+ return out;
221
+ };
222
+ for (let g = 0; g < numLfGroups; g++) {
223
+ const [x0, y0, w, h] = lfGroupBlocks(g), t = new Tokens();
224
+ [1, 0, 2].forEach((c, i) => channelTokens(t, tree, crop(lf[c], x0, y0, w, h, bw), w, h, i, 1 + g));
225
+ lfTokens.push(t);
226
+ const m = new Tokens(), tw = Math.ceil(w / 8), th = Math.ceil(h / 8), stream = 1 + 2 * numLfGroups + g;
227
+ channelTokens(m, tree, crop(xFromY, x0 / 8, y0 / 8, tw, th, tilesPerRow), tw, th, 0, stream);
228
+ channelTokens(m, tree, crop(bFromY, x0 / 8, y0 / 8, tw, th, tilesPerRow), tw, th, 1, stream);
229
+ const blocks = [];
230
+ for (let y = y0; y < y0 + h; y++)
231
+ for (let x = x0; x < x0 + w; x++)
232
+ if (types[y * bw + x] >= 0)
233
+ blocks.push(y * bw + x);
234
+ const info = new Int32Array(2 * blocks.length);
235
+ blocks.forEach((bi, k) => {
236
+ info[k] = types[bi];
237
+ info[blocks.length + k] = muls[bi] - 1;
238
+ });
239
+ numVarblocks.push(blocks.length);
240
+ channelTokens(m, tree, info, blocks.length, 2, 2, stream);
241
+ channelTokens(m, tree, new Int32Array(w * h).fill(4), w, h, 3, stream);
242
+ lfTokens.push(m);
243
+ }
244
+ const alphaTokens = [];
245
+ if (alpha && numGroups === 1) {
246
+ const t = new Tokens();
247
+ channelTokens(t, tree, alpha, width, height, 0, 0);
248
+ alphaTokens.push(t);
249
+ }
250
+ else if (alpha) {
251
+ for (let g = 0; g < numGroups; g++) {
252
+ const x0 = (g % groupsPerRow) * 256, y0 = Math.floor(g / groupsPerRow) * 256, w = Math.min(256, width - x0), h = Math.min(256, height - y0);
253
+ const t = new Tokens();
254
+ channelTokens(t, tree, crop(alpha, x0, y0, w, h, width), w, h, 0, 1 + 3 * numLfGroups + 17 + g);
255
+ alphaTokens.push(t);
256
+ }
257
+ }
258
+ const modularCode = new EntropyCode([...lfTokens, ...alphaTokens], tree.numLeaves);
259
+ const hfTokens = [];
260
+ for (let g = 0; g < numGroups; g++) {
261
+ const bx0 = (g % groupsPerRow) * 32, by0 = Math.floor(g / groupsPerRow) * 32;
262
+ const gw = Math.min(32, bw - bx0), gh = Math.min(32, bh - by0), t = new Tokens();
263
+ const nonZeros = [0, 1, 2].map(() => new Uint32Array(gw));
264
+ for (let y = 0; y < gh; y++) {
265
+ for (let x = 0; x < gw; x++) {
266
+ const type = types[(by0 + y) * bw + bx0 + x];
267
+ if (type < 0)
268
+ continue;
269
+ const w8 = SIZES[2 * type], numBlocks = w8 * SIZES[2 * type + 1], log = 31 - Math.clz32(numBlocks);
270
+ const orderId = ORDER_ID[type], order = naturalOrder(orderId), n = order.length / 2, tr = transposed(type);
271
+ const o = (by0 + y) * 8 * pw + (bx0 + x) * 8;
272
+ const at = (i) => tr ? o + order[2 * i] * pw + order[2 * i + 1] : o + order[2 * i + 1] * pw + order[2 * i];
273
+ for (let k = 0; k < 3; k++) {
274
+ const c = [1, 0, 2][k], q = hf[c], nz = nonZeros[c], blockCtx = DEFAULT_CTX_MAP[k * 13 + orderId];
275
+ let count = 0;
276
+ for (let i = numBlocks; i < n; i++)
277
+ if (q[at(i)])
278
+ count++;
279
+ const predicted = y === 0 ? (x === 0 ? 32 : nz[x - 1]) : x === 0 ? nz[x] : (nz[x] + nz[x - 1] + 1) >> 1;
280
+ t.push(blockCtx + (predicted >= 8 ? 4 + (predicted >> 1) : predicted) * NUM_BLOCK_CTX, count);
281
+ const value = (count + numBlocks - 1) >> log;
282
+ for (let dx = 0; dx < w8; dx++)
283
+ nz[x + dx] = value;
284
+ let prevNonZero = count <= numBlocks * 4 ? 1 : 0;
285
+ const ctxBase = blockCtx * 458 + 37 * NUM_BLOCK_CTX;
286
+ for (let i = numBlocks; i < n && count > 0; i++) {
287
+ const v = q[at(i)];
288
+ t.push(ctxBase + (NUM_NONZERO_CONTEXT[(count - 1) >> log] + FREQ_CONTEXT[(i - numBlocks) >> log]) * 2 + prevNonZero, packSigned(v));
289
+ prevNonZero = v ? 1 : 0;
290
+ if (v)
291
+ count--;
292
+ }
293
+ }
294
+ }
295
+ }
296
+ hfTokens.push(t);
297
+ }
298
+ const hfCode = new EntropyCode(hfTokens, 495 * NUM_BLOCK_CTX);
299
+ const sections = [];
300
+ {
301
+ const w = new BitWriter();
302
+ w.write(1, 1);
303
+ u32(w, globalScale, [[11, 1], [11, 2049], [12, 4097], [16, 8193]]);
304
+ u32(w, quantLf, [[0, 16], [5, 1], [8, 1], [16, 1]]);
305
+ w.write(1, 1);
306
+ w.write(1, 1);
307
+ w.write(1, 1);
308
+ tree.write(w);
309
+ modularCode.writeHeader(w);
310
+ if (alpha) {
311
+ streamHeader(w);
312
+ if (numGroups === 1)
313
+ modularCode.writeTokens(w, alphaTokens[0]);
314
+ }
315
+ sections.push(w);
316
+ }
317
+ for (let g = 0; g < numLfGroups; g++) {
318
+ const w = new BitWriter(), [, , gw, gh] = lfGroupBlocks(g);
319
+ w.write(2, 0);
320
+ streamHeader(w);
321
+ modularCode.writeTokens(w, lfTokens[2 * g]);
322
+ w.write(ceilLog2(gw * gh), numVarblocks[g] - 1);
323
+ streamHeader(w);
324
+ modularCode.writeTokens(w, lfTokens[2 * g + 1]);
325
+ sections.push(w);
326
+ }
327
+ {
328
+ const w = new BitWriter();
329
+ w.write(1, 1);
330
+ w.write(ceilLog2(numGroups), 0);
331
+ w.write(2, 2);
332
+ hfCode.writeHeader(w);
333
+ sections.push(w);
334
+ }
335
+ for (let g = 0; g < numGroups; g++) {
336
+ const w = new BitWriter();
337
+ hfCode.writeTokens(w, hfTokens[g]);
338
+ if (alpha && numGroups > 1) {
339
+ streamHeader(w);
340
+ modularCode.writeTokens(w, alphaTokens[g]);
341
+ }
342
+ sections.push(w);
343
+ }
344
+ const out = new BitWriter();
345
+ imageHeader(out, width, height, alpha ? 4 : 3, true);
346
+ frameHeader(out, xqmScale, bqmScale, epfIters, alpha !== null);
347
+ if (numGroups === 1) {
348
+ const all = new BitWriter();
349
+ for (const s of sections)
350
+ all.append(s);
351
+ sections.length = 0;
352
+ sections.push(all);
353
+ }
354
+ for (const s of sections)
355
+ s.zeroPadToByte();
356
+ out.write(1, 0);
357
+ out.zeroPadToByte();
358
+ for (const s of sections)
359
+ u32(out, s.length, [[10, 0], [14, 1024], [22, 17408], [30, 4211712]]);
360
+ out.zeroPadToByte();
361
+ const total = out.length + sections.reduce((n, s) => n + s.length, 0), bytes = new Uint8Array(total);
362
+ bytes.set(out.bytes());
363
+ let at = out.length;
364
+ for (const s of sections) {
365
+ bytes.set(s.bytes(), at);
366
+ at += s.length;
367
+ }
368
+ return bytes;
369
+ }
370
+ function frameHeader(w, xqmScale, bqmScale, epfIters, alpha) {
371
+ w.write(1, 0);
372
+ w.write(2, 0);
373
+ w.write(1, 0);
374
+ w.write(2, 0);
375
+ w.write(2, 0);
376
+ if (alpha)
377
+ w.write(2, 0);
378
+ w.write(3, xqmScale);
379
+ w.write(3, bqmScale);
380
+ w.write(2, 0);
381
+ w.write(1, 0);
382
+ w.write(2, 0);
383
+ if (alpha)
384
+ w.write(2, 0);
385
+ w.write(1, 1);
386
+ w.write(2, 0);
387
+ w.write(1, 0);
388
+ w.write(1, 0);
389
+ w.write(2, epfIters);
390
+ if (epfIters) {
391
+ w.write(1, 0);
392
+ w.write(1, 0);
393
+ w.write(1, 0);
394
+ }
395
+ w.write(2, 0);
396
+ w.write(2, 0);
397
+ }
@@ -4,7 +4,7 @@ import { ModularImage, channel, decodeModular } from "./modular.js";
4
4
  import { FLAG_SKIP_ADAPTIVE_LF_SMOOTHING } from "./frame-header.js";
5
5
  import { readDequantMatrices, MATRIX_INDEX } from "./quant.js";
6
6
  import { dct2d, inverseTransform, scaleF } from "./dct.js";
7
- const DEFAULT_CTX_MAP = Uint8Array.of(0, 1, 2, 2, 3, 3, 4, 5, 6, 6, 6, 6, 6, 7, 8, 9, 9, 10, 11, 12, 13, 14, 14, 14, 14, 14, 7, 8, 9, 9, 10, 11, 12, 13, 14, 14, 14, 14, 14);
7
+ export const DEFAULT_CTX_MAP = Uint8Array.of(0, 1, 2, 2, 3, 3, 4, 5, 6, 6, 6, 6, 6, 7, 8, 9, 9, 10, 11, 12, 13, 14, 14, 14, 14, 14, 7, 8, 9, 9, 10, 11, 12, 13, 14, 14, 14, 14, 14);
8
8
  export function readVarDCTGlobal(r) {
9
9
  const globalScale = r.u32([1, 11], [2049, 11], [4097, 12], [8193, 16]), quantLf = r.u32(16, [1, 5], [1, 8], [1, 16]);
10
10
  let ctx;
@@ -41,17 +41,17 @@ export function readVarDCTGlobal(r) {
41
41
  }
42
42
  return { globalScale, quantLf, ctx, colourFactor, baseX, baseB, xFactorLf, bFactorLf };
43
43
  }
44
- const SIZES = [
44
+ export const SIZES = [
45
45
  1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 4, 4, 1, 2, 2, 1, 1, 4, 4, 1, 2, 4, 4, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 8, 8, 4, 8, 8, 4,
46
46
  16, 16, 8, 16, 16, 8, 32, 32, 16, 32, 32, 16
47
47
  ];
48
- const ORDER_ID = [0, 1, 1, 1, 2, 3, 4, 4, 5, 5, 6, 6, 1, 1, 1, 1, 1, 1, 7, 8, 8, 9, 10, 10, 11, 12, 12];
48
+ export const ORDER_ID = [0, 1, 1, 1, 2, 3, 4, 4, 5, 5, 6, 6, 1, 1, 1, 1, 1, 1, 7, 8, 8, 9, 10, 10, 11, 12, 12];
49
49
  const ORDER_SIZES = [[8, 8], [8, 8], [16, 16], [32, 32], [16, 8], [32, 8], [32, 16], [64, 64], [64, 32], [128, 128], [128, 64], [256, 256], [256, 128]];
50
50
  const special = (t) => (t >= 1 && t <= 3) || (t >= 12 && t <= 17);
51
- const transposed = (t) => !special(t) && SIZES[2 * t + 1] >= SIZES[2 * t];
52
- const single = (t) => t === 0 || special(t);
51
+ export const transposed = (t) => !special(t) && SIZES[2 * t + 1] >= SIZES[2 * t];
52
+ export const single = (t) => t === 0 || special(t);
53
53
  const naturalOrders = new Map();
54
- function naturalOrder(id) {
54
+ export function naturalOrder(id) {
55
55
  let o = naturalOrders.get(id);
56
56
  if (o)
57
57
  return o;
@@ -5,6 +5,7 @@ import { frameHeader, toc } from "./jxl/frame-header.js";
5
5
  import { decodeFrame } from "./jxl/frame.js";
6
6
  import { ColorOutput } from "./jxl/color.js";
7
7
  import { encodeRGBA } from "./jxl/fast-lossless.js";
8
+ import { encodeVarDCT } from "./jxl/vardct-enc.js";
8
9
  import { concat } from "../util/mem.js";
9
10
  const CONTAINER = [0, 0, 0, 12, 0x4a, 0x58, 0x4c, 0x20, 0x0d, 0x0a, 0x87, 0x0a];
10
11
  export function isJXL(d) {
@@ -12,8 +13,8 @@ export function isJXL(d) {
12
13
  return true;
13
14
  return d.length >= 12 && CONTAINER.every((v, i) => d[i] === v);
14
15
  }
15
- export function encode(rgba, width, height, exif) {
16
- const stream = encodeRGBA(rgba, width, height);
16
+ export function encode(rgba, width, height, exif, distance = 0, effort = 1) {
17
+ const stream = distance > 0 ? encodeVarDCT(rgba, width, height, { distance, effort }) : encodeRGBA(rgba, width, height);
17
18
  if (!exif?.length)
18
19
  return stream;
19
20
  const jxl = [0x6a, 0x78, 0x6c, 0x20];