Brand New the Processing - Need help!

 PImage img;

void setup() {
  size(1000, 1000);
  img = loadImage( "rickroll.jpg" );
}

void draw() {
  loadPixels();
  img.loadPixels();
  
  for (int x = 0; x < img.width; x++ ) {
    for (int y = 0; y < img.height; y++ ) {
     
      int loc = x + y*img.width;

      float r = red  (img.pixels[loc]);
      float g = green(img.pixels[loc]);
      float b = blue (img.pixels[loc]);

   
      float distance = dist(x, y, mouseX, mouseY);

      float adjustBrightness = map(distance, 0, 50, 8, 0);
      r *= adjustBrightness;
      g *= adjustBrightness;
      b *= adjustBrightness;

     
      r = constrain(r, 0, 255);
      g = constrain(g, 0, 255);
      b = constrain(b, 0, 255);

     
      color c = color(r, g, b);
      pixels[loc] = c;
    }
  }

  updatePixels();
}

The file rickroll.jpg is in the right place, but Processing says that
ArrayIndexOutOfBoundsException: Index 40600 out of bounds for length 40000

for this line: pixels[loc] = c;
I know this is beginner stuff. My brain is just having the hardest time right now. Can you help?

How do you know your “rickroll.jpg” is 1000 x 1000?

Yeah, ive no idea what size it is. :woman_facepalming:

How do I find that out?

A more streamlined approach is to make canvas’ size() match the image’s width & height dimensions inside callback settings():

static final String IMAGE_FILENAME = "rickroll.jpg";
PImage img;

void settings() {
  img = loadImage(IMAGE_FILENAME);
  size(img.width, img.height);
}

Hmm. Where does the settings callback go? I am not sure how that works.

Just place it before setup ()

Delete size command in setup () maybe

Check settings() / Reference / Processing.org