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.COSName; 37 import org.pdfbox.cos.COSNumber; 38 import org.pdfbox.cos.COSStream; 39 import org.pdfbox.cos.COSString; 40 41 import java.awt.color.ColorSpace ; 42 import java.awt.image.ColorModel ; 43 import java.awt.image.IndexColorModel ; 44 45 import java.io.InputStream ; 46 import java.io.ByteArrayOutputStream ; 47 import java.io.IOException ; 48 49 55 public class PDIndexed extends PDColorSpace 56 { 57 58 61 public static final String NAME = "Indexed"; 62 63 66 public static final String ABBREVIATED_NAME = "I"; 67 68 private COSArray array; 69 70 73 public PDIndexed() 74 { 75 array = new COSArray(); 76 array.add( COSName.getPDFName( NAME ) ); 77 array.add( COSName.getPDFName( PDDeviceRGB.NAME ) ); 78 array.add( new COSInteger( 255 ) ); 79 array.add( org.pdfbox.cos.COSNull.NULL ); 80 } 81 82 87 public PDIndexed( COSArray indexedArray ) 88 { 89 array = indexedArray; 90 } 91 92 100 public int getNumberOfComponents() throws IOException 101 { 102 return getBaseColorSpace().getNumberOfComponents(); 103 } 104 105 110 public String getName() 111 { 112 return NAME; 113 } 114 115 122 public ColorSpace createColorSpace() throws IOException 123 { 124 throw new IOException ( "Not implemented" ); 125 } 126 127 136 public ColorModel createColorModel( int bpc ) throws IOException 137 { 138 int size = getHighValue(); 139 byte[] index = getLookupData(); 140 142 ColorModel cm = new IndexColorModel (bpc, size+1, index,0,false); 143 return cm; 144 } 145 146 153 public PDColorSpace getBaseColorSpace() throws IOException 154 { 155 PDColorSpace retval = null; 156 COSBase base = array.getObject( 1 ); 157 if( base instanceof COSName ) 158 { 159 retval = PDColorSpaceFactory.createColorSpace( base ); 160 } 161 else 162 { 163 throw new IOException ( "Error:unknown base colorspace" ); 164 } 165 166 return retval; 167 } 168 169 174 public void setBaseColorSpace( PDColorSpace base ) 175 { 176 array.set( 1, base.getCOSObject() ); 177 } 178 179 184 public int getHighValue() 185 { 186 return ((COSNumber)array.getObject( 2 )).intValue(); 187 } 188 189 195 public void setHighValue( int high ) 196 { 197 array.set( 2, new COSInteger( high ) ); 198 } 199 200 210 public int lookupColor( int componentNumber, int lookupIndex ) throws IOException 211 { 212 PDColorSpace baseColor = getBaseColorSpace(); 213 byte[] data = getLookupData(); 214 int numberOfComponents = baseColor.getNumberOfComponents(); 215 return (data[componentNumber*numberOfComponents + lookupIndex]+256)%256; 216 } 217 218 private byte[] getLookupData() throws IOException 219 { 220 COSBase lookupTable = array.getObject( 3 ); 221 byte[] data = null; 222 if( lookupTable instanceof COSString ) 223 { 224 data = ((COSString)lookupTable).getBytes(); 225 } 226 else if( lookupTable instanceof COSStream ) 227 { 228 COSStream lookupStream = (COSStream)lookupTable; 231 InputStream input = lookupStream.getUnfilteredStream(); 232 ByteArrayOutputStream output = new ByteArrayOutputStream (1024); 233 byte[] buffer = new byte[ 1024 ]; 234 int amountRead; 235 while( (amountRead = input.read(buffer, 0, buffer.length)) != -1 ) 236 { 237 output.write( buffer, 0, amountRead ); 238 } 239 data = output.toByteArray(); 240 } 241 else if( lookupTable == null ) 242 { 243 data = new byte[0]; 244 } 245 else 246 { 247 throw new IOException ( "Error: Unknown type for lookup table " + lookupTable ); 248 } 249 return data; 250 } 251 252 261 public void setLookupColor( int componentNumber, int lookupIndex, int color ) throws IOException 262 { 263 PDColorSpace baseColor = getBaseColorSpace(); 264 int numberOfComponents = baseColor.getNumberOfComponents(); 265 byte[] data = getLookupData(); 266 data[componentNumber*numberOfComponents + lookupIndex] = (byte)color; 267 COSString string = new COSString( data ); 268 array.set( 3, string ); 269 } 270 } | Popular Tags |