Search in sources :

Example 1 with ProfileDataException

use of java.awt.color.ProfileDataException in project pdfbox by apache.

the class PDICCBased method loadICCProfile.

/**
 * Load the ICC profile, or init alternateColorSpace color space.
 */
private void loadICCProfile() throws IOException {
    try (InputStream input = this.stream.createInputStream()) {
        // if the embedded profile is sRGB then we can use Java's built-in profile, which
        // results in a large performance gain as it's our native color space, see PDFBOX-2587
        ICC_Profile profile;
        synchronized (LOG) {
            profile = ICC_Profile.getInstance(input);
            if (is_sRGB(profile)) {
                isRGB = true;
                awtColorSpace = (ICC_ColorSpace) ColorSpace.getInstance(ColorSpace.CS_sRGB);
                iccProfile = awtColorSpace.getProfile();
            } else {
                profile = ensureDisplayProfile(profile);
                awtColorSpace = new ICC_ColorSpace(profile);
                iccProfile = profile;
            }
            // set initial colour
            float[] initial = new float[getNumberOfComponents()];
            for (int c = 0; c < getNumberOfComponents(); c++) {
                initial[c] = Math.max(0, getRangeForComponent(c).getMin());
            }
            initialColor = new PDColor(initial, this);
            // do things that trigger a ProfileDataException
            // or CMMException due to invalid profiles, see PDFBOX-1295 and PDFBOX-1740
            // or ArrayIndexOutOfBoundsException, see PDFBOX-3610
            awtColorSpace.toRGB(new float[awtColorSpace.getNumComponents()]);
            // this one triggers an exception for PDFBOX-3549 with KCMS
            new Color(awtColorSpace, new float[getNumberOfComponents()], 1f);
            // PDFBOX-4015: this one triggers "CMMException: LCMS error 13" with LCMS
            new ComponentColorModel(awtColorSpace, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
        }
    } catch (RuntimeException e) {
        if (e instanceof ProfileDataException || e instanceof CMMException || e instanceof IllegalArgumentException || e instanceof ArrayIndexOutOfBoundsException) {
            // fall back to alternateColorSpace color space
            awtColorSpace = null;
            alternateColorSpace = getAlternateColorSpace();
            if (alternateColorSpace.equals(PDDeviceRGB.INSTANCE)) {
                isRGB = true;
            }
            LOG.warn("Can't read embedded ICC profile (" + e.getLocalizedMessage() + "), using alternate color space: " + alternateColorSpace.getName());
            initialColor = alternateColorSpace.getInitialColor();
        } else {
            throw e;
        }
    }
}
Also used : InputStream(java.io.InputStream) ComponentColorModel(java.awt.image.ComponentColorModel) Color(java.awt.Color) ProfileDataException(java.awt.color.ProfileDataException) CMMException(java.awt.color.CMMException) ICC_ColorSpace(java.awt.color.ICC_ColorSpace) ICC_Profile(java.awt.color.ICC_Profile)

Aggregations

Color (java.awt.Color)1 CMMException (java.awt.color.CMMException)1 ICC_ColorSpace (java.awt.color.ICC_ColorSpace)1 ICC_Profile (java.awt.color.ICC_Profile)1 ProfileDataException (java.awt.color.ProfileDataException)1 ComponentColorModel (java.awt.image.ComponentColorModel)1 InputStream (java.io.InputStream)1