Search in sources :

Example 1 with Filter

use of org.apache.pdfbox.filter.Filter in project pdfbox by apache.

the class LosslessFactory method prepareImageXObject.

/**
 * Create a PDImageXObject while making a decision whether not to
 * compress, use Flate filter only, or Flate and LZW filters.
 *
 * @param document The document.
 * @param byteArray array with data.
 * @param width the image width
 * @param height the image height
 * @param bitsPerComponent the bits per component
 * @param initColorSpace the color space
 * @return the newly created PDImageXObject with the data compressed.
 * @throws IOException
 */
private static PDImageXObject prepareImageXObject(PDDocument document, byte[] byteArray, int width, int height, int bitsPerComponent, PDColorSpace initColorSpace) throws IOException {
    // pre-size the output stream to half of the input
    ByteArrayOutputStream baos = new ByteArrayOutputStream(byteArray.length / 2);
    Filter filter = FilterFactory.INSTANCE.getFilter(COSName.FLATE_DECODE);
    filter.encode(new ByteArrayInputStream(byteArray), baos, new COSDictionary(), 0);
    ByteArrayInputStream encodedByteStream = new ByteArrayInputStream(baos.toByteArray());
    return new PDImageXObject(document, encodedByteStream, COSName.FLATE_DECODE, width, height, bitsPerComponent, initColorSpace);
}
Also used : COSDictionary(org.apache.pdfbox.cos.COSDictionary) Filter(org.apache.pdfbox.filter.Filter) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 2 with Filter

use of org.apache.pdfbox.filter.Filter in project pdfbox by apache.

the class PDInlineImage method createInputStream.

@Override
public InputStream createInputStream(List<String> stopFilters) throws IOException {
    List<String> filters = getFilters();
    ByteArrayInputStream in = new ByteArrayInputStream(rawData);
    ByteArrayOutputStream out = new ByteArrayOutputStream(rawData.length);
    for (int i = 0; filters != null && i < filters.size(); i++) {
        // TODO handling of abbreviated names belongs here, rather than in other classes
        out.reset();
        if (stopFilters.contains(filters.get(i))) {
            break;
        } else {
            Filter filter = FilterFactory.INSTANCE.getFilter(filters.get(i));
            filter.decode(in, out, parameters, i);
            in = new ByteArrayInputStream(out.toByteArray());
        }
    }
    return new ByteArrayInputStream(out.toByteArray());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Filter(org.apache.pdfbox.filter.Filter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Paint(java.awt.Paint)

Example 3 with Filter

use of org.apache.pdfbox.filter.Filter in project pdfbox by apache.

the class TestCOSStream method encodeData.

private byte[] encodeData(byte[] original, COSName filter) throws IOException {
    Filter encodingFilter = FilterFactory.INSTANCE.getFilter(filter);
    ByteArrayOutputStream encoded = new ByteArrayOutputStream();
    encodingFilter.encode(new ByteArrayInputStream(original), encoded, new COSDictionary(), 0);
    return encoded.toByteArray();
}
Also used : Filter(org.apache.pdfbox.filter.Filter) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 4 with Filter

use of org.apache.pdfbox.filter.Filter in project pdfbox by apache.

the class PDStream method createInputStream.

/**
 * This will get a stream with some filters applied but not others. This is
 * useful when doing images, ie filters = [flate,dct], we want to remove
 * flate but leave dct
 *
 * @param stopFilters  A list of filters to stop decoding at.
 * @return A stream with decoded data.
 * @throws IOException If there is an error processing the stream.
 */
public InputStream createInputStream(List<String> stopFilters) throws IOException {
    InputStream is = stream.createRawInputStream();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    List<COSName> filters = getFilters();
    if (filters != null) {
        for (int i = 0; i < filters.size(); i++) {
            COSName nextFilter = filters.get(i);
            if ((stopFilters != null) && stopFilters.contains(nextFilter.getName())) {
                break;
            } else {
                Filter filter = FilterFactory.INSTANCE.getFilter(nextFilter);
                filter.decode(is, os, stream, i);
                IOUtils.closeQuietly(is);
                is = new ByteArrayInputStream(os.toByteArray());
                os.reset();
            }
        }
    }
    return is;
}
Also used : COSName(org.apache.pdfbox.cos.COSName) Filter(org.apache.pdfbox.filter.Filter) ByteArrayInputStream(java.io.ByteArrayInputStream) COSInputStream(org.apache.pdfbox.cos.COSInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 5 with Filter

use of org.apache.pdfbox.filter.Filter in project pdfbox by apache.

the class CCITTFactory method prepareImageXObject.

private static PDImageXObject prepareImageXObject(PDDocument document, byte[] byteArray, int width, int height, PDColorSpace initColorSpace) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Filter filter = FilterFactory.INSTANCE.getFilter(COSName.CCITTFAX_DECODE);
    COSDictionary dict = new COSDictionary();
    dict.setInt(COSName.COLUMNS, width);
    dict.setInt(COSName.ROWS, height);
    filter.encode(new ByteArrayInputStream(byteArray), baos, dict, 0);
    ByteArrayInputStream encodedByteStream = new ByteArrayInputStream(baos.toByteArray());
    PDImageXObject image = new PDImageXObject(document, encodedByteStream, COSName.CCITTFAX_DECODE, width, height, 1, initColorSpace);
    dict.setInt(COSName.K, -1);
    image.getCOSObject().setItem(COSName.DECODE_PARMS, dict);
    return image;
}
Also used : COSDictionary(org.apache.pdfbox.cos.COSDictionary) Filter(org.apache.pdfbox.filter.Filter) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 Filter (org.apache.pdfbox.filter.Filter)5 COSDictionary (org.apache.pdfbox.cos.COSDictionary)2 Paint (java.awt.Paint)1 InputStream (java.io.InputStream)1 COSInputStream (org.apache.pdfbox.cos.COSInputStream)1 COSName (org.apache.pdfbox.cos.COSName)1