Complete serialization with pretty printing. Works both in FF and Webkit. This fixes...
[redakcja.git] / platforma / static / filebrowser / uploadify / com / adobe / images / PNGEncoder.as
1 /*\r
2   Copyright (c) 2008, Adobe Systems Incorporated\r
3   All rights reserved.\r
4 \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
7   met:\r
8 \r
9   * Redistributions of source code must retain the above copyright notice, \r
10     this list of conditions and the following disclaimer.\r
11   \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
15   \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
19 \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
31 */\r
32 package com.adobe.images\r
33 {\r
34         import flash.geom.*;\r
35         import flash.display.Bitmap;\r
36         import flash.display.BitmapData;\r
37         import flash.utils.ByteArray;\r
38 \r
39         /**\r
40          * Class that converts BitmapData into a valid PNG\r
41          */     \r
42         public class PNGEncoder\r
43         {\r
44                 /**\r
45                  * Created a PNG image from the specified BitmapData\r
46                  *\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
51                  * @tiptext\r
52                  */                     \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
59                 // Build IHDR chunk\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
64                 IHDR.writeByte(0);\r
65                 writeChunk(png,0x49484452,IHDR);\r
66                 // Build IDAT chunk\r
67                 var IDAT:ByteArray= new ByteArray();\r
68                 for(var i:int=0;i < img.height;i++) {\r
69                     // no filter\r
70                     IDAT.writeByte(0);\r
71                     var p:uint;\r
72                     var j:int;\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
78                         }\r
79                     } else {\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
84                                 (p>>>24)));\r
85                         }\r
86                     }\r
87                 }\r
88                 IDAT.compress();\r
89                 writeChunk(png,0x49444154,IDAT);\r
90                 // Build IEND chunk\r
91                 writeChunk(png,0x49454E44,null);\r
92                 // return PNG\r
93                 return png;\r
94             }\r
95         \r
96             private static var crcTable:Array;\r
97             private static var crcTableComputed:Boolean = false;\r
98         \r
99             private static function writeChunk(png:ByteArray, \r
100                     type:uint, data:ByteArray):void {\r
101                 if (!crcTableComputed) {\r
102                     crcTableComputed = true;\r
103                     crcTable = [];\r
104                     var c:uint;\r
105                     for (var n:uint = 0; n < 256; n++) {\r
106                         c = n;\r
107                         for (var k:uint = 0; k < 8; k++) {\r
108                             if (c & 1) {\r
109                                 c = uint(uint(0xedb88320) ^ \r
110                                     uint(c >>> 1));\r
111                             } else {\r
112                                 c = uint(c >>> 1);\r
113                             }\r
114                         }\r
115                         crcTable[n] = c;\r
116                     }\r
117                 }\r
118                 var len:uint = 0;\r
119                 if (data != null) {\r
120                     len = data.length;\r
121                 }\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
127                 }\r
128                 var e:uint = png.position;\r
129                 png.position = p;\r
130                 c = 0xffffffff;\r
131                 for (var i:int = 0; i < (e-p); i++) {\r
132                     c = uint(crcTable[\r
133                         (c ^ png.readUnsignedByte()) & \r
134                         uint(0xff)] ^ uint(c >>> 8));\r
135                 }\r
136                 c = uint(c^uint(0xffffffff));\r
137                 png.position = e;\r
138                 png.writeUnsignedInt(c);\r
139             }\r
140         }\r
141 }