1 31 package org.pdfbox.pdmodel.graphics.xobject; 32 33 import java.awt.image.BufferedImage ; 34 import java.awt.image.ColorModel ; 35 import java.awt.image.DataBuffer ; 36 import java.awt.image.DataBufferByte ; 37 import java.awt.image.DataBufferInt ; 38 import java.awt.image.IndexColorModel ; 39 import java.awt.image.WritableRaster ; 40 import java.io.ByteArrayInputStream ; 41 import java.io.ByteArrayOutputStream ; 42 import java.io.IOException ; 43 import java.util.List ; 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 56 public class PDInlinedImage 57 { 58 private ImageParameters params; 59 private byte[] imageData; 60 61 66 public ImageParameters getImageParameters() 67 { 68 return params; 69 } 70 71 76 public void setImageParameters( ImageParameters imageParams ) 77 { 78 params = imageParams; 79 } 80 81 86 public byte[] getImageData() 87 { 88 return imageData; 89 } 90 91 96 public void setImageData(byte[] value) 97 { 98 imageData = value; 99 } 100 101 109 public BufferedImage createImage() throws IOException 110 { 111 132 133 134 PDColorSpace pcs = params.getColorSpace(); 136 ColorModel 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 ( 1, 2, 149 colors, colors, colors, transparentColors ); 150 } 151 List filters = params.getFilters(); 152 byte[] finalData = null; 153 if( filters == null ) 154 { 155 finalData = getImageData(); 156 } 157 else 158 { 159 ByteArrayInputStream in = new ByteArrayInputStream ( getImageData() ); 160 ByteArrayOutputStream out = new ByteArrayOutputStream (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 )filters.get( i ) ); 166 filter.decode( in, out, params.getDictionary() ); 167 in = new ByteArrayInputStream ( out.toByteArray() ); 168 } 169 finalData = out.toByteArray(); 170 } 171 172 WritableRaster raster = colorModel.createCompatibleWritableRaster( params.getWidth(), params.getHeight() ); 173 180 DataBuffer rasterBuffer = raster.getDataBuffer(); 181 if( rasterBuffer instanceof DataBufferByte ) 182 { 183 DataBufferByte byteBuffer = (DataBufferByte )rasterBuffer; 184 byte[] data = byteBuffer.getData(); 185 System.arraycopy( finalData, 0, data, 0, data.length ); 186 } 187 else if( rasterBuffer instanceof DataBufferInt ) 188 { 189 DataBufferInt byteBuffer = (DataBufferInt )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 image = new BufferedImage ( 197 colorModel, raster, false, null ); 198 image.setData( raster ); 199 return image; 200 } 201 } | Popular Tags |