001: /**
002: * Copyright (c) 2003-2006, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox.pdmodel.graphics.xobject;
031:
032: import java.awt.image.BufferedImage;
033: import java.awt.image.ColorModel;
034: import java.awt.image.DataBuffer;
035: import java.awt.image.DataBufferByte;
036: import java.awt.image.DataBufferInt;
037: import java.awt.image.IndexColorModel;
038: import java.awt.image.WritableRaster;
039: import java.io.ByteArrayInputStream;
040: import java.io.ByteArrayOutputStream;
041: import java.io.IOException;
042: import java.util.List;
043:
044: import org.pdfbox.filter.Filter;
045: import org.pdfbox.filter.FilterManager;
046: import org.pdfbox.pdmodel.graphics.color.PDColorSpace;
047: import org.pdfbox.util.ImageParameters;
048:
049: /**
050: * This class represents an inlined image.
051: *
052: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
053: * @version $Revision: 1.5 $
054: */
055: public class PDInlinedImage {
056: private ImageParameters params;
057: private byte[] imageData;
058:
059: /**
060: * This will get the image parameters.
061: *
062: * @return The image parameters.
063: */
064: public ImageParameters getImageParameters() {
065: return params;
066: }
067:
068: /**
069: * This will set the image parameters for this image.
070: *
071: * @param imageParams The imageParams.
072: */
073: public void setImageParameters(ImageParameters imageParams) {
074: params = imageParams;
075: }
076:
077: /**
078: * Get the bytes for the image.
079: *
080: * @return The image data.
081: */
082: public byte[] getImageData() {
083: return imageData;
084: }
085:
086: /**
087: * Set the bytes that make up the image.
088: *
089: * @param value The image data.
090: */
091: public void setImageData(byte[] value) {
092: imageData = value;
093: }
094:
095: /**
096: * This will take the inlined image information and create a java.awt.Image from
097: * it.
098: *
099: * @return The image that this object represents.
100: *
101: * @throws IOException If there is an error creating the image.
102: */
103: public BufferedImage createImage() throws IOException {
104: /*
105: * This was the previous implementation, not sure which is better right now.
106: * byte[] transparentColors = new byte[]{(byte)0xFF,(byte)0xFF};
107: byte[] colors=new byte[]{0, (byte)0xFF};
108: IndexColorModel colorModel = new IndexColorModel( 1, 2, colors, colors, colors, transparentColors );
109: BufferedImage image = new BufferedImage(
110: params.getWidth(),
111: params.getHeight(),
112: BufferedImage.TYPE_BYTE_BINARY,
113: colorModel );
114: DataBufferByte buffer = new DataBufferByte( getImageData(), 1 );
115: WritableRaster raster =
116: Raster.createPackedRaster(
117: buffer,
118: params.getWidth(),
119: params.getHeight(),
120: params.getBitsPerComponent(),
121: new Point(0,0) );
122: image.setData( raster );
123: return image;
124: */
125:
126: //verify again pci32.pdf before changing below
127: PDColorSpace pcs = params.getColorSpace();
128: ColorModel colorModel = null;
129: if (pcs != null) {
130: colorModel = params.getColorSpace().createColorModel(
131: params.getBitsPerComponent());
132: } else {
133: byte[] transparentColors = new byte[] { (byte) 0xFF,
134: (byte) 0xFF };
135: byte[] colors = new byte[] { 0, (byte) 0xFF };
136: colorModel = new IndexColorModel(1, 2, colors, colors,
137: colors, transparentColors);
138: }
139: List filters = params.getFilters();
140: byte[] finalData = null;
141: if (filters == null) {
142: finalData = getImageData();
143: } else {
144: ByteArrayInputStream in = new ByteArrayInputStream(
145: getImageData());
146: ByteArrayOutputStream out = new ByteArrayOutputStream(
147: getImageData().length);
148: FilterManager filterManager = new FilterManager();
149: for (int i = 0; i < filters.size(); i++) {
150: out.reset();
151: Filter filter = filterManager
152: .getFilter((String) filters.get(i));
153: filter.decode(in, out, params.getDictionary());
154: in = new ByteArrayInputStream(out.toByteArray());
155: }
156: finalData = out.toByteArray();
157: }
158:
159: WritableRaster raster = colorModel
160: .createCompatibleWritableRaster(params.getWidth(),
161: params.getHeight());
162: /* Raster.createPackedRaster(
163: buffer,
164: params.getWidth(),
165: params.getHeight(),
166: params.getBitsPerComponent(),
167: new Point(0,0) );
168: */
169: DataBuffer rasterBuffer = raster.getDataBuffer();
170: if (rasterBuffer instanceof DataBufferByte) {
171: DataBufferByte byteBuffer = (DataBufferByte) rasterBuffer;
172: byte[] data = byteBuffer.getData();
173: System.arraycopy(finalData, 0, data, 0, data.length);
174: } else if (rasterBuffer instanceof DataBufferInt) {
175: DataBufferInt byteBuffer = (DataBufferInt) rasterBuffer;
176: int[] data = byteBuffer.getData();
177: for (int i = 0; i < finalData.length; i++) {
178: data[i] = (finalData[i] + 256) % 256;
179: }
180: }
181: BufferedImage image = new BufferedImage(colorModel, raster,
182: false, null);
183: image.setData(raster);
184: return image;
185: }
186: }
|