11 January 2010
Some familiarity with ActionScript 3.
Beginning
In this article, you'll learn how to write a Pixel Bender kernel that combines multiple pixels from the same image. If you've been following along with the instructions provided since the beginning of this series, you've been building on the same file with each subsequent section. This time, we're going to start by creating a brand new filter, using the steps provided in the first article in this series.
This is the fourth installment in this series of articles about using the Pixel Bender Toolkit to create visual effects with bitmap images. In the previous section, you learned how to add parameters to create a slider interface that affects the display of the image in the preview window. You also learned how to add metadata to the parameters to ensure consistency when the effects are displayed. In this section, you'll learn how to sample multiple pixels from the same input image.
If you'd like to follow along by reviewing the code in the working copy, download the sample files provided. To get started, follow these steps:
In this part, you'll update the code using the syntax you learned in Part 3 of this article series. Follow these steps:
output pixel4 dst;
float parameter called amount:parameter float amount;
<
minValue: -5.0;
maxValue: 5.0;
defaultValue: 0.0;
>;
Note: Open the sample file named Exercise4FilterB.pbk to see the working example of adding the metadata described above to a float parameter.
<languageVersion : 1.0;>
kernel Part4Filter
< namespace : "com.adobe.devnet.pixelbender";
vendor : "Kevin's Filter Factory";
version : 2;
description : "Playing around with pixels";
>
{
input image4 src;
output pixel4 dst;
parameter float amount
<
minValue: -5.0;
maxValue: 5.0;
defaultValue: 0.0;
>;
void evaluatePixel()
{
dst = sampleNearest(src,outCoord());
}
}
In this part, you'll learn how to write the code to sample the second pixel and store its value in a new variable that you'll declare. Follow these steps:
dst = sampleNearest(src,outCoord());
pixel4 dst2 = sampleNearest(src, outCoord()+float2(amount, amount));
Note: dst2 is a new variable. It is storing the value of a second pixel in the image offset in both x and y axes by the value in the parameter named amount.
evaluatePixel function:dst += dst2;
Dst Void evaluatePixel()
{
dst = sampleNearest(src, outCoord());
pixel4 dst2 = sampleNearest(src, outCoord()+float2(amount, amount));
dst += dst2;
}
The dark borders around two edges of the image appear when running this filter because the returned color is a transparent black when sampling pixels off the end of the image.
The current implementation of the filter causes the colors to be oversaturated because the code is adding two pixel values together. In this section, you'll learn how to remedy this issue and make the saturation level return to normal.
Follow these steps:
dst += dst2;
dst /= 2.0;
Notice that if you leave the slider in its default position, the image appears unchanged. This occurs because you updated the code to add each pixel to itself and then divide the resulting value by 2.0 (restoring it to normal saturation).
If you move the slider, the offset effect is now displayed without making the colors seem too bright (see Figure 2).
Use the instructions provided in Part 2 of this article series to export the Pixel Bender code so that you can use it in Flash. Follow these steps:
In the Export dialog box that appears, enter the name for the filter: Exercise4Filter.pbj. Save the exported file to the desktop, in the folder named pixel_bender.
After experimenting with sampling multiple pixels from the same image, continue with Part 5 in this series, where you'll learn how to apply a Pixel Bender filter to an image in Flash.
Check out the following resources to learn more about working with the Pixel Bender Toolkit:
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.
Tutorials & Samples |
| 04/10/2012 | Fast Fourier Transform (FFT) in Pixel Bender? |
|---|---|
| 04/19/2012 | Shader works in toolkit, not in Flash |
| 04/12/2012 | Pixel Bender removed from AE CS6 !? |
| 03/22/2012 | Load an HDR image into Pixel Bender Toolkit |