Previous Page
Next Page

Recipe 8.9. Copying Channels

Problem

You want to copy the red, green, blue, or alpha from one BitmapData to another.

Solution

Use the copyChannel( ) method of the BitmapData class.

Discussion

The copyChannel( ) method is yet another method that draws information from one bitmap to another. Actually, the first three arguments are the same as the copyPixels( ) method. In addition, it has source channel and destination channel parameters:

bitmap.copyPixels(sourceBmp, srcRect, destPoint,
               srcChannel, destChannel);

The two channel parameters can be one of the integers 1, 2, 4, or 8, which represent the red, green, blue, and alpha channels, respectively. Rather than risking a typographical error, you should use the static properties of the BitmapDataChannel class: RED, GREEN, BLUE, and ALPHA.

You simply tell the method which channel you want to take from the original image and which channel you want to copy it into in the destination image. The following code copies the red, green, and blue channels of a loaded image to a new bitmap, slightly offset from one another:

var loaderBmp:Bitmap = Bitmap(loader.content);
bitmap.copyChannel(loaderBmp.bitmapData,
                loaderBmp.bitmapData.rect,
                new Point(  ),
                BitmapDataChannel.RED,
                BitmapDataChannel.RED);

bitmap.copyChannel(loaderBmp.bitmapData,
                loaderBmp.bitmapData.rect,
                new Point(5, 5),
                BitmapDataChannel.GREEN,
                BitmapDataChannel.GREEN);

bitmap.copyChannel(loaderBmp.bitmapData,
                loaderBmp.bitmapData.rect,
                new Point(10, 10),
                BitmapDataChannel.BLUE,
                BitmapDataChannel.BLUE);

See Also

Recipes 8.5, 8.6, 8.7, 8.8, 8.10, and 8.11 for other ways to add graphical content to a bitmap.


Previous Page
Next Page
Converted from CHM to HTML with chm2web Pro 2.85 (unicode)