Sei sulla pagina 1di 6

ADOBE® PIXEL BENDER™

KERNEL FILTER WRITING


TUTORIAL
© 2008 Adobe Systems Inc.

Adobe, the Adobe logo, Lightroom, and Pixel Bender are either registered trademarks or
trademarks of Adobe Systems Inc. in the United States and/or other countries. All other
trademarks are the property of their respective owners.

The information in this document is furnished for informational use only, is subject to change
without notice, and should not be construed as a commitment by Adobe Systems Inc. Adobe
Systems Inc. assumes no responsibility or liability for any errors or inaccuracies that may appear in
this document. The software described in this document is furnished under license and may only
be used or copied in accordance with the terms of such license.

Adobe Systems Inc., 345 Park Avenue, San Jose, California 95110, USA
Adobe Pixel Bender Kernel Filter Writing
Tutorial
This tutorial assumes the user installed the Adobe® Pixel BenderTM toolkit and has a rudimentary
knowledge of image processing.

Step 1: Become familiar with the Pixel Bender toolkit


Open the Pixel Bender toolkit. It has three basic areas, described below.

1. Image View section — This area shows the


filter results on the loaded images. If a filter is
not loaded, this section displays the image file
loaded in the "Load Image 1..." file menu.

2. Pixel Bender source editor — This area is


where Pixel Bender source code is edited. It
provides basic code-development capabilities,
including syntax highlighting and code
completion.

3. Filter Parameter user Interface — If you


expose a user-specified parameter, the toolkit
automatically creates a user interface to control
that parameter, and displays it here.

There also is a menu which will lets you control some toolkit settings.

Step 2: Load a sample image


Open the File menu and click on "Load Image 1." A file dialog opens up. Navigate to a sample
image of your choosing. The toolkit supports the JPEG and PNG file formats.

Step 3: Create a new Pixel Bender kernel program


On the File menu, choose "New Pixel Bender Kernel Filter." The basic structure of a Pixel Bender
kernel displays in the Pixel Bender source editor. By default, the new filter has the code necessary
to perform a basic identity on the image; no edits to the image.
Step 4: Edit and run the Pixel Bender kernel program
Here, we perform a basic filter operation that reduces all channels in the input image (including
opacity) by half. Edit the following line of code in the Pixel Bender source editor, then click the Run
button in the bottom right of the window:

From:
dst = sampleNearest(src,outCoord());

To:
dst = 0.5 * sampleNearest(src, outCoord());

Reducing all channels by half darkens the image.

Step 5: Split up the image channels


In this step, we split up the one line of Pixel Bender code to apply the math differently to the
different channels of the image. We change the code to darken only the color channels, without
changing the alpha channel, opacity.

In the Pixel Bender source editor, change the following code:

From:
dst = 0.5 * sampleNearest(src, outCoord());

To:
float4 inputColor = sampleNearest(src, outCoord());

dst.rgb = 0.5 * inputColor.rgb;


dst.a = inputColor.a;

The new code saves the input color in a temporary variable of type float4. The float4 has
the red, green, and blue channels in the first three elements and the alpha channel in the last
element. These channels are accessible by using the first letter of their name after the dot
operator, as in the last two lines above.

Step 6: Change the filter operation to exposure


In this step, we change the algorithm from a simple scale to a simple exposure change, similar to
the Exposure setting in Adobe LightroomTM. To do this, we use the pow() built-in function (for a
list of built-in functions, see the Pixel Bender documentation) to do a simple exponential
calculation on the color channels.

In the Pixel Bender source editor, change the following code:

From:
dst.rgb = 0.5 * inputColor.rgb;
To:
dst.rgb = pow(inputColor.rgb, float3(0.5));

We pass in the three channels to the same function. We can do this because the built-in function
pow() can support the float, float2, float3, and float4 types. We must add the
float3() constructor around the constant value, because the types of the two inputs to pow
must match.

Step 7: Export the exposure value as a parameter


In this step, we export the exposure value we use as a user-controlled parameter.

Add the following line of code just before the evaluatePixel () function:
parameter float exposure;

Change the following line of code:

From:
dst.rgb = pow(inputColor.rgb, float3(0.5));

To:
dst.rgb = pow(inputColor.rgb, float3(1.0 - exposure));

Now click Run. In the parameter user-interface portion of the toolkit, you should now see a slider
for the exposure parameter. You can slide the exposure slider to adjust the image. The 1.0 setting
is used to switch the exposure, so a positive exposure value brightens the image.

Step 8: Limit the parameter


If you experiment with the slider, you will notice that you can only lighten the image; this is
because the value is available only from 0 to 1. Here, we add constraints to the parameter, so the
user interface has more intuitive results.

Change the parameter specification to the following, then click Run:


parameter float exposure
<
minValue:float(-0.5);
maxValue:float(0.5);
float(0.0);
>;

We use parameter metadata to specify the limits and default values of the parameters. We now
have a more intuitive limit around our slider, and we have enabled darkening.
Final source
<languageVersion : 1.0;>

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

parameter float exposure


<
minValue:float(-0.5);
maxValue:float(0.5);
defaultValue:float(0.0);
>;

void
evaluatePixel()
{
float4 inputColor = sampleNearest(src, outCoord());
dst.rgb = pow(inputColor.rgb, float3(1.0 - exposure));
dst.a = inputColor.a;
}
}

See also
Pixel Bender toolkit Install/Wiki: http://www.adobe.com/go/pixelbender_toolkit

Potrebbero piacerti anche