JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestPNGPixelRect01NEWT.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 */
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.nativewindow.util.PixelFormat;
36import com.jogamp.opengl.GL;
37import com.jogamp.opengl.GLAutoDrawable;
38import com.jogamp.opengl.GLCapabilities;
39import com.jogamp.opengl.GLEventListener;
40import com.jogamp.opengl.GLProfile;
41
42import org.junit.Assert;
43import org.junit.Test;
44import org.junit.FixMethodOrder;
45import org.junit.runners.MethodSorters;
46
47import com.jogamp.common.util.IOUtil;
48import com.jogamp.newt.opengl.GLWindow;
49import com.jogamp.opengl.test.junit.jogl.demos.TextureDraw01Accessor;
50import com.jogamp.opengl.test.junit.jogl.demos.es2.TextureDraw01ES2Listener;
51import com.jogamp.opengl.test.junit.util.MiscUtils;
52import com.jogamp.opengl.test.junit.util.QuitAdapter;
53import com.jogamp.opengl.test.junit.util.UITestCase;
54import com.jogamp.opengl.util.Animator;
55import com.jogamp.opengl.util.GLReadBufferUtil;
56import com.jogamp.opengl.util.PNGPixelRect;
57import com.jogamp.opengl.util.GLPixelBuffer.GLPixelAttributes;
58import com.jogamp.opengl.util.texture.TextureData;
59import com.jogamp.opengl.util.texture.TextureIO;
60
61/**
62 * Test reading and displaying a PNG image.
63 * <p>
64 * Main function accepts arbitrary PNG file name for manual tests.
65 * </p>
66 */
67@FixMethodOrder(MethodSorters.NAME_ASCENDING)
68public class TestPNGPixelRect01NEWT extends UITestCase {
69 static boolean showFPS = false;
70 static long duration = 200; // ms
71
72 public void testImpl(final int num, final String basename, final InputStream istream, final PixelFormat destFmt) throws InterruptedException, IOException {
73 final GLProfile glp = GLProfile.getGL2ES2();
74 final PNGPixelRect image = PNGPixelRect.read(istream, destFmt, true /* directBuffer */, 0 /* destMinStrideInBytes */, true /* destIsGLOriented */);
75 Assert.assertNotNull(image);
76 System.err.println("PNGPixelRect: "+basename+", "+image);
77 final GLPixelAttributes glpa = new GLPixelAttributes(glp, image.getPixelformat(), false /* pack */);
78 final boolean hasAlpha = 4 == glpa.pfmt.comp.bytesPerPixel();
79 System.err.println("GLPixelAttributes: "+glpa);
80
81 final GLReadBufferUtil screenshot = new GLReadBufferUtil(true, false);
82 final GLCapabilities caps = new GLCapabilities(glp);
83 if( hasAlpha ) {
84 caps.setAlphaBits(1);
85 }
86
87 final int internalFormat;
88 if(glp.isGL2ES3()) {
89 internalFormat = hasAlpha ? GL.GL_RGBA8 : GL.GL_RGB8;
90 } else {
91 internalFormat = hasAlpha ? GL.GL_RGBA : GL.GL_RGB;
92 }
93 final TextureData texData = new TextureData(glp, internalFormat,
94 image.getSize().getWidth(),
95 image.getSize().getHeight(),
96 0,
97 glpa,
98 false /* mipmap */,
99 false /* compressed */,
100 false /* must flip-vert */,
101 image.getPixels(),
102 null);
103
104 // final TextureData texData = TextureIO.newTextureData(glp, istream, false /* mipmap */, TextureIO.PNG);
105 System.err.println("TextureData: "+texData);
106
107 final GLWindow glad = GLWindow.create(caps);
108 glad.setTitle(this.getSimpleTestName("."));
109 // Size OpenGL to Video Surface
110 glad.setSize(texData.getWidth(), texData.getHeight());
111
112 // load texture from file inside current GL context to match the way
113 // the bug submitter was doing it
114 final TextureDraw01ES2Listener gle = new TextureDraw01ES2Listener( texData, 0 ) ;
115 // gle.setClearColor(new float[] { 1.0f, 0.0f, 0.0f, 1.0f } );
116
117 glad.addGLEventListener(gle);
119 boolean shot = false;
120
121 @Override public void init(final GLAutoDrawable drawable) {
122 System.err.println("Chosen Caps: " + drawable.getChosenGLCapabilities());
123 System.err.println("GL ctx: " + drawable.getGL().getContext());
124 }
125
126 @Override public void display(final GLAutoDrawable drawable) {
127 // 1 snapshot
128 if(null!=((TextureDraw01Accessor)gle).getTexture() && !shot) {
129 shot = true;
130 snapshot(num, basename, drawable.getGL(), screenshot, TextureIO.PNG, null);
131 }
132 }
133
134 @Override public void dispose(final GLAutoDrawable drawable) { }
135 @Override public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) { }
136 });
137
138 final Animator animator = new Animator(glad);
139 animator.setUpdateFPSFrames(60, showFPS ? System.err : null);
140 final QuitAdapter quitAdapter = new QuitAdapter();
141 glad.addKeyListener(quitAdapter);
142 glad.addWindowListener(quitAdapter);
143 glad.setVisible(true);
144 animator.start();
145
146 while(!quitAdapter.shouldQuit() && animator.isAnimating() && animator.getTotalFPSDuration()<duration) {
147 Thread.sleep(100);
148 }
149
150 animator.stop();
151 glad.destroy();
152 }
153
154 @Test
155 public void testRead00_Manual() throws InterruptedException, IOException, MalformedURLException {
156 if( null == _fname ) {
157 return;
158 }
159 final URLConnection urlConn = IOUtil.getResource(_fname, this.getClass().getClassLoader(), this.getClass());
160 if( null == urlConn ) {
161 throw new IOException("Cannot find "+_fname+".png");
162 }
163 testImpl(0, _fname, urlConn.getInputStream(), null);
164 }
165
166 @Test
167 public void testRead01_All() throws InterruptedException, IOException, MalformedURLException {
168 if( null != _fname ) {
169 return;
170 }
171 for(int i=0; i<PNGTstFiles.allBasenames.length; i++) {
172 final String basename = PNGTstFiles.allBasenames[i];
173 final URLConnection urlConn = IOUtil.getResource(basename+".png", this.getClass().getClassLoader(), this.getClass());
174 if( null == urlConn ) {
175 throw new IOException("Cannot find "+basename+".png");
176 }
177 testImpl(i, basename, urlConn.getInputStream(), null);
178 }
179 }
180 @Test
181 public void testRead02_Gray2RGBA() throws InterruptedException, IOException, MalformedURLException {
182 if( null != _fname ) {
183 return;
184 }
185 for(int i=0; i<PNGTstFiles.greyBasenames.length; i++) {
186 final String basename = PNGTstFiles.greyBasenames[i];
187 final URLConnection urlConn = IOUtil.getResource(basename+".png", this.getClass().getClassLoader(), this.getClass());
188 if( null == urlConn ) {
189 throw new IOException("Cannot find "+basename+".png");
190 }
191 testImpl(i, basename, urlConn.getInputStream(), PixelFormat.RGBA8888);
192 }
193 }
194
195 static String _fname = null;
196 public static void main(final String args[]) {
197 for(int i=0; i<args.length; i++) {
198 if(args[i].equals("-time")) {
199 i++;
200 duration = MiscUtils.atol(args[i], duration);
201 } else if(args[i].equals("-file")) {
202 i++;
203 _fname = args[i];
204 }
205 }
206 org.junit.runner.JUnitCore.main(TestPNGPixelRect01NEWT.class.getName());
207 }
208}
void setAlphaBits(final int alphaBits)
Sets the number of bits requested for the color buffer's alpha component.
DimensionImmutable getSize()
Returns the size, i.e.
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.
void testImpl(final int num, final String basename, final InputStream istream, final PixelFormat destFmt)
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
final PixelFormat pfmt
PixelFormat describing the component layout
Utility to read out the current FB to TextureData, optionally writing the data back to a texture obje...
static PNGPixelRect read(final InputStream in, final PixelFormat ddestFmt, final boolean destDirectBuffer, final int destMinStrideInBytes, final boolean destIsGLOriented)
Reads a PNG image from the specified InputStream.
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
final Composition comp
Unique Pixel Composition, i.e.
RGBA8888
Stride is 32 bits, 32 bits per pixel, 4 uniform components of 8 bits.
int bytesPerPixel()
Number of bytes per pixel, i.e.
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.
GLContext getContext()
Returns the GLContext associated which this GL object.
GLCapabilitiesImmutable getChosenGLCapabilities()
Fetches the GLCapabilitiesImmutable corresponding to the chosen OpenGL capabilities (pixel format / v...
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