ditherjs
Version:
Image dithering library
1 lines • 5.26 kB
JavaScript
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.DitherJS=e():t.DitherJS=e()}(this,function(){return function(t){function e(o){if(r[o])return r[o].exports;var n=r[o]={exports:{},id:o,loaded:!1};return t[o].call(n.exports,n,n.exports,e),n.loaded=!0,n.exports}var r={};return e.m=t,e.c=r,e.p="",e(0)}([function(t,e,r){var o=function(t){t.fn.ditherJS=function(t){var e=r(4),o=new e(t);o.dither(this.selector)}}(jQuery);t.exports=o},function(t,e){function r(t,e,r,o,n){for(var i,a,s,l,f,p,c,h,u,y,d,m,x,v,g=new Uint8ClampedArray(t),w=new Uint8ClampedArray(t),C=1/8,D=function(t,e){return 4*t+4*e*n},E=0;E<o;E+=r)for(var A=0;A<n;A+=r)for(p=4*A+4*E*n,i=p,a=p+1,s=p+2,l=p+3,c=new Array(g[i],g[a],g[s]),h=this.approximateColor(c,e),f=[],f[i]=g[i]-h[0],f[a]=g[a]-h[1],f[s]=g[s]-h[2],g[D(A+r,E)+0]+=C*f[i],g[D(A-r,E+r)+0]+=C*f[i],g[D(A,E+r)+0]+=C*f[i],g[D(A+r,E+r)+0]+=C*f[i],g[D(A+2*r,E)+0]+=C*f[i],g[D(A,E+2*r)+0]+=C*f[i],g[D(A+r,E)+1]+=C*f[a],g[D(A-r,E+r)+1]+=C*f[a],g[D(A,E+r)+1]+=C*f[a],g[D(A+r,E+r)+1]+=C*f[a],g[D(A+2*r,E)+1]+=C*f[a],g[D(A,E+2*r)+1]+=C*f[a],g[D(A+r,E)+2]+=C*f[s],g[D(A-r,E+r)+2]+=C*f[s],g[D(A,E+r)+2]+=C*f[s],g[D(A+r,E+r)+2]+=C*f[s],g[D(A+2*r,E)+2]+=C*f[s],g[D(A,E+2*r)+2]+=C*f[s],u=h[0],y=h[1],d=h[2],m=0;m<r;m++)for(x=0;x<r;x++)v=p+4*m+4*n*x,w[v]=u,w[v+1]=y,w[v+2]=d;return w}t.exports=r},function(t,e){function r(t,e,r,o,n){var i,a,s,l,f,p,c,h,u,d,m,v,g,w,C=new Uint8ClampedArray(t),D=new Uint8ClampedArray(t),E=1/16,A=function(t,e){return 4*t+4*e*n};for(y=0;y<o;y+=r)for(x=0;x<n;x+=r)for(p=4*x+4*y*n,i=p,a=p+1,s=p+2,l=p+3,c=new Array(C[i],C[a],C[s]),h=this.approximateColor(c,e),f=[],f[i]=C[i]-h[0],f[a]=C[a]-h[1],f[s]=C[s]-h[2],C[A(x+r,y)]=C[A(x+r,y)]+7*E*f[i],C[A(x-r,y+1)]=C[A(x-1,y+r)]+3*E*f[i],C[A(x,y+r)]=C[A(x,y+r)]+5*E*f[i],C[A(x+r,y+r)]=C[A(x+1,y+r)]+1*E*f[i],C[A(x+r,y)+1]=C[A(x+r,y)+1]+7*E*f[a],C[A(x-r,y+r)+1]=C[A(x-r,y+r)+1]+3*E*f[a],C[A(x,y+r)+1]=C[A(x,y+r)+1]+5*E*f[a],C[A(x+r,y+r)+1]=C[A(x+r,y+r)+1]+1*E*f[a],C[A(x+r,y)+2]=C[A(x+r,y)+2]+7*E*f[s],C[A(x-r,y+r)+2]=C[A(x-r,y+r)+2]+3*E*f[s],C[A(x,y+r)+2]=C[A(x,y+r)+2]+5*E*f[s],C[A(x+r,y+r)+2]=C[A(x+r,y+r)+2]+1*E*f[s],u=h[0],d=h[1],m=h[2],v=0;v<r;v++)for(g=0;g<r;g++)w=p+4*v+4*n*g,D[w]=u,D[w+1]=d,D[w+2]=m;return D}t.exports=r},function(t,e){function r(t,e,r,o,n){for(var i,a,s,l,f,p,c,h,u,y,d,m,x,v=new Uint8ClampedArray(t),g=3,w=new Array([1,9,3,11],[13,5,15,7],[4,12,2,10],[16,8,14,6]),C=0;C<o;C+=r)for(var D=0;D<n;D+=r)for(f=4*D+4*C*n,i=f,a=f+1,s=f+2,l=f+3,v[i]+=w[D%4][C%4]*g,v[a]+=w[D%4][C%4]*g,v[s]+=w[D%4][C%4]*g,p=new Array(v[i],v[a],v[s]),c=this.approximateColor(p,e),h=c[0],u=c[1],y=c[2],d=0;d<r;d++)for(m=0;m<r;m++)x=f+4*d+4*n*m,v[x]=h,v[x+1]=u,v[x+2]=y;return v}t.exports=r},function(t,e,r){var o=r(5);o.prototype.dither=function(t,e){"string"==typeof t&&this._fromSelector(t,e),"object"==typeof t&&t instanceof window.HTMLImageElement&&this._fromImgElement(t,e)},o.prototype._replaceElementWithCtx=function(t){var e,r=document.createElement("canvas");return r.height=t.clientHeight,r.width=t.clientWidth,t.parentNode.replaceChild(r,t),r.className=t.className,"hidden"===t.style.visibility&&(r.style.visibility="visible"),e=r.getContext("2d"),e.imageSmoothingEnabled=!1,e},o.prototype._fromImgElement=function(t,e){var r=t.clientHeight,o=t.clientWidth,n=this._replaceElementWithCtx(t);n.drawImage(t,0,0,o,r);var i=n.getImageData(0,0,o,r);this.ditherImageData(i,e),n.putImageData(i,0,0)},o.prototype._fromSelector=function(t,e){var r=this,o=document.querySelectorAll(t),n=function(t){return function(){r._fromImgElement(t,e)}};try{for(var i=0;i<o.length;i++){var a=o[i];a.style.visibility="hidden",a.src=a.src+"?"+Date.now(),a.onload=n(a)}}catch(s){console.error(s)}},t.exports=o},function(t,e,r){var o=r(6),n=function(t){this.options=t||{},this.options.algorithm=this.options.algorithm||"ordered",this.options.step=this.options.step||1,this.options.className=this.options.className||"dither",this.options.palette=this.options.palette||o.defaultPalette};n.orderedDither=r(3),n.atkinsonDither=r(1),n.errorDiffusionDither=r(2),n.prototype.ditherImageData=function(t,e){e=e||this.options;var r;if("diffusion"==e.algorithm)r=n.errorDiffusionDither;else if("ordered"==e.algorithm)r=n.orderedDither;else{if("atkinson"!=e.algorithm)throw o.errors.InvalidAlgorithm;r=n.atkinsonDither}var i;e.debug&&(i=Date.now());var a=r.call(this,t.data,e.palette,e.step,t.height,t.width);e.debug&&console.log("Microtime: ",Date.now()-i),t.data.set(a)},n.prototype.colorDistance=function(t,e){return Math.sqrt(Math.pow(t[0]-e[0],2)+Math.pow(t[1]-e[1],2)+Math.pow(t[2]-e[2],2))},n.prototype.approximateColor=function(t,e){var r=function(t,e,o,n){if(2==o.length)return t(e,n)<=t(e,o[1])?n:o[1];var i=o.slice(1);return n=t(e,n)<=t(e,o[1])?n:o[1],r(t,e,i,n)},o=r(this.colorDistance,t,e,e[0]);return o},t.exports=n},function(t,e,r){e.defaultPalette=[[0,0,0],[255,0,255],[0,255,255],[255,255,255]],e.errors={CanvasNotPresent:new Error("CanvasNotPresent"),TargetNotBuffer:new Error("TargetNotBuffer"),InvalidAlgorithm:new Error("InvalidAlgorithm")},e.targetTypes={selector:"SELECTOR",buffer:"BUFFER"},e.Canvas=null;try{e.Canvas=r(7)}catch(o){e.Canvas=null}},function(t,e){}])});