<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"
paddingBottom="2" paddingLeft="2" paddingRight="2" paddingTop="2"
width="620" height="474" verticalAlign="bottom"
applicationComplete="init();" verticalGap="1" viewSourceURL="swf/alchemy/alchemyJpegUpdate_sv/index.html">
<mx:Script>
<![CDATA[
import cn.elics.kyle.utils.JPGEncoder;
/***
asynchronous jpeg encoding
see it in action at :
http://segfaultlabs.com/blog/post/asynchronous-jpeg-encoding
author: Mateusz Malczak ( http://segfaultlabs.com )
code by Thibault Imbert ( bytearray.org )
Kyle Lu ( http://elics.cn/svn/kylesas3utils/trunk/ )
for more info visit : http://www.bytearray.org/?p=775
***/
import flash.utils.getTimer;
import cmodule.jpegencoder.CLibInit;
import org.bytearray.JPEGEncoder;
import mx.graphics.codec.JPEGEncoder;
private var lib : Object;
private var ba:ByteArray;
private var baout:ByteArray;
private var timer:uint;
private var ctimer:Timer;
private var bitmapData:BitmapData;
private var fname : String;
private function init():void
{
var init:CLibInit = new CLibInit(); lib = init.init();
baout = new ByteArray();
};
private function genImage():void
{
var w:uint = parseInt( widthIn.text );
if ( w > 2048 ) {
w = 2048;
widthIn.text = "2048";
};
var h:uint = parseInt( heightIn.text );
if ( h > 2048 ) {
h = 2048;
heightIn.text = "2048";
};
timer = flash.utils.getTimer();
if ( bitmapData ) bitmapData.dispose();
bitmapData = new BitmapData( w, h, false );
if ( perlinCB.selected )
bitmapData.perlinNoise( 10, 2, 8, Math.random(), true, false );
bitmapData.draw( this );
timer = flash.utils.getTimer() - timer;
memo1.text = "Generating "+bitmapData.width+"x"+bitmapData.height+" pixels bitmapData time : " + timer + "ms";
image0.source = new Bitmap( bitmapData );;
baoBtn.enabled = alchemyBtn.enabled = alchemyBtn2.enabled = binBtn.enabled = kyleBtn.enabled = true;
};
private function alchemyAsync():void
{
memo1.text += "\nJPEG compression start...";
ba = bitmapData.getPixels( bitmapData.rect );
ba.position = 0;
if ( baout )
baout.length = 0;
else
baout = new ByteArray();
btnBox.enabled = false;
timer = flash.utils.getTimer();
lib.encodeAsync( compressFinished, ba, baout, bitmapData.width, bitmapData.height, 80 );
};
private function compressFinished( out:ByteArray ):void
{
timer = flash.utils.getTimer() - timer;
ba.length = 0;
btnBox.enabled = true;
memo1.text += "\nJPEG compression time (alchemy async) : " + timer + "ms";
memo1.text += "\nJPEG size : " + baout.length;
fname = "jpeg_async_alchemy.jpg"
baout.position = 0;
saveBtn.label = "save alchemy ascyn jpeg";
saveBtn.enabled = true;
}
private function alchemySync():void
{
ba = bitmapData.getPixels( bitmapData.rect );
ba.position = 0;
if ( baout )
baout.length = 0;
else
baout = new ByteArray();
timer = flash.utils.getTimer();
lib.encode( ba, baout, bitmapData.width, bitmapData.height, 80 );
timer = flash.utils.getTimer() - timer;
memo1.text += "\nJPEG compression time (alchemy sync) : " + timer + "ms";
memo1.text += "\nJPEG size : " + baout.length;
fname = "jpeg_sync_alchemy.jpg"
saveBtn.label = "save alchemy sync jpeg";
saveBtn.enabled = true;
};
private function runByteArrayORG():void
{
var jenc:org.bytearray.JPEGEncoder = new org.bytearray.JPEGEncoder(80);
baout.length = 0;
baout = null;
timer = flash.utils.getTimer();
baout = jenc.encode( bitmapData );
timer = flash.utils.getTimer() - timer;
memo1.text += "\nJPEG compression time (bytearray.org) : " + timer + "ms";
memo1.text += "\nJPEG size : " + baout.length;
fname = "jpeg_bytearray_org.jpg"
saveBtn.label = "save bytearray.org jpeg";
saveBtn.enabled = true;
};
private function runMX():void
{
var jenc:mx.graphics.codec.JPEGEncoder = new mx.graphics.codec.JPEGEncoder(80);
baout.length = 0;
baout = null;
timer = flash.utils.getTimer();
baout = jenc.encode( bitmapData );
timer = flash.utils.getTimer() - timer;
memo1.text += "\nJPEG compression time (mx.*) : " + timer + "ms";
memo1.text += "\nJPEG size : " + baout.length;
fname = "jpeg_mx_graphics.jpg"
saveBtn.label = "save bytearray.org jpeg";
saveBtn.enabled = true;
}
private function runKyleLu():void
{
baout.length = 0;
baout = null;
var encoder:cn.elics.kyle.utils.JPGEncoder = new cn.elics.kyle.utils.JPGEncoder(80);
timer = flash.utils.getTimer();
baout = encoder.encode( bitmapData );
timer = flash.utils.getTimer() - timer;
memo1.text += "\nJPEG compression time (Kyle Lu) : " + timer + "ms";
memo1.text += "\nJPEG size : " + baout.length;
fname = "jpeg_kyle_elics_cn.jpg"
saveBtn.label = "save Kyle Lu jpeg";
saveBtn.enabled = true;
}
private function saveJpeg():void
{
var f:FileReference = new FileReference();
baout.position = 0;
f.save( baout, fname );
};
]]>
</mx:Script>
<mx:Text width="100%" textAlign="center">
<mx:htmlText><![CDATA[<b>asynchronous JPEG Encoding with Alchemy</b>
compared to optimized JPEGEncoder (<a href='http://www.bytearray.org/?p=775'>http://www.bytearray.org/?p=775</a>)
Mateusz Malczak (http://segfaultlabs.com)]]></mx:htmlText>
</mx:Text>
<mx:Image width="320" height="200" id="image0" scaleContent="true"/>
<mx:HBox width="100%" horizontalAlign="center">
<mx:TextArea width="352" height="100%" id="memo1" />
<mx:VBox id="btnBox" horizontalAlign="center" verticalGap="3">
<mx:HBox>
<mx:TextInput id="widthIn" maxChars="4" restrict="0-9" text="1024" />
<mx:Label text="x" />
<mx:TextInput id="heightIn" maxChars="4" restrict="0-9" text="1024" />
</mx:HBox>
<mx:CheckBox id="perlinCB" label="add Perlin noise" selected="true" />
<mx:Button id="genImgBtn" label="generate image" click="genImage()" />
<mx:Button id="binBtn" enabled="false" label="create jpeg (mx.*)" click="runMX()"/>
<mx:Button id="kyleBtn" enabled="false" label="create jpeg (Kyle Lu encoder)" click="runKyleLu()"/>
<mx:Button id="baoBtn" enabled="false" label="create jpeg (bytearray.org)" click="runByteArrayORG()"/>
<mx:Button id="alchemyBtn" enabled="false" label="create jpeg (asynchronous alchemy)" click="alchemyAsync()"/>
<mx:Button id="alchemyBtn2" enabled="false" label="create jpeg (synchronous alchemy)" click="alchemySync()"/>
<mx:Button id="saveBtn" enabled="false" label="save jpeg" click="saveJpeg()" />
</mx:VBox>
</mx:HBox>
</mx:Application>