<?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>webr3.org &#187; ActionScript</title>
	<atom:link href="http://webr3.org/blog/tag/actionscript/feed/" rel="self" type="application/rss+xml" />
	<link>http://webr3.org/blog</link>
	<description>brain&#039;s on fire!</description>
	<lastBuildDate>Tue, 19 Jul 2011 15:38: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>Why ActionScripts poor performance is a good thing</title>
		<link>http://webr3.org/blog/general/why-actionscripts-poor-performance-is-a-good-thing/</link>
		<comments>http://webr3.org/blog/general/why-actionscripts-poor-performance-is-a-good-thing/#comments</comments>
		<pubDate>Thu, 11 Feb 2010 17:49:22 +0000</pubDate>
		<dc:creator>nathan</dc:creator>
				<category><![CDATA[general]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Adobe Flash]]></category>
		<category><![CDATA[Alchemy]]></category>
		<category><![CDATA[AMF Pensionsforsakring AB]]></category>
		<category><![CDATA[Apparat]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computer science]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[haXe]]></category>
		<category><![CDATA[Literal]]></category>
		<category><![CDATA[player]]></category>
		<category><![CDATA[Software architecture]]></category>
		<category><![CDATA[Software engineering]]></category>
		<category><![CDATA[Software framework]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[Virtual machine]]></category>
		<category><![CDATA[Web Developers]]></category>
		<category><![CDATA[web developers itinerary]]></category>

		<guid isPermaLink="false">http://webr3.org/blog/?p=208</guid>
		<description><![CDATA[ActionScript gives web developers an almost unique chance to grow their skills; out of the common languages in a web developers itinerary ActionScript is almost the only one where a developers code is going to be run 30-100 times per second, with common methods being run literally thousands of times per second.
Further the code isn't [...]]]></description>
			<content:encoded><![CDATA[<p>ActionScript gives web developers an almost unique chance to grow their skills; out of the common languages in a web developers itinerary ActionScript is almost the only one where a developers code is going to be run 30-100 times per second, with common methods being run literally thousands of times per second.</p>
<p>Further the code isn't running on a solid server where the environment is controlled, and where you can simply throw more processors and memory at a problem to make it go away, the code is being run on differently specified machines almost every run, in a different client and in different versions of player.</p>
<p>To compound this unique situation the virtual machines (relatively) poor performance comes in to play.</p>
<h3>Why is this good?</h3>
<p>It has spurred a good percentage of ActionScript developers on to look at more advanced levels of code optimisation, to consider factors which are simply ignored and overlooked in other languages, to work with (and create) other technologies in order to push performance to the max; indeed even to hack the virtual machine to bits then create custom compilers and optimisers.</p>
<p>Libs and frameworks from PV3D through to Flex, projects such as haXe, Apparat and Alchemy; not to mention the wide array of technologies from AMF to RTMFP - this list goes on (and on).</p>
<p>There are 3 types of coders: bad coders, normal coders and ultra coders who's skillsets and level of understanding are far and beyond that of the normal coder; what I'm trying to get at here, is that the ActionScript challenges outlined above create an environment where a higher percentage of coders can grow in to ultra coders, than in any other language I know.</p>
<p>The Flash / ActionScript pair gives both designers and developers the notion that anything is possible - and this spurs them on to the push boundaries of what can be done; that kind of thinking sticks with people, they carry it on to other languages, platforms and communities; bringing inspiration and forward thinking to the web of humans.</p>
<p><em>Context: Really enjoyed reading this post: <a href="http://blog.joa-ebert.com/2010/02/11/the-advantage-of-actionscript/">The Advantage Of ActionScript</a> from Joa, in particular I'm glad that he mentioned "Poor Performance" as an advantage. The above post leads on from Joa's to give some of my own thoughts on why we all owe ActionScript and Flash a little something.</em></p>
<p><img src="http://webr3.org/blog/wp-content/uploads/2010/02/as3poor.jpg" alt="as3poor" title="as3poor" width="600" height="250" class="alignnone size-full wp-image-228" /></p>
]]></content:encoded>
			<wfw:commentRss>http://webr3.org/blog/general/why-actionscripts-poor-performance-is-a-good-thing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Flat Packing, the ultimate code optimization?</title>
		<link>http://webr3.org/blog/optimization/flat-packing-the-ultimate-code-optimization/</link>
		<comments>http://webr3.org/blog/optimization/flat-packing-the-ultimate-code-optimization/#comments</comments>
		<pubDate>Thu, 24 Sep 2009 00:44:50 +0000</pubDate>
		<dc:creator>nathan</dc:creator>
				<category><![CDATA[optimization]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Class]]></category>
		<category><![CDATA[Compiler optimizations]]></category>
		<category><![CDATA[Curly bracket programming languages]]></category>
		<category><![CDATA[haXe]]></category>
		<category><![CDATA[Inline expansion]]></category>
		<category><![CDATA[Java programming language]]></category>

		<guid isPermaLink="false">http://webr3.org/blog/?p=147</guid>
		<description><![CDATA[I've been thinking about something recently, I guess this one's a big thing and hopefully somebody will go and implement this very soon.
A lot of work is always done on the code optimization side of things, and recently this has hit the ActionScript world; most notably (imho) with haxe and TAAS - both of these [...]]]></description>
			<content:encoded><![CDATA[<p>I've been thinking about something recently, I guess this one's a big thing and hopefully somebody will go and implement this very soon.</p>
<p>A lot of work is always done on the code optimization side of things, and recently this has hit the ActionScript world; most notably (imho) with haxe and TAAS - both of these things have a lot of optimizations improving the compile process, which means our code ultimately runs faster :) inlining, code reduction, dead code elimination, flow optimization etc.</p>
<p>Ultimately, the fastest compiled script will always be a flat set of vm operations with none of that expensive method calling and instantiation, and lets remember that OO is pretty much a syntax we use to develop, haxe makes it clear with its common syntax which compiles to multiple vm targets.</p>
<p>So, here's the background question - why do our actionscript classes compile to compiled classes?, all the code from those nested called methods could be factored right the way back in to one big method, and a single class, then heavily optimized - imagine the speed gains.</p>
<p>Thinking further, implementing doesn't appear too hard - i mean all your going to need to do is essentially copy the code from one method and place it in the calling method at the correct position(s); any duplicate code introduced would be offset easily by the amount of code cut out by removing all those un-needed classes and methods - further the unused classes and methods would be lost; and the final chunks of code could be optimized to hell and back.</p>
<p>The only problem i can see is that we need compiled classes to distribute or pull in remotely sometimes, which is why I'd propose a simple "Service" interface (or suchlike) the point of which is that any class marked as a service class (or gateway?) would keep its class structure and public methods.. after all everything behind the public methods is of no concern to any other part of the system.</p>
<p>If you consider a flex application for example, or that class which uses circa 5% of all the large libs included in the final compiled script.. not to mention the additional optimizations that could be made on the big blocks of code in each method!</p>
<p>So that's it, Flat Packing - the ultimate optimization?</p>
<p>ps: quite sure this applies to most OO languages not just actionscript ( haxe guys will agree I'm sure (I hope) ).</p>
<p>Regards!</p>
<p><img src="http://webr3.org/blog/wp-content/uploads/2009/09/flatpack.jpg" alt="flatpack" title="flatpack" width="600" height="250" class="alignnone size-full wp-image-148" /></p>
]]></content:encoded>
			<wfw:commentRss>http://webr3.org/blog/optimization/flat-packing-the-ultimate-code-optimization/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>PHP Events - Actionscript 3 style events in PHP 5</title>
		<link>http://webr3.org/blog/experiments/php-events-actionscript-3-style-events-in-php-5/</link>
		<comments>http://webr3.org/blog/experiments/php-events-actionscript-3-style-events-in-php-5/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 17:33:12 +0000</pubDate>
		<dc:creator>nathan</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[experiments]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Curly bracket programming languages]]></category>
		<category><![CDATA[Event Listener]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[multi site systems]]></category>
		<category><![CDATA[Negation]]></category>
		<category><![CDATA[Object-oriented programming]]></category>
		<category><![CDATA[Scripting languages]]></category>
		<category><![CDATA[Serialization]]></category>

		<guid isPermaLink="false">http://webr3.org/blog/?p=111</guid>
		<description><![CDATA[Here are a set of classes which aim to provide basic actionscript style events in php5;
The Point - Events are useful everywhere even when you don't have a UI, the main reason for building this was to create a kind of "hook" system for php. Often we create multi site systems where each implementation of [...]]]></description>
			<content:encoded><![CDATA[<p>Here are a set of classes which aim to provide basic actionscript style events in php5;</p>
<p><strong>The Point </strong>- Events are useful everywhere even when you don't have a UI, the main reason for building this was to create a kind of "hook" system for php. Often we create multi site systems where each implementation of that system has different client specific considerations; consider a system where one client wants all of there images to go to amazon s3 instead of being saved on the host like your other clients. With this system you can simply:</p>
<ol>
<li>throw an "storeUpload" event which is cancelable</li>
<li>register a default event listener which listens for "storeUpload" events and provides default functionality, saving the upload and setting the location (ur) on the event.target</li>
<li>on one site register a custom event listener which listens for the "storeUpload" event, uploads to amazon s3, then cancels the event and set the event.target to the s3 url instead</li>
</ol>
<p>as you can see, with this you can keep the codebase of your multisite application the same and simply hook in to events whereever you want.</p>
<p>the system consists of 2 public classes:</p>
<h3>Event</h3>
<p>The base class for all events, provides event type and cancelable; with typical usage you create a new event, then dispatch it when ready.</p>
<ul>
<li><strong>__construct</strong>( $type , $cancelable=FALSE )<br />
create a new event listener - if you extend the Event class you must call this with parent::__construct( $type , $cancelable );</p>
<p><strong>$type</strong> - string, event type eg "debugEvent"<br />
<strong>$cancelable</strong> - boolean, provides cancelation functionality, so you can prevent further event listeners from firing thus in effect overriding them.</p>
<p>throws InvalidArgumentException if $type is not a string</li>
<li><strong>cancel</strong>() : void<br />
cancels an event, only if it is cancelable</li>
<li><strong>isCancelable</strong>() : Boolean</li>
<li><strong>isCanceled</strong>() : Boolean</li>
<li><strong>getType</strong>() : string</li>
<li><strong>getTarget</strong>() : obj<br />
target always contains the instance which dispatched the event.</li>
</ul>
<h3>EventDispatcher</h3>
<p>All classes which wish to dispatch events must inherit from EventDispatcher (like in most languages), this class provides:</p>
<ul>
<li><strong>addEventListener</strong>( $type , $callable , $owner ) : void<br />
adds an event listener, this may be a function, a class method or a static class method.<br />
note: a current limitation is that event listener methods must be public</p>
<p><strong>$type</strong> - string, event type eg "debugEvent"<br />
<strong>$callable</strong> - string, the function or method which is to be called when event $type is dispatched<br />
<strong>$owner</strong> - mixed, used when $callable is a method, can be either an object instance, or the string name of a class for static methods.</p>
<p><strong>throws:</strong><br />
InvalidArgumentException when type or callable isn't a string<br />
Exception('unknown method / function')</li>
<li><strong>removeEventListener</strong>( $type , $callable , $owner ) : void<br />
removes an event listener that has been added with addEventListener, this only removes a single event listener each call, so if you have 3 identical event listeners you will need to call this method 3 times to remove them all.</p>
<p>arguments are the same as addEventListener - throws no exceptions, always returns void</li>
<li><strong>hasEventListener</strong>( $type ) : Boolean<br />
checks if any event listeners are registered for event $type; always returns boolean true or false.</li>
<li><strong>dispatchEvent</strong>( Event $event ) : void<br />
the obvious one, dispatches an event and notifies all listeners.</li>
</ul>
<h3>Notes:</h3>
<p>The event dispatcher implements a function which hides all instance properties starting with __ from the serialize process, thus hiding the events and meaning your current serialization process will still work, additionally you can utilize it to hide any properties you don't want to be serialized.</p>
<p>I've tried to make the classes as simple, small and practical as possible, they execute rather quickly and shouldn't make any speed difference to your application - do however be aware that loosely coupling and entire system could allow coding errors to creep in, so maybe don't use it everywhere - but then PHP isn't strict anyways so..!</p>
<h2>Download</h2>
<p>You can download the <a href="http://webr3.org/experiments/php/PHPEvents.zip">PHP Event system</a> here (3 classes + example, 4.2k) - hope somebody finds it useful!</p>
<p><img class="alignnone size-full wp-image-112" title="events" src="http://webr3.org/blog/wp-content/uploads/2009/07/events.jpg" alt="events" width="600" height="250" /></p>
]]></content:encoded>
			<wfw:commentRss>http://webr3.org/blog/experiments/php-events-actionscript-3-style-events-in-php-5/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>PixelBender is more useful than I assumed!</title>
		<link>http://webr3.org/blog/general/pixelbender-is-more-useful-than-i-assumed/</link>
		<comments>http://webr3.org/blog/general/pixelbender-is-more-useful-than-i-assumed/#comments</comments>
		<pubDate>Sat, 18 Jul 2009 11:18:25 +0000</pubDate>
		<dc:creator>nathan</dc:creator>
				<category><![CDATA[Flash 10]]></category>
		<category><![CDATA[PixelBender]]></category>
		<category><![CDATA[general]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[3D computer graphics]]></category>
		<category><![CDATA[Abstract algebra]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Computer graphics]]></category>
		<category><![CDATA[haXe]]></category>
		<category><![CDATA[image processing algorithms]]></category>
		<category><![CDATA[Linear algebra]]></category>
		<category><![CDATA[Perlin Noise]]></category>
		<category><![CDATA[Pixel Bender]]></category>
		<category><![CDATA[Shader]]></category>
		<category><![CDATA[Technology/Internet]]></category>

		<guid isPermaLink="false">http://webr3.org/blog/?p=94</guid>
		<description><![CDATA[All too often it's too easy to overlook a technology, or indeed its uses, by mentaly boxing it in to the role it's advertised to fill.
PixelBender is one of these technologies, as advertised "You can use the Pixel Bender kernel language to implement image processing algorithms (filters or effects) in a hardware-independent manner"; however this [...]]]></description>
			<content:encoded><![CDATA[<p>All too often it's too easy to overlook a technology, or indeed its uses, by mentaly boxing it in to the role it's advertised to fill.</p>
<p>PixelBender is one of these technologies, as advertised "You can use the Pixel Bender kernel language to implement image processing algorithms (filters or effects) in a hardware-independent manner"; however this isn't the whole truth of the matter.</p>
<p>PixelBender is a highly under-rated and under-used technology imho. Simply put PixelBender is actually a very fast way of converting or manipulating large sets of numerical data, whether it's image based or not. In this post I'm only going to skim the surface and give some pointers.</p>
<p>The standard input types cover Vector, ByteArray and BitmapData, likewise with output data, additionally many types are supported for parameters, everything from ints up to 4x4 Matrix' of floats.</p>
<p>Some obvious and simple uses include:</p>
<ul>
<li>3D Data Manipulation (Float3 input and output - rgb or xyz)</li>
<li>Raw Sound Data Manipulation (from Sound.extract, you could even build an FFT implementation in PixelBender)</li>
<li>Visual Effects Calculations</li>
<li>Batch Processing and Data Transformation.</li>
</ul>
<p>Starting with Batch Processing and Data Transformation, here is the simplest PixelBender usage and example you could ever get.</p>
<h3>Problem</h3>
<p>Turn a BitmapData in to a Vector of RGB float values where each value is float &lt; 1 and each set of 3 values comprises a pixel.</p>
<p>Doing this with pure AS3 or haXe or anything else is quite intensive, then you add PixelBender in to the equation</p>
<p><strong>Solution</strong><br />
PixelBender Code:</p>
<pre>&lt;languageVersion : 1.0;&gt;
kernel Identity
&lt;   namespace : "org.webr3.pb";
 vendor : "WebR3";
 version : 1;
 description : "Converts BitmapData to Vector Number (rgb)";
&gt;
{
 input image3 src;
 output pixel3 dst;

 void evaluatePixel()
 {
 dst = sampleNearest( src, outCoord() );
 }
}</pre>
<p>And to use this we can create a simple wrapper class in AS3:</p>
<pre>package test
{
 import __AS3__.vec.Vector;

 import flash.display.BitmapData;
 import flash.display.Shader;
 import flash.display.ShaderJob;
 import flash.utils.ByteArray;

 public class ShaderVector
 {
 [Embed("/rgb.pbj", mimeType="application/octet-stream")]
 private var shaderClass : Class;

 private var shader : Shader;

 public function ShaderVector()
 {        
 shader = new Shader(new shaderClass() as ByteArray);
 }

 public function convert( img : BitmapData ) : Vector.&lt;Number&gt;
 {
 var output : Vector.&lt;Number&gt; = new Vector.&lt;Number&gt;( (img.width*img.height) * 3 , false );
 shader.data.src.input = img;
 var job : ShaderJob = new ShaderJob( shader , output , img.width , img.height );
 job.start( true );
 output.fixed = true;
 return output;
 }

 }
}</pre>
<p>Now we've got a kind of worker / utility class which we can farm all the hard work on to; usage is simple:</p>
<pre>var worker : ShaderVector = new ShaderVector();
var rgb : Vector.&lt;Number&gt; = worker.convert( someBitmapData );</pre>
<p>that's it, in one line we've solved the problem.</p>
<p>PixelBender is a lot easier than you may think, the above code is a small model you can easily manipulate just embed different pixel bender files.</p>
<p>Here's another simple kernel file; this one will take a BitmapData of perlin noise and convert it in to Vector of 3D positions for us to use.</p>
<p>It takes the bitmap data, converts every pixels r,g,b values in to float -0.5 to +0.5 values, then scales them up and finally returns back a vector of what are essentially 3D Perlin Noise Particles (like the ones in my <a href="http://webr3.org/blog/haxe/3d-perlin-particle-light-cloud-and-source-haxe-flash-10/">earlier experiments</a>)</p>
<pre>&lt;languageVersion : 1.0;&gt;
kernel Scaler
&lt;   namespace : "org.webr3.pb";
 vendor : "WebR3";
 version : 1;
 description : "For Perlin Noise, Converts BitmapData to 3D x,y,z positions";
&gt;
{
 input image3 src;
 output pixel3 dst;

 parameter float scaleX
 &lt;
 defaultValue : float(1.0);
 minValue     : float(0.1);
 maxValue     : float(100000.0);
 &gt;;

 parameter float scaleY
 &lt;
 defaultValue : float(1.0);
 minValue     : float(0.1);
 maxValue     : float(100000.0);
 &gt;;

 parameter float scaleZ
 &lt;
 defaultValue : float(1.0);
 minValue     : float(0.000001);
 maxValue     : float(100000.0);
 &gt;;

 void evaluatePixel()
 {
 dst = sampleNearest( src, outCoord() );
 dst -= 0.5;
 dst.r *= scaleX;
 dst.g *= scaleY;
 dst.b *= scaleZ;
 }
}</pre>
<p>and the modified shader vector, only 3 new lines to set up the scale values:</p>
<pre>package test
{
 import __AS3__.vec.Vector;

 import flash.display.BitmapData;
 import flash.display.Shader;
 import flash.display.ShaderJob;
 import flash.utils.ByteArray;

 public class ShaderVector
 {
 [Embed("/rgb.pbj", mimeType="application/octet-stream")]
 private var shaderClass : Class;

 private var shader : Shader;

 public function ShaderVector()
 {        
 shader = new Shader(new shaderClass() as ByteArray);
 shader.data.scaleX.value = [200];
 shader.data.scaleY.value = [200];
 shader.data.scaleZ.value = [200];
 }

 public function convert( img : BitmapData ) : Vector.&lt;Number&gt;
 {
 var output : Vector.&lt;Number&gt; = new Vector.&lt;Number&gt;( (img.width*img.height) * 3 , false );
 shader.data.src.input = img;
 var job : ShaderJob = new ShaderJob( shader , output , img.width , img.height );
 job.start( true );
 output.fixed = true;
 return output;
 }

 }
}</pre>
<p>If you're thinking this is complex, it's honestly not - try coding this in pure AS3 and it'll be a lot of complex slow lines, been there and done that!</p>
<p>I'm going to leave the examples there for now, if your looking for a 3D example then <a href="http://bit.ly/JnQzA" target="_blank">check the PixelBender files in Ralphs 3D Particle example</a>.</p>
<h3>Conclusion.</h3>
<p>AS3 together with PixelBender, haXe and Alchemy is fast becoming an immense tech for us developers, have a play around you may be suprised by the results you can achieve by utilizing these techs.</p>
<p>Coming very soon I'll be posting some examples which show what can be done when you use PixelBender, Fast Memory, Vectors and haXe / AS3 together, but in unconventional ways. Until then I hope this is enough to get some more dev's playing with PixelBender.</p>
<p>peace ::</p>
<p><img class="alignnone size-full wp-image-96" title="pb" src="http://webr3.org/blog/wp-content/uploads/2009/07/pb.jpg" alt="pb" width="600" height="250" /></p>
<p>image is a still from <a href="http://www.apple.com/trailers/wb/iamlegendawakening/" target="_self">I am Legend : Awakening</a> a very well made 5 minute short animation.</p>
]]></content:encoded>
			<wfw:commentRss>http://webr3.org/blog/general/pixelbender-is-more-useful-than-i-assumed/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Auto YouTube &quot;Best Version&quot; Selection with AS3 &amp; Flex</title>
		<link>http://webr3.org/blog/video/auto-youtube-best-version-selection-with-as3-flex/</link>
		<comments>http://webr3.org/blog/video/auto-youtube-best-version-selection-with-as3-flex/#comments</comments>
		<pubDate>Thu, 16 Jul 2009 13:02:15 +0000</pubDate>
		<dc:creator>nathan</dc:creator>
				<category><![CDATA[flex]]></category>
		<category><![CDATA[video]]></category>
		<category><![CDATA[youtube]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Adobe Flash]]></category>
		<category><![CDATA[Entertainment/Culture]]></category>
		<category><![CDATA[High Definition]]></category>
		<category><![CDATA[Technology/Internet]]></category>

		<guid isPermaLink="false">http://webr3.org/blog/?p=88</guid>
		<description><![CDATA[
Here's one from my library, made it a few months ago to auto get the best version of a video from youtube.
Not too much to write on this one, you pass in the url of a youtube video (the normal web url) - it'll first try and load the HD version, then the HQ version [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-90" title="hd" src="http://webr3.org/blog/wp-content/uploads/2009/07/hd.jpg" alt="hd" width="600" height="250" /></p>
<p>Here's one from my library, made it a few months ago to auto get the best version of a video from youtube.</p>
<p>Not too much to write on this one, you pass in the url of a youtube video (the normal web url) - it'll first try and load the HD version, then the HQ version and finally falls back to the "normal" quality version; the class extends mx.controls.VideoDisplay so you have all the normal methods.</p>
<p>Further, it only does all the detection stuff if the url is a youtube video, if it isn't then it loads the video as per usual, so it can be used as a replacement class for VideoDisplay without any negative effects or strange functionality.</p>
<p>no demo, just the source. Uses Flex 3 and ActionScript 3</p>
<p>hope it's of use to somebody!</p>
<p>Source: <a href="http://webr3.org/experiments/flex-classes/CleverVideo.as">CleverVideo.as</a></p>
<p>Usage:</p>
<pre>video : CleverVideo = new CleverVideo( 'http://www.youtube.com/watch?v=_OBlgSz8sSM' );
addChild( video );</pre>
]]></content:encoded>
			<wfw:commentRss>http://webr3.org/blog/video/auto-youtube-best-version-selection-with-as3-flex/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>BitmapData, Vectors, ByteArrays and Optimization</title>
		<link>http://webr3.org/blog/haxe/bitmapdata-vectors-bytearrays-and-optimization/</link>
		<comments>http://webr3.org/blog/haxe/bitmapdata-vectors-bytearrays-and-optimization/#comments</comments>
		<pubDate>Mon, 29 Jun 2009 10:13:31 +0000</pubDate>
		<dc:creator>nathan</dc:creator>
				<category><![CDATA[Flash 10]]></category>
		<category><![CDATA[haXe]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[BitmapData]]></category>
		<category><![CDATA[ByteArray]]></category>
		<category><![CDATA[Flash Optimization]]></category>
		<category><![CDATA[Vector]]></category>

		<guid isPermaLink="false">http://webr3.org/blog/?p=53</guid>
		<description><![CDATA[
In this post I'm looking at the layout of BitmapData and the fastest way to access / manipulate the raw data. The syntax used is haXe, however the information is good for as3/flash10 right up until the last section which deals with using fast memory and is haXe/Alchemy specific.
The Simple Stuff
A BitmapData 5px by 5px [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-58" title="opt" src="http://webr3.org/blog/wp-content/uploads/2009/06/opt.jpg" alt="opt" width="600" height="250" /></p>
<p>In this post I'm looking at the layout of BitmapData and the fastest way to access / manipulate the raw data. The syntax used is haXe, however the information is good for as3/flash10 right up until the last section which deals with using fast memory and is haXe/Alchemy specific.</p>
<h3>The Simple Stuff</h3>
<p>A BitmapData 5px by 5px will comprise of 25 pixels; the flash API is a bit misleading as to the point coordinate system used,<br />
as such here's the layout of the data contained:</p>
<pre>       X
   0,-,-,-,-
   -,1,-,-,-
 Y -,-,2,-,-
   -,-,-,3,-
   -,-,-,-,4</pre>
<p>the upper left corner is point(0,0) - the lower right is point(4,4); [not 1,1 to 5,5 as the manual suggests]</p>
<p>to access the data I'm going to be using three specific methods:</p>
<ul>
<li> BitmapData.getPixel( x, y );</li>
<li> BitmapData.getPixels( rect );</li>
<li> BitmapData.getVector( rect );</li>
</ul>
<p>the data returned by getPixels and getVector is in array format (ByteArray and Vector&lt;UInt&gt; respectively), both are 0 indexed with 25 entries.</p>
<p>Pixel values run from left to right, so the 5px example above converts to</p>
<pre>index[0] = point(0,0)
index[1] = -
index[2] = -
index[3] = -
index[4] = -
index[5] = -
index[6] = point(1,1)
index[7] = -
index[8] = -
index[9] = -
index[10] = -
index[11] = -
index[12] = point(2,2)
index[13] = -
index[14] = -
index[15] = -
index[16] = -
index[17] = -
index[18] = point(3,3)
index[19] = -
index[20] = -
index[21] = -
index[22] = -
index[23] = -
index[24] = point(4,4)</pre>
<h3>Converting between X,Y and Offsets</h3>
<p>to work with the data in its raw format we're going to need some helper functions/code to convert between point(x,y) and offset.</p>
<p><strong>X,Y to Offset</strong></p>
<pre>function pointToOffset( x : Int , y : Int , width : Int ) : Int {
    return (y * width) + x;
}</pre>
<p>thus for point(3,3):</p>
<pre>    (3 x 5) + 3 = 18</pre>
<p><strong>Offset to X,Y</strong></p>
<pre>function offsetToPoint( offset : Int , width : Int ) : Point {
    return new Point( offset%width , Std.int(offset/width) );
}
// or
bitmapData.getPixel( offset%width , Std.int(offset/width) );</pre>
<p>thus for offset 18 :</p>
<pre>    x: 18%5 = 3
    y: 18/5 = 3</pre>
<h2>Speed Tests</h2>
<p>For each of the three methods [ getPixel(x,y); getPixels(rect); getVector(rect); ] I've done a lot of testing to see which is fastest. All tests use a non transparent bitmapData of 1,920,000 pixels (1600x1200) the task of the test itself is to get all pixels from the bitmapData, load them in to an array/bytearray/vector, add 1 to the color for each pixel and then write the new data back to the BitmapData.</p>
<h3>Starting with the Flash 9 methods available:</h3>
<p><strong>Test One, getPixel -&gt; array -&gt; setPixel</strong><br />
This method incurs two full loops because there is no setPixels(array) method, the time of a bare loop of 1920000 is 8ms.</p>
<pre>var o : Array&lt;UInt&gt; = new Array&lt;UInt&gt;();
for( i in 0...1920000 ) {
    o[i] = bitmapData.getPixel( i%WIDTH , Std.int(i/WIDTH) ) + 1;
}
for( i in 0...1920000 ) {
    bitmapData.setPixel( i%WIDTH , Std.int(i/WIDTH) , o[i] );
}</pre>
<p>results [all times in ms]</p>
<pre>555, 569, 574, 573, 575, 570, 570, 570, 568, 576, 575, 573, 569, 563, 570</pre>
<h3>Test Two - getPixels -&gt; ByteArray -&gt; setPixels</h3>
<p>This method needs two ByteArrays to avoid using temporary var's and setting positions multiple times, the time of simply calling getPixels() alone is 90ms!</p>
<pre>var b : ByteArray = bitmapData.getPixels( bitmapData.rect );
var n : ByteArray = new ByteArray();
n.endian = b.endian; // getPixels often returns big endian values and byte arrays are little endian by default
b.position = 0; // other wise we get an error
for( i in 0...1920000 ) {
    n.writeUnsignedInt( b.readUnsignedInt() + 1 );
}
n.position = 0; // otherwise we get an error
bitmapData.setPixels( bitmapData.rect , n );</pre>
<p>results [all times in ms]</p>
<pre>597, 590, 591, 598, 591, 591, 595, 593, 595, 591, 591, 594, 591, 594, 588</pre>
<p>Rather disapointing results really, we'd get a lovely 2fps at that rate (all tests ran on a quad core btw).</p>
<h2>On to the methods available in flash 10, which let's us use Vectors</h2>
<h3>Test Three, getPixel -&gt; Vector -&gt; setVector</h3>
<p>This test is included purely to show the speed difference between Vectors and Arrays (test one)</p>
<pre>var o : Vector&lt;UInt&gt; = new Vector&lt;UInt&gt;( 1920000 , true );
for( i in 0...1920000 ) {
    o[i] = bitmapData.getPixel( i%WIDTH , Std.int(i/WIDTH) ) + 1;
}
for( i in 0...1920000 ) {
    bitmapData.setPixel( i%WIDTH , Std.int(i/WIDTH) , o[i] );
}</pre>
<p>results [all times in ms]</p>
<pre>439, 443, 447, 444, 447, 445, 448, 447, 446, 447, 447, 445, 447, 449, 448</pre>
<h3>Test Four, getVector -&gt; Vector -&gt; setVector (unfixed vector)</h3>
<p>unfixed vector initialization is virtually instant, way too quick for me to measure anyways (or to make a difference)</p>
<pre>var v : Vector&lt;UInt&gt; = bitmapData.getVector( bitmapData.rect );
var o : Vector&lt;UInt&gt; = new Vector&lt;UInt&gt;();
for( i in 0...1920000 ) {
    o[i] = v[i] + 1;
}
bitmapData.setVector( bitmapData.rect , o );</pre>
<p>results [all times in ms]</p>
<pre>96, 66, 80, 71, 75, 65, 74, 72, 76, 68, 77, 73, 72, 77, 68, 70, 72, 72, 81, 68</pre>
<h3>Test Five, getVector -&gt; Vector -&gt; setVector (fixed size vector)</h3>
<p>fixed size vectors take time, in this instance Vector&lt;UInt&gt;(1920000,true) takes circa 5ms</p>
<pre>var v : Vector&lt;UInt&gt; = bitmapData.getVector( bitmapData.rect );
var o : Vector&lt;UInt&gt; = new Vector&lt;UInt&gt;( 1920000 , true );
for( i in 0...1920000 ) {
    o[i] = v[i] + 1;
}
bitmapData.setVector( bitmapData.rect , o );</pre>
<p>results [all times in ms]</p>
<pre>53, 41, 41, 46, 45, 47, 41, 40, 45, 45, 46, 41, 40, 46, 45, 47, 41, 50, 42, 46</pre>
<h3>Test Six, getVector -&gt; setVector</h3>
<p>this time we cut out the second vector, as essentially it's not needed</p>
<pre>var v : Vector&lt;UInt&gt; = bitmapData.getVector( bitmapData.rect );
for( i in 0...1920000 ) {
    v[i] = v[i] + 1;
}
bitmapData.setVector( bitmapData.rect , v );</pre>
<p>results [all times in ms]</p>
<pre>44, 36, 38, 36, 38, 36, 36, 38, 36, 36, 37, 39, 35, 38, 38, 36, 39, 36, 38, 38</pre>
<p>Some fantastic speed gains, and since we're getting to smaller figures we need to start counting the cost of each line:</p>
<pre>bitmapData.getVector( bitmapData.rect );</pre>
<p>10, 8, 9, 9, 8, 10, 8, 9, 8, 11, 8, 10, 8, 10, 8, 9, 8, 9, 8 [ms]</p>
<pre>for( i in 0...1920000 ) {
    v[i] = v[i] + 1;
}</pre>
<p>25, 25, 25, 27, 27, 26, 25, 26, 25, 25, 26, 25, 27, 25, 25, 26, 25, 26, 25, 25</p>
<p>however the loop alone takes 8ms so we're looking at av 19ms for this part</p>
<pre>bitmapData.setVector( bitmapData.rect , v );</pre>
<p>11, 5, 6, 4, 4, 6, 4, 6, 4, 4, 5, 5, 6, 4, 6, 4, 5, 5, 4, 5</p>
<p>and finally to be really count the cost we need to see what the uint+1 would take by itself</p>
<pre>for( i in 0...1920000 ) {
    0xFFFFF9 + 1;
}</pre>
<p>9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 8, 9, 9</p>
<p>so the true cost of using this single vector method in test six is 37-9 = 28ms for getVector, update vector, setVector</p>
<p>the true cost of using a vector inside the loop is 26-9 = 17ms (we need this later)</p>
<h2>Adding haXe in to the equation.</h2>
<p>Finally, as you probably know haXe provides access to fast memory; fast memory is essentially a ByteArray set in flash.system.ApplicationDomain.currentDomain.domainMemory and accessed through the new opcodes.</p>
<p>A little trick we can use with haxe is to write info from this fast memory to BitmapData via setPixels() since it is a ByteArray.</p>
<p>First off we need to set up the fast memory storage somewhere in our app:</p>
<pre>var storage : ByteArray = new ByteArray();
storage.endian = Endian.LITTLE_ENDIAN;
storage.length = BYTES;
flash.Memory.select( storage );</pre>
<p>to calulate the value of BYTES we simply do WIDTH*HEIGHT*4 (because a UInt takes up 4 Bytes of memory)</p>
<p>we know from earlier that getVector only takes circa 9ms, whereas getPixels takes a shocking 90ms; so what I'm going to do is:</p>
<h3>Test Seven : getVector -&gt; flash.Memory -&gt; setPixels</h3>
<pre>var v : Vector&lt;UInt&gt; = bitmapData.getVector( bitmapData.rect );
for( i in 0...1920000 ) {
    flash.Memory.setI32( i*4 , v[i] + 1 );
}
// ensure position of fast memory is 0
flash.system.ApplicationDomain.currentDomain.domainMemory.position = 0;
bitmapData.lock();
bitmapData.setPixels( bitmapData.rect , flash.system.ApplicationDomain.currentDomain.domainMemory );
bitmapData.unlock( bitmapData.rect );</pre>
<p>results [all times in ms]<br />
34, 31, 32, 34, 32, 32, 32, 33, 32, 33, 31, 33, 32, 31, 33, 32, 33, 32, 33, 31,</p>
<p>We've shaved off 5ms! but hold on.. there's more - we need to analyse this</p>
<p>bitmapData.getVector( bitmapData.rect ); as we know from earlier takes 9ms</p>
<p>the loop takes 9ms</p>
<pre>flash.system.ApplicationDomain.currentDomain.domainMemory.position = 0;
bitmapData.lock();
bitmapData.setPixels( bitmapData.rect , flash.system.ApplicationDomain.currentDomain.domainMemory );
bitmapData.unlock( bitmapData.rect );</pre>
<p>together all takes 5ms</p>
<p>which means that the true cost of using flash.Memory is 32-9-9-5 = 9ms</p>
<p>if you remember from test six, the vector cost was 17ms so we've gained some and lost some, but overall came out on top, and certainly a tonne better than flash 9!</p>
<p>now, these tests where all with a big chunk of 1600x1200 pixels, in a real world app we'd have less.. so lets see with 800x450px (360,000 pixels)</p>
<h3>Vector Only:</h3>
<pre>var v : Vector&lt;UInt&gt; = bitmapData.getVector( bitmapData.rect );
for( i in 0...360000 ) {
    v[i] = v[i] + 1;
}
bitmapData.setVector( bitmapData.rect , v );</pre>
<p>results [all times in ms]<br />
13, 6, 6, 6, 6, 7, 6, 7, 6, 6, 7, 6, 6, 7, 7, 7, 7, 7, 7, 7  = 137 / 20 : av 6.85ms = 146fps</p>
<h3>Vector + Memory</h3>
<pre>var v : Vector&lt;UInt&gt; = bitmapData.getVector( bitmapData.rect );
for( i in 0...360000 ) {
flash.Memory.setI32( i*4 , v[i] + 1 );
}

// ensure position of fast memory is 0
flash.system.ApplicationDomain.currentDomain.domainMemory.position = 0;
bitmapData.lock();
bitmapData.setPixels( bitmapData.rect , flash.system.ApplicationDomain.currentDomain.domainMemory );
bitmapData.unlock( bitmapData.rect );</pre>
<p>results [all times in ms]<br />
7, 6, 6, 6, 5, 6, 6, 5, 5, 5, 6, 5, 5, 5, 6, 5, 5, 5, 5, 5 = 109 / 20 : av 5.45ms = 183fps</p>
<h2>Summary</h2>
<p>so, there's your choices 800x450px swf using methods to update raw bitmap data at 3fps for flash 9 vs <strong>146fps</strong> for pure as3 vs <strong>183 fps</strong> for haXe (and probably alchemy).</p>
<p><strong>imho:</strong> vectors are a fantastic choice, my personal preference goes to using fastmemory, but thats because i can avoid the stack using it and know that whilst its a little more coding, you can't get faster.</p>
<p><strong>overall:</strong> I'm just grateful we've got options to be able to deliver this kind of performance, really opens the doors for some stunning work and libraries not least the upcoming version of papervision and no doublt some libs for haXe.</p>
]]></content:encoded>
			<wfw:commentRss>http://webr3.org/blog/haxe/bitmapdata-vectors-bytearrays-and-optimization/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Flash 10, Massive amounts of 3D Particles (with haXe)</title>
		<link>http://webr3.org/blog/haxe/flash-10-massive-amounts-of-3d-particles-with-haxe/</link>
		<comments>http://webr3.org/blog/haxe/flash-10-massive-amounts-of-3d-particles-with-haxe/#comments</comments>
		<pubDate>Mon, 22 Jun 2009 20:13:42 +0000</pubDate>
		<dc:creator>nathan</dc:creator>
				<category><![CDATA[Flash 10]]></category>
		<category><![CDATA[haXe]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Adobe Flash]]></category>
		<category><![CDATA[Curly bracket programming languages]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Ralph Hauwert]]></category>

		<guid isPermaLink="false">http://webr3.org/blog/?p=16</guid>
		<description><![CDATA[A couple of months ago I was playing a round with strange attractors in flash, using AS3 and flex, then a post caught my attention.
Ralph Hauwert somehow managed to push 300k+ 3D particles around in realtime using alchemy, pixelbender and flash 10; I played around with the code a bit and made similar in pure [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of months ago I was playing a round with strange attractors in flash, using AS3 and flex, then a post caught my attention.</p>
<p><a href="http://www.unitzeroone.com/blog/2009/03/18/flash-10-massive-amounts-of-3d-particles-with-alchemy-source-included/" target="_blank">Ralph Hauwert</a> somehow managed to <a href="http://www.unitzeroone.com/labs/alchemyPushingPixels/" target="_blank">push 300k+ 3D particles around in realtime using alchemy, pixelbender and flash 10</a>; I played around with the code a bit and made similar in pure as3, nothing magnificent.. a month later in a totally unrelated event (namely me getting thoroughly p'd off with PHP) I discovered <a href="http://haxe.org/" target="_blank">haXe</a> and set to work playing around with it.</p>
<p>Finally in another totally unrelated event I came across <a href="http://blog.joa-ebert.com/2009/04/03/massive-amounts-of-3d-particles-without-alchemy-and-pixelbender/">Joa Eberts AS3 only version</a> of Ralphs earlier work; upon closer inspection I found it wasn't as fast as Ralph's alchemy version but completely awesome code non-the-less, to do this in pure AS3 is simply amazing.</p>
<p>As if you can't guess where this is going.. I thought I'd give it a go in "pure haXe" to see if one could replicate the speed of Ralphs version or maybe even top it. Sparing any further detail..</p>
<p>Here's <a href="http://webr3.org/experiments/haxe-particle-pusher/300k/">haXe pushing 300k+ 3d particles around in realtime</a> (source included)</p>
<p>and...</p>
<p>here's haXe pushing <a href="http://webr3.org/experiments/haxe-particle-pusher/600k/">600k particles</a> (twice as many), and erm <a href="http://webr3.org/experiments/haxe-particle-pusher/OneMillion/">1 Million</a> (a bit stuttery but hell why not)</p>
<p>and.. (just for fun)</p>
<p>here's haXe pushing <a href="http://webr3.org/experiments/haxe-particle-pusher/300k-Color/">300k particles in color</a>, and <a href="http://webr3.org/experiments/haxe-particle-pusher/500k-Color/">500k in color</a> for good measure! - before downplaying the color versions, it's worth noting that each r,g,b value is independantly calculated and weighted as a float then combined to a render color right before updating the bitmap, so in effect it's not just x,y,z stored for each pixel, it's r,g,b as well - so 2x more number crunching + a bit more.</p>
<p>think I can safely say I love haXe and since doign this I've been going nuts doing <a href="http://webr3.org/experiments/" target="_self">lots of experiments</a> using similar code :p</p>
<p>so, any doublts about haXe's perfomance with flash can be considered resolved imho.</p>
<p>regards, nathan</p>
<p><img class="alignleft size-full wp-image-17" title="300k-lol" src="http://webr3.org/blog/wp-content/uploads/2009/06/300k-lol.jpg" alt="300k-lol" width="600" height="250" /></p>
]]></content:encoded>
			<wfw:commentRss>http://webr3.org/blog/haxe/flash-10-massive-amounts-of-3d-particles-with-haxe/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>

