package org.newdawn.slick.opengl; import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; import org.lwjgl.BufferUtils; /** * The PNG imge data source that is pure java reading PNGs * * @author Matthias Mann (original code) */ public class PNGImageData implements LoadableImageData { /** The width of the data loaded */ private int width; /** The height of the data loaded */ private int height; /** The texture height */ private int texHeight; /** The texture width */ private int texWidth; /** The decoder used to load the PNG */ private PNGDecoder decoder; /** The bit depth of the image */ private int bitDepth; /** The scratch buffer storing the image data */ private ByteBuffer scratch; /** * @see org.newdawn.slick.opengl.ImageData#getDepth() */ public int getDepth() { return bitDepth; } /** * @see org.newdawn.slick.opengl.ImageData#getImageBufferData() */ public ByteBuffer getImageBufferData() { return scratch; } /** * @see org.newdawn.slick.opengl.ImageData#getTexHeight() */ public int getTexHeight() { return texHeight; } /** * @see org.newdawn.slick.opengl.ImageData#getTexWidth() */ public int getTexWidth() { return texWidth; } /** * @see org.newdawn.slick.opengl.LoadableImageData#loadImage(java.io.InputStream) */ public ByteBuffer loadImage(InputStream fis) throws IOException { return loadImage(fis, false, null); } /** * @see org.newdawn.slick.opengl.LoadableImageData#loadImage(java.io.InputStream, boolean, int[]) */ public ByteBuffer loadImage(InputStream fis, boolean flipped, int[] transparent) throws IOException { return loadImage(fis, flipped, false, transparent); } /** * @see org.newdawn.slick.opengl.LoadableImageData#loadImage(java.io.InputStream, boolean, boolean, int[]) */ public ByteBuffer loadImage(InputStream fis, boolean flipped, boolean forceAlpha, int[] transparent) throws IOException { if (transparent != null) { forceAlpha = true; throw new IOException("Transparent color not support in custom PNG Decoder"); } PNGDecoder decoder = new PNGDecoder(fis); if (!decoder.isRGB()) { throw new IOException("Only RGB formatted images are supported by the PNGLoader"); } width = decoder.getWidth(); height = decoder.getHeight(); texWidth = get2Fold(width); texHeight = get2Fold(height); int perPixel = decoder.hasAlpha() ? 4 : 3; bitDepth = decoder.hasAlpha() ? 32 : 24; // Get a pointer to the image memory scratch = BufferUtils.createByteBuffer(texWidth * texHeight * perPixel); decoder.decode(scratch, texWidth * perPixel, perPixel == 4 ? PNGDecoder.RGBA : PNGDecoder.RGB); if (height < texHeight-1) { int topOffset = (texHeight-1) * (texWidth*perPixel); int bottomOffset = (height-1) * (texWidth*perPixel); for (int x=0;x