rapier-jxl 2.1.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/jpeg-ans.min.mjs CHANGED
@@ -1,2 +1,2 @@
1
- /*! Rapier JXL 2.1.0 | MIT License Copyright (c) 2026 rapier.website Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
2
- const t=Object.freeze({bytes:16777216,pixels:24e6,edge:16384}),o=Object.freeze({bytes:16777216,pixels:64e6,edge:16384}),n=[1];for(let t=1;t<=32;t++)n.push(2*n[t-1]);class r{constructor(t=4096){this.bytes=new Uint8Array(t),this.at=0,this.t=0,this.pending=0}write(t,o){if(!(o>=0&&o<n[t]))throw new Error("bit write out of range: "+t+" bits, "+o);let r=this.t+o*(1<<this.pending),e=this.pending+t;this.at+5>=this.bytes.length&&this.grow();const i=this.bytes;for(;e>=8;)i[this.at++]=255&r,r=Math.floor(r/256),e-=8;this.t=r,this.pending=e}o(t,o){for(let r=0;r<4;r++){const[e,i]=t[r];if(o>=i&&o-i<n[e])return this.write(2,r),void(e&&this.write(e,o-i))}throw new Error("U32 value out of range: "+o)}i(){this.pending&&(this.at+1>=this.bytes.length&&this.grow(),this.bytes[this.at++]=this.t,this.t=0,this.pending=0)}get u(){return 8*this.at+this.pending}grow(o=0){const n=t.bytes+64;if(this.at+o+16>n)throw Object.assign(new Error("The encoded picture exceeds 16 MiB."),{code:"JXL_SIZE"});const r=new Uint8Array(Math.min(n,Math.max(2*this.bytes.length,this.at+o+16)));r.set(this.bytes.subarray(0,this.at)),this.bytes=r}append(t){if(this.at+t.at+8>=this.bytes.length&&this.grow(t.at+8),this.pending)for(let o=0;o<t.at;o++)this.write(8,t.bytes[o]);else this.bytes.set(t.bytes.subarray(0,t.at),this.at),this.at+=t.at;t.pending&&this.write(t.pending,t.t)}finish(){const t=this.at+(this.pending?1:0),o=this.bytes.slice(0,t);return this.pending&&(o[this.at]=this.t),o}}function e(t){return t>=0?2*t:2*-t-1}function i(t){return 31-Math.clz32(t)}function s(t){return t<=1?0:32-Math.clz32(t-1)}function f(t){if(0===t)return 0;const o=t<0?32768:0;if(!((t=Math.abs(t))<65520))throw new Error("half float out of range: "+t);let n=0,r=t;for(;r>=2;)r/=2,n++;for(;r<1;)r*=2,n--;n<-14?(r=16384*t,n=-15):r-=1;let e=Math.round(1024*r);return 1024===e&&(e=0,n++),o|n+15<<10|e}const c=[0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63];function a(t,o="JXL_JPEG"){return Object.assign(new Error("JPEG: "+t),{code:o})}function l(t,o){const n=new Uint16Array(65536);let r=0,e=0;for(let i=1;i<=16;i++){for(let s=0;s<t[i-1];s++){const t=o[e++],s=r<<16-i,f=1<<16-i;if(s+f>=65536)throw a("bad Huffman table");n.fill(i<<8|t,s,s+f),r++}r<<=1}return n}const h=[.436,.2225,.0139,.3851,.7169,.0971,.143,.0606,.7139],u=[.5151,.2412,-.001,.292,.6922,.0419,.1571,.0666,.7841],w=[0,.00516,.01435,.02934,.05088,.07958,.11602,.16068,.21404,.27652,.34851,.43039,.52252,.62523,.73884,.86367,1],d=[2.4,1/1.055,.055/1.055,1/12.92,.04045];function y(t){if(t<=.04045)return t/12.92;const o=(t+.055)/1.055;let n=1;for(let t=0;t<24;t++)n=(4*n+o/(n*n*n*n))/5;return o*o*n*n}function p(t,o,n){const r=77===t[o]&&77===t[o+1];if(!r&&(73!==t[o]||73!==t[o+1]))return 1;const e=o=>o+2>n?0:r?t[o]<<8|t[o+1]:t[o]|t[o+1]<<8;if(42!==e(o+2))return 1;const i=o+(o=>o+4>n?0:r?(t[o]<<24|t[o+1]<<16|t[o+2]<<8|t[o+3])>>>0:(t[o]|t[o+1]<<8|t[o+2]<<16|t[o+3]<<24)>>>0)(o+4),s=e(i);for(let t=0,o=i+2;t<s&&o+12<=n;t++,o+=12){if(274!==e(o))continue;const t=e(o+8);return t>=1&&t<=8?t:1}return 1}function b(t,o,n,r,e,i,s,f,l){!function(t,o,n,r,e,i){if(!t.progressive){if(0!==n||63!==r||0!==e||0!==i)throw a("a bad spectral selection");for(const t of o){if(t.component.coverage.some(t=>-1!==t))throw a("a component coded twice");t.component.coverage.fill(0)}return}if(i>13||e&&e!==i+1)throw a("a bad successive approximation");if(0===n){if(0!==r)throw a("a bad spectral selection");for(const{component:t}of o){if(t.coverage[0]!==(e||-1))throw a(e?"a refinement scan out of order":"a DC scan twice");t.coverage[0]=i}return}if(1!==o.length)throw a("an interleaved AC scan");if(r>63||n>r)throw a("a bad spectral selection");const{coverage:s}=o[0].component;if(-1===s[0])throw a("an AC scan before the DC scan");for(let t=n;t<=r;t++){if(s[t]!==(e||-1))throw a(e?"a refinement scan out of order":"a band coded twice");s[t]=i}}(n,r,e,i,s,f);let h=o,u=0,w=0,d=0,y=!1;const p=()=>{for(;w<=24;){let o=0;if(y||h>=t.length)y=!0,d+=8;else if(o=t[h],255===o){const n=t[h+1];if(0===n)h+=2;else{if(255===n){h++;continue}y=!0,o=0,d+=8}}else h++;u=(u<<8|o)>>>0,w+=8}},b=()=>a("a scan cut short"),m=t=>{if(!t)return 0;if(p(),t>w-d)throw b();const o=u>>>w-t&(1<<t)-1;return w-=t,o},M=(t,o)=>t<1<<o-1?t-(1<<o)+1:t,g=t=>{if(!t)throw a("a scan without its Huffman table");p();const o=t[u>>>w-16&65535];if(!o)throw a("bad Huffman code");if(o>>8>w-d)throw b();return w-=o>>8,255&o},A=()=>{const o=w-d;if(o>=8)throw a("bytes left over in a scan");if(v)throw a("an end-of-band run past its interval");if(o&&(u>>>d&(1<<o)-1)!=(1<<o)-1)throw a("bad scan padding");for(;h+1<t.length&&255===t[h]&&255===t[h+1];)h++},U=o=>{if(A(),255!==t[h]||t[h+1]!==208+(7&o))throw a("a restart marker missing or out of order");h+=2,u=0,w=0,d=0,y=!1;for(const t of r)t.component.pred=0;v=0},I=(t,o)=>{if(o>11)throw a("a DC value out of range");const n=o?M(m(o),o):0;if(t.component.pred+=n,t.component.pred>2047||t.component.pred<-2048)throw a("a DC value out of range");return t.component.pred},x=(t,o)=>{if(t>10)throw a("an AC value out of range");const n=M(m(t),t)*(1<<o);if(n>1023||n<-1023)throw a("an AC value out of range");return n};let v=0;const E=1===r.length,S=n.progressive,C=()=>a("coefficients past the "+(S?"band":"block")),J=(t,o,n)=>{if(v>0)v--;else for(let r=e||1;r<=i;){const e=g(t.ac),s=e>>4,l=15&e;if(l){if(r+=s,r>i)throw C();o[n+c[r]]=x(l,f),r++}else{if(s<15){if(s&&!S)throw a("an end-of-band run in a sequential scan");v=(1<<s)-1+(s?m(s):0);break}if(r+=16,r>i+1)throw C()}}},L=S?0===e?0===s?(t,o,n)=>{const r=I(t,g(t.dc))*(1<<f);if(r>2047||r<-2048)throw a("a DC value out of range");o[n]=r}:(t,o,n)=>{m(1)&&(o[n]|=1<<f)}:0===s?J:(t,o,n)=>{const r=1<<f,s=-1<<f;let l=e;if(v<=0)for(;l<=i;l++){const e=g(t.ac);let f=e>>4,h=0;const u=15&e;if(u){if(1!==u)throw a("a bad refinement code");h=m(1)?r:s}else if(15!==f){v=1<<f,f&&(v+=m(f));break}for(;l<=i;l++){const t=n+c[l];if(0!==o[t])m(1)&&0===(o[t]&r)&&(o[t]+=o[t]>=0?r:s);else if(--f<0)break}if(f>=0||h&&l>i)throw a("coefficients past the band");h&&(o[n+c[l]]=h)}if(v>0){for(;l<=i;l++){const t=n+c[l];0!==o[t]&&m(1)&&0===(o[t]&r)&&(o[t]+=o[t]>=0?r:s)}v--}}:(t,o,n)=>{o[n]=I(t,g(t.dc)),J(t,o,n)};for(const t of r)t.component.pred=0;const X=r[0].component,k=E?X.blocksW*X.blocksH:n.mcusX*n.mcusY;for(let t=0;t<k;t++)if(l&&t>0&&t%l===0&&U(t/l-1),E){const o=t/X.blocksW|0,n=t-o*X.blocksW;L(r[0],X.coeffs,64*(o*X.stride+n))}else{const o=t/n.mcusX|0,e=t-o*n.mcusX;for(const t of r){const n=t.component;for(let r=0;r<n.v;r++)for(let i=0;i<n.h;i++)L(t,n.coeffs,64*((o*n.v+r)*n.stride+e*n.h+i))}}if(A(),255===t[h]&&t[h+1]>=208&&t[h+1]<=215){if(!l||k%l||t[h+1]!==208+(k/l-1&7))throw a("a restart marker out of order");for(h+=2;h+1<t.length&&255===t[h]&&255===t[h+1];)h++}if(h<t.length&&!(255===t[h]&&h+1<t.length&&0!==t[h+1]&&255!==t[h+1]))throw a("bytes left over after a scan");return h}const m=(t,o)=>Object.assign(new Error(o),{code:t});function M(o){if(o>t.bytes)throw m("JXL_SIZE","The encoded picture exceeds 16 MiB.")}function g(t){try{return t()}catch(t){if(t instanceof RangeError&&!t.code)throw m("JXL_MEMORY","Not enough memory for this picture.");throw t}}function A(n,r,e){const i=function(t={}){if(null===t||"object"!=typeof t)throw m("JXL_INPUT","Options are an object.");const o=void 0===t.effort?1:t.effort;if(!Number.isInteger(o)||o<1||o>9)throw m("JXL_INPUT","Effort is a whole number from 1 to 9.");return o}(r);if(!(n instanceof Uint8Array&&n.length))throw m("JXL_INPUT","A JPEG is a non-empty Uint8Array.");if(n.length>o.bytes)throw m("JXL_SIZE","A JPEG is at most 16 MiB.");const s=function(t){const o=function*(){let n,r;for(;!(n=g(()=>t.next(o.hurry))).done;)yield r=n.value;return r<1&&(yield 1),o.bytes=function(t){return M(t.length),t}(n.value)}();return o.bytes=null,o.hurry=!1,o}(function*(n,r,e,i){const f=function(t){if(!(t.length>4)||255!==t[0]||216!==t[1])throw a("not a JPEG");const n=[],r=[[],[]],e=new Set,i={count:0,p:[]};let s=null,f=0,m=-1,M=!1,g=1,A=2;const U=o=>t[o]<<8|t[o+1],I=(o,n)=>{for(let r=0;r<n.length;r++)if(t[o+r]!==n.charCodeAt(r))return!1;return!0};for(;;){if(A>=t.length){if(s&&s.scans)break;throw a("truncated")}if(255!==t[A])throw a("marker expected");for(;255===t[A];)A++;const h=t[A++];if(217===h)break;if(216===h)throw a("a second start");if(h>=208&&h<=215)throw a("a restart marker outside a scan");if(1===h)continue;if(A+2>t.length)throw a("truncated");const u=U(A),w=A+2,d=A+u;if(u<2||d>t.length)throw a("bad segment");if(A=d,219===h)for(let o=w;o<d;){const r=t[o]>>4,e=15&t[o];if(r>1||e>3||o+1+(r?128:64)>d)throw a("bad quantisation table");o++;const i=new Int32Array(64);for(let n=0;n<64;n++)if(i[c[n]]=r?U(o):t[o],o+=r?2:1,!i[c[n]])throw a("a zero quantisation step");if(s?.progressive&&s.components.some(t=>t.tq===e&&t.quant&&t.coverage.some(t=>0!==t)&&t.quant.some((t,o)=>t!==i[o])))throw a("a quantisation table changed between progressive scans");n[e]=i}else if(196===h)for(let o=w;o<d;){const n=t[o]>>4,i=15&t[o];if(n>1||i>3||o+17>d)throw a("bad Huffman table");o++;const s=t.subarray(o,o+16);let f=0;for(const t of s)f+=t;if(f>256||o+16+f>d)throw a("bad Huffman table");const c=t.subarray(o+16,o+16+f);r[n][i]=l(s,c),0===n&&(c.some(t=>t>15)?e.add(i):e.delete(i)),o+=16+f}else if(192===h||193===h||194===h){if(s)throw a("two frames");if(d-w<6)throw a("bad frame header");if(8!==t[w])throw a("only 8-bit samples");const n=U(w+1),r=U(w+3),e=t[w+5];if(!r||!n)throw a("a height left to a DNL marker");if(1!==e&&3!==e)throw a("only grey or three components");if(d-w<6+3*e)throw a("bad frame header");if(r>o.edge||n>o.edge)throw a("a picture past "+o.edge+" pixels on a side","JXL_DIMENSIONS");if(r*n>o.pixels)throw a("a picture past "+o.pixels+" pixels","JXL_DIMENSIONS");const i=[];let f=0;for(let o=0,n=w+6;o<e;o++,n+=3){const o=t[n+1]>>4,r=15&t[n+1],e=t[n+2];if(o<1||o>4||r<1||r>4)throw a("bad sampling");if(e>3)throw a("bad quantisation table");if(i.some(o=>o.id===t[n]))throw a("two components with one id");f+=o*r,i.push({id:t[n],h:o,v:r,tq:e,pred:0,coverage:new Int8Array(64).fill(-1)})}if(f>10)throw a("bad sampling");const c=Math.max(...i.map(t=>t.h)),l=Math.max(...i.map(t=>t.v)),u=Math.ceil(r/(8*c)),y=Math.ceil(n/(8*l));for(const t of i)t.blocksW=Math.ceil(Math.ceil(r*t.h/c)/8),t.blocksH=Math.ceil(Math.ceil(n*t.v/l)/8),t.stride=u*t.h,t.rows=y*t.v,t.coeffs=new Int16Array(t.stride*t.rows*64);s={progressive:194===h,width:r,height:n,components:i,hmax:c,vmax:l,mcusX:u,mcusY:y,scans:0}}else{if(195===h||h>=197&&h<=199||h>=201&&h<=203||h>=205&&h<=207)throw a("lossless, hierarchical or arithmetic coding");if(221===h){if(u<4)throw a("bad segment");f=U(w)}else{if(220===h)throw a("a DNL marker");if(218===h){if(!s)throw a("a scan before the frame");const o=t[w],i=[];if(o<1||o>4||w+1+2*o+3>d)throw a("bad scan header");let c=w+1;for(let e=0;e<o;e++,c+=2){const o=s.components.find(o=>o.id===t[c]);if(!o)throw a("a scan of an unknown component");if(i.some(t=>t.component===o))throw a("a component twice in one scan");if(o.quant||(o.quant=n[o.tq]),!o.quant)throw a("a scan without its quantisation table");i.push({component:o,dc:r[0][t[c+1]>>4],ac:r[1][15&t[c+1]],dcSlot:t[c+1]>>4})}const l=t[c],h=t[c+1],u=t[c+2]>>4,y=15&t[c+2];if((!s.progressive||0===l&&0===u)&&i.some(t=>e.has(t.dcSlot)))throw a("bad Huffman table");A=b(t,d,s,i,l,h,u,y,f),s.scans++}else if(224===h)I(w,"JFIF")&&(M=!0);else if(225===h)1===g&&I(w,"Exif")&&(g=p(t,w+6,d));else if(226===h){if(u>=16&&I(w,"ICC_PROFILE\0")){const o=t[w+12],n=t[w+13];if(!o||!n||o>n||i.count&&i.count!==n||i.p[o])throw a("a colour profile cut short");i.count=n,i.p[o]=t.subarray(w+14,d)}}else 238===h&&u>=14&&I(w,"Adobe")&&(m=t[w+11])}}}if(!s||!s.scans)throw a("no picture");for(const t of s.components)for(let o=0;o<64;o++)if(0!==t.coverage[o])throw a(s.progressive?"a progressive JPEG whose scans do not finish every coefficient":"a component without its scan");let x="srgb";if(i.count){const t=[];for(let o=1;o<=i.count;o++){if(!i.p[o])throw a("a colour profile cut short");t.push(i.p[o])}const o=new Uint8Array(t.reduce((t,o)=>t+o.length,0));for(let n=0,r=0;n<t.length;r+=t[n].length,n++)o.set(t[n],r);if(x=function(t){const o=t.length,n=o=>t[o]<<8|t[o+1],r=n=>n+4<=o?(t[n]<<24|t[n+1]<<16|t[n+2]<<8|t[n+3])>>>0:0,e=n=>n+4<=o?String.fromCharCode(t[n],t[n+1],t[n+2],t[n+3]):"",i=t=>(0|r(t))/65536,s=r(128);if(o<132||132+12*s>o||"acsp"!==e(36)||"XYZ "!==e(20))return null;const f=t=>{for(let n=0;n<s;n++){const i=132+12*n,s=r(i+4);if(e(i)===t)return s+r(i+8)<=o?s:-1}return-1},c=t=>{const s=f(t);if(s<0)return!1;if("para"===e(s)){const t=n(s+8),o=[1,3,4,5,7][t];if(3!==t&&4!==t)return!1;for(let t=0;t<o;t++)if(Math.abs(i(s+12+4*t)-(t<5?d[t]:0))>(t?.002:.01))return!1;return!0}const c="curv"===e(s)?r(s+8):0;if(c<2||s+12+2*c>o)return!1;for(let t=0;t<c;t++)if(Math.abs(n(s+12+2*t)/65535-y(t/(c-1)))>.001)return!1;return w.every((t,o)=>{const r=o*(c-1)/16,e=Math.floor(r),i=Math.min(c-1,e+1),f=r-e;return Math.abs((n(s+12+2*e)*(1-f)+n(s+12+2*i)*f)/65535-t)<=.001})};if(["A2B0","A2B1","A2B2"].some(t=>f(t)>=0))return null;if("GRAY"===e(16))return c("kTRC")?"srgb":null;if("RGB "!==e(16)||!["rTRC","gTRC","bTRC"].every(c))return null;const a=["rXYZ","gXYZ","bXYZ"].flatMap(t=>{const o=f(t);return o>=0&&"XYZ "===e(o)?[i(o+8),i(o+12),i(o+16)]:[9,9,9]}),l=t=>t.every((t,o)=>Math.abs(a[o]-t)<=.004);return l(h)?"srgb":l(u)?"display-p3":null}(o),!x)throw a("a colour profile other than sRGB or Display P3 (decode the picture and encode its pixels)")}let v=!1;return 3!==s.components.length||M||(v=m>=0?0===m:82===s.components[0].id&&71===s.components[1].id&&66===s.components[2].id),{width:s.width,height:s.height,components:s.components,progressive:s.progressive,ycbcr:!v,orientation:g,colorSpace:x}}(n);!function(o,n,r=t){if(!Number.isInteger(o)||!Number.isInteger(n)||o<1||n<1)throw m("JXL_INPUT","A picture is at least one pixel wide and high, in whole pixels.");if(o>r.edge||n>r.edge)throw m("JXL_DIMENSIONS","A picture is at most "+r.edge+" pixels on a side.");if(o*n>r.pixels)throw m("JXL_DIMENSIONS","A picture is at most "+r.pixels.toLocaleString("en-US")+" pixels.")}(f.width,f.height,o);const M=f.orientation>=5;return(t=>{Object.assign(s,t)})({width:M?f.height:f.width,height:M?f.width:f.height,orientation:f.orientation}),yield*i(f,r)}(n,i,0,e));return s}const U=256;function I(t,o){t.o([[9,1],[13,1],[18,1],[30,1]],o)}const x=[1,2,3,4,0,5,17,6,16,7,8,9,10,11,12,13,14,15],v=[2,4,3,2,2,4],E=[0,7,3,2,1,15];function S(t,o){const n=new Uint8Array(t.length),r=[];for(let o=0;o<t.length;o++)t[o]>0&&r.push(o);if(!r.length)return n;if(1===r.length)return n[r[0]]=1,n;for(let e=1;;e*=2){const i=r.map(o=>({w:Math.max(t[o],e),m:o,l:null,r:null})).sort((t,o)=>t.w-o.w||t.m-o.m),s=[];let f=0,c=0;const a=()=>f<i.length&&(c>=s.length||i[f].w<=s[c].w)?i[f++]:s[c++];for(;i.length-f+(s.length-c)>1;){const t=a(),o=a();s.push({w:t.w+o.w,m:-1,l:t,r:o})}const l=[[a(),0]];let h=0;for(;l.length;){const[t,o]=l.pop();t.m>=0?(n[t.m]=o,o>h&&(h=o)):l.push([t.l,o+1],[t.r,o+1])}if(h<=o)return n}}function C(t,o,n,r,e,i){const s=[];for(;s.push(r&e),r>>=i;)r--;for(let r=s.length-1;r>=0;r--)t.push(n),o.push(s[r])}function J(t,o){let n=0;for(let r=0;r<o;r++)n=n<<1|1&t,t>>=1;return n}function L(t){const o=new Uint32Array(17),n=new Uint32Array(17),r=new Uint16Array(t.length);for(const n of t)o[n]++;o[0]=0;for(let t=1,r=0;t<=16;t++)r=r+o[t-1]<<1,n[t]=r;for(let o=0;o<t.length;o++)t[o]&&(r[o]=J(n[t[o]]++,t[o]));return r}function X(t){const o=[];for(let n=0;n<t.length;n++)t[n]>0&&o.push(n);const n=o.length,r=o[n-1]+1||1,e=n<=4?new Uint8Array(r):S(t,15).subarray(0,r);let i=0;if(n<=4){o.sort((o,n)=>t[n]-t[o]||o-n),i=4===n&&t[o[0]]>t[o[2]]+t[o[3]]?1:0;const r=n<2?[0]:2===n?[1,1]:3===n?[1,2,2]:i?[1,2,3,3]:[2,2,2,2];o.forEach((t,o)=>{e[t]=r[o]}),o.sort((t,o)=>e[t]-e[o]||t-o)}return{M:r,A:e,U:L(e),I:n&&n<=4?o:null,S:i}}function k(t,o){if(o.M<=1)return;if(o.I){const n=i(o.M-1)+1;t.write(2,1),t.write(2,o.I.length-1);for(const r of o.I)t.write(n,r);return void(4===o.I.length&&t.write(1,o.S))}const n=o.A,r=[],e=[];let s=n.length-1;for(;s>0&&!n[s];)s--;let f=8;for(let t=0;t<=s;){const o=n[t];let i=1;for(;t+i<=s&&n[t+i]===o;)i++;let c=i;if(o&&o!==f&&(r.push(o),e.push(0),c--,f=o),c<3)for(let t=0;t<c;t++)r.push(o),e.push(0);else C(r,e,o?16:17,c-3,o?3:7,o?2:3);t+=i}const c=new Uint32Array(18);for(const t of r)c[t]++;let a=0;for(const t of c)t&&a++;1===a&&(c[r[0]?0:1]=c[r[0]]);const l=S(c,5),h=L(l);let u=18;for(;u>0&&!l[x[u-1]];)u--;t.write(2,0);for(let o=0;o<u;o++){const n=l[x[o]];t.write(v[n],E[n])}for(let o=0;o<r.length;o++){const n=r[o];t.write(l[n],h[n]),16===n?t.write(2,e[o]):17===n&&t.write(3,e[o])}}function N(t,o=0,n=0){return{split:t,C:o,J:n,L:1<<t}}function O(t,o,n=15){t.write(s(n+1),o.split),o.split!==n&&(t.write(s(o.split+1),o.C),t.write(s(o.split-o.C+1),o.J))}function _(t,o,n){if(o<t.L)return n[0]=o,n[1]=0,void(n[2]=0);const r=i(o),e=o-(1<<r),s=r-t.C-t.J;n[0]=t.L+(r-t.split<<t.C+t.J|e>>r-t.C<<t.J|e&(1<<t.J)-1),n[1]=s,n[2]=o>>t.J&(1<<s)-1}function T(t,o,n,r=0){_(t,o,D),n[r+D[0]]++}const D=[0,0,0];function P(t,o,n,r,e=0){_(n,r,D);const i=e+D[0];t.write(o.A[i],o.U[i]),D[1]&&t.write(D[1],D[2])}function G(t,o){let n=0;for(const t of o)n=Math.max(n,t);const r=n?s(n+1):0;if(r<=3){if(t.write(1,1),t.write(2,r),r)for(const n of o)t.write(r,n);return}t.write(1,0),t.write(1,0);const e=N(8),i=N(4),f=o.length>=64,c=new Uint32Array(f?257:n+1),a=[];for(let t=0;t<o.length;){let n=1;for(;f&&t+n<o.length&&o[t+n]===o[t];)n++;if(n>=8)a.push([o[t],n-1]),c[o[t]]++,T(i,n-1-7,c,224),t+=n;else{for(let r=0;r<n;r++)a.push([o[t],0]),c[o[t]]++;t+=n}}const l=X(c);if(!f){R(t,{X:new Uint8Array(1),k:[{config:e,code:l}]});for(const[o]of a)P(t,l,e,o);return}const h=new Uint32Array(1);h[0]=1,R(t,{N:{O:224,minLength:7,_:i},X:new Uint8Array([1,0]),k:[{config:N(0),code:X(h)},{config:e,code:l}]});for(const[o,n]of a)P(t,l,e,o),n&&P(t,l,i,n-7,224)}function R(t,{N:o=null,X:n,k:r}){t.write(1,o?1:0),o&&(t.o([[0,224],[0,512],[0,4096],[15,8]],o.O),t.o([[0,3],[0,4],[2,5],[8,9]],o.minLength),O(t,o._,8)),n.length>1&&G(t,n),t.write(1,1);for(const o of r)O(t,o.config,15);for(const o of r){const n=o.code.M-1;if(n){const o=i(n);t.write(1,1),t.write(4,o),t.write(o,n-(1<<o))}else t.write(1,0)}for(const o of r)k(t,o.code)}const Z=N(4),j=Object.freeze({O:224,minLength:7,_:Z}),q=N(0);function B(t,o){const n=[[3,0],[6,8],[10,72],[13,1096]];if("rct"===o.type)t.write(2,0),t.o(n,o.T),t.o([[0,6],[2,0],[4,2],[6,10]],o.D??6);else if("palette"===o.type)t.write(2,1),t.o(n,o.T),t.o([[0,1],[0,3],[0,4],[13,1]],o.P),t.o([[8,0],[10,256],[12,1280],[16,5376]],o.G),t.o([[0,0],[8,1],[10,257],[16,1281]],o.R??0),t.write(4,o.Z??0);else{if("squeeze"!==o.type)throw new Error("unknown transform "+o.type);{t.write(2,2);const r=o.j??[];t.o([[0,0],[4,1],[6,9],[8,41]],r.length);for(const o of r)t.write(1,o.q?1:0),t.write(1,o.B?1:0),t.o(n,o.T),t.o([[0,1],[0,2],[0,3],[4,4]],o.P)}}}function z(t,o,n,r,i,s){const f=s.Z,c=s.offset,a=s.Y,l=q,h=Z;let u=0;const w=(n,r=l,e=0)=>{t?P(t,o,r,n,e):T(r,n,o,e)},d=()=>{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}},y=(t,o)=>{const r=n[t];let i;1===a?i=r-o-c:(i=Math.round((r-o-c)/a),n[t]=o+c+i*a),0!==i?(d(),w(e(i))):u++};for(let t=0,o=0;t<i;t++)for(let e=0;e<r;e++,o++){let i;if(0===f)i=0;else{const s=e?n[o-1]:t?n[o-r]:0,c=t?n[o-r]:s;if(1===f)i=s;else if(2===f)i=c;else if(3===f)i=(s+c)/2|0;else{const a=s+c-(e&&t?n[o-r-1]:s);if(5===f){const t=s<c?s:c,o=s<c?c:s;i=a<t?t:a>o?o:a}else{if(4!==f)throw new Error("predictor "+f+" is not coded here");i=Math.abs(a-s)<Math.abs(a-c)?s:c}}}y(o,i)}d()}const Y=[N(0),N(1),N(2),N(3),N(4),N(4,1,1),N(5,1,1),N(2,0,1),N(3,0,1)],H=N(4,1,1);class F{constructor(t){this.H=t,this.small=new Uint32Array(64*t),this.F=Array.from({length:t},()=>null),this.K=new Float64Array(t)}add(t,o){if(this.K[t]++,o<64)return void this.small[64*t+o]++;const n=this.F[t]||(this.F[t]=new Map);n.set(o,(n.get(o)||0)+1)}V(t,o,n=64){const r=new Uint32Array(n);for(const n of t){const t=64*n;for(let n=0;n<64;n++)this.small[t+n]&&(r[Q(o,n)]+=this.small[t+n]);const e=this.F[n];if(e)for(const[t,n]of e)r[Q(o,t)]+=n}return r}W(t,o){let n=$(this.V(t,o));for(const r of t){const t=64*r;for(let r=o.L;r<64;r++)this.small[t+r]&&(n+=this.small[t+r]*(i(r)-o.C-o.J));const e=this.F[r];if(e)for(const[t,r]of e)n+=r*(i(t)-o.C-o.J)}return n}}const K=[0,0,0];function Q(t,o){return _(t,o,K),K[0]}const V=[1];for(let t=1;t<32;t++)V.push(V[t-1]/2);function W(t){let o=0;for(t>=2&&t<4294967296&&(o=31-Math.clz32(t),t*=V[o]);t>=1.4142135623730951;)t/=2,o++;for(;t<.7071067811865476;)t*=2,o--;const n=(t-1)/(t+1),r=n*n;let e=n,i=n;for(let t=3;t<30;t+=2)i+=(e*=r)/t;return o+2.885390081777927*i}function $(t){let o=0,n=0;for(const n of t)o+=n;for(const r of t)r&&(n+=r*W(o/r));return n}function tt(t,{maxClusters:o=48,newClusterCost:n=320}={}){const r=[];for(let o=0;o<t.H;o++)t.K[o]>0&&r.push(o);r.sort((o,n)=>t.K[n]-t.K[o]);const e=[];for(const i of r){const r=t.V([i],H),s=$(r);let f=-1,c=1/0;for(let t=0;t<e.length;t++){const o=e[t],n=new Uint32Array(64);for(let t=0;t<64;t++)n[t]=o.$[t]+r[t];const i=$(n)-o.tt-s;i<c&&(c=i,f=t)}if(f<0||e.length<o&&c>n)e.push({H:[i],$:r,tt:s});else{const t=e[f];t.H.push(i);for(let o=0;o<64;o++)t.$[o]+=r[o];t.tt=$(t.$)}}e.length||e.push({H:[],$:new Uint32Array(64),tt:0});const i=new Uint8Array(t.H);let s=2*t.H;const f=e.map((o,n)=>{for(const t of o.H)i[t]=n;let r=Y[0],e=1/0;for(const n of Y){const i=t.W(o.H,n);i<e&&(e=i,r=n)}const f=X(t.V(o.H,r,128));return s+=e+3*f.M+40,{config:r,code:f}});return{X:i,k:f,write:(t,o,n)=>{const r=f[i[o]];P(t,r.code,r.config,n)},tt:s}}const ot=[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],nt=[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],rt=c.map(t=>(7&t)<<3|t>>3),et=(t,o,n)=>2*(nt[t]+ot[o])+n;function it(t,o,n){const r=[];if(o>0){const n=new Uint32Array(2048);let e=0;for(const o of t)n[Math.min(2047,Math.max(0,o+1024))]++,e++;let i=0,s=e/(o+1);for(let t=0;t<2048&&r.length<o;t++)i+=n[t],i>s&&(r.push(t-1025),s=e*(r.length+1)/(o+1))}const e=r.length+1,i=new Uint8Array(39*e);for(let t=0;t<e;t++)for(let o=0;o<13;o++)i[(0+o)*e+t]=t,i[(13+o)*e+t]=n?e:e+(t>>1),i[(26+o)*e+t]=n?e:e+(e-1>>1)+1+(t>>1);let s=0;for(const t of i)s=Math.max(s,t+1);return{thresholds:r,ot:e,map:i,nt:s,rt:t=>{let o=0;for(const n of r)t>n&&o++;return o},et:(t,o)=>i[13*(t<2?1^t:2)*e+o],it:(t,o)=>(Math.min(t,64)<8?Math.min(t,64):4+(Math.min(t,64)>>1))*s+o,st:t=>37*s+458*t,H:495*s}}function st(t,o,n){const r=o.map(()=>function(t,o=0,n=1){return{Z:t,offset:o,Y:n,context:-1}}(n)),i=r.map(()=>new Uint32Array(257));o.forEach((t,o)=>z(null,i[o],t.data,t.w,t.h,r[o])),function(t,{ft:o=!0,ct:n=[]}={}){t.write(1,o?1:0),t.write(1,1),t.o([[0,0],[0,1],[4,2],[8,18]],n.length);for(const o of n)B(t,o)}(t,{ft:!1,ct:[]});const s=function(t,o){const n=[o],r=[],i=[];for(const t of n){if(t.left){r.push(t.lt+1,e(t.ht)),n.push(t.left,t.right);continue}t.context=i.length,i.push(t);const o=1===t.Y?0:31-Math.clz32(t.Y&-t.Y);r.push(0,t.Z,e(t.offset),o,(t.Y>>o)-1)}const s=new Uint32Array(64);for(const t of r)T(q,t,s);const f=X(s);R(t,{X:new Uint8Array(6),k:[{config:q,code:f}]});for(const o of r)P(t,f,q,o);return i}(t,function(t){return function(t,o){const n=(r,e)=>{if(r===e)return o?t[r].ut:t[r];const i=r+e>>1;return function(t,o,n,r){return{lt:t,ht:o,left:n,right:r}}(o,o?t[i].wt:i,n(i+1,e),n(r,i))};return n(0,t.length-1)}(t,0)}(r)),f=function(t,o,n,r=t=>t.context){const e=new Uint8Array(o.length+1);for(const t of o)e[t.context]=r(t)+1;const i=new Uint32Array(2);i[1]=1;const s=[{config:q,code:X(i)}],f=Math.max(...e);for(let t=0;t<f;t++)s.push({config:q,code:X(n[t])});return R(t,{N:j,X:e,k:s}),s}(t,s,i,t=>r.indexOf(t));o.forEach((o,n)=>z(t,f[n+1].code,o.data,o.w,o.h,r[n]))}function*ft(t,o={}){const{width:n,height:i,components:c,alpha:l}=t,h=1===c.length,u=t.quantScale||1;let w=t.quantFieldBase||1;for(;w/(2040*u)<1/16384;)w*=2;const d=t.ycbcr||h,y=h?[0,0,0]:d?[1,0,2]:[0,1,2],p=y.map(t=>c[t]),b=o.orders?y.map(t=>o.orders[t].map(t=>rt[t])):[rt,rt,rt],m=t=>1===t?0:2===t?1:-1,g=p.map(t=>m(t.h)),A=p.map(t=>m(t.v));if(g.includes(-1)||A.includes(-1))throw a("sampling factors other than one or two");const x=Math.max(...g),v=Math.max(...A),E=t=>x-g[t],S=t=>v-A[t];if(!d&&(x||v))throw a("an RGB JPEG with subsampling");const C=Math.ceil(n/(8<<x))<<x,J=Math.ceil(i/(8<<v))<<v,L=Math.ceil(n/U),k=Math.ceil(i/U),O=L*k,_=Math.ceil(C/256),D=_*Math.ceil(J/256),Z=1===O,j=(t,o,n)=>{const r=p[t];return h&&1!==t?-1:64*(n*r.stride+o)},q=t=>{let o=1024;for(;t%o;)o>>=1;return o},B=p.map(t=>d?1:t.quant[0]/q(t.quant[0])),z=p.map(t=>d?0:1024/q(t.quant[0])),Y=new Int32Array(C*J);for(let t=0;t<J;t++)for(let o=0;o<C;o++)Y[t*C+o]=p[1].coeffs[j(1,o,t)]*B[1]+z[1];const H=p[1].quant,K=(H[8]+H[16]+H[24]+H[32]+H[40])/u,Q=C*J,V=Q<256?0:Math.max(1,Math.min(7,s(Q)-s(K)-7));let W=it(Y,V,h);const $=new r(256);if(function(t,o,n,r,e,{xyb:i=!1,orientation:s=1,colorSpace:f}={}){t.write(16,2815),t.write(1,0),I(t,n),t.write(3,0),I(t,o),t.write(1,0);const c=1!==s;t.write(1,c?1:0),c&&t.write(6,s-1),t.write(4,8),e?t.write(3,5):t.write(2,0),t.write(1,i?1:0),3===r&&"display-p3"===f?t.write(21,903568):3===r?t.write(1,1):t.write(15,14100),c&&t.write(1,1),t.write(2,0),t.write(1,1),t.i()}($,n,i,h?1:3,!!l,{orientation:t.orientation,colorSpace:t.colorSpace}),$.write(1,0),$.write(2,0),$.write(1,0),$.write(2,2),$.write(8,111),$.write(1,d?1:0),d)for(let t=0;t<3;t++)$.write(2,0===g[t]&&0===A[t]?0:1===g[t]&&1===A[t]?1:1===g[t]?2:3);$.write(2,0),l&&$.write(2,0),function(t,o){t.write(o?7:5,0),t.write(11,1)}($,l);const ot=[0,1,2].map(()=>new Int32Array(1024)),nt=(t,o)=>{const n=t%L*32,r=32*(t/L|0),i=Math.min(32,C-n),s=Math.min(32,J-r);for(let t=0;t<s;t++)for(let s=0;s<i;s++)for(const i of[1,0,2]){const f=E(i),c=S(i),a=s>>f,l=t>>c;if(a<<f!==s||l<<c!==t)continue;const h=j(i,(n>>f)+a,(r>>c)+l),u=p[i].coeffs;let w=0;if(h>=0)for(let t=1;t<64;t++)u[h+t]&&w++;const d=ot[i],y=0===a?0===l?32:d[32*(l-1)]:0===l?d[32*l+a-1]:d[32*(l-1)+a]+d[32*l+a-1]+1>>1;d[32*l+a]=w;const m=W.et(i,W.rt(Y[(r+t)*C+n+s]));o(W.it(y,m),w);const M=W.st(m);let g=w>4?0:1,A=w;for(let t=1;t<64&&A;t++){const n=u[h+b[i][t]];o(M+et(A,t,g),e(n)),g=n?1:0,A-=g}}},ft=(V>0?3:2)*O+D;let ct=0;const at=function*(t){W=t;const n=new F(t.H);for(let t=0;t<O;t++)nt(t,(t,o)=>n.add(t,o)),yield++ct/ft;return(o.coding||tt)(n,o.clusters)};let lt=yield*at(W);if(V>0){const t=W,o=it(Y,0,h),n=yield*at(o);n.tt<=lt.tt?lt=n:W=t}const ht=Array.from({length:Z?1:2+D+O},()=>null),ut=t=>ht[Z?0:t]||(ht[Z?0:t]=new r(4096)),wt=new Uint32Array(ht.length);let dt=0;const yt=t=>{t=Z?0:t;const o=Math.ceil(ht[t].u/8);dt+=o-wt[t],wt[t]=o,M(dt)},pt=ut(0);pt.write(1,0);const bt=(t,o)=>{const r=t*U,e=o*U,s=Math.min(U,n-r),f=Math.min(U,i-e),c=new Int32Array(s*f);for(let t=0;t<f;t++)for(let o=0;o<s;o++)c[t*s+o]=l[4*((e+t)*n+r+o)+3];return{w:s,h:f,data:c}};for(let t=0;t<3;t++)pt.write(16,f(p[t].quant[0]/B[t]/(2040*u)*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,W.thresholds.length);for(const t of W.thresholds)pt.o([[4,0],[8,16],[16,272],[32,65808]],e(t));pt.write(4,0),pt.write(4,0),G(pt,W.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),l&&st(pt,[Z?bt(0,0):{w:0,h:0,data:new Int32Array(0)}],5),yt(0);for(let o=0;o<D;o++){const n=ut(1+o),r=o%_*256,e=256*(o/_|0),i=Math.min(256,C-r),f=Math.min(256,J-e);n.write(2,0),st(n,[1,0,2].map(t=>{const o=i>>E(t),n=f>>S(t),s=new Int32Array(o*n),c=B[t],a=z[t];for(let i=0;i<n;i++)for(let n=0;n<o;n++){const f=j(t,(r>>E(t))+n,(e>>S(t))+i);s[i*o+n]=f<0?0:p[t].coeffs[f]*c+a}return{w:o,h:n,data:s}}),5);const c=i*f,a=s(c);a&&n.write(a,c-1);const l=i+7>>3,h=f+7>>3,u=new Int32Array(2*c).fill(w-1,c);if(t.quantFields)for(let o=0;o<f;o++)for(let n=0;n<i;n++)u[c+o*i+n]=t.quantFields[(e+o)*C+r+n]-1;st(n,[{w:l,h:h,data:new Int32Array(l*h)},{w:l,h:h,data:new Int32Array(l*h)},{w:c,h:2,data:u},{w:i,h:f,data:new Int32Array(c)}],w>1?5:0),yt(1+o),yield++ct/ft}const mt=ut(1+D);mt.write(1,0),mt.write(3,7),mt.write(16,f(w/(2040*u))),st(mt,[0,1,2].map(t=>{const o=new Int32Array(64),n=p[t].quant;for(let t=0;t<8;t++)for(let r=0;r<8;r++)o[8*r+t]=n[8*t+r];return{w:8,h:8,data:o}}),5);for(let t=1;t<17;t++)mt.write(3,0);if(O>1&&mt.write(s(O),0),o.orders){mt.o([[0,95],[0,19],[0,0],[13,0]],1);const t=[];for(const n of y){const r=Array.from({length:64},(t,o)=>o),e=o.orders[n].map(t=>{const o=r.indexOf(t);return r.splice(o,1),o});let i=64;for(;i>1&&!e[i-1];)i--;t.push(i-1,...e.slice(1,i))}const n=N(0),r=new Uint32Array(64);for(const o of t)T(n,o,r);const e=X(r);R(mt,{X:new Uint8Array(8),k:[{config:n,code:e}]});for(const o of t)P(mt,e,n,o)}else mt.write(2,2);lt.writeHistograms?lt.writeHistograms(mt):R(mt,{X:lt.X,k:lt.k}),yt(1+D);for(let t=0;t<O;t++){const o=ut(2+D+t);nt(t,(t,n)=>lt.write(o,t,n)),lt.flush&&lt.flush(o),l&&!Z&&st(o,[bt(t%L,Math.floor(t/L))],5),yt(2+D+t),yield++ct/ft}return function(t,o){const n=o.map(t=>t.length);!function(t,o){t.write(1,0),t.i();for(const n of o)t.o([[10,0],[14,1024],[22,17408],[30,4211712]],n);t.i()}(t,n);const r=t.finish(),e=r.length+n.reduce((t,o)=>t+o,0);M(e);const i=new Uint8Array(e);i.set(r);let s=r.length;for(const t of o)i.set(t,s),s+=t.length;return i}($,function(t){return t.map(t=>t?t.finish():new Uint8Array(0))}(ht))}function*ct(t,o,n){if(o<3&&!n)return yield*ft(t);let r=n;try{const n=ft(t);let e,i;for(;!(e=n.next()).done;)if(i=yield e.value/2,i&&r&&e.value<1)return n.return(),r;if((!r||e.value.length<r.length)&&(r=e.value),i||o<3)return r;const s=o<4?1:3,f=ft(t,{clusters:{maxClusters:32,newClusterCost:160}});for(;!(e=f.next()).done;)if(i=yield.5+e.value/(2*s),i&&e.value<1)return f.return(),r;if(e.value.length<r.length&&(r=e.value),i||o<4)return r;const a=function*(t){const o=c.map(t=>(7&t)<<3|t>>3),n=t.map(()=>new Uint32Array(64)),r=t.reduce((t,o)=>t+o.coeffs.length,0);let e=0;for(let i=0;i<t.length;i++){const{coeffs:s}=t[i],f=n[i];for(let t=0;t<s.length;t+=65536){const n=Math.min(t+65536,s.length);for(let r=t;r<n;r+=64)for(let t=1;t<64;t++)s[r+o[t]]&&f[t]++;if(e+=n-t,yield e/r)return null}}return n.map(t=>[0,...Array.from({length:63},(t,o)=>o+1).sort((o,n)=>Math.floor(t[n]/8)-Math.floor(t[o]/8)||o-n)])}(t.components);for(;!(e=a.next()).done;)if(yield.5+(1+e.value)/6)return a.return(),r;const l=ft(t,{orders:e.value});for(;!(e=l.next()).done;)if(i=yield.5+(2+e.value)/6,i&&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}}const at=[5,4,4,4,4,4,3,3,3,3,3,6,7],lt=[17,11,15,3,9,7,4,2,5,6,0,33,1],ht=196608;function ut(t,o){if(!o)return void t.write(1,0);const n=i(o);t.write(1,1),t.write(3,n),t.write(n,o-(1<<n))}function wt(t){let o=0,n=0;for(let r=0;r<t.length;r++)o+=t[r],t[r]&&(n=r+1);const r=new Uint16Array(n||1);if(!o)return r[0]=4096,r;let e=0;for(let i=0;i<n;i++)t[i]&&(e+=r[i]=Math.max(1,Math.round(4096*t[i]/o)));for(;4096!==e;){const i=e<4096?1:-1;let s=-1,f=-1/0;for(let e=0;e<n;e++)if(t[e]&&(i>0||r[e]>1)){const n=i*(4096*t[e]-r[e]*o);n>f&&(s=e,f=n)}r[s]+=i,e+=i}return r}function dt(t,o){const n=[];let r=0;for(let t=0;t<o.length;t++)o[t]&&n.push(t),o[t]>o[r]&&(r=t);if(n.length<=2){t.write(1,1),t.write(1,n.length-1);for(const o of n)ut(t,o);return void(2===n.length&&t.write(12,o[n[0]]))}t.write(1,0),t.write(1,0),t.write(3,7),t.write(3,5),ut(t,o.length-3);const e=new Uint8Array(o.length);let s=0;for(let t=0;t<o.length;t++)t!==r&&o[t]&&(e[t]=i(o[t])+1,s=Math.max(s,e[t]+(t<r?1:0)));e[r]=s;for(const o of e)t.write(at[o],lt[o]);for(let n=0;n<o.length;n++)if(n!==r&&e[n]>1){const r=e[n]-1;t.write(r,o[n]-(1<<r))}}function yt(t,o){const n=1<<o,r=4096>>o,e=new Uint16Array(t.length+1),i=new Uint16Array(4096);let s=-1;for(let o=0;o<t.length;o++)e[o+1]=e[o]+t[o],4096===t[o]&&(s=o);if(s>=0){for(let t=0;t<4096;t++)i[t]=t;return{starts:e,reverse:i}}const f=new Int32Array(n),c=new Uint16Array(n),a=new Int32Array(n),l=[],h=[];f.set(t);for(let t=0;t<n;t++)f[t]<r?l.push(t):f[t]>r&&h.push(t);for(;h.length;){const t=h.pop(),o=l.pop(),n=r-f[o];f[t]-=n,c[o]=t,a[o]=f[t]-f[o],f[t]<r?l.push(t):f[t]>r&&h.push(t)}for(let t=0;t<n;t++)for(let o=0;o<r;o++){const n=o<f[t],s=n?t:c[t],l=n?o:o+a[t];i[e[s]+l]=t*r+o}return{starts:e,reverse:i}}function pt(t,o){const n=tt(t,o),e=n.k.map(()=>[]);for(let o=0;o<t.H;o++)t.K[o]&&e[n.X[o]].push(o);const i=n.k.map(({config:o},n)=>({config:o,counts:wt(t.V(e[n],o,256))})),f=Math.max(5,s(Math.max(...i.map(t=>t.counts.length))));for(const t of i)Object.assign(t,yt(t.counts,f));const c=new r;c.write(1,0),n.X.length>1&&G(c,n.X),c.write(1,0),c.write(2,f-5);for(const t of i)O(c,t.config,f);for(const t of i)dt(c,t.counts);const a=new Uint32Array(ht),l=new Uint8Array(ht),h=new Uint16Array(ht),u=[0,0,0];let w=0;return{tt:n.tt,write:(t,o,r)=>{a[w]=r,l[w++]=n.X[o]},flush:t=>{let o=1245184;for(let t=w-1;t>=0;t--){const n=i[l[t]];_(n.config,a[t],u);const r=u[0],e=n.counts[r];a[t]=u[2],l[t]=u[1],o>>>20>=e&&(h[t]=65535&o,l[t]|=64,o>>>=16);const s=Math.floor(o/e);o=4096*s+n.reverse[n.starts[r]+o-s*e]}t.write(32,o);for(let o=0;o<w;o++){64&l[o]&&t.write(16,h[o]);const n=63&l[o];n&&t.write(n,a[o])}w=0},writeHistograms:t=>t.append(c)}}function*bt(t,o,n){if(o<2)return yield*ct(t,o,n);let r=yield*function*(t){let o,n;for(;!(o=t.next(n)).done;)n=yield(0+o.value)/2;return o.value}(ct(t,o,n));if(yield.5)return r;try{const o=ft(t,{coding:pt});let n;for(;!(n=o.next()).done;)if((yield.5+n.value/2)&&n.value<1)return o.return(),r;n.value.length<r.length&&(r=n.value)}catch(t){if((!(t instanceof RangeError)||t.code)&&"JXL_SIZE"!==t.code)throw t}return r}function mt(t,o){return function(t){return function(t){let o;for(;!(o=t.next()).done;);o.value}(t),{bytes:t.bytes,width:t.width,height:t.height,orientation:t.orientation}}(Mt(t,o))}function Mt(t,o){return A(t,o,bt)}export{o as LIMITS,mt as transcode,Mt as transcodeSteps};
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
+ const t=Object.freeze({bytes:16777216,pixels:24e6,edge:16384}),o=Object.freeze({bytes:16777216,pixels:64e6,edge:16384}),n=[1];for(let t=1;t<=32;t++)n.push(2*n[t-1]);class r{constructor(t=4096){this.bytes=new Uint8Array(t),this.at=0,this.t=0,this.pending=0}write(t,o){if(!(o>=0&&o<n[t]))throw new Error("bit write out of range: "+t+" bits, "+o);let r=this.t+o*(1<<this.pending),e=this.pending+t;this.at+5>=this.bytes.length&&this.grow();const i=this.bytes;for(;e>=8;)i[this.at++]=255&r,r=Math.floor(r/256),e-=8;this.t=r,this.pending=e}o(t,o){for(let r=0;r<4;r++){const[e,i]=t[r];if(o>=i&&o-i<n[e])return this.write(2,r),void(e&&this.write(e,o-i))}throw new Error("U32 value out of range: "+o)}i(){this.pending&&(this.at+1>=this.bytes.length&&this.grow(),this.bytes[this.at++]=this.t,this.t=0,this.pending=0)}get u(){return 8*this.at+this.pending}grow(o=0){const n=t.bytes+64;if(this.at+o+16>n)throw Object.assign(new Error("The encoded picture exceeds 16 MiB."),{code:"JXL_SIZE"});const r=new Uint8Array(Math.min(n,Math.max(2*this.bytes.length,this.at+o+16)));r.set(this.bytes.subarray(0,this.at)),this.bytes=r}append(t){if(this.at+t.at+8>=this.bytes.length&&this.grow(t.at+8),this.pending)for(let o=0;o<t.at;o++)this.write(8,t.bytes[o]);else this.bytes.set(t.bytes.subarray(0,t.at),this.at),this.at+=t.at;t.pending&&this.write(t.pending,t.t)}finish(){const t=this.at+(this.pending?1:0),o=this.bytes.slice(0,t);return this.pending&&(o[this.at]=this.t),o}}const e=(t,o)=>"number"==typeof t?o(t):{...t,at:n=>o(t.at(n))};function i(t){return t>=0?2*t:2*-t-1}function s(t){return 31-Math.clz32(t)}function f(t){return t<=1?0:32-Math.clz32(t-1)}function c(t){if(0===t)return 0;const o=t<0?32768:0;if(!((t=Math.abs(t))<65520))throw new Error("half float out of range: "+t);let n=0,r=t;for(;r>=2;)r/=2,n++;for(;r<1;)r*=2,n--;n<-14?(r=16384*t,n=-15):r-=1;let e=Math.round(1024*r);return 1024===e&&(e=0,n++),o|n+15<<10|e}const a=[0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63];function l(t,o="JXL_JPEG"){return Object.assign(new Error("JPEG: "+t),{code:o})}function h(t,o){const n=new Uint16Array(65536);let r=0,e=0;for(let i=1;i<=16;i++){for(let s=0;s<t[i-1];s++){const t=o[e++],s=r<<16-i,f=1<<16-i;if(s+f>=65536)throw l("bad Huffman table");n.fill(i<<8|t,s,s+f),r++}r<<=1}return n}const u=[.436,.2225,.0139,.3851,.7169,.0971,.143,.0606,.7139],w=[.5151,.2412,-.001,.292,.6922,.0419,.1571,.0666,.7841],d=[0,.00516,.01435,.02934,.05088,.07958,.11602,.16068,.21404,.27652,.34851,.43039,.52252,.62523,.73884,.86367,1],y=[2.4,1/1.055,.055/1.055,1/12.92,.04045];function p(t){if(t<=.04045)return t/12.92;const o=(t+.055)/1.055;let n=1;for(let t=0;t<24;t++)n=(4*n+o/(n*n*n*n))/5;return o*o*n*n}function b(t,o,n){const r=77===t[o]&&77===t[o+1];if(!r&&(73!==t[o]||73!==t[o+1]))return 1;const e=o=>o+2>n?0:r?t[o]<<8|t[o+1]:t[o]|t[o+1]<<8;if(42!==e(o+2))return 1;const i=o+(o=>o+4>n?0:r?(t[o]<<24|t[o+1]<<16|t[o+2]<<8|t[o+3])>>>0:(t[o]|t[o+1]<<8|t[o+2]<<16|t[o+3]<<24)>>>0)(o+4),s=e(i);for(let t=0,o=i+2;t<s&&o+12<=n;t++,o+=12){if(274!==e(o))continue;const t=e(o+8);return t>=1&&t<=8?t:1}return 1}function m(t,o,n,r,e,i,s,f,c){!function(t,o,n,r,e,i){if(!t.progressive){if(0!==n||63!==r||0!==e||0!==i)throw l("a bad spectral selection");for(const t of o){if(t.component.coverage.some(t=>-1!==t))throw l("a component coded twice");t.component.coverage.fill(0)}return}if(i>13||e&&e!==i+1)throw l("a bad successive approximation");if(0===n){if(0!==r)throw l("a bad spectral selection");for(const{component:t}of o){if(t.coverage[0]!==(e||-1))throw l(e?"a refinement scan out of order":"a DC scan twice");t.coverage[0]=i}return}if(1!==o.length)throw l("an interleaved AC scan");if(r>63||n>r)throw l("a bad spectral selection");const{coverage:s}=o[0].component;if(-1===s[0])throw l("an AC scan before the DC scan");for(let t=n;t<=r;t++){if(s[t]!==(e||-1))throw l(e?"a refinement scan out of order":"a band coded twice");s[t]=i}}(n,r,e,i,s,f);let h=o,u=0,w=0,d=0,y=!1;const p=()=>{for(;w<=24;){let o=0;if(y||h>=t.length)y=!0,d+=8;else if(o=t[h],255===o){const n=t[h+1];if(0===n)h+=2;else{if(255===n){h++;continue}y=!0,o=0,d+=8}}else h++;u=(u<<8|o)>>>0,w+=8}},b=()=>l("a scan cut short"),m=t=>{if(!t)return 0;if(p(),t>w-d)throw b();const o=u>>>w-t&(1<<t)-1;return w-=t,o},M=(t,o)=>t<1<<o-1?t-(1<<o)+1:t,g=t=>{if(!t)throw l("a scan without its Huffman table");p();const o=t[u>>>w-16&65535];if(!o)throw l("bad Huffman code");if(o>>8>w-d)throw b();return w-=o>>8,255&o},A=()=>{const o=w-d;if(o>=8)throw l("bytes left over in a scan");if(v)throw l("an end-of-band run past its interval");if(o&&(u>>>d&(1<<o)-1)!=(1<<o)-1)throw l("bad scan padding");for(;h+1<t.length&&255===t[h]&&255===t[h+1];)h++},U=o=>{if(A(),255!==t[h]||t[h+1]!==208+(7&o))throw l("a restart marker missing or out of order");h+=2,u=0,w=0,d=0,y=!1;for(const t of r)t.component.pred=0;v=0},I=(t,o)=>{if(o>11)throw l("a DC value out of range");const n=o?M(m(o),o):0;if(t.component.pred+=n,t.component.pred>2047||t.component.pred<-2048)throw l("a DC value out of range");return t.component.pred},x=(t,o)=>{if(t>10)throw l("an AC value out of range");const n=M(m(t),t)*(1<<o);if(n>1023||n<-1023)throw l("an AC value out of range");return n};let v=0;const E=1===r.length,S=n.progressive,C=()=>l("coefficients past the "+(S?"band":"block")),J=(t,o,n)=>{if(v>0)v--;else for(let r=e||1;r<=i;){const e=g(t.ac),s=e>>4,c=15&e;if(c){if(r+=s,r>i)throw C();o[n+a[r]]=x(c,f),r++}else{if(s<15){if(s&&!S)throw l("an end-of-band run in a sequential scan");v=(1<<s)-1+(s?m(s):0);break}if(r+=16,r>i+1)throw C()}}},L=S?0===e?0===s?(t,o,n)=>{const r=I(t,g(t.dc))*(1<<f);if(r>2047||r<-2048)throw l("a DC value out of range");o[n]=r}:(t,o,n)=>{m(1)&&(o[n]|=1<<f)}:0===s?J:(t,o,n)=>{const r=1<<f,s=-1<<f;let c=e;if(v<=0)for(;c<=i;c++){const e=g(t.ac);let f=e>>4,h=0;const u=15&e;if(u){if(1!==u)throw l("a bad refinement code");h=m(1)?r:s}else if(15!==f){v=1<<f,f&&(v+=m(f));break}for(;c<=i;c++){const t=n+a[c];if(0!==o[t])m(1)&&0===(o[t]&r)&&(o[t]+=o[t]>=0?r:s);else if(--f<0)break}if(f>=0||h&&c>i)throw l("coefficients past the band");h&&(o[n+a[c]]=h)}if(v>0){for(;c<=i;c++){const t=n+a[c];0!==o[t]&&m(1)&&0===(o[t]&r)&&(o[t]+=o[t]>=0?r:s)}v--}}:(t,o,n)=>{o[n]=I(t,g(t.dc)),J(t,o,n)};for(const t of r)t.component.pred=0;const X=r[0].component,k=E?X.blocksW*X.blocksH:n.mcusX*n.mcusY;for(let t=0;t<k;t++)if(c&&t>0&&t%c===0&&U(t/c-1),E){const o=t/X.blocksW|0,n=t-o*X.blocksW;L(r[0],X.coeffs,64*(o*X.stride+n))}else{const o=t/n.mcusX|0,e=t-o*n.mcusX;for(const t of r){const n=t.component;for(let r=0;r<n.v;r++)for(let i=0;i<n.h;i++)L(t,n.coeffs,64*((o*n.v+r)*n.stride+e*n.h+i))}}if(A(),255===t[h]&&t[h+1]>=208&&t[h+1]<=215){if(!c||k%c||t[h+1]!==208+(k/c-1&7))throw l("a restart marker out of order");for(h+=2;h+1<t.length&&255===t[h]&&255===t[h+1];)h++}if(h<t.length&&!(255===t[h]&&h+1<t.length&&0!==t[h+1]&&255!==t[h+1]))throw l("bytes left over after a scan");return h}const M=(t,o)=>Object.assign(new Error(o),{code:t});function g(o){if(o>t.bytes)throw M("JXL_SIZE","The encoded picture exceeds 16 MiB.")}function A(t){try{return t()}catch(t){if(t instanceof RangeError&&!t.code)throw M("JXL_MEMORY","Not enough memory for this picture.");throw t}}function U(n,r,e){const i=function(t={}){if(null===t||"object"!=typeof t)throw M("JXL_INPUT","Options are an object.");const o=void 0===t.effort?1:t.effort;if(!Number.isInteger(o)||o<1||o>9)throw M("JXL_INPUT","Effort is a whole number from 1 to 9.");return o}(r);if(!(n instanceof Uint8Array&&n.length))throw M("JXL_INPUT","A JPEG is a non-empty Uint8Array.");if(n.length>o.bytes)throw M("JXL_SIZE","A JPEG is at most 16 MiB.");const s=function(t){const o=function*(){let n,r;for(;!(n=A(()=>t.next(o.hurry))).done;)yield r=n.value;return r<1&&(yield 1),o.bytes=function(t){return g(t.length),t}(n.value)}();return o.bytes=null,o.hurry=!1,o}(function*(n,r,e,i){const f=function(t){if(!(t.length>4)||255!==t[0]||216!==t[1])throw l("not a JPEG");const n=[],r=[[],[]],e=new Set,i={count:0,p:[]};let s=null,f=0,c=-1,M=!1,g=1,A=2;const U=o=>t[o]<<8|t[o+1],I=(o,n)=>{for(let r=0;r<n.length;r++)if(t[o+r]!==n.charCodeAt(r))return!1;return!0};for(;;){if(A>=t.length){if(s&&s.scans)break;throw l("truncated")}if(255!==t[A])throw l("marker expected");for(;255===t[A];)A++;const u=t[A++];if(217===u)break;if(216===u)throw l("a second start");if(u>=208&&u<=215)throw l("a restart marker outside a scan");if(1===u)continue;if(A+2>t.length)throw l("truncated");const w=U(A),d=A+2,y=A+w;if(w<2||y>t.length)throw l("bad segment");if(A=y,219===u)for(let o=d;o<y;){const r=t[o]>>4,e=15&t[o];if(r>1||e>3||o+1+(r?128:64)>y)throw l("bad quantisation table");o++;const i=new Int32Array(64);for(let n=0;n<64;n++)if(i[a[n]]=r?U(o):t[o],o+=r?2:1,!i[a[n]])throw l("a zero quantisation step");if(s?.progressive&&s.components.some(t=>t.tq===e&&t.quant&&t.coverage.some(t=>0!==t)&&t.quant.some((t,o)=>t!==i[o])))throw l("a quantisation table changed between progressive scans");n[e]=i}else if(196===u)for(let o=d;o<y;){const n=t[o]>>4,i=15&t[o];if(n>1||i>3||o+17>y)throw l("bad Huffman table");o++;const s=t.subarray(o,o+16);let f=0;for(const t of s)f+=t;if(f>256||o+16+f>y)throw l("bad Huffman table");const c=t.subarray(o+16,o+16+f);r[n][i]=h(s,c),0===n&&(c.some(t=>t>15)?e.add(i):e.delete(i)),o+=16+f}else if(192===u||193===u||194===u){if(s)throw l("two frames");if(y-d<6)throw l("bad frame header");if(8!==t[d])throw l("only 8-bit samples");const n=U(d+1),r=U(d+3),e=t[d+5];if(!r||!n)throw l("a height left to a DNL marker");if(1!==e&&3!==e)throw l("only grey or three components");if(y-d<6+3*e)throw l("bad frame header");if(r>o.edge||n>o.edge)throw l("a picture past "+o.edge+" pixels on a side","JXL_DIMENSIONS");if(r*n>o.pixels)throw l("a picture past "+o.pixels+" pixels","JXL_DIMENSIONS");const i=[];let f=0;for(let o=0,n=d+6;o<e;o++,n+=3){const o=t[n+1]>>4,r=15&t[n+1],e=t[n+2];if(o<1||o>4||r<1||r>4)throw l("bad sampling");if(e>3)throw l("bad quantisation table");if(i.some(o=>o.id===t[n]))throw l("two components with one id");f+=o*r,i.push({id:t[n],h:o,v:r,tq:e,pred:0,coverage:new Int8Array(64).fill(-1)})}if(f>10)throw l("bad sampling");const c=Math.max(...i.map(t=>t.h)),a=Math.max(...i.map(t=>t.v)),h=Math.ceil(r/(8*c)),w=Math.ceil(n/(8*a));for(const t of i)t.blocksW=Math.ceil(Math.ceil(r*t.h/c)/8),t.blocksH=Math.ceil(Math.ceil(n*t.v/a)/8),t.stride=h*t.h,t.rows=w*t.v,t.coeffs=new Int16Array(t.stride*t.rows*64);s={progressive:194===u,width:r,height:n,components:i,hmax:c,vmax:a,mcusX:h,mcusY:w,scans:0}}else{if(195===u||u>=197&&u<=199||u>=201&&u<=203||u>=205&&u<=207)throw l("lossless, hierarchical or arithmetic coding");if(221===u){if(w<4)throw l("bad segment");f=U(d)}else{if(220===u)throw l("a DNL marker");if(218===u){if(!s)throw l("a scan before the frame");const o=t[d],i=[];if(o<1||o>4||d+1+2*o+3>y)throw l("bad scan header");let c=d+1;for(let e=0;e<o;e++,c+=2){const o=s.components.find(o=>o.id===t[c]);if(!o)throw l("a scan of an unknown component");if(i.some(t=>t.component===o))throw l("a component twice in one scan");if(o.quant||(o.quant=n[o.tq]),!o.quant)throw l("a scan without its quantisation table");i.push({component:o,dc:r[0][t[c+1]>>4],ac:r[1][15&t[c+1]],dcSlot:t[c+1]>>4})}const a=t[c],h=t[c+1],u=t[c+2]>>4,w=15&t[c+2];if((!s.progressive||0===a&&0===u)&&i.some(t=>e.has(t.dcSlot)))throw l("bad Huffman table");A=m(t,y,s,i,a,h,u,w,f),s.scans++}else if(224===u)I(d,"JFIF")&&(M=!0);else if(225===u)1===g&&I(d,"Exif")&&(g=b(t,d+6,y));else if(226===u){if(w>=16&&I(d,"ICC_PROFILE\0")){const o=t[d+12],n=t[d+13];if(!o||!n||o>n||i.count&&i.count!==n||i.p[o])throw l("a colour profile cut short");i.count=n,i.p[o]=t.subarray(d+14,y)}}else 238===u&&w>=14&&I(d,"Adobe")&&(c=t[d+11])}}}if(!s||!s.scans)throw l("no picture");for(const t of s.components)for(let o=0;o<64;o++)if(0!==t.coverage[o])throw l(s.progressive?"a progressive JPEG whose scans do not finish every coefficient":"a component without its scan");let x="srgb";if(i.count){const t=[];for(let o=1;o<=i.count;o++){if(!i.p[o])throw l("a colour profile cut short");t.push(i.p[o])}const o=new Uint8Array(t.reduce((t,o)=>t+o.length,0));for(let n=0,r=0;n<t.length;r+=t[n].length,n++)o.set(t[n],r);if(x=function(t){const o=t.length,n=o=>t[o]<<8|t[o+1],r=n=>n+4<=o?(t[n]<<24|t[n+1]<<16|t[n+2]<<8|t[n+3])>>>0:0,e=n=>n+4<=o?String.fromCharCode(t[n],t[n+1],t[n+2],t[n+3]):"",i=t=>(0|r(t))/65536,s=r(128);if(o<132||132+12*s>o||"acsp"!==e(36)||"XYZ "!==e(20))return null;const f=t=>{for(let n=0;n<s;n++){const i=132+12*n,s=r(i+4);if(e(i)===t)return s+r(i+8)<=o?s:-1}return-1},c=t=>{const s=f(t);if(s<0)return!1;if("para"===e(s)){const t=n(s+8),o=[1,3,4,5,7][t];if(3!==t&&4!==t)return!1;for(let t=0;t<o;t++)if(Math.abs(i(s+12+4*t)-(t<5?y[t]:0))>(t?.002:.01))return!1;return!0}const c="curv"===e(s)?r(s+8):0;if(c<2||s+12+2*c>o)return!1;for(let t=0;t<c;t++)if(Math.abs(n(s+12+2*t)/65535-p(t/(c-1)))>.001)return!1;return d.every((t,o)=>{const r=o*(c-1)/16,e=Math.floor(r),i=Math.min(c-1,e+1),f=r-e;return Math.abs((n(s+12+2*e)*(1-f)+n(s+12+2*i)*f)/65535-t)<=.001})};if(["A2B0","A2B1","A2B2"].some(t=>f(t)>=0))return null;if("GRAY"===e(16))return c("kTRC")?"srgb":null;if("RGB "!==e(16)||!["rTRC","gTRC","bTRC"].every(c))return null;const a=["rXYZ","gXYZ","bXYZ"].flatMap(t=>{const o=f(t);return o>=0&&"XYZ "===e(o)?[i(o+8),i(o+12),i(o+16)]:[9,9,9]}),l=t=>t.every((t,o)=>Math.abs(a[o]-t)<=.004);return l(u)?"srgb":l(w)?"display-p3":null}(o),!x)throw l("a colour profile other than sRGB or Display P3 (decode the picture and encode its pixels)")}let v=!1;return 3!==s.components.length||M||(v=c>=0?0===c:82===s.components[0].id&&71===s.components[1].id&&66===s.components[2].id),{width:s.width,height:s.height,components:s.components,progressive:s.progressive,ycbcr:!v,orientation:g,colorSpace:x}}(n);!function(o,n,r=t){if(!Number.isInteger(o)||!Number.isInteger(n)||o<1||n<1)throw M("JXL_INPUT","A picture is at least one pixel wide and high, in whole pixels.");if(o>r.edge||n>r.edge)throw M("JXL_DIMENSIONS","A picture is at most "+r.edge+" pixels on a side.");if(o*n>r.pixels)throw M("JXL_DIMENSIONS","A picture is at most "+r.pixels.toLocaleString("en-US")+" pixels.")}(f.width,f.height,o);const c=f.orientation>=5;return(t=>{Object.assign(s,t)})({width:c?f.height:f.width,height:c?f.width:f.height,orientation:f.orientation}),yield*i(f,r)}(n,i,0,e));return s}const I=256;function x(t,o){t.o([[9,1],[13,1],[18,1],[30,1]],o)}const v={channel:null,weighted:null,fill:null},E=[1,2,3,4,0,5,17,6,16,7,8,9,10,11,12,13,14,15],S=[2,4,3,2,2,4],C=[0,7,3,2,1,15];function J(t,o){const n=new Uint8Array(t.length),r=[];for(let o=0;o<t.length;o++)t[o]>0&&r.push(o);if(!r.length)return n;if(1===r.length)return n[r[0]]=1,n;for(let e=1;;e*=2){const i=r.map(o=>({w:Math.max(t[o],e),m:o,l:null,r:null})).sort((t,o)=>t.w-o.w||t.m-o.m),s=[];let f=0,c=0;const a=()=>f<i.length&&(c>=s.length||i[f].w<=s[c].w)?i[f++]:s[c++];for(;i.length-f+(s.length-c)>1;){const t=a(),o=a();s.push({w:t.w+o.w,m:-1,l:t,r:o})}const l=[[a(),0]];let h=0;for(;l.length;){const[t,o]=l.pop();t.m>=0?(n[t.m]=o,o>h&&(h=o)):l.push([t.l,o+1],[t.r,o+1])}if(h<=o)return n}}function L(t,o,n,r,e,i){const s=[];for(;s.push(r&e),r>>=i;)r--;for(let r=s.length-1;r>=0;r--)t.push(n),o.push(s[r])}function X(t,o){let n=0;for(let r=0;r<o;r++)n=n<<1|1&t,t>>=1;return n}function k(t){const o=new Uint32Array(17),n=new Uint32Array(17),r=new Uint16Array(t.length);for(const n of t)o[n]++;o[0]=0;for(let t=1,r=0;t<=16;t++)r=r+o[t-1]<<1,n[t]=r;for(let o=0;o<t.length;o++)t[o]&&(r[o]=X(n[t[o]]++,t[o]));return r}function N(t){const o=[];for(let n=0;n<t.length;n++)t[n]>0&&o.push(n);const n=o.length,r=o[n-1]+1||1,e=n<=4?new Uint8Array(r):J(t,15).subarray(0,r);let i=0;if(n<=4){o.sort((o,n)=>t[n]-t[o]||o-n),i=4===n&&t[o[0]]>t[o[2]]+t[o[3]]?1:0;const r=n<2?[0]:2===n?[1,1]:3===n?[1,2,2]:i?[1,2,3,3]:[2,2,2,2];o.forEach((t,o)=>{e[t]=r[o]}),o.sort((t,o)=>e[t]-e[o]||t-o)}return{M:r,A:e,U:k(e),I:n&&n<=4?o:null,S:i}}function O(t,o){if(o.M<=1)return;if(o.I){const n=s(o.M-1)+1;t.write(2,1),t.write(2,o.I.length-1);for(const r of o.I)t.write(n,r);return void(4===o.I.length&&t.write(1,o.S))}const n=o.A,r=[],e=[];let i=n.length-1;for(;i>0&&!n[i];)i--;let f=8;for(let t=0;t<=i;){const o=n[t];let s=1;for(;t+s<=i&&n[t+s]===o;)s++;let c=s;if(o&&o!==f&&(r.push(o),e.push(0),c--,f=o),c<3)for(let t=0;t<c;t++)r.push(o),e.push(0);else L(r,e,o?16:17,c-3,o?3:7,o?2:3);t+=s}const c=new Uint32Array(18);for(const t of r)c[t]++;let a=0;for(const t of c)t&&a++;1===a&&(c[r[0]?0:1]=c[r[0]]);const l=J(c,5),h=k(l);let u=18;for(;u>0&&!l[E[u-1]];)u--;t.write(2,0);for(let o=0;o<u;o++){const n=l[E[o]];t.write(S[n],C[n])}for(let o=0;o<r.length;o++){const n=r[o];t.write(l[n],h[n]),16===n?t.write(2,e[o]):17===n&&t.write(3,e[o])}}function _(t,o=0,n=0){return{split:t,C:o,J:n,L:1<<t}}function T(t,o,n=15){t.write(f(n+1),o.split),o.split!==n&&(t.write(f(o.split+1),o.C),t.write(f(o.split-o.C+1),o.J))}function D(t,o,n){if(o<t.L)return n[0]=o,n[1]=0,void(n[2]=0);const r=s(o),e=o-(1<<r),i=r-t.C-t.J;n[0]=t.L+(r-t.split<<t.C+t.J|e>>r-t.C<<t.J|e&(1<<t.J)-1),n[1]=i,n[2]=o>>t.J&(1<<i)-1}function P(t,o,n,r=0){D(t,o,G),n[r+G[0]]++}const G=[0,0,0];function R(t,o,n,r,e=0){D(n,r,G);const i=e+G[0];t.write(o.A[i],o.U[i]),G[1]&&t.write(G[1],G[2])}function Z(t,o){let n=0;for(const t of o)n=Math.max(n,t);const r=n?f(n+1):0;if(r<=3){if(t.write(1,1),t.write(2,r),r)for(const n of o)t.write(r,n);return}t.write(1,0),t.write(1,0);const e=_(8),i=_(4),s=o.length>=64,c=new Uint32Array(s?257:n+1),a=[];for(let t=0;t<o.length;){let n=1;for(;s&&t+n<o.length&&o[t+n]===o[t];)n++;if(n>=8)a.push([o[t],n-1]),c[o[t]]++,P(i,n-1-7,c,224),t+=n;else{for(let r=0;r<n;r++)a.push([o[t],0]),c[o[t]]++;t+=n}}const l=N(c);if(!s){j(t,{X:new Uint8Array(1),k:[{config:e,code:l}]});for(const[o]of a)R(t,l,e,o);return}const h=new Uint32Array(1);h[0]=1,j(t,{N:{O:224,minLength:7,_:i},X:new Uint8Array([1,0]),k:[{config:_(0),code:N(h)},{config:e,code:l}]});for(const[o,n]of a)R(t,l,e,o),n&&R(t,l,i,n-7,224)}function j(t,{N:o=null,X:n,k:r}){t.write(1,o?1:0),o&&(t.o([[0,224],[0,512],[0,4096],[15,8]],o.O),t.o([[0,3],[0,4],[2,5],[8,9]],o.minLength),T(t,o._,8)),n.length>1&&Z(t,n),t.write(1,1);for(const o of r)T(t,o.config,15);for(const o of r){const n=o.code.M-1;if(n){const o=s(n);t.write(1,1),t.write(4,o),t.write(o,n-(1<<o))}else t.write(1,0)}for(const o of r)O(t,o.code)}const q=_(4),B=Object.freeze({O:224,minLength:7,_:q}),z=_(0);function Y(t,o){const n=[[3,0],[6,8],[10,72],[13,1096]];if("rct"===o.type)t.write(2,0),t.o(n,o.T),t.o([[0,6],[2,0],[4,2],[6,10]],o.D??6);else if("palette"===o.type)t.write(2,1),t.o(n,o.T),t.o([[0,1],[0,3],[0,4],[13,1]],o.P),t.o([[8,0],[10,256],[12,1280],[16,5376]],o.G),t.o([[0,0],[8,1],[10,257],[16,1281]],o.R??0),t.write(4,o.Z??0);else{if("squeeze"!==o.type)throw new Error("unknown transform "+o.type);{t.write(2,2);const r=o.j??[];t.o([[0,0],[4,1],[6,9],[8,41]],r.length);for(const o of r)t.write(1,o.q?1:0),t.write(1,o.B?1:0),t.o(n,o.T),t.o([[0,1],[0,2],[0,3],[4,4]],o.P)}}}function H(t,o,n,r,e,s,f=!1){if(v.channel?.(t,o,n,r,e,s,f))return;const c=s.Z,a=s.offset,l=s.Y,h=s.config||z,u=q;let w=0;const d=(n,r=h,e=0)=>{t?R(t,o,r,n,e):f&&!e?o[n]++:P(r,n,o,f?o.length-33:e)},y=()=>{if(w){if(w>=8)d(0),d(w-7-1,u,224);else for(let t=0;t<w;t++)d(0);w=0}},p=(t,o)=>{const r=n[t];let e;1===l?e=r-o-a:(e=Math.round((r-o-a)/l),n[t]=o+a+e*l),0!==e?(y(),d(i(e))):w++};for(let t=0,o=0;t<e;t++)for(let e=0;e<r;e++,o++){let i;if(0===c)i=0;else{const s=e?n[o-1]:t?n[o-r]:0,f=t?n[o-r]:s;if(1===c)i=s;else if(2===c)i=f;else if(3===c)i=(s+f)/2|0;else{const a=s+f-(e&&t?n[o-r-1]:s);if(5===c){const t=s<f?s:f,o=s<f?f:s;i=a<t?t:a>o?o:a}else{if(4!==c)throw new Error("predictor "+c+" is not coded here");i=Math.abs(a-s)<Math.abs(a-f)?s:f}}}p(o,i)}y()}const F=[_(0),_(1),_(2),_(3),_(4),_(4,1,1),_(5,1,1),_(2,0,1),_(3,0,1)],K=_(4,1,1);class Q{constructor(t){this.H=t,this.small=new Uint32Array(64*t),this.F=Array.from({length:t},()=>null),this.K=new Float64Array(t)}add(t,o){if(this.K[t]++,o<64)return void this.small[64*t+o]++;const n=this.F[t]||(this.F[t]=new Map);n.set(o,(n.get(o)||0)+1)}V(t,o,n=64){const r=new Uint32Array(n);for(const n of t){const t=64*n;for(let n=0;n<64;n++)this.small[t+n]&&(r[W(o,n)]+=this.small[t+n]);const e=this.F[n];if(e)for(const[t,n]of e)r[W(o,t)]+=n}return r}W(t,o){let n=ot(this.V(t,o));for(const r of t){const t=64*r;for(let r=o.L;r<64;r++)this.small[t+r]&&(n+=this.small[t+r]*(s(r)-o.C-o.J));const e=this.F[r];if(e)for(const[t,r]of e)n+=r*(s(t)-o.C-o.J)}return n}}const V=[0,0,0];function W(t,o){return D(t,o,V),V[0]}const $=[1];for(let t=1;t<32;t++)$.push($[t-1]/2);function tt(t){let o=0;for(t>=2&&t<4294967296&&(o=31-Math.clz32(t),t*=$[o]);t>=1.4142135623730951;)t/=2,o++;for(;t<.7071067811865476;)t*=2,o--;const n=(t-1)/(t+1),r=n*n;let e=n,i=n;for(let t=3;t<30;t+=2)i+=(e*=r)/t;return o+2.885390081777927*i}function ot(t){let o=0,n=0;for(const n of t)o+=n;for(const r of t)r&&(n+=r*tt(o/r));return n}function nt(t,{maxClusters:o=48,newClusterCost:n=320}={}){const r=[];for(let o=0;o<t.H;o++)t.K[o]>0&&r.push(o);r.sort((o,n)=>t.K[n]-t.K[o]);const e=[];for(const i of r){const r=t.V([i],K),s=ot(r);let f=-1,c=1/0;for(let t=0;t<e.length;t++){const o=e[t],n=new Uint32Array(64);for(let t=0;t<64;t++)n[t]=o.$[t]+r[t];const i=ot(n)-o.tt-s;i<c&&(c=i,f=t)}if(f<0||e.length<o&&c>n)e.push({H:[i],$:r,tt:s});else{const t=e[f];t.H.push(i);for(let o=0;o<64;o++)t.$[o]+=r[o];t.tt=ot(t.$)}}e.length||e.push({H:[],$:new Uint32Array(64),tt:0});const i=new Uint8Array(t.H);let s=2*t.H;const f=e.map((o,n)=>{for(const t of o.H)i[t]=n;let r=F[0],e=1/0;for(const n of F){const i=t.W(o.H,n);i<e&&(e=i,r=n)}const f=N(t.V(o.H,r,128));return s+=e+3*f.M+40,{config:r,code:f}});return{X:i,k:f,write:(t,o,n)=>{const r=f[i[o]];R(t,r.code,r.config,n)},tt:s}}const rt=[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],et=[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],it=a.map(t=>(7&t)<<3|t>>3),st=(t,o,n)=>2*(et[t]+rt[o])+n;function ft(t,o,n){const r=[];if(o>0){const n=new Uint32Array(2048);let e=0;for(const o of t)n[Math.min(2047,Math.max(0,o+1024))]++,e++;let i=0,s=e/(o+1);for(let t=0;t<2048&&r.length<o;t++)i+=n[t],i>s&&(r.push(t-1025),s=e*(r.length+1)/(o+1))}const e=r.length+1,i=new Uint8Array(39*e);for(let t=0;t<e;t++)for(let o=0;o<13;o++)i[(0+o)*e+t]=t,i[(13+o)*e+t]=n?e:e+(t>>1),i[(26+o)*e+t]=n?e:e+(e-1>>1)+1+(t>>1);let s=0;for(const t of i)s=Math.max(s,t+1);return{thresholds:r,ot:e,map:i,nt:s,rt:t=>{let o=0;for(const n of r)t>n&&o++;return o},et:(t,o)=>i[13*(t<2?1^t:2)*e+o],it:(t,o)=>(Math.min(t,64)<8?Math.min(t,64):4+(Math.min(t,64)>>1))*s+o,st:t=>37*s+458*t,H:495*s}}function ct(t,o,n){const r=o.map(()=>function(t,o=0,n=1){return{Z:t,offset:o,Y:n,context:-1}}(n)),e=r.map(()=>new Uint32Array(257));o.forEach((t,o)=>H(null,e[o],t.data,t.w,t.h,r[o])),function(t,{ft:o=!0,ct:n=[]}={}){t.write(1,o?1:0),t.write(1,1),t.o([[0,0],[0,1],[4,2],[8,18]],n.length);for(const o of n)Y(t,o)}(t,{ft:!1,ct:[]});const s=function(t,o){const n=[o],r=[],e=[];for(const t of n){if(t.left){r.push(t.lt+1,i(t.ht)),n.push(t.left,t.right);continue}t.context=e.length,e.push(t);const o=1===t.Y?0:31-Math.clz32(t.Y&-t.Y);r.push(0,t.Z,i(t.offset),o,(t.Y>>o)-1)}const s=new Uint32Array(64);for(const t of r)P(z,t,s);const f=N(s);j(t,{X:new Uint8Array(6),k:[{config:z,code:f}]});for(const o of r)R(t,f,z,o);return e}(t,function(t){return function(t,o){const n=(r,e)=>{if(r===e)return o?t[r].ut:t[r];const i=r+e>>1;return function(t,o,n,r){return{lt:t,ht:o,left:n,right:r}}(o,o?t[i].wt:i,n(i+1,e),n(r,i))};return n(0,t.length-1)}(t,0)}(r)),f=function(t,o,n,r=t=>t.context,e=()=>z){const i=new Uint8Array(o.length+1);for(const t of o)i[t.context]=r(t)+1;const s=new Uint32Array(2);s[1]=1;const f=[{config:z,code:N(s)}],c=Math.max(...i);for(let t=0;t<c;t++)f.push({config:e(t),code:N(n[t])});return j(t,{N:B,X:i,k:f}),f}(t,s,e,t=>r.indexOf(t));o.forEach((o,n)=>H(t,f[n+1].code,o.data,o.w,o.h,r[n]))}function*at(t,o={}){const{width:n,height:e,components:s,alpha:a}=t,h=1===s.length,u=t.quantScale||1;let w=t.quantFieldBase||1;for(;w/(2040*u)<1/16384;)w*=2;const d=t.ycbcr||h,y=h?[0,0,0]:d?[1,0,2]:[0,1,2],p=y.map(t=>s[t]),b=o.orders?y.map(t=>o.orders[t].map(t=>it[t])):[it,it,it],m=t=>1===t?0:2===t?1:-1,M=p.map(t=>m(t.h)),A=p.map(t=>m(t.v));if(M.includes(-1)||A.includes(-1))throw l("sampling factors other than one or two");const U=Math.max(...M),v=Math.max(...A),E=t=>U-M[t],S=t=>v-A[t];if(!d&&(U||v))throw l("an RGB JPEG with subsampling");const C=Math.ceil(n/(8<<U))<<U,J=Math.ceil(e/(8<<v))<<v,L=Math.ceil(n/I),X=Math.ceil(e/I),k=L*X,O=Math.ceil(C/256),T=O*Math.ceil(J/256),D=1===k,G=(t,o,n)=>{const r=p[t];return h&&1!==t?-1:64*(n*r.stride+o)},q=t=>{let o=1024;for(;t%o;)o>>=1;return o},B=p.map(t=>d?1:t.quant[0]/q(t.quant[0])),z=p.map(t=>d?0:1024/q(t.quant[0])),Y=new Int32Array(C*J);for(let t=0;t<J;t++)for(let o=0;o<C;o++)Y[t*C+o]=p[1].coeffs[G(1,o,t)]*B[1]+z[1];const H=p[1].quant,F=(H[8]+H[16]+H[24]+H[32]+H[40])/u,K=C*J,V=K<256?0:Math.max(1,Math.min(7,f(K)-f(F)-7));let W=ft(Y,V,h);const $=new r(256);if(function(t,o,n,r,e,{xyb:i=!1,orientation:s=1,colorSpace:f}={}){t.write(16,2815),t.write(1,0),x(t,n),t.write(3,0),x(t,o),t.write(1,0);const c=1!==s;t.write(1,c?1:0),c&&t.write(6,s-1),t.write(4,8),e?t.write(3,5):t.write(2,0),t.write(1,i?1:0),3===r&&"display-p3"===f?t.write(21,903568):3===r?t.write(1,1):t.write(15,14100),c&&t.write(1,1),t.write(2,0),t.write(1,1),t.i()}($,n,e,h?1:3,!!a,{orientation:t.orientation,colorSpace:t.colorSpace}),$.write(1,0),$.write(2,0),$.write(1,0),$.write(2,2),$.write(8,111),$.write(1,d?1:0),d)for(let t=0;t<3;t++)$.write(2,0===M[t]&&0===A[t]?0:1===M[t]&&1===A[t]?1:1===M[t]?2:3);$.write(2,0),a&&$.write(2,0),function(t,o){t.write(o?7:5,0),t.write(11,1)}($,a);const tt=[0,1,2].map(()=>new Int32Array(1024)),ot=(t,o)=>{const n=t%L*32,r=32*(t/L|0),e=Math.min(32,C-n),s=Math.min(32,J-r);for(let t=0;t<s;t++)for(let s=0;s<e;s++)for(const e of[1,0,2]){const f=E(e),c=S(e),a=s>>f,l=t>>c;if(a<<f!==s||l<<c!==t)continue;const h=G(e,(n>>f)+a,(r>>c)+l),u=p[e].coeffs;let w=0;if(h>=0)for(let t=1;t<64;t++)u[h+t]&&w++;const d=tt[e],y=0===a?0===l?32:d[32*(l-1)]:0===l?d[32*l+a-1]:d[32*(l-1)+a]+d[32*l+a-1]+1>>1;d[32*l+a]=w;const m=W.et(e,W.rt(Y[(r+t)*C+n+s]));o(W.it(y,m),w);const M=W.st(m);let g=w>4?0:1,A=w;for(let t=1;t<64&&A;t++){const n=u[h+b[e][t]];o(M+st(A,t,g),i(n)),g=n?1:0,A-=g}}},rt=(V>0?3:2)*k+T;let et=0;const at=function*(t){W=t;const n=new Q(t.H);for(let t=0;t<k;t++)ot(t,(t,o)=>n.add(t,o)),yield++et/rt;return(o.coding||nt)(n,o.clusters)};let lt=yield*at(W);if(V>0){const t=W,o=ft(Y,0,h),n=yield*at(o);n.tt<=lt.tt?lt=n:W=t}const ht=Array.from({length:D?1:2+T+k},()=>null),ut=t=>ht[D?0:t]||(ht[D?0:t]=new r(4096)),wt=new Uint32Array(ht.length);let dt=0;const yt=t=>{t=D?0:t;const o=Math.ceil(ht[t].u/8);dt+=o-wt[t],wt[t]=o,g(dt)},pt=ut(0);pt.write(1,0);const bt=(t,o)=>{const r=t*I,i=o*I,s=Math.min(I,n-r),f=Math.min(I,e-i),c=new Int32Array(s*f);for(let t=0;t<f;t++)for(let o=0;o<s;o++)c[t*s+o]=a[4*((i+t)*n+r+o)+3];return{w:s,h:f,data:c}};for(let t=0;t<3;t++)pt.write(16,c(p[t].quant[0]/B[t]/(2040*u)*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,W.thresholds.length);for(const t of W.thresholds)pt.o([[4,0],[8,16],[16,272],[32,65808]],i(t));pt.write(4,0),pt.write(4,0),Z(pt,W.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),a&&ct(pt,[D?bt(0,0):{w:0,h:0,data:new Int32Array(0)}],5),yt(0);for(let o=0;o<T;o++){const n=ut(1+o),r=o%O*256,e=256*(o/O|0),i=Math.min(256,C-r),s=Math.min(256,J-e);n.write(2,0),ct(n,[1,0,2].map(t=>{const o=i>>E(t),n=s>>S(t),f=new Int32Array(o*n),c=B[t],a=z[t];for(let i=0;i<n;i++)for(let n=0;n<o;n++){const s=G(t,(r>>E(t))+n,(e>>S(t))+i);f[i*o+n]=s<0?0:p[t].coeffs[s]*c+a}return{w:o,h:n,data:f}}),5);const c=i*s,a=f(c);a&&n.write(a,c-1);const l=i+7>>3,h=s+7>>3,u=new Int32Array(2*c).fill(w-1,c);if(t.quantFields)for(let o=0;o<s;o++)for(let n=0;n<i;n++)u[c+o*i+n]=t.quantFields[(e+o)*C+r+n]-1;ct(n,[{w:l,h:h,data:new Int32Array(l*h)},{w:l,h:h,data:new Int32Array(l*h)},{w:c,h:2,data:u},{w:i,h:s,data:new Int32Array(c)}],w>1?5:0),yt(1+o),yield++et/rt}const mt=ut(1+T);mt.write(1,0),mt.write(3,7),mt.write(16,c(w/(2040*u))),ct(mt,[0,1,2].map(t=>{const o=new Int32Array(64),n=p[t].quant;for(let t=0;t<8;t++)for(let r=0;r<8;r++)o[8*r+t]=n[8*t+r];return{w:8,h:8,data:o}}),5);for(let t=1;t<17;t++)mt.write(3,0);if(k>1&&mt.write(f(k),0),o.orders){mt.o([[0,95],[0,19],[0,0],[13,0]],1);const t=[];for(const n of y){const r=Array.from({length:64},(t,o)=>o),e=o.orders[n].map(t=>{const o=r.indexOf(t);return r.splice(o,1),o});let i=64;for(;i>1&&!e[i-1];)i--;t.push(i-1,...e.slice(1,i))}const n=_(0),r=new Uint32Array(64);for(const o of t)P(n,o,r);const e=N(r);j(mt,{X:new Uint8Array(8),k:[{config:n,code:e}]});for(const o of t)R(mt,e,n,o)}else mt.write(2,2);lt.writeHistograms?lt.writeHistograms(mt):j(mt,{X:lt.X,k:lt.k}),yt(1+T);for(let t=0;t<k;t++){const o=ut(2+T+t);ot(t,(t,n)=>lt.write(o,t,n)),lt.flush&&lt.flush(o),a&&!D&&ct(o,[bt(t%L,Math.floor(t/L))],5),yt(2+T+t),yield++et/rt}return function(t,o){const n=o.map(t=>t.length);!function(t,o){t.write(1,0),t.i();for(const n of o)t.o([[10,0],[14,1024],[22,17408],[30,4211712]],n);t.i()}(t,n);const r=t.finish(),e=r.length+n.reduce((t,o)=>t+o,0);g(e);const i=new Uint8Array(e);i.set(r);let s=r.length;for(const t of o)i.set(t,s),s+=t.length;return i}($,function(t){return t.map(t=>t?t.finish():new Uint8Array(0))}(ht))}function*lt(t,o,n){if(o<3&&!n)return yield*at(t);let r=n;try{const n=at(t);let e,i;for(;!(e=n.next()).done;)if(i=yield e.value/2,i&&r&&e.value<1)return n.return(),r;if((!r||e.value.length<r.length)&&(r=e.value),i||o<3)return r;const s=o<4?1:3,f=at(t,{clusters:{maxClusters:32,newClusterCost:160}});for(;!(e=f.next()).done;)if(i=yield.5+e.value/(2*s),i&&e.value<1)return f.return(),r;if(e.value.length<r.length&&(r=e.value),i||o<4)return r;const c=function*(t){const o=a.map(t=>(7&t)<<3|t>>3),n=t.map(()=>new Uint32Array(64)),r=t.reduce((t,o)=>t+o.coeffs.length,0);let e=0;for(let i=0;i<t.length;i++){const{coeffs:s}=t[i],f=n[i];for(let t=0;t<s.length;t+=65536){const n=Math.min(t+65536,s.length);for(let r=t;r<n;r+=64)for(let t=1;t<64;t++)s[r+o[t]]&&f[t]++;if(e+=n-t,yield e/r)return null}}return n.map(t=>[0,...Array.from({length:63},(t,o)=>o+1).sort((o,n)=>Math.floor(t[n]/8)-Math.floor(t[o]/8)||o-n)])}(t.components);for(;!(e=c.next()).done;)if(yield.5+(1+e.value)/6)return c.return(),r;const l=at(t,{orders:e.value});for(;!(e=l.next()).done;)if(i=yield.5+(2+e.value)/6,i&&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}}const ht=[5,4,4,4,4,4,3,3,3,3,3,6,7],ut=[17,11,15,3,9,7,4,2,5,6,0,33,1],wt=196608;function dt(t,o){if(!o)return void t.write(1,0);const n=s(o);t.write(1,1),t.write(3,n),t.write(n,o-(1<<n))}function yt(t){let o=0,n=0;for(let r=0;r<t.length;r++)o+=t[r],t[r]&&(n=r+1);const r=new Uint16Array(n||1);if(!o)return r[0]=4096,r;let e=0;for(let i=0;i<n;i++)t[i]&&(e+=r[i]=Math.max(1,Math.round(4096*t[i]/o)));for(;4096!==e;){const i=e<4096?1:-1;let s=-1,f=-1/0;for(let e=0;e<n;e++)if(t[e]&&(i>0||r[e]>1)){const n=i*(4096*t[e]-r[e]*o);n>f&&(s=e,f=n)}r[s]+=i,e+=i}return r}function pt(t,o){const n=[];let r=0;for(let t=0;t<o.length;t++)o[t]&&n.push(t),o[t]>o[r]&&(r=t);if(n.length<=2){t.write(1,1),t.write(1,n.length-1);for(const o of n)dt(t,o);return void(2===n.length&&t.write(12,o[n[0]]))}t.write(1,0),t.write(1,0),t.write(3,7),t.write(3,5),dt(t,o.length-3);const e=new Uint8Array(o.length);let i=0;for(let t=0;t<o.length;t++)t!==r&&o[t]&&(e[t]=s(o[t])+1,i=Math.max(i,e[t]+(t<r?1:0)));e[r]=i;for(const o of e)t.write(ht[o],ut[o]);for(let n=0;n<o.length;n++)if(n!==r&&e[n]>1){const r=e[n]-1;t.write(r,o[n]-(1<<r))}}function bt(t,o){const n=1<<o,r=4096>>o,e=new Uint16Array(t.length+1),i=new Uint16Array(4096);let s=-1;for(let o=0;o<t.length;o++)e[o+1]=e[o]+t[o],4096===t[o]&&(s=o);if(s>=0){for(let t=0;t<4096;t++)i[t]=t;return{starts:e,reverse:i}}const f=new Int32Array(n),c=new Uint16Array(n),a=new Int32Array(n),l=[],h=[];f.set(t);for(let t=0;t<n;t++)f[t]<r?l.push(t):f[t]>r&&h.push(t);for(;h.length;){const t=h.pop(),o=l.pop(),n=r-f[o];f[t]-=n,c[o]=t,a[o]=f[t]-f[o],f[t]<r?l.push(t):f[t]>r&&h.push(t)}for(let t=0;t<n;t++)for(let o=0;o<r;o++){const n=o<f[t],s=n?t:c[t],l=n?o:o+a[t];i[e[s]+l]=t*r+o}return{starts:e,reverse:i}}function mt(t,o){const n=nt(t,o),e=n.k.map(()=>[]);for(let o=0;o<t.H;o++)t.K[o]&&e[n.X[o]].push(o);const i=n.k.map(({config:o},n)=>({config:o,counts:yt(t.V(e[n],o,256))})),s=Math.max(5,f(Math.max(...i.map(t=>t.counts.length))));for(const t of i)Object.assign(t,bt(t.counts,s));const c=new r;c.write(1,0),n.X.length>1&&Z(c,n.X),c.write(1,0),c.write(2,s-5);for(const t of i)T(c,t.config,s);for(const t of i)pt(c,t.counts);const a=new Uint32Array(wt),l=new Uint8Array(wt),h=new Uint16Array(wt),u=[0,0,0];let w=0;return{tt:n.tt,write:(t,o,r)=>{a[w]=r,l[w++]=n.X[o]},flush:t=>{let o=1245184;for(let t=w-1;t>=0;t--){const n=i[l[t]];D(n.config,a[t],u);const r=u[0],e=n.counts[r];a[t]=u[2],l[t]=u[1],o>>>20>=e&&(h[t]=65535&o,l[t]|=64,o>>>=16);const s=Math.floor(o/e);o=4096*s+n.reverse[n.starts[r]+o-s*e]}t.write(32,o);for(let o=0;o<w;o++){64&l[o]&&t.write(16,h[o]);const n=63&l[o];n&&t.write(n,a[o])}w=0},writeHistograms:t=>t.append(c)}}function*Mt(t,o,n){if(o<2)return yield*lt(t,o,n);let r=yield*function*(t){let o,n;for(;!(o=t.next(n)).done;)n=yield e(o.value,t=>(0+t)/2);return o.value}(lt(t,o,n));if(yield.5)return r;try{const o=at(t,{coding:mt});let n;for(;!(n=o.next()).done;)if((yield.5+n.value/2)&&n.value<1)return o.return(),r;n.value.length<r.length&&(r=n.value)}catch(t){if((!(t instanceof RangeError)||t.code)&&"JXL_SIZE"!==t.code)throw t}return r}function gt(t,o){return function(t){return function(t){let o;for(;!(o=t.next()).done;);o.value}(t),{bytes:t.bytes,width:t.width,height:t.height,orientation:t.orientation}}(At(t,o))}function At(t,o){return U(t,o,Mt)}export{o as LIMITS,gt as transcode,At as transcodeSteps};
package/jpeg.min.mjs CHANGED
@@ -1,2 +1,2 @@
1
- /*! Rapier JXL 2.1.0 | MIT License Copyright (c) 2026 rapier.website Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
2
- const t=Object.freeze({bytes:16777216,pixels:24e6,edge:16384}),o=Object.freeze({bytes:16777216,pixels:64e6,edge:16384}),n=[1];for(let t=1;t<=32;t++)n.push(2*n[t-1]);class r{constructor(t=4096){this.bytes=new Uint8Array(t),this.at=0,this.t=0,this.pending=0}write(t,o){if(!(o>=0&&o<n[t]))throw new Error("bit write out of range: "+t+" bits, "+o);let r=this.t+o*(1<<this.pending),e=this.pending+t;this.at+5>=this.bytes.length&&this.grow();const i=this.bytes;for(;e>=8;)i[this.at++]=255&r,r=Math.floor(r/256),e-=8;this.t=r,this.pending=e}o(t,o){for(let r=0;r<4;r++){const[e,i]=t[r];if(o>=i&&o-i<n[e])return this.write(2,r),void(e&&this.write(e,o-i))}throw new Error("U32 value out of range: "+o)}i(){this.pending&&(this.at+1>=this.bytes.length&&this.grow(),this.bytes[this.at++]=this.t,this.t=0,this.pending=0)}get u(){return 8*this.at+this.pending}grow(o=0){const n=t.bytes+64;if(this.at+o+16>n)throw Object.assign(new Error("The encoded picture exceeds 16 MiB."),{code:"JXL_SIZE"});const r=new Uint8Array(Math.min(n,Math.max(2*this.bytes.length,this.at+o+16)));r.set(this.bytes.subarray(0,this.at)),this.bytes=r}append(t){if(this.at+t.at+8>=this.bytes.length&&this.grow(t.at+8),this.pending)for(let o=0;o<t.at;o++)this.write(8,t.bytes[o]);else this.bytes.set(t.bytes.subarray(0,t.at),this.at),this.at+=t.at;t.pending&&this.write(t.pending,t.t)}finish(){const t=this.at+(this.pending?1:0),o=this.bytes.slice(0,t);return this.pending&&(o[this.at]=this.t),o}}function e(t){return t>=0?2*t:2*-t-1}function i(t){return 31-Math.clz32(t)}function s(t){return t<=1?0:32-Math.clz32(t-1)}function f(t){if(0===t)return 0;const o=t<0?32768:0;if(!((t=Math.abs(t))<65520))throw new Error("half float out of range: "+t);let n=0,r=t;for(;r>=2;)r/=2,n++;for(;r<1;)r*=2,n--;n<-14?(r=16384*t,n=-15):r-=1;let e=Math.round(1024*r);return 1024===e&&(e=0,n++),o|n+15<<10|e}const c=[0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63];function a(t,o="JXL_JPEG"){return Object.assign(new Error("JPEG: "+t),{code:o})}function l(t,o){const n=new Uint16Array(65536);let r=0,e=0;for(let i=1;i<=16;i++){for(let s=0;s<t[i-1];s++){const t=o[e++],s=r<<16-i,f=1<<16-i;if(s+f>=65536)throw a("bad Huffman table");n.fill(i<<8|t,s,s+f),r++}r<<=1}return n}const h=[.436,.2225,.0139,.3851,.7169,.0971,.143,.0606,.7139],u=[.5151,.2412,-.001,.292,.6922,.0419,.1571,.0666,.7841],w=[0,.00516,.01435,.02934,.05088,.07958,.11602,.16068,.21404,.27652,.34851,.43039,.52252,.62523,.73884,.86367,1],d=[2.4,1/1.055,.055/1.055,1/12.92,.04045];function p(t){if(t<=.04045)return t/12.92;const o=(t+.055)/1.055;let n=1;for(let t=0;t<24;t++)n=(4*n+o/(n*n*n*n))/5;return o*o*n*n}function y(t,o,n){const r=77===t[o]&&77===t[o+1];if(!r&&(73!==t[o]||73!==t[o+1]))return 1;const e=o=>o+2>n?0:r?t[o]<<8|t[o+1]:t[o]|t[o+1]<<8;if(42!==e(o+2))return 1;const i=o+(o=>o+4>n?0:r?(t[o]<<24|t[o+1]<<16|t[o+2]<<8|t[o+3])>>>0:(t[o]|t[o+1]<<8|t[o+2]<<16|t[o+3]<<24)>>>0)(o+4),s=e(i);for(let t=0,o=i+2;t<s&&o+12<=n;t++,o+=12){if(274!==e(o))continue;const t=e(o+8);return t>=1&&t<=8?t:1}return 1}function b(t,o,n,r,e,i,s,f,l){!function(t,o,n,r,e,i){if(!t.progressive){if(0!==n||63!==r||0!==e||0!==i)throw a("a bad spectral selection");for(const t of o){if(t.component.coverage.some(t=>-1!==t))throw a("a component coded twice");t.component.coverage.fill(0)}return}if(i>13||e&&e!==i+1)throw a("a bad successive approximation");if(0===n){if(0!==r)throw a("a bad spectral selection");for(const{component:t}of o){if(t.coverage[0]!==(e||-1))throw a(e?"a refinement scan out of order":"a DC scan twice");t.coverage[0]=i}return}if(1!==o.length)throw a("an interleaved AC scan");if(r>63||n>r)throw a("a bad spectral selection");const{coverage:s}=o[0].component;if(-1===s[0])throw a("an AC scan before the DC scan");for(let t=n;t<=r;t++){if(s[t]!==(e||-1))throw a(e?"a refinement scan out of order":"a band coded twice");s[t]=i}}(n,r,e,i,s,f);let h=o,u=0,w=0,d=0,p=!1;const y=()=>{for(;w<=24;){let o=0;if(p||h>=t.length)p=!0,d+=8;else if(o=t[h],255===o){const n=t[h+1];if(0===n)h+=2;else{if(255===n){h++;continue}p=!0,o=0,d+=8}}else h++;u=(u<<8|o)>>>0,w+=8}},b=()=>a("a scan cut short"),m=t=>{if(!t)return 0;if(y(),t>w-d)throw b();const o=u>>>w-t&(1<<t)-1;return w-=t,o},M=(t,o)=>t<1<<o-1?t-(1<<o)+1:t,g=t=>{if(!t)throw a("a scan without its Huffman table");y();const o=t[u>>>w-16&65535];if(!o)throw a("bad Huffman code");if(o>>8>w-d)throw b();return w-=o>>8,255&o},A=()=>{const o=w-d;if(o>=8)throw a("bytes left over in a scan");if(E)throw a("an end-of-band run past its interval");if(o&&(u>>>d&(1<<o)-1)!=(1<<o)-1)throw a("bad scan padding");for(;h+1<t.length&&255===t[h]&&255===t[h+1];)h++},U=o=>{if(A(),255!==t[h]||t[h+1]!==208+(7&o))throw a("a restart marker missing or out of order");h+=2,u=0,w=0,d=0,p=!1;for(const t of r)t.component.pred=0;E=0},x=(t,o)=>{if(o>11)throw a("a DC value out of range");const n=o?M(m(o),o):0;if(t.component.pred+=n,t.component.pred>2047||t.component.pred<-2048)throw a("a DC value out of range");return t.component.pred},I=(t,o)=>{if(t>10)throw a("an AC value out of range");const n=M(m(t),t)*(1<<o);if(n>1023||n<-1023)throw a("an AC value out of range");return n};let E=0;const v=1===r.length,C=n.progressive,S=()=>a("coefficients past the "+(C?"band":"block")),J=(t,o,n)=>{if(E>0)E--;else for(let r=e||1;r<=i;){const e=g(t.ac),s=e>>4,l=15&e;if(l){if(r+=s,r>i)throw S();o[n+c[r]]=I(l,f),r++}else{if(s<15){if(s&&!C)throw a("an end-of-band run in a sequential scan");E=(1<<s)-1+(s?m(s):0);break}if(r+=16,r>i+1)throw S()}}},L=C?0===e?0===s?(t,o,n)=>{const r=x(t,g(t.dc))*(1<<f);if(r>2047||r<-2048)throw a("a DC value out of range");o[n]=r}:(t,o,n)=>{m(1)&&(o[n]|=1<<f)}:0===s?J:(t,o,n)=>{const r=1<<f,s=-1<<f;let l=e;if(E<=0)for(;l<=i;l++){const e=g(t.ac);let f=e>>4,h=0;const u=15&e;if(u){if(1!==u)throw a("a bad refinement code");h=m(1)?r:s}else if(15!==f){E=1<<f,f&&(E+=m(f));break}for(;l<=i;l++){const t=n+c[l];if(0!==o[t])m(1)&&0===(o[t]&r)&&(o[t]+=o[t]>=0?r:s);else if(--f<0)break}if(f>=0||h&&l>i)throw a("coefficients past the band");h&&(o[n+c[l]]=h)}if(E>0){for(;l<=i;l++){const t=n+c[l];0!==o[t]&&m(1)&&0===(o[t]&r)&&(o[t]+=o[t]>=0?r:s)}E--}}:(t,o,n)=>{o[n]=x(t,g(t.dc)),J(t,o,n)};for(const t of r)t.component.pred=0;const X=r[0].component,k=v?X.blocksW*X.blocksH:n.mcusX*n.mcusY;for(let t=0;t<k;t++)if(l&&t>0&&t%l===0&&U(t/l-1),v){const o=t/X.blocksW|0,n=t-o*X.blocksW;L(r[0],X.coeffs,64*(o*X.stride+n))}else{const o=t/n.mcusX|0,e=t-o*n.mcusX;for(const t of r){const n=t.component;for(let r=0;r<n.v;r++)for(let i=0;i<n.h;i++)L(t,n.coeffs,64*((o*n.v+r)*n.stride+e*n.h+i))}}if(A(),255===t[h]&&t[h+1]>=208&&t[h+1]<=215){if(!l||k%l||t[h+1]!==208+(k/l-1&7))throw a("a restart marker out of order");for(h+=2;h+1<t.length&&255===t[h]&&255===t[h+1];)h++}if(h<t.length&&!(255===t[h]&&h+1<t.length&&0!==t[h+1]&&255!==t[h+1]))throw a("bytes left over after a scan");return h}const m=(t,o)=>Object.assign(new Error(o),{code:t});function M(o){if(o>t.bytes)throw m("JXL_SIZE","The encoded picture exceeds 16 MiB.")}function g(t){try{return t()}catch(t){if(t instanceof RangeError&&!t.code)throw m("JXL_MEMORY","Not enough memory for this picture.");throw t}}function A(n,r,e){const i=function(t={}){if(null===t||"object"!=typeof t)throw m("JXL_INPUT","Options are an object.");const o=void 0===t.effort?1:t.effort;if(!Number.isInteger(o)||o<1||o>9)throw m("JXL_INPUT","Effort is a whole number from 1 to 9.");return o}(r);if(!(n instanceof Uint8Array&&n.length))throw m("JXL_INPUT","A JPEG is a non-empty Uint8Array.");if(n.length>o.bytes)throw m("JXL_SIZE","A JPEG is at most 16 MiB.");const s=function(t){const o=function*(){let n,r;for(;!(n=g(()=>t.next(o.hurry))).done;)yield r=n.value;return r<1&&(yield 1),o.bytes=function(t){return M(t.length),t}(n.value)}();return o.bytes=null,o.hurry=!1,o}(function*(n,r,e,i){const f=function(t){if(!(t.length>4)||255!==t[0]||216!==t[1])throw a("not a JPEG");const n=[],r=[[],[]],e=new Set,i={count:0,p:[]};let s=null,f=0,m=-1,M=!1,g=1,A=2;const U=o=>t[o]<<8|t[o+1],x=(o,n)=>{for(let r=0;r<n.length;r++)if(t[o+r]!==n.charCodeAt(r))return!1;return!0};for(;;){if(A>=t.length){if(s&&s.scans)break;throw a("truncated")}if(255!==t[A])throw a("marker expected");for(;255===t[A];)A++;const h=t[A++];if(217===h)break;if(216===h)throw a("a second start");if(h>=208&&h<=215)throw a("a restart marker outside a scan");if(1===h)continue;if(A+2>t.length)throw a("truncated");const u=U(A),w=A+2,d=A+u;if(u<2||d>t.length)throw a("bad segment");if(A=d,219===h)for(let o=w;o<d;){const r=t[o]>>4,e=15&t[o];if(r>1||e>3||o+1+(r?128:64)>d)throw a("bad quantisation table");o++;const i=new Int32Array(64);for(let n=0;n<64;n++)if(i[c[n]]=r?U(o):t[o],o+=r?2:1,!i[c[n]])throw a("a zero quantisation step");if(s?.progressive&&s.components.some(t=>t.tq===e&&t.quant&&t.coverage.some(t=>0!==t)&&t.quant.some((t,o)=>t!==i[o])))throw a("a quantisation table changed between progressive scans");n[e]=i}else if(196===h)for(let o=w;o<d;){const n=t[o]>>4,i=15&t[o];if(n>1||i>3||o+17>d)throw a("bad Huffman table");o++;const s=t.subarray(o,o+16);let f=0;for(const t of s)f+=t;if(f>256||o+16+f>d)throw a("bad Huffman table");const c=t.subarray(o+16,o+16+f);r[n][i]=l(s,c),0===n&&(c.some(t=>t>15)?e.add(i):e.delete(i)),o+=16+f}else if(192===h||193===h||194===h){if(s)throw a("two frames");if(d-w<6)throw a("bad frame header");if(8!==t[w])throw a("only 8-bit samples");const n=U(w+1),r=U(w+3),e=t[w+5];if(!r||!n)throw a("a height left to a DNL marker");if(1!==e&&3!==e)throw a("only grey or three components");if(d-w<6+3*e)throw a("bad frame header");if(r>o.edge||n>o.edge)throw a("a picture past "+o.edge+" pixels on a side","JXL_DIMENSIONS");if(r*n>o.pixels)throw a("a picture past "+o.pixels+" pixels","JXL_DIMENSIONS");const i=[];let f=0;for(let o=0,n=w+6;o<e;o++,n+=3){const o=t[n+1]>>4,r=15&t[n+1],e=t[n+2];if(o<1||o>4||r<1||r>4)throw a("bad sampling");if(e>3)throw a("bad quantisation table");if(i.some(o=>o.id===t[n]))throw a("two components with one id");f+=o*r,i.push({id:t[n],h:o,v:r,tq:e,pred:0,coverage:new Int8Array(64).fill(-1)})}if(f>10)throw a("bad sampling");const c=Math.max(...i.map(t=>t.h)),l=Math.max(...i.map(t=>t.v)),u=Math.ceil(r/(8*c)),p=Math.ceil(n/(8*l));for(const t of i)t.blocksW=Math.ceil(Math.ceil(r*t.h/c)/8),t.blocksH=Math.ceil(Math.ceil(n*t.v/l)/8),t.stride=u*t.h,t.rows=p*t.v,t.coeffs=new Int16Array(t.stride*t.rows*64);s={progressive:194===h,width:r,height:n,components:i,hmax:c,vmax:l,mcusX:u,mcusY:p,scans:0}}else{if(195===h||h>=197&&h<=199||h>=201&&h<=203||h>=205&&h<=207)throw a("lossless, hierarchical or arithmetic coding");if(221===h){if(u<4)throw a("bad segment");f=U(w)}else{if(220===h)throw a("a DNL marker");if(218===h){if(!s)throw a("a scan before the frame");const o=t[w],i=[];if(o<1||o>4||w+1+2*o+3>d)throw a("bad scan header");let c=w+1;for(let e=0;e<o;e++,c+=2){const o=s.components.find(o=>o.id===t[c]);if(!o)throw a("a scan of an unknown component");if(i.some(t=>t.component===o))throw a("a component twice in one scan");if(o.quant||(o.quant=n[o.tq]),!o.quant)throw a("a scan without its quantisation table");i.push({component:o,dc:r[0][t[c+1]>>4],ac:r[1][15&t[c+1]],dcSlot:t[c+1]>>4})}const l=t[c],h=t[c+1],u=t[c+2]>>4,p=15&t[c+2];if((!s.progressive||0===l&&0===u)&&i.some(t=>e.has(t.dcSlot)))throw a("bad Huffman table");A=b(t,d,s,i,l,h,u,p,f),s.scans++}else if(224===h)x(w,"JFIF")&&(M=!0);else if(225===h)1===g&&x(w,"Exif")&&(g=y(t,w+6,d));else if(226===h){if(u>=16&&x(w,"ICC_PROFILE\0")){const o=t[w+12],n=t[w+13];if(!o||!n||o>n||i.count&&i.count!==n||i.p[o])throw a("a colour profile cut short");i.count=n,i.p[o]=t.subarray(w+14,d)}}else 238===h&&u>=14&&x(w,"Adobe")&&(m=t[w+11])}}}if(!s||!s.scans)throw a("no picture");for(const t of s.components)for(let o=0;o<64;o++)if(0!==t.coverage[o])throw a(s.progressive?"a progressive JPEG whose scans do not finish every coefficient":"a component without its scan");let I="srgb";if(i.count){const t=[];for(let o=1;o<=i.count;o++){if(!i.p[o])throw a("a colour profile cut short");t.push(i.p[o])}const o=new Uint8Array(t.reduce((t,o)=>t+o.length,0));for(let n=0,r=0;n<t.length;r+=t[n].length,n++)o.set(t[n],r);if(I=function(t){const o=t.length,n=o=>t[o]<<8|t[o+1],r=n=>n+4<=o?(t[n]<<24|t[n+1]<<16|t[n+2]<<8|t[n+3])>>>0:0,e=n=>n+4<=o?String.fromCharCode(t[n],t[n+1],t[n+2],t[n+3]):"",i=t=>(0|r(t))/65536,s=r(128);if(o<132||132+12*s>o||"acsp"!==e(36)||"XYZ "!==e(20))return null;const f=t=>{for(let n=0;n<s;n++){const i=132+12*n,s=r(i+4);if(e(i)===t)return s+r(i+8)<=o?s:-1}return-1},c=t=>{const s=f(t);if(s<0)return!1;if("para"===e(s)){const t=n(s+8),o=[1,3,4,5,7][t];if(3!==t&&4!==t)return!1;for(let t=0;t<o;t++)if(Math.abs(i(s+12+4*t)-(t<5?d[t]:0))>(t?.002:.01))return!1;return!0}const c="curv"===e(s)?r(s+8):0;if(c<2||s+12+2*c>o)return!1;for(let t=0;t<c;t++)if(Math.abs(n(s+12+2*t)/65535-p(t/(c-1)))>.001)return!1;return w.every((t,o)=>{const r=o*(c-1)/16,e=Math.floor(r),i=Math.min(c-1,e+1),f=r-e;return Math.abs((n(s+12+2*e)*(1-f)+n(s+12+2*i)*f)/65535-t)<=.001})};if(["A2B0","A2B1","A2B2"].some(t=>f(t)>=0))return null;if("GRAY"===e(16))return c("kTRC")?"srgb":null;if("RGB "!==e(16)||!["rTRC","gTRC","bTRC"].every(c))return null;const a=["rXYZ","gXYZ","bXYZ"].flatMap(t=>{const o=f(t);return o>=0&&"XYZ "===e(o)?[i(o+8),i(o+12),i(o+16)]:[9,9,9]}),l=t=>t.every((t,o)=>Math.abs(a[o]-t)<=.004);return l(h)?"srgb":l(u)?"display-p3":null}(o),!I)throw a("a colour profile other than sRGB or Display P3 (decode the picture and encode its pixels)")}let E=!1;return 3!==s.components.length||M||(E=m>=0?0===m:82===s.components[0].id&&71===s.components[1].id&&66===s.components[2].id),{width:s.width,height:s.height,components:s.components,progressive:s.progressive,ycbcr:!E,orientation:g,colorSpace:I}}(n);!function(o,n,r=t){if(!Number.isInteger(o)||!Number.isInteger(n)||o<1||n<1)throw m("JXL_INPUT","A picture is at least one pixel wide and high, in whole pixels.");if(o>r.edge||n>r.edge)throw m("JXL_DIMENSIONS","A picture is at most "+r.edge+" pixels on a side.");if(o*n>r.pixels)throw m("JXL_DIMENSIONS","A picture is at most "+r.pixels.toLocaleString("en-US")+" pixels.")}(f.width,f.height,o);const M=f.orientation>=5;return(t=>{Object.assign(s,t)})({width:M?f.height:f.width,height:M?f.width:f.height,orientation:f.orientation}),yield*i(f,r)}(n,i,0,e));return s}const U=256;function x(t,o){t.o([[9,1],[13,1],[18,1],[30,1]],o)}const I=[1,2,3,4,0,5,17,6,16,7,8,9,10,11,12,13,14,15],E=[2,4,3,2,2,4],v=[0,7,3,2,1,15];function C(t,o){const n=new Uint8Array(t.length),r=[];for(let o=0;o<t.length;o++)t[o]>0&&r.push(o);if(!r.length)return n;if(1===r.length)return n[r[0]]=1,n;for(let e=1;;e*=2){const i=r.map(o=>({w:Math.max(t[o],e),m:o,l:null,r:null})).sort((t,o)=>t.w-o.w||t.m-o.m),s=[];let f=0,c=0;const a=()=>f<i.length&&(c>=s.length||i[f].w<=s[c].w)?i[f++]:s[c++];for(;i.length-f+(s.length-c)>1;){const t=a(),o=a();s.push({w:t.w+o.w,m:-1,l:t,r:o})}const l=[[a(),0]];let h=0;for(;l.length;){const[t,o]=l.pop();t.m>=0?(n[t.m]=o,o>h&&(h=o)):l.push([t.l,o+1],[t.r,o+1])}if(h<=o)return n}}function S(t,o,n,r,e,i){const s=[];for(;s.push(r&e),r>>=i;)r--;for(let r=s.length-1;r>=0;r--)t.push(n),o.push(s[r])}function J(t,o){let n=0;for(let r=0;r<o;r++)n=n<<1|1&t,t>>=1;return n}function L(t){const o=new Uint32Array(17),n=new Uint32Array(17),r=new Uint16Array(t.length);for(const n of t)o[n]++;o[0]=0;for(let t=1,r=0;t<=16;t++)r=r+o[t-1]<<1,n[t]=r;for(let o=0;o<t.length;o++)t[o]&&(r[o]=J(n[t[o]]++,t[o]));return r}function X(t){const o=[];for(let n=0;n<t.length;n++)t[n]>0&&o.push(n);const n=o.length,r=o[n-1]+1||1,e=n<=4?new Uint8Array(r):C(t,15).subarray(0,r);let i=0;if(n<=4){o.sort((o,n)=>t[n]-t[o]||o-n),i=4===n&&t[o[0]]>t[o[2]]+t[o[3]]?1:0;const r=n<2?[0]:2===n?[1,1]:3===n?[1,2,2]:i?[1,2,3,3]:[2,2,2,2];o.forEach((t,o)=>{e[t]=r[o]}),o.sort((t,o)=>e[t]-e[o]||t-o)}return{M:r,A:e,U:L(e),I:n&&n<=4?o:null,C:i}}function k(t,o){if(o.M<=1)return;if(o.I){const n=i(o.M-1)+1;t.write(2,1),t.write(2,o.I.length-1);for(const r of o.I)t.write(n,r);return void(4===o.I.length&&t.write(1,o.C))}const n=o.A,r=[],e=[];let s=n.length-1;for(;s>0&&!n[s];)s--;let f=8;for(let t=0;t<=s;){const o=n[t];let i=1;for(;t+i<=s&&n[t+i]===o;)i++;let c=i;if(o&&o!==f&&(r.push(o),e.push(0),c--,f=o),c<3)for(let t=0;t<c;t++)r.push(o),e.push(0);else S(r,e,o?16:17,c-3,o?3:7,o?2:3);t+=i}const c=new Uint32Array(18);for(const t of r)c[t]++;let a=0;for(const t of c)t&&a++;1===a&&(c[r[0]?0:1]=c[r[0]]);const l=C(c,5),h=L(l);let u=18;for(;u>0&&!l[I[u-1]];)u--;t.write(2,0);for(let o=0;o<u;o++){const n=l[I[o]];t.write(E[n],v[n])}for(let o=0;o<r.length;o++){const n=r[o];t.write(l[n],h[n]),16===n?t.write(2,e[o]):17===n&&t.write(3,e[o])}}function N(t,o=0,n=0){return{split:t,S:o,J:n,L:1<<t}}function O(t,o,n=15){t.write(s(n+1),o.split),o.split!==n&&(t.write(s(o.split+1),o.S),t.write(s(o.split-o.S+1),o.J))}function T(t,o,n){if(o<t.L)return n[0]=o,n[1]=0,void(n[2]=0);const r=i(o),e=o-(1<<r),s=r-t.S-t.J;n[0]=t.L+(r-t.split<<t.S+t.J|e>>r-t.S<<t.J|e&(1<<t.J)-1),n[1]=s,n[2]=o>>t.J&(1<<s)-1}function _(t,o,n,r=0){T(t,o,D),n[r+D[0]]++}const D=[0,0,0];function P(t,o,n,r,e=0){T(n,r,D);const i=e+D[0];t.write(o.A[i],o.U[i]),D[1]&&t.write(D[1],D[2])}function G(t,o){let n=0;for(const t of o)n=Math.max(n,t);const r=n?s(n+1):0;if(r<=3){if(t.write(1,1),t.write(2,r),r)for(const n of o)t.write(r,n);return}t.write(1,0),t.write(1,0);const e=N(8),i=N(4),f=o.length>=64,c=new Uint32Array(f?257:n+1),a=[];for(let t=0;t<o.length;){let n=1;for(;f&&t+n<o.length&&o[t+n]===o[t];)n++;if(n>=8)a.push([o[t],n-1]),c[o[t]]++,_(i,n-1-7,c,224),t+=n;else{for(let r=0;r<n;r++)a.push([o[t],0]),c[o[t]]++;t+=n}}const l=X(c);if(!f){R(t,{X:new Uint8Array(1),k:[{config:e,code:l}]});for(const[o]of a)P(t,l,e,o);return}const h=new Uint32Array(1);h[0]=1,R(t,{N:{O:224,minLength:7,T:i},X:new Uint8Array([1,0]),k:[{config:N(0),code:X(h)},{config:e,code:l}]});for(const[o,n]of a)P(t,l,e,o),n&&P(t,l,i,n-7,224)}function R(t,{N:o=null,X:n,k:r}){t.write(1,o?1:0),o&&(t.o([[0,224],[0,512],[0,4096],[15,8]],o.O),t.o([[0,3],[0,4],[2,5],[8,9]],o.minLength),O(t,o.T,8)),n.length>1&&G(t,n),t.write(1,1);for(const o of r)O(t,o.config,15);for(const o of r){const n=o.code.M-1;if(n){const o=i(n);t.write(1,1),t.write(4,o),t.write(o,n-(1<<o))}else t.write(1,0)}for(const o of r)k(t,o.code)}const q=N(4),B=Object.freeze({O:224,minLength:7,T:q}),Z=N(0);function j(t,o){const n=[[3,0],[6,8],[10,72],[13,1096]];if("rct"===o.type)t.write(2,0),t.o(n,o._),t.o([[0,6],[2,0],[4,2],[6,10]],o.D??6);else if("palette"===o.type)t.write(2,1),t.o(n,o._),t.o([[0,1],[0,3],[0,4],[13,1]],o.P),t.o([[8,0],[10,256],[12,1280],[16,5376]],o.G),t.o([[0,0],[8,1],[10,257],[16,1281]],o.R??0),t.write(4,o.q??0);else{if("squeeze"!==o.type)throw new Error("unknown transform "+o.type);{t.write(2,2);const r=o.B??[];t.o([[0,0],[4,1],[6,9],[8,41]],r.length);for(const o of r)t.write(1,o.Z?1:0),t.write(1,o.j?1:0),t.o(n,o._),t.o([[0,1],[0,2],[0,3],[4,4]],o.P)}}}function z(t,o,n,r,i,s){const f=s.q,c=s.offset,a=s.Y,l=Z,h=q;let u=0;const w=(n,r=l,e=0)=>{t?P(t,o,r,n,e):_(r,n,o,e)},d=()=>{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 r=n[t];let i;1===a?i=r-o-c:(i=Math.round((r-o-c)/a),n[t]=o+c+i*a),0!==i?(d(),w(e(i))):u++};for(let t=0,o=0;t<i;t++)for(let e=0;e<r;e++,o++){let i;if(0===f)i=0;else{const s=e?n[o-1]:t?n[o-r]:0,c=t?n[o-r]:s;if(1===f)i=s;else if(2===f)i=c;else if(3===f)i=(s+c)/2|0;else{const a=s+c-(e&&t?n[o-r-1]:s);if(5===f){const t=s<c?s:c,o=s<c?c:s;i=a<t?t:a>o?o:a}else{if(4!==f)throw new Error("predictor "+f+" is not coded here");i=Math.abs(a-s)<Math.abs(a-c)?s:c}}}p(o,i)}d()}const Y=[N(0),N(1),N(2),N(3),N(4),N(4,1,1),N(5,1,1),N(2,0,1),N(3,0,1)],H=N(4,1,1);class F{constructor(t){this.H=t,this.small=new Uint32Array(64*t),this.F=Array.from({length:t},()=>null),this.K=new Float64Array(t)}add(t,o){if(this.K[t]++,o<64)return void this.small[64*t+o]++;const n=this.F[t]||(this.F[t]=new Map);n.set(o,(n.get(o)||0)+1)}V(t,o,n=64){const r=new Uint32Array(n);for(const n of t){const t=64*n;for(let n=0;n<64;n++)this.small[t+n]&&(r[Q(o,n)]+=this.small[t+n]);const e=this.F[n];if(e)for(const[t,n]of e)r[Q(o,t)]+=n}return r}W(t,o){let n=$(this.V(t,o));for(const r of t){const t=64*r;for(let r=o.L;r<64;r++)this.small[t+r]&&(n+=this.small[t+r]*(i(r)-o.S-o.J));const e=this.F[r];if(e)for(const[t,r]of e)n+=r*(i(t)-o.S-o.J)}return n}}const K=[0,0,0];function Q(t,o){return T(t,o,K),K[0]}const V=[1];for(let t=1;t<32;t++)V.push(V[t-1]/2);function W(t){let o=0;for(t>=2&&t<4294967296&&(o=31-Math.clz32(t),t*=V[o]);t>=1.4142135623730951;)t/=2,o++;for(;t<.7071067811865476;)t*=2,o--;const n=(t-1)/(t+1),r=n*n;let e=n,i=n;for(let t=3;t<30;t+=2)i+=(e*=r)/t;return o+2.885390081777927*i}function $(t){let o=0,n=0;for(const n of t)o+=n;for(const r of t)r&&(n+=r*W(o/r));return n}function tt(t,{maxClusters:o=48,newClusterCost:n=320}={}){const r=[];for(let o=0;o<t.H;o++)t.K[o]>0&&r.push(o);r.sort((o,n)=>t.K[n]-t.K[o]);const e=[];for(const i of r){const r=t.V([i],H),s=$(r);let f=-1,c=1/0;for(let t=0;t<e.length;t++){const o=e[t],n=new Uint32Array(64);for(let t=0;t<64;t++)n[t]=o.$[t]+r[t];const i=$(n)-o.tt-s;i<c&&(c=i,f=t)}if(f<0||e.length<o&&c>n)e.push({H:[i],$:r,tt:s});else{const t=e[f];t.H.push(i);for(let o=0;o<64;o++)t.$[o]+=r[o];t.tt=$(t.$)}}e.length||e.push({H:[],$:new Uint32Array(64),tt:0});const i=new Uint8Array(t.H);let s=2*t.H;const f=e.map((o,n)=>{for(const t of o.H)i[t]=n;let r=Y[0],e=1/0;for(const n of Y){const i=t.W(o.H,n);i<e&&(e=i,r=n)}const f=X(t.V(o.H,r,128));return s+=e+3*f.M+40,{config:r,code:f}});return{X:i,k:f,write:(t,o,n)=>{const r=f[i[o]];P(t,r.code,r.config,n)},tt:s}}const ot=[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],nt=[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],rt=c.map(t=>(7&t)<<3|t>>3),et=(t,o,n)=>2*(nt[t]+ot[o])+n;function it(t,o,n){const r=[];if(o>0){const n=new Uint32Array(2048);let e=0;for(const o of t)n[Math.min(2047,Math.max(0,o+1024))]++,e++;let i=0,s=e/(o+1);for(let t=0;t<2048&&r.length<o;t++)i+=n[t],i>s&&(r.push(t-1025),s=e*(r.length+1)/(o+1))}const e=r.length+1,i=new Uint8Array(39*e);for(let t=0;t<e;t++)for(let o=0;o<13;o++)i[(0+o)*e+t]=t,i[(13+o)*e+t]=n?e:e+(t>>1),i[(26+o)*e+t]=n?e:e+(e-1>>1)+1+(t>>1);let s=0;for(const t of i)s=Math.max(s,t+1);return{thresholds:r,ot:e,map:i,nt:s,rt:t=>{let o=0;for(const n of r)t>n&&o++;return o},et:(t,o)=>i[13*(t<2?1^t:2)*e+o],it:(t,o)=>(Math.min(t,64)<8?Math.min(t,64):4+(Math.min(t,64)>>1))*s+o,st:t=>37*s+458*t,H:495*s}}function st(t,o,n){const r=o.map(()=>function(t,o=0,n=1){return{q:t,offset:o,Y:n,context:-1}}(n)),i=r.map(()=>new Uint32Array(257));o.forEach((t,o)=>z(null,i[o],t.data,t.w,t.h,r[o])),function(t,{ft:o=!0,ct:n=[]}={}){t.write(1,o?1:0),t.write(1,1),t.o([[0,0],[0,1],[4,2],[8,18]],n.length);for(const o of n)j(t,o)}(t,{ft:!1,ct:[]});const s=function(t,o){const n=[o],r=[],i=[];for(const t of n){if(t.left){r.push(t.lt+1,e(t.ht)),n.push(t.left,t.right);continue}t.context=i.length,i.push(t);const o=1===t.Y?0:31-Math.clz32(t.Y&-t.Y);r.push(0,t.q,e(t.offset),o,(t.Y>>o)-1)}const s=new Uint32Array(64);for(const t of r)_(Z,t,s);const f=X(s);R(t,{X:new Uint8Array(6),k:[{config:Z,code:f}]});for(const o of r)P(t,f,Z,o);return i}(t,function(t){return function(t,o){const n=(r,e)=>{if(r===e)return o?t[r].ut:t[r];const i=r+e>>1;return function(t,o,n,r){return{lt:t,ht:o,left:n,right:r}}(o,o?t[i].wt:i,n(i+1,e),n(r,i))};return n(0,t.length-1)}(t,0)}(r)),f=function(t,o,n,r=t=>t.context){const e=new Uint8Array(o.length+1);for(const t of o)e[t.context]=r(t)+1;const i=new Uint32Array(2);i[1]=1;const s=[{config:Z,code:X(i)}],f=Math.max(...e);for(let t=0;t<f;t++)s.push({config:Z,code:X(n[t])});return R(t,{N:B,X:e,k:s}),s}(t,s,i,t=>r.indexOf(t));o.forEach((o,n)=>z(t,f[n+1].code,o.data,o.w,o.h,r[n]))}function*ft(t,o={}){const{width:n,height:i,components:c,alpha:l}=t,h=1===c.length,u=t.quantScale||1;let w=t.quantFieldBase||1;for(;w/(2040*u)<1/16384;)w*=2;const d=t.ycbcr||h,p=h?[0,0,0]:d?[1,0,2]:[0,1,2],y=p.map(t=>c[t]),b=o.orders?p.map(t=>o.orders[t].map(t=>rt[t])):[rt,rt,rt],m=t=>1===t?0:2===t?1:-1,g=y.map(t=>m(t.h)),A=y.map(t=>m(t.v));if(g.includes(-1)||A.includes(-1))throw a("sampling factors other than one or two");const I=Math.max(...g),E=Math.max(...A),v=t=>I-g[t],C=t=>E-A[t];if(!d&&(I||E))throw a("an RGB JPEG with subsampling");const S=Math.ceil(n/(8<<I))<<I,J=Math.ceil(i/(8<<E))<<E,L=Math.ceil(n/U),k=Math.ceil(i/U),O=L*k,T=Math.ceil(S/256),D=T*Math.ceil(J/256),q=1===O,B=(t,o,n)=>{const r=y[t];return h&&1!==t?-1:64*(n*r.stride+o)},Z=t=>{let o=1024;for(;t%o;)o>>=1;return o},j=y.map(t=>d?1:t.quant[0]/Z(t.quant[0])),z=y.map(t=>d?0:1024/Z(t.quant[0])),Y=new Int32Array(S*J);for(let t=0;t<J;t++)for(let o=0;o<S;o++)Y[t*S+o]=y[1].coeffs[B(1,o,t)]*j[1]+z[1];const H=y[1].quant,K=(H[8]+H[16]+H[24]+H[32]+H[40])/u,Q=S*J,V=Q<256?0:Math.max(1,Math.min(7,s(Q)-s(K)-7));let W=it(Y,V,h);const $=new r(256);if(function(t,o,n,r,e,{xyb:i=!1,orientation:s=1,colorSpace:f}={}){t.write(16,2815),t.write(1,0),x(t,n),t.write(3,0),x(t,o),t.write(1,0);const c=1!==s;t.write(1,c?1:0),c&&t.write(6,s-1),t.write(4,8),e?t.write(3,5):t.write(2,0),t.write(1,i?1:0),3===r&&"display-p3"===f?t.write(21,903568):3===r?t.write(1,1):t.write(15,14100),c&&t.write(1,1),t.write(2,0),t.write(1,1),t.i()}($,n,i,h?1:3,!!l,{orientation:t.orientation,colorSpace:t.colorSpace}),$.write(1,0),$.write(2,0),$.write(1,0),$.write(2,2),$.write(8,111),$.write(1,d?1:0),d)for(let t=0;t<3;t++)$.write(2,0===g[t]&&0===A[t]?0:1===g[t]&&1===A[t]?1:1===g[t]?2:3);$.write(2,0),l&&$.write(2,0),function(t,o){t.write(o?7:5,0),t.write(11,1)}($,l);const ot=[0,1,2].map(()=>new Int32Array(1024)),nt=(t,o)=>{const n=t%L*32,r=32*(t/L|0),i=Math.min(32,S-n),s=Math.min(32,J-r);for(let t=0;t<s;t++)for(let s=0;s<i;s++)for(const i of[1,0,2]){const f=v(i),c=C(i),a=s>>f,l=t>>c;if(a<<f!==s||l<<c!==t)continue;const h=B(i,(n>>f)+a,(r>>c)+l),u=y[i].coeffs;let w=0;if(h>=0)for(let t=1;t<64;t++)u[h+t]&&w++;const d=ot[i],p=0===a?0===l?32:d[32*(l-1)]:0===l?d[32*l+a-1]:d[32*(l-1)+a]+d[32*l+a-1]+1>>1;d[32*l+a]=w;const m=W.et(i,W.rt(Y[(r+t)*S+n+s]));o(W.it(p,m),w);const M=W.st(m);let g=w>4?0:1,A=w;for(let t=1;t<64&&A;t++){const n=u[h+b[i][t]];o(M+et(A,t,g),e(n)),g=n?1:0,A-=g}}},ft=(V>0?3:2)*O+D;let ct=0;const at=function*(t){W=t;const n=new F(t.H);for(let t=0;t<O;t++)nt(t,(t,o)=>n.add(t,o)),yield++ct/ft;return(o.coding||tt)(n,o.clusters)};let lt=yield*at(W);if(V>0){const t=W,o=it(Y,0,h),n=yield*at(o);n.tt<=lt.tt?lt=n:W=t}const ht=Array.from({length:q?1:2+D+O},()=>null),ut=t=>ht[q?0:t]||(ht[q?0:t]=new r(4096)),wt=new Uint32Array(ht.length);let dt=0;const pt=t=>{t=q?0:t;const o=Math.ceil(ht[t].u/8);dt+=o-wt[t],wt[t]=o,M(dt)},yt=ut(0);yt.write(1,0);const bt=(t,o)=>{const r=t*U,e=o*U,s=Math.min(U,n-r),f=Math.min(U,i-e),c=new Int32Array(s*f);for(let t=0;t<f;t++)for(let o=0;o<s;o++)c[t*s+o]=l[4*((e+t)*n+r+o)+3];return{w:s,h:f,data:c}};for(let t=0;t<3;t++)yt.write(16,f(y[t].quant[0]/j[t]/(2040*u)*128));yt.write(2,3),yt.write(16,57343),yt.write(2,1),yt.write(5,0),yt.write(1,0),yt.write(4,0),yt.write(4,W.thresholds.length);for(const t of W.thresholds)yt.o([[4,0],[8,16],[16,272],[32,65808]],e(t));yt.write(4,0),yt.write(4,0),G(yt,W.map),yt.write(1,0),yt.write(2,0),yt.write(16,0),yt.write(16,0),yt.write(8,128),yt.write(8,128),yt.write(1,0),l&&st(yt,[q?bt(0,0):{w:0,h:0,data:new Int32Array(0)}],5),pt(0);for(let o=0;o<D;o++){const n=ut(1+o),r=o%T*256,e=256*(o/T|0),i=Math.min(256,S-r),f=Math.min(256,J-e);n.write(2,0),st(n,[1,0,2].map(t=>{const o=i>>v(t),n=f>>C(t),s=new Int32Array(o*n),c=j[t],a=z[t];for(let i=0;i<n;i++)for(let n=0;n<o;n++){const f=B(t,(r>>v(t))+n,(e>>C(t))+i);s[i*o+n]=f<0?0:y[t].coeffs[f]*c+a}return{w:o,h:n,data:s}}),5);const c=i*f,a=s(c);a&&n.write(a,c-1);const l=i+7>>3,h=f+7>>3,u=new Int32Array(2*c).fill(w-1,c);if(t.quantFields)for(let o=0;o<f;o++)for(let n=0;n<i;n++)u[c+o*i+n]=t.quantFields[(e+o)*S+r+n]-1;st(n,[{w:l,h:h,data:new Int32Array(l*h)},{w:l,h:h,data:new Int32Array(l*h)},{w:c,h:2,data:u},{w:i,h:f,data:new Int32Array(c)}],w>1?5:0),pt(1+o),yield++ct/ft}const mt=ut(1+D);mt.write(1,0),mt.write(3,7),mt.write(16,f(w/(2040*u))),st(mt,[0,1,2].map(t=>{const o=new Int32Array(64),n=y[t].quant;for(let t=0;t<8;t++)for(let r=0;r<8;r++)o[8*r+t]=n[8*t+r];return{w:8,h:8,data:o}}),5);for(let t=1;t<17;t++)mt.write(3,0);if(O>1&&mt.write(s(O),0),o.orders){mt.o([[0,95],[0,19],[0,0],[13,0]],1);const t=[];for(const n of p){const r=Array.from({length:64},(t,o)=>o),e=o.orders[n].map(t=>{const o=r.indexOf(t);return r.splice(o,1),o});let i=64;for(;i>1&&!e[i-1];)i--;t.push(i-1,...e.slice(1,i))}const n=N(0),r=new Uint32Array(64);for(const o of t)_(n,o,r);const e=X(r);R(mt,{X:new Uint8Array(8),k:[{config:n,code:e}]});for(const o of t)P(mt,e,n,o)}else mt.write(2,2);lt.writeHistograms?lt.writeHistograms(mt):R(mt,{X:lt.X,k:lt.k}),pt(1+D);for(let t=0;t<O;t++){const o=ut(2+D+t);nt(t,(t,n)=>lt.write(o,t,n)),lt.flush&&lt.flush(o),l&&!q&&st(o,[bt(t%L,Math.floor(t/L))],5),pt(2+D+t),yield++ct/ft}return function(t,o){const n=o.map(t=>t.length);!function(t,o){t.write(1,0),t.i();for(const n of o)t.o([[10,0],[14,1024],[22,17408],[30,4211712]],n);t.i()}(t,n);const r=t.finish(),e=r.length+n.reduce((t,o)=>t+o,0);M(e);const i=new Uint8Array(e);i.set(r);let s=r.length;for(const t of o)i.set(t,s),s+=t.length;return i}($,function(t){return t.map(t=>t?t.finish():new Uint8Array(0))}(ht))}function*ct(t,o,n){if(o<3&&!n)return yield*ft(t);let r=n;try{const n=ft(t);let e,i;for(;!(e=n.next()).done;)if(i=yield e.value/2,i&&r&&e.value<1)return n.return(),r;if((!r||e.value.length<r.length)&&(r=e.value),i||o<3)return r;const s=o<4?1:3,f=ft(t,{clusters:{maxClusters:32,newClusterCost:160}});for(;!(e=f.next()).done;)if(i=yield.5+e.value/(2*s),i&&e.value<1)return f.return(),r;if(e.value.length<r.length&&(r=e.value),i||o<4)return r;const a=function*(t){const o=c.map(t=>(7&t)<<3|t>>3),n=t.map(()=>new Uint32Array(64)),r=t.reduce((t,o)=>t+o.coeffs.length,0);let e=0;for(let i=0;i<t.length;i++){const{coeffs:s}=t[i],f=n[i];for(let t=0;t<s.length;t+=65536){const n=Math.min(t+65536,s.length);for(let r=t;r<n;r+=64)for(let t=1;t<64;t++)s[r+o[t]]&&f[t]++;if(e+=n-t,yield e/r)return null}}return n.map(t=>[0,...Array.from({length:63},(t,o)=>o+1).sort((o,n)=>Math.floor(t[n]/8)-Math.floor(t[o]/8)||o-n)])}(t.components);for(;!(e=a.next()).done;)if(yield.5+(1+e.value)/6)return a.return(),r;const l=ft(t,{orders:e.value});for(;!(e=l.next()).done;)if(i=yield.5+(2+e.value)/6,i&&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 at(t,o){return function(t){return function(t){let o;for(;!(o=t.next()).done;);o.value}(t),{bytes:t.bytes,width:t.width,height:t.height,orientation:t.orientation}}(lt(t,o))}function lt(t,o){return A(t,o,ct)}export{o as LIMITS,at as transcode,lt as transcodeSteps};
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
+ const t=Object.freeze({bytes:16777216,pixels:24e6,edge:16384}),o=Object.freeze({bytes:16777216,pixels:64e6,edge:16384}),n=[1];for(let t=1;t<=32;t++)n.push(2*n[t-1]);class r{constructor(t=4096){this.bytes=new Uint8Array(t),this.at=0,this.t=0,this.pending=0}write(t,o){if(!(o>=0&&o<n[t]))throw new Error("bit write out of range: "+t+" bits, "+o);let r=this.t+o*(1<<this.pending),e=this.pending+t;this.at+5>=this.bytes.length&&this.grow();const i=this.bytes;for(;e>=8;)i[this.at++]=255&r,r=Math.floor(r/256),e-=8;this.t=r,this.pending=e}o(t,o){for(let r=0;r<4;r++){const[e,i]=t[r];if(o>=i&&o-i<n[e])return this.write(2,r),void(e&&this.write(e,o-i))}throw new Error("U32 value out of range: "+o)}i(){this.pending&&(this.at+1>=this.bytes.length&&this.grow(),this.bytes[this.at++]=this.t,this.t=0,this.pending=0)}get u(){return 8*this.at+this.pending}grow(o=0){const n=t.bytes+64;if(this.at+o+16>n)throw Object.assign(new Error("The encoded picture exceeds 16 MiB."),{code:"JXL_SIZE"});const r=new Uint8Array(Math.min(n,Math.max(2*this.bytes.length,this.at+o+16)));r.set(this.bytes.subarray(0,this.at)),this.bytes=r}append(t){if(this.at+t.at+8>=this.bytes.length&&this.grow(t.at+8),this.pending)for(let o=0;o<t.at;o++)this.write(8,t.bytes[o]);else this.bytes.set(t.bytes.subarray(0,t.at),this.at),this.at+=t.at;t.pending&&this.write(t.pending,t.t)}finish(){const t=this.at+(this.pending?1:0),o=this.bytes.slice(0,t);return this.pending&&(o[this.at]=this.t),o}}function e(t){return t>=0?2*t:2*-t-1}function i(t){return 31-Math.clz32(t)}function s(t){return t<=1?0:32-Math.clz32(t-1)}function f(t){if(0===t)return 0;const o=t<0?32768:0;if(!((t=Math.abs(t))<65520))throw new Error("half float out of range: "+t);let n=0,r=t;for(;r>=2;)r/=2,n++;for(;r<1;)r*=2,n--;n<-14?(r=16384*t,n=-15):r-=1;let e=Math.round(1024*r);return 1024===e&&(e=0,n++),o|n+15<<10|e}const c=[0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63];function a(t,o="JXL_JPEG"){return Object.assign(new Error("JPEG: "+t),{code:o})}function l(t,o){const n=new Uint16Array(65536);let r=0,e=0;for(let i=1;i<=16;i++){for(let s=0;s<t[i-1];s++){const t=o[e++],s=r<<16-i,f=1<<16-i;if(s+f>=65536)throw a("bad Huffman table");n.fill(i<<8|t,s,s+f),r++}r<<=1}return n}const h=[.436,.2225,.0139,.3851,.7169,.0971,.143,.0606,.7139],u=[.5151,.2412,-.001,.292,.6922,.0419,.1571,.0666,.7841],w=[0,.00516,.01435,.02934,.05088,.07958,.11602,.16068,.21404,.27652,.34851,.43039,.52252,.62523,.73884,.86367,1],d=[2.4,1/1.055,.055/1.055,1/12.92,.04045];function p(t){if(t<=.04045)return t/12.92;const o=(t+.055)/1.055;let n=1;for(let t=0;t<24;t++)n=(4*n+o/(n*n*n*n))/5;return o*o*n*n}function y(t,o,n){const r=77===t[o]&&77===t[o+1];if(!r&&(73!==t[o]||73!==t[o+1]))return 1;const e=o=>o+2>n?0:r?t[o]<<8|t[o+1]:t[o]|t[o+1]<<8;if(42!==e(o+2))return 1;const i=o+(o=>o+4>n?0:r?(t[o]<<24|t[o+1]<<16|t[o+2]<<8|t[o+3])>>>0:(t[o]|t[o+1]<<8|t[o+2]<<16|t[o+3]<<24)>>>0)(o+4),s=e(i);for(let t=0,o=i+2;t<s&&o+12<=n;t++,o+=12){if(274!==e(o))continue;const t=e(o+8);return t>=1&&t<=8?t:1}return 1}function b(t,o,n,r,e,i,s,f,l){!function(t,o,n,r,e,i){if(!t.progressive){if(0!==n||63!==r||0!==e||0!==i)throw a("a bad spectral selection");for(const t of o){if(t.component.coverage.some(t=>-1!==t))throw a("a component coded twice");t.component.coverage.fill(0)}return}if(i>13||e&&e!==i+1)throw a("a bad successive approximation");if(0===n){if(0!==r)throw a("a bad spectral selection");for(const{component:t}of o){if(t.coverage[0]!==(e||-1))throw a(e?"a refinement scan out of order":"a DC scan twice");t.coverage[0]=i}return}if(1!==o.length)throw a("an interleaved AC scan");if(r>63||n>r)throw a("a bad spectral selection");const{coverage:s}=o[0].component;if(-1===s[0])throw a("an AC scan before the DC scan");for(let t=n;t<=r;t++){if(s[t]!==(e||-1))throw a(e?"a refinement scan out of order":"a band coded twice");s[t]=i}}(n,r,e,i,s,f);let h=o,u=0,w=0,d=0,p=!1;const y=()=>{for(;w<=24;){let o=0;if(p||h>=t.length)p=!0,d+=8;else if(o=t[h],255===o){const n=t[h+1];if(0===n)h+=2;else{if(255===n){h++;continue}p=!0,o=0,d+=8}}else h++;u=(u<<8|o)>>>0,w+=8}},b=()=>a("a scan cut short"),m=t=>{if(!t)return 0;if(y(),t>w-d)throw b();const o=u>>>w-t&(1<<t)-1;return w-=t,o},g=(t,o)=>t<1<<o-1?t-(1<<o)+1:t,M=t=>{if(!t)throw a("a scan without its Huffman table");y();const o=t[u>>>w-16&65535];if(!o)throw a("bad Huffman code");if(o>>8>w-d)throw b();return w-=o>>8,255&o},A=()=>{const o=w-d;if(o>=8)throw a("bytes left over in a scan");if(E)throw a("an end-of-band run past its interval");if(o&&(u>>>d&(1<<o)-1)!=(1<<o)-1)throw a("bad scan padding");for(;h+1<t.length&&255===t[h]&&255===t[h+1];)h++},U=o=>{if(A(),255!==t[h]||t[h+1]!==208+(7&o))throw a("a restart marker missing or out of order");h+=2,u=0,w=0,d=0,p=!1;for(const t of r)t.component.pred=0;E=0},x=(t,o)=>{if(o>11)throw a("a DC value out of range");const n=o?g(m(o),o):0;if(t.component.pred+=n,t.component.pred>2047||t.component.pred<-2048)throw a("a DC value out of range");return t.component.pred},I=(t,o)=>{if(t>10)throw a("an AC value out of range");const n=g(m(t),t)*(1<<o);if(n>1023||n<-1023)throw a("an AC value out of range");return n};let E=0;const v=1===r.length,C=n.progressive,S=()=>a("coefficients past the "+(C?"band":"block")),J=(t,o,n)=>{if(E>0)E--;else for(let r=e||1;r<=i;){const e=M(t.ac),s=e>>4,l=15&e;if(l){if(r+=s,r>i)throw S();o[n+c[r]]=I(l,f),r++}else{if(s<15){if(s&&!C)throw a("an end-of-band run in a sequential scan");E=(1<<s)-1+(s?m(s):0);break}if(r+=16,r>i+1)throw S()}}},L=C?0===e?0===s?(t,o,n)=>{const r=x(t,M(t.dc))*(1<<f);if(r>2047||r<-2048)throw a("a DC value out of range");o[n]=r}:(t,o,n)=>{m(1)&&(o[n]|=1<<f)}:0===s?J:(t,o,n)=>{const r=1<<f,s=-1<<f;let l=e;if(E<=0)for(;l<=i;l++){const e=M(t.ac);let f=e>>4,h=0;const u=15&e;if(u){if(1!==u)throw a("a bad refinement code");h=m(1)?r:s}else if(15!==f){E=1<<f,f&&(E+=m(f));break}for(;l<=i;l++){const t=n+c[l];if(0!==o[t])m(1)&&0===(o[t]&r)&&(o[t]+=o[t]>=0?r:s);else if(--f<0)break}if(f>=0||h&&l>i)throw a("coefficients past the band");h&&(o[n+c[l]]=h)}if(E>0){for(;l<=i;l++){const t=n+c[l];0!==o[t]&&m(1)&&0===(o[t]&r)&&(o[t]+=o[t]>=0?r:s)}E--}}:(t,o,n)=>{o[n]=x(t,M(t.dc)),J(t,o,n)};for(const t of r)t.component.pred=0;const X=r[0].component,k=v?X.blocksW*X.blocksH:n.mcusX*n.mcusY;for(let t=0;t<k;t++)if(l&&t>0&&t%l===0&&U(t/l-1),v){const o=t/X.blocksW|0,n=t-o*X.blocksW;L(r[0],X.coeffs,64*(o*X.stride+n))}else{const o=t/n.mcusX|0,e=t-o*n.mcusX;for(const t of r){const n=t.component;for(let r=0;r<n.v;r++)for(let i=0;i<n.h;i++)L(t,n.coeffs,64*((o*n.v+r)*n.stride+e*n.h+i))}}if(A(),255===t[h]&&t[h+1]>=208&&t[h+1]<=215){if(!l||k%l||t[h+1]!==208+(k/l-1&7))throw a("a restart marker out of order");for(h+=2;h+1<t.length&&255===t[h]&&255===t[h+1];)h++}if(h<t.length&&!(255===t[h]&&h+1<t.length&&0!==t[h+1]&&255!==t[h+1]))throw a("bytes left over after a scan");return h}const m=(t,o)=>Object.assign(new Error(o),{code:t});function g(o){if(o>t.bytes)throw m("JXL_SIZE","The encoded picture exceeds 16 MiB.")}function M(t){try{return t()}catch(t){if(t instanceof RangeError&&!t.code)throw m("JXL_MEMORY","Not enough memory for this picture.");throw t}}function A(n,r,e){const i=function(t={}){if(null===t||"object"!=typeof t)throw m("JXL_INPUT","Options are an object.");const o=void 0===t.effort?1:t.effort;if(!Number.isInteger(o)||o<1||o>9)throw m("JXL_INPUT","Effort is a whole number from 1 to 9.");return o}(r);if(!(n instanceof Uint8Array&&n.length))throw m("JXL_INPUT","A JPEG is a non-empty Uint8Array.");if(n.length>o.bytes)throw m("JXL_SIZE","A JPEG is at most 16 MiB.");const s=function(t){const o=function*(){let n,r;for(;!(n=M(()=>t.next(o.hurry))).done;)yield r=n.value;return r<1&&(yield 1),o.bytes=function(t){return g(t.length),t}(n.value)}();return o.bytes=null,o.hurry=!1,o}(function*(n,r,e,i){const f=function(t){if(!(t.length>4)||255!==t[0]||216!==t[1])throw a("not a JPEG");const n=[],r=[[],[]],e=new Set,i={count:0,p:[]};let s=null,f=0,m=-1,g=!1,M=1,A=2;const U=o=>t[o]<<8|t[o+1],x=(o,n)=>{for(let r=0;r<n.length;r++)if(t[o+r]!==n.charCodeAt(r))return!1;return!0};for(;;){if(A>=t.length){if(s&&s.scans)break;throw a("truncated")}if(255!==t[A])throw a("marker expected");for(;255===t[A];)A++;const h=t[A++];if(217===h)break;if(216===h)throw a("a second start");if(h>=208&&h<=215)throw a("a restart marker outside a scan");if(1===h)continue;if(A+2>t.length)throw a("truncated");const u=U(A),w=A+2,d=A+u;if(u<2||d>t.length)throw a("bad segment");if(A=d,219===h)for(let o=w;o<d;){const r=t[o]>>4,e=15&t[o];if(r>1||e>3||o+1+(r?128:64)>d)throw a("bad quantisation table");o++;const i=new Int32Array(64);for(let n=0;n<64;n++)if(i[c[n]]=r?U(o):t[o],o+=r?2:1,!i[c[n]])throw a("a zero quantisation step");if(s?.progressive&&s.components.some(t=>t.tq===e&&t.quant&&t.coverage.some(t=>0!==t)&&t.quant.some((t,o)=>t!==i[o])))throw a("a quantisation table changed between progressive scans");n[e]=i}else if(196===h)for(let o=w;o<d;){const n=t[o]>>4,i=15&t[o];if(n>1||i>3||o+17>d)throw a("bad Huffman table");o++;const s=t.subarray(o,o+16);let f=0;for(const t of s)f+=t;if(f>256||o+16+f>d)throw a("bad Huffman table");const c=t.subarray(o+16,o+16+f);r[n][i]=l(s,c),0===n&&(c.some(t=>t>15)?e.add(i):e.delete(i)),o+=16+f}else if(192===h||193===h||194===h){if(s)throw a("two frames");if(d-w<6)throw a("bad frame header");if(8!==t[w])throw a("only 8-bit samples");const n=U(w+1),r=U(w+3),e=t[w+5];if(!r||!n)throw a("a height left to a DNL marker");if(1!==e&&3!==e)throw a("only grey or three components");if(d-w<6+3*e)throw a("bad frame header");if(r>o.edge||n>o.edge)throw a("a picture past "+o.edge+" pixels on a side","JXL_DIMENSIONS");if(r*n>o.pixels)throw a("a picture past "+o.pixels+" pixels","JXL_DIMENSIONS");const i=[];let f=0;for(let o=0,n=w+6;o<e;o++,n+=3){const o=t[n+1]>>4,r=15&t[n+1],e=t[n+2];if(o<1||o>4||r<1||r>4)throw a("bad sampling");if(e>3)throw a("bad quantisation table");if(i.some(o=>o.id===t[n]))throw a("two components with one id");f+=o*r,i.push({id:t[n],h:o,v:r,tq:e,pred:0,coverage:new Int8Array(64).fill(-1)})}if(f>10)throw a("bad sampling");const c=Math.max(...i.map(t=>t.h)),l=Math.max(...i.map(t=>t.v)),u=Math.ceil(r/(8*c)),p=Math.ceil(n/(8*l));for(const t of i)t.blocksW=Math.ceil(Math.ceil(r*t.h/c)/8),t.blocksH=Math.ceil(Math.ceil(n*t.v/l)/8),t.stride=u*t.h,t.rows=p*t.v,t.coeffs=new Int16Array(t.stride*t.rows*64);s={progressive:194===h,width:r,height:n,components:i,hmax:c,vmax:l,mcusX:u,mcusY:p,scans:0}}else{if(195===h||h>=197&&h<=199||h>=201&&h<=203||h>=205&&h<=207)throw a("lossless, hierarchical or arithmetic coding");if(221===h){if(u<4)throw a("bad segment");f=U(w)}else{if(220===h)throw a("a DNL marker");if(218===h){if(!s)throw a("a scan before the frame");const o=t[w],i=[];if(o<1||o>4||w+1+2*o+3>d)throw a("bad scan header");let c=w+1;for(let e=0;e<o;e++,c+=2){const o=s.components.find(o=>o.id===t[c]);if(!o)throw a("a scan of an unknown component");if(i.some(t=>t.component===o))throw a("a component twice in one scan");if(o.quant||(o.quant=n[o.tq]),!o.quant)throw a("a scan without its quantisation table");i.push({component:o,dc:r[0][t[c+1]>>4],ac:r[1][15&t[c+1]],dcSlot:t[c+1]>>4})}const l=t[c],h=t[c+1],u=t[c+2]>>4,p=15&t[c+2];if((!s.progressive||0===l&&0===u)&&i.some(t=>e.has(t.dcSlot)))throw a("bad Huffman table");A=b(t,d,s,i,l,h,u,p,f),s.scans++}else if(224===h)x(w,"JFIF")&&(g=!0);else if(225===h)1===M&&x(w,"Exif")&&(M=y(t,w+6,d));else if(226===h){if(u>=16&&x(w,"ICC_PROFILE\0")){const o=t[w+12],n=t[w+13];if(!o||!n||o>n||i.count&&i.count!==n||i.p[o])throw a("a colour profile cut short");i.count=n,i.p[o]=t.subarray(w+14,d)}}else 238===h&&u>=14&&x(w,"Adobe")&&(m=t[w+11])}}}if(!s||!s.scans)throw a("no picture");for(const t of s.components)for(let o=0;o<64;o++)if(0!==t.coverage[o])throw a(s.progressive?"a progressive JPEG whose scans do not finish every coefficient":"a component without its scan");let I="srgb";if(i.count){const t=[];for(let o=1;o<=i.count;o++){if(!i.p[o])throw a("a colour profile cut short");t.push(i.p[o])}const o=new Uint8Array(t.reduce((t,o)=>t+o.length,0));for(let n=0,r=0;n<t.length;r+=t[n].length,n++)o.set(t[n],r);if(I=function(t){const o=t.length,n=o=>t[o]<<8|t[o+1],r=n=>n+4<=o?(t[n]<<24|t[n+1]<<16|t[n+2]<<8|t[n+3])>>>0:0,e=n=>n+4<=o?String.fromCharCode(t[n],t[n+1],t[n+2],t[n+3]):"",i=t=>(0|r(t))/65536,s=r(128);if(o<132||132+12*s>o||"acsp"!==e(36)||"XYZ "!==e(20))return null;const f=t=>{for(let n=0;n<s;n++){const i=132+12*n,s=r(i+4);if(e(i)===t)return s+r(i+8)<=o?s:-1}return-1},c=t=>{const s=f(t);if(s<0)return!1;if("para"===e(s)){const t=n(s+8),o=[1,3,4,5,7][t];if(3!==t&&4!==t)return!1;for(let t=0;t<o;t++)if(Math.abs(i(s+12+4*t)-(t<5?d[t]:0))>(t?.002:.01))return!1;return!0}const c="curv"===e(s)?r(s+8):0;if(c<2||s+12+2*c>o)return!1;for(let t=0;t<c;t++)if(Math.abs(n(s+12+2*t)/65535-p(t/(c-1)))>.001)return!1;return w.every((t,o)=>{const r=o*(c-1)/16,e=Math.floor(r),i=Math.min(c-1,e+1),f=r-e;return Math.abs((n(s+12+2*e)*(1-f)+n(s+12+2*i)*f)/65535-t)<=.001})};if(["A2B0","A2B1","A2B2"].some(t=>f(t)>=0))return null;if("GRAY"===e(16))return c("kTRC")?"srgb":null;if("RGB "!==e(16)||!["rTRC","gTRC","bTRC"].every(c))return null;const a=["rXYZ","gXYZ","bXYZ"].flatMap(t=>{const o=f(t);return o>=0&&"XYZ "===e(o)?[i(o+8),i(o+12),i(o+16)]:[9,9,9]}),l=t=>t.every((t,o)=>Math.abs(a[o]-t)<=.004);return l(h)?"srgb":l(u)?"display-p3":null}(o),!I)throw a("a colour profile other than sRGB or Display P3 (decode the picture and encode its pixels)")}let E=!1;return 3!==s.components.length||g||(E=m>=0?0===m:82===s.components[0].id&&71===s.components[1].id&&66===s.components[2].id),{width:s.width,height:s.height,components:s.components,progressive:s.progressive,ycbcr:!E,orientation:M,colorSpace:I}}(n);!function(o,n,r=t){if(!Number.isInteger(o)||!Number.isInteger(n)||o<1||n<1)throw m("JXL_INPUT","A picture is at least one pixel wide and high, in whole pixels.");if(o>r.edge||n>r.edge)throw m("JXL_DIMENSIONS","A picture is at most "+r.edge+" pixels on a side.");if(o*n>r.pixels)throw m("JXL_DIMENSIONS","A picture is at most "+r.pixels.toLocaleString("en-US")+" pixels.")}(f.width,f.height,o);const g=f.orientation>=5;return(t=>{Object.assign(s,t)})({width:g?f.height:f.width,height:g?f.width:f.height,orientation:f.orientation}),yield*i(f,r)}(n,i,0,e));return s}const U=256;function x(t,o){t.o([[9,1],[13,1],[18,1],[30,1]],o)}const I={channel:null,weighted:null,fill:null},E=[1,2,3,4,0,5,17,6,16,7,8,9,10,11,12,13,14,15],v=[2,4,3,2,2,4],C=[0,7,3,2,1,15];function S(t,o){const n=new Uint8Array(t.length),r=[];for(let o=0;o<t.length;o++)t[o]>0&&r.push(o);if(!r.length)return n;if(1===r.length)return n[r[0]]=1,n;for(let e=1;;e*=2){const i=r.map(o=>({w:Math.max(t[o],e),m:o,l:null,r:null})).sort((t,o)=>t.w-o.w||t.m-o.m),s=[];let f=0,c=0;const a=()=>f<i.length&&(c>=s.length||i[f].w<=s[c].w)?i[f++]:s[c++];for(;i.length-f+(s.length-c)>1;){const t=a(),o=a();s.push({w:t.w+o.w,m:-1,l:t,r:o})}const l=[[a(),0]];let h=0;for(;l.length;){const[t,o]=l.pop();t.m>=0?(n[t.m]=o,o>h&&(h=o)):l.push([t.l,o+1],[t.r,o+1])}if(h<=o)return n}}function J(t,o,n,r,e,i){const s=[];for(;s.push(r&e),r>>=i;)r--;for(let r=s.length-1;r>=0;r--)t.push(n),o.push(s[r])}function L(t,o){let n=0;for(let r=0;r<o;r++)n=n<<1|1&t,t>>=1;return n}function X(t){const o=new Uint32Array(17),n=new Uint32Array(17),r=new Uint16Array(t.length);for(const n of t)o[n]++;o[0]=0;for(let t=1,r=0;t<=16;t++)r=r+o[t-1]<<1,n[t]=r;for(let o=0;o<t.length;o++)t[o]&&(r[o]=L(n[t[o]]++,t[o]));return r}function k(t){const o=[];for(let n=0;n<t.length;n++)t[n]>0&&o.push(n);const n=o.length,r=o[n-1]+1||1,e=n<=4?new Uint8Array(r):S(t,15).subarray(0,r);let i=0;if(n<=4){o.sort((o,n)=>t[n]-t[o]||o-n),i=4===n&&t[o[0]]>t[o[2]]+t[o[3]]?1:0;const r=n<2?[0]:2===n?[1,1]:3===n?[1,2,2]:i?[1,2,3,3]:[2,2,2,2];o.forEach((t,o)=>{e[t]=r[o]}),o.sort((t,o)=>e[t]-e[o]||t-o)}return{M:r,A:e,U:X(e),I:n&&n<=4?o:null,C:i}}function N(t,o){if(o.M<=1)return;if(o.I){const n=i(o.M-1)+1;t.write(2,1),t.write(2,o.I.length-1);for(const r of o.I)t.write(n,r);return void(4===o.I.length&&t.write(1,o.C))}const n=o.A,r=[],e=[];let s=n.length-1;for(;s>0&&!n[s];)s--;let f=8;for(let t=0;t<=s;){const o=n[t];let i=1;for(;t+i<=s&&n[t+i]===o;)i++;let c=i;if(o&&o!==f&&(r.push(o),e.push(0),c--,f=o),c<3)for(let t=0;t<c;t++)r.push(o),e.push(0);else J(r,e,o?16:17,c-3,o?3:7,o?2:3);t+=i}const c=new Uint32Array(18);for(const t of r)c[t]++;let a=0;for(const t of c)t&&a++;1===a&&(c[r[0]?0:1]=c[r[0]]);const l=S(c,5),h=X(l);let u=18;for(;u>0&&!l[E[u-1]];)u--;t.write(2,0);for(let o=0;o<u;o++){const n=l[E[o]];t.write(v[n],C[n])}for(let o=0;o<r.length;o++){const n=r[o];t.write(l[n],h[n]),16===n?t.write(2,e[o]):17===n&&t.write(3,e[o])}}function O(t,o=0,n=0){return{split:t,S:o,J:n,L:1<<t}}function T(t,o,n=15){t.write(s(n+1),o.split),o.split!==n&&(t.write(s(o.split+1),o.S),t.write(s(o.split-o.S+1),o.J))}function _(t,o,n){if(o<t.L)return n[0]=o,n[1]=0,void(n[2]=0);const r=i(o),e=o-(1<<r),s=r-t.S-t.J;n[0]=t.L+(r-t.split<<t.S+t.J|e>>r-t.S<<t.J|e&(1<<t.J)-1),n[1]=s,n[2]=o>>t.J&(1<<s)-1}function D(t,o,n,r=0){_(t,o,P),n[r+P[0]]++}const P=[0,0,0];function G(t,o,n,r,e=0){_(n,r,P);const i=e+P[0];t.write(o.A[i],o.U[i]),P[1]&&t.write(P[1],P[2])}function R(t,o){let n=0;for(const t of o)n=Math.max(n,t);const r=n?s(n+1):0;if(r<=3){if(t.write(1,1),t.write(2,r),r)for(const n of o)t.write(r,n);return}t.write(1,0),t.write(1,0);const e=O(8),i=O(4),f=o.length>=64,c=new Uint32Array(f?257:n+1),a=[];for(let t=0;t<o.length;){let n=1;for(;f&&t+n<o.length&&o[t+n]===o[t];)n++;if(n>=8)a.push([o[t],n-1]),c[o[t]]++,D(i,n-1-7,c,224),t+=n;else{for(let r=0;r<n;r++)a.push([o[t],0]),c[o[t]]++;t+=n}}const l=k(c);if(!f){q(t,{X:new Uint8Array(1),k:[{config:e,code:l}]});for(const[o]of a)G(t,l,e,o);return}const h=new Uint32Array(1);h[0]=1,q(t,{N:{O:224,minLength:7,T:i},X:new Uint8Array([1,0]),k:[{config:O(0),code:k(h)},{config:e,code:l}]});for(const[o,n]of a)G(t,l,e,o),n&&G(t,l,i,n-7,224)}function q(t,{N:o=null,X:n,k:r}){t.write(1,o?1:0),o&&(t.o([[0,224],[0,512],[0,4096],[15,8]],o.O),t.o([[0,3],[0,4],[2,5],[8,9]],o.minLength),T(t,o.T,8)),n.length>1&&R(t,n),t.write(1,1);for(const o of r)T(t,o.config,15);for(const o of r){const n=o.code.M-1;if(n){const o=i(n);t.write(1,1),t.write(4,o),t.write(o,n-(1<<o))}else t.write(1,0)}for(const o of r)N(t,o.code)}const B=O(4),Z=Object.freeze({O:224,minLength:7,T:B}),j=O(0);function z(t,o){const n=[[3,0],[6,8],[10,72],[13,1096]];if("rct"===o.type)t.write(2,0),t.o(n,o._),t.o([[0,6],[2,0],[4,2],[6,10]],o.D??6);else if("palette"===o.type)t.write(2,1),t.o(n,o._),t.o([[0,1],[0,3],[0,4],[13,1]],o.P),t.o([[8,0],[10,256],[12,1280],[16,5376]],o.G),t.o([[0,0],[8,1],[10,257],[16,1281]],o.R??0),t.write(4,o.q??0);else{if("squeeze"!==o.type)throw new Error("unknown transform "+o.type);{t.write(2,2);const r=o.B??[];t.o([[0,0],[4,1],[6,9],[8,41]],r.length);for(const o of r)t.write(1,o.Z?1:0),t.write(1,o.j?1:0),t.o(n,o._),t.o([[0,1],[0,2],[0,3],[4,4]],o.P)}}}function Y(t,o,n,r,i,s,f=!1){if(I.channel?.(t,o,n,r,i,s,f))return;const c=s.q,a=s.offset,l=s.Y,h=s.config||j,u=B;let w=0;const d=(n,r=h,e=0)=>{t?G(t,o,r,n,e):f&&!e?o[n]++:D(r,n,o,f?o.length-33:e)},p=()=>{if(w){if(w>=8)d(0),d(w-7-1,u,224);else for(let t=0;t<w;t++)d(0);w=0}},y=(t,o)=>{const r=n[t];let i;1===l?i=r-o-a:(i=Math.round((r-o-a)/l),n[t]=o+a+i*l),0!==i?(p(),d(e(i))):w++};for(let t=0,o=0;t<i;t++)for(let e=0;e<r;e++,o++){let i;if(0===c)i=0;else{const s=e?n[o-1]:t?n[o-r]:0,f=t?n[o-r]:s;if(1===c)i=s;else if(2===c)i=f;else if(3===c)i=(s+f)/2|0;else{const a=s+f-(e&&t?n[o-r-1]:s);if(5===c){const t=s<f?s:f,o=s<f?f:s;i=a<t?t:a>o?o:a}else{if(4!==c)throw new Error("predictor "+c+" is not coded here");i=Math.abs(a-s)<Math.abs(a-f)?s:f}}}y(o,i)}p()}const H=[O(0),O(1),O(2),O(3),O(4),O(4,1,1),O(5,1,1),O(2,0,1),O(3,0,1)],F=O(4,1,1);class K{constructor(t){this.H=t,this.small=new Uint32Array(64*t),this.F=Array.from({length:t},()=>null),this.K=new Float64Array(t)}add(t,o){if(this.K[t]++,o<64)return void this.small[64*t+o]++;const n=this.F[t]||(this.F[t]=new Map);n.set(o,(n.get(o)||0)+1)}V(t,o,n=64){const r=new Uint32Array(n);for(const n of t){const t=64*n;for(let n=0;n<64;n++)this.small[t+n]&&(r[V(o,n)]+=this.small[t+n]);const e=this.F[n];if(e)for(const[t,n]of e)r[V(o,t)]+=n}return r}W(t,o){let n=tt(this.V(t,o));for(const r of t){const t=64*r;for(let r=o.L;r<64;r++)this.small[t+r]&&(n+=this.small[t+r]*(i(r)-o.S-o.J));const e=this.F[r];if(e)for(const[t,r]of e)n+=r*(i(t)-o.S-o.J)}return n}}const Q=[0,0,0];function V(t,o){return _(t,o,Q),Q[0]}const W=[1];for(let t=1;t<32;t++)W.push(W[t-1]/2);function $(t){let o=0;for(t>=2&&t<4294967296&&(o=31-Math.clz32(t),t*=W[o]);t>=1.4142135623730951;)t/=2,o++;for(;t<.7071067811865476;)t*=2,o--;const n=(t-1)/(t+1),r=n*n;let e=n,i=n;for(let t=3;t<30;t+=2)i+=(e*=r)/t;return o+2.885390081777927*i}function tt(t){let o=0,n=0;for(const n of t)o+=n;for(const r of t)r&&(n+=r*$(o/r));return n}function ot(t,{maxClusters:o=48,newClusterCost:n=320}={}){const r=[];for(let o=0;o<t.H;o++)t.K[o]>0&&r.push(o);r.sort((o,n)=>t.K[n]-t.K[o]);const e=[];for(const i of r){const r=t.V([i],F),s=tt(r);let f=-1,c=1/0;for(let t=0;t<e.length;t++){const o=e[t],n=new Uint32Array(64);for(let t=0;t<64;t++)n[t]=o.$[t]+r[t];const i=tt(n)-o.tt-s;i<c&&(c=i,f=t)}if(f<0||e.length<o&&c>n)e.push({H:[i],$:r,tt:s});else{const t=e[f];t.H.push(i);for(let o=0;o<64;o++)t.$[o]+=r[o];t.tt=tt(t.$)}}e.length||e.push({H:[],$:new Uint32Array(64),tt:0});const i=new Uint8Array(t.H);let s=2*t.H;const f=e.map((o,n)=>{for(const t of o.H)i[t]=n;let r=H[0],e=1/0;for(const n of H){const i=t.W(o.H,n);i<e&&(e=i,r=n)}const f=k(t.V(o.H,r,128));return s+=e+3*f.M+40,{config:r,code:f}});return{X:i,k:f,write:(t,o,n)=>{const r=f[i[o]];G(t,r.code,r.config,n)},tt:s}}const nt=[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],rt=[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],et=c.map(t=>(7&t)<<3|t>>3),it=(t,o,n)=>2*(rt[t]+nt[o])+n;function st(t,o,n){const r=[];if(o>0){const n=new Uint32Array(2048);let e=0;for(const o of t)n[Math.min(2047,Math.max(0,o+1024))]++,e++;let i=0,s=e/(o+1);for(let t=0;t<2048&&r.length<o;t++)i+=n[t],i>s&&(r.push(t-1025),s=e*(r.length+1)/(o+1))}const e=r.length+1,i=new Uint8Array(39*e);for(let t=0;t<e;t++)for(let o=0;o<13;o++)i[(0+o)*e+t]=t,i[(13+o)*e+t]=n?e:e+(t>>1),i[(26+o)*e+t]=n?e:e+(e-1>>1)+1+(t>>1);let s=0;for(const t of i)s=Math.max(s,t+1);return{thresholds:r,ot:e,map:i,nt:s,rt:t=>{let o=0;for(const n of r)t>n&&o++;return o},et:(t,o)=>i[13*(t<2?1^t:2)*e+o],it:(t,o)=>(Math.min(t,64)<8?Math.min(t,64):4+(Math.min(t,64)>>1))*s+o,st:t=>37*s+458*t,H:495*s}}function ft(t,o,n){const r=o.map(()=>function(t,o=0,n=1){return{q:t,offset:o,Y:n,context:-1}}(n)),i=r.map(()=>new Uint32Array(257));o.forEach((t,o)=>Y(null,i[o],t.data,t.w,t.h,r[o])),function(t,{ft:o=!0,ct:n=[]}={}){t.write(1,o?1:0),t.write(1,1),t.o([[0,0],[0,1],[4,2],[8,18]],n.length);for(const o of n)z(t,o)}(t,{ft:!1,ct:[]});const s=function(t,o){const n=[o],r=[],i=[];for(const t of n){if(t.left){r.push(t.lt+1,e(t.ht)),n.push(t.left,t.right);continue}t.context=i.length,i.push(t);const o=1===t.Y?0:31-Math.clz32(t.Y&-t.Y);r.push(0,t.q,e(t.offset),o,(t.Y>>o)-1)}const s=new Uint32Array(64);for(const t of r)D(j,t,s);const f=k(s);q(t,{X:new Uint8Array(6),k:[{config:j,code:f}]});for(const o of r)G(t,f,j,o);return i}(t,function(t){return function(t,o){const n=(r,e)=>{if(r===e)return o?t[r].ut:t[r];const i=r+e>>1;return function(t,o,n,r){return{lt:t,ht:o,left:n,right:r}}(o,o?t[i].wt:i,n(i+1,e),n(r,i))};return n(0,t.length-1)}(t,0)}(r)),f=function(t,o,n,r=t=>t.context,e=()=>j){const i=new Uint8Array(o.length+1);for(const t of o)i[t.context]=r(t)+1;const s=new Uint32Array(2);s[1]=1;const f=[{config:j,code:k(s)}],c=Math.max(...i);for(let t=0;t<c;t++)f.push({config:e(t),code:k(n[t])});return q(t,{N:Z,X:i,k:f}),f}(t,s,i,t=>r.indexOf(t));o.forEach((o,n)=>Y(t,f[n+1].code,o.data,o.w,o.h,r[n]))}function*ct(t,o={}){const{width:n,height:i,components:c,alpha:l}=t,h=1===c.length,u=t.quantScale||1;let w=t.quantFieldBase||1;for(;w/(2040*u)<1/16384;)w*=2;const d=t.ycbcr||h,p=h?[0,0,0]:d?[1,0,2]:[0,1,2],y=p.map(t=>c[t]),b=o.orders?p.map(t=>o.orders[t].map(t=>et[t])):[et,et,et],m=t=>1===t?0:2===t?1:-1,M=y.map(t=>m(t.h)),A=y.map(t=>m(t.v));if(M.includes(-1)||A.includes(-1))throw a("sampling factors other than one or two");const I=Math.max(...M),E=Math.max(...A),v=t=>I-M[t],C=t=>E-A[t];if(!d&&(I||E))throw a("an RGB JPEG with subsampling");const S=Math.ceil(n/(8<<I))<<I,J=Math.ceil(i/(8<<E))<<E,L=Math.ceil(n/U),X=Math.ceil(i/U),N=L*X,T=Math.ceil(S/256),_=T*Math.ceil(J/256),P=1===N,B=(t,o,n)=>{const r=y[t];return h&&1!==t?-1:64*(n*r.stride+o)},Z=t=>{let o=1024;for(;t%o;)o>>=1;return o},j=y.map(t=>d?1:t.quant[0]/Z(t.quant[0])),z=y.map(t=>d?0:1024/Z(t.quant[0])),Y=new Int32Array(S*J);for(let t=0;t<J;t++)for(let o=0;o<S;o++)Y[t*S+o]=y[1].coeffs[B(1,o,t)]*j[1]+z[1];const H=y[1].quant,F=(H[8]+H[16]+H[24]+H[32]+H[40])/u,Q=S*J,V=Q<256?0:Math.max(1,Math.min(7,s(Q)-s(F)-7));let W=st(Y,V,h);const $=new r(256);if(function(t,o,n,r,e,{xyb:i=!1,orientation:s=1,colorSpace:f}={}){t.write(16,2815),t.write(1,0),x(t,n),t.write(3,0),x(t,o),t.write(1,0);const c=1!==s;t.write(1,c?1:0),c&&t.write(6,s-1),t.write(4,8),e?t.write(3,5):t.write(2,0),t.write(1,i?1:0),3===r&&"display-p3"===f?t.write(21,903568):3===r?t.write(1,1):t.write(15,14100),c&&t.write(1,1),t.write(2,0),t.write(1,1),t.i()}($,n,i,h?1:3,!!l,{orientation:t.orientation,colorSpace:t.colorSpace}),$.write(1,0),$.write(2,0),$.write(1,0),$.write(2,2),$.write(8,111),$.write(1,d?1:0),d)for(let t=0;t<3;t++)$.write(2,0===M[t]&&0===A[t]?0:1===M[t]&&1===A[t]?1:1===M[t]?2:3);$.write(2,0),l&&$.write(2,0),function(t,o){t.write(o?7:5,0),t.write(11,1)}($,l);const tt=[0,1,2].map(()=>new Int32Array(1024)),nt=(t,o)=>{const n=t%L*32,r=32*(t/L|0),i=Math.min(32,S-n),s=Math.min(32,J-r);for(let t=0;t<s;t++)for(let s=0;s<i;s++)for(const i of[1,0,2]){const f=v(i),c=C(i),a=s>>f,l=t>>c;if(a<<f!==s||l<<c!==t)continue;const h=B(i,(n>>f)+a,(r>>c)+l),u=y[i].coeffs;let w=0;if(h>=0)for(let t=1;t<64;t++)u[h+t]&&w++;const d=tt[i],p=0===a?0===l?32:d[32*(l-1)]:0===l?d[32*l+a-1]:d[32*(l-1)+a]+d[32*l+a-1]+1>>1;d[32*l+a]=w;const m=W.et(i,W.rt(Y[(r+t)*S+n+s]));o(W.it(p,m),w);const g=W.st(m);let M=w>4?0:1,A=w;for(let t=1;t<64&&A;t++){const n=u[h+b[i][t]];o(g+it(A,t,M),e(n)),M=n?1:0,A-=M}}},rt=(V>0?3:2)*N+_;let ct=0;const at=function*(t){W=t;const n=new K(t.H);for(let t=0;t<N;t++)nt(t,(t,o)=>n.add(t,o)),yield++ct/rt;return(o.coding||ot)(n,o.clusters)};let lt=yield*at(W);if(V>0){const t=W,o=st(Y,0,h),n=yield*at(o);n.tt<=lt.tt?lt=n:W=t}const ht=Array.from({length:P?1:2+_+N},()=>null),ut=t=>ht[P?0:t]||(ht[P?0:t]=new r(4096)),wt=new Uint32Array(ht.length);let dt=0;const pt=t=>{t=P?0:t;const o=Math.ceil(ht[t].u/8);dt+=o-wt[t],wt[t]=o,g(dt)},yt=ut(0);yt.write(1,0);const bt=(t,o)=>{const r=t*U,e=o*U,s=Math.min(U,n-r),f=Math.min(U,i-e),c=new Int32Array(s*f);for(let t=0;t<f;t++)for(let o=0;o<s;o++)c[t*s+o]=l[4*((e+t)*n+r+o)+3];return{w:s,h:f,data:c}};for(let t=0;t<3;t++)yt.write(16,f(y[t].quant[0]/j[t]/(2040*u)*128));yt.write(2,3),yt.write(16,57343),yt.write(2,1),yt.write(5,0),yt.write(1,0),yt.write(4,0),yt.write(4,W.thresholds.length);for(const t of W.thresholds)yt.o([[4,0],[8,16],[16,272],[32,65808]],e(t));yt.write(4,0),yt.write(4,0),R(yt,W.map),yt.write(1,0),yt.write(2,0),yt.write(16,0),yt.write(16,0),yt.write(8,128),yt.write(8,128),yt.write(1,0),l&&ft(yt,[P?bt(0,0):{w:0,h:0,data:new Int32Array(0)}],5),pt(0);for(let o=0;o<_;o++){const n=ut(1+o),r=o%T*256,e=256*(o/T|0),i=Math.min(256,S-r),f=Math.min(256,J-e);n.write(2,0),ft(n,[1,0,2].map(t=>{const o=i>>v(t),n=f>>C(t),s=new Int32Array(o*n),c=j[t],a=z[t];for(let i=0;i<n;i++)for(let n=0;n<o;n++){const f=B(t,(r>>v(t))+n,(e>>C(t))+i);s[i*o+n]=f<0?0:y[t].coeffs[f]*c+a}return{w:o,h:n,data:s}}),5);const c=i*f,a=s(c);a&&n.write(a,c-1);const l=i+7>>3,h=f+7>>3,u=new Int32Array(2*c).fill(w-1,c);if(t.quantFields)for(let o=0;o<f;o++)for(let n=0;n<i;n++)u[c+o*i+n]=t.quantFields[(e+o)*S+r+n]-1;ft(n,[{w:l,h:h,data:new Int32Array(l*h)},{w:l,h:h,data:new Int32Array(l*h)},{w:c,h:2,data:u},{w:i,h:f,data:new Int32Array(c)}],w>1?5:0),pt(1+o),yield++ct/rt}const mt=ut(1+_);mt.write(1,0),mt.write(3,7),mt.write(16,f(w/(2040*u))),ft(mt,[0,1,2].map(t=>{const o=new Int32Array(64),n=y[t].quant;for(let t=0;t<8;t++)for(let r=0;r<8;r++)o[8*r+t]=n[8*t+r];return{w:8,h:8,data:o}}),5);for(let t=1;t<17;t++)mt.write(3,0);if(N>1&&mt.write(s(N),0),o.orders){mt.o([[0,95],[0,19],[0,0],[13,0]],1);const t=[];for(const n of p){const r=Array.from({length:64},(t,o)=>o),e=o.orders[n].map(t=>{const o=r.indexOf(t);return r.splice(o,1),o});let i=64;for(;i>1&&!e[i-1];)i--;t.push(i-1,...e.slice(1,i))}const n=O(0),r=new Uint32Array(64);for(const o of t)D(n,o,r);const e=k(r);q(mt,{X:new Uint8Array(8),k:[{config:n,code:e}]});for(const o of t)G(mt,e,n,o)}else mt.write(2,2);lt.writeHistograms?lt.writeHistograms(mt):q(mt,{X:lt.X,k:lt.k}),pt(1+_);for(let t=0;t<N;t++){const o=ut(2+_+t);nt(t,(t,n)=>lt.write(o,t,n)),lt.flush&&lt.flush(o),l&&!P&&ft(o,[bt(t%L,Math.floor(t/L))],5),pt(2+_+t),yield++ct/rt}return function(t,o){const n=o.map(t=>t.length);!function(t,o){t.write(1,0),t.i();for(const n of o)t.o([[10,0],[14,1024],[22,17408],[30,4211712]],n);t.i()}(t,n);const r=t.finish(),e=r.length+n.reduce((t,o)=>t+o,0);g(e);const i=new Uint8Array(e);i.set(r);let s=r.length;for(const t of o)i.set(t,s),s+=t.length;return i}($,function(t){return t.map(t=>t?t.finish():new Uint8Array(0))}(ht))}function*at(t,o,n){if(o<3&&!n)return yield*ct(t);let r=n;try{const n=ct(t);let e,i;for(;!(e=n.next()).done;)if(i=yield e.value/2,i&&r&&e.value<1)return n.return(),r;if((!r||e.value.length<r.length)&&(r=e.value),i||o<3)return r;const s=o<4?1:3,f=ct(t,{clusters:{maxClusters:32,newClusterCost:160}});for(;!(e=f.next()).done;)if(i=yield.5+e.value/(2*s),i&&e.value<1)return f.return(),r;if(e.value.length<r.length&&(r=e.value),i||o<4)return r;const a=function*(t){const o=c.map(t=>(7&t)<<3|t>>3),n=t.map(()=>new Uint32Array(64)),r=t.reduce((t,o)=>t+o.coeffs.length,0);let e=0;for(let i=0;i<t.length;i++){const{coeffs:s}=t[i],f=n[i];for(let t=0;t<s.length;t+=65536){const n=Math.min(t+65536,s.length);for(let r=t;r<n;r+=64)for(let t=1;t<64;t++)s[r+o[t]]&&f[t]++;if(e+=n-t,yield e/r)return null}}return n.map(t=>[0,...Array.from({length:63},(t,o)=>o+1).sort((o,n)=>Math.floor(t[n]/8)-Math.floor(t[o]/8)||o-n)])}(t.components);for(;!(e=a.next()).done;)if(yield.5+(1+e.value)/6)return a.return(),r;const l=ct(t,{orders:e.value});for(;!(e=l.next()).done;)if(i=yield.5+(2+e.value)/6,i&&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 lt(t,o){return function(t){return function(t){let o;for(;!(o=t.next()).done;);o.value}(t),{bytes:t.bytes,width:t.width,height:t.height,orientation:t.orientation}}(ht(t,o))}function ht(t,o){return A(t,o,at)}export{o as LIMITS,lt as transcode,ht as transcodeSteps};
@@ -0,0 +1,3 @@
1
+ /** Optional, realm-local acceleration; call before an encode, not from a progress callback. */
2
+ export function configureKernels(mode?: 'off' | 'auto' | 'scalar' | 'simd', parts?: {channel?: boolean; weighted?: boolean; fill?: boolean}): 'off' | 'scalar' | 'simd';
3
+ export function kernelMode(): 'off' | 'scalar' | 'simd';
@@ -0,0 +1,3 @@
1
+ // MIT. Empty in the small core. The optional kernels door installs synchronous,
2
+ // private-memory kernels; returning false leaves the reference JavaScript in charge.
3
+ export const kernelHooks = {channel: null, weighted: null, fill: null};
@@ -0,0 +1,4 @@
1
+ // Generated from kernels-*.wat. MIT. Do not edit.
2
+ export const SCALAR = "AGFzbQEAAAABRAdgBn9/f39/fwBgAn9/AGADf39/AGACf38Bf2AMf39/f39/f39/f39/AX9gB39/f39/f38AYAx/f39/f39/f39/f38AAhEBA2VudgZtZW1vcnkCAQGAAQMIBwABAgMEBQYGPwx/AUEAC38BQQALfwFBAAt+AUIAC38BQQALfwFBAAt/AUEAC38BQQALfwFBAQt/AUGBAgt/AUHgAQt/AUEACwcnBAhyZXNpZHVhbAAABnRva2VucwAEBGZpbGwABQh3ZWlnaHRlZAAGCtoWB4ADAQh/IAFBAnQhCANAQQAhBgNAQQAhDSADBEAgBgR/IABBBGsoAgAFIAcEfyAAIAhrKAIABUEACwshCSAHBH8gACAIaygCAAUgCQshCiAGQQBHIAdBAEdxBH8gACAIa0EEaygCAAUgCQshCyAJIApqIAtrIQwgA0EBRgRAIAkhDQsgA0ECRgRAIAohDQsgA0EDRgRAIAkgCmpBAm0hDQsgA0EERgRAIAkgCiAMIAlrIAwgCWtBH3VzIAwgCWtBH3VrIAwgCmsgDCAKa0EfdXMgDCAKa0EfdWtIGyENCyADQQVGBEAgCSAKIAkgCkgbIAkgCiAJIApKGyAMIAkgCiAJIApKGyAMSBsgCSAKIAkgCkgbIAkgCiAJIApKGyAMIAkgCiAJIApKGyAMSBtKGyENCwsgBSAAKAIAIA1rIARrQQF0IAAoAgAgDWsgBGtBH3VzNgIAIAVBBGohBSAAQQRqIQAgBkEBaiEGIAYgAUkNAAsgB0EBaiEHIAcgAkkNAAsLQQAjAyABrSMErYaEJAMjBCAAaiQEAkADQCMEQQhJDQEjASMDpzoAACMBQQFqJAEjA0IIiCQDIwRBCGskBAwACwsLoAIBBn8gAgRAIAFBEEkEQCABIwpqIQMFQR8gAWdrIQQgBEEMaiMKaiEDIAQhBSABQQEgBHRBAWtxIQYLBSMLBEAgASMJTwRADwsgASEDBSABIwhJBEAgASEDBUEfIAFnayEEIwYjB3JFBEAjCCAEIwVraiEDIAQhBSABQQEgBHRBAWtxIQYFIAFBASAEdGshByAEIwZrIwdrIQUjCCAEIwVrIwYjB2p0IAcgBCMGa3YjB3RyIAdBASMHdEEBa3FyaiEDIAEjB3ZBASAFdEEBa3EhBgsLCwsjAgRAIwAgACMJbCADakEDdGohCCAIKAIAIAgoAgQQASAFBEAgBSAGEAELBSMAIAAjCWwgA2pBAnRqIQggCCAIKAIAQQFqNgIACwsVACAABH8gACABQQJ0aigCAAVBAAsLigIBBH8gAyQAIAQkASAFJAIgBq0kAyAHJAQgCCQFIAkkBiAKJAdBASAIdCQIIAtBAEckCyALQYECIAsbJAkgC0Eha0HgASALGyQKAkADQCAMIAJPDQEgACAMQQJ0aigCACEOQQEhDSAORQRAAkADQCAMIA1qIAJPDQEgACAMIA1qQQJ0aigCAA0BIA1BAWohDQwACwsLIAEgDBADIA5BABACIA1BB0sEQCABIAxBAWoQAyANQQhrQQEQAgVBASEPAkADQCAPIA1PDQEgASAMIA9qEANBAEEAEAIgD0EBaiEPDAALCwsgDCANaiEMDAALCyAEQQhrIwOnNgIAIARBBGsjBDYCACMBIARrC8QBAQh/A0AgACgCACEHIAdB/wFxIQggAkEDTwRAIAdBCHZB/wFxIQkgB0EQdkH/AXEhCiAIIAprIQsgCiALQQF1aiENIAkgDWshDCADIA0gDEEBdWo2AgAgBCALNgIAIAUgDDYCACACQQRGBEAgBiAHQRh2NgIACwUgAyAINgIAIAJBAkYEQCAEIAdBGHY2AgALCyADQQRqIQMgBEEEaiEEIAVBBGohBSAGQQRqIQYgAEEEaiEAIA5BAWohDiAOIAFJDQALC4kNAS5/IAFBAmpBAnQhDiABQQJ0IQ8gCyE0IAsgDkECbGohNSALIA5BBGxqITYgCyAOQQZsaiE3IAsgDkEIbGohOANAQQAgDiARQQFxGyESIA4gEmshE0EAIRADQCAQBH8gAEEEaygCAAUgEQR/IAAgD2soAgAFQQALCyEXIBEEfyAAIA9rKAIABSAXCyEYIBBBAWogAUkgEUEAR3EEfyAAIA9rQQRqKAIABSAYCyEZIBMgEEECdGohFCAUQQRrIBQgEBshFiAUQQRqIBQgEEEBaiABSRshFSA0IBRqKAIAIDQgFWooAgBqIDQgFmooAgBqIQ0gDUGAEEkEQCAKIA1BgBBqQQJ0aigCACEnBUEAQRogDUEBamdrQQBBGiANQQFqZ2tKGyEMQQRBDSAJIA0gDHVBAnRqKAIAbCAMdWohJwsgNSAUaigCACA1IBVqKAIAaiA1IBZqKAIAaiENIA1BgBBJBEAgCiANQQBqQQJ0aigCACEoBUEAQRogDUEBamdrQQBBGiANQQFqZ2tKGyEMQQRBDCAJIA0gDHVBAnRqKAIAbCAMdWohKAsgNiAUaigCACA2IBVqKAIAaiA2IBZqKAIAaiENIA1BgBBJBEAgCiANQQBqQQJ0aigCACEpBUEAQRogDUEBamdrQQBBGiANQQFqZ2tKGyEMQQRBDCAJIA0gDHVBAnRqKAIAbCAMdWohKQsgNyAUaigCACA3IBVqKAIAaiA3IBZqKAIAaiENIA1BgBBJBEAgCiANQQBqQQJ0aigCACEqBUEAQRogDUEBamdrQQBBGiANQQFqZ2tKGyEMQQRBDCAJIA0gDHVBAnRqKAIAbCAMdWohKgsgGEEDdCEaIBdBA3QhGyAZQQN0IRwgEAR/IDggEmogEEECdGpBBGsoAgAFQQALIR0gOCAUaigCACEeIDggFmooAgAhHyA4IBVqKAIAISAgHSEhIB4gHkEfdXMgHkEfdWsgISAhQR91cyAhQR91a0oEQCAeISELIB8gH0EfdXMgH0EfdWsgISAhQR91cyAhQR91a0oEQCAfISELICAgIEEfdXMgIEEfdWsgISAhQR91cyAhQR91a0oEQCAgISELIAUgITYCACAIQfUDQYt8ICFBi3wgIUobQfUDQYt8ICFBi3wgIUobSBtB9QNqQQJ0aiE5IAYgByA5KAIAQQJ0aigCADYCACAdIB5qISIgGyAcaiAaayEjIBogIiAgakEQbEEFdWshJCAbICIgH2pBCmxBBXVrISUgGiAfIB5qICBqQQdsQQV1ayEmQRsgJyAoaiApICpqamdrISsgJyArdSEnICggK3UhKCApICt1ISkgKiArdSEqICcgKGogKSAqamohLCAsQQF1QQFrrCAjrCAnrH4gJKwgKKx+fCAlrCAprH4gJqwgKqx+fHx8IAkgLEEBa0ECdGooAgCsfkIYh6chLSAeIB1zIB4gH3NyQQBMBEAgGyAcIBogHCAaSBsgGyAcIBogHCAaSBtIGyAtIBsgHCAaIBwgGkobIBsgHCAaIBwgGkobShsgLSAbIBwgGiAcIBpKGyAbIBwgGiAcIBpKG0obSBsgGyAcIBogHCAaSBsgGyAcIBogHCAaSBtIGyAtIBsgHCAaIBwgGkobIBsgHCAaIBwgGkobShsgLSAbIBwgGiAcIBpKGyAbIBwgGiAcIBpKG0obSBtKGyEtCyAAKAIAIS4gBCAuIC1BA2pBA3VrIANrQQF0IC4gLUEDakEDdWsgA2tBH3VzNgIAIC5BA3QhLyASIBBBAnRqITkgOCA5aiAtIC9rNgIAICMgL2sgIyAva0EfdXMgIyAva0EfdWtBA2pBA3UhMCA0IDlqIDA2AgAgNCAUakEEaiA0IBRqQQRqKAIAIDBqNgIAICQgL2sgJCAva0EfdXMgJCAva0EfdWtBA2pBA3UhMSA1IDlqIDE2AgAgNSAUakEEaiA1IBRqQQRqKAIAIDFqNgIAICUgL2sgJSAva0EfdXMgJSAva0EfdWtBA2pBA3UhMiA2IDlqIDI2AgAgNiAUakEEaiA2IBRqQQRqKAIAIDJqNgIAICYgL2sgJiAva0EfdXMgJiAva0EfdWtBA2pBA3UhMyA3IDlqIDM2AgAgNyAUakEEaiA3IBRqQQRqKAIAIDNqNgIAIABBBGohACAEQQRqIQQgBUEEaiEFIAZBBGohBiAQQQFqIRAgECABSQ0ACyARQQFqIREgESACSQ0ACws=";
3
+ export const SIMD = "AGFzbQEAAAABFAJgBn9/f39/fwBgB39/f39/f38AAhEBA2VudgZtZW1vcnkCAQGAAQMDAgABBxMCCHJlc2lkdWFsAAAEZmlsbAABCpYIAtUEAgV7CH8gAUECdCENA0BBACELA0ACQCAMQQBHIAtBAEdxIAtBBGogAU0gA0EDRiADQQVGcnFxBEAgAEEEa/0ABAAhBiAAIA1r/QAEACEHIAYgB/2uASEKIANBA0YEQCAKIApBH/2sAUEB/RH9Tv2uAUEB/awBIQkFIAAgDWtBBGv9AAQAIQggBiAH/bYBIAYgB/24ASAKIAj9sQH9tgH9uAEhCQsgAP0ABAAgCf2xASAE/RH9sQEhCSAFIAlBAf2rASAJQR/9rAH9Uf0LBAAgAEEQaiEAIAVBEGohBSALQQRqIQsMAQtBACESIAMEQCALBH8gAEEEaygCAAUgDAR/IAAgDWsoAgAFQQALCyEOIAwEfyAAIA1rKAIABSAOCyEPIAtBAEcgDEEAR3EEfyAAIA1rQQRrKAIABSAOCyEQIA4gD2ogEGshESADQQFGBEAgDiESCyADQQJGBEAgDyESCyADQQNGBEAgDiAPakECbSESCyADQQRGBEAgDiAPIBEgDmsgESAOa0EfdXMgESAOa0EfdWsgESAPayARIA9rQR91cyARIA9rQR91a0gbIRILIANBBUYEQCAOIA8gDiAPSBsgDiAPIA4gD0obIBEgDiAPIA4gD0obIBFIGyAOIA8gDiAPSBsgDiAPIA4gD0obIBEgDiAPIA4gD0obIBFIG0obIRILCyAFIAAoAgAgEmsgBGtBAXQgACgCACASayAEa0EfdXM2AgAgBUEEaiEFIABBBGohACALQQFqIQsLIAsgAUkNAAsgDEEBaiEMIAwgAkkNAAsLvAMCB3sIfwJAA0AgFUEEaiABSw0BIAD9AAQAIQcgB0H/Af0R/U4hCCACQQNPBEAgB0EI/a0BQf8B/RH9TiEJIAdBEP2tAUH/Af0R/U4hCiAIIAr9sQEhCyAKIAtBAf2sAf2uASENIAkgDf2xASEMIAMgDSAMQQH9rAH9rgH9CwQAIAQgC/0LBAAgBSAM/QsEACACQQRGBEAgBiAHQRj9rQH9CwQACwUgAyAI/QsEACACQQJGBEAgBCAHQRj9rQH9CwQACwsgA0EQaiEDIARBEGohBCAFQRBqIQUgBkEQaiEGIABBEGohACAVQQRqIRUMAAsLAkADQCAVIAFPDQEgACgCACEOIA5B/wFxIQ8gAkEDTwRAIA5BCHZB/wFxIRAgDkEQdkH/AXEhESAPIBFrIRIgESASQQF1aiEUIBAgFGshEyADIBQgE0EBdWo2AgAgBCASNgIAIAUgEzYCACACQQRGBEAgBiAOQRh2NgIACwUgAyAPNgIAIAJBAkYEQCAEIA5BGHY2AgALCyADQQRqIQMgBEEEaiEEIAVBBGohBSAGQQRqIQYgAEEEaiEAIBVBAWohFSAVIAFJDQALCws=";
4
+ export const SIMD_PROBE = "AGFzbQEAAAABBQFgAAF/AwIBAAcJAQVwcm9iZQAACgsBCQBBAP0R/RsACw==";
@@ -0,0 +1 @@
1
+ (module (func $probe (export "probe") (result i32) (i32x4.extract_lane 0 (i32x4.splat (i32.const 0)))))