}
int width = getWidth();
int height = getHeight();
int bpc = getBitsPerComponent();
PDColorSpace colorspace = getColorSpace();
if (colorspace == null)
{
LOG.error("getColorSpace() returned NULL.");
return null;
}
// Get the ColorModel right
ColorModel cm = null;
if (colorspace instanceof PDIndexed)
{
PDIndexed csIndexed = (PDIndexed)colorspace;
COSBase maskArray = getMask();
if (maskArray != null && maskArray instanceof COSArray)
{
cm = csIndexed.createColorModel(bpc, ((COSArray)maskArray).getInt(0));
}
else
{
cm = csIndexed.createColorModel(bpc);
}
}
else if (colorspace instanceof PDSeparation)
{
PDSeparation csSeparation = (PDSeparation)colorspace;
int numberOfComponents = csSeparation.getAlternateColorSpace().getNumberOfComponents();
PDFunction tintTransformFunc = csSeparation.getTintTransform();
COSArray decode = getDecode();
// we have to invert the tint-values,
// if the Decode array exists and consists of (1,0)
boolean invert = decode != null && decode.getInt(0) == 1;
// TODO add interpolation for other decode values then 1,0
int maxValue = (int)Math.pow(2,bpc) - 1;
// destination array
byte[] mappedData = new byte[width*height*numberOfComponents];
int rowLength = width*numberOfComponents;
float[] input = new float[1];
for ( int i = 0; i < height; i++ )
{
int rowOffset = i * rowLength;
for (int j = 0; j < width; j++)
{
// scale tint values to a range of 0...1
int value = (array[ i * width + j ] + 256) % 256;
if (invert)
{
input[0] = 1-(value / maxValue);
}
else
{
input[0] = value / maxValue;
}
float[] mappedColor = tintTransformFunc.eval(input);
int columnOffset = j * numberOfComponents;
for ( int k = 0; k < numberOfComponents; k++ )
{
// redo scaling for every single color value
float mappedValue = mappedColor[k];
mappedData[ rowOffset + columnOffset + k] = (byte)(mappedValue * maxValue);
}
}
}
array = mappedData;
cm = colorspace.createColorModel( bpc );
}
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
{
map = new byte[] {(byte)0x00, (byte)0xff};
}
cm = new IndexColorModel(bpc, map.length, map, map, map, Transparency.OPAQUE);
}
else
{
cm = colorspace.createColorModel( bpc );
}
LOG.debug("ColorModel: " + cm.toString());
WritableRaster raster = cm.createCompatibleWritableRaster( width, height );
DataBufferByte buffer = (DataBufferByte)raster.getDataBuffer();