From b29f221c903aefdf99af8e8a8544b2223036454f Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Sat, 9 Mar 2013 03:41:16 +0100 Subject: Fix buggy unit test for Bug 694: The unpack alignment has to be considered! --- .../jogamp/opengl/util/GLPixelStorageModes.java | 34 ++++- .../opengl/test/junit/jogl/glu/TestBug694AWT.java | 128 ----------------- .../TestBug694ScaleImageUnpackBufferSizeAWT.java | 160 +++++++++++++++++++++ 3 files changed, 189 insertions(+), 133 deletions(-) delete mode 100644 src/test/com/jogamp/opengl/test/junit/jogl/glu/TestBug694AWT.java create mode 100644 src/test/com/jogamp/opengl/test/junit/jogl/glu/TestBug694ScaleImageUnpackBufferSizeAWT.java (limited to 'src') diff --git a/src/jogl/classes/com/jogamp/opengl/util/GLPixelStorageModes.java b/src/jogl/classes/com/jogamp/opengl/util/GLPixelStorageModes.java index 05eb67269..fab80b109 100644 --- a/src/jogl/classes/com/jogamp/opengl/util/GLPixelStorageModes.java +++ b/src/jogl/classes/com/jogamp/opengl/util/GLPixelStorageModes.java @@ -43,32 +43,56 @@ public class GLPixelStorageModes { private int[] savedAlignment = new int[2]; private boolean saved = false; + /** Create instance w/o {@link #save(GL)} */ + public GLPixelStorageModes() {} + + /** Create instance w/ {@link #save(GL)} */ + public GLPixelStorageModes(GL gl) { save(gl); } + /** - * Sets the {@link GL2ES2.GL_PACK_ALIGNMENT}. Saves the pixel storage modes if not saved yet. + * 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); } + 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. + * 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); } + 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); } - private final void save(GL gl) { + /** + * Save the pixel storage mode, if not saved yet. + *

+ * Restore via {@link #restore(GL)} + *

+ */ + public final void save(GL gl) { + if(saved) { + return; + } + if(gl.isGL2GL3()) { if(gl.isGL2()) { gl.getGL2().glPushClientAttrib(GL2.GL_CLIENT_PIXEL_STORE_BIT); diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/glu/TestBug694AWT.java b/src/test/com/jogamp/opengl/test/junit/jogl/glu/TestBug694AWT.java deleted file mode 100644 index e6eabd3d8..000000000 --- a/src/test/com/jogamp/opengl/test/junit/jogl/glu/TestBug694AWT.java +++ /dev/null @@ -1,128 +0,0 @@ -/** - * Copyright 2013 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.glu; - -import java.awt.Frame; -import java.nio.ByteBuffer; - -import javax.media.opengl.GL; -import javax.media.opengl.GLAutoDrawable; -import javax.media.opengl.GLCapabilities; -import javax.media.opengl.GLEventListener; -import javax.media.opengl.GLProfile; -import javax.media.opengl.awt.GLCanvas; -import javax.media.opengl.glu.GLU; - -import org.junit.Assume; -import org.junit.Test; - -import com.jogamp.common.nio.Buffers; -import com.jogamp.opengl.test.junit.util.UITestCase; - -/** - * - * @author Julien Gouesse - */ -public class TestBug694AWT extends UITestCase implements GLEventListener { - - /* @Override */ - public void init(GLAutoDrawable drawable) { - } - - /* @Override */ - public void display(GLAutoDrawable drawable) { - int widthin = 213; - int heightin = 213; - - int widthout = 256; - int heightout = 256; - - int textureInLength = 45369; - int textureOutLength = 66560; - - ByteBuffer bufferIn = Buffers.newDirectByteBuffer(textureInLength); - ByteBuffer bufferOut = Buffers.newDirectByteBuffer(textureOutLength); - GLU glu = GLU.createGLU(drawable.getGL()); - glu.gluScaleImage( GL.GL_LUMINANCE, - widthin, heightin, GL.GL_UNSIGNED_BYTE, bufferIn, - widthout, heightout, GL.GL_UNSIGNED_BYTE, bufferOut ); - } - - /* @Override */ - public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { - } - - - /* @Override */ - public void dispose(GLAutoDrawable drawable) { - } - - @Test - public void test01() throws InterruptedException { - GLProfile glprofile = GLProfile.getDefault(); - GLCapabilities glCapabilities = new GLCapabilities(glprofile); - final GLCanvas canvas = new GLCanvas(glCapabilities); - canvas.addGLEventListener( this ); - - final Frame frame = new Frame("Test"); - frame.add(canvas); - frame.setSize(256, 256); - frame.validate(); - - try { - javax.swing.SwingUtilities.invokeAndWait(new Runnable() { - public void run() { - frame.setVisible(true); - }}); - } catch (Throwable t) { - t.printStackTrace(); - Assume.assumeNoException(t); - } - - canvas.display(); - - Thread.sleep(200); - - try { - javax.swing.SwingUtilities.invokeAndWait(new Runnable() { - public void run() { - frame.setVisible(false); - frame.remove(canvas); - frame.dispose(); - }}); - } catch (Throwable t) { - t.printStackTrace(); - Assume.assumeNoException(t); - } - } - - public static void main(String args[]) { - org.junit.runner.JUnitCore.main(TestBug694AWT.class.getName()); - } -} diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/glu/TestBug694ScaleImageUnpackBufferSizeAWT.java b/src/test/com/jogamp/opengl/test/junit/jogl/glu/TestBug694ScaleImageUnpackBufferSizeAWT.java new file mode 100644 index 000000000..d3bcce116 --- /dev/null +++ b/src/test/com/jogamp/opengl/test/junit/jogl/glu/TestBug694ScaleImageUnpackBufferSizeAWT.java @@ -0,0 +1,160 @@ +/** + * Copyright 2013 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.glu; + +import java.awt.Frame; +import java.nio.ByteBuffer; + +import javax.media.opengl.GL; +import javax.media.opengl.GLAutoDrawable; +import javax.media.opengl.GLCapabilities; +import javax.media.opengl.GLEventListener; +import javax.media.opengl.GLProfile; +import javax.media.opengl.awt.GLCanvas; +import javax.media.opengl.glu.GLU; + +import org.junit.Assume; +import org.junit.Test; + +import com.jogamp.common.nio.Buffers; +import com.jogamp.opengl.test.junit.util.UITestCase; +import com.jogamp.opengl.util.GLBuffers; +import com.jogamp.opengl.util.GLPixelStorageModes; + +/** + * Demonstrates how to use {@link GLBuffers#sizeof(GL, int[], int, int, int, int, int, boolean)} + * to determine the unpack buffer size for {@link GLU#gluScaleImage(int, int, int, int, java.nio.Buffer, int, int, int, java.nio.Buffer)}. + */ +public class TestBug694ScaleImageUnpackBufferSizeAWT extends UITestCase implements GLEventListener { + + /* @Override */ + public void init(GLAutoDrawable drawable) { + } + + /* @Override */ + public void display(GLAutoDrawable drawable) { + if( !testDone ) { + testDone = true; + + final GL gl = drawable.getGL(); + GLU glu = GLU.createGLU(gl); + testGLUScaleImage(gl, glu, 0); // default 4 + testGLUScaleImage(gl, glu, 1); + testGLUScaleImage(gl, glu, 4); + testGLUScaleImage(gl, glu, 8); + glu.destroy(); + } + } + + boolean testDone = false; + + private void testGLUScaleImage(GL gl, GLU glu, int unpackAlignment) { + final GLPixelStorageModes psm = new GLPixelStorageModes(gl); + if(0 < unpackAlignment) { + psm.setUnpackAlignment(gl, unpackAlignment); + } + + final int widthin = 213; + final int heightin = 213; + + final int widthout = 256; + final int heightout = 256; + + final int glFormat = GL.GL_LUMINANCE; + final int glType = GL.GL_UNSIGNED_BYTE; + final int tmp[] = new int[1]; + + final int unpackSizeInLen = GLBuffers.sizeof(gl, tmp, glFormat, glType, widthin, heightin, 1, false); + final int unpackSizeOutLen = GLBuffers.sizeof(gl, tmp, glFormat, glType, widthout, heightout, 1, false); + + System.err.println("Unpack-Alignment "+unpackAlignment+": in-size "+unpackSizeInLen); + System.err.println("Unpack-Alignment "+unpackAlignment+": out-size "+unpackSizeOutLen); + + ByteBuffer bufferIn = Buffers.newDirectByteBuffer(unpackSizeInLen); + ByteBuffer bufferOut = Buffers.newDirectByteBuffer(unpackSizeOutLen); + + glu.gluScaleImage( GL.GL_LUMINANCE, + widthin, heightin, GL.GL_UNSIGNED_BYTE, bufferIn, + widthout, heightout, GL.GL_UNSIGNED_BYTE, bufferOut ); + + psm.restore(gl); + } + + /* @Override */ + public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { + } + + + /* @Override */ + public void dispose(GLAutoDrawable drawable) { + } + + @Test + public void test01() throws InterruptedException { + GLProfile glprofile = GLProfile.getDefault(); + GLCapabilities glCapabilities = new GLCapabilities(glprofile); + final GLCanvas canvas = new GLCanvas(glCapabilities); + canvas.addGLEventListener( this ); + + final Frame frame = new Frame("Test"); + frame.add(canvas); + frame.setSize(256, 256); + frame.validate(); + + try { + javax.swing.SwingUtilities.invokeAndWait(new Runnable() { + public void run() { + frame.setVisible(true); + }}); + } catch (Throwable t) { + t.printStackTrace(); + Assume.assumeNoException(t); + } + + canvas.display(); + + Thread.sleep(200); + + try { + javax.swing.SwingUtilities.invokeAndWait(new Runnable() { + public void run() { + frame.setVisible(false); + frame.remove(canvas); + frame.dispose(); + }}); + } catch (Throwable t) { + t.printStackTrace(); + Assume.assumeNoException(t); + } + } + + public static void main(String args[]) { + org.junit.runner.JUnitCore.main(TestBug694ScaleImageUnpackBufferSizeAWT.class.getName()); + } +} -- cgit v1.2.3