+/*\r
+ Copyright (c) 2008, Adobe Systems Incorporated\r
+ All rights reserved.\r
+\r
+ Redistribution and use in source and binary forms, with or without \r
+ modification, are permitted provided that the following conditions are\r
+ met:\r
+\r
+ * Redistributions of source code must retain the above copyright notice, \r
+ this list of conditions and the following disclaimer.\r
+ \r
+ * Redistributions in binary form must reproduce the above copyright\r
+ notice, this list of conditions and the following disclaimer in the \r
+ documentation and/or other materials provided with the distribution.\r
+ \r
+ * Neither the name of Adobe Systems Incorporated nor the names of its \r
+ contributors may be used to endorse or promote products derived from \r
+ this software without specific prior written permission.\r
+\r
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS\r
+ IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,\r
+ THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\r
+ PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR \r
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,\r
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,\r
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\r
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\r
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+*/\r
+package com.adobe.images\r
+{\r
+ import flash.geom.*;\r
+ import flash.display.Bitmap;\r
+ import flash.display.BitmapData;\r
+ import flash.utils.ByteArray;\r
+\r
+ /**\r
+ * Class that converts BitmapData into a valid PNG\r
+ */ \r
+ public class PNGEncoder\r
+ {\r
+ /**\r
+ * Created a PNG image from the specified BitmapData\r
+ *\r
+ * @param image The BitmapData that will be converted into the PNG format.\r
+ * @return a ByteArray representing the PNG encoded image data.\r
+ * @langversion ActionScript 3.0\r
+ * @playerversion Flash 9.0\r
+ * @tiptext\r
+ */ \r
+ public static function encode(img:BitmapData):ByteArray {\r
+ // Create output byte array\r
+ var png:ByteArray = new ByteArray();\r
+ // Write PNG signature\r
+ png.writeUnsignedInt(0x89504e47);\r
+ png.writeUnsignedInt(0x0D0A1A0A);\r
+ // Build IHDR chunk\r
+ var IHDR:ByteArray = new ByteArray();\r
+ IHDR.writeInt(img.width);\r
+ IHDR.writeInt(img.height);\r
+ IHDR.writeUnsignedInt(0x08060000); // 32bit RGBA\r
+ IHDR.writeByte(0);\r
+ writeChunk(png,0x49484452,IHDR);\r
+ // Build IDAT chunk\r
+ var IDAT:ByteArray= new ByteArray();\r
+ for(var i:int=0;i < img.height;i++) {\r
+ // no filter\r
+ IDAT.writeByte(0);\r
+ var p:uint;\r
+ var j:int;\r
+ if ( !img.transparent ) {\r
+ for(j=0;j < img.width;j++) {\r
+ p = img.getPixel(j,i);\r
+ IDAT.writeUnsignedInt(\r
+ uint(((p&0xFFFFFF) << 8)|0xFF));\r
+ }\r
+ } else {\r
+ for(j=0;j < img.width;j++) {\r
+ p = img.getPixel32(j,i);\r
+ IDAT.writeUnsignedInt(\r
+ uint(((p&0xFFFFFF) << 8)|\r
+ (p>>>24)));\r
+ }\r
+ }\r
+ }\r
+ IDAT.compress();\r
+ writeChunk(png,0x49444154,IDAT);\r
+ // Build IEND chunk\r
+ writeChunk(png,0x49454E44,null);\r
+ // return PNG\r
+ return png;\r
+ }\r
+ \r
+ private static var crcTable:Array;\r
+ private static var crcTableComputed:Boolean = false;\r
+ \r
+ private static function writeChunk(png:ByteArray, \r
+ type:uint, data:ByteArray):void {\r
+ if (!crcTableComputed) {\r
+ crcTableComputed = true;\r
+ crcTable = [];\r
+ var c:uint;\r
+ for (var n:uint = 0; n < 256; n++) {\r
+ c = n;\r
+ for (var k:uint = 0; k < 8; k++) {\r
+ if (c & 1) {\r
+ c = uint(uint(0xedb88320) ^ \r
+ uint(c >>> 1));\r
+ } else {\r
+ c = uint(c >>> 1);\r
+ }\r
+ }\r
+ crcTable[n] = c;\r
+ }\r
+ }\r
+ var len:uint = 0;\r
+ if (data != null) {\r
+ len = data.length;\r
+ }\r
+ png.writeUnsignedInt(len);\r
+ var p:uint = png.position;\r
+ png.writeUnsignedInt(type);\r
+ if ( data != null ) {\r
+ png.writeBytes(data);\r
+ }\r
+ var e:uint = png.position;\r
+ png.position = p;\r
+ c = 0xffffffff;\r
+ for (var i:int = 0; i < (e-p); i++) {\r
+ c = uint(crcTable[\r
+ (c ^ png.readUnsignedByte()) & \r
+ uint(0xff)] ^ uint(c >>> 8));\r
+ }\r
+ c = uint(c^uint(0xffffffff));\r
+ png.position = e;\r
+ png.writeUnsignedInt(c);\r
+ }\r
+ }\r
+}
\ No newline at end of file