From AssetPaths (String) to FlxSprite to BitmapData to String... Can it be simplified?



  • So I'm converting an image inside the AssetPaths into a string for doing things in my game.
    Basicaly, I create a temporary FlxSprite to load the graphic from the AssetPaths, then I steal the BitmapData from it in order to convert it into a String. While everything is working nicely, I was wondering if there was a simplier way to do that...
    Here's my code:

    // Retrieve BitmapData from AssetPaths' String
    public static function getBitmapData(string:String):BitmapData
    {
    	var sprite:FlxSprite = new FlxSprite();
    	sprite.loadGraphic(string);
    	return sprite.pixels;
    }
    
    // Convert BitmapData to String
    public static function serializeBitmapData(bmp:BitmapData):String
    {
    	var bmp_bytes:Bytes = Bytes.ofData(PNGEncoder.encode(bmp));
    	var serializer = new Serializer();
    	serializer.serialize(bmp_bytes);
    	return serializer.toString();
    }
    
    // Here, I convert myImage.png to String
    var bmp:BitmapData = getBitmapData(AssetPaths.myImage__png);
    var str:String = serializeBitmapData(bmp);
    

    I'm sure I'm doing too much here...



  • It's something like:

    #if openfl_legacy
    			var bytes = ByteArray.readFile(fullPath);
    #else
    			var bytes = File.getBytes(fullPath);
    #end
    

    Then there should be some method to convert to Base64, or use static method run() of Serializer class.
    I'm not sure what you want to achieve by loading it as BitmapData or encoding to PNG, since any binary data can be converted to some sort of String



  • Thanks @starry-abyss
    I'm using that in order to store user images inside a FlxSave (http://forum.haxeflixel.com/topic/191/solved-can-we-store-a-bitmapdata-inside-a-flxsave) but I also have to store some images that are in my AssetPaths.
    I'm not familiar AT ALL with bytes, binary data and Base64 stuff... I wrote my thing like that, it's long and redundant but it's working. I wish it was as simple as you say, but ByteArray.readFile don't exist.



  • If you're on -Dnext, then use File.getBytes. My code snippet supports legacy and next at the same time



  • @Txori My bad, the solution won't work for embedded assets. Apparently,

    var bytes = Assets.getBytes(path);
    

    is the way to go.



  • Thanks :)
    So I was playing around with File:getBytes, which retrieves a ByteArray, so I converted it to Bytes.

    var byteArray:ByteArray = Assets.getBytes(path);
    var bytes:Bytes = Bytes.ofData(byteArray);
    

    So I converted that image to three different kind of strings:

    alt text

    trace( Base64.encode(bytes) );

    /3OU0/9zlNP/c5TT/3OU0/9zlNP/c5TT/3OU0/+Po74AAAAAAAAAAAAAAAAAAAAA/3OU0/////////////////////////////////+Po77/j6O+AAAAAAAAAAAAAAAA/3OU0///////8PX///D1///w9f//8PX///D1//+Po77//////4+jvgAAAAAAAAAA/3OU0///////7fT+/+70///u9P7/7vP+/+70//+Po77///////////+Po74AAAAA/3OU0///////6/H+/+rx/f/r8f3/6/H+/+vx/v+Po77/j6O+/4+jvv+Po77/j6O+/3OU0///////5+79/+bv/f/n7v3/5+/9/+fu/f/W3uz/ydLi/8nS4//c3uX/Q1eE/3OU0///////4+v8/+Lr/P/i6/z/4+v8/+Ps/P/i7Pz/4+z8/+Ps/P//////Q1eE/3OU0///////3un7/97o+//e6Pv/3+n7/97o+//e6Pv/3+j7/97o+///////Q1eE/3OU0///////2eX6/9rl+v/Z5Pr/2uX6/9rk+v/a5fr/2uX6/9nk+v//////Q1eE/3OU0///////1eL5/9Xi+f/V4vn/1uH5/9Xi+f/V4fn/1eL5/9Xi+f//////Q1eE/3OU0///////0d/5/9Hf+P/R3/j/0d/4/9He+P/R3/j/0d74/9Hf+P//////Q1eE/3OU0///////zdz4/87c9//N3Pj/zdz3/83c9//N3Pj/zdz3/87c9///////Q1eE/3OU0///////ytr3/8vZ9//K2ff/ytn3/8va9//K2ff/ytr3/8vZ9v//////Q1eE/3OU0///////////////////////////////////////////////////////Q1eE/3OU0/9DV4T/Q1eE/0NXhP9DV4T/Q1eE/0NXhP9DV4T/Q1eE/0NXhP9DV4T/Q1eE

    960 characters

    trace( Serializer.run(bytes) );

    s960::3OU0:9zlNP:c5TT:3OU0:9zlNP:c5TT:3OU0:%Po74AAAAAAAAAAAAAAAAAAAAA:3OU0:::::::::::::::::::::::::::::::::%Po77:j6O%AAAAAAAAAAAAAAAA:3OU0:::::::8PX:::D1:::w9f::8PX:::D1::%Po77::::::4%jvgAAAAAAAAAA:3OU0:::::::7fT%:%70:::u9P7:7vP%:%70::%Po77:::::::::::%Po74AAAAA:3OU0:::::::6:H%:%rx:f:r8f3:6:H%:%vx:v%Po77:j6O%:4%jvv%Po77:j6O%:3OU0:::::::5%79:%bv:f:n7v3:5%:9:%fu:f:W3uz:ydLi:8nS4::c3uX:Q1eE:3OU0:::::::4%v8:%Lr:P:i6:z:4%v8:%Ps:P:i7Pz:4%z8:%Ps:P::::::Q1eE:3OU0:::::::3un7:97o%::e6Pv:3%n7:97o%::e6Pv:3%j7:97o%:::::::Q1eE:3OU0:::::::2eX6:9rl%v:Z5Pr:2uX6:9rk%v:a5fr:2uX6:9nk%v::::::Q1eE:3OU0:::::::1eL5:9Xi%f:V4vn:1uH5:9Xi%f:V4fn:1eL5:9Xi%f::::::Q1eE:3OU0:::::::0d:5:9Hf%P:R3:j:0d:4:9He%P:R3:j:0d74:9Hf%P::::::Q1eE:3OU0:::::::zdz4:87c9::N3Pj:zdz3:83c9::N3Pj:zdz3:87c9:::::::Q1eE:3OU0:::::::ytr3:8vZ9::K2ff:ytn3:8va9::K2ff:ytr3:8vZ9v::::::Q1eE:3OU0:::::::::::::::::::::::::::::::::::::::::::::::::::::::Q1eE:3OU0:9DV4T:Q1eE:0NXhP9DV4T:Q1eE:0NXhP9DV4T:Q1eE:0NXhP9DV4T:Q1eE

    965 characters

    trace( serializeBitmapData(sprite.pixels) ); (Using the code I wrote in my first post)

    s366:iVBORw0KGgoAAAANSUhEUgAAAAwAAAAPCAYAAADQ4S5JAAAA2UlEQVR42o2OUQqCQBRF3VuLaCFtof9%2kH76L:CoCJBkZG0stIsyTIrfc0bZzR7BD043MvVA6N1%gv4RW8wBO378MOvQ4FISjhfKfizEolwTAqIEuDwvJRdCQ1JCUFcwCHOeeaiI%pJn1SCH%WwPeUifZkGC2E0dzke2GwDrXa3FrzgBa4EuxfyHpaJ4DUEtn8C25U4n10mEczNAyyOuS7TUik3IizdrMJYyb6qNyLMnIxzr2HNJIJu32FqpyJ1kXXHnQhjK4WJmcJYMrFkF:uNCv9cJWD5lzeHM0afKoMGTwAAAABJRU5ErkJggg

    371 characters

    So at first sight, the Serializer method seems more interesting for storing the multiple user images that I'm going to have on my server. Even if the method is kind of dumb... But I may be wrong.



  • Also, you could possibly just store data as bytes on the server, and not as strings. But I believe you have some reason against doing it?



  • I'll try that. At first, I went with stringify the BitmapData because the savefile can't handle those. Then I stored every parameters of the player, including his stringified image, inside a generated json file. If I can't store the bytes in the savefile or in the json, it's no use. But I'll try, for the knowlegde ;)



  • Yes, for putting inside JSON, you need a text string. PNG encoding makes sense to me now too.
    BTW, I noticed that serializer output and Base64 are almost the same, they use same letters, different special characters, and serializer prepends type and length to the string. Base64 (I mean PNG -> Base64 for small size) format is known and supported outside Haxe too, but it's up to you. :-)


Log in to reply
 

Looks like your connection to HaxeFlixel was lost, please wait while we try to reconnect.