author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 40429 | jdk/test/javax/imageio/plugins/shared/RepeatingWriteTest.java@d873154f9975 |
permissions | -rw-r--r-- |
36916 | 1 |
/* |
2 |
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
*/ |
|
23 |
||
24 |
/** |
|
25 |
* @test |
|
26 |
* |
|
27 |
* @bug 8144991 8150154 |
|
28 |
* @author a.stepanov |
|
29 |
* @summary Check if repeating image writing doesn't fail |
|
30 |
* (particularly, no AIOOB occurs) |
|
31 |
* |
|
32 |
* @run main RepeatingWriteTest |
|
33 |
*/ |
|
34 |
||
35 |
||
36 |
import java.awt.*; |
|
37 |
import java.awt.image.BufferedImage; |
|
38 |
import java.io.*; |
|
39 |
import javax.imageio.*; |
|
40 |
import javax.imageio.stream.*; |
|
41 |
import java.util.Iterator; |
|
42 |
import java.util.Locale; |
|
43 |
import javax.imageio.plugins.bmp.BMPImageWriteParam; |
|
44 |
import javax.imageio.plugins.jpeg.JPEGImageWriteParam; |
|
45 |
||
46 |
public class RepeatingWriteTest { |
|
47 |
||
48 |
private static final int TYPES[] = new int[]{ |
|
49 |
BufferedImage.TYPE_INT_RGB, BufferedImage.TYPE_INT_ARGB, |
|
50 |
BufferedImage.TYPE_INT_ARGB_PRE, BufferedImage.TYPE_INT_BGR, |
|
51 |
BufferedImage.TYPE_3BYTE_BGR, BufferedImage.TYPE_4BYTE_ABGR, |
|
52 |
BufferedImage.TYPE_4BYTE_ABGR_PRE, BufferedImage.TYPE_BYTE_GRAY, |
|
53 |
BufferedImage.TYPE_USHORT_GRAY, BufferedImage.TYPE_BYTE_BINARY, |
|
54 |
BufferedImage.TYPE_BYTE_INDEXED, BufferedImage.TYPE_USHORT_565_RGB, |
|
55 |
BufferedImage.TYPE_USHORT_555_RGB}; |
|
56 |
||
57 |
private static final String NAMES[] = new String[]{ |
|
58 |
"TYPE_INT_RGB", "TYPE_INT_ARGB", |
|
59 |
"TYPE_INT_ARGB_PRE", "TYPE_INT_BGR", |
|
60 |
"TYPE_3BYTE_BGR", "TYPE_4BYTE_ABGR", |
|
61 |
"TYPE_4BYTE_ABGR_PRE", "TYPE_BYTE_GRAY", |
|
62 |
"TYPE_USHORT_GRAY", "TYPE_BYTE_BINARY", |
|
63 |
"TYPE_BYTE_INDEXED", "TYPE_USHORT_565_RGB", |
|
64 |
"TYPE_USHORT_555_RGB"}; |
|
65 |
||
66 |
private static final int SZ1 = 200, SZ2 = 100; |
|
67 |
private static final Color C1 = Color.BLACK, C2 = Color.WHITE; |
|
68 |
||
69 |
private static ImageWriter getWriter(String format) { |
|
70 |
Iterator<ImageWriter> writers = |
|
71 |
ImageIO.getImageWritersByFormatName(format); |
|
72 |
if (!writers.hasNext()) { |
|
73 |
throw new RuntimeException("no writers available for " + format); |
|
74 |
} |
|
75 |
return writers.next(); |
|
76 |
} |
|
77 |
||
78 |
private static ImageReader getReader(String format) { |
|
79 |
Iterator<ImageReader> readers = |
|
80 |
ImageIO.getImageReadersByFormatName(format); |
|
81 |
if (!readers.hasNext()) { |
|
82 |
throw new RuntimeException("no readers available for " + format); |
|
83 |
} |
|
84 |
return readers.next(); |
|
85 |
} |
|
86 |
||
87 |
private static class ImageCheckException extends Exception { |
|
88 |
public ImageCheckException(String msg) { super(msg); } |
|
89 |
} |
|
90 |
||
91 |
private final String format; |
|
92 |
||
93 |
public RepeatingWriteTest(String format) { this.format = format; } |
|
94 |
||
95 |
private void checkImage(String fileName, boolean is1st) throws Exception { |
|
96 |
||
97 |
Color cRef = is1st ? C1 : C2; |
|
98 |
int szRef = is1st ? SZ1 : SZ2; |
|
99 |
||
100 |
ImageReader reader = getReader(format); |
|
101 |
ImageInputStream iis = ImageIO.createImageInputStream(new File(fileName)); |
|
102 |
reader.setInput(iis); |
|
103 |
BufferedImage img = reader.read(0); |
|
104 |
Color c = new Color(img.getRGB(szRef / 2, szRef / 2)); |
|
105 |
int w = img.getWidth(), h = img.getHeight(); |
|
106 |
||
107 |
if (w != szRef || h != szRef) { |
|
108 |
throw new ImageCheckException(fileName + |
|
40429
d873154f9975
8150154: AIOOB Exception during sequential write of TIFF images
bpb
parents:
36916
diff
changeset
|
109 |
": invalid image size " + w + " x " + h + |
d873154f9975
8150154: AIOOB Exception during sequential write of TIFF images
bpb
parents:
36916
diff
changeset
|
110 |
" expected " + szRef + " x " + szRef); |
36916 | 111 |
} |
112 |
||
113 |
if (!c.equals(cRef)) { |
|
114 |
throw new ImageCheckException(fileName + |
|
40429
d873154f9975
8150154: AIOOB Exception during sequential write of TIFF images
bpb
parents:
36916
diff
changeset
|
115 |
": invalid image color " + c + |
d873154f9975
8150154: AIOOB Exception during sequential write of TIFF images
bpb
parents:
36916
diff
changeset
|
116 |
" expected " + cRef); |
36916 | 117 |
} |
118 |
} |
|
119 |
||
120 |
private void doTest(int i, int j) throws Exception { |
|
121 |
||
122 |
String pair = NAMES[i] + " " + NAMES[j]; |
|
123 |
||
124 |
// some type checks: avoid IO exceptions |
|
125 |
if ((format.equals("jpeg") || format.equals("bmp")) && |
|
126 |
(pair.contains("USHORT_GRAY") || |
|
127 |
pair.contains("ARGB") || pair.contains("ABGR"))) { |
|
128 |
return; |
|
129 |
} |
|
130 |
||
40429
d873154f9975
8150154: AIOOB Exception during sequential write of TIFF images
bpb
parents:
36916
diff
changeset
|
131 |
// If JDK-8163323 is fixed this if block should be removed. |
d873154f9975
8150154: AIOOB Exception during sequential write of TIFF images
bpb
parents:
36916
diff
changeset
|
132 |
if (format.equals("tiff") |
d873154f9975
8150154: AIOOB Exception during sequential write of TIFF images
bpb
parents:
36916
diff
changeset
|
133 |
&& (NAMES[i].equals("TYPE_USHORT_555_RGB") |
d873154f9975
8150154: AIOOB Exception during sequential write of TIFF images
bpb
parents:
36916
diff
changeset
|
134 |
|| NAMES[i].equals("TYPE_USHORT_565_RGB")) |
d873154f9975
8150154: AIOOB Exception during sequential write of TIFF images
bpb
parents:
36916
diff
changeset
|
135 |
&& (NAMES[j].equals("TYPE_USHORT_555_RGB") |
d873154f9975
8150154: AIOOB Exception during sequential write of TIFF images
bpb
parents:
36916
diff
changeset
|
136 |
|| NAMES[j].equals("TYPE_USHORT_565_RGB"))) { |
d873154f9975
8150154: AIOOB Exception during sequential write of TIFF images
bpb
parents:
36916
diff
changeset
|
137 |
return; |
d873154f9975
8150154: AIOOB Exception during sequential write of TIFF images
bpb
parents:
36916
diff
changeset
|
138 |
} |
36916 | 139 |
|
140 |
String f1 = "test-1-" + NAMES[i] + "." + format; |
|
141 |
String f2 = "test-2-" + NAMES[j] + "." + format; |
|
142 |
||
143 |
ImageWriter writer = getWriter(format); |
|
144 |
||
145 |
// --- write 1st image --- |
|
146 |
OutputStream s = new BufferedOutputStream(new FileOutputStream(f1)); |
|
147 |
ImageOutputStream ios = ImageIO.createImageOutputStream(s); |
|
148 |
writer.setOutput(ios); |
|
149 |
||
150 |
BufferedImage img = new BufferedImage(SZ1, SZ1, TYPES[i]); |
|
151 |
Graphics g = img.getGraphics(); |
|
152 |
g.setColor(C1); |
|
153 |
g.fillRect(0, 0, SZ1, SZ1); |
|
154 |
g.dispose(); |
|
155 |
||
156 |
if (format.equals("jpeg")) { |
|
157 |
writer.write(null, new IIOImage(img, null, null), |
|
158 |
new JPEGImageWriteParam(Locale.getDefault())); |
|
159 |
} if (format.equals("bmp")) { |
|
160 |
writer.write(null, new IIOImage(img, null, null), |
|
161 |
new BMPImageWriteParam()); |
|
162 |
} else { |
|
163 |
writer.write(img); |
|
164 |
} |
|
165 |
ios.flush(); |
|
166 |
s.close(); |
|
167 |
||
168 |
// --- write 2nd image --- |
|
169 |
s = new BufferedOutputStream(new FileOutputStream(f2)); |
|
170 |
ios = ImageIO.createImageOutputStream(s); |
|
171 |
writer.setOutput(ios); |
|
172 |
||
173 |
img = new BufferedImage(SZ2, SZ2, TYPES[j]); |
|
174 |
g = img.getGraphics(); |
|
175 |
g.setColor(C2); |
|
176 |
g.fillRect(0, 0, SZ2, SZ2); |
|
177 |
g.dispose(); |
|
178 |
||
179 |
if (format.equals("jpeg")) { |
|
180 |
writer.write(null, new IIOImage(img, null, null), |
|
181 |
new JPEGImageWriteParam(Locale.getDefault())); |
|
182 |
} if (format.equals("bmp")) { |
|
183 |
writer.write(null, new IIOImage(img, null, null), |
|
184 |
new BMPImageWriteParam()); |
|
185 |
} else { |
|
186 |
writer.write(img); |
|
187 |
} |
|
188 |
ios.flush(); |
|
189 |
s.close(); |
|
190 |
||
191 |
// --- check files --- |
|
192 |
checkImage(f1, true); |
|
193 |
checkImage(f2, false); |
|
194 |
} |
|
195 |
||
196 |
public static void main(String args[]) throws Exception { |
|
197 |
||
198 |
||
199 |
int n = TYPES.length; |
|
200 |
int nAIOOB = 0, nChecksFailed = 0; |
|
201 |
||
202 |
String formats[] = {"bmp", "jpeg", "gif", "png", "tiff"}; |
|
203 |
||
204 |
for (String f: formats) { |
|
205 |
System.out.println("\nformat: " + f); |
|
206 |
RepeatingWriteTest test = new RepeatingWriteTest(f); |
|
207 |
for (int i = 0; i < n; ++i) { |
|
208 |
for (int j = 0; j < n; ++j) { |
|
209 |
try { |
|
210 |
test.doTest(i, j); |
|
211 |
} catch (ArrayIndexOutOfBoundsException e) { |
|
212 |
System.err.println(f + ": AIOOB for pair " + |
|
213 |
NAMES[i] + ", " + NAMES[j] + ": " + e.getMessage()); |
|
214 |
nAIOOB++; |
|
215 |
} catch (ImageCheckException e) { |
|
216 |
System.err.println(f + |
|
217 |
": image check failed for " + e.getMessage()); |
|
218 |
nChecksFailed++; |
|
219 |
} |
|
220 |
} |
|
221 |
} |
|
222 |
} |
|
223 |
||
224 |
if (nAIOOB > 0 || nChecksFailed > 0) { |
|
225 |
throw new RuntimeException("test failed: " + nAIOOB + " AIOOBs, " + |
|
226 |
nChecksFailed + " image check failures"); |
|
227 |
} |
|
228 |
} |
|
229 |
} |