diff options
author | Sven Gothel <[email protected]> | 2012-02-22 03:52:34 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2012-02-22 03:52:34 +0100 |
commit | b36ca1f8a21f3aa25a08a40097cb5f07393534c7 (patch) | |
tree | 6764e90e4f4c79319b1c35558a8fd339dc0a825c /src/jogl/classes/com/jogamp/opengl/util | |
parent | 7c76354038ea96c884028d34efa1b8b39363ba49 (diff) |
TextureIO (TGA/NetPbm): Allow GL_BGR[A] and use GL_BGRA if available ; Fix NetPbmTextureWriter ; Added unit tests
- Allow GL_BGR[A] usage (TGA / NetPbm)
- Use GL_BGRA if available (TGA),
utilize GLContext.isTextureFormatBGRA8888Available()
- Fix NetPbmTextureWriter
- Maintain 'auto' magic mode for 'spi' role in TextureIO (was overwritten)
- Use FileChannel for nio buffer streaming, instead of array copy
Diffstat (limited to 'src/jogl/classes/com/jogamp/opengl/util')
4 files changed, 76 insertions, 61 deletions
diff --git a/src/jogl/classes/com/jogamp/opengl/util/texture/TextureData.java b/src/jogl/classes/com/jogamp/opengl/util/texture/TextureData.java index 149a2d46c..928f91ce5 100644 --- a/src/jogl/classes/com/jogamp/opengl/util/texture/TextureData.java +++ b/src/jogl/classes/com/jogamp/opengl/util/texture/TextureData.java @@ -355,7 +355,7 @@ public class TextureData { } public String toString() { - return "TextureData["+width+"x"+height+", internFormat "+internalFormat+", pixelFormat "+pixelFormat+", pixelType "+pixelType+", border "+border+", estSize "+estimatedMemorySize+", alignment "+alignment+", rowlen "+rowLength; + return "TextureData["+width+"x"+height+", internFormat 0x"+Integer.toHexString(internalFormat)+", pixelFormat 0x"+Integer.toHexString(pixelFormat)+", pixelType 0x"+Integer.toHexString(pixelType)+", border "+border+", estSize "+estimatedMemorySize+", alignment "+alignment+", rowlen "+rowLength; } //---------------------------------------------------------------------- diff --git a/src/jogl/classes/com/jogamp/opengl/util/texture/TextureIO.java b/src/jogl/classes/com/jogamp/opengl/util/texture/TextureIO.java index 5be7f922b..b89daffc9 100644 --- a/src/jogl/classes/com/jogamp/opengl/util/texture/TextureIO.java +++ b/src/jogl/classes/com/jogamp/opengl/util/texture/TextureIO.java @@ -54,6 +54,7 @@ import java.util.List; import javax.media.opengl.GL; import javax.media.opengl.GL2; +import javax.media.opengl.GL2GL3; import javax.media.opengl.GLContext; import javax.media.opengl.GLException; import javax.media.opengl.GLProfile; @@ -639,7 +640,7 @@ public class TextureIO { fetchedFormat = GL.GL_RGB; break; case GL.GL_RGBA: - case GL2.GL_BGRA: + case GL.GL_BGRA: case GL2.GL_ABGR_EXT: case GL.GL_RGBA8: bytesPerPixel = 4; @@ -1229,33 +1230,38 @@ public class TextureIO { int pixelFormat = data.getPixelFormat(); int pixelType = data.getPixelType(); if ((pixelFormat == GL.GL_RGB || - pixelFormat == GL.GL_RGBA) && + pixelFormat == GL.GL_RGBA || + pixelFormat == GL2.GL_BGR || + pixelFormat == GL.GL_BGRA ) && (pixelType == GL.GL_BYTE || pixelType == GL.GL_UNSIGNED_BYTE)) { - ByteBuffer buf = ((data.getBuffer() != null) ? - (ByteBuffer) data.getBuffer() : - (ByteBuffer) data.getMipmapData()[0]); - // Must reverse order of red and blue channels to get correct results - int skip = ((pixelFormat == GL.GL_RGB) ? 3 : 4); - for (int i = 0; i < buf.remaining(); i += skip) { - byte red = buf.get(i + 0); - byte blue = buf.get(i + 2); - buf.put(i + 0, blue); - buf.put(i + 2, red); + + ByteBuffer buf = (ByteBuffer) data.getBuffer(); + if (null == buf) { + buf = (ByteBuffer) data.getMipmapData()[0]; + } + buf.rewind(); + + if( pixelFormat == GL.GL_RGB || pixelFormat == GL.GL_RGBA ) { + // Must reverse order of red and blue channels to get correct results + int skip = ((pixelFormat == GL.GL_RGB) ? 3 : 4); + for (int i = 0; i < buf.remaining(); i += skip) { + byte red = buf.get(i + 0); + byte blue = buf.get(i + 2); + buf.put(i + 0, blue); + buf.put(i + 2, red); + } } TGAImage image = TGAImage.createFromData(data.getWidth(), data.getHeight(), - (pixelFormat == GL.GL_RGBA), - false, - ((data.getBuffer() != null) ? - (ByteBuffer) data.getBuffer() : - (ByteBuffer) data.getMipmapData()[0])); + (pixelFormat == GL.GL_RGBA || pixelFormat == GL.GL_BGRA), + false, buf); image.write(file); return true; } - - throw new IOException("TGA writer doesn't support this pixel format / type (only GL_RGB/A + bytes)"); + throw new IOException("TGA writer doesn't support this pixel format 0x"+Integer.toHexString(pixelFormat)+ + " / type 0x"+Integer.toHexString(pixelFormat)+" (only GL_RGB/A, GL_BGR/A + bytes)"); } return false; diff --git a/src/jogl/classes/com/jogamp/opengl/util/texture/spi/NetPbmTextureWriter.java b/src/jogl/classes/com/jogamp/opengl/util/texture/spi/NetPbmTextureWriter.java index ae9618490..216c994c0 100644 --- a/src/jogl/classes/com/jogamp/opengl/util/texture/spi/NetPbmTextureWriter.java +++ b/src/jogl/classes/com/jogamp/opengl/util/texture/spi/NetPbmTextureWriter.java @@ -41,6 +41,7 @@ package com.jogamp.opengl.util.texture.spi; import java.io.*; import java.nio.*; +import java.nio.channels.FileChannel; import javax.media.opengl.*; @@ -81,10 +82,11 @@ public class NetPbmTextureWriter implements TextureWriter { public String getSuffix() { return (magic==6)?PPM:PAM; } - public boolean write(File file, - TextureData data) throws IOException { - - // file suffix selection + public boolean write(File file, TextureData data) throws IOException { + boolean res; + final int magic_old = magic; + + // file suffix selection if (0==magic) { if (PPM.equals(IOUtil.getFileSuffix(file))) { magic = 6; @@ -93,24 +95,52 @@ public class NetPbmTextureWriter implements TextureWriter { } else { return false; } + } + try { + res = writeImpl(file, data); + } finally { + magic = magic_old; } - - final int pixelFormat = data.getPixelFormat(); + return res; + } + + private boolean writeImpl(File file, TextureData data) throws IOException { + int pixelFormat = data.getPixelFormat(); final int pixelType = data.getPixelType(); if ((pixelFormat == GL.GL_RGB || - pixelFormat == GL.GL_RGBA) && + pixelFormat == GL.GL_RGBA || + pixelFormat == GL2.GL_BGR || + pixelFormat == GL.GL_BGRA ) && (pixelType == GL.GL_BYTE || pixelType == GL.GL_UNSIGNED_BYTE)) { - int comps = ( pixelFormat == GL.GL_RGBA ) ? 4 : 3 ; + ByteBuffer buf = (ByteBuffer) data.getBuffer(); + if (null == buf ) { + buf = (ByteBuffer) data.getMipmapData()[0]; + } + buf.rewind(); + + int comps = ( pixelFormat == GL.GL_RGBA || pixelFormat == GL.GL_BGRA ) ? 4 : 3 ; + + if( pixelFormat == GL2.GL_BGR || pixelFormat == GL.GL_BGRA ) { + // Must reverse order of red and blue channels to get correct results + for (int i = 0; i < buf.remaining(); i += comps) { + byte red = buf.get(i + 0); + byte blue = buf.get(i + 2); + buf.put(i + 0, blue); + buf.put(i + 2, red); + } + pixelFormat = ( 4 == comps ) ? GL.GL_RGBA : GL.GL_RGB; + data.setPixelFormat(pixelFormat); + } if(magic==6 && comps==4) { throw new IOException("NetPbmTextureWriter magic 6 (PPM) doesn't RGBA pixel format, use magic 7 (PAM)"); } FileOutputStream fos = new FileOutputStream(file); - - StringBuffer header = new StringBuffer(); + + StringBuilder header = new StringBuilder(); header.append("P"); header.append(magic); header.append("\n"); @@ -139,30 +169,16 @@ public class NetPbmTextureWriter implements TextureWriter { } fos.write(header.toString().getBytes()); - - ByteBuffer buf = (ByteBuffer) data.getBuffer(); - if (buf == null) { - buf = (ByteBuffer) data.getMipmapData()[0]; - } - buf.rewind(); - - byte[] bufArray = null; - - try { - bufArray = buf.array(); - } catch (Throwable t) {} - if(null==bufArray) { - bufArray = new byte[data.getWidth()*data.getHeight()*comps]; - buf.get(bufArray); - buf.rewind(); - } - - fos.write(bufArray); + + FileChannel fosc = fos.getChannel(); + fosc.write(buf); + fosc.force(true); + fosc.close(); fos.close(); + buf.rewind(); return true; - } - + } throw new IOException("NetPbmTextureWriter writer doesn't support this pixel format / type (only GL_RGB/A + bytes)"); } } diff --git a/src/jogl/classes/com/jogamp/opengl/util/texture/spi/TGAImage.java b/src/jogl/classes/com/jogamp/opengl/util/texture/spi/TGAImage.java index 16ba538b5..cf35df464 100644 --- a/src/jogl/classes/com/jogamp/opengl/util/texture/spi/TGAImage.java +++ b/src/jogl/classes/com/jogamp/opengl/util/texture/spi/TGAImage.java @@ -43,9 +43,6 @@ import java.io.*; import java.nio.*; import java.nio.channels.*; import javax.media.opengl.*; -import com.jogamp.opengl.util.*; -import com.jogamp.opengl.util.texture.spi.*; -import com.jogamp.opengl.util.texture.*; /** * Targa image reader and writer adapted from sources of the <a href = @@ -144,8 +141,6 @@ public class TGAImage { } Header(LEDataInputStream in) throws IOException { - int ret; - tgaType = TYPE_OLD; // dont try and get footer. // initial header fields @@ -289,9 +284,7 @@ public class TGAImage { */ private void decodeRGBImageU24_32(LEDataInputStream dIn) throws IOException { int i; // row index - int j; // column index int y; // output row index - int raw; // index through the raw input buffer int rawWidth = header.width() * (header.pixelDepth() / 8); byte[] rawBuf = new byte[rawWidth]; byte[] tmpData = new byte[rawWidth * header.height()]; @@ -320,8 +313,8 @@ public class TGAImage { assert header.pixelDepth() == 32; bpp=4; - if(gl.isGL2GL3()) { - format = GL2GL3.GL_BGRA; + if( gl.getContext().isTextureFormatBGRA8888Available() ) { + format = GL.GL_BGRA; } else { format = GL.GL_RGBA; swapBGR(tmpData, rawWidth, header.height(), bpp); @@ -391,10 +384,10 @@ public class TGAImage { buf.rewind(); chan.write(buf); chan.write(data); - data.rewind(); chan.force(true); chan.close(); stream.close(); + data.rewind(); } /** Creates a TGAImage from data supplied by the end user. Shares |