<?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/"
	xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule"
>

<channel>
	<title>Tiago&#039;s Weblog &#187; Zinc</title>
	<atom:link href="http://blog.six4rty.ch/tag/zinc/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.six4rty.ch</link>
	<description>Code &#38; Technology Aficionado - Come for the Flash, stay for more!</description>
	<lastBuildDate>Sat, 24 Jul 2010 16:52:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
<creativeCommons:license>http://creativecommons.org/licenses/by/2.5/ch/</creativeCommons:license>
		<item>
		<title>Zinc &#8211; Saving images</title>
		<link>http://blog.six4rty.ch/2009/12/02/zinc-saving-images/</link>
		<comments>http://blog.six4rty.ch/2009/12/02/zinc-saving-images/#comments</comments>
		<pubDate>Wed, 02 Dec 2009 19:48:00 +0000</pubDate>
		<dc:creator>Tiago</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Personal]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[Zinc]]></category>

		<guid isPermaLink="false">http://blog.six4rty.ch/?p=616</guid>
		<description><![CDATA[It was obvious that I had to write a few more lines about Zinc in the near future, but somehow this topic didn&#8217;t leave me alone at all, and no, I&#8217;m not writing about Zinc because I like it, I&#8217;m writing this because I don&#8217;t want that other people fall into the traps I fell [...]]]></description>
			<content:encoded><![CDATA[<p>It was obvious that I had to write a few more lines about Zinc in the near future, but somehow this topic didn&#8217;t leave me alone at all, and no, I&#8217;m not writing about Zinc because I like it, I&#8217;m writing this because I don&#8217;t want that other people fall into the traps I fell while using it. Do I like Zinc? Yes &amp; No, it&#8217;s a very weird situation, it&#8217;s useful, it would be even more useful if they would do their job right, but <a href="http://blog.six4rty.ch/2009/11/29/zinc-a-love-hate-relationship/" target="_blank">I already wrote about it here</a>. So let&#8217;s leave this discussion out of here.</p>
<p>So let&#8217;s begin from the ground up, your goal is to resize and save the newly resized files back again into a jpeg. Sounds easy?<br />
It should be but it isn&#8217;t!!</p>
<p>First of all I have to tell you that I began my project with Zinc 3.0 which supports bytearrays, it was really cool to work with it, as I was able to use the JPEGEncoder class that anyway writes bytearrays.</p>
<p><span id="more-616"></span></p>
<p>Let&#8217;s look at the methods available:<br />
<em>setData();<br />
writeData();<br />
</em><em>setDataBA();<br />
writeDataBA();</em></p>
<p>You would achieve our goal somehow like this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> bmp:Bitmap = DisplayToBitmap.<span style="color: #006600;">draw</span><span style="color: #66cc66;">&#40;</span>bSprite, <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>;
<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>bmp.<span style="color: #0066CC;">width</span>, bmp.<span style="color: #0066CC;">height</span><span style="color: #66cc66;">&#41;</span>;
bmd.<span style="color: #006600;">draw</span><span style="color: #66cc66;">&#40;</span>bmp<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">var</span> jpgEnc:JPGEncoder = <span style="color: #000000; font-weight: bold;">new</span> JPGEncoder<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">85</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> jpgStream:ByteArray = jpgEnc.<span style="color: #006600;">encode</span><span style="color: #66cc66;">&#40;</span>bmd<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">var</span> jpgPath:<span style="color: #0066CC;">String</span> = persFolder + <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>My Pictures<span style="color: #000099; font-weight: bold;">\\</span>myScreensaverThumbs<span style="color: #000099; font-weight: bold;">\\</span>&quot;</span> + _arrayCCopy<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>;
&nbsp;
_convertedArray.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>jpgPath<span style="color: #66cc66;">&#41;</span>;
&nbsp;
mdm.<span style="color: #006600;">FileSystem</span>.<span style="color: #006600;">BinaryFile</span>.<span style="color: #006600;">setDataBA</span><span style="color: #66cc66;">&#40;</span>jpgStream<span style="color: #66cc66;">&#41;</span>;
mdm.<span style="color: #006600;">FileSystem</span>.<span style="color: #006600;">BinaryFile</span>.<span style="color: #006600;">writeDataBA</span><span style="color: #66cc66;">&#40;</span>jpgPath<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>But since I had to use Zinc 2.5 because of performance problems with the application, guess what? You don&#8217;t have the possibility to write a bytearray down to the harrdisk with the <em>writeDataBA()</em> method. So how the heck are you going to write a jpeg?</p>
<p>Let&#8217;s look at the methods available:<br />
<em>setData();<br />
writeData();</em></p>
<p>Yeh, it&#8217;s not much, and yeh it didn&#8217;t change a lot in Zinc 3.0 besides that you can write Bytearrays.<br />
So what you have to do is basicall write HEX code or Pipe ( | ) delimited strings, do that on a few dozen images and you can go get a cup of coffee and when you back it might be done by then.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> bmp:Bitmap = DisplayToBitmap.<span style="color: #006600;">draw</span><span style="color: #66cc66;">&#40;</span>sp, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> pngBA:ByteArray = PNGEncoder.<span style="color: #006600;">encode</span><span style="color: #66cc66;">&#40;</span>bmp.<span style="color: #006600;">bitmapData</span><span style="color: #66cc66;">&#41;</span>;
MonsterDebugger.<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>, pngBA<span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> dataString:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;&quot;</span>;
<span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i:uint=<span style="color: #cc66cc;">0</span>; i <span style="color: #66cc66;">&amp;</span>lt; pngBA.<span style="color: #0066CC;">length</span>; i++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&amp;</span>gt;<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
	dataString += <span style="color: #ff0000;">&quot;|&quot;</span>;
    <span style="color: #66cc66;">&#125;</span>
    dataString += pngBA<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>;
<span style="color: #66cc66;">&#125;</span>
mdm.<span style="color: #006600;">FileSystem</span>.<span style="color: #006600;">BinaryFile</span>.<span style="color: #006600;">setData</span><span style="color: #66cc66;">&#40;</span>dataString<span style="color: #66cc66;">&#41;</span>;
mdm.<span style="color: #006600;">FileSystem</span>.<span style="color: #006600;">BinaryFile</span>.<span style="color: #006600;">writeData</span><span style="color: #66cc66;">&#40;</span>persFolder + <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>My Pictures<span style="color: #000099; font-weight: bold;">\\</span>myScreensaverThumbs<span style="color: #000099; font-weight: bold;">\\</span>&quot;</span> + _arrayCCopy<span style="color: #66cc66;">&#91;</span>v<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Now that you know how to to handle this, let&#8217;s think about  a few methods that Zinc 2.5 API provides you with:</p>
<p><em>bmpToJpg();</em><br />
Nice little function but it only works if you have .bmp&#8217;s on your harddrive, who uses .bmp&#8217;s nowadays?</p>
<p><em>jpgToBmp();</em><br />
hmm.. sounds better, you can grab a jpeg and convert it to a .bmp file, after that you can use the <em>bmpToJpg()</em> method to save them back, but come on, does that really make sense? After all you are using 2 methods to do a simple conversion and resave.</p>
<p><em>deleteFile();</em><br />
Believe me or not, actually it&#8217;s quite obvious, you converted a jpeg to bmp and the bmp to jpeg, now you have<br />
1) an original file<br />
2) a bitmap file<br />
3) a converted file<br />
Isn&#8217;t that one too much?<br />
Yep, now you have to use the <em>deletefile() </em>method to delete the bmp file which you are definately not going to use.</p>
<p>Now you used 3 methods to accomplish a simple task, that can be done in pure as3 with less lines of code. Now you ask yourself  &#8220;Tiago you are talking about Zinc 2.5, that&#8217;s anyway outdated&#8221; have a look at the <a href="http://www.multidmedia.com/support/livedocs/" target="_blank">methods available for Zinc 3.0</a>. As you see they don&#8217;t have a simple &#8220;read jpeg / save jpeg&#8221; method. WHY NOT?????</p>
<p>But let&#8217;s continue, by now you know how to resize and resave images with Zinc 2.5 and Zinc 3.0, but you are probably not only going to convert one single image on the fly, that would be kind of boring no?</p>
<p>Let&#8217;s try to get a list of files from a specific directory, to accomplish that we need the <em>getFileList()</em> method, below a simple line on how to use 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> myFiles:<span style="color: #0066CC;">Array</span> = mdm.<span style="color: #006600;">FileSystem</span>.<span style="color: #006600;">getFileList</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;c:<span style="color: #000099; font-weight: bold;">\\</span>my images<span style="color: #000099; font-weight: bold;">\\</span>&quot;</span>, <span style="color: #ff0000;">&quot;*.jpg&quot;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Sounds simple, you build an array and call the getFileList method pointing to the location you wish and filtering it based on the file ending, at the end you don&#8217;t want to get some weird errors because you try to convert a .doc or whatsoever into a .bmp / .jpg</p>
<p>if you don&#8217;t believe me, then try this out on your own doesn&#8217;t matter which version of Zinc you own</p>
<ol>
<li>Create a folder on your c drive (it&#8217;s easier to target then anything else)</li>
<li>Drop a few images and rename the filetype of some of the files to .JPEG, .jpg, .jpeg (case sensitive)</li>
<li>Run the code above in your project using the path to the folder you just created</li>
</ol>
<p><strong>Result:</strong><br />
Only the files with the case sensitive .jpg will be pushed into the array, yeh now you know it, this damn thing is case sensitive, so let&#8217;s create some more arrays to handle the different filetypes and use some more processing time and memory then needed, the same goes for every filetype you want to push in. In my case I had to get all images with a filetype of (.jpg, .jpeg, .JPG, .JPEG, .bmp, .BMP, .png, .PNG) 8 querys on the filesystem all using the same method, sigh..</p>
<p>Have you ever ran into a Zinc problem before, let us hear your experience with it.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.six4rty.ch/2009/12/02/zinc-saving-images/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/2.5/ch/</creativeCommons:license>
	</item>
	</channel>
</rss>
