// Magic Wand (Fuzzy Selection Tool) for Javascript // // The MIT License (MIT) // // Copyright (c) 2014, Ryasnoy Paul (ryasnoypaul@gmail.com) // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. MagicWand=function(){var a={};a.floodFill=function(m,z,B,d,A){var a,j,i,b,g,f,l,p,q,u,e=m.data,n=m.width,r=m.height,t=m.bytes,v=-1,x=n+1,w=-1,y=r+1,c=B*n+z,s=new Uint8Array(n*r),k=new Uint8Array(A?A:n*r);if(k[c]===1)return null;c=c*t;var h=[e[c],e[c+1],e[c+2],e[c+3]],o=[{y:B,left:z-1,right:z+1,dir:1}];do{b=o.shift();u=false;for(j=b.left+1;jd||a<-d)continue;a=e[c+1]-h[1];if(a>d||a<-d)continue;a=e[c+2]-h[2];if(a>d||a<-d)continue;u=true;s[l+j]=1;k[l+j]=1;f=j-1;while(f>-1){p=l+f;c=p*t;if(k[p]===1)break;a=e[c]-h[0];if(a>d||a<-d)break;a=e[c+1]-h[1];if(a>d||a<-d)break;a=e[c+2]-h[2];if(a>d||a<-d)break;s[p]=1;k[p]=1;f--}g=j+1;while(gd||a<-d)break;a=e[c+1]-h[1];if(a>d||a<-d)break;a=e[c+2]-h[2];if(a>d||a<-d)break;s[q]=1;k[q]=1;g++}if(fv)v=g-1;i=b.y-b.dir;if(i>=0&&i=0&&iw)w=b.y}}while(o.length>0);return{data:s,width:m.width,height:m.height,bounds:{minX:x,minY:y,maxX:v,maxY:w}}};a.gaussBlur=function(e,b){for(var m,l,c,d,k,i,j,f=b*2+1,x=b*b,h=new Float32Array(f),p=0,g=e.width,n=e.height,q=e.data,v=e.bounds.minX,t=e.bounds.maxX,w=e.bounds.minY,u=e.bounds.maxY,a=0;a0?b-c:0;j=r-c0?b-d:0;j=s-d.5?1:0}return{data:o,width:g,height:n,bounds:{minX:v,minY:w,maxX:t,maxY:u}}};function b(k,g,y){var a,i,w,c,b,h,e,d=k.width,v=k.height,l=k.data,f=new Uint8Array(l),r=k.bounds.minX,m=k.bounds.maxX,s=k.bounds.minY,n=k.bounds.maxY,u=d*v,o=new Uint8Array(u),j=[],B=Math.max(r,1),C=Math.min(m,d-2),D=Math.max(s,1),E=Math.min(n,v-2);if(y&&y.length>0)for(b=0;b0?g-a:0;t=z-a0?g-c:0;t=A-c0?a-d:0;s=y-d.5){q[h]=1;if(dk)k=d;if(el)l=e;continue}r=a-e>0?a-e:0;s=z-e.5){q[h]=1;if(dk)k=d;if(el)l=e}else q[h]=0}return{data:q,width:j,height:B,bounds:{minX:m,minY:n,maxX:k,maxY:l}}};a.createBorderMask=function(g){for(var a,l,m,n,f=g.width,p=g.height,c=g.data,d=g.bounds.minX,h=g.bounds.maxX,e=g.bounds.minY,i=g.bounds.maxY,k=h-d+1,o=i-e+1,j=new Uint8Array(k*o),q=Math.max(d,1),r=Math.min(h,f-2),s=Math.max(e,1),t=Math.min(i,p-2),b=s;b=Math.sqrt(r*r+m*m))o=r;else if(r>=Math.sqrt(q*q+m*m))o=q;else o=Math.abs((c*i.x-b*i.y+h.x*j.y-j.x*h.y)/m);if(o>t){p=s;t=o}}if(t>x){l.push(p);n.push({first:a.first,last:p});n.push({first:p,last:a.last})}}while(n.length>0);k=[];f=l.length;l.sort(function(a,b){return a-b});for(d=0;d