rapier-jxl 2.0.0 → 2.2.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 +10 -1
- package/ENCODER-COMPARISON.md +6 -6
- package/KERNELS.md +58 -0
- package/README.md +75 -18
- package/admit.mjs +5 -1
- package/ans.mjs +157 -0
- package/bits.mjs +5 -2
- package/build-jxl-kernels.mjs +29 -0
- package/coefficient-ans.mjs +26 -0
- package/coefficient-effort.mjs +62 -0
- package/effort-job.mjs +229 -0
- package/effort-level.mjs +10 -0
- package/effort.d.mts +4 -2
- package/effort.min.mjs +2 -2
- package/effort.mjs +7 -199
- package/frame.mjs +29 -1
- package/index.mjs +2 -2
- package/jfif.mjs +11 -0
- package/jpeg-ans.d.mts +3 -0
- package/jpeg-ans.min.mjs +2 -0
- package/jpeg-ans.mjs +9 -0
- package/jpeg-job.mjs +29 -0
- package/jpeg.d.mts +6 -2
- package/jpeg.min.mjs +2 -2
- package/jpeg.mjs +12 -36
- package/kernel-control.d.mts +3 -0
- package/kernel-hooks.mjs +3 -0
- package/kernels-bytes.mjs +4 -0
- package/kernels-probe.wat +1 -0
- package/kernels-scalar.wat +831 -0
- package/kernels-simd.wat +252 -0
- package/kernels.mjs +141 -0
- package/local.mjs +201 -0
- package/lossless-coding.mjs +30 -0
- package/lossless.mjs +88 -67
- package/modular.mjs +11 -7
- package/package.json +40 -6
- package/photo-ans.d.mts +3 -0
- package/photo-ans.min.mjs +2 -0
- package/photo-ans.mjs +9 -0
- package/photo-dct.mjs +83 -0
- package/photo-job.mjs +27 -0
- package/photo-quant.mjs +150 -0
- package/photo.d.mts +5 -1
- package/photo.min.mjs +2 -2
- package/photo.mjs +7 -101
- package/pool.mjs +136 -0
- package/rapier-jxl.min.mjs +2 -2
- package/rct-search.mjs +61 -0
- package/sizes.json +237 -56
- package/vardct.mjs +45 -8
- package/wasm.d.mts +4 -0
- package/wasm.min.mjs +2 -0
- package/wasm.mjs +6 -0
- package/weighted.mjs +22 -10
package/photo-quant.mjs
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
// The photo door's per-block AC quantisation candidate. DC and alpha keep their original coefficients/samples.
|
|
3
|
+
// Eight block candidates: the original, three rate/error roundings at its field, and four coarser/finer fields.
|
|
4
|
+
// An admitted candidate mutates the supplied coefficient planes, avoiding a second whole-picture allocation;
|
|
5
|
+
// callers keep the already-written previous stream before entering this search.
|
|
6
|
+
import {PHOTO_LIMITS, float16Bits} from './bits.mjs';
|
|
7
|
+
import {dctBlocks} from './photo-dct.mjs';
|
|
8
|
+
|
|
9
|
+
const FIELDS = [4, 4, 4, 4, 3, 2, 6, 8], BASE = 4;
|
|
10
|
+
const PENALTIES = [0, 0.125, 0.25, 0.5, 0.25, 0.25, 0.25, 0.25];
|
|
11
|
+
const BIASES = [0.9299455010825141, 0.945349926692846, 0.9500648966626563];
|
|
12
|
+
// The existing 40 MP photo envelope, including input, measured at about 10.5 bytes per pixel. This is a
|
|
13
|
+
// deterministic allocation estimate for optional work, not a promise about an engine's resident-set high water.
|
|
14
|
+
const QUANT_MEMORY_BUDGET = PHOTO_LIMITS.pixels * 10.5;
|
|
15
|
+
const unbiased = (q, c) => Math.abs(q) < 2 ? q * BIASES[c] : q - 0.145 / q;
|
|
16
|
+
|
|
17
|
+
// Only normal positive half floats are needed: the writer raises its matrix scale to avoid subnormal decoders.
|
|
18
|
+
function half(value) {
|
|
19
|
+
const bits = float16Bits(value);
|
|
20
|
+
let out = 1 + (bits & 1023) / 1024, exponent = (bits >> 10) - 15;
|
|
21
|
+
while (exponent < 0) { out /= 2; exponent++; }
|
|
22
|
+
while (exponent > 0) { out *= 2; exponent--; }
|
|
23
|
+
return out;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Reconstruction model: squared RGB coefficient error across edge-replicated DCT8 blocks, before clipping and
|
|
27
|
+
// output rounding. The stored half-precision matrix and the default quantisation bias are included. The transform
|
|
28
|
+
// is orthonormal, so this is the full padded block's RGB sample error (Parseval); DC is unchanged. The source
|
|
29
|
+
// samples are encoded sRGB or Display P3 values, not linear-light values.
|
|
30
|
+
const rgbError = (y, cb, cr) => {
|
|
31
|
+
const r = y + 1.402 * cr, g = y - 0.344136286 * cb - 0.714136286 * cr, b = y + 1.772 * cb;
|
|
32
|
+
return r * r + g * g + b * b;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export function* quantisationSteps(data, jpeg, storedBytes = 0) {
|
|
36
|
+
const {width, height, components, quantScale} = jpeg, stride = components[0].stride, rows = components[0].rows;
|
|
37
|
+
const count = stride * rows, choices = FIELDS.length;
|
|
38
|
+
// Optional search stays inside the existing photo memory envelope. Account for its score tables, and reserve
|
|
39
|
+
// two completed streams plus a writer's capacities, finished sections and assembly (six stream limits total),
|
|
40
|
+
// with 16 MiB for metadata. The original door's 40 MP admission is unchanged; larger searches keep their floor.
|
|
41
|
+
const pixelsBytes = data.byteLength + components.reduce((bytes, c) => bytes + c.coeffs.byteLength, 0);
|
|
42
|
+
if (pixelsBytes + Math.max(count * (choices * 10 + 2) + storedBytes, 7 * PHOTO_LIMITS.bytes) > QUANT_MEMORY_BUDGET) return null;
|
|
43
|
+
const errors = new Float64Array(count * choices), rates = new Uint16Array(count * choices);
|
|
44
|
+
const transform = dctBlocks(data, width, height), quant = components.map(c => c.quant), coeffs = components.map(c => c.coeffs);
|
|
45
|
+
const steps = FIELDS.map(field => quant.map(table => Float64Array.from(table, q => q * half(BASE / (8 * 255 * quantScale)) * 8 * 255 / field)));
|
|
46
|
+
const trial = new Int16Array(192), delta = new Float64Array(3);
|
|
47
|
+
const values = new Int32Array(6), differences = new Float64Array(6), bitCosts = new Float64Array(6);
|
|
48
|
+
let baselineError = 0, baselineRate = 0;
|
|
49
|
+
const quantise = (sums, block, choice) => {
|
|
50
|
+
let error = 0, rate = 0;
|
|
51
|
+
const penalty = PENALTIES[choice] * quant[0][1] * quant[0][1] / (quantScale * quantScale);
|
|
52
|
+
for (let k = 1; k < 64; k++) {
|
|
53
|
+
if (!choice) {
|
|
54
|
+
for (let c = 0; c < 3; c++) {
|
|
55
|
+
const q = coeffs[c][block * 64 + k];
|
|
56
|
+
delta[c] = unbiased(q, c) * steps[choice][c][k] - sums[c * 64 + k];
|
|
57
|
+
if (q) rate += 2 + 2 * (32 - Math.clz32(Math.abs(q)));
|
|
58
|
+
}
|
|
59
|
+
error += rgbError(delta[0], delta[1], delta[2]);
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
for (let c = 0; c < 3; c++) {
|
|
63
|
+
const source = sums[c * 64 + k], step = steps[choice][c][k], lo = Math.floor(source / step);
|
|
64
|
+
for (let n = 0; n < 2; n++) {
|
|
65
|
+
const q = lo + n;
|
|
66
|
+
values[2 * c + n] = q;
|
|
67
|
+
bitCosts[2 * c + n] = q ? 2 + 2 * (32 - Math.clz32(Math.abs(q))) : 0;
|
|
68
|
+
differences[2 * c + n] = q < -32768 || q > 32767 ? Infinity : unbiased(q, c) * step - source;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
let selected = 0, best = Infinity, selectedError = 0;
|
|
72
|
+
for (let i = 0; i < 8; i++) {
|
|
73
|
+
const current = rgbError(differences[i & 1], differences[2 + ((i >> 1) & 1)], differences[4 + (i >> 2)]);
|
|
74
|
+
const candidate = current + penalty * (bitCosts[i & 1] + bitCosts[2 + ((i >> 1) & 1)] + bitCosts[4 + (i >> 2)]);
|
|
75
|
+
if (candidate < best) { best = candidate; selected = i; selectedError = current; }
|
|
76
|
+
}
|
|
77
|
+
if (best === Infinity) return {error: Infinity, rate: 65535};
|
|
78
|
+
error += selectedError;
|
|
79
|
+
for (let c = 0; c < 3; c++) {
|
|
80
|
+
const q = values[2 * c + ((selected >> c) & 1)];
|
|
81
|
+
trial[c * 64 + k] = q;
|
|
82
|
+
if (q) rate += 2 + 2 * (32 - Math.clz32(Math.abs(q)));
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return {error, rate};
|
|
86
|
+
};
|
|
87
|
+
for (let by = 0; by < rows; by++) {
|
|
88
|
+
for (let bx = 0; bx < stride; bx++) {
|
|
89
|
+
const block = by * stride + bx, sums = transform(bx, by);
|
|
90
|
+
for (let choice = 0; choice < choices; choice++) {
|
|
91
|
+
const result = quantise(sums, block, choice), at = block * choices + choice;
|
|
92
|
+
errors[at] = result.error; rates[at] = result.rate;
|
|
93
|
+
if (!choice) { baselineError += result.error; baselineRate += result.rate; }
|
|
94
|
+
}
|
|
95
|
+
if ((block + 1) % 1024 === 0 || block + 1 === count) if (yield (block + 1) / (3 * count)) return null;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// A coefficient has at most 16 magnitude bits: the per-block proxy rate fits Uint16.
|
|
100
|
+
// The proxy rate only proposes a field map. Binary search finds the cheapest proxy reconstruction under the
|
|
101
|
+
// baseline error; the complete written stream, with the field map and all headers, still has to beat effort 1.
|
|
102
|
+
const selected = new Uint8Array(count);
|
|
103
|
+
let selectionPass = 0;
|
|
104
|
+
const select = function* (lambda, keep) {
|
|
105
|
+
let total = 0, rate = 0;
|
|
106
|
+
for (let block = 0; block < count; block++) {
|
|
107
|
+
const at = block * choices;
|
|
108
|
+
let chosen = 0, best = errors[at] + lambda * rates[at];
|
|
109
|
+
for (let choice = 1; choice < choices; choice++) {
|
|
110
|
+
const cost = errors[at + choice] + lambda * rates[at + choice];
|
|
111
|
+
if (cost < best) { best = cost; chosen = choice; }
|
|
112
|
+
}
|
|
113
|
+
total += errors[at + chosen]; rate += rates[at + chosen];
|
|
114
|
+
if (keep) selected[block] = chosen;
|
|
115
|
+
if ((block + 1) % 1024 === 0 || block + 1 === count) {
|
|
116
|
+
if (yield 1 / 3 + (selectionPass + (block + 1) / count) / 147) return null;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
selectionPass++;
|
|
120
|
+
return {error: total, rate};
|
|
121
|
+
};
|
|
122
|
+
let lo = 0, hi = Math.max(1, baselineError / Math.max(1, baselineRate));
|
|
123
|
+
for (let i = 0; i < 24; i++) {
|
|
124
|
+
const chosen = yield* select(hi, false);
|
|
125
|
+
if (!chosen) return null;
|
|
126
|
+
if (chosen.error > baselineError) break;
|
|
127
|
+
hi *= 2;
|
|
128
|
+
}
|
|
129
|
+
for (let i = 0; i < 24; i++) {
|
|
130
|
+
const mid = (lo + hi) / 2, chosen = yield* select(mid, false);
|
|
131
|
+
if (!chosen) return null;
|
|
132
|
+
if (chosen.error <= baselineError) lo = mid; else hi = mid;
|
|
133
|
+
}
|
|
134
|
+
const chosen = yield* select(lo, true);
|
|
135
|
+
if (!chosen || chosen.error > baselineError || chosen.rate >= baselineRate || !selected.some(choice => choice)) return null;
|
|
136
|
+
const quantFields = new Uint8Array(count);
|
|
137
|
+
for (let by = 0; by < rows; by++) {
|
|
138
|
+
for (let bx = 0; bx < stride; bx++) {
|
|
139
|
+
const block = by * stride + bx, choice = selected[block];
|
|
140
|
+
quantFields[block] = FIELDS[choice];
|
|
141
|
+
if (choice) {
|
|
142
|
+
quantise(transform(bx, by), block, choice);
|
|
143
|
+
for (let c = 0; c < 3; c++) for (let k = 1; k < 64; k++) coeffs[c][block * 64 + k] = trial[c * 64 + k];
|
|
144
|
+
}
|
|
145
|
+
if ((block + 1) % 1024 === 0 || block + 1 === count) if (yield 2 / 3 + (block + 1) / (3 * count)) return null;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return {...jpeg, quantFieldBase: BASE, quantFields,
|
|
149
|
+
reconstructionError: {baseline: baselineError, candidate: chosen.error}};
|
|
150
|
+
}
|
package/photo.d.mts
CHANGED
|
@@ -3,7 +3,11 @@ import type {Pixels, EncodeOptions, Job, Limits} from './index.mjs';
|
|
|
3
3
|
export type {Pixels, Limits, EncodeOptions, ErrorCode, EncoderError, Job} from './index.mjs';
|
|
4
4
|
/** This door's limits, by its memory: the 16 MiB codestream, 40 million pixels, 16,384 on a side. */
|
|
5
5
|
export const LIMITS: Limits;
|
|
6
|
-
/** Photographs: DCT8 coefficients with exact alpha; quality 90 by default, 100 is exact.
|
|
6
|
+
/** Photographs: DCT8 coefficients with exact alpha; quality 90 by default, 100 is exact.
|
|
7
|
+
* Effort 1 is the default; 3 tries a cluster budget, 4 also a learned order, preserving reconstructed pixels.
|
|
8
|
+
* Effort 5 adds per-block quantisation within effort 1's unclipped RGB sample reconstruction-error
|
|
9
|
+
* model, retaining the preceding stream unless the complete candidate is smaller. Optional searches beyond their
|
|
10
|
+
* memory estimate keep the preceding stream; this door's input limits do not change with effort. */
|
|
7
11
|
export function encodePhoto(data: Pixels, width: number, height: number, options?: EncodeOptions): Uint8Array;
|
|
8
12
|
/** encodePhoto's work and bytes as steps; the call is admitted at once. */
|
|
9
13
|
export function encodePhotoSteps(data: Pixels, width: number, height: number, options?: EncodeOptions): Job;
|
package/photo.min.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
/*! Rapier JXL 2.
|
|
2
|
-
function*t(t,n,o){let r,e;for(;!(r=t.next(e)).done;)e=yield(n+r.value)/o;return r.value}function n(t){return t>=0?2*t:2*-t-1}function o(t){return 31-Math.clz32(t)}function r(t){return t<=1?0:32-Math.clz32(t-1)}function e(t){if(0===t)return 0;const n=t<0?32768:0;if(!((t=Math.abs(t))<65520))throw new Error("half float out of range: "+t);let o=0,r=t;for(;r>=2;)r/=2,o++;for(;r<1;)r*=2,o--;o<-14?(r=16384*t,o=-15):r-=1;let e=Math.round(1024*r);return 1024===e&&(e=0,o++),n|o+15<<10|e}function s(t,n){t.t([[9,1],[13,1],[18,1],[30,1]],n)}function i(t,n,o,r,e,{xyb:i=!1,orientation:c=1,colorSpace:f}={}){t.write(16,2815),t.write(1,0),s(t,o),t.write(3,0),s(t,n),t.write(1,0);const l=1!==c;t.write(1,l?1:0),l&&t.write(6,c-1),t.write(4,8),e?t.write(3,5):t.write(2,0),t.write(1,i?1:0),3===r&&"display-p3"===f?t.write(21,903568):3===r?t.write(1,1):t.write(15,14100),l&&t.write(1,1),t.write(2,0),t.write(1,1),t.o()}function c(t,n){t.write(n?7:5,0),t.write(11,1)}function f(t,n){const o=n.map(t=>t.length);!function(t,n){t.write(1,0),t.o();for(const o of n)t.t([[10,0],[14,1024],[22,17408],[30,4211712]],o);t.o()}(t,o);const r=t.finish(),e=new Uint8Array(r.length+o.reduce((t,n)=>t+n,0));e.set(r);let s=r.length;for(const t of n)e.set(t,s),s+=t.length;return e}function l(t,n){const o=new Uint8Array(t.length),r=[];for(let n=0;n<t.length;n++)t[n]>0&&r.push(n);if(!r.length)return o;if(1===r.length)return o[r[0]]=1,o;for(let e=1;;e*=2){const s=r.map(n=>({w:Math.max(t[n],e),i:n,l:null,r:null})).sort((t,n)=>t.w-n.w||t.i-n.i),i=[];let c=0,f=0;const l=()=>c<s.length&&(f>=i.length||s[c].w<=i[f].w)?s[c++]:i[f++];for(;s.length-c+(i.length-f)>1;){const t=l(),n=l();i.push({w:t.w+n.w,i:-1,l:t,r:n})}const a=[[l(),0]];let h=0;for(;a.length;){const[t,n]=a.pop();t.i>=0?(o[t.i]=n,n>h&&(h=n)):a.push([t.l,n+1],[t.r,n+1])}if(h<=n)return o}}function a(t,n,o,r,e,s){const i=[];for(;i.push(r&e),r>>=s;)r--;for(let r=i.length-1;r>=0;r--)t.push(o),n.push(i[r])}function h(t,n){let o=0;for(let r=0;r<n;r++)o=o<<1|1&t,t>>=1;return o}function u(t){const n=new Uint32Array(17),o=new Uint32Array(17),r=new Uint16Array(t.length);for(const o of t)n[o]++;n[0]=0;for(let t=1,r=0;t<=16;t++)r=r+n[t-1]<<1,o[t]=r;for(let n=0;n<t.length;n++)t[n]&&(r[n]=h(o[t[n]]++,t[n]));return r}function w(t){const n=[];for(let o=0;o<t.length;o++)t[o]>0&&n.push(o);const o=n.length,r=n[o-1]+1||1,e=o<=4?new Uint8Array(r):l(t,15).subarray(0,r);let s=0;if(o<=4){n.sort((n,o)=>t[o]-t[n]||n-o),s=4===o&&t[n[0]]>t[n[2]]+t[n[3]]?1:0;const r=o<2?[0]:2===o?[1,1]:3===o?[1,2,2]:s?[1,2,3,3]:[2,2,2,2];n.forEach((t,n)=>{e[t]=r[n]}),n.sort((t,n)=>e[t]-e[n]||t-n)}return{u:r,p:e,M:u(e),A:o&&o<=4?n:null,m:s}}function y(t,n){if(n.u<=1)return;if(n.A){const r=o(n.u-1)+1;t.write(2,1),t.write(2,n.A.length-1);for(const o of n.A)t.write(r,o);return void(4===n.A.length&&t.write(1,n.m))}const r=n.p,e=[],s=[];let i=r.length-1;for(;i>0&&!r[i];)i--;let c=8;for(let t=0;t<=i;){const n=r[t];let o=1;for(;t+o<=i&&r[t+o]===n;)o++;let f=o;if(n&&n!==c&&(e.push(n),s.push(0),f--,c=n),f<3)for(let t=0;t<f;t++)e.push(n),s.push(0);else a(e,s,n?16:17,f-3,n?3:7,n?2:3);t+=o}const f=new Uint32Array(18);for(const t of e)f[t]++;let h=0;for(const t of f)t&&h++;1===h&&(f[e[0]?0:1]=f[e[0]]);const w=l(f,5),y=u(w);let p=18;for(;p>0&&!w[Z[p-1]];)p--;t.write(2,0);for(let n=0;n<p;n++){const o=w[Z[n]];t.write(Y[o],Q[o])}for(let n=0;n<e.length;n++){const o=e[n];t.write(w[o],y[o]),16===o?t.write(2,s[n]):17===o&&t.write(3,s[n])}}function p(t,n=0,o=0){return{split:t,U:n,I:o,S:1<<t}}function M(t,n,o=15){t.write(r(o+1),n.split),n.split!==o&&(t.write(r(n.split+1),n.U),t.write(r(n.split-n.U+1),n.I))}function d(t,n,r){if(n<t.S)return r[0]=n,r[1]=0,void(r[2]=0);const e=o(n),s=n-(1<<e),i=e-t.U-t.I;r[0]=t.S+(e-t.split<<t.U+t.I|s>>e-t.U<<t.I|s&(1<<t.I)-1),r[1]=i,r[2]=n>>t.I&(1<<i)-1}function A(t,n,o,r=0){d(t,n,H),o[r+H[0]]++}function g(t,n,o,r,e=0){d(o,r,H);const s=e+H[0];t.write(n.p[s],n.M[s]),H[1]&&t.write(H[1],H[2])}function b(t,n){let o=0;for(const t of n)o=Math.max(o,t);const e=o?r(o+1):0;if(e<=3){if(t.write(1,1),t.write(2,e),e)for(const o of n)t.write(e,o);return}t.write(1,0),t.write(1,0);const s=p(8),i=p(4),c=n.length>=64,f=new Uint32Array(c?257:o+1),l=[];for(let t=0;t<n.length;){let o=1;for(;c&&t+o<n.length&&n[t+o]===n[t];)o++;if(o>=8)l.push([n[t],o-1]),f[n[t]]++,A(i,o-1-7,f,224),t+=o;else{for(let r=0;r<o;r++)l.push([n[t],0]),f[n[t]]++;t+=o}}const a=w(f);if(!c){m(t,{L:new Uint8Array(1),T:[{config:s,code:a}]});for(const[n]of l)g(t,a,s,n);return}const h=new Uint32Array(1);h[0]=1,m(t,{J:{X:224,minLength:7,N:i},L:new Uint8Array([1,0]),T:[{config:p(0),code:w(h)},{config:s,code:a}]});for(const[n,o]of l)g(t,a,s,n),o&&g(t,a,i,o-7,224)}function m(t,{J:n=null,L:r,T:e}){t.write(1,n?1:0),n&&(t.t([[0,224],[0,512],[0,4096],[15,8]],n.X),t.t([[0,3],[0,4],[2,5],[8,9]],n.minLength),M(t,n.N,8)),r.length>1&&b(t,r),t.write(1,1);for(const n of e)M(t,n.config,15);for(const n of e){const r=n.code.u-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 n of e)y(t,n.code)}function U(t,n=0,o=1){return{O:t,offset:n,_:o,context:-1}}function I(t){return function(t,n){const o=(r,e)=>{if(r===e)return n?t[r].C:t[r];const s=r+e>>1;return function(t,n,o,r){return{G:t,P:n,left:o,right:r}}(n,n?t[s].q:s,o(s+1,e),o(r,s))};return o(0,t.length-1)}(t,0)}function x(t,o){const r=[o],e=[],s=[];for(const t of r){if(t.left){e.push(t.G+1,n(t.P)),r.push(t.left,t.right);continue}t.context=s.length,s.push(t);const o=1===t._?0:31-Math.clz32(t._&-t._);e.push(0,t.O,n(t.offset),o,(t._>>o)-1)}const i=new Uint32Array(64);for(const t of e)A(tt,t,i);const c=w(i);m(t,{L:new Uint8Array(6),T:[{config:tt,code:c}]});for(const n of e)g(t,c,tt,n);return s}function S(t,n){const o=[[3,0],[6,8],[10,72],[13,1096]];if("rct"===n.type)t.write(2,0),t.t(o,n.j),t.t([[0,6],[2,0],[4,2],[6,10]],n.k??6);else if("palette"===n.type)t.write(2,1),t.t(o,n.j),t.t([[0,1],[0,3],[0,4],[13,1]],n.B),t.t([[8,0],[10,256],[12,1280],[16,5376]],n.F),t.t([[0,0],[8,1],[10,257],[16,1281]],n.R??0),t.write(4,n.O??0);else{if("squeeze"!==n.type)throw new Error("unknown transform "+n.type);{t.write(2,2);const r=n.D??[];t.t([[0,0],[4,1],[6,9],[8,41]],r.length);for(const n of r)t.write(1,n.Z?1:0),t.write(1,n.Y?1:0),t.t(o,n.j),t.t([[0,1],[0,2],[0,3],[4,4]],n.B)}}}function E(t,{H:n=!0,K:o=[]}={}){t.write(1,n?1:0),t.write(1,1),t.t([[0,0],[0,1],[4,2],[8,18]],o.length);for(const n of o)S(t,n)}function L(t,n,o,r=t=>t.context){const e=new Uint8Array(n.length+1);for(const t of n)e[t.context]=r(t)+1;const s=new Uint32Array(2);s[1]=1;const i=[{config:tt,code:w(s)}],c=Math.max(...e);for(let t=0;t<c;t++)i.push({config:tt,code:w(o[t])});return m(t,{J:$,L:e,T:i}),i}function T(t,o,r,e,s,i){const c=i.O,f=i.offset,l=i._,a=tt,h=W;let u=0;const w=(n,r=a,e=0)=>{t?g(t,o,r,n,e):A(r,n,o,e)},y=()=>{if(u){if(u>=8)w(0),w(u-7-1,h,224);else for(let t=0;t<u;t++)w(0);u=0}},p=(t,o)=>{const e=r[t];let s;1===l?s=e-o-f:(s=Math.round((e-o-f)/l),r[t]=o+f+s*l),0!==s?(y(),w(n(s))):u++};for(let t=0,n=0;t<s;t++)for(let o=0;o<e;o++,n++){let s;if(0===c)s=0;else{const i=o?r[n-1]:t?r[n-e]:0,f=t?r[n-e]:i;if(1===c)s=i;else if(2===c)s=f;else if(3===c)s=(i+f)/2|0;else{const l=i+f-(o&&t?r[n-e-1]:i);if(5===c){const t=i<f?i:f,n=i<f?f:i;s=l<t?t:l>n?n:l}else{if(4!==c)throw new Error("predictor "+c+" is not coded here");s=Math.abs(l-i)<Math.abs(l-f)?i:f}}}p(n,s)}y()}function J(t,n){return d(t,n,et),et[0]}function X(t){let n=0;for(t>=2&&t<4294967296&&(n=31-Math.clz32(t),t*=st[n]);t>=1.4142135623730951;)t/=2,n++;for(;t<.7071067811865476;)t*=2,n--;const o=(t-1)/(t+1),r=o*o;let e=o,s=o;for(let t=3;t<30;t+=2)s+=(e*=r)/t;return n+2.885390081777927*s}function N(t){let n=0,o=0;for(const o of t)n+=o;for(const r of t)r&&(o+=r*X(n/r));return o}function O(t,n="JXL_JPEG"){return Object.assign(new Error("JPEG: "+t),{code:n})}function _(t,n,o){const r=[];if(n>0){const o=new Uint32Array(2048);let e=0;for(const n of t)o[Math.min(2047,Math.max(0,n+1024))]++,e++;let s=0,i=e/(n+1);for(let t=0;t<2048&&r.length<n;t++)s+=o[t],s>i&&(r.push(t-1025),i=e*(r.length+1)/(n+1))}const e=r.length+1,s=new Uint8Array(39*e);for(let t=0;t<e;t++)for(let n=0;n<it;n++)s[(0+n)*e+t]=t,s[(13+n)*e+t]=o?e:e+(t>>1),s[(26+n)*e+t]=o?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,V:e,map:s,W:i,$:t=>{let n=0;for(const o of r)t>o&&n++;return n},tt:(t,n)=>s[(t<2?1^t:2)*it*e+n],nt:(t,n)=>(Math.min(t,64)<8?Math.min(t,64):4+(Math.min(t,64)>>1))*i+n,ot:t=>37*i+458*t,rt:495*i}}function C(t,n,o){const r=n.map(()=>U(o)),e=r.map(()=>new Uint32Array(257));n.forEach((t,n)=>T(null,e[n],t.data,t.w,t.h,r[n])),E(t,{H:!1,K:[]});const s=x(t,I(r)),i=L(t,s,e,t=>r.indexOf(t));n.forEach((n,o)=>T(t,i[o+1].code,n.data,n.w,n.h,r[o]))}function G(t,n,o,{palette:r=!0}={}){const e=n*o;let s=!0,i=!0;for(let n=0,o=0;o<e&&(255===t[n+3]||(s=!1,i))&&(t[n]===t[n+1]&&t[n]===t[n+2]||(i=!1,s));o++,n+=4);const c=i?1:3,f=!s,l=c+(f?1:0),a=n=>i?f?t[n]+256*t[n+3]:t[n]:t[n]+256*t[n+1]+65536*t[n+2]+(f?16777216*t[n+3]:0),h=new Map;let u=null;for(let t=0,n=0;r&&n<e;n++,t+=4){const n=a(t);if(0!==n&&!h.has(n)&&(h.set(n,1),h.size>=2048))break}if(r&&h.size<2048){const t=[...h.keys()],n=(t,n)=>t>>>8*n&255,o=t=>(.299*n(t,0)+.587*n(t,1)+.114*n(t,2)+.01)*(4===l?n(t,3):1);t.sort((t,n)=>o(t)-o(n));const r=new Map([[0,0]]);t.forEach((t,n)=>r.set(t,n+1)),u={colours:[0,...t],index:r,key:a,byte:n}}return{colour:c,alpha:f,channels:l,palette:u}}function*P(t,n,o,r,e){const{channels:s,palette:l}=r,a=function(t,n,o=256){const r=Math.ceil(t/o),e=Math.ceil(n/o);return{et:r,st:e,it:Math.ceil(t/(8*o)),ct:Math.ceil(n/(8*o)),ft:1===r&&1===e}}(n,o),h=l?2:s,u=Array.from({length:h},()=>U(V)),p=I(u),M=Array.from({length:h},()=>new Int16Array(65536)),d=(o,r,e,i)=>{for(let c=0;c<i;c++){let i=4*((r+c)*n+o),f=c*e;for(let n=0;n<e;n++,i+=4,f++)if(l)M[0][f]=l.index.get(l.key(i));else if(s>=3){const n=t[i],o=t[i+1],r=t[i+2],e=n-r,c=r+(e>>1),l=o-c;M[0][f]=c+(l>>1),M[1][f]=e,M[2][f]=l,4===s&&(M[3][f]=t[i+3])}else M[0][f]=t[i],2===s&&(M[1][f]=t[i+3])}};let A=null;if(l){const t=l.colours.length;A=new Int16Array(t*s),l.colours.forEach((n,o)=>{for(let r=0;r<s;r++)A[r*t+o]=l.byte(n,r)})}const g=t=>u[l?a.ft?1:0:t],b=l?1:s,m=a.et*a.st,S=2*m,J=(t,r)=>{const e=t%a.et*D,s=(t/a.et|0)*D,i=Math.min(D,n-e),c=Math.min(D,o-s);d(e,s,i,c),r(i,c)};let X=0;if(!l){const t=t=>{const n=w(t),o=new R(128);y(o,n);let r=o.lt;for(let o=0;o<t.length;o++)t[o]&&(r+=t[o]*(n.p[o]+Math.max(0,o<224?o-1:o-236)));return r},r=Math.min(n,32),e=Math.min(o,32),i=[V,K].map(t=>({O:t,ht:u.map(()=>new Uint32Array(257))}));for(const t of[0,.5,1]){d(Math.floor((n-r)*t),Math.floor((o-e)*t),r,e);for(const t of i)for(let n=0;n<s;n++)T(null,t.ht[n],M[n],r,e,U(t.O))}u.forEach((n,o)=>{const r=i.map(n=>t(n.ht[o])),e=r[1]<r[0]?1:0;n.O=i[e].O})}const N=l?[{type:"palette",j:0,B:s,F:l.colours.length}]:s>=3?[{type:"rct",j:0,k:6}]:[],O=u.map(()=>new Uint32Array(257)),_=t=>O[u.indexOf(t)];A&&T(null,_(u[0]),A,l.colours.length,s,u[0]);for(let t=0;t<m;t++)J(t,(t,n)=>{for(let o=0;o<b;o++)T(null,_(g(o)),M[o],t,n,g(o))}),yield++X/S;const C=new R(256);i(C,n,o,r.colour,r.alpha,{colorSpace:e}),function(t,{alpha:n,shift:o=1}){t.write(9,8),n&&t.write(2,0),t.write(2,o),c(t,n)}(C,{alpha:r.alpha});const G=new R(4096);G.write(1,1),G.write(1,1);const P=x(G,p),q=L(G,P,O,t=>u.indexOf(t)),v=t=>q[u.indexOf(t)+1].code;E(G,{H:!0,K:N}),A&&T(G,v(u[0]),A,l.colours.length,s,u[0]);const j=[];if(a.ft)J(0,(t,n)=>{for(let o=0;o<b;o++)T(G,v(g(o)),M[o],t,n,g(o))}),yield 1,j.push(G.finish());else{j.push(G.finish());for(let t=0;t<a.it*a.ct+1;t++)j.push(new Uint8Array(0));for(let t=0;t<m;t++)J(t,(t,n)=>{const o=new R(t*n*b+64);E(o,{H:!0,K:[]});for(let r=0;r<b;r++)T(o,v(g(r)),M[r],t,n,g(r));j.push(o.finish())}),yield++X/S}return f(C,j)}function q(t){try{return t()}catch(t){if(t instanceof RangeError&&!t.code)throw ht("JXL_MEMORY","Not enough memory for this picture.");throw t}}function v(t,n,o,r){let e=0;return e+=t[n]*ut[r],e+=t[n+o]*ut[r+1],e+=t[n+2*o]*ut[r+2],e+=t[n+3*o]*ut[r+3],e+=t[n+4*o]*ut[r+4],e+=t[n+5*o]*ut[r+5],e+=t[n+6*o]*ut[r+6],e+=t[n+7*o]*ut[r+7],e}function j(t,n,o,r){return function(t){let n;for(;!(n=t.next()).done;);return n.value}(k(t,n,o,r))}function k(o,s,l,a){const{quality:h,colorSpace:u}=function(t={},n=100){if(null===t||"object"!=typeof t)throw ht("JXL_INPUT","Options are an object: {quality, colorSpace}.");if(void 0!==t.quality&&(n=t.quality),"number"!=typeof n||!Number.isFinite(n)||n<1||n>100)throw ht("JXL_INPUT","Quality is a number from 1 to 100.");const o=void 0===t.colorSpace?"srgb":t.colorSpace;if("srgb"!==o&&"display-p3"!==o)throw ht("JXL_INPUT","The colour space is srgb or display-p3.");return{quality:n,colorSpace:o}}(a,90);return function(t,n,o,r){if(function(t,n,o=z){if(!Number.isInteger(t)||!Number.isInteger(n)||t<1||n<1)throw ht("JXL_INPUT","A picture is at least one pixel wide and high, in whole pixels.");if(t>o.edge||n>o.edge)throw ht("JXL_DIMENSIONS","A picture is at most "+o.edge+" pixels on a side.");if(t*n>o.pixels)throw ht("JXL_DIMENSIONS","A picture is at most "+o.pixels.toLocaleString("en-US")+" pixels.")}(n,o,r),!(t instanceof Uint8Array||t instanceof Uint8ClampedArray))throw ht("JXL_INPUT","Pixels are a Uint8Array or Uint8ClampedArray of RGBA bytes.");if(t.length!==n*o*4)throw ht("JXL_INPUT","Pixels are width * height * 4 bytes: straight (not premultiplied) RGBA, row by row.")}(o,s,l,B),function(t){const n=function*(){let o,r;for(;!(o=q(()=>t.next(n.hurry))).done;)yield r=o.value;return r<1&&(yield 1),n.bytes=function(t){if(t.length>z.bytes)throw ht("JXL_SIZE","The encoded picture exceeds 16 MiB.");return t}(o.value)}();return n.bytes=null,n.hurry=!1,n}(function*(o,s,l,a,h){return a>=100?yield*function*(n,o,r,{shape:e=G(n,o,r),colorSpace:s}={}){let i,c;const f=e.palette?[e,{...e,palette:null}]:[e];for(let e=0;e<f.length;e++)try{const c=yield*t(P(n,o,r,f[e],s),e,f.length);(!i||c.length<i.length)&&(i=c)}catch(t){if("JXL_SIZE"!==t.code)throw t;c=t}if(!i)throw c;return i}(o,s,l,{colorSpace:h}):yield*t(function*(t){const{width:o,height:s,components:l,alpha:a}=t,h=1===l.length,u=t.quantScale||1;let y=1;for(;y/(2040*u)<1/16384;)y*=2;const p=t.ycbcr||h,M=(h?[0,0,0]:p?[1,0,2]:[0,1,2]).map(t=>l[t]),d=t=>1===t?0:2===t?1:-1,A=M.map(t=>d(t.h)),U=M.map(t=>d(t.v));if(A.includes(-1)||U.includes(-1))throw O("sampling factors other than one or two");const I=Math.max(...A),x=Math.max(...U),S=t=>I-A[t],E=t=>x-U[t];if(!p&&(I||x))throw O("an RGB JPEG with subsampling");const L=Math.ceil(o/(8<<I))<<I,T=Math.ceil(s/(8<<x))<<x,J=Math.ceil(o/D),X=Math.ceil(s/D),G=J*X,P=Math.ceil(L/256),q=P*Math.ceil(T/256),v=1===G,j=(t,n,o)=>{const r=M[t];return h&&1!==t?-1:64*(o*r.stride+n)},k=t=>{let n=1024;for(;t%n;)n>>=1;return n},z=M.map(t=>p?1:t.quant[0]/k(t.quant[0])),B=M.map(t=>p?0:1024/k(t.quant[0])),F=new Int32Array(L*T);for(let t=0;t<T;t++)for(let n=0;n<L;n++)F[t*L+n]=M[1].coeffs[j(1,n,t)]*z[1]+B[1];const Z=M[1].quant,Y=(Z[8]+Z[16]+Z[24]+Z[32]+Z[40])/u,Q=L*T,H=Q<256?0:Math.max(1,Math.min(7,r(Q)-r(Y)-7));let K=_(F,H,h);const W=new R(256);if(i(W,o,s,h?1:3,!!a,{orientation:t.orientation,colorSpace:t.colorSpace}),W.write(1,0),W.write(2,0),W.write(1,0),W.write(2,2),W.write(8,111),W.write(1,p?1:0),p)for(let t=0;t<3;t++)W.write(2,0===A[t]&&0===U[t]?0:1===A[t]&&1===U[t]?1:1===A[t]?2:3);W.write(2,0),a&&W.write(2,0),c(W,a);const $=[0,1,2].map(()=>new Int32Array(1024)),tt=(t,o)=>{const r=t%J*32,e=32*(t/J|0),s=Math.min(32,L-r),i=Math.min(32,T-e);for(let t=0;t<i;t++)for(let i=0;i<s;i++)for(const s of[1,0,2]){const c=S(s),f=E(s),l=i>>c,a=t>>f;if(l<<c!==i||a<<f!==t)continue;const h=j(s,(r>>c)+l,(e>>f)+a),u=M[s].coeffs;let w=0;if(h>=0)for(let t=1;t<64;t++)u[h+t]&&w++;const y=$[s],p=0===l?0===a?32:y[32*(a-1)]:0===a?y[32*a+l-1]:y[32*(a-1)+l]+y[32*a+l-1]+1>>1;y[32*a+l]=w;const d=K.tt(s,K.$(F[(e+t)*L+r+i]));o(K.nt(p,d),w);const A=K.ot(d);let g=w>4?0:1,b=w;for(let t=1;t<64&&b;t++){const r=u[h+lt[t]];o(A+at(b,t,g),n(r)),g=r?1:0,b-=g}}},et=(H>0?3:2)*G+q;let st=0;const it=function*(t){K=t;const n=new rt(t.rt);for(let t=0;t<G;t++)tt(t,(t,o)=>n.add(t,o)),yield++st/et;return function(t,{maxClusters:n=48,newClusterCost:o=320}={}){const r=[];for(let n=0;n<t.rt;n++)t.ut[n]>0&&r.push(n);r.sort((n,o)=>t.ut[o]-t.ut[n]);const e=[];for(const s of r){const r=t.wt([s],ot),i=N(r);let c=-1,f=1/0;for(let t=0;t<e.length;t++){const n=e[t],o=new Uint32Array(64);for(let t=0;t<64;t++)o[t]=n.ht[t]+r[t];const s=N(o)-n.yt-i;s<f&&(f=s,c=t)}if(c<0||e.length<n&&f>o)e.push({rt:[s],ht:r,yt:i});else{const t=e[c];t.rt.push(s);for(let n=0;n<64;n++)t.ht[n]+=r[n];t.yt=N(t.ht)}}e.length||e.push({rt:[],ht:new Uint32Array(64),yt:0});const s=new Uint8Array(t.rt);let i=2*t.rt;const c=e.map((n,o)=>{for(const t of n.rt)s[t]=o;let r=nt[0],e=1/0;for(const o of nt){const s=t.Mt(n.rt,o);s<e&&(e=s,r=o)}const c=w(t.wt(n.rt,r,128));return i+=e+3*c.u+40,{config:r,code:c}});return{L:s,T:c,write:(t,n,o)=>{const r=c[s[n]];g(t,r.code,r.config,o)},yt:i}}(n)};let ct=yield*it(K);if(H>0){const t=K,n=_(F,0,h),o=yield*it(n);o.yt<=ct.yt?ct=o:K=t}const ft=Array.from({length:v?1:2+q+G},()=>null),ht=t=>ft[v?0:t]||(ft[v?0:t]=new R(4096)),ut=ht(0);ut.write(1,0);const wt=(t,n)=>{const r=t*D,e=n*D,i=Math.min(D,o-r),c=Math.min(D,s-e),f=new Int32Array(i*c);for(let t=0;t<c;t++)for(let n=0;n<i;n++)f[t*i+n]=a[4*((e+t)*o+r+n)+3];return{w:i,h:c,data:f}};for(let t=0;t<3;t++)ut.write(16,e(M[t].quant[0]/z[t]/(2040*u)*128));ut.write(2,3),ut.write(16,57343),ut.write(2,1),ut.write(5,0),ut.write(1,0),ut.write(4,0),ut.write(4,K.thresholds.length);for(const t of K.thresholds)ut.t([[4,0],[8,16],[16,272],[32,65808]],n(t));ut.write(4,0),ut.write(4,0),b(ut,K.map),ut.write(1,0),ut.write(2,0),ut.write(16,0),ut.write(16,0),ut.write(8,128),ut.write(8,128),ut.write(1,0),a&&C(ut,[v?wt(0,0):{w:0,h:0,data:new Int32Array(0)}],V);for(let t=0;t<q;t++){const n=ht(1+t),o=t%P*256,e=256*(t/P|0),s=Math.min(256,L-o),i=Math.min(256,T-e);n.write(2,0),C(n,[1,0,2].map(t=>{const n=s>>S(t),r=i>>E(t),c=new Int32Array(n*r),f=z[t],l=B[t];for(let s=0;s<r;s++)for(let r=0;r<n;r++){const i=j(t,(o>>S(t))+r,(e>>E(t))+s);c[s*n+r]=i<0?0:M[t].coeffs[i]*f+l}return{w:n,h:r,data:c}}),V);const c=s*i,f=r(c);f&&n.write(f,c-1);const l=s+7>>3,a=i+7>>3,h=new Int32Array(2*c).fill(y-1,c);C(n,[{w:l,h:a,data:new Int32Array(l*a)},{w:l,h:a,data:new Int32Array(l*a)},{w:c,h:2,data:h},{w:s,h:i,data:new Int32Array(c)}],y>1?V:0),yield++st/et}const yt=ht(1+q);yt.write(1,0),yt.write(3,7),yt.write(16,e(y/(2040*u))),C(yt,[0,1,2].map(t=>{const n=new Int32Array(64),o=M[t].quant;for(let t=0;t<8;t++)for(let r=0;r<8;r++)n[8*r+t]=o[8*t+r];return{w:8,h:8,data:n}}),V);for(let t=1;t<17;t++)yt.write(3,0);G>1&&yt.write(r(G),0),yt.write(2,2),m(yt,{L:ct.L,T:ct.T});for(let t=0;t<G;t++){const n=ht(2+q+t);tt(t,(t,o)=>ct.write(n,t,o)),a&&!v&&C(n,[wt(t%J,Math.floor(t/J))],V),yield++st/et}return f(W,function(t){return t.map(t=>t?t.finish():new Uint8Array(0))}(ft))}(yield*t(function*(t,n,o,r,e){const s=Math.ceil(n/8),i=Math.ceil(o/8),c=r>=30?.1+.09*(100-r):53/3e3*r*r-1.15*r+25,f=[0,1,2].map(t=>({h:1,v:1,stride:s,rows:i,quant:Int32Array.from(wt,(n,o)=>Math.max(1,Math.round(16*c*(t?8:14)*(o?n:.5)))),coeffs:new Int16Array(s*i*64)})),l=new Float64Array(192),a=new Float64Array(64),h=(r,e)=>{for(let i=r;i<e;i++)for(let r=0;r<s;r++){for(let e=0;e<8;e++)for(let s=0;s<8;s++){const c=4*(Math.min(o-1,8*i+e)*n+Math.min(n-1,8*r+s)),f=t[c],a=t[c+1],h=t[c+2],u=8*e+s;l[u]=.299*f+.587*a+.114*h-128,l[64+u]=-.168735892*f-.331264108*a+.5*h,l[128+u]=.5*f-.418687589*a-.081312411*h}for(let t=0;t<3;t++){for(let n=0;n<8;n++)for(let o=0;o<8;o++)a[8*n+o]=v(l,64*t+8*n,1,8*o);const{coeffs:n,quant:o}=f[t],e=64*(i*s+r);for(let t=0;t<8;t++)for(let r=0;r<8;r++){const s=v(a,r,8,8*t),i=8*t+r;n[e+i]=Math.round(16*s/o[i])}}}};for(let t=0;t<i;t+=32)h(t,Math.min(i,t+32)),yield Math.min(i,t+32)/i;let u=null;for(let n=3;n<t.length;n+=4)if(255!==t[n]){u=t;break}return{width:n,height:o,components:f,quantScale:16,ycbcr:!0,orientation:1,alpha:u,colorSpace:e}}(o,s,l,a,h),0,2)),1,2)}(o,s,l,h,u))}const z=Object.freeze({bytes:16777216,pixels:24e6,edge:16384}),B=Object.freeze({bytes:16777216,pixels:4e7,edge:16384}),F=[1];for(let t=1;t<=32;t++)F.push(2*F[t-1]);class R{constructor(t=4096){this.bytes=new Uint8Array(t),this.at=0,this.dt=0,this.pending=0}write(t,n){if(!(n>=0&&n<F[t]))throw new Error("bit write out of range: "+t+" bits, "+n);let o=this.dt+n*(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&o,o=Math.floor(o/256),r-=8;this.dt=o,this.pending=r}t(t,n){for(let o=0;o<4;o++){const[r,e]=t[o];if(n>=e&&n-e<F[r])return this.write(2,o),void(r&&this.write(r,n-e))}throw new Error("U32 value out of range: "+n)}o(){this.pending&&(this.at+1>=this.bytes.length&&this.grow(),this.bytes[this.at++]=this.dt,this.dt=0,this.pending=0)}get lt(){return 8*this.at+this.pending}grow(t=0){const n=z.bytes+64;if(this.at+t+16>n)throw Object.assign(new Error("The encoded picture exceeds 16 MiB."),{code:"JXL_SIZE"});const o=new Uint8Array(Math.min(n,Math.max(2*this.bytes.length,this.at+t+16)));o.set(this.bytes.subarray(0,this.at)),this.bytes=o}append(t){if(this.at+t.at+8>=this.bytes.length&&this.grow(t.at+8),this.pending)for(let n=0;n<t.at;n++)this.write(8,t.bytes[n]);else this.bytes.set(t.bytes.subarray(0,t.at),this.at),this.at+=t.at;t.pending&&this.write(t.pending,t.dt)}finish(){const t=this.at+(this.pending?1:0),n=this.bytes.slice(0,t);return this.pending&&(n[this.at]=this.dt),n}}const D=256,Z=[1,2,3,4,0,5,17,6,16,7,8,9,10,11,12,13,14,15],Y=[2,4,3,2,2,4],Q=[0,7,3,2,1,15],H=[0,0,0],K=3,V=5,W=p(4),$=Object.freeze({X:224,minLength:7,N:W}),tt=p(0),nt=[p(0),p(1),p(2),p(3),p(4),p(4,1,1),p(5,1,1),p(2,0,1),p(3,0,1)],ot=p(4,1,1);class rt{constructor(t){this.rt=t,this.small=new Uint32Array(64*t),this.At=Array.from({length:t},()=>null),this.ut=new Float64Array(t)}add(t,n){if(this.ut[t]++,n<64)return void this.small[64*t+n]++;const o=this.At[t]||(this.At[t]=new Map);o.set(n,(o.get(n)||0)+1)}wt(t,n,o=64){const r=new Uint32Array(o);for(const o of t){const t=64*o;for(let o=0;o<64;o++)this.small[t+o]&&(r[J(n,o)]+=this.small[t+o]);const e=this.At[o];if(e)for(const[t,o]of e)r[J(n,t)]+=o}return r}Mt(t,n){let r=N(this.wt(t,n));for(const e of t){const t=64*e;for(let e=n.S;e<64;e++)this.small[t+e]&&(r+=this.small[t+e]*(o(e)-n.U-n.I));const s=this.At[e];if(s)for(const[t,e]of s)r+=e*(o(t)-n.U-n.I)}return r}}const et=[0,0,0],st=[1];for(let t=1;t<32;t++)st.push(st[t-1]/2);const it=13,ct=[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],ft=[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],lt=[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].map(t=>(7&t)<<3|t>>3),at=(t,n,o)=>2*(ft[t]+ct[n])+o,ht=(t,n)=>Object.assign(new Error(n),{code:t}),ut=Float64Array.of(.3535533905932738,.3535533905932738,.3535533905932738,.3535533905932738,.3535533905932738,.3535533905932738,.3535533905932738,.3535533905932738,.4903926402016152,.4157348061512726,.27778511650980114,.09754516100806417,-.0975451610080641,-.277785116509801,-.4157348061512727,-.4903926402016152,.46193976625564337,.19134171618254492,-.19134171618254486,-.46193976625564337,-.4619397662556434,-.19134171618254517,.191341716182545,.46193976625564326,.4157348061512726,-.0975451610080641,-.4903926402016152,-.2777851165098011,.2777851165098009,.4903926402016152,.09754516100806439,-.41573480615127256,.3535533905932738,-.35355339059327373,-.35355339059327384,.3535533905932737,.35355339059327384,-.35355339059327334,-.35355339059327356,.3535533905932733,.27778511650980114,-.4903926402016152,.09754516100806415,.4157348061512728,-.41573480615127256,-.09754516100806401,.4903926402016153,-.27778511650980076,.19134171618254492,-.4619397662556434,.46193976625564326,-.19134171618254495,-.19134171618254528,.46193976625564337,-.4619397662556432,.19134171618254478,.09754516100806417,-.2777851165098011,.4157348061512728,-.4903926402016153,.4903926402016152,-.4157348061512725,.27778511650980076,-.09754516100806429),wt=Float64Array.from({length:64},(t,n)=>{const o=.26236426446749106*Math.max(0,5*Math.sqrt((7&n)*(7&n)+(n>>3)*(n>>3))/Math.sqrt(98)-1);let r=1,e=1;for(let t=1;t<30;t++)e+=r=r*o/t;return e});export{B as LIMITS,j as encodePhoto,k as encodePhotoSteps};
|
|
1
|
+
/*! Rapier JXL 2.2.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
|
+
function*t(t,n,o){let r,e;for(;!(r=t.next(e)).done;)e=yield ot(r.value,t=>(n+t)/o);return r.value}function n(t){return t>=0?2*t:2*-t-1}function o(t){return 31-Math.clz32(t)}function r(t){return t<=1?0:32-Math.clz32(t-1)}function e(t){if(0===t)return 0;const n=t<0?32768:0;if(!((t=Math.abs(t))<65520))throw new Error("half float out of range: "+t);let o=0,r=t;for(;r>=2;)r/=2,o++;for(;r<1;)r*=2,o--;o<-14?(r=16384*t,o=-15):r-=1;let e=Math.round(1024*r);return 1024===e&&(e=0,o++),n|o+15<<10|e}function s(t){if(t>W.bytes)throw rt("JXL_SIZE","The encoded picture exceeds 16 MiB.")}function i(t){try{return t()}catch(t){if(t instanceof RangeError&&!t.code)throw rt("JXL_MEMORY","Not enough memory for this picture.");throw t}}function c(t,n){t.t([[9,1],[13,1],[18,1],[30,1]],n)}function f(t,n,o,r,e,{xyb:s=!1,orientation:i=1,colorSpace:f}={}){t.write(16,2815),t.write(1,0),c(t,o),t.write(3,0),c(t,n),t.write(1,0);const l=1!==i;t.write(1,l?1:0),l&&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),l&&t.write(1,1),t.write(2,0),t.write(1,1),t.o()}function l(t,n){t.write(n?7:5,0),t.write(11,1)}function*a(t,n,o,r){if(t.pooled){const n=yield t;return n?(n.results.forEach((t,n)=>r(n,t)),n.hurried):null}let e=!1;for(let r=0;r<n;r++)if(o(r),e=yield t.at(r),e&&t.stop(r))return null;return e}function u(t,n){const o=n.map(t=>t.length);!function(t,n){t.write(1,0),t.o();for(const o of n)t.t([[10,0],[14,1024],[22,17408],[30,4211712]],o);t.o()}(t,o);const r=t.finish(),e=r.length+o.reduce((t,n)=>t+n,0);s(e);const i=new Uint8Array(e);i.set(r);let c=r.length;for(const t of n)i.set(t,c),c+=t.length;return i}function h(t,n){const o=new Uint8Array(t.length),r=[];for(let n=0;n<t.length;n++)t[n]>0&&r.push(n);if(!r.length)return o;if(1===r.length)return o[r[0]]=1,o;for(let e=1;;e*=2){const s=r.map(n=>({w:Math.max(t[n],e),i:n,l:null,r:null})).sort((t,n)=>t.w-n.w||t.i-n.i),i=[];let c=0,f=0;const l=()=>c<s.length&&(f>=i.length||s[c].w<=i[f].w)?s[c++]:i[f++];for(;s.length-c+(i.length-f)>1;){const t=l(),n=l();i.push({w:t.w+n.w,i:-1,l:t,r:n})}const a=[[l(),0]];let u=0;for(;a.length;){const[t,n]=a.pop();t.i>=0?(o[t.i]=n,n>u&&(u=n)):a.push([t.l,n+1],[t.r,n+1])}if(u<=n)return o}}function y(t,n,o,r,e,s){const i=[];for(;i.push(r&e),r>>=s;)r--;for(let r=i.length-1;r>=0;r--)t.push(o),n.push(i[r])}function w(t,n){let o=0;for(let r=0;r<n;r++)o=o<<1|1&t,t>>=1;return o}function d(t){const n=new Uint32Array(17),o=new Uint32Array(17),r=new Uint16Array(t.length);for(const o of t)n[o]++;n[0]=0;for(let t=1,r=0;t<=16;t++)r=r+n[t-1]<<1,o[t]=r;for(let n=0;n<t.length;n++)t[n]&&(r[n]=w(o[t[n]]++,t[n]));return r}function p(t){const n=[];for(let o=0;o<t.length;o++)t[o]>0&&n.push(o);const o=n.length,r=n[o-1]+1||1,e=o<=4?new Uint8Array(r):h(t,15).subarray(0,r);let s=0;if(o<=4){n.sort((n,o)=>t[o]-t[n]||n-o),s=4===o&&t[n[0]]>t[n[2]]+t[n[3]]?1:0;const r=o<2?[0]:2===o?[1,1]:3===o?[1,2,2]:s?[1,2,3,3]:[2,2,2,2];n.forEach((t,n)=>{e[t]=r[n]}),n.sort((t,n)=>e[t]-e[n]||t-n)}return{u:r,p:e,M:d(e),A:o&&o<=4?n:null,m:s}}function M(t,n){if(n.u<=1)return;if(n.A){const r=o(n.u-1)+1;t.write(2,1),t.write(2,n.A.length-1);for(const o of n.A)t.write(r,o);return void(4===n.A.length&&t.write(1,n.m))}const r=n.p,e=[],s=[];let i=r.length-1;for(;i>0&&!r[i];)i--;let c=8;for(let t=0;t<=i;){const n=r[t];let o=1;for(;t+o<=i&&r[t+o]===n;)o++;let f=o;if(n&&n!==c&&(e.push(n),s.push(0),f--,c=n),f<3)for(let t=0;t<f;t++)e.push(n),s.push(0);else y(e,s,n?16:17,f-3,n?3:7,n?2:3);t+=o}const f=new Uint32Array(18);for(const t of e)f[t]++;let l=0;for(const t of f)t&&l++;1===l&&(f[e[0]?0:1]=f[e[0]]);const a=h(f,5),u=d(a);let w=18;for(;w>0&&!a[it[w-1]];)w--;t.write(2,0);for(let n=0;n<w;n++){const o=a[it[n]];t.write(ct[o],ft[o])}for(let n=0;n<e.length;n++){const o=e[n];t.write(a[o],u[o]),16===o?t.write(2,s[n]):17===o&&t.write(3,s[n])}}function A(t,n=0,o=0){return{split:t,U:n,I:o,S:1<<t}}function g(t,n,o=15){t.write(r(o+1),n.split),n.split!==o&&(t.write(r(n.split+1),n.U),t.write(r(n.split-n.U+1),n.I))}function b(t,n,r){if(n<t.S)return r[0]=n,r[1]=0,void(r[2]=0);const e=o(n),s=n-(1<<e),i=e-t.U-t.I;r[0]=t.S+(e-t.split<<t.U+t.I|s>>e-t.U<<t.I|s&(1<<t.I)-1),r[1]=i,r[2]=n>>t.I&(1<<i)-1}function m(t,n,o,r=0){b(t,n,lt),o[r+lt[0]]++}function U(t,n,o,r,e=0){b(o,r,lt);const s=e+lt[0];t.write(n.p[s],n.M[s]),lt[1]&&t.write(lt[1],lt[2])}function I(t,n){let o=0;for(const t of n)o=Math.max(o,t);const e=o?r(o+1):0;if(e<=3){if(t.write(1,1),t.write(2,e),e)for(const o of n)t.write(e,o);return}t.write(1,0),t.write(1,0);const s=A(8),i=A(4),c=n.length>=64,f=new Uint32Array(c?257:o+1),l=[];for(let t=0;t<n.length;){let o=1;for(;c&&t+o<n.length&&n[t+o]===n[t];)o++;if(o>=8)l.push([n[t],o-1]),f[n[t]]++,m(i,o-1-7,f,224),t+=o;else{for(let r=0;r<o;r++)l.push([n[t],0]),f[n[t]]++;t+=o}}const a=p(f);if(!c){x(t,{L:new Uint8Array(1),J:[{config:s,code:a}]});for(const[n]of l)U(t,a,s,n);return}const u=new Uint32Array(1);u[0]=1,x(t,{T:{X:224,minLength:7,N:i},L:new Uint8Array([1,0]),J:[{config:A(0),code:p(u)},{config:s,code:a}]});for(const[n,o]of l)U(t,a,s,n),o&&U(t,a,i,o-7,224)}function x(t,{T:n=null,L:r,J:e}){t.write(1,n?1:0),n&&(t.t([[0,224],[0,512],[0,4096],[15,8]],n.X),t.t([[0,3],[0,4],[2,5],[8,9]],n.minLength),g(t,n.N,8)),r.length>1&&I(t,r),t.write(1,1);for(const n of e)g(t,n.config,15);for(const n of e){const r=n.code.u-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 n of e)M(t,n.code)}function S(t,n=0,o=1){return{_:t,offset:n,C:o,context:-1}}function E(t){return function(t,n){const o=(r,e)=>{if(r===e)return n?t[r].O:t[r];const s=r+e>>1;return function(t,n,o,r){return{P:t,q:n,left:o,right:r}}(n,n?t[s].F:s,o(s+1,e),o(r,s))};return o(0,t.length-1)}(t,0)}function L(t,o){const r=[o],e=[],s=[];for(const t of r){if(t.left){e.push(t.P+1,n(t.q)),r.push(t.left,t.right);continue}t.context=s.length,s.push(t);const o=1===t.C?0:31-Math.clz32(t.C&-t.C);e.push(0,t._,n(t.offset),o,(t.C>>o)-1)}const i=new Uint32Array(64);for(const t of e)m(wt,t,i);const c=p(i);x(t,{L:new Uint8Array(6),J:[{config:wt,code:c}]});for(const n of e)U(t,c,wt,n);return s}function J(t,n){const o=[[3,0],[6,8],[10,72],[13,1096]];if("rct"===n.type)t.write(2,0),t.t(o,n.G),t.t([[0,6],[2,0],[4,2],[6,10]],n.j??6);else if("palette"===n.type)t.write(2,1),t.t(o,n.G),t.t([[0,1],[0,3],[0,4],[13,1]],n.k),t.t([[8,0],[10,256],[12,1280],[16,5376]],n.B),t.t([[0,0],[8,1],[10,257],[16,1281]],n.R??0),t.write(4,n._??0);else{if("squeeze"!==n.type)throw new Error("unknown transform "+n.type);{t.write(2,2);const r=n.Z??[];t.t([[0,0],[4,1],[6,9],[8,41]],r.length);for(const n of r)t.write(1,n.D?1:0),t.write(1,n.Y?1:0),t.t(o,n.G),t.t([[0,1],[0,2],[0,3],[4,4]],n.k)}}}function T(t,{H:n=!0,K:o=[]}={}){t.write(1,n?1:0),t.write(1,1),t.t([[0,0],[0,1],[4,2],[8,18]],o.length);for(const n of o)J(t,n)}function X(t,n,o,r=t=>t.context,e=()=>wt){const s=new Uint8Array(n.length+1);for(const t of n)s[t.context]=r(t)+1;const i=new Uint32Array(2);i[1]=1;const c=[{config:wt,code:p(i)}],f=Math.max(...s);for(let t=0;t<f;t++)c.push({config:e(t),code:p(o[t])});return x(t,{T:yt,L:s,J:c}),c}function N(t,o,r,e,s,i,c=!1){if(et.channel?.(t,o,r,e,s,i,c))return;const f=i._,l=i.offset,a=i.C,u=i.config||wt,h=ht;let y=0;const w=(n,r=u,e=0)=>{t?U(t,o,r,n,e):c&&!e?o[n]++:m(r,n,o,c?o.length-33:e)},d=()=>{if(y){if(y>=8)w(0),w(y-7-1,h,224);else for(let t=0;t<y;t++)w(0);y=0}},p=(t,o)=>{const e=r[t];let s;1===a?s=e-o-l:(s=Math.round((e-o-l)/a),r[t]=o+l+s*a),0!==s?(d(),w(n(s))):y++};for(let t=0,n=0;t<s;t++)for(let o=0;o<e;o++,n++){let s;if(0===f)s=0;else{const i=o?r[n-1]:t?r[n-e]:0,c=t?r[n-e]:i;if(1===f)s=i;else if(2===f)s=c;else if(3===f)s=(i+c)/2|0;else{const l=i+c-(o&&t?r[n-e-1]:i);if(5===f){const t=i<c?i:c,n=i<c?c:i;s=l<t?t:l>n?n:l}else{if(4!==f)throw new Error("predictor "+f+" is not coded here");s=Math.abs(l-i)<Math.abs(l-c)?i:c}}}p(n,s)}d()}function _(t){const n=t.length-33,r=[0,0,0];let e,s;for(const i of dt){const c=new Uint32Array(257);let f=0;for(let o=0;o<n;o++)t[o]&&(b(i,o,r),c[r[0]]+=t[o],f+=t[o]*r[1]);for(let o=0;o<33;o++)c[224+o]=t[n+o],f+=t[n+o]*Math.max(0,o-12);const l=p(c),a=new nt(128);g(a,i),M(a,l);let u=a.V+f+(1===l.u?1:5+o(l.u-1));for(let t=0;t<c.length;t++)c[t]&&(u+=c[t]*l.p[t]);const h={config:i,W:c,$:u,extra:f};e||(e=h),(!s||u<s.$)&&(s=h)}return[e,s]}function v(t,n,o,{palette:r=!0}={}){const e=n*o;let s=!0,i=!0;for(let n=0,o=0;o<e&&(255===t[n+3]||(s=!1,i))&&(t[n]===t[n+1]&&t[n]===t[n+2]||(i=!1,s));o++,n+=4);const c=i?1:3,f=!s,l=c+(f?1:0),a=n=>pt(t,n,i,f),u=new Map;let h=null;for(let t=0,n=0;r&&n<e;n++,t+=4){const n=a(t);if(0!==n&&!u.has(n)&&(u.set(n,1),u.size>=2048))break}if(r&&u.size<2048){const t=[...u.keys()],n=(t,n)=>t>>>8*n&255,o=t=>(.299*n(t,0)+.587*n(t,1)+.114*n(t,2)+.01)*(4===l?n(t,3):1);t.sort((t,n)=>o(t)-o(n)),h={colours:[0,...t],byte:n}}return{colour:c,alpha:f,channels:l,palette:h}}function C({channels:t,alpha:n,palette:o},r){const e=t-(n?1:0)==1,s=o&&new Map(o.map((t,n)=>[t,n]));return(i,c,f,l,a,u,h)=>{if(r&&!o&&t>=3)r.fill(i,c,f,l,a,u,h);else if(o||!et.fill?.(c,f,l,a,u,h,t,i))for(let r=0;r<h;r++){let h=4*((a+r)*f+l),y=r*u;for(let r=0;r<u;r++,h+=4,y++)if(o)i[0][y]=s.get(pt(c,h,e,n));else if(t>=3){const n=c[h],o=c[h+1],r=c[h+2],e=n-r,s=r+(e>>1),f=o-s;i[0][y]=s+(f>>1),i[1][y]=e,i[2][y]=f,4===t&&(i[3][y]=c[h+3])}else i[0][y]=c[h],2===t&&(i[1][y]=c[h+3])}}}function O(t,n){const{leaves:o,picture:r,M:e}=t,s=C(t,n),i=r.map(()=>new Int16Array(65536));return(t,n,c,f,l,a,u)=>{if(s(i,t,n,c,f,l,a),!e)return void r.forEach((t,n)=>N(null,u[t],i[n],l,a,o[t],!0));const h=u||new nt(l*a*r.length+64);return u||T(h,{H:!0,K:[]}),r.forEach((t,n)=>N(h,e[t],i[n],l,a,o[t])),u?void 0:h.finish()}}function*P(t,n,o,r,e,s,i){const{channels:c,alpha:h,palette:y}=r,w=function(t,n,o=256){const r=Math.ceil(t/o),e=Math.ceil(n/o);return{tt:r,nt:e,ot:Math.ceil(t/(8*o)),rt:Math.ceil(n/(8*o)),et:1===r&&1===e}}(n,o),d=w.tt*w.nt,A=y?2:c,g=Array.from({length:A},()=>S(ut)),b=E(g),m={channels:c,alpha:h,palette:y&&y.colours,rct:s?.type,leaves:g,picture:y?[w.et?1:0]:g.map((t,n)=>n)},U=C(m,s),I=Array.from({length:c},()=>new Int16Array(1024)),x=t=>function(t,n,o,r){const e=r%t.tt*st,s=(r/t.tt|0)*st;return[e,s,Math.min(st,n-e),Math.min(st,o-s)]}(w,n,o,t);let J=null;if(y){const t=y.colours.length;J=new Int16Array(t*c),y.colours.forEach((n,o)=>{for(let r=0;r<c;r++)J[r*t+o]=y.byte(n,r)})}if(!y){const r=t=>{const n=p(t),o=new nt(128);M(o,n);let r=o.V;for(let o=0;o<t.length;o++)t[o]&&(r+=t[o]*(n.p[o]+Math.max(0,o<224?o-1:o-236)));return r},e=Math.min(n,32),s=Math.min(o,32),i=[ut,at].map(t=>({_:t,W:g.map(()=>new Uint32Array(257))}));for(const r of[0,.5,1]){U(I,t,n,Math.floor((n-e)*r),Math.floor((o-s)*r),e,s);for(const t of i)for(let n=0;n<c;n++)N(null,t.W[n],I[n],e,s,S(t._))}g.forEach((t,n)=>{const o=i.map(t=>r(t.W[n])),e=o[1]<o[0]?1:0;t._=i[e]._})}const v=y?[{type:"palette",G:0,k:c,B:y.colours.length}]:c>=3&&0!==s?.type?[{type:"rct",G:0,j:s?.type??6}]:[];let P=g.map(()=>new Uint32Array(33+(y?4096:1024)));J&&N(null,P[0],J,y.colours.length,c,g[0],!0);const q=O(m,s);yield*a({pooled:i,kind:"plan",setup:{...m,sizes:P.map(t=>t.length)},at:t=>(t+1)/(2*d),stop:()=>!1},d,o=>q(t,n,...x(o),P),(t,n)=>function(t,n){t.forEach((t,o)=>{const r=n[o];for(let n=0;n<t.length;n++)t[n]+=r[n]})}(P,n));const F=P.map(_),G=F.reduce((t,n)=>t+n[0].$-n[1].$,0)>27*(w.et?1:d+1)+8?1:0;g.forEach((t,n)=>{t.config=F[n][G].config}),P=F.map(t=>t[G].W);const j=new nt(256);f(j,n,o,r.colour,r.alpha,{colorSpace:e}),function(t,{alpha:n,shift:o=1}){t.write(9,8),n&&t.write(2,0),t.write(2,o),l(t,n)}(j,{alpha:r.alpha});const k=new nt(4096);k.write(1,1),k.write(1,1);const z=X(k,L(k,b),P,t=>g.indexOf(t),t=>g[t].config),B={...m,M:g.map((t,n)=>z[n+1]?.code)},R=O(B,s);T(k,{H:!0,K:v}),J&&N(k,B.M[0],J,y.colours.length,c,g[0]);const Z=[];if(w.et)R(t,n,0,0,n,o,k),yield 1,Z.push(k.finish());else{Z.push(k.finish());for(let t=0;t<w.ot*w.rt+1;t++)Z.push(new Uint8Array(0));const o=Z.length,r=(t,n)=>{Z[o+t]=n};yield*a({pooled:i,kind:"plan",setup:B,at:t=>(d+t+1)/(2*d),stop:()=>!1},d,o=>r(o,R(t,n,...x(o))),r)}return u(j,Z)}function q(t,n,o,r){let e=0;return e+=t[n]*Mt[r],e+=t[n+o]*Mt[r+1],e+=t[n+2*o]*Mt[r+2],e+=t[n+3*o]*Mt[r+3],e+=t[n+4*o]*Mt[r+4],e+=t[n+5*o]*Mt[r+5],e+=t[n+6*o]*Mt[r+6],e+=t[n+7*o]*Mt[r+7],e}function F(t,n,o){const r=new Float64Array(192),e=new Float64Array(64),s=new Float64Array(192);return(i,c)=>{for(let e=0;e<8;e++)for(let s=0;s<8;s++){const f=4*(Math.min(o-1,8*c+e)*n+Math.min(n-1,8*i+s)),l=t[f],a=t[f+1],u=t[f+2],h=8*e+s;r[h]=.299*l+.587*a+.114*u-128,r[64+h]=-.168735892*l-.331264108*a+.5*u,r[128+h]=.5*l-.418687589*a-.081312411*u}for(let t=0;t<3;t++){for(let n=0;n<8;n++)for(let o=0;o<8;o++)e[8*n+o]=q(r,64*t+8*n,1,8*o);for(let n=0;n<8;n++)for(let o=0;o<8;o++)s[64*t+8*n+o]=q(e,o,8,8*n)}return s}}function*G(t,n,o,r,e){const s=Math.ceil(n/8),i=Math.ceil(o/8),c=r>=30?.1+.09*(100-r):53/3e3*r*r-1.15*r+25,f=[0,1,2].map(t=>({h:1,v:1,stride:s,rows:i,quant:Int32Array.from(At,(n,o)=>Math.max(1,Math.round(16*c*(t?18:10)*(o?n:.25)))),coeffs:new Int16Array(s*i*64)})),l=F(t,n,o),a=(t,n)=>{for(let o=t;o<n;o++)for(let t=0;t<s;t++){const n=l(t,o),r=64*(o*s+t);for(let t=0;t<3;t++)for(let o=0;o<64;o++)f[t].coeffs[r+o]=Math.round(16*n[64*t+o]/f[t].quant[o])}};for(let t=0;t<i;t+=32)a(t,Math.min(i,t+32)),yield Math.min(i,t+32)/i;let u=null;for(let n=3;n<t.length;n+=4)if(255!==t[n]){u=t;break}return{width:n,height:o,components:f,quantScale:16,ycbcr:!0,orientation:1,alpha:u,colorSpace:e}}function j(n,o,r,c,f){const{quality:l,colorSpace:a}=function(t={},n=100){if(null===t||"object"!=typeof t)throw rt("JXL_INPUT","Options are an object: {quality, colorSpace}.");if(void 0!==t.quality&&(n=t.quality),"number"!=typeof n||!Number.isFinite(n)||n<1||n>100)throw rt("JXL_INPUT","Quality is a number from 1 to 100.");const o=void 0===t.colorSpace?"srgb":t.colorSpace;if("srgb"!==o&&"display-p3"!==o)throw rt("JXL_INPUT","The colour space is srgb or display-p3.");return{quality:n,colorSpace:o}}(c,90),u=function(t={}){if(null===t||"object"!=typeof t)throw rt("JXL_INPUT","Options are an object.");const n=void 0===t.effort?1:t.effort;if(!Number.isInteger(n)||n<1||n>9)throw rt("JXL_INPUT","Effort is a whole number from 1 to 9.");return n}(c);return function(t,n,o,r){if(function(t,n,o=W){if(!Number.isInteger(t)||!Number.isInteger(n)||t<1||n<1)throw rt("JXL_INPUT","A picture is at least one pixel wide and high, in whole pixels.");if(t>o.edge||n>o.edge)throw rt("JXL_DIMENSIONS","A picture is at most "+o.edge+" pixels on a side.");if(t*n>o.pixels)throw rt("JXL_DIMENSIONS","A picture is at most "+o.pixels.toLocaleString("en-US")+" pixels.")}(n,o,r),!(t instanceof Uint8Array||t instanceof Uint8ClampedArray))throw rt("JXL_INPUT","Pixels are a Uint8Array or Uint8ClampedArray of RGBA bytes.");if(t.length!==n*o*4)throw rt("JXL_INPUT","Pixels are width * height * 4 bytes: straight (not premultiplied) RGBA, row by row.")}(n,o,r,$),function(t){const n=function*(){let o,r;for(;!(o=i(()=>t.next(n.hurry))).done;)yield r=o.value;return r<1&&(yield 1),n.bytes=(s((e=o.value).length),e);var e}();return n.bytes=null,n.hurry=!1,n}(function*(n,o,r,s,i,c,f){if(s>=100)return yield*function*(n,o,r,{shape:e=v(n,o,r),colorSpace:s,rct:i,pooled:c}={}){let f,l;const a=e.palette?[e,{...e,palette:null}]:[e];for(let e=0;e<a.length;e++)try{const l=yield*t(P(n,o,r,a[e],s,i,c),e,a.length);(!f||l.length<f.length)&&(f=l)}catch(t){if("JXL_SIZE"!==t.code)throw t;l=t}if(!f)throw l;return f}(n,o,r,{colorSpace:i});if(c<5)return yield*t(f(yield*t(G(n,o,r,s,i),0,2),c),1,2);const l=yield*t(G(n,o,r,s,i),0,4);let a=yield*t(f(l,4),1,4);if(yield.5)return a;try{const o=yield*t(function*(t,n,o=0){const{width:r,height:s,components:i,quantScale:c}=n,f=i[0].stride,l=i[0].rows,a=f*l,u=gt.length;if(t.byteLength+i.reduce((t,n)=>t+n.coeffs.byteLength,0)+Math.max(a*(10*u+2)+o,7*$.bytes)>Ut)return null;const h=new Float64Array(a*u),y=new Uint16Array(a*u),w=F(t,r,s),d=i.map(t=>t.quant),p=i.map(t=>t.coeffs),M=gt.map(t=>d.map(n=>Float64Array.from(n,n=>n*function(t){const n=e(t);let o=1+(1023&n)/1024,r=(n>>10)-15;for(;r<0;)o/=2,r++;for(;r>0;)o*=2,r--;return o}(4/(2040*c))*8*255/t))),A=new Int16Array(192),g=new Float64Array(3),b=new Int32Array(6),m=new Float64Array(6),U=new Float64Array(6);let I=0,x=0;const S=(t,n,o)=>{let r=0,e=0;const s=bt[o]*d[0][1]*d[0][1]/(c*c);for(let i=1;i<64;i++){if(!o){for(let r=0;r<3;r++){const s=p[r][64*n+i];g[r]=It(s,r)*M[o][r][i]-t[64*r+i],s&&(e+=2+2*(32-Math.clz32(Math.abs(s))))}r+=xt(g[0],g[1],g[2]);continue}for(let n=0;n<3;n++){const r=t[64*n+i],e=M[o][n][i],s=Math.floor(r/e);for(let t=0;t<2;t++){const o=s+t;b[2*n+t]=o,U[2*n+t]=o?2+2*(32-Math.clz32(Math.abs(o))):0,m[2*n+t]=o<-32768||o>32767?1/0:It(o,n)*e-r}}let c=0,f=1/0,l=0;for(let t=0;t<8;t++){const n=xt(m[1&t],m[2+(t>>1&1)],m[4+(t>>2)]),o=n+s*(U[1&t]+U[2+(t>>1&1)]+U[4+(t>>2)]);o<f&&(f=o,c=t,l=n)}if(f===1/0)return{error:1/0,rate:65535};r+=l;for(let t=0;t<3;t++){const n=b[2*t+(c>>t&1)];A[64*t+i]=n,n&&(e+=2+2*(32-Math.clz32(Math.abs(n))))}}return{error:r,rate:e}};for(let t=0;t<l;t++)for(let n=0;n<f;n++){const o=t*f+n,r=w(n,t);for(let t=0;t<u;t++){const n=S(r,o,t),e=o*u+t;h[e]=n.error,y[e]=n.rate,t||(I+=n.error,x+=n.rate)}if(((o+1)%1024==0||o+1===a)&&(yield(o+1)/(3*a)))return null}const E=new Uint8Array(a);let L=0;const J=function*(t,n){let o=0,r=0;for(let e=0;e<a;e++){const s=e*u;let i=0,c=h[s]+t*y[s];for(let n=1;n<u;n++){const o=h[s+n]+t*y[s+n];o<c&&(c=o,i=n)}if(o+=h[s+i],r+=y[s+i],n&&(E[e]=i),((e+1)%1024==0||e+1===a)&&(yield 1/3+(L+(e+1)/a)/147))return null}return L++,{error:o,rate:r}};let T=0,X=Math.max(1,I/Math.max(1,x));for(let t=0;t<24;t++){const t=yield*J(X,!1);if(!t)return null;if(t.error>I)break;X*=2}for(let t=0;t<24;t++){const t=(T+X)/2,n=yield*J(t,!1);if(!n)return null;n.error<=I?T=t:X=t}const N=yield*J(T,!0);if(!N||N.error>I||N.rate>=x||!E.some(t=>t))return null;const _=new Uint8Array(a);for(let t=0;t<l;t++)for(let n=0;n<f;n++){const o=t*f+n,r=E[o];if(_[o]=gt[r],r){S(w(n,t),o,r);for(let t=0;t<3;t++)for(let n=1;n<64;n++)p[t][64*o+n]=A[64*t+n]}if(((o+1)%1024==0||o+1===a)&&(yield 2/3+(o+1)/(3*a)))return null}return{...n,quantFieldBase:4,quantFields:_,reconstructionError:{baseline:I,candidate:N.error}}}(n,l,a.byteLength),2,4);o&&(a=yield*t(f(o,4,a),3,4))}catch(t){if((!(t instanceof RangeError)||t.code)&&"JXL_SIZE"!==t.code)throw t}return a}(n,o,r,l,a,u,f))}function k(t,n){return b(t,n,Jt),Jt[0]}function z(t){let n=0;for(t>=2&&t<4294967296&&(n=31-Math.clz32(t),t*=Tt[n]);t>=1.4142135623730951;)t/=2,n++;for(;t<.7071067811865476;)t*=2,n--;const o=(t-1)/(t+1),r=o*o;let e=o,s=o;for(let t=3;t<30;t+=2)s+=(e*=r)/t;return n+2.885390081777927*s}function B(t){let n=0,o=0;for(const o of t)n+=o;for(const r of t)r&&(o+=r*z(n/r));return o}function R(t,{maxClusters:n=48,newClusterCost:o=320}={}){const r=[];for(let n=0;n<t.st;n++)t.it[n]>0&&r.push(n);r.sort((n,o)=>t.it[o]-t.it[n]);const e=[];for(const s of r){const r=t.ct([s],Et),i=B(r);let c=-1,f=1/0;for(let t=0;t<e.length;t++){const n=e[t],o=new Uint32Array(64);for(let t=0;t<64;t++)o[t]=n.W[t]+r[t];const s=B(o)-n.$-i;s<f&&(f=s,c=t)}if(c<0||e.length<n&&f>o)e.push({st:[s],W:r,$:i});else{const t=e[c];t.st.push(s);for(let n=0;n<64;n++)t.W[n]+=r[n];t.$=B(t.W)}}e.length||e.push({st:[],W:new Uint32Array(64),$:0});const s=new Uint8Array(t.st);let i=2*t.st;const c=e.map((n,o)=>{for(const t of n.st)s[t]=o;let r=St[0],e=1/0;for(const o of St){const s=t.ft(n.st,o);s<e&&(e=s,r=o)}const c=p(t.ct(n.st,r,128));return i+=e+3*c.u+40,{config:r,code:c}});return{L:s,J:c,write:(t,n,o)=>{const r=c[s[n]];U(t,r.code,r.config,o)},$:i}}function Z(t,n="JXL_JPEG"){return Object.assign(new Error("JPEG: "+t),{code:n})}function D(t,n,o){const r=[];if(n>0){const o=new Uint32Array(2048);let e=0;for(const n of t)o[Math.min(2047,Math.max(0,n+1024))]++,e++;let s=0,i=e/(n+1);for(let t=0;t<2048&&r.length<n;t++)s+=o[t],s>i&&(r.push(t-1025),i=e*(r.length+1)/(n+1))}const e=r.length+1,s=new Uint8Array(39*e);for(let t=0;t<e;t++)for(let n=0;n<Nt;n++)s[(0+n)*e+t]=t,s[(13+n)*e+t]=o?e:e+(t>>1),s[(26+n)*e+t]=o?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,lt:e,map:s,ut:i,ht:t=>{let n=0;for(const o of r)t>o&&n++;return n},yt:(t,n)=>s[(t<2?1^t:2)*Nt*e+n],wt:(t,n)=>(Math.min(t,64)<8?Math.min(t,64):4+(Math.min(t,64)>>1))*i+n,dt:t=>37*i+458*t,st:495*i}}function Y(t,n,o){const r=n.map(()=>S(o)),e=r.map(()=>new Uint32Array(257));n.forEach((t,n)=>N(null,e[n],t.data,t.w,t.h,r[n])),T(t,{H:!1,K:[]});const s=L(t,E(r)),i=X(t,s,e,t=>r.indexOf(t));n.forEach((n,o)=>N(t,i[o+1].code,n.data,n.w,n.h,r[o]))}function*Q(t,o={}){const{width:i,height:c,components:a,alpha:h}=t,y=1===a.length,w=t.quantScale||1;let d=t.quantFieldBase||1;for(;d/(2040*w)<1/16384;)d*=2;const M=t.ycbcr||y,g=y?[0,0,0]:M?[1,0,2]:[0,1,2],b=g.map(t=>a[t]),S=o.orders?g.map(t=>o.orders[t].map(t=>Ct[t])):[Ct,Ct,Ct],E=t=>1===t?0:2===t?1:-1,L=b.map(t=>E(t.h)),J=b.map(t=>E(t.v));if(L.includes(-1)||J.includes(-1))throw Z("sampling factors other than one or two");const T=Math.max(...L),X=Math.max(...J),N=t=>T-L[t],_=t=>X-J[t];if(!M&&(T||X))throw Z("an RGB JPEG with subsampling");const v=Math.ceil(i/(8<<T))<<T,C=Math.ceil(c/(8<<X))<<X,O=Math.ceil(i/st),P=Math.ceil(c/st),q=O*P,F=Math.ceil(v/256),G=F*Math.ceil(C/256),j=1===q,k=(t,n,o)=>{const r=b[t];return y&&1!==t?-1:64*(o*r.stride+n)},z=t=>{let n=1024;for(;t%n;)n>>=1;return n},B=b.map(t=>M?1:t.quant[0]/z(t.quant[0])),Q=b.map(t=>M?0:1024/z(t.quant[0])),H=new Int32Array(v*C);for(let t=0;t<C;t++)for(let n=0;n<v;n++)H[t*v+n]=b[1].coeffs[k(1,n,t)]*B[1]+Q[1];const K=b[1].quant,V=(K[8]+K[16]+K[24]+K[32]+K[40])/w,W=v*C,$=W<256?0:Math.max(1,Math.min(7,r(W)-r(V)-7));let tt=D(H,$,y);const ot=new nt(256);if(f(ot,i,c,y?1:3,!!h,{orientation:t.orientation,colorSpace:t.colorSpace}),ot.write(1,0),ot.write(2,0),ot.write(1,0),ot.write(2,2),ot.write(8,111),ot.write(1,M?1:0),M)for(let t=0;t<3;t++)ot.write(2,0===L[t]&&0===J[t]?0:1===L[t]&&1===J[t]?1:1===L[t]?2:3);ot.write(2,0),h&&ot.write(2,0),l(ot,h);const rt=[0,1,2].map(()=>new Int32Array(1024)),et=(t,o)=>{const r=t%O*32,e=32*(t/O|0),s=Math.min(32,v-r),i=Math.min(32,C-e);for(let t=0;t<i;t++)for(let i=0;i<s;i++)for(const s of[1,0,2]){const c=N(s),f=_(s),l=i>>c,a=t>>f;if(l<<c!==i||a<<f!==t)continue;const u=k(s,(r>>c)+l,(e>>f)+a),h=b[s].coeffs;let y=0;if(u>=0)for(let t=1;t<64;t++)h[u+t]&&y++;const w=rt[s],d=0===l?0===a?32:w[32*(a-1)]:0===a?w[32*a+l-1]:w[32*(a-1)+l]+w[32*a+l-1]+1>>1;w[32*a+l]=y;const p=tt.yt(s,tt.ht(H[(e+t)*v+r+i]));o(tt.wt(d,p),y);const M=tt.dt(p);let A=y>4?0:1,g=y;for(let t=1;t<64&&g;t++){const r=h[u+S[s][t]];o(M+Ot(g,t,A),n(r)),A=r?1:0,g-=A}}},it=($>0?3:2)*q+G;let ct=0;const ft=function*(t){tt=t;const n=new Lt(t.st);for(let t=0;t<q;t++)et(t,(t,o)=>n.add(t,o)),yield++ct/it;return(o.coding||R)(n,o.clusters)};let lt=yield*ft(tt);if($>0){const t=tt,n=D(H,0,y),o=yield*ft(n);o.$<=lt.$?lt=o:tt=t}const at=Array.from({length:j?1:2+G+q},()=>null),ht=t=>at[j?0:t]||(at[j?0:t]=new nt(4096)),yt=new Uint32Array(at.length);let wt=0;const dt=t=>{t=j?0:t;const n=Math.ceil(at[t].V/8);wt+=n-yt[t],yt[t]=n,s(wt)},pt=ht(0);pt.write(1,0);const Mt=(t,n)=>{const o=t*st,r=n*st,e=Math.min(st,i-o),s=Math.min(st,c-r),f=new Int32Array(e*s);for(let t=0;t<s;t++)for(let n=0;n<e;n++)f[t*e+n]=h[4*((r+t)*i+o+n)+3];return{w:e,h:s,data:f}};for(let t=0;t<3;t++)pt.write(16,e(b[t].quant[0]/B[t]/(2040*w)*128));pt.write(2,3),pt.write(16,57343),pt.write(2,1),pt.write(5,0),pt.write(1,0),pt.write(4,0),pt.write(4,tt.thresholds.length);for(const t of tt.thresholds)pt.t([[4,0],[8,16],[16,272],[32,65808]],n(t));pt.write(4,0),pt.write(4,0),I(pt,tt.map),pt.write(1,0),pt.write(2,0),pt.write(16,0),pt.write(16,0),pt.write(8,128),pt.write(8,128),pt.write(1,0),h&&Y(pt,[j?Mt(0,0):{w:0,h:0,data:new Int32Array(0)}],ut),dt(0);for(let n=0;n<G;n++){const o=ht(1+n),e=n%F*256,s=256*(n/F|0),i=Math.min(256,v-e),c=Math.min(256,C-s);o.write(2,0),Y(o,[1,0,2].map(t=>{const n=i>>N(t),o=c>>_(t),r=new Int32Array(n*o),f=B[t],l=Q[t];for(let i=0;i<o;i++)for(let o=0;o<n;o++){const c=k(t,(e>>N(t))+o,(s>>_(t))+i);r[i*n+o]=c<0?0:b[t].coeffs[c]*f+l}return{w:n,h:o,data:r}}),ut);const f=i*c,l=r(f);l&&o.write(l,f-1);const a=i+7>>3,u=c+7>>3,h=new Int32Array(2*f).fill(d-1,f);if(t.quantFields)for(let n=0;n<c;n++)for(let o=0;o<i;o++)h[f+n*i+o]=t.quantFields[(s+n)*v+e+o]-1;Y(o,[{w:a,h:u,data:new Int32Array(a*u)},{w:a,h:u,data:new Int32Array(a*u)},{w:f,h:2,data:h},{w:i,h:c,data:new Int32Array(f)}],d>1?ut:0),dt(1+n),yield++ct/it}const At=ht(1+G);At.write(1,0),At.write(3,7),At.write(16,e(d/(2040*w))),Y(At,[0,1,2].map(t=>{const n=new Int32Array(64),o=b[t].quant;for(let t=0;t<8;t++)for(let r=0;r<8;r++)n[8*r+t]=o[8*t+r];return{w:8,h:8,data:n}}),ut);for(let t=1;t<17;t++)At.write(3,0);if(q>1&&At.write(r(q),0),o.orders){At.t([[0,95],[0,19],[0,0],[13,0]],1);const t=[];for(const n of g){const r=Array.from({length:64},(t,n)=>n),e=o.orders[n].map(t=>{const n=r.indexOf(t);return r.splice(n,1),n});let s=64;for(;s>1&&!e[s-1];)s--;t.push(s-1,...e.slice(1,s))}const n=A(0),r=new Uint32Array(64);for(const o of t)m(n,o,r);const e=p(r);x(At,{L:new Uint8Array(8),J:[{config:n,code:e}]});for(const o of t)U(At,e,n,o)}else At.write(2,2);lt.writeHistograms?lt.writeHistograms(At):x(At,{L:lt.L,J:lt.J}),dt(1+G);for(let t=0;t<q;t++){const n=ht(2+G+t);et(t,(t,o)=>lt.write(n,t,o)),lt.flush&<.flush(n),h&&!j&&Y(n,[Mt(t%O,Math.floor(t/O))],ut),dt(2+G+t),yield++ct/it}return u(ot,function(t){return t.map(t=>t?t.finish():new Uint8Array(0))}(at))}function*H(t,n,o){if(n<3&&!o)return yield*Q(t);let r=o;try{const o=Q(t);let e,s;for(;!(e=o.next()).done;)if(s=yield e.value/2,s&&r&&e.value<1)return o.return(),r;if((!r||e.value.length<r.length)&&(r=e.value),s||n<3)return r;const i=n<4?1:3,c=Q(t,{clusters:{maxClusters:32,newClusterCost:160}});for(;!(e=c.next()).done;)if(s=yield.5+e.value/(2*i),s&&e.value<1)return c.return(),r;if(e.value.length<r.length&&(r=e.value),s||n<4)return r;const f=function*(t){const n=Xt.map(t=>(7&t)<<3|t>>3),o=t.map(()=>new Uint32Array(64)),r=t.reduce((t,n)=>t+n.coeffs.length,0);let e=0;for(let s=0;s<t.length;s++){const{coeffs:i}=t[s],c=o[s];for(let t=0;t<i.length;t+=65536){const o=Math.min(t+65536,i.length);for(let r=t;r<o;r+=64)for(let t=1;t<64;t++)i[r+n[t]]&&c[t]++;if(e+=o-t,yield e/r)return null}}return o.map(t=>[0,...Array.from({length:63},(t,n)=>n+1).sort((n,o)=>Math.floor(t[o]/8)-Math.floor(t[n]/8)||n-o)])}(t.components);for(;!(e=f.next()).done;)if(yield.5+(1+e.value)/6)return f.return(),r;const l=Q(t,{orders:e.value});for(;!(e=l.next()).done;)if(s=yield.5+(2+e.value)/6,s&&e.value<1)return l.return(),r;return e.value.length<r.length?e.value:r}catch(t){if(!r||(!(t instanceof RangeError)||t.code)&&"JXL_SIZE"!==t.code)throw t;return r}}function K(t,n,o,r){return function(t){let n;for(;!(n=t.next()).done;);return n.value}(V(t,n,o,r))}function V(t,n,o,r){return j(t,n,o,r,H)}const W=Object.freeze({bytes:16777216,pixels:24e6,edge:16384}),$=Object.freeze({bytes:16777216,pixels:4e7,edge:16384}),tt=[1];for(let t=1;t<=32;t++)tt.push(2*tt[t-1]);class nt{constructor(t=4096){this.bytes=new Uint8Array(t),this.at=0,this.Mt=0,this.pending=0}write(t,n){if(!(n>=0&&n<tt[t]))throw new Error("bit write out of range: "+t+" bits, "+n);let o=this.Mt+n*(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&o,o=Math.floor(o/256),r-=8;this.Mt=o,this.pending=r}t(t,n){for(let o=0;o<4;o++){const[r,e]=t[o];if(n>=e&&n-e<tt[r])return this.write(2,o),void(r&&this.write(r,n-e))}throw new Error("U32 value out of range: "+n)}o(){this.pending&&(this.at+1>=this.bytes.length&&this.grow(),this.bytes[this.at++]=this.Mt,this.Mt=0,this.pending=0)}get V(){return 8*this.at+this.pending}grow(t=0){const n=W.bytes+64;if(this.at+t+16>n)throw Object.assign(new Error("The encoded picture exceeds 16 MiB."),{code:"JXL_SIZE"});const o=new Uint8Array(Math.min(n,Math.max(2*this.bytes.length,this.at+t+16)));o.set(this.bytes.subarray(0,this.at)),this.bytes=o}append(t){if(this.at+t.at+8>=this.bytes.length&&this.grow(t.at+8),this.pending)for(let n=0;n<t.at;n++)this.write(8,t.bytes[n]);else this.bytes.set(t.bytes.subarray(0,t.at),this.at),this.at+=t.at;t.pending&&this.write(t.pending,t.Mt)}finish(){const t=this.at+(this.pending?1:0),n=this.bytes.slice(0,t);return this.pending&&(n[this.at]=this.Mt),n}}const ot=(t,n)=>"number"==typeof t?n(t):{...t,at:o=>n(t.at(o))},rt=(t,n)=>Object.assign(new Error(n),{code:t}),et={channel:null,weighted:null,fill:null},st=256,it=[1,2,3,4,0,5,17,6,16,7,8,9,10,11,12,13,14,15],ct=[2,4,3,2,2,4],ft=[0,7,3,2,1,15],lt=[0,0,0],at=3,ut=5,ht=A(4),yt=Object.freeze({X:224,minLength:7,N:ht}),wt=A(0),dt=[A(0),A(4),A(4,1,1),A(3,0,1)],pt=(t,n,o,r)=>o?r?t[n]+256*t[n+3]:t[n]:t[n]+256*t[n+1]+65536*t[n+2]+(r?16777216*t[n+3]:0),Mt=Float64Array.of(.3535533905932738,.3535533905932738,.3535533905932738,.3535533905932738,.3535533905932738,.3535533905932738,.3535533905932738,.3535533905932738,.4903926402016152,.4157348061512726,.27778511650980114,.09754516100806417,-.0975451610080641,-.277785116509801,-.4157348061512727,-.4903926402016152,.46193976625564337,.19134171618254492,-.19134171618254486,-.46193976625564337,-.4619397662556434,-.19134171618254517,.191341716182545,.46193976625564326,.4157348061512726,-.0975451610080641,-.4903926402016152,-.2777851165098011,.2777851165098009,.4903926402016152,.09754516100806439,-.41573480615127256,.3535533905932738,-.35355339059327373,-.35355339059327384,.3535533905932737,.35355339059327384,-.35355339059327334,-.35355339059327356,.3535533905932733,.27778511650980114,-.4903926402016152,.09754516100806415,.4157348061512728,-.41573480615127256,-.09754516100806401,.4903926402016153,-.27778511650980076,.19134171618254492,-.4619397662556434,.46193976625564326,-.19134171618254495,-.19134171618254528,.46193976625564337,-.4619397662556432,.19134171618254478,.09754516100806417,-.2777851165098011,.4157348061512728,-.4903926402016153,.4903926402016152,-.4157348061512725,.27778511650980076,-.09754516100806429),At=Float64Array.from({length:64},(t,n)=>{const o=.26236426446749106*Math.max(0,5*Math.sqrt((7&n)*(7&n)+(n>>3)*(n>>3))/Math.sqrt(98)-1);let r=1,e=1;for(let t=1;t<30;t++)e+=r=r*o/t;return e}),gt=[4,4,4,4,3,2,6,8],bt=[0,.125,.25,.5,.25,.25,.25,.25],mt=[.9299455010825141,.945349926692846,.9500648966626563],Ut=10.5*$.pixels,It=(t,n)=>Math.abs(t)<2?t*mt[n]:t-.145/t,xt=(t,n,o)=>{const r=t+1.402*o,e=t-.344136286*n-.714136286*o,s=t+1.772*n;return r*r+e*e+s*s},St=[A(0),A(1),A(2),A(3),A(4),A(4,1,1),A(5,1,1),A(2,0,1),A(3,0,1)],Et=A(4,1,1);class Lt{constructor(t){this.st=t,this.small=new Uint32Array(64*t),this.At=Array.from({length:t},()=>null),this.it=new Float64Array(t)}add(t,n){if(this.it[t]++,n<64)return void this.small[64*t+n]++;const o=this.At[t]||(this.At[t]=new Map);o.set(n,(o.get(n)||0)+1)}ct(t,n,o=64){const r=new Uint32Array(o);for(const o of t){const t=64*o;for(let o=0;o<64;o++)this.small[t+o]&&(r[k(n,o)]+=this.small[t+o]);const e=this.At[o];if(e)for(const[t,o]of e)r[k(n,t)]+=o}return r}ft(t,n){let r=B(this.ct(t,n));for(const e of t){const t=64*e;for(let e=n.S;e<64;e++)this.small[t+e]&&(r+=this.small[t+e]*(o(e)-n.U-n.I));const s=this.At[e];if(s)for(const[t,e]of s)r+=e*(o(t)-n.U-n.I)}return r}}const Jt=[0,0,0],Tt=[1];for(let t=1;t<32;t++)Tt.push(Tt[t-1]/2);const Xt=[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],Nt=13,_t=[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],vt=[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],Ct=Xt.map(t=>(7&t)<<3|t>>3),Ot=(t,n,o)=>2*(vt[t]+_t[n])+o;export{$ as LIMITS,K as encodePhoto,V as encodePhotoSteps};
|
package/photo.mjs
CHANGED
|
@@ -1,105 +1,11 @@
|
|
|
1
1
|
// SPDX-License-Identifier: MIT
|
|
2
|
-
//
|
|
3
|
-
// then
|
|
4
|
-
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
2
|
+
// Photographic pixels through the carrier's VarDCT writer: sRGB to YCbCr, DCT8,
|
|
3
|
+
// quantisation, then entropy coding. Quality 90 by default; 100 is exact, including
|
|
4
|
+
// RGB below transparent alpha. Alpha is always exact, and the colour space is declared.
|
|
5
|
+
import {PHOTO_LIMITS, complete} from './bits.mjs';
|
|
6
|
+
import {photoJob} from './photo-job.mjs';
|
|
7
|
+
import {coefficientEffortSteps} from './coefficient-effort.mjs';
|
|
8
8
|
|
|
9
|
-
// This door's limits (bits.mjs): 40 million pixels, its memory 6.5 bytes a pixel besides the picture at that size.
|
|
10
9
|
export {PHOTO_LIMITS as LIMITS};
|
|
11
|
-
|
|
12
|
-
// Neither table comes from Math.cos, Math.hypot or `**`, which each engine rounds its own way; the same pixels must
|
|
13
|
-
// give the same bytes everywhere. The DCT8 basis, (u ? 1 : √½) · cos((2x + 1) · u · π / 16) / 2 at u · 8 + x, is
|
|
14
|
-
// written out as V8 computed it (the cosines of the larger angles carry their rounding, and a coefficient on a
|
|
15
|
-
// rounding edge would move without it). The quantisation shape, 1.3 ** max(0, hypot(u, v) · 5 / √98 − 1) at
|
|
16
|
-
// v · 8 + u, is the exponential series of its logarithm: libjxl 0.7's DCT8 luma distance bands
|
|
17
|
-
// (lib/jxl/quant_weights.cc), the reciprocal weight growing with radial frequency; its integer steps are V8's at every
|
|
18
|
-
// quality by 0.001. Both chroma planes keep full resolution; their scale accounts for YCbCr's larger RGB error.
|
|
19
|
-
const COSINES = Float64Array.of(0.3535533905932738, 0.3535533905932738, 0.3535533905932738, 0.3535533905932738,
|
|
20
|
-
0.3535533905932738, 0.3535533905932738, 0.3535533905932738, 0.3535533905932738, 0.4903926402016152,
|
|
21
|
-
0.4157348061512726, 0.27778511650980114, 0.09754516100806417, -0.0975451610080641, -0.277785116509801,
|
|
22
|
-
-0.4157348061512727, -0.4903926402016152, 0.46193976625564337, 0.19134171618254492, -0.19134171618254486,
|
|
23
|
-
-0.46193976625564337, -0.4619397662556434, -0.19134171618254517, 0.191341716182545, 0.46193976625564326,
|
|
24
|
-
0.4157348061512726, -0.0975451610080641, -0.4903926402016152, -0.2777851165098011, 0.2777851165098009,
|
|
25
|
-
0.4903926402016152, 0.09754516100806439, -0.41573480615127256, 0.3535533905932738, -0.35355339059327373,
|
|
26
|
-
-0.35355339059327384, 0.3535533905932737, 0.35355339059327384, -0.35355339059327334, -0.35355339059327356,
|
|
27
|
-
0.3535533905932733, 0.27778511650980114, -0.4903926402016152, 0.09754516100806415, 0.4157348061512728,
|
|
28
|
-
-0.41573480615127256, -0.09754516100806401, 0.4903926402016153, -0.27778511650980076, 0.19134171618254492,
|
|
29
|
-
-0.4619397662556434, 0.46193976625564326, -0.19134171618254495, -0.19134171618254528, 0.46193976625564337,
|
|
30
|
-
-0.4619397662556432, 0.19134171618254478, 0.09754516100806417, -0.2777851165098011, 0.4157348061512728,
|
|
31
|
-
-0.4903926402016153, 0.4903926402016152, -0.4157348061512725, 0.27778511650980076, -0.09754516100806429);
|
|
32
|
-
const QUANT_SHAPE = Float64Array.from({length: 64}, (_, k) => {
|
|
33
|
-
const y = Math.max(0, Math.sqrt((k & 7) * (k & 7) + (k >> 3) * (k >> 3)) * 5 / Math.sqrt(98) - 1) * 0.26236426446749106;
|
|
34
|
-
let term = 1, sum = 1;
|
|
35
|
-
for (let n = 1; n < 30; n++) sum += term = term * y / n;
|
|
36
|
-
return sum;
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
// Eight terms in the original accumulation order, including the initial zero. The two separable passes
|
|
40
|
-
// share this fixed dot product without a loop or a changed floating-point sum.
|
|
41
|
-
function dot8(data, at, step, basis) {
|
|
42
|
-
let sum = 0;
|
|
43
|
-
sum += data[at] * COSINES[basis];
|
|
44
|
-
sum += data[at + step] * COSINES[basis + 1];
|
|
45
|
-
sum += data[at + 2 * step] * COSINES[basis + 2];
|
|
46
|
-
sum += data[at + 3 * step] * COSINES[basis + 3];
|
|
47
|
-
sum += data[at + 4 * step] * COSINES[basis + 4];
|
|
48
|
-
sum += data[at + 5 * step] * COSINES[basis + 5];
|
|
49
|
-
sum += data[at + 6 * step] * COSINES[basis + 6];
|
|
50
|
-
sum += data[at + 7 * step] * COSINES[basis + 7];
|
|
51
|
-
return sum;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// A row of groups (32 block rows) per step.
|
|
55
|
-
function* coefficientSteps(data, width, height, quality, colorSpace) {
|
|
56
|
-
const stride = Math.ceil(width / 8), rows = Math.ceil(height / 8), quantScale = 16;
|
|
57
|
-
const distance = quality >= 30 ? 0.1 + (100 - quality) * 0.09 : 53 / 3000 * quality * quality - 23 / 20 * quality + 25;
|
|
58
|
-
const components = [0, 1, 2].map(c => ({h: 1, v: 1, stride, rows,
|
|
59
|
-
quant: Int32Array.from(QUANT_SHAPE, (shape, k) => Math.max(1, Math.round(quantScale * distance * (c ? 8 : 14) * (k ? shape : 0.5)))),
|
|
60
|
-
coeffs: new Int16Array(stride * rows * 64)}));
|
|
61
|
-
const block = new Float64Array(192), intermediate = new Float64Array(64);
|
|
62
|
-
const band = (from, to) => {
|
|
63
|
-
for (let by = from; by < to; by++) for (let bx = 0; bx < stride; bx++) {
|
|
64
|
-
// Edge replication gives a complete transform without inventing a dark border in a partial block.
|
|
65
|
-
for (let y = 0; y < 8; y++) for (let x = 0; x < 8; x++) {
|
|
66
|
-
const at = (Math.min(height - 1, by * 8 + y) * width + Math.min(width - 1, bx * 8 + x)) * 4;
|
|
67
|
-
const r = data[at], g = data[at + 1], b = data[at + 2], k = y * 8 + x;
|
|
68
|
-
block[k] = 0.299 * r + 0.587 * g + 0.114 * b - 128;
|
|
69
|
-
block[64 + k] = -0.168735892 * r - 0.331264108 * g + 0.5 * b;
|
|
70
|
-
block[128 + k] = 0.5 * r - 0.418687589 * g - 0.081312411 * b;
|
|
71
|
-
}
|
|
72
|
-
for (let c = 0; c < 3; c++) {
|
|
73
|
-
for (let y = 0; y < 8; y++) for (let u = 0; u < 8; u++) {
|
|
74
|
-
intermediate[y * 8 + u] = dot8(block, c * 64 + y * 8, 1, u * 8);
|
|
75
|
-
}
|
|
76
|
-
const {coeffs, quant} = components[c], offset = (by * stride + bx) * 64;
|
|
77
|
-
for (let v = 0; v < 8; v++) for (let u = 0; u < 8; u++) {
|
|
78
|
-
const sum = dot8(intermediate, u, 8, v * 8), k = v * 8 + u;
|
|
79
|
-
coeffs[offset + k] = Math.round(sum * quantScale / quant[k]);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
};
|
|
84
|
-
for (let by = 0; by < rows; by += 32) { band(by, Math.min(rows, by + 32)); yield Math.min(rows, by + 32) / rows; }
|
|
85
|
-
let alpha = null;
|
|
86
|
-
for (let i = 3; i < data.length; i += 4) if (data[i] !== 255) { alpha = data; break; }
|
|
87
|
-
return {width, height, components, quantScale, ycbcr: true, orientation: 1, alpha, colorSpace};
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// Quality 90 by default, 1 to 99 photographic, on libjxl's quality-to-distance curve; 100 is the core's exact stream,
|
|
91
|
-
// including RGB beneath transparent pixels. Alpha is always exact; the colour space is declared as the core declares
|
|
92
|
-
// it. The options are read before any work.
|
|
93
10
|
export function encodePhoto(data, width, height, options) { return complete(encodePhotoSteps(data, width, height, options)); }
|
|
94
|
-
|
|
95
|
-
// The same work as a job (admit.mjs): the coefficients a row of groups per step, then the VarDCT writer's steps.
|
|
96
|
-
export function encodePhotoSteps(data, width, height, options) {
|
|
97
|
-
const {quality, colorSpace} = admitOptions(options, 90);
|
|
98
|
-
admitPixels(data, width, height, PHOTO_LIMITS);
|
|
99
|
-
return job(photoSteps(data, width, height, quality, colorSpace));
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
function* photoSteps(data, width, height, quality, colorSpace) {
|
|
103
|
-
if (quality >= 100) return yield* losslessSteps(data, width, height, {colorSpace});
|
|
104
|
-
return yield* part(varDCTSteps(yield* part(coefficientSteps(data, width, height, quality, colorSpace), 0, 2)), 1, 2);
|
|
105
|
-
}
|
|
11
|
+
export function encodePhotoSteps(data, width, height, options) { return photoJob(data, width, height, options, coefficientEffortSteps); }
|
package/pool.mjs
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
// Every core: the effort door's exact work over a pool of workers, the same bytes as one thread for any number of them.
|
|
3
|
+
// A frame's groups are coded independently once its global choices are made, and every pass of the door over its
|
|
4
|
+
// groups (the plan's counting and writing, the weighted search's, the local trees') is a function of the pass's setup
|
|
5
|
+
// and the group's pixels alone (frame.mjs, groupPass). So each worker is given its groups' pixels once, by transfer, and
|
|
6
|
+
// keeps them for every pass of the picture; a pass sends its setup and then group numbers; this thread, which has every
|
|
7
|
+
// pixel, codes groups too while it waits. The counts come back per group and are added in group order, the sections
|
|
8
|
+
// are placed in group order, and every choice between passes (predictors, codes, trees, which plan, which stream) stays
|
|
9
|
+
// in this thread. Only the time changes.
|
|
10
|
+
//
|
|
11
|
+
// The workers are the caller's: `spawn()` returns a Worker (postMessage, onmessage, onerror, terminate) whose script
|
|
12
|
+
// hands every message to `servePool` and posts back what it answers, with its transfer list. The pool ends its workers
|
|
13
|
+
// when the job ends or is left. A worker that cannot start or fails leaves its groups to this thread, which runs the
|
|
14
|
+
// same functions on the same pixels: the bytes stand. A picture of one group, or a lossy one, never starts a worker.
|
|
15
|
+
import {groupLayout, groupRect} from './frame.mjs';
|
|
16
|
+
import {planGroup} from './lossless.mjs';
|
|
17
|
+
import {colourTransform} from './rct-search.mjs';
|
|
18
|
+
import {localGroup} from './local.mjs';
|
|
19
|
+
import {searchGroup, effortJob} from './effort-job.mjs';
|
|
20
|
+
import {guard, answer} from './admit.mjs';
|
|
21
|
+
|
|
22
|
+
const WORK = {plan: setup => planGroup(setup, setup.rct === undefined ? undefined : colourTransform(setup.rct, setup.channels)), search: searchGroup, local: localGroup};
|
|
23
|
+
// A group's result: its counts (fresh, the pass's `sizes`), or its section.
|
|
24
|
+
const work = (group, setup, rgba, stride, rect) => { const counts = setup.sizes?.map(n => new Uint32Array(n)); return group(rgba, stride, ...rect, counts) || counts; };
|
|
25
|
+
|
|
26
|
+
// A worker's side. Its tiles: {rgba, w, h} by group; the pass it is working.
|
|
27
|
+
let tiles = [], current = null;
|
|
28
|
+
export function servePool(message) {
|
|
29
|
+
if (message.pool === 'tile') { tiles[message.g] = message; return null; }
|
|
30
|
+
if (message.pool === 'pass') { current = {...message, group: WORK[message.kind](message.setup)}; return null; }
|
|
31
|
+
const {g} = message, {rgba, w, h} = tiles[g];
|
|
32
|
+
try {
|
|
33
|
+
const result = work(current.group, current.setup, rgba, w, [0, 0, w, h]);
|
|
34
|
+
return [{pool: 'done', id: current.id, g, result}, (Array.isArray(result) ? result : [result]).map(a => a.buffer)];
|
|
35
|
+
} catch (error) { return [{pool: 'done', id: current.id, g, error: {name: error.name, code: error.code, message: error.message}}, []]; }
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// The effort door's encodeSteps (the same options, admitted at once) as an async job: `for await (const done of job)`,
|
|
39
|
+
// `job.hurry` and `job.bytes` as the steps' job; leaving the loop ends the workers. `workers` is how many to start.
|
|
40
|
+
export function encodePool(data, width, height, options, {spawn, workers = 4} = {}) {
|
|
41
|
+
const steps = effortJob(data, width, height, options, true), layout = groupLayout(width, height), groups = layout.groupsX * layout.groupsY;
|
|
42
|
+
let members = null, failed = false, live = 0, serial = 0, ready = [], wake = () => {}, last = 0;
|
|
43
|
+
const end = () => { if (members) for (const worker of members) worker.terminate(); members = []; };
|
|
44
|
+
const fail = () => { failed = true; end(); wake(); };
|
|
45
|
+
// Every message to a worker goes through here: a send that throws (a pixel buffer that cannot be transferred, a
|
|
46
|
+
// worker that is gone) is a failure of the pool like any other, and the groups come back to this thread.
|
|
47
|
+
const send = (worker, message, transfer = []) => {
|
|
48
|
+
if (failed) return false;
|
|
49
|
+
try { worker.postMessage(message, transfer); return true; } catch { fail(); return false; }
|
|
50
|
+
};
|
|
51
|
+
// The workers, and each one's groups' pixels.
|
|
52
|
+
const start = () => {
|
|
53
|
+
members = [];
|
|
54
|
+
try {
|
|
55
|
+
for (let k = 0; k < Math.min(workers, groups); k++) {
|
|
56
|
+
const worker = spawn();
|
|
57
|
+
members.push(worker);
|
|
58
|
+
worker.onmessage = ({data: message}) => { worker.up = true; if (message.id === live) { worker.flight--; ready.push(message); worker.feed(); wake(); } };
|
|
59
|
+
worker.onerror = worker.onmessageerror = event => { event?.preventDefault?.(); if (members.includes(worker)) fail(); };
|
|
60
|
+
}
|
|
61
|
+
} catch { fail(); return; }
|
|
62
|
+
for (let g = 0; g < groups; g++) {
|
|
63
|
+
const [x0, y0, w, h] = groupRect(layout, width, height, g), rgba = new Uint8Array(w * h * 4);
|
|
64
|
+
for (let y = 0; y < h; y++) rgba.set(data.subarray(((y0 + y) * width + x0) * 4, ((y0 + y) * width + x0 + w) * 4), y * w * 4);
|
|
65
|
+
if (!send(members[g % members.length], {pool: 'tile', g, w, h, rgba}, [rgba.buffer])) return;
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
// A turn of the event loop, so that the workers' answers come in between this thread's own groups: a channel's
|
|
69
|
+
// message in a browser, which queues behind them, and setImmediate where there is one (Node drains a port's messages
|
|
70
|
+
// in one go, so a channel there would starve the workers' ports).
|
|
71
|
+
let channel;
|
|
72
|
+
const turn = () => new Promise(resolve => {
|
|
73
|
+
if (typeof setImmediate === 'function') { setImmediate(resolve); return; }
|
|
74
|
+
channel ||= new MessageChannel(); channel.port1.onmessage = resolve; channel.port2.postMessage(0);
|
|
75
|
+
});
|
|
76
|
+
// One pass: every group's result, or null when a hurry ended it, or the error a group's work threw. A worker is sent
|
|
77
|
+
// a group at a time until it first answers (it may still be starting), then kept two ahead. This thread is not idle:
|
|
78
|
+
// it takes the last unsent group of the longest queue, else a group sent to a worker that has not answered yet, and
|
|
79
|
+
// only waits when every group left is with a working worker. A group answered twice counts once: it is the same.
|
|
80
|
+
async function* pass({kind, setup, at, stop}) {
|
|
81
|
+
if (!members) start();
|
|
82
|
+
const id = live = ++serial, waiting = new Set(Array.from({length: groups}, (_, g) => g)), results = [];
|
|
83
|
+
const queues = members.map((_, k) => [...waiting].filter(g => g % members.length === k));
|
|
84
|
+
ready = [];
|
|
85
|
+
for (const [k, worker] of members.entries()) {
|
|
86
|
+
worker.flight = 0;
|
|
87
|
+
worker.feed = () => { while (worker.flight < (worker.up ? 2 : 1) && queues[k].length && send(worker, {pool: 'task', g: queues[k][0]})) { worker.flight++; queues[k].shift(); } };
|
|
88
|
+
if (!send(worker, {pool: 'pass', id, kind, setup})) break;
|
|
89
|
+
worker.feed();
|
|
90
|
+
if (failed) break;
|
|
91
|
+
}
|
|
92
|
+
const steal = () => {
|
|
93
|
+
if (failed) return waiting.values().next().value;
|
|
94
|
+
let k = -1;
|
|
95
|
+
queues.forEach((queue, i) => { if (queue.length && (k < 0 || queue.length > queues[k].length)) k = i; });
|
|
96
|
+
if (k >= 0) return queues[k].pop();
|
|
97
|
+
for (const g of waiting) if (!members[g % members.length].up) return g;
|
|
98
|
+
};
|
|
99
|
+
let local, hurried = false;
|
|
100
|
+
while (waiting.size) {
|
|
101
|
+
let done = ready.shift();
|
|
102
|
+
if (!done) {
|
|
103
|
+
const g = steal();
|
|
104
|
+
if (g === undefined) { await new Promise(resolve => { wake = resolve; }); continue; }
|
|
105
|
+
local ||= WORK[kind](setup);
|
|
106
|
+
try { done = {g, result: work(local, setup, data, width, groupRect(layout, width, height, g))}; } catch (error) { live = 0; return error; }
|
|
107
|
+
await turn();
|
|
108
|
+
}
|
|
109
|
+
if (!waiting.has(done.g)) continue;
|
|
110
|
+
if (done.error) { live = 0; return Object.assign(new (done.error.name === 'RangeError' ? RangeError : Error)(done.error.message), done.error.code ? {code: done.error.code} : {}); }
|
|
111
|
+
waiting.delete(done.g); results[done.g] = done.result;
|
|
112
|
+
const g = groups - waiting.size - 1;
|
|
113
|
+
yield last = at(g);
|
|
114
|
+
hurried = it.hurry;
|
|
115
|
+
if (hurried && stop(g)) { live = 0; return null; }
|
|
116
|
+
}
|
|
117
|
+
live = 0;
|
|
118
|
+
return {results, hurried};
|
|
119
|
+
}
|
|
120
|
+
const it = (async function* () {
|
|
121
|
+
let step, reply;
|
|
122
|
+
try {
|
|
123
|
+
// The workers start, and take their pixels, while this thread inspects the picture.
|
|
124
|
+
if (!layout.single && (options?.quality ?? 100) >= 100) start();
|
|
125
|
+
while (!(step = guard(() => reply instanceof Error ? steps.throw(reply) : steps.next(reply))).done) {
|
|
126
|
+
if (typeof step.value === 'number') { yield last = step.value; reply = it.hurry; }
|
|
127
|
+
else reply = yield* pass(step.value);
|
|
128
|
+
}
|
|
129
|
+
} finally { live = -1; end(); channel?.port1.close(); }
|
|
130
|
+
// A candidate given up (the stream limit, or memory for a search) ends early.
|
|
131
|
+
if (last < 1) yield 1;
|
|
132
|
+
return it.bytes = answer(step.value);
|
|
133
|
+
})();
|
|
134
|
+
it.bytes = null; it.hurry = false;
|
|
135
|
+
return it;
|
|
136
|
+
}
|