• Advertisement
Sign in to follow this  

[java] OpenGL question...

This topic is 4216 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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");

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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(...);

Share this post


Link to post
Share on other sites
Note: the Screenshot class in the com.sun.opengl.util package does all of this work automatically for you.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement