[7849] | 1 | /*
|
---|
| 2 | Adobe Systems Incorporated(r) Source Code License Agreement
|
---|
| 3 | Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved.
|
---|
| 4 |
|
---|
| 5 | Please read this Source Code License Agreement carefully before using
|
---|
| 6 | the source code.
|
---|
| 7 |
|
---|
| 8 | Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive,
|
---|
| 9 | no-charge, royalty-free, irrevocable copyright license, to reproduce,
|
---|
| 10 | prepare derivative works of, publicly display, publicly perform, and
|
---|
| 11 | distribute this source code and such derivative works in source or
|
---|
| 12 | object code form without any attribution requirements.
|
---|
| 13 |
|
---|
| 14 | The name "Adobe Systems Incorporated" must not be used to endorse or promote products
|
---|
| 15 | derived from the source code without prior written permission.
|
---|
| 16 |
|
---|
| 17 | You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and
|
---|
| 18 | against any loss, damage, claims or lawsuits, including attorney's
|
---|
| 19 | fees that arise or result from your use or distribution of the source
|
---|
| 20 | code.
|
---|
| 21 |
|
---|
| 22 | THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT
|
---|
| 23 | ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
|
---|
| 24 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
---|
| 25 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF
|
---|
| 26 | NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA
|
---|
| 27 | OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
---|
| 28 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
---|
| 29 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
---|
| 30 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
---|
| 31 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
---|
| 32 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF
|
---|
| 33 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 34 | */
|
---|
| 35 | package com.adobe.images
|
---|
| 36 | {
|
---|
| 37 | import flash.geom.*;
|
---|
| 38 | import flash.display.Bitmap;
|
---|
| 39 | import flash.display.BitmapData;
|
---|
| 40 | import flash.utils.ByteArray;
|
---|
| 41 |
|
---|
| 42 | /**
|
---|
| 43 | * Class that converts BitmapData into a valid PNG
|
---|
| 44 | */
|
---|
| 45 | public class PNGEncoder
|
---|
| 46 | {
|
---|
| 47 | /**
|
---|
| 48 | * Created a PNG image from the specified BitmapData
|
---|
| 49 | *
|
---|
| 50 | * @param image The BitmapData that will be converted into the PNG format.
|
---|
| 51 | * @return a ByteArray representing the PNG encoded image data.
|
---|
| 52 | * @langversion ActionScript 3.0
|
---|
| 53 | * @playerversion Flash 9.0
|
---|
| 54 | * @tiptext
|
---|
| 55 | */
|
---|
| 56 | public static function encode(img:BitmapData):ByteArray {
|
---|
| 57 | // Create output byte array
|
---|
| 58 | var png:ByteArray = new ByteArray();
|
---|
| 59 | // Write PNG signature
|
---|
| 60 | png.writeUnsignedInt(0x89504e47);
|
---|
| 61 | png.writeUnsignedInt(0x0D0A1A0A);
|
---|
| 62 | // Build IHDR chunk
|
---|
| 63 | var IHDR:ByteArray = new ByteArray();
|
---|
| 64 | IHDR.writeInt(img.width);
|
---|
| 65 | IHDR.writeInt(img.height);
|
---|
| 66 | IHDR.writeUnsignedInt(0x08060000); // 32bit RGBA
|
---|
| 67 | IHDR.writeByte(0);
|
---|
| 68 | writeChunk(png,0x49484452,IHDR);
|
---|
| 69 | // Build IDAT chunk
|
---|
| 70 | var IDAT:ByteArray= new ByteArray();
|
---|
| 71 | for(var i:int=0;i < img.height;i++) {
|
---|
| 72 | // no filter
|
---|
| 73 | IDAT.writeByte(0);
|
---|
| 74 | var p:uint;
|
---|
| 75 | var j:int;
|
---|
| 76 | if ( !img.transparent ) {
|
---|
| 77 | for(j=0;j < img.width;j++) {
|
---|
| 78 | p = img.getPixel(j,i);
|
---|
| 79 | IDAT.writeUnsignedInt(
|
---|
| 80 | uint(((p&0xFFFFFF) << 8)|0xFF));
|
---|
| 81 | }
|
---|
| 82 | } else {
|
---|
| 83 | for(j=0;j < img.width;j++) {
|
---|
| 84 | p = img.getPixel32(j,i);
|
---|
| 85 | IDAT.writeUnsignedInt(
|
---|
| 86 | uint(((p&0xFFFFFF) << 8)|
|
---|
| 87 | (p>>>24)));
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 | IDAT.compress();
|
---|
| 92 | writeChunk(png,0x49444154,IDAT);
|
---|
| 93 | // Build IEND chunk
|
---|
| 94 | writeChunk(png,0x49454E44,null);
|
---|
| 95 | // return PNG
|
---|
| 96 | return png;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | private static var crcTable:Array;
|
---|
| 100 | private static var crcTableComputed:Boolean = false;
|
---|
| 101 |
|
---|
| 102 | private static function writeChunk(png:ByteArray,
|
---|
| 103 | type:uint, data:ByteArray):void {
|
---|
| 104 | if (!crcTableComputed) {
|
---|
| 105 | crcTableComputed = true;
|
---|
| 106 | crcTable = [];
|
---|
| 107 | var c:uint;
|
---|
| 108 | for (var n:uint = 0; n < 256; n++) {
|
---|
| 109 | c = n;
|
---|
| 110 | for (var k:uint = 0; k < 8; k++) {
|
---|
| 111 | if (c & 1) {
|
---|
| 112 | c = uint(uint(0xedb88320) ^
|
---|
| 113 | uint(c >>> 1));
|
---|
| 114 | } else {
|
---|
| 115 | c = uint(c >>> 1);
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 | crcTable[n] = c;
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 | var len:uint = 0;
|
---|
| 122 | if (data != null) {
|
---|
| 123 | len = data.length;
|
---|
| 124 | }
|
---|
| 125 | png.writeUnsignedInt(len);
|
---|
| 126 | var p:uint = png.position;
|
---|
| 127 | png.writeUnsignedInt(type);
|
---|
| 128 | if ( data != null ) {
|
---|
| 129 | png.writeBytes(data);
|
---|
| 130 | }
|
---|
| 131 | var e:uint = png.position;
|
---|
| 132 | png.position = p;
|
---|
| 133 | c = 0xffffffff;
|
---|
| 134 | for (var i:int = 0; i < (e-p); i++) {
|
---|
| 135 | c = uint(crcTable[
|
---|
| 136 | (c ^ png.readUnsignedByte()) &
|
---|
| 137 | uint(0xff)] ^ uint(c >>> 8));
|
---|
| 138 | }
|
---|
| 139 | c = uint(c^uint(0xffffffff));
|
---|
| 140 | png.position = e;
|
---|
| 141 | png.writeUnsignedInt(c);
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 | }
|
---|