Listing 6 calls the clone method on the original Bitmap object to create a duplicate Bitmap object, and saves the duplicate Bitmap object's reference in the variable named duplicateB .
Clone the original bitmap to create a duplicate bitmap.
var duplicateB:Bitmap = new Bitmap(
original.bitmapData.clone());duplicateB.x = 5;
duplicateB.y = original.height;var imageB:Image = new Image();imageB.addChild(duplicateB);
this.addChild(imageB);
Display the duplicate bitmap
Then Listing 6 adds the duplicate bitmap to a new Image object, positions the duplicate bitmap immediately below the top image in Figure 1 and adds the new image to the VBox . This is the middle image in Figure 1.
Another curious circumstance
Curiously, the middle image in Figure 1 is five or six pixels further down than I expected it to be. This produces a gap of five or six pixels between thetop two images in Figure 1. I am unable to explain the reason for the gap at this time, but I suspect that it may have something to do with the layout rulesof the VBox container object. When I place two or more ordinary Image objects in a VBox container, they appear in a vertical stack separated by about five or six pixels. However, that is totalspeculation on my part.
Modify the duplicate Bitmap
Listing 7 calls the modify method passing a reference to duplicateB as a parameter. This causes the middle image in Figure 1 to be modified in two different ways.
- First, the magenta and green rows of pixels are inserted near the upper left corner.
- Then a colored border two pixels thick is inserted around the four edges of the bitmap.
Modify the duplicate bitmap.
modify(duplicateB);
Explain the modify method
At this point, I will put the explanation of the complete event handler on hold and explain the method named modify .
Beginning of the modify method
The modify method begins in Listing 8.
Beginning of the modify method.
private function modify(bitmap:Bitmap):void{
var bitmapData:BitmapData = bitmap.bitmapData;
The incoming Bitmap object encapsulates a BitmapData object, which is referenced by a property of the Bitmap object named bitmapData . Listing 8 gets a copy of that reference and saves it in a local variable named bitmapData .
Process pixels using the getPixels and setPixels methods
Listing 9 begins by instantiating a new empty object of type ByteArray .
Process pixels using the getpixels and setpixels methods.
var rawBytes:ByteArray = new ByteArray();
rawBytes = bitmapData.getPixels(new Rectangle(10,10,50,8));
The ByteArray class
According to the documentation ,
"The ByteArray class provides methods and properties to optimize reading, writing, and working with binary data."
A ByteArray object is an object that can be used to store and access data using either square bracket notation []or method calls. The main benefit of using this data structure from our viewpoint is that itwill decompose the 32-bit integers into 8-bit bytes and allow us access the pixel data one byte at a time. Otherwise it would be necessary for us to performthe decomposition ourselves using bit shift operations.