In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

ByteArrayExt fix

Added missing CRC field on IEND chunk.
Get Adobe Flash player
by yonatan 14 Aug 2011
  • Forked from keim_at_Si's convert ByteArray to PNG image
  • Diff: 69
  • Related works: 1
  • Talk

    keim_at_Si at 16 Aug 2011 19:49
    I will merge your modification to the new version. Thank you !
    yonatan at 20 Aug 2011 15:05
    My pleasure. Btw, do you have an issue tracker or an email address for bug reports? I didn't see any of your libraries listed on http://www.libspark.org/newticket/
    keim_at_Si at 21 Aug 2011 20:35
    I dont know how to use spark project's ticket system. And we have no official way to report bugs. MMLTalks' BBS: http://mmltalks.appspot.com/talk. Or DM me on twitter (if you have an account) : http://twitter.com/keim_at_si Or E-mail : keim@nona.dti.ne.jp Or Google+ : https://plus.google.com/113392415863447260930/about?hl=ja Sorry for inconvenience.

    Tags

    Embed
/**
 * Copyright yonatan ( http://wonderfl.net/user/yonatan )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/mEon
 */

// forked from keim_at_Si's convert ByteArray to PNG image
package {
    import flash.events.*;
    import flash.display.*;
    import com.bit101.components.*;
    
    public class main extends Sprite {
        public function main() {
            var me:Sprite = this, imageWidth:Number = 0;
            new Label(me, 140, 4, "Image width (0 to autosize) :");
            new InputText(me, 270, 4, "0", function(e:Event) : void{
                    imageWidth = Number(e.target.text);
                });
            new PushButton(me, 4, 4, "load" , function(e:Event) : void {
                    new ByteArrayExtPatch().browse(function(bae:ByteArrayExtPatch) : void {
                            me.addChild(new Bitmap(bae.toBitmapData(imageWidth))).y = 30;
                            new PushButton(me, 4, 40, "save", function(e:Event) : void {
                                    bae.toPNGData(imageWidth).save("ByteArray.png");
                                });
                        });
                });
        }
    }
}

import flash.utils.ByteArray;
import org.si.utils.ByteArrayExt;

class ByteArrayExtPatch extends ByteArrayExt {
    /** translate to 24bit png data 
    *  @param width png file width, set 0 to calculate automatically.
    *  @param height png file height, set 0 to calculate automatically.
    *  @return ByteArrayExt of PNG data
    */
    override public function toPNGData(width:int=0, height:int=0) : ByteArrayExt
    {
        var i:int, imax:int, reqh:int, pixels:int = (this.length+2)/3, y:int, 
        png:ByteArrayExt = new ByteArrayExt(), 
        header:ByteArray = new ByteArray(), 
        content:ByteArray = new ByteArray();
        //----- settings
        if (width == 0) width = ((int(Math.sqrt(pixels)+65535/65536))+15)&(~15);
        reqh = ((int(pixels/width+65535/65536))+15)&(~15);
        if (height == 0 || reqh > height) height = reqh;
        header.writeInt(width);  // width
        header.writeInt(height); // height
        header.writeUnsignedInt(0x08020000); // 24bit RGB
        header.writeByte(0);
        imax = pixels - width;
        for (y=0, i=0; i<imax; i+=width, y++) {
            content.writeByte(0);
            content.writeBytes(this, i*3, width*3);
        }
        content.writeByte(0);
        content.writeBytes(this, i*3, this.length-i*3);
        imax = (i + width) * 3;
        for (i=this.length; i<imax; i++) content.writeByte(0);
        imax = width * 3 + 1;
        for (y++; y<height; y++) for (i=0; i<imax; i++) content.writeByte(0);
        i = this.length;
        content.position -= 3;
        content.writeByte(i>>>16);
        content.writeByte(i>>>8);
        content.writeByte(i);
        content.compress();
        
        //----- write png data
        // signature
        png.writeUnsignedInt(0x89504e47);
        png.writeUnsignedInt(0x0D0A1A0A);
        // IHDR
        png_writeChunk(0x49484452, header);
        // IDAT
        png_writeChunk(0x49444154, content);
        // IEND
        png_writeChunk(0x49454E44, new ByteArray);
        png.position = 0;
        
        return png;
        
        //----- write png chunk
        function png_writeChunk(type:uint, data:ByteArray) : void {
            png.writeUnsignedInt(data.length);
            var crcStartAt:uint = png.position;
            png.writeUnsignedInt(type);
            png.writeBytes(data);
            png.writeUnsignedInt(calculateCRC32(png, crcStartAt, png.position - crcStartAt));
        }
    }
}