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 › Strange Index out of bounds problem.
Page Index Toggle Pages: 1
Strange Index out of bounds problem. (Read 170 times)
Strange Index out of bounds problem.
Mar 5th, 2009, 12:00am
 
Quote:
color convolut(int x, int y, float[][] matrix, int matrixsize, PImage img) {
  float rtotal = 0.0;
  float gtotal = 0.0;
  float btotal = 0.0;
  
  int offset = matrixsize / 2; //we need an offset to start at the topleft of the matrix
  
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; i++) {
      int xloc = x + i - offset; //get xpos in kernel
      int yloc = y + j - offset; //get ypos in kernel
      int loc = xloc + img.width*yloc; //get the current pixel
      
      loc = constrain(loc,0,img.pixels.length-1);
      //Source * Image = new color for the tested pixel!
      println(i);
      
      //rtotal += (red(img.pixels[loc]) * matrix[i][j]);
      //gtotal += (green(img.pixels[loc]) * matrix[i][j]);
      //btotal += (blue(img.pixels[loc]) * matrix[i][j]);
    }
  }
  
  //make sure rgb is in range
  rtotal = constrain(rtotal,0,255);
  gtotal = constrain(gtotal,0,255);
  btotal = constrain(btotal,0,255);
  //return the color
  return color(rtotal,gtotal,btotal);
}


Hello everyone,

Currently I've been getting my head around the convolution example by Shiffman, and just when I thought I understood the process of multiplying two arrays I got stuck with some odd behaviour.

Against my expectations variable i and j contain far larger numbers. Nowhere in the code do I use these vars, so I really don't understand where they get their values.

And as per the title, for obvious reasons if I uncomment the actual rtotal(etc) equations I get an out of bounds error.

Someone knows/sees what's going on.

Thnx in advance.
Re: Strange Index out of bounds problem.
Reply #1 - Mar 5th, 2009, 1:33pm
 
Code:

for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; i++) {


i++ in both loops...
Re: Strange Index out of bounds problem.
Reply #2 - Mar 5th, 2009, 2:54pm
 
Aaaaaaaaaaahhhhh.... ***smacks head into wall***.

Thank you, thank you, thank you. Smiley
Page Index Toggle Pages: 1