views:

57

answers:

1

How can I get the bytearray data from an image that is under certain shape, for example a circule or square?

Let's say I want to modify ONLY the pixel inside this circule, how can I get this Bytearray data? alt text Any ideas?

+2  A: 

Define a rectangle containing the circle, relative to the top left of the image.

var radius:Number = 100;
var centerX:Number = 50;
var centerY:NUmber = 400;

var rect:Rectangle = new Rectangle(centerX-radius, centerY-radius, radius*2, radius*2);

Then use the getPixels() to return a ByteArray of the pixels inside the rectangle. Now you can loop through each pixel and check if it's contained inside the circle.

var image:BitmapData;
var pixels:ByteArray = image.getPixels(rect);

for(var x:int; x<rect.width; x++){
    for(var y:int=0; y<rect.height; y++){
        // Read the pixels data ->
        var pixel:uint = pixels.readUnsignedInt();
        // Check this pixels distance from the center to make sure it is inside the circle.
        var dx:Number = x - radius;
        var dy:Number = y - radius;
        if(dx*dx+dy*dy <= radius*radius){
            // This pixel is inside the circle.
            ...
        }
    }
}

Then once you've modified it if you want you can write it back to the image using setPixels()

image.setPixels(rect, pixels);

I haven't actually used or tested any of this so it might not work.
It might also be easier to work with the data if you use getVector() and setVector() instead.

cmann