-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgiffer.min.js
3 lines (3 loc) · 7.09 KB
/
giffer.min.js
1
2
3
// Giffer.js
// https://github.com/drdelambre/giffer
var Giffer=function(){function a(){var c,d,a=Array.prototype.slice.call(arguments,0),b={};for(c=a.length-1;c>=0;c--)for(d in a[c])b[d]=a[c][d];return b}function b(a){return a.reduce(function(a,b){return 2*a+b},0)}function c(a){var c,b=[];for(c=7;c>=0;c--)b.push(!!(a&1<<c));return b}function d(a){var b={data:a,len:a.length,pos:0};return b.readByte=function(){if(b.pos>=b.len)throw new Error("Attempted to read past end of stream");return 255&b.data.charCodeAt(b.pos++)},b.readBytes=function(a){var d,c=[];for(d=0;a>d;d++)c.push(b.readByte());return c},b.read=function(a){var d,c="";for(d=0;a>d;d++)c+=String.fromCharCode(b.readByte());return c},b.readUnsigned=function(){var a=b.readBytes(2);return(a[1]<<8)+a[0]},b}function e(a,b){for(var j,k,l,c=1<<a,d=c+1,e=a+1,f="",g=[],i=0;;){for(j=k,k=0,l=0;e>l;l++)b.charCodeAt(i>>3)&1<<(7&i)&&(k|=1<<l),i++;if(k===d)break;if(k!==c){if(k<f.length)j!==c&&(res=[],res.push.apply(res,f[j]),res.push.call(res,f[k][0]),f.push(res));else{if(k!==f.length)throw new Error("Invalid LZW code.");res=[],res.push.apply(res,f[j]),res.push.call(res,f[j][0]),f.push(res)}g.push.apply(g,f[k]),f.length===1<<e&&12>e&&e++}else{for(f=[],e=a+1,l=0;c>l;l++)f[l]=[l];f[c]=[],f[d]=null}}return g}function f(a){var c,b="";do c=a.readByte(),b+=a.read(c);while(0!==c);return b}function g(a,b){var d,c=[];for(d=0;a>d;d++)c.push(b.readBytes(3));return c}function i(){var a=[],b=function(b){"function"==typeof b&&a.push(b)};return b.fire=function(){var c,b=Array.prototype.slice.call(arguments,0);for(c=0;c<a.length;c++)a[c].apply(this,b)},b.disconnect=function(b){for(var c=0;c<a.length;c++)if(a[c]===b)return a.splice(c,1),void 0},b}!function(){var c,a=0,b=["webkit","moz"];for(c=0;c<b.length&&!window.requestAnimationFrame;++c)window.requestAnimationFrame=window[b[c]+"RequestAnimationFrame"],window.cancelAnimationFrame=window[b[c]+"CancelAnimationFrame"]||window[b[c]+"CancelRequestAnimationFrame"];window.requestAnimationFrame||(window.requestAnimationFrame=function(b){var d=(new Date).getTime(),e=Math.max(0,16-(d-a)),f=window.setTimeout(function(){b(d+e)},e);return a=d+e,f}),window.cancelAnimationFrame||(window.cancelAnimationFrame=function(a){clearTimeout(a)})}();var h={gif:function(a,b){b=b||{},h.header(a,b),h.block(a,b)},header:function(d,e){var f=d.read(3);if("GIF"!==f)throw new Error("Not a GIF file.");var h={sig:f,ver:d.read(3),width:d.readUnsigned(),height:d.readUnsigned(),_bits:c(d.readByte()),bgColor:d.readByte(),pixelAspectRatio:d.readByte()},i={gctFlag:h._bits.shift(),colorRes:b(h._bits.splice(0,3)),sorted:h._bits.shift(),gctSize:b(h._bits.splice(0,3))};i.gctFlag&&(i.gct=g(1<<i.gctSize+1,d)),delete h._bits,e.hdr&&e.hdr(a(h,i))},block:function(a,b){var c={sentinel:a.readByte(),type:null};switch(String.fromCharCode(c.sentinel)){case"!":c.type="ext",h.ext(c,a,b);break;case",":c.type="img",h.image(c,a,b);break;case";":c.type="eof",b.eof&&b.eof(c);break;default:throw new Error("Unknown block: 0x"+c.sentinel.toString(16))}"eof"!==c.type&&h.block(a,b)},image:function(d,h,i){var j=function(a,b){var h,i,c=new Array(a.length),d=a.length/b,e=[0,4,2,1],f=[8,8,4,2],g=0;for(h=0;4>h;h++)for(i=e[h];d>i;i+=f[h])c.splice.apply(c,[i*b,b].concat(a.slice(g*b,(g+1)*b))),g++;return c},k={leftPos:h.readUnsigned(),topPos:h.readUnsigned(),width:h.readUnsigned(),height:h.readUnsigned(),_bits:c(h.readByte()),lzwMinCodeSize:h.readByte(),pixels:null},l={lctFlag:k._bits.shift(),interlaced:k._bits.shift(),sorted:k._bits.shift(),reserved:k._bits.splice(0,2),lctSize:b(k._bits.splice(0,3))};l.lctFlag&&(l.lct=g(1<<l.lctSize+1,h)),k.pixels=e(k.lzwMinCodeSize,f(h)),l.interlaced&&(k.pixels=j(k.pixels,k.width)),delete k._bits,i.img&&i.img(a(d,k,l))},ext:function(a,b,c){switch(a.label=b.readByte(),a.label){case 249:a.extType="gce",h.gcext(a,b,c);break;case 254:a.extType="com",h.comext(a,b,c);break;case 1:a.extType="pte",h.ptext(a,b,c);break;case 255:a.extType="app",h.appext(a,b,c);break;default:a.extType="unknown",h.unknownext(a,b,c)}},gcext:function(d,e,f){var h=(e.readByte(),c(e.readByte())),i={reserved:h.splice(0,3),disposalMethod:b(h.splice(0,3)),userInput:h.shift(),transparencyGiven:h.shift(),delayTime:e.readUnsigned(),transparencyIndex:e.readByte(),terminator:e.readByte()};f.gce&&f.gce(a(d,i))},comext:function(a,b,c){a.comment=f(b),c.com&&c.com(a)},ptext:function(a,b,c){b.readByte(),a.ptHeader=b.readBytes(12),a.ptData=f(b),c.pte&&c.pte(a)},appext:function(a,b,c){switch(b.readByte(),a.identifier=b.read(8),a.authCode=b.read(3),a.identifier){case"NETSCAPE":h.netscape(a,b,c);break;default:h.unknownApp(a,b,c)}},netscape:function(a,b,c){b.readByte(),a.unknown=b.readByte(),a.iterations=b.readUnsigned(),a.terminator=b.readByte(),c.app&&c.app.NETSCAPE&&c.app.NETSCAPE(a)},unknownApp:function(a,b,c){a.appData=f(b),c.app&&c.app[a.identifier]&&c.app[a.identifier](a)},unknownext:function(a,b,c){a.data=f(b),c.unknown&&c.unknown(a)}};return function(){var f,a={width:0,height:0,frames:[],current_frame:0,playing:!1},b={ready:!1,transparency:null,disposalMethod:null,lastDisposalMethod:null,startTime:0,delay:0,frame:document.createElement("canvas").getContext("2d")},c=0,e=1;a.load=function(e){var i,j={hdr:function(c){a.width=b.frame.canvas.width=c.width,a.height=b.frame.canvas.height=c.height,f=c.gct},gce:function(c){b.ready&&(a.frames.push({data:b.frame.getImageData(0,0,a.width,a.height),startTime:b.startTime,delay:b.delay}),b.startTime+=b.delay,b.lastDisposalMethod=b.disposalMethod,b.frame.canvas.width=a.width),b.transparency=c.transparencyGiven?c.transparencyIndex:null,b.delay=10*c.delayTime,b.disposalMethod=c.disposalMethod},img:function(a){var k,l,m,c=b.transparency,d=b.lastDisposalMethod,e=a.lctFlag?a.lct:f,g=b.frame.getImageData(a.leftPos,a.topPos,a.width,a.height),h=g.data,i=a.pixels,j=i.length;for(l=0;j>l;l++)m=4*l,c!==i[l]?(k=e[i[l]],h[m+0]=k[0],h[m+1]=k[1],h[m+2]=k[2],h[m+3]=255):(2===d||3===d)&&(h[m+3]=0);b.frame.putImageData(g,a.leftPos,a.topPos),b.ready=!0},eof:function(){a.frames.push({data:b.frame.getImageData(0,0,a.width,a.height),startTime:b.startTime,delay:b.delay}),c=b.startTime+b.delay,delete b,a.play(),a.on_load.fire(a)}},k=new XMLHttpRequest;k.overrideMimeType("text/plain; charset=x-user-defined"),k.onload=function(){i=Date.now(),h.gif(d(k.responseText),j)},k.open("GET",e,!0),k.send()},a.play=function(){a.playing=!0,a.on_play.fire(a),g()},a.pause=function(){a.playing=!1,a.on_pause.fire(a)},a.goto=function(b){a.current_frame=b%a.frames.length,g(),a.playing||window.requestAnimationFrame(function(){a.on_frame.fire(a.frames[a.current_frame].data)})},a.speed=function(a){return arguments.length?(a=parseFloat(a),isNaN(a)||(e=a,g()),void 0):e};var g=function(){function d(){!function(a){a(Date.now()),window.requestAnimationFrame(d)}(function(d){if(a.playing){var i,j,f=(d-b)*e%c,g=a.frames.length,h=a.current_frame;for(i=0;g>i;i++)if(j=a.frames[(h+i)%g],!(f<j.startTime||f>j.startTime+j.delay)){if(i===a.current_frame)return;return a.current_frame=(h+i)%g,a.on_frame.fire(a.frames[a.current_frame].data),void 0}}})}var b=Date.now();return window.requestAnimationFrame(d),function(){a.playing&&(b=Date.now()-a.frames[a.current_frame].startTime/e)}}();return a.on_load=i(),a.on_frame=i(),a.on_play=i(),a.on_pause=i(),a}}();