Hello Guest

Loading 8bpp RGBA png - error

  • 5 Replies
  • 8134 Views
Loading 8bpp RGBA png - error
« on: June 18, 2009, 12:31:49 »
When trying to load an 8bpp RGBA png image, I get the following error:
I get the following error:

Code: [Select]
Exception in thread "main" java.lang.IllegalArgumentException: Number of remaining buffer elements is 24576, must be at least 98304
        at org.lwjgl.BufferChecks.throwBufferSizeException(BufferChecks.java:125)
        at org.lwjgl.BufferChecks.checkBufferSize(BufferChecks.java:140)
        at org.lwjgl.NondirectBufferWrapper.wrapBuffer(NondirectBufferWrapper.java:101)
        at org.lwjgl.opengl.GL11.glTexImage2D(GL11.java:2926)
        at NewGame.Texture.loadTexture(Texture.java:116)
        [...]

Code: [Select]
private void loadTexture(String sPath){
System.out.println(this.getClass().getResource(sPath));
BufferedImage oBufferedImage = null;
try{
System.out.println("Loading image: \"" + sPath + "\"");
oBufferedImage = ImageIO.read(this.getClass().getResource(sPath));
} catch(IOException e){
System.err.println("Oh noes :x");
e.printStackTrace();
}

int bytesPerPixel = oBufferedImage.getColorModel().getPixelSize() / 8;

DataBufferByte oDataBufferByte = ((DataBufferByte) oBufferedImage.getRaster().getDataBuffer());
ByteBuffer oPixelsBuffer = ByteBuffer.allocateDirect(oBufferedImage.getWidth() * oBufferedImage.getHeight() * bytesPerPixel).order(ByteOrder.nativeOrder());

oPixelsBuffer.put(oDataBufferByte.getData());
oPixelsBuffer.rewind();

IntBuffer buf = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
GL11.glGenTextures(buf); // Create Texture In OpenGL

GL11.glBindTexture(GL11.GL_TEXTURE_2D, buf.get(0));
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);

if(oBufferedImage.getColorModel().getPixelSize() == 32){
System.out.println("32");
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, oBufferedImage.getWidth(), oBufferedImage.getHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, oPixelsBuffer);
}else{
System.out.println(oBufferedImage.getColorModel().getPixelSize());
// THIS IS THE LINE WITH THE ERROR
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, oBufferedImage.getWidth(), oBufferedImage.getHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, oPixelsBuffer);
}
_iId = buf.get(0);
_iWidth = oBufferedImage.getWidth();
_iHeight = oBufferedImage.getHeight();

}

Works fine with 32bpp RGBA png images.

I'm guessing it's because I'm using GL11.GL_RGBA for it, I tried using GL11.GL_RGBA2, but got a "wrong enum" error, or something like that.

Any help?

Re: Loading 8bpp RGBA png - error
« Reply #1 on: June 18, 2009, 12:54:35 »
I use the following function for converting a picture to bytes and I never had any problems with my 8 bit png's.

Code: [Select]
public static ByteBuffer getByteBuffer(BufferedImage image) {
    final ByteBuffer buf;
        final byte[] data = ((DataBufferByte)image.getRaster().getDataBuffer()).getData();
buf = ByteBuffer.wrap(new byte[data.length]);
buf.put(data, 0, data.length);
buf.rewind();
return buf;
}

Re: Loading 8bpp RGBA png - error
« Reply #2 on: June 18, 2009, 16:59:06 »
You're right, lainmaster, RGBA is not correct for native BufferedImage. You must first re-arrange bytes inside the raster to match components order OR you may directly use GL_EXT_abgr which correspond to the usual BufferedImage.4bytes_ABGR

I've made a Sf3Texture loader : See a post about it from my project : http://sourceforge.net/apps/phpbb/sf3jswing/viewtopic.php?f=4&t=8 .

Re: Loading 8bpp RGBA png - error
« Reply #3 on: June 21, 2009, 03:25:42 »
Thanks for the info : )

I decided I only want to load 32 and 8 bpp RGBA PNGs. I already had the 32bpp working. For the 8bpp, I did

Code: [Select]
oPixelsBuffer.put((byte)(oBufferedImage.getColorModel().getRed(iData[i])));
oPixelsBuffer.put((byte)(oBufferedImage.getColorModel().getGreen(iData[i])));
oPixelsBuffer.put((byte)(oBufferedImage.getColorModel().getBlue(iData[i])));
oPixelsBuffer.put((byte)(oBufferedImage.getColorModel().getAlpha(iData[i])));

Is this ok? It seems to work...
« Last Edit: June 21, 2009, 04:01:22 by lainmaster »

Re: Loading 8bpp RGBA png - error
« Reply #4 on: June 21, 2009, 13:52:41 »
That could be correct. ;)

Re: Loading 8bpp RGBA png - error
« Reply #5 on: June 21, 2009, 15:06:05 »
Great! Thanks for all the help =D