CFM
YaBB Newbies
Offline
Posts: 5
Only runs with ArrayIndexOutOfBoundsException?
Apr 5th , 2008, 6:56pm
Dear folks, I'm trying to develop a simple application that will 'pixelate' an imported bitmap into rectangles (similar to the Photoshop 'Mosaic' filter, but with custom nonsquare aspect ratios). I've tried to do this by sampling a number of pixels in a target area over the bitmap, averaging the RGB values of the pixels sampled, then draw a rectangle using the averaged color value as fill, offsetting it from the base coordinates. To accomplish this, I set up several array functions for the steps, first defining a single 'cell' to sample (and creating an offset averaged rectangle), then cloning this cell around the window to render averaged tiles for the complete image. I'm getting weird results - the code appears to run fine (I embedded some printlns to monitor the arrays - they seem to be working and passing values as expected), but the rectangles just won't draw. Interestingly, if the code is adjusted to operate on coordinates that fall outside of the y boundaries of the graphic window, an ArrayIndexOutOfBoundsException is thrown. Run stops at this point, but the weird thing is the rectangles ARE drawn up to the point of the Exception. I am a genuine novice and perhaps am missing something really obvious, like a loop that refreshes the background or something? Anyway, it's frustratingly clear that my code only works when it encounters a terminal error. @#*&% Any ideas? Kindest thanks for any input! -cfm (here's the code...) PImage img; int xpos, ypos, count, mx, xcell, ycell; int[] matrix = new int [32]; float[] rAverage = new float [32]; float[] gAverage = new float [32]; float[] bAverage = new float [32]; float[] newColor = new float [3]; float r, rtally, gtally, btally; void setup() { size(400,402); background(196); img = loadImage("targetimage.jpg"); //200x200 pixels mx = 5; // sample factor } void draw() { image(img,width/4,height/2); loadPixels(); noLoop(); translation(); updatePixels(); } void translation() { for (int xcell = 100; xcell < 250; xcell += 37) { for (int ycell = 200; ycell < 388; ycell += 17) { sampleMatrix(xcell, ycell); samplePixel(); average(); stroke(255); fill (newColor[0], newColor[1], newColor[2]); rect (xcell,ycell-200,37,17); //println (xcell); //println (ycell); //println (newColor); } } } void sampleMatrix (int xpos, int ypos) { count = 0; for (int j = ypos*width; j < ypos*width+(width*17); j += width*mx) { for (int i = xpos+j; i < xpos+37+j; i += mx) { println (count); color c = pixels[ i]; matrix [count] = c; count = count +1; println (i); } } } void samplePixel() { for (int sP = 0; sP <32; sP++) { color c = matrix [sP]; float r = red(c); float g = green(c); float b = blue(c); rAverage[sP] = r; gAverage[sP] = g; bAverage[sP] = b; } } void average() { rtally = 0.0; gtally = 0.0; btally = 0.0; for (int aP = 0; aP <32; aP++) { rtally = rtally + rAverage [aP]; gtally = gtally + gAverage [aP]; btally = btally + bAverage [aP]; } newColor [0] = rtally/32; newColor [1] = gtally/32; newColor [2] = btally/32; }