ByteBuffer into BufferedImage

I am reading a binary file and I need to take the data and put it into a BufferedImage so I can display it. It seems like I should know how to do this, but I can’t think of it! Anyone able to help?

Well, there are many ways to do something like that. You can use MemoryImageSource, or BufferedImage’s setRaster or setRGB methods.

You can find usable snippets in this thread:
http://www.java-gaming.org/forums/index.php?topic=12484.0

Ok, I am having a heck of a time with this! What I have are gray scale images that have either 16bpp or 32bpp. When I wrtie them out to a file, Photoshop can read them as raw images, but I still can’t figure out how to get them into a buffered image. The setRGB() takes an integer array as an argument, so I can’t figure out how to use the 16bpp images there and when I use the 32bpp images they show up as just black! Here is the code that I am using:


// Write RAW image format
rasterData.rewind();
while(rasterData.hasRemaining())
{
    output.write(rasterData.get());
}
output.close();
	        
// Create BufferedImage
BufferedImage image = null;
if(!bit16)
{
    int[] rasterArray = new int[rasterData.capacity()/4];
    count = 0;
    while(rasterData.hasRemaining())
    {
        rasterArray[count] = rasterData.getInt();
    }
    image = new BufferedImage(cols, rows, BufferedImage.TYPE_USHORT_565_RGB);
    image.setRGB(0, 0, cols, rows, rasterArray, 0, cols);
}
	        
JFrame previewFrame = new JFrame("Image Preview");
previewFrame.setSize(cols, rows);
previewFrame.setVisible(true);
previewFrame.getContentPane().getGraphics().drawImage(image, 0, 0, null);

and I have attached one of the 16bpp images and one of the 32bpp images in a rar file. I had to rename it as .txt so just change it back to .rar

-=EDIT=-
I uploaded the wrong file initially. It’s fixed now.

The first question you need to ask is, what is the format the data is in?

You say “raw” but what does that really mean? I assume it has somethign other then pixel data in it so that you know things like width and height.

What is actual format of the pixels? Is it a simple grid or isit compressed or otherwise organzized?