KickJava   Java API By Example, From Geeks To Geeks.

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


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.COSInteger;
36 import org.pdfbox.cos.COSFloat;
37 import org.pdfbox.cos.COSName;
38 import org.pdfbox.cos.COSNumber;
39 import org.pdfbox.cos.COSStream;
40
41 import org.pdfbox.pdmodel.PDDocument;
42 import org.pdfbox.pdmodel.common.COSArrayList;
43 import org.pdfbox.pdmodel.common.PDRange;
44 import org.pdfbox.pdmodel.common.PDStream;
45
46 import java.awt.Transparency JavaDoc;
47 import java.awt.color.ColorSpace JavaDoc;
48 import java.awt.color.ICC_ColorSpace JavaDoc;
49 import java.awt.color.ICC_Profile JavaDoc;
50 import java.awt.image.ColorModel JavaDoc;
51 import java.awt.image.ComponentColorModel JavaDoc;
52 import java.awt.image.DataBuffer JavaDoc;
53
54 import java.io.InputStream JavaDoc;
55 import java.io.IOException JavaDoc;
56
57 import java.util.ArrayList JavaDoc;
58 import java.util.List JavaDoc;
59
60 /**
61  * This class represents a ICC profile color space.
62  *
63  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
64  * @version $Revision: 1.6 $
65  */

66 public class PDICCBased extends PDColorSpace
67 {
68     /**
69      * The name of this color space.
70      */

71     public static final String JavaDoc NAME = "ICCBased";
72
73     private COSArray array;
74     private PDStream stream;
75     
76     /**
77      * Default constructor, creates empty stream.
78      *
79      * @param doc The document to store the icc data.
80      */

81     public PDICCBased( PDDocument doc )
82     {
83         array = new COSArray();
84         array.add( COSName.getPDFName( NAME ) );
85         array.add( new PDStream( doc ) );
86     }
87
88     /**
89      * Constructor.
90      *
91      * @param iccArray The ICC stream object.
92      */

93     public PDICCBased( COSArray iccArray )
94     {
95         array = iccArray;
96         stream = new PDStream( (COSStream)iccArray.getObject( 1 ) );
97     }
98
99     /**
100      * This will return the name of the color space.
101      *
102      * @return The name of the color space.
103      */

104     public String JavaDoc getName()
105     {
106         return NAME;
107     }
108
109     /**
110      * Convert this standard java object to a COS object.
111      *
112      * @return The cos object that matches this Java object.
113      */

114     public COSBase getCOSObject()
115     {
116         return array;
117     }
118     
119     /**
120      * Get the pd stream for this icc color space.
121      *
122      * @return Get the stream for this icc based color space.
123      */

124     public PDStream getPDStream()
125     {
126         return stream;
127     }
128
129     /**
130      * Create a Java colorspace for this colorspace.
131      *
132      * @return A color space that can be used for Java AWT operations.
133      *
134      * @throws IOException If there is an error creating the color space.
135      */

136     public ColorSpace JavaDoc createColorSpace() throws IOException JavaDoc
137     {
138         InputStream JavaDoc profile = null;
139         ColorSpace JavaDoc cSpace = null;
140         try
141         {
142             profile = stream.createInputStream();
143             ICC_Profile JavaDoc iccProfile = ICC_Profile.getInstance( profile );
144             cSpace = new ICC_ColorSpace JavaDoc( iccProfile );
145         }
146         finally
147         {
148             if( profile != null )
149             {
150                 profile.close();
151             }
152         }
153         return cSpace;
154     }
155     
156     /**
157      * Create a Java color model for this colorspace.
158      *
159      * @param bpc The number of bits per component.
160      *
161      * @return A color model that can be used for Java AWT operations.
162      *
163      * @throws IOException If there is an error creating the color model.
164      */

165     public ColorModel JavaDoc createColorModel( int bpc ) throws IOException JavaDoc
166     {
167         int[] nbBits = { bpc, bpc, bpc };
168         ComponentColorModel JavaDoc componentColorModel =
169                 new ComponentColorModel JavaDoc( createColorSpace(),
170                                          nbBits,
171                                          false,
172                                          false,
173                                          Transparency.OPAQUE,
174                                          DataBuffer.TYPE_BYTE );
175         
176         return componentColorModel;
177     }
178
179     /**
180      * This will return the number of color components. As of PDF 1.4 this will
181      * be 1,3,4.
182      *
183      * @return The number of components in this color space.
184      *
185      * @throws IOException If there is an error getting the number of color components.
186      */

187     public int getNumberOfComponents() throws IOException JavaDoc
188     {
189         COSNumber n = (COSNumber)stream.getStream().getDictionaryObject( COSName.getPDFName( "N" ) );
190         return n.intValue();
191     }
192
193     /**
194      * This will set the number of color components.
195      *
196      * @param n The number of color components.
197      */

198     public void setNumberOfComponents( int n )
199     {
200         stream.getStream().setItem( COSName.getPDFName( "N" ), new COSInteger( n ) );
201     }
202
203     /**
204      * This will return a list of alternate color spaces(PDColorSpace) if the display application
205      * does not support this icc stream.
206      *
207      * @return A list of alternate color spaces.
208      *
209      * @throws IOException If there is an error getting the alternate color spaces.
210      */

211     public List getAlternateColorSpaces() throws IOException JavaDoc
212     {
213         COSBase alternate = stream.getStream().getDictionaryObject( COSName.getPDFName( "Alternate" ) );
214         COSArray alternateArray = null;
215         if( alternate == null )
216         {
217             alternateArray = new COSArray();
218             int numComponents = getNumberOfComponents();
219             String JavaDoc csName = null;
220             if( numComponents == 1 )
221             {
222                 csName = PDDeviceGray.NAME;
223             }
224             else if( numComponents == 3 )
225             {
226                 csName = PDDeviceRGB.NAME;
227             }
228             else if( numComponents == 4 )
229             {
230                 csName = PDDeviceCMYK.NAME;
231             }
232             else
233             {
234                 throw new IOException JavaDoc( "Unknown colorspace number of components:" + numComponents );
235             }
236             alternateArray.add( COSName.getPDFName( csName ) );
237         }
238         else
239         {
240             if( alternate instanceof COSArray )
241             {
242                 alternateArray = (COSArray)alternate;
243             }
244             else if( alternate instanceof COSName )
245             {
246                 alternateArray = new COSArray();
247                 alternateArray.add( alternate );
248             }
249             else
250             {
251                 throw new IOException JavaDoc( "Error: expected COSArray or COSName and not " +
252                     alternate.getClass().getName() );
253             }
254         }
255         List retval = new ArrayList();
256         for( int i=0; i<alternateArray.size(); i++ )
257         {
258             retval.add( PDColorSpaceFactory.createColorSpace( alternateArray.get( i ) ) );
259         }
260         return new COSArrayList( retval, alternateArray );
261     }
262
263     /**
264      * This will set the list of alternate color spaces. This should be a list
265      * of PDColorSpace objects.
266      *
267      * @param list The list of colorspace objects.
268      */

269     public void setAlternateColorSpaces( List list )
270     {
271         COSArray altArray = null;
272         if( list != null )
273         {
274             altArray = COSArrayList.converterToCOSArray( list );
275         }
276         stream.getStream().setItem( COSName.getPDFName( "Alternate" ), altArray );
277     }
278
279     private COSArray getRangeArray( int n )
280     {
281         COSArray rangeArray = (COSArray)stream.getStream().getDictionaryObject( COSName.getPDFName( "Range" ) );
282         if( rangeArray == null )
283         {
284             rangeArray = new COSArray();
285             stream.getStream().setItem( COSName.getPDFName( "Range" ), rangeArray );
286             while( rangeArray.size() < n*2 )
287             {
288                 rangeArray.add( new COSFloat( -100 ) );
289                 rangeArray.add( new COSFloat( 100 ) );
290             }
291         }
292         return rangeArray;
293     }
294
295     /**
296      * This will get the range for a certain component number. This is will never
297      * return null. If it is not present then the range -100 to 100 will
298      * be returned.
299      *
300      * @param n The component number to get the range for.
301      *
302      * @return The range for this component.
303      */

304     public PDRange getRangeForComponent( int n )
305     {
306         COSArray rangeArray = getRangeArray( n );
307         return new PDRange( rangeArray, n );
308     }
309
310     /**
311      * This will set the a range for this color space.
312      *
313      * @param range The new range for the a component.
314      * @param n The component to set the range for.
315      */

316     public void setRangeForComponent( PDRange range, int n )
317     {
318         COSArray rangeArray = getRangeArray( n );
319         rangeArray.set( n*2, new COSFloat( range.getMin() ) );
320         rangeArray.set( n*2+1, new COSFloat( range.getMax() ) );
321     }
322
323     /**
324      * This will get the metadata stream for this object. Null if there is no
325      * metadata stream.
326      *
327      * @return The metadata stream, if it exists.
328      */

329     public COSStream getMetadata()
330     {
331         return (COSStream)stream.getStream().getDictionaryObject( COSName.getPDFName( "Metadata" ) );
332     }
333
334     /**
335      * This will set the metadata stream that is associated with this color space.
336      *
337      * @param metadata The new metadata stream.
338      */

339     public void setMetadata( COSStream metadata )
340     {
341         stream.getStream().setItem( COSName.getPDFName( "Metadata" ), metadata );
342     }
343 }
Popular Tags