<< Chapter < Page Chapter >> Page >

The processChromaKey method begins in Listing 4.

Beginning of the processchromakey method.

private function processChromaKey(bitmap:Bitmap):void{ //Get the BitmapData object.var bitmapData:BitmapData = bitmap.bitmapData; //Populate a one-dimensional byte array of pixel// data from the bitmapData object. Note that the // pixel data format is ARGB.var rawBytes:ByteArray = new ByteArray(); rawBytes = bitmapData.getPixels(new Rectangle(0,0,bitmapData.width,bitmapData.height));

A reference to a Bitmap object

This method receives a reference to a Bitmap object as an incoming parameter.

It identifies all of the pixels in the incoming bitmap with a pure magenta color and sets the alpha bytes for those pixels to a value of zero.

This causes those pixels to become transparent as shown by the bottom image in Figure 1.

Nothing new in Listing 4

I explained all of the code in Listing 4 in the earlier lesson titled Fundamentals of Image Pixel Processing.

The ByteArray object is populated with pixel data

When the getPixels method returns in Listing 4, the pixels from a rectangular region that encompasses the entire bitmap are stored in the ByteArray object referred to by rawBytes .

The organization of the pixel data

The array is populated with the bitmap pixel data from the rectangular region on a row by row basis.

The first four bytes in the array belong to the pixel in the upper-left corner of the rectangular region. The next four bytes belong to the pixelimmediately to the right of that one, and so on.

Four bytes per pixel

Each set of four bytes represents one pixel in ARGB format. In other words, the first byte in the four-byte group is the alpha byte. That byte is followedby the red byte, the green byte, and the blue byte in that order.

This information is critical when time comes to use the data in the array to modify the bitmap data.

Set selected alpha values to zero

The code in Listing 5 iterates through the entire set of image pixels and sets selected alpha values to zero.

Set selected alpha values to zero.

//Declare working variables. Note that there is // no requirement to deal with the green color// value in this case of testing for magenta. var cnt:uint;var red:uint; var green:uint;var blue:uint;for (cnt = 0; cnt<rawBytes.length; cnt += 4) { //alpha is in rawBytes[cnt]red = rawBytes[cnt + 1];green = rawBytes[cnt + 2];blue = rawBytes[cnt + 3];if ((red == 255)&&(green == 0 )&&(blue == 255)) {//The color is pure magenta. Set the value// of the alpha byte to zero. rawBytes[cnt]= 0; }//end if}//end for loop

A for loop

After declaring some working variables, Listing 5 iterates through all of the data in the ByteArray object. It extracts the red, green, and blue color bytes from each four-byte group and tests to see if red and blue are bothset to full intensity with a value of 255 and green is set to zero.

If true, this is interpreted to match the magenta background color, and the value of the alpha byte in that four-bit group is set to zero.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with actionscript. OpenStax CNX. Jun 04, 2010 Download for free at http://cnx.org/content/col11202/1.19
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with actionscript' conversation and receive update notifications?

Ask