diff options
Diffstat (limited to 'ardor3d-jogl/src/main')
-rw-r--r-- | ardor3d-jogl/src/main/java/com/ardor3d/image/util/jogl/JoglImageLoader.java | 198 |
1 files changed, 128 insertions, 70 deletions
diff --git a/ardor3d-jogl/src/main/java/com/ardor3d/image/util/jogl/JoglImageLoader.java b/ardor3d-jogl/src/main/java/com/ardor3d/image/util/jogl/JoglImageLoader.java index c53e1e3..3446a27 100644 --- a/ardor3d-jogl/src/main/java/com/ardor3d/image/util/jogl/JoglImageLoader.java +++ b/ardor3d-jogl/src/main/java/com/ardor3d/image/util/jogl/JoglImageLoader.java @@ -14,6 +14,7 @@ import java.io.IOException; import java.io.InputStream; import java.nio.Buffer; import java.nio.ByteBuffer; +import java.nio.CharBuffer; import java.nio.DoubleBuffer; import java.nio.FloatBuffer; import java.nio.IntBuffer; @@ -33,10 +34,12 @@ import com.jogamp.opengl.util.texture.TextureIO; public class JoglImageLoader implements ImageLoader { - // private static final Logger logger = Logger.getLogger(JoglImageLoader.class.getName()); - private static boolean createOnHeap = false; + private enum TYPE { + BYTE, SHORT, CHAR, INT, FLOAT, LONG, DOUBLE + }; + private static final String[] supportedFormats = new String[] { "." + TextureIO.DDS.toUpperCase(), "." + TextureIO.GIF.toUpperCase(), "." + TextureIO.JPG.toUpperCase(), "." + TextureIO.PAM.toUpperCase(), "." + TextureIO.PNG.toUpperCase(), "." + TextureIO.PPM.toUpperCase(), "." + TextureIO.SGI.toUpperCase(), @@ -57,89 +60,144 @@ public class JoglImageLoader implements ImageLoader { final TextureData textureData = TextureIO.newTextureData(CapsUtil.getProfile(), is, true, null); final Buffer textureDataBuffer = textureData.getBuffer(); final Image ardorImage = new Image(); - final int elementSize; - if (textureDataBuffer instanceof ShortBuffer) { - elementSize = Buffers.SIZEOF_SHORT; + final TYPE bufferDataType; + if (textureDataBuffer instanceof ByteBuffer) { + bufferDataType = TYPE.BYTE; } else { - if (textureDataBuffer instanceof IntBuffer) { - elementSize = Buffers.SIZEOF_INT; + if (textureDataBuffer instanceof ShortBuffer) { + bufferDataType = TYPE.SHORT; } else { - if (textureDataBuffer instanceof LongBuffer) { - elementSize = Buffers.SIZEOF_LONG; + if (textureDataBuffer instanceof CharBuffer) { + bufferDataType = TYPE.CHAR; } else { - if (textureDataBuffer instanceof FloatBuffer) { - elementSize = Buffers.SIZEOF_FLOAT; + if (textureDataBuffer instanceof IntBuffer) { + bufferDataType = TYPE.INT; } else { - if (textureDataBuffer instanceof DoubleBuffer) { - elementSize = Buffers.SIZEOF_DOUBLE; + if (textureDataBuffer instanceof FloatBuffer) { + bufferDataType = TYPE.FLOAT; } else { - elementSize = 1; + if (textureDataBuffer instanceof LongBuffer) { + bufferDataType = TYPE.LONG; + } else { + if (textureDataBuffer instanceof DoubleBuffer) { + bufferDataType = TYPE.DOUBLE; + } else { + bufferDataType = null; + } + } } } } } } - final int dataSize = textureDataBuffer.capacity() * elementSize; - final ByteBuffer scratch = createOnHeap ? BufferUtils.createByteBufferOnHeap(dataSize) : Buffers - .newDirectByteBuffer(dataSize); - if (textureDataBuffer instanceof ShortBuffer) { - final ShortBuffer shortTextureDataBuffer = (ShortBuffer) textureDataBuffer; - while (textureDataBuffer.hasRemaining()) { - scratch.putShort(shortTextureDataBuffer.get()); - } + if (bufferDataType == null) { + throw new UnsupportedOperationException("Unknown buffer type " + textureDataBuffer.getClass().getName()); } else { - if (textureDataBuffer instanceof IntBuffer) { - final IntBuffer intTextureDataBuffer = (IntBuffer) textureDataBuffer; - while (textureDataBuffer.hasRemaining()) { - scratch.putInt(intTextureDataBuffer.get()); + final int pixelComponentSize; + switch (bufferDataType) { + case BYTE: + pixelComponentSize = Buffers.SIZEOF_BYTE; + break; + case SHORT: + pixelComponentSize = Buffers.SIZEOF_SHORT; + break; + case CHAR: + pixelComponentSize = Buffers.SIZEOF_CHAR; + break; + case INT: + pixelComponentSize = Buffers.SIZEOF_INT; + break; + case FLOAT: + pixelComponentSize = Buffers.SIZEOF_FLOAT; + break; + case LONG: + pixelComponentSize = Buffers.SIZEOF_LONG; + break; + case DOUBLE: + pixelComponentSize = Buffers.SIZEOF_DOUBLE; + break; + default: + // it should never happen + pixelComponentSize = 0; + } + final int dataSize = textureDataBuffer.capacity() * pixelComponentSize; + final ByteBuffer scratch = createOnHeap ? BufferUtils.createByteBufferOnHeap(dataSize) : Buffers + .newDirectByteBuffer(dataSize); + if (flipped) { + final int bytesPerPixel = dataSize / (textureData.getWidth() * textureData.getHeight()); + while (scratch.hasRemaining()) { + final int srcPixelIndex = scratch.position() / bytesPerPixel; + final int srcPixelComponentOffset = scratch.position() - (srcPixelIndex * bytesPerPixel); + // final int srcElementIndex = srcPixelIndex * bytesPerPixel + srcPixelComponentOffset; + final int srcColumnIndex = srcPixelIndex % textureData.getWidth(); + final int scrRowIndex = (srcPixelIndex - srcColumnIndex) / textureData.getHeight(); + final int dstColumnIndex = srcColumnIndex; + final int dstRowIndex = (textureData.getHeight() - 1) - scrRowIndex; + final int dstPixelIndex = dstRowIndex * textureData.getWidth() + dstColumnIndex; + final int dstPixelComponentOffset = srcPixelComponentOffset; + final int dstElementIndex = dstPixelIndex * bytesPerPixel + dstPixelComponentOffset; + switch (bufferDataType) { + case BYTE: + scratch.put(((ByteBuffer) textureDataBuffer).get(dstElementIndex)); + break; + case SHORT: + scratch.putShort(((ShortBuffer) textureDataBuffer).get(dstElementIndex)); + break; + case CHAR: + scratch.putChar(((CharBuffer) textureDataBuffer).get(dstElementIndex)); + break; + case INT: + scratch.putInt(((IntBuffer) textureDataBuffer).get(dstElementIndex)); + break; + case FLOAT: + scratch.putFloat(((FloatBuffer) textureDataBuffer).get(dstElementIndex)); + break; + case LONG: + scratch.putLong(((LongBuffer) textureDataBuffer).get(dstElementIndex)); + break; + case DOUBLE: + scratch.putDouble(((DoubleBuffer) textureDataBuffer).get(dstElementIndex)); + break; + default: + // it should never happen + } } + } else { - if (textureDataBuffer instanceof LongBuffer) { - final LongBuffer longTextureDataBuffer = (LongBuffer) textureDataBuffer; - while (textureDataBuffer.hasRemaining()) { - scratch.putLong(longTextureDataBuffer.get()); - } - } else { - if (textureDataBuffer instanceof FloatBuffer) { - final FloatBuffer floatTextureDataBuffer = (FloatBuffer) textureDataBuffer; - while (textureDataBuffer.hasRemaining()) { - scratch.putFloat(floatTextureDataBuffer.get()); - } - } else { - if (textureDataBuffer instanceof DoubleBuffer) { - final DoubleBuffer doubleTextureDataBuffer = (DoubleBuffer) textureDataBuffer; - while (textureDataBuffer.hasRemaining()) { - scratch.putDouble(doubleTextureDataBuffer.get()); - } - } else { - if (textureDataBuffer instanceof ByteBuffer) { - scratch.put((ByteBuffer) textureDataBuffer); - } - } - } + switch (bufferDataType) { + case BYTE: + scratch.put((ByteBuffer) textureDataBuffer); + break; + case SHORT: + scratch.asShortBuffer().put((ShortBuffer) textureDataBuffer); + break; + case CHAR: + scratch.asCharBuffer().put((CharBuffer) textureDataBuffer); + break; + case INT: + scratch.asIntBuffer().put((IntBuffer) textureDataBuffer); + break; + case FLOAT: + scratch.asFloatBuffer().put((FloatBuffer) textureDataBuffer); + case LONG: + scratch.asLongBuffer().put((LongBuffer) textureDataBuffer); + break; + case DOUBLE: + scratch.asDoubleBuffer().put((DoubleBuffer) textureDataBuffer); + break; + default: + // it should never happen } } + scratch.rewind(); + textureDataBuffer.rewind(); + ardorImage.setWidth(textureData.getWidth()); + ardorImage.setHeight(textureData.getHeight()); + ardorImage.setData(scratch); + ardorImage.setDataFormat(JoglTextureUtil.getImageDataFormat(textureData.getPixelFormat())); + // ardorImage.setDataType(JoglTextureUtil.getPixelDataType(textureData.getPixelType())); + ardorImage.setDataType(PixelDataType.UnsignedByte); + return ardorImage; } - scratch.rewind(); - textureDataBuffer.rewind(); - if (flipped) { - // FIXME - /* - * final int width = textureData.getWidth(); final int height = textureData.getHeight(); final int - * dataLineSize = elementSize * width; final byte[] buf0 = new byte[dataLineSize], buf1 = new - * byte[dataLineSize]; for (int lineIndex = 0; lineIndex < height / 2; lineIndex++) { final int - * line0DataIndex = lineIndex * dataLineSize; final int line1DataIndex = (height - lineIndex - 1) * - * dataLineSize; scratch.position(line0DataIndex); scratch.get(buf0); scratch.position(line1DataIndex); - * scratch.get(buf1); scratch.position(line0DataIndex); scratch.put(buf1); scratch.position(line1DataIndex); - * scratch.put(buf0); } scratch.rewind(); - */ - } - ardorImage.setWidth(textureData.getWidth()); - ardorImage.setHeight(textureData.getHeight()); - ardorImage.setData(scratch); - ardorImage.setDataFormat(JoglTextureUtil.getImageDataFormat(textureData.getPixelFormat())); - // ardorImage.setDataType(JoglTextureUtil.getPixelDataType(textureData.getPixelType())); - ardorImage.setDataType(PixelDataType.UnsignedByte); - return ardorImage; } } |