Example usage for org.apache.pdfbox.pdmodel.graphics.image PDImageXObject getColorSpace

List of usage examples for org.apache.pdfbox.pdmodel.graphics.image PDImageXObject getColorSpace

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel.graphics.image PDImageXObject getColorSpace.

Prototype

@Override
    public PDColorSpace getColorSpace() throws IOException 

Source Link

Usage

From source file:de.rototor.pdfbox.graphics2d.PdfBoxGraphics2DLosslessImageEncoder.java

License:Apache License

@Override
public PDImageXObject encodeImage(PDDocument document, PDPageContentStream contentStream, Image image) {
    final BufferedImage bi;

    if (image instanceof BufferedImage) {
        bi = (BufferedImage) image;
    } else {/*ww w .j  a v  a  2  s  .c o m*/
        int width = image.getWidth(null);
        int height = image.getHeight(null);
        bi = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
        Graphics graphics = bi.getGraphics();
        if (!graphics.drawImage(image, 0, 0, null, null))
            throw new IllegalStateException("Not fully loaded images are not supported.");
        graphics.dispose();
    }

    try {
        if (doc == null || doc.get() != document) {
            imageMap = new HashMap<ImageSoftReference, SoftReference<PDImageXObject>>();
            profileMap = new HashMap<ProfileSoftReference, SoftReference<PDColorSpace>>();
            doc = new SoftReference<PDDocument>(document);
        }
        SoftReference<PDImageXObject> pdImageXObjectSoftReference = imageMap.get(new ImageSoftReference(image));
        PDImageXObject imageXObject = pdImageXObjectSoftReference == null ? null
                : pdImageXObjectSoftReference.get();
        if (imageXObject == null) {
            imageXObject = LosslessFactory.createFromImage(document, bi);

            /*
             * Do we have a color profile we need to embed?
             */
            if (bi.getColorModel().getColorSpace() instanceof ICC_ColorSpace) {
                ICC_Profile profile = ((ICC_ColorSpace) bi.getColorModel().getColorSpace()).getProfile();
                /*
                 * Only tag a profile if it is not the default sRGB profile.
                 */
                if (((ICC_ColorSpace) bi.getColorModel().getColorSpace()).getProfile() != ICC_Profile
                        .getInstance(ColorSpace.CS_sRGB)) {

                    SoftReference<PDColorSpace> pdProfileRef = profileMap
                            .get(new ProfileSoftReference(profile));

                    /*
                     * We try to reduce the copies of the same ICC profile in the PDF file. If the
                     * image already has a profile, it will be the right one. Otherwise we must
                     * assume that the image is now in sRGB.
                     */
                    PDColorSpace pdProfile = pdProfileRef == null ? null : pdProfileRef.get();
                    if (pdProfile == null) {
                        pdProfile = imageXObject.getColorSpace();
                        if (pdProfile instanceof PDICCBased) {
                            profileMap.put(new ProfileSoftReference(profile),
                                    new SoftReference<PDColorSpace>(pdProfile));
                        }
                    }
                    imageXObject.setColorSpace(pdProfile);
                }
            }
            imageMap.put(new ImageSoftReference(image), new SoftReference<PDImageXObject>(imageXObject));
        }

        return imageXObject;
    } catch (IOException e) {
        throw new RuntimeException("Could not encode Image", e);
    }
}

From source file:org.apache.tika.parser.pdf.PDF2XHTMLPureJava.java

License:Apache License

private void writeToBuffer(PDImageXObject pdImage, String suffix, OutputStream out) throws IOException {

    BufferedImage image = pdImage.getImage();
    if (image != null) {
        if ("jpg".equals(suffix)) {
            String colorSpaceName = pdImage.getColorSpace().getName();
            //TODO: figure out if we want directJPEG as a configuration
            //previously: if (directJPeg || PDDeviceGray....
            if (PDDeviceGray.INSTANCE.getName().equals(colorSpaceName)
                    || PDDeviceRGB.INSTANCE.getName().equals(colorSpaceName)) {
                // RGB or Gray colorspace: get and write the unmodifiedJPEG stream
                InputStream data = pdImage.getStream().createInputStream(JPEG);
                org.apache.pdfbox.io.IOUtils.copy(data, out);
                org.apache.pdfbox.io.IOUtils.closeQuietly(data);
            } else {
                // for CMYK and other "unusual" colorspaces, the JPEG will be converted
                //ImageIOUtil.writeImage(image, suffix, out);
            }//from w w  w  .j av a  2s  .com
        } else if ("jp2".equals(suffix) || "jpx".equals(suffix)) {
            InputStream data = pdImage.createInputStream(JP2);
            org.apache.pdfbox.io.IOUtils.copy(data, out);
            org.apache.pdfbox.io.IOUtils.closeQuietly(data);
        } else if ("jb2".equals(suffix)) {
            InputStream data = pdImage.createInputStream(JB2);
            org.apache.pdfbox.io.IOUtils.copy(data, out);
            org.apache.pdfbox.io.IOUtils.closeQuietly(data);
        } else {
            //ImageIOUtil.writeImage(image, suffix, out);
        }
    }
    out.flush();
}