Loading 8bpp RGBA png - error

Started by lainmaster, June 18, 2009, 12:31:49

Previous topic - Next topic

lainmaster

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

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)
        [...]


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?

Mickelukas

I use the following function for converting a picture to bytes and I never had any problems with my 8 bit png's.

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;
}

broumbroum

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 .

lainmaster

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

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...

broumbroum


lainmaster

Great! Thanks for all the help =D