JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestBug694ScaleImageUnpackBufferSizeAWT.java
Go to the documentation of this file.
1/**
2 * Copyright 2013 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28
29package com.jogamp.opengl.test.junit.jogl.glu;
30
31import java.awt.Frame;
32import java.nio.ByteBuffer;
33
34import com.jogamp.opengl.GL;
35import com.jogamp.opengl.GLAutoDrawable;
36import com.jogamp.opengl.GLCapabilities;
37import com.jogamp.opengl.GLEventListener;
38import com.jogamp.opengl.GLProfile;
39import com.jogamp.opengl.awt.GLCanvas;
40import com.jogamp.opengl.glu.GLU;
41
42import org.junit.Assume;
43import org.junit.Test;
44import org.junit.FixMethodOrder;
45import org.junit.runners.MethodSorters;
46
47import com.jogamp.common.nio.Buffers;
48import com.jogamp.opengl.test.junit.util.UITestCase;
49import com.jogamp.opengl.util.GLBuffers;
50import com.jogamp.opengl.util.GLPixelStorageModes;
51
52/**
53 * Demonstrates how to use {@link GLBuffers#sizeof(GL, int[], int, int, int, int, int, boolean)}
54 * to determine the unpack buffer size for {@link GLU#gluScaleImage(int, int, int, int, java.nio.Buffer, int, int, int, java.nio.Buffer)}.
55 */
56@FixMethodOrder(MethodSorters.NAME_ASCENDING)
58
59 /* @Override */
60 public void init(final GLAutoDrawable drawable) {
61 }
62
63 /* @Override */
64 public void display(final GLAutoDrawable drawable) {
65 if( !testDone ) {
66 testDone = true;
67
68 final GL gl = drawable.getGL();
69 final GLU glu = GLU.createGLU(gl);
70 System.err.println("Using GLU "+glu.getClass().getName());
71 testGLUScaleImage(gl, glu, 0); // default 4
72 testGLUScaleImage(gl, glu, 1);
73 testGLUScaleImage(gl, glu, 4);
74 testGLUScaleImage(gl, glu, 8);
75 }
76 }
77
78 boolean testDone = false;
79
80 private void testGLUScaleImage(final GL gl, final GLU glu, final int unpackAlignment) {
82 if(0 < unpackAlignment) {
83 psm.setUnpackAlignment(gl, unpackAlignment);
84 }
85
86 final int widthin = 213;
87 final int heightin = 213;
88
89 final int widthout = 256;
90 final int heightout = 256;
91
92 final int glFormat = GL.GL_LUMINANCE;
93 final int glType = GL.GL_UNSIGNED_BYTE;
94 final int tmp[] = new int[1];
95
96 final int unpackSizeInLen = GLBuffers.sizeof(gl, tmp, glFormat, glType, widthin, heightin, 1, false);
97 final int unpackSizeOutLen = GLBuffers.sizeof(gl, tmp, glFormat, glType, widthout, heightout, 1, false);
98
99 System.err.println("Unpack-Alignment "+unpackAlignment+": in-size "+unpackSizeInLen);
100 System.err.println("Unpack-Alignment "+unpackAlignment+": out-size "+unpackSizeOutLen);
101
102 final ByteBuffer bufferIn = Buffers.newDirectByteBuffer(unpackSizeInLen);
103 final ByteBuffer bufferOut = Buffers.newDirectByteBuffer(unpackSizeOutLen);
104
106 widthin, heightin, GL.GL_UNSIGNED_BYTE, bufferIn,
107 widthout, heightout, GL.GL_UNSIGNED_BYTE, bufferOut );
108
109 if(0 < unpackAlignment) {
110 psm.restore(gl);
111 }
112 }
113
114 /* @Override */
115 public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) {
116 }
117
118
119 /* @Override */
120 public void dispose(final GLAutoDrawable drawable) {
121 }
122
123 @Test
124 public void test01() throws InterruptedException {
125 final GLProfile glprofile = GLProfile.getDefault();
126 final GLCapabilities glCapabilities = new GLCapabilities(glprofile);
127 final GLCanvas canvas = new GLCanvas(glCapabilities);
128 canvas.addGLEventListener( this );
129
130 final Frame frame = new Frame("Test");
131 frame.add(canvas);
132 frame.setSize(256, 256);
133 frame.validate();
134
135 try {
136 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
137 public void run() {
138 frame.setVisible(true);
139 }});
140 } catch (final Throwable t) {
141 t.printStackTrace();
142 Assume.assumeNoException(t);
143 }
144
145 canvas.display();
146
147 Thread.sleep(200);
148
149 try {
150 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
151 public void run() {
152 frame.setVisible(false);
153 frame.remove(canvas);
154 frame.dispose();
155 }});
156 } catch (final Throwable t) {
157 t.printStackTrace();
158 Assume.assumeNoException(t);
159 }
160 }
161
162 public static void main(final String args[]) {
163 org.junit.runner.JUnitCore.main(TestBug694ScaleImageUnpackBufferSizeAWT.class.getName());
164 }
165}
Specifies a set of OpenGL capabilities.
Specifies the the OpenGL profile.
Definition: GLProfile.java:77
static GLProfile getDefault(final AbstractGraphicsDevice device)
Returns a default GLProfile object, reflecting the best for the running platform.
Definition: GLProfile.java:739
A heavyweight AWT component which provides OpenGL rendering support.
Definition: GLCanvas.java:170
void addGLEventListener(final GLEventListener listener)
Adds the given listener to the end of this drawable queue.
Definition: GLCanvas.java:1065
Provides access to the OpenGL Utility Library (GLU).
Definition: GLU.java:43
int gluScaleImage(int format, int widthin, int heightin, int typein, java.nio.Buffer datain, int widthout, int heightout, int typeout, java.nio.Buffer dataout)
Optional, throws GLException if not available in profile.
Definition: GLU.java:1491
static final GLU createGLU()
Instantiates a GLU implementation object in respect to the given GL profile of this thread current GL...
Definition: GLU.java:147
Demonstrates how to use GLBuffers#sizeof(GL, int[], int, int, int, int, int, boolean) to determine th...
void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height)
Called by the drawable during the first repaint after the component has been resized.
void display(final GLAutoDrawable drawable)
Called by the drawable to initiate OpenGL rendering by the client.
void init(final GLAutoDrawable drawable)
Called by the drawable immediately after the OpenGL context is initialized.
void dispose(final GLAutoDrawable drawable)
Notifies the listener to perform the release of all OpenGL resources per GLContext,...
Utility routines for dealing with direct buffers.
Definition: GLBuffers.java:60
static final int sizeof(final GL gl, final int tmp[], final int bytesPerPixel, int width, int height, int depth, final boolean pack)
Returns the number of bytes required to read/write a memory buffer via OpenGL using the current GL pi...
Definition: GLBuffers.java:364
Utility to safely set and restore the PACK and UNPACK pixel storage mode, regardless of the GLProfile...
final void setUnpackAlignment(final GL gl, final int unpackAlignment)
Sets the GL#GL_UNPACK_ALIGNMENT.
final void restore(final GL gl)
Restores PACK and UNPACK pixel storage mode previously saved w/ saveAll(GL) or savePack(GL) and saveU...
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
GL getGL()
Returns the GL pipeline object this GLAutoDrawable uses.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.
static final int GL_LUMINANCE
GL_ES_VERSION_2_0, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_LUMINANCE" with expression '0x1909',...
Definition: GL.java:216
static final int GL_UNSIGNED_BYTE
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_UNSIGNED_BYTE" with e...
Definition: GL.java:284