Skip to main content

BufferedImage's buffer

2 replies [Last post]
bitshit
Offline
Joined: 2004-04-22

Hi,

I have a situation where I want to update the pixel buffer (int[]) for my BufferedImage.

I get the int array with the pixel info like this:

img = gc.createCompatibleImage(w, h, transparency);
imgData = (DataBufferInt)img.getRaster().getDataBuffer();
pixBuffer = imgData.getData();

Now I can alter the contents of the pixBuffer, however if I want to recreate the pixBuffer with a new int[] the reference to this new pixBuffer is lost by the BufferedImage, whats the best way of recreating this reference? I currently do it like this, but im not sure its the best way:

DataBufferInt buffer = new DataBufferInt(pixBuffer, pixBuffer.length);
SampleModel sm = cm.createCompatibleSampleModel(width, height);
WritableRaster raster = Raster.createWritableRaster(sm, buffer, null);
img.setData(raster);

I also tried:
WritableRaster raster = img.getRaster();
raster.setElementData(0,0, pixBuffer);

But that doesnt seem to work?

Thanks!

Martijn

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
mickleness
Offline
Joined: 2005-07-14

I've found these calls let me manipulate BufferedImage's easily:

image.getRaster().getDataElements(x,y,w,h,intArray)
image.getRaster().setDataElements(x,y,w,h,intArray)

Will this work for you?

Also here is an article explaining why talking to DataBuffers can be really bad, depending on what you later do with that BufferedImage:
http://javagraphics.blogspot.com/2007/04/managed-images.html

bitshit
Offline
Joined: 2004-04-22

Thanks, thats just what I was looking for!