Array loosing reference
Hi experts,
I am dealing with a BufferedImage through and array of byte that act as a pointer to it, and then I make all my computations on teh image throught it:
BufferedImage image;
....
byte[] imageData = getImageData(image);
Then at a certain time, I want to reduce the side of the image. getSubImage() doesnt work because it overwirtes the image. So the best thing to do would be to reduce the size of the array imageData. But when I do it this way:
/byte[] imageData2 =newbyte[newSize];
imageData2 = resizeArray(data,new data);
...
publicbyte[] resizeArray(byte[] array,int newSize ){
byte[] newArray =newbyte[newSize];
int lastElement = Math.min( newSize, array.length );
for (int i = 0; i<lastElement; i++ )
newArray[i] = array[i];
return newArray;
}
I finish ending up without the reference/pointers to the image. I there any way to keep that reference?
Thanks for your t ime!>
[1593 byte] By [
Kikensa] at [2007-11-26 19:05:34]

imageData2 = resizeArray(imageData,newSize);Sorry for the typing error...
I don't understand. If you want to change the size of a BufferedImage,why not scale it with an AffineTransform?
If i create the imageData2 with AffineTransform will it not loose the reference that teh byte array imageData has to the buffereImage image?
I guess I don't understand what you are doing. If you are working withBufferedImages, you rarely have to reach into the underlying data buffers.
I am using
//return a pointer to an array of bytes which represent the image data in memory.
public static byte[] getImageData(BufferedImage image) {
WritableRaster WR = image.getRaster();
DataBuffer DB = WR.getDataBuffer();
if (DB.getDataType() != DataBuffer.TYPE_BYTE)
throw new IllegalStateException("Notof type byte");
return ((DataBufferByte)DB).getData();
}
This allows a faster access to the image for processing. The problem comes when I have for example a 300*300 image in the imageData array and I want to reduce it to 280*280, for example.
for a 300x300 image I would get a image data of size [300*300*3(RGB colors)]
and so I would like to reduce it to a [280*280*3] without loosing the reference to the buffered image
Here's a demo of resizing a BufferedImage:
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.io.*;
import java.net.*;
import javax.imageio.*;
import javax.swing.*;
public class Resized {
public static void main(String[] args) throws IOException {
URL url = new URL("http://java.sun.com/docs/books/tutorial/images/penduke.gif");
BufferedImage image1 = ImageIO.read(url);
BufferedImage image2 = resize(image1);
JPanel cp = new JPanel();
cp.add(new JLabel(new ImageIcon(image1)));
cp.add(new JLabel(new ImageIcon(image2)));
JFrame f = new JFrame();
f.setContentPane(cp);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
static BufferedImage resize(BufferedImage image) {
int w = image.getWidth()*2;
int h = image.getHeight()*2;
BufferedImage result = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = result.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g.drawRenderedImage(image, AffineTransform.getScaleInstance(2,2));
g.dispose();
return result;
}
}
To answer your original question, I can think of two options:
1. Pass BufferedImages, and use your getImageData() method when you
need a byte[].
2. Define:
class ABC {
public final BufferedImage image;
public final byte[] data;
public ABC(BufferedImage image) {
this.image = image;
data = XYZ.getImageData(image);
}
}
and pass around a reference to instances of that. I prefer the first way.
I have tried it and it works perfectly well to resize an image, but I still loose reference from it :S Thanks for your time!