topical media & game development

talk show tell print

#graphic-flex-image-effects-04-Flex-CopyChannelTest.ax

#graphic-flex-image-effects-04-Flex-CopyChannelTest.ax [swf] [flash] flex


  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 @ax-graphic-flex-image-effects-04-Flex-CopyChannelTest extends graphic_flex_image_effects_04_Flex_AbstractImageLoader {
  
                  private var _channels:Vector.<uint>;
  
                  
Constructor. Passes path of image to load to super class and pushes references to all three color channel constants into a vector.

  
                  public function @ax-graphic-flex-image-effects-04-Flex-CopyChannelTest() {
                          super("graphic-flex-image-effects-04-assets-goat.jpg");
                          _channels = new Vector.<uint>();
                          _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.
parameter: bitmapData The image to copy channel data to and from.
parameter: channelToCopy The constant specifying the channel to copy.
parameter: 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);
                  }
  
          }
  
  }
  


(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.