Search in sources :

Example 1 with Filter

use of com.tom_roush.pdfbox.filter.Filter in project PdfBox-Android by TomRoush.

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(com.tom_roush.pdfbox.cos.COSName) Filter(com.tom_roush.pdfbox.filter.Filter) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) COSInputStream(com.tom_roush.pdfbox.cos.COSInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 2 with Filter

use of com.tom_roush.pdfbox.filter.Filter in project PdfBox-Android by TomRoush.

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(com.tom_roush.pdfbox.cos.COSDictionary) Filter(com.tom_roush.pdfbox.filter.Filter) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 3 with Filter

use of com.tom_roush.pdfbox.filter.Filter in project PdfBox-Android by TomRoush.

the class LosslessFactory method prepareImageXObject.

/**
 * Create a PDImageXObject using the Flate filter.
 *
 * @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
 */
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(com.tom_roush.pdfbox.cos.COSDictionary) Filter(com.tom_roush.pdfbox.filter.Filter) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 4 with Filter

use of com.tom_roush.pdfbox.filter.Filter in project PdfBox-Android by TomRoush.

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(com.tom_roush.pdfbox.filter.Filter) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 5 with Filter

use of com.tom_roush.pdfbox.filter.Filter in project PdfBox-Android by TomRoush.

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(com.tom_roush.pdfbox.filter.Filter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Paint(android.graphics.Paint)

Aggregations

Filter (com.tom_roush.pdfbox.filter.Filter)7 ByteArrayInputStream (java.io.ByteArrayInputStream)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)3 InputStream (java.io.InputStream)2 Paint (android.graphics.Paint)1 COSInputStream (com.tom_roush.pdfbox.cos.COSInputStream)1 COSName (com.tom_roush.pdfbox.cos.COSName)1 DecodeResult (com.tom_roush.pdfbox.filter.DecodeResult)1 RandomAccess (com.tom_roush.pdfbox.io.RandomAccess)1 RandomAccessInputStream (com.tom_roush.pdfbox.io.RandomAccessInputStream)1 RandomAccessOutputStream (com.tom_roush.pdfbox.io.RandomAccessOutputStream)1 FilterInputStream (java.io.FilterInputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1