package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.BitmapDataChannel; import flash.display.Shape; import flash.filters.BlurFilter; import flash.geom.Point; [SWF(width=600, height=300, backgroundColor=0x000000)] /** * Demonstrates how BitmapData's copyChannel() can be used to copy alpha information into an image. */ public class graphic_flex_image_effects_04_Flex_CopyAlphaChannelTest extends graphic_flex_image_effects_04_Flex_AbstractImageLoader { /** * Constructor. Passes path of image to load to super. */ public function graphic_flex_image_effects_04_Flex_CopyAlphaChannelTest() { super("graphic-flex-image-effects-04-assets-goat.jpg"); } /** * Run after the image loads in super class. This creates a shape and uses the shape's data as an alpha * mask for the loaded image. */ override protected function runPostImageLoad():void { var bitmapData:BitmapData = _loadedBitmap.bitmapData; var width:Number = bitmapData.width; var height:Number = bitmapData.height; // create new 32 bit bitmap data with alpha channel var bitmapCopy:BitmapData = new BitmapData(width, height, true, 0x00000000); // copy in loaded image so that it has alpha data bitmapCopy.copyPixels(bitmapData, bitmapData.rect, new Point()); bitmapData.dispose(); // draws mask shape using drawing API var shape:Shape = createMask(width, height); // draw shape into bitmap data so that its alpha channel can be copied var shapeBitmapData:BitmapData = new BitmapData(width, height, true, 0x00000000); shapeBitmapData.draw(shape); // copy shape's alpha data into loaded image bitmapCopy.copyChannel( shapeBitmapData, shapeBitmapData.rect, new Point(), BitmapDataChannel.ALPHA, BitmapDataChannel.ALPHA ); _loadedBitmap.bitmapData = bitmapCopy; _loadedBitmap.x = width; addChild(_loadedBitmap); } /** * Create a mask shape of the specified width and height. * * @param width The pixel width to draw the mask. * @param height The pixel height to draw the mask. */ private function createMask(width:Number, height:Number):Shape { var shape:Shape = new Shape(); shape.graphics.beginFill(0xFFFFFF); shape.graphics.drawEllipse(30, 30, width-60, height-60); shape.graphics.endFill(); shape.filters = [new BlurFilter(50, 50)]; addChild(shape); return shape; } } }