/** {@inheritDoc} */
public void populateXObjectDictionary(PDFDictionary dict) {
ColorModel cm = getEffectiveColorModel();
if (cm instanceof IndexColorModel) {
IndexColorModel icm = (IndexColorModel)cm;
PDFArray indexed = new PDFArray(dict);
indexed.add(new PDFName("Indexed"));
if (icm.getColorSpace().getType() != ColorSpace.TYPE_RGB) {
log.warn("Indexed color space is not using RGB as base color space."
+ " The image may not be handled correctly."
+ " Base color space: " + icm.getColorSpace()
+ " Image: " + image.getInfo());
}
indexed.add(new PDFName(toPDFColorSpace(icm.getColorSpace()).getName()));
int c = icm.getMapSize();
int hival = c - 1;
if (hival > MAX_HIVAL) {
throw new UnsupportedOperationException("hival must not go beyond " + MAX_HIVAL);
}
indexed.add(new Integer(hival));
int[] palette = new int[c];
icm.getRGBs(palette);
ByteArrayOutputStream baout = new ByteArrayOutputStream();
for (int i = 0; i < c; i++) {
//TODO Probably doesn't work for non RGB based color spaces
//See log warning above
int entry = palette[i];
baout.write((entry & 0xFF0000) >> 16);
baout.write((entry & 0xFF00) >> 8);
baout.write(entry & 0xFF);
}
indexed.add(baout.toByteArray());
dict.put("ColorSpace", indexed);
dict.put("BitsPerComponent", icm.getPixelSize());
Integer index = getIndexOfFirstTransparentColorInPalette(getImage().getRenderedImage());
if (index != null) {
PDFArray mask = new PDFArray(dict);
mask.add(index);
mask.add(index);
dict.put("Mask", mask);
}
}
}