<< Chapter < Page | Chapter >> Page > |
The magenta and green color values
A magenta pixel is produced by setting the red and blue color bytes to full strength (255) and setting the green color byte to 0. A green pixel is produced by setting the red and blue color bytes to 0 and setting the green color byte to255.
You should be OK by now
Knowing what you now know, you should have no difficulty understanding how the data in the ByteArray object is modified to produce the magenta and green colored areas near the upper-left corner of the middle imageof Figure 1.
Not quite finished yet
Note, however that we haven't modified the actual pixel data in the bitmap yet. So far we have made a copy of all the pixel data in the rectangular region andhave modified the color values in the copy of the pixel data. We still need to write the modified pixel data back into the BitmapData object to actually modify the image.
Put the modified pixel data back into the same rectangular region
Listing 11 calls the setPixels method to store the pixel data that is contained in the rawBytes array back into the same rectangular region of the bitmap image.
rawBytes.position = 0;//this is critical
bitmapData.setPixels(new Rectangle(10,10,50,8),rawBytes);
The position property of the ByteArray object
With one exception, you should have no difficulty understanding the code in Listing 11. That exception has to do with the ByteArray property named position . Here is what the documentation has to say about the position property:
"Moves, or returns the current position, in bytes, of the file pointer into the ByteArray object. This is the point at whichthe next call to a read method starts reading or a write method starts writing."
Whether or not you understand what that means, it is critical that you set the value of the position property to zero before calling the setPixels method to cause all of the data in the array to be written into the BitmapData object. Otherwise, you will get a runtime error.
Some things worth noting
A couple of things are worth noting. First, there was no technical requirement to write the data from the array back into the same rectangularregion from which it was read. It could have been written into a different rectangular region in the same bitmap, it could have been written into severaldifferent rectangular regions, or it could even have been written into a completely different BitmapData object.
No requirement to read the bitmap data
Second, since the code in Listing 10 stored color data into the array that was totally independent of the color values in the BitmapData object, there was no requirement to read the color data from the BitmapData object in the first place. We could simply have instantiated a new ByteArray object and set its length to the product of the width and the height of the rectangular region. Then we couldhave executed the code in Listing 10 to populate the bytes in the array with magenta and green color values. Then we could have executed the code in Listing11 to write the pixel data into an appropriate rectangular region in the BitmapData object.
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with actionscript' conversation and receive update notifications?