diff options
21 files changed, 390 insertions, 549 deletions
diff --git a/src/jogl/classes/com/jogamp/opengl/util/FileUtil.java b/src/jogl/classes/com/jogamp/opengl/util/FileUtil.java deleted file mode 100644 index 6ad0da825..000000000 --- a/src/jogl/classes/com/jogamp/opengl/util/FileUtil.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright (c) 2005 Sun Microsystems, Inc. All Rights Reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * - Redistribution of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistribution 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. - * - * Neither the name of Sun Microsystems, Inc. or the names of - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * This software is provided "AS IS," without a warranty of any kind. ALL - * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, - * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A - * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN - * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR - * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR - * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR - * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR - * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE - * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, - * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF - * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. - * - * You acknowledge that this software is not designed or intended for use - * in the design, construction, operation or maintenance of any nuclear - * facility. - * - * Sun gratefully acknowledges that this software was originally authored - * and developed by Kenneth Bradley Russell and Christopher John Kline. - */ - -package com.jogamp.opengl.util; - -import java.io.*; - -/** Utilities for dealing with files. */ - -public class FileUtil { - private FileUtil() {} - - /** - * Returns the lowercase suffix of the given file name (the text - * after the last '.' in the file name). Returns null if the file - * name has no suffix. Only operates on the given file name; - * performs no I/O operations. - * - * @param file name of the file - * @return lowercase suffix of the file name - * @throws NullPointerException if file is null - */ - - public static String getFileSuffix(File file) { - return getFileSuffix(file.getName()); - } - - /** - * Returns the lowercase suffix of the given file name (the text - * after the last '.' in the file name). Returns null if the file - * name has no suffix. Only operates on the given file name; - * performs no I/O operations. - * - * @param filename name of the file - * @return lowercase suffix of the file name - * @throws NullPointerException if filename is null - */ - public static String getFileSuffix(String filename) { - int lastDot = filename.lastIndexOf('.'); - if (lastDot < 0) { - return null; - } - return toLowerCase(filename.substring(lastDot + 1)); - } - - private static String toLowerCase(String arg) { - if (arg == null) { - return null; - } - - return arg.toLowerCase(); - } -} diff --git a/src/jogl/classes/com/jogamp/opengl/util/GLPixelStorageModes.java b/src/jogl/classes/com/jogamp/opengl/util/GLPixelStorageModes.java new file mode 100644 index 000000000..e9cccbb93 --- /dev/null +++ b/src/jogl/classes/com/jogamp/opengl/util/GLPixelStorageModes.java @@ -0,0 +1,108 @@ +/** + * Copyright 2010 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.util; + +import javax.media.opengl.GL; +import javax.media.opengl.GL2; +import javax.media.opengl.GL2ES2; +import javax.media.opengl.GLException; + +/** + * Utility to safely set and restore the pack and unpack pixel storage mode, + * regardless of the GLProfile. + */ +public class GLPixelStorageModes { + int savedPackAlignment; + int savedUnpackAlignment; + int[] tmp = new int[1]; + boolean saved = false; + + static int glGetInteger(GL gl, int pname, int[] tmp) { + gl.glGetIntegerv(pname, tmp, 0); + return tmp[0]; + } + + private final void save(GL gl) { + if(gl.isGL2()) { + gl.getGL2().glPushClientAttrib(GL2.GL_CLIENT_PIXEL_STORE_BIT); + gl.glPixelStorei(GL2.GL_PACK_ROW_LENGTH, 0); + gl.glPixelStorei(GL2.GL_PACK_SKIP_ROWS, 0); + gl.glPixelStorei(GL2.GL_PACK_SKIP_PIXELS, 0); + gl.glPixelStorei(GL2.GL_PACK_SWAP_BYTES, 0); + } else { + // core and embedded only deal with pack/unpack alignment + savedPackAlignment = glGetInteger(gl, GL2ES2.GL_PACK_ALIGNMENT, tmp); + savedUnpackAlignment = glGetInteger(gl, GL2ES2.GL_UNPACK_ALIGNMENT, tmp); + } + saved = true; + } + + /** + * Sets the {@link GL2ES2.GL_PACK_ALIGNMENT}. Saves the pixel storage modes if not saved yet. + */ + public final void setPackAlignment(GL gl, int packAlignment) { + if(!saved) { save(gl); } + gl.glPixelStorei(GL2ES2.GL_PACK_ALIGNMENT, packAlignment); + } + + /** + * Sets the {@link GL2ES2.GL_UNPACK_ALIGNMENT}. Saves the pixel storage modes if not saved yet. + */ + public final void setUnpackAlignment(GL gl, int unpackAlignment) { + if(!saved) { save(gl); } + gl.glPixelStorei(GL2ES2.GL_UNPACK_ALIGNMENT, unpackAlignment); + } + + /** + * Sets the {@link GL2ES2.GL_PACK_ALIGNMENT} and {@link GL2ES2.GL_UNPACK_ALIGNMENT}. + * Saves the pixel storage modes if not saved yet. + */ + public final void setAlignment(GL gl, int packAlignment, int unpackAlignment) { + setPackAlignment(gl, packAlignment); + setUnpackAlignment(gl, unpackAlignment); + } + + /** + * Restores the pixel storage mode. + * @throws GLException if not saved via one of the set methods. + */ + public final void restore(GL gl) throws GLException { + if(!saved) { + throw new GLException("pixel storage modes not saved"); + } + if(gl.isGL2()) { + gl.getGL2().glPopClientAttrib(); + } else { + gl.glPixelStorei(GL2ES2.GL_PACK_ALIGNMENT, savedPackAlignment); + } + saved = false; + } +} + + diff --git a/src/jogl/classes/com/jogamp/opengl/util/GLReadBufferUtil.java b/src/jogl/classes/com/jogamp/opengl/util/GLReadBufferUtil.java new file mode 100644 index 000000000..e5cf7d0d0 --- /dev/null +++ b/src/jogl/classes/com/jogamp/opengl/util/GLReadBufferUtil.java @@ -0,0 +1,181 @@ +/** + * Copyright 2010 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.util; + +import com.jogamp.common.nio.Buffers; + +import java.io.File; +import java.io.IOException; +import java.nio.*; +import javax.media.opengl.*; + +import com.jogamp.opengl.util.texture.Texture; +import com.jogamp.opengl.util.texture.TextureData; +import com.jogamp.opengl.util.texture.TextureIO; + +/** + * Utility to read out the current FB to TextureData, optionally writing the data back to a texture object. + * <p>May be used directly to write the TextureData to file (screenshot).</p> + */ +public class GLReadBufferUtil { + protected final int components, alignment; + protected final Texture readTexture; + protected final GLPixelStorageModes psm; + + protected int readPixelSizeLast = 0; + protected ByteBuffer readPixelBuffer = null; + protected TextureData readTextureData = null; + + /** + * @param alpha true for RGBA readPixels, otherwise RGB readPixels + * @param write2Texture true if readPixel's TextureData shall be written to a 2d Texture + */ + public GLReadBufferUtil(boolean alpha, boolean write2Texture) { + components = alpha ? 4 : 3 ; + alignment = alpha ? 4 : 1 ; + readTexture = write2Texture ? new Texture(GL.GL_TEXTURE_2D) : null ; + psm = new GLPixelStorageModes(); + } + + public boolean isValid() { + return null!=readTextureData && null!=readPixelBuffer ; + } + + /** + * @return the raw pixel ByteBuffer, filled by {@link #readPixels(GLAutoDrawable, boolean)} + */ + public ByteBuffer getPixelBuffer() { return readPixelBuffer; } + + /** + * rewind the raw pixel ByteBuffer + */ + public void rewindPixelBuffer() { if( null != readPixelBuffer ) { readPixelBuffer.rewind(); } } + + /** + * @return the resulting TextureData, filled by {@link #readPixels(GLAutoDrawable, boolean)} + */ + public TextureData getTextureData() { return readTextureData; } + + /** + * @return the Texture object filled by {@link #readPixels(GLAutoDrawable, boolean)}, + * if this instance writes to a 2d Texture, otherwise null. + * @see #GLReadBufferUtil(boolean, boolean) + */ + public Texture getTexture() { return readTexture; } + + /** + * Write the TextureData filled by {@link #readPixels(GLAutoDrawable, boolean)} to file + */ + public void write(File dest) { + try { + TextureIO.write(readTextureData, dest); + rewindPixelBuffer(); + } catch (IOException ex) { + throw new RuntimeException("can not write to file: " + dest.getAbsolutePath(), ex); + } + } + + /** + * Read the drawable's pixels to TextureData and Texture, if requested at construction + * + * @param gl the current GL object + * @param drawable the drawable to read from + * @param flip weather to flip the data vertically or not + * + * @see #GLReadBufferUtil(boolean, boolean) + */ + public void readPixels(GL gl, GLDrawable drawable, boolean flip) { + final int textureInternalFormat, textureDataFormat; + final int textureDataType = GL.GL_UNSIGNED_BYTE; + if(4 == components) { + textureInternalFormat=GL.GL_RGBA; + textureDataFormat=GL.GL_RGBA; + } else { + textureInternalFormat=GL.GL_RGB; + textureDataFormat=GL.GL_RGB; + } + final int readPixelSize = drawable.getWidth() * drawable.getHeight() * components ; + boolean newData = false; + if(readPixelSize>readPixelSizeLast) { + readPixelBuffer = Buffers.newDirectByteBuffer(readPixelSize); + readPixelSizeLast = readPixelSize ; + try { + readTextureData = new TextureData( + gl.getGLProfile(), + textureInternalFormat, + drawable.getWidth(), drawable.getHeight(), + 0, + textureDataFormat, + textureDataType, + false, false, + flip, + readPixelBuffer, + null /* Flusher */); + newData = true; + } catch (Exception e) { + readTextureData = null; + readPixelBuffer = null; + readPixelSizeLast = 0; + throw new RuntimeException("can not fetch offscreen texture", e); + } + } + if(null!=readPixelBuffer) { + psm.setAlignment(gl, alignment, alignment); + readPixelBuffer.clear(); + gl.glReadPixels(0, 0, drawable.getWidth(), drawable.getHeight(), textureDataFormat, textureDataType, readPixelBuffer); + readPixelBuffer.rewind(); + if(null != readTexture) { + if(newData) { + readTexture.updateImage(gl, readTextureData); + } else { + readTexture.updateSubImage(gl, readTextureData, 0, + 0, 0, // src offset + 0, 0, // dst offset + drawable.getWidth(), drawable.getHeight()); + } + readPixelBuffer.rewind(); + } + psm.restore(gl); + } + } + + public void dispose(GL gl) { + if(null != readTexture) { + readTexture.destroy(gl); + readTextureData = null; + } + if(null != readPixelBuffer) { + readPixelBuffer.clear(); + readPixelBuffer = null; + } + readPixelSizeLast = 0; + } + +} + diff --git a/src/jogl/classes/com/jogamp/opengl/util/StreamUtil.java b/src/jogl/classes/com/jogamp/opengl/util/StreamUtil.java deleted file mode 100644 index 4cf8cb1f0..000000000 --- a/src/jogl/classes/com/jogamp/opengl/util/StreamUtil.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * - Redistribution of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistribution 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. - * - * Neither the name of Sun Microsystems, Inc. or the names of - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * This software is provided "AS IS," without a warranty of any kind. ALL - * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, - * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A - * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN - * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR - * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR - * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR - * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR - * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE - * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, - * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF - * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. - * - * You acknowledge that this software is not designed or intended for use - * in the design, construction, operation or maintenance of any nuclear - * facility. - * - * Sun gratefully acknowledges that this software was originally authored - * and developed by Kenneth Bradley Russell and Christopher John Kline. - */ - -package com.jogamp.opengl.util; - -import java.io.*; -import java.nio.*; - -/** Utilities for dealing with streams. */ - -public class StreamUtil { - private StreamUtil() {} - - public static byte[] readAll2Array(InputStream stream) throws IOException { - BytesRead bytesRead = readAllImpl(stream); - byte[] data = bytesRead.data; - if (bytesRead.payloadLen != data.length) { - data = new byte[bytesRead.payloadLen]; - System.arraycopy(bytesRead.data, 0, data, 0, bytesRead.payloadLen); - } - return data; - } - - public static ByteBuffer readAll2Buffer(InputStream stream) throws IOException { - BytesRead bytesRead = readAllImpl(stream); - return GLBuffers.newDirectByteBuffer(bytesRead.data, 0, bytesRead.payloadLen); - } - - private static BytesRead readAllImpl(InputStream stream) throws IOException { - // FIXME: Shall we do this here ? - if( !(stream instanceof BufferedInputStream) ) { - stream = new BufferedInputStream(stream); - } - int avail = stream.available(); - byte[] data = new byte[avail]; - int numRead = 0; - int pos = 0; - do { - if (pos + avail > data.length) { - byte[] newData = new byte[pos + avail]; - System.arraycopy(data, 0, newData, 0, pos); - data = newData; - } - numRead = stream.read(data, pos, avail); - if (numRead >= 0) { - pos += numRead; - } - avail = stream.available(); - } while (avail > 0 && numRead >= 0); - - return new BytesRead(pos, data); - } - - private static class BytesRead { - BytesRead(int payloadLen, byte[] data) { - this.payloadLen=payloadLen; - this.data=data; - } - int payloadLen; - byte[] data; - } -} diff --git a/src/jogl/classes/com/jogamp/opengl/util/awt/Screenshot.java b/src/jogl/classes/com/jogamp/opengl/util/awt/Screenshot.java index 7019d720f..fa66673fd 100644 --- a/src/jogl/classes/com/jogamp/opengl/util/awt/Screenshot.java +++ b/src/jogl/classes/com/jogamp/opengl/util/awt/Screenshot.java @@ -36,17 +36,20 @@ package com.jogamp.opengl.util.awt; -import java.awt.image.*; -import java.io.*; -import java.nio.*; -import java.nio.channels.*; -import javax.imageio.*; +import java.awt.image.BufferedImage; +import java.awt.image.DataBufferByte; +import java.io.File; +import java.io.IOException; +import java.nio.ByteBuffer; -import javax.media.opengl.*; -import javax.media.opengl.glu.*; -import javax.media.opengl.glu.gl2.*; +import javax.imageio.ImageIO; +import javax.media.opengl.GL2; +import javax.media.opengl.GLException; +import javax.media.opengl.glu.gl2.GLUgl2; -import com.jogamp.opengl.util.*; +import com.jogamp.common.util.IOUtil; +import com.jogamp.opengl.util.GLPixelStorageModes; +import com.jogamp.opengl.util.TGAWriter; /** Utilities for taking screenshots of OpenGL applications. */ @@ -148,8 +151,8 @@ public class Screenshot { GL2 gl = GLUgl2.getCurrentGL2(); // Set up pixel storage modes - PixelStorageModes psm = new PixelStorageModes(); - psm.save(gl); + GLPixelStorageModes psm = new GLPixelStorageModes(); + psm.setPackAlignment(gl, 1); int readbackType = (alpha ? GL2.GL_ABGR_EXT : GL2.GL_BGR); @@ -255,8 +258,8 @@ public class Screenshot { GL2 gl = GLUgl2.getCurrentGL2(); // Set up pixel storage modes - PixelStorageModes psm = new PixelStorageModes(); - psm.save(gl); + GLPixelStorageModes psm = new GLPixelStorageModes(); + psm.setPackAlignment(gl, 1); // read the BGR values into the image gl.glReadPixels(x, y, width, height, readbackType, @@ -375,7 +378,7 @@ public class Screenshot { int width, int height, boolean alpha) throws IOException, GLException { - String fileSuffix = FileUtil.getFileSuffix(file); + String fileSuffix = IOUtil.getFileSuffix(file); if (alpha && (fileSuffix.equals("jpg") || fileSuffix.equals("jpeg"))) { // JPEGs can't deal properly with alpha channels alpha = false; @@ -387,46 +390,10 @@ public class Screenshot { } } - private static int glGetInteger(GL2 gl, int pname, int[] tmp) { - gl.glGetIntegerv(pname, tmp, 0); - return tmp[0]; - } - private static void checkExtABGR() { GL2 gl = GLUgl2.getCurrentGL2(); if (!gl.isExtensionAvailable("GL_EXT_abgr")) { throw new IllegalArgumentException("Saving alpha channel requires GL_EXT_abgr"); } - } - - static class PixelStorageModes { - int packAlignment; - int packRowLength; - int packSkipRows; - int packSkipPixels; - int packSwapBytes; - int[] tmp = new int[1]; - - void save(GL2 gl) { - packAlignment = glGetInteger(gl, GL2.GL_PACK_ALIGNMENT, tmp); - packRowLength = glGetInteger(gl, GL2.GL_PACK_ROW_LENGTH, tmp); - packSkipRows = glGetInteger(gl, GL2.GL_PACK_SKIP_ROWS, tmp); - packSkipPixels = glGetInteger(gl, GL2.GL_PACK_SKIP_PIXELS, tmp); - packSwapBytes = glGetInteger(gl, GL2.GL_PACK_SWAP_BYTES, tmp); - - gl.glPixelStorei(GL2.GL_PACK_ALIGNMENT, 1); - gl.glPixelStorei(GL2.GL_PACK_ROW_LENGTH, 0); - gl.glPixelStorei(GL2.GL_PACK_SKIP_ROWS, 0); - gl.glPixelStorei(GL2.GL_PACK_SKIP_PIXELS, 0); - gl.glPixelStorei(GL2.GL_PACK_SWAP_BYTES, 0); - } - - void restore(GL2 gl) { - gl.glPixelStorei(GL2.GL_PACK_ALIGNMENT, packAlignment); - gl.glPixelStorei(GL2.GL_PACK_ROW_LENGTH, packRowLength); - gl.glPixelStorei(GL2.GL_PACK_SKIP_ROWS, packSkipRows); - gl.glPixelStorei(GL2.GL_PACK_SKIP_PIXELS, packSkipPixels); - gl.glPixelStorei(GL2.GL_PACK_SWAP_BYTES, packSwapBytes); - } - } + } } diff --git a/src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderCode.java b/src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderCode.java index ad17dd288..be3a1de73 100644 --- a/src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderCode.java +++ b/src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderCode.java @@ -29,6 +29,8 @@ package com.jogamp.opengl.util.glsl; import com.jogamp.common.nio.Buffers; +import com.jogamp.common.util.IOUtil; + import javax.media.opengl.*; import com.jogamp.opengl.util.*; import jogamp.opengl.Debug; @@ -350,7 +352,7 @@ public class ShaderCode { if (url == null) { return null; } - return StreamUtil.readAll2Buffer(new BufferedInputStream(url.openStream())); + return IOUtil.copyStream2ByteBuffer( new BufferedInputStream( url.openStream() ) ); } catch (IOException e) { throw new RuntimeException(e); } diff --git a/src/jogl/classes/com/jogamp/opengl/util/texture/Texture.java b/src/jogl/classes/com/jogamp/opengl/util/texture/Texture.java index 35604ba30..08f56ef27 100644 --- a/src/jogl/classes/com/jogamp/opengl/util/texture/Texture.java +++ b/src/jogl/classes/com/jogamp/opengl/util/texture/Texture.java @@ -43,7 +43,6 @@ import javax.media.opengl.*; import javax.media.opengl.glu.*; import javax.media.nativewindow.NativeWindowFactory; import jogamp.opengl.*; -import com.jogamp.opengl.util.texture.*; import com.jogamp.opengl.util.texture.spi.*; /** 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 f598422bf..149a2d46c 100644 --- a/src/jogl/classes/com/jogamp/opengl/util/texture/TextureData.java +++ b/src/jogl/classes/com/jogamp/opengl/util/texture/TextureData.java @@ -36,10 +36,11 @@ package com.jogamp.opengl.util.texture; -import java.nio.*; +import java.nio.Buffer; -import javax.media.opengl.*; -import com.jogamp.opengl.util.*; +import javax.media.opengl.GLProfile; + +import com.jogamp.opengl.util.GLBuffers; /** * Represents the data for an OpenGL texture. This is separated from 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 792f80ff8..5be7f922b 100644 --- a/src/jogl/classes/com/jogamp/opengl/util/texture/TextureIO.java +++ b/src/jogl/classes/com/jogamp/opengl/util/texture/TextureIO.java @@ -1,5 +1,6 @@ /* * Copyright (c) 2005 Sun Microsystems, Inc. All Rights Reserved. + * Copyright (c) 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 @@ -39,16 +40,33 @@ package com.jogamp.opengl.util.texture; -import java.io.*; -import java.net.*; -import java.nio.*; -import java.util.*; +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.nio.Buffer; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import javax.media.opengl.GL; +import javax.media.opengl.GL2; +import javax.media.opengl.GLContext; +import javax.media.opengl.GLException; +import javax.media.opengl.GLProfile; -import javax.media.opengl.*; -import javax.media.opengl.glu.*; import jogamp.opengl.Debug; -import com.jogamp.opengl.util.*; -import com.jogamp.opengl.util.texture.spi.*; + +import com.jogamp.common.util.IOUtil; +import com.jogamp.opengl.util.texture.spi.DDSImage; +import com.jogamp.opengl.util.texture.spi.NetPbmTextureWriter; +import com.jogamp.opengl.util.texture.spi.SGIImage; +import com.jogamp.opengl.util.texture.spi.TGAImage; +import com.jogamp.opengl.util.texture.spi.TextureProvider; +import com.jogamp.opengl.util.texture.spi.TextureWriter; /** <P> Provides input and output facilities for both loading OpenGL textures from disk and streams as well as writing textures already @@ -177,7 +195,7 @@ public class TextureIO { boolean mipmap, String fileSuffix) throws IOException { if (fileSuffix == null) { - fileSuffix = FileUtil.getFileSuffix(file); + fileSuffix = IOUtil.getFileSuffix(file); } return newTextureDataImpl(glp, file, 0, 0, mipmap, fileSuffix); } @@ -234,7 +252,7 @@ public class TextureIO { boolean mipmap, String fileSuffix) throws IOException { if (fileSuffix == null) { - fileSuffix = FileUtil.getFileSuffix(url.getPath()); + fileSuffix = IOUtil.getFileSuffix(url.getPath()); } return newTextureDataImpl(glp, url, 0, 0, mipmap, fileSuffix); } @@ -288,7 +306,7 @@ public class TextureIO { } if (fileSuffix == null) { - fileSuffix = FileUtil.getFileSuffix(file); + fileSuffix = IOUtil.getFileSuffix(file); } return newTextureDataImpl(glp, file, internalFormat, pixelFormat, mipmap, fileSuffix); @@ -380,7 +398,7 @@ public class TextureIO { } if (fileSuffix == null) { - fileSuffix = FileUtil.getFileSuffix(url.getPath()); + fileSuffix = IOUtil.getFileSuffix(url.getPath()); } return newTextureDataImpl(glp, url, internalFormat, pixelFormat, mipmap, fileSuffix); @@ -437,7 +455,7 @@ public class TextureIO { public static Texture newTexture(File file, boolean mipmap) throws IOException, GLException { GL gl = GLContext.getCurrentGL(); GLProfile glp = gl.getGLProfile(); - TextureData data = newTextureData(glp, file, mipmap, FileUtil.getFileSuffix(file)); + TextureData data = newTextureData(glp, file, mipmap, IOUtil.getFileSuffix(file)); Texture texture = newTexture(gl, data); data.flush(); return texture; @@ -494,7 +512,7 @@ public class TextureIO { */ public static Texture newTexture(URL url, boolean mipmap, String fileSuffix) throws IOException, GLException { if (fileSuffix == null) { - fileSuffix = FileUtil.getFileSuffix(url.getPath()); + fileSuffix = IOUtil.getFileSuffix(url.getPath()); } GL gl = GLContext.getCurrentGL(); GLProfile glp = gl.getGLProfile(); @@ -878,7 +896,7 @@ public class TextureIO { boolean mipmap, String fileSuffix) throws IOException { if (DDS.equals(fileSuffix) || - DDS.equals(FileUtil.getFileSuffix(file))) { + DDS.equals(IOUtil.getFileSuffix(file))) { DDSImage image = DDSImage.read(file); return newTextureData(glp, image, internalFormat, pixelFormat, mipmap); } @@ -893,7 +911,7 @@ public class TextureIO { String fileSuffix) throws IOException { if (DDS.equals(fileSuffix) || DDSImage.isDDSImage(stream)) { - byte[] data = StreamUtil.readAll2Array(stream); + byte[] data = IOUtil.copyStream2ByteArray(stream); ByteBuffer buf = ByteBuffer.wrap(data); DDSImage image = DDSImage.read(buf); return newTextureData(glp, image, internalFormat, pixelFormat, mipmap); @@ -1014,7 +1032,7 @@ public class TextureIO { internalFormat, pixelFormat, mipmap, - ((fileSuffix != null) ? fileSuffix : FileUtil.getFileSuffix(file))); + ((fileSuffix != null) ? fileSuffix : IOUtil.getFileSuffix(file))); } finally { inStream.close(); } @@ -1113,7 +1131,7 @@ public class TextureIO { static class DDSTextureWriter implements TextureWriter { public boolean write(File file, TextureData data) throws IOException { - if (DDS.equals(FileUtil.getFileSuffix(file))) { + if (DDS.equals(IOUtil.getFileSuffix(file))) { // See whether the DDS writer can handle this TextureData int pixelFormat = data.getPixelFormat(); int pixelType = data.getPixelType(); @@ -1162,7 +1180,7 @@ public class TextureIO { static class SGITextureWriter implements TextureWriter { public boolean write(File file, TextureData data) throws IOException { - String fileSuffix = FileUtil.getFileSuffix(file); + String fileSuffix = IOUtil.getFileSuffix(file); if (SGI.equals(fileSuffix) || SGI_RGB.equals(fileSuffix)) { // See whether the SGI writer can handle this TextureData @@ -1206,7 +1224,7 @@ public class TextureIO { static class TGATextureWriter implements TextureWriter { public boolean write(File file, TextureData data) throws IOException { - if (TGA.equals(FileUtil.getFileSuffix(file))) { + if (TGA.equals(IOUtil.getFileSuffix(file))) { // See whether the TGA writer can handle this TextureData int pixelFormat = data.getPixelFormat(); int pixelType = data.getPixelType(); 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 499dce7fb..ae9618490 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 @@ -40,13 +40,12 @@ package com.jogamp.opengl.util.texture.spi; import java.io.*; -import java.net.*; import java.nio.*; import javax.media.opengl.*; -import com.jogamp.opengl.util.*; + +import com.jogamp.common.util.IOUtil; import com.jogamp.opengl.util.texture.*; -import com.jogamp.opengl.util.texture.spi.*; public class NetPbmTextureWriter implements TextureWriter { int magic; @@ -87,17 +86,17 @@ public class NetPbmTextureWriter implements TextureWriter { // file suffix selection if (0==magic) { - if (PPM.equals(FileUtil.getFileSuffix(file))) { + if (PPM.equals(IOUtil.getFileSuffix(file))) { magic = 6; - } else if (PAM.equals(FileUtil.getFileSuffix(file))) { + } else if (PAM.equals(IOUtil.getFileSuffix(file))) { magic = 7; } else { return false; } } - int pixelFormat = data.getPixelFormat(); - int pixelType = data.getPixelType(); + final int pixelFormat = data.getPixelFormat(); + final int pixelType = data.getPixelType(); if ((pixelFormat == GL.GL_RGB || pixelFormat == GL.GL_RGBA) && (pixelType == GL.GL_BYTE || diff --git a/src/jogl/classes/com/jogamp/opengl/util/texture/spi/awt/IIOTextureWriter.java b/src/jogl/classes/com/jogamp/opengl/util/texture/spi/awt/IIOTextureWriter.java index 405211204..89d0d20a1 100644 --- a/src/jogl/classes/com/jogamp/opengl/util/texture/spi/awt/IIOTextureWriter.java +++ b/src/jogl/classes/com/jogamp/opengl/util/texture/spi/awt/IIOTextureWriter.java @@ -42,12 +42,12 @@ package com.jogamp.opengl.util.texture.spi.awt; import java.awt.Graphics; import java.awt.image.*; import java.io.*; -import java.net.*; import java.nio.*; import javax.imageio.*; import javax.media.opengl.*; -import com.jogamp.opengl.util.*; + +import com.jogamp.common.util.IOUtil; import com.jogamp.opengl.util.awt.*; import com.jogamp.opengl.util.texture.*; import com.jogamp.opengl.util.texture.spi.*; @@ -101,7 +101,7 @@ public class IIOTextureWriter implements TextureWriter { ImageUtil.flipImageVertically(image); // Happened to notice that writing RGBA images to JPEGS is broken - if (TextureIO.JPG.equals(FileUtil.getFileSuffix(file)) && + if (TextureIO.JPG.equals(IOUtil.getFileSuffix(file)) && image.getType() == BufferedImage.TYPE_4BYTE_ABGR) { BufferedImage tmpImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_3BYTE_BGR); @@ -111,7 +111,7 @@ public class IIOTextureWriter implements TextureWriter { image = tmpImage; } - return ImageIO.write(image, FileUtil.getFileSuffix(file), file); + return ImageIO.write(image, IOUtil.getFileSuffix(file), file); } throw new IOException("ImageIO writer doesn't support this pixel format / type (only GL_RGB/A + bytes)"); diff --git a/src/jogl/classes/jogamp/graph/font/typecast/TypecastFontConstructor.java b/src/jogl/classes/jogamp/graph/font/typecast/TypecastFontConstructor.java index 179e4ed2c..cb0d1a372 100644 --- a/src/jogl/classes/jogamp/graph/font/typecast/TypecastFontConstructor.java +++ b/src/jogl/classes/jogamp/graph/font/typecast/TypecastFontConstructor.java @@ -56,7 +56,7 @@ public class TypecastFontConstructor implements FontConstructor { public Font create(URL furl) throws IOException { final File tf = File.createTempFile( "joglfont", ".ttf"); - final int len = IOUtil.copyURLToFile(furl, tf); + final int len = IOUtil.copyURL2File(furl, tf); if(len==0) { tf.delete(); throw new GLException("Font of stream "+furl+" was zero bytes"); diff --git a/src/test/com/jogamp/opengl/test/junit/graph/demos/GPURendererListenerBase01.java b/src/test/com/jogamp/opengl/test/junit/graph/demos/GPURendererListenerBase01.java index 9587a672d..ed6bb3cde 100644 --- a/src/test/com/jogamp/opengl/test/junit/graph/demos/GPURendererListenerBase01.java +++ b/src/test/com/jogamp/opengl/test/junit/graph/demos/GPURendererListenerBase01.java @@ -27,6 +27,7 @@ */ package com.jogamp.opengl.test.junit.graph.demos; +import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; @@ -48,6 +49,7 @@ import com.jogamp.graph.curve.opengl.Renderer; import com.jogamp.newt.event.KeyEvent; import com.jogamp.newt.event.KeyListener; import com.jogamp.newt.opengl.GLWindow; +import com.jogamp.opengl.util.GLReadBufferUtil; /** * @@ -60,7 +62,7 @@ import com.jogamp.newt.opengl.GLWindow; * - s: screenshot */ public abstract class GPURendererListenerBase01 implements GLEventListener { - private Screenshot screenshot; + private GLReadBufferUtil screenshot; private Renderer renderer; private int renderModes; private boolean debug; @@ -88,7 +90,7 @@ public abstract class GPURendererListenerBase01 implements GLEventListener { this.renderModes = renderModes; this.debug = debug; this.trace = trace; - this.screenshot = new Screenshot(); + this.screenshot = new GLReadBufferUtil(false, false); } public final Renderer getRenderer() { return renderer; } @@ -191,8 +193,9 @@ public abstract class GPURendererListenerBase01 implements GLEventListener { PrintWriter pw = new PrintWriter(sw); pw.printf("-%03dx%03d-Z%04d-T%04d-%s", drawable.getWidth(), drawable.getHeight(), (int)Math.abs(zoom), texSize, objName); - String filename = dir + tech + sw +".tga"; - screenshot.surface2File(drawable, filename /*, exportAlpha */); + final String filename = dir + tech + sw +".tga"; + screenshot.readPixels(drawable.getGL(), drawable, false); + screenshot.write(new File(filename)); } int screenshot_num = 0; diff --git a/src/test/com/jogamp/opengl/test/junit/graph/demos/ReadBufferUtil.java b/src/test/com/jogamp/opengl/test/junit/graph/demos/ReadBufferUtil.java deleted file mode 100644 index dc1ea2da3..000000000 --- a/src/test/com/jogamp/opengl/test/junit/graph/demos/ReadBufferUtil.java +++ /dev/null @@ -1,108 +0,0 @@ -/** - * Copyright 2010 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.graph.demos; - -import com.jogamp.opengl.util.GLBuffers; -import java.nio.*; -import javax.media.opengl.*; - -import com.jogamp.opengl.util.texture.Texture; -import com.jogamp.opengl.util.texture.TextureData; - -public class ReadBufferUtil { - protected int readPixelSizeLast = 0; - protected Buffer readPixelBuffer = null; - protected TextureData readTextureData = null; - protected Texture readTexture = new Texture(GL.GL_TEXTURE_2D); - - public Buffer getPixelBuffer() { return readPixelBuffer; } - public void rewindPixelBuffer() { readPixelBuffer.rewind(); } - - public TextureData getTextureData() { return readTextureData; } - public Texture getTexture() { return readTexture; } - - public boolean isValid() { - return null!=readTexture && null!=readTextureData && null!=readPixelBuffer ; - } - - public void fetchOffscreenTexture(GLDrawable drawable, GL gl) { - int readPixelSize = drawable.getWidth() * drawable.getHeight() * 3 ; // RGB - boolean newData = false; - if(readPixelSize>readPixelSizeLast) { - readPixelBuffer = GLBuffers.newDirectGLBuffer(GL.GL_UNSIGNED_BYTE, readPixelSize); - readPixelSizeLast = readPixelSize ; - try { - readTextureData = new TextureData( - gl.getGLProfile(), - // gl.isGL2GL3()?gl.GL_RGBA:gl.GL_RGB, - GL.GL_RGB, - drawable.getWidth(), drawable.getHeight(), - 0, - GL.GL_RGB, - GL.GL_UNSIGNED_BYTE, - false, false, - false /* flip */, - readPixelBuffer, - null /* Flusher */); - newData = true; - } catch (Exception e) { - readTextureData = null; - readPixelBuffer = null; - readPixelSizeLast = 0; - throw new RuntimeException("can not fetch offscreen texture", e); - } - } - if(null!=readPixelBuffer) { - readPixelBuffer.clear(); - gl.glReadPixels(0, 0, drawable.getWidth(), drawable.getHeight(), GL.GL_RGB, GL.GL_UNSIGNED_BYTE, readPixelBuffer); - readPixelBuffer.rewind(); - if(newData) { - readTexture.updateImage(gl, readTextureData); - } else { - readTexture.updateSubImage(gl, readTextureData, 0, - 0, 0, // src offset - 0, 0, // dst offset - drawable.getWidth(), drawable.getHeight()); - } - readPixelBuffer.rewind(); - } - } - - public void dispose(GL gl) { - readTexture.destroy(gl); - readTextureData = null; - if(null != readPixelBuffer) { - readPixelBuffer.clear(); - readPixelBuffer = null; - } - readPixelSizeLast = 0; - } - -} - diff --git a/src/test/com/jogamp/opengl/test/junit/graph/demos/Screenshot.java b/src/test/com/jogamp/opengl/test/junit/graph/demos/Screenshot.java deleted file mode 100644 index f4b6f6dd9..000000000 --- a/src/test/com/jogamp/opengl/test/junit/graph/demos/Screenshot.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.jogamp.opengl.test.junit.graph.demos; - -import java.io.File; -import java.io.IOException; - -import javax.media.opengl.GL; -import javax.media.opengl.GLAutoDrawable; - -import com.jogamp.opengl.util.texture.TextureIO; - -public class Screenshot { - - ReadBufferUtil readBufferUtil = new ReadBufferUtil(); - - public void dispose(GL gl) { - readBufferUtil.dispose(gl); - } - - public void surface2File(GLAutoDrawable drawable, String filename) { - GL gl = drawable.getGL(); - // FIXME glFinish() is an expensive paranoia sync, should not be necessary due to spec - gl.glFinish(); - readBufferUtil.fetchOffscreenTexture(drawable, gl); - gl.glFinish(); - try { - surface2File(filename); - } catch (IOException ex) { - throw new RuntimeException("can not write survace to file", ex); - } - } - - void surface2File(String filename) throws IOException { - File file = new File(filename); - TextureIO.write(readBufferUtil.getTextureData(), file); - System.err.println("Wrote: " + file.getAbsolutePath() + ", ..."); - readBufferUtil.rewindPixelBuffer(); - } - -} diff --git a/src/test/com/jogamp/opengl/test/junit/graph/demos/ui/UIListenerBase01.java b/src/test/com/jogamp/opengl/test/junit/graph/demos/ui/UIListenerBase01.java index df2dca4fb..b89f87be4 100644 --- a/src/test/com/jogamp/opengl/test/junit/graph/demos/ui/UIListenerBase01.java +++ b/src/test/com/jogamp/opengl/test/junit/graph/demos/ui/UIListenerBase01.java @@ -27,6 +27,7 @@ */ package com.jogamp.opengl.test.junit.graph.demos.ui; +import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; @@ -41,13 +42,12 @@ import javax.media.opengl.GLPipelineFactory; import javax.media.opengl.GLRunnable; import com.jogamp.graph.curve.opengl.RegionRenderer; -import com.jogamp.graph.curve.opengl.TextRenderer; import com.jogamp.newt.event.KeyEvent; import com.jogamp.newt.event.KeyListener; import com.jogamp.newt.event.MouseEvent; import com.jogamp.newt.event.MouseListener; import com.jogamp.newt.opengl.GLWindow; -import com.jogamp.opengl.test.junit.graph.demos.Screenshot; +import com.jogamp.opengl.util.GLReadBufferUtil; /** * @@ -60,7 +60,7 @@ import com.jogamp.opengl.test.junit.graph.demos.Screenshot; * - s: screenshot */ public abstract class UIListenerBase01 implements GLEventListener { - private Screenshot screenshot; + private GLReadBufferUtil screenshot; private RegionRenderer rRenderer; private boolean debug; private boolean trace; @@ -85,7 +85,7 @@ public abstract class UIListenerBase01 implements GLEventListener { this.rRenderer = rRenderer; this.debug = debug; this.trace = trace; - this.screenshot = new Screenshot(); + this.screenshot = new GLReadBufferUtil(false, false); } public final RegionRenderer getRegionRenderer() { return rRenderer; } @@ -179,8 +179,9 @@ public abstract class UIListenerBase01 implements GLEventListener { PrintWriter pw = new PrintWriter(sw); pw.printf("-%03dx%03d-Z%04d-T%04d-%s", drawable.getWidth(), drawable.getHeight(), (int)Math.abs(zoom), 0, objName); - String filename = dir + tech + sw +".tga"; - screenshot.surface2File(drawable, filename /*, exportAlpha */); + final String filename = dir + tech + sw +".tga"; + screenshot.readPixels(drawable.getGL(), drawable, false); + screenshot.write(new File(filename)); } int screenshot_num = 0; diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/offscreen/ReadBuffer2File.java b/src/test/com/jogamp/opengl/test/junit/jogl/offscreen/ReadBuffer2File.java index 95e7d6e53..b829c8deb 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/offscreen/ReadBuffer2File.java +++ b/src/test/com/jogamp/opengl/test/junit/jogl/offscreen/ReadBuffer2File.java @@ -37,7 +37,7 @@ import java.io.File; public class ReadBuffer2File extends ReadBufferBase { public ReadBuffer2File(GLDrawable externalRead) { - super(externalRead); + super(externalRead, false); } @Override @@ -52,10 +52,9 @@ public class ReadBuffer2File extends ReadBufferBase { } File file = File.createTempFile("shot" + shotNum + "-", ".ppm"); - TextureIO.write(readBufferUtil.getTextureData(), file); + readBufferUtil.write(file); System.out.println("Wrote: " + file.getAbsolutePath() + ", ..."); shotNum++; - readBufferUtil.rewindPixelBuffer(); } @Override diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/offscreen/ReadBuffer2Screen.java b/src/test/com/jogamp/opengl/test/junit/jogl/offscreen/ReadBuffer2Screen.java index 23271dde2..8c315e97f 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/offscreen/ReadBuffer2Screen.java +++ b/src/test/com/jogamp/opengl/test/junit/jogl/offscreen/ReadBuffer2Screen.java @@ -47,7 +47,7 @@ public class ReadBuffer2Screen extends ReadBufferBase { boolean enableBufferVBO = true; // FIXME public ReadBuffer2Screen (GLDrawable externalRead) { - super(externalRead); + super(externalRead, true); } @Override diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/offscreen/ReadBufferBase.java b/src/test/com/jogamp/opengl/test/junit/jogl/offscreen/ReadBufferBase.java index 53675bc31..e3ca25ae6 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/offscreen/ReadBufferBase.java +++ b/src/test/com/jogamp/opengl/test/junit/jogl/offscreen/ReadBufferBase.java @@ -30,16 +30,19 @@ package com.jogamp.opengl.test.junit.jogl.offscreen; import javax.media.opengl.*; +import com.jogamp.opengl.util.GLReadBufferUtil; + public class ReadBufferBase implements GLEventListener { public boolean glDebug = false ; public boolean glTrace = false ; protected GLDrawable externalRead; - ReadBufferUtil readBufferUtil = new ReadBufferUtil(); + GLReadBufferUtil readBufferUtil; - public ReadBufferBase (GLDrawable externalRead) { + public ReadBufferBase (GLDrawable externalRead, boolean write2Texture) { this.externalRead = externalRead ; + this.readBufferUtil = new GLReadBufferUtil(false, write2Texture); } public void init(GLAutoDrawable drawable) { @@ -84,7 +87,7 @@ public class ReadBufferBase implements GLEventListener { public void display(GLAutoDrawable drawable) { GL gl = drawable.getGL(); - readBufferUtil.fetchOffscreenTexture(drawable, gl); + readBufferUtil.readPixels(gl, drawable, false); } } diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/offscreen/ReadBufferUtil.java b/src/test/com/jogamp/opengl/test/junit/jogl/offscreen/ReadBufferUtil.java deleted file mode 100644 index 5a2c73cf4..000000000 --- a/src/test/com/jogamp/opengl/test/junit/jogl/offscreen/ReadBufferUtil.java +++ /dev/null @@ -1,106 +0,0 @@ -/** - * Copyright 2010 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.offscreen; - -import com.jogamp.opengl.util.GLBuffers; -import java.nio.*; -import javax.media.opengl.*; - -import com.jogamp.opengl.util.texture.Texture; -import com.jogamp.opengl.util.texture.TextureData; - -public class ReadBufferUtil { - protected int readPixelSizeLast = 0; - protected Buffer readPixelBuffer = null; - protected TextureData readTextureData = null; - protected Texture readTexture = new Texture(GL.GL_TEXTURE_2D); - - public Buffer getPixelBuffer() { return readPixelBuffer; } - public void rewindPixelBuffer() { readPixelBuffer.rewind(); } - - public TextureData getTextureData() { return readTextureData; } - public Texture getTexture() { return readTexture; } - - public boolean isValid() { - return null!=readTexture && null!=readTextureData && null!=readPixelBuffer ; - } - - public void fetchOffscreenTexture(GLDrawable drawable, GL gl) { - int readPixelSize = drawable.getWidth() * drawable.getHeight() * 3 ; // RGB - boolean newData = false; - if(readPixelSize>readPixelSizeLast) { - readPixelBuffer = GLBuffers.newDirectGLBuffer(GL.GL_UNSIGNED_BYTE, readPixelSize); - readPixelSizeLast = readPixelSize ; - try { - readTextureData = new TextureData( - gl.getGLProfile(), - // gl.isGL2GL3()?gl.GL_RGBA:gl.GL_RGB, - gl.GL_RGB, - drawable.getWidth(), drawable.getHeight(), - 0, - gl.GL_RGB, - gl.GL_UNSIGNED_BYTE, - false, false, - false /* flip */, - readPixelBuffer, - null /* Flusher */); - newData = true; - } catch (Exception e) { - readTextureData = null; - readPixelBuffer = null; - readPixelSizeLast = 0; - throw new RuntimeException("can not fetch offscreen texture", e); - } - } - if(null!=readPixelBuffer) { - readPixelBuffer.clear(); - gl.glReadPixels(0, 0, drawable.getWidth(), drawable.getHeight(), GL.GL_RGB, GL.GL_UNSIGNED_BYTE, readPixelBuffer); - readPixelBuffer.rewind(); - if(newData) { - readTexture.updateImage(gl, readTextureData); - } else { - readTexture.updateSubImage(gl, readTextureData, 0, - 0, 0, // src offset - 0, 0, // dst offset - drawable.getWidth(), drawable.getHeight()); - } - readPixelBuffer.rewind(); - } - } - - public void dispose(GL gl) { - readTexture.destroy(gl); - readTextureData = null; - readPixelBuffer.clear(); - readPixelBuffer = null; - readPixelSizeLast = 0; - } - -} - diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/offscreen/Surface2File.java b/src/test/com/jogamp/opengl/test/junit/jogl/offscreen/Surface2File.java index 77fd40181..8ca1f3ef8 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/offscreen/Surface2File.java +++ b/src/test/com/jogamp/opengl/test/junit/jogl/offscreen/Surface2File.java @@ -30,6 +30,7 @@ package com.jogamp.opengl.test.junit.jogl.offscreen; import javax.media.opengl.*; +import com.jogamp.opengl.util.GLReadBufferUtil; import com.jogamp.opengl.util.texture.TextureIO; import java.io.File; @@ -39,7 +40,7 @@ import javax.media.nativewindow.*; public class Surface2File implements SurfaceUpdatedListener { - ReadBufferUtil readBufferUtil = new ReadBufferUtil(); + GLReadBufferUtil readBufferUtil = new GLReadBufferUtil(false, false); int shotNum = 0; public void dispose(GL gl) { @@ -54,7 +55,7 @@ public class Surface2File implements SurfaceUpdatedListener { GL gl = ctx.getGL(); // FIXME glFinish() is an expensive paranoia sync, should not be necessary due to spec gl.glFinish(); - readBufferUtil.fetchOffscreenTexture(drawable, gl); + readBufferUtil.readPixels(gl, drawable, false); gl.glFinish(); try { surface2File("shot"); @@ -71,9 +72,8 @@ public class Surface2File implements SurfaceUpdatedListener { } File file = File.createTempFile(basename + shotNum + "-", ".ppm"); - TextureIO.write(readBufferUtil.getTextureData(), file); + readBufferUtil.write(file); System.err.println("Wrote: " + file.getAbsolutePath() + ", ..."); shotNum++; - readBufferUtil.rewindPixelBuffer(); } } |