1 /*
   2  * Copyright (c) 1997, 2018, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /* ********************************************************************
  27  **********************************************************************
  28  **********************************************************************
  29  *** COPYRIGHT (c) Eastman Kodak Company, 1997                      ***
  30  *** As  an unpublished  work pursuant to Title 17 of the United    ***
  31  *** States Code.  All rights reserved.                             ***
  32  **********************************************************************
  33  **********************************************************************
  34  **********************************************************************/
  35 
  36 package java.awt.color;
  37 
  38 import sun.java2d.cmm.Profile;
  39 import sun.java2d.cmm.ProfileDeferralInfo;
  40 
  41 /**
  42  * The {@code ICC_ProfileRGB} class is a subclass of the {@code ICC_Profile}
  43  * class that represents profiles which meet the following criteria: the
  44  * profile's color space type is RGB, and the profile includes the
  45  * {@code redColorantTag}, {@code greenColorantTag}, {@code blueColorantTag},
  46  * {@code redTRCTag}, {@code greenTRCTag}, {@code blueTRCTag},
  47  * {@code mediaWhitePointTag} tags. The {@code getInstance} methods in the
  48  * {@code ICC_Profile} class will return an {@code ICC_ProfileRGB} object when
  49  * the above conditions are met. Three-component, matrix-based input profiles
  50  * and RGB display profiles are examples of this type of profile.
  51  * <p>
  52  * The advantage of this class is that it provides color transform matrices and
  53  * lookup tables that Java or native methods can use directly to optimize color
  54  * conversion in some cases.
  55  * <p>
  56  * To transform from a device profile color space to the CIEXYZ Profile
  57  * Connection Space, each device color component is first linearized by a lookup
  58  * through the corresponding tone reproduction curve (TRC). The resulting linear
  59  * RGB components are converted to the CIEXYZ PCS using a a 3x3 matrix
  60  * constructed from the RGB colorants.
  61  * <pre>
  62  *
  63  * &nbsp;               linearR = redTRC[deviceR]
  64  *
  65  * &nbsp;               linearG = greenTRC[deviceG]
  66  *
  67  * &nbsp;               linearB = blueTRC[deviceB]
  68  *
  69  * &nbsp; _      _       _                                             _   _         _
  70  * &nbsp;[  PCSX  ]     [  redColorantX  greenColorantX  blueColorantX  ] [  linearR  ]
  71  * &nbsp;[        ]     [                                               ] [           ]
  72  * &nbsp;[  PCSY  ]  =  [  redColorantY  greenColorantY  blueColorantY  ] [  linearG  ]
  73  * &nbsp;[        ]     [                                               ] [           ]
  74  * &nbsp;[_ PCSZ _]     [_ redColorantZ  greenColorantZ  blueColorantZ _] [_ linearB _]
  75  *
  76  * </pre>
  77  * The inverse transform is performed by converting PCS XYZ components to linear
  78  * RGB components through the inverse of the above 3x3 matrix, and then
  79  * converting linear RGB to device RGB through inverses of the TRCs.
  80  */
  81 public class ICC_ProfileRGB
  82 extends ICC_Profile {
  83 
  84     static final long serialVersionUID = 8505067385152579334L;
  85 
  86     /**
  87      * Used to get a gamma value or TRC for the red component.
  88      */
  89     public static final int REDCOMPONENT = 0;
  90 
  91     /**
  92      * Used to get a gamma value or TRC for the green component.
  93      */
  94     public static final int GREENCOMPONENT = 1;
  95 
  96     /**
  97      * Used to get a gamma value or TRC for the blue component.
  98      */
  99     public static final int BLUECOMPONENT = 2;
 100 
 101     /**
 102      * Constructs an new {@code ICC_ProfileRGB} from a CMM ID.
 103      *
 104      * @param p the CMM ID for the profile.
 105      */
 106     ICC_ProfileRGB(Profile p) {
 107         super(p);
 108     }
 109 
 110     /**
 111      * Constructs a new {@code ICC_ProfileRGB} from a
 112      * {@code ProfileDeferralInfo} object.
 113      *
 114      * @param pdi
 115      */
 116     ICC_ProfileRGB(ProfileDeferralInfo pdi) {
 117         super(pdi);
 118     }
 119 
 120     /**
 121      * Returns an array that contains the components of the profile's
 122      * {@code mediaWhitePointTag}.
 123      *
 124      * @return a 3-element {@code float} array containing the x, y, and z
 125      *         components of the profile's {@code mediaWhitePointTag}
 126      */
 127     public float[] getMediaWhitePoint() {
 128         return super.getMediaWhitePoint();
 129     }
 130 
 131     /**
 132      * Returns a 3x3 {@code float} matrix constructed from the X, Y, and Z
 133      * components of the profile's {@code redColorantTag},
 134      * {@code greenColorantTag}, and {@code blueColorantTag}.
 135      * <p>
 136      * This matrix can be used for color transforms in the forward direction of
 137      * the profile--from the profile color space to the CIEXYZ PCS.
 138      *
 139      * @return a 3x3 {@code float} array that contains the x, y, and z
 140      *         components of the profile's {@code redColorantTag},
 141      *         {@code greenColorantTag}, and {@code blueColorantTag}
 142      */
 143     public float[][] getMatrix() {
 144         float[][] theMatrix = new float[3][3];
 145         float[] tmpMatrix;
 146 
 147         tmpMatrix = getXYZTag(ICC_Profile.icSigRedColorantTag);
 148         theMatrix[0][0] = tmpMatrix[0];
 149         theMatrix[1][0] = tmpMatrix[1];
 150         theMatrix[2][0] = tmpMatrix[2];
 151         tmpMatrix = getXYZTag(ICC_Profile.icSigGreenColorantTag);
 152         theMatrix[0][1] = tmpMatrix[0];
 153         theMatrix[1][1] = tmpMatrix[1];
 154         theMatrix[2][1] = tmpMatrix[2];
 155         tmpMatrix = getXYZTag(ICC_Profile.icSigBlueColorantTag);
 156         theMatrix[0][2] = tmpMatrix[0];
 157         theMatrix[1][2] = tmpMatrix[1];
 158         theMatrix[2][2] = tmpMatrix[2];
 159         return theMatrix;
 160     }
 161 
 162     /**
 163      * Returns a gamma value representing the tone reproduction curve (TRC) for
 164      * a particular component. The component parameter must be one of
 165      * {@code REDCOMPONENT}, {@code GREENCOMPONENT}, or {@code BLUECOMPONENT}.
 166      * <p>
 167      * If the profile represents the TRC for the corresponding component as a
 168      * table rather than a single gamma value, an exception is thrown. In this
 169      * case the actual table can be obtained through the {@link #getTRC(int)}
 170      * method. When using a gamma value, the linear component (R, G, or B) is
 171      * computed as follows:
 172      * <pre>
 173      *
 174      * &nbsp;                                         gamma
 175      * &nbsp;        linearComponent = deviceComponent
 176      *
 177      * </pre>
 178      *
 179      * @param  component the {@code ICC_ProfileRGB} constant that represents the
 180      *         component whose TRC you want to retrieve
 181      * @return the gamma value as a float
 182      * @throws ProfileDataException if the profile does not specify the
 183      *         corresponding TRC as a single gamma value
 184      */
 185     public float getGamma(int component) {
 186     float theGamma;
 187     int theSignature;
 188 
 189         switch (component) {
 190         case REDCOMPONENT:
 191             theSignature = ICC_Profile.icSigRedTRCTag;
 192             break;
 193 
 194         case GREENCOMPONENT:
 195             theSignature = ICC_Profile.icSigGreenTRCTag;
 196             break;
 197 
 198         case BLUECOMPONENT:
 199             theSignature = ICC_Profile.icSigBlueTRCTag;
 200             break;
 201 
 202         default:
 203             throw new IllegalArgumentException("Must be Red, Green, or Blue");
 204         }
 205 
 206         theGamma = super.getGamma(theSignature);
 207 
 208         return theGamma;
 209     }
 210 
 211     /**
 212      * Returns the TRC for a particular component as an array. Component must be
 213      * {@code REDCOMPONENT}, {@code GREENCOMPONENT}, or {@code BLUECOMPONENT}.
 214      * Otherwise the returned array represents a lookup table where the input
 215      * component value is conceptually in the range [0.0, 1.0]. Value 0.0 maps
 216      * to array index 0 and value 1.0 maps to array index {@code length-1}.
 217      * Interpolation might be used to generate output values for input values
 218      * that do not map exactly to an index in the array. Output values also map
 219      * linearly to the range [0.0, 1.0]. Value 0.0 is represented by an array
 220      * value of 0x0000 and value 1.0 by 0xFFFF. In other words, the values are
 221      * really unsigned {@code short} values even though they are returned in a
 222      * {@code short} array.
 223      * <p>
 224      * If the profile has specified the corresponding TRC as linear (gamma =
 225      * 1.0) or as a simple gamma value, this method throws an exception. In this
 226      * case, the {@link #getGamma(int)} method should be used to get the gamma
 227      * value.
 228      *
 229      * @param  component the {@code ICC_ProfileRGB} constant that represents the
 230      *         component whose TRC you want to retrieve: {@code REDCOMPONENT},
 231      *         {@code GREENCOMPONENT}, or {@code BLUECOMPONENT}
 232      * @return a short array representing the TRC
 233      * @throws ProfileDataException if the profile does not specify the
 234      *         corresponding TRC as a table
 235      */
 236     public short[] getTRC(int component) {
 237     short[] theTRC;
 238     int theSignature;
 239 
 240         switch (component) {
 241         case REDCOMPONENT:
 242             theSignature = ICC_Profile.icSigRedTRCTag;
 243             break;
 244 
 245         case GREENCOMPONENT:
 246             theSignature = ICC_Profile.icSigGreenTRCTag;
 247             break;
 248 
 249         case BLUECOMPONENT:
 250             theSignature = ICC_Profile.icSigBlueTRCTag;
 251             break;
 252 
 253         default:
 254             throw new IllegalArgumentException("Must be Red, Green, or Blue");
 255         }
 256 
 257         theTRC = super.getTRC(theSignature);
 258 
 259         return theTRC;
 260     }
 261 
 262 }