Arrays... aarrrgh

edited October 2013 in Questions about Code

Hi folks,

I'm having a hard time trying to fill a 96x96 array with a portion of an image pixel's brightness data. The image is a long TGA (1920x96) and the array fills with a square selection of 96x96 pixels from the image. This square "window" must move to the right 1px and capture again, etc, like a scanner of some sort. The array operation returns an OutOfBounds error, and for the love of me I can't say why. Code:

PImage displacer;

// Displacer collection image resolution
int coll_res;

// Displacer window resolution
int res;

// Displacer shift
int shift;
int shift_speed;

void setup() {
  size(displayWidth/2, displayHeight/2, P3D);
  noSmooth();
  noFill();
  stroke(255);
  background(0);
  displacer = loadImage("bands1_96.tga");

  // Displacers collection image resolution
  coll_res = 1920;

  // Displacer window resolution
  res = 96;

  // Displacer shift speed in px/second
  shift_speed = 1;

  // Displacer shift state
  shift = -1;
}

void draw() {
  background(0);

  // Shift movement
  if (shift > coll_res) {
    shift = -1;
  }  
  shift += shift_speed;

  float displacerArray[][] = new float[res][res];

  displacer.loadPixels();

  for (int x = shift; x < shift+res; x++) {
    for (int y = 0; y < res; y++ ) {
      // Calculate the 1D pixel location
      int loc = x + y*res;
      // Get normalized (max. value = 1) brightness values from image
      displacerArray[x][y] = (brightness(displacer.pixels[loc]))/255;
    }
  }
}

And here's the image: bands1_96.tga

Any suggestion welcome...

Answers

  • Answer ✓

    I think you will find the error in line 52 should be

      displacerArray[x-shift][y] = (brightness(displacer.pixels[loc]))/255;
    
  • Thanks a lot! This has been answered.

Sign In or Register to comment.