Image processing

Out of bounds exception question

Page 1 of 1

4 Replies - 1115 Views - Last Post: 05 December 2009 - 05:23 AM Rate Topic: -----

#1 betosteam  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 10
  • Joined: 03-December 09

Image processing

Post icon  Posted 03 December 2009 - 05:25 PM

I have been working on an image processing project and when I try to make the image flip vertically it gives me an array out of bounds exception. Can anyone tell me what I'm doing wrong? Here is the code ---


public ImgColor flipVert(){
for (int band = 0; band < NBANDS; band++){
for (int destRow = 0; destRow < this.getHeight()/2; destRow++) {
for (int destCol = 0; destCol < this.getWidth(); destCol++) {
int destIndex = destCol * this.getWidth() + destRow;
int srcIndex = destCol * this.getWidth() + (this.getHeight() - 1 - destRow);
byte tmpPixel = this.data[band][destIndex];
this.data[band][srcIndex] = this.data[band][destIndex];
this.data[band][destIndex] = tmpPixel;

}
}
}
return this;
}

Is This A Good Question/Topic? 0
  • +

Replies To: Image processing

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Image processing

Posted 03 December 2009 - 05:29 PM

Please :code:

You do not showed us enough code

What is NBANDS which determine the range of your for() loop
What is "this" an image a JComponent ?
You array out of bounds exception should specified at which line it occured. Which line ?
Was This Post Helpful? 0
  • +
  • -

#3 betosteam  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 10
  • Joined: 03-December 09

Re: Image processing

Posted 03 December 2009 - 05:59 PM

View Postpbl, on 3 Dec, 2009 - 04:29 PM, said:

Please :code:

You do not showed us enough code

What is NBANDS which determine the range of your for() loop
What is "this" an image a JComponent ?
You array out of bounds exception should specified at which line it occured. Which line ?



Here is all my code for the the color image. THIS represents the image and it doesn't give me line number for the exception but says that it is in thread. Here is the rest of the code. I am getting it for the flipVert() method. Thanks for you help.



import java.awt.Transparency;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.ComponentColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;


public class ImgColor extends Image {
private final int NBANDS = 3;
private byte[][] data;



public ImgColor(int w, int h){
super(w, h);
this.data = new byte[NBANDS][this.getNPixels()];;
}


public ImgColor (int w, int h, byte[][] source) {
super (w, h);
this.data = new byte[NBANDS][this.getNPixels()];
for (int i = 0; i < source[0].length; i++) {
this.data[0][i] = source[0][i];
}
for (int i = 0; i < source[1].length; i++) {
this.data[1][i] = source[1][i];
}
for (int i = 0; i < source[2].length; i++) {
this.data[2][i] = source[2][i];
}

}


public ImgColor(ImgColor orig) {
this(orig.getWidth(), orig.getHeight(), orig.data);

}

public byte[] getRedBand() {
return this.data[0];
}


public byte[] getGreenBand(){
return this.data[1];
}

public byte[] getBlueBand(){
return this.data[2];
}


public boolean equals(Image img2) {
return false;
}

public Image clone() {
return new ImgColor(this);
}

public ImgColor invert(){
return null;
}

public ImgColor flipHoriz(){
int limit = this.getWidth() / 2;
for (int band = 0; band < NBANDS; band++){
for (int destRow = 0; destRow < this.getHeight(); destRow++) {
for (int destCol = 0; destCol < limit; destCol++) {
int destIndex = destRow * this.getWidth() + destCol;
int srcIndex = destRow * this.getWidth() + (this.getWidth() - 1 - destCol);
byte tmpPixel = this.data[band][destIndex];
this.data[band][destIndex] = this.data[band][srcIndex];
this.data[band][srcIndex] = tmpPixel;

}
}
}
return this;
}


public ImgColor flipVert(){
for (int band = 0; band < NBANDS; band++){
for (int destRow = 0; destRow < this.getHeight()/2; destRow++) {
for (int destCol = 0; destCol < this.getWidth(); destCol++) {
int destIndex = destCol * this.getWidth() + destRow;
int srcIndex = destCol * this.getWidth() + (this.getHeight() - 1 - destRow);
byte tmpPixel = this.data[band][destIndex];
this.data[band][srcIndex] = this.data[band][destIndex];
this.data[band][destIndex] = tmpPixel;

}
}
}
return this;
}

public ImgColor rotate90(){
int srcWidth = this.getWidth();
for (int band = 0; band < NBANDS; band++){
for (int destRow = 0; destRow < this.getHeight(); destRow++) {
for (int destCol = 0; destCol < this.getWidth(); destCol++) {
int destIndex = destRow * this.getHeight() + destCol;
int srcIndex = destCol * srcWidth + (srcWidth - (destRow +1));
this.data[band][destIndex] = this.data[band][srcIndex];


}
}
}
return this;
}


public ImgColor rotate180(){
return null;
}

public ImgColor rotate270(){
return null;
}

public ImgColor crop(int x, int y, int nwid, int nhei){
ImgColor cropped = new ImgColor(nwid, nhei);
for (int band = 0; band < NBANDS; band++){
for (int destRow = 0; destRow < cropped.getHeight(); destRow++) {
for (int destCol = 0; destCol < cropped.getWidth(); destCol++) {
int destIndex = destRow * cropped.getWidth() + destCol;
int srcIndex = (destRow + y) * this.getWidth() + (destCol + x);
cropped.data[band][destIndex] = this.data[band][srcIndex];
}
}
}



return cropped;
}

public ImgColor scaleFast(int nwid, int nhei){
ImgColor scaleF = new ImgColor(nwid, nhei);
for (int band = 0; band < NBANDS; band++){
for (int destRow = 0; destRow < scaleF.getHeight(); destRow++) {
for (int destCol = 0; destCol < scaleF.getWidth(); destCol++) {
int destIndex = destRow * scaleF.getWidth() * destCol;
int srcIndex = destRow * this.getWidth() + destCol ;
scaleF.data[band][destIndex] = this.data[band][srcIndex];
}
}
}
return scaleF;
}

public String toString() {
String z = "Color IMAGE ";
String s ="the width is " +getWidth() + " The height is " + getHeight();
String r = " RED ";
for (int i = 0; i < data[0].length; i++) {
r += this.data[0][i] + ",";
}
String g = " GREEN ";
for (int i = 0; i < data[1].length; i++) {
g += this.data[1][i] + ",";
}
String b = " BLUE ";
for (int i = 0; i < data[2].length; i++) {
b += this.data[2][i] + ",";
}
return z + s + r + g + b;
}

private static final int[] bankIndices = { 0, 1, 2 };
private static final int[] bandOffsets = { 0, 0, 0 };

@Override
public BufferedImage getBufImage() {
if (bufimg == null) {
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
ColorModel colormod = new ComponentColorModel(cs, false, false,
Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
DataBuffer databuf = new DataBufferByte(data, getNPixels());
WritableRaster raster = Raster.createBandedRaster(databuf,
getWidth(), getHeight(),
getWidth(), bankIndices, bandOffsets, null);
bufimg = new BufferedImage(colormod, raster, false, null);
}
return bufimg;
}
}
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Image processing

Posted 04 December 2009 - 11:36 PM

:code:
and indent your code properly
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3443
  • View blog
  • Posts: 15,596
  • Joined: 20-September 08

Re: Image processing

Posted 05 December 2009 - 05:23 AM

You need to reverse the array elements of the data buffer in both dimensions. Here is a generic method you can use to swap them:

	static <T> void swap(T[] array, int a, int b) {
	T temp = array[a];
	array[a] = array[b];
	array[b] = temp;
	}


Was This Post Helpful? 1

Page 1 of 1