rapier-jxl 1.1.21 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +14 -10
- package/ENCODER-COMPARISON.md +6 -6
- package/README.md +75 -38
- package/admit.mjs +56 -0
- package/bits.mjs +30 -10
- package/effort.d.mts +11 -0
- package/effort.min.mjs +2 -0
- package/effort.mjs +217 -0
- package/entropy.mjs +16 -1
- package/frame.mjs +16 -9
- package/index.d.mts +13 -17
- package/index.mjs +30 -83
- package/jfif.mjs +407 -0
- package/jpeg.d.mts +14 -2
- package/jpeg.min.mjs +2 -2
- package/jpeg.mjs +34 -377
- package/lossless.mjs +33 -21
- package/lossy.mjs +41 -16
- package/package.json +9 -8
- package/photo.d.mts +6 -2
- package/photo.min.mjs +2 -2
- package/photo.mjs +73 -42
- package/public/examples/browser.html +4 -2
- package/public/examples/worker.mjs +34 -9
- package/rapier-jxl.min.mjs +2 -2
- package/sizes.json +179 -78
- package/vardct.mjs +30 -10
- package/weighted.mjs +79 -0
- package/writer.mjs +17 -0
- package/lossless.d.mts +0 -3
- package/lossless.min.mjs +0 -2
package/rapier-jxl.min.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
/*! Rapier JXL
|
|
2
|
-
function t(t){return t>=0?2*t:2*-t-1}function n(t){return 31-Math.clz32(t)}function o(t){return t<=1?0:32-Math.clz32(t-1)}function e(t){if(0===t)return 0;const n=t<0?32768:0;if(!((t=Math.abs(t))<65520))throw new Error("half float out of range: "+t);let o=Math.floor(Math.log2(t)),e=t/2**o-1;o<-14&&(e=t/2**-14,o=-15);let r=Math.round(1024*e);return 1024===r&&(r=0,o++),n|o+15<<10|r}function r(t,n){const o=new Uint8Array(t.length),e=[];for(let n=0;n<t.length;n++)t[n]>0&&e.push(n);if(!e.length)return o;if(1===e.length)return o[e[0]]=1,o;for(let r=1;;r*=2){const s=e.map(n=>({w:Math.max(t[n],r),t:n,l:null,r:null})).sort((t,n)=>t.w-n.w||t.t-n.t),i=[];let f=0,c=0;const a=()=>f<s.length&&(c>=i.length||s[f].w<=i[c].w)?s[f++]:i[c++];for(;s.length-f+(i.length-c)>1;){const t=a(),n=a();i.push({w:t.w+n.w,t:-1,l:t,r:n})}const l=[[a(),0]];let h=0;for(;l.length;){const[t,n]=l.pop();t.t>=0?(o[t.t]=n,n>h&&(h=n)):l.push([t.l,n+1],[t.r,n+1])}if(h<=n)return o}}function s(t,n,o,e,r,s){const i=[];for(;i.push(e&r),e>>=s;)e--;for(let e=i.length-1;e>=0;e--)t.push(o),n.push(i[e])}function i(t,n){let o=0;for(let e=0;e<n;e++)o=o<<1|1&t,t>>=1;return o}function f(t){const n=new Uint32Array(17),o=new Uint32Array(17),e=new Uint16Array(t.length);for(const o of t)n[o]++;n[0]=0;for(let t=1,e=0;t<=16;t++)e=e+n[t-1]<<1,o[t]=e;for(let n=0;n<t.length;n++)t[n]&&(e[n]=i(o[t[n]]++,t[n]));return e}function c(t){const n=[];for(let o=0;o<t.length;o++)t[o]>0&&n.push(o);const o=n.length,e=n[o-1]+1||1,s=o<=4?new Uint8Array(e):r(t,15).subarray(0,e);let i=0;if(o<=4){n.sort((n,o)=>t[o]-t[n]||n-o),i=4===o&&t[n[0]]>t[n[2]]+t[n[3]]?1:0;const e=o<2?[0]:2===o?[1,1]:3===o?[1,2,2]:i?[1,2,3,3]:[2,2,2,2];n.forEach((t,n)=>{s[t]=e[n]}),n.sort((t,n)=>s[t]-s[n]||t-n)}return{o:e,i:s,u:f(s),p:o&&o<=4?n:null,m:i}}function a(t,o){if(o.o<=1)return;if(o.p){const e=n(o.o-1)+1;t.write(2,1),t.write(2,o.p.length-1);for(const n of o.p)t.write(e,n);return void(4===o.p.length&&t.write(1,o.m))}const e=o.i,i=[],c=[];let a=e.length-1;for(;a>0&&!e[a];)a--;let l=8;for(let t=0;t<=a;){const n=e[t];let o=1;for(;t+o<=a&&e[t+o]===n;)o++;let r=o;if(n&&n!==l&&(i.push(n),c.push(0),r--,l=n),r<3)for(let t=0;t<r;t++)i.push(n),c.push(0);else s(i,c,n?16:17,r-3,n?3:7,n?2:3);t+=o}const h=new Uint32Array(18);for(const t of i)h[t]++;let u=0;for(const t of h)t&&u++;1===u&&(h[i[0]?0:1]=h[i[0]]);const w=r(h,5),d=f(w);let p=18;for(;p>0&&!w[ft[p-1]];)p--;t.write(2,0);for(let n=0;n<p;n++){const o=w[ft[n]];t.write(ct[o],at[o])}for(let n=0;n<i.length;n++){const o=i[n];t.write(w[o],d[o]),16===o?t.write(2,c[n]):17===o&&t.write(3,c[n])}}function l(t,n=0,o=0){return{split:t,M:n,A:o,U:1<<t}}function h(t,n,e=15){t.write(o(e+1),n.split),n.split!==e&&(t.write(o(n.split+1),n.M),t.write(o(n.split-n.M+1),n.A))}function u(t,o,e){if(o<t.U)return e[0]=o,e[1]=0,void(e[2]=0);const r=n(o),s=o-(1<<r),i=r-t.M-t.A;e[0]=t.U+(r-t.split<<t.M+t.A|s>>r-t.M<<t.A|s&(1<<t.A)-1),e[1]=i,e[2]=o>>t.A&(1<<i)-1}function w(t,n,o,e=0){u(t,n,lt),o[e+lt[0]]++}function d(t,n,o,e,r=0){u(o,e,lt);const s=r+lt[0];t.write(n.i[s],n.u[s]),lt[1]&&t.write(lt[1],lt[2])}function p(t,n){let e=0;for(const t of n)e=Math.max(e,t);const r=e?o(e+1):0;if(r<=3){if(t.write(1,1),t.write(2,r),r)for(const o of n)t.write(r,o);return}t.write(1,0),t.write(1,0);const s=l(8),i=l(4),f=n.length>=64,a=new Uint32Array(f?257:e+1),h=[];for(let t=0;t<n.length;){let o=1;for(;f&&t+o<n.length&&n[t+o]===n[t];)o++;if(o>=8)h.push([n[t],o-1]),a[n[t]]++,w(i,o-1-7,a,224),t+=o;else{for(let e=0;e<o;e++)h.push([n[t],0]),a[n[t]]++;t+=o}}const u=c(a);if(!f){y(t,{I:new Uint8Array(1),C:[{config:s,code:u}]});for(const[n]of h)d(t,u,s,n);return}const p=new Uint32Array(1);p[0]=1,y(t,{J:{S:224,minLength:7,L:i},I:new Uint8Array([1,0]),C:[{config:l(0),code:c(p)},{config:s,code:u}]});for(const[n,o]of h)d(t,u,s,n),o&&d(t,u,i,o-7,224)}function y(t,{J:o=null,I:e,C:r}){t.write(1,o?1:0),o&&(t.P([[0,224],[0,512],[0,4096],[15,8]],o.S),t.P([[0,3],[0,4],[2,5],[8,9]],o.minLength),h(t,o.L,8)),e.length>1&&p(t,e),t.write(1,1);for(const n of r)h(t,n.config,15);for(const o of r){const e=o.code.o-1;if(e){const o=n(e);t.write(1,1),t.write(4,o),t.write(o,e-(1<<o))}else t.write(1,0)}for(const n of r)a(t,n.code)}function m(t,n=0,o=1){return{G:t,offset:n,N:o,context:-1}}function b(t,n){const o=(e,r)=>{if(e===r)return n?t[e].k:t[e];const s=e+r>>1;return function(t,n,o,e){return{X:t,T:n,left:o,right:e}}(n,n?t[s].q:s,o(s+1,r),o(e,s))};return o(0,t.length-1)}function M(t){return b(t,0)}function g(n,o){const e=[o],r=[],s=[];for(const n of e){if(n.left){r.push(n.X+1,t(n.T)),e.push(n.left,n.right);continue}n.context=s.length,s.push(n);const o=1===n.N?0:31-Math.clz32(n.N&-n.N);r.push(0,n.G,t(n.offset),o,(n.N>>o)-1)}const i=new Uint32Array(64);for(const t of r)w(pt,t,i);const f=c(i);y(n,{I:new Uint8Array(6),C:[{config:pt,code:f}]});for(const t of r)d(n,f,pt,t);return s}function A(t,n){const o=[[3,0],[6,8],[10,72],[13,1096]];if("rct"===n.type)t.write(2,0),t.P(o,n._),t.P([[0,6],[2,0],[4,2],[6,10]],n.O??6);else if("palette"===n.type)t.write(2,1),t.P(o,n._),t.P([[0,1],[0,3],[0,4],[13,1]],n.D),t.P([[8,0],[10,256],[12,1280],[16,5376]],n.B),t.P([[0,0],[8,1],[10,257],[16,1281]],n.R??0),t.write(4,n.G??0);else{if("squeeze"!==n.type)throw new Error("unknown transform "+n.type);{t.write(2,2);const e=n.j??[];t.P([[0,0],[4,1],[6,9],[8,41]],e.length);for(const n of e)t.write(1,n.H?1:0),t.write(1,n.Z?1:0),t.P(o,n._),t.P([[0,1],[0,2],[0,3],[4,4]],n.D)}}}function U(t,{F:n=!0,Y:o=[]}={}){t.write(1,n?1:0),t.write(1,1),t.P([[0,0],[0,1],[4,2],[8,18]],o.length);for(const n of o)A(t,n)}function I(t,n,o,e=t=>t.context){const r=new Uint8Array(n.length+1);for(const t of n)r[t.context]=e(t)+1;const s=new Uint32Array(2);s[1]=1;const i=[{config:pt,code:c(s)}],f=Math.max(...r);for(let t=0;t<f;t++)i.push({config:pt,code:c(o[t])});return y(t,{J:dt,I:r,C:i}),i}function x(n,o,e,r,s,i){const f=i.G,c=i.offset,a=i.N,l=pt,h=wt;let u=0;const p=(t,e=l,r=0)=>{n?d(n,o,e,t,r):w(e,t,o,r)},y=()=>{if(u){if(u>=8)p(0),p(u-7-1,h,224);else for(let t=0;t<u;t++)p(0);u=0}},m=(n,o)=>{const r=e[n];let s;1===a?s=r-o-c:(s=Math.round((r-o-c)/a),e[n]=o+c+s*a),0!==s?(y(),p(t(s))):u++};for(let t=0,n=0;t<s;t++)for(let o=0;o<r;o++,n++){let s;if(0===f)s=0;else{const i=o?e[n-1]:t?e[n-r]:0,c=t?e[n-r]:i;if(1===f)s=i;else if(2===f)s=c;else if(3===f)s=(i+c)/2|0;else{const a=i+c-(o&&t?e[n-r-1]:i);if(5===f){const t=i<c?i:c,n=i<c?c:i;s=a<t?t:a>n?n:a}else{if(4!==f)throw new Error("predictor "+f+" is not coded here");s=Math.abs(a-i)<Math.abs(a-c)?i:c}}}m(n,s)}y()}function C(t,n){t.P([[9,1],[13,1],[18,1],[30,1]],n)}function v(t,n,o,e,r,{xyb:s=!1,orientation:i=1}={}){t.write(16,2815),t.write(1,0),C(t,o),t.write(3,0),C(t,n),t.write(1,0);const f=1!==i;t.write(1,f?1:0),f&&t.write(6,i-1),t.write(4,8),r?t.write(3,5):t.write(2,0),t.write(1,s?1:0),3===e?t.write(1,1):t.write(15,14100),f&&t.write(1,1),t.write(2,0),t.write(1,1),t.K()}function E(t,{alpha:n}){t.write(9,8),n&&t.write(2,0),t.write(2,1),J(t,n)}function J(t,n){t.write(n?7:5,0),t.write(11,1)}function S(t,n){const o=Math.ceil(t/mt),e=Math.ceil(n/mt);return{V:o,W:e,$:Math.ceil(t/bt),tt:Math.ceil(n/bt),nt:1===o&&1===e}}function L(t){return t.map(t=>t?t.finish():new Uint8Array(0))}function P(t,n){const o=n.map(t=>t.length);!function(t,n){t.write(1,0),t.K();for(const o of n)t.P([[10,0],[14,1024],[22,17408],[30,4211712]],o);t.K()}(t,o);const e=t.finish(),r=new Uint8Array(e.length+o.reduce((t,n)=>t+n,0));r.set(e);let s=e.length;for(const t of n)r.set(t,s),s+=t.length;return r}function G(t,n,o){const e=t>=n&&n>=o?1:t<=n&&n<=o?-1:0;if(!e)return 0;let r=(4*(t*=e)-3*(o*=e)-(n*=e)+6)/12|0;return r-(1&r)>2*(t-n)&&(r=2*(t-n)+1),r+(1&r)>2*(n-o)&&(r=2*(n-o)),e*r}function N(t,n){const{w:o,h:e,data:r}=t,s=n?o:e,i=s+1>>1,f=s-i,c=n?i:o,a=n?e:i,l=n?f:o,h=n?e:f,u=new Int16Array(c*a),w=new Int16Array(l*h),d=n?1:o;for(let t=0;t<a;t++)for(let e=0;e<c;e++){const i=n?e:t,a=n?t*o+2*e:2*t*o+e;if(i===f){u[t*c+e]=r[a];continue}const h=r[a],p=r[a+d],y=Mt(h,p),m=i+1<f?Mt(r[a+2*d],r[a+3*d]):1&s?r[a+2*d]:y;u[t*c+e]=y,w[t*l+e]=h-p-G(i?r[a-d]:y,y,m)}const p=(o,e,r)=>({w:e,h:r,ot:t.ot+ +n,et:t.et+!n,data:o});return[p(u,c,a),p(w,l,h)]}function k(t){const n=[],o=t.length;let e=t[0].w,r=t[0].h;for(o>2&&t[1].w===e&&t[1].h===r&&(n.push({H:!0,Z:!1,_:1,D:2}),n.push({H:!1,Z:!1,_:1,D:2})),!(e>r)&&r>8&&(n.push({H:!1,Z:!0,_:0,D:o}),r=r+1>>1);e>8||r>8;)e>8&&(n.push({H:!0,Z:!0,_:0,D:o}),e=e+1>>1),r>8&&(n.push({H:!1,Z:!0,_:0,D:o}),r=r+1>>1);return n}function X(t,n,o,{palette:e=!0}={}){const r=n*o;let s=!0,i=!0;for(let n=0,o=0;o<r&&(255===t[n+3]||(s=!1,i))&&(t[n]===t[n+1]&&t[n]===t[n+2]||(i=!1,s));o++,n+=4);const f=i?1:3,c=!s,a=f+(c?1:0),l=n=>i?c?t[n]+256*t[n+3]:t[n]:t[n]+256*t[n+1]+65536*t[n+2]+(c?16777216*t[n+3]:0),h=new Map;let u=null;for(let t=0,n=0;e&&n<r;n++,t+=4){const n=l(t);if(0!==n&&!h.has(n)&&(h.set(n,1),h.size>=2048))break}if(e&&h.size<2048){const t=[...h.keys()],n=(t,n)=>Math.floor(t/256**n)%256,o=t=>(.299*n(t,0)+.587*n(t,1)+.114*n(t,2)+.01)*(4===a?n(t,3):1);t.sort((t,n)=>o(t)-o(n));const e=new Map([[0,0]]);t.forEach((t,n)=>e.set(t,n+1)),u={colours:[0,...t],index:e,key:l,byte:n}}return{colour:f,alpha:c,channels:a,palette:u}}function T(t,n,o,{shape:e=X(t,n,o)}={}){let r,s;for(const i of e.palette?[e,{...e,palette:null}]:[e])try{const e=q(t,n,o,i);(!r||e.length<r.length)&&(r=e)}catch(t){if("JXL_SIZE"!==t.code)throw t;s=t}if(!r)throw s;return r}function q(t,n,o,e){const{channels:r,palette:s}=e,i=S(n,o),f=s?2:r,l=Array.from({length:f},()=>m(ut)),h=M(l),u=Array.from({length:f},()=>new Int16Array(65536)),w=(o,e,i,f)=>{for(let c=0;c<f;c++){let f=4*((e+c)*n+o),a=c*i;for(let n=0;n<i;n++,f+=4,a++)if(s)u[0][a]=s.index.get(s.key(f));else if(r>=3){const n=t[f],o=t[f+1],e=t[f+2],s=n-e,i=e+(s>>1),c=o-i;u[0][a]=i+(c>>1),u[1][a]=s,u[2][a]=c,4===r&&(u[3][a]=t[f+3])}else u[0][a]=t[f],2===r&&(u[1][a]=t[f+3])}};let d=null;if(s){const t=s.colours.length;d=new Int16Array(t*r),s.colours.forEach((n,o)=>{for(let e=0;e<r;e++)d[e*t+o]=s.byte(n,e)})}const p=t=>l[s?i.nt?1:0:t],y=s?1:r,b=t=>{for(let e=0;e<i.W;e++)for(let r=0;r<i.V;r++){const s=r*mt,f=e*mt,c=Math.min(mt,n-s),a=Math.min(mt,o-f);w(s,f,c,a),t(e*i.V+r,c,a)}};if(!s){const t=t=>{const n=c(t),o=new it(128);a(o,n);let e=o.rt;for(let o=0;o<t.length;o++)t[o]&&(e+=t[o]*(n.i[o]+Math.max(0,o<224?o-1:o-236)));return e},e=Math.min(n,32),s=Math.min(o,32),i=[ut,ht].map(t=>({G:t,st:l.map(()=>new Uint32Array(yt))}));for(const t of[0,.5,1]){w(Math.floor((n-e)*t),Math.floor((o-s)*t),e,s);for(const t of i)for(let n=0;n<r;n++)x(null,t.st[n],u[n],e,s,m(t.G))}l.forEach((n,o)=>{const e=i.map(n=>t(n.st[o])),r=e[1]<e[0]?1:0;n.G=i[r].G})}const A=s?[{type:"palette",_:0,D:r,B:s.colours.length}]:r>=3?[{type:"rct",_:0,O:6}]:[],C=l.map(()=>new Uint32Array(yt)),J=t=>C[l.indexOf(t)];d&&x(null,J(l[0]),d,s.colours.length,r,l[0]),b((t,n,o)=>{for(let t=0;t<y;t++)x(null,J(p(t)),u[t],n,o,p(t))});const L=new it(256);v(L,n,o,e.colour,e.alpha),E(L,{alpha:e.alpha});const G=new it(4096);G.write(1,1),G.write(1,1);const N=g(G,h),k=I(G,N,C,t=>l.indexOf(t)),X=t=>k[l.indexOf(t)+1].code;U(G,{F:!0,Y:A}),d&&x(G,X(l[0]),d,s.colours.length,r,l[0]);const T=[];if(i.nt)b((t,n,o)=>{for(let t=0;t<y;t++)x(G,X(p(t)),u[t],n,o,p(t))}),T.push(G.finish());else{T.push(G.finish());for(let t=0;t<i.$*i.tt+1;t++)T.push(new Uint8Array(0));b((t,n,o)=>{const e=new it(n*o*y+64);U(e,{F:!0,Y:[]});for(let t=0;t<y;t++)x(e,X(p(t)),u[t],n,o,p(t));T.push(e.finish())})}return P(L,T)}function _(t,n,o,{quality:e=90,shape:r=X(t,n,o,{palette:!1})}={}){const s=function(t){return t>=100?0:t>=30?.1+.09*(100-t):53/3e3*t*t-1.15*t+25}(e);if(!(s>0))throw new Error("lossy encoding needs a quality below 100");const{colour:i,alpha:f,channels:c}=r,a=n*o,l=Array.from({length:c},()=>new Int16Array(a));for(let n=0,o=0;o<a;o++,n+=4){if(3===i){const e=t[n],r=t[n+1],s=t[n+2],i=e-s,f=s+(i>>1),c=r-f;l[0][o]=f+(c>>1),l[1][o]=i,l[2][o]=c}else l[0][o]=t[n];f&&(l[c-1][o]=t[n+3])}const h=l.map((t,e)=>({w:n,h:o,ot:0,et:0,data:t,component:3===i?e:0===e?0:3})),u=1===n&&o>mt&&3===i?k(h).filter(t=>!t.H):[],w=function(t,n=k(t)){const o=t.map(t=>({...t,level:0,it:!1}));for(const t of n){const n=t._+t.D-1,e=t.Z?n+1:o.length;for(let r=t._;r<=n;r++){const[n,s]=N(o[r],t.H),i=o[r].level+1;o[r]={...n,level:i,it:o[r].it,component:o[r].component},o.splice(e+(r-t._),0,{...s,level:i,it:!0,component:o[r].component})}}return o}(h,u.length?u:void 0),d=t=>t.it?0:ut,p=t=>function(t,n,o,e){let r=Math.min(16,n+o);if(r>0&&r--,3===t)return 1;const s=.25*e**1.2,i=2**r,f=1===t||2===t?.35*s*Math.max(.5,1024/i):.35*s*1.1*(163.84/i);return Math.max(1,Math.floor(f))}(t.component,t.ot+(u.length&&t.component>0&&t.component<3?1:0),t.et,s),y=[...3===i?[{type:"rct",_:0,O:6}]:[],{type:"squeeze",j:u}],A=S(n,o),C=A.$*A.tt,J=A.V*A.W;let G=0;for(;G<w.length&&w[G].w<=mt&&w[G].h<=mt;)G++;const T=(t,n,o,e)=>{const r=n>>t.ot,s=o>>t.et;return{x:r,y:s,w:Math.min(t.w,r+(e>>t.ot))-r,h:Math.min(t.h,s+(e>>t.et))-s}},q=[{index:0,q:0,ft:[]}];for(let t=0;t<G;t++)q[0].ft.push({c:t,rect:{x:0,y:0,w:w[t].w,h:w[t].h}});if(!A.nt){for(let t=0;t<C;t++){const n=t%A.$,o=t/A.$|0,e={index:1+t,q:1+C+t,ft:[]};for(let t=G;t<w.length;t++){const r=w[t];if(Math.min(r.ot,r.et)<3)continue;const s=T(r,n*bt,o*bt,bt);s.w>0&&s.h>0&&e.ft.push({c:t,rect:s})}q.push(e)}for(let t=0;t<J;t++){const n=t%A.V,o=t/A.V|0,e={index:2+C+t,q:1+3*C+17+t,ft:[]};for(let t=G;t<w.length;t++){const r=w[t];if(Math.min(r.ot,r.et)>2)continue;const s=T(r,n*mt,o*mt,mt);s.w>0&&s.h>0&&e.ft.push({c:t,rect:s})}q.push(e)}}for(const t of q)for(const n of t.ft){const t=w[n.c];n.ct=m(d(t),0,p(t)),n.ct.channel=n.c}const _=q.filter(t=>t.ft.length),O=1===_.length?M(_[0].ft.map(t=>t.ct)):function(t){return b(t,1)}(_.map(t=>({q:t.q,k:M(t.ft.map(t=>t.ct))}))),z=new Int16Array(65536),D=t=>{const n=w[t.c],o=t.rect;if(o.w===n.w&&o.h===n.h)return n.data;for(let t=0;t<o.h;t++)z.set(n.data.subarray((o.y+t)*n.w+o.x,(o.y+t)*n.w+o.x+o.w),t*o.w);return z},B=[...new Set(q.flatMap(t=>t.ft.map(t=>t.c)))].sort((t,n)=>t-n),R=new Map(B.map((t,n)=>[t,n])),j=B.map(()=>new Uint32Array(yt));for(const t of q)for(const n of t.ft)x(null,j[R.get(n.c)],D(n),n.rect.w,n.rect.h,n.ct);const H=new it(256);v(H,n,o,i,f),E(H,{alpha:f});const Z=Array.from({length:A.nt?1:2+C+J},()=>null),F=Z[0]=new it(65536);F.write(1,1),F.write(1,1);const Y=I(F,g(F,O),j,t=>R.get(t.channel));U(F,{F:!0,Y:y});for(const t of q){let n=Z[t.index];n||(n=Z[t.index]=new it(4096),t.ft.length&&U(n,{F:!0,Y:[]}));for(const o of t.ft)x(n,Y[R.get(o.c)+1].code,D(o),o.rect.w,o.rect.h,o.ct)}return P(H,L(Z))}function O(t,n="JXL_JPEG"){return Object.assign(new Error("JPEG: "+t),{code:n})}function z(t,n){const o=new Uint16Array(65536);let e=0,r=0;for(let s=1;s<=16;s++){for(let i=0;i<t[s-1];i++){const t=n[r++],i=e<<16-s,f=1<<16-s;if(i+f>=65536)throw O("bad Huffman table");o.fill(s<<8|t,i,i+f),e++}e<<=1}return o}function D(t){if(!(t.length>4)||255!==t[0]||216!==t[1])throw O("not a JPEG");const n=[],o=[[],[]],e=new Set,r={count:0,lt:[]};let s=null,i=0,f=-1,c=!1,a=1,l=2;const h=n=>t[n]<<8|t[n+1],u=(n,o)=>{for(let e=0;e<o.length;e++)if(t[n+e]!==o.charCodeAt(e))return!1;return!0};for(;;){if(l>=t.length){if(s&&s.scans)break;throw O("truncated")}if(255!==t[l])throw O("marker expected");for(;255===t[l];)l++;const w=t[l++];if(217===w)break;if(216===w)throw O("a second start");if(w>=208&&w<=215)throw O("a restart marker outside a scan");if(1===w)continue;if(l+2>t.length)throw O("truncated");const d=h(l),p=l+2,y=l+d;if(d<2||y>t.length)throw O("bad segment");if(l=y,219===w)for(let o=p;o<y;){const e=t[o]>>4,r=15&t[o];if(e>1||r>3||o+1+(e?128:64)>y)throw O("bad quantisation table");o++;const i=new Int32Array(64);for(let n=0;n<64;n++)if(i[gt[n]]=e?h(o):t[o],o+=e?2:1,!i[gt[n]])throw O("a zero quantisation step");if(s?.progressive&&s.components.some(t=>t.tq===r&&t.quant&&t.coverage.some(t=>0!==t)&&t.quant.some((t,n)=>t!==i[n])))throw O("a quantisation table changed between progressive scans");n[r]=i}else if(196===w)for(let n=p;n<y;){const r=t[n]>>4,s=15&t[n];if(r>1||s>3||n+17>y)throw O("bad Huffman table");n++;const i=t.subarray(n,n+16);let f=0;for(const t of i)f+=t;if(f>256||n+16+f>y)throw O("bad Huffman table");const c=t.subarray(n+16,n+16+f);o[r][s]=z(i,c),0===r&&(c.some(t=>t>15)?e.add(s):e.delete(s)),n+=16+f}else if(192===w||193===w||194===w){if(s)throw O("two frames");if(y-p<6)throw O("bad frame header");if(8!==t[p])throw O("only 8-bit samples");const n=h(p+1),o=h(p+3),e=t[p+5];if(!o||!n)throw O("a height left to a DNL marker");if(1!==e&&3!==e)throw O("only grey or three components");if(y-p<6+3*e)throw O("bad frame header");if(o>rt.edge||n>rt.edge)throw O("a picture past "+rt.edge+" pixels on a side","JXL_DIMENSIONS");if(o*n>rt.pixels)throw O("a picture past "+rt.pixels+" pixels","JXL_DIMENSIONS");const r=[];let i=0;for(let n=0,o=p+6;n<e;n++,o+=3){const n=t[o+1]>>4,e=15&t[o+1],s=t[o+2];if(n<1||n>4||e<1||e>4)throw O("bad sampling");if(s>3)throw O("bad quantisation table");if(r.some(n=>n.id===t[o]))throw O("two components with one id");i+=n*e,r.push({id:t[o],h:n,v:e,tq:s,pred:0,coverage:new Int8Array(64).fill(-1)})}if(i>10)throw O("bad sampling");const f=Math.max(...r.map(t=>t.h)),c=Math.max(...r.map(t=>t.v)),a=Math.ceil(o/(8*f)),l=Math.ceil(n/(8*c));for(const t of r)t.blocksW=Math.ceil(Math.ceil(o*t.h/f)/8),t.blocksH=Math.ceil(Math.ceil(n*t.v/c)/8),t.stride=a*t.h,t.rows=l*t.v,t.coeffs=new Int16Array(t.stride*t.rows*64);s={progressive:194===w,width:o,height:n,components:r,hmax:f,vmax:c,mcusX:a,mcusY:l,scans:0}}else{if(195===w||w>=197&&w<=199||w>=201&&w<=203||w>=205&&w<=207)throw O("lossless, hierarchical or arithmetic coding");if(221===w){if(d<4)throw O("bad segment");i=h(p)}else{if(220===w)throw O("a DNL marker");if(218===w){if(!s)throw O("a scan before the frame");const r=t[p],f=[];if(r<1||r>4||p+1+2*r+3>y)throw O("bad scan header");let c=p+1;for(let e=0;e<r;e++,c+=2){const e=s.components.find(n=>n.id===t[c]);if(!e)throw O("a scan of an unknown component");if(f.some(t=>t.component===e))throw O("a component twice in one scan");if(e.quant||(e.quant=n[e.tq]),!e.quant)throw O("a scan without its quantisation table");f.push({component:e,dc:o[0][t[c+1]>>4],ac:o[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)&&f.some(t=>e.has(t.dcSlot)))throw O("bad Huffman table");l=R(t,y,s,f,a,h,u,w,i),s.scans++}else if(224===w)u(p,"JFIF")&&(c=!0);else if(225===w)1===a&&u(p,"Exif")&&(a=B(t,p+6,y));else if(226===w){if(d>=16&&u(p,"ICC_PROFILE\0")){const n=t[p+12],o=t[p+13];if(!n||!o||n>o||r.count&&r.count!==o||r.lt[n])throw O("a colour profile cut short");r.count=o,r.lt[n]=t.subarray(p+14,y)}}else 238===w&&d>=14&&u(p,"Adobe")&&(f=t[p+11])}}}if(!s||!s.scans)throw O("no picture");for(const t of s.components)for(let n=0;n<64;n++)if(0!==t.coverage[n])throw O(s.progressive?"a progressive JPEG whose scans do not finish every coefficient":"a component without its scan");if(r.count&&!function(t){const n=[];for(let o=1;o<=t.count;o++){if(!t.lt[o])throw O("a colour profile cut short");n.push(t.lt[o])}const o=n.reduce((t,n)=>t+n.length,0),e=new Uint8Array(o);for(let t=0,o=0;t<n.length;o+=n[t].length,t++)e.set(n[t],o);const r=t=>t+4<=o?(e[t]<<24|e[t+1]<<16|e[t+2]<<8|e[t+3])>>>0:0,s=t=>t+4<=o?String.fromCharCode(e[t],e[t+1],e[t+2],e[t+3]):"",i=r(128);if(o<132||132+12*i>o)return!1;for(let t=0;t<i;t++){const n=132+12*t;if("desc"!==s(n))continue;const i=r(n+4),f=r(n+8);if(i+f>o||f<16)return!1;let c="";if("desc"===s(i)){const t=Math.min(r(i+8),f-12);for(let n=0;n<t;n++)c+=String.fromCharCode(e[i+12+n])}else{if("mluc"!==s(i))return!1;{if(!r(i+8)||f<28)return!1;const t=r(i+20),n=i+r(i+24);if(n+t>o)return!1;for(let o=0;o+1<t;o+=2)c+=String.fromCharCode(e[n+o]<<8|e[n+o+1])}}return/sRGB|sGray|IEC ?61966/i.test(c)}return!1}(r))throw O("a colour profile other than sRGB (decode the picture and encode its pixels)");let w=!1;return 3!==s.components.length||c||(w=f>=0?0===f: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:!w,orientation:a}}function B(t,n,o){const e=77===t[n]&&77===t[n+1];if(!e&&(73!==t[n]||73!==t[n+1]))return 1;const r=n=>n+2>o?0:e?t[n]<<8|t[n+1]:t[n]|t[n+1]<<8;if(42!==r(n+2))return 1;const s=n+((f=n+4)+4>o?0:e?(t[f]<<24|t[f+1]<<16|t[f+2]<<8|t[f+3])>>>0:(t[f]|t[f+1]<<8|t[f+2]<<16|t[f+3]<<24)>>>0),i=r(s);var f;for(let t=0,n=s+2;t<i&&n+12<=o;t++,n+=12){if(274!==r(n))continue;const t=r(n+8);return t>=1&&t<=8?t:1}return 1}function R(t,n,o,e,r,s,i,f,c){!function(t,n,o,e,r,s){if(!t.progressive){if(0!==o||63!==e||0!==r||0!==s)throw O("a bad spectral selection");for(const t of n){if(t.component.coverage.some(t=>-1!==t))throw O("a component coded twice");t.component.coverage.fill(0)}return}if(s>13||r&&r!==s+1)throw O("a bad successive approximation");if(0===o){if(0!==e)throw O("a bad spectral selection");for(const{component:t}of n){if(t.coverage[0]!==(r||-1))throw O(r?"a refinement scan out of order":"a DC scan twice");t.coverage[0]=s}return}if(1!==n.length)throw O("an interleaved AC scan");if(e>63||o>e)throw O("a bad spectral selection");const{coverage:i}=n[0].component;if(-1===i[0])throw O("an AC scan before the DC scan");for(let t=o;t<=e;t++){if(i[t]!==(r||-1))throw O(r?"a refinement scan out of order":"a band coded twice");i[t]=s}}(o,e,r,s,i,f);let a=n,l=0,h=0,u=0,w=!1;const d=()=>{for(;h<=24;){let n=0;if(w||a>=t.length)w=!0,u+=8;else if(n=t[a],255===n){const o=t[a+1];if(0===o)a+=2;else{if(255===o){a++;continue}w=!0,n=0,u+=8}}else a++;l=(l<<8|n)>>>0,h+=8}},p=()=>O("a scan cut short"),y=t=>{if(!t)return 0;if(d(),t>h-u)throw p();const n=l>>>h-t&(1<<t)-1;return h-=t,n},m=(t,n)=>t<1<<n-1?t-(1<<n)+1:t,b=t=>{if(!t)throw O("a scan without its Huffman table");d();const n=t[l>>>h-16&65535];if(!n)throw O("bad Huffman code");if(n>>8>h-u)throw p();return h-=n>>8,255&n},M=()=>{const n=h-u;if(n>=8)throw O("bytes left over in a scan");if(I)throw O("an end-of-band run past its interval");if(n&&(l>>>u&(1<<n)-1)!=(1<<n)-1)throw O("bad scan padding");for(;a+1<t.length&&255===t[a]&&255===t[a+1];)a++},g=n=>{if(M(),255!==t[a]||t[a+1]!==208+(7&n))throw O("a restart marker missing or out of order");a+=2,l=0,h=0,u=0,w=!1;for(const t of e)t.component.pred=0;I=0},A=(t,n)=>{if(n>11)throw O("a DC value out of range");const o=n?m(y(n),n):0;if(t.component.pred+=o,t.component.pred>2047||t.component.pred<-2048)throw O("a DC value out of range");return t.component.pred},U=(t,n)=>{if(t>10)throw O("an AC value out of range");const o=m(y(t),t)*(1<<n);if(o>1023||o<-1023)throw O("an AC value out of range");return o};let I=0;const x=1===e.length,C=o.progressive,v=()=>O("coefficients past the "+(C?"band":"block")),E=(t,n,o)=>{if(I>0)I--;else for(let e=r||1;e<=s;){const r=b(t.ac),i=r>>4,c=15&r;if(c){if(e+=i,e>s)throw v();n[o+gt[e]]=U(c,f),e++}else{if(i<15){if(i&&!C)throw O("an end-of-band run in a sequential scan");I=(1<<i)-1+(i?y(i):0);break}if(e+=16,e>s+1)throw v()}}},J=C?0===r?0===i?(t,n,o)=>{const e=A(t,b(t.dc))*(1<<f);if(e>2047||e<-2048)throw O("a DC value out of range");n[o]=e}:(t,n,o)=>{y(1)&&(n[o]|=1<<f)}:0===i?E:(t,n,o)=>{const e=1<<f,i=-1<<f;let c=r;if(I<=0)for(;c<=s;c++){const r=b(t.ac);let f=r>>4,a=0;const l=15&r;if(l){if(1!==l)throw O("a bad refinement code");a=y(1)?e:i}else if(15!==f){I=1<<f,f&&(I+=y(f));break}for(;c<=s;c++){const t=o+gt[c];if(0!==n[t])y(1)&&0===(n[t]&e)&&(n[t]+=n[t]>=0?e:i);else if(--f<0)break}if(f>=0||a&&c>s)throw O("coefficients past the band");a&&(n[o+gt[c]]=a)}if(I>0){for(;c<=s;c++){const t=o+gt[c];0!==n[t]&&y(1)&&0===(n[t]&e)&&(n[t]+=n[t]>=0?e:i)}I--}}:(t,n,o)=>{n[o]=A(t,b(t.dc)),E(t,n,o)};for(const t of e)t.component.pred=0;const S=e[0].component,L=x?S.blocksW*S.blocksH:o.mcusX*o.mcusY;for(let t=0;t<L;t++)if(c&&t>0&&t%c===0&&g(t/c-1),x){const n=t/S.blocksW|0,o=t-n*S.blocksW;J(e[0],S.coeffs,64*(n*S.stride+o))}else{const n=t/o.mcusX|0,r=t-n*o.mcusX;for(const t of e){const o=t.component;for(let e=0;e<o.v;e++)for(let s=0;s<o.h;s++)J(t,o.coeffs,64*((n*o.v+e)*o.stride+r*o.h+s))}}if(M(),255===t[a]&&t[a+1]>=208&&t[a+1]<=215){if(!c||L%c||t[a+1]!==208+(L/c-1&7))throw O("a restart marker out of order");for(a+=2;a+1<t.length&&255===t[a]&&255===t[a+1];)a++}if(a<t.length&&!(255===t[a]&&a+1<t.length&&0!==t[a+1]&&255!==t[a+1]))throw O("bytes left over after a scan");return a}function j(t,n){return u(t,n,xt),xt[0]}function H(t){let n=0,o=0;for(const o of t)n+=o;for(const e of t)e&&(o+=e*Math.log2(n/e));return o}function Z(t,n,o){const e=[];if(n>0){const o=new Uint32Array(2048);let r=0;for(const n of t)o[Math.min(2047,Math.max(0,n+1024))]++,r++;let s=0,i=r/(n+1);for(let t=0;t<2048&&e.length<n;t++)s+=o[t],s>i&&(e.push(t-1025),i=r*(e.length+1)/(n+1))}const r=e.length+1,s=new Uint8Array(39*r);for(let t=0;t<r;t++)for(let n=0;n<Ct;n++)s[(0+n)*r+t]=t,s[(13+n)*r+t]=o?r:r+(t>>1),s[(26+n)*r+t]=o?r:r+(r-1>>1)+1+(t>>1);let i=0;for(const t of s)i=Math.max(i,t+1);return{thresholds:e,ht:r,map:s,ut:i,wt:t=>{let n=0;for(const o of e)t>o&&n++;return n},dt:(t,n)=>s[(t<2?1^t:2)*Ct*r+n],yt:(t,n)=>(Math.min(t,64)<8?Math.min(t,64):4+(Math.min(t,64)>>1))*i+n,bt:t=>37*i+458*t,Mt:495*i}}function F(t,n,o){const e=n.map(()=>m(o)),r=e.map(()=>new Uint32Array(yt));n.forEach((t,n)=>x(null,r[n],t.data,t.w,t.h,e[n])),U(t,{F:!1,Y:[]});const s=g(t,M(e)),i=I(t,s,r,t=>e.indexOf(t));n.forEach((n,o)=>x(t,i[o+1].code,n.data,n.w,n.h,e[o]))}function Y(n,r=D(n)){return function(n){const{width:r,height:s,components:i,alpha:f}=n,a=1===i.length,l=n.quantScale||1,h=n.ycbcr||a,u=(a?[0,0,0]:h?[1,0,2]:[0,1,2]).map(t=>i[t]),w=t=>1===t?0:2===t?1:-1,m=u.map(t=>w(t.h)),b=u.map(t=>w(t.v));if(m.includes(-1)||b.includes(-1))throw O("sampling factors other than one or two");const M=Math.max(...m),g=Math.max(...b),A=t=>M-m[t],U=t=>g-b[t];if(!h&&(M||g))throw O("an RGB JPEG with subsampling");const I=Math.ceil(r/(8<<M))<<M,x=Math.ceil(s/(8<<g))<<g,C=Math.ceil(r/mt),E=Math.ceil(s/mt),S=C*E,G=Math.ceil(I/256),N=G*Math.ceil(x/256),k=1===S,X=(t,n,o)=>{const e=u[t];return a&&1!==t?-1:64*(o*e.stride+n)},T=t=>{let n=1024;for(;t%n;)n>>=1;return n},q=u.map(t=>h?1:t.quant[0]/T(t.quant[0])),_=u.map(t=>h?0:1024/T(t.quant[0])),z=new Int32Array(I*x);for(let t=0;t<x;t++)for(let n=0;n<I;n++)z[t*I+n]=u[1].coeffs[X(1,n,t)]*q[1]+_[1];const D=u[1].quant,B=(D[8]+D[16]+D[24]+D[32]+D[40])/l,R=I*x,j=R<256?0:Math.max(1,Math.min(7,o(R)-o(B)-7));let Y=Z(z,j,a);const Q=new it(256);if(v(Q,r,s,a?1:3,!!f,{orientation:n.orientation}),Q.write(1,0),Q.write(2,0),Q.write(1,0),Q.write(2,2),Q.write(8,111),Q.write(1,h?1:0),h)for(let t=0;t<3;t++)Q.write(2,0===m[t]&&0===b[t]?0:1===m[t]&&1===b[t]?1:1===m[t]?2:3);Q.write(2,0),f&&Q.write(2,0),J(Q,f);const K=[0,1,2].map(()=>new Int32Array(1024)),V=(n,o)=>{const e=n%C*32,r=32*(n/C|0),s=Math.min(32,I-e),i=Math.min(32,x-r);for(let n=0;n<i;n++)for(let i=0;i<s;i++)for(const s of[1,0,2]){const f=A(s),c=U(s),a=i>>f,l=n>>c;if(a<<f!==i||l<<c!==n)continue;const h=X(s,(e>>f)+a,(r>>c)+l),w=u[s].coeffs;let d=0;if(h>=0)for(let t=1;t<64;t++)w[h+t]&&d++;const p=K[s],y=0===a?0===l?32:p[32*(l-1)]:0===l?p[32*l+a-1]:p[32*(l-1)+a]+p[32*l+a-1]+1>>1;p[32*l+a]=d;const m=Y.dt(s,Y.wt(z[(r+n)*I+e+i]));o(Y.yt(y,m),d);const b=Y.bt(m);let M=d>4?0:1,g=d;for(let n=1;n<64&&g;n++){const e=w[h+Jt[n]];o(b+St(g,n,M),t(e)),M=e?1:0,g-=M}}},W=t=>{Y=t;const n=new It(t.Mt);for(let t=0;t<S;t++)V(t,(t,o)=>n.add(t,o));return function(t,{maxClusters:n=48,newClusterCost:o=320}={}){const e=[];for(let n=0;n<t.Mt;n++)t.gt[n]>0&&e.push(n);e.sort((n,o)=>t.gt[o]-t.gt[n]);const r=[];for(const s of e){const e=t.At([s],Ut),i=H(e);let f=-1,c=1/0;for(let t=0;t<r.length;t++){const n=r[t],o=new Uint32Array(64);for(let t=0;t<64;t++)o[t]=n.st[t]+e[t];const s=H(o)-n.Ut-i;s<c&&(c=s,f=t)}if(f<0||r.length<n&&c>o)r.push({Mt:[s],st:e,Ut:i});else{const t=r[f];t.Mt.push(s);for(let n=0;n<64;n++)t.st[n]+=e[n];t.Ut=H(t.st)}}r.length||r.push({Mt:[],st:new Uint32Array(64),Ut:0});const s=new Uint8Array(t.Mt);let i=2*t.Mt;const f=r.map((n,o)=>{for(const t of n.Mt)s[t]=o;let e=At[0],r=1/0;for(const o of At){const s=t.It(n.Mt,o);s<r&&(r=s,e=o)}const f=c(t.At(n.Mt,e,128));return i+=r+3*f.o+40,{config:e,code:f}});return{I:s,C:f,write:(t,n,o)=>{const e=f[s[n]];d(t,e.code,e.config,o)},Ut:i}}(n)};let $=W(Y);if(j>0){const t=Y,n=W(Z(z,0,a));n.Ut<=$.Ut?$=n:Y=t}const tt=Array.from({length:k?1:2+N+S},()=>null),nt=t=>tt[k?0:t]||(tt[k?0:t]=new it(4096)),ot=nt(0);ot.write(1,0);const et=(t,n)=>{const o=t*mt,e=n*mt,i=Math.min(mt,r-o),c=Math.min(mt,s-e),a=new Int32Array(i*c);for(let t=0;t<c;t++)for(let n=0;n<i;n++)a[t*i+n]=f[4*((e+t)*r+o+n)+3];return{w:i,h:c,data:a}};for(let t=0;t<3;t++)ot.write(16,e(u[t].quant[0]/q[t]/(2040*l)*128));ot.write(2,3),ot.write(16,57343),ot.write(2,1),ot.write(5,0),ot.write(1,0),ot.write(4,0),ot.write(4,Y.thresholds.length);for(const n of Y.thresholds)ot.P([[4,0],[8,16],[16,272],[32,65808]],t(n));ot.write(4,0),ot.write(4,0),p(ot,Y.map),ot.write(1,0),ot.write(2,0),ot.write(16,0),ot.write(16,0),ot.write(8,128),ot.write(8,128),ot.write(1,0),f&&F(ot,[k?et(0,0):{w:0,h:0,data:new Int32Array(0)}],ut);for(let t=0;t<N;t++){const n=nt(1+t),e=t%G*256,r=256*(t/G|0),s=Math.min(256,I-e),i=Math.min(256,x-r);n.write(2,0),F(n,[1,0,2].map(t=>{const n=s>>A(t),o=i>>U(t),f=new Int32Array(n*o),c=q[t],a=_[t];for(let s=0;s<o;s++)for(let o=0;o<n;o++){const i=X(t,(e>>A(t))+o,(r>>U(t))+s);f[s*n+o]=i<0?0:u[t].coeffs[i]*c+a}return{w:n,h:o,data:f}}),ut);const f=s*i,c=o(f);c&&n.write(c,f-1);const a=s+7>>3,l=i+7>>3;F(n,[{w:a,h:l,data:new Int32Array(a*l)},{w:a,h:l,data:new Int32Array(a*l)},{w:f,h:2,data:new Int32Array(2*f)},{w:s,h:i,data:new Int32Array(f)}],0)}const rt=nt(1+N);rt.write(1,0),rt.write(3,7),rt.write(16,e(1/(2040*l))),F(rt,[0,1,2].map(t=>{const n=new Int32Array(64),o=u[t].quant;for(let t=0;t<8;t++)for(let e=0;e<8;e++)n[8*e+t]=o[8*t+e];return{w:8,h:8,data:n}}),ut);for(let t=1;t<17;t++)rt.write(3,0);S>1&&rt.write(o(S),0),rt.write(2,2),y(rt,{I:$.I,C:$.C});for(let t=0;t<S;t++){const n=nt(2+N+t);V(t,(t,o)=>$.write(n,t,o)),f&&!k&&F(n,[et(t%C,Math.floor(t/C))],ut)}return P(Q,L(tt))}(r)}function Q(t,n){if(!Number.isInteger(t)||!Number.isInteger(n)||t<1||n<1)throw Lt("JXL_INPUT","A picture is at least one pixel wide and high, in whole pixels.");if(t>rt.edge||n>rt.edge)throw Lt("JXL_DIMENSIONS","A picture is at most "+rt.edge+" pixels on a side.");if(t*n>rt.pixels)throw Lt("JXL_DIMENSIONS","A picture is at most "+rt.pixels.toLocaleString("en-US")+" pixels.")}function K(t,n,o){if(Q(n,o),!(t instanceof Uint8Array||t instanceof Uint8ClampedArray))throw Lt("JXL_INPUT","Pixels are a Uint8Array or Uint8ClampedArray of RGBA bytes.");if(t.length!==n*o*4)throw Lt("JXL_INPUT","Pixels are width * height * 4 bytes: straight (not premultiplied) RGBA, row by row.")}function V(t){if("number"!=typeof t||!Number.isFinite(t)||t<1||t>100)throw Lt("JXL_INPUT","Quality is a number from 1 to 100.")}function W(t){if(t.length>rt.bytes)throw Lt("JXL_SIZE","The encoded picture exceeds 16 MiB.");return t}function $(t){try{return t()}catch(t){if(t instanceof RangeError&&!t.code)throw Lt("JXL_MEMORY","Not enough memory for this picture.");throw t}}function tt(t,n,o){return K(t,n,o),$(()=>W(T(t,n,o,{shape:X(t,n,o)})))}function nt(t,n,o,e=90){return K(t,n,o),V(e),$(()=>{const r=X(t,n,o);if(e>=100)return W(T(t,n,o,{shape:r}));if(!r.palette)return W(_(t,n,o,{quality:e,shape:r}));const s=T(t,n,o,{shape:r});let i;try{i=_(t,n,o,{quality:e,shape:r})}catch(t){if(!(t instanceof RangeError)||t.code)throw t;i=s}return W(s.length<=i.length?s:i)})}function ot(t,n,o,e={}){if(null===e||"object"!=typeof e)throw Lt("JXL_INPUT","Options are an object: {quality}.");const r=void 0===e.quality?100:e.quality;return V(r),r>=100?tt(t,n,o):nt(t,n,o,r)}function et(t){if(!(t instanceof Uint8Array&&t.length))throw Lt("JXL_INPUT","A JPEG is a non-empty Uint8Array.");if(t.length>rt.bytes)throw Lt("JXL_SIZE","A JPEG is at most 16 MiB.");return $(()=>{const n=D(t);Q(n.width,n.height);const o=W(Y(t,n)),e=n.orientation>=5;return{bytes:o,width:e?n.height:n.width,height:e?n.width:n.height,orientation:n.orientation}})}const rt=Object.freeze({bytes:16777216,pixels:24e6,edge:16384}),st=Array.from({length:33},(t,n)=>2**n);class it{constructor(t=4096){this.bytes=new Uint8Array(t),this.at=0,this.xt=0,this.pending=0}write(t,n){if(t>32||n<0||n>=(st[t]??2**t))throw new Error("bit write out of range: "+t+" bits, "+n);let o=this.xt+n*(1<<this.pending),e=this.pending+t;this.at+5>=this.bytes.length&&this.grow();const r=this.bytes;for(;e>=8;)r[this.at++]=255&o,o=Math.floor(o/256),e-=8;this.xt=o,this.pending=e}P(t,n){for(let o=0;o<4;o++){const[e,r]=t[o];if(n>=r&&n-r<2**e)return this.write(2,o),void(e&&this.write(e,n-r))}throw new Error("U32 value out of range: "+n)}K(){this.pending&&(this.at+1>=this.bytes.length&&this.grow(),this.bytes[this.at++]=this.xt,this.xt=0,this.pending=0)}get rt(){return 8*this.at+this.pending}grow(t=0){const n=rt.bytes+64;if(this.at+t+16>n)throw Object.assign(new Error("The encoded picture exceeds 16 MiB."),{code:"JXL_SIZE"});const o=new Uint8Array(Math.min(n,Math.max(2*this.bytes.length,this.at+t+16)));o.set(this.bytes.subarray(0,this.at)),this.bytes=o}append(t){if(this.at+t.at+8>=this.bytes.length&&this.grow(t.at+8),this.pending)for(let n=0;n<t.at;n++)this.write(8,t.bytes[n]);else this.bytes.set(t.bytes.subarray(0,t.at),this.at),this.at+=t.at;t.pending&&this.write(t.pending,t.xt)}finish(){const t=this.at+(this.pending?1:0),n=this.bytes.slice(0,t);return this.pending&&(n[this.at]=this.xt),n}}const ft=[1,2,3,4,0,5,17,6,16,7,8,9,10,11,12,13,14,15],ct=[2,4,3,2,2,4],at=[0,7,3,2,1,15],lt=[0,0,0],ht=3,ut=5,wt=l(4),dt=Object.freeze({S:224,minLength:7,L:wt}),pt=l(0),yt=257,mt=256,bt=2048,Mt=(t,n)=>t+n+(t>n?1:0)>>1,gt=[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],At=[l(0),l(1),l(2),l(3),l(4),l(4,1,1),l(5,1,1),l(2,0,1),l(3,0,1)],Ut=l(4,1,1);class It{constructor(t){this.Mt=t,this.small=new Uint32Array(64*t),this.Ct=Array.from({length:t},()=>null),this.gt=new Float64Array(t)}add(t,n){if(this.gt[t]++,n<64)return void this.small[64*t+n]++;const o=this.Ct[t]||(this.Ct[t]=new Map);o.set(n,(o.get(n)||0)+1)}At(t,n,o=64){const e=new Uint32Array(o);for(const o of t){const t=64*o;for(let o=0;o<64;o++)this.small[t+o]&&(e[j(n,o)]+=this.small[t+o]);const r=this.Ct[o];if(r)for(const[t,o]of r)e[j(n,t)]+=o}return e}It(t,o){let e=H(this.At(t,o));for(const r of t){const t=64*r;for(let r=o.U;r<64;r++)this.small[t+r]&&(e+=this.small[t+r]*(n(r)-o.M-o.A));const s=this.Ct[r];if(s)for(const[t,r]of s)e+=r*(n(t)-o.M-o.A)}return e}}const xt=[0,0,0],Ct=13,vt=[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],Jt=gt.map(t=>(7&t)<<3|t>>3),St=(t,n,o)=>2*(Et[t]+vt[n])+o,Lt=(t,n)=>Object.assign(new Error(n),{code:t});export{ot as encode,tt as encodeLosslessRGBA,nt as encodeLossyRGBA,et as transcode,rt as LIMITS,T as encodeLossless,_ as encodeLossy,Y as transcodeJPEG,D as parseJPEG,X as inspectPixels};
|
|
1
|
+
/*! Rapier JXL 2.0.0 | MIT License Copyright (c) 2026 rapier.website Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
2
|
+
function*t(t,n,o){let e,r;for(;!(e=t.next(r)).done;)r=yield(n+e.value)/o;return e.value}function n(t){return t>=0?2*t:2*-t-1}function o(t){return 31-Math.clz32(t)}function e(t){return t<=1?0:32-Math.clz32(t-1)}function r(t,n){t.t([[9,1],[13,1],[18,1],[30,1]],n)}function i(t,n,o,e,i,{xyb:s=!1,orientation:c=1,colorSpace:f}={}){t.write(16,2815),t.write(1,0),r(t,o),t.write(3,0),r(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),i?t.write(3,5):t.write(2,0),t.write(1,s?1:0),3===e&&"display-p3"===f?t.write(21,903568):3===e?t.write(1,1):t.write(15,14100),l&&t.write(1,1),t.write(2,0),t.write(1,1),t.o()}function s(t,{alpha:n,shift:o=1}){t.write(9,8),n&&t.write(2,0),t.write(2,o),function(t,n){t.write(n?7:5,0),t.write(11,1)}(t,n)}function c(t,n,o=256){const e=Math.ceil(t/o),r=Math.ceil(n/o);return{i:e,u:r,p:Math.ceil(t/(8*o)),m:Math.ceil(n/(8*o)),M:1===e&&1===r}}function f(t,n){const o=n.map(t=>t.length);!function(t,n){t.write(1,0),t.o();for(const o of n)t.t([[10,0],[14,1024],[22,17408],[30,4211712]],o);t.o()}(t,o);const e=t.finish(),r=new Uint8Array(e.length+o.reduce((t,n)=>t+n,0));r.set(e);let i=e.length;for(const t of n)r.set(t,i),i+=t.length;return r}function l(t,n){const o=new Uint8Array(t.length),e=[];for(let n=0;n<t.length;n++)t[n]>0&&e.push(n);if(!e.length)return o;if(1===e.length)return o[e[0]]=1,o;for(let r=1;;r*=2){const i=e.map(n=>({w:Math.max(t[n],r),A:n,l:null,r:null})).sort((t,n)=>t.w-n.w||t.A-n.A),s=[];let c=0,f=0;const l=()=>c<i.length&&(f>=s.length||i[c].w<=s[f].w)?i[c++]:s[f++];for(;i.length-c+(s.length-f)>1;){const t=l(),n=l();s.push({w:t.w+n.w,A:-1,l:t,r:n})}const a=[[l(),0]];let u=0;for(;a.length;){const[t,n]=a.pop();t.A>=0?(o[t.A]=n,n>u&&(u=n)):a.push([t.l,n+1],[t.r,n+1])}if(u<=n)return o}}function a(t,n,o,e,r,i){const s=[];for(;s.push(e&r),e>>=i;)e--;for(let e=s.length-1;e>=0;e--)t.push(o),n.push(s[e])}function u(t,n){let o=0;for(let e=0;e<n;e++)o=o<<1|1&t,t>>=1;return o}function h(t){const n=new Uint32Array(17),o=new Uint32Array(17),e=new Uint16Array(t.length);for(const o of t)n[o]++;n[0]=0;for(let t=1,e=0;t<=16;t++)e=e+n[t-1]<<1,o[t]=e;for(let n=0;n<t.length;n++)t[n]&&(e[n]=u(o[t[n]]++,t[n]));return e}function p(t){const n=[];for(let o=0;o<t.length;o++)t[o]>0&&n.push(o);const o=n.length,e=n[o-1]+1||1,r=o<=4?new Uint8Array(e):l(t,15).subarray(0,e);let i=0;if(o<=4){n.sort((n,o)=>t[o]-t[n]||n-o),i=4===o&&t[n[0]]>t[n[2]]+t[n[3]]?1:0;const e=o<2?[0]:2===o?[1,1]:3===o?[1,2,2]:i?[1,2,3,3]:[2,2,2,2];n.forEach((t,n)=>{r[t]=e[n]}),n.sort((t,n)=>r[t]-r[n]||t-n)}return{U:e,S:r,I:h(r),C:o&&o<=4?n:null,T:i}}function y(t,n){if(n.U<=1)return;if(n.C){const e=o(n.U-1)+1;t.write(2,1),t.write(2,n.C.length-1);for(const o of n.C)t.write(e,o);return void(4===n.C.length&&t.write(1,n.T))}const e=n.S,r=[],i=[];let s=e.length-1;for(;s>0&&!e[s];)s--;let c=8;for(let t=0;t<=s;){const n=e[t];let o=1;for(;t+o<=s&&e[t+o]===n;)o++;let f=o;if(n&&n!==c&&(r.push(n),i.push(0),f--,c=n),f<3)for(let t=0;t<f;t++)r.push(n),i.push(0);else a(r,i,n?16:17,f-3,n?3:7,n?2:3);t+=o}const f=new Uint32Array(18);for(const t of r)f[t]++;let u=0;for(const t of f)t&&u++;1===u&&(f[r[0]?0:1]=f[r[0]]);const p=l(f,5),y=h(p);let w=18;for(;w>0&&!p[k[w-1]];)w--;t.write(2,0);for(let n=0;n<w;n++){const o=p[k[n]];t.write(Y[o],Z[o])}for(let n=0;n<r.length;n++){const o=r[n];t.write(p[o],y[o]),16===o?t.write(2,i[n]):17===o&&t.write(3,i[n])}}function w(t,n=0,o=0){return{split:t,L:n,N:o,P:1<<t}}function d(t,n,o=15){t.write(e(o+1),n.split),n.split!==o&&(t.write(e(n.split+1),n.L),t.write(e(n.split-n.L+1),n.N))}function g(t,n,e){if(n<t.P)return e[0]=n,e[1]=0,void(e[2]=0);const r=o(n),i=n-(1<<r),s=r-t.L-t.N;e[0]=t.P+(r-t.split<<t.L+t.N|i>>r-t.L<<t.N|i&(1<<t.N)-1),e[1]=s,e[2]=n>>t.N&(1<<s)-1}function m(t,n,o,e=0){g(t,n,D),o[e+D[0]]++}function M(t,n,o,e,r=0){g(o,e,D);const i=r+D[0];t.write(n.S[i],n.I[i]),D[1]&&t.write(D[1],D[2])}function b(t,{X:n=null,v:r,J:i}){t.write(1,n?1:0),n&&(t.t([[0,224],[0,512],[0,4096],[15,8]],n._),t.t([[0,3],[0,4],[2,5],[8,9]],n.minLength),d(t,n.q,8)),r.length>1&&function(t,n){let o=0;for(const t of n)o=Math.max(o,t);const r=o?e(o+1):0;if(r<=3){if(t.write(1,1),t.write(2,r),r)for(const o of n)t.write(r,o);return}t.write(1,0),t.write(1,0);const i=w(8),s=w(4),c=n.length>=64,f=new Uint32Array(c?257:o+1),l=[];for(let t=0;t<n.length;){let o=1;for(;c&&t+o<n.length&&n[t+o]===n[t];)o++;if(o>=8)l.push([n[t],o-1]),f[n[t]]++,m(s,o-1-7,f,224),t+=o;else{for(let e=0;e<o;e++)l.push([n[t],0]),f[n[t]]++;t+=o}}const a=p(f);if(!c){b(t,{v:new Uint8Array(1),J:[{config:i,code:a}]});for(const[n]of l)M(t,a,i,n);return}const u=new Uint32Array(1);u[0]=1,b(t,{X:{_:224,minLength:7,q:s},v:new Uint8Array([1,0]),J:[{config:w(0),code:p(u)},{config:i,code:a}]});for(const[n,o]of l)M(t,a,i,n),o&&M(t,a,s,o-7,224)}(t,r),t.write(1,1);for(const n of i)d(t,n.config,15);for(const n of i){const e=n.code.U-1;if(e){const n=o(e);t.write(1,1),t.write(4,n),t.write(n,e-(1<<n))}else t.write(1,0)}for(const n of i)y(t,n.code)}function A(t,n=0,o=1){return{G:t,offset:n,O:o,context:-1}}function U(t,n){const o=(e,r)=>{if(e===r)return n?t[e].j:t[e];const i=e+r>>1;return function(t,n,o,e){return{B:t,R:n,left:o,right:e}}(n,n?t[i].k:i,o(i+1,r),o(e,i))};return o(0,t.length-1)}function S(t){return U(t,0)}function x(t,o){const e=[o],r=[],i=[];for(const t of e){if(t.left){r.push(t.B+1,n(t.R)),e.push(t.left,t.right);continue}t.context=i.length,i.push(t);const o=1===t.O?0:31-Math.clz32(t.O&-t.O);r.push(0,t.G,n(t.offset),o,(t.O>>o)-1)}const s=new Uint32Array(64);for(const t of r)m(V,t,s);const c=p(s);b(t,{v:new Uint8Array(6),J:[{config:V,code:c}]});for(const n of r)M(t,c,V,n);return i}function I(t,n){const o=[[3,0],[6,8],[10,72],[13,1096]];if("rct"===n.type)t.write(2,0),t.t(o,n.Y),t.t([[0,6],[2,0],[4,2],[6,10]],n.Z??6);else if("palette"===n.type)t.write(2,1),t.t(o,n.Y),t.t([[0,1],[0,3],[0,4],[13,1]],n.D),t.t([[8,0],[10,256],[12,1280],[16,5376]],n.F),t.t([[0,0],[8,1],[10,257],[16,1281]],n.H??0),t.write(4,n.G??0);else{if("squeeze"!==n.type)throw new Error("unknown transform "+n.type);{t.write(2,2);const e=n.K??[];t.t([[0,0],[4,1],[6,9],[8,41]],e.length);for(const n of e)t.write(1,n.V?1:0),t.write(1,n.W?1:0),t.t(o,n.Y),t.t([[0,1],[0,2],[0,3],[4,4]],n.D)}}}function C(t,{$:n=!0,tt:o=[]}={}){t.write(1,n?1:0),t.write(1,1),t.t([[0,0],[0,1],[4,2],[8,18]],o.length);for(const n of o)I(t,n)}function T(t,n,o,e=t=>t.context){const r=new Uint8Array(n.length+1);for(const t of n)r[t.context]=e(t)+1;const i=new Uint32Array(2);i[1]=1;const s=[{config:V,code:p(i)}],c=Math.max(...r);for(let t=0;t<c;t++)s.push({config:V,code:p(o[t])});return b(t,{X:K,v:r,J:s}),s}function E(t,o,e,r,i,s){const c=s.G,f=s.offset,l=s.O,a=V,u=H;let h=0;const p=(n,e=a,r=0)=>{t?M(t,o,e,n,r):m(e,n,o,r)},y=()=>{if(h){if(h>=8)p(0),p(h-7-1,u,224);else for(let t=0;t<h;t++)p(0);h=0}},w=(t,o)=>{const r=e[t];let i;1===l?i=r-o-f:(i=Math.round((r-o-f)/l),e[t]=o+f+i*l),0!==i?(y(),p(n(i))):h++};for(let t=0,n=0;t<i;t++)for(let o=0;o<r;o++,n++){let i;if(0===c)i=0;else{const s=o?e[n-1]:t?e[n-r]:0,f=t?e[n-r]:s;if(1===c)i=s;else if(2===c)i=f;else if(3===c)i=(s+f)/2|0;else{const l=s+f-(o&&t?e[n-r-1]:s);if(5===c){const t=s<f?s:f,n=s<f?f:s;i=l<t?t:l>n?n:l}else{if(4!==c)throw new Error("predictor "+c+" is not coded here");i=Math.abs(l-s)<Math.abs(l-f)?s:f}}}w(n,i)}y()}function L(t,n,o,{palette:e=!0}={}){const r=n*o;let i=!0,s=!0;for(let n=0,o=0;o<r&&(255===t[n+3]||(i=!1,s))&&(t[n]===t[n+1]&&t[n]===t[n+2]||(s=!1,i));o++,n+=4);const c=s?1:3,f=!i,l=c+(f?1:0),a=n=>s?f?t[n]+256*t[n+3]:t[n]:t[n]+256*t[n+1]+65536*t[n+2]+(f?16777216*t[n+3]:0),u=new Map;let h=null;for(let t=0,n=0;e&&n<r;n++,t+=4){const n=a(t);if(0!==n&&!u.has(n)&&(u.set(n,1),u.size>=2048))break}if(e&&u.size<2048){const t=[...u.keys()],n=(t,n)=>t>>>8*n&255,o=t=>(.299*n(t,0)+.587*n(t,1)+.114*n(t,2)+.01)*(4===l?n(t,3):1);t.sort((t,n)=>o(t)-o(n));const e=new Map([[0,0]]);t.forEach((t,n)=>e.set(t,n+1)),h={colours:[0,...t],index:e,key:a,byte:n}}return{colour:c,alpha:f,channels:l,palette:h}}function*N(n,o,e,{shape:r=L(n,o,e),colorSpace:i}={}){let s,c;const f=r.palette?[r,{...r,palette:null}]:[r];for(let r=0;r<f.length;r++)try{const c=yield*t(P(n,o,e,f[r],i),r,f.length);(!s||c.length<s.length)&&(s=c)}catch(t){if("JXL_SIZE"!==t.code)throw t;c=t}if(!s)throw c;return s}function*P(t,n,o,e,r){const{channels:l,palette:a}=e,u=c(n,o),h=a?2:l,w=Array.from({length:h},()=>A(F)),d=S(w),g=Array.from({length:h},()=>new Int16Array(65536)),m=(o,e,r,i)=>{for(let s=0;s<i;s++){let i=4*((e+s)*n+o),c=s*r;for(let n=0;n<r;n++,i+=4,c++)if(a)g[0][c]=a.index.get(a.key(i));else if(l>=3){const n=t[i],o=t[i+1],e=t[i+2],r=n-e,s=e+(r>>1),f=o-s;g[0][c]=s+(f>>1),g[1][c]=r,g[2][c]=f,4===l&&(g[3][c]=t[i+3])}else g[0][c]=t[i],2===l&&(g[1][c]=t[i+3])}};let M=null;if(a){const t=a.colours.length;M=new Int16Array(t*l),a.colours.forEach((n,o)=>{for(let e=0;e<l;e++)M[e*t+o]=a.byte(n,e)})}const b=t=>w[a?u.M?1:0:t],U=a?1:l,I=u.i*u.u,L=2*I,N=(t,e)=>{const r=t%u.i*R,i=(t/u.i|0)*R,s=Math.min(R,n-r),c=Math.min(R,o-i);m(r,i,s,c),e(s,c)};let P=0;if(!a){const t=t=>{const n=p(t),o=new B(128);y(o,n);let e=o.nt;for(let o=0;o<t.length;o++)t[o]&&(e+=t[o]*(n.S[o]+Math.max(0,o<224?o-1:o-236)));return e},e=Math.min(n,32),r=Math.min(o,32),i=[F,Q].map(t=>({G:t,ot:w.map(()=>new Uint32Array(257))}));for(const t of[0,.5,1]){m(Math.floor((n-e)*t),Math.floor((o-r)*t),e,r);for(const t of i)for(let n=0;n<l;n++)E(null,t.ot[n],g[n],e,r,A(t.G))}w.forEach((n,o)=>{const e=i.map(n=>t(n.ot[o])),r=e[1]<e[0]?1:0;n.G=i[r].G})}const X=a?[{type:"palette",Y:0,D:l,F:a.colours.length}]:l>=3?[{type:"rct",Y:0,Z:6}]:[],v=w.map(()=>new Uint32Array(257)),z=t=>v[w.indexOf(t)];M&&E(null,z(w[0]),M,a.colours.length,l,w[0]);for(let t=0;t<I;t++)N(t,(t,n)=>{for(let o=0;o<U;o++)E(null,z(b(o)),g[o],t,n,b(o))}),yield++P/L;const J=new B(256);i(J,n,o,e.colour,e.alpha,{colorSpace:r}),s(J,{alpha:e.alpha});const _=new B(4096);_.write(1,1),_.write(1,1);const q=x(_,d),G=T(_,q,v,t=>w.indexOf(t)),O=t=>G[w.indexOf(t)+1].code;C(_,{$:!0,tt:X}),M&&E(_,O(w[0]),M,a.colours.length,l,w[0]);const j=[];if(u.M)N(0,(t,n)=>{for(let o=0;o<U;o++)E(_,O(b(o)),g[o],t,n,b(o))}),yield 1,j.push(_.finish());else{j.push(_.finish());for(let t=0;t<u.p*u.m+1;t++)j.push(new Uint8Array(0));for(let t=0;t<I;t++)N(t,(t,n)=>{const o=new B(t*n*U+64);C(o,{$:!0,tt:[]});for(let e=0;e<U;e++)E(o,O(b(e)),g[e],t,n,b(e));j.push(o.finish())}),yield++P/L}return f(J,j)}function X(t,n,o){const e=t>=n&&n>=o?1:t<=n&&n<=o?-1:0;if(!e)return 0;let r=(4*(t*=e)-3*(o*=e)-(n*=e)+6)/12|0;return r-(1&r)>2*(t-n)&&(r=2*(t-n)+1),r+(1&r)>2*(n-o)&&(r=2*(n-o)),e*r}function v(t,n){const{w:o,h:e,data:r}=t,i=n?o:e,s=i+1>>1,c=i-s,f=n?s:o,l=n?e:s,a=n?c:o,u=n?e:c,h=new Int16Array(f*l),p=new Int16Array(a*u),y=n?1:o;for(let t=0;t<l;t++)for(let e=0;e<f;e++){const s=n?e:t,l=n?t*o+2*e:2*t*o+e;if(s===c){h[t*f+e]=r[l];continue}const u=r[l],w=r[l+y],d=W(u,w),g=s+1<c?W(r[l+2*y],r[l+3*y]):1&i?r[l+2*y]:d;h[t*f+e]=d,p[t*a+e]=u-w-X(s?r[l-y]:d,d,g)}const w=(o,e,r)=>({w:e,h:r,et:t.et+ +n,rt:t.rt+!n,data:o});return[w(h,f,l),w(p,a,u)]}function z(t){const n=[],o=t.length;let e=t[0].w,r=t[0].h;for(o>2&&t[1].w===e&&t[1].h===r&&(n.push({V:!0,W:!1,Y:1,D:2}),n.push({V:!1,W:!1,Y:1,D:2})),!(e>r)&&r>8&&(n.push({V:!1,W:!0,Y:0,D:o}),r=r+1>>1);e>8||r>8;)e>8&&(n.push({V:!0,W:!0,Y:0,D:o}),e=e+1>>1),r>8&&(n.push({V:!1,W:!0,Y:0,D:o}),r=r+1>>1);return n}function*J(t,n,o,{quality:e=90,shape:r=L(t,n,o,{palette:!1}),colorSpace:l}={}){const a=function(t){return t>=100?0:t>=30?.1+.09*(100-t):53/3e3*t*t-1.15*t+25}(e);if(!(a>0))throw new Error("lossy encoding needs a quality below 100");const{colour:u,alpha:h,channels:p}=r,y=n*o,w=Array.from({length:p},()=>new Int16Array(y));for(let n=0,o=0;o<y;o++,n+=4){if(3===u){const e=t[n],r=t[n+1],i=t[n+2],s=e-i,c=i+(s>>1),f=r-c;w[0][o]=c+(f>>1),w[1][o]=s,w[2][o]=f}else w[0][o]=t[n];h&&(w[p-1][o]=t[n+3])}const d=w.map((t,e)=>({w:n,h:o,et:0,rt:0,data:t,component:3===u?e:0===e?0:3})),g=1===n&&o>$&&3===u,m=g?z(d).filter(t=>!t.V):1!==n&&1!==o||3!==u?[]:z(d),M=function(t,n=z(t)){const o=t.map(t=>({...t,level:0,it:!1}));for(const t of n){const n=t.Y+t.D-1,e=t.W?n+1:o.length;for(let r=t.Y;r<=n;r++){const[n,i]=v(o[r],t.V),s=o[r].level+1;o[r]={...n,level:s,it:o[r].it,component:o[r].component},o.splice(e+(r-t.Y),0,{...i,level:s,it:!0,component:o[r].component})}}return o}(d,m.length?m:void 0),b=t=>t.it?0:F,I=t=>function(t,n,o,e){let r=Math.min(16,n+o);if(r>0&&r--,3===t)return 1;const i=.25*function(t){let n=1;for(let o=0;o<24;o++)n=(4*n+t/(n*n*n*n))/5;return t*n}(e),s=1<<r,c=1===t||2===t?.35*i*Math.max(.5,1024/s):.35*i*1.1*(163.84/s);return Math.max(1,Math.floor(c))}(t.component,t.et+(g&&t.component>0&&t.component<3?1:0),t.rt,a),N=[...3===u?[{type:"rct",Y:0,Z:6}]:[],{type:"squeeze",K:m}],P=c(n,o,$),X=P.p*P.m,J=P.i*P.u;let _=0;for(;_<M.length&&M[_].w<=$&&M[_].h<=$;)_++;const q=(t,n,o,e)=>{const r=n>>t.et,i=o>>t.rt;return{x:r,y:i,w:Math.min(t.w,r+(e>>t.et))-r,h:Math.min(t.h,i+(e>>t.rt))-i}},G=[{index:0,k:0,st:[]}];for(let t=0;t<_;t++)G[0].st.push({c:t,rect:{x:0,y:0,w:M[t].w,h:M[t].h}});if(!P.M){for(let t=0;t<X;t++){const n=t%P.p,o=t/P.p|0,e={index:1+t,k:1+X+t,st:[]};for(let t=_;t<M.length;t++){const r=M[t];if(Math.min(r.et,r.rt)<3)continue;const i=q(r,n*tt,o*tt,tt);i.w>0&&i.h>0&&e.st.push({c:t,rect:i})}G.push(e)}for(let t=0;t<J;t++){const n=t%P.i,o=t/P.i|0,e={index:2+X+t,k:1+3*X+17+t,st:[]};for(let t=_;t<M.length;t++){const r=M[t];if(Math.min(r.et,r.rt)>2)continue;const i=q(r,n*$,o*$,$);i.w>0&&i.h>0&&e.st.push({c:t,rect:i})}G.push(e)}}for(const t of G)for(const n of t.st){const t=M[n.c];n.ct=A(b(t),0,I(t)),n.ct.channel=n.c}const O=G.filter(t=>t.st.length),j=1===O.length?S(O[0].st.map(t=>t.ct)):function(t){return U(t,1)}(O.map(t=>({k:t.k,j:S(t.st.map(t=>t.ct))}))),R=new Int16Array(1048576),k=t=>{const n=M[t.c],o=t.rect;if(o.w===n.w&&o.h===n.h)return n.data;for(let t=0;t<o.h;t++)R.set(n.data.subarray((o.y+t)*n.w+o.x,(o.y+t)*n.w+o.x+o.w),t*o.w);return R},Y=[...new Set(G.flatMap(t=>t.st.map(t=>t.c)))].sort((t,n)=>t-n),Z=new Map(Y.map((t,n)=>[t,n])),D=Y.map(()=>new Uint32Array(257)),Q=1+2*G.length;let H=1;yield H/Q;for(const t of G){for(const n of t.st)E(null,D[Z.get(n.c)],k(n),n.rect.w,n.rect.h,n.ct);yield++H/Q}const K=new B(256);i(K,n,o,u,h,{colorSpace:l}),s(K,{alpha:h,shift:n>256||o>256?3:1});const V=Array.from({length:P.M?1:2+X+J},()=>null),W=V[0]=new B(65536);W.write(1,1),W.write(1,1);const nt=T(W,x(W,j),D,t=>Z.get(t.channel));C(W,{$:!0,tt:N});for(const t of G){let n=V[t.index];n||(n=V[t.index]=new B(4096),t.st.length&&C(n,{$:!0,tt:[]}));for(const o of t.st)E(n,nt[Z.get(o.c)+1].code,k(o),o.rect.w,o.rect.h,o.ct);yield++H/Q}return f(K,function(t){return t.map(t=>t?t.finish():new Uint8Array(0))}(V))}function _(t){try{return t()}catch(t){if(t instanceof RangeError&&!t.code)throw nt("JXL_MEMORY","Not enough memory for this picture.");throw t}}function q(t,n,o,e){return function(t){let n;for(;!(n=t.next()).done;);return n.value}(G(t,n,o,e))}function G(n,o,e,r){const{quality:i,colorSpace:s}=function(t={},n=100){if(null===t||"object"!=typeof t)throw nt("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 nt("JXL_INPUT","Quality is a number from 1 to 100.");const o=void 0===t.colorSpace?"srgb":t.colorSpace;if("srgb"!==o&&"display-p3"!==o)throw nt("JXL_INPUT","The colour space is srgb or display-p3.");return{quality:n,colorSpace:o}}(r);return function(t,n,o,e){if(function(t,n,o=O){if(!Number.isInteger(t)||!Number.isInteger(n)||t<1||n<1)throw nt("JXL_INPUT","A picture is at least one pixel wide and high, in whole pixels.");if(t>o.edge||n>o.edge)throw nt("JXL_DIMENSIONS","A picture is at most "+o.edge+" pixels on a side.");if(t*n>o.pixels)throw nt("JXL_DIMENSIONS","A picture is at most "+o.pixels.toLocaleString("en-US")+" pixels.")}(n,o,e),!(t instanceof Uint8Array||t instanceof Uint8ClampedArray))throw nt("JXL_INPUT","Pixels are a Uint8Array or Uint8ClampedArray of RGBA bytes.");if(t.length!==n*o*4)throw nt("JXL_INPUT","Pixels are width * height * 4 bytes: straight (not premultiplied) RGBA, row by row.")}(n,o,e),function(t){const n=function*(){let o,e;for(;!(o=_(()=>t.next(n.hurry))).done;)yield e=o.value;return e<1&&(yield 1),n.bytes=function(t){if(t.length>O.bytes)throw nt("JXL_SIZE","The encoded picture exceeds 16 MiB.");return t}(o.value)}();return n.bytes=null,n.hurry=!1,n}(function*(n,o,e,r,i){const s=L(n,o,e);if(r>=100)return yield*N(n,o,e,{shape:s,colorSpace:i});if(!s.palette)return yield*J(n,o,e,{quality:r,shape:s,colorSpace:i});const c=yield*t(N(n,o,e,{shape:s,colorSpace:i}),0,2);let f;try{f=yield*t(J(n,o,e,{quality:r,shape:s,colorSpace:i}),1,2)}catch(t){if(!(t instanceof RangeError)||t.code)throw t;f=c}return c.length<=f.length?c:f}(n,o,e,i,s))}const O=Object.freeze({bytes:16777216,pixels:24e6,edge:16384}),j=[1];for(let t=1;t<=32;t++)j.push(2*j[t-1]);class B{constructor(t=4096){this.bytes=new Uint8Array(t),this.at=0,this.ft=0,this.pending=0}write(t,n){if(!(n>=0&&n<j[t]))throw new Error("bit write out of range: "+t+" bits, "+n);let o=this.ft+n*(1<<this.pending),e=this.pending+t;this.at+5>=this.bytes.length&&this.grow();const r=this.bytes;for(;e>=8;)r[this.at++]=255&o,o=Math.floor(o/256),e-=8;this.ft=o,this.pending=e}t(t,n){for(let o=0;o<4;o++){const[e,r]=t[o];if(n>=r&&n-r<j[e])return this.write(2,o),void(e&&this.write(e,n-r))}throw new Error("U32 value out of range: "+n)}o(){this.pending&&(this.at+1>=this.bytes.length&&this.grow(),this.bytes[this.at++]=this.ft,this.ft=0,this.pending=0)}get nt(){return 8*this.at+this.pending}grow(t=0){const n=O.bytes+64;if(this.at+t+16>n)throw Object.assign(new Error("The encoded picture exceeds 16 MiB."),{code:"JXL_SIZE"});const o=new Uint8Array(Math.min(n,Math.max(2*this.bytes.length,this.at+t+16)));o.set(this.bytes.subarray(0,this.at)),this.bytes=o}append(t){if(this.at+t.at+8>=this.bytes.length&&this.grow(t.at+8),this.pending)for(let n=0;n<t.at;n++)this.write(8,t.bytes[n]);else this.bytes.set(t.bytes.subarray(0,t.at),this.at),this.at+=t.at;t.pending&&this.write(t.pending,t.ft)}finish(){const t=this.at+(this.pending?1:0),n=this.bytes.slice(0,t);return this.pending&&(n[this.at]=this.ft),n}}const R=256,k=[1,2,3,4,0,5,17,6,16,7,8,9,10,11,12,13,14,15],Y=[2,4,3,2,2,4],Z=[0,7,3,2,1,15],D=[0,0,0],Q=3,F=5,H=w(4),K=Object.freeze({_:224,minLength:7,q:H}),V=w(0),W=(t,n)=>t+n+(t>n?1:0)>>1,$=1024,tt=8192,nt=(t,n)=>Object.assign(new Error(n),{code:t});export{O as LIMITS,q as encode,G as encodeSteps};
|
package/sizes.json
CHANGED
|
@@ -1,108 +1,209 @@
|
|
|
1
1
|
{
|
|
2
2
|
"package": "rapier-jxl",
|
|
3
|
-
"version": "
|
|
4
|
-
"measured": "exact bytes of the staged files; gzip level 9; Brotli quality 11, text mode",
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"measured": "exact bytes of the staged files; gzip level 9; Brotli quality 11, text mode; added is a door bundled with the core less the core alone, or the door alone less the core where it covers the core (effort); every measured bundle loads as a module",
|
|
5
5
|
"node": "v22.22.2",
|
|
6
6
|
"terser": "5.51.2",
|
|
7
7
|
"privateProperties": "^(alphabetSize|lengths|codes|simple|treeSelect|splitToken|msb|lsb|contextMap|histograms|predictor|multiplier|context|splitval|property|offset|left|right|channel|acc|pending|at|bitLength|grow|zeroPadToByte|writeU32|append|finish|write|small|large|totals|contexts|tokens|cost|bucketOf|contextOf|nonZero|zeroDensityOffset|thresholds|numDc|numCtxs|freqs|hshift|vshift|residual|component|inPlace|horizontal|beginC|numC|rctType|nbColors|nbDeltas|useGlobalTree|streamId|pieces|rect|minSymbol|minLength|lengthConfig|params|type|leaf|groupsX|groupsY|dcGroupsX|dcGroupsY|single|config|bits|sym|tree|count|parts|lz77|transforms)$",
|
|
8
8
|
"source": {
|
|
9
|
-
"bytes":
|
|
10
|
-
"gzip":
|
|
9
|
+
"bytes": 136580,
|
|
10
|
+
"gzip": 42707,
|
|
11
11
|
"sha256": {
|
|
12
|
-
"
|
|
13
|
-
"
|
|
12
|
+
"admit.mjs": "52177c34daece7e57aa0e3addf18da9611774ba3830958b054e00cd031d94fbd",
|
|
13
|
+
"bits.mjs": "aee959d6bf46344f38291b461e8c40c225a23b7f4f093b21848432d9223abf3a",
|
|
14
|
+
"effort.mjs": "fefa2a62508ab1969fe8f28e1eda7b829f4fb5d15dd48170077e7a6e43b7d47c",
|
|
15
|
+
"entropy.mjs": "9cb52a9d174d63dc2b694725267c5ab0e8d693f5366e76fa0134f87cc878fe3a",
|
|
16
|
+
"frame.mjs": "139c001fc0e8f666b4b107dff3f135f866a69995305bb87c79de74c2b3cc28f9",
|
|
17
|
+
"index.mjs": "7ac659657a069f75841053a9e18dcd00650006312c6ef2de128a20707ae69589",
|
|
18
|
+
"jfif.mjs": "430c4a7efbf53007c7084fc6da85f4de9a05caad050b17ec9439ab0d17645d5c",
|
|
19
|
+
"jpeg.mjs": "5189e1b68dea7449f3e8b42d1e27e59eaf13132a9510c497546d525c8ae47499",
|
|
20
|
+
"lossless.mjs": "6260af5e685a94360ee7eb2c0f3e6ac94dc931ab0f199a7d5102ba13893f390c",
|
|
21
|
+
"lossy.mjs": "e297311f5f2d304d17ea277fb156d2a575599aecb5d2473cbfbbd1be6b76ff6d",
|
|
14
22
|
"modular.mjs": "9be937ceb8d4eb015a54f7e9f92aeff7be42eeaef94fd13a32530ea5ff204662",
|
|
15
|
-
"
|
|
23
|
+
"photo.mjs": "6728fca4786f9a48404b1fdbd90c2496012ead8a3b9e11bae3f230c01336e339",
|
|
24
|
+
"prefix.mjs": "ed51a5b6a3cf0a98b66fe54b3a1c050122ed312f7c47b136aa9fd5c84ac1a180",
|
|
16
25
|
"squeeze.mjs": "ec01352dcf22b7346c7f0014e17e6ba2e4fe8243c358db77eee5fc830a3217e8",
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
"entropy.mjs": "639028e81f06933ea9ca9100f25f48964e0d39620140a0b0623f4a0e40ff1fa6",
|
|
21
|
-
"vardct.mjs": "f4c3e606cab6d737a498f427b8f45597e7fbbfcd675c81ba14d744ca980f85f8",
|
|
22
|
-
"index.mjs": "1e981e0943f4321a4903048daae0ee2f5a0df1d16e242d4259239d0d53a1fab5",
|
|
23
|
-
"photo.mjs": "3458e73edbc2322bedace0ab52b39f3c5e4e18bea078bb1366f5a7739af45393"
|
|
26
|
+
"vardct.mjs": "ad52f0f95fe1fe843caab986af4535308b4773c33bf52339b0a71d0289e78b2e",
|
|
27
|
+
"weighted.mjs": "db14c4ec83ef9389436f7d4e75a46b7424d18b371946ce96cf0b68d49f32c8c2",
|
|
28
|
+
"writer.mjs": "f8ec54c0d79cdf148f6be797d72f375cf9635cb00097897bdf1d570457d3fcc9"
|
|
24
29
|
}
|
|
25
30
|
},
|
|
26
|
-
"
|
|
31
|
+
"doors": [
|
|
27
32
|
{
|
|
33
|
+
"entry": ".",
|
|
34
|
+
"module": "index",
|
|
28
35
|
"file": "rapier-jxl.min.mjs",
|
|
29
|
-
"entry": "./min",
|
|
30
36
|
"exports": [
|
|
31
|
-
"encode",
|
|
32
|
-
"encodeLosslessRGBA",
|
|
33
|
-
"encodeLossyRGBA",
|
|
34
|
-
"transcode",
|
|
35
37
|
"LIMITS",
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"transcodeJPEG",
|
|
39
|
-
"parseJPEG",
|
|
40
|
-
"inspectPixels"
|
|
38
|
+
"encode",
|
|
39
|
+
"encodeSteps"
|
|
41
40
|
],
|
|
42
|
-
"
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"
|
|
50
|
-
"
|
|
51
|
-
|
|
52
|
-
},
|
|
53
|
-
{
|
|
54
|
-
"file": "lossless.min.mjs",
|
|
55
|
-
"entry": "./lossless",
|
|
56
|
-
"exports": [
|
|
57
|
-
"encodeLosslessRGBA",
|
|
58
|
-
"LIMITS"
|
|
41
|
+
"modules": [
|
|
42
|
+
"bits",
|
|
43
|
+
"frame",
|
|
44
|
+
"prefix",
|
|
45
|
+
"modular",
|
|
46
|
+
"lossless",
|
|
47
|
+
"squeeze",
|
|
48
|
+
"lossy",
|
|
49
|
+
"admit",
|
|
50
|
+
"index"
|
|
59
51
|
],
|
|
60
|
-
"
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
"
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
52
|
+
"alone": {
|
|
53
|
+
"bytes": 19495,
|
|
54
|
+
"gzip": 8475,
|
|
55
|
+
"brotli": 7494,
|
|
56
|
+
"sha256": "50ea1947a6f9e2b0bb4a557d859a29c44031dea2726127e62b29e6c809748fe2",
|
|
57
|
+
"compress": {
|
|
58
|
+
"passes": 2,
|
|
59
|
+
"hoist_funs": true,
|
|
60
|
+
"hoist_vars": true,
|
|
61
|
+
"module": true
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
"added": {
|
|
65
|
+
"bytes": 0,
|
|
66
|
+
"gzip": 0,
|
|
67
|
+
"brotli": 0
|
|
68
|
+
},
|
|
69
|
+
"provedByteCases": 94
|
|
70
70
|
},
|
|
71
71
|
{
|
|
72
|
-
"file": "jpeg.min.mjs",
|
|
73
72
|
"entry": "./jpeg",
|
|
73
|
+
"module": "jpeg",
|
|
74
|
+
"file": "jpeg.min.mjs",
|
|
74
75
|
"exports": [
|
|
76
|
+
"LIMITS",
|
|
75
77
|
"transcode",
|
|
76
|
-
"
|
|
78
|
+
"transcodeSteps"
|
|
79
|
+
],
|
|
80
|
+
"modules": [
|
|
81
|
+
"bits",
|
|
82
|
+
"jfif",
|
|
83
|
+
"frame",
|
|
84
|
+
"prefix",
|
|
85
|
+
"modular",
|
|
86
|
+
"entropy",
|
|
87
|
+
"vardct",
|
|
88
|
+
"admit",
|
|
89
|
+
"jpeg"
|
|
77
90
|
],
|
|
78
|
-
"
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
"
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
91
|
+
"alone": {
|
|
92
|
+
"bytes": 29592,
|
|
93
|
+
"gzip": 12533,
|
|
94
|
+
"brotli": 11072,
|
|
95
|
+
"sha256": "4b408d463dbb6bf32f98143b4f2b0e4aa0b2f060ba7778795cedc9d2f26355e5",
|
|
96
|
+
"compress": {
|
|
97
|
+
"passes": 2,
|
|
98
|
+
"inline": 1,
|
|
99
|
+
"module": true
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
"added": {
|
|
103
|
+
"bytes": 19242,
|
|
104
|
+
"gzip": 7673,
|
|
105
|
+
"brotli": 6802
|
|
106
|
+
},
|
|
107
|
+
"provedByteCases": 9
|
|
88
108
|
},
|
|
89
109
|
{
|
|
90
|
-
"file": "photo.min.mjs",
|
|
91
110
|
"entry": "./photo",
|
|
111
|
+
"module": "photo",
|
|
112
|
+
"file": "photo.min.mjs",
|
|
92
113
|
"exports": [
|
|
93
|
-
"
|
|
94
|
-
"
|
|
114
|
+
"LIMITS",
|
|
115
|
+
"encodePhoto",
|
|
116
|
+
"encodePhotoSteps"
|
|
117
|
+
],
|
|
118
|
+
"modules": [
|
|
119
|
+
"bits",
|
|
120
|
+
"frame",
|
|
121
|
+
"prefix",
|
|
122
|
+
"modular",
|
|
123
|
+
"entropy",
|
|
124
|
+
"jfif",
|
|
125
|
+
"vardct",
|
|
126
|
+
"lossless",
|
|
127
|
+
"admit",
|
|
128
|
+
"photo"
|
|
129
|
+
],
|
|
130
|
+
"alone": {
|
|
131
|
+
"bytes": 25646,
|
|
132
|
+
"gzip": 10804,
|
|
133
|
+
"brotli": 9537,
|
|
134
|
+
"sha256": "e7cdd24b5e9c1e718dad54c4b40ccc5a2088e7ae8144a457352fdc00b2e22c82",
|
|
135
|
+
"compress": {
|
|
136
|
+
"passes": 2,
|
|
137
|
+
"hoist_funs": true,
|
|
138
|
+
"hoist_vars": true,
|
|
139
|
+
"module": true
|
|
140
|
+
}
|
|
141
|
+
},
|
|
142
|
+
"added": {
|
|
143
|
+
"bytes": 11055,
|
|
144
|
+
"gzip": 4242,
|
|
145
|
+
"brotli": 3762
|
|
146
|
+
},
|
|
147
|
+
"provedByteCases": 36
|
|
148
|
+
},
|
|
149
|
+
{
|
|
150
|
+
"entry": "./effort",
|
|
151
|
+
"module": "effort",
|
|
152
|
+
"file": "effort.min.mjs",
|
|
153
|
+
"exports": [
|
|
154
|
+
"LIMITS",
|
|
155
|
+
"encode",
|
|
156
|
+
"encodeSteps"
|
|
157
|
+
],
|
|
158
|
+
"modules": [
|
|
159
|
+
"bits",
|
|
160
|
+
"frame",
|
|
161
|
+
"prefix",
|
|
162
|
+
"modular",
|
|
163
|
+
"lossless",
|
|
164
|
+
"squeeze",
|
|
165
|
+
"lossy",
|
|
166
|
+
"weighted",
|
|
167
|
+
"admit",
|
|
168
|
+
"effort"
|
|
95
169
|
],
|
|
96
|
-
"
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
"
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
170
|
+
"alone": {
|
|
171
|
+
"bytes": 25229,
|
|
172
|
+
"gzip": 10606,
|
|
173
|
+
"brotli": 9351,
|
|
174
|
+
"sha256": "895c80728398f0f98e9154f9788cb9fbaa46a00d26d5518e3b8186bfe8bec7c0",
|
|
175
|
+
"compress": {
|
|
176
|
+
"passes": 2,
|
|
177
|
+
"hoist_funs": true,
|
|
178
|
+
"hoist_vars": true,
|
|
179
|
+
"module": true
|
|
180
|
+
}
|
|
181
|
+
},
|
|
182
|
+
"added": {
|
|
183
|
+
"bytes": 5734,
|
|
184
|
+
"gzip": 2131,
|
|
185
|
+
"brotli": 1857
|
|
186
|
+
},
|
|
187
|
+
"provedByteCases": 94
|
|
106
188
|
}
|
|
107
|
-
]
|
|
189
|
+
],
|
|
190
|
+
"all": {
|
|
191
|
+
"exports": [
|
|
192
|
+
"LIMITS",
|
|
193
|
+
"encode",
|
|
194
|
+
"encodePhoto",
|
|
195
|
+
"encodePhotoSteps",
|
|
196
|
+
"encodeSteps",
|
|
197
|
+
"transcode",
|
|
198
|
+
"transcodeSteps"
|
|
199
|
+
],
|
|
200
|
+
"doors": [
|
|
201
|
+
"./effort",
|
|
202
|
+
"./jpeg",
|
|
203
|
+
"./photo"
|
|
204
|
+
],
|
|
205
|
+
"bytes": 47499,
|
|
206
|
+
"gzip": 19228,
|
|
207
|
+
"brotli": 16977
|
|
208
|
+
}
|
|
108
209
|
}
|
package/vardct.mjs
CHANGED
|
@@ -3,12 +3,12 @@
|
|
|
3
3
|
// a JPEG: its quantisation tables as raw dequantisation matrices, its DC values as the DC image, its colour
|
|
4
4
|
// (YCbCr, or RGB) and chroma subsampling kept, every block an 8x8 DCT, no filters, no smoothing, no chroma from
|
|
5
5
|
// luma. Only the entropy coding changes: JPEG XL's contexts over the coefficients, with prefix codes.
|
|
6
|
-
import {BitWriter, float16Bits, packSigned, ceilLog2} from './bits.mjs';
|
|
6
|
+
import {BitWriter, float16Bits, packSigned, ceilLog2, complete} from './bits.mjs';
|
|
7
7
|
import {writeImageHeader, writeFrameHeaderEnd, finishSections, assembleCodestream, GROUP_DIM} from './frame.mjs';
|
|
8
8
|
import {ZERO_PREDICTOR, GRADIENT_PREDICTOR, ALPHABET, leaf, channelTree, writeTree, writeModularHeader, writeChannelHistograms, codeChannel} from './modular.mjs';
|
|
9
9
|
import {writeContextMap, writeHistograms} from './prefix.mjs';
|
|
10
10
|
import {TokenCounts, buildTokenCoding} from './entropy.mjs';
|
|
11
|
-
import {parseJPEG, jpegError, ZIGZAG} from './
|
|
11
|
+
import {parseJPEG, jpegError, ZIGZAG} from './jfif.mjs';
|
|
12
12
|
|
|
13
13
|
const NONZERO_BUCKETS = 37, ZERO_DENSITY_CONTEXTS = 458, ORDERS = 13;
|
|
14
14
|
const COEFF_FREQ_CONTEXT = [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,
|
|
@@ -69,9 +69,17 @@ export function transcodeJPEG(bytes, jpeg = parseJPEG(bytes)) { return encodeVar
|
|
|
69
69
|
|
|
70
70
|
// Shared coefficient writer. Pixel encoding supplies the same natural-order DCT8 planes as the JPEG reader;
|
|
71
71
|
// quantScale lets its raw integer matrices carry fractional steps without a second entropy/frame writer.
|
|
72
|
-
export function encodeVarDCT(jpeg) {
|
|
72
|
+
export function encodeVarDCT(jpeg) { return complete(varDCTSteps(jpeg)); }
|
|
73
|
+
|
|
74
|
+
// The same as steps: a group of each counting pass, a DC group, an AC group per step.
|
|
75
|
+
export function* varDCTSteps(jpeg) {
|
|
73
76
|
const {width, height, components, alpha} = jpeg, grey = components.length === 1;
|
|
74
77
|
const quantScale = jpeg.quantScale || 1;
|
|
78
|
+
// The raw tables' denominator, 1 / (8 x 255 x quantScale), is a normal binary16: where it would be subnormal (the
|
|
79
|
+
// photo door's), it is doubled until it is not and every block's quantisation field doubled with it, so the decoder's
|
|
80
|
+
// dequantised coefficients are the same. A decoder that reads a subnormal wrong (jxl-rs 0.7.4 halves them) reads these.
|
|
81
|
+
let field = 1;
|
|
82
|
+
while (field / (8 * 255 * quantScale) < 1 / 16384) field *= 2;
|
|
75
83
|
const ycbcr = jpeg.ycbcr || grey;
|
|
76
84
|
const map = grey ? [0, 0, 0] : ycbcr ? [1, 0, 2] : [0, 1, 2]; // frame channel (X, Y, B) to JPEG component
|
|
77
85
|
const comps = map.map(i => components[i]);
|
|
@@ -105,7 +113,7 @@ export function encodeVarDCT(jpeg) {
|
|
|
105
113
|
let contexts = blockContexts(lumaDc, wanted, grey);
|
|
106
114
|
|
|
107
115
|
const header = new BitWriter(256);
|
|
108
|
-
writeImageHeader(header, width, height, grey ? 1 : 3, !!alpha, {orientation: jpeg.orientation});
|
|
116
|
+
writeImageHeader(header, width, height, grey ? 1 : 3, !!alpha, {orientation: jpeg.orientation, colorSpace: jpeg.colorSpace});
|
|
109
117
|
// The frame header: VarDCT, adaptive DC smoothing skipped, the colour and subsampling of the JPEG.
|
|
110
118
|
header.write(1, 0); header.write(2, 0); header.write(1, 0);
|
|
111
119
|
header.write(2, 2); header.write(8, 128 - 17); // flags: kSkipAdaptiveDCSmoothing
|
|
@@ -140,10 +148,17 @@ export function encodeVarDCT(jpeg) {
|
|
|
140
148
|
}
|
|
141
149
|
};
|
|
142
150
|
// Bucketed contexts win on photographs and lose on small or flat pictures: both are counted and the cheaper kept.
|
|
143
|
-
const
|
|
144
|
-
let
|
|
151
|
+
const total = (wanted > 0 ? 3 : 2) * numGroups + numDcGroups;
|
|
152
|
+
let done = 0;
|
|
153
|
+
const countWith = function* (chosen) {
|
|
154
|
+
contexts = chosen;
|
|
155
|
+
const counts = new TokenCounts(chosen.contexts);
|
|
156
|
+
for (let g = 0; g < numGroups; g++) { tokens(g, (ctx, value) => counts.add(ctx, value)); yield ++done / total; }
|
|
157
|
+
return buildTokenCoding(counts);
|
|
158
|
+
};
|
|
159
|
+
let coding = yield* countWith(contexts);
|
|
145
160
|
if (wanted > 0) {
|
|
146
|
-
const bucketed = contexts, plain = blockContexts(lumaDc, 0, grey), plainCoding = countWith(plain);
|
|
161
|
+
const bucketed = contexts, plain = blockContexts(lumaDc, 0, grey), plainCoding = yield* countWith(plain);
|
|
147
162
|
if (plainCoding.bits <= coding.bits) coding = plainCoding; else contexts = bucketed;
|
|
148
163
|
}
|
|
149
164
|
|
|
@@ -197,17 +212,21 @@ export function encodeVarDCT(jpeg) {
|
|
|
197
212
|
const count = rw * rh, bits = ceilLog2(count);
|
|
198
213
|
if (bits) w.write(bits, count - 1);
|
|
199
214
|
const cw = (rw + 7) >> 3, ch = (rh + 7) >> 3;
|
|
215
|
+
// Every block's strategy (DCT8, row 0) and quantisation field less one (row 1); a field above one is a constant row,
|
|
216
|
+
// which the gradient predictor codes as one residual.
|
|
217
|
+
const blocks = new Int32Array(count * 2).fill(field - 1, count);
|
|
200
218
|
writeModularStream(w, [
|
|
201
219
|
{w: cw, h: ch, data: new Int32Array(cw * ch)}, {w: cw, h: ch, data: new Int32Array(cw * ch)},
|
|
202
|
-
{w: count, h: 2, data:
|
|
203
|
-
], ZERO_PREDICTOR);
|
|
220
|
+
{w: count, h: 2, data: blocks}, {w: rw, h: rh, data: new Int32Array(count)},
|
|
221
|
+
], field > 1 ? GRADIENT_PREDICTOR : ZERO_PREDICTOR);
|
|
222
|
+
yield ++done / total;
|
|
204
223
|
}
|
|
205
224
|
|
|
206
225
|
// AC global: the JPEG's quantisation tables as the raw DCT8 matrices (transposed into the frame's layout), the
|
|
207
226
|
// other kinds from the library, one histogram set, the natural coefficient order, the AC histograms.
|
|
208
227
|
const ac = section(1 + numDcGroups);
|
|
209
228
|
ac.write(1, 0);
|
|
210
|
-
ac.write(3, 7); ac.write(16, float16Bits(
|
|
229
|
+
ac.write(3, 7); ac.write(16, float16Bits(field / (8 * 255 * quantScale)));
|
|
211
230
|
writeModularStream(ac, [0, 1, 2].map(c => {
|
|
212
231
|
const data = new Int32Array(64), quant = comps[c].quant;
|
|
213
232
|
for (let y = 0; y < 8; y++) for (let x = 0; x < 8; x++) data[8 * x + y] = quant[8 * y + x];
|
|
@@ -222,6 +241,7 @@ export function encodeVarDCT(jpeg) {
|
|
|
222
241
|
const w = section(2 + numDcGroups + g);
|
|
223
242
|
tokens(g, (ctx, value) => coding.write(w, ctx, value));
|
|
224
243
|
if (alpha && !single) writeModularStream(w, [alphaPlane(g % groupsX, Math.floor(g / groupsX))], GRADIENT_PREDICTOR);
|
|
244
|
+
yield ++done / total;
|
|
225
245
|
}
|
|
226
246
|
return assembleCodestream(header, finishSections(writers));
|
|
227
247
|
}
|
package/weighted.mjs
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// Rapier's JPEG XL encoder: the self-correcting (weighted) predictor. MIT (LICENSE).
|
|
2
|
+
// The specification's modular predictor 6 under the default header every Rapier stream writes (p1 16, p2 10, p3 7, 7,
|
|
3
|
+
// 7, 0, 0; weights 13, 12, 12, 12): four sub-predictions in eighths of a level, each weighted by its error on the
|
|
4
|
+
// pixels above and to the left, clamped to the neighbours when their errors disagree in sign. The decoder keeps the
|
|
5
|
+
// same state for each channel of each group, so the encoder runs it over the plane it codes, and with it the
|
|
6
|
+
// property it offers a tree (15: of the errors to the west, north, north-west and north-east, the one largest in
|
|
7
|
+
// size). Integer arithmetic only: every engine predicts alike.
|
|
8
|
+
import {packSigned} from './bits.mjs';
|
|
9
|
+
import {countToken, writeHybrid} from './prefix.mjs';
|
|
10
|
+
import {LZ77, RESIDUAL_CONFIG} from './modular.mjs';
|
|
11
|
+
|
|
12
|
+
export const WEIGHTED_PREDICTOR = 6, WEIGHTED_PROPERTY = 15;
|
|
13
|
+
// libjxl's cut points on that property for its fixed weighted tree: 34 contexts, from which a coder merges neighbours.
|
|
14
|
+
export const WEIGHTED_CUTS = [-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,
|
|
15
|
+
63, 95, 127, 191, 255, 392, 500];
|
|
16
|
+
// The context of a property value: how many cut points lie below it.
|
|
17
|
+
const BUCKET = Int8Array.from({length: 1003}, (_, i) => WEIGHTED_CUTS.filter(cut => cut < i - 501).length);
|
|
18
|
+
const DIVISORS = Int32Array.from({length: 64}, (_, i) => Math.floor(16777216 / (i + 1)));
|
|
19
|
+
const errorWeight = (sum, most) => { const shift = Math.max(0, 26 - Math.clz32(sum + 1)); return 4 + ((most * DIVISORS[sum >> shift]) >> shift); };
|
|
20
|
+
const SINGLE = new Int32Array(WEIGHTED_CUTS.length + 1);
|
|
21
|
+
|
|
22
|
+
// Codes one channel plane with the weighted predictor, as codeChannel codes the others (a residual per pixel, eight
|
|
23
|
+
// or more zero residuals as one zero and an LZ77 copy), each token through the target of its pixel's context:
|
|
24
|
+
// `contextOf[k]` is the target of the k-th cut interval (all one target when absent). Counting when `w` is null
|
|
25
|
+
// (targets are histograms), writing otherwise (targets are prefix codes). Exact planes only: the offset applies, the
|
|
26
|
+
// multiplier is one.
|
|
27
|
+
export function codeWeighted(w, targets, plane, width, height, offset = 0, contextOf = SINGLE) {
|
|
28
|
+
const stride = width + 2;
|
|
29
|
+
const errors0 = new Int32Array(2 * stride), errors1 = new Int32Array(2 * stride), errors2 = new Int32Array(2 * stride), errors3 = new Int32Array(2 * stride);
|
|
30
|
+
const error = new Int32Array(2 * stride), runContexts = new Int32Array(8);
|
|
31
|
+
let run = 0;
|
|
32
|
+
const emit = (context, value, config = RESIDUAL_CONFIG, base = 0) => { if (w) writeHybrid(w, targets[context], config, value, base); else countToken(config, value, targets[context], base); };
|
|
33
|
+
// A run's first zero goes through its own pixel's context and the copy's length through the next pixel's, where the
|
|
34
|
+
// decoder reads them.
|
|
35
|
+
const flush = () => {
|
|
36
|
+
if (!run) return;
|
|
37
|
+
if (run > LZ77.minLength) { emit(runContexts[0], 0); emit(runContexts[1], run - LZ77.minLength - 1, LZ77.lengthConfig, LZ77.minSymbol); }
|
|
38
|
+
else for (let i = 0; i < run; i++) emit(runContexts[i], 0);
|
|
39
|
+
run = 0;
|
|
40
|
+
};
|
|
41
|
+
for (let y = 0, index = 0; y < height; y++) {
|
|
42
|
+
const cur = y & 1 ? 0 : stride, prev = y & 1 ? stride : 0;
|
|
43
|
+
for (let x = 0; x < width; x++, index++) {
|
|
44
|
+
const left = x ? plane[index - 1] : y ? plane[index - width] : 0, top = y ? plane[index - width] : left;
|
|
45
|
+
const topleft = x && y ? plane[index - width - 1] : left, topright = x + 1 < width && y ? plane[index - width + 1] : top;
|
|
46
|
+
const toptop = y > 1 ? plane[index - 2 * width] : top;
|
|
47
|
+
const n = prev + x, ne = x < width - 1 ? n + 1 : n, nw = x ? n - 1 : n;
|
|
48
|
+
let w0 = errorWeight(errors0[n] + errors0[ne] + errors0[nw], 13), w1 = errorWeight(errors1[n] + errors1[ne] + errors1[nw], 12);
|
|
49
|
+
let w2 = errorWeight(errors2[n] + errors2[ne] + errors2[nw], 12), w3 = errorWeight(errors3[n] + errors3[ne] + errors3[nw], 12);
|
|
50
|
+
const N = top * 8, W = left * 8, NE = topright * 8, NW = topleft * 8, NN = toptop * 8;
|
|
51
|
+
const teW = x ? error[cur + x - 1] : 0, teN = error[n], teNW = error[nw], teNE = error[ne], sumWN = teN + teW;
|
|
52
|
+
let most = teW;
|
|
53
|
+
if (Math.abs(teN) > Math.abs(most)) most = teN;
|
|
54
|
+
if (Math.abs(teNW) > Math.abs(most)) most = teNW;
|
|
55
|
+
if (Math.abs(teNE) > Math.abs(most)) most = teNE;
|
|
56
|
+
const context = contextOf[BUCKET[most < -501 ? 0 : most > 501 ? 1002 : most + 501]];
|
|
57
|
+
const p0 = W + NE - N, p1 = N - (((sumWN + teNE) * 16) >> 5), p2 = W - (((sumWN + teNW) * 10) >> 5);
|
|
58
|
+
const p3 = N - ((teNW * 7 + teN * 7 + teNE * 7 + (NN - N) * 0 + (NW - W) * 0) >> 5);
|
|
59
|
+
// The weighted average: the weights scaled to sum between 16 and 64, then a division by table.
|
|
60
|
+
const shift = 27 - Math.clz32(w0 + w1 + w2 + w3);
|
|
61
|
+
w0 >>= shift; w1 >>= shift; w2 >>= shift; w3 >>= shift;
|
|
62
|
+
const sum = w0 + w1 + w2 + w3;
|
|
63
|
+
let pred = Math.floor(((sum >> 1) - 1 + p0 * w0 + p1 * w1 + p2 * w2 + p3 * w3) * DIVISORS[sum - 1] / 16777216);
|
|
64
|
+
if (((teN ^ teW) | (teN ^ teNW)) <= 0) {
|
|
65
|
+
const lo = Math.min(W, NE, N), hi = Math.max(W, NE, N);
|
|
66
|
+
pred = pred < lo ? lo : pred > hi ? hi : pred;
|
|
67
|
+
}
|
|
68
|
+
const value = plane[index], r = value - ((pred + 3) >> 3) - offset;
|
|
69
|
+
if (r === 0) { if (run < 8) runContexts[run] = context; run++; }
|
|
70
|
+
else { flush(); emit(context, packSigned(r)); }
|
|
71
|
+
// The state the decoder keeps: the prediction's error, and each sub-prediction's, added into the next row's view.
|
|
72
|
+
const v = value * 8, e0 = (Math.abs(p0 - v) + 3) >> 3, e1 = (Math.abs(p1 - v) + 3) >> 3, e2 = (Math.abs(p2 - v) + 3) >> 3, e3 = (Math.abs(p3 - v) + 3) >> 3;
|
|
73
|
+
error[cur + x] = pred - v;
|
|
74
|
+
errors0[cur + x] = e0; errors1[cur + x] = e1; errors2[cur + x] = e2; errors3[cur + x] = e3;
|
|
75
|
+
errors0[prev + x + 1] += e0; errors1[prev + x + 1] += e1; errors2[prev + x + 1] += e2; errors3[prev + x + 1] += e3;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
flush();
|
|
79
|
+
}
|
package/writer.mjs
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
// Rapier JXL's layers, for the author of a module: the JPEG XL specification's structures (ISO/IEC 18181-1), written.
|
|
3
|
+
// A module imports from here alone, composes its headers and frames from these parts, never changes a layer's object
|
|
4
|
+
// or default, and refuses with the five codes. This surface changes only with the package's major version; what lies
|
|
5
|
+
// behind it may change in any release. Readable modules only: the one-file builds rename these parts' properties.
|
|
6
|
+
export {LIMITS, PHOTO_LIMITS, JPEG_LIMITS, BitWriter, packSigned, floorLog2, ceilLog2, float16Bits, complete, part} from './bits.mjs';
|
|
7
|
+
export {codeLengths, canonicalCodes, buildCode, writePrefixCode, uintConfig, writeUintConfig, hybridToken, countToken, writeHybrid, writeContextMap, writeHistograms} from './prefix.mjs';
|
|
8
|
+
export {GROUP_DIM, DC_GROUP_DIM, writeImageHeader, writeModularFrameHeader, writeFrameHeaderEnd, writeTOC, groupLayout, finishSections, assembleCodestream} from './frame.mjs';
|
|
9
|
+
export {PREDICTOR, LZ77, RESIDUAL_CONFIG, ALPHABET, leaf, split, channelTree, streamTree, writeTree, writeTransform, writeModularHeader, writeChannelHistograms, codeChannel} from './modular.mjs';
|
|
10
|
+
export {WEIGHTED_PREDICTOR, WEIGHTED_PROPERTY, WEIGHTED_CUTS, codeWeighted} from './weighted.mjs';
|
|
11
|
+
export {forwardSqueeze, defaultSqueezeParams} from './squeeze.mjs';
|
|
12
|
+
export {inspectPixels, encodeLossless, losslessSteps} from './lossless.mjs';
|
|
13
|
+
export {distanceFromQuality, quantiserFor, encodeLossy, lossySteps} from './lossy.mjs';
|
|
14
|
+
export {TokenCounts, buildTokenCoding} from './entropy.mjs';
|
|
15
|
+
export {encodeVarDCT, varDCTSteps, transcodeJPEG} from './vardct.mjs';
|
|
16
|
+
export {parseJPEG, profileSpace, ZIGZAG} from './jfif.mjs';
|
|
17
|
+
export {fault, admitOptions, admitSize, admitPixels, answer, guard, job} from './admit.mjs';
|
package/lossless.d.mts
DELETED