From b36ca1f8a21f3aa25a08a40097cb5f07393534c7 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Wed, 22 Feb 2012 03:52:34 +0100 Subject: 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 --- .../jogamp/opengl/util/texture/TextureData.java | 2 +- .../com/jogamp/opengl/util/texture/TextureIO.java | 44 +++++---- .../util/texture/spi/NetPbmTextureWriter.java | 78 +++++++++------ .../jogamp/opengl/util/texture/spi/TGAImage.java | 13 +-- .../TestGLReadBufferUtilTextureIOWrite01NEWT.java | 98 ++++++++++++++++++ .../TestGLReadBufferUtilTextureIOWrite02NEWT.java | 110 +++++++++++++++++++++ 6 files changed, 284 insertions(+), 61 deletions(-) create mode 100644 src/test/com/jogamp/opengl/test/junit/jogl/util/TestGLReadBufferUtilTextureIOWrite01NEWT.java create mode 100644 src/test/com/jogamp/opengl/test/junit/jogl/util/TestGLReadBufferUtilTextureIOWrite02NEWT.java (limited to 'src') 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