[java] OpenGL question...

Started by
3 comments, last by kbr 17 years, 8 months ago
I have an image created using OpenGL via JOGL. I need to programmatically output the image to a JPEG file. On the GL side, it seems glReadPixels gives access to the image. Then, on the Java side, the stuff in javax.ImageIO is all there to output it. The part I'm missing is, loading the image data INTO the ImageIO types (like BufferedImage). Any help on doing this, or other suggestions to solve this issue, would be greatly appreciated. Thanks!
Advertisement
Assuming you have an array/buffer containing the texture's pixel data, you can do something like this. It isn't perfect, but it should set you on the right track.

int[] pixelData;BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);glReadPixels(startX, startY, width, height, GL_BGRA, GL_UNSIGNED_BYTE, pixelData);int counter = 0;for(int x = 0; x < width; x++)   for(int y = 0; y < height; y++)      image.setRGB(x, y, pixelData[counter++]);ImageIO.write(image, ".jpg", new File("newFile.jpg");

So it is a matter of brute-forcing it from the pixelData array into the BufferedImage...ok, that probably shouldn't be too surprising, I suppose.

Thanks for the response, I'll give it a try!
You don't have to copy each pixel individually. You just make sure that the data you are pulling from opengl is in the same format as your bufferedimage and copy that data into the backing array ofthe buffered image. This should give you a good start.

BufferedImage offscreenImage = new BufferedImage(panelWidth,                                               panelHeight,                                               awtFormat);Byte[] byteArray;byteArray = ((DataBufferByte) offscreenImage.getRaster().getDataBuffer()).getData();gl.glReadPixels(...);System.arraycopy(...);
"... we should have such an empire for liberty as she has never surveyed since the creation ..."Thomas Jefferson
Note: the Screenshot class in the com.sun.opengl.util package does all of this work automatically for you.

This topic is closed to new replies.

Advertisement