LWJGL
May 22, 2013, 10:33:30 *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: LWJGL 2.9.0 released!
 
   Home   Help Search Login Register  



Pages: [1]
  Print  
Author Topic: Loading 8bpp RGBA png - error  (Read 3472 times)
lainmaster
Regular nerd
**
Posts: 64


« on: June 18, 2009, 05:31:49 »

When trying to load an 8bpp RGBA png image, I get the following error:
I get the following error:

Code:
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:
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?
Logged
Mickelukas
Talks Too Much
***
Posts: 167


WWW
« Reply #1 on: June 18, 2009, 05: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:
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;
}
Logged

broumbroum
Prolific Timewaster
****
Posts: 316



WWW
« Reply #2 on: June 18, 2009, 09: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 .
Logged

Sf3JSwing project, code repo (Apache License) -  http://www.sourceforge.net/p/sf3jswing
Sf3jswing Project Forum - http://apps.sourceforge.net/phpbb/sf3jswing/
lainmaster
Regular nerd
**
Posts: 64


« Reply #3 on: June 20, 2009, 20: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:
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...
Logged
broumbroum
Prolific Timewaster
****
Posts: 316



WWW
« Reply #4 on: June 21, 2009, 06:52:41 »

That could be correct. Wink
Logged

Sf3JSwing project, code repo (Apache License) -  http://www.sourceforge.net/p/sf3jswing
Sf3jswing Project Forum - http://apps.sourceforge.net/phpbb/sf3jswing/
lainmaster
Regular nerd
**
Posts: 64


« Reply #5 on: June 21, 2009, 08:06:05 »

Great! Thanks for all the help =D
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines
SMFAds for Free Forums
Valid XHTML 1.0! Valid CSS!