ingroxd
ingroxd

Reputation: 1025

ArrayOutOfBoundsException in Image editing

I'm trying to make a function that flip horizontally a given image. Processing as you know use the PImage type.

Here what I'm trying:

PImage reverseHorOf(PImage toReverse){
  PImage toReturn = createImage(toReverse.width, toReverse.height, ARGB);
  toReturn.loadPixels();
  toReverse.loadPixels();
  for(int i = 0; i < toReverse.height; i ++)
    for(int j = 0; j < toReverse.width; j ++)
      toReturn.pixels[(i * width) + (width - 1 - j)] = toReverse.pixels[(i * width) + j];
  toReverse.updatePixels();
  toReturn.updatePixels();
  return toReturn;
}

@Java-only programmers:

So guys, what I tried is:

What I have is an ArrayOutOfBoundsException: 1499.

I am making a mistake, but... where?

Upvotes: 0

Views: 58

Answers (1)

Andremoniy
Andremoniy

Reputation: 34900

Your formulas seem to be right in tote. But the only one thing which could lead to the mentioned exception here is unknown (for us) variable width. It seems, that this variable is not related to object toReverse and its dimension. I'm sure, you have to fix it like this:

toReturn.pixels[(i * toReverse.width) + (toReverse.width - 1 - j)] = toReverse.pixels[(i * toReverse.width) + j];

Upvotes: 1

Related Questions