A Flash Developer Resource Site

Page 1 of 2 12 LastLast
Results 1 to 20 of 31

Thread: base64

  1. #1
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404

    base64

    hi, i have a script to decode base64 data which is supposed to be a raw png's data in as2

    how can i use that decoded png data to create bitmap again.

    thanks.
    PHP Code:
    var _Chars:Array=new Array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/');_CharsReverseLookup=new Array();for(i=0;i<_Chars.length;i++){_CharsReverseLookup[_Chars[i]]=i;}function readBase64(){if(!_base64Str){return_EndOfInput;}if(_base64Count>=_base64Str.length){return _EndOfInput;}var c:Number=_base64Str.charCodeAt(_base64Count)&0xff;_base64Count++;return c;}function base64encode(str){_base64Str=str;_base64Count=0;var result="";var inBuffer=new Array(3);var lineCount=0;var done=false;while(!done &&(inBuffer[0]=readBase64())!=_EndOfInput){inBuffer[1]=readBase64();inBuffer[2]=readBase64();result+=(_Chars[inBuffer[0]>>2]);if(inBuffer[1]!=_EndOfInput){result+=(_Chars[((inBuffer[0]<<4)&0x30)|(inBuffer[1]>>4)]);if(inBuffer[2]!=_EndOfInput){result+=(_Chars[((inBuffer[1]<<2)&0x3c)|(inBuffer[2]>>6)]);
    result+=(_Chars[inBuffer[2]&0x3F]);}else{result+=(_Chars[((inBuffer[1]<<2)&0x3c)]);result+=("=");done=true;}}else{result+=(_Chars[((inBuffer[0]<<4)&0x30)]);result+="=";result+="=";done=true;}lineCount+= 4;if(lineCount>=76){result+=('\n');lineCount=0;}}return result;}function readReverseBase64(){if(!_base64Str){return _EndOfInput;}while(true){if(_base64Count>=_base64Str.length){return _EndOfInput;}var nextCharacter:String=_base64Str.charAt(_base64Count);_base64Count++;if(_CharsReverseLookup[nextCharacter]){return _CharsReverseLookup[nextCharacter];}if(nextCharacter =='A'){return 0;}}return _EndOfInput;}function ntos(n){var str:String=n.toString(16);if(str.length ==1)str="0"+str;str="%"+str;return unescape(str);}function base64decode(str){_base64Str=str;_base64Count=0;var result="";var inBuffer:Array=new Array(4);var done=false;while(!done &&(inBuffer[0]=readReverseBase64())!=_EndOfInput&&(inBuffer[1]=readReverseBase64())!=_EndOfInput){inBuffer[2]=readReverseBase64();inBuffer[3]=readReverseBase64();
    result+=ntos((((inBuffer[0]<<2)&0xff)|inBuffer[1]>>4));if(inBuffer[2]!=_EndOfInput){result+=ntos((((inBuffer[1]<<4)&0xff)|inBuffer[2]>>2));if(inBuffer[3]!=_EndOfInput){result+=  ntos((((inBuffer[2]<<6)  & 0xff) | inBuffer[3]));}else{done=true;}}else{done=true;}}return result;}function pad(strlenpad){var result:String=str;for(i=str.length;i<len;i++){result=pad+result;}return result;} 
    and this is the function i call which decodes the png its like a little 8bit guy:
    PHP Code:
    trace(base64decode('iVBORw0KGgoAAAANSUhEUgAAAB8AAAAwCAYAAADpVKHaAAAB2UlEQVRYhe1Xu23DMBB9MryCVLjyDsoi6TKBPEUQBAmQAVLZE2SGFJkg2sGVCmmDNJfCInGieEdSVmQDzgMMWD6K7+7dh3SGRJQbkGavG2Sxe60SSVVitm5e8r+AKlFKFBK0NHgNEXl1109yYK2/JhOGbCGHgIVyLikZHTntq6h12dsnyp+jfa4bWQVv5HUzlPL7KY7Yt16Tf7FWKzcgV/5RFfIFPIJsdxisc9Ng7dstcDyC9hWy3cFGbuTnlW9zPkdPp0KU3ZdnIgKq/uOiopOdrw8UaXTOY6s9Za3V35U9tcIl3D0Pa8Wbc/YjxUynVPhGrDjb6yZNah9Yh3h5LnqkShMu+jZyDkIkBPjl50NHsav7Rx8sI6/OrAfgmq9RDHYGaBE78z+4t092MoeCj6y7fw17eHLE1ouUf5931LYtiqIAALRtq3ovgb/ff1fJSSPL8zyKtOs61RnOaWQnTsqJpM1CcPcw+xdFQcaB65twS8E7ZKZKnbrHCn1rLYWeiwBQBqfYJJxb7Rym8i+f8/zrffBj9vAy6ncTkaSA1t/08ei13W6r2Uuja5g6012wsTriXfMHhrn/vVzfBVI68BeJfOodbpY03W6r/ZPfHrl2sZfaJ3U2iBy/86G9nGexkcsAAAAASUVORK5CYII=')); 

  2. #2
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    927
    1 stop using that crap (as2)

    2 in as3 you have this:
    PHP Code:
    var loader:Loader = new Loader();
    loader.contentLoaderInfo.addEventListener(Event.INIT, function(e:Event):void {
       
    bmpData.draw(loader);    
    });
    loader.loadBytes(byteArray); 
    who is this? a word of friendly advice: FFS stop using AS2

  3. #3
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    Thanks bro

  4. #4
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    you dont have an as2 version?

  5. #5
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    927
    who is this? a word of friendly advice: FFS stop using AS2

  6. #6
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    ok so this script loads a textfile that has bitmap data and loads up the image properly.
    PHP Code:
    var mc:MovieClip this.createEmptyMovieClip("mc"this.getNextHighestDepth()); 
    var 
    image:MovieClip mc.createEmptyMovieClip("image"mc.getNextHighestDepth()); 
    image.loadMovie("8bit.txt"); 
    how do i replace that scripts file path, with the actual bitmap data
    i'll give you $5 paypal for that answer.

  7. #7
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    927
    you could use some echo.php?data=... where the script would just do <?php echo base64_decode($_GET["data"]); ?> but this would be gaping security hole if placed into wrong domain
    who is this? a word of friendly advice: FFS stop using AS2

  8. #8
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    no php plz

  9. #9
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    927
    who is this? a word of friendly advice: FFS stop using AS2

  10. #10
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    image.loadBytes('decoded data here');

    doesnt seem to be working for me. :/

  11. #11
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    927
    because that's as3 ) it totally works in as3: http://wonderfl.net/c/ufnD
    who is this? a word of friendly advice: FFS stop using AS2

  12. #12

  13. #13
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    oh hey fruitbeard, i was listening to station 14 the other day on fruitbeard.net. Why do you guys keep posting as3, do either of you have the formula to create a png in as2 using these tools: >> << .setPixel(,,,); and decoded data. im going to try to ri the as3 encoder than. unless you have a solution for as2

  14. #14
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    927
    Why do you guys keep posting as3, do either of you have the formula to create a png in as2...
    that's like asking why do you guys keep sending me to the doctor when I specifically ask for homeopathic remedies.
    who is this? a word of friendly advice: FFS stop using AS2

  15. #15
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    Quote Originally Posted by realMakc View Post
    that's like asking why do you guys keep sending me to the doctor when I specifically ask for homeopathic remedies.
    thats like implying you rather go downhill than uphill

  16. #16
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    If someone helps me out with changing the pixel getters to setters in this and converts it to as2 ill buy adobe flan-imate and move on how about that.

    as3:
    PHP Code:
    package com.adobe.images
    {
    import flash.geom.*;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.utils.ByteArray;
    /**
    * Class that converts BitmapData into a valid PNG
    */    
    public class PNGEncoder
    {
    /**
    * 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):ByteArray {
    // 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);
    IHDR.writeUnsignedInt(0x08060000); // 32bit RGBA
    IHDR.writeByte(0);
    writeChunk(png,0x49484452,IHDR);
    // Build IDAT chunk
    var IDAT:ByteArray= new ByteArray();
    for(var 
    i:int=0;img.height;i++) {
    // no filter
    IDAT.writeByte(0);
    var 
    p:uint;
    var 
    j:int;
    if ( !
    img.transparent ) {
    for(
    j=0;img.width;j++) {
    img.getPixel(j,i);
    IDAT.writeUnsignedInt(
    uint(((p&0xFFFFFF) << 8)|0xFF));
    }
    } else {
    for(
    j=0;img.width;j++) {
    img.getPixel32(j,i);
    IDAT.writeUnsignedInt(
    uint(((p&0xFFFFFF) << 8)|
    (
    p>>>24)));
    }
    }
    }
    IDAT.compress();
    writeChunk(png,0x49444154,IDAT);
    // Build IEND chunk
    writeChunk(png,0x49454E44,null);
    // return PNG
    return png;
    }
    private static var 
    crcTable:Array;
    private static var 
    crcTableComputed:Boolean false;
    private static function 
    writeChunk(png:ByteArray,
    type:uintdata:ByteArray):void {
    if (!
    crcTableComputed) {
    crcTableComputed true;
    crcTable = [];
    var 
    c:uint;
    for (var 
    n:uint 0256n++) {
    n;
    for (var 
    k:uint 08k++) {
    if (
    1) {
    uint(uint(0xedb88320) ^
    uint(>>> 1));
    } else {
    uint(>>> 1);
    }
    }
    crcTable[n] = c;
    }
    }
    var 
    len:uint 0;
    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;
    0xffffffff;
    for (var 
    i:int 0< (e-p); i++) {
    uint(crcTable[(png.readUnsignedByte()) & uint(0xff)] ^ uint(>>> 8));
    }
    uint(c^uint(0xffffffff));
    png.position e;
    png.writeUnsignedInt(c);
    }
    }

    look at this poor dessert waiting to be combined and subscribed too: http://i.imgur.com/yujeyyJ.png
    Last edited by AS3.0; 05-24-2016 at 05:47 PM.

  17. #17

  18. #18
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    this attempt felt promising but it comes out as a black image using the data so idk:

    PHP Code:
    var _Chars:Array=new Array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/');_CharsReverseLookup=new Array();for(i=0;i<_Chars.length;i++){_CharsReverseLookup[_Chars[i]]=i;}function readBase64(){if(!_base64Str){return_EndOfInput;}if(_base64Count>=_base64Str.length){return _EndOfInput;}var c:Number=_base64Str.charCodeAt(_base64Count)&0xff;_base64Count++;return c;}function base64encode(str){_base64Str=str;_base64Count=0;var result="";var inBuffer=new Array(3);var lineCount=0;var done=false;while(!done &&(inBuffer[0]=readBase64())!=_EndOfInput){inBuffer[1]=readBase64();inBuffer[2]=readBase64();result+=(_Chars[inBuffer[0]>>2]);if(inBuffer[1]!=_EndOfInput){result+=(_Chars[((inBuffer[0]<<4)&0x30)|(inBuffer[1]>>4)]);if(inBuffer[2]!=_EndOfInput){result+=(_Chars[((inBuffer[1]<<2)&0x3c)|(inBuffer[2]>>6)]); 
    result+=(_Chars[inBuffer[2]&0x3F]);}else{result+=(_Chars[((inBuffer[1]<<2)&0x3c)]);result+=("=");done=true;}}else{result+=(_Chars[((inBuffer[0]<<4)&0x30)]);result+="=";result+="=";done=true;}lineCount+= 4;if(lineCount>=76){result+=('\n');lineCount=0;}}return result;}function readReverseBase64(){if(!_base64Str){return _EndOfInput;}while(true){if(_base64Count>=_base64Str.length){return _EndOfInput;}var nextCharacter:String=_base64Str.charAt(_base64Count);_base64Count++;if(_CharsReverseLookup[nextCharacter]){return _CharsReverseLookup[nextCharacter];}if(nextCharacter =='A'){return 0;}}return _EndOfInput;}function ntos(n){var str:String=n.toString(16);if(str.length ==1)str="0"+str;str="%"+str;return unescape(str);}function base64decode(str){_base64Str=str;_base64Count=0;var result="";var inBuffer:Array=new Array(4);var done=false;while(!done &&(inBuffer[0]=readReverseBase64())!=_EndOfInput&&(inBuffer[1]=readReverseBase64())!=_EndOfInput){inBuffer[2]=readReverseBase64();inBuffer[3]=readReverseBase64(); 
    result+=ntos((((inBuffer[0]<<2)&0xff)|inBuffer[1]>>4));if(inBuffer[2]!=_EndOfInput){result+=ntos((((inBuffer[1]<<4)&0xff)|inBuffer[2]>>2));if(inBuffer[3]!=_EndOfInput){result+=  ntos((((inBuffer[2]<<6)  & 0xff) | inBuffer[3]));}else{done=true;}}else{done=true;}}return result;}function pad(strlenpad){var result:String=str;for(i=str.length;i<len;i++){result=pad+result;}return result;}  

    import flash.display.BitmapData
    import flash.geom.Rectangle
    import flash.geom.Point

    var 
    bitmapData_1:BitmapData = new BitmapData(10080falsebase64decode('iVBORw0KGgoAAAANSUhEUgAAAB8AAAAwCAYAAADpVKHaAAAB2UlEQVRYhe1Xu23DMBB9MryCVLjyDsoi6TKBPEUQBAmQAVLZE2SGFJkg2sGVCmmDNJfCInGieEdSVmQDzgMMWD6K7+7dh3SGRJQbkGavG2Sxe60SSVVitm5e8r+AKlFKFBK0NHgNEXl1109yYK2/JhOGbCGHgIVyLikZHTntq6h12dsnyp+jfa4bWQVv5HUzlPL7KY7Yt16Tf7FWKzcgV/5RFfIFPIJsdxisc9Ng7dstcDyC9hWy3cFGbuTnlW9zPkdPp0KU3ZdnIgKq/uOiopOdrw8UaXTOY6s9Za3V35U9tcIl3D0Pa8Wbc/YjxUynVPhGrDjb6yZNah9Yh3h5LnqkShMu+jZyDkIkBPjl50NHsav7Rx8sI6/OrAfgmq9RDHYGaBE78z+4t092MoeCj6y7fw17eHLE1ouUf5931LYtiqIAALRtq3ovgb/ff1fJSSPL8zyKtOs61RnOaWQnTsqJpM1CcPcw+xdFQcaB65twS8E7ZKZKnbrHCn1rLYWeiwBQBqfYJJxb7Rym8i+f8/zrffBj9vAy6ncTkaSA1t/08ei13W6r2Uuja5g6012wsTriXfMHhrn/vVzfBVI68BeJfOodbpY03W6r/ZPfHrl2sZfaJ3U2iBy/86G9nGexkcsAAAAASUVORK5CYII=')); 
    var 
    bitmapData_2:BitmapData = new BitmapData(10080false0x00FF0000); 
     
    var 
    mc_1:MovieClip this.createEmptyMovieClip("mc"this.getNextHighestDepth()); 
    mc_1.attachBitmap(bitmapData_1this.getNextHighestDepth()); 

    var 
    mc_2:MovieClip this.createEmptyMovieClip("mc"this.getNextHighestDepth()); 
    mc_2.attachBitmap(bitmapData_2this.getNextHighestDepth()); 
    mc_2._x 101

     
    myListener.onRelease = function () { 
    bitmapData_2.copyPixels(bitmapData_1, new Rectangle(0010080), new Point(0,0)); 
    }; 

  19. #19
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    Your above code is just converting new BitmapData(100, 80, false, base64decode(........ to new BitmapData(100, 80, false,0x00000000 as it doesn't know what to make of it, defaults to it.
    it isn't loading any image at all.

    so you might as well just use:

    var bitmapData_1:BitmapData = new BitmapData(100, 80, false, 0x00000000);
    var bitmapData_2:BitmapData = new BitmapData(100, 80, false, 0x00FF0000);

    http://www.mzan.com/article/4239930-...-jpg-png.shtml

    if you read the links given you will see why you are being told to use AS3

    http://stackoverflow.com/questions/6...tring-possible
    Last edited by fruitbeard; 05-25-2016 at 05:46 AM.

  20. #20
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    Here is an example in AS3 with your 8bit guy image loading.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center