}
private static BufferedImage from1Bit(PDImage pdImage, WritableRaster raster)
throws IOException
{
final PDColorSpace colorSpace = pdImage.getColorSpace();
final int width = pdImage.getWidth();
final int height = pdImage.getHeight();
final float[] decode = getDecodeArray(pdImage);
byte[] output = ((DataBufferByte) raster.getDataBuffer()).getData();
// read bit stream
InputStream iis = null;
try
{
// create stream
iis = pdImage.getStream().createInputStream();
final boolean isIndexed = colorSpace instanceof PDIndexed;
int rowLen = width / 8;
if (width % 8 > 0)
{
rowLen++;
}
// read stream
byte value0;
byte value1;
if (isIndexed || decode[0] < decode[1])
{
value0 = 0;
value1 = (byte) 255;
}
else
{
value0 = (byte) 255;
value1 = 0;
}
byte[] buff = new byte[rowLen];
int idx = 0;
for (int y = 0; y < height; y++)
{
int x = 0;
iis.read(buff);
for (int r = 0; r < rowLen; r++)
{
int value = buff[r];
int mask = 128;
for (int i = 0; i < 8; i++)
{
int bit = value & mask;
mask >>= 1;
output[idx++] = bit == 0 ? value0 : value1;
x++;
if (x == width)
{
break;
}
}
}
}
// use the color space to convert the image to RGB
BufferedImage rgbImage = colorSpace.toRGBImage(raster);
return rgbImage;
}
finally
{