1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.pdfbox.pdmodel.graphics.color; 18 19 import org.apache.pdfbox.cos.COSArray; 20 import org.apache.pdfbox.cos.COSBase; 21 import org.apache.pdfbox.cos.COSDictionary; 22 import org.apache.pdfbox.cos.COSFloat; 23 import org.apache.pdfbox.cos.COSName; 24 25 import org.apache.pdfbox.pdmodel.common.PDMatrix; 26 27 import java.awt.color.ColorSpace; 28 import java.awt.image.ColorModel; 29 30 import java.io.IOException; 31 32 /** 33 * This class represents a Cal RGB color space. 34 * 35 * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a> 36 * @version $Revision: 1.3 $ 37 */ 38 public class PDCalRGB extends PDColorSpace 39 { 40 /** 41 * The name of this color space. 42 */ 43 public static final String NAME = "CalRGB"; 44 45 private COSArray array; 46 private COSDictionary dictionary; 47 48 /** 49 * Constructor. 50 */ 51 public PDCalRGB() 52 { 53 array = new COSArray(); 54 dictionary = new COSDictionary(); 55 array.add( COSName.getPDFName( NAME ) ); 56 array.add( dictionary ); 57 } 58 59 /** 60 * Constructor with array. 61 * 62 * @param rgb The underlying color space. 63 */ 64 public PDCalRGB( COSArray rgb ) 65 { 66 array = rgb; 67 dictionary = (COSDictionary)array.getObject( 1 ); 68 } 69 70 /** 71 * This will get the number of components that this color space is made up of. 72 * 73 * @return The number of components in this color space. 74 * 75 * @throws IOException If there is an error getting the number of color components. 76 */ 77 public int getNumberOfComponents() throws IOException 78 { 79 return 3; 80 } 81 82 /** 83 * This will return the name of the color space. 84 * 85 * @return The name of the color space. 86 */ 87 public String getName() 88 { 89 return NAME; 90 } 91 92 /** 93 * Create a Java colorspace for this colorspace. 94 * 95 * @return A color space that can be used for Java AWT operations. 96 */ 97 protected ColorSpace createColorSpace() 98 { 99 return new ColorSpaceCalRGB(getGamma(),getWhitepoint(),getBlackPoint(),getLinearInterpretation()); 100 } 101 102 /** 103 * Create a Java color model for this colorspace. 104 * 105 * @param bpc The number of bits per component. 106 * 107 * @return A color model that can be used for Java AWT operations. 108 * 109 * @throws IOException If there is an error creating the color model. 110 */ 111 public ColorModel createColorModel( int bpc ) throws IOException 112 { 113 throw new IOException( "Not implemented" ); 114 } 115 116 /** 117 * Convert this standard java object to a COS object. 118 * 119 * @return The cos object that matches this Java object. 120 */ 121 public COSBase getCOSObject() 122 { 123 return array; 124 } 125 126 /** 127 * This will return the whitepoint tristimulus. As this is a required field 128 * this will never return null. A default of 1,1,1 will be returned if the 129 * pdf does not have any values yet. 130 * 131 * @return The whitepoint tristimulus. 132 */ 133 public PDTristimulus getWhitepoint() 134 { 135 COSArray wp = (COSArray)dictionary.getDictionaryObject( COSName.getPDFName( "WhitePoint" ) ); 136 if( wp == null ) 137 { 138 wp = new COSArray(); 139 wp.add( new COSFloat( 1.0f ) ); 140 wp.add( new COSFloat( 1.0f ) ); 141 wp.add( new COSFloat( 1.0f ) ); 142 dictionary.setItem( COSName.getPDFName( "WhitePoint" ), wp ); 143 } 144 return new PDTristimulus( wp ); 145 } 146 147 /** 148 * This will set the whitepoint tristimulus. As this is a required field 149 * this null should not be passed into this function. 150 * 151 * @param wp The whitepoint tristimulus. 152 */ 153 public void setWhitepoint( PDTristimulus wp ) 154 { 155 COSBase wpArray = wp.getCOSObject(); 156 if( wpArray != null ) 157 { 158 dictionary.setItem( COSName.getPDFName( "WhitePoint" ), wpArray ); 159 } 160 } 161 162 /** 163 * This will return the BlackPoint tristimulus. This is an optional field but 164 * has defaults so this will never return null. 165 * A default of 0,0,0 will be returned if the pdf does not have any values yet. 166 * 167 * @return The blackpoint tristimulus. 168 */ 169 public PDTristimulus getBlackPoint() 170 { 171 COSArray bp = (COSArray)dictionary.getDictionaryObject( COSName.getPDFName( "BlackPoint" ) ); 172 if( bp == null ) 173 { 174 bp = new COSArray(); 175 bp.add( new COSFloat( 0.0f ) ); 176 bp.add( new COSFloat( 0.0f ) ); 177 bp.add( new COSFloat( 0.0f ) ); 178 dictionary.setItem( COSName.getPDFName( "BlackPoint" ), bp ); 179 } 180 return new PDTristimulus( bp ); 181 } 182 183 /** 184 * This will set the BlackPoint tristimulus. As this is a required field 185 * this null should not be passed into this function. 186 * 187 * @param bp The BlackPoint tristimulus. 188 */ 189 public void setBlackPoint( PDTristimulus bp ) 190 { 191 192 COSBase bpArray = null; 193 if( bp != null ) 194 { 195 bpArray = bp.getCOSObject(); 196 } 197 dictionary.setItem( COSName.getPDFName( "BlackPoint" ), bpArray ); 198 } 199 200 /** 201 * This will get the gamma value. If none is present then the default of 1,1,1 202 * will be returned. 203 * 204 * @return The gamma value. 205 */ 206 public PDGamma getGamma() 207 { 208 COSArray gamma = (COSArray)dictionary.getDictionaryObject( COSName.getPDFName( "Gamma" ) ); 209 if( gamma == null ) 210 { 211 gamma = new COSArray(); 212 gamma.add( new COSFloat( 1.0f ) ); 213 gamma.add( new COSFloat( 1.0f ) ); 214 gamma.add( new COSFloat( 1.0f ) ); 215 dictionary.setItem( COSName.getPDFName( "Gamma" ), gamma ); 216 } 217 return new PDGamma( gamma ); 218 } 219 220 /** 221 * Set the gamma value. 222 * 223 * @param value The new gamma value. 224 */ 225 public void setGamma( PDGamma value ) 226 { 227 COSArray gamma = null; 228 if( value != null ) 229 { 230 gamma = value.getCOSArray(); 231 } 232 dictionary.setItem( COSName.getPDFName( "Gamma" ), gamma ); 233 } 234 235 /** 236 * This will get the linear interpretation array. This is guaranteed to not 237 * return null. If the underlying dictionary contains null then the identity 238 * matrix will be returned. 239 * 240 * @return The linear interpretation matrix. 241 */ 242 public PDMatrix getLinearInterpretation() 243 { 244 PDMatrix retval = null; 245 COSArray matrix = (COSArray)dictionary.getDictionaryObject( COSName.getPDFName( "Matrix" ) ); 246 if( matrix == null ) 247 { 248 retval = new PDMatrix(); 249 setLinearInterpretation( retval ); 250 } 251 else 252 { 253 retval = new PDMatrix( matrix ); 254 } 255 return retval; 256 } 257 258 /** 259 * This will set the linear interpretation matrix. Passing in null will 260 * clear the matrix. 261 * 262 * @param matrix The new linear interpretation matrix. 263 */ 264 public void setLinearInterpretation( PDMatrix matrix ) 265 { 266 COSArray matrixArray = null; 267 if( matrix != null ) 268 { 269 matrixArray = matrix.getCOSArray(); 270 } 271 dictionary.setItem( COSName.getPDFName( "Matrix" ), matrixArray ); 272 } 273 }