package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.BitmapDataChannel; import flash.geom.Point; [SWF(width=900, height=300, backgroundColor=0x000000)] /** * Demonstrates use of BitmapData's copyChannel() by copying a single channel from one image * into all three channels of a new image, creating a grayscale representation of the channel. * This is done for all three color channels. */ public class graphic_flex_image_effects_04_Flex_CopyChannelTest extends graphic_flex_image_effects_04_Flex_AbstractImageLoader { private var _channels:Vector.; /** * Constructor. Passes path of image to load to super class and pushes references to all three color * channel constants into a vector. */ public function graphic_flex_image_effects_04_Flex_CopyChannelTest() { super("graphic-flex-image-effects-04-assets-goat.jpg"); _channels = new Vector.(); _channels.push(BitmapDataChannel.RED); _channels.push(BitmapDataChannel.GREEN); _channels.push(BitmapDataChannel.BLUE); } /** * Run after the image loads in super class. This creates three new images based on the data from the * the loaded image's channels and adds them to the stage. */ override protected function runPostImageLoad():void { var bitmapData:BitmapData = _loadedBitmap.bitmapData; // create three new BitmapData instances with the same dimensions and color depth var red:BitmapData = bitmapData.clone(); var green:BitmapData = bitmapData.clone(); var blue:BitmapData = bitmapData.clone(); // copies the single channel into all three channels of the image createChannelBitmap(red, BitmapDataChannel.RED, 0); createChannelBitmap(green, BitmapDataChannel.GREEN, red.width); createChannelBitmap(blue, BitmapDataChannel.BLUE, red.width*2); bitmapData.dispose(); } /** * Copies a single channel from the image into the other channels in the image. * This also wraps the data in a bitmap and attaches it to the stage. * * @param bitmapData The image to copy channel data to and from. * @param channelToCopy The constant specifying the channel to copy. * @param x The x position to place the bitmap on the stage. */ private function createChannelBitmap( bitmapData:BitmapData, channelToCopy:uint, x:Number ):void { for each (var channel:uint in _channels) { // don't need to copy the channel that is being copied into the same channel if (channel != channelToCopy) { bitmapData.copyChannel( bitmapData, bitmapData.rect, new Point(), channelToCopy, channel ); } } var bitmap:Bitmap = new Bitmap(bitmapData); bitmap.x = x; addChild(bitmap); } } }