Code optimization

Hey there,

I’m currently building a flashapp in which I have to access the pixelinformation from a webcamfeed and store the individual values within a array for further work with the accumulated data.
Now while I have already accomplished this, I’m unfortunately also running into a performance problem… As long as the webcamfeed is somewhere below 320240 I can sustain about 25 frames per second, but as soon as I go up to 640480 the framerate goes below 10 and with HD even to 1-2.
This of course is not the performance I desire and while I don’t expect flash to cope with HD I would like for it to be able to work with the average webcam which sits at about 640*480.

Down below you will see a very stripped down version of the app which only implements the capture of the information. I’m sorry that it’s still a lot of code, but I can’t strip it down anymore without breaking it.

What I’m asking you guys now, is how can I optimize the logic that you’ll find in the [COLOR=“Red”]loop [/COLOR]function (I have annotated every segment and hope you’ll get what I’m doing there)?

if you have any questions, feel free to ask.

I’m not an AS3 expert and would really appreciate some help.

Thanks a lot!

package 
{
	

	import flash.display.MovieClip;
	import flash.media.Camera;
	import flash.media.Video;
	import flash.events.Event;
	import flash.display.Sprite;
	import flash.display.BitmapData;
	import flash.display.Bitmap;
	
	[SWF(width="1200", height="800", frameRate="30", Backgroundcolor="0xEEEEEE")]
	
	public class Lightpainter extends Sprite
	{
		private var cam:Camera;
		private var vid:Video;
		
		private var vid_bmd:BitmapData;
		
		private var imgWidth:int;
		private var imgHeight:int;
		
		private var bmp:Bitmap;
		
		public function Lightpainter()
		{
			
			this.addEventListener(Event.ADDED_TO_STAGE, setUpCam);				
			
		}
		
		
		private function setUpCam(e:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE, setUpCam);
			
			
			if (! Camera.getCamera())
			{
				trace("NO CAMERA");
			}
			else
			{			
				cam = Camera.getCamera();
				trace(cam);
				
				imgWidth = cam.width;
				imgHeight = cam.height;
				trace(imgWidth, imgHeight);
				
				cam.setMode(imgWidth, imgHeight, 30);			
				vid = new Video(imgWidth, imgHeight);
				vid.attachCamera(cam);
				

				setupBitmap();	
                                this.addEventListener(Event.ENTER_FRAME, loop);
			}
		}

		
		private function setupBitmap():void
		{
			
			vid_bmd = new BitmapData(imgWidth, imgHeight);
			
			initializeArrays();
		}
		
		
		
		private function initializeArrays():void
		{
			var pixelnumber:uint = imgWidth*imgHeight;
			
			rArray = new Vector.<uint>(pixelnumber); 
			gArray = new Vector.<uint>(pixelnumber);
			bArray = new Vector.<uint>(pixelnumber);
		}
		

		private var rArray:Vector.<uint>;
		private var gArray:Vector.<uint>;
		private var bArray:Vector.<uint>;

		private function [COLOR="Red"]loop[/COLOR](ev:Event):void
		{
                       //The video with our webcam attached to it gets drawn to a BitmapDataObject
			vid_bmd.draw(vid);
			
                        //The variables used in every run of the nested loop are initialized
			var pos:uint = 0;
			var pixel:uint = 0;
			var r:uint = 0;
			var g:uint = 0;
			var b:uint = 0;
			
			//loops through the BitmapDataObject
			for (var by:int = 0; by < imgHeight; ++by)
			{
				for (var bx:int = 0; bx < imgWidth; ++bx)
				{	
					//We grab a pixel from the BMDO
					pixel = vid_bmd.getPixel(bx, by);
					
                                        //We seperate the uint(pixel) into the 3 different channels
					r = (pixel >> 16) & 0xFF;
					g = (pixel >> 8) & 0xFF;
					b = (pixel) & 0xFF;
					
                                        //The color-channel-values are added to their respective position in the array
					rArray[pos] += r;
					gArray[pos] += g;
					bArray[pos] += b;
                                        
                                        //the position within the BMDO gets incremented
					pos++;
				}
			}
	    }
		
	}
	
}