Skip to content

Commit abdbbe3

Browse files
committed
8254370: Update the classes in the java.awt.color package
Reviewed-by: prr
1 parent 8ca59c9 commit abdbbe3

File tree

6 files changed

+312
-105
lines changed

6 files changed

+312
-105
lines changed

src/java.desktop/share/classes/java/awt/color/ColorSpace.java

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,12 @@ public abstract class ColorSpace implements Serializable {
102102
/**
103103
* One of the {@code ColorSpace} type constants.
104104
*/
105-
private int type;
105+
private final int type;
106106

107107
/**
108108
* The number of components in the color space.
109109
*/
110-
private int numComponents;
110+
private final int numComponents;
111111
private transient String [] compName = null;
112112

113113
// Cache of singletons for the predefined color spaces.
@@ -242,6 +242,7 @@ public abstract class ColorSpace implements Serializable {
242242
*/
243243
@Native public static final int TYPE_FCLR = 25;
244244

245+
245246
/**
246247
* The sRGB color space defined at
247248
* <a href="http://www.w3.org/pub/WWW/Graphics/Color/sRGB.html">
@@ -275,11 +276,11 @@ public abstract class ColorSpace implements Serializable {
275276
* number of components.
276277
*
277278
* @param type one of the {@code ColorSpace} type constants
278-
* @param numcomponents the number of components in the color space
279+
* @param numComponents the number of components in the color space
279280
*/
280-
protected ColorSpace(int type, int numcomponents) {
281+
protected ColorSpace(int type, int numComponents) {
281282
this.type = type;
282-
this.numComponents = numcomponents;
283+
this.numComponents = numComponents;
283284
}
284285

285286
/**
@@ -441,8 +442,8 @@ public boolean isCS_sRGB () {
441442
* {@link ICC_ColorSpace#toCIEXYZ(float[]) toCIEXYZ} method of
442443
* {@code ICC_ColorSpace} for further information.
443444
*
444-
* @param colorvalue a float array with length of at least the number of
445-
* components in this {@code ColorSpace}
445+
* @param colorvalue a float array with length of at least the number of
446+
* components in this {@code ColorSpace}
446447
* @return a float array of length 3
447448
* @throws ArrayIndexOutOfBoundsException if array length is not at least
448449
* the number of components in this {@code ColorSpace}.
@@ -508,11 +509,7 @@ public int getNumComponents() {
508509
*/
509510
public String getName (int idx) {
510511
/* REMIND - handle common cases here */
511-
if ((idx < 0) || (idx > numComponents - 1)) {
512-
throw new IllegalArgumentException(
513-
"Component index out of range: " + idx);
514-
}
515-
512+
rangeCheck(idx);
516513
if (compName == null) {
517514
switch (type) {
518515
case ColorSpace.TYPE_XYZ:
@@ -573,10 +570,7 @@ public String getName (int idx) {
573570
* @since 1.4
574571
*/
575572
public float getMinValue(int component) {
576-
if ((component < 0) || (component > numComponents - 1)) {
577-
throw new IllegalArgumentException(
578-
"Component index out of range: " + component);
579-
}
573+
rangeCheck(component);
580574
return 0.0f;
581575
}
582576

@@ -592,17 +586,21 @@ public float getMinValue(int component) {
592586
* @since 1.4
593587
*/
594588
public float getMaxValue(int component) {
595-
if ((component < 0) || (component > numComponents - 1)) {
596-
throw new IllegalArgumentException(
597-
"Component index out of range: " + component);
598-
}
589+
rangeCheck(component);
599590
return 1.0f;
600591
}
601592

602-
/*
603-
* Returns {@code true} if {@code cspace} is the XYZspace.
593+
/**
594+
* Checks that {@code component} is in range of the number of components.
595+
*
596+
* @param component the component index
597+
* @throws IllegalArgumentException if component is less than 0 or greater
598+
* than {@code numComponents - 1}
604599
*/
605-
static boolean isCS_CIEXYZ(ColorSpace cspace) {
606-
return (cspace == XYZspace);
600+
final void rangeCheck(int component) {
601+
if (component < 0 || component > getNumComponents() - 1) {
602+
throw new IllegalArgumentException(
603+
"Component index out of range: " + component);
604+
}
607605
}
608606
}

src/java.desktop/share/classes/java/awt/color/ICC_ColorSpace.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -569,10 +569,7 @@ public float[] fromCIEXYZ(float[] colorvalue) {
569569
* @since 1.4
570570
*/
571571
public float getMinValue(int component) {
572-
if ((component < 0) || (component > this.getNumComponents() - 1)) {
573-
throw new IllegalArgumentException(
574-
"Component index out of range: " + component);
575-
}
572+
rangeCheck(component);
576573
return minVal[component];
577574
}
578575

@@ -595,10 +592,7 @@ public float getMinValue(int component) {
595592
* @since 1.4
596593
*/
597594
public float getMaxValue(int component) {
598-
if ((component < 0) || (component > this.getNumComponents() - 1)) {
599-
throw new IllegalArgumentException(
600-
"Component index out of range: " + component);
601-
}
595+
rangeCheck(component);
602596
return maxVal[component];
603597
}
604598

src/java.desktop/share/classes/java/awt/color/ICC_ProfileGray.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,7 @@ public float[] getMediaWhitePoint() {
115115
* single gamma value
116116
*/
117117
public float getGamma() {
118-
float theGamma;
119-
120-
theGamma = super.getGamma(ICC_Profile.icSigGrayTRCTag);
121-
return theGamma;
118+
return getGamma(ICC_Profile.icSigGrayTRCTag);
122119
}
123120

124121
/**
@@ -140,10 +137,6 @@ public float getGamma() {
140137
* table
141138
*/
142139
public short[] getTRC() {
143-
short[] theTRC;
144-
145-
theTRC = super.getTRC(ICC_Profile.icSigGrayTRCTag);
146-
return theTRC;
140+
return getTRC(ICC_Profile.icSigGrayTRCTag);
147141
}
148-
149142
}

src/java.desktop/share/classes/java/awt/color/ICC_ProfileRGB.java

Lines changed: 34 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public class ICC_ProfileRGB extends ICC_Profile {
103103
/**
104104
* Constructs an new {@code ICC_ProfileRGB} from a CMM ID.
105105
*
106-
* @param p the CMM ID for the profile.
106+
* @param p the CMM ID for the profile.
107107
*/
108108
ICC_ProfileRGB(Profile p) {
109109
super(p);
@@ -113,7 +113,7 @@ public class ICC_ProfileRGB extends ICC_Profile {
113113
* Constructs a new {@code ICC_ProfileRGB} from a
114114
* {@code ProfileDeferralInfo} object.
115115
*
116-
* @param pdi
116+
* @param pdi
117117
*/
118118
ICC_ProfileRGB(ProfileDeferralInfo pdi) {
119119
super(pdi);
@@ -143,22 +143,12 @@ public float[] getMediaWhitePoint() {
143143
* {@code greenColorantTag}, and {@code blueColorantTag}
144144
*/
145145
public float[][] getMatrix() {
146-
float[][] theMatrix = new float[3][3];
147-
float[] tmpMatrix;
148-
149-
tmpMatrix = getXYZTag(ICC_Profile.icSigRedColorantTag);
150-
theMatrix[0][0] = tmpMatrix[0];
151-
theMatrix[1][0] = tmpMatrix[1];
152-
theMatrix[2][0] = tmpMatrix[2];
153-
tmpMatrix = getXYZTag(ICC_Profile.icSigGreenColorantTag);
154-
theMatrix[0][1] = tmpMatrix[0];
155-
theMatrix[1][1] = tmpMatrix[1];
156-
theMatrix[2][1] = tmpMatrix[2];
157-
tmpMatrix = getXYZTag(ICC_Profile.icSigBlueColorantTag);
158-
theMatrix[0][2] = tmpMatrix[0];
159-
theMatrix[1][2] = tmpMatrix[1];
160-
theMatrix[2][2] = tmpMatrix[2];
161-
return theMatrix;
146+
float[] red = getXYZTag(ICC_Profile.icSigRedColorantTag);
147+
float[] green = getXYZTag(ICC_Profile.icSigGreenColorantTag);
148+
float[] blue = getXYZTag(ICC_Profile.icSigBlueColorantTag);
149+
return new float[][]{{red[0], green[0], blue[0]},
150+
{red[1], green[1], blue[1]},
151+
{red[2], green[2], blue[2]}};
162152
}
163153

164154
/**
@@ -181,33 +171,14 @@ public float[][] getMatrix() {
181171
* @param component the {@code ICC_ProfileRGB} constant that represents the
182172
* component whose TRC you want to retrieve
183173
* @return the gamma value as a float
174+
* @throws IllegalArgumentException if the component is not
175+
* {@code REDCOMPONENT}, {@code GREENCOMPONENT}, or
176+
* {@code BLUECOMPONENT}
184177
* @throws ProfileDataException if the profile does not specify the
185178
* corresponding TRC as a single gamma value
186179
*/
187180
public float getGamma(int component) {
188-
float theGamma;
189-
int theSignature;
190-
191-
switch (component) {
192-
case REDCOMPONENT:
193-
theSignature = ICC_Profile.icSigRedTRCTag;
194-
break;
195-
196-
case GREENCOMPONENT:
197-
theSignature = ICC_Profile.icSigGreenTRCTag;
198-
break;
199-
200-
case BLUECOMPONENT:
201-
theSignature = ICC_Profile.icSigBlueTRCTag;
202-
break;
203-
204-
default:
205-
throw new IllegalArgumentException("Must be Red, Green, or Blue");
206-
}
207-
208-
theGamma = super.getGamma(theSignature);
209-
210-
return theGamma;
181+
return super.getGamma(toTag(component));
211182
}
212183

213184
/**
@@ -232,33 +203,32 @@ public float getGamma(int component) {
232203
* component whose TRC you want to retrieve: {@code REDCOMPONENT},
233204
* {@code GREENCOMPONENT}, or {@code BLUECOMPONENT}
234205
* @return a short array representing the TRC
206+
* @throws IllegalArgumentException if the component is not
207+
* {@code REDCOMPONENT}, {@code GREENCOMPONENT}, or
208+
* {@code BLUECOMPONENT}
235209
* @throws ProfileDataException if the profile does not specify the
236210
* corresponding TRC as a table
237211
*/
238212
public short[] getTRC(int component) {
239-
short[] theTRC;
240-
int theSignature;
241-
242-
switch (component) {
243-
case REDCOMPONENT:
244-
theSignature = ICC_Profile.icSigRedTRCTag;
245-
break;
246-
247-
case GREENCOMPONENT:
248-
theSignature = ICC_Profile.icSigGreenTRCTag;
249-
break;
250-
251-
case BLUECOMPONENT:
252-
theSignature = ICC_Profile.icSigBlueTRCTag;
253-
break;
254-
255-
default:
256-
throw new IllegalArgumentException("Must be Red, Green, or Blue");
257-
}
258-
259-
theTRC = super.getTRC(theSignature);
260-
261-
return theTRC;
213+
return super.getTRC(toTag(component));
262214
}
263215

216+
/**
217+
* Converts the {@code ICC_ProfileRGB} constant to the appropriate tag.
218+
*
219+
* @param component the {@code ICC_ProfileRGB} constant
220+
* @return the tag signature
221+
* @throws IllegalArgumentException if the component is not
222+
* {@code REDCOMPONENT}, {@code GREENCOMPONENT}, or
223+
* {@code BLUECOMPONENT}
224+
*/
225+
private static int toTag(int component) {
226+
return switch (component) {
227+
case REDCOMPONENT -> ICC_Profile.icSigRedTRCTag;
228+
case GREENCOMPONENT -> ICC_Profile.icSigGreenTRCTag;
229+
case BLUECOMPONENT -> ICC_Profile.icSigBlueTRCTag;
230+
default -> throw new IllegalArgumentException(
231+
"Must be Red, Green, or Blue");
232+
};
233+
}
264234
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright (c) 2020, 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+
import java.awt.color.ColorSpace;
25+
import java.awt.color.ICC_Profile;
26+
import java.awt.color.ICC_ProfileGray;
27+
import java.awt.color.ProfileDataException;
28+
29+
/**
30+
* @test
31+
* @bug 8254370
32+
* @summary Tests basic ICC_ProfileGray functionality
33+
*/
34+
public final class ICC_ProfileGrayTest {
35+
36+
public static void main(String[] args) throws Exception {
37+
ICC_Profile csProfile = ICC_Profile.getInstance(ColorSpace.CS_GRAY);
38+
ICC_Profile dataProfile = ICC_Profile.getInstance(csProfile.getData());
39+
ICC_Profile stringProfile = ICC_Profile.getInstance("GRAY.pf");
40+
test(csProfile);
41+
test(dataProfile);
42+
test(stringProfile);
43+
}
44+
45+
private static void test(ICC_Profile profile) {
46+
// Gray profile should be implemented as ICC_ProfileGray and includes
47+
// the mediaWhitePointTag and grayTRCTag tags
48+
if (!(profile instanceof ICC_ProfileGray)
49+
|| profile.getData(ICC_Profile.icSigMediaWhitePointTag) == null
50+
|| profile.getData(ICC_Profile.icSigGrayTRCTag) == null) {
51+
throw new RuntimeException("Wrong profile: " + profile);
52+
}
53+
54+
ICC_ProfileGray gray = (ICC_ProfileGray) profile;
55+
56+
int length = gray.getMediaWhitePoint().length;
57+
if (length != 3) {
58+
throw new RuntimeException("Wrong data length: " + length);
59+
}
60+
61+
// if getTRC() throws an exception then getGamma() should work
62+
boolean trc = false;
63+
try {
64+
gray.getTRC();
65+
trc = true;
66+
System.out.println("getTRC() works fine");
67+
} catch (ProfileDataException ignore) {
68+
gray.getGamma();
69+
}
70+
// if getGamma() throws an exception then getTRC() should work
71+
boolean gamma = false;
72+
try {
73+
gray.getGamma();
74+
gamma = true;
75+
System.out.println("getGamma() works fine");
76+
} catch (ProfileDataException ignore) {
77+
gray.getTRC();
78+
}
79+
80+
if (gamma == trc) {
81+
// only one should work
82+
throw new RuntimeException("Only one operation should work");
83+
}
84+
}
85+
}

0 commit comments

Comments
 (0)