KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * Copyright (c) 2004-2006, 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.DataBufferByte JavaDoc;
34 import java.awt.image.BufferedImage JavaDoc;
35 import java.awt.image.ColorModel JavaDoc;
36 import java.awt.image.WritableRaster JavaDoc;
37 import java.io.IOException JavaDoc;
38 import java.io.OutputStream JavaDoc;
39
40 import javax.imageio.ImageIO JavaDoc;
41
42 import org.pdfbox.cos.COSArray;
43 import org.pdfbox.cos.COSBase;
44 import org.pdfbox.cos.COSDictionary;
45 import org.pdfbox.pdmodel.common.PDStream;
46
47 import org.pdfbox.pdmodel.graphics.color.PDColorSpace;
48 import org.pdfbox.pdmodel.graphics.predictor.PredictorAlgorithm;
49
50 /**
51  * This class contains a PixelMap Image.
52  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
53  * @author mathiak
54  * @version $Revision: 1.8 $
55  */

56 public class PDPixelMap extends PDXObjectImage
57 {
58     private BufferedImage JavaDoc image = null;
59     
60     /**
61      * Standard constructor. Basically does nothing.
62      * @param pdStream The stream that holds the pixel map.
63      */

64     public PDPixelMap(PDStream pdStream)
65     {
66         super(pdStream, "png");
67     }
68     
69     /**
70      * Construct a pixel map image from an AWT image.
71      *
72      * @param doc The PDF document to embed the image in.
73      * @param awtImage The image to read data from.
74      *
75      * @throws IOException If there is an error while embedding this image.
76      */

77     /*
78      * This method is broken and needs to be implemented, any takers?
79     public PDPixelMap(PDDocument doc, BufferedImage awtImage) throws IOException
80     {
81         super( doc, "png");
82         image = awtImage;
83         setWidth( image.getWidth() );
84         setHeight( image.getHeight() );
85         
86         ColorModel cm = image.getColorModel();
87         ColorSpace cs = cm.getColorSpace();
88         PDColorSpace pdColorSpace = PDColorSpaceFactory.createColorSpace( doc, cs );
89         setColorSpace( pdColorSpace );
90         //setColorSpace( )
91         
92         PDStream stream = getPDStream();
93         OutputStream output = null;
94         try
95         {
96             output = stream.createOutputStream();
97             DataBuffer buffer = awtImage.getRaster().getDataBuffer();
98             if( buffer instanceof DataBufferByte )
99             {
100                 DataBufferByte byteBuffer = (DataBufferByte)buffer;
101                 byte[] data = byteBuffer.getData();
102                 output.write( data );
103             }
104             setBitsPerComponent( cm.getPixelSize() );
105         }
106         finally
107         {
108             if( output != null )
109             {
110                 output.close();
111             }
112         }
113     }*/

114
115     /**
116      * Returns a {@link java.awt.image.BufferedImage} of the COSStream
117      * set in the constructor or null if the COSStream could not be encoded.
118      *
119      * @return {@inheritDoc}
120      *
121      * @throws IOException {@inheritDoc}
122      */

123     public BufferedImage JavaDoc getRGBImage() throws IOException JavaDoc
124     {
125         if( image != null )
126         {
127             return image;
128         }
129
130         //byte[] index =
131
//ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
132
int width = getWidth();
133         int height = getHeight();
134         int bpc = getBitsPerComponent();
135         //COSInteger length =
136
// (COSInteger) stream.getStream().getDictionary().getDictionaryObject(COSName.LENGTH);
137
//byte[] array = new byte[stream.getFilteredStream().];
138
byte[] array = getPDStream().getByteArray();
139
140 // Get the ColorModel right
141
PDColorSpace colorspace = getColorSpace();
142         ColorModel JavaDoc cm = colorspace.createColorModel( bpc );
143         WritableRaster JavaDoc raster = cm.createCompatibleWritableRaster( width, height );
144         //DataBufferByte buffer = (DataBufferByte)raster.getDataBuffer();
145
DataBufferByte JavaDoc buffer = (DataBufferByte JavaDoc)raster.getDataBuffer();
146         byte[] bufferData = buffer.getData();
147         //System.arraycopy( array, 0, bufferData, 0, array.length );
148
int predictor = getPredictor();
149
150         PredictorAlgorithm filter = PredictorAlgorithm.getFilter(predictor);
151         filter.setWidth(width);
152         filter.setHeight(height);
153         filter.setBpp((bpc * 3) / 8);
154         filter.decode(array, bufferData);
155         image = new BufferedImage JavaDoc(cm, raster, false, null);
156         return image;
157     }
158
159     /**
160      * Writes the image as .png.
161      *
162      * {@inheritDoc}
163      */

164     public void write2OutputStream(OutputStream JavaDoc out) throws IOException JavaDoc
165     {
166         getRGBImage();
167         if (image!=null)
168         {
169             ImageIO.write(image, "png", out);
170         }
171     }
172
173     /**
174      * DecodeParms is an optional parameter for filters.
175      *
176      * It is provided if any of the filters has nondefault parameters. If there
177      * is only one filter it is a dictionary, if there are multiple filters it
178      * is an array with an entry for each filter. An array entry can hold a null
179      * value if only the default values are used or a dictionary with
180      * parameters.
181      *
182      * @return The decoding parameters.
183      *
184      */

185     public COSDictionary getDecodeParams()
186     {
187         COSBase decodeParms = getCOSStream().getDictionaryObject("DecodeParms");
188         if (decodeParms != null)
189         {
190             if (decodeParms instanceof COSDictionary)
191             {
192                 return (COSDictionary) decodeParms;
193             }
194             else if (decodeParms instanceof COSArray)
195             {
196                 // not implemented yet, which index should we use?
197
return null;//(COSDictionary)((COSArray)decodeParms).get(0);
198
}
199             else
200             {
201                 return null;
202             }
203         }
204         return null;
205     }
206
207     /**
208      * A code that selects the predictor algorithm.
209      *
210      * <ul>
211      * <li>1 No prediction (the default value)
212      * <li>2 TIFF Predictor 2
213      * <li>10 PNG prediction (on encoding, PNG None on all rows)
214      * <li>11 PNG prediction (on encoding, PNG Sub on all rows)
215      * <li>12 PNG prediction (on encoding, PNG Up on all rows)
216      * <li>13 PNG prediction (on encoding, PNG Average on all rows)
217      * <li>14 PNG prediction (on encoding, PNG Paeth on all rows)
218      * <li>15 PNG prediction (on encoding, PNG optimum)
219      * </ul>
220      *
221      * Default value: 1.
222      *
223      * @return predictor algorithm code
224      */

225     public int getPredictor()
226     {
227         COSDictionary decodeParms = getDecodeParams();
228         if (decodeParms != null)
229         {
230             int i = decodeParms.getInt("Predictor");
231             if (i != -1)
232             {
233                 return i;
234             }
235         }
236         return 1;
237     }
238 }
Popular Tags