Example usage for org.apache.pdfbox.cos COSName IMAGE

List of usage examples for org.apache.pdfbox.cos COSName IMAGE

Introduction

In this page you can find the example usage for org.apache.pdfbox.cos COSName IMAGE.

Prototype

COSName IMAGE

To view the source code for org.apache.pdfbox.cos COSName IMAGE.

Click Source Link

Usage

From source file:pdfpicmangler.PDPng.java

License:Open Source License

/**
 * Construct from a stream./*from ww w  .  j  a v  a2  s.c  o  m*/
 * 
 * @param doc The document to create the image as part of.
 * @param is The stream that contains the png data.
 * @throws IOException If there is an error reading the png data.
 */
public PDPng(PDDocument doc, InputStream is) throws IOException {
    super(doc, "png");

    System.out.println("reading in png");

    COSDictionary dic = getCOSStream();

    dic.setItem(COSName.SUBTYPE, COSName.IMAGE);
    //dic.setItem(COSName.TYPE, COSName.XOBJECT);

    data = getCOSStream().createFilteredStream();

    readPng(is);

    setWidth(imageWidth);
    setHeight(imageHeight);
    dic.setInt(COSName.BITS_PER_COMPONENT, bitDepth);

    if ((colorType & PNG_TYPE_PALETTE) != 0) {
        getCOSStream().setItem(COSName.COLORSPACE, paldata);
    } else if ((colorType & PNG_TYPE_COLOR) != 0) {
        setColorSpace(PDDeviceRGB.INSTANCE);
    } else {
        setColorSpace(new PDDeviceGray());
    }

    COSDictionary filterParams = new COSDictionary();
    filterParams.setInt(COSName.PREDICTOR, 15); // png adaptive predictor
    filterParams.setInt(COSName.COLORS,
            ((colorType & PNG_TYPE_COLOR) == 0 || (colorType & PNG_TYPE_PALETTE) != 0) ? 1 : 3);
    filterParams.setInt(COSName.BITS_PER_COMPONENT, bitDepth);
    filterParams.setInt(COSName.COLUMNS, imageWidth);
    filterParams.setDirect(true);

    dic.setItem(COSName.DECODE_PARMS, filterParams);
    dic.setItem(COSName.FILTER, COSName.FLATE_DECODE);

    dic.setInt(COSName.LENGTH, dataLen);
    dic.getDictionaryObject(COSName.LENGTH).setDirect(true);
}