This page is optimized for mobile devices, if you would prefer the desktop version just click here

0.11 Fundamentals of image pixel processing  (Page 14/17)

A comparison

If you compare the top half of the bottom image in Figure 1 with the top half of the other two images, you can see the dramatic effect of color inversion.However, all that is required to exactly restore the original colors is to run the inverted color pixels through the inversion process again.

Because of these characteristics, some major software products use color inversion to change the colors in an image that has been selected forprocessing to provide a visual indication that it has been selected.

The color inversion algorithm

To invert the color of a pixel, you simply subtract the red, green, and blue color values from 255 without modifying the alpha value. To reverse the process,you simply subtract the inverted color values from 255 again, which produces the original color values.

Beginning of the invert method

The invert method begins in Listing 15.

Beginning of the invert method.

private function invert(bitmap:Bitmap):void{ //Get the BitmapData object.var bitmapData:BitmapData = bitmap.bitmapData; //Get a one-dimensional byte array of pixel data// from the top half of the bitmapData object var rawBytes:ByteArray = new ByteArray();rawBytes = bitmapData.getPixels(new Rectangle( 0,0,bitmapData.width,bitmapData.height/2));

The code in Listing 15 gets the BitmapData object encapsulated in the incoming Bitmap object and extracts the pixel data from a rectangle that comprises the entire top half of the bitmapdata into a ByteArray object.

Apply the inversion algorithm

Listing 16 applies the color inversion algorithm to all of the pixel data in the ByteArray object by subtracting each color value from 255 and storing the result back into the same element of the ByteArray object.

Apply the inversion algorithm.

var cnt:uint = 1; while(cnt<rawBytes.length){ rawBytes[cnt]= 255 - rawBytes[cnt];rawBytes[cnt + 1] = 255 - rawBytes[cnt + 1]; rawBytes[cnt + 2]= 255 - rawBytes[cnt + 2];cnt += 4;//increment the counter }//end while loop

Put the modified pixel data back into the BitmapData object

Listing 17 calls the setPixels method to put the modified pixel data back into the BitmapData object producing the final output shown in the bottom image of Figure 1.

Put the modified pixel data back into the bitmapdata object.

rawBytes.position = 0;//this is critical bitmapData.setPixels(new Rectangle(0,0,bitmapData.width,bitmapData.height/2), rawBytes);} //end invert method//--------------------------------------------------//} //end class } //end package

This is a case where the new pixel color values depend on the original color values. Therefore, it was necessary to get and use the old color values tocompute the new color values.

An interesting side note

If you compare the two color bars in the upper-left corner of the middle and bottom images in Figure 1, you will see that they appear to have swappedpositions. This is because the numeric value of magenta is the inverse of the numeric value of cyan and vice versa. The same is true of the cyan and magentaborders.

<< Chapter < Page Page > Chapter >>

Read also:

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.
Jobilize.com uses cookies to ensure that you get the best experience. By continuing to use Jobilize.com web-site, you agree to the Terms of Use and Privacy Policy.