-
Notifications
You must be signed in to change notification settings - Fork 8
/
stackblur.min.js
1 lines (1 loc) · 7.11 KB
/
stackblur.min.js
1
var Blur=Class.extend({defaults:{radius:1,mul_table:[512,512,456,512,328,456,335,512,405,328,271,456,388,335,292,512,454,405,364,328,298,271,496,456,420,388,360,335,312,292,273,512,482,454,428,405,383,364,345,328,312,298,284,271,259,496,475,456,437,420,404,388,374,360,347,335,323,312,302,292,282,273,265,512,497,482,468,454,441,428,417,405,394,383,373,364,354,345,337,328,320,312,305,298,291,284,278,271,265,259,507,496,485,475,465,456,446,437,428,420,412,404,396,388,381,374,367,360,354,347,341,335,329,323,318,312,307,302,297,292,287,282,278,273,269,265,261,512,505,497,489,482,475,468,461,454,447,441,435,428,422,417,411,405,399,394,389,383,378,373,368,364,359,354,350,345,341,337,332,328,324,320,316,312,309,305,301,298,294,291,287,284,281,278,274,271,268,265,262,259,257,507,501,496,491,485,480,475,470,465,460,456,451,446,442,437,433,428,424,420,416,412,408,404,400,396,392,388,385,381,377,374,370,367,363,360,357,354,350,347,344,341,338,335,332,329,326,323,320,318,315,312,310,307,304,302,299,297,294,292,289,287,285,282,280,278,275,273,271,269,267,265,263,261,259],shg_table:[9,11,12,13,13,14,14,15,15,15,15,16,16,16,16,17,17,17,17,17,17,17,18,18,18,18,18,18,18,18,18,19,19,19,19,19,19,19,19,19,19,19,19,19,19,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24],fullscreen:!1,styles:{backgroundRepeat:"no-repeat",backgroundPosition:"center center",backgroundAttachment:"fixed",width:"100%",minHeight:"100%",WebkitBackgroundSize:"cover",MozBackgroundSize:"cover",OBackgroundSize:"cover",backgroundSize:"cover"}},init:function(e){for(var t in e)this.defaults[t]=e[t];if(this.defaults.fullscreen)for(var t in this.defaults.styles)this.defaults.el.style[t]=this.defaults.styles[t];var n=this,r=this.defaults.radius,i=document.createElement("img");i.src=e.path,i.onload=function(t){var s=document.createElement("canvas");n.defaults.width=i.naturalWidth,n.defaults.height=i.naturalHeight,s.id="canvas",s.style.display="none",s.style.width=n.defaults.width+"px",s.style.height=n.defaults.height+"px",s.width=n.defaults.width,s.height=n.defaults.height,document.body.insertBefore(s,document.body.childNodes[0]);var o=s.getContext("2d");o.clearRect(0,0,n.defaults.width,n.defaults.height),o.drawImage(i,0,0);if(isNaN(r)||r<1)throw new Error("Radius required.");e.path.match(/\w.\S+png/)?n.stackBlurCanvasRGBA(function(){n.swap(s.toDataURL("image/png"))}):n.stackBlurCanvasRGB(function(){n.swap(s.toDataURL("image/jpg"))})}},stackBlurCanvasRGBA:function(e){var t=this,n=document.getElementById("canvas"),r=n.getContext("2d"),i=this.defaults.radius,s=0,o=0,u=this.defaults.width,a=this.defaults.height,f;if(isNaN(i)||i<1)return;i|=0;try{try{f=r.getImageData(s,o,u,a)}catch(l){try{netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead"),f=r.getImageData(s,o,u,a)}catch(l){throw new Error("unable to access local image data: "+l)}}}catch(l){throw new Error("unable to access image data: "+l)}var c=f.data,h,p,d,v,m,g,y,b,w,E,S,x,T,N,C,k,L,A,O,M,_,D,P,H,B=i+i+1,j=u<<2,F=u-1,I=a-1,q=i+1,R=q*(q+1)/2,U=t.BlurStack(),z=U;for(d=1;d<B;d++){z=z.next=t.BlurStack();if(d==q)var W=z}z.next=U;var X=null,V=null;y=g=0;var $=this.defaults.mul_table[i],J=this.defaults.shg_table[i];for(p=0;p<a;p++){k=L=A=O=b=w=E=S=0,x=q*(M=c[g]),T=q*(_=c[g+1]),N=q*(D=c[g+2]),C=q*(P=c[g+3]),b+=R*M,w+=R*_,E+=R*D,S+=R*P,z=U;for(d=0;d<q;d++)z.r=M,z.g=_,z.b=D,z.a=P,z=z.next;for(d=1;d<q;d++)v=g+((F<d?F:d)<<2),b+=(z.r=M=c[v])*(H=q-d),w+=(z.g=_=c[v+1])*H,E+=(z.b=D=c[v+2])*H,S+=(z.a=P=c[v+3])*H,k+=M,L+=_,A+=D,O+=P,z=z.next;X=U,V=W;for(h=0;h<u;h++)c[g+3]=P=S*$>>J,P!=0?(P=255/P,c[g]=(b*$>>J)*P,c[g+1]=(w*$>>J)*P,c[g+2]=(E*$>>J)*P):c[g]=c[g+1]=c[g+2]=0,b-=x,w-=T,E-=N,S-=C,x-=X.r,T-=X.g,N-=X.b,C-=X.a,v=y+((v=h+i+1)<F?v:F)<<2,k+=X.r=c[v],L+=X.g=c[v+1],A+=X.b=c[v+2],O+=X.a=c[v+3],b+=k,w+=L,E+=A,S+=O,X=X.next,x+=M=V.r,T+=_=V.g,N+=D=V.b,C+=P=V.a,k-=M,L-=_,A-=D,O-=P,V=V.next,g+=4;y+=u}for(h=0;h<u;h++){L=A=O=k=w=E=S=b=0,g=h<<2,x=q*(M=c[g]),T=q*(_=c[g+1]),N=q*(D=c[g+2]),C=q*(P=c[g+3]),b+=R*M,w+=R*_,E+=R*D,S+=R*P,z=U;for(d=0;d<q;d++)z.r=M,z.g=_,z.b=D,z.a=P,z=z.next;m=u;for(d=1;d<=i;d++)g=m+h<<2,b+=(z.r=M=c[g])*(H=q-d),w+=(z.g=_=c[g+1])*H,E+=(z.b=D=c[g+2])*H,S+=(z.a=P=c[g+3])*H,k+=M,L+=_,A+=D,O+=P,z=z.next,d<I&&(m+=u);g=h,X=U,V=W;for(p=0;p<a;p++)v=g<<2,c[v+3]=P=S*$>>J,P>0?(P=255/P,c[v]=(b*$>>J)*P,c[v+1]=(w*$>>J)*P,c[v+2]=(E*$>>J)*P):c[v]=c[v+1]=c[v+2]=0,b-=x,w-=T,E-=N,S-=C,x-=X.r,T-=X.g,N-=X.b,C-=X.a,v=h+((v=p+q)<I?v:I)*u<<2,b+=k+=X.r=c[v],w+=L+=X.g=c[v+1],E+=A+=X.b=c[v+2],S+=O+=X.a=c[v+3],X=X.next,x+=M=V.r,T+=_=V.g,N+=D=V.b,C+=P=V.a,k-=M,L-=_,A-=D,O-=P,V=V.next,g+=u}r.putImageData(f,s,o),e()},stackBlurCanvasRGB:function(e){var t=this,n=document.getElementById("canvas"),r=n.getContext("2d"),i=this.defaults.radius,s=0,o=0,u=this.defaults.width,a=this.defaults.height,f;if(isNaN(i)||i<1)return;i|=0;try{try{f=r.getImageData(s,o,u,a)}catch(l){try{netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead"),f=r.getImageData(s,o,u,a)}catch(l){throw new Error("unable to access local image data: "+l)}}}catch(l){throw new Error("unable to access image data: "+l)}var c=f.data,h,p,d,v,m,g,y,b,w,E,S,x,T,N,C,k,L,A,O,M,_=i+i+1,D=u<<2,P=u-1,H=a-1,B=i+1,j=B*(B+1)/2,F=t.BlurStack(),I=F;for(d=1;d<_;d++){I=I.next=t.BlurStack();if(d==B)var q=I}I.next=F;var R=null,U=null;y=g=0;var z=this.defaults.mul_table[i],W=this.defaults.shg_table[i];for(p=0;p<a;p++){N=C=k=b=w=E=0,S=B*(L=c[g]),x=B*(A=c[g+1]),T=B*(O=c[g+2]),b+=j*L,w+=j*A,E+=j*O,I=F;for(d=0;d<B;d++)I.r=L,I.g=A,I.b=O,I=I.next;for(d=1;d<B;d++)v=g+((P<d?P:d)<<2),b+=(I.r=L=c[v])*(M=B-d),w+=(I.g=A=c[v+1])*M,E+=(I.b=O=c[v+2])*M,N+=L,C+=A,k+=O,I=I.next;R=F,U=q;for(h=0;h<u;h++)c[g]=b*z>>W,c[g+1]=w*z>>W,c[g+2]=E*z>>W,b-=S,w-=x,E-=T,S-=R.r,x-=R.g,T-=R.b,v=y+((v=h+i+1)<P?v:P)<<2,N+=R.r=c[v],C+=R.g=c[v+1],k+=R.b=c[v+2],b+=N,w+=C,E+=k,R=R.next,S+=L=U.r,x+=A=U.g,T+=O=U.b,N-=L,C-=A,k-=O,U=U.next,g+=4;y+=u}for(h=0;h<u;h++){C=k=N=w=E=b=0,g=h<<2,S=B*(L=c[g]),x=B*(A=c[g+1]),T=B*(O=c[g+2]),b+=j*L,w+=j*A,E+=j*O,I=F;for(d=0;d<B;d++)I.r=L,I.g=A,I.b=O,I=I.next;m=u;for(d=1;d<=i;d++)g=m+h<<2,b+=(I.r=L=c[g])*(M=B-d),w+=(I.g=A=c[g+1])*M,E+=(I.b=O=c[g+2])*M,N+=L,C+=A,k+=O,I=I.next,d<H&&(m+=u);g=h,R=F,U=q;for(p=0;p<a;p++)v=g<<2,c[v]=b*z>>W,c[v+1]=w*z>>W,c[v+2]=E*z>>W,b-=S,w-=x,E-=T,S-=R.r,x-=R.g,T-=R.b,v=h+((v=p+B)<H?v:H)*u<<2,b+=N+=R.r=c[v],w+=C+=R.g=c[v+1],E+=k+=R.b=c[v+2],R=R.next,S+=L=U.r,x+=A=U.g,T+=O=U.b,N-=L,C-=A,k-=O,U=U.next,g+=u}r.putImageData(f,s,o),e()},BlurStack:function(){return{r:0,g:0,b:0,a:0,next:null}},remove:function(e){c=document.getElementById(e),c.parentNode.removeChild(c)},swap:function(e){this.remove("canvas"),this.defaults.el instanceof HTMLImageElement?this.defaults.el.src=e:this.defaults.el.style.backgroundImage="url("+e+")"}});