JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestJPEGJoglAWTCompareNewtAWT.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.os.Platform;
33import com.jogamp.common.util.IOUtil;
34import com.jogamp.junit.util.JunitTracer;
35import com.jogamp.newt.opengl.GLWindow;
36import com.jogamp.opengl.test.junit.jogl.demos.TextureDraw01Accessor;
37import com.jogamp.opengl.test.junit.jogl.demos.es2.TextureDraw01ES2Listener;
38import com.jogamp.opengl.test.junit.util.MiscUtils;
39import com.jogamp.opengl.test.junit.util.QuitAdapter;
40import com.jogamp.opengl.test.junit.util.UITestCase;
41
42import javax.imageio.ImageIO;
43import com.jogamp.opengl.GL;
44import com.jogamp.opengl.GLAutoDrawable;
45import com.jogamp.opengl.GLEventListener;
46import com.jogamp.opengl.GLProfile;
47import com.jogamp.opengl.GLCapabilities;
48
49import com.jogamp.opengl.util.texture.TextureData;
50import com.jogamp.opengl.util.texture.TextureIO;
51import com.jogamp.opengl.util.texture.awt.AWTTextureData;
52import com.jogamp.opengl.util.texture.spi.JPEGImage;
53import com.jogamp.opengl.util.Animator;
54import com.jogamp.opengl.util.GLPixelBuffer.GLPixelAttributes;
55import com.jogamp.opengl.util.GLReadBufferUtil;
56
57import java.awt.image.BufferedImage;
58import java.io.IOException;
59import java.io.InputStream;
60import java.net.URLConnection;
61
62import org.junit.Assert;
63import org.junit.BeforeClass;
64import org.junit.Test;
65import org.junit.FixMethodOrder;
66import org.junit.runners.MethodSorters;
67
68@FixMethodOrder(MethodSorters.NAME_ASCENDING)
70 static boolean showFPS = false;
71 static long duration = 100; // ms
72
73 String[] files = { "test-ntscN_3-01-160x90-90pct-yuv444-base.jpg", // 0
74 "test-ntscN_3-01-160x90-90pct-yuv444-prog.jpg", // 1
75 "test-ntscN_3-01-160x90-60pct-yuv422h-base.jpg", // 2
76 "test-ntscN_3-01-160x90-60pct-yuv422h-prog.jpg", // 3
77 "j1-baseline.jpg", // 4
78 "j2-progressive.jpg", // 5
79 "j3-baseline_gray.jpg", // 6
80 "test-cmyk-01.jpg", // 7
81 "test-ycck-01.jpg" }; // 8
82
83 void testImpl(final String fname) throws InterruptedException, IOException {
84 final Animator animator = new Animator();
85
86 final GLWindow w1 = testJOGLJpeg(fname);
87 final GLWindow w2 = testAWTJpeg(fname, w1.getSurfaceWidth() + 50);
88
89 animator.add(w1);
90 animator.add(w2);
91 animator.setUpdateFPSFrames(60, showFPS ? System.err : null);
92 final QuitAdapter quitAdapter = new QuitAdapter();
93 w1.setVisible(true);
94 w2.setVisible(true);
95 animator.start();
96
97 while(!quitAdapter.shouldQuit() && animator.isAnimating() && animator.getTotalFPSDuration()<duration) {
98 Thread.sleep(100);
99 }
100
101 animator.stop();
102 w1.destroy();
103 w2.destroy();
104 }
105
106 GLWindow testJOGLJpeg(final String fname) throws InterruptedException, IOException {
107 final URLConnection testTextureUrlConn = IOUtil.getResource(fname, this.getClass().getClassLoader(), this.getClass());
108 Assert.assertNotNull(testTextureUrlConn);
109 final InputStream istream = testTextureUrlConn.getInputStream();
110 Assert.assertNotNull(istream);
111
112 final JPEGImage image = JPEGImage.read(istream);
113 Assert.assertNotNull(image);
114 System.err.println("JPEGImage: "+image);
115
116 final GLProfile glp = GLProfile.getGL2ES2();
117 final int internalFormat = (image.getBytesPerPixel()==4)?GL.GL_RGBA:GL.GL_RGB;
118 final TextureData texData = new TextureData(glp, internalFormat,
119 image.getWidth(),
120 image.getHeight(),
121 0,
122 new GLPixelAttributes(image.getGLFormat(), image.getGLType()),
123 false /* mipmap */,
124 false /* compressed */,
125 false /* must flip-vert */,
126 image.getData(),
127 null);
128 // final TextureData texData = TextureIO.newTextureData(glp, istream, false /* mipmap */, TextureIO.JPG);
129 System.err.println("TextureData: "+texData);
130
131 final GLReadBufferUtil screenshot = new GLReadBufferUtil(true, false);
132 final GLCapabilities caps = new GLCapabilities(glp);
133 caps.setAlphaBits(1);
134
135 final GLWindow glad1 = GLWindow.create(caps);
136 glad1.setTitle("JPEG JOGL");
137 // Size OpenGL to Video Surface
138 glad1.setSize(texData.getWidth(), texData.getHeight());
139 glad1.setPosition(0, 0);
140
141 // load texture from file inside current GL context to match the way
142 // the bug submitter was doing it
143 final GLEventListener gle = new TextureDraw01ES2Listener( texData, 0 ) ;
144 glad1.addGLEventListener(gle);
146 boolean shot = false;
147
148 @Override public void init(final GLAutoDrawable drawable) {}
149
150 @Override
151 public void display(final GLAutoDrawable drawable) {
152 // 1 snapshot
153 if(null!=((TextureDraw01Accessor)gle).getTexture() && !shot) {
154 shot = true;
155 snapshot(0, "JoglJPEG", drawable.getGL(), screenshot, TextureIO.PNG, null);
156 }
157 }
158
159 @Override public void dispose(final GLAutoDrawable drawable) { }
160 @Override public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) { }
161 });
162
163 return glad1;
164 }
165
166 GLWindow testAWTJpeg(final String fname, final int xpos) throws InterruptedException, IOException {
167 final URLConnection testTextureUrlConn = IOUtil.getResource(fname, this.getClass().getClassLoader(), this.getClass());
168 Assert.assertNotNull(testTextureUrlConn);
169 final InputStream istream = testTextureUrlConn.getInputStream();
170 Assert.assertNotNull(istream);
171
172 final GLProfile glp = GLProfile.getGL2ES2();
173 TextureData texData = null;
174 int w = 300, h = 300;
175 try {
176 final BufferedImage img = ImageIO.read(istream);
177 texData = new AWTTextureData(glp, 0, 0, false, img);
178 System.err.println("TextureData: "+texData);
179 w = texData.getWidth();
180 h = texData.getHeight();
181 } catch (final Exception e) {
182 System.err.println("AWT ImageIO failure w/ file "+fname+": "+e.getMessage());
183 // e.printStackTrace(); // : CMYK, YCCK -> com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(Unknown Source)
184 }
185
186 final GLReadBufferUtil screenshot = new GLReadBufferUtil(true, false);
187 final GLCapabilities caps = new GLCapabilities(glp);
188 caps.setAlphaBits(1);
189
190 final GLWindow glad1 = GLWindow.create(caps);
191 glad1.setTitle("JPEG AWT");
192 // Size OpenGL to Video Surface
193 glad1.setSize(w, h);
194 glad1.setPosition(xpos, 0);
195
196 // load texture from file inside current GL context to match the way
197 // the bug submitter was doing it
198 final GLEventListener gle;
199 if( texData != null ) {
200 gle = new TextureDraw01ES2Listener( texData, 0 ) ;
201 glad1.addGLEventListener(gle);
202 } else {
203 gle = null;
204 }
206 boolean shot = false;
207
208 @Override public void init(final GLAutoDrawable drawable) {}
209
210 @Override
211 public void display(final GLAutoDrawable drawable) {
212 // 1 snapshot
213 if( null!=gle && null!=((TextureDraw01Accessor)gle).getTexture() && !shot) {
214 shot = true;
215 snapshot(0, "AWTJPEG", drawable.getGL(), screenshot, TextureIO.PNG, null);
216 }
217 }
218
219 @Override public void dispose(final GLAutoDrawable drawable) { }
220 @Override public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) { }
221 });
222
223 return glad1;
224 }
225
226 @Test
227 public void test01YUV444Base__ES2() throws InterruptedException, IOException {
228 testImpl(files[0]);
229 }
230 @Test
231 public void test01YUV444Prog__ES2() throws InterruptedException, IOException {
232 testImpl(files[1]);
233 }
234
235 @Test
236 public void test01YUV422hBase__ES2() throws InterruptedException, IOException {
237 testImpl(files[2]);
238 }
239 @Test
240 public void test01YUV422hProg_ES2() throws InterruptedException, IOException {
241 testImpl(files[3]);
242 }
243
244 @Test
245 public void test02YUV420Base__ES2() throws InterruptedException, IOException {
246 testImpl(files[4]);
247 }
248 @Test
249 public void test02YUV420Prog_ES2() throws InterruptedException, IOException {
250 testImpl(files[5]);
251 }
252 @Test
253 public void test02YUV420BaseGray_ES2() throws InterruptedException, IOException {
254 testImpl(files[6]);
255 }
256
257 @Test
258 public void test03CMYK_01_ES2() throws InterruptedException, IOException {
259 testImpl(files[7]);
260 }
261 @Test
262 public void test03YCCK_01_ES2() throws InterruptedException, IOException {
263 testImpl(files[8]);
264 }
265
266 @BeforeClass
267 public static void beforeClass() throws Exception {
268 if( !manual_test ) {
269 if( Platform.OSType.MACOS == Platform.getOSType() ) {
270 JunitTracer.setTestSupported(false);
271 }
272 }
273 }
274 static boolean manual_test = false;
275
276 public static void main(final String args[]) throws IOException {
277 manual_test = true;
278 for(int i=0; i<args.length; i++) {
279 if(args[i].equals("-time")) {
280 i++;
281 duration = MiscUtils.atol(args[i], duration);
282 }
283 }
284 org.junit.runner.JUnitCore.main(TestJPEGJoglAWTCompareNewtAWT.class.getName());
285 }
286}
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 setPosition(final int x, final int y)
Sets the location of the window's client area excluding insets (window decorations) in window units.
Definition: GLWindow.java:525
final void setTitle(final String title)
Definition: GLWindow.java:297
final int getSurfaceWidth()
Returns the width of this GLDrawable's surface client area in pixel units.
Definition: GLWindow.java:461
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 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 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 synchronized void add(final GLAutoDrawable drawable)
Adds a drawable to this animator's list of rendering drawables.
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
ByteBuffer getData()
Returns the raw data for this texture in the correct (bottom-to-top) order for calls to glTexImage2D.
Definition: JPEGImage.java:173
int getBytesPerPixel()
Returns the bytes per pixel.
Definition: JPEGImage.java:169
int getGLFormat()
Returns the OpenGL format for this texture; e.g.
Definition: JPEGImage.java:163
int getWidth()
Returns the width of the image.
Definition: JPEGImage.java:154
int getHeight()
Returns the height of the image.
Definition: JPEGImage.java:157
int getGLType()
Returns the OpenGL data type: GL.GL_UNSIGNED_BYTE.
Definition: JPEGImage.java:166
static JPEGImage read(final InputStream in, final ColorSpace cs)
Reads a JPEG image from the specified InputStream, using the given color space for storage.
Definition: JPEGImage.java:54
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.
static final int GL_RGB
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_RGB" with expression ...
Definition: GL.java:374
static final int GL_RGBA
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_RGBA" with expression...
Definition: GL.java:150