Array Index Out Of BoundsException?

Hello,

I am reading an image processing text and working on an program to double an image size.

When the program runs I got a ArrayIndexOutOfBoundsException compiler error, the algorithm takes an average of the pixels surrounding the pixel we are working with.

Could anyone see the error in the code?

Thank you

pseudocode code is:

Read imageIn from file

imageOut =new image twice the size as imageIn

for x = 0 to imageOut.width{

for y = 0 to imageOut.height{

if (x is even and y is even){

imageOut(x, y) = imageIn(x/2, y/2)

}elseif (x is odd but y is even){

imageOut(x, y) = average(imageIn(x/2, y/2),

imageIn(x/2+1, y/2))

}elseif (x is even but y is odd){

imageOut(x, y) = average(imageIn(x/2, y/2),

imageIn(x/2, y/2+1))

}else{

imageOut(x, y) = average(imageIn(x/2, y/2),

imageIn(x/2+1, y/2),

imageIn(x/2, y/2+1),

imageIn(x/2+1, y/2+1))

}

}

}

DbImage tree.jpg db

image.jpg

Exception in thread"main" java.lang.ArrayIndexOutOfBoundsException: Coordinate

out of bounds!

at sun.awt.image.IntegerInterleavedRaster.getDataElements(Unknown Source

)

at java.awt.image.BufferedImage.getRGB(Unknown Source)

at ImageClass.getRed(ImageClass.java:45)

at DbImage.main(DbImage.java:43)

import java.io.*;

publicclass DbImage{

publicstaticvoid main(String args[]){

int width = 0, height = 0;

ImageClass imageOne =new ImageClass();

try{

imageOne.read(args[0]);

}catch(Exception e){

System.err.println(e.getMessage());

System.exit(1);

}

width = imageOne.getWidth() * 2;

height = imageOne.getHeight() * 2;

ImageClass imageTwo =new ImageClass(width, height);

for(int x = 0; x < width; x++){

for(int y = 0; y < height; y++){

int red = 0, green = 0, blue = 0;

if(x%2 < 1 && y%2 < 1){//If x and y is even

red = imageOne.getRed(x/2, y/2);

green = imageOne.getGreen(x/2, y/2);

blue = imageOne.getBlue(x/2, y/2);

}

else{

if(x%2 !=0 && y%2 < 1){//Else if x is odd and y is even

red = (imageOne.getRed(x/2, y/2) + imageOne.getRed(x/2+1, y/2));

red = red / 2;

green = (imageOne.getGreen(x/2, y/2) + imageOne.getGreen(x/2+1, y/2));

green = green / 2;

blue = (imageOne.getBlue(x/2, y/2) + imageOne.getBlue(x/2+1, y/2));

blue = blue / 2;

}

elseif(x%2 < 1 && y%2 !=0){//Else if x is even and y is odd

red = (imageOne.getRed(x/2, y/2) + imageOne.getRed(x/2, y/2+1));

red = red / 2;

green = (imageOne.getGreen(x/2, y/2) + imageOne.getGreen(x/2, y/2+1));

green = green / 2;

blue = (imageOne.getBlue(x/2, y/2) + imageOne.getBlue(x/2, y/2+1));

blue = blue / 2;

}

else{

red = (imageOne.getRed(x/2, y/2) + imageOne.getRed(x/2+1, y/2) +

imageOne.getRed(x/2, y/2+1) + imageOne.getRed(x/2+1, y/2+1));

red = red / 4;

green = (imageOne.getGreen(x/2, y/2) + imageOne.getGreen(x/2+1, y/2) +

imageOne.getGreen(x/2, y/2+1) + imageOne.getGreen(x/2+1, y/2+1));

green = green / 4;

blue = (imageOne.getBlue(x/2, y/2) + imageOne.getBlue(x/2+1, y/2) +

imageOne.getBlue(x/2, y/2+1) + imageOne.getBlue(x/2+1, y/2+1));

blue = blue / 4;

}

}

imageTwo.setRGB(x,y,red,green,blue);

}

}

try{

imageTwo.write(args[1]);

}catch(Exception e){

System.err.println(e.getMessage());

System.exit(1);

}

}

}

[5909 byte] By [sam04a] at [2007-10-3 5:54:47]
# 1

This was the ImageClass.java I am using.

import com.sun.image.codec.jpeg.*;

import java.awt.image.*;

import java.io.*;

public class ImageClass {

private BufferedImage img;

public ImageClass() {

}

public ImageClass(int width, int height) {

img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

for(int x = 0; x < width; x++) {

for(int y = 0; y < height; y++) {

img.setRGB(x,y,0xff000000);

}

}

}

public void read(String filename)

throws IOException, ImageFormatException {

InputStream istream = new FileInputStream(filename);

JPEGImageDecoder jpegDec = JPEGCodec.createJPEGDecoder(istream);

img = jpegDec.decodeAsBufferedImage();

}

public void write(String filename)

throws IOException {

OutputStream ostream = new FileOutputStream(filename);

JPEGImageEncoder jpegEnc = JPEGCodec.createJPEGEncoder(ostream);

jpegEnc.encode(img);

}

public int getHeight() {

return img.getHeight();

}

public int getWidth() {

return img.getWidth();

}

public int getRed(int x, int y) {

return (img.getRGB(x, y) & 0x00ff0000) >> 16;

}

public int getGreen(int x, int y) {

return (img.getRGB(x, y) & 0x0000ff00) >> 8;

}

public int getBlue(int x, int y) {

return (img.getRGB(x, y) & 0x000000ff);

}

public void setRed(int x, int y, int value) {

img.setRGB(x, y, (img.getRGB(x, y) & 0xff00ffff) | (value << 16));

}

public void setGreen(int x, int y, int value) {

img.setRGB(x, y, (img.getRGB(x, y) & 0xffff00ff) | (value << 8));

}

public void setBlue(int x, int y, int value) {

img.setRGB(x, y, (img.getRGB(x, y) & 0xffffff00) | value);

}

public void setRGB(int x, int y, int r, int g, int b) {

img.setRGB(x, y, 0xff000000 | (r << 16) | (g << 8) | b);

}

}

sam04a at 2007-7-15 0:35:28 > top of Java-index,Java Essentials,Java Programming...
# 2

Right here:

public int getRed(int x, int y) {

return (img.getRGB(x, y) & 0x00ff0000) >> 16;

}

You are giving out of range coordinates.

CaptainMorgan08a at 2007-7-15 0:35:28 > top of Java-index,Java Essentials,Java Programming...
# 3

> DbImage tree.jpg db

> image.jpg

> Exception in thread "main"

> java.lang.ArrayIndexOutOfBoundsException: Coordinate

> out of bounds!

> at

> sun.awt.image.IntegerInterleavedRaster.getDataElements

> (Unknown Source

> )

> at

> java.awt.image.BufferedImage.getRGB(Unknown Source)

> at ImageClass.getRed(ImageClass.java:45)

> at DbImage.main(DbImage.java:43)

To read error messages, look at the bottom line to see where to start.

It says DBImage.java had an error at line 43. Go to line 43 of that file.

There should be a call to getRed() on an ImageClass. Go to line 45 of ImageClass.

There should be a call to getRGB() on a BufferedImage. That's where the error is coming from.

Look at this documentation for that class [url=http://java.sun.com/j2se/1.5.0/docs/api/java/awt/image/BufferedImage.html#getRGB(int,%20int)]here[/url]

It says:

"An ArrayOutOfBoundsException may be thrown if the coordinates are not in bounds. However, explicit bounds checking is not guaranteed."

Most likely you are passing x and y that are outside the boundaries of the image.

hunter9000a at 2007-7-15 0:35:28 > top of Java-index,Java Essentials,Java Programming...
# 4

Let's just take one of those numbers.

> y/2+1

in the code snippet:

> imageOne.getRed(x/2+1, y/2+1));

Let's say the original 'y' value was 3. Your new array (doubled) 'y' max is 6. Your loop will run while 'y' is less than 6, so let's say it is 5 right now, so it's in the loop.

(5 / 2) + 1 = 3. That's out of bounds for the original object whose max 'y' size is 3 (you can only go up to 2, not 3 in this case, right?)

Looks like a classic "off-by-one" error to me.

warnerjaa at 2007-7-15 0:35:28 > top of Java-index,Java Essentials,Java Programming...
# 5
Could I resolve this without changing all the code.In the basic code they gave:
sam04a at 2007-7-15 0:35:28 > top of Java-index,Java Essentials,Java Programming...
# 6

Read imageIn from file

imageOut = new image twice the size as imageIn

for x = 0 to imageOut.width {

for y = 0 to imageOut.height {

if (x is even and y is even) {

imageOut(x, y) = imageIn(x/2, y/2)

} else if (x is odd but y is even) {

imageOut(x, y) = average(imageIn(x/2, y/2),

imageIn(x/2+1, y/2))

} else if (x is even but y is odd) {

imageOut(x, y) = average(imageIn(x/2, y/2),

imageIn(x/2, y/2+1))

} else {

imageOut(x, y) = average(imageIn(x/2, y/2),

imageIn(x/2+1, y/2),

imageIn(x/2, y/2+1),

imageIn(x/2+1, y/2+1))

}

}

}

sam04a at 2007-7-15 0:35:28 > top of Java-index,Java Essentials,Java Programming...