topical media & game development
#graphic-flex-image-effects-04-Flex-LevelsTest.ax
#graphic-flex-image-effects-04-Flex-LevelsTest.ax
[swf]
[flash]
flex
package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.geom.Point;
[SWF(width=900, height=300, backgroundColor=0x000000)]
Demonstrates how BitmapData's paletteMap() can be used to set levels on an image by remapping
the brightness values of each pixel in each channel.
public class @ax-graphic-flex-image-effects-04-Flex-LevelsTest extends graphic_flex_image_effects_04_Flex_AbstractImageLoader {
Constructor. Passes path of image to load to super.
public function @ax-graphic-flex-image-effects-04-Flex-LevelsTest() {
super("graphic-flex-image-effects-04-assets-goat.jpg");
}
Run after the image loads in super class. This creates two copies of the loaded image and adjusts
the levels on both, attaching all three images to the stage.
override protected function runPostImageLoad():void {
var bitmapData:BitmapData = _loadedBitmap.bitmapData;
addChild(_loadedBitmap);
// copy and set levels
var clone:BitmapData = bitmapData.clone();
setLevels(clone, 0, 100, 200);
var bitmap:Bitmap = new Bitmap(clone);
bitmap.x = bitmapData.width;
addChild(bitmap);
// copy and set levels on another
clone = bitmapData.clone();
setLevels(clone, 100, 127, 155);
bitmap = new Bitmap(clone);
bitmap.x = bitmapData.width*2;
addChild(bitmap);
}
Adjusts the brightness values of all pixels in the bitmap data by remapping the palette
using paletteMap().
parameter: bitmapData The bitmap data to adjust.
parameter: blackPoint The number between 0 and 255 below which all pixels
will have a brightness value of 0 in each channel.
parameter: midPoint The middle gray point in the image used to set the remapping
ratio between the black and white points. This value should be
be between the values set for the black and white points.
parameter: whitePoint The number between 0 and 255 above which all pixels
will have a brightness value of 255 in each channel.
protected function setLevels(
bitmapData:BitmapData,
blackPoint:uint,
midPoint:uint,
whitePoint:uint
):void {
// the arrays that will specify how each pixel value will be remapped in each channel
var r:Array = [];
var g:Array = [];
var b:Array = [];
// for all brightness values below the black point, set the new brightness value to 0
for (var i:uint = 0; i <= blackPoint; i++){
r.push(0);
g.push(0);
b.push(0);
}
var value:uint = 0;
// in between the black and middle gray point, redistribute the brightness values evenly
var range:uint = midPoint - blackPoint;
for (i = blackPoint + 1; i <= midPoint; i++){
value = ((i - blackPoint)/range) * 127;
r.push(value << 16);
g.push(value << 8);
b.push(value);
}
range = whitePoint - midPoint;
// in between the middle gray and white point, redistribute the brightness values evenly
for (i = midPoint + 1; i <= whitePoint; i++){
value = ((i - midPoint)/range) * 128 + 127;
r.push(value << 16);
g.push(value << 8);
b.push(value);
}
// for all brightness values above the white point, set the new brightness value to 255
for (i = whitePoint + 1; i < 256; i++){
r.push(0xFF << 16);
g.push(0xFF << 8);
b.push(0xFF);
}
// remap the image palette to the new brightness values
bitmapData.paletteMap(
bitmapData,
bitmapData.rect,
new Point(),
r,
g,
b
);
}
}
}
(C) Æliens
04/09/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.