KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > pdmodel > graphics > color > PDCalRGB


1 /**
2  * Copyright (c) 2004, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.pdmodel.graphics.color;
32
33 import org.pdfbox.cos.COSArray;
34 import org.pdfbox.cos.COSBase;
35 import org.pdfbox.cos.COSDictionary;
36 import org.pdfbox.cos.COSFloat;
37 import org.pdfbox.cos.COSName;
38
39 import org.pdfbox.pdmodel.common.PDMatrix;
40
41 import java.awt.color.ColorSpace JavaDoc;
42 import java.awt.image.ColorModel JavaDoc;
43
44 import java.io.IOException JavaDoc;
45
46 /**
47  * This class represents a Cal RGB color space.
48  *
49  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
50  * @version $Revision: 1.3 $
51  */

52 public class PDCalRGB extends PDColorSpace
53 {
54     /**
55      * The name of this color space.
56      */

57     public static final String JavaDoc NAME = "CalRGB";
58
59     private COSArray array;
60     private COSDictionary dictionary;
61
62     /**
63      * Constructor.
64      */

65     public PDCalRGB()
66     {
67         array = new COSArray();
68         dictionary = new COSDictionary();
69         array.add( COSName.getPDFName( NAME ) );
70         array.add( dictionary );
71     }
72
73     /**
74      * Constructor with array.
75      *
76      * @param rgb The underlying color space.
77      */

78     public PDCalRGB( COSArray rgb )
79     {
80         array = rgb;
81         dictionary = (COSDictionary)array.getObject( 1 );
82     }
83
84     /**
85      * This will get the number of components that this color space is made up of.
86      *
87      * @return The number of components in this color space.
88      *
89      * @throws IOException If there is an error getting the number of color components.
90      */

91     public int getNumberOfComponents() throws IOException JavaDoc
92     {
93         return 3;
94     }
95
96     /**
97      * This will return the name of the color space.
98      *
99      * @return The name of the color space.
100      */

101     public String JavaDoc getName()
102     {
103         return NAME;
104     }
105
106     /**
107      * Create a Java colorspace for this colorspace.
108      *
109      * @return A color space that can be used for Java AWT operations.
110      *
111      * @throws IOException If there is an error creating the color space.
112      */

113     public ColorSpace JavaDoc createColorSpace() throws IOException JavaDoc
114     {
115         throw new IOException JavaDoc( "Not implemented" );
116     }
117     
118     /**
119      * Create a Java color model for this colorspace.
120      *
121      * @param bpc The number of bits per component.
122      *
123      * @return A color model that can be used for Java AWT operations.
124      *
125      * @throws IOException If there is an error creating the color model.
126      */

127     public ColorModel JavaDoc createColorModel( int bpc ) throws IOException JavaDoc
128     {
129         throw new IOException JavaDoc( "Not implemented" );
130     }
131
132     /**
133      * Convert this standard java object to a COS object.
134      *
135      * @return The cos object that matches this Java object.
136      */

137     public COSBase getCOSObject()
138     {
139         return array;
140     }
141
142     /**
143      * This will return the whitepoint tristimulus. As this is a required field
144      * this will never return null. A default of 1,1,1 will be returned if the
145      * pdf does not have any values yet.
146      *
147      * @return The whitepoint tristimulus.
148      */

149     public PDTristimulus getWhitepoint()
150     {
151         COSArray wp = (COSArray)dictionary.getDictionaryObject( COSName.getPDFName( "WhitePoint" ) );
152         if( wp == null )
153         {
154             wp = new COSArray();
155             wp.add( new COSFloat( 1.0f ) );
156             wp.add( new COSFloat( 1.0f ) );
157             wp.add( new COSFloat( 1.0f ) );
158             dictionary.setItem( COSName.getPDFName( "WhitePoint" ), wp );
159         }
160         return new PDTristimulus( wp );
161     }
162
163     /**
164      * This will set the whitepoint tristimulus. As this is a required field
165      * this null should not be passed into this function.
166      *
167      * @param wp The whitepoint tristimulus.
168      */

169     public void setWhitepoint( PDTristimulus wp )
170     {
171         COSBase wpArray = wp.getCOSObject();
172         if( wpArray != null )
173         {
174             dictionary.setItem( COSName.getPDFName( "WhitePoint" ), wpArray );
175         }
176     }
177
178     /**
179      * This will return the BlackPoint tristimulus. This is an optional field but
180      * has defaults so this will never return null.
181      * A default of 0,0,0 will be returned if the pdf does not have any values yet.
182      *
183      * @return The blackpoint tristimulus.
184      */

185     public PDTristimulus getBlackPoint()
186     {
187         COSArray bp = (COSArray)dictionary.getDictionaryObject( COSName.getPDFName( "BlackPoint" ) );
188         if( bp == null )
189         {
190             bp = new COSArray();
191             bp.add( new COSFloat( 0.0f ) );
192             bp.add( new COSFloat( 0.0f ) );
193             bp.add( new COSFloat( 0.0f ) );
194             dictionary.setItem( COSName.getPDFName( "BlackPoint" ), bp );
195         }
196         return new PDTristimulus( bp );
197     }
198
199     /**
200      * This will set the BlackPoint tristimulus. As this is a required field
201      * this null should not be passed into this function.
202      *
203      * @param bp The BlackPoint tristimulus.
204      */

205     public void setBlackPoint( PDTristimulus bp )
206     {
207
208         COSBase bpArray = null;
209         if( bp != null )
210         {
211             bpArray = bp.getCOSObject();
212         }
213         dictionary.setItem( COSName.getPDFName( "BlackPoint" ), bpArray );
214     }
215
216     /**
217      * This will get the gamma value. If none is present then the default of 1,1,1
218      * will be returned.
219      *
220      * @return The gamma value.
221      */

222     public PDGamma getGamma()
223     {
224         COSArray gamma = (COSArray)dictionary.getDictionaryObject( COSName.getPDFName( "Gamma" ) );
225         if( gamma == null )
226         {
227             gamma = new COSArray();
228             gamma.add( new COSFloat( 1.0f ) );
229             gamma.add( new COSFloat( 1.0f ) );
230             gamma.add( new COSFloat( 1.0f ) );
231             dictionary.setItem( COSName.getPDFName( "Gamma" ), gamma );
232         }
233         return new PDGamma( gamma );
234     }
235
236     /**
237      * Set the gamma value.
238      *
239      * @param value The new gamma value.
240      */

241     public void setGamma( PDGamma value )
242     {
243         COSArray gamma = null;
244         if( value != null )
245         {
246             gamma = value.getCOSArray();
247         }
248         dictionary.setItem( COSName.getPDFName( "Gamma" ), gamma );
249     }
250
251     /**
252      * This will get the linear interpretation array. This is guaranteed to not
253      * return null. If the underlying dictionary contains null then the identity
254      * matrix will be returned.
255      *
256      * @return The linear interpretation matrix.
257      */

258     public PDMatrix getLinearInterpretation()
259     {
260         PDMatrix retval = null;
261         COSArray matrix = (COSArray)dictionary.getDictionaryObject( COSName.getPDFName( "Matrix" ) );
262         if( matrix == null )
263         {
264             retval = new PDMatrix();
265             setLinearInterpretation( retval );
266         }
267         else
268         {
269             retval = new PDMatrix( matrix );
270         }
271         return retval;
272     }
273
274     /**
275      * This will set the linear interpretation matrix. Passing in null will
276      * clear the matrix.
277      *
278      * @param matrix The new linear interpretation matrix.
279      */

280     public void setLinearInterpretation( PDMatrix matrix )
281     {
282         COSArray matrixArray = null;
283         if( matrix != null )
284         {
285             matrixArray = matrix.getCOSArray();
286         }
287         dictionary.setItem( COSName.getPDFName( "Matrix" ), matrixArray );
288     }
289 }
Popular Tags