<?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>GhostWire Studios &#187; Flash AS3</title>
	<atom:link href="http://www.ghostwire.com/blog/archives/category/flash-as3/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ghostwire.com/blog</link>
	<description>Flash UI Components</description>
	<lastBuildDate>Tue, 23 Aug 2011 03:51:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>[AS3] Referencing Embedded Assets Of Another Class Using Underscore</title>
		<link>http://www.ghostwire.com/blog/archives/as3-referencing-embedded-assets-of-another-class-using-underscore/</link>
		<comments>http://www.ghostwire.com/blog/archives/as3-referencing-embedded-assets-of-another-class-using-underscore/#comments</comments>
		<pubDate>Tue, 16 Aug 2011 10:18:10 +0000</pubDate>
		<dc:creator>sunny</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flash AS3]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Bitmap]]></category>
		<category><![CDATA[BitmapData]]></category>

		<guid isPermaLink="false">http://www.ghostwire.com/blog/?p=1303</guid>
		<description><![CDATA[This is rather unorthodox but it works. In Flash, when you embed an image into the SWF using a static class member of a class, a new class with the name of CLASSNAME_MEMBERNAME is actually created internally. Using flash.utils.getDefinitionByName(), you can access this class, regardless of whether the static member was declared as public or [...]]]></description>
			<content:encoded><![CDATA[<p>This is rather unorthodox but it works.  In Flash, when you embed an image into the SWF using a static class member of a class, a new class with the name of <code>CLASSNAME_MEMBERNAME</code> is actually created internally.  Using flash.utils.getDefinitionByName(), you can access this class, regardless of whether the static member was declared as <code>public</code> or <code>private</code>. This makes it possible to make a very simple embedded image assets manager.</p>
<p><span id="more-1303"></span></p>
<p>Consider the following:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #66cc66;">&#91;</span>Embed<span style="color: #66cc66;">&#40;</span>source=<span style="color: #ff0000;">&quot;assets/background.png&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> const BACKGROUNDIMAGE:<span style="color: #000000; font-weight: bold;">Class</span>;</pre></div></div>

<p>Let&#8217;s say that is done within a class named AssetsManager.  Then, internally, we actually have a class named &#8220;AssetsManager_BACKGROUNDIMAGE&#8221; created.  The name that comes after the underscore matches whatever you have given to that static member.</p>
<p>We can then write a generic handler function to grab the associated Bitmap image, given the asset name:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> GetImage<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">name</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Bitmap
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">name</span> = <span style="color: #0066CC;">name</span>.<span style="color: #0066CC;">toUpperCase</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #0066CC;">try</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">var</span> cls:<span style="color: #000000; font-weight: bold;">Class</span> = getDefinitionByName<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;AssetsManager_&quot;</span> + <span style="color: #0066CC;">name</span><span style="color: #66cc66;">&#41;</span> as <span style="color: #000000; font-weight: bold;">Class</span>;
		<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> cls<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> as Bitmap;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #0066CC;">catch</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:<span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #808080; font-style: italic;">// ** else return a default 16x16 opaque white square bitmap **</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Bitmap<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> BitmapData<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">16</span>,<span style="color: #cc66cc;">16</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p><strong>Line 3:</strong> This is optional, but it is a simple way to eliminate possible naming confusion.</p>
<p><strong>Line 4:</strong> Use a try-catch block, because getDefinitionByName() expects the class to exist, but it is possible that during development we have not embedded the assets yet, and this method is crafted to return a default 16&#215;16 opaque white square where the asset is missing.</p>
<p><strong>Line 6:</strong> Here it is, concatenating the helper class name, an underscore, and the given <code>name</code> parameter, we get the class name of the embedded bitmap asset.</p>
<p><strong>Line 7:</strong> If the class/asset exists, this line will be executed (instantiating and returning the requested Bitmap object), otherwise the try-catch block kicks in.</p>
<p>Example complete class code below:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package  
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Bitmap</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">BitmapData</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">utils</span>.<span style="color: #006600;">getDefinitionByName</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> AssetsManager 
	<span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// ** START EMBED **</span>
&nbsp;
		<span style="color: #66cc66;">&#91;</span>Embed<span style="color: #66cc66;">&#40;</span>source=<span style="color: #ff0000;">&quot;assets/background.png&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> const BACKGROUNDIMAGE:<span style="color: #000000; font-weight: bold;">Class</span>;
&nbsp;
		<span style="color: #66cc66;">&#91;</span>Embed<span style="color: #66cc66;">&#40;</span>source=<span style="color: #ff0000;">&quot;assets/iconfileopen.png&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> const ICONFILEOPEN:<span style="color: #000000; font-weight: bold;">Class</span>;
&nbsp;
		<span style="color: #66cc66;">&#91;</span>Embed<span style="color: #66cc66;">&#40;</span>source=<span style="color: #ff0000;">&quot;assets/iconfileclose.png&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> const ICONFILECLOSE:<span style="color: #000000; font-weight: bold;">Class</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">// ** embed other assets as required **</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// ** END EMBED **</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * Returns a Bitmap display object identified by the specified name.
		 * @param	The name of the bitmap asset to return.  This name must match one of the 
		 * @return	A Bitmap display object.
		 */</span>
		<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> GetImage<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">name</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Bitmap
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">name</span> = <span style="color: #0066CC;">name</span>.<span style="color: #0066CC;">toUpperCase</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">try</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">var</span> cls:<span style="color: #000000; font-weight: bold;">Class</span> = getDefinitionByName<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;AssetsManager_&quot;</span> + <span style="color: #0066CC;">name</span><span style="color: #66cc66;">&#41;</span> as <span style="color: #000000; font-weight: bold;">Class</span>;
				<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> cls<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> as Bitmap;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #0066CC;">catch</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:<span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #808080; font-style: italic;">// ** else return a default 16x16 opaque white square bitmap **</span>
			<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Bitmap<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> BitmapData<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">16</span>,<span style="color: #cc66cc;">16</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The above makes it easy to add more assets later on, compared to just accessing the public static members directly.  What is more, if you are compiling a library of embedded images and distributing it as a SWC, this makes it so much easier to modify/update the library, and for other collaborators to use the assets.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ghostwire.com/blog/archives/as3-referencing-embedded-assets-of-another-class-using-underscore/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>FlashDevelop, Flex SDK, Windows 7, Missing DLL</title>
		<link>http://www.ghostwire.com/blog/archives/flashdevelop-flex-sdk-windows-7-missing-dll/</link>
		<comments>http://www.ghostwire.com/blog/archives/flashdevelop-flex-sdk-windows-7-missing-dll/#comments</comments>
		<pubDate>Wed, 09 Feb 2011 06:58:47 +0000</pubDate>
		<dc:creator>sunny</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flash AS3]]></category>
		<category><![CDATA[Others]]></category>
		<category><![CDATA[FlashDevelop]]></category>
		<category><![CDATA[Flex]]></category>

		<guid isPermaLink="false">http://www.ghostwire.com/blog/?p=1902</guid>
		<description><![CDATA[This post is for my own future reference because I have forgotten about this problem until a recent reinstall of Windows 7. After a clean install of Windows 7, trying to use compc and/or mxmlc from the Flex SDK through command line may yield a &#8220;System Error&#8221; message: It seems like Microsoft somehow forgot to [...]]]></description>
			<content:encoded><![CDATA[<p>This post is for my own future reference because I have forgotten about this problem until a recent reinstall of Windows 7.</p>
<p>After a clean install of Windows 7, trying to use compc and/or mxmlc from the Flex SDK through command line may yield a &#8220;System Error&#8221; message:</p>
<p><img src="http://www.ghostwire.com/blog/wp-content/uploads/missingdll_compc.png" alt="" title="missingdll_compc" width="473" height="171" class="alignleft size-full wp-image-1903" /></p>
<p><img src="http://www.ghostwire.com/blog/wp-content/uploads/missingdll_mxmlc.png" alt="" title="missingdll_mxmlc" width="473" height="171" class="alignleft size-full wp-image-1904" /></p>
<p>It seems like Microsoft somehow forgot to ship this Microsoft C Runtime Library DLL in Windows 7.  When installing some programs, this DLL may be installed by the respective installers.  If you encounter this missing DLL problem, the only way to fix it is to install one of these programs, or otherwise grab hold of the DLL from <a href="http://www.google.com.sg/search?q=msvcr71.dll">somewhere</a> and place it in the correct folder:</p>
<p>For Windows 7 32-bit, place the DLL in<br />
C:\Windows\System32</p>
<p>For Windows 7 64-bit, place it in<br />
C:\Windows\SysWOW64</p>
<p>WARNING: Do not install DLL obtained from untrusted sources.  You can actually copy the DLL from one of your old Windows OS installations.  If you want to use my copy, <a href="http://db.tt/OlUX7U3">here</a> it is.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ghostwire.com/blog/archives/flashdevelop-flex-sdk-windows-7-missing-dll/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[Game Programming] Consider A Different Approach To Cards Shuffling</title>
		<link>http://www.ghostwire.com/blog/archives/game-programming-consider-a-different-approach-to-cards-shuffling/</link>
		<comments>http://www.ghostwire.com/blog/archives/game-programming-consider-a-different-approach-to-cards-shuffling/#comments</comments>
		<pubDate>Sat, 21 Aug 2010 11:54:32 +0000</pubDate>
		<dc:creator>sunny</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flash AS3]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Game Programming]]></category>

		<guid isPermaLink="false">http://www.ghostwire.com/blog/?p=1864</guid>
		<description><![CDATA[Very commonly, computer programs (usually games) that have the need to simulate a shuffling of a deck of cards (or drawing a tile from a bag, etc.) will go the obvious way of putting the items in an array, and then randomize the order of the elements of the array. A common algorithm for doing [...]]]></description>
			<content:encoded><![CDATA[<p>Very commonly, computer programs (usually games) that have the need to simulate a shuffling of a deck of cards (or drawing a tile from a bag, etc.) will go the obvious way of putting the items in an array, and then randomize the order of the elements of the array.  A common algorithm for doing that is the<a href="http://en.wikipedia.org/wiki/Knuth_shuffle"> Fisher–Yates</a> shuffle (you can find a suitable implementation easily by googling).</p>
<p>The purpose of this post, however, is not to discuss &#8220;how&#8221; to shuffle arrays.  Rather, I would like to bring up the question of &#8220;why&#8221; (do it) in the first place.</p>
<p><span id="more-1864"></span>In the non-digital world, the shuffling of a deck of cards is mostly necessary given the circumstances &#8211; ie, for convenience.  In a card game, you shuffle the deck of cards to randomize the order of the cards, and players draw from the top (or bottom) of the deck in sequence.  Why do players draw from the top (or bottom) of the deck?  For practical reasons, of course.  Convenience.</p>
<p>Imagine a card game where the game designer decides that players must pick a card randomly from the deck each time they need to draw a card &#8211; the game will end up taking too long to play, drawing cards become tedious, and the game won&#8217;t be very popular.</p>
<p>In both methods of card drawing, the outcome is really the same &#8211; whether you (i) shuffle a deck of cards and then draw from the top of the deck sequentially, or (ii) leave a deck as it is and then randomly pick a card from the deck each time you need to draw a card, the probability of a particular card being drawn by a player is exactly the same.  Let&#8217;s take a look at a deck of 52 cards.  If you shuffle the deck rigorously, the chance of a card being positioned at the location it is shuffled into, and therefore being drawn at that particular position, is 1.92% (1/52).</p>
<p>Now, look at the other scenario.  Leave the deck as it is without shuffling.  When a player randomly picks a card from the deck, the probability of picking a particular card as the first card remains at 1.92% (1/52). What is the probability of a particular card to be drawn as the second card?  A common misconception is that with less cards left, the probability for subsequent cards are now skewed.  Not really.  Let&#8217;s look at the calculations.  First, for a card to be drawn as the second card, it must not be drawn as the first card &#8211; got that?  The chance of that happening is 98.08% (51/52).  Next, the chance of the card being drawn as the second card in the reduced deck of 51 cards is 1.96% (1/51).  The composite probability is&#8230; 51/52 x 1/51 = 1/52, which is still 1.92%!  You can go on to compute for the third card, which is 51/52 x 50/51 x 1/50 = 1/52, and so on.</p>
<p>Okay, we have established that the probability remains the same for both methods of card drawing.  So what?</p>
<p>Well, when it comes to programming, I would debate that it is unnecessary to shuffle a deck of cards.  Players no longer need the convenience here, so why not pick a card at random each time you need to draw a card?  This approach has a couple of advantages:</p>
<p>(i) Preempt cheating by memory peeking.  If you use a shuffled deck, you naturally store the sequence in memory, which means hackers can potentially look at the sequence.  If you generate a random number on demand, picking a card at random each time a card is to be drawn, nobody can have fore-knowledge of what is to come.</p>
<p>(ii) Fast.  You can jump right into a game session without expensive CPU overhead for shuffling arrays.  If you have a large deck, shuffling can be inefficient no matter which algorithm you use.  Instead of shuffling the deck, you just write the code to pick a card randomly from the remaining deck.  Hassle in the non-digital world, fast in the digital version.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// ActionScript 3.0</span>
<span style="color: #000000; font-weight: bold;">function</span> drawNext<span style="color: #66cc66;">&#40;</span>deck:<span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #66cc66;">*</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> j:uint = deck.<span style="color: #0066CC;">length</span>;
	<span style="color: #000000; font-weight: bold;">var</span> r:uint = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">random</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> j;
	<span style="color: #000000; font-weight: bold;">var</span> c:<span style="color: #66cc66;">*</span> = deck<span style="color: #66cc66;">&#91;</span>r<span style="color: #66cc66;">&#93;</span>;
	deck.<span style="color: #0066CC;">splice</span><span style="color: #66cc66;">&#40;</span>r, <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// removes item from array</span>
	<span style="color: #b1b100;">return</span> c;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Therefore, when translating a non-digital game into a digital one, take a step back and ponder over whether it is really necessary to implement the rules literally.  As long as the intended outcome (in this case the intended probability of each card being drawn at a certain position) is the same, it would probably be worthwhile exploring more efficient implementations.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ghostwire.com/blog/archives/game-programming-consider-a-different-approach-to-cards-shuffling/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>FlashDevelop: Getting trace() Statements To Show In Output Panel</title>
		<link>http://www.ghostwire.com/blog/archives/flashdevelop-getting-trace-statements-to-show-in-output-panel/</link>
		<comments>http://www.ghostwire.com/blog/archives/flashdevelop-getting-trace-statements-to-show-in-output-panel/#comments</comments>
		<pubDate>Thu, 19 Aug 2010 08:44:10 +0000</pubDate>
		<dc:creator>sunny</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flash AS3]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[FlashDevelop]]></category>

		<guid isPermaLink="false">http://www.ghostwire.com/blog/?p=1860</guid>
		<description><![CDATA[This is quite an obscure, little known, hidden-by-default feature, but the FlashDevelop IDE actually has a built-in Flash tracer. The plug-in is unfortunately not enabled by default, and new users of FlashDevelop often get frustrated wondering where to look for the output of their trace statements. To get the tracer to work: 1) Go to [...]]]></description>
			<content:encoded><![CDATA[<p>This is quite an obscure, little known, hidden-by-default feature, but the <a href="http://www.flashdevelop.org">FlashDevelop</a> IDE actually has a built-in Flash tracer.  The plug-in is unfortunately not enabled by default, and new users of FlashDevelop often get frustrated wondering where to look for the output of their trace statements.</p>
<p>To get the tracer to work:</p>
<p>1) Go to Tools -> Program Settings (F10).</p>
<p>2) In the Plugins list on the left of the dialog box, look for FlashLogViewer and click on it.</p>
<p>3) On the right panel, look for the &#8220;Start Tracking&#8221; property, click on the drop down box and select &#8220;OnBuildComplete&#8221; (it is set to &#8220;Manually&#8221; by default).</p>
<p><img src="http://www.ghostwire.com/blog/wp-content/uploads/flashlogviewer.png" alt="" title="flashlogviewer" width="621" height="489" class="alignleft size-full wp-image-1861" /></p>
<p>The output of your trace statements are now shown in the Output Panel after you build your project.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ghostwire.com/blog/archives/flashdevelop-getting-trace-statements-to-show-in-output-panel/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>[AS3] Serializing A Bundle Of Bitmaps As Data Objects</title>
		<link>http://www.ghostwire.com/blog/archives/as3-serializing-a-bundle-of-bitmaps-as-data-objects/</link>
		<comments>http://www.ghostwire.com/blog/archives/as3-serializing-a-bundle-of-bitmaps-as-data-objects/#comments</comments>
		<pubDate>Sun, 13 Dec 2009 14:31:19 +0000</pubDate>
		<dc:creator>sunny</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flash AS3]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Bitmap]]></category>
		<category><![CDATA[BitmapData]]></category>
		<category><![CDATA[ByteArray]]></category>

		<guid isPermaLink="false">http://www.ghostwire.com/blog/?p=1601</guid>
		<description><![CDATA[This post is a supplement to &#8220;Serializing Bitmaps (Storing BitmapData As Raw Binary/ByteArray)&#8221;. In that article, we looked at how to convert BitmapData to a ByteArray, save that ByteArray, and re-construct the BitmapData from the saved ByteArray. It is important to note that the technique saves the ByteArray &#8220;as is&#8221; in a flat binary file [...]]]></description>
			<content:encoded><![CDATA[<p>This post is a supplement to <a href="http://www.ghostwire.com/blog/archives/as3-serializing-bitmaps-storing-bitmapdata-as-raw-binarybytearray/">&#8220;Serializing Bitmaps (Storing BitmapData As Raw Binary/ByteArray)&#8221;</a>. In that article, we looked at how to convert <code>BitmapData</code> to a <code>ByteArray</code>, save that <code>ByteArray</code>, and re-construct the <code>BitmapData</code> from the saved <code>ByteArray</code>.</p>
<p>It is important to note that the technique saves the <code>ByteArray</code> &#8220;as is&#8221; in a flat binary file without any header or any block of metadata &#8211; this means that the file will in itself not be able to communicate its data structure and therefore, proper usage of the data requires prior knowledge of how the data has been packed (we used the first four bytes for storing the value of the <code>width</code> of the image).  As a result, that method may be deemed as an &#8220;unorthodox&#8221; hack and unsuitable in team development.</p>
<p>In this post, we look at how you can employ the same basic idea while making the saved data more &#8220;consumable&#8221; by other developers.</p>
<p><span id="more-1601"></span><br />
<strong>Action Message Format (AMF)</strong><br />
ByteArray objects store ActionScript objects using the AMF format.  So instead of saving the binary bitmap data in raw form in the way we described previously, we will put the serialized data in an <code>Object</code> and write that object to a <code>ByteArray</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// assume you have Bitmap object &quot;bitmapImage&quot; you want to save</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// convert the BitmapData to ByteArray</span>
<span style="color: #808080; font-style: italic;">// this time, do not pack any other data in the same ByteArray object</span>
<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">data</span>:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> ByteArray<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0066CC;">data</span>.<span style="color: #006600;">writeBytes</span><span style="color: #66cc66;">&#40;</span>bitmapImage.<span style="color: #006600;">bitmapData</span>.<span style="color: #006600;">getPixels</span><span style="color: #66cc66;">&#40;</span>bitmapImage.<span style="color: #006600;">bitmapData</span>.<span style="color: #006600;">rect</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// create a new data object</span>
<span style="color: #000000; font-weight: bold;">var</span> bmObj:<span style="color: #0066CC;">Object</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
bmObj.<span style="color: #0066CC;">width</span> = bitmapImage.<span style="color: #006600;">bitmapData</span>.<span style="color: #0066CC;">width</span>;
bmObj.<span style="color: #0066CC;">height</span> = bitmapImage.<span style="color: #006600;">bitmapData</span>.<span style="color: #0066CC;">height</span>;
bmObj.<span style="color: #0066CC;">data</span> = <span style="color: #0066CC;">data</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// create a new ByteArray object for saving</span>
<span style="color: #000000; font-weight: bold;">var</span> bytes:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> ByteArray<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
bytes.<span style="color: #006600;">writeObject</span><span style="color: #66cc66;">&#40;</span>bmObj<span style="color: #66cc66;">&#41;</span>;
bytes.<span style="color: #006600;">compress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>So that does it &#8211; you can now save the binary data normally (by posting it to a server script, via AIR local filesystem API, via SharedObject, via FP10 FileReference, etc.).  Although we are still saving our data in a flat binary file, the difference here is that developers consuming the data only need to know that they must read in an <code>Object</code> &#8211; they do not need to have the low-level knowledge of how exactly the data is packed.</p>
<p><!-- --><br />
<strong>ByteArray To BitmapData</strong><br />
Let&#8217;s look at how the binary data can now be consumed in an application.</p>
<p>First, load the file as normal:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> ldr:URLLoader	= <span style="color: #000000; font-weight: bold;">new</span> URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
ldr.<span style="color: #006600;">dataFormat</span>	= URLLoaderDataFormat.<span style="color: #006600;">BINARY</span>; <span style="color: #808080; font-style: italic;">// ** make sure you do this **</span>
ldr.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>, on_fileLoad<span style="color: #66cc66;">&#41;</span>;
ldr.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>IOErrorEvent.<span style="color: #006600;">IO_ERROR</span>, on_fileLoadError<span style="color: #66cc66;">&#41;</span>;
ldr.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span>pathToBitmapDataFile<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Process the loaded data:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> on_fileLoad<span style="color: #66cc66;">&#40;</span>evt:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>evt.<span style="color: #0066CC;">type</span> == Event.<span style="color: #006600;">COMPLETE</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">var</span> bytes:ByteArray = URLLoader<span style="color: #66cc66;">&#40;</span>evt.<span style="color: #0066CC;">target</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">data</span> as ByteArray;
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">try</span>
			<span style="color: #66cc66;">&#123;</span>
				bytes.<span style="color: #006600;">uncompress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #0066CC;">catch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:<span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #808080; font-style: italic;">// bytes is now the uncompressed byte array</span>
			<span style="color: #808080; font-style: italic;">// ... process bytes ...</span>
			<span style="color: #000000; font-weight: bold;">var</span> obj:<span style="color: #0066CC;">Object</span> = bytes.<span style="color: #006600;">readObject</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// obj now has the properties &quot;width&quot;, &quot;height&quot; and &quot;data&quot; as saved previously</span>
&nbsp;
			<span style="color: #000000; font-weight: bold;">var</span> bmd:BitmapData = <span style="color: #000000; font-weight: bold;">new</span> BitmapData<span style="color: #66cc66;">&#40;</span>obj.<span style="color: #0066CC;">width</span>, obj.<span style="color: #0066CC;">height</span>, <span style="color: #000000; font-weight: bold;">true</span>, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 32 bit transparent bitmap</span>
			bmd.<span style="color: #006600;">setPixels</span><span style="color: #66cc66;">&#40;</span>bmd.<span style="color: #006600;">rect</span>, obj.<span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #000000; font-weight: bold;">var</span> bm:Bitmap = <span style="color: #000000; font-weight: bold;">new</span> Bitmap<span style="color: #66cc66;">&#40;</span>bmd<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>bm<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p><!-- --><br />
<strong>Bundle of Bitmaps</strong><br />
What if you have multiple images you want to save and use later?  You can consider serializing the bitmaps and store them together in a single binary file.  To do that, we will give each image a unique id (such as the path of the image file if you are attempting to load and serialize external image files), store the serialized data in a hash, write that hash to a ByteArray, and then write that ByteArray to file.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// for storing the serialized images</span>
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> imgLibrary:<span style="color: #0066CC;">Object</span> = <span style="color: #66cc66;">&#123;</span> <span style="color: #66cc66;">&#125;</span>;
&nbsp;
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> saveImage<span style="color: #66cc66;">&#40;</span>id:<span style="color: #0066CC;">String</span>, image:Bitmap<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>id == <span style="color: #000000; font-weight: bold;">null</span> <span style="color: #66cc66;">||</span> id == <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">return</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>image == <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		imgLibrary<span style="color: #66cc66;">&#91;</span>id<span style="color: #66cc66;">&#93;</span> = <span style="color: #000000; font-weight: bold;">null</span>; <span style="color: #808080; font-style: italic;">// ** remove previously stored data **</span>
		<span style="color: #b1b100;">return</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">data</span>:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> ByteArray<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #0066CC;">data</span>.<span style="color: #006600;">writeBytes</span><span style="color: #66cc66;">&#40;</span>image.<span style="color: #006600;">bitmapData</span>.<span style="color: #006600;">getPixels</span><span style="color: #66cc66;">&#40;</span>image.<span style="color: #006600;">bitmapData</span>.<span style="color: #006600;">rect</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">// create a new data object</span>
	<span style="color: #000000; font-weight: bold;">var</span> bmObj:<span style="color: #0066CC;">Object</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	bmObj.<span style="color: #0066CC;">width</span> = image.<span style="color: #006600;">bitmapData</span>.<span style="color: #0066CC;">width</span>;
	bmObj.<span style="color: #0066CC;">height</span> = image.<span style="color: #006600;">bitmapData</span>.<span style="color: #0066CC;">height</span>;
	bmObj.<span style="color: #0066CC;">data</span> = <span style="color: #0066CC;">data</span>;
&nbsp;
	imgLibrary<span style="color: #66cc66;">&#91;</span>id<span style="color: #66cc66;">&#93;</span> = bmObj; <span style="color: #808080; font-style: italic;">// ** will overwrite previously stored data **</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
saveImage<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;iconA&quot;</span>, imageIconA<span style="color: #66cc66;">&#41;</span>;
saveImage<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;iconB&quot;</span>, imageIconB<span style="color: #66cc66;">&#41;</span>;
saveImage<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;iconC&quot;</span>, imageIconC<span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// imgLibrary now contains three data objects each representing an image</span></pre></div></div>

<p>Now, write imgLibrary to file:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// ** must target Flash Player version 10+ **</span>
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> on_buttonClick<span style="color: #66cc66;">&#40;</span>evt:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> bytes:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> ByteArray<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	bytes.<span style="color: #006600;">writeObject</span><span style="color: #66cc66;">&#40;</span>imgLibrary<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// store width of image</span>
	bytes.<span style="color: #006600;">compress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">new</span> FileReference<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span>bytes, <span style="color: #ff0000;">&quot;image.bmd&quot;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// default name &quot;image.bmd&quot;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>In another application, we load the saved binary file as usual:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> ldr:URLLoader	= <span style="color: #000000; font-weight: bold;">new</span> URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
ldr.<span style="color: #006600;">dataFormat</span>	= URLLoaderDataFormat.<span style="color: #006600;">BINARY</span>; <span style="color: #808080; font-style: italic;">// ** make sure you do this **</span>
ldr.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>, on_fileLoad<span style="color: #66cc66;">&#41;</span>;
ldr.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>IOErrorEvent.<span style="color: #006600;">IO_ERROR</span>, on_fileLoadError<span style="color: #66cc66;">&#41;</span>;
ldr.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span>pathToFile<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>And process and use the loaded binary data as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> imgLibrary:<span style="color: #0066CC;">Object</span>;
&nbsp;
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> on_fileLoad<span style="color: #66cc66;">&#40;</span>evt:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>evt.<span style="color: #0066CC;">type</span> == Event.<span style="color: #006600;">COMPLETE</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">var</span> bytes:ByteArray = URLLoader<span style="color: #66cc66;">&#40;</span>evt.<span style="color: #0066CC;">target</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">data</span> as ByteArray;
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">try</span>
			<span style="color: #66cc66;">&#123;</span>
				bytes.<span style="color: #006600;">uncompress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #0066CC;">catch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:<span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #808080; font-style: italic;">// bytes is now the uncompressed byte array</span>
			<span style="color: #808080; font-style: italic;">// ... process bytes ...</span>
			imgLibrary = bytes.<span style="color: #006600;">readObject</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// imgLibrary is now initialized and contains data objects each representing a bitmap image</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>After populating the imgLibrary hash, you can use a method like the one below to query it:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getImage<span style="color: #66cc66;">&#40;</span>id:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Bitmap
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>id == <span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #66cc66;">||</span> id == <span style="color: #000000; font-weight: bold;">null</span> <span style="color: #66cc66;">||</span> imgLibrary == <span style="color: #000000; font-weight: bold;">null</span> <span style="color: #66cc66;">||</span> imgLibrary<span style="color: #66cc66;">&#91;</span>id<span style="color: #66cc66;">&#93;</span> == <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">var</span> obj:<span style="color: #0066CC;">Object</span> = imgLibrary<span style="color: #66cc66;">&#91;</span>id<span style="color: #66cc66;">&#93;</span>;
	<span style="color: #0066CC;">try</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">// obj now has the properties &quot;width&quot;, &quot;height&quot; and &quot;data&quot; as saved previously</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">var</span> bmd:BitmapData = <span style="color: #000000; font-weight: bold;">new</span> BitmapData<span style="color: #66cc66;">&#40;</span>obj.<span style="color: #0066CC;">width</span>, obj.<span style="color: #0066CC;">height</span>, <span style="color: #000000; font-weight: bold;">true</span>, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 32 bit transparent bitmap</span>
		bmd.<span style="color: #006600;">setPixels</span><span style="color: #66cc66;">&#40;</span>bmd.<span style="color: #006600;">rect</span>, obj.<span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
 		<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Bitmap<span style="color: #66cc66;">&#40;</span>bmd<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #0066CC;">catch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:<span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
getImage<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;iconA&quot;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// returns a Bitmap with BitmapData constructed from saved byte array</span></pre></div></div>

<p>The <code>getImage()</code> method can also be improved such that the <code>ByteArray-to-BitmapData</code> conversion is only done once per image:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getImage<span style="color: #66cc66;">&#40;</span>id:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Bitmap
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>id == <span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #66cc66;">||</span> id == <span style="color: #000000; font-weight: bold;">null</span> <span style="color: #66cc66;">||</span> imgLibrary == <span style="color: #000000; font-weight: bold;">null</span> <span style="color: #66cc66;">||</span> imgLibrary<span style="color: #66cc66;">&#91;</span>id<span style="color: #66cc66;">&#93;</span> == <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">var</span> obj:<span style="color: #66cc66;">*</span> = imgLibrary<span style="color: #66cc66;">&#91;</span>id<span style="color: #66cc66;">&#93;</span>;
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>obj is BitmapData<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Bitmap<span style="color: #66cc66;">&#40;</span>BitmapData<span style="color: #66cc66;">&#40;</span>obj<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">clone</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #0066CC;">try</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">// obj now has the properties &quot;width&quot;, &quot;height&quot; and &quot;data&quot; as saved previously</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">var</span> bmd:BitmapData = <span style="color: #000000; font-weight: bold;">new</span> BitmapData<span style="color: #66cc66;">&#40;</span>obj.<span style="color: #0066CC;">width</span>, obj.<span style="color: #0066CC;">height</span>, <span style="color: #000000; font-weight: bold;">true</span>, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 32 bit transparent bitmap</span>
		bmd.<span style="color: #006600;">setPixels</span><span style="color: #66cc66;">&#40;</span>bmd.<span style="color: #006600;">rect</span>, obj.<span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		imgLibrary<span style="color: #66cc66;">&#91;</span>id<span style="color: #66cc66;">&#93;</span> = bmd;
&nbsp;
 		<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Bitmap<span style="color: #66cc66;">&#40;</span>bmd.<span style="color: #006600;">clone</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #0066CC;">catch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:<span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Note: If you are confident that the <code>BitmapData</code> is not going to be modified by instances, remove the <code>clone()</code> calls above &#8211; it will save memory since that means all <code>Bitmap</code> instances with the same <code>id</code> will be using the exact same <code>BitmapData</code>.</p>
<p><!-- --><br />
<strong>Embedding Image Asset(s)</strong><br />
After you have saved the binary file, remember that you are not limited to using it only as an external resource &#8211; you can also embed the file into your SWF:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// ActionScript 3.0</span>
<span style="color: #66cc66;">&#91;</span>Embed<span style="color: #66cc66;">&#40;</span>source=<span style="color: #ff0000;">&quot;assets/images/icons.bin&quot;</span>, mimeType=<span style="color: #ff0000;">&quot;application/octet-stream&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> const iconsBundle:<span style="color: #000000; font-weight: bold;">Class</span>;
&nbsp;
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> imgLibrary:<span style="color: #0066CC;">Object</span>;
&nbsp;
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> initIcons<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> bytes:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> iconsBundle<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> as ByteArray;
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">try</span>
		<span style="color: #66cc66;">&#123;</span>
			bytes.<span style="color: #006600;">uncompress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">catch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:<span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// data not compressed</span>
		<span style="color: #66cc66;">&#125;</span>
		imgLibrary = bytes.<span style="color: #006600;">readObject</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #808080; font-style: italic;">// imgLibrary is now initialized and contains data objects each representing a bitmap image</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.ghostwire.com/blog/archives/as3-serializing-a-bundle-of-bitmaps-as-data-objects/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>[AS3] Embedding Binary XML</title>
		<link>http://www.ghostwire.com/blog/archives/as3-embedding-binary-xml/</link>
		<comments>http://www.ghostwire.com/blog/archives/as3-embedding-binary-xml/#comments</comments>
		<pubDate>Fri, 04 Dec 2009 06:12:54 +0000</pubDate>
		<dc:creator>sunny</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flash AS3]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[ByteArray]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.ghostwire.com/blog/?p=1554</guid>
		<description><![CDATA[This post is a supplement to &#8220;Saving XML As Binary&#8221;. I neglected to mention in that post that you can, if so desired, embed the binary XML within your SWF instead of loading it during run-time. That means you still get to keep the XML externally, not as part of your application code, so that [...]]]></description>
			<content:encoded><![CDATA[<p>This post is a supplement to <a href="http://www.ghostwire.com/blog/archives/as3-saving-xml-as-binary/">&#8220;Saving XML As Binary&#8221;</a>.  I neglected to mention in that post that you can, if so desired, embed the binary XML within your SWF instead of loading it during run-time.  That means you still get to keep the XML externally, not as part of your application code, so that the code and data can still be kept separate and maintained more easily.</p>
<p>However, it must be mentioned that embedding a text XML within SWF will get it compressed as part of the SWF compression anyway.  Therefore, if compression is the only motivation, don&#8217;t convert the XML to binary and embed the binary version &#8211; it does not make sense.  But if you wish to do some data encryption, then the additional work may be appropriate.</p>
<p><span id="more-1554"></span><br />
<strong>Embedding External Data</strong><br />
You can embed any external source, including any external XML file, within your SWF using the [Embed] tag:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// ActionScript 3.0</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// assuming you have an XML file named &quot;data.xml&quot; in folder &quot;assets/data/&quot;</span>
&nbsp;
<span style="color: #66cc66;">&#91;</span>Embed<span style="color: #66cc66;">&#40;</span>source=<span style="color: #ff0000;">&quot;assets/data/data.xml&quot;</span>, mimeType=<span style="color: #ff0000;">&quot;application/octet-stream&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> const binaryXML:<span style="color: #000000; font-weight: bold;">Class</span>;</pre></div></div>

<p><!-- --><br />
<strong>Back To XML</strong><br />
You can then instantiate the binary data and convert it back to XML in your code:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> xmlData:<span style="color: #0066CC;">XML</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">var</span> bytes:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> binaryXML<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> as ByteArray
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">try</span>
	<span style="color: #66cc66;">&#123;</span>
		bytes.<span style="color: #006600;">uncompress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #0066CC;">catch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:<span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
	<span style="color: #66cc66;">&#125;</span>
	xmlData = <span style="color: #0066CC;">XML</span><span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>NOTE: The above code will be able to handle both binary XML and text XML files.</p>
<p><!-- --><br />
<strong>ROT128 Encrypted Binary XML</strong><br />
If you had performed <a href="http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-binary-xml/">ROT128 encryption</a> on the binary XML, you decrypt it accordingly before calling <code>uncompress()</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> xmlData:<span style="color: #0066CC;">XML</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">var</span> bytes:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> binaryXML<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> as ByteArray
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">// BEGIN ROT128</span>
	<span style="color: #000000; font-weight: bold;">var</span> j:<span style="color: #0066CC;">int</span> = <span style="color: #0066CC;">data</span>.<span style="color: #0066CC;">length</span>;
	<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>j--<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> += <span style="color: #cc66cc;">128</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #808080; font-style: italic;">// END ROT128</span>
	bytes.<span style="color: #006600;">uncompress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	xmlData = <span style="color: #0066CC;">XML</span><span style="color: #66cc66;">&#40;</span>bytes<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>NOTE: The above code assumes the data is binary and ROT128 encrypted; it will not be able to handle text XML.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ghostwire.com/blog/archives/as3-embedding-binary-xml/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>[AS3] Serializing Bitmaps (Storing BitmapData As Raw Binary/ByteArray)</title>
		<link>http://www.ghostwire.com/blog/archives/as3-serializing-bitmaps-storing-bitmapdata-as-raw-binarybytearray/</link>
		<comments>http://www.ghostwire.com/blog/archives/as3-serializing-bitmaps-storing-bitmapdata-as-raw-binarybytearray/#comments</comments>
		<pubDate>Thu, 03 Dec 2009 14:46:00 +0000</pubDate>
		<dc:creator>sunny</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flash AS3]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Bitmap]]></category>
		<category><![CDATA[BitmapData]]></category>
		<category><![CDATA[ByteArray]]></category>

		<guid isPermaLink="false">http://www.ghostwire.com/blog/?p=1029</guid>
		<description><![CDATA[Whenever an application needs to save bitmap images to local storage or post them to a server script, a common practice is to encode the image as JPEG or PNG before sending that binary data off as the respective mimeType. However, if the intention is simply to save the bitmap image, ie to serialize the [...]]]></description>
			<content:encoded><![CDATA[<p>Whenever an application needs to save bitmap images to local storage or post them to a server script, a common practice is to encode the image as JPEG or PNG before sending that binary data off as the respective mimeType.  However, if the intention is simply to save the bitmap image, ie to serialize the <code>BitmapData</code>, then converting the image to JPEG/PNG would actually be unnecessary.</p>
<p><span id="more-1029"></span><br />
<strong>BitmapData to ByteArray</strong><br />
To get a byte array representing the <code>BitmapData</code>, all you need to do is to call the <code>getPixels()</code> method.  The <code>getPixels()</code> method requires you to specify the rectangular area to capture; the most convenient way to specify this would be to use the <code>rect</code> property of the <code>BitmapData</code> you are serializing.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// ActionScript 3.0</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// assuming you have a Bitmap object &quot;bitmapImage&quot; that you would like to serialize</span>
<span style="color: #000000; font-weight: bold;">var</span> bytes:ByteArray = bitmapImage.<span style="color: #006600;">bitmapData</span>.<span style="color: #006600;">getPixels</span><span style="color: #66cc66;">&#40;</span>bitmapImage.<span style="color: #006600;">bitmapData</span>.<span style="color: #006600;">rect</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>The method returns a <code>ByteArray</code> object, with each pixel in the <code>BitmapData</code> represented by an unsigned integer, ie four (4) bytes, in the <code>ByteArray</code>.  This means that you can expect a 20&#215;20 bitmap image to be represented by a <code>ByteArray</code> object that has 1600 bytes (20 x 20 x 4 = 1600) before compression.</p>
<p>After you have the <code>ByteArray</code> object, compress it:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> bytes:ByteArray = bitmapImage.<span style="color: #006600;">bitmapData</span>.<span style="color: #006600;">getPixels</span><span style="color: #66cc66;">&#40;</span>bitmapImage.<span style="color: #006600;">bitmapData</span>.<span style="color: #006600;">rect</span><span style="color: #66cc66;">&#41;</span>;
bytes.<span style="color: #006600;">compress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>That effectively gives you the lossless compressed binary data of the bitmap image.</p>
<p><!-- --><br />
<strong>Bitmap Dimensions (Width And Height)</strong><br />
So, getting the <code>ByteArray</code> representation of a bitmap image is simple &#8211; just call the <code>getPixels()</code> method.</p>
<p>However, in order for the data to be useful, we must be able to re-construct the bitmap image from it.  The byte array in itself does not give any clue as to the dimensions of the bitmap image, only the total number of pixels.  This means that you must store the dimensions of the bitmap image as well. Actually, you only need to store either the <code>width</code> or the <code>height</code>, because you can compute the other side once you have either one since you also know the total number of pixels.</p>
<p>In the code below, we will store information about the <code>width</code> of the <code>BitmapData</code> in the first four (4) bytes, followed by the byte array representing the image:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> bytes:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> ByteArray<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
bytes.<span style="color: #006600;">writeUnsignedInt</span><span style="color: #66cc66;">&#40;</span>bitmapImage.<span style="color: #006600;">bitmapData</span>.<span style="color: #0066CC;">width</span><span style="color: #66cc66;">&#41;</span>;
bytes.<span style="color: #006600;">writeBytes</span><span style="color: #66cc66;">&#40;</span>bitmapImage.<span style="color: #006600;">bitmapData</span>.<span style="color: #006600;">getPixels</span><span style="color: #66cc66;">&#40;</span>bitmapImage.<span style="color: #006600;">bitmapData</span>.<span style="color: #006600;">rect</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
bytes.<span style="color: #006600;">compress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p><!-- --><br />
<strong>Saving To File</strong><br />
Once the above is done, you can save the binary data normally (by posting it to a server script, via AIR local filesystem API, via <code>SharedObject</code>, via FP10 <code>FileReference</code>, etc.).</p>
<p>For this example, we will save the binary data to file using the <code>save()</code> method of the <code>FileReference</code> class (available when targeting Flash Player 10).  As a security measure, the save() method will only work in the Flash Player if you call it in response to a user event (for example, MouseEvent.CLICK event). Therefore, you need to create a button, attach a listener to it, and call the save() method in the event handler.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// ** must target Flash Player version 10+ **</span>
<span style="color: #000000; font-weight: bold;">function</span> on_buttonClick<span style="color: #66cc66;">&#40;</span>evt:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> bytes:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> ByteArray<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	bytes.<span style="color: #006600;">writeUnsignedInt</span><span style="color: #66cc66;">&#40;</span>bitmapImage.<span style="color: #006600;">bitmapData</span>.<span style="color: #0066CC;">width</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// store width of image</span>
	bytes.<span style="color: #006600;">writeBytes</span><span style="color: #66cc66;">&#40;</span>bitmapImage.<span style="color: #006600;">bitmapData</span>.<span style="color: #006600;">getPixels</span><span style="color: #66cc66;">&#40;</span>bitmapImage.<span style="color: #006600;">bitmapData</span>.<span style="color: #006600;">rect</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// store bitmapdata as bytearray</span>
	bytes.<span style="color: #006600;">compress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">new</span> FileReference<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span>bytes, <span style="color: #ff0000;">&quot;image.bmd&quot;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// default name &quot;image.bmd&quot;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>You can give the file any name, any extension.  In the example code above, I gave the saved file a &#8220;.bmd&#8221; extension (BitmapData), but this is just a fictitious file type.  The resulting file has no valid mimeType and does not work as any known file type &#8211; it is our own custom binary file format that is useful only for storage and subsequently re-used by our own application code.</p>
<p><!-- --><br />
<strong>ByteArray to BitmapData</strong><br />
As mentioned above, in order for the saved data to be useful, we must be able to re-construct the original bitmap image from it.</p>
<p>First, we load the file normally via <code>URLLoader</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> ldr:URLLoader	= <span style="color: #000000; font-weight: bold;">new</span> URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
ldr.<span style="color: #006600;">dataFormat</span>	= URLLoaderDataFormat.<span style="color: #006600;">BINARY</span>; <span style="color: #808080; font-style: italic;">// ** make sure you do this **</span>
ldr.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>, on_fileLoad<span style="color: #66cc66;">&#41;</span>;
ldr.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>IOErrorEvent.<span style="color: #006600;">IO_ERROR</span>, on_fileLoadError<span style="color: #66cc66;">&#41;</span>;
ldr.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span>pathToBitmapDataFile<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>The following shows a simple <code>on_fileLoad</code> handler:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> on_fileLoad<span style="color: #66cc66;">&#40;</span>evt:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>evt.<span style="color: #0066CC;">type</span> == Event.<span style="color: #006600;">COMPLETE</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">data</span>:ByteArray = URLLoader<span style="color: #66cc66;">&#40;</span>evt.<span style="color: #0066CC;">target</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">data</span> as ByteArray;
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">try</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #0066CC;">data</span>.<span style="color: #006600;">uncompress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #0066CC;">catch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:<span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #808080; font-style: italic;">// data is now the uncompressed byte array</span>
			<span style="color: #808080; font-style: italic;">// ... process data ...</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>We need to recall the dimensions of the bitmap image.  Remember that we saved the value of the <code>width</code> in the first four (4) bytes of the binary data:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// after data.uncompress()</span>
<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">width</span>:<span style="color: #0066CC;">int</span> = <span style="color: #0066CC;">data</span>.<span style="color: #006600;">readUnsignedInt</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// first 4 bytes (unsigned integer)</span></pre></div></div>

<p>We can then derive the <code>height</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// after data.uncompress()</span>
<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">height</span>:<span style="color: #0066CC;">int</span> = <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span>.<span style="color: #0066CC;">length</span> - <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">/</span> <span style="color: #0066CC;">width</span>;
<span style="color: #808080; font-style: italic;">// (data.length - 4) ** byte array less the first four bytes gives the representation of the bitmap **</span>
<span style="color: #808080; font-style: italic;">// ((data.length - 4) / 4) ** length of the representation is divided by four because each pixel takes four bytes **</span>
<span style="color: #808080; font-style: italic;">// ((data.length - 4) / 4) / width ** remember, it is a rectangle, so we can get the height this way **</span></pre></div></div>

<p>NOTE: If you want to skip &#8220;dimension derivation&#8221;, you can store both <code>width</code> and <code>height</code> in your binary data.  Either way will work, the choice is yours.</p>
<p>Once you know the dimensions, you can re-construct the <code>Bitmap</code> object using the <code>setPixels()</code> method:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> bmd:BitmapData = <span style="color: #000000; font-weight: bold;">new</span> BitmapData<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">width</span>, <span style="color: #0066CC;">height</span>, <span style="color: #000000; font-weight: bold;">true</span>, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 32 bit transparent bitmap</span>
bmd.<span style="color: #006600;">setPixels</span><span style="color: #66cc66;">&#40;</span>bmd.<span style="color: #006600;">rect</span>, <span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// position of data is now at 5th byte</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">var</span> bm:Bitmap = <span style="color: #000000; font-weight: bold;">new</span> Bitmap<span style="color: #66cc66;">&#40;</span>bmd<span style="color: #66cc66;">&#41;</span>;
addChild<span style="color: #66cc66;">&#40;</span>bm<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p><!-- --><br />
<strong>Conclusion</strong><br />
The above shows how you can convert <code>BitmapData</code> to a <code>ByteArray</code>, save that <code>ByteArray</code>, and re-construct the <code>BitmapData </code>from the saved <code>ByteArray</code>.</p>
<p>Although the basic purpose would be to save bitmap images to server/local storage, there would also be other scenarios where the above technique will be useful.  For example, after you obtain the <code>ByteArray</code> representation of the image, you can post it to your server for further processing.  You can also use the technique to trim down your external JPEG/PNG image files, stripping away all meta information from the JPEG/PNG encoding leaving behind only the raw image data (possibly resulting in smaller file sizes).  Of course, the resulting binary files will no longer work as JPEG/PNG image files, but your applications will be able to reconstruct the images easily during run-time.  Actually, you can also consider this as a way to protect your external image files from unwanted hot linking.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ghostwire.com/blog/archives/as3-serializing-bitmaps-storing-bitmapdata-as-raw-binarybytearray/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>[AS3] Drawing Outline on Text Glyphs</title>
		<link>http://www.ghostwire.com/blog/archives/as3-drawing-outline-on-text-glyphs/</link>
		<comments>http://www.ghostwire.com/blog/archives/as3-drawing-outline-on-text-glyphs/#comments</comments>
		<pubDate>Wed, 18 Nov 2009 09:20:51 +0000</pubDate>
		<dc:creator>sunny</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flash AS3]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Filters]]></category>
		<category><![CDATA[Fonts]]></category>
		<category><![CDATA[TextField]]></category>

		<guid isPermaLink="false">http://ghostwire.com/blog/?p=429</guid>
		<description><![CDATA[Here is how you can draw an outline on text glyphs during run-time via ActionScript. This method works with embedded as well as non-embedded fonts. First, set up the TextField: // ActionScript 3.0 var txt:TextField = new TextField&#40;&#41;; // new TextFormat(font, size, color) txt.defaultTextFormat = new TextFormat&#40;&#34;DejaVu Sans&#34;, 21, 0xFFFFFF&#41;; txt.antiAliasType = AntiAliasType.ADVANCED; txt.autoSize = [...]]]></description>
			<content:encoded><![CDATA[<p>Here is how you can draw an outline on text glyphs during run-time via ActionScript. This method works with embedded as well as non-embedded fonts.</p>
<p><span id="more-429"></span><br />
First, set up the TextField:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// ActionScript 3.0</span>
<span style="color: #000000; font-weight: bold;">var</span> txt:<span style="color: #0066CC;">TextField</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// new TextFormat(font, size, color)</span>
txt.<span style="color: #006600;">defaultTextFormat</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextFormat</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;DejaVu Sans&quot;</span>, <span style="color: #cc66cc;">21</span>, 0xFFFFFF<span style="color: #66cc66;">&#41;</span>;
txt.<span style="color: #006600;">antiAliasType</span> = AntiAliasType.<span style="color: #006600;">ADVANCED</span>;
txt.<span style="color: #0066CC;">autoSize</span> = TextFieldAutoSize.<span style="color: #0066CC;">LEFT</span>;
txt.<span style="color: #0066CC;">text</span> = <span style="color: #ff0000;">&quot;The quick brown fox jumps over the lazy dog.&quot;</span>;
txt.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">5</span>;
txt.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">5</span>;
addChild<span style="color: #66cc66;">&#40;</span>txt<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p><!-- --><br />
<strong>Drawing The Outline</strong><br />
To draw an outline around the text glyphs, use the <code>flash.filters.GlowFilter</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// new GlowFilter(color, alpha, blurX, blurY, strength)</span>
txt.<span style="color: #006600;">filters</span> = <span style="color: #66cc66;">&#91;</span><span style="color: #000000; font-weight: bold;">new</span> GlowFilter<span style="color: #66cc66;">&#40;</span>0x000000, <span style="color: #cc66cc;">1.0</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>;</pre></div></div>

<p><img src="http://ghostwire.com/blog/wp-content/uploads/outlinetext_4.png" alt="outline text weight 4" title="outline text weight 4" width="596" height="96" class="alignleft size-full wp-image-433" /></p>
<p><!-- --><br />
<strong>Adjusting The Outline Thickness</strong><br />
To thicken the outline, increase the values for &#8220;blurX&#8221;, &#8220;blurY&#8221; and &#8220;strength&#8221;.  The value of &#8220;blurX&#8221;/&#8221;blurY&#8221; should be the same.  You can try various values to suit your needs, but the value of &#8220;strength&#8221; should be kept at double that of &#8220;blurX&#8221;/&#8221;blurY&#8221;.  For example, if I were to use value of 4 for &#8220;blurX&#8221;/&#8221;blurY&#8221;, I would set &#8220;strength&#8221; as 8.  Setting &#8220;strength&#8221; too low will cause blurring to be visible; setting it excessively high and the outline may become distorted.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// new GlowFilter(color, alpha, blurX, blurY, strength)</span>
txt.<span style="color: #006600;">filters</span> = <span style="color: #66cc66;">&#91;</span><span style="color: #000000; font-weight: bold;">new</span> GlowFilter<span style="color: #66cc66;">&#40;</span>0x000000, <span style="color: #cc66cc;">1.0</span>, <span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>;</pre></div></div>

<p><img src="http://ghostwire.com/blog/wp-content/uploads/outlinetext_8.png" alt="outline text weight 8" title="outline text weight 8" width="596" height="96" class="alignleft size-full wp-image-432" /></p>
<p><!-- --><br />
<strong>Aspire UI</strong><br />
If you are using the <a href="http://www.ghostwire.com/aspireui/download/">Aspire UI library</a>, this feature is already built-in via the <code>uiTextStyles</code> manager:<br />
<a href="http://ghostwire.com/aspireui/docs/textstyles/outline-and-shadow">http://ghostwire.com/aspireui/docs/textstyles/outline-and-shadow</a><br />
The &#8220;outlineWeight&#8221; text style property (v1.2.2+) determines the thickness of the outline.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ghostwire.com/blog/archives/as3-drawing-outline-on-text-glyphs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[AS3] Applying ROT128 Encryption On Binary XML</title>
		<link>http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-binary-xml/</link>
		<comments>http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-binary-xml/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 11:05:12 +0000</pubDate>
		<dc:creator>sunny</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flash AS3]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[ByteArray]]></category>
		<category><![CDATA[Encryption]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.ghostwire.com/blog/?p=1381</guid>
		<description><![CDATA[This is Part III of our discussion on ROT128 Encryption. Part I: &#8220;Applying ROT128 Encryption On ByteArray&#8221; Part II: &#8220;Applying ROT128 Encryption On Embedded/Module SWFs&#8221; In &#8220;Saving XML As Binary&#8221;, we looked at how text XML can be stored in a ByteArray object so that it can be compressed and made non-human-readable. In this post, [...]]]></description>
			<content:encoded><![CDATA[<p>This is Part III of our discussion on ROT128 Encryption.</p>
<p>Part I: <a href="http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-bytearray/">&#8220;Applying ROT128 Encryption On ByteArray&#8221;</a><br />
Part II: <a href="http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-embeddedmodule-swfs/">&#8220;Applying ROT128 Encryption On Embedded/Module SWFs&#8221;</a></p>
<p>In <a href="http://www.ghostwire.com/blog/archives/as3-saving-xml-as-binary/">&#8220;Saving XML As Binary&#8221;</a>, we looked at how text XML can be stored in a ByteArray object so that it can be compressed and made non-human-readable.  In this post, we look at how you can integrate ROT128 into the XML-to-ByteArray-to-XML routines.  Binary XML is used only as an example; you can definitely apply the same concept to other binary data.</p>
<p><span id="more-1381"></span><br />
<strong>XML-to-ByteArray:</strong><br />
ROT128 is applied after the XML is stored and compressed as ByteArray.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">data</span>:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> ByteArray<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0066CC;">data</span>.<span style="color: #006600;">writeUTFBytes</span><span style="color: #66cc66;">&#40;</span>xmlData<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// xmlData is original XML string</span>
<span style="color: #0066CC;">data</span>.<span style="color: #006600;">compress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// BEGIN ROT128</span>
<span style="color: #000000; font-weight: bold;">var</span> j:<span style="color: #0066CC;">int</span> = <span style="color: #0066CC;">data</span>.<span style="color: #0066CC;">length</span>;
<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>j--<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> += <span style="color: #cc66cc;">128</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #808080; font-style: italic;">// END ROT128</span>
<span style="color: #000000; font-weight: bold;">new</span> FileReference<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span>, <span style="color: #ff0000;">&quot;bindata.xml&quot;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// default name &quot;bindata.xml&quot;</span></pre></div></div>

<p><!-- --><br />
<strong>ByteArray-to-XML:</strong><br />
With ROT128 applied, the saved binary file is no longer a valid compressed ByteArray &#8211; it cannot be uncompressed and converted back to XML without first reversing the encryption on the raw data.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// event handler for Event.COMPLETE of URLLoader loading external XML</span>
<span style="color: #000000; font-weight: bold;">function</span> on_XML<span style="color: #66cc66;">&#40;</span>evt:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">data</span>:<span style="color: #66cc66;">*</span> = URLLoader<span style="color: #66cc66;">&#40;</span>evt.<span style="color: #0066CC;">target</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">data</span>;
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span> is ByteArray<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">try</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// BEGIN ROT128</span>
			<span style="color: #000000; font-weight: bold;">var</span> j:<span style="color: #0066CC;">int</span> = <span style="color: #0066CC;">data</span>.<span style="color: #0066CC;">length</span>;
			<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>j--<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> += <span style="color: #cc66cc;">128</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #808080; font-style: italic;">// END ROT128</span>
			<span style="color: #0066CC;">data</span>.<span style="color: #006600;">uncompress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">catch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:<span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #0066CC;">data</span> = <span style="color: #0066CC;">XML</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">// handle data as XML</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p><!-- --><br />
<strong>Partial Encryption</strong><br />
In order to make the process of reversing ROT128 encryption less predictable, you could vary the conditions under which the encryption is to be applied.</p>
<p>Here is an example (alternate bytes):</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// BEGIN ROT128</span>
<span style="color: #000000; font-weight: bold;">var</span> j:<span style="color: #0066CC;">int</span> = <span style="color: #0066CC;">data</span>.<span style="color: #0066CC;">length</span>;
<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>j--<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> += <span style="color: #cc66cc;">128</span>;
	j--; <span style="color: #808080; font-style: italic;">// skip next byte</span>
<span style="color: #66cc66;">&#125;</span>
<span style="color: #808080; font-style: italic;">// END ROT128</span></pre></div></div>

<p>Another example (first 1024 bytes):</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// BEGIN ROT128</span>
<span style="color: #000000; font-weight: bold;">var</span> j:<span style="color: #0066CC;">int</span> = <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span>.<span style="color: #0066CC;">length</span> <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">1024</span><span style="color: #66cc66;">&#41;</span> ? <span style="color: #0066CC;">data</span>.<span style="color: #0066CC;">length</span> : <span style="color: #cc66cc;">1024</span>;
<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>j--<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> += <span style="color: #cc66cc;">128</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #808080; font-style: italic;">// END ROT128</span></pre></div></div>

<p>Correspondingly, your applications would reverse the encryption under the same conditions.  Effectively, these conditions determine the complexity of your cipher and become the &#8220;secret key&#8221; necessary to decrypt the data.</p>
<p><!-- --><br />
<strong>Conclusion</strong><br />
ROT128 is a weak encryption technique that you should not use to protect sensitive data.  However, the light-weight algorithm can be used to make binary blobs inaccessible to unauthorized parties while allowing your applications to easily restore the data when needed.</p>
<p>This method of encryption may seem amateurish, but it does what it is supposed to do well enough while remaining light-weight and flexible.</p>
<p>It worked well enough for Julius Caesar, after all.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-binary-xml/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>[AS3] Applying ROT128 Encryption On Embedded/Module SWFs</title>
		<link>http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-embeddedmodule-swfs/</link>
		<comments>http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-embeddedmodule-swfs/#comments</comments>
		<pubDate>Fri, 13 Nov 2009 04:02:43 +0000</pubDate>
		<dc:creator>sunny</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flash AS3]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[ByteArray]]></category>
		<category><![CDATA[Copy Protection]]></category>
		<category><![CDATA[Encryption]]></category>
		<category><![CDATA[Module SWF]]></category>

		<guid isPermaLink="false">http://www.ghostwire.com/blog/?p=1382</guid>
		<description><![CDATA[This post is a supplement to &#8220;Applying ROT128 Encryption On ByteArray&#8221;. Some time back, we posted a simple technique for hiding assets and AS3 code from prying eyes by embedding one SWF within another SWF. In this post, we revisit that topic and look at how ROT128 can be used to provide an additional layer [...]]]></description>
			<content:encoded><![CDATA[<p>This post is a supplement to <a href="http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-bytearray/">&#8220;Applying ROT128 Encryption On ByteArray&#8221;</a>.</p>
<p>Some time back, we posted <a href="http://www.ghostwire.com/blog/archives/as3-hiding-assets-and-code-by-embedding-swf-within-another-swf/">a simple technique</a> for hiding assets and AS3 code from prying eyes by embedding one SWF within another SWF. In this post, we revisit that topic and look at how ROT128 can be used to provide an additional layer of protection.</p>
<p><span id="more-1382"></span><br />
<strong>Step I: Apply ROT128 To Actual SWF</strong><br />
Using the following code, we will apply ROT128 to the SWF that is going to be embedded:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// assuming the raw data of the SWF has been stored in a variable swfBytes</span>
<span style="color: #000000; font-weight: bold;">var</span> j:<span style="color: #0066CC;">int</span> = swfBytes.<span style="color: #0066CC;">length</span>;
<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>j--<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	swfBytes<span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> += <span style="color: #cc66cc;">128</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The following shows a simple tool you can create to load a SWF file, apply ROT128 to it, and save the encrypted file.</p>
<ul>
<li>Flash Player 10 is required.</li>
<li>Click the Browse button to bring up a native file dialog.</li>
<li>Select a Flash Movie (.swf) file.</li>
<li>Click the Save button that will be shown after loading the SWF file.</li>
</ul>
<p>(80&#215;80 SWF, 2KB)<br />

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_ROT128SWF_989360771"
			class="flashmovie"
			width="80"
			height="80">
	<param name="movie" value="/swf/ROT128SWF.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="/swf/ROT128SWF.swf"
			name="fm_ROT128SWF_989360771"
			width="80"
			height="80">
	<!--<![endif]-->
		
<p><a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<p>You can verify that encryption has been done by running the saved SWF file &#8220;ActualSWF.swf&#8221; &#8211; it should show a blank screen.</p>
<p>Remember, applying ROT128 twice restores the file, so the tool can also be used to restore a previously ROT128-encrypted file if the algorithm used was the same (same n increment, same conditional loop).</p>
<p>The ROT128SWF class:<br />
(please feel free to customize to fit your own requirements)</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package 
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">Stage</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">StageAlign</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">StageScaleMode</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">IOErrorEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">FileFilter</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">FileReference</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextField</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #006600;">TextFieldAutoSize</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #006600;">TextFieldType</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextFormat</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ROT128SWF <span style="color: #0066CC;">extends</span> Sprite 
	<span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// ** minimalist text button **</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> opButton:<span style="color: #0066CC;">TextField</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">// ** browse/load/save **</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> swfFile:FileReference;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> ROT128SWF<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> 
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">stage</span><span style="color: #66cc66;">&#41;</span> init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">else</span> addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ADDED_TO_STAGE</span>, init<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> 
		<span style="color: #66cc66;">&#123;</span>
			removeEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ADDED_TO_STAGE</span>, init<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// entry point</span>
			<span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">align</span>			= StageAlign.<span style="color: #0066CC;">LEFT</span>;
			<span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">scaleMode</span>			= StageScaleMode.<span style="color: #006600;">NO_SCALE</span>;
			<span style="color: #0066CC;">stage</span>.<span style="color: #006600;">showDefaultContextMenu</span>	= <span style="color: #000000; font-weight: bold;">false</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// ** draw minimalist text button **</span>
			opButton			= <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			opButton.<span style="color: #0066CC;">autoSize</span>		= TextFieldAutoSize.<span style="color: #0066CC;">LEFT</span>;
			opButton.<span style="color: #0066CC;">background</span>		= <span style="color: #000000; font-weight: bold;">true</span>;
			opButton.<span style="color: #0066CC;">backgroundColor</span>	= 0x000000;
			opButton.<span style="color: #006600;">defaultTextFormat</span>	= <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextFormat</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Tahoma&quot;</span>, <span style="color: #cc66cc;">14</span>, 0xFFFFFF, <span style="color: #000000; font-weight: bold;">true</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span>;
			opButton.<span style="color: #0066CC;">selectable</span>		= <span style="color: #000000; font-weight: bold;">false</span>;
			opButton.<span style="color: #0066CC;">text</span>		= <span style="color: #ff0000;">&quot;BROWSE&quot;</span>;
			opButton.<span style="color: #006600;">x</span>			= <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageWidth</span> - opButton.<span style="color: #0066CC;">width</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">0.5</span>;
			opButton.<span style="color: #006600;">y</span>			= <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageHeight</span> - opButton.<span style="color: #0066CC;">height</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">0.5</span>;
			addChild<span style="color: #66cc66;">&#40;</span>opButton<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// ** button click listener **</span>
			opButton.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, on_buttonClick, <span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		* handle browse or save
		*/</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> on_buttonClick<span style="color: #66cc66;">&#40;</span>evt:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> btn:<span style="color: #0066CC;">TextField</span> = evt.<span style="color: #0066CC;">target</span> as <span style="color: #0066CC;">TextField</span>;
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>btn<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>btn.<span style="color: #0066CC;">text</span> == <span style="color: #ff0000;">&quot;BROWSE&quot;</span><span style="color: #66cc66;">&#41;</span>
				<span style="color: #66cc66;">&#123;</span>
					swfFile = <span style="color: #000000; font-weight: bold;">new</span> FileReference<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
					swfFile.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">SELECT</span>, on_swfSelect, <span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
					swfFile.<span style="color: #006600;">browse</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span><span style="color: #000000; font-weight: bold;">new</span> FileFilter<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Flash Movie&quot;</span>,<span style="color: #ff0000;">&quot;*.swf&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #66cc66;">&#125;</span>
				<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>btn.<span style="color: #0066CC;">text</span> == <span style="color: #ff0000;">&quot;SAVE&quot;</span><span style="color: #66cc66;">&#41;</span>
				<span style="color: #66cc66;">&#123;</span>
					<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>swfFile<span style="color: #66cc66;">&#41;</span>
					<span style="color: #66cc66;">&#123;</span>
						<span style="color: #808080; font-style: italic;">// ** BEGIN ROT128 **</span>
						<span style="color: #000000; font-weight: bold;">var</span> j:<span style="color: #0066CC;">int</span> = swfFile.<span style="color: #0066CC;">data</span>.<span style="color: #0066CC;">length</span>;
						<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>j--<span style="color: #66cc66;">&#41;</span>
						<span style="color: #66cc66;">&#123;</span>
							swfFile.<span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> += <span style="color: #cc66cc;">128</span>;
						<span style="color: #66cc66;">&#125;</span>
						<span style="color: #808080; font-style: italic;">// ** END ROT128 **</span>
						<span style="color: #000000; font-weight: bold;">new</span> FileReference<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span>swfFile.<span style="color: #0066CC;">data</span>, <span style="color: #ff0000;">&quot;ActualSWF.swf&quot;</span><span style="color: #66cc66;">&#41;</span>;
					<span style="color: #66cc66;">&#125;</span>
				<span style="color: #66cc66;">&#125;</span>
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		* handle browse, load swf file
		*/</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> on_swfSelect<span style="color: #66cc66;">&#40;</span>evt:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			swfFile.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">SELECT</span>, on_swfSelect<span style="color: #66cc66;">&#41;</span>;
			swfFile.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>, on_swfComplete, <span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
			swfFile.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		* handle load, change browse button to save button
		*/</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> on_swfComplete<span style="color: #66cc66;">&#40;</span>evt:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			swfFile.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>, on_swfComplete<span style="color: #66cc66;">&#41;</span>;
			opButton.<span style="color: #0066CC;">text</span>		= <span style="color: #ff0000;">&quot;SAVE&quot;</span>;
			opButton.<span style="color: #006600;">x</span>			= <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageWidth</span> - opButton.<span style="color: #0066CC;">width</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">0.5</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>	
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p><!-- --><br />
<strong>Step II: Embed Actual SWF In Shell SWF</strong><br />
If you have not done so already, please see <a href="http://www.ghostwire.com/blog/archives/as3-hiding-assets-and-code-by-embedding-swf-within-another-swf/">&#8220;Hiding Assets And Code By Embedding SWF Within Another SWF&#8221;</a> for the original discussion on how the technique works.</p>
<p>The following is a <code>MainShell</code> class you can use to embed the ROT128-encrypted SWF:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Loader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">utils</span>.<span style="color: #006600;">ByteArray</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MainShell <span style="color: #0066CC;">extends</span> Sprite 
	<span style="color: #66cc66;">&#123;</span>		
		<span style="color: #66cc66;">&#91;</span>Embed<span style="color: #66cc66;">&#40;</span>source=<span style="color: #ff0000;">&quot;ActualSWF.swf&quot;</span>, mimeType=<span style="color: #ff0000;">&quot;application/octet-stream&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> const bytes:<span style="color: #000000; font-weight: bold;">Class</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		* REMINDER
		* 
		* MAKE SURE THAT THIS SHELL SWF IS PUBLISHED USING THE
		* ORIGINAL WIDTH/HEIGHT DIMENSIONS OF THE EMBEDDED SWF
		*/</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> MainShell<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> swf:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> bytes<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> as ByteArray;
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>swf<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #808080; font-style: italic;">// ** BEGIN ROT128 **</span>
				<span style="color: #000000; font-weight: bold;">var</span> j:<span style="color: #0066CC;">int</span> = swf.<span style="color: #0066CC;">length</span>;
				<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>j--<span style="color: #66cc66;">&#41;</span>
				<span style="color: #66cc66;">&#123;</span>
					swf<span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> += <span style="color: #cc66cc;">128</span>;
				<span style="color: #66cc66;">&#125;</span>
				<span style="color: #808080; font-style: italic;">// ** END ROT128 **</span>
				Loader<span style="color: #66cc66;">&#40;</span>addChild<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Loader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">loadBytes</span><span style="color: #66cc66;">&#40;</span>swf<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>With the above code, the &#8220;ActualSWF.swf&#8221; file is embedded into the shell SWF.  It is then instantiated as a ByteArray during run-time.  ROT128 is then applied to the ByteArray before it is loaded into a Loader object.</p>
<p><!-- --><br />
<strong>Why Do This At All?</strong><br />
Hiding assets and code by embedding one SWF within another SWF is simple to implement and so far, there has not been any report on any decompiler overcoming the protection.</p>
<p>In the original post on the technique, I suggested that you could, if you wish, add another layer of protection by encrypting the embedded SWF just in case a decompiler may in future automatically identify a binary blob and &#8220;guess&#8221; that it is an embedded SWF (and decompile it separately).</p>
<p>I feel that ROT128 is light-weight and simple enough that can achieve that objective.  Remember that the objective here is not to overcome meticulous hacking, but merely to thwart possible attempts to implement <em>automatic</em> extraction and identification of embedded binary blobs as separate SWFs.</p>
<p>The above is only an example of how the concept can be applied.  As mentioned in <a href=""http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-bytearray/">the previous post on ROT128</a>, you can also consider applying ROT128 only to part of the file so that the logic for reversing the encryption could be a little less predictable.</p>
<p><!-- --><br />
<strong>Module SWFs</strong><br />
Although the above mentioned only embedded SWFs, you can definitely apply the same concept to module SWFs that will be loaded into your application during run-time.  By applying ROT128 to module SWFs, even if they are obtained via unauthorized means such as via the web browser&#8217;s cache, they are non-executable (or will just show a blank screen when run) if the ROT128 encryption is not reversed first.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-embeddedmodule-swfs/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

