We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › NegativeArraySizeException
Page Index Toggle Pages: 1
NegativeArraySizeException (Read 1389 times)
NegativeArraySizeException
Sep 24th, 2009, 5:30am
 
Hi, I got the error NegativeArraySizeException as soon as

PImage imgCell9 = imgSource.get(288, 0, cellWidth, cellHeight); // Assign

gets uncommented, I can't get futher then 8 for some stange reason since it should give a problem when it gets higher then 64.
Here's the image i use:
...

Code:
PImage imgSource;
PImage[] imgCell; // Declare
int cellWidth = 32;
int cellHeight = 32;
int cells; //total ammount of cells
int rows, columns;


void setup(){
size(500, 500);

imgSource = loadImage("test.png");
columns = imgSource.width/cellWidth;
rows = imgSource.height/cellHeight;
cells = rows*columns;
imgCell = new PImage[cells]; // Create

noLoop();
}

void draw(){

PImage imgCell0 = imgSource.get(0, 0, cellWidth, cellHeight); // Assign
PImage imgCell1 = imgSource.get(32, 0, cellWidth, cellHeight); // Assign
PImage imgCell2 = imgSource.get(64, 0, cellWidth, cellHeight); // Assign
PImage imgCell3 = imgSource.get(96, 0, cellWidth, cellHeight); // Assign
PImage imgCell4 = imgSource.get(128, 0, cellWidth, cellHeight); // Assign
PImage imgCell5 = imgSource.get(160, 0, cellWidth, cellHeight); // Assign
PImage imgCell6 = imgSource.get(192, 0, cellWidth, cellHeight); // Assign
PImage imgCell7 = imgSource.get(224, 0, cellWidth, cellHeight); // Assign
PImage imgCell8 = imgSource.get(256, 0, cellWidth, cellHeight); // Assign
/* PImage imgCell9 = imgSource.get(288, 0, cellWidth, cellHeight); // Assign
PImage imgCell10 = imgSource.get(320, 0, cellWidth, cellHeight); // Assign
PImage imgCell11 = imgSource.get(352, 0, cellWidth, cellHeight); // Assign
PImage imgCell12 = imgSource.get(384, 0, cellWidth, cellHeight); // Assign
PImage imgCell13 = imgSource.get(416, 0, cellWidth, cellHeight); // Assign
*/

image(imgCell0, 0, 0);
image(imgCell1, 32, 0);
image(imgCell2, 64, 0);
image(imgCell3, 96, 0);
image(imgCell4, 128, 0);
image(imgCell5, 160, 0);
image(imgCell6, 192, 0);
image(imgCell7, 224, 0);
image(imgCell8, 256, 0);
/* image(imgCell9, 288, 0);
image(imgCell10, 320, 0);
image(imgCell11, 352, 0);
image(imgCell12, 384, 0);
image(imgCell13, 416, 0);
*/
}
Re: NegativeArraySizeException
Reply #1 - Sep 24th, 2009, 6:21am
 
Sorry, but it doesn't make sense. I think such exception occurs if you have cells variable negative, so it should be unrelated to the line you mention.

Let's push you up a bit:
Code:
PImage imgSource;
PImage[] imgCell; // Declare
int cellWidth = 32;
int cellHeight = 32;
int cells; //total amount of cells
int rows, columns;

void setup() {
 size(500, 500);

 imgSource = loadImage("testwh.png");
 columns = imgSource.width/cellWidth;
 rows = imgSource.height/cellHeight;
 cells = rows*columns;
 imgCell = new PImage[cells]; // Create
 for (int i = 0; i < columns; i++) {
   imgCell[i] = imgSource.get(i * cellWidth, 0, cellWidth, cellHeight); // Assign
 }

 noLoop();
}

void draw() {
 for (int i = 0; i < columns; i++) {
   // +4 to space them a bit to show the image splitting in action
   image(imgCell[i], i * (cellWidth + 4), 0);
 }
}

Ensure the image is in the data folder, of course, perhaps you get a negative dimension because it isn't found.
I let you try and extend the sketch to handle rows as well... Smiley
Re: NegativeArraySizeException
Reply #2 - Sep 24th, 2009, 7:51am
 
thx a lot,
why do you get the for loop to work and I not? I mean i can't see what i did wrong (talking about the other topic).

anyway this is how i do it:
(I have my doubts about the cellID and the resetting of it, it meight be confusing when things get bigger but i don't know another solution.

Code:
PImage imgSource;
PImage[] imgCell; // Declare
int cellWidth = 32;
int cellHeight = 32;
int cells; //total amount of cells
int rows, columns;
int cellID = 0;

void setup() {
size(500, 500);

imgSource = loadImage("test.png");
columns = imgSource.width/cellWidth;
rows = imgSource.height/cellHeight;
cells = rows*columns;
imgCell = new PImage[cells]; // Create

for (int i = 0; i < columns; i++) {
for (int j = 0; j < rows; j++){
imgCell[cellID] = imgSource.get(i * cellWidth, j * cellHeight, cellWidth, cellHeight); // Assign
cellID ++;
}
}
cellID = 0;//reset cellID

noLoop();
}

void draw() {
for (int i = 0; i < columns; i++) {
for (int j = 0; j < rows; j++){
// +4 to space them a bit to show the image splitting in action
image(imgCell[cellID], i * (cellWidth + 1), j * (cellHeight + 1));
cellID ++;
}
}
}
Re: NegativeArraySizeException
Reply #3 - Sep 24th, 2009, 8:20am
 
Quote:
why do you get the for loop to work and I not? I mean i can't see what i did wrong (talking about the other topic).

You should re-read my answers in the other topic. And perhaps read a bit on Java variable declaration and scope.

Your way is fine. An alternative way is to compute the cellID, a bit like we do when using the pixels[] array: replace [cellID] with [i * rows + j] (not obvious at first sight, but makes senses after a while... Smiley).
Re: NegativeArraySizeException
Reply #4 - Sep 24th, 2009, 9:01am
 
Quote:
bla bla bla [i * rows + j]


really clever, thx Smiley

btw,

is this a good function?

Code:
color averageColor(PImage img){

int aRed = 0; //this way it starts at zero evertime the function gets used
int aGreen = 0; //same
int aBlue = 0; //same

img.loadPixels();

for (int i = 0; i < img.pixels.length; i++){
aRed += red(img.pixels[i]);
aGreen += green(img.pixels[i]);
aBlue += blue(img.pixels[i]);
}

aRed = aRed/img.pixels.length;
aGreen = aGreen/img.pixels.length;
aBlue = aBlue/img.pixels.length;

return color(aRed, aGreen, aBlue);

}
Re: NegativeArraySizeException
Reply #5 - Sep 24th, 2009, 9:09am
 
Yes, if you don't care about speed (using bit masking & shifting might be faster than red() and similar calls).
I would also use long instead of int for declaration of aRed and friends: if the image is big, you risk a capacity overflow.
Although... with an average value of 128, it might happen after some 16777216 pixels, ie. something like a 4096x4096 image, so the risk is minimal.
Re: NegativeArraySizeException
Reply #6 - Sep 24th, 2009, 10:04am
 
I meight need it for very large images so I use long now, never heard or saw it before.

I looked into the reference of bit shifting but I don't understand it at all.
Speed doesn't matter atm since I'm not planning of using it for video.

thx
Page Index Toggle Pages: 1