-
Notifications
You must be signed in to change notification settings - Fork 5
/
UPNG.min.js
1 lines (1 loc) · 17.1 KB
/
UPNG.min.js
1
!function(){var e={};"object"==typeof module?module.exports=e:window.UPNG=e,function(e,r){e.toRGBA8=function(r){var t=r.width,n=r.height;if(null==r.tabs.acTL)return[e.toRGBA8.decodeImage(r.data,t,n,r).buffer];var a=[];null==r.frames[0].data&&(r.frames[0].data=r.data);for(var i,f=new Uint8Array(t*n*4),o=0;o<r.frames.length;o++){var s=r.frames[o],l=s.rect.x,c=s.rect.y,u=s.rect.width,d=s.rect.height,h=e.toRGBA8.decodeImage(s.data,u,d,r);if(0==o?i=h:0==s.blend?e._copyTile(h,u,d,i,t,n,l,c,0):1==s.blend&&e._copyTile(h,u,d,i,t,n,l,c,1),a.push(i.buffer),i=i.slice(0),0==s.dispose);else if(1==s.dispose)e._copyTile(f,u,d,i,t,n,l,c,0);else if(2==s.dispose){for(var v=o-1;2==r.frames[v].dispose;)v--;i=new Uint8Array(a[v]).slice(0)}}return a},e.toRGBA8.decodeImage=function(r,t,n,a){var i=t*n,f=e.decode._getBPP(a),o=Math.ceil(t*f/8),s=new Uint8Array(4*i),l=new Uint32Array(s.buffer),c=a.ctype,u=a.depth,d=e._bin.readUshort;if(6==c){var h=i<<2;if(8==u)for(var v=0;v<h;v++)s[v]=r[v];if(16==u)for(v=0;v<h;v++)s[v]=r[v<<1]}else if(2==c){var p=a.tabs.tRNS,b=-1,g=-1,y=-1;if(p&&(b=p[0],g=p[1],y=p[2]),8==u)for(v=0;v<i;v++){var m=3*v;s[M=v<<2]=r[m],s[M+1]=r[m+1],s[M+2]=r[m+2],s[M+3]=255,-1!=b&&r[m]==b&&r[m+1]==g&&r[m+2]==y&&(s[M+3]=0)}if(16==u)for(v=0;v<i;v++){m=6*v;s[M=v<<2]=r[m],s[M+1]=r[m+2],s[M+2]=r[m+4],s[M+3]=255,-1!=b&&d(r,m)==b&&d(r,m+2)==g&&d(r,m+4)==y&&(s[M+3]=0)}}else if(3==c){var w=a.tabs.PLTE,A=a.tabs.tRNS,U=A?A.length:0;if(1==u)for(var _=0;_<n;_++){var I=_*o,q=_*t;for(v=0;v<t;v++){var M=q+v<<2,T=3*(R=r[I+(v>>3)]>>7-((7&v)<<0)&1);s[M]=w[T],s[M+1]=w[T+1],s[M+2]=w[T+2],s[M+3]=R<U?A[R]:255}}if(2==u)for(_=0;_<n;_++)for(I=_*o,q=_*t,v=0;v<t;v++){M=q+v<<2,T=3*(R=r[I+(v>>2)]>>6-((3&v)<<1)&3);s[M]=w[T],s[M+1]=w[T+1],s[M+2]=w[T+2],s[M+3]=R<U?A[R]:255}if(4==u)for(_=0;_<n;_++)for(I=_*o,q=_*t,v=0;v<t;v++){M=q+v<<2,T=3*(R=r[I+(v>>1)]>>4-((1&v)<<2)&15);s[M]=w[T],s[M+1]=w[T+1],s[M+2]=w[T+2],s[M+3]=R<U?A[R]:255}if(8==u)for(v=0;v<i;v++){var R;M=v<<2,T=3*(R=r[v]);s[M]=w[T],s[M+1]=w[T+1],s[M+2]=w[T+2],s[M+3]=R<U?A[R]:255}}else if(4==c){if(8==u)for(v=0;v<i;v++){M=v<<2;var N=r[z=v<<1];s[M]=N,s[M+1]=N,s[M+2]=N,s[M+3]=r[z+1]}if(16==u)for(v=0;v<i;v++){var z;M=v<<2,N=r[z=v<<2];s[M]=N,s[M+1]=N,s[M+2]=N,s[M+3]=r[z+2]}}else if(0==c){b=a.tabs.tRNS?a.tabs.tRNS:-1;if(1==u)for(v=0;v<i;v++){var P=(N=255*(r[v>>3]>>7-(7&v)&1))==255*b?0:255;l[v]=P<<24|N<<16|N<<8|N}if(2==u)for(v=0;v<i;v++){P=(N=85*(r[v>>2]>>6-((3&v)<<1)&3))==85*b?0:255;l[v]=P<<24|N<<16|N<<8|N}if(4==u)for(v=0;v<i;v++){P=(N=17*(r[v>>1]>>4-((1&v)<<2)&15))==17*b?0:255;l[v]=P<<24|N<<16|N<<8|N}if(8==u)for(v=0;v<i;v++){P=(N=r[v])==b?0:255;l[v]=P<<24|N<<16|N<<8|N}if(16==u)for(v=0;v<i;v++){N=r[v<<1],P=d(r,v<<1)==b?0:255;l[v]=P<<24|N<<16|N<<8|N}}return s},e.decode=function(r){for(var t,n=new Uint8Array(r),a=8,i=e._bin,f=i.readUshort,o=i.readUint,s={tabs:{},frames:[]},l=new Uint8Array(n.length),c=0,u=0,d=[137,80,78,71,13,10,26,10],h=0;h<8;h++)if(n[h]!=d[h])throw"The input is not a PNG file!";for(;a<n.length;){var v=i.readUint(n,a);a+=4;var p=i.readASCII(n,a,4);if(a+=4,"IHDR"==p)e.decode._IHDR(n,a,s);else if("IDAT"==p){for(h=0;h<v;h++)l[c+h]=n[a+h];c+=v}else if("acTL"==p)s.tabs[p]={num_frames:o(n,a),num_plays:o(n,a+4)},t=new Uint8Array(n.length);else if("fcTL"==p){if(0!=u){(I=s.frames[s.frames.length-1]).data=e.decode._decompress(s,t.slice(0,u),I.rect.width,I.rect.height),u=0}var b={x:o(n,a+12),y:o(n,a+16),width:o(n,a+4),height:o(n,a+8)},g=f(n,a+22);g=f(n,a+20)/(0==g?100:g);var y={rect:b,delay:Math.round(1e3*g),dispose:n[a+24],blend:n[a+25]};s.frames.push(y)}else if("fdAT"==p){for(h=0;h<v-4;h++)t[u+h]=n[a+h+4];u+=v-4}else if("pHYs"==p)s.tabs[p]=[i.readUint(n,a),i.readUint(n,a+4),n[a+8]];else if("cHRM"==p){s.tabs[p]=[];for(h=0;h<8;h++)s.tabs[p].push(i.readUint(n,a+4*h))}else if("tEXt"==p){null==s.tabs[p]&&(s.tabs[p]={});var m=i.nextZero(n,a),w=i.readASCII(n,a,m-a),A=i.readASCII(n,m+1,a+v-m-1);s.tabs[p][w]=A}else if("iTXt"==p){null==s.tabs[p]&&(s.tabs[p]={});m=0;var U=a;m=i.nextZero(n,U);w=i.readASCII(n,U,m-U),n[U=m+1],n[U+1];U+=2,m=i.nextZero(n,U);i.readASCII(n,U,m-U);U=m+1,m=i.nextZero(n,U);i.readUTF8(n,U,m-U);U=m+1;A=i.readUTF8(n,U,v-(U-a));s.tabs[p][w]=A}else if("PLTE"==p)s.tabs[p]=i.readBytes(n,a,v);else if("hIST"==p){var _=s.tabs.PLTE.length/3;s.tabs[p]=[];for(h=0;h<_;h++)s.tabs[p].push(f(n,a+2*h))}else if("tRNS"==p)3==s.ctype?s.tabs[p]=i.readBytes(n,a,v):0==s.ctype?s.tabs[p]=f(n,a):2==s.ctype&&(s.tabs[p]=[f(n,a),f(n,a+2),f(n,a+4)]);else if("gAMA"==p)s.tabs[p]=i.readUint(n,a)/1e5;else if("sRGB"==p)s.tabs[p]=n[a];else if("bKGD"==p)0==s.ctype||4==s.ctype?s.tabs[p]=[f(n,a)]:2==s.ctype||6==s.ctype?s.tabs[p]=[f(n,a),f(n,a+2),f(n,a+4)]:3==s.ctype&&(s.tabs[p]=n[a]);else if("IEND"==p){if(0!=u){var I;(I=s.frames[s.frames.length-1]).data=e.decode._decompress(s,t.slice(0,u),I.rect.width,I.rect.height),u=0}s.data=e.decode._decompress(s,l,s.width,s.height);break}a+=v;i.readUint(n,a);a+=4}return delete s.compress,delete s.interlace,delete s.filter,s},e.decode._decompress=function(r,t,n,a){return 0==r.compress&&(t=e.decode._inflate(t)),0==r.interlace?t=e.decode._filterZero(t,r,0,n,a):1==r.interlace&&(t=e.decode._readInterlace(t,r)),t},e.decode._inflate=function(e){return r.inflate(e)},e.decode._readInterlace=function(r,t){for(var n=t.width,a=t.height,i=e.decode._getBPP(t),f=i>>3,o=Math.ceil(n*i/8),s=new Uint8Array(a*o),l=0,c=[0,0,4,0,2,0,1],u=[0,4,0,2,0,1,0],d=[8,8,8,4,4,2,2],h=[8,8,4,4,2,2,1],v=0;v<7;){for(var p=d[v],b=h[v],g=0,y=0,m=c[v];m<a;)m+=p,y++;for(var w=u[v];w<n;)w+=b,g++;var A=Math.ceil(g*i/8);e.decode._filterZero(r,t,l,g,y);for(var U=0,_=c[v];_<a;){for(var I=u[v],q=l+U*A<<3;I<n;){if(1==i){M=(M=r[q>>3])>>7-(7&q)&1,s[_*o+(I>>3)]|=M<<7-((3&I)<<0)}if(2==i){M=(M=r[q>>3])>>6-(7&q)&3,s[_*o+(I>>2)]|=M<<6-((3&I)<<1)}if(4==i){var M;M=(M=r[q>>3])>>4-(7&q)&15,s[_*o+(I>>1)]|=M<<4-((1&I)<<2)}if(i>=8)for(var T=_*o+I*f,R=0;R<f;R++)s[T+R]=r[(q>>3)+R];q+=i,I+=b}U++,_+=p}g*y!=0&&(l+=y*(1+A)),v+=1}return s},e.decode._getBPP=function(e){return[1,null,3,1,2,null,4][e.ctype]*e.depth},e.decode._filterZero=function(r,t,n,a,i){var f=e.decode._getBPP(t),o=Math.ceil(a*f/8),s=e.decode._paeth;f=Math.ceil(f/8);for(var l=0;l<i;l++){var c=n+l*o,u=c+l+1,d=r[u-1];if(0==d)for(var h=0;h<o;h++)r[c+h]=r[u+h];else if(1==d){for(h=0;h<f;h++)r[c+h]=r[u+h];for(h=f;h<o;h++)r[c+h]=r[u+h]+r[c+h-f]&255}else if(0==l){for(h=0;h<f;h++)r[c+h]=r[u+h];if(2==d)for(h=f;h<o;h++)r[c+h]=255&r[u+h];if(3==d)for(h=f;h<o;h++)r[c+h]=r[u+h]+(r[c+h-f]>>1)&255;if(4==d)for(h=f;h<o;h++)r[c+h]=r[u+h]+s(r[c+h-f],0,0)&255}else{if(2==d)for(h=0;h<o;h++)r[c+h]=r[u+h]+r[c+h-o]&255;if(3==d){for(h=0;h<f;h++)r[c+h]=r[u+h]+(r[c+h-o]>>1)&255;for(h=f;h<o;h++)r[c+h]=r[u+h]+(r[c+h-o]+r[c+h-f]>>1)&255}if(4==d){for(h=0;h<f;h++)r[c+h]=r[u+h]+s(0,r[c+h-o],0)&255;for(h=f;h<o;h++)r[c+h]=r[u+h]+s(r[c+h-f],r[c+h-o],r[c+h-f-o])&255}}}return r},e.decode._paeth=function(e,r,t){var n=e+r-t,a=Math.abs(n-e),i=Math.abs(n-r),f=Math.abs(n-t);return a<=i&&a<=f?e:i<=f?r:t},e.decode._IHDR=function(r,t,n){var a=e._bin;n.width=a.readUint(r,t),t+=4,n.height=a.readUint(r,t),t+=4,n.depth=r[t],t++,n.ctype=r[t],t++,n.compress=r[t],t++,n.filter=r[t],t++,n.interlace=r[t],t++},e._bin={nextZero:function(e,r){for(;0!=e[r];)r++;return r},readUshort:function(e,r){return e[r]<<8|e[r+1]},writeUshort:function(e,r,t){e[r]=t>>8&255,e[r+1]=255&t},readUint:function(e,r){return 16777216*e[r]+(e[r+1]<<16|e[r+2]<<8|e[r+3])},writeUint:function(e,r,t){e[r]=t>>24&255,e[r+1]=t>>16&255,e[r+2]=t>>8&255,e[r+3]=255&t},readASCII:function(e,r,t){for(var n="",a=0;a<t;a++)n+=String.fromCharCode(e[r+a]);return n},writeASCII:function(e,r,t){for(var n=0;n<t.length;n++)e[r+n]=t.charCodeAt(n)},readBytes:function(e,r,t){for(var n=[],a=0;a<t;a++)n.push(e[r+a]);return n},pad:function(e){return e.length<2?"0"+e:e},readUTF8:function(r,t,n){for(var a,i="",f=0;f<n;f++)i+="%"+e._bin.pad(r[t+f].toString(16));try{a=decodeURIComponent(i)}catch(a){return e._bin.readASCII(r,t,n)}return a}},e._copyTile=function(e,r,t,n,a,i,f,o,s){for(var l=Math.min(r,a),c=Math.min(t,i),u=0,d=0,h=0;h<c;h++)for(var v=0;v<l;v++)if(f>=0&&o>=0?(u=h*r+v<<2,d=(o+h)*a+f+v<<2):(u=(-o+h)*r-f+v<<2,d=h*a+v<<2),0==s)n[d]=e[u],n[d+1]=e[u+1],n[d+2]=e[u+2],n[d+3]=e[u+3];else if(1==s){var p=e[u+3]*(1/255),b=e[u]*p,g=e[u+1]*p,y=e[u+2]*p,m=n[d+3]*(1/255),w=n[d]*m,A=n[d+1]*m,U=n[d+2]*m,_=1-p,I=p+m*_,q=0==I?0:1/I;n[d+3]=255*I,n[d+0]=(b+w*_)*q,n[d+1]=(g+A*_)*q,n[d+2]=(y+U*_)*q}else if(2==s){p=e[u+3],b=e[u],g=e[u+1],y=e[u+2],m=n[d+3],w=n[d],A=n[d+1],U=n[d+2];p==m&&b==w&&g==A&&y==U?(n[d]=0,n[d+1]=0,n[d+2]=0,n[d+3]=0):(n[d]=b,n[d+1]=g,n[d+2]=y,n[d+3]=p)}else if(3==s){p=e[u+3],b=e[u],g=e[u+1],y=e[u+2],m=n[d+3],w=n[d],A=n[d+1],U=n[d+2];if(p==m&&b==w&&g==A&&y==U)continue;if(p<220&&m>20)return!1}return!0},e.encode=function(r,t,n,a,i,f){null==a&&(a=0),null==f&&(f=!1);for(var o=new Uint8Array(r[0].byteLength*r.length+100),s=[137,80,78,71,13,10,26,10],l=0;l<8;l++)o[l]=s[l];var c=8,u=e._bin,d=e.crc.crc,h=u.writeUint,v=u.writeUshort,p=u.writeASCII,b=e.encode.compressPNG(r,t,n,a,f);h(o,c,13),p(o,c+=4,"IHDR"),h(o,c+=4,t),h(o,c+=4,n),o[c+=4]=b.depth,o[++c]=b.ctype,o[++c]=0,o[++c]=0,o[++c]=0,h(o,++c,d(o,c-17,17)),h(o,c+=4,1),p(o,c+=4,"sRGB"),o[c+=4]=1,h(o,++c,d(o,c-5,5)),c+=4;var g=r.length>1;if(g&&(h(o,c,8),p(o,c+=4,"acTL"),h(o,c+=4,r.length),h(o,c+=4,0),h(o,c+=4,d(o,c-12,12)),c+=4),3==b.ctype){h(o,c,3*(M=b.plte.length)),p(o,c+=4,"PLTE"),c+=4;for(l=0;l<M;l++){var y=3*l,m=b.plte[l],w=255&m,A=m>>8&255,U=m>>16&255;o[c+y+0]=w,o[c+y+1]=A,o[c+y+2]=U}if(c+=3*M,h(o,c,d(o,c-3*M-4,3*M+4)),c+=4,b.gotAlpha){h(o,c,M),p(o,c+=4,"tRNS"),c+=4;for(l=0;l<M;l++)o[c+l]=b.plte[l]>>24&255;h(o,c+=M,d(o,c-M-4,M+4)),c+=4}}for(var _=0,I=0;I<b.frames.length;I++){var q=b.frames[I];g&&(h(o,c,26),p(o,c+=4,"fcTL"),h(o,c+=4,_++),h(o,c+=4,q.rect.width),h(o,c+=4,q.rect.height),h(o,c+=4,q.rect.x),h(o,c+=4,q.rect.y),v(o,c+=4,i[I]),v(o,c+=2,1e3),o[c+=2]=q.dispose,o[++c]=q.blend,h(o,++c,d(o,c-30,30)),c+=4);var M,T=q.cimg;h(o,c,(M=T.length)+(0==I?0:4));var R=c+=4;p(o,c,0==I?"IDAT":"fdAT"),c+=4,0!=I&&(h(o,c,_++),c+=4);for(l=0;l<M;l++)o[c+l]=T[l];h(o,c+=M,d(o,R,c-R)),c+=4}return h(o,c,0),c+=4,p(o,c,"IEND"),c+=4,h(o,c,d(o,c-4,4)),c+=4,o.buffer.slice(0,c)},e.encode.compressPNG=function(r,t,n,a,i){for(var f=e.encode.compress(r,t,n,a,!1,i),o=0;o<r.length;o++){var s=f.frames[o],l=(s.rect.width,s.rect.height),c=s.bpl,u=s.bpp,d=new Uint8Array(l*c+l);s.cimg=e.encode._filterZero(s.img,l,u,c,d)}return f},e.encode.compress=function(r,t,n,a,i,f){null==f&&(f=!1);for(var o=6,s=8,l=4,c=255,u=0;u<r.length;u++)for(var d=new Uint8Array(r[u]),h=d.length,v=0;v<h;v+=4)c&=d[v+3];var p=255!=c,b={},g=[];if(0!=r.length&&(b[0]=0,g.push(0),0!=a&&a--),0!=a){var y=e.quantize(r,a,i);r=y.bufs;for(v=0;v<y.plte.length;v++){null==b[w=y.plte[v].est.rgba]&&(b[w]=g.length,g.push(w))}}else for(u=0;u<r.length;u++){var m=new Uint32Array(r[u]);for(h=m.length,v=0;v<h;v++){var w=m[v];if((v<t||w!=m[v-1]&&w!=m[v-t])&&null==b[w]&&(b[w]=g.length,g.push(w),g.length>=300))break}}var A=!!p&&i,U=g.length;U<=256&&0==f&&(s=U<=2?1:U<=4?2:U<=16?4:8,i&&(s=8),p=!0);var _=[];for(u=0;u<r.length;u++){var I=new Uint8Array(r[u]),q=new Uint32Array(I.buffer),M=0,T=0,R=t,N=n,z=0;if(0!=u&&!A){for(var P=i||1==u||2==_[_.length-2].dispose?1:2,S=0,L=1e9,C=0;C<P;C++){for(var D=new Uint8Array(r[u-1-C]),B=new Uint32Array(r[u-1-C]),x=t,G=n,Z=-1,k=-1,E=0;E<n;E++)for(var H=0;H<t;H++){q[v=E*t+H]!=B[v]&&(H<x&&(x=H),H>Z&&(Z=H),E<G&&(G=E),E>k&&(k=E))}var F=-1==Z?1:(Z-x+1)*(k-G+1);F<L&&(L=F,S=C,-1==Z?(M=T=0,R=N=1):(M=x,T=G,R=Z-x+1,N=k-G+1))}D=new Uint8Array(r[u-1-S]);1==S&&(_[_.length-1].dispose=2);var V=new Uint8Array(R*N*4);new Uint32Array(V.buffer);e._copyTile(D,t,n,V,R,N,-M,-T,0),e._copyTile(I,t,n,V,R,N,-M,-T,3)?(e._copyTile(I,t,n,V,R,N,-M,-T,2),z=1):(e._copyTile(I,t,n,V,R,N,-M,-T,0),z=0),I=V,q=new Uint32Array(I.buffer)}var X=4*R;if(U<=256&&0==f){X=Math.ceil(s*R/8);for(V=new Uint8Array(X*N),E=0;E<N;E++){v=E*X;var j=E*R;if(8==s)for(H=0;H<R;H++)V[v+H]=b[q[j+H]];else if(4==s)for(H=0;H<R;H++)V[v+(H>>1)]|=b[q[j+H]]<<4-4*(1&H);else if(2==s)for(H=0;H<R;H++)V[v+(H>>2)]|=b[q[j+H]]<<6-2*(3&H);else if(1==s)for(H=0;H<R;H++)V[v+(H>>3)]|=b[q[j+H]]<<7-1*(7&H)}I=V,o=3,l=1}else if(0==p&&1==r.length){V=new Uint8Array(R*N*3);var K=R*N;for(v=0;v<K;v++){var Y=3*v,J=4*v;V[Y]=I[J],V[Y+1]=I[J+1],V[Y+2]=I[J+2]}I=V,o=2,l=3,X=3*R}_.push({rect:{x:M,y:T,width:R,height:N},img:I,bpl:X,bpp:l,blend:z,dispose:A?1:0})}return{ctype:o,depth:s,plte:g,gotAlpha:p,frames:_}},e.encode._filterZero=function(t,n,a,i,f){for(var o=[],s=0;s<5;s++)if(!(n*i>5e5)||2!=s&&3!=s&&4!=s){for(var l=0;l<n;l++)e.encode._filterLine(f,t,l,i,a,s);if(o.push(r.deflate(f)),1==a)break}for(var c,u=1e9,d=0;d<o.length;d++)o[d].length<u&&(c=d,u=o[d].length);return o[c]},e.encode._filterLine=function(r,t,n,a,i,f){var o=n*a,s=o+n,l=e.decode._paeth;if(r[s]=f,s++,0==f)for(var c=0;c<a;c++)r[s+c]=t[o+c];else if(1==f){for(c=0;c<i;c++)r[s+c]=t[o+c];for(c=i;c<a;c++)r[s+c]=t[o+c]-t[o+c-i]+256&255}else if(0==n){for(c=0;c<i;c++)r[s+c]=t[o+c];if(2==f)for(c=i;c<a;c++)r[s+c]=t[o+c];if(3==f)for(c=i;c<a;c++)r[s+c]=t[o+c]-(t[o+c-i]>>1)+256&255;if(4==f)for(c=i;c<a;c++)r[s+c]=t[o+c]-l(t[o+c-i],0,0)+256&255}else{if(2==f)for(c=0;c<a;c++)r[s+c]=t[o+c]+256-t[o+c-a]&255;if(3==f){for(c=0;c<i;c++)r[s+c]=t[o+c]+256-(t[o+c-a]>>1)&255;for(c=i;c<a;c++)r[s+c]=t[o+c]+256-(t[o+c-a]+t[o+c-i]>>1)&255}if(4==f){for(c=0;c<i;c++)r[s+c]=t[o+c]+256-l(0,t[o+c-a],0)&255;for(c=i;c<a;c++)r[s+c]=t[o+c]+256-l(t[o+c-i],t[o+c-a],t[o+c-i-a])&255}}},e.crc={table:function(){for(var e=new Uint32Array(256),r=0;r<256;r++){for(var t=r,n=0;n<8;n++)1&t?t=3988292384^t>>>1:t>>>=1;e[r]=t}return e}(),update:function(r,t,n,a){for(var i=0;i<a;i++)r=e.crc.table[255&(r^t[n+i])]^r>>>8;return r},crc:function(r,t,n){return 4294967295^e.crc.update(4294967295,r,t,n)}},e.quantize=function(r,t,n){for(var a=[],i=0,f=0;f<r.length;f++)a.push(e.encode.alphaMul(new Uint8Array(r[f]),n)),i+=r[f].byteLength;var o=new Uint8Array(i),s=new Uint32Array(o.buffer),l=0;for(f=0;f<a.length;f++){for(var c=a[f],u=c.length,d=0;d<u;d++)o[l+d]=c[d];l+=u}var h={i0:0,i1:o.length,bst:null,est:null,tdst:0,left:null,right:null};h.bst=e.quantize.stats(o,h.i0,h.i1),h.est=e.quantize.estats(h.bst);for(var v=[h];v.length<t;){var p=0,b=0;for(f=0;f<v.length;f++)v[f].est.L>p&&(p=v[f].est.L,b=f);if(p<.001)break;var g=v[b],y=e.quantize.splitPixels(o,s,g.i0,g.i1,g.est.e,g.est.eMq255),m={i0:g.i0,i1:y,bst:null,est:null,tdst:0,left:null,right:null};m.bst=e.quantize.stats(o,m.i0,m.i1),m.est=e.quantize.estats(m.bst);var w={i0:y,i1:g.i1,bst:null,est:null,tdst:0,left:null,right:null};w.bst={R:[],m:[],N:g.bst.N-m.bst.N};for(f=0;f<16;f++)w.bst.R[f]=g.bst.R[f]-m.bst.R[f];for(f=0;f<4;f++)w.bst.m[f]=g.bst.m[f]-m.bst.m[f];w.est=e.quantize.estats(w.bst),g.left=m,g.right=w,v[b]=m,v.push(w)}v.sort(function(e,r){return r.bst.N-e.bst.N});for(var A=0;A<a.length;A++){var U=e.quantize.planeDst,_=new Uint8Array(a[A].buffer),I=new Uint32Array(a[A].buffer),q=_.length;for(f=0;f<q;f+=4){for(var M=_[f]*(1/255),T=_[f+1]*(1/255),R=_[f+2]*(1/255),N=_[f+3]*(1/255),z=h;z.left;)z=U(z.est,M,T,R,N)<=0?z.left:z.right;I[f>>2]=z.est.rgba}a[A]=I.buffer}return{bufs:a,plte:v}},e.quantize.getNearest=function(r,t,n,a,i){if(null==r.left)return r.tdst=e.quantize.dist(r.est.q,t,n,a,i),r;var f=e.quantize.planeDst(r.est,t,n,a,i),o=r.left,s=r.right;f>0&&(o=r.right,s=r.left);var l=e.quantize.getNearest(o,t,n,a,i);if(l.tdst<=f*f)return l;var c=e.quantize.getNearest(s,t,n,a,i);return c.tdst<l.tdst?c:l},e.quantize.planeDst=function(e,r,t,n,a){var i=e.e;return i[0]*r+i[1]*t+i[2]*n+i[3]*a-e.eMq},e.quantize.dist=function(e,r,t,n,a){var i=r-e[0],f=t-e[1],o=n-e[2],s=a-e[3];return i*i+f*f+o*o+s*s},e.quantize.splitPixels=function(r,t,n,a,i,f){var o=e.quantize.vecDot;a-=4;for(;n<a;){for(;o(r,n,i)<=f;)n+=4;for(;o(r,a,i)>f;)a-=4;if(n>=a)break;var s=t[n>>2];t[n>>2]=t[a>>2],t[a>>2]=s,n+=4,a-=4}for(;o(r,n,i)>f;)n-=4;return n+4},e.quantize.vecDot=function(e,r,t){return e[r]*t[0]+e[r+1]*t[1]+e[r+2]*t[2]+e[r+3]*t[3]},e.quantize.stats=function(e,r,t){for(var n=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],a=[0,0,0,0],i=t-r>>2,f=r;f<t;f+=4){var o=e[f]*(1/255),s=e[f+1]*(1/255),l=e[f+2]*(1/255),c=e[f+3]*(1/255);a[0]+=o,a[1]+=s,a[2]+=l,a[3]+=c,n[0]+=o*o,n[1]+=o*s,n[2]+=o*l,n[3]+=o*c,n[5]+=s*s,n[6]+=s*l,n[7]+=s*c,n[10]+=l*l,n[11]+=l*c,n[15]+=c*c}return n[4]=n[1],n[8]=n[2],n[12]=n[3],n[9]=n[6],n[13]=n[7],n[14]=n[11],{R:n,m:a,N:i}},e.quantize.estats=function(r){var t=r.R,n=r.m,a=r.N,i=n[0],f=n[1],o=n[2],s=n[3],l=0==a?0:1/a,c=[t[0]-i*i*l,t[1]-i*f*l,t[2]-i*o*l,t[3]-i*s*l,t[4]-f*i*l,t[5]-f*f*l,t[6]-f*o*l,t[7]-f*s*l,t[8]-o*i*l,t[9]-o*f*l,t[10]-o*o*l,t[11]-o*s*l,t[12]-s*i*l,t[13]-s*f*l,t[14]-s*o*l,t[15]-s*s*l],u=c,d=e.M4,h=[.5,.5,.5,.5],v=0,p=0;if(0!=a)for(var b=0;b<10&&(h=d.multVec(u,h),p=Math.sqrt(d.dot(h,h)),h=d.sml(1/p,h),!(Math.abs(p-v)<1e-9));b++)v=p;var g=[i*l,f*l,o*l,s*l],y=d.dot(d.sml(255,g),h),m=g[3]<.001?0:1/g[3];return{Cov:c,q:g,e:h,L:v,eMq255:y,eMq:d.dot(h,g),rgba:(Math.round(255*g[3])<<24|Math.round(255*g[2]*m)<<16|Math.round(255*g[1]*m)<<8|Math.round(255*g[0]*m)<<0)>>>0}},e.M4={multVec:function(e,r){return[e[0]*r[0]+e[1]*r[1]+e[2]*r[2]+e[3]*r[3],e[4]*r[0]+e[5]*r[1]+e[6]*r[2]+e[7]*r[3],e[8]*r[0]+e[9]*r[1]+e[10]*r[2]+e[11]*r[3],e[12]*r[0]+e[13]*r[1]+e[14]*r[2]+e[15]*r[3]]},dot:function(e,r){return e[0]*r[0]+e[1]*r[1]+e[2]*r[2]+e[3]*r[3]},sml:function(e,r){return[e*r[0],e*r[1],e*r[2],e*r[3]]}},e.encode.alphaMul=function(e,r){for(var t=new Uint8Array(e.length),n=e.length>>2,a=0;a<n;a++){var i=a<<2,f=e[i+3];r&&(f=f<128?0:255);var o=f*(1/255);t[i+0]=e[i+0]*o,t[i+1]=e[i+1]*o,t[i+2]=e[i+2]*o,t[i+3]=f}return t}}(e,"function"==typeof require?require("pako"):window.pako)}();