Search in sources :

Example 6 with Filter

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

the class JPEGFactory 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 {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    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 // TODO: PdfBox-Android should be DCT
    PDImageXObject(// TODO: PdfBox-Android should be DCT
    document, // TODO: PdfBox-Android should be DCT
    encodedByteStream, // TODO: PdfBox-Android should be DCT
    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 7 with Filter

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

the class COSInputStream method create.

static COSInputStream create(List<Filter> filters, COSDictionary parameters, InputStream in, ScratchFile scratchFile, DecodeOptions options) throws IOException {
    List<DecodeResult> results = new ArrayList<DecodeResult>();
    InputStream input = in;
    if (filters.isEmpty()) {
        input = in;
    } else {
        Set<Filter> filterSet = new HashSet<Filter>(filters);
        if (filterSet.size() != filters.size()) {
            throw new IOException("Duplicate");
        }
        // apply filters
        for (int i = 0; i < filters.size(); i++) {
            if (scratchFile != null) {
                // scratch file
                final RandomAccess buffer = scratchFile.createBuffer();
                DecodeResult result = filters.get(i).decode(input, new RandomAccessOutputStream(buffer), parameters, i, options);
                results.add(result);
                input = new RandomAccessInputStream(buffer) {

                    @Override
                    public void close() throws IOException {
                        buffer.close();
                    }
                };
            } else {
                // in-memory
                ByteArrayOutputStream output = new ByteArrayOutputStream();
                DecodeResult result = filters.get(i).decode(input, output, parameters, i, options);
                results.add(result);
                input = new ByteArrayInputStream(output.toByteArray());
            }
        }
    }
    return new COSInputStream(input, results);
}
Also used : FilterInputStream(java.io.FilterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) RandomAccessInputStream(com.tom_roush.pdfbox.io.RandomAccessInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) RandomAccess(com.tom_roush.pdfbox.io.RandomAccess) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DecodeResult(com.tom_roush.pdfbox.filter.DecodeResult) Filter(com.tom_roush.pdfbox.filter.Filter) ByteArrayInputStream(java.io.ByteArrayInputStream) RandomAccessOutputStream(com.tom_roush.pdfbox.io.RandomAccessOutputStream) RandomAccessInputStream(com.tom_roush.pdfbox.io.RandomAccessInputStream) HashSet(java.util.HashSet)

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