Search in sources :

Example 1 with CMMException

use of java.awt.color.CMMException in project jdk8u_jdk by JetBrains.

the class ImageTypeProducer method setImageData.

/*
     * Called by the native code whenever an image header has been
     * read.  Whether we read metadata or not, we always need this
     * information, so it is passed back independently of
     * metadata, which may never be read.
     */
private void setImageData(int width, int height, int colorSpaceCode, int outColorSpaceCode, int numComponents, byte[] iccData) {
    this.width = width;
    this.height = height;
    this.colorSpaceCode = colorSpaceCode;
    this.outColorSpaceCode = outColorSpaceCode;
    this.numComponents = numComponents;
    if (iccData == null) {
        iccCS = null;
        return;
    }
    ICC_Profile newProfile = null;
    try {
        newProfile = ICC_Profile.getInstance(iccData);
    } catch (IllegalArgumentException e) {
        /*
             * Color profile data seems to be invalid.
             * Ignore this profile.
             */
        iccCS = null;
        warningOccurred(WARNING_IGNORE_INVALID_ICC);
        return;
    }
    byte[] newData = newProfile.getData();
    ICC_Profile oldProfile = null;
    if (iccCS instanceof ICC_ColorSpace) {
        oldProfile = ((ICC_ColorSpace) iccCS).getProfile();
    }
    byte[] oldData = null;
    if (oldProfile != null) {
        oldData = oldProfile.getData();
    }
    /*
         * At the moment we can't rely on the ColorSpace.equals()
         * and ICC_Profile.equals() because they do not detect
         * the case when two profiles are created from same data.
         *
         * So, we have to do data comparison in order to avoid
         * creation of different ColorSpace instances for the same
         * embedded data.
         */
    if (oldData == null || !java.util.Arrays.equals(oldData, newData)) {
        iccCS = new ICC_ColorSpace(newProfile);
        // verify new color space
        try {
            float[] colors = iccCS.fromRGB(new float[] { 1f, 0f, 0f });
        } catch (CMMException e) {
            /*
                 * Embedded profile seems to be corrupted.
                 * Ignore this profile.
                 */
            iccCS = null;
            cbLock.lock();
            try {
                warningOccurred(WARNING_IGNORE_INVALID_ICC);
            } finally {
                cbLock.unlock();
            }
        }
    }
}
Also used : ICC_ColorSpace(java.awt.color.ICC_ColorSpace) ICC_Profile(java.awt.color.ICC_Profile) CMMException(java.awt.color.CMMException)

Example 2 with CMMException

use of java.awt.color.CMMException in project jdk8u_jdk by JetBrains.

the class LCMSTransform method colorConvert.

/* convert an array of colors in short format */
/* each color is a contiguous set of array elements */
/* the number of colors is (size of the array) / (number of input/output
       components */
public short[] colorConvert(short[] src, short[] dst) {
    if (dst == null) {
        dst = new short[(src.length / getNumInComponents()) * getNumOutComponents()];
    }
    try {
        LCMSImageLayout srcIL = new LCMSImageLayout(src, src.length / getNumInComponents(), LCMSImageLayout.CHANNELS_SH(getNumInComponents()) | LCMSImageLayout.BYTES_SH(2), getNumInComponents() * 2);
        LCMSImageLayout dstIL = new LCMSImageLayout(dst, dst.length / getNumOutComponents(), LCMSImageLayout.CHANNELS_SH(getNumOutComponents()) | LCMSImageLayout.BYTES_SH(2), getNumOutComponents() * 2);
        doTransform(srcIL, dstIL);
        return dst;
    } catch (ImageLayoutException e) {
        throw new CMMException("Unable to convert data");
    }
}
Also used : ImageLayoutException(sun.java2d.cmm.lcms.LCMSImageLayout.ImageLayoutException) CMMException(java.awt.color.CMMException)

Example 3 with CMMException

use of java.awt.color.CMMException in project jdk8u_jdk by JetBrains.

the class LCMSTransform method colorConvert.

public byte[] colorConvert(byte[] src, byte[] dst) {
    if (dst == null) {
        dst = new byte[(src.length / getNumInComponents()) * getNumOutComponents()];
    }
    try {
        LCMSImageLayout srcIL = new LCMSImageLayout(src, src.length / getNumInComponents(), LCMSImageLayout.CHANNELS_SH(getNumInComponents()) | LCMSImageLayout.BYTES_SH(1), getNumInComponents());
        LCMSImageLayout dstIL = new LCMSImageLayout(dst, dst.length / getNumOutComponents(), LCMSImageLayout.CHANNELS_SH(getNumOutComponents()) | LCMSImageLayout.BYTES_SH(1), getNumOutComponents());
        doTransform(srcIL, dstIL);
        return dst;
    } catch (ImageLayoutException e) {
        throw new CMMException("Unable to convert data");
    }
}
Also used : ImageLayoutException(sun.java2d.cmm.lcms.LCMSImageLayout.ImageLayoutException) CMMException(java.awt.color.CMMException)

Example 4 with CMMException

use of java.awt.color.CMMException in project jdk8u_jdk by JetBrains.

the class LCMSTransform method colorConvert.

public void colorConvert(Raster src, WritableRaster dst, float[] srcMinVal, float[] srcMaxVal, float[] dstMinVal, float[] dstMaxVal) {
    LCMSImageLayout srcIL, dstIL;
    // Can't pass src and dst directly to CMM, so process per scanline
    SampleModel srcSM = src.getSampleModel();
    SampleModel dstSM = dst.getSampleModel();
    int srcTransferType = src.getTransferType();
    int dstTransferType = dst.getTransferType();
    boolean srcIsFloat, dstIsFloat;
    if ((srcTransferType == DataBuffer.TYPE_FLOAT) || (srcTransferType == DataBuffer.TYPE_DOUBLE)) {
        srcIsFloat = true;
    } else {
        srcIsFloat = false;
    }
    if ((dstTransferType == DataBuffer.TYPE_FLOAT) || (dstTransferType == DataBuffer.TYPE_DOUBLE)) {
        dstIsFloat = true;
    } else {
        dstIsFloat = false;
    }
    int w = src.getWidth();
    int h = src.getHeight();
    int srcNumBands = src.getNumBands();
    int dstNumBands = dst.getNumBands();
    float[] srcScaleFactor = new float[srcNumBands];
    float[] dstScaleFactor = new float[dstNumBands];
    float[] srcUseMinVal = new float[srcNumBands];
    float[] dstUseMinVal = new float[dstNumBands];
    for (int i = 0; i < srcNumBands; i++) {
        if (srcIsFloat) {
            srcScaleFactor[i] = 65535.0f / (srcMaxVal[i] - srcMinVal[i]);
            srcUseMinVal[i] = srcMinVal[i];
        } else {
            if (srcTransferType == DataBuffer.TYPE_SHORT) {
                srcScaleFactor[i] = 65535.0f / 32767.0f;
            } else {
                srcScaleFactor[i] = 65535.0f / ((float) ((1 << srcSM.getSampleSize(i)) - 1));
            }
            srcUseMinVal[i] = 0.0f;
        }
    }
    for (int i = 0; i < dstNumBands; i++) {
        if (dstIsFloat) {
            dstScaleFactor[i] = (dstMaxVal[i] - dstMinVal[i]) / 65535.0f;
            dstUseMinVal[i] = dstMinVal[i];
        } else {
            if (dstTransferType == DataBuffer.TYPE_SHORT) {
                dstScaleFactor[i] = 32767.0f / 65535.0f;
            } else {
                dstScaleFactor[i] = ((float) ((1 << dstSM.getSampleSize(i)) - 1)) / 65535.0f;
            }
            dstUseMinVal[i] = 0.0f;
        }
    }
    int ys = src.getMinY();
    int yd = dst.getMinY();
    int xs, xd;
    float sample;
    short[] srcLine = new short[w * srcNumBands];
    short[] dstLine = new short[w * dstNumBands];
    int idx;
    try {
        srcIL = new LCMSImageLayout(srcLine, srcLine.length / getNumInComponents(), LCMSImageLayout.CHANNELS_SH(getNumInComponents()) | LCMSImageLayout.BYTES_SH(2), getNumInComponents() * 2);
        dstIL = new LCMSImageLayout(dstLine, dstLine.length / getNumOutComponents(), LCMSImageLayout.CHANNELS_SH(getNumOutComponents()) | LCMSImageLayout.BYTES_SH(2), getNumOutComponents() * 2);
    } catch (ImageLayoutException e) {
        throw new CMMException("Unable to convert rasters");
    }
    // process each scanline
    for (int y = 0; y < h; y++, ys++, yd++) {
        // get src scanline
        xs = src.getMinX();
        idx = 0;
        for (int x = 0; x < w; x++, xs++) {
            for (int i = 0; i < srcNumBands; i++) {
                sample = src.getSampleFloat(xs, ys, i);
                srcLine[idx++] = (short) ((sample - srcUseMinVal[i]) * srcScaleFactor[i] + 0.5f);
            }
        }
        // color convert srcLine to dstLine
        doTransform(srcIL, dstIL);
        // store dst scanline
        xd = dst.getMinX();
        idx = 0;
        for (int x = 0; x < w; x++, xd++) {
            for (int i = 0; i < dstNumBands; i++) {
                sample = ((dstLine[idx++] & 0xffff) * dstScaleFactor[i]) + dstUseMinVal[i];
                dst.setSample(xd, yd, i, sample);
            }
        }
    }
}
Also used : ComponentSampleModel(java.awt.image.ComponentSampleModel) SampleModel(java.awt.image.SampleModel) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) ImageLayoutException(sun.java2d.cmm.lcms.LCMSImageLayout.ImageLayoutException) CMMException(java.awt.color.CMMException)

Example 5 with CMMException

use of java.awt.color.CMMException 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

CMMException (java.awt.color.CMMException)17 BufferedImage (java.awt.image.BufferedImage)8 IOException (java.io.IOException)8 FileImageInputStream (javax.imageio.stream.FileImageInputStream)6 ImageInputStream (javax.imageio.stream.ImageInputStream)6 MemoryCacheImageInputStream (javax.imageio.stream.MemoryCacheImageInputStream)6 ImageLayoutException (sun.java2d.cmm.lcms.LCMSImageLayout.ImageLayoutException)5 ICC_ColorSpace (java.awt.color.ICC_ColorSpace)3 ICC_Profile (java.awt.color.ICC_Profile)3 InputStream (java.io.InputStream)3 ComponentColorModel (java.awt.image.ComponentColorModel)2 ComponentSampleModel (java.awt.image.ComponentSampleModel)2 SampleModel (java.awt.image.SampleModel)2 SinglePixelPackedSampleModel (java.awt.image.SinglePixelPackedSampleModel)2 BufferedOutputStream (java.io.BufferedOutputStream)2 FileInputStream (java.io.FileInputStream)2 OutputStream (java.io.OutputStream)2 FileImageOutputStream (javax.imageio.stream.FileImageOutputStream)2 ImageOutputStream (javax.imageio.stream.ImageOutputStream)2 MemoryCacheImageOutputStream (javax.imageio.stream.MemoryCacheImageOutputStream)2