KickJava   Java API By Example, From Geeks To Geeks.

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


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.xobject;
32
33 import java.awt.image.BufferedImage JavaDoc;
34 import java.io.InputStream JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.io.OutputStream JavaDoc;
37
38 import java.util.ArrayList JavaDoc;
39 import java.util.List JavaDoc;
40
41 import javax.imageio.ImageIO JavaDoc;
42
43 import org.pdfbox.cos.COSDictionary;
44 import org.pdfbox.cos.COSName;
45
46 import org.pdfbox.pdmodel.PDDocument;
47 import org.pdfbox.pdmodel.common.PDStream;
48 import org.pdfbox.pdmodel.graphics.color.PDDeviceRGB;
49
50 /**
51  * An image class for JPegs.
52  *
53  * @author mathiak
54  * @version $Revision: 1.5 $
55  */

56 public class PDJpeg extends PDXObjectImage
57 {
58     
59     private static final List JavaDoc DCT_FILTERS = new ArrayList JavaDoc();
60     
61     static
62     {
63         DCT_FILTERS.add( COSName.DCT_DECODE.getName() );
64         DCT_FILTERS.add( COSName.DCT_DECODE_ABBREVIATION.getName() );
65     }
66     
67     /**
68      * Standard constructor.
69      *
70      * @param jpeg The COSStream from which to extract the JPeg
71      */

72     public PDJpeg(PDStream jpeg)
73     {
74         super(jpeg, "jpg");
75     }
76     
77     /**
78      * Construct from a stream.
79      *
80      * @param doc The document to create the image as part of.
81      * @param is The stream that contains the jpeg data.
82      * @throws IOException If there is an error reading the jpeg data.
83      */

84     public PDJpeg( PDDocument doc, InputStream JavaDoc is ) throws IOException JavaDoc
85     {
86         super( new PDStream( doc, is, true ), "jpg" );
87         COSDictionary dic = getCOSStream();
88         dic.setItem( COSName.FILTER, COSName.DCT_DECODE );
89         dic.setItem( COSName.SUBTYPE, COSName.IMAGE);
90         dic.setItem( COSName.TYPE, COSName.getPDFName( "XObject" ) );
91         
92         BufferedImage JavaDoc image = getRGBImage();
93         setBitsPerComponent( 8 );
94         setColorSpace( PDDeviceRGB.INSTANCE );
95         setHeight( image.getHeight() );
96         setWidth( image.getWidth() );
97         
98     }
99     
100     /**
101      * Construct from a buffered image.
102      *
103      * @param doc The document to create the image as part of.
104      * @param bi The image to convert to a jpeg
105      * @throws IOException If there is an error processing the jpeg data.
106      */

107     public PDJpeg( PDDocument doc, BufferedImage JavaDoc bi ) throws IOException JavaDoc
108     {
109         super( new PDStream( doc ), "jpg" );
110         
111         java.io.OutputStream JavaDoc os = getCOSStream().createFilteredStream();
112         try
113         {
114             
115             ImageIO.write(bi,"jpeg",os);
116             
117             COSDictionary dic = getCOSStream();
118             dic.setItem( COSName.FILTER, COSName.DCT_DECODE );
119             dic.setItem( COSName.SUBTYPE, COSName.IMAGE);
120             dic.setItem( COSName.TYPE, COSName.getPDFName( "XObject" ) );
121         
122             setBitsPerComponent( 8 );
123             setColorSpace( PDDeviceRGB.INSTANCE );
124             setHeight( bi.getHeight() );
125             setWidth( bi.getWidth() );
126         }
127         finally
128         {
129             os.close();
130         }
131     }
132     
133     /**
134      * Returns an image of the JPeg, or null if JPegs are not supported. (They should be. )
135      * {@inheritDoc}
136      */

137     public BufferedImage JavaDoc getRGBImage() throws IOException JavaDoc
138     {
139         return ImageIO.read(getPDStream().getPartiallyFilteredStream( DCT_FILTERS ));
140     }
141     
142     /**
143      * This writes the JPeg to out.
144      * {@inheritDoc}
145      */

146     public void write2OutputStream(OutputStream JavaDoc out) throws IOException JavaDoc
147     {
148         InputStream JavaDoc data = getPDStream().getPartiallyFilteredStream( DCT_FILTERS );
149         byte[] buf = new byte[1024];
150         int amountRead = -1;
151         while( (amountRead = data.read( buf )) != -1 )
152         {
153             out.write( buf, 0, amountRead );
154         }
155     }
156 }
157
Popular Tags