1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.pdfbox.pdmodel.graphics.xobject; 18 19 import java.awt.image.BufferedImage; 20 import java.awt.image.ColorModel; 21 import java.awt.image.DataBuffer; 22 import java.awt.image.DataBufferByte; 23 import java.awt.image.DataBufferInt; 24 import java.awt.image.IndexColorModel; 25 import java.awt.image.WritableRaster; 26 import java.io.ByteArrayInputStream; 27 import java.io.ByteArrayOutputStream; 28 import java.io.IOException; 29 import java.util.List; 30 import java.util.Map; 31 32 import org.apache.pdfbox.filter.Filter; 33 import org.apache.pdfbox.filter.FilterManager; 34 import org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace; 35 import org.apache.pdfbox.util.ImageParameters; 36 37 /** 38 * This class represents an inlined image. 39 * 40 * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a> 41 * @version $Revision: 1.6 $ 42 */ 43 public class PDInlinedImage 44 { 45 private ImageParameters params; 46 private byte[] imageData; 47 48 /** 49 * This will get the image parameters. 50 * 51 * @return The image parameters. 52 */ 53 public ImageParameters getImageParameters() 54 { 55 return params; 56 } 57 58 /** 59 * This will set the image parameters for this image. 60 * 61 * @param imageParams The imageParams. 62 */ 63 public void setImageParameters( ImageParameters imageParams ) 64 { 65 params = imageParams; 66 } 67 68 /** 69 * Get the bytes for the image. 70 * 71 * @return The image data. 72 */ 73 public byte[] getImageData() 74 { 75 return imageData; 76 } 77 78 /** 79 * Set the bytes that make up the image. 80 * 81 * @param value The image data. 82 */ 83 public void setImageData(byte[] value) 84 { 85 imageData = value; 86 } 87 88 /** 89 * This will take the inlined image information and create a java.awt.Image from 90 * it. 91 * 92 * @return The image that this object represents. 93 * 94 * @throws IOException If there is an error creating the image. 95 */ 96 public BufferedImage createImage() throws IOException 97 { 98 return createImage( null ); 99 } 100 101 /** 102 * This will take the inlined image information and create a java.awt.Image from 103 * it. 104 * 105 * @param colorSpaces The ColorSpace dictionary from the current resources, if any. 106 * 107 * @return The image that this object represents. 108 * 109 * @throws IOException If there is an error creating the image. 110 */ 111 public BufferedImage createImage( Map colorSpaces ) throws IOException 112 { 113 /* 114 * This was the previous implementation, not sure which is better right now. 115 * byte[] transparentColors = new byte[]{(byte)0xFF,(byte)0xFF}; 116 byte[] colors=new byte[]{0, (byte)0xFF}; 117 IndexColorModel colorModel = new IndexColorModel( 1, 2, colors, colors, colors, transparentColors ); 118 BufferedImage image = new BufferedImage( 119 params.getWidth(), 120 params.getHeight(), 121 BufferedImage.TYPE_BYTE_BINARY, 122 colorModel ); 123 DataBufferByte buffer = new DataBufferByte( getImageData(), 1 ); 124 WritableRaster raster = 125 Raster.createPackedRaster( 126 buffer, 127 params.getWidth(), 128 params.getHeight(), 129 params.getBitsPerComponent(), 130 new Point(0,0) ); 131 image.setData( raster ); 132 return image; 133 */ 134 135 136 //verify again pci32.pdf before changing below 137 PDColorSpace pcs = params.getColorSpace( colorSpaces ); 138 ColorModel colorModel = null; 139 if(pcs != null) 140 { 141 colorModel = 142 pcs.createColorModel( 143 params.getBitsPerComponent() ); 144 } 145 else 146 { 147 byte[] transparentColors = new 148 byte[]{(byte)0xFF,(byte)0xFF}; 149 byte[] colors=new byte[]{0, (byte)0xFF}; 150 colorModel = new IndexColorModel( 1, 2, 151 colors, colors, colors, transparentColors ); 152 } 153 List filters = params.getFilters(); 154 byte[] finalData = null; 155 if( filters == null ) 156 { 157 finalData = getImageData(); 158 } 159 else 160 { 161 ByteArrayInputStream in = new ByteArrayInputStream( getImageData() ); 162 ByteArrayOutputStream out = new ByteArrayOutputStream(getImageData().length); 163 FilterManager filterManager = new FilterManager(); 164 for( int i=0; i<filters.size(); i++ ) 165 { 166 out.reset(); 167 Filter filter = filterManager.getFilter( (String)filters.get( i ) ); 168 filter.decode( in, out, params.getDictionary(), i ); 169 in = new ByteArrayInputStream( out.toByteArray() ); 170 } 171 finalData = out.toByteArray(); 172 } 173 174 WritableRaster raster = colorModel.createCompatibleWritableRaster( params.getWidth(), params.getHeight() ); 175 /* Raster.createPackedRaster( 176 buffer, 177 params.getWidth(), 178 params.getHeight(), 179 params.getBitsPerComponent(), 180 new Point(0,0) ); 181 */ 182 DataBuffer rasterBuffer = raster.getDataBuffer(); 183 if( rasterBuffer instanceof DataBufferByte ) 184 { 185 DataBufferByte byteBuffer = (DataBufferByte)rasterBuffer; 186 byte[] data = byteBuffer.getData(); 187 System.arraycopy( finalData, 0, data, 0, data.length ); 188 } 189 else if( rasterBuffer instanceof DataBufferInt ) 190 { 191 DataBufferInt byteBuffer = (DataBufferInt)rasterBuffer; 192 int[] data = byteBuffer.getData(); 193 for( int i=0; i<finalData.length; i++ ) 194 { 195 data[i] = (finalData[i]+256)%256; 196 } 197 } 198 BufferedImage image = new BufferedImage( 199 colorModel, raster, false, null ); 200 image.setData( raster ); 201 return image; 202 } 203 }