Turn Texture into BufferedImage

Started by Shinmera, June 28, 2011, 21:53:11

Previous topic - Next topic

Shinmera

Greetings,

I've been trying to wrap my mind about this for some time but can't seem to find a working solution for it.
Is there any way to turn a texture into a BufferedImage?
I suppose copying the texture data to a byte buffer with glTexImage2D and then converting that could work but I just can't seem to get a working example.

Any help would be appreciated.

CodeBunny

Are you using Slick Textures? I believe they have a method that allows you to access the RGBA bytes.

Shinmera

I've converted my program to use Slick textures now so I can use the getTextureData(); function.
However, I still get an empty Image. Here's the code I've been trying so far:
        //render scene to texture
        render();
        texture.bind();
        glCopyTexImage2D(GL_TEXTURE_2D,0,GL_RGB,0,0,DISPLAY_WIDTH, DISPLAY_HEIGHT,0);
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
        glBindTexture(GL_TEXTURE_2D,0);
        
        //turn texture into a BufferedImage
        BufferedImage image = new BufferedImage(DISPLAY_WIDTH, DISPLAY_HEIGHT,BufferedImage.TYPE_INT_RGB);
        InputStream in = new ByteArrayInputStream(texture.getTextureData());
        try{image = ImageIO.read(in);}catch(Exception ex){ex.printStackTrace();}

jediTofu

I'm betting that the array returned doesn't function like BufferedImage and InputStream expect.

Just do this for a test:

byte[] b=texture.getTextureData();
for(int i = 0; i < b.length; ++i) {
  System.out.print(i + " ");
}

Then you can kind of look at that and do any tests to see what it's returning.  It may include the alpha when calling getTextureData or have any other number of different orders for RGB:
http://slick.cokeandcode.com/javadoc-util/org/newdawn/slick/opengl/Texture.html#getTextureData()
cool story, bro

jediTofu

Also Slick-Util uses BufferedImage underneath I believe (at least to load the data), so there's probably a way to get to that directly (probably have to hack the code).
cool story, bro