为什么 Pixel Bender 滤镜在 Toolkit 中的行为与在 Flex/Air 应用程序中的行为不同?

发布于 2024-12-20 03:23:48 字数 9389 浏览 0 评论 0原文

好的,所以我尝试了这个更简单的过滤器,其空气代码如下所示:

<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);
    }
}

事实证明,如果我取消注释此行 float minVal = min(r, g);,我不会得到原始的不再有图片了,但是这个: 结果

而不是这样: 原创

如果有人可以向我解释这一点...我将非常感激...

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

我正在努力使用于 Flex 应用程序的像素弯曲过滤器,它将输入图像的色调值的选定范围更改为定义的新色调值。 我在 Pixel Bender Toolkit 中制作了这样的滤镜,它给出了令人满意的结果。

这是该过滤器的代码:

<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);
        }
    }
}

原理很简单,将每个像素转换为 HSV 颜色空间,检查色调是否落在所选范围内,如果确实将其色调更改为定义的色调,然后转换回 RGB。

问题是,当在空气应用程序中使用时,从仅使用过滤器的默认参数开始,我没有得到相同的结果。

这是 Air 应用程序的代码:

<?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>

因此,在 Air 应用程序中使用默认参数应用 PixelBender 过滤器会产生以下结果:

https://i.sstatic.net/UyoZR.png

但在 Pixel Bender Toolkit 中我看到了这个使用相同的参数:

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

在应用程序中更改 HUE_MAX 滑块(绑定到滤镜的 MAX_RANGE 参数),它不会平滑地过滤掉 HUE 值,而是阈值在 HUE_MAX=59.9 处,在较低值时看起来没有应用任何滤镜,在 HUE_MAX=299.9 处,在 60 到 299.9 之间看起来像这样:

https://i.sstatic.net/5kePu.png(抱歉,新用户)

有什么想法我做错了什么吗?

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:

https://i.sstatic.net/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:

https://i.sstatic.net/5kePu.png (sorry, new user)

Any ideas what am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦归所梦 2024-12-27 03:23:48

min 函数对其参数做了一些我无法解释的事情。
时问题就解决了

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

但是当使用而不是

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

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);
梦在夏天 2024-12-27 03:23:48

你的 PB 代码中有很多浮点比较。像这样的行:

if (maxVal == 0.) 

极易出现舍入错误。我建议使用这样的东西来代替:

if (abs(maxVal) < epsilon) 

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) 
束缚m 2024-12-27 03:23:48

您的 AS 代码将设置参数值的行注释掉。您可以尝试重新注释它们并将这些值设置为默认值吗?您放入 PB 文件中的默认值不会由运行时自动设置,它们只是作为 UI 的提示。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文