KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > pdmodel > graphics > xobject > PDXObjectImage


1 /**
2  * Copyright (c) 2004-2005, 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.xobject;
32
33 import java.awt.image.BufferedImage JavaDoc;
34 import java.io.FileOutputStream JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.io.OutputStream JavaDoc;
37
38 import org.pdfbox.cos.COSBase;
39 import org.pdfbox.cos.COSName;
40 import org.pdfbox.pdmodel.PDDocument;
41 import org.pdfbox.pdmodel.common.PDStream;
42 import org.pdfbox.pdmodel.graphics.color.PDColorSpace;
43 import org.pdfbox.pdmodel.graphics.color.PDColorSpaceFactory;
44 import org.pdfbox.pdmodel.graphics.color.PDDeviceGray;
45
46 /**
47  * The prototype for all PDImages.
48  *
49  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
50  * @author mathiak
51  * @version $Revision: 1.9 $
52  */

53 public abstract class PDXObjectImage extends PDXObject
54 {
55     /**
56      * The XObject subtype.
57      */

58     public static final String JavaDoc SUB_TYPE = "Image";
59     
60     /**
61      * This contains the suffix used when writing to file.
62      */

63     private String JavaDoc suffix;
64     
65     /**
66      * Standard constuctor.
67      *
68      * @param imageStream The XObject is passed as a COSStream.
69      * @param fileSuffix The file suffix, jpg/png.
70      */

71     public PDXObjectImage(PDStream imageStream, String JavaDoc fileSuffix)
72     {
73         super( imageStream );
74         suffix = fileSuffix;
75     }
76     
77     /**
78      * Standard constuctor.
79      *
80      * @param doc The document to store the stream in.
81      * @param fileSuffix The file suffix, jpg/png.
82      */

83     public PDXObjectImage(PDDocument doc, String JavaDoc fileSuffix)
84     {
85         super( doc );
86         getCOSStream().setName( COSName.SUBTYPE, SUB_TYPE );
87         suffix = fileSuffix;
88     }
89     
90     /**
91      * Returns an java.awt.Image, that can be used for display etc.
92      *
93      * @return This PDF object as an AWT image.
94      *
95      * @throws IOException If there is an error creating the image.
96      */

97     public abstract BufferedImage JavaDoc getRGBImage() throws IOException JavaDoc;
98     
99     /**
100      * Writes the Image to out.
101      * @param out the OutputStream that the Image is written to.
102      * @throws IOException when somethings wrong with out
103      */

104     public abstract void write2OutputStream(OutputStream JavaDoc out) throws IOException JavaDoc;
105     
106     /**
107      * Writes the image to a file with the filename + an appropriate suffix, like "Image.jpg".
108      * The suffix is automatically set by the
109      * @param filename the filename
110      * @throws IOException When somethings wrong with the corresponding file.
111      */

112     public void write2file(String JavaDoc filename) throws IOException JavaDoc
113     {
114         FileOutputStream JavaDoc out = null;
115         try
116         {
117             out = new FileOutputStream JavaDoc(filename + "." + suffix);
118             write2OutputStream(out);
119             out.flush();
120         }
121         finally
122         {
123             if( out != null )
124             {
125                 out.close();
126             }
127         }
128     }
129     
130     /**
131      * Get the height of the image.
132      *
133      * @return The height of the image.
134      */

135     public int getHeight()
136     {
137         return getCOSStream().getInt( "Height", -1 );
138     }
139     
140     /**
141      * Set the height of the image.
142      *
143      * @param height The height of the image.
144      */

145     public void setHeight( int height )
146     {
147         getCOSStream().setInt( "Height", height );
148     }
149     
150     /**
151      * Get the width of the image.
152      *
153      * @return The width of the image.
154      */

155     public int getWidth()
156     {
157         return getCOSStream().getInt( "Width", -1 );
158     }
159     
160     /**
161      * Set the width of the image.
162      *
163      * @param width The width of the image.
164      */

165     public void setWidth( int width )
166     {
167         getCOSStream().setInt( "Width", width );
168     }
169     
170     /**
171      * The bits per component of this image. This will return -1 if one has not
172      * been set.
173      *
174      * @return The number of bits per component.
175      */

176     public int getBitsPerComponent()
177     {
178         return getCOSStream().getInt( new String JavaDoc[] { "BPC", "BitsPerComponent"}, -1 );
179     }
180
181     /**
182      * Set the number of bits per component.
183      *
184      * @param bpc The number of bits per component.
185      */

186     public void setBitsPerComponent( int bpc )
187     {
188         getCOSStream().setInt( "BitsPerComponent", bpc );
189     }
190     
191     /**
192      * This will get the color space or null if none exists.
193      *
194      * @return The color space for this image.
195      *
196      * @throws IOException If there is an error getting the colorspace.
197      */

198     public PDColorSpace getColorSpace() throws IOException JavaDoc
199     {
200         COSBase cs = getCOSStream().getDictionaryObject( new String JavaDoc[]{ "CS", "ColorSpace" } );
201         PDColorSpace retval = null;
202         if( cs != null )
203         {
204             retval = PDColorSpaceFactory.createColorSpace( cs );
205         }
206         else
207         {
208             //there are some cases where the 'required' CS value is not present
209
//but we know that it will be grayscale for a CCITT filter.
210
COSBase filter = getCOSStream().getDictionaryObject( "Filter" );
211             if( COSName.CCITTFAX_DECODE.equals( filter ) ||
212                 COSName.CCITTFAX_DECODE_ABBREVIATION.equals( filter ) )
213             {
214                 retval = new PDDeviceGray();
215             }
216         }
217         return retval;
218     }
219
220     /**
221      * This will set the color space for this image.
222      *
223      * @param cs The color space for this image.
224      */

225     public void setColorSpace( PDColorSpace cs )
226     {
227         COSBase base = null;
228         if( cs != null )
229         {
230             base = cs.getCOSObject();
231         }
232         getCOSStream().setItem( COSName.getPDFName( "ColorSpace" ), base );
233     }
234     
235     /**
236      * This will get the suffix for this image type, jpg/png.
237      *
238      * @return The image suffix.
239      */

240     public String JavaDoc getSuffix()
241     {
242         return suffix;
243     }
244 }
245
Popular Tags