<< 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

what does preconceived mean
sammie Reply
physiological Psychology
Nwosu Reply
How can I develope my cognitive domain
Amanyire Reply
why is communication effective
Dakolo Reply
Communication is effective because it allows individuals to share ideas, thoughts, and information with others.
effective communication can lead to improved outcomes in various settings, including personal relationships, business environments, and educational settings. By communicating effectively, individuals can negotiate effectively, solve problems collaboratively, and work towards common goals.
it starts up serve and return practice/assessments.it helps find voice talking therapy also assessments through relaxed conversation.
miss
Every time someone flushes a toilet in the apartment building, the person begins to jumb back automatically after hearing the flush, before the water temperature changes. Identify the types of learning, if it is classical conditioning identify the NS, UCS, CS and CR. If it is operant conditioning, identify the type of consequence positive reinforcement, negative reinforcement or punishment
Wekolamo Reply
please i need answer
Wekolamo
because it helps many people around the world to understand how to interact with other people and understand them well, for example at work (job).
Manix Reply
Agreed 👍 There are many parts of our brains and behaviors, we really need to get to know. Blessings for everyone and happy Sunday!
ARC
A child is a member of community not society elucidate ?
JESSY Reply
Isn't practices worldwide, be it psychology, be it science. isn't much just a false belief of control over something the mind cannot truly comprehend?
Simon Reply
compare and contrast skinner's perspective on personality development on freud
namakula Reply
Skinner skipped the whole unconscious phenomenon and rather emphasized on classical conditioning
war
explain how nature and nurture affect the development and later the productivity of an individual.
Amesalu Reply
nature is an hereditary factor while nurture is an environmental factor which constitute an individual personality. so if an individual's parent has a deviant behavior and was also brought up in an deviant environment, observation of the behavior and the inborn trait we make the individual deviant.
Samuel
I am taking this course because I am hoping that I could somehow learn more about my chosen field of interest and due to the fact that being a PsyD really ignites my passion as an individual the more I hope to learn about developing and literally explore the complexity of my critical thinking skills
Zyryn Reply
good👍
Jonathan
and having a good philosophy of the world is like a sandwich and a peanut butter 👍
Jonathan
generally amnesi how long yrs memory loss
Kelu Reply
interpersonal relationships
Abdulfatai Reply
What would be the best educational aid(s) for gifted kids/savants?
Heidi Reply
treat them normal, if they want help then give them. that will make everyone happy
Saurabh
What are the treatment for autism?
Magret Reply
hello. autism is a umbrella term. autistic kids have different disorder overlapping. for example. a kid may show symptoms of ADHD and also learning disabilities. before treatment please make sure the kid doesn't have physical disabilities like hearing..vision..speech problem. sometimes these
Jharna
continue.. sometimes due to these physical problems..the diagnosis may be misdiagnosed. treatment for autism. well it depends on the severity. since autistic kids have problems in communicating and adopting to the environment.. it's best to expose the child in situations where the child
Jharna
child interact with other kids under doc supervision. play therapy. speech therapy. Engaging in different activities that activate most parts of the brain.. like drawing..painting. matching color board game. string and beads game. the more you interact with the child the more effective
Jharna
results you'll get.. please consult a therapist to know what suits best on your child. and last as a parent. I know sometimes it's overwhelming to guide a special kid. but trust the process and be strong and patient as a parent.
Jharna
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