Created
March 6, 2017 15:53
-
-
Save TerryCavanagh/450bfeafaa3e1f1be9a106dc9472dd7a to your computer and use it in GitHub Desktop.
Useful functions: Convert bitmapdata to array of ints, and back
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function convert_bitmapdata_to_intarray(bmp:BitmapData):String { | |
| var bmpbytes:ByteArray = bmp.getPixels(bmp.rect); | |
| bmpbytes.compress(); | |
| var outputstring:String = "["; | |
| try { | |
| bmpbytes.position = 0; | |
| while (true) { | |
| var b:UInt = bmpbytes.readUnsignedInt(); | |
| outputstring += "0x" + StringTools.hex(b).toLowerCase() + ", "; | |
| } | |
| } catch (e:Dynamic) {} | |
| outputstring = outputstring.substr(0, outputstring.length - 2) + "];"; | |
| return outputstring; | |
| } | |
| function convert_intarray_to_bitmapdata(arr:Array<UInt>, width:Int, height:Int):BitmapData{ | |
| var bmpData:BitmapData = new BitmapData(width, height); | |
| var bmpBytes:ByteArray = new ByteArray(); | |
| for (i in 0 ... arr.length) bmpBytes.writeUnsignedInt(arr[i]); | |
| bmpBytes.uncompress(); | |
| bmpData.setPixels(new Rectangle(0, 0, width, height), bmpBytes); | |
| bmpBytes.clear(); | |
| return bmpData; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment