ColorLookup – the code

//  looks up the color in the PImage input at position of vector here
//  in pixel coordinates
//  use a fitting Coordinates(input) object to transform image coordinates to pixel coordinates

class ColorLookup{
  int thisWidth, thisHeight;
  float xLength,yLength,xPeriod,yPeriod;
  float x,y,dx,dy;
  int i,j,index;
  int f11,f12,f21,f22,rot,gruen,blau;
  int f0,f1,f2,f3;
  float w,w0,w1,w2,w3,reds,greens,blues,norm;
  PImage thisImage;

  ColorLookup(PImage input){
    thisImage=input;
    thisImage.loadPixels();
    thisWidth=input.width;
    thisHeight=input.height;
    println(thisWidth+" wh "+thisHeight);
    xLength=thisWidth-4;
    yLength=thisHeight-4;
    xPeriod=2*xLength;
    yPeriod=2*yLength;
  }
  
  void limits(Vector here){
    x=constrain(here.x,1,thisWidth-3);
    y=constrain(here.y,1,thisHeight-3);
  }

  
  void mirrors(Vector here){
    x=abs(here.x);
    if (x>=xPeriod) x=x%xPeriod;
    if(x >= xLength) x=xPeriod-x;
    x+=1;                                   // now 1 <= x <= thisWidth-3
    y=abs(here.y);
    if (y>=yPeriod) y=y%yPeriod;
    if(y >= yLength) y=yPeriod-y; 
    y+=1;                                   //  now 1 <= y <= thisHeight-3
  }
  
  
  //  direct color look up, faster
  color nextPixel(Vector here) {
//    mirrors(here);
    limits(here);
    i=round(x);
    j=round(y);
    return thisImage.pixels[i+j*thisWidth];
  }

  //  looking up color with linear interpolation
  color linearInterpolation(Vector here) {
  //  mirrors(here);
    limits(here);
    i=floor(x);
    dx=x-i;
    j=floor(y);
    dy=y-j;
    index=i+j*thisWidth;
    f11=thisImage.pixels[index];
    f21=thisImage.pixels[index+thisWidth];
    f12=thisImage.pixels[index+1];
    f22=thisImage.pixels[index+1+thisWidth];
    rot = round((1-dy)*(((f11 >> 16) & 0xFF)*(1-dx)+dx*((f12 >> 16) & 0xFF))
      +dy*(((f21 >> 16) & 0xFF)*(1-dx)+dx*((f22 >> 16) & 0xFF))); 
    gruen = round((1-dy)*(((f11 >> 8) & 0xFF)*(1-dx)+dx*((f12 >> 8) & 0xFF))
      +dy*(((f21 >> 8) & 0xFF)*(1-dx)+dx*((f22 >> 8) & 0xFF))); 
    blau = round((1-dy)*((f11 & 0xFF)*(1-dx)+dx*(f12 & 0xFF))
      +dy*((f21 & 0xFF)*(1-dx)+dx*(f22 & 0xFF))); 
    return ( 255<<24|rot<<16 | gruen<<8 | blau);
  }
  
  
  float catmullRom(float x){
    x=abs(x);
    if(x<1) return (1.5*x-2.5)*x*x+1;
    if(x<2) return ((2.5-0.5*x)*x-4)*x+2;
    return 0;
  }
  
  float mitchellNetravali(float x){     //not at all normalized            
    x=abs(x);
    if(x<1) return (21*x-36)*x*x+16;
    if(x<2) return ((36-7*x)*x-60)*x+32;
    return 0;
  }
  
  
   //  looking up color with cubic interpolation
  color cubicInterpolation(Vector here) {
  //  mirrors(here);
    limits(here);
    i=floor(x);
    dx=x-i;
    j=floor(y);
    dy=y-j;
    reds=0;
    blues=0;
    greens=0;
    norm=0;
    w0=mitchellNetravali(dx+1);
    w1=mitchellNetravali(dx);
    w2=mitchellNetravali(1-dx);
    w3=mitchellNetravali(2-dx);
    int ii,jj;
    for (jj=j-1;jj<j+3;jj++){
      index=i+jj*thisWidth;
      f0=thisImage.pixels[index-1];
      f1=thisImage.pixels[index];
      f2=thisImage.pixels[index+1];
      f3=thisImage.pixels[index+2];
      w=mitchellNetravali(y-jj);
      reds+=w*(w0*((f0 >> 16) & 0xFF)+w1*((f1 >> 16) & 0xFF)+w2*((f2 >> 16) & 0xFF)+w3*((f3 >> 16) & 0xFF));
      greens+=w*(w0*((f0 >> 8) & 0xFF)+w1*((f1 >> 8) & 0xFF)+w2*((f2 >> 8) & 0xFF)+w3*((f3 >> 8) & 0xFF));
      blues+=w*(w0*((f0) & 0xFF)+w1*((f1) & 0xFF)+w2*((f2) & 0xFF)+w3*((f3) & 0xFF));
      norm+=w*(w0+w1+w2+w3);
    }
    norm=1/norm;
    rot=constrain(round(reds*norm),0,255);
    gruen=constrain(round(greens*norm),0,255);
    blau=constrain(round(blues*norm),0,255);
    return ( 255<<24|rot<<16 | gruen<<8 | blau);
  }

}
This entry was posted in programming and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s