Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //ImageData pixel diff_map (difference from surrounding pixels radius 10
- self.get_points_in_cir=function(r){
- var px=[],
- cx=-r,cy=-r;
- while(cx<r){
- if(Math.round(Math.sqrt((cy*cy)+(cx*cx)))<r){
- px.splice(px.length,0,cx,cy);
- } cy++; if(cy>r){cy=-r;cx++;}
- }
- return px;
- };
- self.diff_map=function(idat,a_c_rad,inv_dif){
- if(a_c_rad>25||(!a_c_rad)||typeof a_c_rad!="number")a_c_rad=10;
- var w=idat.width,h=idat.height,X=0,Y=0,
- D=new DataView(idat.data.buffer), L=D.byteLength,
- cir=get_points_in_cir(a_c_rad), ci=0,
- aox,aoy, //actuall
- tv, //temporary storage variables
- b0,b1,b2,b3, //current pxl (bytes0-3)
- p0,p1,p2,p3, //current real pxl for diff_map
- HD0=0,HD1=0,HD2=0,HD3=0, LD0=255,LD1=255,LD2=255,LD3=255, //High Diff, Low Diff for diff_map
- ad0=0,ad1=0,ad2=0,ad3=0, //average diff
- a0=0,a1=0,a2=0,a3=0, //avg pxl
- avc=0, //avg count
- id2=new ImageData(w,h), D2=new DataView(id2.data.buffer), //output blur
- id3=new ImageData(w,h), D3=new DataView(id3.data.buffer); //output diff_map
- while(X<w&&Y<h){
- if(ci<cir.length){ //iterate through cir
- aox=cir[ci]+X;aoy=cir[ci+1]+Y;
- if(aox>0&&aox<w&&aoy>0&&aoy<h){
- b0=(tv=D.getUint32(((aoy*w)+aox)<<2))>>24;
- b1=(tv>>16)&255;b2=(tv>>8)&255;b3=tv&255;
- if(aox==X&&aoy==Y){ p0=b0;p1=b1;p2=b2;p3=b3; } //this line for diff map
- avc++;
- a0+=b0;a1+=b1;a2+=b2;a3+=b3;
- }
- ci+=2;
- }else{ //now done with 1 pix and it's cir (got average for that px)
- a0/=avc;a1/=avc;a2/=avc;a3/=avc;
- D2.setUint32(tv=((Y*w)+X)<<2,(a0<<24)+(a1<<16)+(a2<<8)+a3); //put the found average in the avg_map (blur_map) ImageData Object
- //do next step here (diff_map) (remember to invert for drawing on canvas)
- b0=p0-a0;b1=p1-a1;b2=p2-a2;b3=p3-a3;
- b0<0?b0=-b0:0; b1<0?b1=-b1:0; b2<0?b2=-b2:0; b3<0?b3=-b3:0; //absolute value
- if(b0<LD0)LD0=b0; if(b0>HD0)HD0=b0;
- if(b1<LD1)LD1=b1; if(b1>HD1)HD1=b1;
- if(b2<LD2)LD2=b2; if(b2>HD2)HD2=b2;
- if(b3<LD3)LD3=b3; if(b3>HD3)HD3=b3;
- ad0+=b0;ad1+=b1;ad2+=b2;ad3+=b3; //average diff
- if(inv_dif){b0^=255;b1^=255;b2^=255;b3^=255;}
- D3.setUint32(tv,(b0<<24)+(b1<<16)+(b2<<8)+b3);
- //end diff_map part
- a0=(a1=(a2=(a3=0))); //set avgerage vars to 0
- ci=0; avc=0;
- X++; if(X==w){ Y++; X=0;} //go to next pixel
- }
- }
- return {"avg":id2,"dif":id3,"hiDif":[HD0,HD1,HD2,HD3],"loDif":[LD0,LD1,LD2,LD3],"avDif":[ad0/L,ad1/L,ad2/L,ad3/L]};
- };
- //this \/ goes before that /\
- self.cnvs1=document.createElement("canvas");
- cnvs1.ctx=cnvs1.getContext("2d");
- document.body.appendChild(cnvs1);
- cnvs1.style.border="2px solid black";
- self.opnb=document.createElement("button");
- opnb.innerText="open image"; document.body.appendChild(opnb);
- opnb.onclick=function(){
- TJA.openf(function(d,f){ self.imgFile_1=f;
- f.blb=new Blob([f.data=d],{"type":f.type});
- f.url=URL.createObjectURL(f.blb);
- var ie=document.createElement("img");
- ie.onload=function(){
- cnvs1.width=ie.width; cnvs1.height=ie.height;
- cnvs1.ctx.drawImage(ie,0,0,ie.width,ie.height);
- };
- ie.src=f.url;
- });
- };
- self.wkb1=new Blob(["self.get_points_in_cir="+get_points_in_cir.toString()+";\nself.diff_map="+diff_map.toString()+";\n\
- self.onmessage=function(e){var d=e.data; if(d.js){try{eval(d.js);}catch(e){console.log(e);}} if(d.img_p){ var r=diff_map(d.img_p,d.br,d.inv); self.postMessage(r); } };"],{"type":"text/javascript"});
- self.wkr1=new Worker(wkb1.url=URL.createObjectURL(wkb1));
- self.cnvs2=cnvs1.cloneNode();
- document.body.appendChild(cnvs2);
- cnvs2.ctx=cnvs2.getContext("2d");
- self.prcb=document.createElement("button");
- prcb.innerText="proccess image"; document.body.appendChild(prcb);
- wkr1.onmessage=function(e){self._LM_=e.data;if(e.data.dif)cnvs2.ctx.putImageData(e.data.dif,0,0);};
- prcb.onclick=function(){ cnvs2.width=cnvs1.width;cnvs2.height=cnvs1.height;
- self.idat1=cnvs1.ctx.getImageData(0,0,cnvs1.width,cnvs1.height);
- wkr1.postMessage({"img_p":idat1});
- };
- //some pixels turn red/cyan in diffmap for unknown reason
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement