KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > util > ImageParameters


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.util;
32
33 import org.pdfbox.cos.COSArray;
34 import org.pdfbox.cos.COSBase;
35 import org.pdfbox.cos.COSDictionary;
36 import org.pdfbox.cos.COSInteger;
37 import org.pdfbox.cos.COSName;
38 import org.pdfbox.cos.COSNumber;
39
40 import org.pdfbox.pdmodel.common.COSArrayList;
41 import org.pdfbox.pdmodel.graphics.color.PDColorSpace;
42 import org.pdfbox.pdmodel.graphics.color.PDColorSpaceFactory;
43
44 import java.io.IOException JavaDoc;
45 import java.util.List JavaDoc;
46
47 /**
48  * This contains all of the image parameters for in inlined image.
49  *
50  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
51  * @version $Revision: 1.4 $
52  */

53 public class ImageParameters
54 {
55     private COSDictionary dictionary;
56
57     /**
58      * Constructor.
59      */

60     public ImageParameters()
61     {
62         dictionary = new COSDictionary();
63     }
64
65     /**
66      * Constructor.
67      *
68      * @param params The image parameters.
69      */

70     public ImageParameters( COSDictionary params )
71     {
72         dictionary = params;
73     }
74     
75     /**
76      * This will get the dictionary that stores the image parameters.
77      *
78      * @return The COS dictionary that stores the image parameters.
79      */

80     public COSDictionary getDictionary()
81     {
82         return dictionary;
83     }
84
85     private COSBase getCOSObject( String JavaDoc abbreviatedName, String JavaDoc name )
86     {
87         COSBase retval = dictionary.getDictionaryObject( COSName.getPDFName( abbreviatedName ) );
88         if( retval == null )
89         {
90             retval = dictionary.getDictionaryObject( COSName.getPDFName( name ) );
91         }
92         return retval;
93     }
94
95     private int getNumberOrNegativeOne( String JavaDoc abbreviatedName, String JavaDoc name )
96     {
97         int retval = -1;
98         COSNumber number = (COSNumber)getCOSObject( abbreviatedName, name );
99         if( number != null )
100         {
101             retval = number.intValue();
102         }
103         return retval;
104     }
105
106     /**
107      * The bits per component of this image. This will return -1 if one has not
108      * been set.
109      *
110      * @return The number of bits per component.
111      */

112     public int getBitsPerComponent()
113     {
114         return getNumberOrNegativeOne( "BPC", "BitsPerComponent" );
115     }
116
117     /**
118      * Set the number of bits per component.
119      *
120      * @param bpc The number of bits per component.
121      */

122     public void setBitsPerComponent( int bpc )
123     {
124         dictionary.setItem( COSName.getPDFName( "BPC" ), new COSInteger( bpc ) );
125     }
126
127
128     /**
129      * This will get the color space or null if none exists.
130      *
131      * @return The color space for this image.
132      *
133      * @throws IOException If there is an error getting the colorspace.
134      */

135     public PDColorSpace getColorSpace() throws IOException JavaDoc
136     {
137         COSBase cs = getCOSObject( "CS", "ColorSpace" );
138         PDColorSpace retval = null;
139         if( cs != null )
140         {
141             retval = PDColorSpaceFactory.createColorSpace( cs );
142         }
143         return retval;
144     }
145
146     /**
147      * This will set the color space for this image.
148      *
149      * @param cs The color space for this image.
150      */

151     public void setColorSpace( PDColorSpace cs )
152     {
153         COSBase base = null;
154         if( cs != null )
155         {
156             base = cs.getCOSObject();
157         }
158         dictionary.setItem( COSName.getPDFName( "CS" ), base );
159     }
160
161     /**
162      * The height of this image. This will return -1 if one has not
163      * been set.
164      *
165      * @return The height.
166      */

167     public int getHeight()
168     {
169         return getNumberOrNegativeOne( "H", "Height" );
170     }
171
172     /**
173      * Set the height of the image.
174      *
175      * @param h The height of the image.
176      */

177     public void setHeight( int h )
178     {
179         dictionary.setItem( COSName.getPDFName( "H" ), new COSInteger( h ) );
180     }
181
182     /**
183      * The width of this image. This will return -1 if one has not
184      * been set.
185      *
186      * @return The width.
187      */

188     public int getWidth()
189     {
190         return getNumberOrNegativeOne( "W", "Width" );
191     }
192
193     /**
194      * Set the width of the image.
195      *
196      * @param w The width of the image.
197      */

198     public void setWidth( int w )
199     {
200         dictionary.setItem( COSName.getPDFName( "W" ), new COSInteger( w ) );
201     }
202     
203     /**
204      * This will get the list of filters that are associated with this stream. Or
205      * null if there are none.
206      * @return A list of all encoding filters to apply to this stream.
207      */

208     public List getFilters()
209     {
210         List retval = null;
211         COSBase filters = dictionary.getDictionaryObject( new String JavaDoc[] {"Filter", "F"} );
212         if( filters instanceof COSName )
213         {
214             COSName name = (COSName)filters;
215             retval = new COSArrayList( name.getName(), name, dictionary, "Filter" );
216         }
217         else if( filters instanceof COSArray )
218         {
219             retval = COSArrayList.convertCOSNameCOSArrayToList( (COSArray)filters );
220         }
221         return retval;
222     }
223     
224     /**
225      * This will set the filters that are part of this stream.
226      *
227      * @param filters The filters that are part of this stream.
228      */

229     public void setFilters( List filters )
230     {
231         COSBase obj = COSArrayList.convertStringListToCOSNameCOSArray( filters );
232         dictionary.setItem( "Filter", obj );
233     }
234 }
Popular Tags