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);
}
}
}

