Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/imageio/plugins/gif/EncodeSubImageTest.java
41154 views
1
/*
2
* Copyright (c) 2009, 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 6795544
27
*
28
* @summary Test verifes that Image I/O gif writer correctly handles
29
* buffered images based on translated reasters (typically
30
* produced by getSubImage() method).
31
*
32
* @run main EncodeSubImageTest gif
33
*/
34
35
import java.awt.Color;
36
import java.awt.Graphics;
37
import java.awt.image.BufferedImage;
38
import java.awt.image.Raster;
39
import java.io.File;
40
import java.io.IOException;
41
import javax.imageio.IIOImage;
42
import javax.imageio.ImageIO;
43
import javax.imageio.ImageWriteParam;
44
import javax.imageio.ImageWriter;
45
import javax.imageio.stream.ImageOutputStream;
46
47
public class EncodeSubImageTest {
48
private static String format = "gif";
49
private static ImageWriter writer;
50
private static String file_suffix;
51
private static final int subSampleX = 2;
52
private static final int subSampleY = 2;
53
54
public static void main(String[] args) throws IOException {
55
if (args.length > 0) {
56
format = args[0];
57
}
58
59
writer = ImageIO.getImageWritersByFormatName(format).next();
60
61
file_suffix =writer.getOriginatingProvider().getFileSuffixes()[0];
62
63
BufferedImage src = createTestImage();
64
EncodeSubImageTest m1 = new EncodeSubImageTest(src);
65
m1.doTest("test_src");
66
67
BufferedImage sub = src.getSubimage(subImageOffset, subImageOffset,
68
src.getWidth() - 2 * subImageOffset,
69
src.getHeight() - 2 * subImageOffset);
70
EncodeSubImageTest m2 = new EncodeSubImageTest(sub);
71
m2.doTest("test_sub");
72
}
73
74
BufferedImage img;
75
76
public EncodeSubImageTest(BufferedImage img) {
77
this.img = img;
78
}
79
80
public void doTest(String prefix) throws IOException {
81
System.out.println(prefix);
82
File f = new File(prefix + file_suffix);
83
write(f, false);
84
verify(f, false);
85
86
System.out.println(prefix + "_subsampled");
87
f = new File(prefix + "_subsampled");
88
write(f, true);
89
verify(f, true);
90
91
System.out.println(prefix + ": Test PASSED.");
92
}
93
94
private static final int subImageOffset = 10;
95
96
private void verify(File f, boolean isSubsampled) {
97
BufferedImage dst = null;
98
try {
99
dst = ImageIO.read(f);
100
} catch (IOException e) {
101
throw new RuntimeException("Test FAILED: can't readin test image " +
102
f.getAbsolutePath(), e);
103
}
104
if (dst == null) {
105
throw new RuntimeException("Test FAILED: no dst image available.");
106
}
107
108
checkPixel(dst, 0, 0, isSubsampled);
109
110
checkPixel(dst, img.getWidth() / 2, img.getHeight() / 2, isSubsampled);
111
}
112
113
private void checkPixel(BufferedImage dst, int x, int y,
114
boolean isSubsampled)
115
{
116
int dx = isSubsampled ? x / subSampleX : x;
117
int dy = isSubsampled ? y / subSampleY : y;
118
int src_rgb = img.getRGB(x, y);
119
System.out.printf("src_rgb: %x\n", src_rgb);
120
121
int dst_rgb = dst.getRGB(dx, dy);
122
System.out.printf("dst_rgb: %x\n", dst_rgb);
123
124
if (src_rgb != dst_rgb) {
125
throw new RuntimeException("Test FAILED: invalid color in dst");
126
}
127
}
128
129
private static BufferedImage createTestImage() {
130
int w = 100;
131
int h = 100;
132
133
BufferedImage src = new BufferedImage(w, h,
134
BufferedImage.TYPE_BYTE_INDEXED);
135
Graphics g = src.createGraphics();
136
g.setColor(Color.red);
137
g.fillRect(0, 0, w, h);
138
g.setColor(Color.green);
139
g.fillRect(subImageOffset, subImageOffset,
140
w - 2 * subImageOffset, h - 2* subImageOffset);
141
g.setColor(Color.blue);
142
g.fillRect(2 * subImageOffset, 2 * subImageOffset,
143
w - 4 * subImageOffset, h - 4 * subImageOffset);
144
g.dispose();
145
146
return src;
147
}
148
149
private void write(File f, boolean subsample) throws IOException {
150
ImageOutputStream ios = ImageIO.createImageOutputStream(f);
151
152
writer.setOutput(ios);
153
ImageWriteParam p = writer.getDefaultWriteParam();
154
if (subsample) {
155
p.setSourceSubsampling(subSampleX, subSampleY, 0, 0);
156
}
157
writer.write(null, new IIOImage(img, null, null), p);
158
ios.close();
159
writer.reset();
160
}
161
}
162
163