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 › ArrayIndexOutOfBoundsExeption: #
Page Index Toggle Pages: 1
ArrayIndexOutOfBoundsExeption: # (Read 503 times)
ArrayIndexOutOfBoundsExeption: #
Apr 16th, 2009, 2:36am
 
Hi, I got a prob...

the error is caused by this line:
   for (int i = 0; i < out.pixels.length; i+= 3){

if I change it to i += 4); then it works but I get caps in the drawning lines.

the curve code is based on:

Code:
noFill();
beginShape();
curveVertex(84, 91);
curveVertex(84, 91);
curveVertex(68, 19);
curveVertex(21, 17);
curveVertex(32, 100);
curveVertex(32, 100);
endShape();


I know how the error is caused but I don't know a way to solve it.


Code:
import processing.video.*;
//import processing.opengl.*;


PImage out = new PImage();

Capture cam;

String cMode = "HSB";
//String dMode = "point";

void setup(){
size(600, 600, P3D);
frameRate(10);
smooth();
noFill();
rectMode(CENTER);

cam = new Capture(this, 10, 10);
}


void draw() {
background(204);
pushMatrix();

translate(width/2, height/2, 0);
rotateX( radians(mouseY));
rotateY( radians(mouseX));
//scale(2.0);
//box(250);

translate( -128, -128, -128);

if (cam.available() == true) {
cam.read();
//image(cam, 0, 0); //set(160, 100, cam);

out.pixels = sortArray(cam.pixels, 0);

//strokeWeight(4);

for (int i = 0; i < out.pixels.length; i+= 3){

stroke(color(out.pixels[i]));

beginShape();

if (cMode == "RGB"){
curveVertex(red(out.pixels[i]), green(out.pixels[i]), blue(out.pixels[i]));
}
else if(cMode == "HSB"){
curveVertex(hue(cam.pixels[i]), saturation(cam.pixels[i]), brightness(cam.pixels[i]));
curveVertex(hue(cam.pixels[i]), saturation(cam.pixels[i]), brightness(cam.pixels[i]));
curveVertex(hue(cam.pixels[i+1]), saturation(cam.pixels[i+1]), brightness(cam.pixels[i+1]));
curveVertex(hue(cam.pixels[i+2]), saturation(cam.pixels[i+2]), brightness(cam.pixels[i+2]));
curveVertex(hue(cam.pixels[i+3]), saturation(cam.pixels[i+3]), brightness(cam.pixels[i+3]));
curveVertex(hue(cam.pixels[i+3]), saturation(cam.pixels[i+3]), brightness(cam.pixels[i+3]));
//point(hue(cam.pixels[i]), saturation(cam.pixels[i]), brightness(cam.pixels[i]));

}

endShape();

}

}
if (frameCount % 15 == 0){
saveFrame();
}
popMatrix();
}








//DUMP

//rotateY(map(mouseX, 0, width, -PI, PI));
//rotateX(map(mouseY, 0, height, PI, -PI));

int[] sortArray(int[] tosort, int depth) {

int[][] contents = new int[256][];//256 levels for RGB/hue/brightness/etc

int[] counts = new int[256];//256 levels for RGB/hue/brightness/etc
int[] indexlist = new int[tosort.length];

int[] sorted = new int[tosort.length];

//Pass 1
int tx = 0;
for (int i=0; i<tosort.length; i++) {

switch (depth) {//This sort sequence orders them by hue then brightness
case 0:
indexlist[i] = int(hue(tosort[i]));//alpha
break;
default:
indexlist[i] = int(brightness(tosort[i]));//target
}

counts[indexlist[i]]++;//increment the count table
}

//Pass 2
for (int i=0; i<256; i++) {
contents[i] = new int[counts[i]];
//output.println("contents["+i+"] is "+contents[i].length);
}

//Pass 3
for (int i=0; i<tosort.length; i++) {
contents[ indexlist[i] ][ contents[indexlist[i]].length - counts[indexlist[i]] ] = tosort[i];
counts[indexlist[i]]--;//back down the count table
}


if ( (depth >= 0) && (depth < 4) ) {//=======================recursion call===============
for (int i=0; i<256; i++) {
if (contents[i].length > 0) {

switch (depth) {//Sort by hue then brightness
case 0:
contents[i] = sortArray(contents[i], 1);
break;
default:
//indexlist[i] = int(brightness(tosort[i]));//target
}
}
}
}


//Pass 4
int idx = 0;//Location to place into output
for (int i=0; i<contents.length; i++) {//outer array size
for (int d=0; d<contents[i].length; d++) {
sorted[idx] = contents[i][d];
idx++;
}
}

return sorted;
}





Re: ArrayIndexOutOfBoundsExeption: #
Reply #1 - Apr 16th, 2009, 2:56am
 
This is just a guess at what you're doing wrong, but I think you need to change the test in your for statement.

I think it should be: for(int i=0;i<out.pixels.length-3;i+=3)

Why? Well because assume out.pixels.length is 20, your loop will go 0,3,6,9,12,15,18 now the test 18<20 will be true, so it'll add 3, 21 and then try to access pixel 21 in a 20 pixel image and give you the error. So you need to make sure that after adding 3, it'll still be within the image.
Re: ArrayIndexOutOfBoundsExeption: #
Reply #2 - Apr 16th, 2009, 3:46am
 
o stupid, I tried something like that but then with + instand of -
Page Index Toggle Pages: 1