public void decode(InputStream compressedData,
OutputStream result,
COSDictionary options,
int filterIndex) throws IOException {
// log.warn( "Warning: CCITTFaxDecode.decode is not implemented yet,
// skipping this stream." );
// Get ImageParams from PDF
COSBase baseObj = options.getDictionaryObject(new String[] {"DecodeParms","DP"});
COSDictionary dict = null;
if( baseObj instanceof COSDictionary )
{
dict = (COSDictionary)baseObj;
}
else if( baseObj instanceof COSArray )
{
COSArray paramArray = (COSArray)baseObj;
if( filterIndex < paramArray.size() )
{
dict = (COSDictionary)paramArray.getObject( filterIndex );
}
else
{
throw new IOException( "Error: DecodeParms cannot be null for CCITTFaxDecode" );
}
}
else if( baseObj == null )
{
throw new IOException( "Error: DecodeParms cannot be null for CCITTFaxDecode" );
}
else
{
throw new IOException( "Error: Expected COSArray or COSDictionary and not "
+ baseObj.getClass().getName() );
}
int width = options.getInt("Width");
int height = options.getInt("Height");
int length = options.getInt(COSName.LENGTH);
int compressionType = dict.getInt("K");
boolean blackIs1 = dict.getBoolean("BlackIs1", false);
// HEADER-INFO and starting point of TAG-DICTIONARY
writeTagHeader(result, length);
// IMAGE-DATA
int i = 0;
//int sum = 0;
byte[] buffer = new byte[32768];
int lentoread = length;
while ((lentoread > 0) && ((i = compressedData.read(buffer, 0, Math.min(lentoread, 32768))) != -1))
{
//sum += i;
result.write(buffer, 0, i);
lentoread = lentoread - i;
}
// If lentoread is > 0 then we need to write out some padding to equal the header
// We'll use what we have in the buffer it's just padding after all
while (lentoread > 0)
{
result.write(buffer, 0, Math.min(lentoread, 32768));
lentoread = lentoread - Math.min(lentoread, 32738);
}
//System.out.println("Gelesen: " + sum);
// TAG-COUNT
writeTagCount(result);
// WIDTH 0x0100
writeTagWidth(result, width);
// HEIGHT 0x0101
writeTagHeight(result, height);
// BITSPERSAMPLE 0x0102
// Always 1 for CCITTFax
writeTagBitsPerSample(result, 1);
// COMPRESSION 0x0103
writeTagCompression(result, compressionType);
// PHOTOMETRIC 0x0106
writeTagPhotometric(result, blackIs1);
// STRIPOFFSET 0x0111
// HERE ALWAYS 8, because ImageData comes before TAG-DICTIONARY
writeTagStripOffset(result, 8);
// ORIENTATION 0x0112
writeTagOrientation(result, 1);
// SamplesPerPixel 0x0115
writeTagSamplesPerPixel(result, 1);
// RowsPerStrip 0x0116
writeTagRowsPerStrip(result, height);
// Stripcount 0x0117
writeTagStripByteCount(result, length);
// XRESOLUTION 0x011A
// HERE: 200 DPI
writeTagXRes(result, 200, 1);
// YRESOLITION 0x011B
// HERE: 200 DPI
writeTagYRes(result, 200, 1);
// ResolutionUnit 0x0128
// HERE: DPI
writeTagResolutionUnit(result, 2);
// SOFTWARE 0x0131
// minimum 4 chars
writeTagSoftware(result, "pdfbox".getBytes());
// DATE AND TIME 0x0132
writeTagDateTime(result, new Date());
// END OF TAG-DICT
writeTagTailer(result);
}
|