getting a bufferedimage from glGetTexImage

hello,

how can i get the buffer that is returned from glGetTexImage into a BufferedImage?

what kind of buffer is that actually, that is given into the glGetTexImage function? i.e. how should i allocate it?

thanks!

For uncompressed textures, you allocate the buffer in the same way for a glTexImage() call based on the source and type parameters. OpenGL should take care of the conversion between the textures dst format/type and the src and type given in glGetTextureImage(). That is my understanding, unfortunately, I haven’t had a chance to test my code yet.

thanks for the info.

so it looks like it is a ByteBuffer.

suppose i manage to correctly allocate the buffer and fill it with the glGetTexImage function, how can i convert it into a bufferedimage?

thanks!

First, just create an empty Buffered Image with the specific type you need:

BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);

Then, you can either iterator through all of the pixels and call image.setRGB(x,y,color) or you can somehow do image.createGraphics and use the Graphics/2D class.


int bitsPerPixel = 1;  //If this doesn't work, change this to 32
int totalWidth = width * bitsPerPixel;

for(int y = 0; y < height; y++) {
  for(int x = 0; x < width; x++) {
    image.setRGB(x,y,byteBuffer.get(x * totalWidth + y));
  }
}

or


Graphics2D g = null;

try {
  g = image.createGraphics();
  //I'm not sure how you would do this?
}
finally {
  if(g != null) {
    g.dispose();
  }
}

I’m kind of curious why you even need to do this when you can do the following:


private boolean createTextures = true;
private BufferedImage bufferedImage = null;
private Texture texture = null;

public void loadBufferedImage(File file) {
  BufferedImage image = null;
  try {
    image = ImageIO.read(file);
  }
  catch(IOException e) {
    System.err.println("Unable to load the IMAGE.");
    System.exit(0);
  }
  return(image);
}

public Constructor() {
  bufferedImage = loadBufferedImage(new File("Image.png");
}

//JOGL's display method...
public display(...) {
  if(createTextures) {
    texture = TextureIO.newTexture(image,false);
    createTextures = false;
  }

  //draw the textures
  texture.enable();
  texture.bind();
  TextureCoords coords = texture.getImageTexCoords();
    //drawing here...
  texture.disable();
}

thank you! i am actually trying to get the texture back from the graphics card, so i can view it as a regular image.