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/effort.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}),n=[1];for(let t=1;t<=32;t++)n.push(2*n[t-1]);class e{constructor(t=4096){this.bytes=new Uint8Array(t),this.at=0,this.t=0,this.pending=0}write(t,e){if(!(e>=0&&e<n[t]))throw new Error("bit write out of range: "+t+" bits, "+e);let o=this.t+e*(1<<this.pending),r=this.pending+t;this.at+5>=this.bytes.length&&this.grow();const s=this.bytes;for(;r>=8;)s[this.at++]=255&o,o=Math.floor(o/256),r-=8;this.t=o,this.pending=r}o(t,e){for(let o=0;o<4;o++){const[r,s]=t[o];if(e>=s&&e-s<n[r])return this.write(2,o),void(r&&this.write(r,e-s))}throw new Error("U32 value out of range: "+e)}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(n=0){const e=t.bytes+64;if(this.at+n+16>e)throw Object.assign(new Error("The encoded picture exceeds 16 MiB."),{code:"JXL_SIZE"});const o=new Uint8Array(Math.min(e,Math.max(2*this.bytes.length,this.at+n+16)));o.set(this.bytes.subarray(0,this.at)),this.bytes=o}append(t){if(this.at+t.at+8>=this.bytes.length&&this.grow(t.at+8),this.pending)for(let n=0;n<t.at;n++)this.write(8,t.bytes[n]);else this.bytes.set(t.bytes.subarray(0,t.at),this.at),this.at+=t.at;t.pending&&this.write(t.pending,t.t)}finish(){const t=this.at+(this.pending?1:0),n=this.bytes.slice(0,t);return this.pending&&(n[this.at]=this.t),n}}function*o(t,n,e){let o,r;for(;!(o=t.next(r)).done;)r=yield(n+o.value)/e;return o.value}function r(t){return t>=0?2*t:2*-t-1}function s(t){return 31-Math.clz32(t)}function c(t){return t<=1?0:32-Math.clz32(t-1)}const i=(t,n)=>Object.assign(new Error(n),{code:t});function l(n){if(n>t.bytes)throw i("JXL_SIZE","The encoded picture exceeds 16 MiB.")}function f(t){try{return t()}catch(t){if(t instanceof RangeError&&!t.code)throw i("JXL_MEMORY","Not enough memory for this picture.");throw t}}const a=256;function u(t,n){t.o([[9,1],[13,1],[18,1],[30,1]],n)}function h(t,n,e,o,r,{xyb:s=!1,orientation:c=1,colorSpace:i}={}){t.write(16,2815),t.write(1,0),u(t,e),t.write(3,0),u(t,n),t.write(1,0);const l=1!==c;t.write(1,l?1:0),l&&t.write(6,c-1),t.write(4,8),r?t.write(3,5):t.write(2,0),t.write(1,s?1:0),3===o&&"display-p3"===i?t.write(21,903568):3===o?t.write(1,1):t.write(15,14100),l&&t.write(1,1),t.write(2,0),t.write(1,1),t.i()}function y(t,{alpha:n,shift:e=1}){t.write(9,8),n&&t.write(2,0),t.write(2,e),function(t,n){t.write(n?7:5,0),t.write(11,1)}(t,n)}function w(t,n,e=256){const o=Math.ceil(t/e),r=Math.ceil(n/e);return{p:o,M:r,A:Math.ceil(t/(8*e)),m:Math.ceil(n/(8*e)),U:1===o&&1===r}}function p(t,n){const e=n.map(t=>t.length);!function(t,n){t.write(1,0),t.i();for(const e of n)t.o([[10,0],[14,1024],[22,17408],[30,4211712]],e);t.i()}(t,e);const o=t.finish(),r=o.length+e.reduce((t,n)=>t+n,0);l(r);const s=new Uint8Array(r);s.set(o);let c=o.length;for(const t of n)s.set(t,c),c+=t.length;return s}const d=[1,2,3,4,0,5,17,6,16,7,8,9,10,11,12,13,14,15],M=[2,4,3,2,2,4],A=[0,7,3,2,1,15];function g(t,n){const e=new Uint8Array(t.length),o=[];for(let n=0;n<t.length;n++)t[n]>0&&o.push(n);if(!o.length)return e;if(1===o.length)return e[o[0]]=1,e;for(let r=1;;r*=2){const s=o.map(n=>({w:Math.max(t[n],r),I:n,l:null,r:null})).sort((t,n)=>t.w-n.w||t.I-n.I),c=[];let i=0,l=0;const f=()=>i<s.length&&(l>=c.length||s[i].w<=c[l].w)?s[i++]:c[l++];for(;s.length-i+(c.length-l)>1;){const t=f(),n=f();c.push({w:t.w+n.w,I:-1,l:t,r:n})}const a=[[f(),0]];let u=0;for(;a.length;){const[t,n]=a.pop();t.I>=0?(e[t.I]=n,n>u&&(u=n)):a.push([t.l,n+1],[t.r,n+1])}if(u<=n)return e}}function m(t,n,e,o,r,s){const c=[];for(;c.push(o&r),o>>=s;)o--;for(let o=c.length-1;o>=0;o--)t.push(e),n.push(c[o])}function b(t,n){let e=0;for(let o=0;o<n;o++)e=e<<1|1&t,t>>=1;return e}function U(t){const n=new Uint32Array(17),e=new Uint32Array(17),o=new Uint16Array(t.length);for(const e of t)n[e]++;n[0]=0;for(let t=1,o=0;t<=16;t++)o=o+n[t-1]<<1,e[t]=o;for(let n=0;n<t.length;n++)t[n]&&(o[n]=b(e[t[n]]++,t[n]));return o}function I(t){const n=[];for(let e=0;e<t.length;e++)t[e]>0&&n.push(e);const e=n.length,o=n[e-1]+1||1,r=e<=4?new Uint8Array(o):g(t,15).subarray(0,o);let s=0;if(e<=4){n.sort((n,e)=>t[e]-t[n]||n-e),s=4===e&&t[n[0]]>t[n[2]]+t[n[3]]?1:0;const o=e<2?[0]:2===e?[1,1]:3===e?[1,2,2]:s?[1,2,3,3]:[2,2,2,2];n.forEach((t,n)=>{r[t]=o[n]}),n.sort((t,n)=>r[t]-r[n]||t-n)}return{S:o,C:r,T:U(r),v:e&&e<=4?n:null,q:s}}function S(t,n){if(n.S<=1)return;if(n.v){const e=s(n.S-1)+1;t.write(2,1),t.write(2,n.v.length-1);for(const o of n.v)t.write(e,o);return void(4===n.v.length&&t.write(1,n.q))}const e=n.C,o=[],r=[];let c=e.length-1;for(;c>0&&!e[c];)c--;let i=8;for(let t=0;t<=c;){const n=e[t];let s=1;for(;t+s<=c&&e[t+s]===n;)s++;let l=s;if(n&&n!==i&&(o.push(n),r.push(0),l--,i=n),l<3)for(let t=0;t<l;t++)o.push(n),r.push(0);else m(o,r,n?16:17,l-3,n?3:7,n?2:3);t+=s}const l=new Uint32Array(18);for(const t of o)l[t]++;let f=0;for(const t of l)t&&f++;1===f&&(l[o[0]?0:1]=l[o[0]]);const a=g(l,5),u=U(a);let h=18;for(;h>0&&!a[d[h-1]];)h--;t.write(2,0);for(let n=0;n<h;n++){const e=a[d[n]];t.write(M[e],A[e])}for(let n=0;n<o.length;n++){const e=o[n];t.write(a[e],u[e]),16===e?t.write(2,r[n]):17===e&&t.write(3,r[n])}}function x(t,n=0,e=0){return{split:t,L:n,X:e,J:1<<t}}function C(t,n,e=15){t.write(c(e+1),n.split),n.split!==e&&(t.write(c(n.split+1),n.L),t.write(c(n.split-n.L+1),n.X))}function T(t,n,e){if(n<t.J)return e[0]=n,e[1]=0,void(e[2]=0);const o=s(n),r=n-(1<<o),c=o-t.L-t.X;e[0]=t.J+(o-t.split<<t.L+t.X|r>>o-t.L<<t.X|r&(1<<t.X)-1),e[1]=c,e[2]=n>>t.X&(1<<c)-1}function v(t,n,e,o=0){T(t,n,E),e[o+E[0]]++}const E=[0,0,0];function q(t,n,e,o,r=0){T(e,o,E);const s=r+E[0];t.write(n.C[s],n.T[s]),E[1]&&t.write(E[1],E[2])}function L(t,{N:n=null,_:e,P:o}){t.write(1,n?1:0),n&&(t.o([[0,224],[0,512],[0,4096],[15,8]],n.G),t.o([[0,3],[0,4],[2,5],[8,9]],n.minLength),C(t,n.O,8)),e.length>1&&function(t,n){let e=0;for(const t of n)e=Math.max(e,t);const o=e?c(e+1):0;if(o<=3){if(t.write(1,1),t.write(2,o),o)for(const e of n)t.write(o,e);return}t.write(1,0),t.write(1,0);const r=x(8),s=x(4),i=n.length>=64,l=new Uint32Array(i?257:e+1),f=[];for(let t=0;t<n.length;){let e=1;for(;i&&t+e<n.length&&n[t+e]===n[t];)e++;if(e>=8)f.push([n[t],e-1]),l[n[t]]++,v(s,e-1-7,l,224),t+=e;else{for(let o=0;o<e;o++)f.push([n[t],0]),l[n[t]]++;t+=e}}const a=I(l);if(!i){L(t,{_:new Uint8Array(1),P:[{config:r,code:a}]});for(const[n]of f)q(t,a,r,n);return}const u=new Uint32Array(1);u[0]=1,L(t,{N:{G:224,minLength:7,O:s},_:new Uint8Array([1,0]),P:[{config:x(0),code:I(u)},{config:r,code:a}]});for(const[n,e]of f)q(t,a,r,n),e&&q(t,a,s,e-7,224)}(t,e),t.write(1,1);for(const n of o)C(t,n.config,15);for(const n of o){const e=n.code.S-1;if(e){const n=s(e);t.write(1,1),t.write(4,n),t.write(n,e-(1<<n))}else t.write(1,0)}for(const n of o)S(t,n.code)}const X=3,J=5,N=x(4),_=Object.freeze({G:224,minLength:7,O:N}),P=x(0),z=257;function G(t,n=0,e=1){return{R:t,offset:n,j:e,context:-1}}function O(t,n,e,o){return{Z:t,B:n,left:e,right:o}}function R(t,n){const e=(o,r)=>{if(o===r)return n?t[o].k:t[o];const s=o+r>>1;return O(n,n?t[s].Y:s,e(s+1,r),e(o,s))};return e(0,t.length-1)}function j(t){return R(t,0)}function Z(t,n){const e=[n],o=[],s=[];for(const t of e){if(t.left){o.push(t.Z+1,r(t.B)),e.push(t.left,t.right);continue}t.context=s.length,s.push(t);const n=1===t.j?0:31-Math.clz32(t.j&-t.j);o.push(0,t.R,r(t.offset),n,(t.j>>n)-1)}const c=new Uint32Array(64);for(const t of o)v(P,t,c);const i=I(c);L(t,{_:new Uint8Array(6),P:[{config:P,code:i}]});for(const n of o)q(t,i,P,n);return s}function B(t,n){const e=[[3,0],[6,8],[10,72],[13,1096]];if("rct"===n.type)t.write(2,0),t.o(e,n.D),t.o([[0,6],[2,0],[4,2],[6,10]],n.F??6);else if("palette"===n.type)t.write(2,1),t.o(e,n.D),t.o([[0,1],[0,3],[0,4],[13,1]],n.H),t.o([[8,0],[10,256],[12,1280],[16,5376]],n.K),t.o([[0,0],[8,1],[10,257],[16,1281]],n.V??0),t.write(4,n.R??0);else{if("squeeze"!==n.type)throw new Error("unknown transform "+n.type);{t.write(2,2);const o=n.W??[];t.o([[0,0],[4,1],[6,9],[8,41]],o.length);for(const n of o)t.write(1,n.$?1:0),t.write(1,n.tt?1:0),t.o(e,n.D),t.o([[0,1],[0,2],[0,3],[4,4]],n.H)}}}function k(t,{nt:n=!0,et:e=[]}={}){t.write(1,n?1:0),t.write(1,1),t.o([[0,0],[0,1],[4,2],[8,18]],e.length);for(const n of e)B(t,n)}function Y(t,n,e,o=t=>t.context){const r=new Uint8Array(n.length+1);for(const t of n)r[t.context]=o(t)+1;const s=new Uint32Array(2);s[1]=1;const c=[{config:P,code:I(s)}],i=Math.max(...r);for(let t=0;t<i;t++)c.push({config:P,code:I(e[t])});return L(t,{N:_,_:r,P:c}),c}function D(t,n,e,o,s,c){const i=c.R,l=c.offset,f=c.j,a=P,u=N;let h=0;const y=(e,o=a,r=0)=>{t?q(t,n,o,e,r):v(o,e,n,r)},w=()=>{if(h){if(h>=8)y(0),y(h-7-1,u,224);else for(let t=0;t<h;t++)y(0);h=0}},p=(t,n)=>{const o=e[t];let s;1===f?s=o-n-l:(s=Math.round((o-n-l)/f),e[t]=n+l+s*f),0!==s?(w(),y(r(s))):h++};for(let t=0,n=0;t<s;t++)for(let r=0;r<o;r++,n++){let s;if(0===i)s=0;else{const c=r?e[n-1]:t?e[n-o]:0,l=t?e[n-o]:c;if(1===i)s=c;else if(2===i)s=l;else if(3===i)s=(c+l)/2|0;else{const f=c+l-(r&&t?e[n-o-1]:c);if(5===i){const t=c<l?c:l,n=c<l?l:c;s=f<t?t:f>n?n:f}else{if(4!==i)throw new Error("predictor "+i+" is not coded here");s=Math.abs(f-c)<Math.abs(f-l)?c:l}}}p(n,s)}w()}function F(t,n,e,{palette:o=!0}={}){const r=n*e;let s=!0,c=!0;for(let n=0,e=0;e<r&&(255===t[n+3]||(s=!1,c))&&(t[n]===t[n+1]&&t[n]===t[n+2]||(c=!1,s));e++,n+=4);const i=c?1:3,l=!s,f=i+(l?1:0),a=n=>c?l?t[n]+256*t[n+3]:t[n]:t[n]+256*t[n+1]+65536*t[n+2]+(l?16777216*t[n+3]:0),u=new Map;let h=null;for(let t=0,n=0;o&&n<r;n++,t+=4){const n=a(t);if(0!==n&&!u.has(n)&&(u.set(n,1),u.size>=2048))break}if(o&&u.size<2048){const t=[...u.keys()],n=(t,n)=>t>>>8*n&255,e=t=>(.299*n(t,0)+.587*n(t,1)+.114*n(t,2)+.01)*(4===f?n(t,3):1);t.sort((t,n)=>e(t)-e(n));const o=new Map([[0,0]]);t.forEach((t,n)=>o.set(t,n+1)),h={colours:[0,...t],index:o,key:a,byte:n}}return{colour:i,alpha:l,channels:f,palette:h}}function*Q(t,n,e,{shape:r=F(t,n,e),colorSpace:s}={}){let c,i;const l=r.palette?[r,{...r,palette:null}]:[r];for(let r=0;r<l.length;r++)try{const i=yield*o(H(t,n,e,l[r],s),r,l.length);(!c||i.length<c.length)&&(c=i)}catch(t){if("JXL_SIZE"!==t.code)throw t;i=t}if(!c)throw i;return c}function*H(t,n,o,r,s){const{channels:c,palette:i}=r,l=w(n,o),f=i?2:c,u=Array.from({length:f},()=>G(J)),d=j(u),M=Array.from({length:f},()=>new Int16Array(65536)),A=(e,o,r,s)=>{for(let l=0;l<s;l++){let s=4*((o+l)*n+e),f=l*r;for(let n=0;n<r;n++,s+=4,f++)if(i)M[0][f]=i.index.get(i.key(s));else if(c>=3){const n=t[s],e=t[s+1],o=t[s+2],r=n-o,i=o+(r>>1),l=e-i;M[0][f]=i+(l>>1),M[1][f]=r,M[2][f]=l,4===c&&(M[3][f]=t[s+3])}else M[0][f]=t[s],2===c&&(M[1][f]=t[s+3])}};let g=null;if(i){const t=i.colours.length;g=new Int16Array(t*c),i.colours.forEach((n,e)=>{for(let o=0;o<c;o++)g[o*t+e]=i.byte(n,o)})}const m=t=>u[i?l.U?1:0:t],b=i?1:c,U=l.p*l.M,x=2*U,C=(t,e)=>{const r=t%l.p*a,s=(t/l.p|0)*a,c=Math.min(a,n-r),i=Math.min(a,o-s);A(r,s,c,i),e(c,i)};let T=0;if(!i){const t=t=>{const n=I(t),o=new e(128);S(o,n);let r=o.u;for(let e=0;e<t.length;e++)t[e]&&(r+=t[e]*(n.C[e]+Math.max(0,e<224?e-1:e-236)));return r},r=Math.min(n,32),s=Math.min(o,32),i=[J,X].map(t=>({R:t,ot:u.map(()=>new Uint32Array(z))}));for(const t of[0,.5,1]){A(Math.floor((n-r)*t),Math.floor((o-s)*t),r,s);for(const t of i)for(let n=0;n<c;n++)D(null,t.ot[n],M[n],r,s,G(t.R))}u.forEach((n,e)=>{const o=i.map(n=>t(n.ot[e])),r=o[1]<o[0]?1:0;n.R=i[r].R})}const v=i?[{type:"palette",D:0,H:c,K:i.colours.length}]:c>=3?[{type:"rct",D:0,F:6}]:[],E=u.map(()=>new Uint32Array(z)),q=t=>E[u.indexOf(t)];g&&D(null,q(u[0]),g,i.colours.length,c,u[0]);for(let t=0;t<U;t++)C(t,(t,n)=>{for(let e=0;e<b;e++)D(null,q(m(e)),M[e],t,n,m(e))}),yield++T/x;const L=new e(256);h(L,n,o,r.colour,r.alpha,{colorSpace:s}),y(L,{alpha:r.alpha});const N=new e(4096);N.write(1,1),N.write(1,1);const _=Z(N,d),P=Y(N,_,E,t=>u.indexOf(t)),O=t=>P[u.indexOf(t)+1].code;k(N,{nt:!0,et:v}),g&&D(N,O(u[0]),g,i.colours.length,c,u[0]);const R=[];if(l.U)C(0,(t,n)=>{for(let e=0;e<b;e++)D(N,O(m(e)),M[e],t,n,m(e))}),yield 1,R.push(N.finish());else{R.push(N.finish());for(let t=0;t<l.A*l.m+1;t++)R.push(new Uint8Array(0));for(let t=0;t<U;t++)C(t,(t,n)=>{const o=new e(t*n*b+64);k(o,{nt:!0,et:[]});for(let e=0;e<b;e++)D(o,O(m(e)),M[e],t,n,m(e));R.push(o.finish())}),yield++T/x}return p(L,R)}const K=(t,n)=>t+n+(t>n?1:0)>>1;function V(t,n,e){const o=t>=n&&n>=e?1:t<=n&&n<=e?-1:0;if(!o)return 0;let r=(4*(t*=o)-3*(e*=o)-(n*=o)+6)/12|0;return r-(1&r)>2*(t-n)&&(r=2*(t-n)+1),r+(1&r)>2*(n-e)&&(r=2*(n-e)),o*r}function W(t,n){const{w:e,h:o,data:r}=t,s=n?e:o,c=s+1>>1,i=s-c,l=n?c:e,f=n?o:c,a=n?i:e,u=n?o:i,h=new Int16Array(l*f),y=new Int16Array(a*u),w=n?1:e;for(let t=0;t<f;t++)for(let o=0;o<l;o++){const c=n?o:t,f=n?t*e+2*o:2*t*e+o;if(c===i){h[t*l+o]=r[f];continue}const u=r[f],p=r[f+w],d=K(u,p),M=c+1<i?K(r[f+2*w],r[f+3*w]):1&s?r[f+2*w]:d;h[t*l+o]=d,y[t*a+o]=u-p-V(c?r[f-w]:d,d,M)}const p=(e,o,r)=>({w:o,h:r,rt:t.rt+ +n,st:t.st+!n,data:e});return[p(h,l,f),p(y,a,u)]}function $(t){const n=[],e=t.length;let o=t[0].w,r=t[0].h;for(e>2&&t[1].w===o&&t[1].h===r&&(n.push({$:!0,tt:!1,D:1,H:2}),n.push({$:!1,tt:!1,D:1,H:2})),!(o>r)&&r>8&&(n.push({$:!1,tt:!0,D:0,H:e}),r=r+1>>1);o>8||r>8;)o>8&&(n.push({$:!0,tt:!0,D:0,H:e}),o=o+1>>1),r>8&&(n.push({$:!1,tt:!0,D:0,H:e}),r=r+1>>1);return n}const tt=1024,nt=8192;function*et(t,n,o,{quality:r=90,shape:s=F(t,n,o,{palette:!1}),colorSpace:c}={}){const i=function(t){return t>=100?0:t>=30?.1+.09*(100-t):53/3e3*t*t-1.15*t+25}(r);if(!(i>0))throw new Error("lossy encoding needs a quality below 100");const{colour:l,alpha:f,channels:a}=s,u=n*o,d=Array.from({length:a},()=>new Int16Array(u));for(let n=0,e=0;e<u;e++,n+=4){if(3===l){const o=t[n],r=t[n+1],s=t[n+2],c=o-s,i=s+(c>>1),l=r-i;d[0][e]=i+(l>>1),d[1][e]=c,d[2][e]=l}else d[0][e]=t[n];f&&(d[a-1][e]=t[n+3])}const M=d.map((t,e)=>({w:n,h:o,rt:0,st:0,data:t,component:3===l?e:0===e?0:3})),A=1===n&&o>tt&&3===l,g=A?$(M).filter(t=>!t.$):1!==n&&1!==o||3!==l?[]:$(M),m=function(t,n=$(t)){const e=t.map(t=>({...t,level:0,ct:!1}));for(const t of n){const n=t.D+t.H-1,o=t.tt?n+1:e.length;for(let r=t.D;r<=n;r++){const[n,s]=W(e[r],t.$),c=e[r].level+1;e[r]={...n,level:c,ct:e[r].ct,component:e[r].component},e.splice(o+(r-t.D),0,{...s,level:c,ct:!0,component:e[r].component})}}return e}(M,g.length?g:void 0),b=t=>t.ct?0:J,U=t=>function(t,n,e,o){let r=Math.min(16,n+e);if(r>0&&r--,3===t)return 1;const s=.25*function(t){let n=1;for(let e=0;e<24;e++)n=(4*n+t/(n*n*n*n))/5;return t*n}(o),c=1<<r,i=1===t||2===t?.35*s*Math.max(.5,1024/c):.35*s*1.1*(163.84/c);return Math.max(1,Math.floor(i))}(t.component,t.rt+(A&&t.component>0&&t.component<3?1:0),t.st,i),I=[...3===l?[{type:"rct",D:0,F:6}]:[],{type:"squeeze",W:g}],S=w(n,o,tt),x=S.A*S.m,C=S.p*S.M;let T=0;for(;T<m.length&&m[T].w<=tt&&m[T].h<=tt;)T++;const v=(t,n,e,o)=>{const r=n>>t.rt,s=e>>t.st;return{x:r,y:s,w:Math.min(t.w,r+(o>>t.rt))-r,h:Math.min(t.h,s+(o>>t.st))-s}},E=[{index:0,Y:0,it:[]}];for(let t=0;t<T;t++)E[0].it.push({c:t,rect:{x:0,y:0,w:m[t].w,h:m[t].h}});if(!S.U){for(let t=0;t<x;t++){const n=t%S.A,e=t/S.A|0,o={index:1+t,Y:1+x+t,it:[]};for(let t=T;t<m.length;t++){const r=m[t];if(Math.min(r.rt,r.st)<3)continue;const s=v(r,n*nt,e*nt,nt);s.w>0&&s.h>0&&o.it.push({c:t,rect:s})}E.push(o)}for(let t=0;t<C;t++){const n=t%S.p,e=t/S.p|0,o={index:2+x+t,Y:1+3*x+17+t,it:[]};for(let t=T;t<m.length;t++){const r=m[t];if(Math.min(r.rt,r.st)>2)continue;const s=v(r,n*tt,e*tt,tt);s.w>0&&s.h>0&&o.it.push({c:t,rect:s})}E.push(o)}}for(const t of E)for(const n of t.it){const t=m[n.c];n.lt=G(b(t),0,U(t)),n.lt.channel=n.c}const q=E.filter(t=>t.it.length),L=1===q.length?j(q[0].it.map(t=>t.lt)):function(t){return R(t,1)}(q.map(t=>({Y:t.Y,k:j(t.it.map(t=>t.lt))}))),X=new Int16Array(1048576),N=t=>{const n=m[t.c],e=t.rect;if(e.w===n.w&&e.h===n.h)return n.data;for(let t=0;t<e.h;t++)X.set(n.data.subarray((e.y+t)*n.w+e.x,(e.y+t)*n.w+e.x+e.w),t*e.w);return X},_=[...new Set(E.flatMap(t=>t.it.map(t=>t.c)))].sort((t,n)=>t-n),P=new Map(_.map((t,n)=>[t,n])),O=_.map(()=>new Uint32Array(z)),B=1+2*E.length;let Q=1;yield Q/B;for(const t of E){for(const n of t.it)D(null,O[P.get(n.c)],N(n),n.rect.w,n.rect.h,n.lt);yield++Q/B}const H=new e(256);h(H,n,o,l,f,{colorSpace:c}),y(H,{alpha:f,shift:n>256||o>256?3:1});const K=Array.from({length:S.U?1:2+x+C},()=>null),V=K[0]=new e(65536);V.write(1,1),V.write(1,1);const et=Y(V,Z(V,L),O,t=>P.get(t.channel));k(V,{nt:!0,et:I});for(const t of E){let n=K[t.index];n||(n=K[t.index]=new e(4096),t.it.length&&k(n,{nt:!0,et:[]}));for(const e of t.it)D(n,et[P.get(e.c)+1].code,N(e),e.rect.w,e.rect.h,e.lt);yield++Q/B}return p(H,function(t){return t.map(t=>t?t.finish():new Uint8Array(0))}(K))}const ot=[-500,-392,-255,-191,-127,-95,-63,-47,-31,-23,-15,-11,-7,-4,-3,-1,0,1,3,5,7,11,15,23,31,47,63,95,127,191,255,392,500],rt=Int8Array.from({length:1003},(t,n)=>ot.filter(t=>t<n-501).length),st=Int32Array.from({length:64},(t,n)=>Math.floor(16777216/(n+1))),ct=(t,n)=>{const e=Math.max(0,26-Math.clz32(t+1));return 4+(n*st[t>>e]>>e)},it=[12,13].map(t=>Int32Array.from({length:2048},(n,e)=>ct(e,t))),lt=(t,n)=>t<2048?it[n-12][t]:ct(t,n),ft=new Int32Array(ot.length+1);let at;function ut(t,n,e,o,s,c=0,i=ft,l,f){const a=o+2,u=at&&at[0].length>=2*a?at:Array.from({length:6},(t,n)=>new Int32Array(n<5?2*a:8));at=null;for(const t of u)t.fill(0);const[h,y,w,p,d,M]=u;let A=0;const g=(e,o,r=P,s=0)=>{t?q(t,n[e],r,o,s):v(r,o,n[e],s)},m=()=>{if(A){if(A>_.minLength)g(M[0],0),g(M[1],A-_.minLength-1,_.O,_.G);else for(let t=0;t<A;t++)g(M[t],0);A=0}};for(let t=0,n=0;t<s;t++){const s=1&t?0:a,u=1&t?a:0;for(let a=0;a<o;a++,n++){const b=a?e[n-1]:t?e[n-o]:0,U=t?e[n-o]:b,I=a+1<o&&t?e[n-o+1]:U,S=u+a,x=a<o-1?S+1:S,C=a?S-1:S;let T=lt(h[S]+h[x]+h[C],13),v=lt(y[S]+y[x]+y[C],12),E=lt(w[S]+w[x]+w[C],12),q=lt(p[S]+p[x]+p[C],12);const L=8*U,X=8*b,J=8*I,N=a?d[s+a-1]:0,_=d[S],P=d[C],z=d[x],G=_+N;let O=N;Math.abs(_)>Math.abs(O)&&(O=_),Math.abs(P)>Math.abs(O)&&(O=P),Math.abs(z)>Math.abs(O)&&(O=z);const R=i[rt[O<-501?0:O>501?1002:O+501]];f&&(f[n]=O);const j=X+J-L,Z=L-(16*(G+z)>>5),B=X-(10*(G+P)>>5),k=L-(7*(P+_+z)>>5),Y=27-Math.clz32(T+v+E+q);T>>=Y,v>>=Y,E>>=Y,q>>=Y;const D=T+v+E+q;let F=Math.floor(((D>>1)-1+j*T+Z*v+B*E+k*q)*st[D-1]/16777216);if((_^N|_^P)<=0){const t=Math.min(X,J,L),n=Math.max(X,J,L);F=F<t?t:F>n?n:F}const Q=e[n],H=Q-(F+3>>3)-c;l?l[n]=r(H):0===H?(A<8&&(M[A]=R),A++):(m(),g(R,r(H)));const K=8*Q,V=Math.abs(j-K)+3>>3,W=Math.abs(Z-K)+3>>3,$=Math.abs(B-K)+3>>3,tt=Math.abs(k-K)+3>>3;d[s+a]=F-K,h[s+a]=V,y[s+a]=W,w[s+a]=$,p[s+a]=tt,h[u+a+1]+=V,y[u+a+1]+=W,w[u+a+1]+=$,p[u+a+1]+=tt}}m(),at=u}const ht=t=>Math.max(0,t<224?t-1:t-236),yt=t=>{const n=I(t),o=new e(128);S(o,n);let r=o.u;for(let e=0;e<t.length;e++)t[e]&&(r+=t[e]*(n.C[e]+ht(e)));return r};function wt(t,n,e,o,s){const c=new Uint32Array(n*e);if(6===o)return ut(null,null,t,n,e,0,void 0,c,s),c;for(let s=0,i=0;s<e;s++)for(let e=0;e<n;e++,i++){const l=e?t[i-1]:s?t[i-n]:0,f=s?t[i-n]:l,a=e&&s?t[i-n-1]:l,u=3===o?(l+f)/2|0:Math.max(Math.min(l,f),Math.min(Math.max(l,f),l+f-a));c[i]=r(t[i]-u)}return c}function pt(t,n,e=1){const o=Array.from({length:e},()=>new Uint32Array(z)),r=[],s=(t,e,s=0)=>{const c=n?n[t]:0;r.push(e,s,c),v(s?_.O:P,e,o[c],s)};for(let n=0;n<t.length;){let e=1;if(!t[n])for(;n+e<t.length&&!t[n+e];)e++;if(s(n,t[n]),e>_.minLength)s(n+1,e-1-_.minLength,_.G);else for(let t=1;t<e;t++)s(n+t,0);n+=e}return{ot:o,it:r}}function dt(t,n,e,o,r){const s=pt(t,Uint8Array.from(n,t=>{let n=0,e=r.length;for(;n<e;){const o=n+e>>1;t>r[o]?n=o+1:e=o}return n}),r.length+1),c=s.ot.length,i=s.ot.map(t=>t.some(t=>t)),l=new Float64Array(c+1).fill(1/0),f=new Uint8Array(c+1);l[0]=0;for(let t=1;t<=c;t++){const n=new Uint32Array(z);let e=0;for(let o=t-1;o>=0;o--){if(i[o]){for(let t=0;t<z;t++)n[t]+=s.ot[o][t];e=yt(n)}const r=l[o]+e+24;r<l[t]&&(l[t]=r,f[t]=o)}}const a=[];for(let t=c;t;t=f[t])a.unshift([f[t],t]);const u=a.map(()=>G(e)),h=new Uint8Array(c),y=a.map(([t,n],e)=>{const o=new Uint32Array(z);for(let r=t;r<n;r++){h[r]=e;for(let t=0;t<z;t++)o[t]+=s.ot[r][t]}return o});for(let t=2;t<s.it.length;t+=3)s.it[t]=h[s.it[t]];const w=(t,n)=>{if(t===n)return u[t];const e=t+n>>1;return O(o,r[a[e][1]-1],w(e+1,n),w(t,e))};return{leaves:u,k:w(0,a.length-1),ot:y,it:s.it}}const Mt=[-127,-31,-7,-1,0,1,7,31,127],At=[-191,-127,-63,-31,-7,-1,0,7,31,63,127,191,255];function gt(t,n,e,o){const r=new Int32Array(n*e);for(let s=0,c=0;s<e;s++)for(let e=0;e<n;e++,c++){const i=e?t[c-1]:s?t[c-n]:0,l=s?t[c-n]:i,f=e&&s?t[c-n-1]:i;r[c]=9===o?i+l-f:i-f}return r}function mt(t){const n=new e(512),o=Y(n,Z(n,t.k),t.ot,n=>t.leaves.indexOf(n));let r=n.u;return t.ot.forEach((t,n)=>{const{C:e}=o[n+1].code;for(let n=0;n<t.length;n++)r+=t[n]*((e[n]||0)+ht(n))}),r}function bt(t,n,e,o){return t.map(t=>{let r;for(const s of[5,3,6]){const c=6===s?new Int32Array(n*e):null,i=wt(t,n,e,s,c),l=G(s),f=[{...pt(i),leaves:[l],k:l}];if(c&&f.push(dt(i,c,s,15,Mt)),o>=6&&5===s)for(const o of[9,10]){const r=gt(t,n,e,o),c=9===o?At:Mt;f.push(dt(i,r,s,o,c))}for(const t of f){const n=mt(t);(!r||n<r.ft)&&(r={...t,ft:n})}}return r})}function Ut(t,n){const e=n.flatMap(t=>t.leaves),o=Y(t,Z(t,j(n.map(t=>t.k))),n.flatMap(t=>t.ot),t=>e.indexOf(t));return()=>{for(let r=0;r<n.length;r++){const{it:s}=n[r],c=n[r].leaves.map(t=>o[e.indexOf(t)+1].code);for(let n=0;n<s.length;n+=3){const e=s[n+1];q(t,c[s[n+2]],e?_.O:P,s[n],e)}}}}function*It(t,n,o,r,s,c=4,i=!1){const{channels:f}=r,u=w(n,o),d=u.p*u.M,M=i?r.palette:null;if(i&&!M)return null;const A=Array.from({length:M?1:f},()=>new Int16Array(65536)),g=new e(256);h(g,n,o,r.colour,r.alpha,{colorSpace:s}),y(g,{alpha:r.alpha});const m=M?[{type:"palette",D:0,H:f,K:M.colours.length}]:f>=3?[{type:"rct",D:0,F:6}]:[];let b;if(M){const t=M.colours.length,n=new Int16Array(t*f);M.colours.forEach((e,o)=>{for(let r=0;r<f;r++)n[r*t+o]=M.byte(e,r)}),b=bt([n],t,f,c)}const U=new e(4096),I=[];let S=0;const x=t=>{S+=Math.ceil(t.u/8),l(S),I.push(t.finish())};if(U.write(1,1),U.write(1,1),!u.U){let t;if(b)t=Ut(U,b);else{const t=A.map(()=>G(5));Y(U,Z(U,j(t)),A.map(()=>new Uint32Array(z)),n=>t.indexOf(n))}k(U,{et:m}),t&&t(),x(U);for(let t=0;t<u.A*u.m+1;t++)I.push(new Uint8Array(0))}for(let r=0;r<d;r++){const s=r%u.p*a,i=(r/u.p|0)*a,l=Math.min(a,n-s),h=Math.min(a,o-i);for(let e=0;e<h;e++)for(let o=0,r=e*l,c=4*((i+e)*n+s);o<l;o++,r++,c+=4)if(M)A[0][r]=M.index.get(M.key(c));else if(f>=3){const n=t[c]-t[c+2],e=t[c+2]+(n>>1),o=t[c+1]-e;A[0][r]=e+(o>>1),A[1][r]=n,A[2][r]=o,4===f&&(A[3][r]=t[c+3])}else A[0][r]=t[c],2===f&&(A[1][r]=t[c+3]);const y=bt(A,l,h,c),w=M&&u.U?[...b,...y]:y,p=u.U?U:new e(l*h*A.length+256);u.U||k(p,{nt:!1});const g=Ut(p,w);if(u.U&&k(p,{et:m}),g(),x(p),yield(r+1)/d)return null}return p(g,I)}function St(t,n,e,o){return function(t){let n;for(;!(n=t.next()).done;);return n.value}(xt(t,n,e,o))}function xt(n,e,r,s){const{quality:c,colorSpace:a}=function(t={},n=100){if(null===t||"object"!=typeof t)throw i("JXL_INPUT","Options are an object: {quality, colorSpace}.");if(void 0!==t.quality&&(n=t.quality),"number"!=typeof n||!Number.isFinite(n)||n<1||n>100)throw i("JXL_INPUT","Quality is a number from 1 to 100.");const e=void 0===t.colorSpace?"srgb":t.colorSpace;if("srgb"!==e&&"display-p3"!==e)throw i("JXL_INPUT","The colour space is srgb or display-p3.");return{quality:n,colorSpace:e}}(s),u=void 0===s?.effort?1:s.effort;if(!Number.isInteger(u)||u<1||u>9)throw i("JXL_INPUT","Effort is a whole number from 1 to 9.");return function(n,e,o,r){if(function(n,e,o=t){if(!Number.isInteger(n)||!Number.isInteger(e)||n<1||e<1)throw i("JXL_INPUT","A picture is at least one pixel wide and high, in whole pixels.");if(n>o.edge||e>o.edge)throw i("JXL_DIMENSIONS","A picture is at most "+o.edge+" pixels on a side.");if(n*e>o.pixels)throw i("JXL_DIMENSIONS","A picture is at most "+o.pixels.toLocaleString("en-US")+" pixels.")}(e,o,r),!(n instanceof Uint8Array||n instanceof Uint8ClampedArray))throw i("JXL_INPUT","Pixels are a Uint8Array or Uint8ClampedArray of RGBA bytes.");if(n.length!==e*o*4)throw i("JXL_INPUT","Pixels are width * height * 4 bytes: straight (not premultiplied) RGBA, row by row.")}(n,e,r),function(t){const n=function*(){let e,o;for(;!(e=f(()=>t.next(n.hurry))).done;)yield o=e.value;return o<1&&(yield 1),n.bytes=function(t){return l(t.length),t}(e.value)}();return n.bytes=null,n.hurry=!1,n}(function*(t,n,e,r,s,c){const i=F(t,n,e);if(r<100){if(!i.palette)return yield*et(t,n,e,{quality:r,shape:i,colorSpace:s});const c=yield*o(Q(t,n,e,{shape:i,colorSpace:s}),0,2);let l;try{l=yield*o(et(t,n,e,{quality:r,shape:i,colorSpace:s}),1,2)}catch(t){if("JXL_SIZE"!==t.code&&(!(t instanceof RangeError)||t.code))throw t;l=c}return c.length<=l.length?c:l}if(c<2)return yield*Q(t,n,e,{shape:i,colorSpace:s});let l=yield*o(Q(t,n,e,{shape:i,colorSpace:s}),0,2);if(c>=4){const o=[Et(t,n,e,i.palette?{...i,palette:null}:i,s,3)];i.palette&&o.push(It(t,n,e,i,s,4,!0)),c>=6&&(o.push(It(t,n,e,i,s,6)),i.palette&&o.push(It(t,n,e,i,s,6,!0)));for(let t=0;t<o.length;t++)try{let n,e=!1;for(;!(n=o[t].next(e)).done;)e=yield.5+(t+n.value)/(2*o.length);if(n.value&&n.value.length<l.length&&(l=n.value),e)return l}catch(t){if("JXL_SIZE"!==t.code&&(!(t instanceof RangeError)||t.code))throw t}return l}try{const r=yield*o(Et(t,n,e,i.palette?{...i,palette:null}:i,s,c),1,2);r&&r.length<l.length&&(l=r)}catch(t){if("JXL_SIZE"!==t.code&&(!(t instanceof RangeError)||t.code))throw t}return l}(n,e,r,c,a,u))}const Ct=t=>Math.max(0,t<224?t-1:t-236);function Tt(t){const n=I(t),o=new e(128);S(o,n);let r=o.u;for(let e=0;e<t.length;e++)t[e]&&(r+=t[e]*(n.C[e]+Ct(e)));return r}const vt=t=>{const n=new Uint32Array(z);for(const e of t)for(let t=0;t<z;t++)n[t]+=e[t];return n};function*Et(t,n,o,r,s,c){const{channels:i}=r,l=w(n,o),f=l.p*l.M,u=Array.from({length:i},()=>new Int16Array(65536)),d=(e,o,r,s)=>{for(let c=0;c<s;c++){let s=4*((o+c)*n+e),l=c*r;for(let n=0;n<r;n++,s+=4,l++)if(i>=3){const n=t[s],e=t[s+1],o=t[s+2],r=n-o,c=o+(r>>1),f=e-c;u[0][l]=c+(f>>1),u[1][l]=r,u[2][l]=f,4===i&&(u[3][l]=t[s+3])}else u[0][l]=t[s],2===i&&(u[1][l]=t[s+3])}},M=(t,e)=>{const r=t%l.p*a,s=(t/l.p|0)*a,c=Math.min(a,n-r),i=Math.min(a,o-s);d(r,s,c,i),e(c,i)},A=Math.min(n,32),g=Math.min(o,32),m=[J,X].map(t=>({R:t,ot:u.map(()=>new Uint32Array(z))}));for(const t of[0,.5,1]){d(Math.floor((n-A)*t),Math.floor((o-g)*t),A,g);for(const t of m)for(let n=0;n<i;n++)D(null,t.ot[n],u[n],A,g,G(t.R))}const b=u.map((t,n)=>G(m[Tt(m[1].ot[n])<Tt(m[0].ot[n])?1:0].R)),U=ot.length+1,I=Int32Array.from({length:U},(t,n)=>n),S=u.map(()=>new Uint32Array(z)),x=u.map(()=>Array.from({length:U},()=>new Uint32Array(z)));let C=0;for(let t=0;t<f;t++)if(M(t,(t,n)=>{for(let e=0;e<i;e++)D(null,S[e],u[e],t,n,b[e]),ut(null,x[e],u[e],t,n,0,I)}),yield++C/(2*f))return null;const T=u.map((t,n)=>{const e={ut:Tt(S[n]),leaves:[b[n]],ot:[S[n]],cuts:[]},o=vt(x[n]),r=Tt(o);return r<e.ut?{ut:r,leaves:[G(6)],ot:[o],cuts:[]}:e}),v=c<3?T:T.map((t,n)=>{const e=x[n].map(t=>t.some(t=>t)),o=new Float64Array(U+1).fill(1/0),r=new Int32Array(U+1);o[0]=0;for(let t=1;t<=U;t++){const s=new Uint32Array(z);let c=0;for(let i=t-1;i>=0;i--){if(e[i]){const t=x[n][i];for(let n=0;n<z;n++)s[n]+=t[n];c=Tt(s)}const l=o[i]+c+24;l<o[t]&&(o[t]=l,r[t]=i)}}if(!(o[U]<t.ut))return t;const s=[];for(let t=U;t>0;t=r[t])s.unshift([r[t],t]);return{ut:o[U],leaves:s.map(()=>G(6)),ot:s.map(([t,e])=>vt(x[n].slice(t,e))),cuts:s.slice(0,-1).map(([,t])=>ot[t-1]),ht:Int32Array.from({length:U},(t,n)=>s.findIndex(([t,e])=>n>=t&&n<e))}}),E=[T,v].filter((t,n)=>n?t.some((t,n)=>t!==T[n]):t.some((t,n)=>t.leaves[0]!==b[n]));if(!E.length)return null;const q=t=>({k:j(t.map(t=>((t,n)=>{const e=(o,r)=>{if(o===r)return t[o];const s=o+r>>1;return O(15,n[s],e(s+1,r),e(o,s))};return e(0,t.length-1)})(t.leaves,t.cuts))),leaves:t.flatMap(t=>t.leaves),ot:t.flatMap(t=>t.ot)}),L=t=>{const{k:n,leaves:o,ot:r}=q(t),s=new e(4096),c=Y(s,Z(s,n),r,t=>o.indexOf(t));let i=s.u;return r.forEach((t,n)=>{const{C:e}=c[n+1].code;for(let n=0;n<t.length;n++)t[n]&&(i+=t[n]*(e[n]+Ct(n)))}),i};let N=E;if(E.length>1){const[t,n]=E.map(L),e=27*(l.U?1:f+1)+8;n+e<=t?N=[E[1]]:t+e<=n?N=[E[0]]:n<t&&(N=[E[1],E[0]])}let _=0,P=!1;const R=()=>.5+ ++_/(2*N.length*f);let B=null;for(const t of N){const n=yield*F(t);if(!n)return B;if((!B||n.length<B.length||n.length===B.length&&t===E[0])&&(B=n),P)return B}return B;function*F(t){const{k:c,leaves:a,ot:w}=q(t),d=i>=3?[{type:"rct",D:0,F:6}]:[],A=new e(256);h(A,n,o,r.colour,r.alpha,{colorSpace:s}),y(A,{alpha:r.alpha});const g=new e(4096);g.write(1,1),g.write(1,1);const m=Y(g,Z(g,c),w,t=>a.indexOf(t));k(g,{nt:!0,et:d});const b=t.map(t=>t.leaves.map(t=>m[a.indexOf(t)+1].code)),U=(n,e,o,r)=>{const s=t[e];6===s.leaves[0].R?ut(n,b[e],u[e],o,r,0,s.ht):D(n,b[e][0],u[e],o,r,s.leaves[0])},I=[];if(l.U)M(0,(t,n)=>{for(let e=0;e<i;e++)U(g,e,t,n)}),P=yield R(),I.push(g.finish());else{I.push(g.finish());for(let t=0;t<l.A*l.m+1;t++)I.push(new Uint8Array(0));for(let t=0;t<f;t++)if(M(t,(t,n)=>{const o=new e(t*n*i+64);k(o,{nt:!0,et:[]});for(let e=0;e<i;e++)U(o,e,t,n);I.push(o.finish())}),(P=yield R())&&t+1<f)return null}return p(A,I)}}export{t as LIMITS,St as encode,xt as encodeSteps};
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}),n=[1];for(let t=1;t<=32;t++)n.push(2*n[t-1]);class e{constructor(t=4096){this.bytes=new Uint8Array(t),this.at=0,this.t=0,this.pending=0}write(t,e){if(!(e>=0&&e<n[t]))throw new Error("bit write out of range: "+t+" bits, "+e);let r=this.t+e*(1<<this.pending),o=this.pending+t;this.at+5>=this.bytes.length&&this.grow();const s=this.bytes;for(;o>=8;)s[this.at++]=255&r,r=Math.floor(r/256),o-=8;this.t=r,this.pending=o}o(t,e){for(let r=0;r<4;r++){const[o,s]=t[r];if(e>=s&&e-s<n[o])return this.write(2,r),void(o&&this.write(o,e-s))}throw new Error("U32 value out of range: "+e)}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(n=0){const e=t.bytes+64;if(this.at+n+16>e)throw Object.assign(new Error("The encoded picture exceeds 16 MiB."),{code:"JXL_SIZE"});const r=new Uint8Array(Math.min(e,Math.max(2*this.bytes.length,this.at+n+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 n=0;n<t.at;n++)this.write(8,t.bytes[n]);else this.bytes.set(t.bytes.subarray(0,t.at),this.at),this.at+=t.at;t.pending&&this.write(t.pending,t.t)}finish(){const t=this.at+(this.pending?1:0),n=this.bytes.slice(0,t);return this.pending&&(n[this.at]=this.t),n}}function*r(t,n,e){let r,s;for(;!(r=t.next(s)).done;)s=yield o(r.value,t=>(n+t)/e);return r.value}const o=(t,n)=>"number"==typeof t?n(t):{...t,at:e=>n(t.at(e))};function s(t){return t>=0?2*t:2*-t-1}function c(t){return 31-Math.clz32(t)}function l(t){return t<=1?0:32-Math.clz32(t-1)}const i=(t,n)=>Object.assign(new Error(n),{code:t});function f(n){if(n>t.bytes)throw i("JXL_SIZE","The encoded picture exceeds 16 MiB.")}function a(t){try{return t()}catch(t){if(t instanceof RangeError&&!t.code)throw i("JXL_MEMORY","Not enough memory for this picture.");throw t}}const u=256;function h(t,n){t.o([[9,1],[13,1],[18,1],[30,1]],n)}function y(t,n,e,r,o,{xyb:s=!1,orientation:c=1,colorSpace:l}={}){t.write(16,2815),t.write(1,0),h(t,e),t.write(3,0),h(t,n),t.write(1,0);const i=1!==c;t.write(1,i?1:0),i&&t.write(6,c-1),t.write(4,8),o?t.write(3,5):t.write(2,0),t.write(1,s?1:0),3===r&&"display-p3"===l?t.write(21,903568):3===r?t.write(1,1):t.write(15,14100),i&&t.write(1,1),t.write(2,0),t.write(1,1),t.i()}function p(t,{alpha:n,shift:e=1}){t.write(9,8),n&&t.write(2,0),t.write(2,e),function(t,n){t.write(n?7:5,0),t.write(11,1)}(t,n)}function w(t,n,e=256){const r=Math.ceil(t/e),o=Math.ceil(n/e);return{p:r,A:o,M:Math.ceil(t/(8*e)),m:Math.ceil(n/(8*e)),U:1===r&&1===o}}function d(t,n,e,r){const o=r%t.p*u,s=(r/t.p|0)*u;return[o,s,Math.min(u,n-o),Math.min(u,e-s)]}function*A(t,n,e,r){if(t.pooled){const n=yield t;return n?(n.results.forEach((t,n)=>r(n,t)),n.hurried):null}let o=!1;for(let r=0;r<n;r++)if(e(r),o=yield t.at(r),o&&t.stop(r))return null;return o}function M(t,n){t.forEach((t,e)=>{const r=n[e];for(let n=0;n<t.length;n++)t[n]+=r[n]})}function g(t,n){const e=n.map(t=>t.length);!function(t,n){t.write(1,0),t.i();for(const e of n)t.o([[10,0],[14,1024],[22,17408],[30,4211712]],e);t.i()}(t,e);const r=t.finish(),o=r.length+e.reduce((t,n)=>t+n,0);f(o);const s=new Uint8Array(o);s.set(r);let c=r.length;for(const t of n)s.set(t,c),c+=t.length;return s}const b=[1,2,3,4,0,5,17,6,16,7,8,9,10,11,12,13,14,15],m=[2,4,3,2,2,4],U=[0,7,3,2,1,15];function I(t,n){const e=new Uint8Array(t.length),r=[];for(let n=0;n<t.length;n++)t[n]>0&&r.push(n);if(!r.length)return e;if(1===r.length)return e[r[0]]=1,e;for(let o=1;;o*=2){const s=r.map(n=>({w:Math.max(t[n],o),I:n,l:null,r:null})).sort((t,n)=>t.w-n.w||t.I-n.I),c=[];let l=0,i=0;const f=()=>l<s.length&&(i>=c.length||s[l].w<=c[i].w)?s[l++]:c[i++];for(;s.length-l+(c.length-i)>1;){const t=f(),n=f();c.push({w:t.w+n.w,I:-1,l:t,r:n})}const a=[[f(),0]];let u=0;for(;a.length;){const[t,n]=a.pop();t.I>=0?(e[t.I]=n,n>u&&(u=n)):a.push([t.l,n+1],[t.r,n+1])}if(u<=n)return e}}function S(t,n,e,r,o,s){const c=[];for(;c.push(r&o),r>>=s;)r--;for(let r=c.length-1;r>=0;r--)t.push(e),n.push(c[r])}function v(t,n){let e=0;for(let r=0;r<n;r++)e=e<<1|1&t,t>>=1;return e}function x(t){const n=new Uint32Array(17),e=new Uint32Array(17),r=new Uint16Array(t.length);for(const e of t)n[e]++;n[0]=0;for(let t=1,r=0;t<=16;t++)r=r+n[t-1]<<1,e[t]=r;for(let n=0;n<t.length;n++)t[n]&&(r[n]=v(e[t[n]]++,t[n]));return r}function C(t){const n=[];for(let e=0;e<t.length;e++)t[e]>0&&n.push(e);const e=n.length,r=n[e-1]+1||1,o=e<=4?new Uint8Array(r):I(t,15).subarray(0,r);let s=0;if(e<=4){n.sort((n,e)=>t[e]-t[n]||n-e),s=4===e&&t[n[0]]>t[n[2]]+t[n[3]]?1:0;const r=e<2?[0]:2===e?[1,1]:3===e?[1,2,2]:s?[1,2,3,3]:[2,2,2,2];n.forEach((t,n)=>{o[t]=r[n]}),n.sort((t,n)=>o[t]-o[n]||t-n)}return{S:r,v:o,C:x(o),T:e&&e<=4?n:null,q:s}}function T(t,n){if(n.S<=1)return;if(n.T){const e=c(n.S-1)+1;t.write(2,1),t.write(2,n.T.length-1);for(const r of n.T)t.write(e,r);return void(4===n.T.length&&t.write(1,n.q))}const e=n.v,r=[],o=[];let s=e.length-1;for(;s>0&&!e[s];)s--;let l=8;for(let t=0;t<=s;){const n=e[t];let c=1;for(;t+c<=s&&e[t+c]===n;)c++;let i=c;if(n&&n!==l&&(r.push(n),o.push(0),i--,l=n),i<3)for(let t=0;t<i;t++)r.push(n),o.push(0);else S(r,o,n?16:17,i-3,n?3:7,n?2:3);t+=c}const i=new Uint32Array(18);for(const t of r)i[t]++;let f=0;for(const t of i)t&&f++;1===f&&(i[r[0]?0:1]=i[r[0]]);const a=I(i,5),u=x(a);let h=18;for(;h>0&&!a[b[h-1]];)h--;t.write(2,0);for(let n=0;n<h;n++){const e=a[b[n]];t.write(m[e],U[e])}for(let n=0;n<r.length;n++){const e=r[n];t.write(a[e],u[e]),16===e?t.write(2,o[n]):17===e&&t.write(3,o[n])}}function q(t,n=0,e=0){return{split:t,L:n,X:e,J:1<<t}}function E(t,n,e=15){t.write(l(e+1),n.split),n.split!==e&&(t.write(l(n.split+1),n.L),t.write(l(n.split-n.L+1),n.X))}function L(t,n,e){if(n<t.J)return e[0]=n,e[1]=0,void(e[2]=0);const r=c(n),o=n-(1<<r),s=r-t.L-t.X;e[0]=t.J+(r-t.split<<t.L+t.X|o>>r-t.L<<t.X|o&(1<<t.X)-1),e[1]=s,e[2]=n>>t.X&(1<<s)-1}function X(t,n,e,r=0){L(t,n,J),e[r+J[0]]++}const J=[0,0,0];function N(t,n,e,r,o=0){L(e,r,J);const s=o+J[0];t.write(n.v[s],n.C[s]),J[1]&&t.write(J[1],J[2])}function _(t,{N:n=null,_:e,P:r}){t.write(1,n?1:0),n&&(t.o([[0,224],[0,512],[0,4096],[15,8]],n.G),t.o([[0,3],[0,4],[2,5],[8,9]],n.minLength),E(t,n.O,8)),e.length>1&&function(t,n){let e=0;for(const t of n)e=Math.max(e,t);const r=e?l(e+1):0;if(r<=3){if(t.write(1,1),t.write(2,r),r)for(const e of n)t.write(r,e);return}t.write(1,0),t.write(1,0);const o=q(8),s=q(4),c=n.length>=64,i=new Uint32Array(c?257:e+1),f=[];for(let t=0;t<n.length;){let e=1;for(;c&&t+e<n.length&&n[t+e]===n[t];)e++;if(e>=8)f.push([n[t],e-1]),i[n[t]]++,X(s,e-1-7,i,224),t+=e;else{for(let r=0;r<e;r++)f.push([n[t],0]),i[n[t]]++;t+=e}}const a=C(i);if(!c){_(t,{_:new Uint8Array(1),P:[{config:o,code:a}]});for(const[n]of f)N(t,a,o,n);return}const u=new Uint32Array(1);u[0]=1,_(t,{N:{G:224,minLength:7,O:s},_:new Uint8Array([1,0]),P:[{config:q(0),code:C(u)},{config:o,code:a}]});for(const[n,e]of f)N(t,a,o,n),e&&N(t,a,s,e-7,224)}(t,e),t.write(1,1);for(const n of r)E(t,n.config,15);for(const n of r){const e=n.code.S-1;if(e){const n=c(e);t.write(1,1),t.write(4,n),t.write(n,e-(1<<n))}else t.write(1,0)}for(const n of r)T(t,n.code)}const P={channel:null,weighted:null,fill:null},z=3,G=5,O=q(4),k=Object.freeze({G:224,minLength:7,O:O}),j=q(0),R=257;function Z(t,n=0,e=1){return{k:t,offset:n,j:e,context:-1}}function B(t,n,e,r){return{R:t,Z:n,left:e,right:r}}function Y(t,n){const e=(r,o)=>{if(r===o)return n?t[r].B:t[r];const s=r+o>>1;return B(n,n?t[s].Y:s,e(s+1,o),e(r,s))};return e(0,t.length-1)}function D(t){return Y(t,0)}function F(t,n){const e=[n],r=[],o=[];for(const t of e){if(t.left){r.push(t.R+1,s(t.Z)),e.push(t.left,t.right);continue}t.context=o.length,o.push(t);const n=1===t.j?0:31-Math.clz32(t.j&-t.j);r.push(0,t.k,s(t.offset),n,(t.j>>n)-1)}const c=new Uint32Array(64);for(const t of r)X(j,t,c);const l=C(c);_(t,{_:new Uint8Array(6),P:[{config:j,code:l}]});for(const n of r)N(t,l,j,n);return o}function Q(t,n){const e=[[3,0],[6,8],[10,72],[13,1096]];if("rct"===n.type)t.write(2,0),t.o(e,n.D),t.o([[0,6],[2,0],[4,2],[6,10]],n.F??6);else if("palette"===n.type)t.write(2,1),t.o(e,n.D),t.o([[0,1],[0,3],[0,4],[13,1]],n.H),t.o([[8,0],[10,256],[12,1280],[16,5376]],n.K),t.o([[0,0],[8,1],[10,257],[16,1281]],n.V??0),t.write(4,n.k??0);else{if("squeeze"!==n.type)throw new Error("unknown transform "+n.type);{t.write(2,2);const r=n.W??[];t.o([[0,0],[4,1],[6,9],[8,41]],r.length);for(const n of r)t.write(1,n.$?1:0),t.write(1,n.tt?1:0),t.o(e,n.D),t.o([[0,1],[0,2],[0,3],[4,4]],n.H)}}}function H(t,{nt:n=!0,et:e=[]}={}){t.write(1,n?1:0),t.write(1,1),t.o([[0,0],[0,1],[4,2],[8,18]],e.length);for(const n of e)Q(t,n)}function K(t,n,e,r=t=>t.context,o=()=>j){const s=new Uint8Array(n.length+1);for(const t of n)s[t.context]=r(t)+1;const c=new Uint32Array(2);c[1]=1;const l=[{config:j,code:C(c)}],i=Math.max(...s);for(let t=0;t<i;t++)l.push({config:o(t),code:C(e[t])});return _(t,{N:k,_:s,P:l}),l}function V(t,n,e,r,o,c,l=!1){if(P.channel?.(t,n,e,r,o,c,l))return;const i=c.k,f=c.offset,a=c.j,u=c.config||j,h=O;let y=0;const p=(e,r=u,o=0)=>{t?N(t,n,r,e,o):l&&!o?n[e]++:X(r,e,n,l?n.length-33:o)},w=()=>{if(y){if(y>=8)p(0),p(y-7-1,h,224);else for(let t=0;t<y;t++)p(0);y=0}},d=(t,n)=>{const r=e[t];let o;1===a?o=r-n-f:(o=Math.round((r-n-f)/a),e[t]=n+f+o*a),0!==o?(w(),p(s(o))):y++};for(let t=0,n=0;t<o;t++)for(let o=0;o<r;o++,n++){let s;if(0===i)s=0;else{const c=o?e[n-1]:t?e[n-r]:0,l=t?e[n-r]:c;if(1===i)s=c;else if(2===i)s=l;else if(3===i)s=(c+l)/2|0;else{const f=c+l-(o&&t?e[n-r-1]:c);if(5===i){const t=c<l?c:l,n=c<l?l:c;s=f<t?t:f>n?n:f}else{if(4!==i)throw new Error("predictor "+i+" is not coded here");s=Math.abs(f-c)<Math.abs(f-l)?c:l}}}d(n,s)}w()}const W=[q(0),q(4),q(4,1,1),q(3,0,1)];function $(t){const n=t.length-33,r=[0,0,0];let o,s;for(const l of W){const i=new Uint32Array(R);let f=0;for(let e=0;e<n;e++)t[e]&&(L(l,e,r),i[r[0]]+=t[e],f+=t[e]*r[1]);for(let e=0;e<33;e++)i[224+e]=t[n+e],f+=t[n+e]*Math.max(0,e-12);const a=C(i),u=new e(128);E(u,l),T(u,a);let h=u.u+f+(1===a.S?1:5+c(a.S-1));for(let t=0;t<i.length;t++)i[t]&&(h+=i[t]*a.v[t]);const y={config:l,rt:i,ot:h,extra:f};o||(o=y),(!s||h<s.ot)&&(s=y)}return[o,s]}const tt=(t,n,e,r)=>e?r?t[n]+256*t[n+3]:t[n]:t[n]+256*t[n+1]+65536*t[n+2]+(r?16777216*t[n+3]:0);function nt(t,n,e,{palette:r=!0}={}){const o=n*e;let s=!0,c=!0;for(let n=0,e=0;e<o&&(255===t[n+3]||(s=!1,c))&&(t[n]===t[n+1]&&t[n]===t[n+2]||(c=!1,s));e++,n+=4);const l=c?1:3,i=!s,f=l+(i?1:0),a=n=>tt(t,n,c,i),u=new Map;let h=null;for(let t=0,n=0;r&&n<o;n++,t+=4){const n=a(t);if(0!==n&&!u.has(n)&&(u.set(n,1),u.size>=2048))break}if(r&&u.size<2048){const t=[...u.keys()],n=(t,n)=>t>>>8*n&255,e=t=>(.299*n(t,0)+.587*n(t,1)+.114*n(t,2)+.01)*(4===f?n(t,3):1);t.sort((t,n)=>e(t)-e(n)),h={colours:[0,...t],byte:n}}return{colour:l,alpha:i,channels:f,palette:h}}function et({channels:t,alpha:n,palette:e},r){const o=t-(n?1:0)==1,s=e&&new Map(e.map((t,n)=>[t,n]));return(c,l,i,f,a,u,h)=>{if(r&&!e&&t>=3)r.fill(c,l,i,f,a,u,h);else if(e||!P.fill?.(l,i,f,a,u,h,t,c))for(let r=0;r<h;r++){let h=4*((a+r)*i+f),y=r*u;for(let r=0;r<u;r++,h+=4,y++)if(e)c[0][y]=s.get(tt(l,h,o,n));else if(t>=3){const n=l[h],e=l[h+1],r=l[h+2],o=n-r,s=r+(o>>1),i=e-s;c[0][y]=s+(i>>1),c[1][y]=o,c[2][y]=i,4===t&&(c[3][y]=l[h+3])}else c[0][y]=l[h],2===t&&(c[1][y]=l[h+3])}}}function rt(t,n){const{leaves:r,picture:o,C:s}=t,c=et(t,n),l=o.map(()=>new Int16Array(65536));return(t,n,i,f,a,u,h)=>{if(c(l,t,n,i,f,a,u),!s)return void o.forEach((t,n)=>V(null,h[t],l[n],a,u,r[t],!0));const y=h||new e(a*u*o.length+64);return h||H(y,{nt:!0,et:[]}),o.forEach((t,n)=>V(y,s[t],l[n],a,u,r[t])),h?void 0:y.finish()}}function*ot(t,n,e,{shape:o=nt(t,n,e),colorSpace:s,rct:c,pooled:l}={}){let i,f;const a=o.palette?[o,{...o,palette:null}]:[o];for(let o=0;o<a.length;o++)try{const f=yield*r(st(t,n,e,a[o],s,c,l),o,a.length);(!i||f.length<i.length)&&(i=f)}catch(t){if("JXL_SIZE"!==t.code)throw t;f=t}if(!i)throw f;return i}function*st(t,n,r,o,s,c,l){const{channels:i,alpha:f,palette:a}=o,u=w(n,r),h=u.p*u.A,b=a?2:i,m=Array.from({length:b},()=>Z(G)),U=D(m),I={channels:i,alpha:f,palette:a&&a.colours,rct:c?.type,leaves:m,picture:a?[u.U?1:0]:m.map((t,n)=>n)},S=et(I,c),v=Array.from({length:i},()=>new Int16Array(1024)),x=t=>d(u,n,r,t);let q=null;if(a){const t=a.colours.length;q=new Int16Array(t*i),a.colours.forEach((n,e)=>{for(let r=0;r<i;r++)q[r*t+e]=a.byte(n,r)})}if(!a){const o=t=>{const n=C(t),r=new e(128);T(r,n);let o=r.u;for(let e=0;e<t.length;e++)t[e]&&(o+=t[e]*(n.v[e]+Math.max(0,e<224?e-1:e-236)));return o},s=Math.min(n,32),c=Math.min(r,32),l=[G,z].map(t=>({k:t,rt:m.map(()=>new Uint32Array(R))}));for(const e of[0,.5,1]){S(v,t,n,Math.floor((n-s)*e),Math.floor((r-c)*e),s,c);for(const t of l)for(let n=0;n<i;n++)V(null,t.rt[n],v[n],s,c,Z(t.k))}m.forEach((t,n)=>{const e=l.map(t=>o(t.rt[n])),r=e[1]<e[0]?1:0;t.k=l[r].k})}const E=a?[{type:"palette",D:0,H:i,K:a.colours.length}]:i>=3&&0!==c?.type?[{type:"rct",D:0,F:c?.type??6}]:[];let L=m.map(()=>new Uint32Array(33+(a?4096:1024)));q&&V(null,L[0],q,a.colours.length,i,m[0],!0);const X=rt(I,c);yield*A({pooled:l,kind:"plan",setup:{...I,sizes:L.map(t=>t.length)},at:t=>(t+1)/(2*h),stop:()=>!1},h,e=>X(t,n,...x(e),L),(t,n)=>M(L,n));const J=L.map($),N=J.reduce((t,n)=>t+n[0].ot-n[1].ot,0)>27*(u.U?1:h+1)+8?1:0;m.forEach((t,n)=>{t.config=J[n][N].config}),L=J.map(t=>t[N].rt);const _=new e(256);y(_,n,r,o.colour,o.alpha,{colorSpace:s}),p(_,{alpha:o.alpha});const P=new e(4096);P.write(1,1),P.write(1,1);const O=K(P,F(P,U),L,t=>m.indexOf(t),t=>m[t].config),k={...I,C:m.map((t,n)=>O[n+1]?.code)},j=rt(k,c);H(P,{nt:!0,et:E}),q&&V(P,k.C[0],q,a.colours.length,i,m[0]);const B=[];if(u.U)j(t,n,0,0,n,r,P),yield 1,B.push(P.finish());else{B.push(P.finish());for(let t=0;t<u.M*u.m+1;t++)B.push(new Uint8Array(0));const e=B.length,r=(t,n)=>{B[e+t]=n};yield*A({pooled:l,kind:"plan",setup:k,at:t=>(h+t+1)/(2*h),stop:()=>!1},h,e=>r(e,j(t,n,...x(e))),r)}return g(_,B)}const ct=(t,n)=>t+n+(t>n?1:0)>>1;function lt(t,n,e){const r=t>=n&&n>=e?1:t<=n&&n<=e?-1:0;if(!r)return 0;let o=(4*(t*=r)-3*(e*=r)-(n*=r)+6)/12|0;return o-(1&o)>2*(t-n)&&(o=2*(t-n)+1),o+(1&o)>2*(n-e)&&(o=2*(n-e)),r*o}function it(t,n){const{w:e,h:r,data:o}=t,s=n?e:r,c=s+1>>1,l=s-c,i=n?c:e,f=n?r:c,a=n?l:e,u=n?r:l,h=new Int16Array(i*f),y=new Int16Array(a*u),p=n?1:e;for(let t=0;t<f;t++)for(let r=0;r<i;r++){const c=n?r:t,f=n?t*e+2*r:2*t*e+r;if(c===l){h[t*i+r]=o[f];continue}const u=o[f],w=o[f+p],d=ct(u,w),A=c+1<l?ct(o[f+2*p],o[f+3*p]):1&s?o[f+2*p]:d;h[t*i+r]=d,y[t*a+r]=u-w-lt(c?o[f-p]:d,d,A)}const w=(e,r,o)=>({w:r,h:o,st:t.st+ +n,ct:t.ct+!n,data:e});return[w(h,i,f),w(y,a,u)]}function ft(t){const n=[],e=t.length;let r=t[0].w,o=t[0].h;for(e>2&&t[1].w===r&&t[1].h===o&&(n.push({$:!0,tt:!1,D:1,H:2}),n.push({$:!1,tt:!1,D:1,H:2})),!(r>o)&&o>8&&(n.push({$:!1,tt:!0,D:0,H:e}),o=o+1>>1);r>8||o>8;)r>8&&(n.push({$:!0,tt:!0,D:0,H:e}),r=r+1>>1),o>8&&(n.push({$:!1,tt:!0,D:0,H:e}),o=o+1>>1);return n}const at=1024,ut=8192;function*ht(t,n,r,{quality:o=90,shape:s=nt(t,n,r,{palette:!1}),colorSpace:c}={}){const l=function(t){return t>=100?0:t>=30?.1+.09*(100-t):53/3e3*t*t-1.15*t+25}(o);if(!(l>0))throw new Error("lossy encoding needs a quality below 100");const{colour:i,alpha:f,channels:a}=s,u=n*r,h=Array.from({length:a},()=>new Int16Array(u));for(let n=0,e=0;e<u;e++,n+=4){if(3===i){const r=t[n],o=t[n+1],s=t[n+2],c=r-s,l=s+(c>>1),i=o-l;h[0][e]=l+(i>>1),h[1][e]=c,h[2][e]=i}else h[0][e]=t[n];f&&(h[a-1][e]=t[n+3])}const d=h.map((t,e)=>({w:n,h:r,st:0,ct:0,data:t,component:3===i?e:0===e?0:3})),A=1===n&&r>at&&3===i,M=A?ft(d).filter(t=>!t.$):1!==n&&1!==r||3!==i?[]:ft(d),b=function(t,n=ft(t)){const e=t.map(t=>({...t,level:0,lt:!1}));for(const t of n){const n=t.D+t.H-1,r=t.tt?n+1:e.length;for(let o=t.D;o<=n;o++){const[n,s]=it(e[o],t.$),c=e[o].level+1;e[o]={...n,level:c,lt:e[o].lt,component:e[o].component},e.splice(r+(o-t.D),0,{...s,level:c,lt:!0,component:e[o].component})}}return e}(d,M.length?M:void 0),m=t=>t.lt?0:G,U=t=>function(t,n,e,r){let o=Math.min(16,n+e);if(o>0&&o--,3===t)return 1;const s=.25*function(t){let n=1;for(let e=0;e<24;e++)n=(4*n+t/(n*n*n*n))/5;return t*n}(r),c=1<<o,l=1===t||2===t?.35*s*Math.max(.5,1024/c):.35*s*1.1*(163.84/c);return Math.max(1,Math.floor(l))}(t.component,t.st+(A&&t.component>0&&t.component<3?1:0),t.ct,l),I=[...3===i?[{type:"rct",D:0,F:6}]:[],{type:"squeeze",W:M}],S=w(n,r,at),v=S.M*S.m,x=S.p*S.A;let C=0;for(;C<b.length&&b[C].w<=at&&b[C].h<=at;)C++;const T=(t,n,e,r)=>{const o=n>>t.st,s=e>>t.ct;return{x:o,y:s,w:Math.min(t.w,o+(r>>t.st))-o,h:Math.min(t.h,s+(r>>t.ct))-s}},q=[{index:0,Y:0,it:[]}];for(let t=0;t<C;t++)q[0].it.push({c:t,rect:{x:0,y:0,w:b[t].w,h:b[t].h}});if(!S.U){for(let t=0;t<v;t++){const n=t%S.M,e=t/S.M|0,r={index:1+t,Y:1+v+t,it:[]};for(let t=C;t<b.length;t++){const o=b[t];if(Math.min(o.st,o.ct)<3)continue;const s=T(o,n*ut,e*ut,ut);s.w>0&&s.h>0&&r.it.push({c:t,rect:s})}q.push(r)}for(let t=0;t<x;t++){const n=t%S.p,e=t/S.p|0,r={index:2+v+t,Y:1+3*v+17+t,it:[]};for(let t=C;t<b.length;t++){const o=b[t];if(Math.min(o.st,o.ct)>2)continue;const s=T(o,n*at,e*at,at);s.w>0&&s.h>0&&r.it.push({c:t,rect:s})}q.push(r)}}for(const t of q)for(const n of t.it){const t=b[n.c];n.ft=Z(m(t),0,U(t)),n.ft.channel=n.c}const E=q.filter(t=>t.it.length),L=1===E.length?D(E[0].it.map(t=>t.ft)):function(t){return Y(t,1)}(E.map(t=>({Y:t.Y,B:D(t.it.map(t=>t.ft))}))),X=new Int16Array(1048576),J=t=>{const n=b[t.c],e=t.rect;if(e.w===n.w&&e.h===n.h)return n.data;for(let t=0;t<e.h;t++)X.set(n.data.subarray((e.y+t)*n.w+e.x,(e.y+t)*n.w+e.x+e.w),t*e.w);return X},N=[...new Set(q.flatMap(t=>t.it.map(t=>t.c)))].sort((t,n)=>t-n),_=new Map(N.map((t,n)=>[t,n])),P=N.map(()=>new Uint32Array(R)),z=1+2*q.length;let O=1;yield O/z;for(const t of q){for(const n of t.it)V(null,P[_.get(n.c)],J(n),n.rect.w,n.rect.h,n.ft);yield++O/z}const k=new e(256);y(k,n,r,i,f,{colorSpace:c}),p(k,{alpha:f,shift:n>256||r>256?3:1});const j=Array.from({length:S.U?1:2+v+x},()=>null),B=j[0]=new e(65536);B.write(1,1),B.write(1,1);const Q=K(B,F(B,L),P,t=>_.get(t.channel));H(B,{nt:!0,et:I});for(const t of q){let n=j[t.index];n||(n=j[t.index]=new e(4096),t.it.length&&H(n,{nt:!0,et:[]}));for(const e of t.it)V(n,Q[_.get(e.c)+1].code,J(e),e.rect.w,e.rect.h,e.ft);yield++O/z}return g(k,function(t){return t.map(t=>t?t.finish():new Uint8Array(0))}(j))}const yt=[-500,-392,-255,-191,-127,-95,-63,-47,-31,-23,-15,-11,-7,-4,-3,-1,0,1,3,5,7,11,15,23,31,47,63,95,127,191,255,392,500],pt=Int8Array.from({length:1003},(t,n)=>yt.filter(t=>t<n-501).length),wt=Int32Array.from({length:64},(t,n)=>Math.floor(16777216/(n+1))),dt=(t,n)=>{const e=Math.max(0,26-Math.clz32(t+1));return 4+(n*wt[t>>e]>>e)},At=[12,13].map(t=>Int32Array.from({length:2048},(n,e)=>dt(e,t))),Mt=(t,n)=>t<2048?At[n-12][t]:dt(t,n),gt=new Int32Array(yt.length+1);let bt;function mt(t,n,e,r,o,c=0,l=gt,i,f){if(P.weighted?.(t,n,e,r,o,c,l,i,f))return;const a=r+2,u=bt&&bt[0].length>=2*a?bt:Array.from({length:6},(t,n)=>new Int32Array(n<5?2*a:8));bt=null;for(const t of u)t.fill(0);const[h,y,p,w,d,A]=u;let M=0;const g=(e,r,o=j,s=0)=>{t?N(t,n[e],o,r,s):X(o,r,n[e],s)},b=()=>{if(M){if(M>k.minLength)g(A[0],0),g(A[1],M-k.minLength-1,k.O,k.G);else for(let t=0;t<M;t++)g(A[t],0);M=0}};for(let t=0,n=0;t<o;t++){const o=1&t?0:a,u=1&t?a:0;for(let a=0;a<r;a++,n++){const m=a?e[n-1]:t?e[n-r]:0,U=t?e[n-r]:m,I=a+1<r&&t?e[n-r+1]:U,S=u+a,v=a<r-1?S+1:S,x=a?S-1:S;let C=Mt(h[S]+h[v]+h[x],13),T=Mt(y[S]+y[v]+y[x],12),q=Mt(p[S]+p[v]+p[x],12),E=Mt(w[S]+w[v]+w[x],12);const L=8*U,X=8*m,J=8*I,N=a?d[o+a-1]:0,_=d[S],P=d[x],z=d[v],G=_+N;let O=N;Math.abs(_)>Math.abs(O)&&(O=_),Math.abs(P)>Math.abs(O)&&(O=P),Math.abs(z)>Math.abs(O)&&(O=z);const k=l[pt[O<-501?0:O>501?1002:O+501]];f&&(f[n]=O);const j=X+J-L,R=L-(16*(G+z)>>5),Z=X-(10*(G+P)>>5),B=L-(7*(P+_+z)>>5),Y=27-Math.clz32(C+T+q+E);C>>=Y,T>>=Y,q>>=Y,E>>=Y;const D=C+T+q+E;let F=Math.floor(((D>>1)-1+j*C+R*T+Z*q+B*E)*wt[D-1]/16777216);if((_^N|_^P)<=0){const t=Math.min(X,J,L),n=Math.max(X,J,L);F=F<t?t:F>n?n:F}const Q=e[n],H=Q-(F+3>>3)-c;i?i[n]=s(H):0===H?(M<8&&(A[M]=k),M++):(b(),g(k,s(H)));const K=8*Q,V=Math.abs(j-K)+3>>3,W=Math.abs(R-K)+3>>3,$=Math.abs(Z-K)+3>>3,tt=Math.abs(B-K)+3>>3;d[o+a]=F-K,h[o+a]=V,y[o+a]=W,p[o+a]=$,w[o+a]=tt,h[u+a+1]+=V,y[u+a+1]+=W,p[u+a+1]+=$,w[u+a+1]+=tt}}b(),bt=u}const Ut=t=>Math.max(0,t<224?t-1:t-236),It=t=>{const n=C(t),r=new e(128);T(r,n);let o=r.u;for(let e=0;e<t.length;e++)t[e]&&(o+=t[e]*(n.v[e]+Ut(e)));return o};function St(t,n,e,r,o){const c=new Uint32Array(n*e);if(6===r)return mt(null,null,t,n,e,0,void 0,c,o),c;for(let o=0,l=0;o<e;o++)for(let e=0;e<n;e++,l++){const i=e?t[l-1]:o?t[l-n]:0,f=o?t[l-n]:i,a=e&&o?t[l-n-1]:i,u=3===r?(i+f)/2|0:Math.max(Math.min(i,f),Math.min(Math.max(i,f),i+f-a));c[l]=s(t[l]-u)}return c}function vt(t,n,e=1){const r=Array.from({length:e},()=>new Uint32Array(R)),o=[],s=(t,e,s=0)=>{const c=n?n[t]:0;o.push(e,s,c),X(s?k.O:j,e,r[c],s)};for(let n=0;n<t.length;){let e=1;if(!t[n])for(;n+e<t.length&&!t[n+e];)e++;if(s(n,t[n]),e>k.minLength)s(n+1,e-1-k.minLength,k.G);else for(let t=1;t<e;t++)s(n+t,0);n+=e}return{rt:r,it:o}}function xt(t,n,e,r,o){const s=vt(t,Uint8Array.from(n,t=>{let n=0,e=o.length;for(;n<e;){const r=n+e>>1;t>o[r]?n=r+1:e=r}return n}),o.length+1),c=s.rt.length,l=s.rt.map(t=>t.some(t=>t)),i=new Float64Array(c+1).fill(1/0),f=new Uint8Array(c+1);i[0]=0;for(let t=1;t<=c;t++){const n=new Uint32Array(R);let e=0;for(let r=t-1;r>=0;r--){if(l[r]){for(let t=0;t<R;t++)n[t]+=s.rt[r][t];e=It(n)}const o=i[r]+e+24;o<i[t]&&(i[t]=o,f[t]=r)}}const a=[];for(let t=c;t;t=f[t])a.unshift([f[t],t]);const u=a.map(()=>Z(e)),h=new Uint8Array(c),y=a.map(([t,n],e)=>{const r=new Uint32Array(R);for(let o=t;o<n;o++){h[o]=e;for(let t=0;t<R;t++)r[t]+=s.rt[o][t]}return r});for(let t=2;t<s.it.length;t+=3)s.it[t]=h[s.it[t]];const p=(t,n)=>{if(t===n)return u[t];const e=t+n>>1;return B(r,o[a[e][1]-1],p(e+1,n),p(t,e))};return{leaves:u,B:p(0,a.length-1),rt:y,it:s.it}}const Ct=[-127,-31,-7,-1,0,1,7,31,127],Tt=[-191,-127,-63,-31,-7,-1,0,7,31,63,127,191,255];function qt(t,n,e,r){const o=new Int32Array(n*e);for(let s=0,c=0;s<e;s++)for(let e=0;e<n;e++,c++){const l=e?t[c-1]:s?t[c-n]:0,i=s?t[c-n]:l,f=e&&s?t[c-n-1]:l;o[c]=9===r?l+i-f:l-f}return o}function Et(t){const n=new e(512),r=K(n,F(n,t.B),t.rt,n=>t.leaves.indexOf(n));let o=n.u;return t.rt.forEach((t,n)=>{const{v:e}=r[n+1].code;for(let n=0;n<t.length;n++)o+=t[n]*((e[n]||0)+Ut(n))}),o}function Lt(t,n,e,r){return t.map(t=>{let o;for(const s of[5,3,6]){const c=6===s?new Int32Array(n*e):null,l=St(t,n,e,s,c),i=Z(s),f=[{...vt(l),leaves:[i],B:i}];if(c&&f.push(xt(l,c,s,15,Ct)),r>=6&&5===s)for(const r of[9,10]){const o=qt(t,n,e,r),c=9===r?Tt:Ct;f.push(xt(l,o,s,r,c))}for(const t of f){const n=Et(t);(!o||n<o.ot)&&(o={...t,ot:n})}}return o})}function Xt(t,n){const e=n.flatMap(t=>t.leaves),r=K(t,F(t,D(n.map(t=>t.B))),n.flatMap(t=>t.rt),t=>e.indexOf(t));return()=>{for(let o=0;o<n.length;o++){const{it:s}=n[o],c=n[o].leaves.map(t=>r[e.indexOf(t)+1].code);for(let n=0;n<s.length;n+=3){const e=s[n+1];N(t,c[s[n+2]],e?k.O:j,s[n],e)}}}}function*Jt(t,n,r,o,s,c=4,l=!1,i){const{channels:a}=o,u=w(n,r),h=u.p*u.A,M=l?o.palette:null;if(l&&!M)return null;const b={channels:a,alpha:o.alpha,palette:M&&M.colours,rung:c},m=new e(256);y(m,n,r,o.colour,o.alpha,{colorSpace:s}),p(m,{alpha:o.alpha});const U=M?[{type:"palette",D:0,H:a,K:M.colours.length}]:a>=3?[{type:"rct",D:0,F:6}]:[];let I;if(M){const t=M.colours.length,n=new Int16Array(t*a);M.colours.forEach((e,r)=>{for(let o=0;o<a;o++)n[o*t+r]=M.byte(e,o)}),I=Lt([n],t,a,c)}const S=new e(4096),v=[];let x=0;const C=t=>{x+=t.length,f(x),v.push(t)};if(S.write(1,1),S.write(1,1),u.U){const e=Array.from({length:M?1:a},()=>new Int16Array(65536));et(b)(e,t,n,0,0,n,r);const o=Xt(S,[...I||[],...Lt(e,n,r,c)]);return H(S,{et:U}),o(),C(S.finish()),(yield 1)?null:g(m,v)}let T;if(I)T=Xt(S,I);else{const t=Array.from({length:a},()=>Z(5));K(S,F(S,D(t)),t.map(()=>new Uint32Array(R)),n=>t.indexOf(n))}H(S,{et:U}),T&&T(),C(S.finish());for(let t=0;t<u.M*u.m+1;t++)v.push(new Uint8Array(0));const q=function(t){const n=et(t),r=Array.from({length:t.palette?1:t.channels},()=>new Int16Array(65536));return(o,s,c,l,i,f)=>{n(r,o,s,c,l,i,f);const a=new e(i*f*r.length+256);return H(a,{nt:!1}),Xt(a,Lt(r,i,f,t.rung))(),a.finish()}}(b);return null===(yield*A({pooled:i,kind:"local",setup:b,at:t=>(t+1)/h,stop:()=>!0},h,e=>C(q(t,n,...d(u,n,r,e))),(t,n)=>C(n)))?null:g(m,v)}function Nt(t,n){const e=Math.floor(t/7),r=t%7,o=e%3,s=(e+1+Math.floor(e/3))%3,c=(e+2-Math.floor(e/3))%3;return{type:t,fill(t,e,l,i,f,a,u){for(let h=0;h<u;h++)for(let u=0,y=h*a,p=4*((f+h)*l+i);u<a;u++,y++,p+=4){const l=e[p+o],i=e[p+s],f=e[p+c];if(6===r){const n=l-f,e=f+(n>>1),r=i-e;t[0][y]=e+(r>>1),t[1][y]=n,t[2][y]=r}else t[0][y]=l,t[1][y]=i-(r>>1==1?l:r>>1==2?l+f>>1:0),t[2][y]=f-(1&r?l:0);4===n&&(t[3][y]=e[p+3])}}}}const _t=t=>null===t||("object"==typeof t?t.hurried:t);function Pt(n,s,c,l,f){const{quality:a,colorSpace:u}=function(t={},n=100){if(null===t||"object"!=typeof t)throw i("JXL_INPUT","Options are an object: {quality, colorSpace}.");if(void 0!==t.quality&&(n=t.quality),"number"!=typeof n||!Number.isFinite(n)||n<1||n>100)throw i("JXL_INPUT","Quality is a number from 1 to 100.");const e=void 0===t.colorSpace?"srgb":t.colorSpace;if("srgb"!==e&&"display-p3"!==e)throw i("JXL_INPUT","The colour space is srgb or display-p3.");return{quality:n,colorSpace:e}}(l),h=void 0===l?.effort?1:l.effort;if(!Number.isInteger(h)||h<1||h>9)throw i("JXL_INPUT","Effort is a whole number from 1 to 9.");return function(n,e,r,o){if(function(n,e,r=t){if(!Number.isInteger(n)||!Number.isInteger(e)||n<1||e<1)throw i("JXL_INPUT","A picture is at least one pixel wide and high, in whole pixels.");if(n>r.edge||e>r.edge)throw i("JXL_DIMENSIONS","A picture is at most "+r.edge+" pixels on a side.");if(n*e>r.pixels)throw i("JXL_DIMENSIONS","A picture is at most "+r.pixels.toLocaleString("en-US")+" pixels.")}(e,r,o),!(n instanceof Uint8Array||n instanceof Uint8ClampedArray))throw i("JXL_INPUT","Pixels are a Uint8Array or Uint8ClampedArray of RGBA bytes.");if(n.length!==e*r*4)throw i("JXL_INPUT","Pixels are width * height * 4 bytes: straight (not premultiplied) RGBA, row by row.")}(n,s,c),function*(t,n,s,c,l,i,f){const a=nt(t,n,s);if(c<100){if(!a.palette)return yield*ht(t,n,s,{quality:c,shape:a,colorSpace:l});const e=yield*r(ot(t,n,s,{shape:a,colorSpace:l}),0,2);let o;try{o=yield*r(ht(t,n,s,{quality:c,shape:a,colorSpace:l}),1,2)}catch(t){if("JXL_SIZE"!==t.code&&(!(t instanceof RangeError)||t.code))throw t;o=e}return e.length<=o.length?e:o}if(i<2)return yield*ot(t,n,s,{shape:a,colorSpace:l,pooled:f});let u=yield*r(ot(t,n,s,{shape:a,colorSpace:l,pooled:f}),0,2);const h=a.palette?{...a,palette:null}:a;if(i>=4){const r=[Zt(t,n,s,h,l,3,f)];a.palette&&r.push(Jt(t,n,s,a,l,4,!0,f)),i>=6&&(r.push(Jt(t,n,s,a,l,6,!1,f)),a.palette&&r.push(Jt(t,n,s,a,l,6,!0,f))),r.push(function*(t,n,r,o,s,c){if(3!==o.colour||n*r<4096)return null;const l=Math.min(n,32),i=Math.min(r,32),f=Array.from({length:o.channels},()=>new Int16Array(l*i)),a=[6,...Array.from({length:42},(t,n)=>n).filter(t=>6!==t)];let u;for(let s=0;s<a.length;s++){const c=Nt(a[s],o.channels),h=[5,3].map(t=>({k:t,rt:Array.from({length:3},()=>new Uint32Array(1057))}));for(let e=0;e<3;e++){c.fill(f,t,n,Math.floor((n-l)*e/2),Math.floor((r-i)*e/2),l,i);for(const t of h)for(let n=0;n<3;n++)V(null,t.rt[n],f[n],l,i,Z(t.k),!0)}const y=new e(32);c.type&&Q(y,{type:"rct",D:0,F:c.type});let p=y.u;for(let t=0;t<3;t++)p+=Math.min(...h.map(n=>$(n.rt[t])[1].ot));if((!u||p<u.ot)&&(u={rct:c,ot:p}),yield(s+1)/(4*a.length))return null}if(6===u.rct.type)return null;const h=ot(t,n,r,{shape:{...o,palette:null},colorSpace:s,rct:u.rct,pooled:c});let y,p;for(;!(y=h.next(p)).done;){const t=y.value,n="number"==typeof t;if(p=yield n?.25+.75*t:{...t,at:n=>.25+.75*t.at(n),stop:n=>t.at(n)<1},n?p&&t<1:!p)return null}return y.value}(t,n,s,a,l,f));for(let t=0;t<r.length;t++)try{let n,e,s=!1;for(;!(n=r[t].next(e)).done;)s=_t(e=yield o(n.value,n=>.5+(t+n)/(2*r.length)));if(n.value&&n.value.length<u.length&&(u=n.value),s)return u}catch(t){if("JXL_SIZE"!==t.code&&(!(t instanceof RangeError)||t.code))throw t}return u}try{const e=yield*r(Zt(t,n,s,h,l,i,f),1,2);e&&e.length<u.length&&(u=e)}catch(t){if("JXL_SIZE"!==t.code&&(!(t instanceof RangeError)||t.code))throw t}return u}(n,s,c,a,u,h,f&&a>=100&&!w(s,c).U)}const zt=t=>Math.max(0,t<224?t-1:t-236);function Gt(t){const n=C(t),r=new e(128);T(r,n);let o=r.u;for(let e=0;e<t.length;e++)t[e]&&(o+=t[e]*(n.v[e]+zt(e)));return o}const Ot=t=>{const n=new Uint32Array(R);for(const e of t)for(let t=0;t<R;t++)n[t]+=e[t];return n},kt=yt.length+1,jt=Int32Array.from({length:kt},(t,n)=>n);function Rt(t){const{channels:n,first:r,plans:o}=t,s=et(t),c=r?.map(t=>Z(t)),l=Array.from({length:n},()=>new Int16Array(65536));return(t,r,i,f,a,u,h)=>{if(s(l,t,r,i,f,a,u),!o){for(let t=0;t<n;t++)V(null,h[t],l[t],a,u,c[t]),mt(null,h.slice(n+t*kt,n+(t+1)*kt),l[t],a,u,0,jt);return}const y=h||new e(a*u*n+64);return h||H(y,{nt:!0,et:[]}),o.forEach(({ft:t,C:n,ut:e},r)=>{6===t.k?mt(y,n,l[r],a,u,0,e):V(y,n[0],l[r],a,u,t)}),h?void 0:y.finish()}}function*Zt(t,n,r,o,s,c,l){const{channels:i}=o,f=w(n,r),a=f.p*f.A,u={channels:i,alpha:o.alpha,palette:null},h=et(u),b=Array.from({length:i},()=>new Int16Array(1024)),m=t=>d(f,n,r,t),U=Math.min(n,32),I=Math.min(r,32),S=[G,z].map(t=>({k:t,rt:b.map(()=>new Uint32Array(R))}));for(const e of[0,.5,1]){h(b,t,n,Math.floor((n-U)*e),Math.floor((r-I)*e),U,I);for(const t of S)for(let n=0;n<i;n++)V(null,t.rt[n],b[n],U,I,Z(t.k))}const v=b.map((t,n)=>Z(S[Gt(S[1].rt[n])<Gt(S[0].rt[n])?1:0].k)),x=kt,C=b.map(()=>new Uint32Array(R)),T=b.map(()=>Array.from({length:x},()=>new Uint32Array(R))),q=[...C,...T.flat()],E={...u,first:v.map(t=>t.k)},L=Rt(E);if(null===(yield*A({pooled:l,kind:"search",setup:{...E,sizes:q.map(t=>t.length)},at:t=>(t+1)/(2*a),stop:()=>!0},a,e=>L(t,n,...m(e),q),(t,n)=>M(q,n))))return null;const X=b.map((t,n)=>{const e={ht:Gt(C[n]),leaves:[v[n]],rt:[C[n]],cuts:[]},r=Ot(T[n]),o=Gt(r);return o<e.ht?{ht:o,leaves:[Z(6)],rt:[r],cuts:[]}:e}),J=c<3?X:X.map((t,n)=>{const e=T[n].map(t=>t.some(t=>t)),r=new Float64Array(x+1).fill(1/0),o=new Int32Array(x+1);r[0]=0;for(let t=1;t<=x;t++){const s=new Uint32Array(R);let c=0;for(let l=t-1;l>=0;l--){if(e[l]){const t=T[n][l];for(let n=0;n<R;n++)s[n]+=t[n];c=Gt(s)}const i=r[l]+c+24;i<r[t]&&(r[t]=i,o[t]=l)}}if(!(r[x]<t.ht))return t;const s=[];for(let t=x;t>0;t=o[t])s.unshift([o[t],t]);return{ht:r[x],leaves:s.map(()=>Z(6)),rt:s.map(([t,e])=>Ot(T[n].slice(t,e))),cuts:s.slice(0,-1).map(([,t])=>yt[t-1]),ut:Int32Array.from({length:x},(t,n)=>s.findIndex(([t,e])=>n>=t&&n<e))}}),N=[X,J].filter((t,n)=>n?t.some((t,n)=>t!==X[n]):t.some((t,n)=>t.leaves[0]!==v[n]));if(!N.length)return null;const _=t=>({B:D(t.map(t=>((t,n)=>{const e=(r,o)=>{if(r===o)return t[r];const s=r+o>>1;return B(15,n[s],e(s+1,o),e(r,s))};return e(0,t.length-1)})(t.leaves,t.cuts))),leaves:t.flatMap(t=>t.leaves),rt:t.flatMap(t=>t.rt)}),P=t=>{const{B:n,leaves:r,rt:o}=_(t),s=new e(4096),c=K(s,F(s,n),o,t=>r.indexOf(t));let l=s.u;return o.forEach((t,n)=>{const{v:e}=c[n+1].code;for(let n=0;n<t.length;n++)t[n]&&(l+=t[n]*(e[n]+zt(n)))}),l};let O=N;if(N.length>1){const[t,n]=N.map(P),e=27*(f.U?1:a+1)+8;n+e<=t?O=[N[1]]:t+e<=n?O=[N[0]]:n<t&&(O=[N[1],N[0]])}let k=0,j=!1,Y=null;for(const t of O){const n=yield*Q(t);if(!n)return Y;if((!Y||n.length<Y.length||n.length===Y.length&&t===N[0])&&(Y=n),j)return Y}return Y;function*Q(c){const{B:h,leaves:w,rt:d}=_(c),M=i>=3?[{type:"rct",D:0,F:6}]:[],b=new e(256);y(b,n,r,o.colour,o.alpha,{colorSpace:s}),p(b,{alpha:o.alpha});const U=new e(4096);U.write(1,1),U.write(1,1);const I=K(U,F(U,h),d,t=>w.indexOf(t));H(U,{nt:!0,et:M});const S={...u,plans:c.map(t=>({ft:t.leaves[0],C:t.leaves.map(t=>I[w.indexOf(t)+1].code),ut:t.ut}))},v=Rt(S),x=[];if(f.U)v(t,n,0,0,n,r,U),j=yield.5+ ++k/(2*O.length*a),x.push(U.finish());else{x.push(U.finish());for(let t=0;t<f.M*f.m+1;t++)x.push(new Uint8Array(0));const e=k,r=x.length,o=(t,n)=>{x[r+t]=n};if(j=yield*A({pooled:l,kind:"search",setup:S,at:t=>.5+(e+t+1)/(2*O.length*a),stop:t=>t+1<a},a,e=>o(e,v(t,n,...m(e))),o),null===j)return null;k+=a}return g(b,x)}}function Bt(t,n,e,r){return function(t){let n;for(;!(n=t.next()).done;);return n.value}(Yt(t,n,e,r))}function Yt(t,n,e,r){return function(t){const n=function*(){let e,r;for(;!(e=a(()=>t.next(n.hurry))).done;)yield r=e.value;return r<1&&(yield 1),n.bytes=function(t){return f(t.length),t}(e.value)}();return n.bytes=null,n.hurry=!1,n}(Pt(t,n,e,r))}export{t as LIMITS,Bt as encode,Yt as encodeSteps};
package/effort.mjs CHANGED
@@ -6,20 +6,15 @@
6
6
  // 3. the weighted predictor with its channel's tokens split by the predictor's own error, neighbouring intervals of
7
7
  // libjxl's cut points merged wherever a shared prefix code costs less.
8
8
  // 4. palette indices with local trees and histograms, the predictor and error intervals learned per group;
9
+ // also a sampled search over all 42 reversible colour transforms, accepted only after a smaller complete stream;
9
10
  // 6. direct and palette planes with splits on the unclamped gradient and west-minus-northwest difference.
10
11
  // A lossy request is effort 1's. Above effort 1 an exact picture's job spends its first half (fractions up to 0.5)
11
12
  // writing effort 1's stream and its second searching, so a caller's clock can tell the search's own pace; `hurry`
12
13
  // ends the search at its next step with the smallest stream written so far. Every choice is integer arithmetic: the
13
14
  // same input and options write the same bytes everywhere.
14
- import {LIMITS, BitWriter, complete, part} from './bits.mjs';
15
- import {writeImageHeader, writeModularFrameHeader, groupLayout, assembleCodestream, GROUP_DIM} from './frame.mjs';
16
- import {buildCode, writePrefixCode} from './prefix.mjs';
17
- import {AVERAGE_PREDICTOR, GRADIENT_PREDICTOR, ALPHABET, leaf, split, channelTree, writeTree, writeModularHeader, writeChannelHistograms, codeChannel} from './modular.mjs';
18
- import {inspectPixels, losslessSteps} from './lossless.mjs';
19
- import {lossySteps} from './lossy.mjs';
20
- import {WEIGHTED_PREDICTOR, WEIGHTED_PROPERTY, WEIGHTED_CUTS, codeWeighted} from './weighted.mjs';
21
- import {fault, admitOptions, admitPixels, job} from './admit.mjs';
22
- import {localSteps} from './local.mjs';
15
+ import {LIMITS, complete} from './bits.mjs';
16
+ import {job} from './admit.mjs';
17
+ import {effortJob} from './effort-job.mjs';
23
18
 
24
19
  export {LIMITS};
25
20
 
@@ -27,214 +22,4 @@ export function encode(data, width, height, options) { return complete(encodeSte
27
22
 
28
23
  // The options as every door reads them, and the effort, which this door alone reads (the core, one rung, does not): a
29
24
  // whole number from 1 to 9, 1 when absent.
30
- export function encodeSteps(data, width, height, options) {
31
- const {quality, colorSpace} = admitOptions(options), effort = options?.effort === undefined ? 1 : options.effort;
32
- if (!Number.isInteger(effort) || effort < 1 || effort > 9) throw fault('JXL_INPUT', 'Effort is a whole number from 1 to 9.');
33
- admitPixels(data, width, height);
34
- return job(effortSteps(data, width, height, quality, colorSpace, effort));
35
- }
36
-
37
- // Effort 1 is the core's work (index.mjs), step for step; the rungs run after it and keep a smaller stream if they
38
- // find one. A search that runs out of memory leaves the smallest completed stream standing.
39
- function* effortSteps(data, width, height, quality, colorSpace, effort) {
40
- const shape = inspectPixels(data, width, height);
41
- if (quality < 100) {
42
- if (!shape.palette) return yield* lossySteps(data, width, height, {quality, shape, colorSpace});
43
- const exact = yield* part(losslessSteps(data, width, height, {shape, colorSpace}), 0, 2);
44
- let bytes;
45
- try { bytes = yield* part(lossySteps(data, width, height, {quality, shape, colorSpace}), 1, 2); }
46
- catch (error) { if (error.code !== 'JXL_SIZE' && (!(error instanceof RangeError) || error.code)) throw error; bytes = exact; }
47
- return exact.length <= bytes.length ? exact : bytes;
48
- }
49
- if (effort < 2) return yield* losslessSteps(data, width, height, {shape, colorSpace});
50
- let best = yield* part(losslessSteps(data, width, height, {shape, colorSpace}), 0, 2);
51
- if (effort >= 4) {
52
- const searches = [searchSteps(data, width, height, shape.palette ? {...shape, palette: null} : shape, colorSpace, 3)];
53
- if (shape.palette) searches.push(localSteps(data, width, height, shape, colorSpace, 4, true));
54
- if (effort >= 6) {
55
- searches.push(localSteps(data, width, height, shape, colorSpace, 6));
56
- if (shape.palette) searches.push(localSteps(data, width, height, shape, colorSpace, 6, true));
57
- }
58
- for (let i = 0; i < searches.length; i++) {
59
- try {
60
- let step, hurried = false;
61
- while (!(step = searches[i].next(hurried)).done) hurried = yield 0.5 + (i + step.value) / (2 * searches.length);
62
- if (step.value && step.value.length < best.length) best = step.value;
63
- if (hurried) return best;
64
- } catch (error) { if (error.code !== 'JXL_SIZE' && (!(error instanceof RangeError) || error.code)) throw error; }
65
- }
66
- return best;
67
- }
68
- try {
69
- const bytes = yield* part(searchSteps(data, width, height, shape.palette ? {...shape, palette: null} : shape, colorSpace, effort), 1, 2);
70
- if (bytes && bytes.length < best.length) best = bytes;
71
- } catch (error) { if (error.code !== 'JXL_SIZE' && (!(error instanceof RangeError) || error.code)) throw error; }
72
- return best;
73
- }
74
-
75
- // A token's raw bits: a residual token s above zero carries s - 1 of them, an LZ77 length token above 235 s - 236.
76
- const raw = s => Math.max(0, s < 224 ? s - 1 : s - 236);
77
- // A histogram's complete cost in bits: its prefix code's header and every token's code and raw bits.
78
- function cost(freqs) {
79
- const code = buildCode(freqs), writer = new BitWriter(128);
80
- writePrefixCode(writer, code);
81
- let bits = writer.bitLength;
82
- for (let s = 0; s < freqs.length; s++) if (freqs[s]) bits += freqs[s] * (code.lengths[s] + raw(s));
83
- return bits;
84
- }
85
- const sum = histograms => { const out = new Uint32Array(ALPHABET); for (const h of histograms) for (let s = 0; s < ALPHABET; s++) out[s] += h[s]; return out; };
86
-
87
- // The direct plan of lossless.mjs (YCoCg, 256-pixel groups), every channel counted over the whole picture under effort
88
- // 1's predictor (chosen by samples, as the core chooses it) and under the weighted one; each rung's plan takes every
89
- // channel's cheapest, and the plans are written the cheapest way. Nothing written (null) when no plan leaves effort 1.
90
- function* searchSteps(rgba, width, height, shape, colorSpace, effort) {
91
- const {channels} = shape, layout = groupLayout(width, height), groups = layout.groupsX * layout.groupsY;
92
- const planes = Array.from({length: channels}, () => new Int16Array(GROUP_DIM * GROUP_DIM));
93
- const fill = (x0, y0, w, h) => {
94
- for (let y = 0; y < h; y++) {
95
- let i = ((y0 + y) * width + x0) * 4, at = y * w;
96
- for (let x = 0; x < w; x++, i += 4, at++) {
97
- if (channels >= 3) {
98
- const r = rgba[i], g = rgba[i + 1], b = rgba[i + 2];
99
- const co = r - b, tmp = b + (co >> 1), cg = g - tmp;
100
- planes[0][at] = tmp + (cg >> 1); planes[1][at] = co; planes[2][at] = cg;
101
- if (channels === 4) planes[3][at] = rgba[i + 3];
102
- } else { planes[0][at] = rgba[i]; if (channels === 2) planes[1][at] = rgba[i + 3]; }
103
- }
104
- }
105
- };
106
- const group = (g, visit) => {
107
- const x0 = g % layout.groupsX * GROUP_DIM, y0 = (g / layout.groupsX | 0) * GROUP_DIM, w = Math.min(GROUP_DIM, width - x0), h = Math.min(GROUP_DIM, height - y0);
108
- fill(x0, y0, w, h);
109
- visit(w, h);
110
- };
111
- // Effort 1's predictor per channel: three 32-pixel samples priced under gradient and average, as the core does.
112
- const sw = Math.min(width, 32), sh = Math.min(height, 32);
113
- const sampled = [GRADIENT_PREDICTOR, AVERAGE_PREDICTOR].map(predictor => ({predictor, freqs: planes.map(() => new Uint32Array(ALPHABET))}));
114
- for (const fraction of [0, 0.5, 1]) {
115
- fill(Math.floor((width - sw) * fraction), Math.floor((height - sh) * fraction), sw, sh);
116
- for (const candidate of sampled) for (let c = 0; c < channels; c++) codeChannel(null, candidate.freqs[c], planes[c], sw, sh, leaf(candidate.predictor));
117
- }
118
- const first = planes.map((_, c) => leaf(sampled[cost(sampled[1].freqs[c]) < cost(sampled[0].freqs[c]) ? 1 : 0].predictor));
119
- // Every channel counted under effort 1's predictor and under the weighted one, its tokens kept by error interval.
120
- // A step is a group of the counting pass (the first half of the search's fractions) or of a plan's writing (the
121
- // second half, shared by the plans written), so the pace of the steps holds whether one plan is written or two.
122
- const contexts = WEIGHTED_CUTS.length + 1, identity = Int32Array.from({length: contexts}, (_, k) => k);
123
- const firstCounts = planes.map(() => new Uint32Array(ALPHABET)), intervals = planes.map(() => Array.from({length: contexts}, () => new Uint32Array(ALPHABET)));
124
- let done = 0;
125
- for (let g = 0; g < groups; g++) {
126
- group(g, (w, h) => {
127
- for (let c = 0; c < channels; c++) {
128
- codeChannel(null, firstCounts[c], planes[c], w, h, first[c]);
129
- codeWeighted(null, intervals[c], planes[c], w, h, 0, identity);
130
- }
131
- });
132
- if (yield ++done / (2 * groups)) return null;
133
- }
134
- // Rung 2: each channel's cheaper of effort 1's predictor and the weighted one in one context.
135
- const rung2 = planes.map((_, c) => {
136
- const plan = {cost: cost(firstCounts[c]), leaves: [first[c]], freqs: [firstCounts[c]], cuts: []};
137
- const whole = sum(intervals[c]), single = cost(whole);
138
- return single < plan.cost ? {cost: single, leaves: [leaf(WEIGHTED_PREDICTOR)], freqs: [whole], cuts: []} : plan;
139
- });
140
- // Rung 3: or its intervals grouped into runs of neighbours, the grouping of least cost found exactly by dynamic
141
- // programming. best[j] is the least cost of intervals 0..j-1 grouped, `from[j]` where its last group begins; an
142
- // interval no token reaches leaves the merged cost as it was.
143
- const rung3 = effort < 3 ? rung2 : rung2.map((plan, c) => {
144
- const filled = intervals[c].map(h => h.some(v => v));
145
- const best = new Float64Array(contexts + 1).fill(Infinity), from = new Int32Array(contexts + 1);
146
- best[0] = 0;
147
- for (let j = 1; j <= contexts; j++) {
148
- const merged = new Uint32Array(ALPHABET);
149
- let bits = 0;
150
- for (let i = j - 1; i >= 0; i--) {
151
- if (filled[i]) { const h = intervals[c][i]; for (let s = 0; s < ALPHABET; s++) merged[s] += h[s]; bits = cost(merged); }
152
- // Every group is a leaf of the tree and an entry of the context map: 24 bits more, so an interval no token
153
- // reaches joins its neighbour.
154
- const price = best[i] + bits + 24;
155
- if (price < best[j]) { best[j] = price; from[j] = i; }
156
- }
157
- }
158
- if (!(best[contexts] < plan.cost)) return plan;
159
- const groupsOf = [];
160
- for (let j = contexts; j > 0; j = from[j]) groupsOf.unshift([from[j], j]);
161
- return {cost: best[contexts], leaves: groupsOf.map(() => leaf(WEIGHTED_PREDICTOR)), freqs: groupsOf.map(([i, j]) => sum(intervals[c].slice(i, j))), cuts: groupsOf.slice(0, -1).map(([, j]) => WEIGHTED_CUTS[j - 1]),
162
- contextOf: Int32Array.from({length: contexts}, (_, k) => groupsOf.findIndex(([i, j]) => k >= i && k < j))};
163
- });
164
- const candidates = [rung2, rung3].filter((plans, k) => k ? plans.some((plan, c) => plan !== rung2[c]) : plans.some((plan, c) => plan.leaves[0] !== first[c]));
165
- if (!candidates.length) return null;
166
- // The tree: one subtree per channel on the channel property, and inside a split channel a balanced tree on the
167
- // weighted predictor's property, values above a cut to the left.
168
- const byError = (leaves, cuts) => {
169
- const build = (lo, hi) => { if (lo === hi) return leaves[lo]; const mid = (lo + hi) >> 1; return split(WEIGHTED_PROPERTY, cuts[mid], build(mid + 1, hi), build(lo, mid)); };
170
- return build(0, leaves.length - 1);
171
- };
172
- const assemble = plans => ({tree: channelTree(plans.map(plan => byError(plan.leaves, plan.cuts))), leaves: plans.flatMap(plan => plan.leaves), freqs: plans.flatMap(plan => plan.freqs)});
173
- // A plan's price in bits: its tree and histograms as written, and every token's code and raw bits. Two plans' streams
174
- // differ by their prices and at most 27 bits a section more (a section's padding, and its size's field in the table
175
- // of contents, 12 to 32 bits) and a byte: a plan that far cheaper is written alone, closer plans both.
176
- const price = plans => {
177
- const {tree, leaves, freqs} = assemble(plans), w = new BitWriter(4096);
178
- const histograms = writeChannelHistograms(w, writeTree(w, tree), freqs, l => leaves.indexOf(l));
179
- let bits = w.bitLength;
180
- freqs.forEach((f, i) => { const {lengths} = histograms[i + 1].code; for (let s = 0; s < f.length; s++) if (f[s]) bits += f[s] * (lengths[s] + raw(s)); });
181
- return bits;
182
- };
183
- let chosen = candidates;
184
- if (candidates.length > 1) {
185
- const [a, b] = candidates.map(price), margin = 27 * (layout.single ? 1 : groups + 1) + 8;
186
- if (b + margin <= a) chosen = [candidates[1]];
187
- else if (a + margin <= b) chosen = [candidates[0]];
188
- else if (b < a) chosen = [candidates[1], candidates[0]];
189
- }
190
- let written = 0, hurried = false;
191
- const next = () => 0.5 + ++written / (2 * chosen.length * groups);
192
- let smallest = null;
193
- for (const plans of chosen) {
194
- const bytes = yield* write(plans);
195
- if (!bytes) return smallest;
196
- // Price the likely winner first, but retain rung 2's byte choice on an equal-length completed pair.
197
- if (!smallest || bytes.length < smallest.length || (bytes.length === smallest.length && plans === candidates[0])) smallest = bytes;
198
- if (hurried) return smallest;
199
- }
200
- return smallest;
201
-
202
- // A plan's stream, a group per step; a hurry at its final group keeps the already-completed candidate.
203
- function* write(plans) {
204
- const {tree, leaves, freqs} = assemble(plans);
205
- const transforms = channels >= 3 ? [{type: 'rct', beginC: 0, rctType: 6}] : [];
206
- const header = new BitWriter(256);
207
- writeImageHeader(header, width, height, shape.colour, shape.alpha, {colorSpace});
208
- writeModularFrameHeader(header, {alpha: shape.alpha});
209
- const global = new BitWriter(4096);
210
- global.write(1, 1); // default DC quantisation
211
- global.write(1, 1); // a global tree
212
- const histograms = writeChannelHistograms(global, writeTree(global, tree), freqs, l => leaves.indexOf(l));
213
- writeModularHeader(global, {useGlobalTree: true, transforms});
214
- const codes = plans.map(plan => plan.leaves.map(l => histograms[leaves.indexOf(l) + 1].code));
215
- const code = (w, c, width, height) => {
216
- const plan = plans[c];
217
- if (plan.leaves[0].predictor === WEIGHTED_PREDICTOR) codeWeighted(w, codes[c], planes[c], width, height, 0, plan.contextOf);
218
- else codeChannel(w, codes[c][0], planes[c], width, height, plan.leaves[0]);
219
- };
220
- const sections = [];
221
- if (layout.single) {
222
- group(0, (w, h) => { for (let c = 0; c < channels; c++) code(global, c, w, h); });
223
- hurried = yield next();
224
- sections.push(global.finish());
225
- } else {
226
- sections.push(global.finish());
227
- for (let i = 0; i < layout.dcGroupsX * layout.dcGroupsY + 1; i++) sections.push(new Uint8Array(0));
228
- for (let g = 0; g < groups; g++) {
229
- group(g, (w, h) => {
230
- const section = new BitWriter(w * h * channels + 64);
231
- writeModularHeader(section, {useGlobalTree: true, transforms: []});
232
- for (let c = 0; c < channels; c++) code(section, c, w, h);
233
- sections.push(section.finish());
234
- });
235
- if ((hurried = yield next()) && g + 1 < groups) return null;
236
- }
237
- }
238
- return assembleCodestream(header, sections);
239
- }
240
- }
25
+ export function encodeSteps(data, width, height, options) { return job(effortJob(data, width, height, options)); }
package/frame.mjs CHANGED
@@ -75,6 +75,31 @@ export function groupLayout(width, height, dim = GROUP_DIM) {
75
75
  return {groupsX, groupsY, dcGroupsX, dcGroupsY, single: groupsX === 1 && groupsY === 1};
76
76
  }
77
77
 
78
+ // Group g's rectangle in the picture: [x0, y0, width, height].
79
+ export function groupRect(layout, width, height, g) {
80
+ const x0 = g % layout.groupsX * GROUP_DIM, y0 = (g / layout.groupsX | 0) * GROUP_DIM;
81
+ return [x0, y0, Math.min(GROUP_DIM, width - x0), Math.min(GROUP_DIM, height - y0)];
82
+ }
83
+
84
+ // A pass over a frame's groups. A group's work is a function of the pass's setup and the group's own pixels alone, so
85
+ // it runs here, one group a step (`run(g)`), or, when the request is `pooled`, in a pool of workers (pool.mjs): the
86
+ // pass is then one step, answered with every group's result for `accept(g, result)` in group order, or with null when a
87
+ // hurry ended it. `at(g)` is the fraction done once group g is, and `stop(g)` whether a hurry there ends the pass.
88
+ // Returns null when a hurry ended it, else whether its last step was hurried.
89
+ export function* groupPass(request, groups, run, accept) {
90
+ if (request.pooled) {
91
+ const reply = yield request;
92
+ if (!reply) return null;
93
+ reply.results.forEach((result, g) => accept(g, result));
94
+ return reply.hurried;
95
+ }
96
+ let hurried = false;
97
+ for (let g = 0; g < groups; g++) { run(g); hurried = yield request.at(g); if (hurried && request.stop(g)) return null; }
98
+ return hurried;
99
+ }
100
+ // A group's counts added into the picture's, histogram by histogram.
101
+ export function addCounts(into, counts) { into.forEach((h, i) => { const c = counts[i]; for (let s = 0; s < h.length; s++) h[s] += c[s]; }); }
102
+
78
103
  // finish() already zero-pads its returned copy. Empty sections need no writer, and completed writers are not
79
104
  // mutated just to obtain the same bytes a second way.
80
105
  export function finishSections(writers) { return writers.map(w => w ? w.finish() : new Uint8Array(0)); }