Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/imageio/plugins/bmp/BMPSubsamplingTest.java
41153 views
1
/*
2
* Copyright (c) 2006, 2007, 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
* @bug 6299168 6399660 6519600
27
* @summary Test verifies that the subsampling usage does not causes color
28
* changes.
29
* @run main BMPSubsamplingTest
30
* @author andrew.brygin
31
*/
32
import java.awt.Color;
33
import java.awt.Graphics2D;
34
import java.awt.Transparency;
35
import java.awt.color.ColorSpace;
36
import java.awt.image.BufferedImage;
37
import java.awt.image.ColorModel;
38
import java.awt.image.ComponentColorModel;
39
import java.awt.image.DataBuffer;
40
import java.awt.image.DirectColorModel;
41
import java.awt.image.IndexColorModel;
42
import java.awt.image.Raster;
43
import java.awt.image.WritableRaster;
44
import java.io.File;
45
import java.io.IOException;
46
import javax.imageio.IIOImage;
47
import javax.imageio.ImageIO;
48
import javax.imageio.ImageTypeSpecifier;
49
import javax.imageio.ImageWriteParam;
50
import javax.imageio.ImageWriter;
51
import javax.imageio.stream.ImageOutputStream;
52
53
54
public class BMPSubsamplingTest {
55
private static final int TYPE_INT_GRB = 0x100;
56
private static final int TYPE_INT_GBR = 0x101;
57
private static final int TYPE_INT_RBG = 0x102;
58
private static final int TYPE_INT_BRG = 0x103;
59
private static final int TYPE_3BYTE_RGB = 0x104;
60
private static final int TYPE_3BYTE_GRB = 0x105;
61
private static final int TYPE_USHORT_555_GRB = 0x106;
62
private static final int TYPE_USHORT_555_BGR = 0x107;
63
private static final int TYPE_USHORT_565_BGR = 0x108;
64
private static final int TYPE_4BPP_INDEXED = 0x109;
65
66
private String format = "BMP";
67
68
private int[] img_types = new int[] {
69
BufferedImage.TYPE_INT_RGB,
70
BufferedImage.TYPE_INT_BGR,
71
TYPE_INT_GRB,
72
TYPE_INT_GBR,
73
TYPE_INT_RBG,
74
TYPE_INT_BRG,
75
BufferedImage.TYPE_USHORT_555_RGB,
76
BufferedImage.TYPE_USHORT_565_RGB,
77
TYPE_USHORT_555_GRB,
78
TYPE_USHORT_555_BGR,
79
TYPE_USHORT_565_BGR,
80
BufferedImage.TYPE_3BYTE_BGR,
81
TYPE_3BYTE_RGB,
82
TYPE_3BYTE_GRB,
83
BufferedImage.TYPE_BYTE_INDEXED,
84
TYPE_4BPP_INDEXED
85
};
86
Color[] colors = new Color[] { Color.red, Color.green, Color.blue };
87
88
private final int srcXSubsampling = 3;
89
private final int srcYSubsampling = 3;
90
91
int dx = 300;
92
int h = 300;
93
int w = dx * colors.length + srcXSubsampling;
94
95
96
97
public BMPSubsamplingTest() throws IOException {
98
ImageWriter writer =
99
ImageIO.getImageWritersByFormatName(format).next();
100
101
ImageWriteParam wparam = writer.getDefaultWriteParam();
102
wparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
103
String[] types = wparam.getCompressionTypes();
104
for (int t = 0; t < img_types.length; t++) {
105
int img_type = img_types[t];
106
System.out.println("Test for " + getImageTypeName(img_type));
107
BufferedImage image = getTestImage(img_type);
108
109
ImageTypeSpecifier specifier = new ImageTypeSpecifier(image);
110
111
if (!writer.getOriginatingProvider().canEncodeImage(specifier)) {
112
System.out.println("Writer does not support encoding this buffered image type.");
113
continue;
114
}
115
116
for(int i=0; i<types.length; i++) {
117
if ("BI_JPEG".equals(types[i])) {
118
// exclude BI_JPEG from automatic test
119
// due to color diffusion effect on the borders.
120
continue;
121
}
122
123
if (canEncodeImage(types[i], specifier, img_type)) {
124
System.out.println("compression type: " + types[i] +
125
" Supported for " + getImageTypeName(img_type));
126
} else {
127
System.out.println("compression type: " + types[i] +
128
" NOT Supported for " + getImageTypeName(img_type));
129
continue;
130
}
131
ImageWriteParam imageWriteParam = getImageWriteParam(writer, types[i]);
132
133
imageWriteParam.setSourceSubsampling(srcXSubsampling,
134
srcYSubsampling,
135
0, 0);
136
File outputFile = new File("subsampling_test_" +
137
getImageTypeName(img_type) + "__" +
138
types[i] + ".bmp");
139
ImageOutputStream ios =
140
ImageIO.createImageOutputStream(outputFile);
141
writer.setOutput(ios);
142
143
IIOImage iioImg = new IIOImage(image, null, null);
144
145
writer.write(null, iioImg, imageWriteParam);
146
147
ios.flush();
148
ios.close();
149
150
BufferedImage outputImage = ImageIO.read(outputFile);
151
checkTestImage(outputImage);
152
}
153
}
154
}
155
156
private ImageWriteParam getImageWriteParam(ImageWriter writer, String value) {
157
ImageWriteParam imageWriteParam = writer.getDefaultWriteParam();
158
imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
159
imageWriteParam.setCompressionType(value);
160
return imageWriteParam;
161
}
162
163
164
private boolean canEncodeImage(String compression,
165
ImageTypeSpecifier imgType, int rawType)
166
{
167
int biType = imgType.getBufferedImageType();
168
if ((!compression.equals("BI_BITFIELDS")) &&
169
((rawType == BufferedImage.TYPE_USHORT_565_RGB) ||
170
(rawType == TYPE_USHORT_565_BGR)))
171
{
172
return false;
173
}
174
175
int bpp = imgType.getColorModel().getPixelSize();
176
if (compression.equals("BI_RLE4") && bpp != 4) {
177
// only 4bpp images can be encoded as BI_RLE4
178
return false;
179
}
180
if (compression.equals("BI_RLE8") && bpp != 8) {
181
// only 8bpp images can be encoded as BI_RLE8
182
return false;
183
}
184
185
if (compression.equals("BI_PNG") &&
186
((rawType == TYPE_USHORT_555_GRB) ||
187
(rawType == TYPE_USHORT_555_BGR)))
188
{
189
return false;
190
}
191
192
return true;
193
}
194
195
private String getImageTypeName(int t) {
196
switch(t) {
197
case BufferedImage.TYPE_INT_RGB:
198
return "TYPE_INT_RGB";
199
case BufferedImage.TYPE_INT_BGR:
200
return "TYPE_INT_BGR";
201
case TYPE_INT_GRB:
202
return "TYPE_INT_GRB";
203
case TYPE_INT_GBR:
204
return "TYPE_INT_GBR";
205
case TYPE_INT_RBG:
206
return "TYPE_INT_RBG";
207
case TYPE_INT_BRG:
208
return "TYPE_INT_BRG";
209
case BufferedImage.TYPE_USHORT_555_RGB:
210
return "TYPE_USHORT_555_RGB";
211
case BufferedImage.TYPE_USHORT_565_RGB:
212
return "TYPE_USHORT_565_RGB";
213
case TYPE_USHORT_555_GRB:
214
return "TYPE_USHORT_555_GRB";
215
case TYPE_USHORT_555_BGR:
216
return "TYPE_USHORT_555_BGR";
217
case TYPE_USHORT_565_BGR:
218
return "TYPE_USHORT_565_BGR";
219
case BufferedImage.TYPE_3BYTE_BGR:
220
return "TYPE_3BYTE_BGR";
221
case TYPE_3BYTE_RGB:
222
return "TYPE_3BYTE_RGB";
223
case TYPE_3BYTE_GRB:
224
return "TYPE_3BYTE_GRB";
225
case BufferedImage.TYPE_BYTE_INDEXED:
226
return "TYPE_BYTE_INDEXED";
227
case TYPE_4BPP_INDEXED:
228
return "TYPE_BYTE_INDEXED (4bpp)";
229
default:
230
throw new IllegalArgumentException("Unknown image type: " + t);
231
}
232
}
233
234
private BufferedImage getTestImage(int type) {
235
BufferedImage dst = null;
236
ColorModel colorModel = null;
237
WritableRaster raster = null;
238
switch(type) {
239
case TYPE_INT_GRB:
240
colorModel = new DirectColorModel(24,
241
0x0000ff00,
242
0x00ff0000,
243
0x000000ff);
244
break;
245
case TYPE_INT_GBR:
246
colorModel = new DirectColorModel(24,
247
0x000000ff,
248
0x00ff0000,
249
0x0000ff00);
250
break;
251
case TYPE_INT_RBG:
252
colorModel = new DirectColorModel(24,
253
0x00ff0000,
254
0x000000ff,
255
0x0000ff00);
256
break;
257
case TYPE_INT_BRG:
258
colorModel = new DirectColorModel(24,
259
0x0000ff00,
260
0x000000ff,
261
0x00ff0000);
262
break;
263
case TYPE_3BYTE_RGB:
264
dst = create3ByteImage(new int[] {8, 8, 8},
265
new int[] {0, 1, 2});
266
break;
267
case TYPE_3BYTE_GRB:
268
dst = create3ByteImage(new int[] {8, 8, 8},
269
new int[] {1, 0, 2});
270
break;
271
case TYPE_USHORT_555_GRB:
272
colorModel = new DirectColorModel(16,
273
0x03E0,
274
0x7C00,
275
0x001F);
276
break;
277
case TYPE_USHORT_555_BGR:
278
colorModel = new DirectColorModel(16,
279
0x001F,
280
0x03E0,
281
0x7C00);
282
break;
283
case TYPE_USHORT_565_BGR:
284
colorModel = new DirectColorModel(16,
285
0x001F,
286
0x07E0,
287
0xf800);
288
break;
289
case TYPE_4BPP_INDEXED:
290
dst = createIndexImage(4);
291
break;
292
default:
293
dst = new BufferedImage(w, h, type);
294
}
295
if (dst == null) {
296
raster = colorModel.createCompatibleWritableRaster(w, h);
297
dst = new BufferedImage(colorModel, raster, false, null);
298
}
299
Graphics2D g = dst.createGraphics();
300
for (int i = 0; i < colors.length; i++) {
301
g.setColor(colors[i]);
302
g.fillRect(i * dx, 0, dx, h);
303
}
304
g.dispose();
305
return dst;
306
}
307
308
private BufferedImage createIndexImage(int bpp) {
309
// calculate palette size
310
int psize = (1 << bpp);
311
312
// prepare palette;
313
byte[] r = new byte[psize];
314
byte[] g = new byte[psize];
315
byte[] b = new byte[psize];
316
317
for (int i = 0; i < colors.length; i++) {
318
r[i] = (byte)(0xff & colors[i].getRed());
319
g[i] = (byte)(0xff & colors[i].getGreen());
320
b[i] = (byte)(0xff & colors[i].getBlue());
321
}
322
323
// now prepare appropriate index clor model
324
IndexColorModel icm = new IndexColorModel(bpp, psize, r, g, b);
325
326
return new BufferedImage(w, h, BufferedImage.TYPE_BYTE_INDEXED, icm);
327
}
328
329
private BufferedImage create3ByteImage(int[] nBits, int[] bOffs) {
330
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
331
ColorModel colorModel =
332
new ComponentColorModel(cs, nBits,
333
false, false,
334
Transparency.OPAQUE,
335
DataBuffer.TYPE_BYTE);
336
WritableRaster raster =
337
Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
338
w, h,
339
w*3, 3,
340
bOffs, null);
341
return new BufferedImage(colorModel, raster, false, null);
342
}
343
344
private void checkTestImage(BufferedImage dst) {
345
// NB: do not forget about subsampling factor.
346
int x = dx / (2 * srcXSubsampling);
347
int y = h / (2 * srcYSubsampling);
348
System.out.println("Check result: width=" + dst.getWidth() +
349
", height=" + dst.getHeight());
350
for (int i = 0; i < colors.length; i++) {
351
System.out.println("\tcheck at: " + x + ", " + y);
352
int srcRgb = colors[i].getRGB();
353
int dstRgb = dst.getRGB(x, y);
354
if (srcRgb != dstRgb) {
355
throw new RuntimeException("Test failed due to wrong dst color " +
356
Integer.toHexString(dstRgb) + " at " + x + "," + y +
357
"instead of " + Integer.toHexString(srcRgb));
358
}
359
x += dx / srcXSubsampling;
360
}
361
}
362
363
public static void main(String args[]) throws IOException {
364
BMPSubsamplingTest test = new BMPSubsamplingTest();
365
}
366
}
367
368