1 31 package org.pdfbox.pdmodel.graphics.color; 32 33 import org.pdfbox.cos.COSArray; 34 import org.pdfbox.cos.COSBase; 35 import org.pdfbox.cos.COSInteger; 36 import org.pdfbox.cos.COSFloat; 37 import org.pdfbox.cos.COSName; 38 import org.pdfbox.cos.COSNumber; 39 import org.pdfbox.cos.COSStream; 40 41 import org.pdfbox.pdmodel.PDDocument; 42 import org.pdfbox.pdmodel.common.COSArrayList; 43 import org.pdfbox.pdmodel.common.PDRange; 44 import org.pdfbox.pdmodel.common.PDStream; 45 46 import java.awt.Transparency ; 47 import java.awt.color.ColorSpace ; 48 import java.awt.color.ICC_ColorSpace ; 49 import java.awt.color.ICC_Profile ; 50 import java.awt.image.ColorModel ; 51 import java.awt.image.ComponentColorModel ; 52 import java.awt.image.DataBuffer ; 53 54 import java.io.InputStream ; 55 import java.io.IOException ; 56 57 import java.util.ArrayList ; 58 import java.util.List ; 59 60 66 public class PDICCBased extends PDColorSpace 67 { 68 71 public static final String NAME = "ICCBased"; 72 73 private COSArray array; 74 private PDStream stream; 75 76 81 public PDICCBased( PDDocument doc ) 82 { 83 array = new COSArray(); 84 array.add( COSName.getPDFName( NAME ) ); 85 array.add( new PDStream( doc ) ); 86 } 87 88 93 public PDICCBased( COSArray iccArray ) 94 { 95 array = iccArray; 96 stream = new PDStream( (COSStream)iccArray.getObject( 1 ) ); 97 } 98 99 104 public String getName() 105 { 106 return NAME; 107 } 108 109 114 public COSBase getCOSObject() 115 { 116 return array; 117 } 118 119 124 public PDStream getPDStream() 125 { 126 return stream; 127 } 128 129 136 public ColorSpace createColorSpace() throws IOException 137 { 138 InputStream profile = null; 139 ColorSpace cSpace = null; 140 try 141 { 142 profile = stream.createInputStream(); 143 ICC_Profile iccProfile = ICC_Profile.getInstance( profile ); 144 cSpace = new ICC_ColorSpace ( iccProfile ); 145 } 146 finally 147 { 148 if( profile != null ) 149 { 150 profile.close(); 151 } 152 } 153 return cSpace; 154 } 155 156 165 public ColorModel createColorModel( int bpc ) throws IOException 166 { 167 int[] nbBits = { bpc, bpc, bpc }; 168 ComponentColorModel componentColorModel = 169 new ComponentColorModel ( createColorSpace(), 170 nbBits, 171 false, 172 false, 173 Transparency.OPAQUE, 174 DataBuffer.TYPE_BYTE ); 175 176 return componentColorModel; 177 } 178 179 187 public int getNumberOfComponents() throws IOException 188 { 189 COSNumber n = (COSNumber)stream.getStream().getDictionaryObject( COSName.getPDFName( "N" ) ); 190 return n.intValue(); 191 } 192 193 198 public void setNumberOfComponents( int n ) 199 { 200 stream.getStream().setItem( COSName.getPDFName( "N" ), new COSInteger( n ) ); 201 } 202 203 211 public List getAlternateColorSpaces() throws IOException 212 { 213 COSBase alternate = stream.getStream().getDictionaryObject( COSName.getPDFName( "Alternate" ) ); 214 COSArray alternateArray = null; 215 if( alternate == null ) 216 { 217 alternateArray = new COSArray(); 218 int numComponents = getNumberOfComponents(); 219 String csName = null; 220 if( numComponents == 1 ) 221 { 222 csName = PDDeviceGray.NAME; 223 } 224 else if( numComponents == 3 ) 225 { 226 csName = PDDeviceRGB.NAME; 227 } 228 else if( numComponents == 4 ) 229 { 230 csName = PDDeviceCMYK.NAME; 231 } 232 else 233 { 234 throw new IOException ( "Unknown colorspace number of components:" + numComponents ); 235 } 236 alternateArray.add( COSName.getPDFName( csName ) ); 237 } 238 else 239 { 240 if( alternate instanceof COSArray ) 241 { 242 alternateArray = (COSArray)alternate; 243 } 244 else if( alternate instanceof COSName ) 245 { 246 alternateArray = new COSArray(); 247 alternateArray.add( alternate ); 248 } 249 else 250 { 251 throw new IOException ( "Error: expected COSArray or COSName and not " + 252 alternate.getClass().getName() ); 253 } 254 } 255 List retval = new ArrayList(); 256 for( int i=0; i<alternateArray.size(); i++ ) 257 { 258 retval.add( PDColorSpaceFactory.createColorSpace( alternateArray.get( i ) ) ); 259 } 260 return new COSArrayList( retval, alternateArray ); 261 } 262 263 269 public void setAlternateColorSpaces( List list ) 270 { 271 COSArray altArray = null; 272 if( list != null ) 273 { 274 altArray = COSArrayList.converterToCOSArray( list ); 275 } 276 stream.getStream().setItem( COSName.getPDFName( "Alternate" ), altArray ); 277 } 278 279 private COSArray getRangeArray( int n ) 280 { 281 COSArray rangeArray = (COSArray)stream.getStream().getDictionaryObject( COSName.getPDFName( "Range" ) ); 282 if( rangeArray == null ) 283 { 284 rangeArray = new COSArray(); 285 stream.getStream().setItem( COSName.getPDFName( "Range" ), rangeArray ); 286 while( rangeArray.size() < n*2 ) 287 { 288 rangeArray.add( new COSFloat( -100 ) ); 289 rangeArray.add( new COSFloat( 100 ) ); 290 } 291 } 292 return rangeArray; 293 } 294 295 304 public PDRange getRangeForComponent( int n ) 305 { 306 COSArray rangeArray = getRangeArray( n ); 307 return new PDRange( rangeArray, n ); 308 } 309 310 316 public void setRangeForComponent( PDRange range, int n ) 317 { 318 COSArray rangeArray = getRangeArray( n ); 319 rangeArray.set( n*2, new COSFloat( range.getMin() ) ); 320 rangeArray.set( n*2+1, new COSFloat( range.getMax() ) ); 321 } 322 323 329 public COSStream getMetadata() 330 { 331 return (COSStream)stream.getStream().getDictionaryObject( COSName.getPDFName( "Metadata" ) ); 332 } 333 334 339 public void setMetadata( COSStream metadata ) 340 { 341 stream.getStream().setItem( COSName.getPDFName( "Metadata" ), metadata ); 342 } 343 } | Popular Tags |