forked from jcubic/favloader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseGIF.min.js
More file actions
1 lines (1 loc) · 5.23 KB
/
parseGIF.min.js
File metadata and controls
1 lines (1 loc) · 5.23 KB
1
(function(e){var t=typeof window!=="undefined"?window:global;if(typeof define==="function"&&define.amd){define([],e)}else if(typeof module==="object"&&module.exports){module.exports=e()}else{t.parseGIF=e()}})(function(e){var c=function(e){return e.reduce(function(e,t){return e*2+t},0)};var f=function(e){var t=[];for(var r=7;r>=0;r--){t.push(!!(e&1<<r))}return t};var n=function(e){this.data=e;this.len=this.data.length;this.pos=0;this.readByte=function(){if(this.pos>=this.data.length){throw new Error("Attempted to read past end of stream.")}return e.charCodeAt(this.pos++)&255};this.readBytes=function(e){var t=[];for(var r=0;r<e;r++){t.push(this.readByte())}return t};this.read=function(e){var t="";for(var r=0;r<e;r++){t+=String.fromCharCode(this.readByte())}return t};this.readUnsigned=function(){var e=this.readBytes(2);return(e[1]<<8)+e[0]}};var u=function(t,a){var n=0;var e=function(e){var t=0;for(var r=0;r<e;r++){if(a.charCodeAt(n>>3)&1<<(n&7)){t|=1<<r}n++}return t};var r=[];var i=1<<t;var o=i+1;var s=t+1;var d=[];var c=function(){d=[];s=t+1;for(var e=0;e<i;e++){d[e]=[e]}d[i]=[];d[o]=null};var f;var u;while(true){u=f;f=e(s);if(f===i){c();continue}if(f===o)break;if(f<d.length){if(u!==i){d.push(d[u].concat(d[f][0]))}}else{if(f!==d.length)throw new Error("Invalid LZW code.");d.push(d[u].concat(d[u][0]))}r.push.apply(r,d[f]);if(d.length===1<<s&&s<12){s++}}return r};var i=function(o,s){s||(s={});var n=function(e){var t=[];for(var r=0;r<e;r++){t.push(o.readBytes(3))}return t};var d=function(){var e,t;t="";do{e=o.readByte();t+=o.read(e)}while(e!==0);return t};var e=function(){var e={};e.sig=o.read(3);e.ver=o.read(3);if(e.sig!=="GIF")throw new Error("Not a GIF file.");e.width=o.readUnsigned();e.height=o.readUnsigned();var t=f(o.readByte());e.gctFlag=t.shift();e.colorRes=c(t.splice(0,3));e.sorted=t.shift();e.gctSize=c(t.splice(0,3));e.bgColor=o.readByte();e.pixelAspectRatio=o.readByte();if(e.gctFlag){e.gct=n(1<<e.gctSize+1)}s.hdr&&s.hdr(e)};var t=function(e){var t=function(e){var t=o.readByte();var r=f(o.readByte());e.reserved=r.splice(0,3);e.disposalMethod=c(r.splice(0,3));e.userInput=r.shift();e.transparencyGiven=r.shift();e.delayTime=o.readUnsigned();e.transparencyIndex=o.readByte();e.terminator=o.readByte();s.gce&&s.gce(e)};var r=function(e){e.comment=d();s.com&&s.com(e)};var a=function(e){var t=o.readByte();e.ptHeader=o.readBytes(12);e.ptData=d();s.pte&&s.pte(e)};var n=function(e){var t=function(e){var t=o.readByte();e.unknown=o.readByte();e.iterations=o.readUnsigned();e.terminator=o.readByte();s.app&&s.app.NETSCAPE&&s.app.NETSCAPE(e)};var r=function(e){e.appData=d();s.app&&s.app[e.identifier]&&s.app[e.identifier](e)};var a=o.readByte();e.identifier=o.read(8);e.authCode=o.read(3);switch(e.identifier){case"NETSCAPE":t(e);break;default:r(e);break}};var i=function(e){e.data=d();s.unknown&&s.unknown(e)};e.label=o.readByte();switch(e.label){case 249:e.extType="gce";t(e);break;case 254:e.extType="com";r(e);break;case 1:e.extType="pte";a(e);break;case 255:e.extType="app";n(e);break;default:e.extType="unknown";i(e);break}};var r=function(e){var t=function(a,n){var i=new Array(a.length);var e=a.length/n;var t=function(e,t){var r=a.slice(t*n,(t+1)*n);i.splice.apply(i,[e*n,n].concat(r))};var r=[0,4,2,1];var o=[8,8,4,2];var s=0;for(var d=0;d<4;d++){for(var c=r[d];c<e;c+=o[d]){t(c,s);s++}}return i};e.leftPos=o.readUnsigned();e.topPos=o.readUnsigned();e.width=o.readUnsigned();e.height=o.readUnsigned();var r=f(o.readByte());e.lctFlag=r.shift();e.interlaced=r.shift();e.sorted=r.shift();e.reserved=r.splice(0,2);e.lctSize=c(r.splice(0,3));if(e.lctFlag){e.lct=n(1<<e.lctSize+1)}e.lzwMinCodeSize=o.readByte();var a=d();e.pixels=u(e.lzwMinCodeSize,a);if(e.interlaced){e.pixels=t(e.pixels,e.width)}s.img&&s.img(e)};var a=function(){var e={};e.sentinel=o.readByte();switch(String.fromCharCode(e.sentinel)){case"!":e.type="ext";t(e);break;case",":e.type="img";r(e);break;case";":e.type="eof";s.eof&&s.eof(e);break;default:throw new Error("Unknown block: 0x"+e.sentinel.toString(16))}if(e.type!=="eof")setTimeout(a,0)};var i=function(){e();setTimeout(a,0)};i()};function o(a){return new Promise(function(t,e){var r=new XMLHttpRequest;r.overrideMimeType("text/plain; charset=x-user-defined");r.onload=function(e){t(new n(r.responseText))};r.onerror=e;r.open("GET",a,true);r.send()})}function s(t,e){var r=document.createElement("canvas");r.setAttribute("width",e[0].width);r.setAttribute("height",e[0].height);var i=r.getContext("2d");var o=[];e.forEach(function(r,e){i.clearRect(r.leftPos,r.topPos,r.width,r.height);var a=i.getImageData(r.leftPos,r.topPos,r.width,r.height);var n=r.lctFlag?r.lct:t.gtc;r.pixels.forEach(function(e,t){if(r.transparency!==e){a.data[t*4+0]=n[e][0];a.data[t*4+1]=n[e][1];a.data[t*4+2]=n[e][2];a.data[t*4+3]=255}else{if(r.disposalMethod===2||r.disposalMethod===3){a.data[t*4+3]=0}else{}}});i.putImageData(a,r.leftPos,r.topPos);o.push(i.canvas.toDataURL())});return o}return function(e){return new Promise(function(t){var r={};var a=[];var n;o(e).then(function(e){setTimeout(function(){i(e,{gce:function(e){n={transparency:e.transparencyGiven?e.transparencyIndex:null,delay:e.delayTime,disposalMethod:e.disposalMethod}},hdr:function(e){r.width=e.width;r.height=e.height;r.gtc=e.gct},img:function(e){a.push(Object.assign(e,n))},eof:function(){t({uris:s(r,a),delay:r.delay*10})}})},0)})})}});