diff options
6 files changed, 284 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 diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/util/TestGLReadBufferUtilTextureIOWrite01NEWT.java b/src/test/com/jogamp/opengl/test/junit/jogl/util/TestGLReadBufferUtilTextureIOWrite01NEWT.java new file mode 100644 index 000000000..4bae83e74 --- /dev/null +++ b/src/test/com/jogamp/opengl/test/junit/jogl/util/TestGLReadBufferUtilTextureIOWrite01NEWT.java @@ -0,0 +1,98 @@ +/** + * Copyright 2011 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of JogAmp Community. + */ + +package com.jogamp.opengl.test.junit.jogl.util; + +import java.io.File; + +import com.jogamp.newt.opengl.GLWindow; + +import javax.media.opengl.GLAutoDrawable; +import javax.media.opengl.GLCapabilities; +import javax.media.opengl.GLEventListener; +import javax.media.opengl.GLProfile; +import com.jogamp.opengl.util.GLReadBufferUtil; + +import com.jogamp.opengl.test.junit.util.UITestCase; +import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2; + +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +public class TestGLReadBufferUtilTextureIOWrite01NEWT extends UITestCase { + static GLProfile glp; + static GLCapabilities caps; + static int width, height; + + @BeforeClass + public static void initClass() { + glp = GLProfile.getDefault(); + Assert.assertNotNull(glp); + caps = new GLCapabilities(glp); + Assert.assertNotNull(caps); + width = 256; + height = 256; + } + + protected void snapshot(GLAutoDrawable drawable, boolean alpha, boolean flip, String filename) { + GLReadBufferUtil screenshot = new GLReadBufferUtil(alpha, false); + if(screenshot.readPixels(drawable.getGL(), drawable, flip)) { + screenshot.write(new File(filename)); + } + } + + @Test + public void testWriteTGAAndPAM() throws InterruptedException { + GLWindow glWindow = GLWindow.create(caps); + Assert.assertNotNull(glWindow); + glWindow.setTitle("Shared Gears NEWT Test"); + glWindow.setSize(width, height); + glWindow.addGLEventListener(new GearsES2(1)); + glWindow.addGLEventListener(new GLEventListener() { + public void init(GLAutoDrawable drawable) {} + public void dispose(GLAutoDrawable drawable) {} + public void display(GLAutoDrawable drawable) { + // snapshot(drawable, false, true, getSimpleTestName(".")+"-rgb_-"+drawable.getGLProfile().getName()+".ppm"); + snapshot(drawable, true, false, getSimpleTestName(".")+"-rgba-"+drawable.getGLProfile().getName()+".tga"); + snapshot(drawable, true, true, getSimpleTestName(".")+"-rgba-"+drawable.getGLProfile().getName()+".pam"); + snapshot(drawable, false, false, getSimpleTestName(".")+"-rgb_-"+drawable.getGLProfile().getName()+".tga"); + snapshot(drawable, false, true, getSimpleTestName(".")+"-rgb_-"+drawable.getGLProfile().getName()+".pam"); + } + public void reshape(GLAutoDrawable drawable, int x, int y, + int width, int height) { } + }); + glWindow.setVisible(true); + Thread.sleep(60); + glWindow.destroy(); + } + + public static void main(String args[]) { + org.junit.runner.JUnitCore.main(TestGLReadBufferUtilTextureIOWrite01NEWT.class.getName()); + } +} diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/util/TestGLReadBufferUtilTextureIOWrite02NEWT.java b/src/test/com/jogamp/opengl/test/junit/jogl/util/TestGLReadBufferUtilTextureIOWrite02NEWT.java new file mode 100644 index 000000000..0c48a10e5 --- /dev/null +++ b/src/test/com/jogamp/opengl/test/junit/jogl/util/TestGLReadBufferUtilTextureIOWrite02NEWT.java @@ -0,0 +1,110 @@ +/** + * Copyright 2011 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of JogAmp Community. + */ + +package com.jogamp.opengl.test.junit.jogl.util; + +import java.io.File; +import java.io.PrintWriter; +import java.io.StringWriter; + +import com.jogamp.newt.opengl.GLWindow; + +import javax.media.opengl.GLAutoDrawable; +import javax.media.opengl.GLCapabilities; +import javax.media.opengl.GLEventListener; +import javax.media.opengl.GLProfile; +import com.jogamp.opengl.util.GLReadBufferUtil; + +import com.jogamp.opengl.test.junit.util.UITestCase; +import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2; + +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +public class TestGLReadBufferUtilTextureIOWrite02NEWT extends UITestCase { + static GLProfile glp; + static GLCapabilities caps; + static int width, height; + + @BeforeClass + public static void initClass() { + glp = GLProfile.getDefault(); + Assert.assertNotNull(glp); + caps = new GLCapabilities(glp); + Assert.assertNotNull(caps); + width = 256; + height = 256; + } + + protected void snapshot(GLAutoDrawable drawable, GLReadBufferUtil screenshot, String filename) { + if(screenshot.readPixels(drawable.getGL(), drawable, false)) { + screenshot.write(new File(filename)); + } + } + + @Test + public void testWriteTGAWithResize() throws InterruptedException { + final GLReadBufferUtil screenshot = new GLReadBufferUtil(true, false); + GLWindow glWindow = GLWindow.create(caps); + Assert.assertNotNull(glWindow); + glWindow.setTitle("Shared Gears NEWT Test"); + glWindow.setSize(width, height); + glWindow.addGLEventListener(new GearsES2(1)); + glWindow.addGLEventListener(new GLEventListener() { + int i=0; + public void init(GLAutoDrawable drawable) {} + public void dispose(GLAutoDrawable drawable) {} + public void display(GLAutoDrawable drawable) { + StringWriter filename = new StringWriter(); + { + PrintWriter pw = new PrintWriter(filename); + pw.printf("%s-rgba-%s-%03dx%03d-n%03d.tga", + getSimpleTestName("."), drawable.getGLProfile().getName(), + drawable.getWidth(), drawable.getHeight(), i++); + } + if(screenshot.readPixels(drawable.getGL(), drawable, false)) { + screenshot.write(new File(filename.toString())); + } + } + public void reshape(GLAutoDrawable drawable, int x, int y, + int width, int height) { } + }); + glWindow.setVisible(true); + Thread.sleep(60); + glWindow.setSize(300, 300); + Thread.sleep(60); + glWindow.setSize(400, 400); + Thread.sleep(60); + glWindow.destroy(); + } + + public static void main(String args[]) { + org.junit.runner.JUnitCore.main(TestGLReadBufferUtilTextureIOWrite02NEWT.class.getName()); + } +} |