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);
}
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());
}
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();
}
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;
}
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;
}
Aggregations