var e=256,t=e-1,n=4,r=16,i=1<>o,c=i<>3,u=6,d=l*(1<>3);for(var t=0,r;t>=n,h[t][1]>>=n,h[t][2]>>=n,h[t][3]=t}function k(e,t,n,r,i){h[t][0]-=e*(h[t][0]-n)/m,h[t][1]-=e*(h[t][1]-r)/m,h[t][2]-=e*(h[t][2]-i)/m}function A(t,n,r,i,a){for(var o=Math.abs(n-t),s=Math.min(n+t,e),c=n+1,l=n-1,u=1,d,f;co;)f=E[u++],co&&(d=h[l--],d[0]-=f*(d[0]-r)/_,d[1]-=f*(d[1]-i)/_,d[2]-=f*(d[2]-a)/_)}function j(t,i,l){for(var u=2147483647,d=u,f=-1,p=f,m=0,g,_,v,y;m>r-n),v>o,T[m]-=y,w[m]+=y<>1,r=c+1;r>1,r=c+1;r<256;r++)C[r]=t}function N(t,n,r){for(var i,a,o,s=1e3,c=-1,l=C[n],u=l-1;l=0;)l=s?l=e:(l++,o<0&&(o=-o),i=a[0]-t,i<0&&(i=-i),o+=i,o=0&&(a=h[u],o=n-a[1],o>=s?u=-1:(u--,o<0&&(o=-o),i=a[0]-t,i<0&&(i=-i),o+=i,o>u;for(c<=1&&(c=0),e=0;e=t&&(D-=t),e++,a===0&&(a=1),e%a===0)for(o-=o/r,s-=s/f,c=s>>u,c<=1&&(c=0),T=0;T=254&&k(t)}function x(e){S(E),d=h+2,p=!0,M(h,e)}function S(e){for(var t=0;t=0){c=l-i,i===0&&(c=1);do if((i-=c)<0&&(i+=l),o[i]===n){a=s[i];continue outer_loop}while(o[i]>=0)}M(a,t),a=r,d<4096?(s[i]=d++,o[i]=n):x(t)}M(a,t),M(g,t)}function O(n){n.writeByte(i),_=e*t,v=0,C(i+1,n),n.writeByte(0)}function k(e){u>0&&(e.writeByte(u),e.writeBytes(a,0,u),u=0)}function A(e){return(1<0?c|=e<=8;)b(c&255,t),c>>=8,l-=8;if((d>f||p)&&(p?(f=A(y=m),p=!1):(++y,f=y==T?4096:A(y))),e==g){for(;l>0;)b(c&255,t),c>>=8,l-=8;k(t)}}this.encode=O}function k(){this.page=-1,this.pages=[],this.newPage()}k.pageSize=4096,k.charMap={};for(var A=0;A<256;A++)k.charMap[A]=String.fromCharCode(A);k.prototype.newPage=function(){this.pages[++this.page]=new Uint8Array(k.pageSize),this.cursor=0},k.prototype.getData=function(){for(var e=``,t=0;te+t.length,0));return e.reduce((e,t)=>(n.set(t,e),e+t.length),0),n},k.prototype.writeByte=function(e){this.cursor>=k.pageSize&&this.newPage(),this.pages[this.page][this.cursor++]=e},k.prototype.writeUTFBytes=function(e){for(var t=e.length,n=0;n=0&&(this.dispose=e)},j.prototype.setRepeat=function(e){this.repeat=e},j.prototype.setTransparent=function(e){this.transparent=e},j.prototype.addFrame=function(e){this.image=e,this.colorTab=this.globalPalette&&this.globalPalette.slice?this.globalPalette:null,this.getImagePixels(),this.analyzePixels(),this.globalPalette===!0&&(this.globalPalette=this.colorTab),this.firstFrame&&(this.writeHeader(),this.writeLSD(),this.writePalette(),this.repeat>=0&&this.writeNetscapeExt()),this.writeGraphicCtrlExt(),this.writeImageDesc(),!this.firstFrame&&!this.globalPalette&&this.writePalette(),this.writePixels(),this.firstFrame=!1},j.prototype.finish=function(){this.out.writeByte(59)},j.prototype.setQuality=function(e){e<1&&(e=1),this.sample=e},j.prototype.setDither=function(e){e===!0&&(e=`FloydSteinberg`),this.dither=e},j.prototype.setGlobalPalette=function(e){this.globalPalette=e},j.prototype.getGlobalPalette=function(){return this.globalPalette&&this.globalPalette.slice&&this.globalPalette.slice(0)||this.globalPalette},j.prototype.writeHeader=function(){this.out.writeUTFBytes(`GIF89a`)},j.prototype.analyzePixels=function(){this.colorTab||=(this.neuQuant=new C(this.pixels,this.sample),this.neuQuant.buildColormap(),this.neuQuant.getColormap()),this.dither?this.ditherPixels(this.dither.replace(`-serpentine`,``),this.dither.match(/-serpentine/)!==null):this.indexPixels(),this.pixels=null,this.colorDepth=8,this.palSize=7,this.transparent!==null&&(this.transIndex=this.findClosest(this.transparent,!0))},j.prototype.indexPixels=function(e){var t=this.pixels.length/3;this.indexedPixels=new Uint8Array(t);for(var n=0,r=0;r=0&&w+u=0&&T+l>16,(e&65280)>>8,e&255,t)},j.prototype.findClosestRGB=function(e,t,n,r){if(this.colorTab===null)return-1;if(this.neuQuant&&!r)return this.neuQuant.lookupRGB(e,t,n);n|t<<8|e<<16;for(var i=0,a=16777216,o=this.colorTab.length,s=0,c=0;s=0&&(t=this.dispose&7),t<<=2,this.out.writeByte(t|0|e),this.writeShort(this.delay),this.out.writeByte(this.transIndex),this.out.writeByte(0)},j.prototype.writeImageDesc=function(){this.out.writeByte(44),this.writeShort(0),this.writeShort(0),this.writeShort(this.width),this.writeShort(this.height),this.firstFrame||this.globalPalette?this.out.writeByte(0):this.out.writeByte(128|this.palSize)},j.prototype.writeLSD=function(){this.writeShort(this.width),this.writeShort(this.height),this.out.writeByte(240|this.palSize),this.out.writeByte(0),this.out.writeByte(0)},j.prototype.writeNetscapeExt=function(){this.out.writeByte(33),this.out.writeByte(255),this.out.writeByte(11),this.out.writeUTFBytes(`NETSCAPE2.0`),this.out.writeByte(3),this.out.writeByte(1),this.writeShort(this.repeat),this.out.writeByte(0)},j.prototype.writePalette=function(){this.out.writeBytes(this.colorTab);for(var e=768-this.colorTab.length,t=0;t>8&255)},j.prototype.writePixels=function(){new O(this.width,this.height,this.indexedPixels,this.colorDepth).encode(this.out)},j.prototype.stream=function(){return this.out};export{j as default};