[Pixel Bender] Bright Pass filter

I’m pretty sure you already heard about pixel bender, if not check Adobe’s  or Lee Brimelow’s website for awsome tutorials. 

Today I gonna share with you, my first pixel bender code ever, which is a bright pass filter. This filter set the dark pixels to black according a threshold value for the ‘darkness’.

<languageVersion : 1.0;>

kernel BrightPassFilter
<   namespace : "com.gun.uieffect.filter";
    vendor : "Guillaume Nachury. http://proofofconcepts.wordpress.com/";
    version : 1;
    description : "Set all the dark pixels to black. The dark threshold value can be modified.";
>
{
    input image4 src;
    output pixel4 dst;

    parameter float thresholdValue;

    void
    evaluatePixel()
    {
        float Mx;
        float mn;
        float l;

        pixel4 p;
        p = sampleNearest(src,outCoord());

        /*-----------------------------------------------------------------------
            Here are some formulas to get the lightness :
        -----------------------------------------------------------------------*/

        /*(1) This one gave the best results so far.*/
         l = (240.0/255.0)*(0.239*p.r+0.686*p.g+0.075*p.b);

        /*(2) Quite the same as above but a bit tweaked
         l = (240.0/255.0)*(0.300*p.r+0.590*p.g+0.110*p.b);
         */

        /*(3) This one requires some steps before getting the lightness
        //Find which component is the highest
        if((p.r > p.g) && (p.r > p.b)){
        Mx = p.r;
        }
        else if((p.g > p.r) && (p.g > p.b)){
        Mx = p.g;
        }
        else if((p.b > p.g) && (p.b > p.r)){
        Mx = p.b;
        }

        //Find which component is the lowest
        if((p.r < p.g) && (p.r < p.b)){
        mn = p.r;
        }
        else if((p.g < p.r) && (p.g < p.b)){
        mn = p.g;
        }
        else if((p.b < p.g) && (p.b < p.r)){
        mn = p.b;
        }    

        l = 0.5*240.0*( (Mx+mn)/255.0),

      */

       /*Formula I found, but not sure of the result
         l = (240.0/255.0)*Mx;
         */

       if(l<thresholdValue){
            p.r = p.b = p.g = 0.0; //black pxl
       } 

        dst = p;

    }
}

As usual this is a kind of proof of concept, feel free to share any comment/change.
Stay tuned for the next Pixel Bender code which will reuse this filter.

Leave a Reply