forked from Safecast/Tilemap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
png_zlib_min.js
executable file
·1 lines (1 loc) · 15.1 KB
/
png_zlib_min.js
1
var DecodeStream=function(){function e(){this.pos=0;this.bufferLength=0;this.eof=false;this.buffer=null}e.prototype={ensureBuffer:function(t){var n=this.buffer;var r=n?n.byteLength:0;if(t<r)return n;var i=512;while(i<t)i<<=1;var s=new Uint8Array(i);for(var o=0;o<r;++o)s[o]=n[o];return this.buffer=s},getByte:function(){var t=this.pos;while(this.bufferLength<=t){if(this.eof)return null;this.readBlock()}return this.buffer[this.pos++]},getBytes:function(t){var n=this.pos;if(t){this.ensureBuffer(n+t);var r=n+t;while(!this.eof&&this.bufferLength<r)this.readBlock();var i=this.bufferLength;if(r>i)r=i}else{while(!this.eof)this.readBlock();var r=this.bufferLength}this.pos=r;return this.buffer.subarray(n,r)},lookChar:function(){var t=this.pos;while(this.bufferLength<=t){if(this.eof)return null;this.readBlock()}return String.fromCharCode(this.buffer[this.pos])},getChar:function(){var t=this.pos;while(this.bufferLength<=t){if(this.eof)return null;this.readBlock()}return String.fromCharCode(this.buffer[this.pos++])},makeSubStream:function(t,n,r){var i=t+n;while(this.bufferLength<=i&&!this.eof)this.readBlock();return new Stream(this.buffer,t,n,r)},skip:function(t){if(!t)t=1;this.pos+=t},reset:function(){this.pos=0}};return e}();var FlateStream=function(){function s(e){throw new Error(e)}function o(e){var t=0;var n=e[t++];var r=e[t++];if(n==-1||r==-1)s("Invalid header in flate stream");if((n&15)!=8)s("Unknown compression method in flate stream");if(((n<<8)+r)%31!=0)s("Bad FCHECK in flate stream");if(r&32)s("FDICT bit set in flate stream");this.bytes=e;this.bytesPos=t;this.codeSize=0;this.codeBuf=0;DecodeStream.call(this)}var e=new Uint32Array([16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15]);var t=new Uint32Array([3,4,5,6,7,8,9,10,65547,65549,65551,65553,131091,131095,131099,131103,196643,196651,196659,196667,262211,262227,262243,262259,327811,327843,327875,327907,258,258,258]);var n=new Uint32Array([1,2,3,4,65541,65543,131081,131085,196625,196633,262177,262193,327745,327777,393345,393409,459009,459137,524801,525057,590849,591361,657409,658433,724993,727041,794625,798721,868353,876545]);var r=[new Uint32Array([459008,524368,524304,524568,459024,524400,524336,590016,459016,524384,524320,589984,524288,524416,524352,590048,459012,524376,524312,589968,459028,524408,524344,590032,459020,524392,524328,59e4,524296,524424,524360,590064,459010,524372,524308,524572,459026,524404,524340,590024,459018,524388,524324,589992,524292,524420,524356,590056,459014,524380,524316,589976,459030,524412,524348,590040,459022,524396,524332,590008,524300,524428,524364,590072,459009,524370,524306,524570,459025,524402,524338,590020,459017,524386,524322,589988,524290,524418,524354,590052,459013,524378,524314,589972,459029,524410,524346,590036,459021,524394,524330,590004,524298,524426,524362,590068,459011,524374,524310,524574,459027,524406,524342,590028,459019,524390,524326,589996,524294,524422,524358,590060,459015,524382,524318,589980,459031,524414,524350,590044,459023,524398,524334,590012,524302,524430,524366,590076,459008,524369,524305,524569,459024,524401,524337,590018,459016,524385,524321,589986,524289,524417,524353,590050,459012,524377,524313,589970,459028,524409,524345,590034,459020,524393,524329,590002,524297,524425,524361,590066,459010,524373,524309,524573,459026,524405,524341,590026,459018,524389,524325,589994,524293,524421,524357,590058,459014,524381,524317,589978,459030,524413,524349,590042,459022,524397,524333,590010,524301,524429,524365,590074,459009,524371,524307,524571,459025,524403,524339,590022,459017,524387,524323,589990,524291,524419,524355,590054,459013,524379,524315,589974,459029,524411,524347,590038,459021,524395,524331,590006,524299,524427,524363,590070,459011,524375,524311,524575,459027,524407,524343,590030,459019,524391,524327,589998,524295,524423,524359,590062,459015,524383,524319,589982,459031,524415,524351,590046,459023,524399,524335,590014,524303,524431,524367,590078,459008,524368,524304,524568,459024,524400,524336,590017,459016,524384,524320,589985,524288,524416,524352,590049,459012,524376,524312,589969,459028,524408,524344,590033,459020,524392,524328,590001,524296,524424,524360,590065,459010,524372,524308,524572,459026,524404,524340,590025,459018,524388,524324,589993,524292,524420,524356,590057,459014,524380,524316,589977,459030,524412,524348,590041,459022,524396,524332,590009,524300,524428,524364,590073,459009,524370,524306,524570,459025,524402,524338,590021,459017,524386,524322,589989,524290,524418,524354,590053,459013,524378,524314,589973,459029,524410,524346,590037,459021,524394,524330,590005,524298,524426,524362,590069,459011,524374,524310,524574,459027,524406,524342,590029,459019,524390,524326,589997,524294,524422,524358,590061,459015,524382,524318,589981,459031,524414,524350,590045,459023,524398,524334,590013,524302,524430,524366,590077,459008,524369,524305,524569,459024,524401,524337,590019,459016,524385,524321,589987,524289,524417,524353,590051,459012,524377,524313,589971,459028,524409,524345,590035,459020,524393,524329,590003,524297,524425,524361,590067,459010,524373,524309,524573,459026,524405,524341,590027,459018,524389,524325,589995,524293,524421,524357,590059,459014,524381,524317,589979,459030,524413,524349,590043,459022,524397,524333,590011,524301,524429,524365,590075,459009,524371,524307,524571,459025,524403,524339,590023,459017,524387,524323,589991,524291,524419,524355,590055,459013,524379,524315,589975,459029,524411,524347,590039,459021,524395,524331,590007,524299,524427,524363,590071,459011,524375,524311,524575,459027,524407,524343,590031,459019,524391,524327,589999,524295,524423,524359,590063,459015,524383,524319,589983,459031,524415,524351,590047,459023,524399,524335,590015,524303,524431,524367,590079]),9];var i=[new Uint32Array([327680,327696,327688,327704,327684,327700,327692,327708,327682,327698,327690,327706,327686,327702,327694,0,327681,327697,327689,327705,327685,327701,327693,327709,327683,327699,327691,327707,327687,327703,327695,0]),5];o.prototype=Object.create(DecodeStream.prototype);o.prototype.getBits=function(e){var t=this.codeSize;var n=this.codeBuf;var r=this.bytes;var i=this.bytesPos;var o;while(t<e){if(typeof (o=r[i++])=="undefined")s("Bad encoding in flate stream");n|=o<<t;t+=8}o=n&(1<<e)-1;this.codeBuf=n>>e;this.codeSize=t-=e;this.bytesPos=i;return o};o.prototype.getCode=function(e){var t=e[0];var n=e[1];var r=this.codeSize;var i=this.codeBuf;var o=this.bytes;var u=this.bytesPos;while(r<n){var a;if(typeof (a=o[u++])=="undefined")s("Bad encoding in flate stream");i|=a<<r;r+=8}var f=t[i&(1<<n)-1];var l=f>>16;var c=f&65535;if(r==0||r<l||l==0)s("Bad encoding in flate stream");this.codeBuf=i>>l;this.codeSize=r-l;this.bytesPos=u;return c};o.prototype.generateHuffmanTable=function(e){var t=e.length;var n=0;for(var r=0;r<t;++r){if(e[r]>n)n=e[r]}var i=1<<n;var s=new Uint32Array(i);for(var o=1,u=0,a=2;o<=n;++o,u<<=1,a<<=1){for(var f=0;f<t;++f){if(e[f]==o){var l=0;var c=u;for(var r=0;r<o;++r){l=l<<1|c&1;c>>=1}for(var r=l;r<i;r+=a)s[r]=o<<16|f;++u}}}return[s,n]};o.prototype.readBlock=function(){function o(e,t,n,r,i){var s=e.getBits(n)+r;while(s-->0)t[x++]=i}var u=this.getBits(3);if(u&1)this.eof=true;u>>=1;if(u==0){var a=this.bytes;var f=this.bytesPos;var l;if(typeof (l=a[f++])=="undefined")s("Bad block header in flate stream");var c=l;if(typeof (l=a[f++])=="undefined")s("Bad block header in flate stream");c|=l<<8;if(typeof (l=a[f++])=="undefined")s("Bad block header in flate stream");var h=l;if(typeof (l=a[f++])=="undefined")s("Bad block header in flate stream");h|=l<<8;if(h!=(~c&65535))s("Bad uncompressed block length in flate stream");this.codeBuf=0;this.codeSize=0;var p=this.bufferLength;var d=this.ensureBuffer(p+c);var v=p+c;this.bufferLength=v;for(var m=p;m<v;++m){if(typeof (l=a[f++])=="undefined"){this.eof=true;break}d[m]=l}this.bytesPos=f;return}var g;var y;if(u==1){g=r;y=i}else if(u==2){var b=this.getBits(5)+257;var w=this.getBits(5)+1;var E=this.getBits(4)+4;var S=Array(e.length);var x=0;while(x<E)S[e[x++]]=this.getBits(3);var T=this.generateHuffmanTable(S);var N=0;var x=0;var C=b+w;var k=new Array(C);while(x<C){var L=this.getCode(T);if(L==16){o(this,k,2,3,N)}else if(L==17){o(this,k,3,3,N=0)}else if(L==18){o(this,k,7,11,N=0)}else{k[x++]=N=L}}g=this.generateHuffmanTable(k.slice(0,b));y=this.generateHuffmanTable(k.slice(b,C))}else{s("Unknown block type in flate stream")}var d=this.buffer;var A=d?d.length:0;var O=this.bufferLength;while(true){var M=this.getCode(g);if(M<256){if(O+1>=A){d=this.ensureBuffer(O+1);A=d.length}d[O++]=M;continue}if(M==256){this.bufferLength=O;return}M-=257;M=t[M];var _=M>>16;if(_>0)_=this.getBits(_);var N=(M&65535)+_;M=this.getCode(y);M=n[M];_=M>>16;if(_>0)_=this.getBits(_);var D=(M&65535)+_;if(O+N>=A){d=this.ensureBuffer(O+N);A=d.length}for(var P=0;P<N;++P,++O)d[O]=d[O-D]}};return o}();(function(){var e;e=function(){function a(e){var t,n,r,i,s,o,u,a,f,l,c,h,p,d;this.data=e;this.pos=8;this.palette=[];this.imgData=[];this.transparency={};this.animation=null;this.text={};s=null;while(true){t=this.readUInt32();f=function(){var e,t;t=[];for(o=e=0;e<4;o=++e){t.push(String.fromCharCode(this.data[this.pos++]))}return t}.call(this).join("");switch(f){case"IHDR":this.width=this.readUInt32();this.height=this.readUInt32();this.bits=this.data[this.pos++];this.colorType=this.data[this.pos++];this.compressionMethod=this.data[this.pos++];this.filterMethod=this.data[this.pos++];this.interlaceMethod=this.data[this.pos++];break;case"acTL":this.animation={numFrames:this.readUInt32(),numPlays:this.readUInt32()||Infinity,frames:[]};break;case"PLTE":this.palette=this.read(t);break;case"fcTL":if(s){this.animation.frames.push(s)}this.pos+=4;s={width:this.readUInt32(),height:this.readUInt32(),xOffset:this.readUInt32(),yOffset:this.readUInt32()};i=this.readUInt16();r=this.readUInt16()||100;s.delay=1e3*i/r;s.disposeOp=this.data[this.pos++];s.blendOp=this.data[this.pos++];s.data=[];break;case"IDAT":case"fdAT":if(f==="fdAT"){this.pos+=4;t-=4}e=(s!=null?s.data:void 0)||this.imgData;for(o=h=0;0<=t?h<t:h>t;o=0<=t?++h:--h){e.push(this.data[this.pos++])}break;case"tRNS":this.transparency={};switch(this.colorType){case 3:this.transparency.indexed=this.read(t);l=255-this.transparency.indexed.length;if(l>0){for(o=p=0;0<=l?p<l:p>l;o=0<=l?++p:--p){this.transparency.indexed.push(255)}}break;case 0:this.transparency.grayscale=this.read(t)[0];break;case 2:this.transparency.rgb=this.read(t)}break;case"tEXt":c=this.read(t);u=c.indexOf(0);a=String.fromCharCode.apply(String,c.slice(0,u));this.text[a]=String.fromCharCode.apply(String,c.slice(u+1));break;case"IEND":if(s){this.animation.frames.push(s)}this.colors=function(){switch(this.colorType){case 0:case 3:case 4:return 1;case 2:case 6:return 3}}.call(this);this.hasAlphaChannel=(d=this.colorType)===4||d===6;n=this.colors+(this.hasAlphaChannel?1:0);this.pixelBitlength=this.bits*n;this.colorSpace=function(){switch(this.colors){case 1:return"DeviceGray";case 3:return"DeviceRGB"}}.call(this);this.imgData=new Uint8Array(this.imgData);return;default:this.pos+=t}this.pos+=4;if(this.pos>this.data.length){throw new Error("Incomplete or corrupt PNG file")}}return}var e,t,n,r,i,s,o,u;a.load=function(e,t,n){var r,i=this;if(typeof t==="function"){n=t}r=new XMLHttpRequest;r.open("GET",e,true);r.responseType="arraybuffer";r.onload=function(){var e,i;e=new Uint8Array(r.response||r.mozResponseArrayBuffer);i=new a(e);if(typeof (t!=null?t.getContext:void 0)==="function"){i.render(t)}return typeof n==="function"?n(i):void 0};return r.send(null)};r=0;n=1;i=2;t=0;e=1;a.prototype.read=function(e){var t,n,r;r=[];for(t=n=0;0<=e?n<e:n>e;t=0<=e?++n:--n){r.push(this.data[this.pos++])}return r};a.prototype.readUInt32=function(){var e,t,n,r;e=this.data[this.pos++]<<24;t=this.data[this.pos++]<<16;n=this.data[this.pos++]<<8;r=this.data[this.pos++];return e|t|n|r};a.prototype.readUInt16=function(){var e,t;e=this.data[this.pos++]<<8;t=this.data[this.pos++];return e|t};a.prototype.decodePixels=function(e){var t,n,r,i,s,o,u,a,f,l,c,h,p,d,v,m,g,y,b,w,E,S,x;if(e==null){e=this.imgData}if(e.length===0){return new Uint8Array(0)}e=new FlateStream(e);e=e.getBytes();h=this.pixelBitlength/8;m=h*this.width;p=new Uint8Array(m*this.height);o=e.length;v=0;d=0;n=0;while(d<o){switch(e[d++]){case 0:for(i=b=0;b<m;i=b+=1){p[n++]=e[d++]}break;case 1:for(i=w=0;w<m;i=w+=1){t=e[d++];s=i<h?0:p[n-h];p[n++]=(t+s)%256}break;case 2:for(i=E=0;E<m;i=E+=1){t=e[d++];r=(i-i%h)/h;g=v&&p[(v-1)*m+r*h+i%h];p[n++]=(g+t)%256}break;case 3:for(i=S=0;S<m;i=S+=1){t=e[d++];r=(i-i%h)/h;s=i<h?0:p[n-h];g=v&&p[(v-1)*m+r*h+i%h];p[n++]=(t+Math.floor((s+g)/2))%256}break;case 4:for(i=x=0;x<m;i=x+=1){t=e[d++];r=(i-i%h)/h;s=i<h?0:p[n-h];if(v===0){g=y=0}else{g=p[(v-1)*m+r*h+i%h];y=r&&p[(v-1)*m+(r-1)*h+i%h]}u=s+g-y;a=Math.abs(u-s);l=Math.abs(u-g);c=Math.abs(u-y);if(a<=l&&a<=c){f=s}else if(l<=c){f=g}else{f=y}p[n++]=(t+f)%256}break;default:throw new Error("Invalid filter algorithm: "+e[d-1])}v++}return p};a.prototype.decodePalette=function(){var e,t,n,r,i,s,o,u,a,f;r=this.palette;o=this.transparency.indexed||[];s=new Uint8Array((o.length||0)+r.length);i=0;n=r.length;e=0;for(t=u=0,a=r.length;u<a;t=u+=3){s[i++]=r[t];s[i++]=r[t+1];s[i++]=r[t+2];s[i++]=(f=o[e++])!=null?f:255}return s};a.prototype.copyToImageData=function(e,t){var n,r,i,s,o,u,a,f,l,c,h;r=this.colors;l=null;n=this.hasAlphaChannel;if(this.palette.length){l=(h=this._decodedPalette)!=null?h:this._decodedPalette=this.decodePalette();r=4;n=true}i=e.data||e;f=i.length;o=l||t;s=u=0;if(r===1){while(s<f){a=l?t[s/4]*4:u;c=o[a++];i[s++]=c;i[s++]=c;i[s++]=c;i[s++]=n?o[a++]:255;u=a}}else{while(s<f){a=l?t[s/4]*4:u;i[s++]=o[a++];i[s++]=o[a++];i[s++]=o[a++];i[s++]=n?o[a++]:255;u=a}}};a.prototype.decode=function(){var e;e=new Uint8Array(this.width*this.height*4);this.copyToImageData(e,this.decodePixels());return e};o=document.createElement("canvas");u=o.getContext("2d");s=function(e){var t;u.width=e.width;u.height=e.height;u.clearRect(0,0,e.width,e.height);u.putImageData(e,0,0);t=new Image;t.src=o.toDataURL();return t};a.prototype.decodeFrames=function(e){var t,n,r,i,o,u,a,f;if(!this.animation){return}a=this.animation.frames;f=[];for(n=o=0,u=a.length;o<u;n=++o){t=a[n];r=e.createImageData(t.width,t.height);i=this.decodePixels(new Uint8Array(t.data));this.copyToImageData(r,i);t.imageData=r;f.push(t.image=s(r))}return f};a.prototype.renderFrame=function(e,r){var s,o,u;o=this.animation.frames;s=o[r];u=o[r-1];if(r===0){e.clearRect(0,0,this.width,this.height)}if((u!=null?u.disposeOp:void 0)===n){e.clearRect(u.xOffset,u.yOffset,u.width,u.height)}else if((u!=null?u.disposeOp:void 0)===i){e.putImageData(u.imageData,u.xOffset,u.yOffset)}if(s.blendOp===t){e.clearRect(s.xOffset,s.yOffset,s.width,s.height)}return e.drawImage(s.image,s.xOffset,s.yOffset)};a.prototype.animate=function(e){var t,n,r,i,s,o,u=this;n=0;o=this.animation,i=o.numFrames,r=o.frames,s=o.numPlays;return(t=function(){var o,a;o=n++%i;a=r[o];u.renderFrame(e,o);if(i>1&&n/i<s){return u.animation._timeout=setTimeout(t,a.delay)}})()};a.prototype.stopAnimation=function(){var e;return clearTimeout((e=this.animation)!=null?e._timeout:void 0)};a.prototype.render=function(e){var t,n;if(e._png){e._png.stopAnimation()}e._png=this;e.width=this.width;e.height=this.height;t=e.getContext("2d");if(this.animation){this.decodeFrames(t);return this.animate(t)}else{n=t.createImageData(this.width,this.height);this.copyToImageData(n,this.decodePixels());return t.putImageData(n,0,0)}};return a}();window.PNG=e}).call(this)