Skip to content

Instantly share code, notes, and snippets.

@azrafe7
Last active August 29, 2015 14:06
Show Gist options
  • Save azrafe7/1a7b22f271addf37ce9f to your computer and use it in GitHub Desktop.
Save azrafe7/1a7b22f271addf37ce9f to your computer and use it in GitHub Desktop.
[OpenFl 2.0] Pixel values inconstistency
package;
import openfl.Assets;
import openfl.display.Sprite;
import openfl.events.KeyboardEvent;
import openfl.system.System;
#if !flash
import sys.io.File;
#end
class Main extends Sprite {
public function new ()
{
super();
var bmd = Assets.getBitmapData("assets/pirate_small.png");
var ba = bmd.getPixels(bmd.rect);
var nPixels = 150;
var px:UInt = 0;
ba.position = 0;
var baValues = "bytearray [" + ba.length + "]: ";
for (i in 0...nPixels) {
px = convert(ba.readUnsignedInt());
if (px != 0) { // don't dump pixels with 0 alpha as they work fine once converted
baValues += " " + StringTools.lpad(StringTools.hex(px), "0", 8);
}
}
var vec = bmd.getVector(bmd.rect);
var vecValues = "vector [" + vec.length + "]: ";
for (i in 0...nPixels) {
px = convert(vec[i]);
if (px != 0) { // don't dump pixels with 0 alpha as they work fine once converted
vecValues += " " + StringTools.lpad(StringTools.hex(px), "0", 8);
}
}
var px32Values = "getPixel32 : ";
for (i in 0...nPixels) {
px = bmd.getPixel32(nPixels % bmd.width, Std.int(nPixels / bmd.height) * bmd.width);
px = convert(vec[i]);
if (px != 0) { // don't dump pixels with 0 alpha as they work fine once converted
px32Values += " " + StringTools.lpad(StringTools.hex(px), "0", 8);
}
}
var pxValues = "getPixel : ";
for (i in 0...nPixels) {
px = bmd.getPixel(nPixels % bmd.width, Std.int(nPixels / bmd.height) * bmd.width);
px = convert(vec[i]);
if (px != 0) { // don't dump pixels with 0 alpha as they work fine once converted
pxValues += " " + StringTools.lpad(StringTools.hex(px), "0", 8);
}
}
trace(baValues);
trace(vecValues);
trace(px32Values);
trace(pxValues);
#if !flash
File.saveContent("log.txt", baValues + "\n" + vecValues + "\n" + px32Values + "\n" + pxValues);
#end
quit();
}
// zero out rgb when alpha is 0 (for consistency with flash)
public function convert(color:UInt):UInt
{
if (color >> 24 & 0xFF == 0) return 0;
else return color;
}
public function quit():Void
{
#if (flash || html5)
System.exit(1);
#else
Sys.exit(1);
#end
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment