1 31 package org.pdfbox.pdmodel.graphics.xobject; 32 33 import java.awt.image.BufferedImage ; 34 import java.io.FileOutputStream ; 35 import java.io.IOException ; 36 import java.io.OutputStream ; 37 38 import org.pdfbox.cos.COSBase; 39 import org.pdfbox.cos.COSName; 40 import org.pdfbox.pdmodel.PDDocument; 41 import org.pdfbox.pdmodel.common.PDStream; 42 import org.pdfbox.pdmodel.graphics.color.PDColorSpace; 43 import org.pdfbox.pdmodel.graphics.color.PDColorSpaceFactory; 44 import org.pdfbox.pdmodel.graphics.color.PDDeviceGray; 45 46 53 public abstract class PDXObjectImage extends PDXObject 54 { 55 58 public static final String SUB_TYPE = "Image"; 59 60 63 private String suffix; 64 65 71 public PDXObjectImage(PDStream imageStream, String fileSuffix) 72 { 73 super( imageStream ); 74 suffix = fileSuffix; 75 } 76 77 83 public PDXObjectImage(PDDocument doc, String fileSuffix) 84 { 85 super( doc ); 86 getCOSStream().setName( COSName.SUBTYPE, SUB_TYPE ); 87 suffix = fileSuffix; 88 } 89 90 97 public abstract BufferedImage getRGBImage() throws IOException ; 98 99 104 public abstract void write2OutputStream(OutputStream out) throws IOException ; 105 106 112 public void write2file(String filename) throws IOException 113 { 114 FileOutputStream out = null; 115 try 116 { 117 out = new FileOutputStream (filename + "." + suffix); 118 write2OutputStream(out); 119 out.flush(); 120 } 121 finally 122 { 123 if( out != null ) 124 { 125 out.close(); 126 } 127 } 128 } 129 130 135 public int getHeight() 136 { 137 return getCOSStream().getInt( "Height", -1 ); 138 } 139 140 145 public void setHeight( int height ) 146 { 147 getCOSStream().setInt( "Height", height ); 148 } 149 150 155 public int getWidth() 156 { 157 return getCOSStream().getInt( "Width", -1 ); 158 } 159 160 165 public void setWidth( int width ) 166 { 167 getCOSStream().setInt( "Width", width ); 168 } 169 170 176 public int getBitsPerComponent() 177 { 178 return getCOSStream().getInt( new String [] { "BPC", "BitsPerComponent"}, -1 ); 179 } 180 181 186 public void setBitsPerComponent( int bpc ) 187 { 188 getCOSStream().setInt( "BitsPerComponent", bpc ); 189 } 190 191 198 public PDColorSpace getColorSpace() throws IOException 199 { 200 COSBase cs = getCOSStream().getDictionaryObject( new String []{ "CS", "ColorSpace" } ); 201 PDColorSpace retval = null; 202 if( cs != null ) 203 { 204 retval = PDColorSpaceFactory.createColorSpace( cs ); 205 } 206 else 207 { 208 COSBase filter = getCOSStream().getDictionaryObject( "Filter" ); 211 if( COSName.CCITTFAX_DECODE.equals( filter ) || 212 COSName.CCITTFAX_DECODE_ABBREVIATION.equals( filter ) ) 213 { 214 retval = new PDDeviceGray(); 215 } 216 } 217 return retval; 218 } 219 220 225 public void setColorSpace( PDColorSpace cs ) 226 { 227 COSBase base = null; 228 if( cs != null ) 229 { 230 base = cs.getCOSObject(); 231 } 232 getCOSStream().setItem( COSName.getPDFName( "ColorSpace" ), base ); 233 } 234 235 240 public String getSuffix() 241 { 242 return suffix; 243 } 244 } 245 | Popular Tags |