int bpc = getBitsPerComponent();
byte[] array = getPDStream().getByteArray();
// Get the ColorModel right
PDColorSpace colorspace = getColorSpace();
if (colorspace == null)
{
log.error("getColorSpace() returned NULL. Predictor = " + getPredictor());
return null;
}
ColorModel cm = null;
if (colorspace instanceof PDIndexed)
{
PDIndexed csIndexed = (PDIndexed)colorspace;
ColorModel baseColorModel = csIndexed.getBaseColorSpace().createColorModel(bpc);
int size = csIndexed.getHighValue();
byte[] index = csIndexed.getLookupData();
boolean hasAlpha = baseColorModel.hasAlpha();
COSArray maskArray = getMask();
if( baseColorModel.getTransferType() != DataBuffer.TYPE_BYTE )
{
throw new IOException( "Not implemented" );
}
byte[] r = new byte[size+1];
byte[] g = new byte[size+1];
byte[] b = new byte[size+1];
byte[] a = hasAlpha ? new byte[size+1] : null;
byte[] inData = new byte[baseColorModel.getNumComponents()];
for( int i = 0; i <= size; i++ )
{
System.arraycopy(index, i * inData.length, inData, 0, inData.length);
r[i] = (byte)baseColorModel.getRed(inData);
g[i] = (byte)baseColorModel.getGreen(inData);
b[i] = (byte)baseColorModel.getBlue(inData);
if( hasAlpha )
{
a[i] = (byte)baseColorModel.getAlpha(inData);
}
}
if (hasAlpha)
{
cm = new IndexColorModel(bpc, size+1, r, g, b, a);
}
else {
if (maskArray != null)
{
cm = new IndexColorModel(bpc, size+1, r, g, b, maskArray.getInt(0));
}
else
{
cm = new IndexColorModel(bpc, size+1, r, g, b);
}
}
}
else if (bpc == 1)
{
byte[] map = null;
if (colorspace instanceof PDDeviceGray)
{
COSArray decode = getDecode();
// we have to invert the b/w-values,
// if the Decode array exists and consists of (1,0)
if (decode != null && decode.getInt(0) == 1)
{
map = new byte[] {(byte)0xff};
}
else
{
map = new byte[] {(byte)0x00, (byte)0xff};
}
}
else if (colorspace instanceof PDICCBased)
{
if ( ((PDICCBased)colorspace).getNumberOfComponents() == 1)
{
map = new byte[] {(byte)0xff};
}
else
{
map = new byte[] {(byte)0x00, (byte)0xff};
}
}
else
{
map = new byte[] {(byte)0x00, (byte)0xff};
}
cm = new IndexColorModel(bpc, map.length, map, map, map, Transparency.OPAQUE);
}
else
{
if (colorspace instanceof PDICCBased)
{
if (((PDICCBased)colorspace).getNumberOfComponents() == 1)
{
byte[] map = new byte[] {(byte)0xff};
cm = new IndexColorModel(bpc, 1, map, map, map, Transparency.OPAQUE);
}
else
cm = colorspace.createColorModel( bpc );
}
else
cm = colorspace.createColorModel( bpc );
}
log.debug("ColorModel: " + cm.toString());
WritableRaster raster = cm.createCompatibleWritableRaster( width, height );
DataBufferByte buffer = (DataBufferByte)raster.getDataBuffer();