-1

I am trying to figure out the formula for the loc position on line 31 but i can't figure it out. Can anyone guide me to a solution?

PImage theImage;
int cellSize = 6; // dimension of a cell where logic is applied
int cols, rows; // number of rows and columns based on cellsize 
int col; // column in the cell
int row; // row in the cell
float edgeR; // (amount of red in the corners)
float edgeB; // (amount of blue in the corners)
float edgeG; // (amount of green in the corners)
float middleR; // (amount of red in the center)
float middleB; // (amount of blue in the center)
float middleG; // (amount of green in the center)

void setup()
 {
  size(1570,1112); // fits the image
  theImage = loadImage("im.png"); // load the image
  cols = width/cellSize ; // number of colums in the grid of the image
  rows = height/cellSize ; // number of rows in the grid of the image
 }

void draw ()
 {
  background(0);
  loadPixels();
  theImage.loadPixels(); // load every pixel of the image in an array
  image(theImage,0,0); // display the image at pos 0 0 
  for ( int i=0; i< rows;i++) {  
    for ( int j = 0; j< cols;j++) {    // loop matrix of cells         
        for ( int x = i*cellSize; x<i*cellSize+cellSize;x++) {
         for ( int y = j*cellSize; y<j*cellSize+cellSize;y++) {  // 
            loop every pixel of the cell

            ---> int loc = (i*cellSize+x) + 
             (j*cellSize*width+y*cellSize*width); // grrrrr  <-----
              float r = red(theImage.pixels[loc]);  // red value
              float g = green(theImage.pixels[loc]); // green value
              float b = blue(theImage.pixels[loc]);  // blue value             
              if (x - i*cellSize <= cellSize /3 ) {   // it is in a column 1
           col = 1;
          }
           else if (x - i*cellSize >= cellSize*2/3 ) { // it is in a column 3
           col = 3;
           }
           else {
           col = 2;   // it is in a column 2
           }
           if (y - j*cellSize <= cellSize /3 ) {  // it is in row 1
           row = 1;
           }

           else if (y - j*cellSize >= cellSize*2/3)  { // it is in row 3
           row = 3;
           }
           else {   // in row 2
           row = 2;
           }                       
          if (col == 1 & row ==1 || (col == 3 & row == 3) || (col == 1 & row == 3) || (col == 3 & row == 1)) {
                edgeR = edgeR + r;   // aggregate the edge color values
                edgeG = edgeG  + g;
                edgeB = edgeB + b;             
           }
         else {
              middleR = middleR + r; // aggregate the center color values
              middleG = middleG + g;
              middleB = middleB + b;

           // here some magic will be applied if there would be no ArrayIndexOutOfBoundsException: 1745840 error

        }    
     }     
   }
}
}
}

When I run the code the output window freezes and I get an ArrayIndexOutOfBoundsException: 1752120 error/ if i put debugging on this also keep saying: debugger busy.

I am pretty sure the loc position is incorrect, but i don't have an idea how to fix the formula. Also the nested for loop might be the issue as well.

Thanks a lot for any help, it is very much appreciated.

All the best,

Tim

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
Tlow
  • 117
  • 1
  • 1
  • 9
  • a quadruple nested loop...what could go wrong! :) seriously, comment out all but the outermost loop , and put in some debug print statements to test the bounds of your loops against what you expect. then add one loop in at a time to diagnose where the problem really is. – Jeremy Sep 13 '17 at 15:28
  • Thanks Jeremy, but my diagnose is that in line 31 where I try to assign a value to r, based on the position loc and because loc is the formula to calculate loc is incorrect it gets a worong value and that causes the error. Or do you mean that this diagnose is incorrect? – Tlow Sep 13 '17 at 15:46
  • 1
    to speak to the answer below...start with a smaller image. say 5 x 5 or 10 x 10...something debuggable. use paper or excel to calculate your expected values for the upper bounds, and then verify whether your formulae give you the expected result. – Jeremy Sep 13 '17 at 16:51
  • Hello, thanks a lot for your reply. I changed the image for an image of 60 by 60 and the line for loc is now: int loc = x + y*width. Then the sketch works fine. But when I then use the bigger image it doesn't work anymore and I get the ArrayIndexOutOfBoundsException. – Tlow Sep 14 '17 at 18:12
  • I now gradually increased the image and it works until i go over 1000 by 1000. I guess it exceeds the limit for indexes in an array at a certain point. A,y way, I will reduce the image and then it will work. Thanks for the help. – Tlow Sep 14 '17 at 18:35
  • there should be no upper bound on array indexes. you can theoritically consume all of the available memory on your machine if the OS would let you. check out also the bounds of your for loops, not just the loc value. also, is this C++, C#? something else? – Jeremy Sep 14 '17 at 18:53
  • Thanks, I ill try a few things this weekend. This is processing, which is a JAVA kind of language. – Tlow Sep 16 '17 at 06:57
  • Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Raedwald Oct 26 '17 at 08:40

2 Answers2

0

I changed

int loc = (i*cellSize+x) + 
         (j*cellSize*width+y*cellSize*width);

with

int loc = x + y*width;

And then it works. The only issue is that if I use a big image I get the ArrayIndexOutOfBoundsException, which is likely caused by exceeding the maximum number of indexes in an array. Thanks for helping me out!

Tlow
  • 117
  • 1
  • 1
  • 9
-1

This is too much code to ask us to debug for you.

You need to break your problem down into smaller steps and do some debugging to isolate your problem. Note that the first step of debugging is stepping through the code with a piece of paper and a pencil, not the debugger itself!

You need to be able to answer these types of questions: What is the value of every variable when you get the error? What values do i, j, x, and y have? What value does loc have? How many indexes does theImage.pixels have?

Note that I'm not asking you to tell me the answers to those questions. You need to answer them yourself, so you can start figuring out which line of code is not behaving how you expected it to.

If you still can't figure it out, you need to narrow your problem down to a MCVE.

Also, just a side note: you should not write entire programs like this and then only test it when it's done. You need to be working in smaller chunks and making sure that chunks works correctly before moving on. In your example, you might have started with just the outer-most for loop, and made sure that iterated over the values you expected it to. Then the second for loop, etc. You should probably also refactor your code so you don't have a quadruple nested for loop.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107