Search in sources :

Example 1 with PDJPXColorSpace

use of org.apache.pdfbox.pdmodel.graphics.color.PDJPXColorSpace in project pdfbox by apache.

the class JPXFilter method readJPX.

// try to read using JAI Image I/O
private BufferedImage readJPX(InputStream input, DecodeOptions options, DecodeResult result) throws IOException {
    ImageReader reader = findImageReader("JPEG2000", "Java Advanced Imaging (JAI) Image I/O Tools are not installed");
    // PDFBOX-4121: ImageIO.createImageInputStream() is much slower
    try (ImageInputStream iis = new MemoryCacheImageInputStream(input)) {
        reader.setInput(iis, true, true);
        ImageReadParam irp = reader.getDefaultReadParam();
        irp.setSourceRegion(options.getSourceRegion());
        irp.setSourceSubsampling(options.getSubsamplingX(), options.getSubsamplingY(), options.getSubsamplingOffsetX(), options.getSubsamplingOffsetY());
        options.setFilterSubsampled(true);
        BufferedImage image;
        try {
            image = reader.read(0, irp);
        } catch (Exception e) {
            // wrap and rethrow any exceptions
            throw new IOException("Could not read JPEG 2000 (JPX) image", e);
        }
        COSDictionary parameters = result.getParameters();
        // "If the image stream uses the JPXDecode filter, this entry is optional
        // and shall be ignored if present"
        // 
        // note that indexed color spaces make the BPC logic tricky, see PDFBOX-2204
        int bpc = image.getColorModel().getPixelSize() / image.getRaster().getNumBands();
        parameters.setInt(COSName.BITS_PER_COMPONENT, bpc);
        // "Decode shall be ignored, except in the case where the image is treated as a mask"
        if (!parameters.getBoolean(COSName.IMAGE_MASK, false)) {
            parameters.setItem(COSName.DECODE, null);
        }
        // override dimensions, see PDFBOX-1735
        parameters.setInt(COSName.WIDTH, reader.getWidth(0));
        parameters.setInt(COSName.HEIGHT, reader.getHeight(0));
        // extract embedded color space
        if (!parameters.containsKey(COSName.COLORSPACE)) {
            if (image.getSampleModel() instanceof MultiPixelPackedSampleModel && image.getColorModel().getPixelSize() == 1 && image.getRaster().getNumBands() == 1 && image.getColorModel() instanceof IndexColorModel) {
                // PDFBOX-4326:
                // force CS_GRAY colorspace because colorspace in IndexColorModel
                // has 3 colors despite that there is only 1 color per pixel
                // in raster
                result.setColorSpace(new PDJPXColorSpace(ColorSpace.getInstance(ColorSpace.CS_GRAY)));
            } else {
                result.setColorSpace(new PDJPXColorSpace(image.getColorModel().getColorSpace()));
            }
        }
        return image;
    } finally {
        reader.dispose();
    }
}
Also used : PDJPXColorSpace(org.apache.pdfbox.pdmodel.graphics.color.PDJPXColorSpace) ImageReadParam(javax.imageio.ImageReadParam) COSDictionary(org.apache.pdfbox.cos.COSDictionary) ImageInputStream(javax.imageio.stream.ImageInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) IOException(java.io.IOException) ImageReader(javax.imageio.ImageReader) BufferedImage(java.awt.image.BufferedImage) IOException(java.io.IOException) IndexColorModel(java.awt.image.IndexColorModel)

Aggregations

BufferedImage (java.awt.image.BufferedImage)1 IndexColorModel (java.awt.image.IndexColorModel)1 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)1 IOException (java.io.IOException)1 ImageReadParam (javax.imageio.ImageReadParam)1 ImageReader (javax.imageio.ImageReader)1 ImageInputStream (javax.imageio.stream.ImageInputStream)1 MemoryCacheImageInputStream (javax.imageio.stream.MemoryCacheImageInputStream)1 COSDictionary (org.apache.pdfbox.cos.COSDictionary)1 PDJPXColorSpace (org.apache.pdfbox.pdmodel.graphics.color.PDJPXColorSpace)1