11 August 2006

Simple BitmapData.getPixels() / ByteArray Example

Written by Richard Leggett ( Contact the author of this post )
Published on August 11th, 2006 @ 04:54:29 am, using 295 words, 1158 views
Categories: Flash

I found the Flex 2 docs didn't really contain an example of using the pixel data getPixels() puts into a ByteArray. I wanted to take the pixels in that ByteArray and have them explode or move around in a controlled fashion not obtainable with a simple Convolution Matrix or DisplacementMap.

I'm not going to go through all of that, but just a quick example of getting data back out of a ByteArray and displaying it again as a copy. Normally copyPixels() would be the better option for this specific task, but this as I say gives the opportunity to move the pixels around indepentently in 3D for example.

If you have the Flash 9 preview, you can simple create a movieclip on stage, named mc and put the following code on frame 1:

import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.geom.Rectangle;
import flash.utils.ByteArray;

var imageWidth:uint = mc.width;
var imageHeight:uint = mc.height;

var srcBmp:BitmapData = new BitmapData( imageWidth, imageHeight );
srcBmp.draw( mc );

var pixels:ByteArray = srcBmp.getPixels( new Rectangle(0,0,imageWidth,imageHeight) );
var copyBmp:BitmapData = new BitmapData( imageWidth, imageHeight, true );

pixels.position = 0;

for( var i:uint=0; i<imageHeight ; i++ )
{
  for( var j:uint=0; j<imageWidth; j++ )
  {
    copyBmp.setPixel( j, i, pixels.readUnsignedInt() );
  }
}

var bmp:Bitmap = new Bitmap( copyBmp );
bmp.x = 100;
addChild( bmp );

Probably the only thing that needs pointing out at all is that you can read out a pixel at a time using ByteArray.readUnsignedInt(), this automatically increments the ByteArray's .position by the required number of bytes. The reason for this is that BitmapData.getPixels() returns a ByteArray of unsigned ints of course (range 0 to 4,294,967,295 - 32bits of data, which allows for the 24-bits of colour, and 8 bits of alpha).

Comments, Pingbacks:

Comment from: Stefan Richter
way over my head man, sounds cool though :-)
PermalinkPermalink 11/08/06 @ 05:32
Comment from: Tink
Nice

Shame there no setPixels() method.

I mean we craved getPixels() so we didn't have to do that loooong loop, but if you gonna build the image back up in Flash, ya still have too (although if you were just copying the whole thing you wouldn't take this approach). That said it would be nice to be able to apply a ByteArray directly to a BitmapData.
PermalinkPermalink 11/08/06 @ 07:55
Comment from: matthias
I'm afraid that you made a typo.
Line 16 you typed 'imageheight' instaid of 'imageHeight'.
PermalinkPermalink 12/08/06 @ 03:48
Comment from: Richard Leggett [Member] Email
Thankyou matthias, typo fixed.
PermalinkPermalink 13/08/06 @ 08:19

Comments are closed for this post.

About

Richard Leggett is a Freelance RIA and Web Developer (download CV, skills/portfolio coming soon). He is co-author of Foundation Flash Applications for Mobile Devices (Friend of ED), an Adobe Community Expert and speaker at industry conferences and user groups.



July 2008
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      

Categories

Miscellany

XML Feeds

Information

Contact the admin  /   b2evo template by Two18 Media