Question

Ok, so I've tried this simpler filter with same Air code as below:

<languageVersion : 1.0;>

kernel NewFilter
<   namespace : "Your Namespace";
    vendor : "Your Vendor";
    version : 1;
    description : "your description";
>
{
    input image4 src;
    output pixel4 dst;

    void
    evaluatePixel()
    {
        pixel4 cPix  = sampleNearest(src,outCoord());
        pixel3 RGB = cPix .rgb;
        float r = RGB.r; 
        float g = RGB.g; 
        float b = RGB.b;
        //float minVal = min(r, g); 
        dst = pixel4(r, g, b, 1);
    }
}

It turns out, that if I uncomment this line float minVal = min(r, g);, I do not get original picture any more, but this: Result

Instead of this: Original

If someone could explain this to me... I'd be very grateful...

//------------------------------------------------------------------
//------------------- ORIGINAL POST --------------------------------

I'm trying to make pixel bender filter for flex application, which changes selected range of hue values of input image to defined new hue value. I made such filter in Pixel bender Toolkit, and it gives satisfactory results.

This is the code for that filter:

<languageVersion : 1.0;>

kernel ColorChange
<   namespace : "Your Namespace";
    vendor : "Your Vendor";
    version : 1;
    description : "your description";
>
{
    input image4 src;
    output pixel4 dst;

    parameter float HUE
    <
        minValue: 0.;
        maxValue: 359.9;
        defaultValue: 0.;
    >;
    parameter float SAT
    <
        minValue: -1.;
        maxValue: 1.;
        defaultValue: 0.;
    >;
    parameter float VAL
    <
        minValue: -1.;
        maxValue: 1.;
        defaultValue: 0.;
    >;

    parameter float MIN_RANGE
    <
        minValue: 0.;
        maxValue: 360.;
        defaultValue: 0.;
    >;

    parameter float MAX_RANGE
    <
        minValue: 0.;
        maxValue: 360.;
        defaultValue: 360.;
    >;

    void
    evaluatePixel()
    {
        pixel4 cPix = sample(src,outCoord());
        pixel3 RGB = cPix.rgb;
        float3 HSV;
        //--------------------------------------------------------------
        // CONVERT RGB TO HSV
        //--------------------------------------------------------------
        pixel1 r = RGB.r; 
        pixel1 g = RGB.g; 
        pixel1 b = RGB.b;

        pixel1 minVal = min(min(r, g), b); 
        pixel1 maxVal = max(max(r, g), b);
        pixel1 delta = maxVal - minVal;
        HSV[2] = maxVal;
        if (maxVal == 0.) {
            HSV[0] = 0.;
            HSV[1] = 0.;
        } 
        else
        {
            HSV[1] = delta / maxVal;
        }

        if(r == maxVal)
            HSV[0] = (g-b)/delta;
        else if(g == maxVal)
            HSV[0] = 2. + (b-r) / delta;
        else
            HSV[0] = 4. + (r-g) / delta;

        HSV[0] *= 60.;
        if(HSV[0] <0.)
            HSV[0] += 360.;

        //--------------------------------------------------------------
        // FILTER RANGE OF HUE
        //--------------------------------------------------------------
        if((HSV[0] < MIN_RANGE) || (HSV[0] > MAX_RANGE))
        {
            dst = cPix;
        }
        else
        {
            //--------------------------------------------------------------
            // CHNAGE HSV
            //--------------------------------------------------------------
            float hH = HUE;
            float sS = SAT;
            float vV = VAL;
            HSV[0] = HUE;
            HSV[1] += SAT;
            if(HSV[1] > 1.)
                HSV[1] = 1.;
            else if(HSV[1] < 0.)
                HSV[1] = 0.;
            HSV[2] += VAL;
                if(HSV[2] > 1.)
                    HSV[2] = 1.;
                else if(HSV[2] < 0.)
                    HSV[2] = 0.;
            //----------------------------------------------------------------------
            // CONVERT HSV TO RGB
            //----------------------------------------------------------------------
            float h = HSV[0];// / 360.; 
            float s = HSV[1];// / 100. * 255.; 
            float v = HSV[2];// / 100. * 255.;

            if (s == 0.) {
                RGB.r = v;
                RGB.g = v;
                RGB.b = v;
            } else {
                h = h / 60.;
                int var_i = int(floor(h));
                float f = h - float(var_i);
                float p = v * (1.-s);
                float q = v * (1.-s*f);
                float t = v * (1.-s*(1.-f));
                if (var_i == 0) {
                    RGB.r = v; 
                    RGB.g = t; 
                    RGB.b = p;
                }
                else if (var_i == 1) {
                    RGB.r = q; 
                    RGB.g = v; 
                    RGB.b = p;
                }
                else if (var_i == 2) {
                    RGB.r = p; 
                    RGB.g = v; 
                    RGB.b = t;
                }
                else if (var_i == 3) {
                    RGB.r = p; 
                    RGB.g = q; 
                    RGB.b = v;
                }
                else if (var_i == 4) {
                    RGB.r = t; 
                    RGB.g = p; 
                    RGB.b = v;
                }
                else {
                    RGB.r = v; 
                    RGB.g = p; 
                    RGB.b = q;
                }
            }
            dst = pixel4(RGB.r, RGB.g, RGB.b, 1);
        }
    }
}

So the principle is simple, convert every pixel to HSV color space, check if hue falls between selected range, if it does change it's hue to defined one and convert back to RGB.

The problem is, when used in Air application, I don't get the same results, starting from point of using only default parameters of filter.

This is the code of Air application:

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
                       xmlns:s="library://ns.adobe.com/flex/spark"
                       width="1400" height="800" backgroundAlpha="0.0" xmlns:mx="library://ns.adobe.com/flex/mx" 
                       creationComplete="windowedapplication1_creationCompleteHandler(event)">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>

    <fx:Script>
        <![CDATA[
            import mx.events.ColorPickerEvent;
            import mx.events.FlexEvent;

            import spark.filters.ShaderFilter;
            import spark.utils.BitmapUtil;

            [Embed(source="myFilter.pbj", mimeType="application/octet-stream")]
            private var MyBender:Class;

            private var shader:Shader = new Shader();
            private var shaderJob:ShaderJob;
            private var shaderResult:BitmapData;


            private function filter():void
            {                       
                // Configure desired input parameters of shader.
                shader.data.src.input = originalImage.bitmapData;

//              shader.data.HUE.value = [H_slider.value];
//              shader.data.SAT.value = [S_slider.value];
//              shader.data.VAL.value = [V_slider.value];
//              shader.data.MAX_RANGE.value = [H_max_slider.value];
//              shader.data.MIN_RANGE.value = [H_min_slider.value];

                shaderJob = new ShaderJob(shader, shaderResult);
                shaderJob.start(true);
                bendedImage.source = new Bitmap(shaderResult);
            }

            private function windowedapplication1_creationCompleteHandler(event:FlexEvent):void
            {
                // Create new shader instance and initialize with embedded byte code.
                shader = new Shader(new MyBender());
                shaderResult = new BitmapData(originalImage.width, originalImage.height);
                filter();
            }

        ]]>
    </fx:Script>
    <s:HGroup width="100%">
        <s:Group id="originalGroup" width="100%">
            <s:Image id="originalImage" source="mandelbrot.png"/>
        </s:Group>
        <s:Group id="bendedGroup" width="100%">
            <s:SWFLoader id="bendedImage" source="mandelbrot.png" />
            <s:HSlider id="H_slider" minimum="0" maximum="359.9" stepSize="0.1" value="0" change="filter()" width="500" toolTip="HUE"/>
            <s:HSlider id="S_slider" minimum="-1" maximum="1" stepSize="0.1" value="0" y="20" change="filter()" width="500" toolTip="SAT"/>
            <s:HSlider id="V_slider" minimum="-1" maximum="1" stepSize="0.1" value="0" y="40" change="filter()" width="500" toolTip="VAL"/>
            <s:HSlider id="H_max_slider" minimum="0" maximum="360" stepSize="0.1" value="360" y="60" change="filter()" width="500" toolTip="HUE MAX"/>
            <s:HSlider id="H_min_slider" minimum="0" maximum="360" stepSize="0.1" value="0" y="80" change="filter()" width="500" toolTip="HUE MIN"/>
        </s:Group>
    </s:HGroup> 
</s:WindowedApplication>

So, applying PixelBender filter in Air app with default parameters produces this:

http://i.stack.imgur.com/UyoZR.png

But in Pixel Bender Toolkit i see this with same parameters:

http://i.imgur.com/LNnCi.png

Changing HUE_MAX slider in app (binded to MAX_RANGE parameter of filter), it doesn't filter out HUE values smoothly, but instead thresholds at HUE_MAX=59.9, where at lower values looks like no filter is applyed, and at HUE_MAX=299.9 where between 60 and 299.9 looks like this:

http://i.stack.imgur.com/5kePu.png (sorry, new user)

Any ideas what am I doing wrong?

Was it helpful?

Solution

min function does somenthing to its parameters which i cannot explain. But the problem is solved when using

pixel1 minVal = r;
if(g < minVal)
minVal = g;
if(b < minVal)
minVal = b;

instead of

pixel1 minVal = min(min(r, g), b);

OTHER TIPS

You have a lot of float comparisons in your PB code. Lines like this:

if (maxVal == 0.) 

are extrenely susceptable to rounding errors. I suggest using something like this instead:

if (abs(maxVal) < epsilon) 

Your AS code has the lines that set the parameter values commented out. Can you try commenting them back in and set the values to the defaults. The default values you put into a PB file aren't automatically set by the runtime, they're just meant as hints to the UI.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top