Project

General

Profile

Statistics
| Branch: | Revision:

colonymech / docs / www / colonyscout / internal / includes / uploadify / com / adobe / images / PNGEncoder.as @ f59acf11

History | View | Annotate | Download (4.92 KB)

1
/*
2
  Copyright (c) 2008, Adobe Systems Incorporated
3
  All rights reserved.
4

    
5
  Redistribution and use in source and binary forms, with or without 
6
  modification, are permitted provided that the following conditions are
7
  met:
8

    
9
  * Redistributions of source code must retain the above copyright notice, 
10
    this list of conditions and the following disclaimer.
11
  
12
  * Redistributions in binary form must reproduce the above copyright
13
    notice, this list of conditions and the following disclaimer in the 
14
    documentation and/or other materials provided with the distribution.
15
  
16
  * Neither the name of Adobe Systems Incorporated nor the names of its 
17
    contributors may be used to endorse or promote products derived from 
18
    this software without specific prior written permission.
19

    
20
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21
  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22
  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23
  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
24
  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25
  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26
  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27
  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28
  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29
  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
*/
32
package com.adobe.images
33
{
34
	import flash.geom.*;
35
	import flash.display.Bitmap;
36
	import flash.display.BitmapData;
37
	import flash.utils.ByteArray;
38

    
39
	/**
40
	 * Class that converts BitmapData into a valid PNG
41
	 */	
42
	public class PNGEncoder
43
	{
44
		/**
45
		 * Created a PNG image from the specified BitmapData
46
		 *
47
		 * @param image The BitmapData that will be converted into the PNG format.
48
		 * @return a ByteArray representing the PNG encoded image data.
49
		 * @langversion ActionScript 3.0
50
		 * @playerversion Flash 9.0
51
		 * @tiptext
52
		 */			
53
	    public static function encode(img:BitmapData):ByteArray {
54
	        // Create output byte array
55
	        var png:ByteArray = new ByteArray();
56
	        // Write PNG signature
57
	        png.writeUnsignedInt(0x89504e47);
58
	        png.writeUnsignedInt(0x0D0A1A0A);
59
	        // Build IHDR chunk
60
	        var IHDR:ByteArray = new ByteArray();
61
	        IHDR.writeInt(img.width);
62
	        IHDR.writeInt(img.height);
63
	        IHDR.writeUnsignedInt(0x08060000); // 32bit RGBA
64
	        IHDR.writeByte(0);
65
	        writeChunk(png,0x49484452,IHDR);
66
	        // Build IDAT chunk
67
	        var IDAT:ByteArray= new ByteArray();
68
	        for(var i:int=0;i < img.height;i++) {
69
	            // no filter
70
	            IDAT.writeByte(0);
71
	            var p:uint;
72
	            var j:int;
73
	            if ( !img.transparent ) {
74
	                for(j=0;j < img.width;j++) {
75
	                    p = img.getPixel(j,i);
76
	                    IDAT.writeUnsignedInt(
77
	                        uint(((p&0xFFFFFF) << 8)|0xFF));
78
	                }
79
	            } else {
80
	                for(j=0;j < img.width;j++) {
81
	                    p = img.getPixel32(j,i);
82
	                    IDAT.writeUnsignedInt(
83
	                        uint(((p&0xFFFFFF) << 8)|
84
	                        (p>>>24)));
85
	                }
86
	            }
87
	        }
88
	        IDAT.compress();
89
	        writeChunk(png,0x49444154,IDAT);
90
	        // Build IEND chunk
91
	        writeChunk(png,0x49454E44,null);
92
	        // return PNG
93
	        return png;
94
	    }
95
	
96
	    private static var crcTable:Array;
97
	    private static var crcTableComputed:Boolean = false;
98
	
99
	    private static function writeChunk(png:ByteArray, 
100
	            type:uint, data:ByteArray):void {
101
	        if (!crcTableComputed) {
102
	            crcTableComputed = true;
103
	            crcTable = [];
104
	            var c:uint;
105
	            for (var n:uint = 0; n < 256; n++) {
106
	                c = n;
107
	                for (var k:uint = 0; k < 8; k++) {
108
	                    if (c & 1) {
109
	                        c = uint(uint(0xedb88320) ^ 
110
	                            uint(c >>> 1));
111
	                    } else {
112
	                        c = uint(c >>> 1);
113
	                    }
114
	                }
115
	                crcTable[n] = c;
116
	            }
117
	        }
118
	        var len:uint = 0;
119
	        if (data != null) {
120
	            len = data.length;
121
	        }
122
	        png.writeUnsignedInt(len);
123
	        var p:uint = png.position;
124
	        png.writeUnsignedInt(type);
125
	        if ( data != null ) {
126
	            png.writeBytes(data);
127
	        }
128
	        var e:uint = png.position;
129
	        png.position = p;
130
	        c = 0xffffffff;
131
	        for (var i:int = 0; i < (e-p); i++) {
132
	            c = uint(crcTable[
133
	                (c ^ png.readUnsignedByte()) & 
134
	                uint(0xff)] ^ uint(c >>> 8));
135
	        }
136
	        c = uint(c^uint(0xffffffff));
137
	        png.position = e;
138
	        png.writeUnsignedInt(c);
139
	    }
140
	}
141
}