<?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; as3</title>
	<atom:link href="http://www.mikechambers.com/blog/tag/as3/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mikechambers.com/blog</link>
	<description>code = joy</description>
	<lastBuildDate>Thu, 11 Mar 2010 17:52:29 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<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 a [...]]]></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?url=?url=http%3A%2F%2Fwww.mikechambers.com%2Fblog%2F2009%2F10%2F13%2Fcase-study-actionscript-3-performance-optimization%2F&amp;source=mesh&amp;style=normal&amp;service=bit.ly" height="61" width="51" /><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>21</slash:comments>
		</item>
		<item>
		<title>Parsing and displaying BMP files via ActionScript</title>
		<link>http://www.mikechambers.com/blog/2009/09/17/parsing-and-displaying-bmp-files-via-actionscript/</link>
		<comments>http://www.mikechambers.com/blog/2009/09/17/parsing-and-displaying-bmp-files-via-actionscript/#comments</comments>
		<pubDate>Thu, 17 Sep 2009 21:32:20 +0000</pubDate>
		<dc:creator>mikechambers</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[bytearray]]></category>

		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=1830</guid>
		<description><![CDATA[
			
				
			
		
I dont have a formal computer science training / education, so I never got the chance to learn about working with low level data structures (bits and bytes). I have wanted to learn this for some time, but had difficulty finding resources for it which didnt assume I had a computer science degree.
Well, yesterday, FITC [...]]]></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%2F09%2F17%2Fparsing-and-displaying-bmp-files-via-actionscript%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=?url=http%3A%2F%2Fwww.mikechambers.com%2Fblog%2F2009%2F09%2F17%2Fparsing-and-displaying-bmp-files-via-actionscript%2F&amp;source=mesh&amp;style=normal&amp;service=bit.ly" height="61" width="51" /><br />
			</a>
		</div>
<p>I dont have a formal computer science training / education, so I never got the chance to learn about working with low level data structures (bits and bytes). I have wanted to learn this for some time, but had difficulty finding resources for it which didnt assume I had a computer science degree.</p>
<p>Well, yesterday, FITC posted <a href="http://www.fitc.ca/media/">all of the video sessions from FITC Toronto</a>, and I spent some time watching Lee Brimelow&#8217;s presentation on working with ByteArrays. It is a really great session, that provides a clear and solid foundation and understanding of working with ByteArrays and bits and bytes.</p>
<p>Anyways, after watching Lee&#8217;s session, it all finally clicked for me, and I spent some time last night putting together a simple parser that would dynamically load and display a <a href="http://en.wikipedia.org/wiki/BMP%5Ffile%5Fformat">24Bit BMP image file</a> within Flash.<br />
<span id="more-1830"></span><br />
I wanted to post the code below, along with complete comments, in order to provide a simple, real world example for anyone else interested in learning how to work with lower level file formats. </p>
<p>The code requires Adobe AIR (so I can load the BMP directly). In order to convert to the Flash Player in the browser, just replace the File loading with FileReference.browse.</p>
<div class="highlight">
<pre>package
{
	<span style="color: #008000; font-weight: bold">import</span> flash.filesystem.File<span style="color: #666666">;</span>
	<span style="color: #008000; font-weight: bold">import</span> flash.filesystem.FileStream<span style="color: #666666">;</span>
	<span style="color: #008000; font-weight: bold">import</span> flash.filesystem.FileMode<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.display.<span style="color: #008000">BitmapData</span><span style="color: #666666">;</span>
	<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.utils.Endian<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>

	[SWF(width<span style="color: #666666">=</span><span style="color: #BA2121">&#39;550&#39;</span><span style="color: #666666">,</span> height<span style="color: #666666">=</span><span style="color: #BA2121">&#39;400&#39;</span><span style="color: #666666">,</span> backgroundColor<span style="color: #666666">=</span><span style="color: #BA2121">&#39;#FFFFFF&#39;</span><span style="color: #666666">,</span> frameRate<span style="color: #666666">=</span><span style="color: #BA2121">&#39;12&#39;</span>)]
	<span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">class</span> BMPViewer <span style="color: #008000; font-weight: bold">extends</span> Sprite
	{
		<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">static</span> const MAGIC_NUMBER<span style="color: #666666">:</span><span style="color: #008000">String</span> <span style="color: #666666">=</span> <span style="color: #BA2121">&quot;BM&quot;</span><span style="color: #666666">;</span>
		<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">static</span> const BMP_DATA_OFFSET_POSITION<span style="color: #666666">:</span>int <span style="color: #666666">=</span> <span style="color: #666666">0</span>xA<span style="color: #666666">;</span>
		<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">static</span> const WIDTH_POSITION<span style="color: #666666">:</span>int <span style="color: #666666">=</span> <span style="color: #666666">0x12;</span>
		<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">static</span> const HEIGHT_POSITION<span style="color: #666666">:</span>int <span style="color: #666666">=</span> <span style="color: #666666">0x16;</span>

		<span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">function</span> BMPViewer()
		{
			loadBMP();
			<span style="color: #008000; font-weight: bold">super</span>();
		}

		<span style="color: #408080; font-style: italic">/*</span>
<span style="color: #408080; font-style: italic">			Loads and reads a 24 Bit bitmap file.</span>
<span style="color: #408080; font-style: italic">			</span>
<span style="color: #408080; font-style: italic">			Based on BMP info from:</span>
<span style="color: #408080; font-style: italic">			http://en.wikipedia.org/wiki/BMP%5Ffile%5Fformat</span>
<span style="color: #408080; font-style: italic">		*/</span>
		<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">function</span> loadBMP()<span style="color: #666666">:</span>void
		{
			<span style="color: #408080; font-style: italic">//Load BMP. This requires AIR.</span>
			<span style="color: #408080; font-style: italic">//Use FileReference.browse for</span>
			<span style="color: #408080; font-style: italic">//Flash Player</span>
			<span style="color: #008000; font-weight: bold">var</span> bmpFile<span style="color: #666666">:</span>File <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> File(<span style="color: #BA2121">&quot;app:/image.bmp&quot;</span>);
			<span style="color: #008000; font-weight: bold">var</span> fs<span style="color: #666666">:</span>FileStream <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> FileStream();

			<span style="color: #408080; font-style: italic">//BMP files are Little Endian, which means their</span>
			<span style="color: #408080; font-style: italic">//least significant byte is first (right to left)</span>
			fs.endian <span style="color: #666666">=</span> Endian.LITTLE_ENDIAN<span style="color: #666666">;</span>

			<span style="color: #408080; font-style: italic">//open the file in READ mode</span>
			fs.open(bmpFile<span style="color: #666666">,</span> FileMode.READ);

			<span style="color: #408080; font-style: italic">//check the first two bytes to make sure</span>
			<span style="color: #408080; font-style: italic">//it is a valid BMP file</span>
			<span style="color: #008000; font-weight: bold">if</span>(fs.readUTFBytes(<span style="color: #666666">2</span>) <span style="color: #666666">!=</span> MAGIC_NUMBER)
			{
				<span style="color: #0000FF">trace</span>(<span style="color: #BA2121">&quot;FAIL : NOT A BMP FILE&quot;</span>);

				<span style="color: #408080; font-style: italic">//not a BMP file, close steam</span>
				<span style="color: #408080; font-style: italic">//and exit</span>
				fs.close();
				<span style="color: #008000; font-weight: bold">return</span><span style="color: #666666">;</span>
			}

			<span style="color: #408080; font-style: italic">//note, we could also grab the length from the </span>
			<span style="color: #408080; font-style: italic">//header and make sure the file was the correct</span>
			<span style="color: #408080; font-style: italic">//length</span>

			<span style="color: #408080; font-style: italic">//change the cursors position to the point</span>
			<span style="color: #408080; font-style: italic">//in the header that contains the value / offset</span>
			<span style="color: #408080; font-style: italic">//of where the actual bitmap data begins</span>
			<span style="color: #408080; font-style: italic">//read in the 4 Bytes that contain the value</span>
			fs.position <span style="color: #666666">=</span> BMP_DATA_OFFSET_POSITION<span style="color: #666666">;</span>
			<span style="color: #008000; font-weight: bold">var</span> dataPosition<span style="color: #666666">:</span>int <span style="color: #666666">=</span> fs.readInt();

			<span style="color: #408080; font-style: italic">//set cursor position to where the BMP</span>
			<span style="color: #408080; font-style: italic">//width is stored</span>
			fs.position <span style="color: #666666">=</span> WIDTH_POSITION<span style="color: #666666">;</span>

			<span style="color: #408080; font-style: italic">//read in the 4 Bytes that contain the width</span>
			<span style="color: #008000; font-weight: bold">var</span> bmpWidth<span style="color: #666666">:</span>int <span style="color: #666666">=</span> fs.readInt();

			<span style="color: #408080; font-style: italic">//read in the 4 Bytes that contain the height</span>
			<span style="color: #008000; font-weight: bold">var</span> bmpHeight<span style="color: #666666">:</span>int <span style="color: #666666">=</span> fs.readInt();

			<span style="color: #408080; font-style: italic">//set cursor to where the BMP pixel data begins</span>
			fs.position <span style="color: #666666">=</span> dataPosition<span style="color: #666666">;</span>

			<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> column<span style="color: #666666">:</span>int <span style="color: #666666">=</span> <span style="color: #666666">0;</span>

			<span style="color: #408080; font-style: italic">//every row length in a BMP file must bee a multiple</span>
			<span style="color: #408080; font-style: italic">//of 4 (see the spec). So, we need to determine how much</span>
			<span style="color: #408080; font-style: italic">//padding we need to add at the end of each line. </span>
			<span style="color: #008000; font-weight: bold">var</span> padding<span style="color: #666666">:</span>int <span style="color: #666666">=</span> (bmpWidth <span style="color: #666666">%</span> <span style="color: #666666">4</span>);

			<span style="color: #408080; font-style: italic">//create a fixed length Vector to store the pixel</span>
			<span style="color: #408080; font-style: italic">//values as we read them.</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> <span style="color: #008000; font-weight: bold">new</span> Vector.<span style="color: #666666">&lt;</span>uint<span style="color: #666666">&gt;</span>(bmpWidth <span style="color: #666666">*</span> bmpHeight<span style="color: #666666">,</span> <span style="color: #008000; font-weight: bold">true</span>);

			<span style="color: #408080; font-style: italic">//loop through data (rows and columns)</span>
			<span style="color: #408080; font-style: italic">//note that data stored in BMP is backwards to Flash and is</span>
			<span style="color: #408080; font-style: italic">//stored from bottom row up, not top row down.</span>
			<span style="color: #408080; font-style: italic">//So we have to loop backwards</span>
			<span style="color: #008000; font-weight: bold">var</span> counter<span style="color: #666666">:</span>int <span style="color: #666666">=</span> <span style="color: #666666">0;</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> bmpHeight<span style="color: #666666">;</span> i <span style="color: #666666">&gt;</span> <span style="color: #666666">0;</span> i<span style="color: #666666">--</span>)
			{
				<span style="color: #008000; font-weight: bold">for</span>(<span style="color: #008000; font-weight: bold">var</span> k<span style="color: #666666">:</span>int <span style="color: #666666">=</span> <span style="color: #666666">0;</span> k <span style="color: #666666">&lt;</span> bmpWidth<span style="color: #666666">;</span> k<span style="color: #666666">++</span>)
				{

					<span style="color: #008000; font-weight: bold">var</span> position<span style="color: #666666">:</span>int <span style="color: #666666">=</span> ((i <span style="color: #666666">-</span> <span style="color: #666666">1</span>) <span style="color: #666666">*</span> bmpWidth) <span style="color: #666666">+</span> k<span style="color: #666666">;</span>
					<span style="color: #408080; font-style: italic">/*</span>
<span style="color: #408080; font-style: italic">						This is the original code that I had which works fine</span>
<span style="color: #408080; font-style: italic">						but is not as effecient as what I have now.</span>
<span style="color: #408080; font-style: italic">						</span>
<span style="color: #408080; font-style: italic">						Basically, Pixels are stored within 3 sucessive Bytes</span>
<span style="color: #408080; font-style: italic">						in a BMP file, with one Byte each for Blue, Green and</span>
<span style="color: #408080; font-style: italic">						Red values (in that order).</span>
<span style="color: #408080; font-style: italic">						</span>
<span style="color: #408080; font-style: italic">						So, this reads the Bytes for each pixel, one at a time</span>
<span style="color: #408080; font-style: italic">						and then combines them into a single value which is</span>
<span style="color: #408080; font-style: italic">						the combined RGB pixel value.</span>
<span style="color: #408080; font-style: italic">						</span>
<span style="color: #408080; font-style: italic">						I left the code as I think it make it a little easier to</span>
<span style="color: #408080; font-style: italic">						understand what is going on, as well as how some of these</span>
<span style="color: #408080; font-style: italic">						calls can be optimized.</span>
<span style="color: #408080; font-style: italic">					*/</span>

					<span style="color: #408080; font-style: italic">/*</span>
<span style="color: #408080; font-style: italic">					var blue:int = fs.readUnsignedByte();</span>
<span style="color: #408080; font-style: italic">					var green:int = fs.readUnsignedByte();</span>
<span style="color: #408080; font-style: italic">					var red:int = fs.readUnsignedByte();</span>
<span style="color: #408080; font-style: italic">					</span>
<span style="color: #408080; font-style: italic">					pixels[position] = (red &lt;&lt; 16 ^ green &lt;&lt; 8 ^ blue);</span>
<span style="color: #408080; font-style: italic">					*/</span>

					<span style="color: #408080; font-style: italic">/*</span>
<span style="color: #408080; font-style: italic">						Here is the final code which is more efficient, as it only</span>
<span style="color: #408080; font-style: italic">						needs to make 2 read calls in order to get the values.</span>
<span style="color: #408080; font-style: italic">						</span>
<span style="color: #408080; font-style: italic">						Thanks to Thibault Imbert (bytearray.org) for pointing out</span>
<span style="color: #408080; font-style: italic">						and helping me understand the optimization.</span>
<span style="color: #408080; font-style: italic">					*/</span>

					<span style="color: #408080; font-style: italic">//bytes in file are in Blue, Green, Red order</span>
					<span style="color: #408080; font-style: italic">//int is 32 bits (8 bytes). So, we store the first two bytes of the pixel</span>
					<span style="color: #408080; font-style: italic">// (which contain the Red value), and</span>
					<span style="color: #408080; font-style: italic">//then shift everything over 1 byte (8bits) to make room for</span>
					<span style="color: #408080; font-style: italic">//the green and blue values (remember the file is little endian), which we</span>
					<span style="color: #408080; font-style: italic">// then write into the int in the right position</span>
					<span style="color: #408080; font-style: italic">//The final value has the colors in the correct order (Red, Green, Blue)</span>

					<span style="color: #008000; font-weight: bold">var</span> pixelValue<span style="color: #666666">:</span>uint <span style="color: #666666">=</span> fs.readUnsignedByte() <span style="color: #666666">|</span> fs.readUnsignedShort() <span style="color: #666666">&lt;&lt;</span> <span style="color: #666666">8;</span>
					pixels[position] <span style="color: #666666">=</span> pixelValue<span style="color: #666666">;</span>
				}

				<span style="color: #408080; font-style: italic">//we are at the end of the row, so now we have to move the cursor</span>
				<span style="color: #408080; font-style: italic">//forward so it ends on a multiple of 4</span>
				<span style="color: #008000; font-weight: bold">if</span>(padding)
				{
					fs.position <span style="color: #666666">+=</span> padding<span style="color: #666666">;</span>
				}
			}

			<span style="color: #408080; font-style: italic">//done reading file, close stream.</span>
			fs.close();

			<span style="color: #408080; font-style: italic">//create a Rectangle with width / height of Bitmap</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,</span> <span style="color: #666666">0,</span> bmpWidth<span style="color: #666666">,</span> bmpHeight);

			<span style="color: #408080; font-style: italic">//create the BitmapData object to hold hold the BMP data.</span>
			<span style="color: #408080; font-style: italic">//we do a red fill here so it is easier to see if we have any errors</span>
			<span style="color: #408080; font-style: italic">//in our code</span>
			<span style="color: #008000; font-weight: bold">var</span> bmpData<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>(bmpWidth<span style="color: #666666">,</span> bmpHeight<span style="color: #666666">,</span> <span style="color: #008000; font-weight: bold">false</span><span style="color: #666666">,</span> <span style="color: #666666">0</span>xFF0000);

			<span style="color: #408080; font-style: italic">//copy the BMP pixel data into the BitmapData</span>
			bmpData.setVector(rect<span style="color: #666666">,</span> pixels);

			<span style="color: #408080; font-style: italic">//create a new Bitmap instance using the BitmapData</span>
			<span style="color: #008000; font-weight: bold">var</span> bitmap<span style="color: #666666">:</span>Bitmap <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> Bitmap(bmpData);
			bitmap.x <span style="color: #666666">=</span> <span style="color: #666666">10;</span>
			bitmap.y <span style="color: #666666">=</span> <span style="color: #666666">10;</span>

			<span style="color: #408080; font-style: italic">//add Bitmap to the display list</span>
			addChild(bitmap);
		}
	}
}
</pre>
</div>
<p>&nbsp;</p>
<p>You can download the example from <a href="/blog/files/examples/BMPViewer.zip">here</a>.</p>
<p>Thanks to <a href="http://www.theflashblog.com">Lee</a> for his presentation, and <a href="http://www.bytearray.org">Thibault Imber</a>t who helped me understand some of the details around endianes, as well as made some suggestions for optimizations.</p>
<p>If you are interested in learning more, I strong suggest watching L<a href="http://www.fitc.ca/media/">ee&#8217;s FITC Presentation</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mikechambers.com/blog/2009/09/17/parsing-and-displaying-bmp-files-via-actionscript/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Relative performance for collision detection techniques in ActionScript 3</title>
		<link>http://www.mikechambers.com/blog/2009/06/26/relative-performance-for-collision-detection-techniques-in-actionscript-3/</link>
		<comments>http://www.mikechambers.com/blog/2009/06/26/relative-performance-for-collision-detection-techniques-in-actionscript-3/#comments</comments>
		<pubDate>Fri, 26 Jun 2009 22:28:22 +0000</pubDate>
		<dc:creator>mikechambers</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[collision_detection]]></category>

		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=1758</guid>
		<description><![CDATA[
			
				
			
		
If you have read my blog any this week, you have probably noticed that I have been doing some basic research on collision detection within the Flash Player. As part of this, I have put together a simple test suite, showing the performance of a couple of different techniques for checking for collision. This is [...]]]></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%2F06%2F26%2Frelative-performance-for-collision-detection-techniques-in-actionscript-3%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=?url=http%3A%2F%2Fwww.mikechambers.com%2Fblog%2F2009%2F06%2F26%2Frelative-performance-for-collision-detection-techniques-in-actionscript-3%2F&amp;source=mesh&amp;style=normal&amp;service=bit.ly" height="61" width="51" /><br />
			</a>
		</div>
<p>If you have read my blog any <a href="http://www.mikechambers.com/blog/2009/06/24/using-bitmapdata-hittest-for-collision-detection/">this</a> <a href="http://www.mikechambers.com/blog/2009/06/25/strategies-for-optimizing-collision-detection-with-bitmapdata-hittest/">week</a>, you have probably noticed that I have been doing some basic research on collision detection within the Flash Player. As part of this, I have put together a simple test suite, showing the performance of a couple of different techniques for checking for collision. This is by no means meant to be exhaustive (and currently tilts towards boundary collision). However, I wanted to post the results as the current information is useful (if nothing more than to confirm existing assumptions), and perhaps generate more tests an ideas around collision detection.<br />
<span id="more-1758"></span><br />
First, the test code (uses Flash Pro). clip1 and clip2 reference two overlapping MovieClips on the stage (clip2 is about 1/2 the size of clip1).</p>
<div class="highlight">
<pre>package
{
	<span style="color: #008000; font-weight: bold">import</span> flash.display.<span style="color: #008000">MovieClip</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> 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.geom.<span style="color: #008000">Rectangle</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> CollisionDetectionTests <span style="color: #008000; font-weight: bold">extends</span> <span style="color: #008000">MovieClip</span>
	{
		<span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">var</span> clip1<span style="color: #666666">:</span><span style="color: #008000">MovieClip</span><span style="color: #666666">;</span>
		<span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">var</span> clip2<span style="color: #666666">:</span><span style="color: #008000">MovieClip</span><span style="color: #666666">;</span>

		<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">var</span> dynamicClip1<span style="color: #666666">:</span><span style="color: #008000">MovieClip</span><span style="color: #666666">;</span>
		<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">var</span> dynamicClip2<span style="color: #666666">:</span><span style="color: #008000">MovieClip</span><span style="color: #666666">;</span>	

		<span style="color: #008000; font-weight: bold">var</span> point1<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> point2<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">private</span> <span style="color: #008000; font-weight: bold">var</span> clip1Rect<span style="color: #666666">:</span><span style="color: #008000">Rectangle</span><span style="color: #666666">;</span>
		<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">var</span> clip1ClipBmpData<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">var</span> clip2Rect<span style="color: #666666">:</span><span style="color: #008000">Rectangle</span><span style="color: #666666">;</span>
		<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">var</span> clip2ClipBmpData<span style="color: #666666">:</span><span style="color: #008000">BitmapData</span><span style="color: #666666">;</span>

		<span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">function</span> CollisionDetectionTests()
		{
			<span style="color: #008000; font-weight: bold">super</span>();

			init();
			checkCollisions();
			runTests();
		}

		<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">function</span> init()<span style="color: #666666">:</span>void
		{

			clip1Rect <span style="color: #666666">=</span> clip1.getBounds(<span style="color: #008000; font-weight: bold">this</span>);
			clip1ClipBmpData <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> <span style="color: #008000">BitmapData</span>(clip1Rect.width<span style="color: #666666">,</span> clip1Rect.height<span style="color: #666666">,</span> <span style="color: #008000; font-weight: bold">true</span><span style="color: #666666">,</span> <span style="color: #666666">0</span>);
			clip1ClipBmpData.draw(clip1);

			clip2Rect <span style="color: #666666">=</span> clip2.getBounds(<span style="color: #008000; font-weight: bold">this</span>);
			clip2ClipBmpData <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> <span style="color: #008000">BitmapData</span>(clip2Rect.width<span style="color: #666666">,</span> clip2Rect.height<span style="color: #666666">,</span> <span style="color: #008000; font-weight: bold">true</span><span style="color: #666666">,</span> <span style="color: #666666">0</span>);
			clip2ClipBmpData.draw(clip2);

			dynamicClip1 <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> <span style="color: #008000">MovieClip</span>();

			dynamicClip1.graphics.beginFill(<span style="color: #666666">0</span>xFF0000);
			dynamicClip1.graphics.drawEllipse(<span style="color: #666666">0,0,</span> <span style="color: #666666">100,100</span>);
			dynamicClip1.cacheAsBitmap <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">true</span><span style="color: #666666">;</span>

			dynamicClip1.x <span style="color: #666666">=</span> <span style="color: #666666">300;</span>
			dynamicClip1.y <span style="color: #666666">=</span> <span style="color: #666666">300;</span>

			addChild(dynamicClip1);

			dynamicClip2 <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> <span style="color: #008000">MovieClip</span>();

			dynamicClip2.graphics.beginFill(<span style="color: #666666">0x0000</span>FF);
			dynamicClip2.graphics.moveTo(<span style="color: #666666">0,</span> <span style="color: #666666">0</span>);
			dynamicClip2.graphics.lineTo(<span style="color: #666666">100,</span> <span style="color: #666666">0</span>);
			dynamicClip2.graphics.lineTo(<span style="color: #666666">100,</span> <span style="color: #666666">100</span>);
			dynamicClip2.graphics.lineTo(<span style="color: #666666">0,</span> <span style="color: #666666">100</span>);
			dynamicClip2.graphics.lineTo(<span style="color: #666666">0,</span> <span style="color: #666666">0</span>);
			dynamicClip2.cacheAsBitmap <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">true</span><span style="color: #666666">;</span>

			dynamicClip2.x <span style="color: #666666">=</span> <span style="color: #666666">250;</span>
			dynamicClip2.y <span style="color: #666666">=</span> <span style="color: #666666">250;</span>

			addChild(dynamicClip2);
		}

		<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">function</span> checkCollisions()<span style="color: #666666">:</span>void
		{
			<span style="color: #408080; font-style: italic">//make sure everything is working and returns true</span>
			<span style="color: #0000FF">trace</span>(checkHitTest());
			<span style="color: #0000FF">trace</span>(boundsIntersection());
			<span style="color: #0000FF">trace</span>(checkHitTestReverse());
			<span style="color: #0000FF">trace</span>(checkBoundsManually());
			<span style="color: #0000FF">trace</span>(hitTestCircle());
			<span style="color: #0000FF">trace</span>(checkBitmapDataHit());
			<span style="color: #0000FF">trace</span>(checkBitmapDataHitReverse());
			<span style="color: #0000FF">trace</span>(checkBitmapDataHitInternal());
			<span style="color: #0000FF">trace</span>(checkBitmapDataHitDynamic());
		}

		<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">function</span> runTests()<span style="color: #666666">:</span>void
		{
			<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> out<span style="color: #666666">;</span>

			<span style="color: #408080; font-style: italic">//this is to work around bug in test lib</span>
			perfTest.testSuite({});

			perfTest.testFunction(checkHitTest<span style="color: #666666">,</span> <span style="color: #666666">10000,</span> <span style="color: #BA2121">&quot;checkHitTest&quot;</span><span style="color: #666666">,</span> <span style="color: #BA2121">&quot;Uses DisplayObject.hitTest&quot;</span>);
			perfTest.testFunction(checkHitTestReverse<span style="color: #666666">,</span> <span style="color: #666666">10000,</span> <span style="color: #BA2121">&quot;checkHitTestReverse&quot;</span><span style="color: #666666">,</span> <span style="color: #BA2121">&quot;Uses DisplayObject.hitTest with clips reversed&quot;</span>);
			perfTest.testFunction(boundsIntersection<span style="color: #666666">,</span> <span style="color: #666666">10000,</span> <span style="color: #BA2121">&quot;boundsIntersection&quot;</span><span style="color: #666666">,</span> <span style="color: #BA2121">&quot;Uses Rectangle.intersects()&quot;</span>);
			perfTest.testFunction(checkBoundsManually<span style="color: #666666">,</span> <span style="color: #666666">10000,</span> <span style="color: #BA2121">&quot;checkBoundsManually&quot;</span><span style="color: #666666">,</span> <span style="color: #BA2121">&quot;manually checks if bounds intersect.&quot;</span>);
			perfTest.testFunction(hitTestCircle<span style="color: #666666">,</span> <span style="color: #666666">10000,</span> <span style="color: #BA2121">&quot;hitTestCircle&quot;</span><span style="color: #666666">,</span> <span style="color: #BA2121">&quot;Check if bounding circles intersect.&quot;</span>);
			perfTest.testFunction(checkBitmapDataHit<span style="color: #666666">,</span> <span style="color: #666666">10000,</span> <span style="color: #BA2121">&quot;checkBitmapDataHit&quot;</span><span style="color: #666666">,</span> <span style="color: #BA2121">&quot;Uses BitmapData.hitTest to check for collision.&quot;</span>);
			perfTest.testFunction(checkBitmapDataHitReverse<span style="color: #666666">,</span> <span style="color: #666666">10000,</span> <span style="color: #BA2121">&quot;checkBitmapDataHitReverse&quot;</span><span style="color: #666666">,</span> <span style="color: #BA2121">&quot;Uses BitmapData.hitTest to check for collision. Instances reversed.&quot;</span>);
			perfTest.testFunction(checkBitmapDataHitInternal<span style="color: #666666">,</span> <span style="color: #666666">10000,</span> <span style="color: #BA2121">&quot;checkBitmapDataHitInternal&quot;</span><span style="color: #666666">,</span> <span style="color: #BA2121">&quot;Uses BitmapData.hitTest to check for collision. BitmapData not cached.&quot;</span>);
			perfTest.testFunction(checkBitmapDataHitDynamic<span style="color: #666666">,</span> <span style="color: #666666">10000,</span> <span style="color: #BA2121">&quot;checkBitmapDataHitDynamic&quot;</span><span style="color: #666666">,</span> <span style="color: #BA2121">&quot;Uses BitmapData.hitTest to check for collision. BitmapData not cached. MovieClips dynamic.&quot;</span>);
		}

		<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">function</span> checkHitTest()<span style="color: #666666">:</span><span style="color: #008000">Boolean</span>
		{
			<span style="color: #008000; font-weight: bold">return</span> clip1.hitTestObject(clip2);
		}

		<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">function</span> checkHitTestReverse()<span style="color: #666666">:</span><span style="color: #008000">Boolean</span>
		{
			<span style="color: #008000; font-weight: bold">return</span> clip2.hitTestObject(clip1);
		}

		<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">function</span> boundsIntersection()<span style="color: #666666">:</span><span style="color: #008000">Boolean</span>
		{
			<span style="color: #008000; font-weight: bold">return</span> clip1.getBounds(<span style="color: #008000; font-weight: bold">this</span>).intersects(clip2.getBounds(<span style="color: #008000; font-weight: bold">this</span>));
		}

		<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">function</span> hitTestCircle()<span style="color: #666666">:</span><span style="color: #008000">Boolean</span>
		{
			<span style="color: #008000; font-weight: bold">var</span> dx<span style="color: #666666">:</span><span style="color: #008000">Number</span> <span style="color: #666666">=</span> (clip2.x <span style="color: #666666">+</span> clip2.width <span style="color: #BB6688">/ 2) - (clip1.x + clip1.width /</span> <span style="color: #666666">2</span>);
			<span style="color: #008000; font-weight: bold">var</span> dy<span style="color: #666666">:</span><span style="color: #008000">Number</span> <span style="color: #666666">=</span> (clip2.y <span style="color: #666666">+</span> clip2.height <span style="color: #BB6688">/ 2) - (clip1.y + clip1.height /</span> <span style="color: #666666">2</span>);
			<span style="color: #008000; font-weight: bold">var</span> dist<span style="color: #666666">:</span><span style="color: #008000">Number</span> <span style="color: #666666">=</span> <span style="color: #008000">Math</span>.sqrt(dx <span style="color: #666666">*</span> dx <span style="color: #666666">+</span> dy <span style="color: #666666">*</span> dy);

			<span style="color: #008000; font-weight: bold">return</span> (dist <span style="color: #666666">&lt;</span> ((clip1.width <span style="color: #BB6688">/ 2) + (clip2.width /</span> <span style="color: #666666">2</span>)));
		}

		<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">function</span> checkBoundsManually()<span style="color: #666666">:</span><span style="color: #008000">Boolean</span>
		{
			<span style="color: #008000; font-weight: bold">if</span>(clip1.x <span style="color: #666666">&lt;</span> clip2.x <span style="color: #666666">+</span> clip2.width <span style="color: #666666">&amp;&amp;</span>
			   clip2.x <span style="color: #666666">&lt;</span> clip1.x <span style="color: #666666">+</span> clip1.width <span style="color: #666666">&amp;&amp;</span>
			   clip1.y <span style="color: #666666">&lt;</span> clip2.y <span style="color: #666666">+</span> clip2.height <span style="color: #666666">&amp;&amp;</span>
			   clip2.y <span style="color: #666666">&lt;</span> clip1.y <span style="color: #666666">+</span> clip1.height
			   )
			{
				<span style="color: #008000; font-weight: bold">return</span> <span style="color: #008000; font-weight: bold">true</span><span style="color: #666666">;</span>
			}

			<span style="color: #008000; font-weight: bold">return</span> <span style="color: #008000; font-weight: bold">false</span><span style="color: #666666">;</span>
		}

		<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">function</span> checkBitmapDataHit()<span style="color: #666666">:</span><span style="color: #008000">Boolean</span>
		{
			point1.x <span style="color: #666666">=</span> clip1.x<span style="color: #666666">;</span>
			point1.y <span style="color: #666666">=</span> clip1.y<span style="color: #666666">;</span>

			point2.x <span style="color: #666666">=</span> clip2.x<span style="color: #666666">;</span>
			point2.y <span style="color: #666666">=</span> clip2.y<span style="color: #666666">;</span>
			<span style="color: #008000; font-weight: bold">if</span>(clip1ClipBmpData.hitTest(point1<span style="color: #666666">,</span>
										<span style="color: #666666">255,</span>
										clip2ClipBmpData<span style="color: #666666">,</span>
										point2<span style="color: #666666">,</span>
										<span style="color: #666666">255</span>

								  ))
			{
				<span style="color: #008000; font-weight: bold">return</span> <span style="color: #008000; font-weight: bold">true</span><span style="color: #666666">;</span>
			}

			<span style="color: #008000; font-weight: bold">return</span> <span style="color: #008000; font-weight: bold">false</span><span style="color: #666666">;</span>
		}

		<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">function</span> checkBitmapDataHitInternal()<span style="color: #666666">:</span><span style="color: #008000">Boolean</span>
		{
			<span style="color: #008000; font-weight: bold">var</span> _clip1Rect<span style="color: #666666">:</span><span style="color: #008000">Rectangle</span> <span style="color: #666666">=</span> clip1.getBounds(<span style="color: #008000; font-weight: bold">this</span>);
			<span style="color: #008000; font-weight: bold">var</span> _clip1ClipBmpData<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>(_clip1Rect.width<span style="color: #666666">,</span> _clip1Rect.height<span style="color: #666666">,</span> <span style="color: #008000; font-weight: bold">true</span><span style="color: #666666">,</span> <span style="color: #666666">0</span>);
			_clip1ClipBmpData.draw(clip1);

			<span style="color: #008000; font-weight: bold">var</span> _clip2Rect<span style="color: #666666">:</span><span style="color: #008000">Rectangle</span> <span style="color: #666666">=</span> clip2.getBounds(<span style="color: #008000; font-weight: bold">this</span>);
			<span style="color: #008000; font-weight: bold">var</span> _clip2ClipBmpData<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>(_clip2Rect.width<span style="color: #666666">,</span> _clip2Rect.height<span style="color: #666666">,</span> <span style="color: #008000; font-weight: bold">true</span><span style="color: #666666">,</span> <span style="color: #666666">0</span>);
			_clip2ClipBmpData.draw(clip2);	

			point1.x <span style="color: #666666">=</span> clip1.x<span style="color: #666666">;</span>
			point1.y <span style="color: #666666">=</span> clip1.y<span style="color: #666666">;</span>

			point2.x <span style="color: #666666">=</span> clip2.x<span style="color: #666666">;</span>
			point2.y <span style="color: #666666">=</span> clip2.y<span style="color: #666666">;</span>

			<span style="color: #008000; font-weight: bold">var</span> hits<span style="color: #666666">:</span><span style="color: #008000">Boolean</span> <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">false</span><span style="color: #666666">;</span>
			<span style="color: #008000; font-weight: bold">if</span>(_clip1ClipBmpData.hitTest(point1<span style="color: #666666">,</span>
										<span style="color: #666666">255,</span>
										_clip2ClipBmpData<span style="color: #666666">,</span>
										point2<span style="color: #666666">,</span>
										<span style="color: #666666">255</span>
								  ))
			{
				hits <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">true</span><span style="color: #666666">;</span>
			}

			_clip1ClipBmpData.dispose();
			_clip2ClipBmpData.dispose();

			<span style="color: #008000; font-weight: bold">return</span> hits<span style="color: #666666">;</span>
		}

		<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">function</span> checkBitmapDataHitDynamic()<span style="color: #666666">:</span><span style="color: #008000">Boolean</span>
		{
			<span style="color: #008000; font-weight: bold">var</span> dynamicClip1Rect<span style="color: #666666">:</span><span style="color: #008000">Rectangle</span> <span style="color: #666666">=</span> dynamicClip1.getBounds(<span style="color: #008000; font-weight: bold">this</span>);
			<span style="color: #008000; font-weight: bold">var</span> dynamicClip1ClipBmpData<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>(dynamicClip1Rect.width<span style="color: #666666">,</span>
																	dynamicClip1Rect.height<span style="color: #666666">,</span> <span style="color: #008000; font-weight: bold">true</span><span style="color: #666666">,</span> <span style="color: #666666">0</span>);
			dynamicClip1ClipBmpData.draw(dynamicClip1);

			<span style="color: #008000; font-weight: bold">var</span> dynamicClip2Rect<span style="color: #666666">:</span><span style="color: #008000">Rectangle</span> <span style="color: #666666">=</span> dynamicClip2.getBounds(<span style="color: #008000; font-weight: bold">this</span>);
			<span style="color: #008000; font-weight: bold">var</span> dynamicClip2ClipBmpData<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>(dynamicClip2Rect.width<span style="color: #666666">,</span>
																	dynamicClip2Rect.height<span style="color: #666666">,</span> <span style="color: #008000; font-weight: bold">true</span><span style="color: #666666">,</span> <span style="color: #666666">0</span>);
			dynamicClip2ClipBmpData.draw(dynamicClip2);	

			point1.x <span style="color: #666666">=</span> dynamicClip1.x<span style="color: #666666">;</span>
			point1.y <span style="color: #666666">=</span> dynamicClip1.y<span style="color: #666666">;</span>

			point2.x <span style="color: #666666">=</span> dynamicClip2.x<span style="color: #666666">;</span>
			point2.y <span style="color: #666666">=</span> dynamicClip2.y<span style="color: #666666">;</span>	

			<span style="color: #008000; font-weight: bold">var</span> hits<span style="color: #666666">:</span><span style="color: #008000">Boolean</span> <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">false</span><span style="color: #666666">;</span>
			<span style="color: #008000; font-weight: bold">if</span>(dynamicClip1ClipBmpData.hitTest(point1<span style="color: #666666">,</span>
										<span style="color: #666666">255,</span>
										dynamicClip2ClipBmpData<span style="color: #666666">,</span>
										point2<span style="color: #666666">,</span>
										<span style="color: #666666">255</span>
								  ))
			{
				hits <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">true</span><span style="color: #666666">;</span>
			}

			dynamicClip1ClipBmpData.dispose();
			dynamicClip2ClipBmpData.dispose();

			<span style="color: #008000; font-weight: bold">return</span> hits<span style="color: #666666">;</span>
		}

		<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">function</span> checkBitmapDataHitReverse()<span style="color: #666666">:</span><span style="color: #008000">Boolean</span>
		{
			point1.x <span style="color: #666666">=</span> clip1.x<span style="color: #666666">;</span>
			point1.y <span style="color: #666666">=</span> clip1.y<span style="color: #666666">;</span>

			point2.x <span style="color: #666666">=</span> clip2.x<span style="color: #666666">;</span>
			point2.y <span style="color: #666666">=</span> clip2.y<span style="color: #666666">;</span>	

			<span style="color: #008000; font-weight: bold">if</span>(clip2ClipBmpData.hitTest(point2<span style="color: #666666">,</span>
										<span style="color: #666666">255,</span>
										clip1ClipBmpData<span style="color: #666666">,</span>
										point1<span style="color: #666666">,</span>
										<span style="color: #666666">255</span>

								  ))
			{
				<span style="color: #008000; font-weight: bold">return</span> <span style="color: #008000; font-weight: bold">true</span><span style="color: #666666">;</span>
			}

			<span style="color: #008000; font-weight: bold">return</span> <span style="color: #008000; font-weight: bold">false</span><span style="color: #666666">;</span>
		}

		<span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">function</span> out(str<span style="color: #666666">:*</span>)<span style="color: #666666">:</span>void
		{
			<span style="color: #0000FF">trace</span>(str);
		}
	}
}
</pre>
</div>
<p>&nbsp;</p>
<p>You can download the code from <a href="http://www.mikechambers.com/blog/files/collisiondetection/CollisionDetectionTests.zip">here</a> (requires Flash Pro and <a href="http://www.gskinner.com/blog/archives/2009/04/as3_performance.html">Grant Skinner&#8217;s Performance Testing Harness</a>).</p>
<p>And here are the raw results:</p>
<pre>––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
checkHitTest (10000 iterations)
Uses DisplayObject.hitTest
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
method...................................................ttl ms...avg ms
[function]                                                    8     0.00
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
checkHitTestReverse (10000 iterations)
Uses DisplayObject.hitTest with clips reversed
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
method...................................................ttl ms...avg ms
[function]                                                    7     0.00
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
boundsIntersection (10000 iterations)
Uses Rectangle.intersects()
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
method...................................................ttl ms...avg ms
[function]                                                   33     0.00
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
checkBoundsManually (10000 iterations)
manually checks if bounds intersect.
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
method...................................................ttl ms...avg ms
[function]                                                   18     0.00
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
hitTestCircle (10000 iterations)
Check if bounding circles intersect.
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
method...................................................ttl ms...avg ms
[function]                                                   22     0.00
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
checkBitmapDataHit (10000 iterations)
Uses BitmapData.hitTest to check for collision.
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
method...................................................ttl ms...avg ms
[function]                                                    7     0.00
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
checkBitmapDataHitReverse (10000 iterations)
Uses BitmapData.hitTest to check for collision. Instances reversed.
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
method...................................................ttl ms...avg ms
[function]                                                    7     0.00
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
checkBitmapDataHitInternal (10000 iterations)
Uses BitmapData.hitTest to check for collision. BitmapData not cached.
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
method...................................................ttl ms...avg ms
[function]                                                 6332     0.63
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
checkBitmapDataHitDynamic (10000 iterations)
Uses BitmapData.hitTest to check for collision. BitmapData not cached.
Dynamic MovieClips
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
method...................................................ttl ms...avg ms
[function]                                                 1892     0.19
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––</pre>
<p>&nbsp;</p>
<p>Most of the results are as expected, although there is one surprise. </p>
<p>Doing a BitmapData.hitTest on a MovieClip placed onto the stage at author time (in Flash Pro) is 3 times slower than testing against a MovieClip generated and drawn at runtime. The only thing I can think that is happening is that perhaps for some reason the garbage collector is being triggered in the checkBitmapDataHitInternal and not the checkBitmapDataHitDynamic test. I have asked around internally what might be causing it, but I would be curious if anyone else is seeing the same results.</p>
<p>So, some quick conclusions from this:</p>
<ol>
<li>DisplayObject.hitTest is a fast way to do a quick boundaries check.</li>
<li>Having to dynamically generate BitmapData for BitmapData.hitTest leads to a big performance hit.</li>
<li>If you can cache the BitmapData being compared, then BitmapData.hitTest performance is on par with the other techniques / APIs tested. However, performance degrades as the dimensions of the clips increase (see <a href="http://www.mikechambers.com/blog/2009/06/26/relative-performance-for-collision-detection-techniques-in-actionscript-3/comment-page-1/#comment-16495">here</a>).</li>
<li>Getting the BitmapData from a MovieClip that has cacheAsBitmap = false is 3 times slower than getting it from a MovieClip with the property set to true (Thanks to Renaun Erickson who figured out the performance discrepancy).</li>
</ol>
<p>I will update the post and results as I learn more techniques and get more information.</p>
<p>If you find any bugs with the test, or would like to add some more tests (such as a test using <a href="http://livedocs.adobe.com/flex/3/langref/flash/display/BitmapData.html#getColorBoundsRect()">BitmapData.getColorBoundsRect</a>), just post them in the comments.</p>
<p><strong>Updated : July 6, 2009 : Added info on cacheAsBitmap property impact on performance.</strong> (See this <a href="http://www.mikechambers.com/blog/2009/06/26/relative-performance-for-collision-detection-techniques-in-actionscript-3/#comment-16494">comment</a>).</p>
<p><strong>Updated : November 6, 2009 : Added optimized circle test in<a href="#comment-17507"> comments</a></strong> : Approaches performance of hitTest.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mikechambers.com/blog/2009/06/26/relative-performance-for-collision-detection-techniques-in-actionscript-3/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>What new game APIs do you want in the Flash Player?</title>
		<link>http://www.mikechambers.com/blog/2009/06/16/what-new-game-apis-do-you-want-in-the-flash-player/</link>
		<comments>http://www.mikechambers.com/blog/2009/06/16/what-new-game-apis-do-you-want-in-the-flash-player/#comments</comments>
		<pubDate>Tue, 16 Jun 2009 19:03:57 +0000</pubDate>
		<dc:creator>mikechambers</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[flashplayer]]></category>
		<category><![CDATA[games]]></category>

		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=1741</guid>
		<description><![CDATA[
			
				
			
		
I have been learning some game development lately, and building my first game (well, at least my first game since Flash 4). I think game development and deployment are some of the real strengths of the Flash player, but ones which we haven&#8217;t specifically focused on in a while.
While working on my game, there were [...]]]></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%2F06%2F16%2Fwhat-new-game-apis-do-you-want-in-the-flash-player%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=?url=http%3A%2F%2Fwww.mikechambers.com%2Fblog%2F2009%2F06%2F16%2Fwhat-new-game-apis-do-you-want-in-the-flash-player%2F&amp;source=mesh&amp;style=normal&amp;service=bit.ly" height="61" width="51" /><br />
			</a>
		</div>
<p>I have been learning some game development lately, and building my first game (well, at least my first game since Flash 4). I think game development and deployment are some of the real strengths of the Flash player, but ones which we haven&#8217;t specifically focused on in a while.</p>
<p>While working on my game, there were a couple of things I needed to do where additional player APIs could have made the development easier (as well as likely speeding up execution). This got me to thinking about other APIs that would be useful for game development. So, what new Flash Player APIs would you like to see that would make game development easier?</p>
<p>Here are a couple from me:<br />
<span id="more-1741"></span><br />
Hit test that tests visible bounds of Sprites. Something like:</p>
<div class="highlight">
<pre>sprite.hitTestVisible(anotherSprite)<span style="color: #666666">:</span><span style="color: #008000">Boolean</span>
</pre>
</div>
<p>&nbsp;</p>
<p>Basically, something similar the <a href="http://www.gskinner.com/blog/archives/2005/10/source_code_sha.html">API Grant Skinner created</a>, but native to the player.</p>
<p>The next API I would like is one that would check for hits against multiple sprites, and return a vector of sprites that we colliding.</p>
<div class="highlight">
<pre>sprite.hitTestMultiple(sprites<span style="color: #666666">:</span>Vector.<span style="color: #666666">&lt;</span>Sprite<span style="color: #666666">&gt;</span>)<span style="color: #666666">:</span>Vector.<span style="color: #666666">&lt;</span>Sprite<span style="color: #666666">&gt;;</span>
</pre>
</div>
<p>&nbsp;</p>
<p>This would make it easy to do collision detection for multiple items, and perhaps lead to a performance boost.</p>
<p>So, what APIs would you like to see that would make game development easier. Please be as specific as possible (i.e. don&#8217;t just say &#8220;Physics engine&#8221; or 3D support). Leave you suggestions in the comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mikechambers.com/blog/2009/06/16/what-new-game-apis-do-you-want-in-the-flash-player/feed/</wfw:commentRss>
		<slash:comments>91</slash:comments>
		</item>
		<item>
		<title>Scripting with ActionScript 3 and Flash CS4</title>
		<link>http://www.mikechambers.com/blog/2009/03/09/scripting-with-actionscript-3-and-flash-cs4/</link>
		<comments>http://www.mikechambers.com/blog/2009/03/09/scripting-with-actionscript-3-and-flash-cs4/#comments</comments>
		<pubDate>Mon, 09 Mar 2009 20:41:48 +0000</pubDate>
		<dc:creator>mikechambers</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[as3]]></category>

		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=1686</guid>
		<description><![CDATA[
			
				
			
		
I have just uploaded my slides from my FlashCamp London presentation on migrating from ActionScript 2 to ActionScript 3 using Flash Authoring.


You can view and download the slides from here.
]]></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%2F03%2F09%2Fscripting-with-actionscript-3-and-flash-cs4%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=?url=http%3A%2F%2Fwww.mikechambers.com%2Fblog%2F2009%2F03%2F09%2Fscripting-with-actionscript-3-and-flash-cs4%2F&amp;source=mesh&amp;style=normal&amp;service=bit.ly" height="61" width="51" /><br />
			</a>
		</div>
<p>I have just uploaded my slides from my <a href="http://www.flashcamp.co.uk/">FlashCamp London</a> presentation on <a href="https://share.acrobat.com/adc/document.do?docid=d305d49f-0ebe-4f2e-8f5b-9608fc2171d0">migrating from ActionScript 2 to ActionScript 3 using Flash Authoring</a>.<br />
<span id="more-1686"></span><br />
<a href="http://twitpic.com/1p96k" title="The timeline is not evil! on TwitPic"><img src="http://twitpic.com/show/thumb/1p96k.jpg" width="150" height="150" alt="The timeline is not evil! on TwitPic"></a></p>
<p>You can view and download the slides from <a href="https://share.acrobat.com/adc/document.do?docid=d305d49f-0ebe-4f2e-8f5b-9608fc2171d0">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mikechambers.com/blog/2009/03/09/scripting-with-actionscript-3-and-flash-cs4/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Update to JSON support in as3corelib</title>
		<link>http://www.mikechambers.com/blog/2008/12/23/update-to-json-support-in-as3corelib/</link>
		<comments>http://www.mikechambers.com/blog/2008/12/23/update-to-json-support-in-as3corelib/#comments</comments>
		<pubDate>Tue, 23 Dec 2008 19:55:54 +0000</pubDate>
		<dc:creator>mikechambers</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[as3corelib]]></category>

		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=1639</guid>
		<description><![CDATA[
			
				
			
		
Darron Schall has just checked in some changes and bug fixes to the JSON support in as3corelib. Among other things, Darron has added a strict mode which allows you to specify how strict of a conformance to the JSON spec you want the library to follow.

You can read all of the details over on Darron&#8217;s [...]]]></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%2F12%2F23%2Fupdate-to-json-support-in-as3corelib%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=?url=http%3A%2F%2Fwww.mikechambers.com%2Fblog%2F2008%2F12%2F23%2Fupdate-to-json-support-in-as3corelib%2F&amp;source=mesh&amp;style=normal&amp;service=bit.ly" height="61" width="51" /><br />
			</a>
		</div>
<p><a href="http://www.darronschall.com/weblog/2008/12/as3-json-library-now-a-little-less-strict.cfm">Darron Schall</a> has just checked in some changes and bug fixes to the JSON support in <a href="http://code.google.com/p/as3corelib/">as3corelib</a>. Among other things, Darron has <a href="http://www.darronschall.com/weblog/2008/12/as3-json-library-now-a-little-less-strict.cfm">added a strict mode</a> which allows you to specify how strict of a conformance to the JSON spec you want the library to follow.<br />
<span id="more-1639"></span><br />
You can <a href="http://www.darronschall.com/weblog/2008/12/as3-json-library-now-a-little-less-strict.cfm">read all of the details over on Darron&#8217;s blog</a>. As Darron notes, these are not in the build yet, so if you want to play around with the changes, you have to grab the source.</p>
<p>I will try and do a new build soon with some more JSON changes, as well as some new code contributed from the community.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mikechambers.com/blog/2008/12/23/update-to-json-support-in-as3corelib/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Growl support for Adobe AIR applications</title>
		<link>http://www.mikechambers.com/blog/2008/11/13/growl-support-for-adobe-air-applications/</link>
		<comments>http://www.mikechambers.com/blog/2008/11/13/growl-support-for-adobe-air-applications/#comments</comments>
		<pubDate>Thu, 13 Nov 2008 17:39:53 +0000</pubDate>
		<dc:creator>mikechambers</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[as3growl]]></category>
		<category><![CDATA[growl]]></category>

		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=1604</guid>
		<description><![CDATA[
			
				
			
		
One of the top feature requests that end users have been asking for from AIR applications (on Mac) is the ability for those applications to leverage the open source Growl notification framework on Mac. However, this was not previously possible due to how applications are required to communicate with Growl.
I am really excited to announce [...]]]></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%2F11%2F13%2Fgrowl-support-for-adobe-air-applications%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=?url=http%3A%2F%2Fwww.mikechambers.com%2Fblog%2F2008%2F11%2F13%2Fgrowl-support-for-adobe-air-applications%2F&amp;source=mesh&amp;style=normal&amp;service=bit.ly" height="61" width="51" /><br />
			</a>
		</div>
<p>One of the top feature requests that end users have been asking for from AIR applications (on Mac) is the ability for those applications to leverage the <a href="http://growl.info/">open source Growl notification framework</a> on Mac. However, this was not previously possible due to how applications are required to communicate with Growl.</p>
<p>I am really excited to announce that Adobe has been working with the Growl team to add support to Growl for Adobe AIR and Flash applications. Among other things, this will enable Adobe AIR applications to leverage the Growl notification framework on Mac. The best part, is that it is implemented in a way that is not just limited to use by Flash and AIR.<br />
<span id="more-1604"></span><br />
The new functionality is not yet in the release version of Growl, but if you are a developer you can download a nightly source build of Growl with the support. This will allow you to play around with the functionality and begin to add Growl support to your application.</p>
<p>In addition, I have put together a new open source ActionScript project, named <a href="http://code.google.com/p/as3growl/">as3growl</a>, which provides an API for working with the new Growl functionality. The <a href="http://code.google.com/p/as3growl/">project is hosted at Google code</a> and already contains the complete API source, API docs, examples, documentation, unit tests and other information.</p>
<p>I have posted information on how to grab the correct Growl source and compile it. You can find it in the <a href="http://code.google.com/p/as3growl/wiki/ReleaseNotes">Known Issues section of the as3growl Release Notes</a>.</p>
<p>If you find any issues, or have any feature requests, you can log them <a href="http://code.google.com/p/as3growl/issues/list">here</a>.</p>
<p>You can find more info about Growl on their <a href="http://growl.info/">site</a>.</p>
<p>Update (November 17, 2008) : I created a mailing list for the project <a href="http://groups.google.com/group/as3growl">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mikechambers.com/blog/2008/11/13/growl-support-for-adobe-air-applications/feed/</wfw:commentRss>
		<slash:comments>50</slash:comments>
		</item>
		<item>
		<title>Bash Scripts for working with ActionScript 3 in TextMate</title>
		<link>http://www.mikechambers.com/blog/2008/10/09/bash-scripts-for-working-with-actionscript-3-in-textmate/</link>
		<comments>http://www.mikechambers.com/blog/2008/10/09/bash-scripts-for-working-with-actionscript-3-in-textmate/#comments</comments>
		<pubDate>Thu, 09 Oct 2008 18:41:01 +0000</pubDate>
		<dc:creator>mikechambers</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[textmate]]></category>

		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=1566</guid>
		<description><![CDATA[
			
				
			
		
I have switched over to using TextMate for some of my experimentations with ActionScript. I like how lightweight it is, its extensibility, command completion functionality, and ease of setting up new projects. I find it is perfect for quickly testing new code and ideas.
I have put together a couple of bash scripts, which coupled with [...]]]></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%2F09%2Fbash-scripts-for-working-with-actionscript-3-in-textmate%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=?url=http%3A%2F%2Fwww.mikechambers.com%2Fblog%2F2008%2F10%2F09%2Fbash-scripts-for-working-with-actionscript-3-in-textmate%2F&amp;source=mesh&amp;style=normal&amp;service=bit.ly" height="61" width="51" /><br />
			</a>
		</div>
<p>I have switched over to using <a href="http://macromates.com/">TextMate</a> for some of my experimentations with ActionScript. I like how lightweight it is, its extensibility, command completion functionality, and ease of setting up new projects. I find it is perfect for quickly testing new code and ideas.</p>
<p>I have put together a couple of bash scripts, which coupled with the <a href="http://flashalisious.com/2007/07/30/installing-as3-and-flex-bundle-for-textmate/">ActionScript 3 and Flex TextMate bundles</a> have made working in TextMate a little easier for me.<br />
<span id="more-1566"></span><br />
The first script is called <code>autocompile</code>, which takes a class file, and compiles it anytime the file changes. This is really useful in TextMate as you can see any compile errors as you code.</p>
<p><strong>autocompile</strong></p>
<div class="highlight">
<pre><span style="color: #408080; font-style: italic">#!/bin/bash</span>

<span style="color: #408080; font-style: italic"># mxmlc autocompile bash script</span>
<span style="color: #408080; font-style: italic"># create by mike chambers</span>
<span style="color: #408080; font-style: italic"># http://www.mikechambers.com</span>

<span style="color: #408080; font-style: italic">#how often it check for changes in seconds</span>
<span style="color: #19177C">LOOP_INTERVAL</span><span style="color: #666666">=</span>2

<span style="color: #408080; font-style: italic">#make sure that a file name was passed in</span>
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #666666">[</span> -z <span style="color: #19177C">$1</span> <span style="color: #666666">]</span>;then
        <span style="color: #008000">echo</span> <span style="color: #BA2121">&quot;You must specify a file to compile&quot;</span>
        <span style="color: #008000">exit </span>0
<span style="color: #008000; font-weight: bold">fi</span>

<span style="color: #408080; font-style: italic">#make sure that the file exists</span>
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #666666">[</span> ! -e <span style="color: #19177C">$1</span> <span style="color: #666666">]</span>; <span style="color: #008000; font-weight: bold">then</span>
<span style="color: #008000; font-weight: bold">        </span><span style="color: #008000">echo</span> <span style="color: #BA2121">&quot;$1 does not exist&quot;</span>
        <span style="color: #008000">exit </span>0;
<span style="color: #008000; font-weight: bold">fi</span>

<span style="color: #408080; font-style: italic">#function to call mxmlc</span>
<span style="color: #008000; font-weight: bold">function </span>compile<span style="color: #666666">()</span>
<span style="color: #666666">{</span>
        mxmlc <span style="color: #19177C">$1</span>
<span style="color: #666666">}</span>

<span style="color: #408080; font-style: italic">#compile on load</span>
compile <span style="color: #19177C">$1</span>

<span style="color: #408080; font-style: italic">#get and store the last modified date of the file</span>
<span style="color: #19177C">MODDATE</span><span style="color: #666666">=</span><span style="color: #BA2121">`</span>stat -f %m <span style="color: #19177C">$1</span><span style="color: #BA2121">`</span>

<span style="color: #408080; font-style: italic">#loop every 2 seconds</span>
<span style="color: #008000; font-weight: bold">while</span> <span style="color: #666666">[</span> <span style="color: #008000">true</span> <span style="color: #666666">]</span>
<span style="color: #008000; font-weight: bold">do</span>
        <span style="color: #408080; font-style: italic">#get the modified date</span>
        <span style="color: #19177C">TDATE</span><span style="color: #666666">=</span><span style="color: #BA2121">`</span>stat -f %m <span style="color: #19177C">$1</span><span style="color: #BA2121">`</span>

        <span style="color: #408080; font-style: italic">#check to see if it has changed</span>
        <span style="color: #008000; font-weight: bold">if</span> <span style="color: #666666">[</span> <span style="color: #BA2121">&quot;$TDATE&quot;</span> !<span style="color: #666666">=</span> <span style="color: #BA2121">&quot;$MODDATE&quot;</span> <span style="color: #666666">]</span>; <span style="color: #008000; font-weight: bold">then</span>
                <span style="color: #408080; font-style: italic">#file changed. Store new date</span>
                <span style="color: #19177C">MODDATE</span><span style="color: #666666">=</span><span style="color: #19177C">$TDATE</span>

                <span style="color: #408080; font-style: italic">#compile</span>
                compile <span style="color: #19177C">$1</span>
        <span style="color: #008000; font-weight: bold">fi</span>

        <span style="color: #408080; font-style: italic">#sleep until we check file again</span>
        sleep <span style="color: #19177C">$LOOP_INTERVAL</span>
<span style="color: #008000; font-weight: bold">done</span>
</pre>
</div>
<p>&nbsp;</p>
<p>Using the script is easy, just pass in the name of the file you want it to compile:</p>
<p><code>autocompile Foo.as</code></p>
<p>The next script is called <code>as</code>. Basically, it takes the name of an ActionScript class file, generates the file from a template, opens it within a new project in TextMate, and automatically compiles the file when it is changed. This makes it very simple to setup a new project and start coding.</p>
<div class="highlight">
<pre><span style="color: #408080; font-style: italic">#!/bin/bash</span>

<span style="color: #408080; font-style: italic"># script for creating and setting up ActionScript projects in</span>
<span style="color: #408080; font-style: italic"># TextMate</span>
<span style="color: #408080; font-style: italic"># create by mike chambers</span>
<span style="color: #408080; font-style: italic"># http://www.mikechambers.com</span>

<span style="color: #408080; font-style: italic">#make sure that a file name was passed in</span>
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #666666">[</span> -z <span style="color: #19177C">$1</span> <span style="color: #666666">]</span>;then
        <span style="color: #008000">echo</span> <span style="color: #BA2121">&quot;You must specify a Class file name&quot;</span>
        <span style="color: #008000">exit </span>0
<span style="color: #008000; font-weight: bold">fi</span>

<span style="color: #408080; font-style: italic">#get the class name (remove .as)</span>
<span style="color: #19177C">classname</span><span style="color: #666666">=</span><span style="color: #BA2121">`</span><span style="color: #008000">echo</span> <span style="color: #19177C">$1</span> | cut -d<span style="color: #BA2121">&#39;.&#39;</span> -f1<span style="color: #BA2121">`</span>

<span style="color: #408080; font-style: italic">#dir that contains the as.template file</span>
<span style="color: #408080; font-style: italic">#make sure to set this</span>
<span style="color: #408080; font-style: italic">#there should be a way to get this automatically</span>
<span style="color: #19177C">wdir</span><span style="color: #666666">=</span><span style="color: #BA2121">&quot;/Users/mesh/bin/astmp&quot;</span>

<span style="color: #408080; font-style: italic">#write the contents of the template to the class file.</span>
cat <span style="color: #19177C">$wdir</span>/as.template | sed s/CLASSNAME/<span style="color: #19177C">$classname</span>/ &gt; <span style="color: #19177C">$1</span>

<span style="color: #408080; font-style: italic">#open the current directory and file in textmate</span>
mate .

<span style="color: #408080; font-style: italic">#begin to autocompile it</span>
autocompile <span style="color: #19177C">$1</span>
</pre>
</div>
<p>&nbsp;</p>
<p>This requires that the following <em>as.template</em> file is in the directory referenced in the script (in the <code>wdir</code> variable).</p>
<p><strong>as.template</strong></p>
<div class="highlight">
<pre>package
{
        <span style="color: #008000; font-weight: bold">import</span> flash.display.Sprite<span style="color: #666666">;</span>
        <span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">class</span> CLASSNAME <span style="color: #008000; font-weight: bold">extends</span> Sprite
        {
                <span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">function</span> CLASSNAME()
                {
                }
        }
}
</pre>
</div>
<p>&nbsp;</p>
<p>In order to create the new project, create a new directory, <code>cd</code> to it and run the <code>as</code> command, passing in the name of the ActionScript class file you want it to create.</p>
<pre><code>mkdir Foo
cd Foo
as Foo.as</code></pre>
<p>&nbsp;</p>
<p>This will create a new <em>File.as</em> file that contains the stub code for the <code>Foo</code> class. It will then open the <em>Foo.as</em> file in a new project in TextMate, and will compile the class anytime it changes (printing any compile errors and warnings to the terminal).</p>
<p>Note that I could automate the directory creation also, although I decided not to do that right now.</p>
<p>Couple of notes:</p>
<ul>
<li>You must have the TextMate command line tools installed and in your path (TextMate &gt; Help &gt; Terminal Usage).</li>
<li><code>as</code> and <code>autocompile</code> must be placed within your path.</li>
<li>You must update the <code>wdir</code> variable in the as script to point to the path where the as.template file is stored.</li>
<li>Make sure to set the <code>as</code> and <code>autocompile</code> script to be executable : chmod 755</li>
<li>The Flex SDK must be installed with the <em>bin</em> directory (that contains <code>mxmlc</code>) added to your path.</li>
</ul>
<p>I also have the ActionScript 3 and Flex bundles installed within TextMate. I <a href="http://blog.flexexamples.com/2007/08/26/debugging-flex-applications-with-mmcfg-and-flashlogtxt/">setup my mm.cfg so that it writes trace statements and warnings to the flashlog.txt file</a>, and then I open that file in the Console so I can view runtime debug info (you can also just <code>tail -f</code> the file in terminal). (You can open the console from the ActionScript 3 Bundle in TextMate : Bundles &gt; ActionScript 3 &gt; Debug &gt; Open Flash Log in Console)</p>
<p>Finally, I modify the Run command for the file in TextMate to open the SWF in the standalone debug Flash player (which I have set to be the default handler for the swf file type). In order to do this, I just modified the ActionScript 3 Bundle Run command (CMD-R) to execute:</p>
<div class="highlight">
<pre><span style="color: #19177C">name</span><span style="color: #666666">=</span><span style="color: #BA2121">`</span><span style="color: #008000">echo</span> <span style="color: #19177C">$TM_FILEPATH</span> | cut -d<span style="color: #BA2121">&#39;.&#39;</span> -f1<span style="color: #BA2121">`</span>
open <span style="color: #19177C">$name</span>.swf
</pre>
</div>
<p>&nbsp;</p>
<p>So, now as I type, I can see the errors as I make them, and can just hit CMD-R to test the SWF and see the runtime debug info in the console.</p>
<p>One additional thing I might do is to modify the <code>autocompile</code> script to play a sound when the compile fails.</p>
<p>Post any questions, suggestions or errors in the comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mikechambers.com/blog/2008/10/09/bash-scripts-for-working-with-actionscript-3-in-textmate/feed/</wfw:commentRss>
		<slash:comments>10</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 a [...]]]></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?url=?url=http%3A%2F%2Fwww.mikechambers.com%2Fblog%2F2008%2F09%2F08%2Fencapsulating-custom-pixel-bender-filters-in-actionscript-3%2F&amp;source=mesh&amp;style=normal&amp;service=bit.ly" height="61" width="51" /><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>
	</channel>
</rss>
