<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Mike Chambers &#187; pixelbender</title>
	<atom:link href="http://www.mikechambers.com/blog/tag/pixelbender/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mikechambers.com/blog</link>
	<description>code = joy</description>
	<lastBuildDate>Sun, 15 Jan 2012 05:46:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Pixel Bender Grayscale Filter</title>
		<link>http://www.mikechambers.com/blog/2008/09/18/pixel-bender-grayscale-filter/</link>
		<comments>http://www.mikechambers.com/blog/2008/09/18/pixel-bender-grayscale-filter/#comments</comments>
		<pubDate>Thu, 18 Sep 2008 15:38:15 +0000</pubDate>
		<dc:creator>mikechambers</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[pixelbender]]></category>

		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=1531</guid>
		<description><![CDATA[Below is another simple Pixel Bender filter that I created last night. This one basically, converts an image to gray scale, using the ITU-R Recommendation BT.709 algorithm described here. Here is the filter: &#60;languageVersion : 1.0;&#62; kernel GrayScale &#60; namespace : &#34;mesh&#34;; vendor : &#34;Mike Chambers&#34;; version : 1; description : &#34;Gray scale filter&#34;; &#62; [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.mikechambers.com%2Fblog%2F2008%2F09%2F18%2Fpixel-bender-grayscale-filter%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif&amp;source=mesh&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Below is another simple <a href="http://labs.adobe.com/wiki/index.php/Pixel_Bender_Toolkit">Pixel Bender</a> filter that I created last night. This one basically, converts an image to gray scale, using the ITU-R Recommendation BT.709 algorithm described <a href="http://local.wasp.uwa.edu.au/~pbourke/texture_colour/imageprocess/">here</a>.</p>
<p><a href="http://www.flickr.com/photos/mikechambers/2868222770/" title="Pixel Bender Grayscale Filter by mike.chambers, on Flickr"><img src="http://farm4.static.flickr.com/3057/2868222770_e537d4b918.jpg" width="500" height="192" alt="Pixel Bender Grayscale Filter" /></a><br />
<span id="more-1531"></span><br />
Here is the filter:</p>
<div class="highlight">
<pre><span style="color: #666666">&lt;</span>languageVersion <span style="color: #666666">:</span> <span style="color: #666666">1.0;&gt;</span>

kernel GrayScale
<span style="color: #666666">&lt;</span>   namespace <span style="color: #666666">:</span> <span style="color: #BA2121">&quot;mesh&quot;</span><span style="color: #666666">;</span>
    vendor <span style="color: #666666">:</span> <span style="color: #BA2121">&quot;Mike Chambers&quot;</span><span style="color: #666666">;</span>
    version <span style="color: #666666">:</span> <span style="color: #666666">1;</span>
    description <span style="color: #666666">:</span> <span style="color: #BA2121">&quot;Gray scale filter&quot;</span><span style="color: #666666">;</span>
<span style="color: #666666">&gt;</span>
{
    input image4 src<span style="color: #666666">;</span>
    output pixel4 dst<span style="color: #666666">;</span>

    void
    evaluatePixel()
    {
        dst <span style="color: #666666">=</span> sampleNearest(src<span style="color: #666666">,</span>outCoord());

        <span style="color: #408080; font-style: italic">//algorithm from ITU-R Recommendation BT.709</span>
        <span style="color: #408080; font-style: italic">//http://local.wasp.uwa.edu.au/~pbourke/texture_colour/imageprocess/</span>
        float avg <span style="color: #666666">=</span> <span style="color: #666666">0.2125</span> <span style="color: #666666">*</span> dst.r <span style="color: #666666">+</span> <span style="color: #666666">0.7154</span> <span style="color: #666666">*</span> dst.g <span style="color: #666666">+</span> <span style="color: #666666">0.0721</span> <span style="color: #666666">*</span> dst.b<span style="color: #666666">;</span>
        dst <span style="color: #666666">=</span> float4(avg<span style="color: #666666">,</span> avg<span style="color: #666666">,</span> avg<span style="color: #666666">,</span> dst.a);

    }
}
</pre>
</div>
<p>Again, its pretty simple, but I am trying to learn both Pixel Bender and image processing in general. The biggest thing I learned for this example is how to work with different vector sizes (which can be a little confusing). Im still wrapping my head around it, but once I get it down, Ill make a post on vectors in Pixel Bender.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mikechambers.com/blog/2008/09/18/pixel-bender-grayscale-filter/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Pixel Bender TV Scanline Filter</title>
		<link>http://www.mikechambers.com/blog/2008/09/17/pixel-bender-tv-scanline-filter/</link>
		<comments>http://www.mikechambers.com/blog/2008/09/17/pixel-bender-tv-scanline-filter/#comments</comments>
		<pubDate>Thu, 18 Sep 2008 05:06:02 +0000</pubDate>
		<dc:creator>mikechambers</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[pixelbender]]></category>

		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=1520</guid>
		<description><![CDATA[After seeing all of the cool stuff people have been doing with Pixel Bender, I finally decided to spend a little time and start learning how to build them myself. The learning curve isn&#8217;t too difficult, although you do have to learn some new concepts, and deal with some limitations (at least when creating filters [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.mikechambers.com%2Fblog%2F2008%2F09%2F17%2Fpixel-bender-tv-scanline-filter%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif&amp;source=mesh&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>After seeing all of the <a href="http://pixelero.wordpress.com/">cool stuff people have been doing with Pixel Bender,</a> I finally decided to spend a little time and start learning how to build them myself.</p>
<p>The learning curve isn&#8217;t too difficult, although you do have to learn some new concepts, and deal with some limitations (at least when creating filters for Flash). The most difficult part for me thus far, is just understanding color and pixel math and manipulation (something I need to read up on more).</p>
<p>Anyways, below if one of my first filters. I wanted to post it as it is a pretty simple example, while still potentially being useful. Basically, it creates a TV scanline effect by making every other row of pixels black.<br />
<span id="more-1520"></span><br />
Here is a before an after example (you need to click to view the full size in order to see the effect well).</p>
<p><a href="http://www.flickr.com/photos/mikechambers/2867328548/" title="Scanline FIlter by mike.chambers, on Flickr"><img src="http://farm3.static.flickr.com/2217/2867328548_a35f316ee5.jpg" width="500" height="190" alt="Scanline FIlter" /></a></p>
<p>Here is the code for the filter:</p>
<div class="highlight">
<pre><span style="color: #666666">&lt;</span>languageVersion <span style="color: #666666">:</span> <span style="color: #666666">1.0;&gt;</span>

kernel ScanLines
<span style="color: #666666">&lt;</span>   namespace <span style="color: #666666">:</span> <span style="color: #BA2121">&quot;mesh&quot;</span><span style="color: #666666">;</span>
    vendor <span style="color: #666666">:</span> <span style="color: #BA2121">&quot;Mike Chambers&quot;</span><span style="color: #666666">;</span>
    version <span style="color: #666666">:</span> <span style="color: #666666">1;</span>
    description <span style="color: #666666">:</span> <span style="color: #BA2121">&quot;Generates a TV Scanline effect&quot;</span><span style="color: #666666">;</span>
<span style="color: #666666">&gt;</span>
{
    input image4 src<span style="color: #666666">;</span>
    output pixel4 dst<span style="color: #666666">;</span>

    void
    evaluatePixel()
    {
        <span style="color: #408080; font-style: italic">//get the current pixel</span>
        dst <span style="color: #666666">=</span> sampleNearest(src<span style="color: #666666">,</span> outCoord());

        <span style="color: #408080; font-style: italic">//find out the vertical pixel size. In Flash this will always be 1</span>
        float pixelVSize <span style="color: #666666">=</span> pixelSize(src).y<span style="color: #666666">;</span>

        <span style="color: #408080; font-style: italic">//pixelVSize / 2.0 - outCoord() gives center of pixel, so we have to adjust to find</span>
        <span style="color: #408080; font-style: italic">//center of our pixel</span>
        <span style="color: #008000; font-weight: bold">if</span>( int(mod(((pixelVSize <span style="color: #666666">*</span> outCoord().y) <span style="color: #666666">-</span> pixelVSize <span style="color: #666666">/</span> <span style="color: #666666">2.0</span>)<span style="color: #666666">,</span> <span style="color: #666666">2.0</span> )) <span style="color: #666666">==</span> <span style="color: #666666">0</span>)
        {
            <span style="color: #408080; font-style: italic">//if on an even row, set the pixel to blackt</span>
            dst.r <span style="color: #666666">=</span> <span style="color: #666666">0.0;</span>
            dst.b <span style="color: #666666">=</span> <span style="color: #666666">0.0;</span>
            dst.g <span style="color: #666666">=</span> <span style="color: #666666">0.0;</span>

            <span style="color: #408080; font-style: italic">//can also do</span>
            <span style="color: #408080; font-style: italic">////dst = float4(0.0,0.0,0.0, 1.0);</span>
        }
    }
}
</pre>
</div>
<p>Thanks to <a href="http://blogs.adobe.com/kevin.goldsmith/">Kevin Goldsmith</a> from the Pixel Bender team for helping me out.</p>
<p>The comments in the code explain what is going on.</p>
<p>You can load this into <a href="http://labs.adobe.com/wiki/index.php/Pixel_Bender_Toolkit">Pixel Bender</a> to play around with it, and compile it for use in Flash Player 10.</p>
<p>I wanted to add a parameter to allow the line size to be set. It doesn&#8217;t look like I can store variables in between calls  to evaluatePixel(). Im trying to figure out an algorithm to figure it out in one pass, but im not sure that my math skills are up to the task at the moment. </p>
<p>You can find all of my other posts on Pixel Bender <a href="http://www.mikechambers.com/blog/tag/pixelbender/">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mikechambers.com/blog/2008/09/17/pixel-bender-tv-scanline-filter/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Creating Re-distributable ActionScript Libraries of Pixel Bender Filters</title>
		<link>http://www.mikechambers.com/blog/2008/09/17/creating-re-distributable-actionscript-libraries-of-pixel-bender-filters/</link>
		<comments>http://www.mikechambers.com/blog/2008/09/17/creating-re-distributable-actionscript-libraries-of-pixel-bender-filters/#comments</comments>
		<pubDate>Wed, 17 Sep 2008 19:43:17 +0000</pubDate>
		<dc:creator>mikechambers</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[pixelbender]]></category>

		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=1512</guid>
		<description><![CDATA[Last week I wrote a number of blog posts showing how to work with Pixel Bender filters in Flash, Flex and ActionScript. In particular, I wrote about how to embed pixel bender filters within a SWF and how to encapsulate custom Pixel Bender filters in an ActionScript 3 class. For this post, I am going [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.mikechambers.com%2Fblog%2F2008%2F09%2F17%2Fcreating-re-distributable-actionscript-libraries-of-pixel-bender-filters%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif&amp;source=mesh&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Last week I wrote a number of blog posts showing how to work with <a href="http://labs.adobe.com/wiki/index.php/Pixel_Bender_Toolkit">Pixel Bender</a> filters in Flash, Flex and ActionScript. In particular, I wrote about how to <a href="http://www.mikechambers.com/blog/2008/09/08/embedding-pixel-bender-filters-within-a-swf/">embed pixel bender filters within a SWF</a> and how to <a href="http://www.mikechambers.com/blog/2008/09/08/encapsulating-custom-pixel-bender-filters-in-actionscript-3/">encapsulate custom Pixel Bender filters in an ActionScript 3 class</a>. For this post, I am going to take the next step, and show how to create re-distributable SWC libraries of custom Pixel Bender filters that can be used in <a href="http://labs.adobe.com/technologies/flashplayer10/">Flash Player 10</a> based projects (including Flex Builder, MXMLC and Flash Professional).<br />
<span id="more-1512"></span><br />
Basically, we will create a SWC (redistributable library of ActionScript classes) that contains our Pixel Bender filter encapsulated inside of an ActionScript class. We will create the SWC in <a href="http://www.adobe.com/products/flex/">Flex Builder</a> although you could also easily do it using the <em>compc</em> command line tool included in the Flex SDK.</p>
<p>First, we need to set up Flex builder so that it will recognize the Flash Player 10 classes that we need to use to create and compile our filter class. </p>
<p>Download one of the <a href="http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+3">Flex 3.2 nightly builds</a> (I am using 3.2.0.3238) and extract it to a location on your desktop (I will refer to this as FLEX3.2_PATH). The builds contain support for compiling to Flash Player 9 and Flash Player 10.</p>
<p>Next, open up Flex Builder. We need to add the Flex SDK we just downloaded to Flex Builder. You can find complete instructions in this <a href="http://livedocs.adobe.com/flex/3/html/help.html?content=build_6.html#162812">technote</a>. Basically, go to <em>Window &gt; Preferences &gt; Flex &gt; Installed Flex SDKs &gt; Add</em> and point to the SDK you just downloaded.</p>
<p>Next, created a new &#8220;Flex Library Project&#8221; by going to <em>File &gt; New &gt; Flex Library Project</em> (dont worry, this is actually just an ActionScript library and doesn&#8217;t require or use Flex).</p>
<p>Specify a project name and where the project will be stored. In the <em>Flex SDK version</em> section, click the <em>Use a specific SDK</em> radio button, and then from the drop down, select the Flex SDK you just added (which should be named <em>Flex 3.2</em>). </p>
<p><a href="http://www.flickr.com/photos/mikechambers/2865425551/" title="Creating a Project for FP 10 by mike.chambers, on Flickr"><img src="http://farm4.static.flickr.com/3239/2865425551_dc8c0fb7c7.jpg" width="500" height="476" alt="Creating a Project for FP 10" /></a></p>
<p>Click the <em>Next</em> button (not the <em>Finish</em> button). This will take you to a panel where you can set build paths. We need to tell Flex Builder to compile for Flash Player 10, and not Flash Player 9. Select the <em>Library path</em> tab button at the top. This should show the Flex 3.2 SDK which you are using. Click the little arrow next to the SDK listing to expand the entry. This will show the SWCs being used to compile the project.  Select the <em>playerglobal.swc</em> entry, and click the <em>Remove</em> button. This will remove the Flash Player 9 SWC from the project.</p>
<p>Next, we need to add the Flash Player 10 SWC. Click the <em>Add SWC</em> button and then browse to <em>FLEX3.2_PATH/frameworks/libs/player/10</em> and select the <em>playerglobal.swc</em> in that directory and click ok.</p>
<p>Finally, make sure the <em>playerglobal.swc</em> entry is expanded and select the <em>Link Type</em> entry, and click the <em>Edit</em> button. Set the <em>Link Type</em> to <em>External</em> and click ok, and then click the <em>Finish</em> button. </p>
<p><a href="http://www.flickr.com/photos/mikechambers/2865425531/" title="Setting Flex Builder to compile to FP 10 by mike.chambers, on Flickr"><img src="http://farm4.static.flickr.com/3006/2865425531_890d4092e8.jpg" width="500" height="417" alt="Setting Flex Builder to compile to FP 10" /></a></p>
<p>The project is now configured to create SWC libraries that work with Flash Player 10 classes.</p>
<p>Note, you may get a compile error in the <em>Problem view</em> saying that nothing has been specified to be included in the SWC. You can ignore that for now.</p>
<p>Next, we need to add our Pixel Bender filter, and create the class that embeds it and provides an APIs for it. We will use the same filter and classes that I used in my earlier blog post <a href="http://www.mikechambers.com/blog/2008/09/08/encapsulating-custom-pixel-bender-filters-in-actionscript-3/">here</a>. Copy the <em>testfilter.pbj</em> file into the filters directory.</p>
<p>In the <em>src</em> directory of the project, create a new folder called &#8220;filters&#8221;. We will use this as the namespace for all of our custom filters (of course, you may want to use a different namespace and directory structure). Copy the filters pbj file into that directory.</p>
<p>Now we are ready to add our ActionScript class for the filter. I am not going to go through all of the steps of creating the class, you can get most of that in this post. </p>
<p>Right click on the filters folder, and select <em>New &gt; ActionScript Class</em>. Give the class a name (I am using <em>TestFilter</em>  for this example), set the <em>Superclass</em> to <em>ShaderFilter</em> and click the <em>Finish</em> button. If you just copy the class from the previous past, make sure to update the package path in the class definition.</p>
<p>Here is what the project layout looks like now:</p>
<p><img src="/images/blog/pixelbenderswcs/project.png" height="112" width="190" /></p>
<p>Here is the filter class I created for the filter:</p>
<div class="highlight">
<pre>package filters
{
	<span style="color: #008000; font-weight: bold">import</span> flash.display.Shader<span style="color: #666666">;</span>
	<span style="color: #008000; font-weight: bold">import</span> flash.filters.ShaderFilter<span style="color: #666666">;</span>
	<span style="color: #008000; font-weight: bold">import</span> flash.utils.ByteArray<span style="color: #666666">;</span>

	<span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">class</span> TestFilter <span style="color: #008000; font-weight: bold">extends</span> ShaderFilter
	{
		<span style="color: #408080; font-style: italic">//the file that contains the binary bytes of the PixelBender filter</span>
		[Embed(<span style="color: #BA2121">&quot;testfilter.pbj&quot;</span><span style="color: #666666">,</span> mimeType<span style="color: #666666">=</span><span style="color: #BA2121">&quot;application/octet-stream&quot;</span>)]
		<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">var</span> Filter<span style="color: #666666">:</span>Class<span style="color: #666666">;</span>		

		<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">var</span> _shader<span style="color: #666666">:</span>Shader<span style="color: #666666">;</span>

		<span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">function</span> TestFilter(value<span style="color: #666666">:</span><span style="color: #008000">Number</span> <span style="color: #666666">=</span> <span style="color: #666666">50</span>)
		{
			<span style="color: #408080; font-style: italic">//initialize the ShaderFilter with the PixelBender filter we</span>
			<span style="color: #408080; font-style: italic">//embedded</span>
			_shader <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> Shader(<span style="color: #008000; font-weight: bold">new</span> Filter() as ByteArray);

			<span style="color: #408080; font-style: italic">//set the default value</span>
			<span style="color: #008000; font-weight: bold">this</span>.value <span style="color: #666666">=</span> value<span style="color: #666666">;</span>
			<span style="color: #008000; font-weight: bold">super</span>(_shader);
		}

		<span style="color: #408080; font-style: italic">//This filter only has one value, named value</span>
		<span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">function</span> get value()<span style="color: #666666">:</span><span style="color: #008000">Number</span>
		{
			<span style="color: #008000; font-weight: bold">return</span> _shader.data.amount.value[<span style="color: #666666">0</span>];
		}

		<span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">function</span> set value(value<span style="color: #666666">:</span><span style="color: #008000">Number</span>)<span style="color: #666666">:</span>void
		{
			<span style="color: #408080; font-style: italic">//not that pixel bender filters take an array of values, even</span>
			<span style="color: #408080; font-style: italic">//though we only have one in our example</span>
			_shader.data.amount.value <span style="color: #666666">=</span> [value];
		}		

	}
}
</pre>
</div>
<p>At this point, we are ready to add the class to the SWC, and then compile the project.</p>
<p>Open <em>Project &gt; Properties &gt; Flex Library Build Path</em>. On the <em>Classes</em> tab, make sure the <em>TestFilter</em> class is selected. You do not need to include the filter pbj file in the SWC as this is compiled into the <em>TestFilter</em> class when the class is compiled into the SWC.</p>
<p>Click the <em>OK</em> button. The class and SWC should be compiled. If you get any compile errors you will need to fix them first. Once the SWC is compiled, you can find it in the projects <em>bin</em> folder.</p>
<p>You now have a SWC that contains your custom ActionScript Pixel Bender class and filter. At this point, you can distribute and / or reuse the SWC for other projects. Just link the SWC like you would any other SWC and your filter class will be available. Today, this works in Flex Builder and MXMLC (provided you set them up to compile to Flash Player 10). Once Flash Professional CS4 is released (which includes support for Flash Player 10) you will be able to use the SWC and filter there also (and yes, the SWC import bug is fixed).</p>
<p>Using this technique, you can create reusable libraries of custom Pixel Bender filters, and use them in your project just like the Flash Player&#8217;s built in filters.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mikechambers.com/blog/2008/09/17/creating-re-distributable-actionscript-libraries-of-pixel-bender-filters/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Using Custom Pixel Bender Filter Classes in MXML</title>
		<link>http://www.mikechambers.com/blog/2008/09/08/using-custom-pixel-bender-filter-classes-in-mxml/</link>
		<comments>http://www.mikechambers.com/blog/2008/09/08/using-custom-pixel-bender-filter-classes-in-mxml/#comments</comments>
		<pubDate>Mon, 08 Sep 2008 23:43:23 +0000</pubDate>
		<dc:creator>mikechambers</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[pixelbender]]></category>

		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=1468</guid>
		<description><![CDATA[Ok. Last post on Pixel Bender for today (I promise). This one is simple, but ties together my previous posts. Now, that we know how to load and use Pixel Bender filters in Flex, and know how encapsulte Pixel Bender filters in an ActionScript class, lets combine the two to leverage custom Pixel Bender filters [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.mikechambers.com%2Fblog%2F2008%2F09%2F08%2Fusing-custom-pixel-bender-filter-classes-in-mxml%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif&amp;source=mesh&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Ok. Last post on Pixel Bender for today (I promise). This one is simple, but ties together my previous posts.</p>
<p>Now, that we know how to load and use Pixel Bender filters in Flex, and know how encapsulte Pixel Bender filters in an ActionScript class, lets combine the two to leverage custom Pixel Bender filters in MXML.<br />
<span id="more-1468"></span><br />
First, again, we need our custom Pixel Bender class:</p>
<p><strong>Filter Class : TestFilter.as</strong></p>
<div class="highlight">
<pre>package
{
	<span style="color: #008000; font-weight: bold">import</span> flash.display.Shader<span style="color: #666666">;</span>
	<span style="color: #008000; font-weight: bold">import</span> flash.filters.ShaderFilter<span style="color: #666666">;</span>
	<span style="color: #008000; font-weight: bold">import</span> flash.utils.ByteArray<span style="color: #666666">;</span>

	<span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">class</span> TestFilter <span style="color: #008000; font-weight: bold">extends</span> ShaderFilter
	{
		<span style="color: #408080; font-style: italic">//the file that contains the binary bytes of the PixelBender filter</span>
		[Embed(<span style="color: #BA2121">&quot;testfilter.pbj&quot;</span><span style="color: #666666">,</span> mimeType<span style="color: #666666">=</span><span style="color: #BA2121">&quot;application/octet-stream&quot;</span>)]
		<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">var</span> Filter<span style="color: #666666">:</span>Class<span style="color: #666666">;</span>		

		<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">var</span> _shader<span style="color: #666666">:</span>Shader<span style="color: #666666">;</span>

		<span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">function</span> TestFilter(value<span style="color: #666666">:</span><span style="color: #008000">Number</span> <span style="color: #666666">=</span> <span style="color: #666666">50</span>)
		{
			<span style="color: #408080; font-style: italic">//initialize the ShaderFilter with the PixelBender filter we</span>
			<span style="color: #408080; font-style: italic">//embedded</span>
			_shader <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> Shader(<span style="color: #008000; font-weight: bold">new</span> Filter() as ByteArray);

			<span style="color: #408080; font-style: italic">//set the default value</span>
			<span style="color: #008000; font-weight: bold">this</span>.value <span style="color: #666666">=</span> value<span style="color: #666666">;</span>
			<span style="color: #008000; font-weight: bold">super</span>(_shader);
		}

		<span style="color: #408080; font-style: italic">//This filter only has one value, named value</span>
		<span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">function</span> get value()<span style="color: #666666">:</span><span style="color: #008000">Number</span>
		{
			<span style="color: #008000; font-weight: bold">return</span> _shader.data.amount.value[<span style="color: #666666">0</span>];
		}

		<span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">function</span> set value(value<span style="color: #666666">:</span><span style="color: #008000">Number</span>)<span style="color: #666666">:</span>void
		{
			<span style="color: #408080; font-style: italic">//not that pixel bender filters take an array of values, even</span>
			<span style="color: #408080; font-style: italic">//though we only have one in our example</span>
			_shader.data.amount.value <span style="color: #666666">=</span> [value];
		}		

	}
}
</pre>
</div>
<p>Once we have defined that, we can then treat the class like any other filter, and use it directly within MXML, like so:</p>
<div class="highlight">
<pre><span style="color: #BC7A00">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span>
<span style="color: #008000; font-weight: bold">&lt;mx:Application</span> <span style="color: #7D9029">xmlns:mx=</span><span style="color: #BA2121">&quot;http://www.adobe.com/2006/mxml&quot;</span> <span style="color: #7D9029">layout=</span><span style="color: #BA2121">&quot;absolute&quot;</span>
	<span style="color: #7D9029">xmlns:local=</span><span style="color: #BA2121">&quot;*&quot;</span>
	<span style="color: #008000; font-weight: bold">&gt;</span>
	<span style="color: #008000; font-weight: bold">&lt;mx:Image</span> <span style="color: #7D9029">left=</span><span style="color: #BA2121">&quot;10&quot;</span> <span style="color: #7D9029">top=</span><span style="color: #BA2121">&quot;10&quot;</span> <span style="color: #7D9029">width=</span><span style="color: #BA2121">&quot;375&quot;</span> <span style="color: #7D9029">height=</span><span style="color: #BA2121">&quot;500&quot;</span> <span style="color: #7D9029">source=</span><span style="color: #BA2121">&quot;@Embed(source=&#39;image.jpg&#39;)&quot;</span><span style="color: #008000; font-weight: bold">&gt;</span>
    	<span style="color: #008000; font-weight: bold">&lt;mx:filters&gt;</span>
     		<span style="color: #008000; font-weight: bold">&lt;local:TestFilter</span> <span style="color: #7D9029">value=</span><span style="color: #BA2121">&quot;25&quot;</span> <span style="color: #008000; font-weight: bold">/&gt;</span>
    	<span style="color: #008000; font-weight: bold">&lt;/mx:filters&gt;</span>
	<span style="color: #008000; font-weight: bold">&lt;/mx:Image&gt;</span>
<span style="color: #008000; font-weight: bold">&lt;/mx:Application&gt;</span>
</pre>
</div>
<p>Note that the local namespace maps to the package that contains our custom filter (in this case, the root package).</p>
<p>Compiled using the <a href="http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+3">Flex 3.1.0.2710 SDK</a>, and requires <a href="http://labs/technologies/flashplayer10/">Flash Player 10</a>. The Flex team is working on making this workflow even easier for Flex 4, so keep an eye on the <a href="http://opensource.adobe.com/wiki/display/flexsdk/Gumbo">Flex 4 project page</a>.</p>
<p>You can see an example of the filter <a href="http://flickr.com/photos/mikechambers/2840691963/">here</a>.</p>
<p>You can find more information on Pixel Bender <a href="www.adobe.com/go/pixelbender">here</a>.</p>
<p>You can download the filter from <a href="http://www.gotoandlearn.com/player.php?id=84">here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mikechambers.com/blog/2008/09/08/using-custom-pixel-bender-filter-classes-in-mxml/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Encapsulating Custom Pixel Bender Filters in ActionScript 3</title>
		<link>http://www.mikechambers.com/blog/2008/09/08/encapsulating-custom-pixel-bender-filters-in-actionscript-3/</link>
		<comments>http://www.mikechambers.com/blog/2008/09/08/encapsulating-custom-pixel-bender-filters-in-actionscript-3/#comments</comments>
		<pubDate>Mon, 08 Sep 2008 21:30:07 +0000</pubDate>
		<dc:creator>mikechambers</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[pixelbender]]></category>

		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=1463</guid>
		<description><![CDATA[If you read my blog regularly (or just today) you should have noticed that I have been playing around with some Pixel Bender filters and ActionScript / Flex (all inspired by Lee Brimelow&#8217;s video screencast on creating and using Pixel Bender filters in Flash Player 10.) Previously, I posted some code showing how to embed [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.mikechambers.com%2Fblog%2F2008%2F09%2F08%2Fencapsulating-custom-pixel-bender-filters-in-actionscript-3%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif&amp;source=mesh&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>If you read my blog regularly (or just today) you should have noticed that I have been playing around with some Pixel Bender filters and ActionScript / Flex (all inspired by <a href="http://theflashblog.com/?p=435">Lee Brimelow&#8217;s video screencast on creating and using Pixel Bender filters in Flash Player 10</a>.)</p>
<p>Previously, I posted some code showing <a href="http://www.mikechambers.com/blog/2008/09/08/embedding-pixel-bender-filters-within-a-swf/">how to embed a custom Pixel Bender filter within a SWF</a> and then apply the filter to an image. That works well, but the code is not that reusable since the filter loading code is mixed in with the main code.</p>
<p>Below is a simple example that shows how to encapsulate a custom filter inside of an ActionScript 3 class, which you can then use and re-use like any other built in filter.<br />
<span id="more-1463"></span><br />
<strong>Document Class : PBFilter.as</strong></p>
<div class="highlight">
<pre>package
{
	<span style="color: #008000; font-weight: bold">import</span> flash.display.Bitmap<span style="color: #666666">;</span>
	<span style="color: #008000; font-weight: bold">import</span> flash.display.Sprite<span style="color: #666666">;</span>

	<span style="color: #408080; font-style: italic">// SWF Metadata</span>
	[SWF(width<span style="color: #666666">=</span><span style="color: #BA2121">&quot;600&quot;</span><span style="color: #666666">,</span> height<span style="color: #666666">=</span><span style="color: #BA2121">&quot;400&quot;</span><span style="color: #666666">,</span> backgroundColor<span style="color: #666666">=</span><span style="color: #BA2121">&quot;#000000&quot;</span><span style="color: #666666">,</span> framerate<span style="color: #666666">=</span><span style="color: #BA2121">&quot;30&quot;</span>)]

	<span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">class</span> PBFilter <span style="color: #008000; font-weight: bold">extends</span> Sprite
	{
		<span style="color: #408080; font-style: italic">//The image to display the filter on</span>
		[Embed(source<span style="color: #666666">=</span><span style="color: #BA2121">&quot;image.jpg&quot;</span>)]
		<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">var</span> image<span style="color: #666666">:</span>Class<span style="color: #666666">;</span>

		<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">var</span> im<span style="color: #666666">:</span>Bitmap<span style="color: #666666">;</span>

		<span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">function</span> PBFilter()<span style="color: #666666">:</span>void
		{
			im <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> image() as Bitmap<span style="color: #666666">;</span>
			addChild(im);

			<span style="color: #008000; font-weight: bold">var</span> filter<span style="color: #666666">:</span>TestFilter <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> TestFilter();
				filter.value <span style="color: #666666">=</span> <span style="color: #666666">30;</span>

			<span style="color: #408080; font-style: italic">//add the filter to the image</span>
			im.filters <span style="color: #666666">=</span> [filter];
		}

	}
}
</pre>
</div>
<p><strong>Filter Class : TestFilter.as</strong></p>
<div class="highlight">
<pre>package
{
	<span style="color: #008000; font-weight: bold">import</span> flash.display.Shader<span style="color: #666666">;</span>
	<span style="color: #008000; font-weight: bold">import</span> flash.filters.ShaderFilter<span style="color: #666666">;</span>
	<span style="color: #008000; font-weight: bold">import</span> flash.utils.ByteArray<span style="color: #666666">;</span>

	<span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">class</span> TestFilter <span style="color: #008000; font-weight: bold">extends</span> ShaderFilter
	{
		<span style="color: #408080; font-style: italic">//the file that contains the binary bytes of the PixelBender filter</span>
		[Embed(<span style="color: #BA2121">&quot;testfilter.pbj&quot;</span><span style="color: #666666">,</span> mimeType<span style="color: #666666">=</span><span style="color: #BA2121">&quot;application/octet-stream&quot;</span>)]
		<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">var</span> Filter<span style="color: #666666">:</span>Class<span style="color: #666666">;</span>		

		<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">var</span> _shader<span style="color: #666666">:</span>Shader<span style="color: #666666">;</span>

		<span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">function</span> TestFilter(value<span style="color: #666666">:</span><span style="color: #008000">Number</span> <span style="color: #666666">=</span> <span style="color: #666666">50</span>)
		{
			<span style="color: #408080; font-style: italic">//initialize the ShaderFilter with the PixelBender filter we</span>
			<span style="color: #408080; font-style: italic">//embedded</span>
			_shader <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> Shader(<span style="color: #008000; font-weight: bold">new</span> Filter() as ByteArray);

			<span style="color: #408080; font-style: italic">//set the default value</span>
			<span style="color: #008000; font-weight: bold">this</span>.value <span style="color: #666666">=</span> value<span style="color: #666666">;</span>
			<span style="color: #008000; font-weight: bold">super</span>(_shader);
		}

		<span style="color: #408080; font-style: italic">//This filter only has one value, named value</span>
		<span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">function</span> get value()<span style="color: #666666">:</span><span style="color: #008000">Number</span>
		{
			<span style="color: #008000; font-weight: bold">return</span> _shader.data.amount.value[<span style="color: #666666">0</span>];
		}

		<span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">function</span> set value(value<span style="color: #666666">:</span><span style="color: #008000">Number</span>)<span style="color: #666666">:</span>void
		{
			<span style="color: #408080; font-style: italic">//not that pixel bender filters take an array of values, even</span>
			<span style="color: #408080; font-style: italic">//though we only have one in our example</span>
			_shader.data.amount.value <span style="color: #666666">=</span> [value];
		}		

	}
}
</pre>
</div>
<p>Compiled using the <a href="http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+3">Flex 3.1.0.2710 SDK</a> (although it does not use the Flex Framework), and requires <a href="http://labs/technologies/flashplayer10/">Flash Player 10</a>.</p>
<p>You would of course, have to add the appropriate getters and setters depending on the values your custom Pixel Bender filter accepts.</p>
<p>Using this technique, you could then create a SWC (distributable ActionScript library) to package your custom filter(s) and re-use them in MXMLC, Flex Builder and / or Flash Authoring.</p>
<p>You can see an example of the filter <a href="http://flickr.com/photos/mikechambers/2840691963/">here</a>.</p>
<p>You can find more information on Pixel Bender <a href="www.adobe.com/go/pixelbender">here</a>.</p>
<p>You can download the filter from <a href="http://www.gotoandlearn.com/player.php?id=84">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mikechambers.com/blog/2008/09/08/encapsulating-custom-pixel-bender-filters-in-actionscript-3/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Using Pixel Bender Filters within Flex</title>
		<link>http://www.mikechambers.com/blog/2008/09/08/using-pixel-bender-filters-within-flex/</link>
		<comments>http://www.mikechambers.com/blog/2008/09/08/using-pixel-bender-filters-within-flex/#comments</comments>
		<pubDate>Mon, 08 Sep 2008 20:26:36 +0000</pubDate>
		<dc:creator>mikechambers</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[pixelbender]]></category>

		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=1459</guid>
		<description><![CDATA[Following up on my earlier post on how to embed Pixel Bender filters within a SWF, here is a super simple example that show how to use a Pixel Bender filter within a Flex application. Compiled using the Flex 3.1.0.2710 SDK, and requires Flash Player 10. &#60;?xml version=&#34;1.0&#34; encoding=&#34;utf-8&#34;?&#62; &#60;mx:Application xmlns:mx=&#34;http://www.adobe.com/2006/mxml&#34; layout=&#34;absolute&#34; applicationComplete=&#34;onApplicationComplete()&#34;&#62; &#60;mx:Script&#62; &#60;![CDATA[ [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.mikechambers.com%2Fblog%2F2008%2F09%2F08%2Fusing-pixel-bender-filters-within-flex%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif&amp;source=mesh&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Following up on my earlier post on <a href="http://www.mikechambers.com/blog/2008/09/08/embedding-pixel-bender-filters-within-a-swf/">how to embed Pixel Bender filters within a SWF</a>, here is a super simple example that show how to use a Pixel Bender filter within a Flex application.</p>
<p>Compiled using the <a href="http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+3">Flex 3.1.0.2710 SDK</a>, and requires <a href="http://labs/technologies/flashplayer10/">Flash Player 10</a>.<br />
<span id="more-1459"></span></p>
<div class="highlight">
<pre><span style="color: #666666">&lt;?</span>xml version<span style="color: #666666">=</span><span style="color: #BA2121">&quot;1.0&quot;</span> encoding<span style="color: #666666">=</span><span style="color: #BA2121">&quot;utf-8&quot;</span><span style="color: #666666">?&gt;</span>
<span style="color: #666666">&lt;</span>mx<span style="color: #666666">:</span>Application xmlns<span style="color: #666666">:</span>mx<span style="color: #666666">=</span><span style="color: #BA2121">&quot;http://www.adobe.com/2006/mxml&quot;</span> layout<span style="color: #666666">=</span><span style="color: #BA2121">&quot;absolute&quot;</span>
	applicationComplete<span style="color: #666666">=</span><span style="color: #BA2121">&quot;onApplicationComplete()&quot;</span><span style="color: #666666">&gt;</span>

	<span style="color: #666666">&lt;</span>mx<span style="color: #666666">:</span>Script<span style="color: #666666">&gt;</span>
		<span style="color: #666666">&lt;!</span>[CDATA[
			<span style="color: #008000; font-weight: bold">import</span> flash.filters.<span style="color: #666666">*;</span>
			<span style="color: #008000; font-weight: bold">import</span> flash.utils.ByteArray<span style="color: #666666">;</span>		

			<span style="color: #408080; font-style: italic">//the file that contains the binary bytes of the PixelBender filter</span>
			[Embed(<span style="color: #BA2121">&quot;testfilter.pbj&quot;</span><span style="color: #666666">,</span> mimeType<span style="color: #666666">=</span><span style="color: #BA2121">&quot;application/octet-stream&quot;</span>)]
			<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">var</span> TestFilter<span style="color: #666666">:</span>Class<span style="color: #666666">;</span>		

			<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">function</span> onApplicationComplete()<span style="color: #666666">:</span>void
			{
				<span style="color: #408080; font-style: italic">//Pass the loaded filter to the Shader as a ByteArray</span>
				<span style="color: #008000; font-weight: bold">var</span> shader<span style="color: #666666">:</span>Shader <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> Shader(<span style="color: #008000; font-weight: bold">new</span> TestFilter() as ByteArray);

				shader.data.amount.value <span style="color: #666666">=</span> [<span style="color: #666666">100</span>];
				<span style="color: #008000; font-weight: bold">var</span> filter<span style="color: #666666">:</span>ShaderFilter <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> ShaderFilter(shader);

				<span style="color: #408080; font-style: italic">//add the filter to the image</span>
				im.filters <span style="color: #666666">=</span> [filter];
			}
		]]<span style="color: #666666">&gt;</span>
	<span style="color: #666666">&lt;/</span>mx<span style="color: #666666">:</span>Script<span style="color: #666666">&gt;</span>

	<span style="color: #666666">&lt;</span>mx<span style="color: #666666">:</span>Image right<span style="color: #666666">=</span><span style="color: #BA2121">&quot;288&quot;</span> left<span style="color: #666666">=</span><span style="color: #BA2121">&quot;40&quot;</span> top<span style="color: #666666">=</span><span style="color: #BA2121">&quot;26&quot;</span> bottom<span style="color: #666666">=</span><span style="color: #BA2121">&quot;108&quot;</span> id<span style="color: #666666">=</span><span style="color: #BA2121">&quot;im&quot;</span> source<span style="color: #666666">=</span><span style="color: #BA2121">&quot;@Embed(source=&#39;image.jpg&#39;)&quot;</span><span style="color: #666666">/&gt;</span>
<span style="color: #666666">&lt;/</span>mx<span style="color: #666666">:</span>Application<span style="color: #666666">&gt;</span>
</pre>
</div>
<p>You can see an example of the filter <a href="http://flickr.com/photos/mikechambers/2840691963/">here</a>.</p>
<p>You can find more information on Pixel Bender <a href="www.adobe.com/go/pixelbender">here</a>. </p>
<p>You can download the filter from <a href="http://www.gotoandlearn.com/player.php?id=84">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mikechambers.com/blog/2008/09/08/using-pixel-bender-filters-within-flex/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Embedding Pixel Bender Filters within a SWF</title>
		<link>http://www.mikechambers.com/blog/2008/09/08/embedding-pixel-bender-filters-within-a-swf/</link>
		<comments>http://www.mikechambers.com/blog/2008/09/08/embedding-pixel-bender-filters-within-a-swf/#comments</comments>
		<pubDate>Mon, 08 Sep 2008 20:01:37 +0000</pubDate>
		<dc:creator>mikechambers</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[pixelbender]]></category>

		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=1454</guid>
		<description><![CDATA[I was just watching Lee Brimelow&#8217;s excellent video introductions to creating and using Pixel Bender filters in Flash Player 10. In his second video, he shows how to use a custom Pixel Bender filter within Flash Player 10. One issue that Lee had was that you have to load the filter at runtime in order [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.mikechambers.com%2Fblog%2F2008%2F09%2F08%2Fembedding-pixel-bender-filters-within-a-swf%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif&amp;source=mesh&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>I was just watching Lee Brimelow&#8217;s excellent video introductions to <a href="http://theflashblog.com/?p=435">creating and using Pixel Bender filters in Flash Player 10</a>.</p>
<p>In his second video, he shows <a href="http://theflashblog.com/?p=435">how to use a custom Pixel Bender filter within Flash Player 10</a>. One issue that Lee had was that you have to load the filter at runtime in order to use it. </p>
<p>I have modified Lee&#8217;s example to allow the filter to be embedded within the SWF, and not require it to be dynamically loaded at runtime.<br />
<span id="more-1454"></span></p>
<div class="highlight">
<pre>package
{
	<span style="color: #008000; font-weight: bold">import</span> flash.display.<span style="color: #666666">*;</span>
	<span style="color: #008000; font-weight: bold">import</span> flash.events.<span style="color: #666666">*;</span>
	<span style="color: #008000; font-weight: bold">import</span> flash.filters.<span style="color: #666666">*;</span>
	<span style="color: #008000; font-weight: bold">import</span> flash.net.<span style="color: #666666">*;</span>
	<span style="color: #008000; font-weight: bold">import</span> flash.utils.ByteArray<span style="color: #666666">;</span>

	<span style="color: #408080; font-style: italic">// SWF Metadata</span>
	[SWF(width<span style="color: #666666">=</span><span style="color: #BA2121">&quot;600&quot;</span><span style="color: #666666">,</span> height<span style="color: #666666">=</span><span style="color: #BA2121">&quot;400&quot;</span><span style="color: #666666">,</span> backgroundColor<span style="color: #666666">=</span><span style="color: #BA2121">&quot;#000000&quot;</span><span style="color: #666666">,</span> framerate<span style="color: #666666">=</span><span style="color: #BA2121">&quot;30&quot;</span>)]

	<span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">class</span> PB <span style="color: #008000; font-weight: bold">extends</span> Sprite
	{
		<span style="color: #408080; font-style: italic">//the file that contains the binary bytes of the PixelBender filter</span>
		[Embed(<span style="color: #BA2121">&quot;testfilter.pbj&quot;</span><span style="color: #666666">,</span> mimeType<span style="color: #666666">=</span><span style="color: #BA2121">&quot;application/octet-stream&quot;</span>)]
		<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">var</span> TestFilter<span style="color: #666666">:</span>Class<span style="color: #666666">;</span>		

		<span style="color: #408080; font-style: italic">//The image to display the filter on</span>
		[Embed(source<span style="color: #666666">=</span><span style="color: #BA2121">&quot;image.jpg&quot;</span>)]
		<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">var</span> image<span style="color: #666666">:</span>Class<span style="color: #666666">;</span>

		<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">var</span> im<span style="color: #666666">:</span>Bitmap<span style="color: #666666">;</span>

		<span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">function</span> PB()<span style="color: #666666">:</span>void
		{
			im <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> image() as Bitmap<span style="color: #666666">;</span>
			addChild(im);

			<span style="color: #408080; font-style: italic">//Pass the loaded filter to the Shader as a ByteArray</span>
			<span style="color: #008000; font-weight: bold">var</span> shader<span style="color: #666666">:</span>Shader <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> Shader(<span style="color: #008000; font-weight: bold">new</span> TestFilter() as ByteArray);

			shader.data.amount.value <span style="color: #666666">=</span> [<span style="color: #666666">100</span>];
			<span style="color: #008000; font-weight: bold">var</span> filter<span style="color: #666666">:</span>ShaderFilter <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> ShaderFilter(shader);

			<span style="color: #408080; font-style: italic">//add the filter to the image</span>
			im.filters <span style="color: #666666">=</span> [filter];
		}

	}
}
</pre>
</div>
<p>Check out the Flex Cookbook for some good information on <a href="http://www.adobe.com/cfusion/communityengine/index.cfm?event=showdetails&#038;productId=2&#038;postId=8046">How to embed any type of data within a SWF.</a></p>
<p>Make sure to check out Lee&#8217;s video tutorial on <a href="http://theflashblog.com/?p=435">using Pixel Bender filters in Flash Player 10</a>.</p>
<p>You can see an example of the filter <a href="http://flickr.com/photos/mikechambers/2840691963/">here</a>.</p>
<p>You can find more information on <a href="http://labs.adobe.com/wiki/index.php/Pixel_Bender_Toolkit">Pixel Bender on Labs</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mikechambers.com/blog/2008/09/08/embedding-pixel-bender-filters-within-a-swf/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>

