<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/halo" minWidth="520" minHeight="300" width="520" height="300" backgroundColor="#ffffff" viewSourceURL="srcview/index.html">
    <s:layout>
        <s:BasicLayout/>
    </s:layout>
    <fx:Script>
        <![CDATA[
            // Alchemy jpegencoder
            import cmodule.jpegencoder.CLibInit;
            
            // cmodule.jpegencoder instance
            private var jpegEncoder:Object;
            
            private var ba:ByteArray;
            private var baout:ByteArray;
            private var bitmapData:BitmapData;
            
            private function init ():void
            {
                // Alchemy jpegencoder initialize
                var init:cmodule.jpegencoder.CLibInit = new cmodule.jpegencoder.CLibInit();
                jpegEncoder = init.init();
                
                baout = new ByteArray();
            }
            // capture image
            private function captureImage ():void
            {
                if ( !jpegEncoder )
                    init();
                
                var target:Image = pngimage;
                var rect:Rectangle = target.getBounds(this);
                if ( bitmapData )
                    bitmapData.dispose();
                
                bitmapData = new BitmapData(rect.width, rect.height); 
                bitmapData.draw(target);
                
                encodeJPEG();
            }
            // encode
            private function encodeJPEG ():void
            {
                ba = bitmapData.getPixels(bitmapData.rect);
                ba.position = 0;
                if (baout )
                    baout.length = 0;
                else
                    baout = new ByteArray();
                
                //jpegEncoder.encodeAsync(onEncodeJPEGFinished, ba, baout, bitmapData.width, bitmapData.height, 80);
                
                // sync
                jpegEncoder.encode(ba, baout, bitmapData.width, bitmapData.height, 95);                
                saveImage();
            }
            // async complete handler
            private function onEncodeJPEGFinished (out:ByteArray):void
            {
                ba.length = 0;
                baout.position = 0;
                saveImage();
            }
            // save image by FileReference
            private function saveImage ():void
            {
                var fileName:String = "alchemy_encodejpeg.jpg";
                baout.position = 0;
                (new FileReference()).save(baout, fileName);
            }
            // Button Handler
            protected function button1_clickHandler(event:MouseEvent):void
            {
                captureImage();
            }
        ]]>
    </fx:Script>
    <fx:Style source="JPEGEncodeGumbo.css"/>

    <mx:Image x="10" y="10" width="500" height="250" source="img0.png" autoLoad="true" scaleContent="false" id="pngimage"/>
    <s:Button label="Save Image" enabled="true" click="button1_clickHandler(event)" color="#2C2C2C" x="417" y="271" fontFamily="Georgia" id="saveImage_btn" focusColor="#1473BC" toolTip="Convert to JPEG" focusEnabled="true"/>
    
</s:Application>