Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/imageio/plugins/bmp/BMPCompressionTest.java
38853 views
1
/*
2
* Copyright (c) 2003, 2017, 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 4641872
27
* @summary Tests writing compression modes of BMP plugin
28
*/
29
30
import java.awt.Color;
31
import java.awt.Dimension;
32
import java.awt.Graphics;
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.PixelInterleavedSampleModel;
43
import java.awt.image.Raster;
44
import java.awt.image.SampleModel;
45
import java.awt.image.SinglePixelPackedSampleModel;
46
import java.awt.image.WritableRaster;
47
import java.io.ByteArrayInputStream;
48
import java.io.ByteArrayOutputStream;
49
import java.io.File;
50
import java.io.FileOutputStream;
51
import java.io.IOException;
52
import java.util.Arrays;
53
import java.util.Iterator;
54
import java.util.LinkedList;
55
import java.util.List;
56
57
import javax.imageio.IIOImage;
58
import javax.imageio.ImageIO;
59
import javax.imageio.ImageReader;
60
import javax.imageio.ImageTypeSpecifier;
61
import javax.imageio.ImageWriteParam;
62
import javax.imageio.ImageWriter;
63
import javax.imageio.metadata.IIOMetadata;
64
import javax.imageio.plugins.bmp.BMPImageWriteParam;
65
import javax.imageio.stream.ImageOutputStream;
66
import javax.swing.JComponent;
67
import javax.swing.JFrame;
68
69
import com.sun.imageio.plugins.bmp.BMPMetadata;
70
71
public class BMPCompressionTest {
72
73
static final String format = "BMP";
74
75
public static void main(String[] args) {
76
77
ImageWriter iw = null;
78
Iterator writers = ImageIO.getImageWritersByFormatName(format);
79
if (!writers.hasNext()) {
80
throw new RuntimeException("No available Image writer for "+format);
81
}
82
iw = (ImageWriter)writers.next();
83
84
85
Iterator tests = Test.createTestSet(iw);
86
87
while(tests.hasNext()) {
88
89
Test t = (Test)tests.next();
90
System.out.println(t.getDescription());
91
t.doTest();
92
}
93
94
}
95
96
97
static class Test {
98
static ImageWriter iw;
99
private BufferedImage img;
100
private String description;
101
private BMPImageWriteParam param;
102
private IIOMetadata meta;
103
104
105
public static Iterator createTestSet(ImageWriter w) {
106
List l = new LinkedList();
107
108
Test.iw = w;
109
110
// variate compression types
111
BMPImageWriteParam param = (BMPImageWriteParam)iw.getDefaultWriteParam();
112
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
113
param.setCompressionType("BI_RGB");
114
if (param.canWriteCompressed()) {
115
String[] cTypes = param.getCompressionTypes();
116
String[] cDescr = param.getCompressionQualityDescriptions();
117
float[] cValues = param.getCompressionQualityValues();
118
119
if (cDescr == null) {
120
System.out.println("There are no compression quality description!");
121
} else {
122
for(int i=0; i<cDescr.length; i++) {
123
System.out.println("Quality[" + i + "]=\""+cDescr[i]+"\"");
124
}
125
}
126
if (cValues == null) {
127
System.out.println("There are no compression quality values!");
128
} else {
129
for(int i=0; i<cValues.length; i++) {
130
System.out.println("Value["+i+"]=\""+cValues[i]+"\"");
131
}
132
}
133
134
for(int i=0; i<cTypes.length; i++) {
135
String compressionType = cTypes[i];
136
BufferedImage img = null;
137
138
int type = BufferedImage.TYPE_INT_BGR;
139
try {
140
img = createTestImage(type);
141
if (compressionType.equals("BI_RLE8")) {
142
img = createTestImage2(8, DataBuffer.TYPE_BYTE);
143
} else if (compressionType.equals("BI_RLE4")) {
144
img = createTestImage3(4, DataBuffer.TYPE_BYTE);
145
} else if (compressionType.equals("BI_BITFIELDS")) {
146
img = createTestImage4(32);
147
}
148
149
} catch (IOException ex) {
150
throw new RuntimeException("Unable to create test image");
151
}
152
BMPImageWriteParam p = (BMPImageWriteParam)iw.getDefaultWriteParam();
153
System.out.println("Current compression type is \""+cTypes[i]+"\"");
154
p.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
155
p.setCompressionType(compressionType);
156
157
IIOMetadata md = iw.getDefaultImageMetadata(new ImageTypeSpecifier(img), p);
158
159
l.add( new Test(p, md, img));
160
}
161
}
162
// }
163
return l.iterator();
164
165
}
166
167
private Test(BMPImageWriteParam p, IIOMetadata md, BufferedImage i) {
168
param = p;
169
meta = md;
170
img = i;
171
172
173
description = "Compression type is " + p.getCompressionType();
174
}
175
176
public String getDescription() {
177
return description;
178
}
179
180
public void doTest() {
181
try {
182
System.out.println(this.getDescription());
183
if (param.getCompressionMode() != ImageWriteParam.MODE_EXPLICIT) {
184
System.out.println("Warning: compression mode is not MODE_EXPLICIT");
185
}
186
// change metadata according to ImageWriteParam
187
IIOMetadata new_meta = iw.convertImageMetadata(meta, new ImageTypeSpecifier(img), param);
188
189
IIOImage iio_img = new IIOImage(img, null, new_meta);
190
191
ByteArrayOutputStream baos = new ByteArrayOutputStream();
192
ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
193
iw.setOutput(ios);
194
System.out.print("write image...");
195
System.out.println("Current compression Type is \""+param.getCompressionType()+"\"");
196
iw.write(new_meta, iio_img, param);
197
//iw.write(iio_img);
198
System.out.println("OK");
199
System.out.print("read image ... ");
200
ios.flush();
201
202
byte[] ba_image = baos.toByteArray();
203
204
System.out.println("Array length=" + ba_image.length);
205
FileOutputStream fos = new FileOutputStream(new File(param.getCompressionType()+".bmp"));
206
fos.write(ba_image);
207
fos.flush();
208
fos = null;
209
ByteArrayInputStream bais = new ByteArrayInputStream(ba_image);
210
211
ImageReader ir = ImageIO.getImageReader(iw);
212
ir.setInput(ImageIO.createImageInputStream(bais));
213
214
BufferedImage res = ir.read(0);
215
System.out.println("OK");
216
217
if (!param.getCompressionType().equals("BI_JPEG")) {
218
System.out.print("compare images ... ");
219
boolean r = compare(img,res);
220
System.out.println(r?"OK":"FAILED");
221
if (!r) {
222
throw new RuntimeException("Compared images are not equals. Test failed.");
223
}
224
}
225
226
227
BMPMetadata mdata = (BMPMetadata)ir.getImageMetadata(0);
228
229
if (!param.getCompressionType().equals(param.getCompressionTypes()[mdata.compression])) {
230
throw new RuntimeException("Different compression value");
231
}
232
233
} catch (Exception ex) {
234
ex.printStackTrace();
235
throw new RuntimeException("Unexpected exception: " + ex);
236
}
237
238
}
239
240
private boolean compare(final BufferedImage in, final BufferedImage out) {
241
242
final int width = in.getWidth();
243
int height = in.getHeight();
244
if (out.getWidth() != width || out.getHeight() != height) {
245
throw new RuntimeException("Dimensions changed!");
246
}
247
248
Raster oldras = in.getRaster();
249
ColorModel oldcm = in.getColorModel();
250
Raster newras = out.getRaster();
251
ColorModel newcm = out.getColorModel();
252
253
for (int j = 0; j < height; j++) {
254
for (int i = 0; i < width; i++) {
255
Object oldpixel = oldras.getDataElements(i, j, null);
256
int oldrgb = oldcm.getRGB(oldpixel);
257
int oldalpha = oldcm.getAlpha(oldpixel);
258
259
Object newpixel = newras.getDataElements(i, j, null);
260
int newrgb = newcm.getRGB(newpixel);
261
int newalpha = newcm.getAlpha(newpixel);
262
263
if (newrgb != oldrgb ||
264
newalpha != oldalpha) {
265
// showDiff(in, out);
266
throw new RuntimeException("Pixels differ at " + i +
267
", " + j + " new = " + Integer.toHexString(newrgb) + " old = " + Integer.toHexString(oldrgb));
268
}
269
}
270
}
271
return true;
272
}
273
274
private static BufferedImage createTestImage2(int nbits, int transfertype) {
275
final int colorShift = 2;
276
int SIZE = 256;
277
BufferedImage image = null;
278
279
ColorSpace colorSpace =
280
ColorSpace.getInstance(ColorSpace.CS_GRAY);
281
ColorModel colorModel =
282
new ComponentColorModel(colorSpace,
283
new int[] {nbits},
284
false,
285
false,
286
Transparency.OPAQUE,
287
transfertype);
288
289
SampleModel sampleModel =
290
new PixelInterleavedSampleModel(transfertype,
291
SIZE,
292
SIZE,
293
1,
294
SIZE,
295
new int[] {0});
296
297
image =
298
new BufferedImage(colorModel,
299
Raster.createWritableRaster(sampleModel, null),
300
false, null);
301
WritableRaster raster = image.getWritableTile(0, 0);
302
int[] samples = raster.getSamples(0, 0, SIZE, SIZE, 0, (int[])null);
303
int off = 0;
304
int[] row = new int[SIZE];
305
for(int i = 0; i < SIZE; i++) {
306
Arrays.fill(row, i << colorShift);
307
System.arraycopy(row, 0, samples, off, SIZE);
308
off += SIZE;
309
}
310
raster.setSamples(0, 0, SIZE, SIZE, 0, samples);
311
312
return image;
313
}
314
315
316
private static BufferedImage createTestImage3(int nbits, int transfertype) {
317
final int colorShift = 2;
318
int SIZE = 256;
319
BufferedImage image = null;
320
321
ColorSpace colorSpace =
322
ColorSpace.getInstance(ColorSpace.CS_sRGB);
323
ColorModel colorModel =
324
new IndexColorModel(nbits,
325
4,
326
new byte[] { (byte)255, 0, 0, (byte)255},
327
new byte[] { 0, (byte)255, 0, (byte)255},
328
new byte[] { 0, 0, (byte)255, (byte)255});
329
330
SampleModel sampleModel =
331
new PixelInterleavedSampleModel(transfertype,
332
SIZE,
333
SIZE,
334
1,
335
SIZE,
336
new int[] {0});
337
338
image =
339
new BufferedImage(colorModel,
340
Raster.createWritableRaster(sampleModel, null),
341
342
false, null);
343
344
Graphics2D g = image.createGraphics();
345
g.setColor(Color.white);
346
g.fillRect(0,0, SIZE, SIZE);
347
g.setColor(Color.red);
348
g.fillOval(10, 10, SIZE -20, SIZE-20);
349
350
return image;
351
}
352
353
private static BufferedImage createTestImage4(int nbits) {
354
int SIZE = 10;
355
356
357
BufferedImage image = null;
358
359
ColorSpace colorSpace =
360
ColorSpace.getInstance(ColorSpace.CS_sRGB);
361
ColorModel colorModel =
362
new DirectColorModel(colorSpace,
363
nbits, 0xff0000, 0x00ff00, 0x0000ff, 0x000000, false, DataBuffer.TYPE_INT);
364
365
SampleModel sampleModel =
366
new SinglePixelPackedSampleModel(DataBuffer.TYPE_INT,
367
SIZE,
368
SIZE,
369
new int[] { 0xff0000, 0x00ff00, 0x0000ff} );
370
371
372
image =
373
new BufferedImage(colorModel,
374
Raster.createWritableRaster(sampleModel, null),
375
376
false, null);
377
378
Graphics2D g = image.createGraphics();
379
g.setColor(Color.red);
380
g.fillRect(0,0, SIZE, SIZE);
381
g.setColor(Color.green);
382
//g.fillOval(10, 10, SIZE -20, SIZE-20);
383
g.drawLine(7, 0, 7, SIZE);
384
g.setColor(Color.blue);
385
g.drawLine(1, 0, 1, SIZE);
386
g.setColor(Color.white);
387
g.drawLine(3, 0, 3, SIZE);
388
g.setColor(Color.yellow);
389
g.drawLine(5, 0, 5, SIZE);
390
return image;
391
}
392
393
private static BufferedImage createTestImage(int type)
394
throws IOException {
395
396
int w = 200;
397
int h = 200;
398
BufferedImage b = new BufferedImage(w, h, type);
399
Graphics2D g = b.createGraphics();
400
g.setColor(Color.white);
401
g.fillRect(0,0, w, h);
402
g.setColor(Color.black);
403
g.fillOval(10, 10, w -20, h-20);
404
405
return b;
406
}
407
408
409
}
410
411
private static void showDiff(final BufferedImage in,
412
final BufferedImage out) {
413
final int width = in.getWidth();
414
final int height = in.getHeight();
415
416
JFrame f = new JFrame("");
417
f.getContentPane().add( new JComponent() {
418
public Dimension getPreferredSize() {
419
return new Dimension(2*width+2, height);
420
}
421
public void paintComponent(Graphics g) {
422
g.setColor(Color.black);
423
g.drawImage(in, 0,0, null);
424
425
g.drawImage(out, width+2, 0, null);
426
}
427
});
428
f.pack();
429
f.setVisible(true);
430
}
431
432
}
433
434