Skip to main content

ImageIO -> NIO

1 reply [Last post]
Joined: 2004-06-21
Points: 0

Hi, has anyone tried to get ImageIO
to load images directly into NIO buffers?
Is it possible to create Image Rasters
to wrap around NIO buffers?

The reason I'm asking this is because
I'm trying to send the image data to
OpenGL quickly, avoiding any data
conversion or copying.



Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2005-09-06
Points: 0

This is something I was playing around with yesterday trying to get good performance transfering images from C++ to Java. Heres a chunk of code that might point you in the right direction:

m_oImageByteBuffer = ByteBuffer of your image data in 32bit RGB data
oSize = Dimension size of the image

ColorModel oColorModel = new DirectColorModel(24, 0x00ff0000, 0x0000ff00, 0x000000ff);

SinglePixelPackedSampleModel oSampleModel = new SinglePixelPackedSampleModel(DataBuffer.TYPE_INT, oSize.width, oSize.height,
new int[]{0x00ff0000, 0x0000ff00, 0x000000ff});

final IntBuffer oIntegerBuffer = m_oImageByteBuffer.asIntBuffer();

DataBuffer oDataBuffer = new DataBuffer(DataBuffer.TYPE_INT, oSize.width * oSize.height * 4, 1) {
public int getElem(int bank, int i) {
return oIntegerBuffer.get(i);

public void setElem(int bank, int i, int val) {
oIntegerBuffer.put(i, val);

WritableRaster oRaster = new WritableRaster(oSampleModel, oDataBuffer, new Point()){};
BufferedImage oImage = new BufferedImage(oColorModel, oRaster, true, new Hashtable());

Its for wrapping a ByteBuffer as a BufferedImage.

The problem I found with this is the java bug that scaling a image that is not type BufferedImage.TYPE_INT_RGB is very slow 22 seconds compared to 0.2 seconds.

What I have ended up doing is creating a new BufferedImage of type TYPE_INT_RGB in C++ and getting its data array and writting the image data into that in C++. This is by far the fastest I have found.