Personal tools

Views

Pixel Bender Toolkit:Polar filter

From Adobe Labs

<languageVersion : 1.0;> kernel Polar <

   namespace : "default";
   vendor : "tvzero";
   version : 1;
   description : "Polar filter";

> {

   // Boolean parameters not supported by Bytecode
   // parameter bool cartesian_to_polar;


   parameter int cartesian_to_polar <
       minValue:int(0);
       maxValue:int(1);
       defaultValue:int(0);
       description:"Choose mode (0=normal, 1=add)";
   >;
   parameter float radius
   <
       minValue: float(0.0);
       maxValue: float(256.0);
       defaultValue: float(128.0);
   >;
   
   parameter float2 center
   <
       minValue: float2(0.0, 0.0);
       maxValue: float2(256.0, 256.0);
       defaultValue: float2(128.0, 128.0);
   >;
   
   input image4 src;
   output pixel4 dst;


   void evaluatePixel()
   {
       float2 coord = outCoord();
       float2 p = coord-center;     // point
       float  d = length(p);        // length
       float  w = radius*2.0;       // width
       float  h = radius*2.0;       // height
       
       if(cartesian_to_polar == 0){
           float theta = atan(p.y/p.x); // angle
           if(coord.x < radius) theta += 3.14;
           
           float degrees = degrees(theta) + 90.0;
           float dx = w - (degrees*w/360.0);
           float dy = (d*h/radius);
           // coord.x = (dx>=w) ? (w-1.0):dx;
           if(dx>=w)
               coord.x = w-1.0;
           else 
               coord.x = dx;
           
           // coord.y = (dy>=h) ? (h-1.0):dy;
           if(dy>=h)
               coord.y = h-1.0;
           else 
               coord.y = dy;
       
       }else{
           
           float polardegrees = 90.0 - (coord.x + w)/w*360.0;
           float polard       = coord.y*radius/h;
           float polartheta   = radians(polardegrees);
           float polarx = center.x - polard*cos(polartheta);
           float polary = center.y - polard*sin(polartheta);
                       
           coord.x = polarx;
           coord.y = polary;
       
       }
       
       dst = sampleNearest(src, coord);
   }

}

Retrieved from "http://labs.adobe.com/wiki/index.php/Pixel_Bender_Toolkit:Polar_filter"