JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestPNGTextureFromFileAWT.java
Go to the documentation of this file.
1/**
2 * Copyright 2010 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.util.texture;
30
31
32import com.jogamp.common.util.IOUtil;
33import com.jogamp.opengl.test.junit.jogl.demos.TextureDraw01Accessor;
34import com.jogamp.opengl.test.junit.jogl.demos.es2.TextureDraw01ES2Listener;
35import com.jogamp.opengl.test.junit.jogl.demos.gl2.TextureDraw01GL2Listener;
36import com.jogamp.opengl.test.junit.util.MiscUtils;
37import com.jogamp.opengl.test.junit.util.QuitAdapter;
38import com.jogamp.opengl.test.junit.util.UITestCase;
39
40import com.jogamp.opengl.GLAutoDrawable;
41import com.jogamp.opengl.GLEventListener;
42import com.jogamp.opengl.GLProfile;
43import com.jogamp.opengl.GLCapabilities;
44import com.jogamp.opengl.awt.GLCanvas;
45
46import com.jogamp.opengl.util.texture.TextureData;
47import com.jogamp.opengl.util.texture.TextureIO;
48import com.jogamp.opengl.util.texture.spi.TextureProvider;
49import com.jogamp.opengl.util.Animator;
50import com.jogamp.opengl.util.GLReadBufferUtil;
51
52import java.awt.Dimension;
53import java.awt.Frame;
54import java.io.IOException;
55import java.io.InputStream;
56import java.net.URLConnection;
57
58import org.junit.Assert;
59import org.junit.After;
60import org.junit.Assume;
61import org.junit.Before;
62import org.junit.BeforeClass;
63import org.junit.Test;
64import org.junit.FixMethodOrder;
65import org.junit.runners.MethodSorters;
66
67/**
68 * Unit test for bug 417, which shows a GLException when reading a grayscale texture.
69 * Couldn't duplicate the failure, so it must have been fixed unknowingly sometime
70 * after the bug was submitted.
71 * @author Wade Walker, et.al.
72 */
73@FixMethodOrder(MethodSorters.NAME_ASCENDING)
75 static boolean showFPS = false;
76 static long duration = 100; // ms
77 InputStream grayTextureStream;
78 InputStream testTextureStream;
79
80 @BeforeClass
81 public static void initClass() {
82 }
83
84 @Before
85 public void initTest() throws IOException {
86 grayTextureStream = TestPNGTextureFromFileAWT.class.getResourceAsStream( "grayscale_texture.png" );
87 Assert.assertNotNull(grayTextureStream);
88 {
89 final URLConnection testTextureUrlConn = IOUtil.getResource("test-ntscN_3-01-160x90.png", this.getClass().getClassLoader(), this.getClass());
90 Assert.assertNotNull(testTextureUrlConn);
91 testTextureStream = testTextureUrlConn.getInputStream();
92 Assert.assertNotNull(testTextureStream);
93 }
94 }
95
96 @After
97 public void cleanupTest() {
98 grayTextureStream = null;
99 testTextureStream = null;
100 }
101
102 public void testImpl(final boolean useFFP, final InputStream istream, final boolean useAWTIIOP)
103 throws InterruptedException, IOException
104 {
105 final GLReadBufferUtil screenshot = new GLReadBufferUtil(true, false);
106 GLProfile glp;
107 if(useFFP && GLProfile.isAvailable(GLProfile.GL2)) {
108 glp = GLProfile.getMaxFixedFunc(true);
109 } else if(!useFFP && GLProfile.isAvailable(GLProfile.GL2ES2)) {
110 glp = GLProfile.getGL2ES2();
111 } else {
112 System.err.println(getSimpleTestName(".")+": GLProfile n/a, useFFP: "+useFFP);
113 return;
114 }
115 final GLCapabilities caps = new GLCapabilities(glp);
116 final TextureData texData;
117 if(useAWTIIOP) {
118 final TextureProvider texProvider = new com.jogamp.opengl.util.texture.spi.awt.IIOTextureProvider();
119 texData = texProvider.newTextureData(glp, istream, 0 /* internalFormat */, 0 /* pixelFormat */, false /* mipmap */, TextureIO.PNG);
120 } else {
121 texData = TextureIO.newTextureData(glp, istream, false /* mipmap */, TextureIO.PNG);
122 }
123 System.err.println("TextureData: "+texData);
124
125 final GLCanvas glc = new GLCanvas(caps);
126 final Dimension glc_sz = new Dimension(texData.getWidth(), texData.getHeight());
127 glc.setMinimumSize(glc_sz);
128 glc.setPreferredSize(glc_sz);
129 final Frame frame = new Frame("TestPNGTextureGL2FromFileAWT");
130 Assert.assertNotNull(frame);
131 frame.add(glc);
132
133 // load texture from file inside current GL context to match the way
134 // the bug submitter was doing it
135 final GLEventListener gle = useFFP ? new TextureDraw01GL2Listener( texData ) : new TextureDraw01ES2Listener( texData, 0 ) ;
136 glc.addGLEventListener(gle);
138 boolean shot = false;
139
140 @Override public void init(final GLAutoDrawable drawable) {}
141
142 @Override
143 public void display(final GLAutoDrawable drawable) {
144 // 1 snapshot
145 if(null!=((TextureDraw01Accessor)gle).getTexture() && !shot) {
146 shot = true;
147 snapshot(0, null, drawable.getGL(), screenshot, TextureIO.PNG, null);
148 }
149 }
150
151 @Override public void dispose(final GLAutoDrawable drawable) { }
152 @Override public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) { }
153 });
154
155 final QuitAdapter quitAdapter = new QuitAdapter();
156 new com.jogamp.newt.event.awt.AWTKeyAdapter(quitAdapter, glc).addTo(glc);
157 new com.jogamp.newt.event.awt.AWTWindowAdapter(quitAdapter, glc).addTo(glc);
158
159 try {
160 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
161 public void run() {
162 frame.pack();
163 frame.setVisible(true);
164 }});
165 } catch( final Throwable throwable ) {
166 throwable.printStackTrace();
167 Assume.assumeNoException( throwable );
168 }
169
170 final Animator animator = new Animator(glc);
171 animator.setUpdateFPSFrames(60, showFPS ? System.err : null);
172 animator.start();
173
174 while(!quitAdapter.shouldQuit() && animator.isAnimating() && animator.getTotalFPSDuration()<duration) {
175 Thread.sleep(100);
176 }
177
178 animator.stop();
179 try {
180 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
181 public void run() {
182 frame.setVisible(false);
183 frame.remove(glc);
184 frame.dispose();
185 }});
186 } catch( final Throwable throwable ) {
187 throwable.printStackTrace();
188 Assume.assumeNoException( throwable );
189 }
190 }
191
192 @Test
193 public void testGrayAWTILoaderGL2() throws InterruptedException, IOException {
194 testImpl(true, grayTextureStream, true);
195 }
196 @Test
197 public void testGrayAWTILoaderES2() throws InterruptedException, IOException {
198 testImpl(false, grayTextureStream, true);
199 }
200
201 @Test
202 public void testGrayPNGJLoaderGL2() throws InterruptedException, IOException {
203 testImpl(true, grayTextureStream, false);
204 }
205 @Test
206 public void testGrayPNGJLoaderES2() throws InterruptedException, IOException {
207 testImpl(false, grayTextureStream, false);
208 }
209
210 @Test
211 public void testTestAWTILoaderGL2() throws InterruptedException, IOException {
212 testImpl(true, testTextureStream, true);
213 }
214 @Test
215 public void testTestAWTILoaderES2() throws InterruptedException, IOException {
216 testImpl(false, testTextureStream, true);
217 }
218
219 @Test
220 public void testTestPNGJLoaderGL2() throws InterruptedException, IOException {
221 testImpl(true, testTextureStream, false);
222 }
223 @Test
224 public void testTestPNGJLoaderES2() throws InterruptedException, IOException {
225 testImpl(false, testTextureStream, false);
226 }
227
228 public static void main(final String args[]) throws IOException {
229 for(int i=0; i<args.length; i++) {
230 if(args[i].equals("-time")) {
231 i++;
232 duration = MiscUtils.atol(args[i], duration);
233 }
234 }
235 org.junit.runner.JUnitCore.main(TestPNGTextureFromFileAWT.class.getName());
236 }
237}
Specifies a set of OpenGL capabilities.
Specifies the the OpenGL profile.
Definition: GLProfile.java:77
static boolean isAvailable(final AbstractGraphicsDevice device, final String profile)
Returns the availability of a profile on a device.
Definition: GLProfile.java:305
static GLProfile getMaxFixedFunc(final AbstractGraphicsDevice device, final boolean favorHardwareRasterizer)
Returns the highest profile, implementing the fixed function pipeline.
Definition: GLProfile.java:808
static final String GL2ES2
The intersection of the desktop GL3, GL2 and embedded ES2 profile.
Definition: GLProfile.java:594
static final String GL2
The desktop OpenGL profile 1.x up to 3.0.
Definition: GLProfile.java:579
static GLProfile getGL2ES2(final AbstractGraphicsDevice device)
Returns the GL2ES2 profile implementation, hence compatible w/ GL2ES2.
Definition: GLProfile.java:913
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
Unit test for bug 417, which shows a GLException when reading a grayscale texture.
void testImpl(final boolean useFFP, final InputStream istream, final boolean useAWTIIOP)
static long atol(final String str, final long def)
Definition: MiscUtils.java:66
final void setUpdateFPSFrames(final int frames, final PrintStream out)
final synchronized boolean start()
Starts this animator, if not running.
Definition: Animator.java:344
final synchronized boolean stop()
Stops this animator.
Definition: Animator.java:368
Utility to read out the current FB to TextureData, optionally writing the data back to a texture obje...
Represents the data for an OpenGL texture.
int getHeight()
Returns the height in pixels of the texture data.
int getWidth()
Returns the width in pixels of the texture data.
static final String PNG
Constant which can be used as a file suffix to indicate a PNG file, value {@value}.
Definition: TextureIO.java:171
static TextureData newTextureData(final GLProfile glp, final File file, final boolean mipmap, String fileSuffix)
Creates a TextureData from the given file.
Definition: TextureIO.java:233
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.
Plug-in interface to TextureIO to support reading OpenGL textures from new file formats.
TextureData newTextureData(GLProfile glp, InputStream stream, int internalFormat, int pixelFormat, boolean mipmap, String fileSuffix)
Produces a TextureData object from a stream, or returns null if the file format was not supported by ...