KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.pdfbox.cos.COSNumber;
39
40 import java.awt.color.ColorSpace JavaDoc;
41 import java.awt.image.ColorModel JavaDoc;
42
43 import java.io.IOException JavaDoc;
44
45 /**
46  * This class represents a Cal Gray color space.
47  *
48  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
49  * @version $Revision: 1.5 $
50  */

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

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

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

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

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

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

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

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

136     public COSBase getCOSObject()
137     {
138         return array;
139     }
140
141     /**
142      * This will get the gamma value. If none is present then the default of 1
143      * will be returned.
144      *
145      * @return The gamma value.
146      */

147     public float getGamma()
148     {
149         float retval = 1.0f;
150         COSNumber gamma = (COSNumber)dictionary.getDictionaryObject( COSName.getPDFName( "Gamma" ) );
151         if( gamma != null )
152         {
153             retval = gamma.floatValue();
154         }
155         return retval;
156     }
157
158     /**
159      * Set the gamma value.
160      *
161      * @param value The new gamma value.
162      */

163     public void setGamma( float value )
164     {
165         dictionary.setItem( COSName.getPDFName( "Gamma" ), new COSFloat( value ) );
166     }
167
168     /**
169      * This will return the whitepoint tristimulus. As this is a required field
170      * this will never return null. A default of 1,1,1 will be returned if the
171      * pdf does not have any values yet.
172      *
173      * @return The whitepoint tristimulus.
174      */

175     public PDTristimulus getWhitepoint()
176     {
177         COSArray wp = (COSArray)dictionary.getDictionaryObject( COSName.getPDFName( "WhitePoint" ) );
178         if( wp == null )
179         {
180             wp = new COSArray();
181             wp.add( new COSFloat( 1.0f ) );
182             wp.add( new COSFloat( 1.0f ) );
183             wp.add( new COSFloat( 1.0f ) );
184             dictionary.setItem( COSName.getPDFName( "WhitePoint" ), wp );
185         }
186         return new PDTristimulus( wp );
187     }
188
189     /**
190      * This will set the whitepoint tristimulus. As this is a required field
191      * this null should not be passed into this function.
192      *
193      * @param wp The whitepoint tristimulus.
194      */

195     public void setWhitepoint( PDTristimulus wp )
196     {
197         COSBase wpArray = wp.getCOSObject();
198         if( wpArray != null )
199         {
200             dictionary.setItem( COSName.getPDFName( "WhitePoint" ), wpArray );
201         }
202     }
203
204     /**
205      * This will return the BlackPoint tristimulus. This is an optional field but
206      * has defaults so this will never return null.
207      * A default of 0,0,0 will be returned if the pdf does not have any values yet.
208      *
209      * @return The blackpoint tristimulus.
210      */

211     public PDTristimulus getBlackPoint()
212     {
213         COSArray bp = (COSArray)dictionary.getDictionaryObject( COSName.getPDFName( "BlackPoint" ) );
214         if( bp == null )
215         {
216             bp = new COSArray();
217             bp.add( new COSFloat( 0.0f ) );
218             bp.add( new COSFloat( 0.0f ) );
219             bp.add( new COSFloat( 0.0f ) );
220             dictionary.setItem( COSName.getPDFName( "BlackPoint" ), bp );
221         }
222         return new PDTristimulus( bp );
223     }
224
225     /**
226      * This will set the BlackPoint tristimulus. As this is a required field
227      * this null should not be passed into this function.
228      *
229      * @param bp The BlackPoint tristimulus.
230      */

231     public void setBlackPoint( PDTristimulus bp )
232     {
233         COSBase bpArray = null;
234         if( bp != null )
235         {
236             bpArray = bp.getCOSObject();
237         }
238         dictionary.setItem( COSName.getPDFName( "BlackPoint" ), bpArray );
239     }
240 }
Popular Tags