1 31 package org.pdfbox.util; 32 33 import org.pdfbox.cos.COSArray; 34 import org.pdfbox.cos.COSBase; 35 import org.pdfbox.cos.COSDictionary; 36 import org.pdfbox.cos.COSInteger; 37 import org.pdfbox.cos.COSName; 38 import org.pdfbox.cos.COSNumber; 39 40 import org.pdfbox.pdmodel.common.COSArrayList; 41 import org.pdfbox.pdmodel.graphics.color.PDColorSpace; 42 import org.pdfbox.pdmodel.graphics.color.PDColorSpaceFactory; 43 44 import java.io.IOException ; 45 import java.util.List ; 46 47 53 public class ImageParameters 54 { 55 private COSDictionary dictionary; 56 57 60 public ImageParameters() 61 { 62 dictionary = new COSDictionary(); 63 } 64 65 70 public ImageParameters( COSDictionary params ) 71 { 72 dictionary = params; 73 } 74 75 80 public COSDictionary getDictionary() 81 { 82 return dictionary; 83 } 84 85 private COSBase getCOSObject( String abbreviatedName, String name ) 86 { 87 COSBase retval = dictionary.getDictionaryObject( COSName.getPDFName( abbreviatedName ) ); 88 if( retval == null ) 89 { 90 retval = dictionary.getDictionaryObject( COSName.getPDFName( name ) ); 91 } 92 return retval; 93 } 94 95 private int getNumberOrNegativeOne( String abbreviatedName, String name ) 96 { 97 int retval = -1; 98 COSNumber number = (COSNumber)getCOSObject( abbreviatedName, name ); 99 if( number != null ) 100 { 101 retval = number.intValue(); 102 } 103 return retval; 104 } 105 106 112 public int getBitsPerComponent() 113 { 114 return getNumberOrNegativeOne( "BPC", "BitsPerComponent" ); 115 } 116 117 122 public void setBitsPerComponent( int bpc ) 123 { 124 dictionary.setItem( COSName.getPDFName( "BPC" ), new COSInteger( bpc ) ); 125 } 126 127 128 135 public PDColorSpace getColorSpace() throws IOException 136 { 137 COSBase cs = getCOSObject( "CS", "ColorSpace" ); 138 PDColorSpace retval = null; 139 if( cs != null ) 140 { 141 retval = PDColorSpaceFactory.createColorSpace( cs ); 142 } 143 return retval; 144 } 145 146 151 public void setColorSpace( PDColorSpace cs ) 152 { 153 COSBase base = null; 154 if( cs != null ) 155 { 156 base = cs.getCOSObject(); 157 } 158 dictionary.setItem( COSName.getPDFName( "CS" ), base ); 159 } 160 161 167 public int getHeight() 168 { 169 return getNumberOrNegativeOne( "H", "Height" ); 170 } 171 172 177 public void setHeight( int h ) 178 { 179 dictionary.setItem( COSName.getPDFName( "H" ), new COSInteger( h ) ); 180 } 181 182 188 public int getWidth() 189 { 190 return getNumberOrNegativeOne( "W", "Width" ); 191 } 192 193 198 public void setWidth( int w ) 199 { 200 dictionary.setItem( COSName.getPDFName( "W" ), new COSInteger( w ) ); 201 } 202 203 208 public List getFilters() 209 { 210 List retval = null; 211 COSBase filters = dictionary.getDictionaryObject( new String [] {"Filter", "F"} ); 212 if( filters instanceof COSName ) 213 { 214 COSName name = (COSName)filters; 215 retval = new COSArrayList( name.getName(), name, dictionary, "Filter" ); 216 } 217 else if( filters instanceof COSArray ) 218 { 219 retval = COSArrayList.convertCOSNameCOSArrayToList( (COSArray)filters ); 220 } 221 return retval; 222 } 223 224 229 public void setFilters( List filters ) 230 { 231 COSBase obj = COSArrayList.convertStringListToCOSNameCOSArray( filters ); 232 dictionary.setItem( "Filter", obj ); 233 } 234 } | Popular Tags |