Reputation: 1025
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:
createImage
is a "substitute" for new PImage
;loadPixels()
and updatePixels()
are needed to [You don't say?!?] load and update the pixels of an image or the frame;PImage
for two reasons: 1) Syntax meanings; 2) Semantic meaning: i can't copy a whole image if I start to modify part of it =PSo guys, what I tried is:
(i * width)
;toReturn
;toReverse
;What I have is an ArrayOutOfBoundsException: 1499
.
I am making a mistake, but... where?
Upvotes: 0
Views: 58
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