KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * Copyright (c) 2003-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.BufferedImage JavaDoc;
34 import java.awt.image.ColorModel JavaDoc;
35 import java.awt.image.DataBuffer JavaDoc;
36 import java.awt.image.DataBufferByte JavaDoc;
37 import java.awt.image.DataBufferInt JavaDoc;
38 import java.awt.image.IndexColorModel JavaDoc;
39 import java.awt.image.WritableRaster JavaDoc;
40 import java.io.ByteArrayInputStream JavaDoc;
41 import java.io.ByteArrayOutputStream JavaDoc;
42 import java.io.IOException JavaDoc;
43 import java.util.List JavaDoc;
44
45 import org.pdfbox.filter.Filter;
46 import org.pdfbox.filter.FilterManager;
47 import org.pdfbox.pdmodel.graphics.color.PDColorSpace;
48 import org.pdfbox.util.ImageParameters;
49
50 /**
51  * This class represents an inlined image.
52  *
53  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
54  * @version $Revision: 1.5 $
55  */

56 public class PDInlinedImage
57 {
58     private ImageParameters params;
59     private byte[] imageData;
60
61     /**
62      * This will get the image parameters.
63      *
64      * @return The image parameters.
65      */

66     public ImageParameters getImageParameters()
67     {
68         return params;
69     }
70
71     /**
72      * This will set the image parameters for this image.
73      *
74      * @param imageParams The imageParams.
75      */

76     public void setImageParameters( ImageParameters imageParams )
77     {
78         params = imageParams;
79     }
80
81     /**
82      * Get the bytes for the image.
83      *
84      * @return The image data.
85      */

86     public byte[] getImageData()
87     {
88         return imageData;
89     }
90
91     /**
92      * Set the bytes that make up the image.
93      *
94      * @param value The image data.
95      */

96     public void setImageData(byte[] value)
97     {
98         imageData = value;
99     }
100
101     /**
102      * This will take the inlined image information and create a java.awt.Image from
103      * it.
104      *
105      * @return The image that this object represents.
106      *
107      * @throws IOException If there is an error creating the image.
108      */

109     public BufferedImage JavaDoc createImage() throws IOException JavaDoc
110     {
111         /*
112          * This was the previous implementation, not sure which is better right now.
113          * byte[] transparentColors = new byte[]{(byte)0xFF,(byte)0xFF};
114         byte[] colors=new byte[]{0, (byte)0xFF};
115         IndexColorModel colorModel = new IndexColorModel( 1, 2, colors, colors, colors, transparentColors );
116         BufferedImage image = new BufferedImage(
117             params.getWidth(),
118             params.getHeight(),
119             BufferedImage.TYPE_BYTE_BINARY,
120             colorModel );
121         DataBufferByte buffer = new DataBufferByte( getImageData(), 1 );
122         WritableRaster raster =
123             Raster.createPackedRaster(
124                 buffer,
125                 params.getWidth(),
126                 params.getHeight(),
127                 params.getBitsPerComponent(),
128                 new Point(0,0) );
129         image.setData( raster );
130         return image;
131          */

132         
133         
134         //verify again pci32.pdf before changing below
135
PDColorSpace pcs = params.getColorSpace();
136         ColorModel JavaDoc colorModel = null;
137         if(pcs != null)
138         {
139             colorModel =
140                 params.getColorSpace().createColorModel(
141                         params.getBitsPerComponent() );
142         }
143         else
144         {
145             byte[] transparentColors = new
146             byte[]{(byte)0xFF,(byte)0xFF};
147             byte[] colors=new byte[]{0, (byte)0xFF};
148             colorModel = new IndexColorModel JavaDoc( 1, 2,
149                     colors, colors, colors, transparentColors );
150         }
151         List JavaDoc filters = params.getFilters();
152         byte[] finalData = null;
153         if( filters == null )
154         {
155             finalData = getImageData();
156         }
157         else
158         {
159             ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc( getImageData() );
160             ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc(getImageData().length);
161             FilterManager filterManager = new FilterManager();
162             for( int i=0; i<filters.size(); i++ )
163             {
164                 out.reset();
165                 Filter filter = filterManager.getFilter( (String JavaDoc)filters.get( i ) );
166                 filter.decode( in, out, params.getDictionary() );
167                 in = new ByteArrayInputStream JavaDoc( out.toByteArray() );
168             }
169             finalData = out.toByteArray();
170         }
171
172         WritableRaster JavaDoc raster = colorModel.createCompatibleWritableRaster( params.getWidth(), params.getHeight() );
173         /* Raster.createPackedRaster(
174                 buffer,
175                 params.getWidth(),
176                 params.getHeight(),
177                 params.getBitsPerComponent(),
178                 new Point(0,0) );
179                 */

180         DataBuffer JavaDoc rasterBuffer = raster.getDataBuffer();
181         if( rasterBuffer instanceof DataBufferByte JavaDoc )
182         {
183             DataBufferByte JavaDoc byteBuffer = (DataBufferByte JavaDoc)rasterBuffer;
184             byte[] data = byteBuffer.getData();
185             System.arraycopy( finalData, 0, data, 0, data.length );
186         }
187         else if( rasterBuffer instanceof DataBufferInt JavaDoc )
188         {
189             DataBufferInt JavaDoc byteBuffer = (DataBufferInt JavaDoc)rasterBuffer;
190             int[] data = byteBuffer.getData();
191             for( int i=0; i<finalData.length; i++ )
192             {
193                 data[i] = (finalData[i]+256)%256;
194             }
195         }
196         BufferedImage JavaDoc image = new BufferedImage JavaDoc(
197                 colorModel, raster, false, null );
198         image.setData( raster );
199         return image;
200     }
201 }
Popular Tags