@thi.ng/pixel
Advanced tools
Comparing version 0.10.2 to 0.10.3
@@ -6,3 +6,3 @@ # Change Log | ||
## [0.10.2](https://github.com/thi-ng/umbrella/compare/@thi.ng/pixel@0.10.1...@thi.ng/pixel@0.10.2) (2021-04-24) | ||
## [0.10.3](https://github.com/thi-ng/umbrella/compare/@thi.ng/pixel@0.10.2...@thi.ng/pixel@0.10.3) (2021-06-08) | ||
@@ -15,10 +15,2 @@ **Note:** Version bump only for package @thi.ng/pixel | ||
## [0.10.1](https://github.com/thi-ng/umbrella/compare/@thi.ng/pixel@0.10.0...@thi.ng/pixel@0.10.1) (2021-04-20) | ||
**Note:** Version bump only for package @thi.ng/pixel | ||
# [0.10.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/pixel@0.9.0...@thi.ng/pixel@0.10.0) (2021-04-19) | ||
@@ -25,0 +17,0 @@ |
@@ -89,2 +89,5 @@ import { assert } from "@thi.ng/api"; | ||
}; | ||
const declOffset = (idx, i, pre, stride, min, max) => idx < 0 | ||
? `const ${pre}${i} = max(${pre}${idx < -1 ? idx + "*" : "-"}${stride},${min});` | ||
: `const ${pre}${i} = min(${pre}+${idx > 1 ? idx + "*" : ""}${stride},${max});`; | ||
/** | ||
@@ -130,5 +133,3 @@ * HOF convolution or pooling kernel code generator. Takes either a | ||
if (y === 0 && xx !== 0) { | ||
prefix.push(xx < 0 | ||
? `const x${x} = max(x${xx < -1 ? xx + "*" : "-"}stride,channel);` | ||
: `const x${x} = min(x+${xx > 1 ? xx + "*" : ""}stride,maxX+channel);`); | ||
prefix.push(declOffset(xx, x, "x", "stride", "channel", "maxX+channel")); | ||
} | ||
@@ -138,5 +139,3 @@ } | ||
if (yy !== 0) { | ||
prefix.push(yy < 0 | ||
? `const y${y} = max(y${yy < -1 ? yy + "*" : "-"}rowStride,0);` | ||
: `const y${y} = min(y+${yy > 1 ? yy + "*" : ""}rowStride,maxY);`); | ||
prefix.push(declOffset(yy, y, "y", "rowStride", "0", "maxY")); | ||
} | ||
@@ -143,0 +142,0 @@ } |
@@ -1158,2 +1158,5 @@ 'use strict'; | ||
}; | ||
const declOffset = (idx, i, pre, stride, min, max) => idx < 0 | ||
? `const ${pre}${i} = max(${pre}${idx < -1 ? idx + "*" : "-"}${stride},${min});` | ||
: `const ${pre}${i} = min(${pre}+${idx > 1 ? idx + "*" : ""}${stride},${max});`; | ||
const defKernel = (tpl, w, h) => { | ||
@@ -1180,5 +1183,3 @@ if (w * h > 512 && !checks.isFunction(tpl)) | ||
if (y === 0 && xx !== 0) { | ||
prefix.push(xx < 0 | ||
? `const x${x} = max(x${xx < -1 ? xx + "*" : "-"}stride,channel);` | ||
: `const x${x} = min(x+${xx > 1 ? xx + "*" : ""}stride,maxX+channel);`); | ||
prefix.push(declOffset(xx, x, "x", "stride", "channel", "maxX+channel")); | ||
} | ||
@@ -1188,5 +1189,3 @@ } | ||
if (yy !== 0) { | ||
prefix.push(yy < 0 | ||
? `const y${y} = max(y${yy < -1 ? yy + "*" : "-"}rowStride,0);` | ||
: `const y${y} = min(y+${yy > 1 ? yy + "*" : ""}rowStride,maxY);`); | ||
prefix.push(declOffset(yy, y, "y", "rowStride", "0", "maxY")); | ||
} | ||
@@ -1193,0 +1192,0 @@ } |
@@ -1,1 +0,1 @@ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@thi.ng/checks"),require("@thi.ng/api"),require("@thi.ng/math"),require("@thi.ng/porter-duff"),require("@thi.ng/k-means"),require("@thi.ng/binary")):"function"==typeof define&&define.amd?define(["exports","@thi.ng/checks","@thi.ng/api","@thi.ng/math","@thi.ng/porter-duff","@thi.ng/k-means","@thi.ng/binary"],e):e(((t="undefined"!=typeof globalThis?globalThis:t||self).thi=t.thi||{},t.thi.ng=t.thi.ng||{},t.thi.ng.pixel={}),t.thi.ng.checks,t.thi.ng.api,t.thi.ng.math,t.thi.ng.porterDuff,t.thi.ng.kMeans,t.thi.ng.binary)}(this,(function(t,e,s,n,i,r,a){"use strict";var h;t.Lane=void 0,(h=t.Lane||(t.Lane={}))[h.ALPHA=0]="ALPHA",h[h.BLUE=1]="BLUE",h[h.GREEN=2]="GREEN",h[h.RED=3]="RED";const l=(t,e=t,s)=>{const n=document.createElement("canvas");return n.width=t,n.height=e,s&&s.appendChild(n),{canvas:n,ctx:n.getContext("2d")}};function o(t,s){let n,i;if(e.isNumber(t)){const e=l(t,s);n=e.canvas,i=e.ctx}else n=t,i=n.getContext("2d");const r=i.getImageData(0,0,n.width,n.height);return{canvas:n,ctx:i,img:r,pixels:new Uint32Array(r.data.buffer)}}const c=(t,s,n=s,i)=>{const r=e.isNumber(s)?l(s,n,i):l(t.width,t.height,i);return r.ctx.drawImage(t,0,0,r.canvas.width,r.canvas.height),r},f=(t,e,n,i=1)=>s.assert(t.length>=e*n*i,"pixel buffer too small"),p=(t,e)=>{const n=t.channels[e];return s.assert(null!=n,`invalid channel ID: ${e}`),n},u=t=>(29*(t>>>16&255)+150*(t>>>8&255)+76*(255&t))/255,m=(t,e,s,i,r,a,h=0,l=0)=>(s|=0,i|=0,(t|=0)<0&&(s+=t,h-=t,t=0),(e|=0)<0&&(i+=e,l-=e,e=0),[t,e,n.clamp(s,0,r-t),n.clamp(i,0,a-e),h,l]),d=(t,e,s={})=>{let n,i,r,a,h,l,o=t.width,c=e.width;return[n,i,h,l]=m(s.sx||0,s.sy||0,s.w||o,s.h||t.height,o,t.height),[r,a,h,l,n,i]=m(s.dx||0,s.dy||0,h,l,c,e.height,n,i),{sx:n,sy:i,dx:r,dy:a,rw:h,rh:l}},g=(t,e,s)=>{for(let n=t.length;--n>=0;)t[n]=s(t[n],e)},A=(t,e,s,n)=>{for(let i=t.length;--i>=0;)t[i]=n(t[i],s(e[i]))},w=(t,e,s,n,i)=>{const r=~i;for(let a=t.length;--a>=0;)t[a]=t[a]&r|s(n(e[a]))&i},x=(t,e,s)=>{const n=e.fromABGR,i=e.toABGR;for(let e=t.length;--e>=0;)t[e]=n(s(i(t[e])))},R=t=>e.isNumber(t)?[t,t]:t,y=t=>{const e=R(t);return[0|e[0],0|e[1]]},L=t=>new Uint8Array(t).map(((t,e)=>e)),B=(t,e)=>e>0?`(${t} << ${e})`:e<0?`(${t} >>> ${-e})`:`${t}`,G=(t,e)=>B(t,-e),z=t=>`0x${t.toString(16)}`,E=t=>{const e=(1<<t)-1;return new Function("luma",`return (x) => ${G("luma(x)",8-t)} & ${e};`)(u)},b=t=>{let e;if(8!==t){const s=(1<<t)-1;e=`(((x & ${s}) * ${255/s}) | 0)`}else e="x";return new Function("x",`return 0xff000000 | (${e} * 0x010101);`)},$=t=>new Function("x","return ("+t.map((t=>{const e=t.abgrShift+(8-t.size);return`(${G("x",e)} & ${z(t.maskA)})`})).join(" | ")+") >>> 0;"),S=(t,e)=>{const s=t.map((t=>{if(8!==t.size){const e=t.mask0,s=255/e,n=G("x",t.shift);return B(`((${n} & ${e}) * ${s})`,24-8*t.lane)}return B(`(x & ${z(t.maskA)})`,t.abgrShift)})).join(" | ");return new Function("x",`return (${e?"":"0xff000000 | "}${s}) >>> 0;`)},_=t=>{const e=t.channels,s=e.reduce(((t,e)=>(t[e]=3-e<<3,t)),{}),i=Object.assign(Object.assign({},t),{size:e.length,shift:s,__float:!0});if(t.convert)return Object.assign(i,t.convert),i;const r=(t,i)=>(255*n.clamp01(t[i])+.5|0)<<s[e[i]],a=(t,n)=>(t>>>s[e[n]]&255)/255;switch(e.length){case 1:t.gray?P(i):v(i,a,r);break;case 2:t.gray?D(i,a):k(i,a,r);break;case 3:C(i,a,r);break;case 4:O(i,a,r)}return i},v=(t,e,s)=>{t.toABGR=e=>{let n=t.alpha?0:4278190080;return n|=s(e,0),n>>>0},t.fromABGR=(t,s=[])=>(s[0]=e(t,0),s)},P=t=>{t.toABGR=t=>(65793*(255*n.clamp01(t[0])+.5|0)|4278190080)>>>0,t.fromABGR=(t,e=[])=>(e[0]=u(t)/255,e)},k=(t,e,s)=>{t.toABGR=e=>{let n=t.alpha?0:4278190080;return n|=s(e,0),n|=s(e,1),n>>>0},t.fromABGR=(t,s=[])=>(s[0]=e(t,0),s[1]=e(t,1),s)},D=(e,s)=>{const i=~~(e.channels[0]===t.Lane.ALPHA),r=1^i;e.toABGR=t=>{let e=65793*(255*n.clamp01(t[i])+.5|0);return e|=(255*t[r]+.5|0)<<24,e>>>0},e.fromABGR=(t,e=[])=>(e[i]=u(t)/255,e[r]=s(t,r),e)},C=(t,e,s)=>{t.toABGR=e=>{let n=t.alpha?0:4278190080;return n|=s(e,0),n|=s(e,1),n|=s(e,2),n>>>0},t.fromABGR=(t,s=[])=>(s[0]=e(t,0),s[1]=e(t,1),s[2]=e(t,2),s)},O=(t,e,s)=>{t.toABGR=e=>{let n=t.alpha?0:4278190080;return n|=s(e,0),n|=s(e,1),n|=s(e,2),n|=s(e,3),n>>>0},t.fromABGR=(t,s=[])=>(s[0]=e(t,0),s[1]=e(t,1),s[2]=e(t,2),s[3]=e(t,3),s)},F=_({gray:!0,channels:[t.Lane.RED]}),N=(t,e,s,n,i,r)=>{if(1===s)return!r[e]&&(r[e]=[]),r[e][t]=n,r;const a=i<<2;return N(t,e,s>>=1,n,a,r),N(t+s,e+s,s,n+i,a,r),N(t+s,e,s,n+2*i,a,r),N(t,e+s,s,n+3*i,a,r),r},U=t=>({mat:N(0,0,t,0,1,[]),invSize:1/(t*t),mask:t-1}),I=({mat:t,mask:e,invSize:s},i,r,a,h,l,o)=>(o=i*(o/a)+t[l&e][h&e]*s-.5|0,i--,n.clamp(o,0,i)*((r-1)/i)),H=t=>{s.assert(t.channels.length>0,"no channel specs given");const e=t.channels.reduce((([t,e],s,i)=>(e-=s.size,t.push(((t,e,s)=>{const i=1<<t.size,r=i-1,a=r<<s>>>0,h=~a>>>0,l=null!=t.lane?t.lane:e,o=t=>t>>>s&r,c=(t,e)=>t&h|(e&r)<<s;return{size:t.size,abgrShift:24-8*l-s,lane:l,shift:s,mask0:r,maskA:a,int:o,setInt:c,float:t=>o(t)/r,setFloat:(t,e)=>c(t,n.clamp01(e)*r),dither:(t,e,s,n,r)=>I(t,e,i,i,s,n,r)}})(s,i,e)),[t,e])),[[],t.size])[0];return{__packed:!0,type:t.type,size:t.size,alpha:t.alpha||0,channels:e,fromABGR:t.fromABGR||$(e),toABGR:t.toABGR||S(e,!!t.alpha)}},M=H({type:"u32",size:32,alpha:8,channels:[{size:8,lane:t.Lane.ALPHA},{size:8,lane:t.Lane.BLUE},{size:8,lane:t.Lane.GREEN},{size:8,lane:t.Lane.RED}],fromABGR:t=>t,toABGR:t=>t});function j(t,e="linear",n="clamp"){const i=!!t.format.__float,r=1===t.format.channels.length?"1":"",a=`${e[0]}${n[0]}${r}`,h=(i?{nc1:q,nw1:K,nr1:V,nc:q,nw:K,nr:V,lc1:t=>J(T(t)),lw1:t=>J(Y(t)),lr1:t=>J(X(t)),lc:t=>tt(t,q(t)),lw:t=>tt(t,K(t)),lr:t=>tt(t,V(t)),cc1:t=>nt(T(t)),cw1:t=>nt(Y(t)),cr1:t=>nt(X(t)),cc:t=>ht(t,q(t)),cw:t=>ht(t,K(t)),cr:t=>ht(t,V(t))}:{nc1:T,nw1:Y,nr1:X,nc:T,nw:Y,nr:X,lc1:t=>Z(T(t)),lw1:t=>Z(Y(t)),lr1:t=>Z(X(t)),lc:t=>Q(t,T(t)),lw:t=>Q(t,Y(t)),lr:t=>Q(t,X(t)),cc1:t=>st(t,T(t)),cw1:t=>st(t,Y(t)),cr1:t=>st(t,X(t)),cc:t=>at(t,T(t)),cw:t=>at(t,Y(t)),cr:t=>at(t,X(t))})[a];return s.assert(!!h,`missing impl for ${a}`),h(t)}const T=({pixels:t,width:e,height:s})=>(n,i)=>n>=0&&n<e&&i>=0&&i<s?t[(0|i)*e+(0|n)]:0,Y=({pixels:t,width:e,height:s})=>(i,r)=>t[n.mod(0|r,s)*e+n.mod(0|i,e)],X=({pixels:t,width:e,height:s})=>{const i=e-1,r=s-1;return(s,a)=>t[n.clamp(0|a,0,r)*e+n.clamp(0|s,0,i)]},q=({pixels:t,width:e,height:s,rowStride:n,stride:i})=>(r,a)=>{let h;return r>=0&&r<e&&a>=0&&a<s?(h=(0|a)*n+(0|r)*i,t.slice(h,h+i)):[0]},K=({pixels:t,width:e,height:s,rowStride:i,stride:r})=>(a,h)=>{let l=n.mod(0|h,s)*i+n.mod(0|a,e)*r;return t.slice(l,l+r)},V=({pixels:t,width:e,height:s,rowStride:i,stride:r})=>{const a=e-1,h=s-1;return(e,s)=>{let l=n.clamp(0|s,0,h)*i+n.clamp(0|e,0,a)*r;return t.slice(l,l+r)}},W=(t,e,s,i,r=4)=>n.mixBilinear(t[i],t[i+r],t[i+2*r],t[i+3*r],e,s),Z=t=>(e,s)=>(e-=.5,s-=.5,n.mixBilinear(t(e,s),t(e+1,s),t(e,s+1),t(e+1,s+1),n.fract(e),n.fract(s))),J=t=>(t=Z(t),(e,s)=>[t(e,s)]),Q=(t,e)=>{const{fromABGR:s,toABGR:i}=t.format,r=new Uint32Array(4),a=new Uint8Array(r.buffer);return(t,h)=>{t-=.5,h-=.5,r[0]=i(e(t,h)),r[1]=i(e(t+1,h)),r[2]=i(e(t,h+1)),r[3]=i(e(t+1,h+1));const l=n.fract(t),o=n.fract(h);return s(W(a,l,o,0)|W(a,l,o,1)<<8|W(a,l,o,2)<<16|W(a,l,o,3)<<24)>>>0}},tt=({stride:t},e)=>{const s=new Float32Array(4*t);return(i,r)=>{i-=.5,r-=.5,s.set(e(i,r),0),s.set(e(i+1,r),t),s.set(e(i,r+1),2*t),s.set(e(i+1,r+1),3*t);const a=n.fract(i),h=n.fract(r);let l=[];for(let e=0;e<t;e++)l.push(W(s,a,h,e,t));return l}},et=t=>(e,s)=>{const i=(e-=.5)-1,r=e+1,a=e+2,h=(s-=.5)-1,l=s+1,o=s+2;return n.mixBicubic(t(i,h),t(e,h),t(r,h),t(a,h),t(i,s),t(e,s),t(r,s),t(a,s),t(i,l),t(e,l),t(r,l),t(a,l),t(i,o),t(e,o),t(r,o),t(a,o),n.fract(e),n.fract(s))},st=(t,e)=>{const s=t.format.channels[0].mask0;return e=et(e),(t,i)=>n.clamp(e(t,i),0,s)},nt=t=>(t=et(t),(e,s)=>[t(e,s)]),it=(t,e,s,i,r=4)=>n.mixBicubic(t[i],t[i+r],t[i+2*r],t[i+3*r],t[i+4*r],t[i+5*r],t[i+6*r],t[i+7*r],t[i+8*r],t[i+9*r],t[i+10*r],t[i+11*r],t[i+12*r],t[i+13*r],t[i+14*r],t[i+15*r],e,s),rt=(t,e,s,i,r=4)=>n.clamp(it(t,e,s,i,r),0,255),at=(t,e)=>{const{fromABGR:s,toABGR:i}=t.format,r=new Uint32Array(16),a=new Uint8Array(r.buffer);return(t,h)=>{const l=(t-=.5)-1,o=t+1,c=t+2,f=(h-=.5)-1,p=h+1,u=h+2,m=n.fract(t),d=n.fract(h);return r[0]=i(e(l,f)),r[1]=i(e(t,f)),r[2]=i(e(o,f)),r[3]=i(e(c,f)),r[4]=i(e(l,h)),r[5]=i(e(t,h)),r[6]=i(e(o,h)),r[7]=i(e(c,h)),r[8]=i(e(l,p)),r[9]=i(e(t,p)),r[10]=i(e(o,p)),r[11]=i(e(c,p)),r[12]=i(e(l,u)),r[13]=i(e(t,u)),r[14]=i(e(o,u)),r[15]=i(e(c,u)),s(rt(a,m,d,0)|rt(a,m,d,1)<<8|rt(a,m,d,2)<<16|rt(a,m,d,3)<<24)>>>0}},ht=({stride:t},e)=>{const s=new Float32Array(16*t);return(i,r)=>{const a=(i-=.5)-1,h=i+1,l=i+2,o=(r-=.5)-1,c=r+1,f=r+2,p=n.fract(i),u=n.fract(r);s.set(e(a,o),0),s.set(e(i,o),t),s.set(e(h,o),2*t),s.set(e(l,o),3*t),s.set(e(a,r),4*t),s.set(e(i,r),5*t),s.set(e(h,r),6*t),s.set(e(l,r),7*t),s.set(e(a,c),8*t),s.set(e(i,c),9*t),s.set(e(h,c),10*t),s.set(e(l,c),11*t),s.set(e(a,f),12*t),s.set(e(i,f),13*t),s.set(e(h,f),14*t),s.set(e(l,f),15*t);let m=[];for(let e=0;e<t;e++)m.push(it(s,p,u,e,t));return m}};function lt(...t){return t[0]instanceof ct?t[0].as(t[1]):new ct(...t)}const ot=lt;class ct{constructor(t,e,n,i){this.width=t,this.height=e,this.format=n.__packed?n:H(n),this.pixels=i||s.typedArray(n.type,t*e)}static fromImage(t,e,s,n=s){return ct.fromCanvas(c(t,s,n).canvas,e)}static fromCanvas(t,e=M){const n=o(t),i=t.width,r=t.height;let a;if(e===M)a=n.pixels;else{a=s.typedArray(e.type,i*r);const t=n.pixels,h=e.fromABGR;for(let e=a.length;--e>=0;)a[e]=h(t[e])}return new ct(i,r,e,a)}get stride(){return 1}as(t){return this.getRegion(0,0,this.width,this.height,t)}copy(){const t=this.empty();return t.pixels.set(this.pixels),t}empty(){return new ct(this.width,this.height,this.format)}getAt(t,e){return t>=0&&t<this.width&&e>=0&&e<this.height?this.pixels[(0|t)+(0|e)*this.width]:0}setAt(t,e,s){return t>=0&&t<this.width&&e>=0&&e<this.height&&(this.pixels[(0|t)+(0|e)*this.width]=s),this}getChannelAt(t,e,s,n=!1){const i=p(this.format,s),r=this.getAt(t,e);return n?i.float(r):i.int(r)}setChannelAt(t,e,s,n,i=!1){const r=p(this.format,s),a=this.getAt(t,e);return i?r.setFloat(a,n):r.setInt(a,n),this}blend(t,e,s){let n=this.width,i=e.width;const{sx:r,sy:a,dx:h,dy:l,rw:o,rh:c}=d(this,e,s);if(o<1||c<1)return e;const f=this.pixels,p=e.pixels,u=this.format.toABGR,m=e.format.toABGR,g=e.format.fromABGR;for(let e=(0|r)+(0|a)*n,s=(0|h)+(0|l)*i,d=0;d<c;d++,e+=n,s+=i)for(let n=0;n<o;n++)p[s+n]=g(t(u(f[e+n]),m(p[s+n])));return e}blit(t,e){let s=this.width,n=t.width;const{sx:i,sy:r,dx:a,dy:h,rw:l,rh:o}=d(this,t,e);if(l<1||o<1)return t;const c=this.pixels,f=t.pixels,p=this.format.toABGR,u=t.format.fromABGR,m=this.format!==t.format?(t,e)=>{for(let s=0;s<l;s++)f[e+s]=u(p(c[t+s]))}:(t,e)=>f.set(c.subarray(t,t+l),e);for(let t=(0|i)+(0|r)*s,e=(0|a)+(0|h)*n,l=0;l<o;l++,t+=s,e+=n)m(t,e);return t}blitCanvas(t,e=0,s=0){(t instanceof HTMLCanvasElement?t.getContext("2d"):t).putImageData(this.toImageData(),e,s)}toImageData(){const t=new ImageData(this.width,this.height),e=new Uint32Array(t.data.buffer),s=this.pixels,n=this.format.toABGR;for(let t=e.length;--t>=0;)e[t]=n(s[t]);return t}getRegion(t,e,s,n,i){const[r,a,h,l]=m(t,e,s,n,this.width,this.height);return this.blit(new ct(h,l,i||this.format),{sx:r,sy:a,w:h,h:l})}getChannel(e){const n=p(this.format,e),i=new ct(this.width,this.height,{type:s.uintTypeForBits(n.size),size:n.size,channels:[{size:n.size,lane:t.Lane.RED}],fromABGR:E(n.size),toABGR:b(n.size)}),r=this.pixels,a=i.pixels,h=n.int;for(let t=r.length;--t>=0;)a[t]=h(r[t]);return i}setChannel(t,s){const n=p(this.format,t),i=this.pixels,r=n.setInt;if(e.isNumber(s))g(i,s,r);else{const t=s.pixels,e=s.format.channels[0];f(t,this.width,this.height),n.size===e.size?A(i,t,e.int,r):w(i,t,this.format.fromABGR,s.format.toABGR,n.maskA)}return this}invert(){const{format:t,pixels:e}=this,s=Math.pow(2,t.size-t.alpha)-1;for(let t=e.length;--t>=0;)e[t]^=s;return this}premultiply(){return x(this.pixels,this.format,i.premultiplyInt),this}postmultiply(){return x(this.pixels,this.format,i.postmultiplyInt),this}isPremultiplied(){const t=this.pixels,e=this.format.toABGR;for(let s=t.length;--s>=0;)if(!i.isPremultipliedInt(e(t[s])))return!1;return!0}forEach(t){const e=this.pixels;for(let s=e.length;--s>=0;)e[s]=t(e[s],s);return this}dither(t,s){const{pixels:n,format:i,width:r}=this,a=e.isNumber(s)?new Array(i.channels.length).fill(s):s,h=e.isNumber(t)?U(t):t;for(let t=0,e=n.length,s=i.channels.length,l=0,o=0;t<e;t++){let e=n[t];for(let t=0;t<s;t++){const s=i.channels[t],n=a[t];n>0&&(e=s.setInt(e,s.dither(h,n,l,o,s.int(e))))}n[t]=e,++l===r&&(l=0,o++)}return this}flipY(){const{pixels:t,width:e}=this,n=s.typedArray(this.format.type,e);for(let s=0,i=t.length-e;s<i;s+=e,i-=e)n.set(t.subarray(s,s+e)),t.copyWithin(s,i,i+e),t.set(n,i);return this}scale(t,e="linear"){return s.assert(t>0,"scale must be > 0"),this.resize(this.width*t,this.height*t,e)}resize(t,n,i="linear"){t|=0,n|=0,s.assert(t>0&&n>0,"target width & height must be > 0");const r=lt(t,n,this.format),a=r.pixels,h=t>0?this.width/t:0,l=n>0?this.height/n:0;i=e.isString(i)?j(this,i,"repeat"):i;for(let e=0,s=0;e<n;e++){const n=e*l;for(let e=0;e<t;e++,s++)a[s]=i(e*h,n)}return r}upsize(){const{width:t,height:e,pixels:s}=this,n=new ct(2*t,2*e,this.format),i=n.pixels;for(let n=0,r=0;n<e;n++)for(let e=0,a=n*t*4;e<t;e++,r++,a+=2)i[a]=s[r];return n}}function ft(...t){return t[0]instanceof ct?pt.fromPacked(...t):new pt(...t)}class pt{constructor(t,e,s,n){this.width=t,this.height=e,this.format=s.__float?s:_(s),this.stride=s.channels.length,this.rowStride=t*this.stride,this.pixels=n||new Float32Array(t*e*this.stride),this.__empty=Object.freeze(new Array(this.stride).fill(0))}static fromPacked(t,e){const s=new pt(t.width,t.height,e),{pixels:n,format:i,stride:r}=s,{pixels:a,format:h}=t;for(let t=a.length;--t>=0;)n.set(i.fromABGR(h.toABGR(a[t])),t*r);return s}as(t){const{width:e,height:s,stride:n,pixels:i,format:r}=this,a=new ct(e,s,t),h=a.pixels;for(let e=0,s=0,a=i.length;e<a;e+=n,s++)h[s]=t.fromABGR(r.toABGR(i.subarray(e,e+n)));return a}copy(){const t=this.empty();return t.pixels.set(this.pixels),t}empty(){return new pt(this.width,this.height,this.format)}getAt(t,e){const{width:s,stride:n}=this;if(t>=0&&t<s&&e>=0&&e<this.height){const s=(0|t)*n+(0|e)*this.rowStride;return this.pixels.subarray(s,s+n)}return this.__empty}setAt(t,e,s){return t>=0&&t<this.width&&e>=0&&e<this.height&&this.pixels.set(s,(0|t)*this.stride+(0|e)*this.rowStride),this}getChannelAt(t,e,s){p(this.format,s);const{width:n,stride:i}=this;if(t>=0&&t<n&&e>=0&&e<this.height)return this.pixels[(0|t)*i+(0|e)*this.rowStride+s]}setChannelAt(t,e,s,n){p(this.format,s);const{width:i,stride:r}=this;return t>=0&&t<i&&e>=0&&e<this.height&&(this.pixels[(0|t)*r+(0|e)*this.rowStride+s]=n),this}getChannel(t){p(this.format,t);const{pixels:e,stride:s}=this,i=new Float32Array(this.width*this.height);for(let r=t,a=0,h=e.length;r<h;r+=s,a++)i[a]=n.clamp01(e[r]);return new pt(this.width,this.height,F,i)}setChannel(t,s){p(this.format,t);const{pixels:n,stride:i}=this;if(e.isNumber(s))for(let e=t,r=n.length;e<r;e+=i)n[e]=s;else{const{pixels:e,stride:r}=s;f(e,this.width,this.height,r);for(let s=t,a=0,h=n.length;s<h;s+=i,a+=r)n[s]=e[a]}return this}blend(t,e,s){this.ensureFormat(e);const{sx:n,sy:i,dx:r,dy:a,rw:h,rh:l}=d(this,e,s);if(h<1||l<1)return e;const o=this.pixels,c=e.pixels,f=this.rowStride,p=e.rowStride,u=this.stride;for(let e=(0|n)*u+(0|i)*f,s=(0|r)*u+(0|a)*p,m=0;m<l;m++,e+=f,s+=p)for(let n=h,i=e,r=s;--n>=0;i+=u,r+=u){const e=c.subarray(r,r+u);t(e,o.subarray(i,i+u),e)}return e}blit(t,e){this.ensureFormat(t);const{sx:s,sy:n,dx:i,dy:r,rw:a,rh:h}=d(this,t,e);if(a<1||h<1)return t;const l=this.pixels,o=t.pixels,c=this.rowStride,f=t.rowStride,p=a*this.stride;for(let t=(0|s)*this.stride+(0|n)*c,e=(0|i)*this.stride+(0|r)*f,a=0;a<h;a++,t+=c,e+=f)o.set(l.subarray(t,t+p),e);return t}blitCanvas(t,e=0,s=0){(t instanceof HTMLCanvasElement?t.getContext("2d"):t).putImageData(this.toImageData(),e,s)}toImageData(){const t=new ImageData(this.width,this.height),e=new Uint32Array(t.data.buffer),{stride:s,pixels:n,format:i}=this;for(let t=0,r=0,a=n.length;t<a;t+=s,r++)e[r]=i.toABGR(n.subarray(t,t+s));return t}getRegion(t,e,s,n){const[i,r,a,h]=m(t,e,s,n,this.width,this.height);return this.blit(new pt(a,h,this.format),{sx:i,sy:r,w:a,h})}forEach(t){const{pixels:e,stride:s}=this;for(let n=0,i=0,r=e.length;n<r;n+=s,i++)e.set(t(e.subarray(n,n+s),i),n);return this}clamp(){const t=this.pixels;for(let e=t.length;--e>=0;)t[e]=n.clamp01(t[e]);return this}clampChannel(t){p(this.format,t);const{pixels:e,stride:s}=this;for(let i=t,r=e.length;i<r;i+=s)e[i]=n.clamp01(e[i])}flipY(){const{pixels:t,rowStride:e}=this,s=new Float32Array(e);for(let n=0,i=t.length-e;n<i;n+=e,i-=e)s.set(t.subarray(n,n+e)),t.copyWithin(n,i,i+e),t.set(s,i);return this}invert(){const{pixels:t,format:e,stride:s}=this;for(let n=0,i=t.length,r=e.alpha?s-1:s;n<i;n+=s)for(let e=0;e<r;e++)t[n+e]=1-t[n+e];return this}scale(t,e){return s.assert(t>0,"scale must be > 0"),this.resize(this.width*t,this.height*t,e)}resize(t,n,i="linear"){t|=0,n|=0,s.assert(t>0&&n>0,"target width & height must be > 0");const r=ft(t,n,this.format),a=r.pixels,h=t>0?this.width/t:0,l=n>0?this.height/n:0,o=this.stride;i=e.isString(i)?j(this,i,"repeat"):i;for(let e=0,s=0;e<n;e++){const n=e*l;for(let e=0;e<t;e++,s+=o)a.set(i(e*h,n),s)}return r}upsize(){const{width:t,height:e,pixels:s,stride:n,rowStride:i}=this,r=2*n,a=ft(2*t,2*e,this.format),h=a.pixels;for(let a=0,l=0;a<e;a++)for(let e=0,o=a*i*4;e<t;e++,l+=n,o+=r)h.set(s.subarray(l,l+n),o);return a}ensureFormat(t){s.assert(t.format===this.format,"dest buffer format must be same as src")}}const ut=(t,e)=>dt(At(t,e)),mt=(t,e)=>{const s=At(t,e),n=new pt(s.dwidth,s.dheight,t.format);for(let i of e.channels||L(t.format.channels.length))n.setChannel(i,dt(Object.assign(Object.assign({},s),{channel:i})));return n},dt=({channel:t,dest:e,dwidth:s,dheight:n,kernel:i,offsetX:r,offsetY:a,rowStride:h,scale:l,src:o,srcStride:c,strideX:f,strideY:u})=>{p(o.format,t);const m=e.pixels,d=f*c,g=u*h;for(let e=a*h,o=0,f=0;o<n;e+=g,o++)for(let n=r*c+t,a=0;a<s;n+=d,a++,f++)m[f]=i(n,e,t)*l;return e},gt=(t,s,n,i)=>(e.isFunction(s.fn)?s.fn:wt(s.spec||s.pool,n,i))(t),At=(t,e)=>{const{kernel:n,channel:i,stride:r,scale:a,offset:h}=Object.assign({channel:0,offset:0,scale:1,stride:1},e),l=n.size,[o,c]=y(l),[f,p]=y(r),[u,m]=y(h);s.assert(f>=1&&p>=1,`illegal stride: ${r}`);const{width:d,height:g,stride:A,rowStride:w}=t,x=Math.floor(d/f),R=Math.floor(g/p);s.assert(x>0&&R>0,"too large stride(s) for given image");return{channel:i,dest:new pt(x,R,F),dheight:R,dwidth:x,kernel:gt(t,n,o,c),offsetX:u,offsetY:m,rowStride:w,scale:a,src:t,srcStride:A,strideX:f,strideY:p}},wt=(t,s,n)=>{if(s*n>512&&!e.isFunction(t))return xt(t,s,n);const i=e.isFunction(t),r=[],a=[],h=[],l=n>>1,o=s>>1;for(let e=0,c=0;e<n;e++){const n=e-l,f=[];for(let a=0;a<s;a++,c++){const s=`k${e}_${a}`;h.push(s);const l=a-o,p=(0!==n?`y${e}`:"y")+(0!==l?`+x${a}`:"+x");i?f.push(`pix[${p}]`):0!==t[c]&&f.push(`${s}*pix[${p}]`),0===e&&0!==l&&r.push(l<0?`const x${a} = max(x${l<-1?l+"*":"-"}stride,channel);`:`const x${a} = min(x+${l>1?l+"*":""}stride,maxX+channel);`)}f.length&&a.push(...f),0!==n&&r.push(n<0?`const y${e} = max(y${n<-1?n+"*":"-"}rowStride,0);`:`const y${e} = min(y+${n>1?n+"*":""}rowStride,maxY);`)}const c=i?"":`const [${h.join(", ")}] = [${t.join(", ")}];`,f=i?t(a,s,n):a.join(" + "),p=[c,"const { min, max } = Math;","const { pixels: pix, stride, rowStride } = src;","const maxX = (src.width - 1) * stride;","const maxY = (src.height - 1) * rowStride;","return (x, y, channel) => {",...r,`return ${f};`,"}"].join("\n");return new Function("src",p)},xt=(t,e,s)=>i=>{const{pixels:r,rowStride:a,stride:h}=i,l=-(e>>1)*h,o=-l+(1&e?h:0),c=-(s>>1)*a,f=-c+(1&s?a:0),p=(i.width-1)*h,u=(i.height-1)*a;return(e,s,i)=>{const m=p+i;let d,g,A,w,x=0;for(d=c,A=0;d<f;d+=a)for(g=l,w=n.clamp(s+d,0,u);g<o;g+=h,A++)x+=t[A]*r[w+n.clamp(e+g,i,m)];return x}},Rt=(t,e,s)=>t[(s>>1)*e+(e>>1)],yt=(t,e,s)=>`(${t.join("+")})*${1/(e*s)}`,Lt={pool:yt,size:3},Bt={pool:yt,size:5},Gt=(t,e=2)=>{s.assert(t>0,`invalid coefficient: ${t}`);const i=Math.ceil(t*e),r=[];let a=0;for(let s=-i;s<=i;s++){const h=s/e,l=n.lanczos(t,h);for(let s=-i;s<=i;s++){const i=Math.hypot(s/e,h)<t?l*n.lanczos(t,s/e):0;r.push(i),a+=i}}return{spec:r.map((t=>t/a)),size:2*i+1}},zt=t=>t/127.5-1,Et=(t,e)=>n.clamp(127.5*t+128,0,255)<<e,bt={__float:!0,alpha:!1,gray:!1,channels:[t.Lane.RED,t.Lane.GREEN,t.Lane.BLUE],shift:{3:0,2:8,1:16},size:3,fromABGR:t=>[zt(255&t),zt(t>>8&255),zt(t>>16&255)],toABGR:t=>Et(t[0],0)|Et(t[1],8)|Et(t[2],16)|4278190080};const $t=H({type:"u8",size:8,alpha:8,channels:[{size:8,lane:0}]}),St=H({type:"u16",size:16,alpha:1,channels:[{size:1,lane:t.Lane.ALPHA},{size:5,lane:t.Lane.RED},{size:5,lane:t.Lane.GREEN},{size:5,lane:t.Lane.BLUE}]}),_t=H({type:"u16",size:16,alpha:4,channels:[{size:4,lane:t.Lane.ALPHA},{size:4,lane:t.Lane.RED},{size:4,lane:t.Lane.GREEN},{size:4,lane:t.Lane.BLUE}]}),vt=H({type:"u32",size:32,alpha:8,channels:[{size:8,lane:t.Lane.ALPHA},{size:8,lane:t.Lane.RED},{size:8,lane:t.Lane.GREEN},{size:8,lane:t.Lane.BLUE}],fromABGR:a.swapLane13,toABGR:a.swapLane13}),Pt=H({type:"u32",size:24,channels:[{size:8,lane:t.Lane.BLUE},{size:8,lane:t.Lane.GREEN},{size:8,lane:t.Lane.RED}],fromABGR:t=>16777215&t,toABGR:t=>4278190080|t}),kt=H({type:"u16",size:16,channels:[{size:16,lane:t.Lane.RED}],fromABGR:t=>257*(u(t)+.5|0),toABGR:t=>(4278190080|65793*(t>>>8))>>>0}),Dt=H({type:"u32",size:32,channels:[{size:8,lane:t.Lane.ALPHA},{size:16,lane:t.Lane.RED}],fromABGR:t=>257*(u(t)+.5|0)|257*(t>>>8&16711680),toABGR:t=>(4278190080&t|65793*(t>>>8&255))>>>0}),Ct=H({type:"u8",size:8,channels:[{size:8,lane:t.Lane.RED}],fromABGR:t=>u(t),toABGR:t=>(4278190080|65793*(255&t))>>>0}),Ot=H({type:"u16",size:16,alpha:8,channels:[{size:8,lane:t.Lane.ALPHA},{size:8,lane:t.Lane.RED}],fromABGR:t=>u(t)|t>>>16&65280,toABGR:t=>((65280&t)<<16|65793*(255&t))>>>0}),Ft=(t,e)=>{let s=0,n=1/0;for(let a=e.length;--a>=0;){const h=(i=t,r=e[a],Math.hypot((i>>16&255)-(r>>16&255),(i>>8&255)-(r>>8&255),(255&i)-(255&r)));h<n&&(s=a,n=h)}var i,r;return s},Nt=H({type:"u16",size:16,channels:[{size:5,lane:t.Lane.RED},{size:6,lane:t.Lane.GREEN},{size:5,lane:t.Lane.BLUE}]}),Ut=H({type:"u32",size:24,channels:[{size:8,lane:t.Lane.RED},{size:8,lane:t.Lane.GREEN},{size:8,lane:t.Lane.BLUE}]}),It=_({gray:!0,alpha:!0,channels:[t.Lane.RED,t.Lane.ALPHA]}),Ht=Math.abs,Mt=Math.min,jt=_({alpha:!0,channels:[t.Lane.RED,t.Lane.GREEN,t.Lane.BLUE,t.Lane.ALPHA],convert:{fromABGR:(t,e=[])=>{const s=(t>>>24&255)/255,i=(t>>>16&255)/255,r=(t>>>8&255)/255,a=(255&t)/255;let h,l,o,c,f,p,u,m;r<i?(h=i,l=r,o=-1,c=2/3):(h=r,l=i,o=0,c=-1/3),a<h?(f=h,p=l,u=c,m=a):(f=a,p=l,u=o,m=h);const d=f-Mt(p,m);return f=n.clamp01(f),e[0]=n.clamp01(Ht((m-p)/(6*d+n.EPS)+u)),e[1]=n.clamp01(d/(f+n.EPS)),e[2]=f,e[3]=s,e},toABGR:t=>{const e=6*t[0],s=t[1],i=255*t[2],r=255*t[3],a=((n.clamp01(Ht(e-3)-1)-1)*s+1)*i,h=((n.clamp01(2-Ht(e-2))-1)*s+1)*i;return(r<<24|((n.clamp01(2-Ht(e-4))-1)*s+1)*i<<16|h<<8|a)>>>0}}}),Tt=_({channels:[t.Lane.RED,t.Lane.GREEN,t.Lane.BLUE]}),Yt=_({alpha:!0,channels:[t.Lane.RED,t.Lane.GREEN,t.Lane.BLUE,t.Lane.ALPHA]});t.ABGR8888=M,t.ALPHA8=$t,t.ARGB1555=St,t.ARGB4444=_t,t.ARGB8888=vt,t.BGR888=Pt,t.BOX_BLUR3=Lt,t.BOX_BLUR5=Bt,t.FLOAT_GRAY=F,t.FLOAT_GRAY_ALPHA=It,t.FLOAT_HSVA=jt,t.FLOAT_NORMAL=bt,t.FLOAT_RGB=Tt,t.FLOAT_RGBA=Yt,t.FloatBuffer=pt,t.GAUSSIAN=t=>{t|=0,s.assert(t>0,`invalid kernel radius: ${t}`);const e=-1/(2*(Math.hypot(t,t)/3)**2),n=[];let i=0;for(let s=-t;s<=t;s++)for(let r=-t;r<=t;r++){const t=Math.exp((r*r+s*s)*e);n.push(t),i+=t}return{spec:n.map((t=>t/i)),size:2*t+1}},t.GAUSSIAN_BLUR3={spec:[1/16,1/8,1/16,1/8,1/4,1/8,1/16,1/8,1/16],size:3},t.GAUSSIAN_BLUR5={spec:[1/256,1/64,3/128,1/64,1/256,1/64,1/16,3/32,1/16,1/64,3/128,3/32,9/64,3/32,3/128,1/64,1/16,3/32,1/16,1/64,1/256,1/64,3/128,1/64,1/256],size:5},t.GRAY16=kt,t.GRAY8=Ct,t.GRAY_ALPHA16=Dt,t.GRAY_ALPHA8=Ot,t.HIGHPASS3={spec:[-1,-1,-1,-1,9,-1,-1,-1,-1],size:3},t.LANCZOS=Gt,t.POOL_MAX=t=>`Math.max(${t.join(",")})`,t.POOL_MEAN=yt,t.POOL_MIN=t=>`Math.min(${t.join(",")})`,t.POOL_NEAREST=Rt,t.POOL_THRESHOLD=(t=0)=>(e,s,n)=>`(${Rt(e,s,n)} - ${`(${e.join("+")})/${s*n}`} + ${t}) < 0 ? 0 : 1`,t.PackedBuffer=ct,t.RGB565=Nt,t.RGB888=Ut,t.SHARPEN3={spec:[0,-1,0,-1,5,-1,0,-1,0],size:3},t.SOBEL_X={spec:[-1,-2,-1,0,0,0,1,2,1],size:3},t.SOBEL_Y={spec:[-1,0,1,-2,0,2,-1,0,1],size:3},t.UNSHARP_MASK5={spec:[-1/256,-1/64,-3/128,-1/64,-1/256,-1/64,-1/16,-3/32,-1/16,-1/64,-3/128,-3/32,119/64,-3/32,-3/128,-1/64,-1/16,-3/32,-1/16,-1/64,-1/256,-1/64,-3/128,-1/64,-1/256],size:5},t.asIntVec=y,t.asVec=R,t.buffer=ot,t.canvas2d=l,t.canvasPixels=o,t.clampRegion=m,t.compileFromABGR=$,t.compileGrayFromABGR=E,t.compileGrayToABGR=b,t.compileToABGR=S,t.convolveChannel=ut,t.convolveImage=mt,t.defBayer=U,t.defFloatFormat=_,t.defIndexed=(e,n=!1)=>{const i=e.length;return s.assert(i>0&&i<=256,`invalid palette size: ${i}`),e=n?e:e.map(a.swapLane13),H({type:"u8",size:8,channels:[{size:8,lane:t.Lane.RED}],fromABGR:t=>Ft(t,e),toABGR:t=>e[t]})},t.defKernel=wt,t.defLargeKernel=xt,t.defPackedFormat=H,t.defSampler=j,t.ditherPixels=(t,e,s,n,i,r,a,h)=>{!t&&(t=e.slice()),a--;for(let l=0,o=0;l<n;l++)for(let n=0;n<s;n++)t[o]=I(i,r,a,h,n,l,e[o]),o++;return t},t.dominantColors=(t,e,s)=>{const n=t.width*t.height,i=[];for(let e=0,s=0,r=t.stride;e<n;e++,s+=r)i.push(t.pixels.subarray(s,s+r));return r.kmeans(e,i,s).sort(((t,e)=>e.items.length-t.items.length)).map((t=>({color:[...t.centroid],area:t.items.length/n})))},t.ensureChannel=p,t.ensureSize=f,t.floatBuffer=ft,t.imageCanvas=c,t.imagePromise=async t=>{const e=new Image;return e.src=t,await e.decode(),e},t.imagePyramid=function*(t,e=Gt(2),n=1,i=!0){for(s.assert(n>0,"invalid min size"),n<<=1,i&&(yield t);t.width>=n&&t.height>=n;)t=mt(t,{kernel:e,stride:2}),yield t},t.luminanceABGR=u,t.normalMap=(t,e)=>{const{channel:s,step:n,scale:i,z:r}=Object.assign({channel:0,step:0,scale:1,z:1},e);p(t.format,s);const a=[-1,...new Array(n).fill(0),1],[h,l]=R(i),o=new pt(t.width,t.height,bt);return o.setChannel(0,ut(t,{kernel:{spec:a,size:[n+2,1]},scale:h,channel:s})),o.setChannel(1,ut(t,{kernel:{spec:a,size:[1,n+2]},scale:l,channel:s})),o.setChannel(2,r),o},t.orderedDither=I,t.packedBuffer=lt,t.prepRegions=d,t.range=L,t.setChannelConvert=w,t.setChannelSame=A,t.setChannelUni=g,t.transformABGR=x,Object.defineProperty(t,"__esModule",{value:!0})})); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@thi.ng/checks"),require("@thi.ng/api"),require("@thi.ng/math"),require("@thi.ng/porter-duff"),require("@thi.ng/k-means"),require("@thi.ng/binary")):"function"==typeof define&&define.amd?define(["exports","@thi.ng/checks","@thi.ng/api","@thi.ng/math","@thi.ng/porter-duff","@thi.ng/k-means","@thi.ng/binary"],e):e(((t="undefined"!=typeof globalThis?globalThis:t||self).thi=t.thi||{},t.thi.ng=t.thi.ng||{},t.thi.ng.pixel={}),t.thi.ng.checks,t.thi.ng.api,t.thi.ng.math,t.thi.ng.porterDuff,t.thi.ng.kMeans,t.thi.ng.binary)}(this,(function(t,e,s,n,i,r,a){"use strict";var h;t.Lane=void 0,(h=t.Lane||(t.Lane={}))[h.ALPHA=0]="ALPHA",h[h.BLUE=1]="BLUE",h[h.GREEN=2]="GREEN",h[h.RED=3]="RED";const l=(t,e=t,s)=>{const n=document.createElement("canvas");return n.width=t,n.height=e,s&&s.appendChild(n),{canvas:n,ctx:n.getContext("2d")}};function o(t,s){let n,i;if(e.isNumber(t)){const e=l(t,s);n=e.canvas,i=e.ctx}else n=t,i=n.getContext("2d");const r=i.getImageData(0,0,n.width,n.height);return{canvas:n,ctx:i,img:r,pixels:new Uint32Array(r.data.buffer)}}const c=(t,s,n=s,i)=>{const r=e.isNumber(s)?l(s,n,i):l(t.width,t.height,i);return r.ctx.drawImage(t,0,0,r.canvas.width,r.canvas.height),r},f=(t,e,n,i=1)=>s.assert(t.length>=e*n*i,"pixel buffer too small"),p=(t,e)=>{const n=t.channels[e];return s.assert(null!=n,`invalid channel ID: ${e}`),n},u=t=>(29*(t>>>16&255)+150*(t>>>8&255)+76*(255&t))/255,m=(t,e,s,i,r,a,h=0,l=0)=>(s|=0,i|=0,(t|=0)<0&&(s+=t,h-=t,t=0),(e|=0)<0&&(i+=e,l-=e,e=0),[t,e,n.clamp(s,0,r-t),n.clamp(i,0,a-e),h,l]),d=(t,e,s={})=>{let n,i,r,a,h,l,o=t.width,c=e.width;return[n,i,h,l]=m(s.sx||0,s.sy||0,s.w||o,s.h||t.height,o,t.height),[r,a,h,l,n,i]=m(s.dx||0,s.dy||0,h,l,c,e.height,n,i),{sx:n,sy:i,dx:r,dy:a,rw:h,rh:l}},g=(t,e,s)=>{for(let n=t.length;--n>=0;)t[n]=s(t[n],e)},A=(t,e,s,n)=>{for(let i=t.length;--i>=0;)t[i]=n(t[i],s(e[i]))},w=(t,e,s,n,i)=>{const r=~i;for(let a=t.length;--a>=0;)t[a]=t[a]&r|s(n(e[a]))&i},R=(t,e,s)=>{const n=e.fromABGR,i=e.toABGR;for(let e=t.length;--e>=0;)t[e]=n(s(i(t[e])))},x=t=>e.isNumber(t)?[t,t]:t,y=t=>{const e=x(t);return[0|e[0],0|e[1]]},L=t=>new Uint8Array(t).map(((t,e)=>e)),B=(t,e)=>e>0?`(${t} << ${e})`:e<0?`(${t} >>> ${-e})`:`${t}`,G=(t,e)=>B(t,-e),z=t=>`0x${t.toString(16)}`,E=t=>{const e=(1<<t)-1;return new Function("luma",`return (x) => ${G("luma(x)",8-t)} & ${e};`)(u)},b=t=>{let e;if(8!==t){const s=(1<<t)-1;e=`(((x & ${s}) * ${255/s}) | 0)`}else e="x";return new Function("x",`return 0xff000000 | (${e} * 0x010101);`)},$=t=>new Function("x","return ("+t.map((t=>{const e=t.abgrShift+(8-t.size);return`(${G("x",e)} & ${z(t.maskA)})`})).join(" | ")+") >>> 0;"),S=(t,e)=>{const s=t.map((t=>{if(8!==t.size){const e=t.mask0,s=255/e,n=G("x",t.shift);return B(`((${n} & ${e}) * ${s})`,24-8*t.lane)}return B(`(x & ${z(t.maskA)})`,t.abgrShift)})).join(" | ");return new Function("x",`return (${e?"":"0xff000000 | "}${s}) >>> 0;`)},_=t=>{const e=t.channels,s=e.reduce(((t,e)=>(t[e]=3-e<<3,t)),{}),i=Object.assign(Object.assign({},t),{size:e.length,shift:s,__float:!0});if(t.convert)return Object.assign(i,t.convert),i;const r=(t,i)=>(255*n.clamp01(t[i])+.5|0)<<s[e[i]],a=(t,n)=>(t>>>s[e[n]]&255)/255;switch(e.length){case 1:t.gray?P(i):v(i,a,r);break;case 2:t.gray?D(i,a):k(i,a,r);break;case 3:C(i,a,r);break;case 4:O(i,a,r)}return i},v=(t,e,s)=>{t.toABGR=e=>{let n=t.alpha?0:4278190080;return n|=s(e,0),n>>>0},t.fromABGR=(t,s=[])=>(s[0]=e(t,0),s)},P=t=>{t.toABGR=t=>(65793*(255*n.clamp01(t[0])+.5|0)|4278190080)>>>0,t.fromABGR=(t,e=[])=>(e[0]=u(t)/255,e)},k=(t,e,s)=>{t.toABGR=e=>{let n=t.alpha?0:4278190080;return n|=s(e,0),n|=s(e,1),n>>>0},t.fromABGR=(t,s=[])=>(s[0]=e(t,0),s[1]=e(t,1),s)},D=(e,s)=>{const i=~~(e.channels[0]===t.Lane.ALPHA),r=1^i;e.toABGR=t=>{let e=65793*(255*n.clamp01(t[i])+.5|0);return e|=(255*t[r]+.5|0)<<24,e>>>0},e.fromABGR=(t,e=[])=>(e[i]=u(t)/255,e[r]=s(t,r),e)},C=(t,e,s)=>{t.toABGR=e=>{let n=t.alpha?0:4278190080;return n|=s(e,0),n|=s(e,1),n|=s(e,2),n>>>0},t.fromABGR=(t,s=[])=>(s[0]=e(t,0),s[1]=e(t,1),s[2]=e(t,2),s)},O=(t,e,s)=>{t.toABGR=e=>{let n=t.alpha?0:4278190080;return n|=s(e,0),n|=s(e,1),n|=s(e,2),n|=s(e,3),n>>>0},t.fromABGR=(t,s=[])=>(s[0]=e(t,0),s[1]=e(t,1),s[2]=e(t,2),s[3]=e(t,3),s)},F=_({gray:!0,channels:[t.Lane.RED]}),N=(t,e,s,n,i,r)=>{if(1===s)return!r[e]&&(r[e]=[]),r[e][t]=n,r;const a=i<<2;return N(t,e,s>>=1,n,a,r),N(t+s,e+s,s,n+i,a,r),N(t+s,e,s,n+2*i,a,r),N(t,e+s,s,n+3*i,a,r),r},U=t=>({mat:N(0,0,t,0,1,[]),invSize:1/(t*t),mask:t-1}),I=({mat:t,mask:e,invSize:s},i,r,a,h,l,o)=>(o=i*(o/a)+t[l&e][h&e]*s-.5|0,i--,n.clamp(o,0,i)*((r-1)/i)),H=t=>{s.assert(t.channels.length>0,"no channel specs given");const e=t.channels.reduce((([t,e],s,i)=>(e-=s.size,t.push(((t,e,s)=>{const i=1<<t.size,r=i-1,a=r<<s>>>0,h=~a>>>0,l=null!=t.lane?t.lane:e,o=t=>t>>>s&r,c=(t,e)=>t&h|(e&r)<<s;return{size:t.size,abgrShift:24-8*l-s,lane:l,shift:s,mask0:r,maskA:a,int:o,setInt:c,float:t=>o(t)/r,setFloat:(t,e)=>c(t,n.clamp01(e)*r),dither:(t,e,s,n,r)=>I(t,e,i,i,s,n,r)}})(s,i,e)),[t,e])),[[],t.size])[0];return{__packed:!0,type:t.type,size:t.size,alpha:t.alpha||0,channels:e,fromABGR:t.fromABGR||$(e),toABGR:t.toABGR||S(e,!!t.alpha)}},M=H({type:"u32",size:32,alpha:8,channels:[{size:8,lane:t.Lane.ALPHA},{size:8,lane:t.Lane.BLUE},{size:8,lane:t.Lane.GREEN},{size:8,lane:t.Lane.RED}],fromABGR:t=>t,toABGR:t=>t});function j(t,e="linear",n="clamp"){const i=!!t.format.__float,r=1===t.format.channels.length?"1":"",a=`${e[0]}${n[0]}${r}`,h=(i?{nc1:q,nw1:K,nr1:V,nc:q,nw:K,nr:V,lc1:t=>J(T(t)),lw1:t=>J(Y(t)),lr1:t=>J(X(t)),lc:t=>tt(t,q(t)),lw:t=>tt(t,K(t)),lr:t=>tt(t,V(t)),cc1:t=>nt(T(t)),cw1:t=>nt(Y(t)),cr1:t=>nt(X(t)),cc:t=>ht(t,q(t)),cw:t=>ht(t,K(t)),cr:t=>ht(t,V(t))}:{nc1:T,nw1:Y,nr1:X,nc:T,nw:Y,nr:X,lc1:t=>Z(T(t)),lw1:t=>Z(Y(t)),lr1:t=>Z(X(t)),lc:t=>Q(t,T(t)),lw:t=>Q(t,Y(t)),lr:t=>Q(t,X(t)),cc1:t=>st(t,T(t)),cw1:t=>st(t,Y(t)),cr1:t=>st(t,X(t)),cc:t=>at(t,T(t)),cw:t=>at(t,Y(t)),cr:t=>at(t,X(t))})[a];return s.assert(!!h,`missing impl for ${a}`),h(t)}const T=({pixels:t,width:e,height:s})=>(n,i)=>n>=0&&n<e&&i>=0&&i<s?t[(0|i)*e+(0|n)]:0,Y=({pixels:t,width:e,height:s})=>(i,r)=>t[n.mod(0|r,s)*e+n.mod(0|i,e)],X=({pixels:t,width:e,height:s})=>{const i=e-1,r=s-1;return(s,a)=>t[n.clamp(0|a,0,r)*e+n.clamp(0|s,0,i)]},q=({pixels:t,width:e,height:s,rowStride:n,stride:i})=>(r,a)=>{let h;return r>=0&&r<e&&a>=0&&a<s?(h=(0|a)*n+(0|r)*i,t.slice(h,h+i)):[0]},K=({pixels:t,width:e,height:s,rowStride:i,stride:r})=>(a,h)=>{let l=n.mod(0|h,s)*i+n.mod(0|a,e)*r;return t.slice(l,l+r)},V=({pixels:t,width:e,height:s,rowStride:i,stride:r})=>{const a=e-1,h=s-1;return(e,s)=>{let l=n.clamp(0|s,0,h)*i+n.clamp(0|e,0,a)*r;return t.slice(l,l+r)}},W=(t,e,s,i,r=4)=>n.mixBilinear(t[i],t[i+r],t[i+2*r],t[i+3*r],e,s),Z=t=>(e,s)=>(e-=.5,s-=.5,n.mixBilinear(t(e,s),t(e+1,s),t(e,s+1),t(e+1,s+1),n.fract(e),n.fract(s))),J=t=>(t=Z(t),(e,s)=>[t(e,s)]),Q=(t,e)=>{const{fromABGR:s,toABGR:i}=t.format,r=new Uint32Array(4),a=new Uint8Array(r.buffer);return(t,h)=>{t-=.5,h-=.5,r[0]=i(e(t,h)),r[1]=i(e(t+1,h)),r[2]=i(e(t,h+1)),r[3]=i(e(t+1,h+1));const l=n.fract(t),o=n.fract(h);return s(W(a,l,o,0)|W(a,l,o,1)<<8|W(a,l,o,2)<<16|W(a,l,o,3)<<24)>>>0}},tt=({stride:t},e)=>{const s=new Float32Array(4*t);return(i,r)=>{i-=.5,r-=.5,s.set(e(i,r),0),s.set(e(i+1,r),t),s.set(e(i,r+1),2*t),s.set(e(i+1,r+1),3*t);const a=n.fract(i),h=n.fract(r);let l=[];for(let e=0;e<t;e++)l.push(W(s,a,h,e,t));return l}},et=t=>(e,s)=>{const i=(e-=.5)-1,r=e+1,a=e+2,h=(s-=.5)-1,l=s+1,o=s+2;return n.mixBicubic(t(i,h),t(e,h),t(r,h),t(a,h),t(i,s),t(e,s),t(r,s),t(a,s),t(i,l),t(e,l),t(r,l),t(a,l),t(i,o),t(e,o),t(r,o),t(a,o),n.fract(e),n.fract(s))},st=(t,e)=>{const s=t.format.channels[0].mask0;return e=et(e),(t,i)=>n.clamp(e(t,i),0,s)},nt=t=>(t=et(t),(e,s)=>[t(e,s)]),it=(t,e,s,i,r=4)=>n.mixBicubic(t[i],t[i+r],t[i+2*r],t[i+3*r],t[i+4*r],t[i+5*r],t[i+6*r],t[i+7*r],t[i+8*r],t[i+9*r],t[i+10*r],t[i+11*r],t[i+12*r],t[i+13*r],t[i+14*r],t[i+15*r],e,s),rt=(t,e,s,i,r=4)=>n.clamp(it(t,e,s,i,r),0,255),at=(t,e)=>{const{fromABGR:s,toABGR:i}=t.format,r=new Uint32Array(16),a=new Uint8Array(r.buffer);return(t,h)=>{const l=(t-=.5)-1,o=t+1,c=t+2,f=(h-=.5)-1,p=h+1,u=h+2,m=n.fract(t),d=n.fract(h);return r[0]=i(e(l,f)),r[1]=i(e(t,f)),r[2]=i(e(o,f)),r[3]=i(e(c,f)),r[4]=i(e(l,h)),r[5]=i(e(t,h)),r[6]=i(e(o,h)),r[7]=i(e(c,h)),r[8]=i(e(l,p)),r[9]=i(e(t,p)),r[10]=i(e(o,p)),r[11]=i(e(c,p)),r[12]=i(e(l,u)),r[13]=i(e(t,u)),r[14]=i(e(o,u)),r[15]=i(e(c,u)),s(rt(a,m,d,0)|rt(a,m,d,1)<<8|rt(a,m,d,2)<<16|rt(a,m,d,3)<<24)>>>0}},ht=({stride:t},e)=>{const s=new Float32Array(16*t);return(i,r)=>{const a=(i-=.5)-1,h=i+1,l=i+2,o=(r-=.5)-1,c=r+1,f=r+2,p=n.fract(i),u=n.fract(r);s.set(e(a,o),0),s.set(e(i,o),t),s.set(e(h,o),2*t),s.set(e(l,o),3*t),s.set(e(a,r),4*t),s.set(e(i,r),5*t),s.set(e(h,r),6*t),s.set(e(l,r),7*t),s.set(e(a,c),8*t),s.set(e(i,c),9*t),s.set(e(h,c),10*t),s.set(e(l,c),11*t),s.set(e(a,f),12*t),s.set(e(i,f),13*t),s.set(e(h,f),14*t),s.set(e(l,f),15*t);let m=[];for(let e=0;e<t;e++)m.push(it(s,p,u,e,t));return m}};function lt(...t){return t[0]instanceof ct?t[0].as(t[1]):new ct(...t)}const ot=lt;class ct{constructor(t,e,n,i){this.width=t,this.height=e,this.format=n.__packed?n:H(n),this.pixels=i||s.typedArray(n.type,t*e)}static fromImage(t,e,s,n=s){return ct.fromCanvas(c(t,s,n).canvas,e)}static fromCanvas(t,e=M){const n=o(t),i=t.width,r=t.height;let a;if(e===M)a=n.pixels;else{a=s.typedArray(e.type,i*r);const t=n.pixels,h=e.fromABGR;for(let e=a.length;--e>=0;)a[e]=h(t[e])}return new ct(i,r,e,a)}get stride(){return 1}as(t){return this.getRegion(0,0,this.width,this.height,t)}copy(){const t=this.empty();return t.pixels.set(this.pixels),t}empty(){return new ct(this.width,this.height,this.format)}getAt(t,e){return t>=0&&t<this.width&&e>=0&&e<this.height?this.pixels[(0|t)+(0|e)*this.width]:0}setAt(t,e,s){return t>=0&&t<this.width&&e>=0&&e<this.height&&(this.pixels[(0|t)+(0|e)*this.width]=s),this}getChannelAt(t,e,s,n=!1){const i=p(this.format,s),r=this.getAt(t,e);return n?i.float(r):i.int(r)}setChannelAt(t,e,s,n,i=!1){const r=p(this.format,s),a=this.getAt(t,e);return i?r.setFloat(a,n):r.setInt(a,n),this}blend(t,e,s){let n=this.width,i=e.width;const{sx:r,sy:a,dx:h,dy:l,rw:o,rh:c}=d(this,e,s);if(o<1||c<1)return e;const f=this.pixels,p=e.pixels,u=this.format.toABGR,m=e.format.toABGR,g=e.format.fromABGR;for(let e=(0|r)+(0|a)*n,s=(0|h)+(0|l)*i,d=0;d<c;d++,e+=n,s+=i)for(let n=0;n<o;n++)p[s+n]=g(t(u(f[e+n]),m(p[s+n])));return e}blit(t,e){let s=this.width,n=t.width;const{sx:i,sy:r,dx:a,dy:h,rw:l,rh:o}=d(this,t,e);if(l<1||o<1)return t;const c=this.pixels,f=t.pixels,p=this.format.toABGR,u=t.format.fromABGR,m=this.format!==t.format?(t,e)=>{for(let s=0;s<l;s++)f[e+s]=u(p(c[t+s]))}:(t,e)=>f.set(c.subarray(t,t+l),e);for(let t=(0|i)+(0|r)*s,e=(0|a)+(0|h)*n,l=0;l<o;l++,t+=s,e+=n)m(t,e);return t}blitCanvas(t,e=0,s=0){(t instanceof HTMLCanvasElement?t.getContext("2d"):t).putImageData(this.toImageData(),e,s)}toImageData(){const t=new ImageData(this.width,this.height),e=new Uint32Array(t.data.buffer),s=this.pixels,n=this.format.toABGR;for(let t=e.length;--t>=0;)e[t]=n(s[t]);return t}getRegion(t,e,s,n,i){const[r,a,h,l]=m(t,e,s,n,this.width,this.height);return this.blit(new ct(h,l,i||this.format),{sx:r,sy:a,w:h,h:l})}getChannel(e){const n=p(this.format,e),i=new ct(this.width,this.height,{type:s.uintTypeForBits(n.size),size:n.size,channels:[{size:n.size,lane:t.Lane.RED}],fromABGR:E(n.size),toABGR:b(n.size)}),r=this.pixels,a=i.pixels,h=n.int;for(let t=r.length;--t>=0;)a[t]=h(r[t]);return i}setChannel(t,s){const n=p(this.format,t),i=this.pixels,r=n.setInt;if(e.isNumber(s))g(i,s,r);else{const t=s.pixels,e=s.format.channels[0];f(t,this.width,this.height),n.size===e.size?A(i,t,e.int,r):w(i,t,this.format.fromABGR,s.format.toABGR,n.maskA)}return this}invert(){const{format:t,pixels:e}=this,s=Math.pow(2,t.size-t.alpha)-1;for(let t=e.length;--t>=0;)e[t]^=s;return this}premultiply(){return R(this.pixels,this.format,i.premultiplyInt),this}postmultiply(){return R(this.pixels,this.format,i.postmultiplyInt),this}isPremultiplied(){const t=this.pixels,e=this.format.toABGR;for(let s=t.length;--s>=0;)if(!i.isPremultipliedInt(e(t[s])))return!1;return!0}forEach(t){const e=this.pixels;for(let s=e.length;--s>=0;)e[s]=t(e[s],s);return this}dither(t,s){const{pixels:n,format:i,width:r}=this,a=e.isNumber(s)?new Array(i.channels.length).fill(s):s,h=e.isNumber(t)?U(t):t;for(let t=0,e=n.length,s=i.channels.length,l=0,o=0;t<e;t++){let e=n[t];for(let t=0;t<s;t++){const s=i.channels[t],n=a[t];n>0&&(e=s.setInt(e,s.dither(h,n,l,o,s.int(e))))}n[t]=e,++l===r&&(l=0,o++)}return this}flipY(){const{pixels:t,width:e}=this,n=s.typedArray(this.format.type,e);for(let s=0,i=t.length-e;s<i;s+=e,i-=e)n.set(t.subarray(s,s+e)),t.copyWithin(s,i,i+e),t.set(n,i);return this}scale(t,e="linear"){return s.assert(t>0,"scale must be > 0"),this.resize(this.width*t,this.height*t,e)}resize(t,n,i="linear"){t|=0,n|=0,s.assert(t>0&&n>0,"target width & height must be > 0");const r=lt(t,n,this.format),a=r.pixels,h=t>0?this.width/t:0,l=n>0?this.height/n:0;i=e.isString(i)?j(this,i,"repeat"):i;for(let e=0,s=0;e<n;e++){const n=e*l;for(let e=0;e<t;e++,s++)a[s]=i(e*h,n)}return r}upsize(){const{width:t,height:e,pixels:s}=this,n=new ct(2*t,2*e,this.format),i=n.pixels;for(let n=0,r=0;n<e;n++)for(let e=0,a=n*t*4;e<t;e++,r++,a+=2)i[a]=s[r];return n}}function ft(...t){return t[0]instanceof ct?pt.fromPacked(...t):new pt(...t)}class pt{constructor(t,e,s,n){this.width=t,this.height=e,this.format=s.__float?s:_(s),this.stride=s.channels.length,this.rowStride=t*this.stride,this.pixels=n||new Float32Array(t*e*this.stride),this.__empty=Object.freeze(new Array(this.stride).fill(0))}static fromPacked(t,e){const s=new pt(t.width,t.height,e),{pixels:n,format:i,stride:r}=s,{pixels:a,format:h}=t;for(let t=a.length;--t>=0;)n.set(i.fromABGR(h.toABGR(a[t])),t*r);return s}as(t){const{width:e,height:s,stride:n,pixels:i,format:r}=this,a=new ct(e,s,t),h=a.pixels;for(let e=0,s=0,a=i.length;e<a;e+=n,s++)h[s]=t.fromABGR(r.toABGR(i.subarray(e,e+n)));return a}copy(){const t=this.empty();return t.pixels.set(this.pixels),t}empty(){return new pt(this.width,this.height,this.format)}getAt(t,e){const{width:s,stride:n}=this;if(t>=0&&t<s&&e>=0&&e<this.height){const s=(0|t)*n+(0|e)*this.rowStride;return this.pixels.subarray(s,s+n)}return this.__empty}setAt(t,e,s){return t>=0&&t<this.width&&e>=0&&e<this.height&&this.pixels.set(s,(0|t)*this.stride+(0|e)*this.rowStride),this}getChannelAt(t,e,s){p(this.format,s);const{width:n,stride:i}=this;if(t>=0&&t<n&&e>=0&&e<this.height)return this.pixels[(0|t)*i+(0|e)*this.rowStride+s]}setChannelAt(t,e,s,n){p(this.format,s);const{width:i,stride:r}=this;return t>=0&&t<i&&e>=0&&e<this.height&&(this.pixels[(0|t)*r+(0|e)*this.rowStride+s]=n),this}getChannel(t){p(this.format,t);const{pixels:e,stride:s}=this,i=new Float32Array(this.width*this.height);for(let r=t,a=0,h=e.length;r<h;r+=s,a++)i[a]=n.clamp01(e[r]);return new pt(this.width,this.height,F,i)}setChannel(t,s){p(this.format,t);const{pixels:n,stride:i}=this;if(e.isNumber(s))for(let e=t,r=n.length;e<r;e+=i)n[e]=s;else{const{pixels:e,stride:r}=s;f(e,this.width,this.height,r);for(let s=t,a=0,h=n.length;s<h;s+=i,a+=r)n[s]=e[a]}return this}blend(t,e,s){this.ensureFormat(e);const{sx:n,sy:i,dx:r,dy:a,rw:h,rh:l}=d(this,e,s);if(h<1||l<1)return e;const o=this.pixels,c=e.pixels,f=this.rowStride,p=e.rowStride,u=this.stride;for(let e=(0|n)*u+(0|i)*f,s=(0|r)*u+(0|a)*p,m=0;m<l;m++,e+=f,s+=p)for(let n=h,i=e,r=s;--n>=0;i+=u,r+=u){const e=c.subarray(r,r+u);t(e,o.subarray(i,i+u),e)}return e}blit(t,e){this.ensureFormat(t);const{sx:s,sy:n,dx:i,dy:r,rw:a,rh:h}=d(this,t,e);if(a<1||h<1)return t;const l=this.pixels,o=t.pixels,c=this.rowStride,f=t.rowStride,p=a*this.stride;for(let t=(0|s)*this.stride+(0|n)*c,e=(0|i)*this.stride+(0|r)*f,a=0;a<h;a++,t+=c,e+=f)o.set(l.subarray(t,t+p),e);return t}blitCanvas(t,e=0,s=0){(t instanceof HTMLCanvasElement?t.getContext("2d"):t).putImageData(this.toImageData(),e,s)}toImageData(){const t=new ImageData(this.width,this.height),e=new Uint32Array(t.data.buffer),{stride:s,pixels:n,format:i}=this;for(let t=0,r=0,a=n.length;t<a;t+=s,r++)e[r]=i.toABGR(n.subarray(t,t+s));return t}getRegion(t,e,s,n){const[i,r,a,h]=m(t,e,s,n,this.width,this.height);return this.blit(new pt(a,h,this.format),{sx:i,sy:r,w:a,h})}forEach(t){const{pixels:e,stride:s}=this;for(let n=0,i=0,r=e.length;n<r;n+=s,i++)e.set(t(e.subarray(n,n+s),i),n);return this}clamp(){const t=this.pixels;for(let e=t.length;--e>=0;)t[e]=n.clamp01(t[e]);return this}clampChannel(t){p(this.format,t);const{pixels:e,stride:s}=this;for(let i=t,r=e.length;i<r;i+=s)e[i]=n.clamp01(e[i])}flipY(){const{pixels:t,rowStride:e}=this,s=new Float32Array(e);for(let n=0,i=t.length-e;n<i;n+=e,i-=e)s.set(t.subarray(n,n+e)),t.copyWithin(n,i,i+e),t.set(s,i);return this}invert(){const{pixels:t,format:e,stride:s}=this;for(let n=0,i=t.length,r=e.alpha?s-1:s;n<i;n+=s)for(let e=0;e<r;e++)t[n+e]=1-t[n+e];return this}scale(t,e){return s.assert(t>0,"scale must be > 0"),this.resize(this.width*t,this.height*t,e)}resize(t,n,i="linear"){t|=0,n|=0,s.assert(t>0&&n>0,"target width & height must be > 0");const r=ft(t,n,this.format),a=r.pixels,h=t>0?this.width/t:0,l=n>0?this.height/n:0,o=this.stride;i=e.isString(i)?j(this,i,"repeat"):i;for(let e=0,s=0;e<n;e++){const n=e*l;for(let e=0;e<t;e++,s+=o)a.set(i(e*h,n),s)}return r}upsize(){const{width:t,height:e,pixels:s,stride:n,rowStride:i}=this,r=2*n,a=ft(2*t,2*e,this.format),h=a.pixels;for(let a=0,l=0;a<e;a++)for(let e=0,o=a*i*4;e<t;e++,l+=n,o+=r)h.set(s.subarray(l,l+n),o);return a}ensureFormat(t){s.assert(t.format===this.format,"dest buffer format must be same as src")}}const ut=(t,e)=>dt(At(t,e)),mt=(t,e)=>{const s=At(t,e),n=new pt(s.dwidth,s.dheight,t.format);for(let i of e.channels||L(t.format.channels.length))n.setChannel(i,dt(Object.assign(Object.assign({},s),{channel:i})));return n},dt=({channel:t,dest:e,dwidth:s,dheight:n,kernel:i,offsetX:r,offsetY:a,rowStride:h,scale:l,src:o,srcStride:c,strideX:f,strideY:u})=>{p(o.format,t);const m=e.pixels,d=f*c,g=u*h;for(let e=a*h,o=0,f=0;o<n;e+=g,o++)for(let n=r*c+t,a=0;a<s;n+=d,a++,f++)m[f]=i(n,e,t)*l;return e},gt=(t,s,n,i)=>(e.isFunction(s.fn)?s.fn:Rt(s.spec||s.pool,n,i))(t),At=(t,e)=>{const{kernel:n,channel:i,stride:r,scale:a,offset:h}=Object.assign({channel:0,offset:0,scale:1,stride:1},e),l=n.size,[o,c]=y(l),[f,p]=y(r),[u,m]=y(h);s.assert(f>=1&&p>=1,`illegal stride: ${r}`);const{width:d,height:g,stride:A,rowStride:w}=t,R=Math.floor(d/f),x=Math.floor(g/p);s.assert(R>0&&x>0,"too large stride(s) for given image");return{channel:i,dest:new pt(R,x,F),dheight:x,dwidth:R,kernel:gt(t,n,o,c),offsetX:u,offsetY:m,rowStride:w,scale:a,src:t,srcStride:A,strideX:f,strideY:p}},wt=(t,e,s,n,i,r)=>t<0?`const ${s}${e} = max(${s}${t<-1?t+"*":"-"}${n},${i});`:`const ${s}${e} = min(${s}+${t>1?t+"*":""}${n},${r});`,Rt=(t,s,n)=>{if(s*n>512&&!e.isFunction(t))return xt(t,s,n);const i=e.isFunction(t),r=[],a=[],h=[],l=n>>1,o=s>>1;for(let e=0,c=0;e<n;e++){const n=e-l,f=[];for(let a=0;a<s;a++,c++){const s=`k${e}_${a}`;h.push(s);const l=a-o,p=(0!==n?`y${e}`:"y")+(0!==l?`+x${a}`:"+x");i?f.push(`pix[${p}]`):0!==t[c]&&f.push(`${s}*pix[${p}]`),0===e&&0!==l&&r.push(wt(l,a,"x","stride","channel","maxX+channel"))}f.length&&a.push(...f),0!==n&&r.push(wt(n,e,"y","rowStride","0","maxY"))}const c=i?"":`const [${h.join(", ")}] = [${t.join(", ")}];`,f=i?t(a,s,n):a.join(" + "),p=[c,"const { min, max } = Math;","const { pixels: pix, stride, rowStride } = src;","const maxX = (src.width - 1) * stride;","const maxY = (src.height - 1) * rowStride;","return (x, y, channel) => {",...r,`return ${f};`,"}"].join("\n");return new Function("src",p)},xt=(t,e,s)=>i=>{const{pixels:r,rowStride:a,stride:h}=i,l=-(e>>1)*h,o=-l+(1&e?h:0),c=-(s>>1)*a,f=-c+(1&s?a:0),p=(i.width-1)*h,u=(i.height-1)*a;return(e,s,i)=>{const m=p+i;let d,g,A,w,R=0;for(d=c,A=0;d<f;d+=a)for(g=l,w=n.clamp(s+d,0,u);g<o;g+=h,A++)R+=t[A]*r[w+n.clamp(e+g,i,m)];return R}},yt=(t,e,s)=>t[(s>>1)*e+(e>>1)],Lt=(t,e,s)=>`(${t.join("+")})*${1/(e*s)}`,Bt={pool:Lt,size:3},Gt={pool:Lt,size:5},zt=(t,e=2)=>{s.assert(t>0,`invalid coefficient: ${t}`);const i=Math.ceil(t*e),r=[];let a=0;for(let s=-i;s<=i;s++){const h=s/e,l=n.lanczos(t,h);for(let s=-i;s<=i;s++){const i=Math.hypot(s/e,h)<t?l*n.lanczos(t,s/e):0;r.push(i),a+=i}}return{spec:r.map((t=>t/a)),size:2*i+1}},Et=t=>t/127.5-1,bt=(t,e)=>n.clamp(127.5*t+128,0,255)<<e,$t={__float:!0,alpha:!1,gray:!1,channels:[t.Lane.RED,t.Lane.GREEN,t.Lane.BLUE],shift:{3:0,2:8,1:16},size:3,fromABGR:t=>[Et(255&t),Et(t>>8&255),Et(t>>16&255)],toABGR:t=>bt(t[0],0)|bt(t[1],8)|bt(t[2],16)|4278190080};const St=H({type:"u8",size:8,alpha:8,channels:[{size:8,lane:0}]}),_t=H({type:"u16",size:16,alpha:1,channels:[{size:1,lane:t.Lane.ALPHA},{size:5,lane:t.Lane.RED},{size:5,lane:t.Lane.GREEN},{size:5,lane:t.Lane.BLUE}]}),vt=H({type:"u16",size:16,alpha:4,channels:[{size:4,lane:t.Lane.ALPHA},{size:4,lane:t.Lane.RED},{size:4,lane:t.Lane.GREEN},{size:4,lane:t.Lane.BLUE}]}),Pt=H({type:"u32",size:32,alpha:8,channels:[{size:8,lane:t.Lane.ALPHA},{size:8,lane:t.Lane.RED},{size:8,lane:t.Lane.GREEN},{size:8,lane:t.Lane.BLUE}],fromABGR:a.swapLane13,toABGR:a.swapLane13}),kt=H({type:"u32",size:24,channels:[{size:8,lane:t.Lane.BLUE},{size:8,lane:t.Lane.GREEN},{size:8,lane:t.Lane.RED}],fromABGR:t=>16777215&t,toABGR:t=>4278190080|t}),Dt=H({type:"u16",size:16,channels:[{size:16,lane:t.Lane.RED}],fromABGR:t=>257*(u(t)+.5|0),toABGR:t=>(4278190080|65793*(t>>>8))>>>0}),Ct=H({type:"u32",size:32,channels:[{size:8,lane:t.Lane.ALPHA},{size:16,lane:t.Lane.RED}],fromABGR:t=>257*(u(t)+.5|0)|257*(t>>>8&16711680),toABGR:t=>(4278190080&t|65793*(t>>>8&255))>>>0}),Ot=H({type:"u8",size:8,channels:[{size:8,lane:t.Lane.RED}],fromABGR:t=>u(t),toABGR:t=>(4278190080|65793*(255&t))>>>0}),Ft=H({type:"u16",size:16,alpha:8,channels:[{size:8,lane:t.Lane.ALPHA},{size:8,lane:t.Lane.RED}],fromABGR:t=>u(t)|t>>>16&65280,toABGR:t=>((65280&t)<<16|65793*(255&t))>>>0}),Nt=(t,e)=>{let s=0,n=1/0;for(let a=e.length;--a>=0;){const h=(i=t,r=e[a],Math.hypot((i>>16&255)-(r>>16&255),(i>>8&255)-(r>>8&255),(255&i)-(255&r)));h<n&&(s=a,n=h)}var i,r;return s},Ut=H({type:"u16",size:16,channels:[{size:5,lane:t.Lane.RED},{size:6,lane:t.Lane.GREEN},{size:5,lane:t.Lane.BLUE}]}),It=H({type:"u32",size:24,channels:[{size:8,lane:t.Lane.RED},{size:8,lane:t.Lane.GREEN},{size:8,lane:t.Lane.BLUE}]}),Ht=_({gray:!0,alpha:!0,channels:[t.Lane.RED,t.Lane.ALPHA]}),Mt=Math.abs,jt=Math.min,Tt=_({alpha:!0,channels:[t.Lane.RED,t.Lane.GREEN,t.Lane.BLUE,t.Lane.ALPHA],convert:{fromABGR:(t,e=[])=>{const s=(t>>>24&255)/255,i=(t>>>16&255)/255,r=(t>>>8&255)/255,a=(255&t)/255;let h,l,o,c,f,p,u,m;r<i?(h=i,l=r,o=-1,c=2/3):(h=r,l=i,o=0,c=-1/3),a<h?(f=h,p=l,u=c,m=a):(f=a,p=l,u=o,m=h);const d=f-jt(p,m);return f=n.clamp01(f),e[0]=n.clamp01(Mt((m-p)/(6*d+n.EPS)+u)),e[1]=n.clamp01(d/(f+n.EPS)),e[2]=f,e[3]=s,e},toABGR:t=>{const e=6*t[0],s=t[1],i=255*t[2],r=255*t[3],a=((n.clamp01(Mt(e-3)-1)-1)*s+1)*i,h=((n.clamp01(2-Mt(e-2))-1)*s+1)*i;return(r<<24|((n.clamp01(2-Mt(e-4))-1)*s+1)*i<<16|h<<8|a)>>>0}}}),Yt=_({channels:[t.Lane.RED,t.Lane.GREEN,t.Lane.BLUE]}),Xt=_({alpha:!0,channels:[t.Lane.RED,t.Lane.GREEN,t.Lane.BLUE,t.Lane.ALPHA]});t.ABGR8888=M,t.ALPHA8=St,t.ARGB1555=_t,t.ARGB4444=vt,t.ARGB8888=Pt,t.BGR888=kt,t.BOX_BLUR3=Bt,t.BOX_BLUR5=Gt,t.FLOAT_GRAY=F,t.FLOAT_GRAY_ALPHA=Ht,t.FLOAT_HSVA=Tt,t.FLOAT_NORMAL=$t,t.FLOAT_RGB=Yt,t.FLOAT_RGBA=Xt,t.FloatBuffer=pt,t.GAUSSIAN=t=>{t|=0,s.assert(t>0,`invalid kernel radius: ${t}`);const e=-1/(2*(Math.hypot(t,t)/3)**2),n=[];let i=0;for(let s=-t;s<=t;s++)for(let r=-t;r<=t;r++){const t=Math.exp((r*r+s*s)*e);n.push(t),i+=t}return{spec:n.map((t=>t/i)),size:2*t+1}},t.GAUSSIAN_BLUR3={spec:[1/16,1/8,1/16,1/8,1/4,1/8,1/16,1/8,1/16],size:3},t.GAUSSIAN_BLUR5={spec:[1/256,1/64,3/128,1/64,1/256,1/64,1/16,3/32,1/16,1/64,3/128,3/32,9/64,3/32,3/128,1/64,1/16,3/32,1/16,1/64,1/256,1/64,3/128,1/64,1/256],size:5},t.GRAY16=Dt,t.GRAY8=Ot,t.GRAY_ALPHA16=Ct,t.GRAY_ALPHA8=Ft,t.HIGHPASS3={spec:[-1,-1,-1,-1,9,-1,-1,-1,-1],size:3},t.LANCZOS=zt,t.POOL_MAX=t=>`Math.max(${t.join(",")})`,t.POOL_MEAN=Lt,t.POOL_MIN=t=>`Math.min(${t.join(",")})`,t.POOL_NEAREST=yt,t.POOL_THRESHOLD=(t=0)=>(e,s,n)=>`(${yt(e,s,n)} - ${`(${e.join("+")})/${s*n}`} + ${t}) < 0 ? 0 : 1`,t.PackedBuffer=ct,t.RGB565=Ut,t.RGB888=It,t.SHARPEN3={spec:[0,-1,0,-1,5,-1,0,-1,0],size:3},t.SOBEL_X={spec:[-1,-2,-1,0,0,0,1,2,1],size:3},t.SOBEL_Y={spec:[-1,0,1,-2,0,2,-1,0,1],size:3},t.UNSHARP_MASK5={spec:[-1/256,-1/64,-3/128,-1/64,-1/256,-1/64,-1/16,-3/32,-1/16,-1/64,-3/128,-3/32,119/64,-3/32,-3/128,-1/64,-1/16,-3/32,-1/16,-1/64,-1/256,-1/64,-3/128,-1/64,-1/256],size:5},t.asIntVec=y,t.asVec=x,t.buffer=ot,t.canvas2d=l,t.canvasPixels=o,t.clampRegion=m,t.compileFromABGR=$,t.compileGrayFromABGR=E,t.compileGrayToABGR=b,t.compileToABGR=S,t.convolveChannel=ut,t.convolveImage=mt,t.defBayer=U,t.defFloatFormat=_,t.defIndexed=(e,n=!1)=>{const i=e.length;return s.assert(i>0&&i<=256,`invalid palette size: ${i}`),e=n?e:e.map(a.swapLane13),H({type:"u8",size:8,channels:[{size:8,lane:t.Lane.RED}],fromABGR:t=>Nt(t,e),toABGR:t=>e[t]})},t.defKernel=Rt,t.defLargeKernel=xt,t.defPackedFormat=H,t.defSampler=j,t.ditherPixels=(t,e,s,n,i,r,a,h)=>{!t&&(t=e.slice()),a--;for(let l=0,o=0;l<n;l++)for(let n=0;n<s;n++)t[o]=I(i,r,a,h,n,l,e[o]),o++;return t},t.dominantColors=(t,e,s)=>{const n=t.width*t.height,i=[];for(let e=0,s=0,r=t.stride;e<n;e++,s+=r)i.push(t.pixels.subarray(s,s+r));return r.kmeans(e,i,s).sort(((t,e)=>e.items.length-t.items.length)).map((t=>({color:[...t.centroid],area:t.items.length/n})))},t.ensureChannel=p,t.ensureSize=f,t.floatBuffer=ft,t.imageCanvas=c,t.imagePromise=async t=>{const e=new Image;return e.src=t,await e.decode(),e},t.imagePyramid=function*(t,e=zt(2),n=1,i=!0){for(s.assert(n>0,"invalid min size"),n<<=1,i&&(yield t);t.width>=n&&t.height>=n;)t=mt(t,{kernel:e,stride:2}),yield t},t.luminanceABGR=u,t.normalMap=(t,e)=>{const{channel:s,step:n,scale:i,z:r}=Object.assign({channel:0,step:0,scale:1,z:1},e);p(t.format,s);const a=[-1,...new Array(n).fill(0),1],[h,l]=x(i),o=new pt(t.width,t.height,$t);return o.setChannel(0,ut(t,{kernel:{spec:a,size:[n+2,1]},scale:h,channel:s})),o.setChannel(1,ut(t,{kernel:{spec:a,size:[1,n+2]},scale:l,channel:s})),o.setChannel(2,r),o},t.orderedDither=I,t.packedBuffer=lt,t.prepRegions=d,t.range=L,t.setChannelConvert=w,t.setChannelSame=A,t.setChannelUni=g,t.transformABGR=R,Object.defineProperty(t,"__esModule",{value:!0})})); |
{ | ||
"name": "@thi.ng/pixel", | ||
"version": "0.10.2", | ||
"version": "0.10.3", | ||
"description": "Typedarray integer & float pixel buffers w/ customizable formats, blitting, dithering, convolution", | ||
@@ -45,15 +45,15 @@ "module": "./index.js", | ||
"@types/node": "^14.14.14", | ||
"mocha": "^8.3.0", | ||
"mocha": "^9.0.0", | ||
"nyc": "^15.1.0", | ||
"ts-node": "^9.1.1", | ||
"ts-node": "^10.0.0", | ||
"typedoc": "^0.20.30", | ||
"typescript": "^4.2.3" | ||
"typescript": "4.2.4" | ||
}, | ||
"dependencies": { | ||
"@thi.ng/api": "^7.1.4", | ||
"@thi.ng/binary": "^2.2.4", | ||
"@thi.ng/checks": "^2.9.6", | ||
"@thi.ng/k-means": "^0.2.1", | ||
"@thi.ng/math": "^4.0.0", | ||
"@thi.ng/porter-duff": "^0.1.46" | ||
"@thi.ng/api": "^7.1.5", | ||
"@thi.ng/binary": "^2.2.5", | ||
"@thi.ng/checks": "^2.9.7", | ||
"@thi.ng/k-means": "^0.2.2", | ||
"@thi.ng/math": "^4.0.1", | ||
"@thi.ng/porter-duff": "^0.1.47" | ||
}, | ||
@@ -111,3 +111,3 @@ "files": [ | ||
}, | ||
"gitHead": "8fcf77133e53fe5871ea12030b9b079cca315d2d" | ||
"gitHead": "bcc3e88102fb0eb20e2763908488b7bca3e062d1" | ||
} |
@@ -324,3 +324,3 @@ <!-- This file is generated - DO NOT EDIT! --> | ||
Package sizes (gzipped, pre-treeshake): ESM: 9.18 KB / CJS: 9.47 KB / UMD: 9.19 KB | ||
Package sizes (gzipped, pre-treeshake): ESM: 9.20 KB / CJS: 9.49 KB / UMD: 9.20 KB | ||
@@ -344,11 +344,12 @@ ## Dependencies | ||
| Screenshot | Description | Live demo | Source | | ||
| ------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------- | -------------------------------------------------------- | ------------------------------------------------------------------------------------- | | ||
| <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/adaptive-threshold.png" width="240"/> | Interactive image processing (adaptive threshold) | [Demo](https://demo.thi.ng/umbrella/adaptive-threshold/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/adaptive-threshold) | | ||
| <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/pixel/pixel-basics.png" width="240"/> | Pixel buffer manipulations | [Demo](https://demo.thi.ng/umbrella/pixel-basics/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/pixel-basics) | | ||
| <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/pixel-sorting.png" width="240"/> | Interactive pixel sorting tool using thi.ng/color & thi.ng/pixel | [Demo](https://demo.thi.ng/umbrella/pixel-sorting/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/pixel-sorting) | | ||
| <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/porter-duff/porter-duff2.png" width="240"/> | Port-Duff image compositing / alpha blending | [Demo](https://demo.thi.ng/umbrella/porter-duff/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/porter-duff) | | ||
| <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/shader-ast-workers.jpg" width="240"/> | Fork-join worker-based raymarch renderer | [Demo](https://demo.thi.ng/umbrella/shader-ast-workers/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/shader-ast-workers) | | ||
| <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/text-canvas-image.png" width="240"/> | Textmode image warping w/ 16bit color output | [Demo](https://demo.thi.ng/umbrella/text-canvas-image/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/text-canvas-image) | | ||
| | Minimal multi-pass / GPGPU example | [Demo](https://demo.thi.ng/umbrella/webgl-multipass/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/webgl-multipass) | | ||
| Screenshot | Description | Live demo | Source | | ||
| ------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------- | -------------------------------------------------------- | ------------------------------------------------------------------------------------- | | ||
| <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/adaptive-threshold.png" width="240"/> | Interactive image processing (adaptive threshold) | [Demo](https://demo.thi.ng/umbrella/adaptive-threshold/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/adaptive-threshold) | | ||
| <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/dominant-colors.png" width="240"/> | Color palette generation via dominant color extraction from uploaded images | [Demo](https://demo.thi.ng/umbrella/dominant-colors/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/dominant-colors) | | ||
| <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/pixel/pixel-basics.png" width="240"/> | Pixel buffer manipulations | [Demo](https://demo.thi.ng/umbrella/pixel-basics/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/pixel-basics) | | ||
| <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/pixel-sorting.png" width="240"/> | Interactive pixel sorting tool using thi.ng/color & thi.ng/pixel | [Demo](https://demo.thi.ng/umbrella/pixel-sorting/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/pixel-sorting) | | ||
| <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/porter-duff/porter-duff2.png" width="240"/> | Port-Duff image compositing / alpha blending | [Demo](https://demo.thi.ng/umbrella/porter-duff/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/porter-duff) | | ||
| <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/shader-ast-workers.jpg" width="240"/> | Fork-join worker-based raymarch renderer | [Demo](https://demo.thi.ng/umbrella/shader-ast-workers/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/shader-ast-workers) | | ||
| <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/text-canvas-image.png" width="240"/> | Textmode image warping w/ 16bit color output | [Demo](https://demo.thi.ng/umbrella/text-canvas-image/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/text-canvas-image) | | ||
| | Minimal multi-pass / GPGPU example | [Demo](https://demo.thi.ng/umbrella/webgl-multipass/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/webgl-multipass) | | ||
@@ -355,0 +356,0 @@ ## API |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
398169
440
4482
Updated@thi.ng/api@^7.1.5
Updated@thi.ng/binary@^2.2.5
Updated@thi.ng/checks@^2.9.7
Updated@thi.ng/k-means@^0.2.2
Updated@thi.ng/math@^4.0.1
Updated@thi.ng/porter-duff@^0.1.47