summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2014-02-13 01:04:47 +0100
committerSven Gothel <[email protected]>2014-02-13 01:04:47 +0100
commit1d2515da352a79c239efecdfa3e0fade47779781 (patch)
tree74e9d8fccd407d0d8557dd4362d6cec2402f740a /src
parent21b68a5e73a672da34b37b5641b779aa1e3fa41d (diff)
Bug 962 - AWTGLReadBufferUtil should use aligned BufferedImage [for resized images]; Fix GLReadBufferUtil GL_PACK_ROW_LENGTH
AWTGLPixelBuffer is being reused when used via AWTGLPixelBufferProvider even when resized. AWTGLPixelBufferProvider uses GLPixelBufferProvider's requiresNewBuffer(..) which returns true if - allowRowStride==true and pixel-buffer size < required-size, or - allowRowStride==false and pixel-buffer size < required _or_ width doesn't match otherwise it returns true, i.e. the AWTGLPixelBuffer is reused. Hence the used BufferedImage might need to be aligned, i.e. using AWTGLPixelBuffer's getAlignedImage(..). +++ GLReadBufferUtil shall use current texture-data width for GL_PACK_ROW_LENGTH, not the static GLPixelBuffer's width, which may not reflect image dimension (resize) +++
Diffstat (limited to 'src')
-rw-r--r--src/jogl/classes/com/jogamp/opengl/util/GLPixelBuffer.java14
-rw-r--r--src/jogl/classes/com/jogamp/opengl/util/GLReadBufferUtil.java30
-rw-r--r--src/jogl/classes/com/jogamp/opengl/util/awt/AWTGLReadBufferUtil.java46
-rw-r--r--src/test/com/jogamp/opengl/test/junit/jogl/awt/TestGLJPanelResize02AWT.java204
4 files changed, 278 insertions, 16 deletions
diff --git a/src/jogl/classes/com/jogamp/opengl/util/GLPixelBuffer.java b/src/jogl/classes/com/jogamp/opengl/util/GLPixelBuffer.java
index a921a2818..223c23ebb 100644
--- a/src/jogl/classes/com/jogamp/opengl/util/GLPixelBuffer.java
+++ b/src/jogl/classes/com/jogamp/opengl/util/GLPixelBuffer.java
@@ -324,9 +324,19 @@ public class GLPixelBuffer {
/** The {@link GLPixelAttributes}. */
public final GLPixelAttributes pixelAttributes;
- /** Width in pixels. */
+ /**
+ * Width in pixels, representing {@link #buffer}'s {@link #byteSize}.
+ * <p>
+ * May not represent actual image width as user may re-use buffer for different dimensions, see {@link #requiresNewBuffer(GL, int, int, int)}.
+ * </p>
+ */
public final int width;
- /** Height in pixels. */
+ /**
+ * Height in pixels, representing {@link #buffer}'s {@link #byteSize}.
+ * <p>
+ * May not represent actual image height as user may re-use buffer for different dimensions, see {@link #requiresNewBuffer(GL, int, int, int)}.
+ * </p>
+ */
public final int height;
/** Depth in pixels. */
public final int depth;
diff --git a/src/jogl/classes/com/jogamp/opengl/util/GLReadBufferUtil.java b/src/jogl/classes/com/jogamp/opengl/util/GLReadBufferUtil.java
index b942c9ab2..b7d2980df 100644
--- a/src/jogl/classes/com/jogamp/opengl/util/GLReadBufferUtil.java
+++ b/src/jogl/classes/com/jogamp/opengl/util/GLReadBufferUtil.java
@@ -151,17 +151,6 @@ public class GLReadBufferUtil {
* @see #GLReadBufferUtil(boolean, boolean)
*/
public boolean readPixels(GL gl, int inX, int inY, int inWidth, int inHeight, boolean mustFlipVertically) {
- final int glerr0 = gl.glGetError();
- if(GL.GL_NO_ERROR != glerr0) {
- System.err.println("Info: GLReadBufferUtil.readPixels: pre-exisiting GL error 0x"+Integer.toHexString(glerr0));
- }
- final GLPixelAttributes pixelAttribs = pixelBufferProvider.getAttributes(gl, componentCount);
- final int internalFormat;
- if(gl.isGL2GL3() && 3 == componentCount) {
- internalFormat = GL.GL_RGB;
- } else {
- internalFormat = (4 == componentCount) ? GL.GL_RGBA : GL.GL_RGB;
- }
final GLDrawable drawable = gl.getContext().getGLReadDrawable();
final int width, height;
if( 0 >= inWidth || drawable.getWidth() < inWidth ) {
@@ -174,6 +163,23 @@ public class GLReadBufferUtil {
} else {
height= inHeight;
}
+ return readPixelsImpl(drawable, gl, inX, inY, width, height, mustFlipVertically);
+ }
+
+ protected boolean readPixelsImpl(final GLDrawable drawable, final GL gl,
+ final int inX, final int inY, final int width, final int height,
+ final boolean mustFlipVertically) {
+ final int glerr0 = gl.glGetError();
+ if(GL.GL_NO_ERROR != glerr0) {
+ System.err.println("Info: GLReadBufferUtil.readPixels: pre-exisiting GL error 0x"+Integer.toHexString(glerr0));
+ }
+ final GLPixelAttributes pixelAttribs = pixelBufferProvider.getAttributes(gl, componentCount);
+ final int internalFormat;
+ if(gl.isGL2GL3() && 3 == componentCount) {
+ internalFormat = GL.GL_RGB;
+ } else {
+ internalFormat = (4 == componentCount) ? GL.GL_RGBA : GL.GL_RGB;
+ }
final boolean flipVertically;
if( drawable.isGLOriented() ) {
@@ -216,7 +222,7 @@ public class GLReadBufferUtil {
if(res) {
psm.setAlignment(gl, alignment, alignment);
if(gl.isGL2GL3()) {
- gl.getGL2GL3().glPixelStorei(GL2GL3.GL_PACK_ROW_LENGTH, readPixelBuffer.width);
+ gl.getGL2GL3().glPixelStorei(GL2GL3.GL_PACK_ROW_LENGTH, width);
}
readPixelBuffer.clear();
try {
diff --git a/src/jogl/classes/com/jogamp/opengl/util/awt/AWTGLReadBufferUtil.java b/src/jogl/classes/com/jogamp/opengl/util/awt/AWTGLReadBufferUtil.java
index f5d31a132..9490e041b 100644
--- a/src/jogl/classes/com/jogamp/opengl/util/awt/AWTGLReadBufferUtil.java
+++ b/src/jogl/classes/com/jogamp/opengl/util/awt/AWTGLReadBufferUtil.java
@@ -30,6 +30,7 @@ package com.jogamp.opengl.util.awt;
import java.awt.image.BufferedImage;
import javax.media.opengl.GL;
+import javax.media.opengl.GLDrawable;
import javax.media.opengl.GLProfile;
import com.jogamp.opengl.util.GLReadBufferUtil;
@@ -51,12 +52,53 @@ public class AWTGLReadBufferUtil extends GLReadBufferUtil {
public AWTGLPixelBuffer getAWTGLPixelBuffer() { return (AWTGLPixelBuffer)this.getPixelBuffer(); }
+ /**
+ * Read the drawable's pixels to TextureData and Texture, if requested at construction,
+ * and returns an aligned {@link BufferedImage}.
+ *
+ * @param gl the current GL context object. It's read drawable is being used as the pixel source.
+ * @param awtOrientation flips the data vertically if <code>true</code>.
+ * The context's drawable {@link GLDrawable#isGLOriented()} state
+ * is taken into account.
+ * Vertical flipping is propagated to TextureData
+ * and handled in a efficient manner there (TextureCoordinates and TextureIO writer).
+ * @see #AWTGLReadBufferUtil(GLProfile, boolean)
+ */
public BufferedImage readPixelsToBufferedImage(GL gl, boolean awtOrientation) {
return readPixelsToBufferedImage(gl, 0, 0, 0, 0, awtOrientation);
}
+
+ /**
+ * Read the drawable's pixels to TextureData and Texture, if requested at construction,
+ * and returns an aligned {@link BufferedImage}.
+ *
+ * @param gl the current GL context object. It's read drawable is being used as the pixel source.
+ * @param inX readPixel x offset
+ * @param inY readPixel y offset
+ * @param inWidth optional readPixel width value, used if [1 .. drawable.width], otherwise using drawable.width
+ * @param inHeight optional readPixel height, used if [1 .. drawable.height], otherwise using drawable.height
+ * @param awtOrientation flips the data vertically if <code>true</code>.
+ * The context's drawable {@link GLDrawable#isGLOriented()} state
+ * is taken into account.
+ * Vertical flipping is propagated to TextureData
+ * and handled in a efficient manner there (TextureCoordinates and TextureIO writer).
+ * @see #AWTGLReadBufferUtil(GLProfile, boolean)
+ */
public BufferedImage readPixelsToBufferedImage(GL gl, int inX, int inY, int inWidth, int inHeight, boolean awtOrientation) {
- if( readPixels(gl, inX, inY, inWidth, inHeight, awtOrientation) ) {
- final BufferedImage image = getAWTGLPixelBuffer().image;
+ final GLDrawable drawable = gl.getContext().getGLReadDrawable();
+ final int width, height;
+ if( 0 >= inWidth || drawable.getWidth() < inWidth ) {
+ width = drawable.getWidth();
+ } else {
+ width = inWidth;
+ }
+ if( 0 >= inHeight || drawable.getHeight() < inHeight ) {
+ height = drawable.getHeight();
+ } else {
+ height= inHeight;
+ }
+ if( readPixelsImpl(drawable, gl, inX, inY, width, height, awtOrientation) ) {
+ final BufferedImage image = getAWTGLPixelBuffer().getAlignedImage(width, height);
if( getTextureData().getMustFlipVertically() ) {
ImageUtil.flipImageVertically(image);
}
diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/awt/TestGLJPanelResize02AWT.java b/src/test/com/jogamp/opengl/test/junit/jogl/awt/TestGLJPanelResize02AWT.java
new file mode 100644
index 000000000..dabfb737a
--- /dev/null
+++ b/src/test/com/jogamp/opengl/test/junit/jogl/awt/TestGLJPanelResize02AWT.java
@@ -0,0 +1,204 @@
+/**
+ * Copyright 2014 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.awt;
+
+import java.awt.BorderLayout;
+import java.awt.Dimension;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+
+import javax.imageio.ImageIO;
+import javax.media.opengl.GL;
+import javax.media.opengl.GLAutoDrawable;
+import javax.media.opengl.GLCapabilities;
+import javax.media.opengl.GLCapabilitiesImmutable;
+import javax.media.opengl.GLDrawable;
+import javax.media.opengl.GLEventListener;
+import javax.media.opengl.GLProfile;
+import javax.media.opengl.awt.GLJPanel;
+import javax.swing.JFrame;
+import javax.swing.JPanel;
+import javax.swing.SwingUtilities;
+
+import org.junit.Assume;
+import org.junit.BeforeClass;
+import org.junit.FixMethodOrder;
+import org.junit.Test;
+import org.junit.runners.MethodSorters;
+
+import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
+import com.jogamp.opengl.test.junit.util.MiscUtils;
+import com.jogamp.opengl.test.junit.util.UITestCase;
+import com.jogamp.opengl.util.awt.AWTGLReadBufferUtil;
+import com.jogamp.opengl.util.texture.TextureIO;
+
+/**
+ * Multiple GLJPanels in a JFrame
+ */
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
+public class TestGLJPanelResize02AWT extends UITestCase {
+
+ @BeforeClass
+ public static void initClass() {
+ GLProfile.initSingleton();
+ }
+
+ public void test(final GLCapabilitiesImmutable caps, final boolean useSwingDoubleBuffer) {
+ final AWTGLReadBufferUtil awtGLReadBufferUtil = new AWTGLReadBufferUtil(caps.getGLProfile(), false);
+ final JFrame frame = new JFrame();
+ final Dimension d = new Dimension(320, 240);
+
+ try {
+ javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
+ public void run() {
+ frame.setLocation(64, 64);
+ final JPanel panel = new JPanel();
+ panel.setLayout(new BorderLayout());
+ panel.setDoubleBuffered(useSwingDoubleBuffer);
+ frame.getContentPane().add(panel);
+
+ final GLJPanel glad = createGLJPanel(useSwingDoubleBuffer, caps, d);
+ final GearsES2 gears = new GearsES2(0);
+ gears.setVerbose(false);
+ glad.addGLEventListener(gears);
+ glad.addGLEventListener(new SnapshotGLEL(awtGLReadBufferUtil));
+ panel.add(glad);
+ frame.pack();
+ frame.setVisible(true);
+ } } );
+ } catch( Throwable throwable ) {
+ throwable.printStackTrace();
+ Assume.assumeNoException( throwable );
+ }
+ final Dimension size0 = frame.getSize();
+ final Dimension size1 = new Dimension(size0.width+30, size0.height+30);
+ final Dimension size2 = new Dimension(size0.width-30, size0.height-30);
+ try {
+ try { Thread.sleep(duration); } catch (InterruptedException e) { }
+ javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
+ public void run() {
+ frame.setSize(size1);
+ frame.validate();
+ } } );
+ try { Thread.sleep(duration); } catch (InterruptedException e) { }
+ javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
+ public void run() {
+ frame.setSize(size2);
+ frame.validate();
+ } } );
+ try { Thread.sleep(duration); } catch (InterruptedException e) { }
+ javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
+ public void run() {
+ frame.setSize(size0);
+ frame.validate();
+ } } );
+ try { Thread.sleep(duration); } catch (InterruptedException e) { }
+ } catch (Exception e1) {
+ e1.printStackTrace();
+ }
+ try {
+ SwingUtilities.invokeAndWait(new Runnable() {
+ public void run() {
+ frame.dispose();
+ } } );
+ } catch (Exception e1) {
+ e1.printStackTrace();
+ }
+ }
+
+ private class SnapshotGLEL implements GLEventListener {
+ final AWTGLReadBufferUtil glReadBufferUtil;
+ int i;
+
+ SnapshotGLEL(final AWTGLReadBufferUtil glReadBufferUtil) {
+ this.glReadBufferUtil = glReadBufferUtil;
+ i = 0;
+ }
+
+ @Override
+ public void init(GLAutoDrawable drawable) { }
+ @Override
+ public void dispose(GLAutoDrawable drawable) { }
+ @Override
+ public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { }
+ @Override
+ public void display(GLAutoDrawable drawable) {
+ snapshot(i++, drawable.getGL(), TextureIO.PNG, null);
+ }
+ public void snapshot(int sn, GL gl, String fileSuffix, String destPath) {
+ final GLDrawable drawable = gl.getContext().getGLReadDrawable();
+ final String filenameAWT = getSnapshotFilename(sn, "awt",
+ drawable.getChosenGLCapabilities(), drawable.getWidth(), drawable.getHeight(),
+ glReadBufferUtil.hasAlpha(), fileSuffix, destPath);
+ final String filenameJGL = getSnapshotFilename(sn, "jgl",
+ drawable.getChosenGLCapabilities(), drawable.getWidth(), drawable.getHeight(),
+ glReadBufferUtil.hasAlpha(), fileSuffix, destPath);
+ System.err.println(Thread.currentThread().getName()+": ** screenshot: "+filenameAWT+", "+filenameJGL);
+ gl.glFinish(); // just make sure rendering finished ..
+ final BufferedImage image = glReadBufferUtil.readPixelsToBufferedImage(gl, true /* awtOrientation */);
+ final File fout = new File(filenameAWT);
+ try {
+ ImageIO.write(image, "png", fout);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ glReadBufferUtil.write(new File(filenameJGL));
+ }
+ };
+
+ private GLJPanel createGLJPanel(final boolean useSwingDoubleBuffer, final GLCapabilitiesImmutable caps, final Dimension size) {
+ final GLJPanel canvas = new GLJPanel(caps);
+ canvas.setSize(size);
+ canvas.setPreferredSize(size);
+ canvas.setMinimumSize(size);
+ canvas.setDoubleBuffered(useSwingDoubleBuffer);
+ return canvas;
+ }
+
+ static GLCapabilitiesImmutable caps = null;
+
+ @Test
+ public void test00() throws InterruptedException, InvocationTargetException {
+ test(new GLCapabilities(null), false /*useSwingDoubleBuffer*/);
+ }
+
+ static long duration = 500; // ms
+
+ public static void main(String[] args) {
+ for(int i=0; i<args.length; i++) {
+ if(args[i].equals("-time")) {
+ i++;
+ duration = MiscUtils.atol(args[i], duration);
+ }
+ }
+ org.junit.runner.JUnitCore.main(TestGLJPanelResize02AWT.class.getName());
+ }
+
+}