KickJava   Java API By Example, From Geeks To Geeks.

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


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.PDRange;
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 Lab color space.
48  *
49  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
50  * @version $Revision: 1.4 $
51  */

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

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

65     public PDLab()
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 lab The underlying color space.
77      */

78     public PDLab( COSArray lab )
79     {
80         array = lab;
81         dictionary = (COSDictionary)array.getObject( 1 );
82     }
83
84     /**
85      * This will return the name of the color space.
86      *
87      * @return The name of the color space.
88      */

89     public String JavaDoc getName()
90     {
91         return NAME;
92     }
93
94     /**
95      * Convert this standard java object to a COS object.
96      *
97      * @return The cos object that matches this Java object.
98      */

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

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

125     public ColorModel JavaDoc createColorModel( int bpc ) throws IOException JavaDoc
126     {
127         throw new IOException JavaDoc( "Not implemented" );
128     }
129
130     /**
131      * This will get the number of components that this color space is made up of.
132      *
133      * @return The number of components in this color space.
134      *
135      * @throws IOException If there is an error getting the number of color components.
136      */

137     public int getNumberOfComponents() throws IOException JavaDoc
138     {
139         //BJL
140
//hmm is this correct, I am not 100% sure.
141
return 3;
142     }
143
144     /**
145      * This will return the whitepoint tristimulus. As this is a required field
146      * this will never return null. A default of 1,1,1 will be returned if the
147      * pdf does not have any values yet.
148      *
149      * @return The whitepoint tristimulus.
150      */

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

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

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

207     public void setBlackPoint( PDTristimulus bp )
208     {
209
210         COSBase bpArray = null;
211         if( bp != null )
212         {
213             bpArray = bp.getCOSObject();
214         }
215         dictionary.setItem( COSName.getPDFName( "BlackPoint" ), bpArray );
216     }
217
218     private COSArray getRangeArray()
219     {
220         COSArray range = (COSArray)dictionary.getDictionaryObject( COSName.getPDFName( "Range" ) );
221         if( range == null )
222         {
223             range = new COSArray();
224             dictionary.setItem( COSName.getPDFName( "Range" ), array );
225             range.add( new COSFloat( -100 ) );
226             range.add( new COSFloat( 100 ) );
227             range.add( new COSFloat( -100 ) );
228             range.add( new COSFloat( 100 ) );
229         }
230         return range;
231     }
232
233     /**
234      * This will get the valid range for the a component. If none is found
235      * then the default will be returned, which is -100 to 100.
236      *
237      * @return The a range.
238      */

239     public PDRange getARange()
240     {
241         COSArray range = getRangeArray();
242         return new PDRange( range, 0 );
243     }
244
245     /**
246      * This will set the a range for this color space.
247      *
248      * @param range The new range for the a component.
249      */

250     public void setARange( PDRange range )
251     {
252         COSArray rangeArray = null;
253         //if null then reset to defaults
254
if( range == null )
255         {
256             rangeArray = getRangeArray();
257             rangeArray.set( 0, new COSFloat( -100 ) );
258             rangeArray.set( 1, new COSFloat( 100 ) );
259         }
260         else
261         {
262             rangeArray = range.getCOSArray();
263         }
264         dictionary.setItem( COSName.getPDFName( "Range" ), rangeArray );
265     }
266
267     /**
268      * This will get the valid range for the b component. If none is found
269      * then the default will be returned, which is -100 to 100.
270      *
271      * @return The b range.
272      */

273     public PDRange getBRange()
274     {
275         COSArray range = getRangeArray();
276         return new PDRange( range, 1 );
277     }
278
279     /**
280      * This will set the b range for this color space.
281      *
282      * @param range The new range for the b component.
283      */

284     public void setBRange( PDRange range )
285     {
286         COSArray rangeArray = null;
287         //if null then reset to defaults
288
if( range == null )
289         {
290             rangeArray = getRangeArray();
291             rangeArray.set( 2, new COSFloat( -100 ) );
292             rangeArray.set( 3, new COSFloat( 100 ) );
293         }
294         else
295         {
296             rangeArray = range.getCOSArray();
297         }
298         dictionary.setItem( COSName.getPDFName( "Range" ), rangeArray );
299     }
300 }
Popular Tags