We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Array Index Out of Bounds in image processing
Page Index Toggle Pages: 1
Array Index Out of Bounds in image processing (Read 807 times)
Array Index Out of Bounds in image processing
Jan 9th, 2010, 1:57am
 
I'm just going throught the image tutorial on how to import an manipulate jpgs and whatnot. I changed the code a little, but I can't figure out why I'm getting this error... its always when the 3rd image (2.jpg) is loading. I tried duplicating the 2nd image, but it seems to be the code...

oh and I'm only displaying every 4 pixels to make the framerate faster.

Code:

PImage pimage;
int i = 0;
int timer = 1;

void setup(){
 colorMode(HSB,100);
 background(0,0,0);
 frameRate(30);
 size(640, 480);  
 pimage = loadImage("0.jpg");
}
void draw(){

   println("timer is"+timer);
   if(timer>=60){
     pimage = loadImage(i+".jpg");
     i++;
     timer=1;
     println("i is now "+i +".jpg");
     int loc=0;
   }  
     loadPixels();
     // Since we are going to access the image's pixels too  
     pimage.loadPixels();
     for (int y = 0; y < height; y+=4) {
       for (int x = 0; x < width; x+=4) {
         int loc = x + y*width;
     
         // The functions red(), green(), and blue() pull out the 3 color components from a pixel.
         float r = red(pimage.pixels[loc]);
         float g = green(pimage.pixels[loc]);
         float b = blue(pimage.pixels[loc]);
     
         // Image Processing would go here
         // If we were to change the RGB values, we would do it here, before setting the pixel in the display window.
         //float adjustBrightness = ((float) mouseX / width) * 8.0;
         float adjustBrightness = timer/2;
         
         r *= adjustBrightness/4;
         g *= adjustBrightness;
         b *= adjustBrightness;
         // Constrain RGB to between 0-255
         r = constrain(r,0,255);
         g = constrain(g,0,255);
         b = constrain(b,0,255);
         // Make a new color and set pixel in the window
         color c = color(r,g,b);
         pixels[loc] = c;

         // Set the display pixel to the image pixel
         pixels[loc] =  color(r,g,b);  
         
       }
     }
     
     updatePixels();
   timer++;
 //}
}
Re: Array Index Out of Bounds in image processing
Reply #1 - Jan 9th, 2010, 2:14am
 
Note: you should make a good habit of putting the size() call at the start of setup().

When reporting an error, it is better to point out which line is involved.
Are all your images of same size, which should be 640x480?
Re: Array Index Out of Bounds in image processing
Reply #2 - Jan 9th, 2010, 2:28am
 
yeah sorry.... the error is on line 30

ohh and i resized all the images to be exact size... and no more errors!!

I guess it doesn't store pixel info thats out of frame, or becomes problematic...

thanks!
Page Index Toggle Pages: 1