1 31 package org.pdfbox.pdmodel.graphics.xobject; 32 33 import java.awt.image.DataBufferByte ; 34 import java.awt.image.BufferedImage ; 35 import java.awt.image.ColorModel ; 36 import java.awt.image.WritableRaster ; 37 import java.io.IOException ; 38 import java.io.OutputStream ; 39 40 import javax.imageio.ImageIO ; 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 56 public class PDPixelMap extends PDXObjectImage 57 { 58 private BufferedImage image = null; 59 60 64 public PDPixelMap(PDStream pdStream) 65 { 66 super(pdStream, "png"); 67 } 68 69 77 114 115 123 public BufferedImage getRGBImage() throws IOException 124 { 125 if( image != null ) 126 { 127 return image; 128 } 129 130 int width = getWidth(); 133 int height = getHeight(); 134 int bpc = getBitsPerComponent(); 135 byte[] array = getPDStream().getByteArray(); 139 140 PDColorSpace colorspace = getColorSpace(); 142 ColorModel cm = colorspace.createColorModel( bpc ); 143 WritableRaster raster = cm.createCompatibleWritableRaster( width, height ); 144 DataBufferByte buffer = (DataBufferByte )raster.getDataBuffer(); 146 byte[] bufferData = buffer.getData(); 147 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 (cm, raster, false, null); 156 return image; 157 } 158 159 164 public void write2OutputStream(OutputStream out) throws IOException 165 { 166 getRGBImage(); 167 if (image!=null) 168 { 169 ImageIO.write(image, "png", out); 170 } 171 } 172 173 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 return null; } 199 else 200 { 201 return null; 202 } 203 } 204 return null; 205 } 206 207 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 |