/** Constructor, default DeviceRGB, hival 255. */ public PDIndexed() { array = new COSArray(); array.add(COSName.getPDFName(NAME)); array.add(COSName.getPDFName(PDDeviceRGB.NAME)); array.add(new COSInteger(255)); array.add(com.progdan.pdf2txt.cos.COSNull.NULL); }
/** * This will return a list of alternate color spaces(PDColorSpace) if the display application does * not support this icc stream. * * @return A list of alternate color spaces. * @throws IOException If there is an error getting the alternate color spaces. */ public List getAlternateColorSpaces() throws IOException { COSBase alternate = stream.getDictionaryObject(COSName.getPDFName("Alternate")); COSArray alternateArray = null; if (alternate == null) { alternateArray = new COSArray(); int numComponents = getNumberOfComponents(); String csName = null; if (numComponents == 1) { csName = PDDeviceGray.NAME; } else if (numComponents == 3) { csName = PDDeviceRGB.NAME; } else if (numComponents == 4) { csName = PDDeviceCMYK.NAME; } else { throw new IOException("Unknown colorspace number of components:" + numComponents); } alternateArray.add(COSName.getPDFName(csName)); } else { if (alternate instanceof COSArray) { alternateArray = (COSArray) alternate; } else if (alternate instanceof COSName) { alternateArray = new COSArray(); alternateArray.add(alternate); } else { throw new IOException( "Error: expected COSArray or COSName and not " + alternate.getClass().getName()); } } List retval = new ArrayList(); for (int i = 0; i < alternateArray.size(); i++) { retval.add(PDColorSpaceFactory.createColorSpace((COSName) alternateArray.get(i))); } return new COSArrayList(retval, alternateArray); }
private COSArray getRangeArray(int n) { COSArray rangeArray = (COSArray) stream.getDictionaryObject(COSName.getPDFName("Range")); if (rangeArray == null) { rangeArray = new COSArray(); stream.setItem(COSName.getPDFName("Range"), rangeArray); while (rangeArray.size() < n * 2) { rangeArray.add(new COSFloat(-100)); rangeArray.add(new COSFloat(100)); } } return rangeArray; }
/** * This will perform a lookup into the color lookup table. * * @param componentNumber The component number, probably 1,2,3,3. * @param lookupIndex The zero-based index into the table, should not exceed the high value. * @return The value that was from the lookup table. * @throws IOException If there is an error looking up the color. */ public int lookupColor(int componentNumber, int lookupIndex) throws IOException { COSBase lookupTable = array.getObject(3); PDColorSpace baseColor = getBaseColorSpace(); byte[] data = getLookupData(); int numberOfComponents = baseColor.getNumberOfComponents(); return (data[componentNumber * numberOfComponents + lookupIndex] + 256) % 256; }
/** * This will set a color in the color lookup table. * * @param componentNumber The component number, probably 1,2,3,3. * @param lookupIndex The zero-based index into the table, should not exceed the high value. * @param color The color that will go into the table. * @throws IOException If there is an error looking up the color. */ public void setLookupColor(int componentNumber, int lookupIndex, int color) throws IOException { PDColorSpace baseColor = getBaseColorSpace(); int numberOfComponents = baseColor.getNumberOfComponents(); byte[] data = getLookupData(); data[componentNumber * numberOfComponents + lookupIndex] = (byte) color; COSString string = new COSString(data); array.set(3, string); }
/** * This will get the color space that acts as the index for this color space. * * @return The base color space. * @throws IOException If there is error creating the base color space. */ public PDColorSpace getBaseColorSpace() throws IOException { PDColorSpace retval = null; COSBase base = array.getObject(1); if (base instanceof COSName) { retval = PDColorSpaceFactory.createColorSpace((COSName) base); } else { throw new IOException("Error:unknown base colorspace"); } return retval; }
private byte[] getLookupData() throws IOException { COSBase lookupTable = array.getObject(3); byte[] data = null; if (lookupTable instanceof COSString) { data = ((COSString) lookupTable).getBytes(); } else if (lookupTable instanceof COSStream) { // Data will be small so just load the whole thing into memory for // easier processing COSStream lookupStream = (COSStream) lookupTable; InputStream input = lookupStream.getUnfilteredStream(); ByteArrayOutputStream output = new ByteArrayOutputStream(1024); byte[] buffer = new byte[1024]; int amountRead; while ((amountRead = input.read(buffer, 0, buffer.length)) != -1) { output.write(buffer, 0, amountRead); } data = output.toByteArray(); } else if (lookupTable == null) { data = new byte[0]; } else { throw new IOException("Error: Unknown type for lookup table " + lookupTable); } return data; }
/** * Constructor. * * @param iccArray The ICC stream object. */ public PDICCBased(COSArray iccArray) { array = iccArray; stream = (COSStream) iccArray.getObject(1); }
/** * This will set the a range for this color space. * * @param range The new range for the a component. * @param n The component to set the range for. */ public void setRangeForComponent(PDRange range, int n) { COSArray rangeArray = getRangeArray(n); rangeArray.set(n * 2, new COSFloat(range.getMin())); rangeArray.set(n * 2 + 1, new COSFloat(range.getMax())); }
/** * This will set the highest value that is allowed. This cannot be higher than 255. * * @param high The highest value for the lookup table. */ public void setHighValue(int high) { array.set(2, new COSInteger(high)); }
/** * Get the highest value for the lookup. * * @return The hival entry. */ public int getHighValue() { return ((COSNumber) array.getObject(2)).intValue(); }
/** * This will set the base color space. * * @param base The base color space to use as the index. */ public void setBaseColorSpace(PDColorSpace base) { array.set(1, base.getCOSObject()); }