Faster Pixel Loop Itteration

Generally Pixels are looped though using 2 loops, 1 for the width and 1 for the height.

Using multiple loops, can always become a difficult way to manage and I wanted to share another way to accomplish a traverse of a full BitmapData.

//import declerations
import flash.geom.Point;
import flash.display.Bitmap;
import flash.display.BitmapData;
//declare your bitmapdata
var bmpd:BitmapData= new BadIdea(1,1)
//declare the bitmap
var bitmap:Bitmap= new Bitmap()
//assign the bitmapdata to the bitmap
//add the bitmap to the stage
//assign the width and height and calculate the total Pixels this image has
var _wide:int=bmpd.width, var _tall:int=bmpd.height, var totalPixels:int=_wide*_tall
//to take advantage of counting downwards add 1 to the totalPixels
var index:int=totalPixels+1
//create 1 loop that will itterate down from the totalPixels
//since we use index-1 in two places, lets limit that amount of times to calculate that
var useIndex:int=index-1
//assign the xCoordinate which is calculated through modolus of the width.
var xPixel:int=useIndex%_wide
//assign the yCoordinate which is calculated by dividing the current index by its width and then floored.  no different then finding PERCENT of a number think of it that way.  0/100 = 0 percent or line 0 1/100 is 1 or line 1 etc
var yPixel:int=int(useIndex/_wide)
//here you can see i set the pixels of the bitmap to red at the given xCoordinate yCoordinate.  This was to show you that all pixels are complete.

Your Comment

You must be logged into post a comment.