KickJava   Java API By Example, From Geeks To Geeks.

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


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.COSName;
37 import org.pdfbox.cos.COSNumber;
38 import org.pdfbox.cos.COSStream;
39 import org.pdfbox.cos.COSString;
40
41 import java.awt.color.ColorSpace JavaDoc;
42 import java.awt.image.ColorModel JavaDoc;
43 import java.awt.image.IndexColorModel JavaDoc;
44
45 import java.io.InputStream JavaDoc;
46 import java.io.ByteArrayOutputStream JavaDoc;
47 import java.io.IOException JavaDoc;
48
49 /**
50  * This class represents an Indexed color space.
51  *
52  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
53  * @version $Revision: 1.4 $
54  */

55 public class PDIndexed extends PDColorSpace
56 {
57
58     /**
59      * The name of this color space.
60      */

61     public static final String JavaDoc NAME = "Indexed";
62
63     /**
64      * The abbreviated name of this color space.
65      */

66     public static final String JavaDoc ABBREVIATED_NAME = "I";
67
68     private COSArray array;
69
70     /**
71      * Constructor, default DeviceRGB, hival 255.
72      */

73     public PDIndexed()
74     {
75         array = new COSArray();
76         array.add( COSName.getPDFName( NAME ) );
77         array.add( COSName.getPDFName( PDDeviceRGB.NAME ) );
78         array.add( new COSInteger( 255 ) );
79         array.add( org.pdfbox.cos.COSNull.NULL );
80     }
81
82     /**
83      * Constructor.
84      *
85      * @param indexedArray The array containing the indexed parameters
86      */

87     public PDIndexed( COSArray indexedArray )
88     {
89         array = indexedArray;
90     }
91
92     /**
93      * This will return the number of color components. This will return the
94      * number of color components in the base color.
95      *
96      * @return The number of components in this color space.
97      *
98      * @throws IOException If there is an error getting the number of color components.
99      */

100     public int getNumberOfComponents() throws IOException JavaDoc
101     {
102         return getBaseColorSpace().getNumberOfComponents();
103     }
104
105     /**
106      * This will return the name of the color space.
107      *
108      * @return The name of the color space.
109      */

110     public String JavaDoc getName()
111     {
112         return NAME;
113     }
114
115     /**
116      * Create a Java colorspace for this colorspace.
117      *
118      * @return A color space that can be used for Java AWT operations.
119      *
120      * @throws IOException If there is an error creating the color space.
121      */

122     public ColorSpace JavaDoc createColorSpace() throws IOException JavaDoc
123     {
124         throw new IOException JavaDoc( "Not implemented" );
125     }
126     
127     /**
128      * Create a Java color model for this colorspace.
129      *
130      * @param bpc The number of bits per component.
131      *
132      * @return A color model that can be used for Java AWT operations.
133      *
134      * @throws IOException If there is an error creating the color model.
135      */

136     public ColorModel JavaDoc createColorModel( int bpc ) throws IOException JavaDoc
137     {
138         int size = getHighValue();
139         byte[] index = getLookupData();
140         //for (int i=0;i<index.length;i++) System.out.print(index[i]+" ");
141

142         ColorModel JavaDoc cm = new IndexColorModel JavaDoc(bpc, size+1, index,0,false);
143         return cm;
144     }
145
146     /**
147      * This will get the color space that acts as the index for this color space.
148      *
149      * @return The base color space.
150      *
151      * @throws IOException If there is error creating the base color space.
152      */

153     public PDColorSpace getBaseColorSpace() throws IOException JavaDoc
154     {
155         PDColorSpace retval = null;
156         COSBase base = array.getObject( 1 );
157         if( base instanceof COSName )
158         {
159             retval = PDColorSpaceFactory.createColorSpace( base );
160         }
161         else
162         {
163             throw new IOException JavaDoc( "Error:unknown base colorspace" );
164         }
165
166         return retval;
167     }
168
169     /**
170      * This will set the base color space.
171      *
172      * @param base The base color space to use as the index.
173      */

174     public void setBaseColorSpace( PDColorSpace base )
175     {
176         array.set( 1, base.getCOSObject() );
177     }
178
179     /**
180      * Get the highest value for the lookup.
181      *
182      * @return The hival entry.
183      */

184     public int getHighValue()
185     {
186         return ((COSNumber)array.getObject( 2 )).intValue();
187     }
188
189     /**
190      * This will set the highest value that is allowed. This cannot be higher
191      * than 255.
192      *
193      * @param high The highest value for the lookup table.
194      */

195     public void setHighValue( int high )
196     {
197         array.set( 2, new COSInteger( high ) );
198     }
199
200     /**
201      * This will perform a lookup into the color lookup table.
202      *
203      * @param componentNumber The component number, probably 1,2,3,3.
204      * @param lookupIndex The zero-based index into the table, should not exceed the high value.
205      *
206      * @return The value that was from the lookup table.
207      *
208      * @throws IOException If there is an error looking up the color.
209      */

210     public int lookupColor( int componentNumber, int lookupIndex ) throws IOException JavaDoc
211     {
212         PDColorSpace baseColor = getBaseColorSpace();
213         byte[] data = getLookupData();
214         int numberOfComponents = baseColor.getNumberOfComponents();
215         return (data[componentNumber*numberOfComponents + lookupIndex]+256)%256;
216     }
217
218     private byte[] getLookupData() throws IOException JavaDoc
219     {
220         COSBase lookupTable = array.getObject( 3 );
221         byte[] data = null;
222         if( lookupTable instanceof COSString )
223         {
224             data = ((COSString)lookupTable).getBytes();
225         }
226         else if( lookupTable instanceof COSStream )
227         {
228             //Data will be small so just load the whole thing into memory for
229
//easier processing
230
COSStream lookupStream = (COSStream)lookupTable;
231             InputStream JavaDoc input = lookupStream.getUnfilteredStream();
232             ByteArrayOutputStream JavaDoc output = new ByteArrayOutputStream JavaDoc(1024);
233             byte[] buffer = new byte[ 1024 ];
234             int amountRead;
235             while( (amountRead = input.read(buffer, 0, buffer.length)) != -1 )
236             {
237                 output.write( buffer, 0, amountRead );
238             }
239             data = output.toByteArray();
240         }
241         else if( lookupTable == null )
242         {
243             data = new byte[0];
244         }
245         else
246         {
247             throw new IOException JavaDoc( "Error: Unknown type for lookup table " + lookupTable );
248         }
249         return data;
250     }
251
252     /**
253      * This will set a color in the color lookup table.
254      *
255      * @param componentNumber The component number, probably 1,2,3,3.
256      * @param lookupIndex The zero-based index into the table, should not exceed the high value.
257      * @param color The color that will go into the table.
258      *
259      * @throws IOException If there is an error looking up the color.
260      */

261     public void setLookupColor( int componentNumber, int lookupIndex, int color ) throws IOException JavaDoc
262     {
263         PDColorSpace baseColor = getBaseColorSpace();
264         int numberOfComponents = baseColor.getNumberOfComponents();
265         byte[] data = getLookupData();
266         data[componentNumber*numberOfComponents + lookupIndex] = (byte)color;
267         COSString string = new COSString( data );
268         array.set( 3, string );
269     }
270 }
Popular Tags