<< Chapter < Page Chapter >> Page >

The last statement that is marked as being different gets a reference to the Bitmap object and stores it in the variable named original just like was done in Listing 4.

As before, referencing the content property returns the Bitmap object as type DisplayObject . Therefore, it must be cast to type Bitmap before it can be used for the intended purpose of this program.

Beyond this point - no changes

Beyond this point, the two programs are identical except that the IO error handler was omitted from this program. As I explained earlier, because the imagefile is embedded in the SWF file at compile time, there is no need to worry about IO errors involving the image file at runtime.

Run the programs

I encourage you to run the online versions of the two programs from the web. Then copy the code from Listing 19 through Listing 21. Use that code tocreate Flex projects. Compile and run the projects. Experiment with the code, making changes, and observing the results of your changes. Makecertain that you can explain why your changes behave as they do.

Resources

I will publish a list containing links to ActionScript resources as a separate document.Search for ActionScript Resources in the Connexions search box.

Complete program listings

Complete listings of the MXML code and the ActionScript code for the programs discussed in this lesson are provided below.

Mxml code for the program named bitmap05.

<?xml version="1.0" encoding="utf-8"?><!-- This program illustrates loading an image andmodifying the pixels in the image. --><mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"xmlns:cc="CustomClasses.*"><cc:Driver/></mx:Application>

Actionscript code for the program named bitmap05.

/*Bitmap05 Provides skeleton code for creating a Bitmap object froman image file. Explains the use of the getPixels, setPixels, and
Listing

Actionscript code for the program named bitmap06.

/*Bitmap06 This is an update to Bitmap05 that uses an image that isembedded in the SWF file rather than a separate downloaded image file. This eliminates the requirementto make the following change to the configuration file at:C:\Program Files\Adobe\Flex Builder 3\sdks\3.2.0\ frameworks\flex-config.xml<!-- Prevents SWFs from accessing the network. --><use-network>false</use-network>The behavior of this program is identical to the behavior of Bitmap05.*********************************************************/ package CustomClasses{import flash.display.Bitmap; import flash.display.BitmapData;import flash.geom.Rectangle; import flash.utils.ByteArray;import mx.containers.VBox; import mx.controls.Image;import mx.events.FlexEvent; //====================================================//public class Driver extends VBox {private var image:Image = new Image();public function Driver(){//constructor //Make the VBox visible.setStyle("backgroundColor",0xFFFF00); setStyle("backgroundAlpha",1.0);[Embed("snowscene.jpg")] var img:Class;image.load(img); //Note that the type of completion event specified// here is different from the type of completion // event used in Bitmap05.this.addEventListener(FlexEvent.CREATION_COMPLETE, completeHandler);} //end constructor //--------------------------------------------------////This handler method is executed when the VBox has // been fully created. Note that the type of the// incoming parameter is more specific than was the // case in Bitmap05. However, it isn't used in this// program. private function completeHandler(event:mx.events.FlexEvent):void{ //Get and save a reference to a Bitmap object// containing the content of the image file. This // statement is different from Bitmap05.var original:Bitmap = Bitmap(image.content); //Everything beyond this point is identical to// Bitmap05 except that the IO error handler was // removed. It isn't needed for an embedded image// file.//Set the width and height of the VBox object based // on the size of the original bitmap.this.width = original.width + 10; this.height = 3*original.height + 12;//Encapsulate the bitmap in an Image object and add// the Image object to the VBox. Display it at // x=5 and y=0original.x = 5; original.y = 0;var imageA:Image = new Image(); imageA.addChild(original);this.addChild(imageA);//Clone the original bitmap to create a duplicate.var duplicateB:Bitmap = new Bitmap( original.bitmapData.clone());//Place the duplicate bitmap below the original in // the VBox. There is a six-pixel downward shift// that I am unable to explain at this time. The // shift produces a gap of about six pixels between// the two images. duplicateB.x = 5;duplicateB.y = original.height;var imageB:Image = new Image(); imageB.addChild(duplicateB);this.addChild(imageB);//Modify this duplicate. modify(duplicateB);//Clone the original bitmap to create another// duplicate. var duplicateC:Bitmap = new Bitmap(original.bitmapData.clone()); //Place the duplicateC below the other two in the// VBox. duplicateC.x = 5;duplicateC.y = 2*original.height;var imageC:Image = new Image(); imageC.addChild(duplicateC);this.addChild(imageC);//Modify the pixels as above to add some color to // the image.modify(duplicateC); //Now invert the colors in the top half of this// bitmap. Note that the magenta and green colors // swap positions.invert(duplicateC); } //end completeHandler//--------------------------------------------------////This method modifies the pixels in the incoming // bitmap in a variety of ways.private function modify(bitmap:Bitmap):void{ //Get the BitmapData object from the incoming// Bitmap object. var bitmapData:BitmapData = bitmap.bitmapData;//Process pixels using the getPixels and // setPixels methods.//Get a rectangular array of pixels comprising// 50 columns by 8 rows in a one-dimensional // array of bytes. The bytes are ordered in the// array as row 0, row 1, etc. Each pixel is // represented by four consecutive bytes in ARGB// order. var rawBytes:ByteArray = new ByteArray();rawBytes = bitmapData.getPixels( new Rectangle(10,10,50,8));//Set the colors of the top four rows to magenta// and the color of the bottom four rows to // green. Don't modify alpha.var cnt:uint = 1; while(cnt<rawBytes.length){ if(cnt<rawBytes.length/2){ rawBytes[cnt]= 255; rawBytes[cnt + 1]= 0; rawBytes[cnt + 2]= 255; }else{rawBytes[cnt] = 0;rawBytes[cnt + 1] = 255;rawBytes[cnt + 2] = 0;} //end if-elsecnt += 4;//Increment the counter by 4. }//end while loop//Put the modified pixels back in the bitmapData // object.rawBytes.position = 0;//this is critical bitmapData.setPixels(new Rectangle(10,10,50,8),rawBytes);//Process pixels using the setPixel32 method.//Put a magenta border on the left edge and a// cyan border on the right edge. Note that the // byte values in the 32-bit pixel are in ARGB order// and the border thickness is two pixels. for(var row:uint = 0;row<bitmapData.height; row++){bitmapData.setPixel32(0,row,0xFFFF00FF); bitmapData.setPixel32(1,row,0xFFFF00FF);bitmapData.setPixel32(bitmapData.width - 1, row,0xFF00FFFF);bitmapData.setPixel32(bitmapData.width - 2, row,0xFF00FFFF);}//end for loop//Put a cyan border along the top edge and a // magenta border along the bottom edge.for(var col:uint = 0;col<bitmapData.width; col++){bitmapData.setPixel32(col,0,0xFF00FFFF); bitmapData.setPixel32(col,1,0xFF00FFFF);bitmapData.setPixel32(col,bitmapData.height - 1, 0xFFFF00FF);bitmapData.setPixel32(col,bitmapData.height - 2, 0xFFFF00FF);} //End for loop} //end modify method//--------------------------------------------------////This method inverts all of the pixels in the top // half of the incoming bitmap.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));//Invert the colors by subtracting each color// component value from 255. 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 pixels back in 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

Miscellaneous

This section contains a variety of miscellaneous materials.

Housekeeping material
  • Module name: Fundamentals of Image Pixel Processing
  • Files:
    • ActionScript0132\ActionScript0132.htm
    • ActionScript0132\Connexions\ActionScriptXhtml0132.htm
PDF disclaimer: Although the Connexions site makes it possible for you to download a PDF file for thismodule at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, you should beaware that some of the HTML elements in this module may not translate well into PDF.

-end-

Questions & Answers

it is the relatively stable flow of income
Chidubem Reply
what is circular flow of income
Divine Reply
branches of macroeconomics
SHEDRACK Reply
what is Flexible exchang rate?
poudel Reply
is gdp a reliable measurement of wealth
Atega Reply
introduction to econometrics
Husseini Reply
Hi
mostafa
hi
LEMLEM
hello
Sammol
hi
Mahesh
bi
Ruqayat
hi
Ruqayat
Hi fellas
Nyawa
hey
Sammol
hi
God
hello
Jahara
Good morning
Jorge
hi
abubakar
hi
Nmesoma
hi
Mahesh
Hi
Tom
Why is unemployment rate never zero at full employment?
Priyanka Reply
bcoz of existence of frictional unemployment in our economy.
Umashankar
what is flexible exchang rate?
poudel
due to existence of the pple with disabilities
Abdulraufu
the demand of a good rises, causing the demand for another good to fall
Rushawn Reply
is it possible to leave every good at the same level
Joseph
I don't think so. because check it, if the demand for chicken increases, people will no longer consume fish like they used to causing a fall in the demand for fish
Anuolu
is not really possible to let the value of a goods to be same at the same time.....
Salome
Suppose the inflation rate is 6%, does it mean that all the goods you purchase will cost 6% more than previous year? Provide with reasoning.
Geetha Reply
Not necessarily. To measure the inflation rate economists normally use an averaged price index of a basket of certain goods. So if you purchase goods included in the basket, you will notice that you pay 6% more, otherwise not necessarily.
Waeth
discus major problems of macroeconomics
Alii Reply
what is the problem of macroeconomics
Yoal
Economic growth Stable prices and low unemployment
Ephraim
explain inflationcause and itis degre
Miresa Reply
what is inflation
Getu
increase in general price levels
WEETO
Good day How do I calculate this question: C= 100+5yd G= 2000 T= 2000 I(planned)=200. Suppose the actual output is 3000. What is the level of planned expenditures at this level of output?
Chisomo Reply
how to calculate actual output?
Chisomo
how to calculate the equilibrium income
Beshir
Criteria for determining money supply
Thapase Reply
who we can define macroeconomics in one line
Muhammad
Aggregate demand
Mohammed
C=k100 +9y and i=k50.calculate the equilibrium level of output
Mercy Reply
Hi
Isiaka
Hi
Geli
hy
Man
👋
Bahunda
hy how are you?
Man
ys
Amisha
how are you guys
Sekou
f9 guys
Amisha
how are you guys
Sekou
ys am also fine
Amisha
fine and you guys
Geli
from Nepal
Amisha
nawalparasi district from belatari
Amisha
nd u
Amisha
I am Camara from Guinea west Africa... happy to meet you guys here
Sekou
ma management ho
Amisha
ahile becheclor ho
Amisha
hjr ktm bta ho ani k kaam grnu hunxa tw
Amisha
belatari
Amisha
1st year ho
Amisha
nd u
Amisha
ahh
Amisha
kaha biratnagar
Amisha
ys
Amisha
kina k vo
Amisha
money as unit of account means what?
Kalombe
A unit of account is something that can be used to value goods and services and make calculations
Jim
all of you please speak in English I can't understand you're language
Muhammad
I want to know how can we define macroeconomics in one line
Muhammad
it must be .9 or 0.9 no Mpc is greater than 1 Y=100+.9Y+50 Y-.9Y=150 0.1Y/0.1=150/0.1 Y=1500
Kalombe
Mercy is it clear?😋
Kalombe
hi can someone help me on this question If a negative shocks shifts the IS curve to the left, what type of policy do you suggest so as to stabilize the level of output? discuss your answer using appropriate graph.
Galge Reply
if interest rate is increased this will will reduce the level of income shifting the curve to the left ◀️
Kalombe
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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