JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestJPEGImage01NEWT.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 */
28package com.jogamp.opengl.test.junit.jogl.util.texture;
29
30import java.io.IOException;
31import java.io.InputStream;
32import java.net.MalformedURLException;
33import java.net.URLConnection;
34
35import com.jogamp.opengl.GLAutoDrawable;
36import com.jogamp.opengl.GLCapabilities;
37import com.jogamp.opengl.GLEventListener;
38import com.jogamp.opengl.GLProfile;
39
40import org.junit.Assert;
41import org.junit.Test;
42import org.junit.FixMethodOrder;
43import org.junit.runners.MethodSorters;
44
45import com.jogamp.common.util.IOUtil;
46import com.jogamp.newt.opengl.GLWindow;
47import com.jogamp.opengl.test.junit.jogl.demos.TextureDraw01Accessor;
48import com.jogamp.opengl.test.junit.jogl.demos.es2.TextureDraw01ES2Listener;
49import com.jogamp.opengl.test.junit.util.MiscUtils;
50import com.jogamp.opengl.test.junit.util.QuitAdapter;
51import com.jogamp.opengl.test.junit.util.UITestCase;
52import com.jogamp.opengl.util.Animator;
53import com.jogamp.opengl.util.GLPixelBuffer.GLPixelAttributes;
54import com.jogamp.opengl.util.GLReadBufferUtil;
55import com.jogamp.opengl.util.texture.TextureData;
56import com.jogamp.opengl.util.texture.TextureIO;
57import com.jogamp.opengl.util.texture.spi.JPEGImage;
58import com.jogamp.opengl.GL;
59
60/**
61 * Test reading and displaying a JPG image.
62 * <p>
63 * Main function accepts arbitrary JPG file name for manual tests.
64 * </p>
65 */
66@FixMethodOrder(MethodSorters.NAME_ASCENDING)
67public class TestJPEGImage01NEWT extends UITestCase {
68
69 static boolean showFPS = false;
70 static long duration = 100; // ms
71
72 public void testImpl(final InputStream istream) throws InterruptedException, IOException {
73 final JPEGImage image = JPEGImage.read(istream);
74 Assert.assertNotNull(image);
75 final boolean hasAlpha = 4 == image.getBytesPerPixel();
76 System.err.println("JPEGImage: "+image+", hasAlpha "+hasAlpha);
77
78 final GLReadBufferUtil screenshot = new GLReadBufferUtil(true, false);
79 final GLProfile glp = GLProfile.getGL2ES2();
80 final GLCapabilities caps = new GLCapabilities(glp);
81 if( hasAlpha ) {
82 caps.setAlphaBits(1);
83 }
84
85 final int internalFormat;
86 if(glp.isGL2ES3()) {
87 internalFormat = hasAlpha ? GL.GL_RGBA8 : GL.GL_RGB8;
88 } else {
89 internalFormat = hasAlpha ? GL.GL_RGBA : GL.GL_RGB;
90 }
91 final TextureData texData = new TextureData(glp, internalFormat,
92 image.getWidth(),
93 image.getHeight(),
94 0,
95 new GLPixelAttributes(image.getGLFormat(), image.getGLType()),
96 false /* mipmap */,
97 false /* compressed */,
98 false /* must flip-vert */,
99 image.getData(),
100 null);
101 // final TextureData texData = TextureIO.newTextureData(glp, istream, false /* mipmap */, TextureIO.JPG);
102 System.err.println("TextureData: "+texData);
103
104 final GLWindow glad = GLWindow.create(caps);
105 glad.setTitle("TestJPEGImage01NEWT");
106 // Size OpenGL to Video Surface
107 glad.setSize(texData.getWidth(), texData.getHeight());
108
109 // load texture from file inside current GL context to match the way
110 // the bug submitter was doing it
111 final GLEventListener gle = new TextureDraw01ES2Listener( texData, 0 ) ;
112 glad.addGLEventListener(gle);
114 boolean shot = false;
115
116 @Override public void init(final GLAutoDrawable drawable) {}
117
118 public void display(final GLAutoDrawable drawable) {
119 // 1 snapshot
120 if(null!=((TextureDraw01Accessor)gle).getTexture() && !shot) {
121 shot = true;
122 snapshot(0, null, drawable.getGL(), screenshot, TextureIO.PNG, null);
123 }
124 }
125
126 @Override public void dispose(final GLAutoDrawable drawable) { }
127 @Override public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) { }
128 });
129
130 final Animator animator = new Animator(glad);
131 animator.setUpdateFPSFrames(60, showFPS ? System.err : null);
132 final QuitAdapter quitAdapter = new QuitAdapter();
133 glad.addKeyListener(quitAdapter);
134 glad.addWindowListener(quitAdapter);
135 glad.setVisible(true);
136 animator.start();
137
138 while(!quitAdapter.shouldQuit() && animator.isAnimating() && animator.getTotalFPSDuration()<duration) {
139 Thread.sleep(100);
140 }
141
142 animator.stop();
143 glad.destroy();
144 }
145
146 @Test
147 public void testReadES2_RGBn() throws InterruptedException, IOException, MalformedURLException {
148 final String fname = null == _fname ? "test-ntscN_3-01-160x90-90pct-yuv444-base.jpg" : _fname;
149 final URLConnection urlConn = IOUtil.getResource(fname, this.getClass().getClassLoader(), this.getClass());
150 testImpl(urlConn.getInputStream());
151 }
152
153 static String _fname = null;
154 public static void main(final String args[]) {
155 for(int i=0; i<args.length; i++) {
156 if(args[i].equals("-time")) {
157 i++;
158 duration = MiscUtils.atol(args[i], duration);
159 } else if(args[i].equals("-file")) {
160 i++;
161 _fname = args[i];
162 }
163 }
164 org.junit.runner.JUnitCore.main(TestJPEGImage01NEWT.class.getName());
165 }
166}
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 GLProfile getGL2ES2(final AbstractGraphicsDevice device)
Returns the GL2ES2 profile implementation, hence compatible w/ GL2ES2.
Definition: GLProfile.java:913
final boolean isGL2ES3()
Indicates whether this profile is capable of GL2ES3.
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
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_RGB8
GL_ES_VERSION_3_0, GL_VERSION_1_1, GL_EXT_texture, GL_OES_rgb8_rgba8, GL_OES_required_internalformat ...
Definition: GL.java:479
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