2 Copyright (c) 2008, Adobe Systems Incorporated
\r
5 Redistribution and use in source and binary forms, with or without
\r
6 modification, are permitted provided that the following conditions are
\r
9 * Redistributions of source code must retain the above copyright notice,
\r
10 this list of conditions and the following disclaimer.
\r
12 * Redistributions in binary form must reproduce the above copyright
\r
13 notice, this list of conditions and the following disclaimer in the
\r
14 documentation and/or other materials provided with the distribution.
\r
16 * Neither the name of Adobe Systems Incorporated nor the names of its
\r
17 contributors may be used to endorse or promote products derived from
\r
18 this software without specific prior written permission.
\r
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
\r
21 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
\r
22 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
\r
23 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
\r
24 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
\r
25 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
\r
26 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
\r
27 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
\r
28 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
\r
29 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
\r
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
32 package com.adobe.images
\r
34 import flash.geom.*;
\r
35 import flash.display.Bitmap;
\r
36 import flash.display.BitmapData;
\r
37 import flash.utils.ByteArray;
\r
40 * Class that converts BitmapData into a valid PNG
\r
42 public class PNGEncoder
\r
45 * Created a PNG image from the specified BitmapData
\r
47 * @param image The BitmapData that will be converted into the PNG format.
\r
48 * @return a ByteArray representing the PNG encoded image data.
\r
49 * @langversion ActionScript 3.0
\r
50 * @playerversion Flash 9.0
\r
53 public static function encode(img:BitmapData):ByteArray {
\r
54 // Create output byte array
\r
55 var png:ByteArray = new ByteArray();
\r
56 // Write PNG signature
\r
57 png.writeUnsignedInt(0x89504e47);
\r
58 png.writeUnsignedInt(0x0D0A1A0A);
\r
60 var IHDR:ByteArray = new ByteArray();
\r
61 IHDR.writeInt(img.width);
\r
62 IHDR.writeInt(img.height);
\r
63 IHDR.writeUnsignedInt(0x08060000); // 32bit RGBA
\r
65 writeChunk(png,0x49484452,IHDR);
\r
67 var IDAT:ByteArray= new ByteArray();
\r
68 for(var i:int=0;i < img.height;i++) {
\r
73 if ( !img.transparent ) {
\r
74 for(j=0;j < img.width;j++) {
\r
75 p = img.getPixel(j,i);
\r
76 IDAT.writeUnsignedInt(
\r
77 uint(((p&0xFFFFFF) << 8)|0xFF));
\r
80 for(j=0;j < img.width;j++) {
\r
81 p = img.getPixel32(j,i);
\r
82 IDAT.writeUnsignedInt(
\r
83 uint(((p&0xFFFFFF) << 8)|
\r
89 writeChunk(png,0x49444154,IDAT);
\r
91 writeChunk(png,0x49454E44,null);
\r
96 private static var crcTable:Array;
\r
97 private static var crcTableComputed:Boolean = false;
\r
99 private static function writeChunk(png:ByteArray,
\r
100 type:uint, data:ByteArray):void {
\r
101 if (!crcTableComputed) {
\r
102 crcTableComputed = true;
\r
105 for (var n:uint = 0; n < 256; n++) {
\r
107 for (var k:uint = 0; k < 8; k++) {
\r
109 c = uint(uint(0xedb88320) ^
\r
119 if (data != null) {
\r
122 png.writeUnsignedInt(len);
\r
123 var p:uint = png.position;
\r
124 png.writeUnsignedInt(type);
\r
125 if ( data != null ) {
\r
126 png.writeBytes(data);
\r
128 var e:uint = png.position;
\r
131 for (var i:int = 0; i < (e-p); i++) {
\r
133 (c ^ png.readUnsignedByte()) &
\r
134 uint(0xff)] ^ uint(c >>> 8));
\r
136 c = uint(c^uint(0xffffffff));
\r
138 png.writeUnsignedInt(c);
\r