/** ///// -- BadNoobComLib -- ///// [CLASS] :: com.badnoob.images.encoder.PNGBGRAEncoder [CREATED] :: v.1-alpha Oct 7, 2010 [VERSION] :: v.1-alpha $Rev$ [MODIFIED] :: $LastModifiedDate$ [AUTHOR] :: badnoob.com, Dieselstr. 18, 30827 Garbsen [Daniel Bunte] ///// [ D E S C R I P T I O N ] Encodes images in BGRA. END -::- [ D E S C R I P T I O N ] ///// [ C H A N G E L O G ] ::07th October 2010 { created the class and defined basic methods } ::11th October 2010 { finished encoder with variable filter on every line tested with a png from lightwave original LW : 256KB photoshop saved for web : 252KB PNGBGRAEncoder : 246KB so, we're even better than photoshop :) } END -::- [ C H A N G E L O G ] ///// */ /** * ADDITIONAL COPYRIGHT NOTICES * * Some parts of this class use code by Adobe, see the copyright below. * I catched the algorithms from there: http://wonderfl.net/c/Pbyd * I just finally hacked all the pieces together and optimized loops as well as speed. * This is a brute force method, so it's still very slow. The speed might be improved using * the ApplicationDomain-ByteArray, but we already use this BA in our project, so I use * the normal BAs here. If I'd use the AppDomain-BA, the encoder would probably override * parts of our application. * * Copyirght (c) 2010 * badnoob.com * bunte schneider GbR * Dieselstr. 18 * D-30827 Garbsen * Germany */ /* Copyright (c) 2008, Adobe Systems Incorporated All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Adobe Systems Incorporated nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package com.badnoob.images.encoder { import flash.display.Bitmap; import flash.display.BitmapData; import flash.geom.*; import flash.utils.ByteArray; import flash.utils.Endian; /** * Class that converts BitmapData into a valid PNG */ public class PNGBGRAEncoder { /** * 0 None * 1 Sub * 2 Up * 3 Average * 4 Paeth */ private static const PNGFilterType_NONE :uint = uint(0), PNGFilterType_SUB :uint = uint(1), PNGFilterType_UP :uint = uint(2), PNGFilterType_AVERAGE :uint = uint(3), PNGFilterType_PAETH :uint = uint(4); private static var crcTable :Vector., crcInitialized :Boolean; /** * Created a PNG image from the specified BitmapData * * @param image The BitmapData that will be converted into the PNG format. * @return a ByteArray representing the PNG encoded image data. * @langversion ActionScript 3.0 * @playerversion Flash 9.0 * @tiptext */ public static function encode(img:BitmapData, blnCompressOutput:Boolean = true, blnConvertToBGRA:Boolean = false):ByteArray { if(PNGBGRAEncoder.crcInitialized === false) { PNGBGRAEncoder.crcTable = new []; var c :uint, n :int = int(0 - 1), k :int, lenn:int = int(256), lenk:int = int(8); while(++n !== lenn)//for (var n:uint = 0; n < 256; n++) { c = n; k = int(0 - 1); while(++k !== lenk)//for (var k:uint = 0; k < 8; k++) { if (c & 1) { c = uint(uint(0xedb88320) ^ uint(c >>> 1)); } else { c = uint(c >>> 1); } } PNGBGRAEncoder.crcTable[n] = c; } } // Create output byte array var png:ByteArray = new ByteArray(); // Write PNG signature png.writeUnsignedInt(0x89504e47); png.writeUnsignedInt(0x0D0A1A0A); // Build IHDR chunk var IHDR:ByteArray = new ByteArray(); IHDR.writeInt(img.width); IHDR.writeInt(img.height); //write bit depth, always 8 IHDR[IHDR.position++] = int(8); /** * write color type, depending on transparency, either RGBA(wich is * later converted to BGRA), or RGB */ if(img.transparent === true) { IHDR[IHDR.position++] = int(6); } else { IHDR[IHDR.position++] = int(2); } //write compression-method, always 0 IHDR[IHDR.position++] = int(0); //write filtermethod, always 0 IHDR[IHDR.position++] = int(0); //write interlacemethod, always 0, since we don't use it IHDR[IHDR.position++] = int(0); PNGBGRAEncoder.writeChunk(png,0x49484452,IHDR); var IDAT:ByteArray= new ByteArray; IDAT = PNGBGRAEncoder.autoEncode(img, blnCompressOutput, blnConvertToBGRA); PNGBGRAEncoder.writeChunk(png,0x49444154,IDAT); // Build IEND chunk PNGBGRAEncoder.writeChunk(png,0x49454E44,null); // return PNG png.position = int(0); return png; } private static function writeChunk(png:ByteArray, type:uint, data:ByteArray):void { var len :uint, c :uint; if (data !== null) { len = data.length; } png.writeUnsignedInt(len); var p:uint = png.position; png.writeUnsignedInt(type); if ( data !== null ) { png.writeBytes(data); } var e:uint = png.position; png.position = p; c = 0xffffffff; var i :int = int(0 - 1), lenep :uint = (e-p); while(++i < lenep) { c = uint(crcTable[(c ^ png[png.position++]) & uint(0xff)] ^ uint(c >>> 8)); } c = uint(c^uint(0xffffffff)); png.position = e; png.writeUnsignedInt(c); } private static function autoEncode(img:BitmapData, blnCompressOutput:Boolean = true, blnConvertToBGRA:Boolean = false):ByteArray { var a :uint, aLeft :uint, aLeftAbove :uint, aPrediction :uint, b :uint, blnTransparent :Boolean = img.transparent, bLeft :uint, bLeftAbove :uint, bPrediction :uint, baOut :ByteArray = new ByteArray, baFilterNONE :ByteArray = new ByteArray, baFilterSUB :ByteArray = new ByteArray, baFilterUP :ByteArray = new ByteArray, baFilterAVERAGE :ByteArray = new ByteArray, baFilterPAETH :ByteArray = new ByteArray, color :uint, g :uint, gLeft :uint, gLeftAbove :uint, gPrediction :uint, intFilterType :int, intHeight :int = img.height, intLine :int, intSmallestLine :int, intWidth :int = img.width, p0 :int, p1 :int, p2 :int, r :uint, rLeft :uint, rLeftAbove :uint, rPrediction :uint, vecAAboves :Vector. = new Vector.(intWidth, true), vecBAboves :Vector. = new Vector.(intWidth, true), vecGAboves :Vector. = new Vector.(intWidth, true), vecRAboves :Vector. = new Vector.(intWidth, true), vecPixels :Vector. = img.getVector(img.rect), x :int, y :int; vecPixels.fixed = true; baOut.clear(); while(y < intHeight) { baFilterNONE.clear(); baFilterSUB.clear(); baFilterUP.clear(); baFilterAVERAGE.clear(); baFilterPAETH.clear(); intSmallestLine = int(0); x = int(0); rLeft = int(0); rLeftAbove = int(0); gLeft = int(0); gLeftAbove = int(0); bLeft = int(0); bLeftAbove = int(0); aLeft = int(0); aLeftAbove = int(0); while(x < intWidth) { color = vecPixels[(intLine + x as uint)]; if(blnConvertToBGRA === true) { r = (color & 0x000000FF); b = (color & 0x00FF0000) >>> 16; } else { r = (color & 0x00FF0000) >>> 16; b = (color & 0x000000FF); } g = (color & 0x0000FF00) >>> 8;//((color & 0x0000FF00) << 8); if(blnTransparent === true) { a = (color >>> 24); } else { a = 0xFF; } // color = uint(r | g | b | a); //try all the filters at once, decide later which one uses least bytes and save it //Filter: NONE baFilterNONE[baFilterNONE.position++] = r; //B baFilterNONE[baFilterNONE.position++] = g; //G baFilterNONE[baFilterNONE.position++] = b; //R //Filter: SUB baFilterSUB[baFilterSUB.position++] = (r - rLeft + 0x100 & 0xFF); baFilterSUB[baFilterSUB.position++] = (g - gLeft + 0x100 & 0xFF); baFilterSUB[baFilterSUB.position++] = (b - bLeft + 0x100 & 0xFF); //Filter: UP baFilterUP[baFilterUP.position++] = (r - vecRAboves[x] + 0x100 & 0xFF); baFilterUP[baFilterUP.position++] = (g - vecGAboves[x] + 0x100 & 0xFF); baFilterUP[baFilterUP.position++] = (b - vecBAboves[x] + 0x100 & 0xFF); //Filter: AVERAGE baFilterAVERAGE[baFilterAVERAGE.position++] = (r - (rLeft + vecRAboves[x] >> 1) + 0x100 & 0xFF); baFilterAVERAGE[baFilterAVERAGE.position++] = (g - (gLeft + vecGAboves[x] >> 1) + 0x100 & 0xFF); baFilterAVERAGE[baFilterAVERAGE.position++] = (b - (bLeft + vecBAboves[x] >> 1) + 0x100 & 0xFF); //Filter: PAETH p0 = (vecRAboves[x] > rLeftAbove) ? vecRAboves[x] - rLeftAbove : rLeftAbove - vecRAboves[x]; p1 = (rLeft > rLeftAbove) ? rLeft - rLeftAbove : rLeftAbove - rLeft; p2 = (vecRAboves[x] + rLeft > (rLeftAbove << 1)) ? vecRAboves[x] + rLeft - (rLeftAbove << 1) : (rLeftAbove << 1) - vecRAboves[x] - rLeft; rPrediction = (p0 <= p1 && p0 <= p2) ? rLeft : (p1 <= p2) ? vecRAboves[x] : rLeftAbove; p0 = (vecGAboves[x] > gLeftAbove) ? vecGAboves[x] - gLeftAbove : gLeftAbove - vecGAboves[x]; p1 = (gLeft > gLeftAbove) ? gLeft - gLeftAbove : gLeftAbove - gLeft; p2 = (vecGAboves[x] + gLeft > (gLeftAbove << 1)) ? vecGAboves[x] + gLeft - (gLeftAbove << 1) : (gLeftAbove << 1) - vecGAboves[x] - gLeft; gPrediction = (p0 <= p1 && p0 <= p2) ? gLeft : (p1 <= p2) ? vecGAboves[x] : gLeftAbove; p0 = (vecBAboves[x] > bLeftAbove) ? vecBAboves[x] - bLeftAbove : bLeftAbove - vecBAboves[x]; p1 = (bLeft > bLeftAbove) ? bLeft - bLeftAbove : bLeftAbove - bLeft; p2 = (vecBAboves[x] + bLeft > (bLeftAbove << 1)) ? vecBAboves[x] + bLeft - (bLeftAbove << 1) : (bLeftAbove << 1) - vecBAboves[x] - bLeft; bPrediction = (p0 <= p1 && p0 <= p2) ? bLeft : (p1 <= p2) ? vecBAboves[x] : bLeftAbove; p0 = (vecAAboves[x] > aLeftAbove) ? vecAAboves[x] - aLeftAbove : aLeftAbove - vecAAboves[x]; p1 = (aLeft > aLeftAbove) ? aLeft - aLeftAbove : aLeftAbove - aLeft; p2 = (vecAAboves[x] + aLeft > (aLeftAbove << 1)) ? vecAAboves[x] + aLeft - (aLeftAbove << 1) : (aLeftAbove << 1) - vecAAboves[x] - aLeft; aPrediction = (p0 <= p1 && p0 <= p2) ? aLeft : (p1 <= p2) ? vecAAboves[x] : aLeftAbove; baFilterPAETH[baFilterPAETH.position++] = (r - rPrediction + 0x100 & 0xFF); baFilterPAETH[baFilterPAETH.position++] = (g - gPrediction + 0x100 & 0xFF); baFilterPAETH[baFilterPAETH.position++] = (b - bPrediction + 0x100 & 0xFF); //write alpha if we have transparency only if(blnTransparent === true) { baFilterNONE[baFilterNONE.position++] = a; //A baFilterSUB[baFilterSUB.position++] = (a - aLeft + 0x100 & 0xFF); baFilterUP[baFilterUP.position++] = (a - vecAAboves[x] + 0x100 & 0xFF); baFilterAVERAGE[baFilterAVERAGE.position++] = (a - (aLeft + vecAAboves[x] >> 1) + 0x100 & 0xFF); baFilterPAETH[baFilterPAETH.position++] = (a - aPrediction + 0x100 & 0xFF); } rLeftAbove = vecRAboves[x]; gLeftAbove = vecGAboves[x]; bLeftAbove = vecBAboves[x]; aLeftAbove = vecAAboves[x]; rLeft = vecRAboves[x] = r; gLeft = vecGAboves[x] = g; bLeft = vecBAboves[x] = b; aLeft = vecAAboves[x] = a; ++x; } baFilterNONE.position = int(0); baFilterSUB.position = int(0); baFilterUP.position = int(0); baFilterAVERAGE.position = int(0); baFilterPAETH.position = int(0); baFilterNONE.compress(); baFilterSUB.compress(); baFilterUP.compress(); baFilterAVERAGE.compress(); baFilterPAETH.compress(); intSmallestLine = baFilterSUB.length; intFilterType = PNGBGRAEncoder.PNGFilterType_SUB; if(baFilterPAETH.length < intSmallestLine) { intSmallestLine = baFilterPAETH.length; intFilterType = PNGBGRAEncoder.PNGFilterType_PAETH; } if(baFilterUP.length < intSmallestLine) { intSmallestLine = baFilterUP.length; intFilterType = PNGBGRAEncoder.PNGFilterType_UP; } if(baFilterAVERAGE.length < intSmallestLine) { intSmallestLine = baFilterAVERAGE.length; intFilterType = PNGBGRAEncoder.PNGFilterType_AVERAGE; } if(baFilterNONE.length < intSmallestLine) { intSmallestLine = baFilterNONE.length; intFilterType = PNGBGRAEncoder.PNGFilterType_NONE; } //write FilterType of current line into stream, then append filtered bytes baOut[baOut.position++] = intFilterType; switch(intSmallestLine) { case baFilterPAETH.length : { baFilterPAETH.position = int(0); baFilterPAETH.uncompress(); baOut.writeBytes(baFilterPAETH); }; break; case baFilterSUB.length : { baFilterSUB.position = int(0); baFilterSUB.uncompress(); baOut.writeBytes(baFilterSUB); }; break; case baFilterUP.length : { baFilterUP.position = int(0); baFilterUP.uncompress(); baOut.writeBytes(baFilterUP); }; break; case baFilterAVERAGE.length : { baFilterAVERAGE.position = int(0); baFilterAVERAGE.uncompress(); baOut.writeBytes(baFilterAVERAGE); }; break; case baFilterNONE.length : { baFilterNONE.position = int(0); baFilterNONE.uncompress(); baOut.writeBytes(baFilterNONE); } } intLine = (++y * intWidth as int); } baOut.position = int(0); if(blnCompressOutput === true) { baOut.compress(); } return baOut; } } }