<?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; Search Results  &#187;  pixel+bender</title>
	<atom:link href="http://www.mikechambers.com/blog/search/pixel+bender/feed/rss2/" 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>Case Study : ActionScript 3 Performance Optimization</title>
		<link>http://www.mikechambers.com/blog/2009/10/13/case-study-actionscript-3-performance-optimization/</link>
		<comments>http://www.mikechambers.com/blog/2009/10/13/case-study-actionscript-3-performance-optimization/#comments</comments>
		<pubDate>Tue, 13 Oct 2009 17:27:49 +0000</pubDate>
		<dc:creator>mikechambers</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[actionscript3]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=1865</guid>
		<description><![CDATA[Prompted by some of the work from Grant Skinner (in particular his FOTB 2009 session) and Thibault Imbert, I have been doing a lot of research lately into optimizing ActionScript 3 content. Not just how to make it run faster, but how to approach the process of optimization. I am also starting to work on [...]]]></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%2F2009%2F10%2F13%2Fcase-study-actionscript-3-performance-optimization%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>Prompted by some of the work from <a href="http://www.gskinner.com/blog/">Grant Skinner</a> (in particular his <a href="http://gskinner.com/blog/archives/2004/06/conference_sess.html">FOTB 2009 session</a>) and <a href="http://www.bytearray.org/">Thibault Imbert</a>, I have been doing a lot of research lately into optimizing ActionScript 3 content. Not just how to make it run faster, but how to approach the process of optimization.</p>
<p>I am also starting to work on a small project which works with pixel data from images, and on which I anticipate performance might be an issue when working with larger images. I figured this would be a good opportunity to use some of the early code as a case study. I wanted to post the process and results here.<br />
<span id="more-1865"></span><br />
The task that I will focus on is grabbing a palette of 16 colors from an image, created by averaging the colors within that image. Upon searching on google, I found a <a href="http://blog.soulwire.co.uk/flash/actionscript-3/extract-average-colours-from-bitmapdata/">very good solution over at soulwire.co.uk</a>, which I will use as the base for creating the palette. I want to point out that the original code targeted Flash Player 9 (and thus couldn&#8217;t take advantage of some things such as Vectors), and already ran pretty blazingly fast.</p>
<p>I am using Grant Skinner&#8217;s <a href="http://www.gskinner.com/blog/archives/2009/04/as3_performance.html">performance test harness</a> to profile performance. Each test is run 50 times, and is tested in Flash Player MAC 10,0,32,18 (debug) in the browser. </p>
<p>You can download all of the code from <a href="/blog/files/examples/PixelSort.zip">here</a>.</p>
<p>First, here is the original test case, based on soulwire&#8217;s code:</p>
<div class="highlight">
<pre><span style="color: #408080; font-style: italic">/*</span>
<span style="color: #408080; font-style: italic">	Code adapted from:</span>
<span style="color: #408080; font-style: italic">	http://blog.soulwire.co.uk/flash/actionscript-3/colourutils-bitmapdata-extract-colour-palette/</span>
<span style="color: #408080; font-style: italic">*/</span>

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.<span style="color: #008000">BitmapData</span><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: #008000; font-weight: bold">import</span> flash.events.Event<span style="color: #666666">;</span>
	<span style="color: #008000; font-weight: bold">import</span> flash.geom.<span style="color: #008000">Rectangle</span><span style="color: #666666">;</span>
	<span style="color: #008000; font-weight: bold">import</span> flash.geom.<span style="color: #008000">Point</span><span style="color: #666666">;</span>

	<span style="color: #008000; font-weight: bold">import</span> com.gskinner.utils.PerformanceTest<span style="color: #666666">;</span>

	<span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">class</span> PixelSort <span style="color: #008000; font-weight: bold">extends</span> Sprite
	{

		[Embed(source<span style="color: #666666">=</span><span style="color: #BA2121">&quot;../graphics/image.jpg&quot;</span>)]
		<span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">var</span> TestImage<span style="color: #666666">:</span>Class<span style="color: #666666">;</span>

		<span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">function</span> PixelSort()
		{
			addEventListener(Event.ADDED_TO_STAGE<span style="color: #666666">,</span> onAddedToStage);
		}

		<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">var</span> d<span style="color: #666666">:</span><span style="color: #008000">BitmapData</span><span style="color: #666666">;</span>
		<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">function</span> onAddedToStage(evet<span style="color: #666666">:</span>Event)<span style="color: #666666">:</span>void
		{
			removeEventListener(Event.ADDED_TO_STAGE<span style="color: #666666">,</span> onAddedToStage);

			<span style="color: #008000; font-weight: bold">var</span> b<span style="color: #666666">:</span>Bitmap <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> TestImage();

			d <span style="color: #666666">=</span> b.bitmapData<span style="color: #666666">;</span>

			<span style="color: #008000; font-weight: bold">var</span> perfTest<span style="color: #666666">:</span>PerformanceTest <span style="color: #666666">=</span> PerformanceTest.getInstance();
			perfTest.out <span style="color: #666666">=</span> <span style="color: #0000FF">trace</span><span style="color: #666666">;</span>
			perfTest.testFunction(run<span style="color: #666666">,</span> <span style="color: #666666">50,</span> <span style="color: #BA2121">&quot;averagecolors&quot;</span><span style="color: #666666">,</span> <span style="color: #BA2121">&quot;averagecolors&quot;</span>);
		}

		<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">function</span> run()<span style="color: #666666">:</span>void
		{
			<span style="color: #008000; font-weight: bold">var</span> out<span style="color: #666666">:</span><span style="color: #008000">Array</span> <span style="color: #666666">=</span> averagecolors(d<span style="color: #666666">,</span> <span style="color: #666666">16</span>);
		}

		<span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">function</span> averageColour( source<span style="color: #666666">:</span><span style="color: #008000">BitmapData</span> )<span style="color: #666666">:</span>uint
		{
			<span style="color: #008000; font-weight: bold">var</span> red<span style="color: #666666">:</span><span style="color: #008000">Number</span> <span style="color: #666666">=</span> <span style="color: #666666">0;</span>
			<span style="color: #008000; font-weight: bold">var</span> green<span style="color: #666666">:</span><span style="color: #008000">Number</span> <span style="color: #666666">=</span> <span style="color: #666666">0;</span>
			<span style="color: #008000; font-weight: bold">var</span> blue<span style="color: #666666">:</span><span style="color: #008000">Number</span> <span style="color: #666666">=</span> <span style="color: #666666">0;</span>

			<span style="color: #008000; font-weight: bold">var</span> count<span style="color: #666666">:</span><span style="color: #008000">Number</span> <span style="color: #666666">=</span> <span style="color: #666666">0;</span>
			<span style="color: #008000; font-weight: bold">var</span> pixel<span style="color: #666666">:</span><span style="color: #008000">Number</span><span style="color: #666666">;</span>

			<span style="color: #008000; font-weight: bold">for</span> (<span style="color: #008000; font-weight: bold">var</span> x<span style="color: #666666">:</span>int <span style="color: #666666">=</span> <span style="color: #666666">0;</span> x <span style="color: #666666">&lt;</span> source.width<span style="color: #666666">;</span> x<span style="color: #666666">++</span>)
			{
				<span style="color: #008000; font-weight: bold">for</span> (<span style="color: #008000; font-weight: bold">var</span> y<span style="color: #666666">:</span>int <span style="color: #666666">=</span> <span style="color: #666666">0;</span> y <span style="color: #666666">&lt;</span> source.height<span style="color: #666666">;</span> y<span style="color: #666666">++</span>)
				{
					pixel <span style="color: #666666">=</span> source.getPixel(x<span style="color: #666666">,</span> y);

					red <span style="color: #666666">+=</span> pixel <span style="color: #666666">&gt;&gt;</span> <span style="color: #666666">16</span> <span style="color: #666666">&amp;</span> <span style="color: #666666">0</span>xFF<span style="color: #666666">;</span>
					green <span style="color: #666666">+=</span> pixel <span style="color: #666666">&gt;&gt;</span> <span style="color: #666666">8</span> <span style="color: #666666">&amp;</span> <span style="color: #666666">0</span>xFF<span style="color: #666666">;</span>
					blue <span style="color: #666666">+=</span> pixel <span style="color: #666666">&amp;</span> <span style="color: #666666">0</span>xFF<span style="color: #666666">;</span>

					count<span style="color: #666666">++</span>
				}
			}

			red <span style="color: #666666">/=</span> count<span style="color: #666666">;</span>
			green <span style="color: #666666">/=</span> count<span style="color: #666666">;</span>
			blue <span style="color: #666666">/=</span> count<span style="color: #666666">;</span>

			<span style="color: #008000; font-weight: bold">return</span> red <span style="color: #666666">&lt;&lt;</span> <span style="color: #666666">16</span> <span style="color: #666666">|</span> green <span style="color: #666666">&lt;&lt;</span> <span style="color: #666666">8</span> <span style="color: #666666">|</span> blue<span style="color: #666666">;</span>
		}		

		<span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">function</span> averagecolors( source<span style="color: #666666">:</span><span style="color: #008000">BitmapData</span><span style="color: #666666">,</span> colors<span style="color: #666666">:</span>int )<span style="color: #666666">:</span><span style="color: #008000">Array</span>
		{
			<span style="color: #008000; font-weight: bold">var</span> averages<span style="color: #666666">:</span><span style="color: #008000">Array</span> <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> <span style="color: #008000">Array</span>();
			<span style="color: #008000; font-weight: bold">var</span> columns<span style="color: #666666">:</span>int <span style="color: #666666">=</span> <span style="color: #008000">Math</span>.round( <span style="color: #008000">Math</span>.sqrt( colors ) );

			<span style="color: #008000; font-weight: bold">var</span> row<span style="color: #666666">:</span>int <span style="color: #666666">=</span> <span style="color: #666666">0;</span>
			<span style="color: #008000; font-weight: bold">var</span> col<span style="color: #666666">:</span>int <span style="color: #666666">=</span> <span style="color: #666666">0;</span>

			<span style="color: #008000; font-weight: bold">var</span> x<span style="color: #666666">:</span>int <span style="color: #666666">=</span> <span style="color: #666666">0;</span>
			<span style="color: #008000; font-weight: bold">var</span> y<span style="color: #666666">:</span>int <span style="color: #666666">=</span> <span style="color: #666666">0;</span>

			<span style="color: #008000; font-weight: bold">var</span> w<span style="color: #666666">:</span>int <span style="color: #666666">=</span> <span style="color: #008000">Math</span>.round( source.width <span style="color: #666666">/</span> columns );
			<span style="color: #008000; font-weight: bold">var</span> h<span style="color: #666666">:</span>int <span style="color: #666666">=</span> <span style="color: #008000">Math</span>.round( source.height <span style="color: #666666">/</span> columns );

			<span style="color: #008000; font-weight: bold">for</span> (<span style="color: #008000; font-weight: bold">var</span> i<span style="color: #666666">:</span>int <span style="color: #666666">=</span> <span style="color: #666666">0;</span> i <span style="color: #666666">&lt;</span> colors<span style="color: #666666">;</span> i<span style="color: #666666">++</span>)
			{
				<span style="color: #008000; font-weight: bold">var</span> rect<span style="color: #666666">:</span><span style="color: #008000">Rectangle</span> <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> <span style="color: #008000">Rectangle</span>( x<span style="color: #666666">,</span> y<span style="color: #666666">,</span> w<span style="color: #666666">,</span> h );

				<span style="color: #008000; font-weight: bold">var</span> box<span style="color: #666666">:</span><span style="color: #008000">BitmapData</span> <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> <span style="color: #008000">BitmapData</span>( w<span style="color: #666666">,</span> h<span style="color: #666666">,</span> <span style="color: #008000; font-weight: bold">false</span> );
				box.copyPixels( source<span style="color: #666666">,</span> rect<span style="color: #666666">,</span> <span style="color: #008000; font-weight: bold">new</span> <span style="color: #008000">Point</span>() );

				averages.push( averageColour( box ) );
				box.dispose();

				col <span style="color: #666666">=</span> i <span style="color: #666666">%</span> columns<span style="color: #666666">;</span>

				x <span style="color: #666666">=</span> w <span style="color: #666666">*</span> col<span style="color: #666666">;</span>
				y <span style="color: #666666">=</span> h <span style="color: #666666">*</span> row<span style="color: #666666">;</span>

				<span style="color: #008000; font-weight: bold">if</span> ( col <span style="color: #666666">==</span> columns <span style="color: #666666">-</span> <span style="color: #666666">1</span> ) row<span style="color: #666666">++;</span>
			}

			<span style="color: #008000; font-weight: bold">return</span> averages<span style="color: #666666">;</span>
		}
	}
}
</pre>
</div>
<p>&nbsp;</p>
<p>And here is the initial performance test:</p>
<pre>––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
averagecolors (50 iterations)
Player version: MAC 10,0,32,18 (debug)
averagecolors
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
method...................................................ttl ms...avg ms
averagecolors                                              1264    25.28
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––</pre>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>First, considering what the code is doing, it is already pretty fast, taking only 25 ms to split the image into a grid, and loop through all of the pixels and averaging the values. However, there is probably some room for improvement, especially given that the original code targets Flash Player 9 and thus cant take care of Flash Player 10 optimizations such as using Vectors.</p>
<p>Now, the first thing I would normally do is to profile the SWF using the profiler in Flash Builder to find out where the most time is being sent. However, in this case, there are only two methods that do anything, <em>averageColors</em> and <em>averageColor</em>. <em>averageColors</em> is called once, while <em>averageColor</em> is called once for each swatch we want to create (in this case 16), and ends up looping over each pixel in the image (over those 16 calls). So these are the two areas we will focus on, with particular attention directed to <em>averageColor</em>.</p>
<p>The first thing I did was look at updating the content to Flash Player 10 by converting all of the Arrays to Vectors. I expected to get a decent boost from this, but the improvement was minimal.</p>
<p>Within the <em>averageColors</em> method, I looked at reusing the <em>Point</em>, <em>Rectangle</em> and <em>BitmapData</em> instances, instead of creating new ones on each iteration of the loop. Again, on the desktop this didn&#8217;t really make any difference. However, one thing to consider is that on a mobile device where memory allocation can be more expensive (and there is less RAM altogether), this change may have had a bigger impact (which I didnt test). This leads to an important point. It is important to test performance on the platforms which you are targeting, as some optimizations can have a different impact depending on where the content is running.</p>
<p>Next, I set the <em>averageColor</em> and <em>averageColors</em> methods as <em>final</em>, which allows them too be looked up at compile time (as opposed to runtime), this led to small improvement in performance, but again, not really anything significant.</p>
<p>At this point, I was getting a very slight performance improvement, but not really anything that mattered (basically, small enough to be insignificant),</p>
<pre>––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
averagecolors (50 iterations)
Player version: MAC 10,0,32,18 (debug)
averagecolors
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
method...................................................ttl ms...avg ms
averagecolors                                              1224    24.48
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––</pre>
<p>&nbsp;</p>
<p>Next, I moved on to the <em>averageColor</em> method, where I expected (and hoped) to have better results, as this is where the bulk of the work occurs.</p>
<p>First I converter some of the Numbers to ints and uints in places where Numbers were not needed. This led to a small improvement.</p>
<pre>––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
averagecolors (50 iterations)
Player version: MAC 10,0,32,18 (debug)
averagecolors
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
method...................................................ttl ms...avg ms
averagecolors                                              1190    23.80
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––</pre>
<p>&nbsp;</p>
<p>Next, I changed the <code>bitmapData.getPixel</code> call to use <code>bitmapData.getVector</code>. Doing this then allowed me to loop through the pixels using a single loop, instead of a nested double loop, and also eliminated a <em>getPixel</em> call for each pixel. I used a <em>for each</em> loop to loop through the pixel color values.</p>
<p>This provided another slight improvement (although not quite as much as I expected). We are now making some small gains.</p>
<pre>––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
averagecolors (50 iterations)
Player version: MAC 10,0,32,18 (debug)
averagecolors
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
method...................................................ttl ms...avg ms
averagecolors                                              1137    22.74
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––</pre>
<p>&nbsp;</p>
<p>Next, I decided to try a <em>for</em> loop, instead of a <em>for each</em> loop.</p>
<pre>––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
averagecolors (50 iterations)
Player version: MAC 10,0,32,18 (debug)
averagecolors
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
method...................................................ttl ms...avg ms
averagecolors                                               282     5.64
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––</pre>
<p>&nbsp;</p>
<p>Wow! As you can see, that makes a huge difference.</p>
<p>Finally, I explicitly cast <em>i</em> to an <em>int</em> when pulling the value from the Vector. This gave a small improvement, but again, nothing significant:</p>
<pre>––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
averagecolors (50 iterations)
Player version: MAC 10,0,32,18 (debug)
averagecolors
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
method...................................................ttl ms...avg ms
averagecolors                                               268     5.36
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––</pre>
<p>&nbsp;</p>
<p>I tried a couple of more optimizations in the method, around converting division operations to multiplication operation, and replacing <code>Math.round</code> calls but in this case it didnt make any difference.</p>
<p>I also looked at caching some constants used in some of the bitwise operations, changing</p>
<div class="highlight">
<pre>red <span style="color: #666666">+=</span> pixel <span style="color: #666666">&gt;&gt;</span> <span style="color: #666666">16</span> <span style="color: #666666">&amp;</span> <span style="color: #666666">0</span>xFF<span style="color: #666666">;</span>
green <span style="color: #666666">+=</span> pixel <span style="color: #666666">&gt;&gt;</span> <span style="color: #666666">8</span> <span style="color: #666666">&amp;</span> <span style="color: #666666">0</span>xFF<span style="color: #666666">;</span>
</pre>
</div>
<p>&nbsp;</p>
<p>to</p>
<div class="highlight">
<pre><span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">var</span> s16<span style="color: #666666">:</span><span style="color: #008000">Number</span> <span style="color: #666666">=</span> <span style="color: #666666">16</span> <span style="color: #666666">&amp;</span> <span style="color: #666666">0</span>xFF<span style="color: #666666">;</span>
<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">var</span> s8<span style="color: #666666">:</span><span style="color: #008000">Number</span> <span style="color: #666666">=</span> <span style="color: #666666">8</span> <span style="color: #666666">&amp;</span> <span style="color: #666666">0</span>xFF<span style="color: #666666">;</span>

red <span style="color: #666666">+=</span> pixel <span style="color: #666666">&gt;&gt;</span> s16<span style="color: #666666">;</span>
green <span style="color: #666666">+=</span> pixel <span style="color: #666666">&gt;&gt;</span> s8<span style="color: #666666">;</span>
</pre>
</div>
<p>&nbsp;</p>
<p>First, that optimization actually produces the wrong result (I had my operator precedence backwards). Second, it was actually slower:</p>
<pre>––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
averagecolors (50 iterations)
Player version: MAC 10,0,32,18 (debug)
averagecolors
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
method...................................................ttl ms...avg ms
averagecolors                                               349     6.98
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––</pre>
<p>&nbsp;</p>
<p>There are two lessons from this. First, make sure your optimizations produce the same results (ideally by creating and using unit tests). Second, bitwise operations are really, really fast. In this case, they are even faster than doing a variable lookup.</p>
<p>So, after going through the code, and applying a number of different optimizations, I was able to improve performance from an average of 25.28 ms, to 5.36 ms, an improvement of about 470%.</p>
<p>Here is the final code:</p>
<div class="highlight">
<pre><span style="color: #408080; font-style: italic">/*</span>
<span style="color: #408080; font-style: italic">	Code adapted from:</span>
<span style="color: #408080; font-style: italic">	http://blog.soulwire.co.uk/flash/actionscript-3/colourutils-bitmapdata-extract-colour-palette/</span>
<span style="color: #408080; font-style: italic">*/</span>

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.<span style="color: #008000">BitmapData</span><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: #008000; font-weight: bold">import</span> flash.events.Event<span style="color: #666666">;</span>
	<span style="color: #008000; font-weight: bold">import</span> flash.geom.<span style="color: #008000">Rectangle</span><span style="color: #666666">;</span>
	<span style="color: #008000; font-weight: bold">import</span> flash.geom.<span style="color: #008000">Point</span><span style="color: #666666">;</span>

	<span style="color: #008000; font-weight: bold">import</span> com.gskinner.utils.PerformanceTest<span style="color: #666666">;</span>

	<span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">class</span> PixelSort <span style="color: #008000; font-weight: bold">extends</span> Sprite
	{

		[Embed(source<span style="color: #666666">=</span><span style="color: #BA2121">&quot;../graphics/image.jpg&quot;</span>)]
		<span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">var</span> TestImage<span style="color: #666666">:</span>Class<span style="color: #666666">;</span>

		<span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">function</span> PixelSort()
		{
			addEventListener(Event.ADDED_TO_STAGE<span style="color: #666666">,</span> onAddedToStage);
		}

		<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">var</span> d<span style="color: #666666">:</span><span style="color: #008000">BitmapData</span><span style="color: #666666">;</span>
		<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">function</span> onAddedToStage(evet<span style="color: #666666">:</span>Event)<span style="color: #666666">:</span>void
		{
			removeEventListener(Event.ADDED_TO_STAGE<span style="color: #666666">,</span> onAddedToStage);

			<span style="color: #008000; font-weight: bold">var</span> b<span style="color: #666666">:</span>Bitmap <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> TestImage();

			d <span style="color: #666666">=</span> b.bitmapData<span style="color: #666666">;</span>

			<span style="color: #008000; font-weight: bold">var</span> perfTest<span style="color: #666666">:</span>PerformanceTest <span style="color: #666666">=</span> PerformanceTest.getInstance();
			perfTest.out <span style="color: #666666">=</span> <span style="color: #0000FF">trace</span><span style="color: #666666">;</span>
			perfTest.testFunction(run<span style="color: #666666">,</span> <span style="color: #666666">50,</span> <span style="color: #BA2121">&quot;averagecolors&quot;</span><span style="color: #666666">,</span> <span style="color: #BA2121">&quot;averagecolors&quot;</span>);
		}

		<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">function</span> run()<span style="color: #666666">:</span>void
		{
			<span style="color: #008000; font-weight: bold">var</span> out<span style="color: #666666">:</span>Vector.<span style="color: #666666">&lt;</span>uint<span style="color: #666666">&gt;</span> <span style="color: #666666">=</span> averagecolors(d<span style="color: #666666">,</span> <span style="color: #666666">16</span>);
		}		

		<span style="color: #008000; font-weight: bold">public</span> final <span style="color: #008000; font-weight: bold">function</span> averageColour( source<span style="color: #666666">:</span><span style="color: #008000">BitmapData</span> )<span style="color: #666666">:</span>uint
		{
			<span style="color: #008000; font-weight: bold">var</span> red<span style="color: #666666">:</span><span style="color: #008000">Number</span> <span style="color: #666666">=</span> <span style="color: #666666">0;</span>
			<span style="color: #008000; font-weight: bold">var</span> green<span style="color: #666666">:</span><span style="color: #008000">Number</span> <span style="color: #666666">=</span> <span style="color: #666666">0;</span>
			<span style="color: #008000; font-weight: bold">var</span> blue<span style="color: #666666">:</span><span style="color: #008000">Number</span> <span style="color: #666666">=</span> <span style="color: #666666">0;</span>

			<span style="color: #008000; font-weight: bold">var</span> count<span style="color: #666666">:</span>int <span style="color: #666666">=</span> <span style="color: #666666">0;</span>
			<span style="color: #008000; font-weight: bold">var</span> pixel<span style="color: #666666">:</span>uint<span style="color: #666666">;</span>

			<span style="color: #008000; font-weight: bold">var</span> pixels<span style="color: #666666">:</span>Vector.<span style="color: #666666">&lt;</span>uint<span style="color: #666666">&gt;</span> <span style="color: #666666">=</span> source.getVector(<span style="color: #008000; font-weight: bold">new</span> <span style="color: #008000">Rectangle</span>(<span style="color: #666666">0,0,</span> source.width<span style="color: #666666">,</span> source.height));
			<span style="color: #008000; font-weight: bold">var</span> len<span style="color: #666666">:</span>int <span style="color: #666666">=</span> pixels.length<span style="color: #666666">;</span>

			<span style="color: #008000; font-weight: bold">for</span>(<span style="color: #008000; font-weight: bold">var</span> i<span style="color: #666666">:</span>int <span style="color: #666666">=</span> <span style="color: #666666">0;</span> i <span style="color: #666666">&lt;</span> len<span style="color: #666666">;</span> i<span style="color: #666666">++</span>)
			{
				pixel <span style="color: #666666">=</span> pixels[int(i)];

				red <span style="color: #666666">+=</span> pixel <span style="color: #666666">&gt;&gt;</span> <span style="color: #666666">16</span> <span style="color: #666666">&amp;</span> <span style="color: #666666">0</span>xFF<span style="color: #666666">;</span>
				green <span style="color: #666666">+=</span> pixel <span style="color: #666666">&gt;&gt;</span> <span style="color: #666666">8</span> <span style="color: #666666">&amp;</span> <span style="color: #666666">0</span>xFF<span style="color: #666666">;</span>
				blue <span style="color: #666666">+=</span> pixel <span style="color: #666666">&amp;</span> <span style="color: #666666">0</span>xFF<span style="color: #666666">;</span>

				count<span style="color: #666666">++;</span>
			}

			red <span style="color: #666666">/=</span> count<span style="color: #666666">;</span>
			green <span style="color: #666666">/=</span> count<span style="color: #666666">;</span>
			blue <span style="color: #666666">/=</span> count<span style="color: #666666">;</span>

			<span style="color: #008000; font-weight: bold">return</span> red <span style="color: #666666">&lt;&lt;</span> <span style="color: #666666">16</span> <span style="color: #666666">|</span> green <span style="color: #666666">&lt;&lt;</span> <span style="color: #666666">8</span> <span style="color: #666666">|</span> blue<span style="color: #666666">;</span>
		}		

		<span style="color: #008000; font-weight: bold">public</span> final <span style="color: #008000; font-weight: bold">function</span> averagecolors( source<span style="color: #666666">:</span><span style="color: #008000">BitmapData</span><span style="color: #666666">,</span> colors<span style="color: #666666">:</span>int )<span style="color: #666666">:</span>Vector.<span style="color: #666666">&lt;</span>uint<span style="color: #666666">&gt;</span>
		{

			<span style="color: #008000; font-weight: bold">var</span> averages<span style="color: #666666">:</span>Vector.<span style="color: #666666">&lt;</span>uint<span style="color: #666666">&gt;</span> <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> Vector.<span style="color: #666666">&lt;</span>uint<span style="color: #666666">&gt;</span>(colors<span style="color: #666666">,</span> <span style="color: #008000; font-weight: bold">false</span>);
			<span style="color: #008000; font-weight: bold">var</span> columns<span style="color: #666666">:</span>int <span style="color: #666666">=</span> <span style="color: #008000">Math</span>.round( <span style="color: #008000">Math</span>.sqrt( colors ) );

			<span style="color: #008000; font-weight: bold">var</span> row<span style="color: #666666">:</span>int <span style="color: #666666">=</span> <span style="color: #666666">0;</span>
			<span style="color: #008000; font-weight: bold">var</span> col<span style="color: #666666">:</span>int <span style="color: #666666">=</span> <span style="color: #666666">0;</span>

			<span style="color: #008000; font-weight: bold">var</span> x<span style="color: #666666">:</span>int <span style="color: #666666">=</span> <span style="color: #666666">0;</span>
			<span style="color: #008000; font-weight: bold">var</span> y<span style="color: #666666">:</span>int <span style="color: #666666">=</span> <span style="color: #666666">0;</span>

			<span style="color: #008000; font-weight: bold">var</span> w<span style="color: #666666">:</span>int <span style="color: #666666">=</span> <span style="color: #008000">Math</span>.round( source.width <span style="color: #666666">/</span> columns );
			<span style="color: #008000; font-weight: bold">var</span> h<span style="color: #666666">:</span>int <span style="color: #666666">=</span> <span style="color: #008000">Math</span>.round( source.height <span style="color: #666666">/</span> columns );

			<span style="color: #008000; font-weight: bold">var</span> p<span style="color: #666666">:</span><span style="color: #008000">Point</span> <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> <span style="color: #008000">Point</span>();
			<span style="color: #008000; font-weight: bold">var</span> rect<span style="color: #666666">:</span><span style="color: #008000">Rectangle</span> <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> <span style="color: #008000">Rectangle</span>(<span style="color: #666666">0,0,0,0</span>);
			<span style="color: #008000; font-weight: bold">var</span> box<span style="color: #666666">:</span><span style="color: #008000">BitmapData</span> <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> <span style="color: #008000">BitmapData</span>( w<span style="color: #666666">,</span> h<span style="color: #666666">,</span> <span style="color: #008000; font-weight: bold">false</span> );

			<span style="color: #008000; font-weight: bold">for</span> (<span style="color: #008000; font-weight: bold">var</span> i<span style="color: #666666">:</span>int <span style="color: #666666">=</span> <span style="color: #666666">0;</span> i <span style="color: #666666">&lt;</span> colors<span style="color: #666666">;</span> i<span style="color: #666666">++</span>)
			{
				rect.x <span style="color: #666666">=</span> x<span style="color: #666666">;</span>
				rect.y <span style="color: #666666">=</span> y<span style="color: #666666">;</span>
				rect.width <span style="color: #666666">=</span> w<span style="color: #666666">;</span>
				rect.height <span style="color: #666666">=</span> h<span style="color: #666666">;</span>

				box.copyPixels( source<span style="color: #666666">,</span> rect<span style="color: #666666">,</span> p );

				averages[i] <span style="color: #666666">=</span>  averageColour( box );

				col <span style="color: #666666">=</span> i <span style="color: #666666">%</span> columns<span style="color: #666666">;</span>

				x <span style="color: #666666">=</span> w <span style="color: #666666">*</span> col<span style="color: #666666">;</span>
				y <span style="color: #666666">=</span> h <span style="color: #666666">*</span> row<span style="color: #666666">;</span>

				<span style="color: #008000; font-weight: bold">if</span> ( col <span style="color: #666666">==</span> columns <span style="color: #666666">-</span> <span style="color: #666666">1</span> )
				{
					row<span style="color: #666666">++;</span>
				}
			}
			box.dispose();
			<span style="color: #008000; font-weight: bold">return</span> averages<span style="color: #666666">;</span>
		}
	}
}
</pre>
</div>
<p>&nbsp;</p>
<p><strong>Lessons learned</strong></p>
<p><strong>Profile content to isolate bottlenecks</strong> : I skipped that step in this case since my code consisted of only two methods, but even in that case, the most significant improvement came from a single optimization. Profile so you know where to focus your efforts.</p>
<p><strong>Test and profile all optimizations</strong> : Make sure to test performance after each optimization, as optimizations do not always have the desired effect.</p>
<p><strong>Test on target devices and platforms</strong> : Optimizations can have a different impact on where they are run. This includes browser, platform and device, as well as player type (debug vs release). For example, when testing directly from Flash Authoring, results where significantly slower than when testing in the browser. </p>
<p><strong>Test the results of the optimizations</strong> : Make sure that your optimizations do not break your code or content. The best way to do this is by using unit tests and running them after each optimization.</p>
<p>There is still some potential for optimization. In particular, since the code is essentially looping over all of the pixels of a bitmap and then doing some math operations on their values, this could be a good candidate for porting to PixelBender.</p>
<p>If you have any additional optimizations, questions or suggestions, post them in the comments.</p>
<p>Also, make sure to check out <a href="http://blog.soulwire.co.uk/">soulwire&#8217;s blog</a>, as he is doing some very cool stuff with ActionScript 3 and Flash.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mikechambers.com/blog/2009/10/13/case-study-actionscript-3-performance-optimization/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>New Flash CS4 and Flash Player 10 Articles online</title>
		<link>http://www.mikechambers.com/blog/2008/10/14/new-flash-cs4-and-flash-player-10-articles-online/</link>
		<comments>http://www.mikechambers.com/blog/2008/10/14/new-flash-cs4-and-flash-player-10-articles-online/#comments</comments>
		<pubDate>Wed, 15 Oct 2008 06:36:09 +0000</pubDate>
		<dc:creator>mikechambers</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=1581</guid>
		<description><![CDATA[In conjunction with the CS4 release, the Adobe Developer Center has just post some new articles on Flash Player 10, and Flash Professional CS4. Flash Introducing Adobe Flash CS4 Professional Generating sounds dynamically in Flash Player 10 Creating blends, filters and fills with Adobe Pixel Bender New Flash samples Updated Flash quick starts Flash Player [...]]]></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%2F10%2F14%2Fnew-flash-cs4-and-flash-player-10-articles-online%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>In conjunction with the <a href="http://www.adobe.com/products/creativesuite/">CS4 release</a>, the Adobe Developer Center has just post some new articles on Flash Player 10, and Flash Professional CS4.<br />
<span id="more-1581"></span><br />
<strong>Flash</strong></p>
<ul>
<li><a href="http://www.adobe.com/devnet/logged_in/dwinnie_flcs4.html">Introducing Adobe Flash CS4 Professional</a></li>
<li><a href="http://www.adobe.com/devnet/flash/articles/dynamic_sound_generation.html">Generating sounds dynamically in Flash Player 10</a></li>
<li><a href="http://www.adobe.com/devnet/flash/articles/pixel_bender_basics.html">Creating blends, filters and fills with Adobe Pixel Bender</a></li>
<li><a href="http://www.adobe.com/devnet/flash/?navID=samples">New Flash samples</a></li>
<li><a href="http://www.adobe.com/devnet/flash/?navID=quickstart">Updated Flash quick starts</a></li>
</ul>
<p><strong>Flash Player</strong></p>
<ul>
<li><a href="http://www.adobe.com/devnet/logged_in/jchurch_flashplayer10.html">Introducing Adobe Flash Player 10</a></li>
<li><a href="http://www.adobe.com/devnet/flashplayer/articles/swfobject.html">Detecting Flash Player versions and embedding SWF files with SWFObject 2</a></li>
<li><a href="http://www.adobe.com/devnet/flashplayer/articles/flash_player_admin_guide.html">Adobe Flash Player Administration Guide for Flash Player 10</a></li>
<li><a href="http://www.adobe.com/devnet/flashplayer/articles/fplayer10_security_changes.html">Understanding the security changes in Flash Player 10</a></li>
<li><a href="http://www.adobe.com/devnet/flashplayer/articles/fplayer9_security.html">Policy file changes in Flash Player 9 and Flash Player 10</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.mikechambers.com/blog/2008/10/14/new-flash-cs4-and-flash-player-10-articles-online/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Links for SparkProject.org meeting</title>
		<link>http://www.mikechambers.com/blog/2008/09/26/links-for-sparkprojectorg-meeting/</link>
		<comments>http://www.mikechambers.com/blog/2008/09/26/links-for-sparkprojectorg-meeting/#comments</comments>
		<pubDate>Fri, 26 Sep 2008 11:47:25 +0000</pubDate>
		<dc:creator>mikechambers</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=1547</guid>
		<description><![CDATA[Here are the links I mentioned during the SparkProject.org meeting tonight in Tokyo: Pixel Bender on Labs Creating ActionScript libraries of Pixel Bender Filters Tinic Uro&#8217;s Post on Pixel Bender in the Flash Player Flash Player 10 Drawing API overview Mysql ActioScript 3 Library]]></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%2F26%2Flinks-for-sparkprojectorg-meeting%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>Here are the links I mentioned during the <a href="http://www.libspark.org/">SparkProject.org</a> meeting tonight in Tokyo:</p>
<p><a href="http://labs.adobe.com/wiki/index.php/Pixel_Bender_Toolkit">Pixel Bender on Labs</a><br />
<a href="http://www.mikechambers.com/blog/2008/09/17/creating-re-distributable-actionscript-libraries-of-pixel-bender-filters/">Creating ActionScript libraries of Pixel Bender Filters</a><br />
<a href="http://www.kaourantin.net/2008/05/adobe-pixel-bender-in-flash-player-10.html">Tinic Uro&#8217;s Post on Pixel Bender in the Flash Player</a><br />
<a href="http://www.senocular.com/flash/tutorials/flash10drawingapi/">Flash Player 10 Drawing API overview</a><br />
<a href="http://code.google.com/p/assql/">Mysql ActioScript 3 Library</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mikechambers.com/blog/2008/09/26/links-for-sparkprojectorg-meeting/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<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>Adobe AIR 1.5 (&#8220;Cosmo&#8221;) builds now in Flex SDK Nightly builds</title>
		<link>http://www.mikechambers.com/blog/2008/09/11/adobe-air-15-cosmo-builds-now-in-flex-sdk-nightly-builds/</link>
		<comments>http://www.mikechambers.com/blog/2008/09/11/adobe-air-15-cosmo-builds-now-in-flex-sdk-nightly-builds/#comments</comments>
		<pubDate>Thu, 11 Sep 2008 19:14:14 +0000</pubDate>
		<dc:creator>mikechambers</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=1479</guid>
		<description><![CDATA[If you check out the Flex SDK nightly builds, then you may have noticed that there are now Adobe AIR 1.5 bits included in the SDK. This is part of the AIR teams ongoing effort to continue to integrate closer into the Flex SDKs build process, and in general to provide more visibility into what [...]]]></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%2F11%2Fadobe-air-15-cosmo-builds-now-in-flex-sdk-nightly-builds%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 check out the <a href="http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+3">Flex SDK nightly builds</a>, then you may have noticed that there are now <a href="http://www.adobe.com/go/air">Adobe AIR</a> 1.5 bits included in the SDK. This is part of the AIR teams ongoing effort to continue to integrate closer into the Flex SDKs build process, and in general to provide more visibility into what is going on with AIR.</p>
<p>As the version number indicates, the Adobe AIR 1.5 release  (code-named &#8220;Cosmo&#8221;) is a dot / incremental release. One of our original goals with AIR has been to update more frequently, as opposed to just having large updates at infrequent intervals. This release focuses on continuing to improve the foundation laid by the <a href="http://kb.adobe.com/selfservice/viewContent.do?externalId=kb403153">1.0</a> and <a href="http://kb.adobe.com/selfservice/viewContent.do?externalId=kb403978">1.1</a> release, and includes a number of changes and bug fixes, including, but not limited to:<br />
<span id="more-1479"></span></p>
<ul>
<li>Flash Player 10 integration, including
<ul>
<li>3D Effects and APIs</li>
<li>Custom Filters and Effects (<a href="http://labs.adobe.com/wiki/index.php/Pixel_Bender_Toolkit">Pixel Bender</a>)</li>
<li>New Text engine (with support for right to left, and vertical text layout)</li>
<li>Drawing API enhancements</li>
<li>Updated Saffron engine for font rendering</li>
<li>Vector data type (typed Arrays)</li>
<li>Enhanced Sound APIs</li>
<li>Dynamic video streaming</li>
<li>and <a href="http://www.adobe.com/devnet/logged_in/jchurch_flashplayer10.html">much more</a></li>
</ul>
</li>
<li>Integration of the <a href="http://webkit.org/blog/189/announcing-squirrelfish/">SquirrelFish</a> JavaScript VM into the Webkit HTML engine (should provide significant improvements to JavaScript execution)</li>
<li>Support for new languages
<ul>
<li>Swedish</li>
<li>Dutch</li>
<li>Czech</li>
<li>Polish</li>
<li>Turkish</li>
</ul>
</li>
<li>Encrypted Local / SQLite databases</li>
<li>Lots of bugs fixes and stability improvements, including the uber-annoying
<ul>
<li><a href="http://www.mikechambers.com/blog/2008/07/09/adobe-air-firefox-issue-on-vista/">AIR applications do not use the default browser to launch URLs on Vista</a></li>
<li>AIR applications open a new instance of Firefox to handle urls (as opposed to opening a new tab)</li>
</ul>
</li>
</ul>
<p>Note, that a couple of things from Flash Player 10 may not make the release for AIR 1.5. This includes Mac vector printing, and GPU / hardware accelerated rendering. We are also not picking up experimental features from the current WebKit builds.</p>
<p>The SDK contains the command line tools and libraries to author, test and package AIR files. <strong>It does not include a runtime installer or updated documentation.</strong> There is currently support for Mac and Windows, but not support for Linux (yet). However, expect more news on Linux very soon.	</p>
<p>The nightly releases are not the same as labs releases. Just as with the Flex SDK nightly builds, they have not been tested nearly the same as milestone and labs releases. There are bugs. Sometimes, there will be really bad bugs, but in general, the quality and stability level will continually improve as we move toward the release this fall. While the nightly releases are not targeted at locating bugs, if you do find issues you want to report, you can use the <a href="http://www.adobe.com/cfusion/mmform/index.cfm?name=wishform">bug report form</a> to do so.</p>
<p>Of course, as with any pre-release information, this is all subject to change, although at this point we don&#8217;t anticipate any major changes.</p>
<p>If you have any questions, or want clarification on anything just post a comment below.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mikechambers.com/blog/2008/09/11/adobe-air-15-cosmo-builds-now-in-flex-sdk-nightly-builds/feed/</wfw:commentRss>
		<slash:comments>49</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>
	</channel>
</rss>

