JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestPNGTextureFromFileNEWT.java
Go to the documentation of this file.
1/**
2 * Copyright 2012 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.newt.opengl.GLWindow;
34import com.jogamp.opengl.test.junit.jogl.demos.TextureDraw01Accessor;
35import com.jogamp.opengl.test.junit.jogl.demos.es2.TextureDraw01ES2Listener;
36import com.jogamp.opengl.test.junit.jogl.demos.gl2.TextureDraw01GL2Listener;
37import com.jogamp.opengl.test.junit.util.MiscUtils;
38import com.jogamp.opengl.test.junit.util.QuitAdapter;
39import com.jogamp.opengl.test.junit.util.UITestCase;
40
41import com.jogamp.opengl.GLAutoDrawable;
42import com.jogamp.opengl.GLEventListener;
43import com.jogamp.opengl.GLProfile;
44import com.jogamp.opengl.GLCapabilities;
45
46import com.jogamp.opengl.util.texture.TextureData;
47import com.jogamp.opengl.util.texture.TextureIO;
48import com.jogamp.opengl.util.Animator;
49import com.jogamp.opengl.util.GLReadBufferUtil;
50
51import java.io.IOException;
52import java.io.InputStream;
53import java.net.URLConnection;
54
55import org.junit.Assert;
56import org.junit.After;
57import org.junit.Before;
58import org.junit.Test;
59import org.junit.FixMethodOrder;
60import org.junit.runners.MethodSorters;
61
62@FixMethodOrder(MethodSorters.NAME_ASCENDING)
64 static boolean showFPS = false;
65 static long duration = 100; // ms
66 InputStream grayTextureStream;
67
68 InputStream testTextureStreamN_3;
69 InputStream testTextureStreamN_4;
70 InputStream testTextureStreamNG4;
71
72 InputStream testTextureStreamI_3;
73 InputStream testTextureStreamIG3;
74 InputStream testTextureStreamI_4;
75 InputStream testTextureStreamIG4;
76
77 InputStream testTextureStreamP_3;
78 InputStream testTextureStreamP_4;
79
80 @Before
81 public void initTest() throws IOException {
82 grayTextureStream = TestPNGTextureFromFileNEWT.class.getResourceAsStream( "grayscale_texture.png" );
83 Assert.assertNotNull(grayTextureStream);
84 {
85 final URLConnection testTextureUrlConn = IOUtil.getResource("test-ntscN_3-01-160x90.png", this.getClass().getClassLoader(), this.getClass());
86 Assert.assertNotNull(testTextureUrlConn);
87 testTextureStreamN_3 = testTextureUrlConn.getInputStream();
88 Assert.assertNotNull(testTextureStreamN_3);
89 }
90 {
91 final URLConnection testTextureUrlConn = IOUtil.getResource("test-ntscN_4-01-160x90.png", this.getClass().getClassLoader(), this.getClass());
92 Assert.assertNotNull(testTextureUrlConn);
93 testTextureStreamN_4 = testTextureUrlConn.getInputStream();
94 Assert.assertNotNull(testTextureStreamN_4);
95 }
96 {
97 final URLConnection testTextureUrlConn = IOUtil.getResource("test-ntscNG4-01-160x90.png", this.getClass().getClassLoader(), this.getClass());
98 Assert.assertNotNull(testTextureUrlConn);
99 testTextureStreamNG4 = testTextureUrlConn.getInputStream();
100 Assert.assertNotNull(testTextureStreamNG4);
101 }
102
103 {
104 final URLConnection testTextureUrlConn = IOUtil.getResource("test-ntscI_3-01-160x90.png", this.getClass().getClassLoader(), this.getClass());
105 Assert.assertNotNull(testTextureUrlConn);
106 testTextureStreamI_3 = testTextureUrlConn.getInputStream();
107 Assert.assertNotNull(testTextureStreamI_3);
108 }
109 {
110 final URLConnection testTextureUrlConn = IOUtil.getResource("test-ntscIG3-01-160x90.png", this.getClass().getClassLoader(), this.getClass());
111 Assert.assertNotNull(testTextureUrlConn);
112 testTextureStreamIG3 = testTextureUrlConn.getInputStream();
113 Assert.assertNotNull(testTextureStreamIG3);
114 }
115 {
116 final URLConnection testTextureUrlConn = IOUtil.getResource("test-ntscI_4-01-160x90.png", this.getClass().getClassLoader(), this.getClass());
117 Assert.assertNotNull(testTextureUrlConn);
118 testTextureStreamI_4 = testTextureUrlConn.getInputStream();
119 Assert.assertNotNull(testTextureStreamI_4);
120 }
121 {
122 final URLConnection testTextureUrlConn = IOUtil.getResource("test-ntscIG4-01-160x90.png", this.getClass().getClassLoader(), this.getClass());
123 Assert.assertNotNull(testTextureUrlConn);
124 testTextureStreamIG4 = testTextureUrlConn.getInputStream();
125 Assert.assertNotNull(testTextureStreamIG4);
126 }
127
128
129 {
130 final URLConnection testTextureUrlConn = IOUtil.getResource("test-ntscP_3-01-160x90.png", this.getClass().getClassLoader(), this.getClass());
131 Assert.assertNotNull(testTextureUrlConn);
132 testTextureStreamP_3 = testTextureUrlConn.getInputStream();
133 Assert.assertNotNull(testTextureStreamP_3);
134 }
135 {
136 final URLConnection testTextureUrlConn = IOUtil.getResource("test-ntscP_4-01-160x90.png", this.getClass().getClassLoader(), this.getClass());
137 Assert.assertNotNull(testTextureUrlConn);
138 testTextureStreamP_4 = testTextureUrlConn.getInputStream();
139 Assert.assertNotNull(testTextureStreamP_4);
140 }
141 }
142
143 @After
144 public void cleanupTest() {
145 grayTextureStream = null;
146 testTextureStreamN_3 = null;
147 testTextureStreamI_3 = null;
148 testTextureStreamIG3 = null;
149 testTextureStreamP_3 = null;
150 testTextureStreamP_4 = null;
151 }
152
153 public void testImpl(final boolean useFFP, final InputStream istream) throws InterruptedException, IOException {
154 final GLReadBufferUtil screenshot = new GLReadBufferUtil(true, false);
155 GLProfile glp;
156 if(useFFP && GLProfile.isAvailable(GLProfile.GL2)) {
157 glp = GLProfile.getMaxFixedFunc(true);
158 } else if(!useFFP && GLProfile.isAvailable(GLProfile.GL2ES2)) {
159 glp = GLProfile.getGL2ES2();
160 } else {
161 System.err.println(getSimpleTestName(".")+": GLProfile n/a, useFFP: "+useFFP);
162 return;
163 }
164 final GLCapabilities caps = new GLCapabilities(glp);
165 caps.setAlphaBits(1);
166
167 final TextureData texData = TextureIO.newTextureData(glp, istream, false /* mipmap */, TextureIO.PNG);
168 System.err.println("TextureData: "+texData);
169
170 final GLWindow glad = GLWindow.create(caps);
171 glad.setTitle("TestPNGTextureGL2FromFileNEWT");
172 // Size OpenGL to Video Surface
173 glad.setSize(texData.getWidth(), texData.getHeight());
174
175 // load texture from file inside current GL context to match the way
176 // the bug submitter was doing it
177 final GLEventListener gle = useFFP ? new TextureDraw01GL2Listener( texData ) : new TextureDraw01ES2Listener( texData, 0 ) ;
178 glad.addGLEventListener(gle);
180 boolean shot = false;
181
182 @Override public void init(final GLAutoDrawable drawable) {}
183
184 public void display(final GLAutoDrawable drawable) {
185 // 1 snapshot
186 if(null!=((TextureDraw01Accessor)gle).getTexture() && !shot) {
187 shot = true;
188 snapshot(0, null, drawable.getGL(), screenshot, TextureIO.PNG, null);
189 }
190 }
191
192 @Override public void dispose(final GLAutoDrawable drawable) { }
193 @Override public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) { }
194 });
195
196 final Animator animator = new Animator(glad);
197 animator.setUpdateFPSFrames(60, showFPS ? System.err : null);
198 final QuitAdapter quitAdapter = new QuitAdapter();
199 glad.addKeyListener(quitAdapter);
200 glad.addWindowListener(quitAdapter);
201 glad.setVisible(true);
202 animator.start();
203
204 while(!quitAdapter.shouldQuit() && animator.isAnimating() && animator.getTotalFPSDuration()<duration) {
205 Thread.sleep(100);
206 }
207
208 animator.stop();
209 glad.destroy();
210 }
211
212 @Test
213 public void testGray__GL2() throws InterruptedException, IOException {
214 testImpl(true, grayTextureStream);
215 }
216 @Test
217 public void testGray__ES2() throws InterruptedException, IOException {
218 testImpl(false, grayTextureStream);
219 }
220
221 @Test
222 public void testRGB3__GL2() throws InterruptedException, IOException {
223 testImpl(true, testTextureStreamN_3);
224 }
225 @Test
226 public void testRGB3__ES2() throws InterruptedException, IOException {
227 testImpl(false, testTextureStreamN_3);
228 }
229 @Test
230 public void testRGB4__GL2() throws InterruptedException, IOException {
231 testImpl(true, testTextureStreamN_4);
232 }
233 @Test
234 public void testRGB4__ES2() throws InterruptedException, IOException {
235 testImpl(false, testTextureStreamN_4);
236 }
237 @Test
238 public void testRGB4G_ES2() throws InterruptedException, IOException {
239 testImpl(false, testTextureStreamNG4);
240 }
241
242 @Test
243 public void testInterl3__ES2() throws InterruptedException, IOException {
244 testImpl(false, testTextureStreamI_3);
245 }
246 @Test
247 public void testInterl4__ES2() throws InterruptedException, IOException {
248 testImpl(false, testTextureStreamI_4);
249 }
250 @Test
251 public void testInterl3G_ES2() throws InterruptedException, IOException {
252 testImpl(false, testTextureStreamIG3);
253 }
254 @Test
255 public void testInterl4G_ES2() throws InterruptedException, IOException {
256 testImpl(false, testTextureStreamIG4);
257 }
258
259 @Test
260 public void testPalette3__ES2() throws InterruptedException, IOException {
261 testImpl(false, testTextureStreamP_3);
262 }
263 @Test
264 public void testPalette4__ES2() throws InterruptedException, IOException {
265 testImpl(false, testTextureStreamP_4);
266 }
267
268 public static void main(final String args[]) throws IOException {
269 for(int i=0; i<args.length; i++) {
270 if(args[i].equals("-time")) {
271 i++;
272 duration = MiscUtils.atol(args[i], duration);
273 }
274 }
275 org.junit.runner.JUnitCore.main(TestPNGTextureFromFileNEWT.class.getName());
276 }
277}
void setAlphaBits(final int alphaBits)
Sets the number of bits requested for the color buffer's alpha component.
An implementation of GLAutoDrawable and Window interface, using a delegated Window instance,...
Definition: GLWindow.java:121
final void setTitle(final String title)
Definition: GLWindow.java:297
final void addKeyListener(final KeyListener l)
Appends the given com.jogamp.newt.event.KeyListener to the end of the list.
Definition: GLWindow.java:902
final void setSize(final int width, final int height)
Sets the size of the window's client area in window units, excluding decorations.
Definition: GLWindow.java:625
final void setVisible(final boolean visible)
Calls setVisible(true, visible), i.e.
Definition: GLWindow.java:615
final void addWindowListener(final WindowListener l)
Appends the given com.jogamp.newt.event.WindowListener to the end of the list.
Definition: GLWindow.java:882
final void destroy()
Destroys all resources associated with this GLAutoDrawable, inclusive the GLContext.
Definition: GLWindow.java:605
static GLWindow create(final GLCapabilitiesImmutable caps)
Creates a new GLWindow attaching a new Window referencing a new default Screen and default Display wi...
Definition: GLWindow.java:169
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
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.
void addGLEventListener(GLEventListener listener)
Adds the given listener to the end of this drawable queue.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.