JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestPNGPixelRect00NEWT.java
Go to the documentation of this file.
1/**
2 * Copyright 2014 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.BufferedOutputStream;
31import java.io.File;
32import java.io.IOException;
33import java.io.OutputStream;
34import java.net.MalformedURLException;
35import java.net.URLConnection;
36
37import com.jogamp.nativewindow.util.PixelFormat;
38import com.jogamp.nativewindow.util.PixelFormatUtil;
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.opengl.test.junit.util.UITestCase;
47import com.jogamp.opengl.util.PNGPixelRect;
48
49@FixMethodOrder(MethodSorters.NAME_ASCENDING)
50public class TestPNGPixelRect00NEWT extends UITestCase {
51 @Test
52 public void testPNGRead01_All() throws InterruptedException, IOException, MalformedURLException {
53 for(int i=0; i<PNGTstFiles.allBasenames.length; i++) {
54 final String basename = PNGTstFiles.allBasenames[i];
55 final String pathname="";
56 testPNG01Impl(pathname, basename, null, 0 /* destMinStrideInBytes */, false /* destIsGLOriented */);
57 }
58 }
59
60 @Test
61 public void testPNGRead02_RGB888_to_RGBA8888() throws InterruptedException, IOException, MalformedURLException {
62 final String basename ="test-ntscN_3-01-160x90";
63 final String pathname="";
64 testPNG01Impl(pathname, basename, PixelFormat.RGBA8888, 0 /* destMinStrideInBytes */, false /* destIsGLOriented */);
65 }
66
67 @Test
68 public void testPNGRead03_RGB888_to_RGBA8888_stride1000() throws InterruptedException, IOException, MalformedURLException {
69 final String basename ="test-ntscN_3-01-160x90"; // 640 bytes = 4 * 160
70 final String pathname="";
71 testPNG01Impl(pathname, basename, PixelFormat.RGBA8888, 1000 /* destMinStrideInBytes */, false /* destIsGLOriented */);
72 }
73
74 @Test
75 public void testPNGRead04_RGB888_to_RGBA8888_stride999() throws InterruptedException, IOException, MalformedURLException {
76 final String basename ="test-ntscN_3-01-160x90"; // 640 bytes = 4 * 160
77 final String pathname="";
78 testPNG01Impl(pathname, basename, PixelFormat.RGBA8888, 999 /* destMinStrideInBytes */, false /* destIsGLOriented */);
79 }
80
81 @Test
82 public void testPNGRead11_RGBA8888_to_LUMINA() throws InterruptedException, IOException, MalformedURLException {
83 final String basename ="test-ntscN_4-01-160x90";
84 final String pathname="";
85 testPNG02Impl(pathname, basename, PixelFormat.LUMINANCE, 0 /* destMinStrideInBytes */, false /* destIsGLOriented */);
86 }
87
88 @Test
89 public void testPNGRead12_RGBA8888_to_RGB888() throws InterruptedException, IOException, MalformedURLException {
90 final String basename ="test-ntscN_4-01-160x90";
91 final String pathname="";
92 testPNG02Impl(pathname, basename, PixelFormat.RGB888, 0 /* destMinStrideInBytes */, false /* destIsGLOriented */);
93 }
94
95 @Test
96 public void testPNGRead13_RGBA8888_to_BGR888() throws InterruptedException, IOException, MalformedURLException {
97 final String basename ="test-ntscN_4-01-160x90";
98 final String pathname="";
99 testPNG02Impl(pathname, basename, PixelFormat.BGR888, 0 /* destMinStrideInBytes */, false /* destIsGLOriented */);
100 }
101
102 @Test
103 public void testPNGRead14_RGBA8888_to_BGRA8888() throws InterruptedException, IOException, MalformedURLException {
104 final String basename ="test-ntscN_4-01-160x90";
105 final String pathname="";
106 testPNG02Impl(pathname, basename, PixelFormat.BGRA8888, 0 /* destMinStrideInBytes */, false /* destIsGLOriented */);
107 }
108 @Test
109 public void testPNGRead15_RGBA8888_to_ARGB8888() throws InterruptedException, IOException, MalformedURLException {
110 final String basename ="test-ntscN_4-01-160x90";
111 final String pathname="";
112 testPNG02Impl(pathname, basename, PixelFormat.ARGB8888, 0 /* destMinStrideInBytes */, false /* destIsGLOriented */);
113 }
114 @Test
115 public void testPNGRead16_RGBA8888_to_ABGR8888() throws InterruptedException, IOException, MalformedURLException {
116 final String basename ="test-ntscN_4-01-160x90";
117 final String pathname="";
118 testPNG02Impl(pathname, basename, PixelFormat.ABGR8888, 0 /* destMinStrideInBytes */, false /* destIsGLOriented */);
119 }
120
121 private void testPNG01Impl(final String pathname, final String basename,
122 final PixelFormat destFmt, final int destMinStrideInBytes, final boolean destIsGLOriented)
123 throws InterruptedException, IOException, MalformedURLException
124 {
125 System.err.println("Test01: "+pathname+basename+".png, destFmt "+destFmt+", destMinStrideInBytes "+destMinStrideInBytes+", destIsGLOriented "+destIsGLOriented);
126
127 final File out1_f=new File(getSimpleTestName(".")+"-01-"+basename+"-orig.png");
128 final File out2F_f=new File(getSimpleTestName(".")+"-02-"+basename+"-flipped.png");
129 final File out2R_f=new File(getSimpleTestName(".")+"-03-"+basename+"-reversed.png");
130 final File out2RF_f=new File(getSimpleTestName(".")+"-04-"+basename+"-reversed_flipped.png");
131 final URLConnection urlConn = IOUtil.getResource(pathname+basename+".png", this.getClass().getClassLoader(), this.getClass());
132 if( null == urlConn ) {
133 throw new IOException("Cannot find "+pathname+basename+".png");
134 }
135 final PNGPixelRect image1 = PNGPixelRect.read(urlConn.getInputStream(), destFmt, false /* directBuffer */, destMinStrideInBytes, destIsGLOriented);
136 System.err.println("PNGPixelRect - Orig: "+image1);
137 {
138 final OutputStream outs = new BufferedOutputStream(IOUtil.getFileOutputStream(out1_f, true /* allowOverwrite */));
139 image1.write(outs, true /* close */);
140 {
141 final PNGPixelRect image1_R = PNGPixelRect.read(out1_f.toURI().toURL().openStream(), image1.getPixelformat(), false /* directBuffer */, destMinStrideInBytes, destIsGLOriented);
142 System.err.println("PNGPixelRect - Orig (Read Back): "+image1_R);
143 Assert.assertEquals(image1.getPixels(), image1_R.getPixels());
144 }
145 }
146
147 //
148 // Flipped Orientation
149 //
150 {
151 final PNGPixelRect image2F = new PNGPixelRect(image1.getPixelformat(), image1.getSize(),
152 image1.getStride(), !image1.isGLOriented(), image1.getPixels(),
153 image1.getDpi()[0], image1.getDpi()[1]);
154 System.err.println("PNGPixelRect - Flip : "+image2F);
155 final OutputStream outs = new BufferedOutputStream(IOUtil.getFileOutputStream(out2F_f, true /* allowOverwrite */));
156 image2F.write(outs, true /* close */);
157 {
158 // flip again .. to compare w/ original
159 final PNGPixelRect image2F_R = PNGPixelRect.read(out2F_f.toURI().toURL().openStream(), image1.getPixelformat(), false /* directBuffer */, destMinStrideInBytes, !destIsGLOriented);
160 System.err.println("PNGPixelRect - Flip (Read Back): "+image2F_R);
161 Assert.assertEquals(image1.getPixels(), image2F_R.getPixels());
162 }
163 }
164
165 //
166 // Reversed Components
167 //
168 final PixelFormat revFmt = PixelFormatUtil.getReversed(image1.getPixelformat());
169 {
170 final PNGPixelRect image2R = new PNGPixelRect(revFmt, image1.getSize(),
171 image1.getStride(), image1.isGLOriented(), image1.getPixels(),
172 image1.getDpi()[0], image1.getDpi()[1]);
173 System.err.println("PNGPixelRect - Reversed : "+image2R);
174 final OutputStream outs = new BufferedOutputStream(IOUtil.getFileOutputStream(out2R_f, true /* allowOverwrite */));
175 image2R.write(outs, true /* close */);
176 {
177 // reverse again .. to compare w/ original
178 final PNGPixelRect image2R_R = PNGPixelRect.read(out2R_f.toURI().toURL().openStream(), revFmt, false /* directBuffer */, destMinStrideInBytes, destIsGLOriented);
179 System.err.println("PNGPixelRect - Reversed (Read Back): "+image2R_R);
180 Assert.assertEquals(image1.getPixels(), image2R_R.getPixels());
181 }
182 }
183
184 // reversed channels and flipped
185 {
186 final PNGPixelRect image2RF = new PNGPixelRect(revFmt, image1.getSize(),
187 image1.getStride(), !image1.isGLOriented(), image1.getPixels(),
188 image1.getDpi()[0], image1.getDpi()[1]);
189 System.err.println("PNGPixelRect - Reversed+Flipped : "+image2RF);
190 final OutputStream outs = new BufferedOutputStream(IOUtil.getFileOutputStream(out2RF_f, true /* allowOverwrite */));
191 image2RF.write(outs, true /* close */);
192 {
193 // reverse+flip again .. to compare w/ original
194 final PNGPixelRect image2RF_R = PNGPixelRect.read(out2RF_f.toURI().toURL().openStream(), revFmt, false /* directBuffer */, destMinStrideInBytes, !destIsGLOriented);
195 System.err.println("PNGPixelRect - Reversed+FLipped (Read Back): "+image2RF_R);
196 Assert.assertEquals(image1.getPixels(), image2RF_R.getPixels());
197 }
198 }
199 }
200
201 private void testPNG02Impl(final String pathname, final String basename,
202 final PixelFormat destFmt, final int destMinStrideInBytes, final boolean destIsGLOriented)
203 throws InterruptedException, IOException, MalformedURLException
204 {
205 System.err.println("Test02: "+pathname+basename+".png, destFmt "+destFmt+", destMinStrideInBytes "+destMinStrideInBytes+", destIsGLOriented "+destIsGLOriented);
206
207 final File out1_f=new File(getSimpleTestName(".")+"-"+basename+"-orig.png");
208 final URLConnection urlConn = IOUtil.getResource(pathname+basename+".png", this.getClass().getClassLoader(), this.getClass());
209
210 final PNGPixelRect image1 = PNGPixelRect.read(urlConn.getInputStream(), destFmt, false /* directBuffer */, destMinStrideInBytes, destIsGLOriented);
211 System.err.println("PNGPixelRect - Orig: "+image1);
212 {
213 final OutputStream outs = new BufferedOutputStream(IOUtil.getFileOutputStream(out1_f, true /* allowOverwrite */));
214 image1.write(outs, true /* close */);
215 {
216 final PNGPixelRect image1_R = PNGPixelRect.read(out1_f.toURI().toURL().openStream(), image1.getPixelformat(), false /* directBuffer */, destMinStrideInBytes, destIsGLOriented);
217 System.err.println("PNGPixelRect - Orig (Read Back): "+image1_R);
218 Assert.assertEquals(image1.getPixels(), image1_R.getPixels());
219 }
220 }
221 }
222
223 public static void main(final String args[]) {
224 org.junit.runner.JUnitCore.main(TestPNGPixelRect00NEWT.class.getName());
225 }
226}
LUMINANCE
Stride is 8 bits, 8 bits per pixel, 1 component of 8 bits.
ABGR8888
Stride is 32 bits, 32 bits per pixel, 4 uniform components of 8 bits.
BGRA8888
Stride is 32 bits, 32 bits per pixel, 4 uniform components of 8 bits.
BGR888
Stride is 24 bits, 24 bits per pixel, 3 uniform components of of 8 bits.
RGBA8888
Stride is 32 bits, 32 bits per pixel, 4 uniform components of 8 bits.
ARGB8888
Stride is 32 bits, 32 bits per pixel, 4 uniform components of 8 bits.
RGB888
Stride 24 bits, 24 bits per pixel, 3 uniform components of 8 bits.