I’m trying to write a function that will take a greyscale BitmapData as input (say, one created with PerlinNoise, or the like,) and adjust its contrast so that the darkest color in the image is 0x000000 and the brightest color is 0xFFFFFF. Then return the new BitmapData. It’s only going to run once at program start, so there’s no need for it to be fast. Just accurate.
I guess the first step is to go through and find out what the darkest and lightest colors are in the image… does anyone know how to step through all the pixels in a BitmapData? I couldn’t find an easy-to-follow example of that in the built-in help.
The next step would be, um, once you know the brightness of the brightest and darkest color actually present in the image, figure out what percent of 255 the brightest grey is and what percentage of 255 the darkest grey is…
And I can’t think what the third step would be… Some kind of math, I suppose. Probably multiply it by something. I just don’t have enough math under my belt to immagine it.
If anyone can figure this out for me, I’d really appreciate it. In return, here’s a handy function I worked out earlier today along simmilar lines. This one takes a greyscale image and turns it into a colored image made up of a gradient between any two colors:
function applyGradientBMD(passedBMD:BitmapData, blackColor:uint,whiteColor:uint):BitmapData {
//Assumes source bitmapdata is greyscale!
var localRectangle:Rectangle=passedBMD.rect;
var baseColorBMD:BitmapData=new BitmapData(passedBMD.width,passedBMD.height,passedBMD.transparent,blackColor);
var applyColorBMD:BitmapData=new BitmapData(passedBMD.width,passedBMD.height,true,whiteColor);
applyColorBMD.copyChannel(passedBMD, localRectangle, zeroPoint, BitmapDataChannel.RED , BitmapDataChannel.ALPHA );
baseColorBMD.draw(applyColorBMD);
return baseColorBMD;
}
This one was more of a logic puzzle than a math puzzle, so I was able to figure it out. Also, it was doable without using getPixels… I can’t even figure out how to parse a byteArray once I’ve extracted it.
Thanks in advance for any helpful replies.