Advertisement
IWBH_01

Image color value difference map

Jul 5th, 2020
1,260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. //ImageData  pixel diff_map (difference from surrounding pixels radius 10
  4.  
  5.  
  6. self.get_points_in_cir=function(r){
  7.  var px=[],
  8.  cx=-r,cy=-r;
  9.  while(cx<r){
  10.   if(Math.round(Math.sqrt((cy*cy)+(cx*cx)))<r){
  11.    px.splice(px.length,0,cx,cy);
  12.   } cy++; if(cy>r){cy=-r;cx++;}
  13.  }
  14.  return px;
  15. };
  16.  
  17.  
  18.  
  19. self.diff_map=function(idat,a_c_rad,inv_dif){
  20.  if(a_c_rad>25||(!a_c_rad)||typeof a_c_rad!="number")a_c_rad=10;
  21.  var w=idat.width,h=idat.height,X=0,Y=0,
  22.  D=new DataView(idat.data.buffer), L=D.byteLength,
  23.  cir=get_points_in_cir(a_c_rad), ci=0,
  24.  aox,aoy, //actuall
  25.  tv,  //temporary storage variables
  26.  b0,b1,b2,b3, //current pxl (bytes0-3)
  27.  
  28.  p0,p1,p2,p3, //current real pxl for diff_map
  29.  HD0=0,HD1=0,HD2=0,HD3=0, LD0=255,LD1=255,LD2=255,LD3=255,  //High Diff, Low Diff for diff_map
  30.  ad0=0,ad1=0,ad2=0,ad3=0,  //average diff
  31.  
  32.  a0=0,a1=0,a2=0,a3=0, //avg pxl
  33.  avc=0, //avg count
  34.  id2=new ImageData(w,h), D2=new DataView(id2.data.buffer), //output blur
  35.  
  36.  id3=new ImageData(w,h), D3=new DataView(id3.data.buffer); //output diff_map
  37.  
  38.  while(X<w&&Y<h){
  39.   if(ci<cir.length){ //iterate through cir
  40.    aox=cir[ci]+X;aoy=cir[ci+1]+Y;
  41.    if(aox>0&&aox<w&&aoy>0&&aoy<h){
  42.     b0=(tv=D.getUint32(((aoy*w)+aox)<<2))>>24;
  43.     b1=(tv>>16)&255;b2=(tv>>8)&255;b3=tv&255;
  44.  
  45.     if(aox==X&&aoy==Y){ p0=b0;p1=b1;p2=b2;p3=b3; } //this line for diff map
  46.  
  47.     avc++;
  48.     a0+=b0;a1+=b1;a2+=b2;a3+=b3;
  49.  
  50.    }
  51.    ci+=2;
  52.   }else{ //now done with 1 pix and it's cir (got average for that px)
  53.     a0/=avc;a1/=avc;a2/=avc;a3/=avc;
  54.    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
  55.    
  56.    //do next step here (diff_map) (remember to invert for drawing on canvas)
  57.    b0=p0-a0;b1=p1-a1;b2=p2-a2;b3=p3-a3;
  58.    b0<0?b0=-b0:0; b1<0?b1=-b1:0; b2<0?b2=-b2:0; b3<0?b3=-b3:0; //absolute value
  59.    if(b0<LD0)LD0=b0; if(b0>HD0)HD0=b0;
  60.    if(b1<LD1)LD1=b1; if(b1>HD1)HD1=b1;
  61.    if(b2<LD2)LD2=b2; if(b2>HD2)HD2=b2;
  62.    if(b3<LD3)LD3=b3; if(b3>HD3)HD3=b3;
  63.    ad0+=b0;ad1+=b1;ad2+=b2;ad3+=b3; //average diff
  64.    if(inv_dif){b0^=255;b1^=255;b2^=255;b3^=255;}
  65.    D3.setUint32(tv,(b0<<24)+(b1<<16)+(b2<<8)+b3);
  66.    //end diff_map part
  67.  
  68.    a0=(a1=(a2=(a3=0))); //set avgerage vars to 0
  69.    ci=0; avc=0;
  70.    X++; if(X==w){ Y++; X=0;}  //go to next pixel
  71.   }
  72.  }
  73.  return {"avg":id2,"dif":id3,"hiDif":[HD0,HD1,HD2,HD3],"loDif":[LD0,LD1,LD2,LD3],"avDif":[ad0/L,ad1/L,ad2/L,ad3/L]};
  74. };
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81. //this \/ goes before that /\
  82.  
  83.  
  84. self.cnvs1=document.createElement("canvas");
  85. cnvs1.ctx=cnvs1.getContext("2d");
  86. document.body.appendChild(cnvs1);
  87. cnvs1.style.border="2px solid black";
  88. self.opnb=document.createElement("button");
  89. opnb.innerText="open image"; document.body.appendChild(opnb);
  90.  
  91. opnb.onclick=function(){
  92.  
  93.  TJA.openf(function(d,f){ self.imgFile_1=f;
  94.   f.blb=new Blob([f.data=d],{"type":f.type});
  95.   f.url=URL.createObjectURL(f.blb);
  96.   var ie=document.createElement("img");
  97.   ie.onload=function(){
  98.    cnvs1.width=ie.width; cnvs1.height=ie.height;
  99.    cnvs1.ctx.drawImage(ie,0,0,ie.width,ie.height);
  100.   };
  101.   ie.src=f.url;
  102.  });
  103. };
  104.  
  105.  
  106. self.wkb1=new Blob(["self.get_points_in_cir="+get_points_in_cir.toString()+";\nself.diff_map="+diff_map.toString()+";\n\
  107. 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"});
  108.  
  109. self.wkr1=new Worker(wkb1.url=URL.createObjectURL(wkb1));
  110.  
  111. self.cnvs2=cnvs1.cloneNode();
  112. document.body.appendChild(cnvs2);
  113. cnvs2.ctx=cnvs2.getContext("2d");
  114.  
  115. self.prcb=document.createElement("button");
  116. prcb.innerText="proccess image"; document.body.appendChild(prcb);
  117.  
  118. wkr1.onmessage=function(e){self._LM_=e.data;if(e.data.dif)cnvs2.ctx.putImageData(e.data.dif,0,0);};
  119.  
  120. prcb.onclick=function(){ cnvs2.width=cnvs1.width;cnvs2.height=cnvs1.height;
  121.  self.idat1=cnvs1.ctx.getImageData(0,0,cnvs1.width,cnvs1.height);
  122.  wkr1.postMessage({"img_p":idat1});
  123. };
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130. //some pixels turn red/cyan in diffmap for unknown reason
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement