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/lossless.min.mjs
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
/*! Rapier JXL 1.1.21 | 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){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,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),t:n,l:null,r:null})).sort((t,n)=>t.w-n.w||t.t-n.t),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,t:-1,l:t,r:n})}const h=[[l(),0]];let a=0;for(;h.length;){const[t,n]=h.pop();t.t>=0?(o[t.t]=n,n>a&&(a=n)):h.push([t.l,n+1],[t.r,n+1])}if(a<=n)return o}}function r(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 i(t,n){let o=0;for(let e=0;e<n;e++)o=o<<1|1&t,t>>=1;return o}function s(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,r=n[o-1]+1||1,i=o<=4?new Uint8Array(r):e(t,15).subarray(0,r);let c=0;if(o<=4){n.sort((n,o)=>t[o]-t[n]||n-o),c=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]:c?[1,2,3,3]:[2,2,2,2];n.forEach((t,n)=>{i[t]=e[n]}),n.sort((t,n)=>i[t]-i[n]||t-n)}return{o:r,i:i,h:s(i),u:o&&o<=4?n:null,p:c}}function f(t,o){if(o.o<=1)return;if(o.u){const e=n(o.o-1)+1;t.write(2,1),t.write(2,o.u.length-1);for(const n of o.u)t.write(e,n);return void(4===o.u.length&&t.write(1,o.p))}const i=o.i,c=[],f=[];let l=i.length-1;for(;l>0&&!i[l];)l--;let h=8;for(let t=0;t<=l;){const n=i[t];let o=1;for(;t+o<=l&&i[t+o]===n;)o++;let e=o;if(n&&n!==h&&(c.push(n),f.push(0),e--,h=n),e<3)for(let t=0;t<e;t++)c.push(n),f.push(0);else r(c,f,n?16:17,e-3,n?3:7,n?2:3);t+=o}const a=new Uint32Array(18);for(const t of c)a[t]++;let u=0;for(const t of a)t&&u++;1===u&&(a[c[0]?0:1]=a[c[0]]);const w=e(a,5),p=s(w);let y=18;for(;y>0&&!w[S[y-1]];)y--;t.write(2,0);for(let n=0;n<y;n++){const o=w[S[n]];t.write(T[o],X[o])}for(let n=0;n<c.length;n++){const o=c[n];t.write(w[o],p[o]),16===o?t.write(2,f[n]):17===o&&t.write(3,f[n])}}function l(t,n=0,o=0){return{split:t,M:n,A:o,m: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 a(t,o,e){if(o<t.m)return e[0]=o,e[1]=0,void(e[2]=0);const r=n(o),i=o-(1<<r),s=r-t.M-t.A;e[0]=t.m+(r-t.split<<t.M+t.A|i>>r-t.M<<t.A|i&(1<<t.A)-1),e[1]=s,e[2]=o>>t.A&(1<<s)-1}function u(t,n,o,e=0){a(t,n,N),o[e+N[0]]++}function w(t,n,o,e,r=0){a(o,e,N);const i=r+N[0];t.write(n.i[i],n.h[i]),N[1]&&t.write(N[1],N[2])}function p(t,{U:e=null,I:r,L:i}){t.write(1,e?1:0),e&&(t.S([[0,224],[0,512],[0,4096],[15,8]],e.T),t.S([[0,3],[0,4],[2,5],[8,9]],e.minLength),h(t,e.X,8)),r.length>1&&function(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 i=l(8),s=l(4),f=n.length>=64,h=new Uint32Array(f?257:e+1),a=[];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)a.push([n[t],o-1]),h[n[t]]++,u(s,o-1-7,h,224),t+=o;else{for(let e=0;e<o;e++)a.push([n[t],0]),h[n[t]]++;t+=o}}const y=c(h);if(!f){p(t,{I:new Uint8Array(1),L:[{config:i,code:y}]});for(const[n]of a)w(t,y,i,n);return}const g=new Uint32Array(1);g[0]=1,p(t,{U:{T:224,minLength:7,X:s},I:new Uint8Array([1,0]),L:[{config:l(0),code:c(g)},{config:i,code:y}]});for(const[n,o]of a)w(t,y,i,n),o&&w(t,y,s,o-7,224)}(t,r),t.write(1,1);for(const n of i)h(t,n.config,15);for(const o of i){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 i)f(t,n.code)}function y(t,n=0,o=1){return{N:t,offset:n,J:o,context:-1}}function g(t,n){const o=[[3,0],[6,8],[10,72],[13,1096]];if("rct"===n.type)t.write(2,0),t.S(o,n._),t.S([[0,6],[2,0],[4,2],[6,10]],n.C??6);else if("palette"===n.type)t.write(2,1),t.S(o,n._),t.S([[0,1],[0,3],[0,4],[13,1]],n.G),t.S([[8,0],[10,256],[12,1280],[16,5376]],n.O),t.S([[0,0],[8,1],[10,257],[16,1281]],n.P??0),t.write(4,n.N??0);else{if("squeeze"!==n.type)throw new Error("unknown transform "+n.type);{t.write(2,2);const e=n.v??[];t.S([[0,0],[4,1],[6,9],[8,41]],e.length);for(const n of e)t.write(1,n.B?1:0),t.write(1,n.j?1:0),t.S(o,n._),t.S([[0,1],[0,2],[0,3],[4,4]],n.G)}}}function M(t,{k:n=!0,R:o=[]}={}){t.write(1,n?1:0),t.write(1,1),t.S([[0,0],[0,1],[4,2],[8,18]],o.length);for(const n of o)g(t,n)}function d(n,o,e,r,i,s){const c=s.N,f=s.offset,l=s.J,h=O,a=C;let p=0;const y=(t,e=h,r=0)=>{n?w(n,o,e,t,r):u(e,t,o,r)},g=()=>{if(p){if(p>=8)y(0),y(p-7-1,a,224);else for(let t=0;t<p;t++)y(0);p=0}},M=(n,o)=>{const r=e[n];let i;1===l?i=r-o-f:(i=Math.round((r-o-f)/l),e[n]=o+f+i*l),0!==i?(g(),y(t(i))):p++};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}}}M(n,i)}g()}function A(t,n){t.S([[9,1],[13,1],[18,1],[30,1]],n)}function m(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),h=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),a=new Map;let u=null;for(let t=0,n=0;e&&n<r;n++,t+=4){const n=h(t);if(0!==n&&!a.has(n)&&(a.set(n,1),a.size>=2048))break}if(e&&a.size<2048){const t=[...a.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===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)),u={colours:[0,...t],index:e,key:h,byte:n}}return{colour:c,alpha:f,channels:l,palette:u}}function U(n,o,e,r){const{channels:i,palette:s}=r,l=function(t,n){const o=Math.ceil(t/z),e=Math.ceil(n/z);return{Y:o,Z:e,q:Math.ceil(t/2048),D:Math.ceil(n/2048),F:1===o&&1===e}}(o,e),h=s?2:i,a=Array.from({length:h},()=>y(_)),g=function(t){return function(t,n){const o=(e,r)=>{if(e===r)return n?t[e].H:t[e];const i=e+r>>1;return function(t,n,o,e){return{K:t,V:n,left:o,right:e}}(n,n?t[i].W:i,o(i+1,r),o(e,i))};return o(0,t.length-1)}(t,0)}(a),m=Array.from({length:h},()=>new Int16Array(65536)),U=(t,e,r,c)=>{for(let f=0;f<c;f++){let c=4*((e+f)*o+t),l=f*r;for(let t=0;t<r;t++,c+=4,l++)if(s)m[0][l]=s.index.get(s.key(c));else if(i>=3){const t=n[c],o=n[c+1],e=n[c+2],r=t-e,s=e+(r>>1),f=o-s;m[0][l]=s+(f>>1),m[1][l]=r,m[2][l]=f,4===i&&(m[3][l]=n[c+3])}else m[0][l]=n[c],2===i&&(m[1][l]=n[c+3])}};let b=null;if(s){const t=s.colours.length;b=new Int16Array(t*i),s.colours.forEach((n,o)=>{for(let e=0;e<i;e++)b[e*t+o]=s.byte(n,e)})}const x=t=>a[s?l.F?1:0:t],E=s?1:i,I=t=>{for(let n=0;n<l.Z;n++)for(let r=0;r<l.Y;r++){const i=r*z,s=n*z,c=Math.min(z,o-i),f=Math.min(z,e-s);U(i,s,c,f),t(n*l.Y+r,c,f)}};if(!s){const t=t=>{const n=c(t),o=new L(128);f(o,n);let e=o.$;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},n=Math.min(o,32),r=Math.min(e,32),s=[_,J].map(t=>({N:t,tt:a.map(()=>new Uint32Array(257))}));for(const t of[0,.5,1]){U(Math.floor((o-n)*t),Math.floor((e-r)*t),n,r);for(const t of s)for(let o=0;o<i;o++)d(null,t.tt[o],m[o],n,r,y(t.N))}a.forEach((n,o)=>{const e=s.map(n=>t(n.tt[o])),r=e[1]<e[0]?1:0;n.N=s[r].N})}const S=s?[{type:"palette",_:0,G:i,O:s.colours.length}]:i>=3?[{type:"rct",_:0,C:6}]:[],T=a.map(()=>new Uint32Array(257)),X=t=>T[a.indexOf(t)];b&&d(null,X(a[0]),b,s.colours.length,i,a[0]),I((t,n,o)=>{for(let t=0;t<E;t++)d(null,X(x(t)),m[t],n,o,x(t))});const N=new L(256);!function(t,n,o,e,r,{xyb:i=!1,orientation:s=1}={}){t.write(16,2815),t.write(1,0),A(t,o),t.write(3,0),A(t,n),t.write(1,0);const c=1!==s;t.write(1,c?1:0),c&&t.write(6,s-1),t.write(4,8),r?t.write(3,5):t.write(2,0),t.write(1,i?1:0),3===e?t.write(1,1):t.write(15,14100),c&&t.write(1,1),t.write(2,0),t.write(1,1),t.nt()}(N,o,e,r.colour,r.alpha),function(t,{alpha:n}){t.write(9,8),n&&t.write(2,0),t.write(2,1),function(t,n){t.write(n?7:5,0),t.write(11,1)}(t,n)}(N,{alpha:r.alpha});const C=new L(4096);C.write(1,1),C.write(1,1);const P=function(n,o){const e=[o],r=[],i=[];for(const n of e){if(n.left){r.push(n.K+1,t(n.V)),e.push(n.left,n.right);continue}n.context=i.length,i.push(n);const o=1===n.J?0:31-Math.clz32(n.J&-n.J);r.push(0,n.N,t(n.offset),o,(n.J>>o)-1)}const s=new Uint32Array(64);for(const t of r)u(O,t,s);const f=c(s);p(n,{I:new Uint8Array(6),L:[{config:O,code:f}]});for(const t of r)w(n,f,O,t);return i}(C,g),v=function(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:O,code:c(i)}],f=Math.max(...r);for(let t=0;t<f;t++)s.push({config:O,code:c(o[t])});return p(t,{U:G,I:r,L:s}),s}(C,P,T,t=>a.indexOf(t)),B=t=>v[a.indexOf(t)+1].code;M(C,{k:!0,R:S}),b&&d(C,B(a[0]),b,s.colours.length,i,a[0]);const j=[];if(l.F)I((t,n,o)=>{for(let t=0;t<E;t++)d(C,B(x(t)),m[t],n,o,x(t))}),j.push(C.finish());else{j.push(C.finish());for(let t=0;t<l.q*l.D+1;t++)j.push(new Uint8Array(0));I((t,n,o)=>{const e=new L(n*o*E+64);M(e,{k:!0,R:[]});for(let t=0;t<E;t++)d(e,B(x(t)),m[t],n,o,x(t));j.push(e.finish())})}return function(t,n){const o=n.map(t=>t.length);!function(t,n){t.write(1,0),t.nt();for(const o of n)t.S([[10,0],[14,1024],[22,17408],[30,4211712]],o);t.nt()}(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}(N,j)}function b(t,n,o){if(function(t,n){if(!Number.isInteger(t)||!Number.isInteger(n)||t<1||n<1)throw P("JXL_INPUT","A picture is at least one pixel wide and high, in whole pixels.");if(t>E.edge||n>E.edge)throw P("JXL_DIMENSIONS","A picture is at most "+E.edge+" pixels on a side.");if(t*n>E.pixels)throw P("JXL_DIMENSIONS","A picture is at most "+E.pixels.toLocaleString("en-US")+" pixels.")}(n,o),!(t instanceof Uint8Array||t instanceof Uint8ClampedArray))throw P("JXL_INPUT","Pixels are a Uint8Array or Uint8ClampedArray of RGBA bytes.");if(t.length!==n*o*4)throw P("JXL_INPUT","Pixels are width * height * 4 bytes: straight (not premultiplied) RGBA, row by row.")}function x(t,n,o){return b(t,n,o),function(t){try{return t()}catch(t){if(t instanceof RangeError&&!t.code)throw P("JXL_MEMORY","Not enough memory for this picture.");throw t}}(()=>function(t){if(t.length>E.bytes)throw P("JXL_SIZE","The encoded picture exceeds 16 MiB.");return t}(function(t,n,o,{shape:e=m(t,n,o)}={}){let r,i;for(const s of e.palette?[e,{...e,palette:null}]:[e])try{const e=U(t,n,o,s);(!r||e.length<r.length)&&(r=e)}catch(t){if("JXL_SIZE"!==t.code)throw t;i=t}if(!r)throw i;return r}(t,n,o,{shape:m(t,n,o)})))}const E=Object.freeze({bytes:16777216,pixels:24e6,edge:16384}),I=Array.from({length:33},(t,n)=>2**n);class L{constructor(t=4096){this.bytes=new Uint8Array(t),this.at=0,this.ot=0,this.pending=0}write(t,n){if(t>32||n<0||n>=(I[t]??2**t))throw new Error("bit write out of range: "+t+" bits, "+n);let o=this.ot+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.ot=o,this.pending=e}S(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)}nt(){this.pending&&(this.at+1>=this.bytes.length&&this.grow(),this.bytes[this.at++]=this.ot,this.ot=0,this.pending=0)}get $(){return 8*this.at+this.pending}grow(t=0){const n=E.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.ot)}finish(){const t=this.at+(this.pending?1:0),n=this.bytes.slice(0,t);return this.pending&&(n[this.at]=this.ot),n}}const S=[1,2,3,4,0,5,17,6,16,7,8,9,10,11,12,13,14,15],T=[2,4,3,2,2,4],X=[0,7,3,2,1,15],N=[0,0,0],J=3,_=5,C=l(4),G=Object.freeze({T:224,minLength:7,X:C}),O=l(0),z=256,P=(t,n)=>Object.assign(new Error(n),{code:t});export{x as encodeLosslessRGBA,E as LIMITS};
|