KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.awt.color.ColorSpace JavaDoc;
34 import java.awt.color.ICC_ColorSpace JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.io.OutputStream JavaDoc;
37
38 import org.pdfbox.cos.COSArray;
39 import org.pdfbox.cos.COSBase;
40 import org.pdfbox.cos.COSFloat;
41 import org.pdfbox.cos.COSName;
42 import org.pdfbox.pdmodel.PDDocument;
43 import org.pdfbox.pdmodel.common.PDStream;
44
45 /**
46  * This class represents a color space in a pdf document.
47  *
48  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
49  * @version $Revision: 1.11 $
50  */

51 public final class PDColorSpaceFactory
52 {
53     /**
54      * Private constructor for utility classes.
55      */

56     private PDColorSpaceFactory()
57     {
58         //utility class should not be implemented
59
}
60
61     /**
62      * This will create the correct color space given the name.
63      *
64      * @param colorSpace The color space object.
65      *
66      * @return The color space.
67      *
68      * @throws IOException If the color space name is unknown.
69      */

70     public static PDColorSpace createColorSpace( COSBase colorSpace ) throws IOException JavaDoc
71     {
72         PDColorSpace retval = null;
73         if( colorSpace instanceof COSName )
74         {
75             retval = createColorSpace( ((COSName)colorSpace).getName() );
76         }
77         else if( colorSpace instanceof COSArray )
78         {
79             COSArray array = (COSArray)colorSpace;
80             COSName type = (COSName)array.getObject( 0 );
81             if( type.getName().equals( PDCalGray.NAME ) )
82             {
83                 retval = new PDCalGray( array );
84             }
85             else if( type.getName().equals( PDCalRGB.NAME ) )
86             {
87                 retval = new PDCalRGB( array );
88             }
89             else if( type.getName().equals( PDDeviceN.NAME ) )
90             {
91                 retval = new PDDeviceN( array );
92             }
93             else if( type.getName().equals( PDIndexed.NAME ) ||
94                    type.getName().equals( PDIndexed.ABBREVIATED_NAME ))
95             {
96                 retval = new PDIndexed( array );
97             }
98             else if( type.getName().equals( PDLab.NAME ) )
99             {
100                 retval = new PDLab( array );
101             }
102             else if( type.getName().equals( PDSeparation.NAME ) )
103             {
104                 retval = new PDSeparation( array );
105             }
106             else if( type.getName().equals( PDICCBased.NAME ) )
107             {
108                 retval = new PDICCBased( array );
109             }
110             else if( type.getName().equals( PDPattern.NAME ) )
111             {
112                 retval = new PDPattern( array );
113             }
114             else
115             {
116                 throw new IOException JavaDoc( "Unknown colorspace array type:" + type );
117             }
118         }
119         else
120         {
121             throw new IOException JavaDoc( "Unknown colorspace type:" + colorSpace );
122         }
123         return retval;
124     }
125
126     /**
127      * This will create the correct color space given the name.
128      *
129      * @param colorSpaceName The name of the colorspace.
130      *
131      * @return The color space.
132      *
133      * @throws IOException If the color space name is unknown.
134      */

135     public static PDColorSpace createColorSpace( String JavaDoc colorSpaceName ) throws IOException JavaDoc
136     {
137         PDColorSpace cs = null;
138         if( colorSpaceName.equals( PDDeviceCMYK.NAME ) ||
139                  colorSpaceName.equals( PDDeviceCMYK.ABBREVIATED_NAME ) )
140         {
141             cs = PDDeviceCMYK.INSTANCE;
142         }
143         else if( colorSpaceName.equals( PDDeviceRGB.NAME ) ||
144                  colorSpaceName.equals( PDDeviceRGB.ABBREVIATED_NAME ) )
145         {
146             cs = PDDeviceRGB.INSTANCE;
147         }
148         else if( colorSpaceName.equals( PDDeviceGray.NAME ) ||
149                  colorSpaceName.equals( PDDeviceGray.ABBREVIATED_NAME ))
150         {
151             cs = new PDDeviceGray();
152         }
153         else if( colorSpaceName.equals( PDLab.NAME ) )
154         {
155             cs = new PDLab();
156         }
157         else if( colorSpaceName.equals( PDPattern.NAME ) )
158         {
159             cs = new PDPattern();
160         }
161         else
162         {
163             throw new IOException JavaDoc( "Error: Unknown colorspace '" + colorSpaceName + "'" );
164         }
165         return cs;
166     }
167     
168     /**
169      * This will create the correct color space from a java colorspace.
170      *
171      * @param doc The doc to potentiall write information to.
172      * @param cs The awt colorspace.
173      *
174      * @return The color space.
175      *
176      * @throws IOException If the color space name is unknown.
177      */

178     public static PDColorSpace createColorSpace( PDDocument doc, ColorSpace JavaDoc cs ) throws IOException JavaDoc
179     {
180         PDColorSpace retval = null;
181         if( cs.isCS_sRGB() )
182         {
183             retval = PDDeviceRGB.INSTANCE;
184         }
185         else if( cs instanceof ICC_ColorSpace JavaDoc )
186         {
187             ICC_ColorSpace JavaDoc ics = (ICC_ColorSpace JavaDoc)cs;
188             PDICCBased pdCS = new PDICCBased( doc );
189             retval = pdCS;
190             COSArray ranges = new COSArray();
191             for( int i=0; i<cs.getNumComponents(); i++ )
192             {
193                 ranges.add( new COSFloat( ics.getMinValue( i ) ) );
194                 ranges.add( new COSFloat( ics.getMaxValue( i ) ) );
195             }
196             PDStream iccData = pdCS.getPDStream();
197             OutputStream JavaDoc output = null;
198             try
199             {
200                 output = iccData.createOutputStream();
201                 output.write( ics.getProfile().getData() );
202             }
203             finally
204             {
205                 if( output != null )
206                 {
207                     output.close();
208                 }
209             }
210             pdCS.setNumberOfComponents( cs.getNumComponents() );
211         }
212         else
213         {
214             throw new IOException JavaDoc( "Not yet implemented:" + cs );
215         }
216         return retval;
217     }
218 }
Popular Tags