<?xml version="1.0" encoding="utf-8"?>
<mx:Application layout="vertical"
     xmlns:mx="http://www.adobe.com/2006/mxml"
     backgroundColor="#FFFFFF"
     horizontalAlign="left" viewSourceURL="srcview/index.html">
    
    <mx:Style source="global.css"/>
    
    <mx:Script>
        <![CDATA[
            import mx.graphics.codec.*;
            
            private var _fLoad:FileReference;
            private var _fSave:FileReference;
            private var _fName:String = "";
            private var _loader:Loader;
            private var _icon:ByteArray;
            
            private function browse():void
            {
                if(!_fLoad)
                {
                    _fLoad = new FileReference();
                    _fLoad.addEventListener(Event.SELECT, onSelect);
                    _fLoad.addEventListener(Event.COMPLETE, onLocalFileRead);
                }
                
                var ft:FileFilter = null;
                ft = new FileFilter("image files","*.gif;*.jpg;*.png;*.bmp");
                
                _fLoad.browse([ft]);
            }
            
            private function onSelect(e:Event):void
            {
                if(!_loader)
                {
                    _loader = new Loader();
                    _loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadBytes);
                }
                _fLoad.load();
            }
            
            private function onLocalFileRead(e:Event):void
            {
                var context:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain, null);
                var b:ByteArray = _fLoad.data;
                _loader.loadBytes(b, context);
            }
            
            private function onLoadBytes(e:Event):void
            {
                var ld:Loader = _loader;
                img.source = _loader.content;
                
                callLater(onImageLoad);
            }
            
            private function onImageLoad():void
            {
                var bd:BitmapData = new BitmapData(img.width, img.height, true, 0x00FFFF);
                bd.draw(img);
                
                var bm:Bitmap = new Bitmap(bd.clone(), PixelSnapping.AUTO, true);
                iconImage16.source = bm;
                
                bm = new Bitmap(bd.clone(), PixelSnapping.AUTO, true);
                iconImage24.source = bm;

                bm = new Bitmap(bd.clone(), PixelSnapping.AUTO, true);
                iconImage32.source = bm;

                bm = new Bitmap(bd.clone(), PixelSnapping.AUTO, true);
                iconImage48.source = bm;

                bm = new Bitmap(bd.clone(), PixelSnapping.AUTO, true);
                iconImage256.source = bm;
                
                callLater(convert);
            }
            
            private function convert():void
            {
                var png:PNGEncoder = new PNGEncoder();
                var bds:Array = new Array();
                var bd:BitmapData = new BitmapData(16,16,true,0x00FFFFFF);
                bd.draw(cvs16);
                bds.push(bd);
                
                bd = new BitmapData(24,24,true,0x00FFFFFF);
                bd.draw(cvs24);
                bds.push(bd);

                bd = new BitmapData(32,32,true,0x00000000);
                bd.draw(cvs32);
                bds.push(bd);

                bd = new BitmapData(48,48,true,0x00000000);
                bd.draw(cvs48);
                bds.push(bd);

                bd = new BitmapData(256,256,true,0x00000000);
                bd.draw(cvs256);
                bds.push(bd);
                
                // icon format is here
                // http://en.wikipedia.org/wiki/ICO_(icon_image_file_format)
                
                var icon:ByteArray = new ByteArray();
                icon.endian = Endian.LITTLE_ENDIAN;
                
                // header
                //    reserved (00-01)
                icon.writeByte(0);
                icon.writeByte(0);
                //    type 1:icon, 2:cursor (02-03)
                icon.writeByte(1);
                icon.writeByte(0);
                //    image count (04-05)
                icon.writeByte(5);
                icon.writeByte(0);
                
                var pngData:ByteArray = null;
                var iconDatas:Array = new Array();
                
                // header length is 6
                // one directory length is 16
                // if there is only an image, this should be 22
                var dataPos:int = 6 + 16*bds.length;
                
                for each(bd in bds)
                {
                    pngData = png.encode(bd);
                    pngData.position = 0;
                    pngData.endian = Endian.LITTLE_ENDIAN;
                    iconDatas.push(pngData);
                    
                    // directory
                    //    width (06)
                    icon.writeByte(bd.width);
                    //    height (07)
                    icon.writeByte(bd.height);
                    //    color (08)
                    icon.writeByte(0);
                    //    reserved (09)
                    icon.writeByte(0);
                    //    color planes (0a-0b)
                    icon.writeByte(1);
                    icon.writeByte(0);
                    //    bit per pixel (0c-0d)
                    icon.writeByte(32);
                    icon.writeByte(0);
                    //    size in bytes of the bitmap data (0e-11)
                    icon.writeInt(pngData.length);
                    //    offset of icon data (12-15)
                    icon.writeInt(dataPos);
                    dataPos += pngData.length;
                }
                
                for each(pngData in iconDatas)
                {
                    //  image data
                    icon.writeBytes(pngData, 0, pngData.length);
                }
                
                _icon = icon;
                _fName = _fLoad.name;
            }
            
            private function saveIcon():void
            {
                if(!_fSave)
                {
                    _fSave = new FileReference();
                    _fSave.addEventListener(Event.COMPLETE, onSave);
                }
                
                var fn:String = _fName.substring(0,_fName.lastIndexOf(".")) + ".ico";
                _fSave.save(_icon, fn);
            }
            
            private function onSave(e:Event):void
            {
                trace("saved");
            }
            
        ]]>
    </mx:Script>
    
    <mx:ApplicationControlBar dock="true">
        <mx:Label text="IconMaker Simple edition"/>
        <mx:Spacer width="100"/>
        <mx:Button label="open a local image" fontSize="14"
             click="browse()"/>
        <mx:Button label="save as an icon" fontSize="14"
             click="saveIcon()"/>
    </mx:ApplicationControlBar>
    
    <mx:HBox fontSize="16" fontWeight="bold">
        <mx:VBox>
            <mx:Label text="Original image"/>
            
            <mx:HRule width="100%" height="10"/>
            
            <mx:Image id="img"/>
        </mx:VBox>
        
        <mx:VRule height="100%"/>
        
        <mx:VBox horizontalAlign="center">
            <mx:Label text="temporary images to convert"/>
            
            <mx:HRule width="100%" height="10"/>
            
            <mx:Label text="icon : 16 x 16"/>
            <mx:Canvas id="cvs16" width="16" height="16"
                 backgroundColor="#FFFFFF">
                <mx:Image id="iconImage16" width="16" height="16"
                     horizontalAlign="center" verticalAlign="middle"/>
            </mx:Canvas>

            <mx:Label text="icon : 24 x 24"/>
            <mx:Canvas id="cvs24" width="24" height="24"
                 backgroundColor="#FFFFFF">
                <mx:Image id="iconImage24" width="24" height="24"
                     horizontalAlign="center" verticalAlign="middle"
                     horizontalCenter="0" verticalCenter="0"/>
            </mx:Canvas>

            <mx:Label text="icon : 32 x 32"/>
            <mx:Canvas id="cvs32" width="32" height="32"
                 backgroundColor="#FFFFFF">
                <mx:Image id="iconImage32" width="32" height="32"
                     horizontalAlign="center" verticalAlign="middle"
                     horizontalCenter="0" verticalCenter="0"/>
            </mx:Canvas>

            <mx:Label text="icon : 48 x 48"/>
            <mx:Canvas id="cvs48" width="48" height="48"
                 backgroundColor="#FFFFFF">
                <mx:Image id="iconImage48" width="48" height="48"
                     horizontalAlign="center" verticalAlign="middle"
                     horizontalCenter="0" verticalCenter="0"/>
            </mx:Canvas>

            <mx:Label text="icon : 256 x 256"/>
            <mx:Canvas id="cvs256" width="256" height="256"
                 backgroundColor="#FFFFFF">
                <mx:Image id="iconImage256" width="256" height="256"
                     horizontalAlign="center" verticalAlign="middle"
                     horizontalCenter="0" verticalCenter="0"/>
            </mx:Canvas>
            
        </mx:VBox>
    </mx:HBox>
    
    
</mx:Application>