Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/javax/imageio/plugins/jpeg/CMYK/CMYKJPEGTest.java
66646 views
1
/*
2
* Copyright (c) 2022, 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
*
26
* This test verifies that using the built-in ImageI/O JPEG plugin that JPEG images
27
* that are in a CMYK ColorSpace can be read into a BufferedImage using the convemience
28
* APIS and that and the colours are properly interpreted.
29
* Since there is no standard JDK CMYK ColorSpace, this requires that either the image
30
* contain an ICC_Profile which can be used by the plugin to create an ICC_ColorSpace
31
* or that the plugin provides a suitable default CMYK ColorSpace instance by some other means.
32
*
33
* The test further verifies that the resultant BufferedImage will be re-written as a CMYK
34
* BufferedImage. It can do this so long as the BufferedImage has that CMYK ColorSpace
35
* used by its ColorModel.
36
*
37
* The verification requires re-reading again the re-written image and checking the
38
* re-read image still has a CMYK ColorSpace and the same colours.
39
*
40
* Optionally - not for use in the test harness - the test can be passed a parameter
41
* -display to create a UI which renders all the images the test is
42
* verifying so it can be manually verified
43
*/
44
45
/*
46
* @test
47
* @bug 8274735
48
* @summary Verify CMYK JPEGs can be read and written
49
*/
50
51
import java.awt.Color;
52
import static java.awt.Color.*;
53
import java.awt.Dimension;
54
import java.awt.Graphics;
55
import java.awt.GridLayout;
56
import java.awt.image.BufferedImage;
57
import java.awt.image.ColorModel;
58
import java.io.ByteArrayInputStream;
59
import java.io.ByteArrayOutputStream;
60
import java.io.File;
61
import java.io.IOException;
62
import javax.imageio.ImageIO;
63
import javax.swing.JComponent;
64
import javax.swing.JFrame;
65
import javax.swing.JLabel;
66
import javax.swing.JPanel;
67
import javax.swing.SwingUtilities;
68
69
public class CMYKJPEGTest {
70
71
static String[] fileNames = {
72
"black_cmyk.jpg",
73
"white_cmyk.jpg",
74
"gray_cmyk.jpg",
75
"red_cmyk.jpg",
76
"blue_cmyk.jpg",
77
"green_cmyk.jpg",
78
"cyan_cmyk.jpg",
79
"magenta_cmyk.jpg",
80
"yellow_cmyk.jpg",
81
};
82
83
static Color[] colors = {
84
black,
85
white,
86
gray,
87
red,
88
blue,
89
green,
90
cyan,
91
magenta,
92
yellow,
93
};
94
95
static boolean display;
96
97
static BufferedImage[] readImages;
98
static BufferedImage[] writtenImages;
99
static int imageIndex = 0;
100
101
public static void main(String[] args) throws Exception {
102
103
if (args.length > 0) {
104
display = "-display".equals(args[0]);
105
}
106
107
String sep = System.getProperty("file.separator");
108
String dir = System.getProperty("test.src", ".");
109
String prefix = dir+sep;
110
111
readImages = new BufferedImage[fileNames.length];
112
writtenImages = new BufferedImage[fileNames.length];
113
114
for (String fileName : fileNames) {
115
String color = fileName.replace("_cmyk.jpg", "");
116
test(prefix+fileName, color, imageIndex++);
117
}
118
if (display) {
119
SwingUtilities.invokeAndWait(() -> createUI());
120
}
121
}
122
123
static void test(String fileName, String color, int index)
124
throws IOException {
125
126
readImages[index] = ImageIO.read(new File(fileName));
127
verify(readImages[index], color, colors[index]);
128
ByteArrayOutputStream baos = new ByteArrayOutputStream();
129
ImageIO.write(readImages[index], "jpg", baos);
130
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
131
writtenImages[index] = ImageIO.read(bais);
132
verify(writtenImages[index], color, colors[index]);
133
}
134
135
static void verify(BufferedImage img, String colorName, Color c) {
136
ColorModel cm = img.getColorModel();
137
int tc = cm.getNumComponents();
138
int cc = cm.getNumColorComponents();
139
if (cc != 4 || tc != 4) {
140
throw new RuntimeException("Unexpected num comp for " + img);
141
}
142
143
int rgb = img.getRGB(0,0);
144
int c_red = c.getRed();
145
int c_green = c.getGreen();
146
int c_blue = c.getBlue();
147
int i_red = (rgb & 0x0ff0000) >> 16;
148
int i_green = (rgb & 0x000ff00) >> 8;
149
int i_blue = (rgb & 0x00000ff);
150
int tol = 16;
151
if ((Math.abs(i_red - c_red) > tol) ||
152
(Math.abs(i_green - c_green) > tol) ||
153
(Math.abs(i_blue - c_blue) > tol))
154
{
155
System.err.println("red="+i_red+" green="+i_green+" blue="+i_blue);
156
throw new RuntimeException("Too different " + img + " " + colorName + " " + c);
157
}
158
}
159
160
static class ImageComp extends JComponent {
161
162
BufferedImage img;
163
164
ImageComp(BufferedImage img) {
165
this.img = img;
166
}
167
168
public Dimension getPreferredSize() {
169
return new Dimension(img.getWidth(), img.getHeight());
170
}
171
172
public Dimension getMinimumSize() {
173
return getPreferredSize();
174
}
175
176
public void paintComponent(Graphics g) {
177
super.paintComponent(g);
178
g.drawImage(img, 0, 0, null);
179
}
180
}
181
182
static void createUI() {
183
JFrame f = new JFrame("CMYK JPEG Test");
184
JPanel p = new JPanel();
185
p.setLayout(new GridLayout(3, colors.length, 10, 10));
186
for (String s : fileNames) {
187
p.add(new JLabel(s.replace("_cmyk.jpg", "")));
188
}
189
for (BufferedImage i : readImages) {
190
p.add(new ImageComp(i));
191
}
192
for (BufferedImage i : writtenImages) {
193
p.add(new ImageComp(i));
194
}
195
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
196
f.add(p);
197
f.pack();
198
f.setVisible(true);
199
}
200
}
201
202