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

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

Introduction

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

Prototype

@Override
    public void setColorSpace(PDColorSpace cs) 

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 {// w  w w  . ja va2s .  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);
    }
}