Search in sources :

Example 1 with ICC_ColorSpace

use of java.awt.color.ICC_ColorSpace in project frostwire by frostwire.

the class CMYKJPEGImageReader method createRGBImageFromCMYK.

/**
 * Creates a buffered image from a raster in the CMYK color space, converting
 * the colors to RGB using the provided CMYK ICC_Profile.
 *
 * As seen from a comment made by 'phelps' at
 * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4799903
 *
 * @param cmykRaster A raster with (at least) 4 bands of samples.
 * @param cmykProfile An ICC_Profile for conversion from the CMYK color space
 * to the RGB color space. If this parameter is null, a default profile is used.
 * @return a BufferedImage in the RGB color space.
 */
public static BufferedImage createRGBImageFromCMYK(Raster cmykRaster, ICC_Profile cmykProfile) {
    BufferedImage image;
    int w = cmykRaster.getWidth();
    int h = cmykRaster.getHeight();
    if (cmykProfile != null) {
        ColorSpace cmykCS = new ICC_ColorSpace(cmykProfile);
        image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        WritableRaster rgbRaster = image.getRaster();
        ColorSpace rgbCS = image.getColorModel().getColorSpace();
        ColorConvertOp cmykToRgb = new ColorConvertOp(cmykCS, rgbCS, null);
        cmykToRgb.filter(cmykRaster, rgbRaster);
    } else {
        int[] rgb = new int[w * h];
        int[] C = cmykRaster.getSamples(0, 0, w, h, 0, (int[]) null);
        int[] M = cmykRaster.getSamples(0, 0, w, h, 1, (int[]) null);
        int[] Y = cmykRaster.getSamples(0, 0, w, h, 2, (int[]) null);
        int[] K = cmykRaster.getSamples(0, 0, w, h, 3, (int[]) null);
        for (int i = 0, imax = C.length; i < imax; i++) {
            int k = K[i];
            rgb[i] = (255 - Math.min(255, C[i] + k)) << 16 | (255 - Math.min(255, M[i] + k)) << 8 | (255 - Math.min(255, Y[i] + k));
        }
        Raster rgbRaster = Raster.createPackedRaster(new DataBufferInt(rgb, rgb.length), w, h, w, new int[] { 0xff0000, 0xff00, 0xff }, null);
        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
        ColorModel cm = new DirectColorModel(cs, 24, 0xff0000, 0xff00, 0xff, 0x0, false, DataBuffer.TYPE_INT);
        image = new BufferedImage(cm, (WritableRaster) rgbRaster, true, null);
    }
    return image;
}
Also used : ICC_ColorSpace(java.awt.color.ICC_ColorSpace) ColorSpace(java.awt.color.ColorSpace) ICC_ColorSpace(java.awt.color.ICC_ColorSpace)

Example 2 with ICC_ColorSpace

use of java.awt.color.ICC_ColorSpace in project frostwire by frostwire.

the class JPEGImageIO method createRGBImageFromCMYK.

/**
 * Creates a buffered image from a raster in the CMYK color space, converting
 * the colors to RGB using the provided CMYK ICC_Profile.
 *
 * As seen from a comment made by 'phelps' at
 * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4799903
 *
 * @param cmykRaster A raster with (at least) 4 bands of samples.
 * @param cmykProfile An ICC_Profile for conversion from the CMYK color space
 * to the RGB color space. If this parameter is null, a default profile is used.
 * @return a BufferedImage in the RGB color space.
 */
public static BufferedImage createRGBImageFromCMYK(Raster cmykRaster, ICC_Profile cmykProfile) {
    BufferedImage image;
    int w = cmykRaster.getWidth();
    int h = cmykRaster.getHeight();
    if (cmykProfile != null) {
        ColorSpace cmykCS = new ICC_ColorSpace(cmykProfile);
        image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        WritableRaster rgbRaster = image.getRaster();
        ColorSpace rgbCS = image.getColorModel().getColorSpace();
        ColorConvertOp cmykToRgb = new ColorConvertOp(cmykCS, rgbCS, null);
        cmykToRgb.filter(cmykRaster, rgbRaster);
    } else {
        int[] rgb = new int[w * h];
        int[] C = cmykRaster.getSamples(0, 0, w, h, 0, (int[]) null);
        int[] M = cmykRaster.getSamples(0, 0, w, h, 1, (int[]) null);
        int[] Y = cmykRaster.getSamples(0, 0, w, h, 2, (int[]) null);
        int[] K = cmykRaster.getSamples(0, 0, w, h, 3, (int[]) null);
        for (int i = 0, imax = C.length; i < imax; i++) {
            int k = K[i];
            rgb[i] = (255 - Math.min(255, C[i] + k)) << 16 | (255 - Math.min(255, M[i] + k)) << 8 | (255 - Math.min(255, Y[i] + k));
        }
        Raster rgbRaster = Raster.createPackedRaster(new DataBufferInt(rgb, rgb.length), w, h, w, new int[] { 0xff0000, 0xff00, 0xff }, null);
        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
        ColorModel cm = new DirectColorModel(cs, 24, 0xff0000, 0xff00, 0xff, 0x0, false, DataBuffer.TYPE_INT);
        image = new BufferedImage(cm, (WritableRaster) rgbRaster, true, null);
    }
    return image;
}
Also used : ICC_ColorSpace(java.awt.color.ICC_ColorSpace) ICC_ColorSpace(java.awt.color.ICC_ColorSpace) ColorSpace(java.awt.color.ColorSpace)

Example 3 with ICC_ColorSpace

use of java.awt.color.ICC_ColorSpace 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 4 with ICC_ColorSpace

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

the class ComponentColorModel method setupLUTs.

private void setupLUTs() {
    // something different from min/max color component values.
    if (is_sRGB) {
        is_sRGB_stdScale = true;
        nonStdScale = false;
    } else if (ColorModel.isLinearRGBspace(colorSpace)) {
        // Note that the built-in Linear RGB space has a normalized
        // range of 0.0 - 1.0 for each coordinate.  Usage of these
        // LUTs makes that assumption.
        is_LinearRGB_stdScale = true;
        nonStdScale = false;
        if (transferType == DataBuffer.TYPE_BYTE) {
            tosRGB8LUT = ColorModel.getLinearRGB8TosRGB8LUT();
            fromsRGB8LUT8 = ColorModel.getsRGB8ToLinearRGB8LUT();
        } else {
            tosRGB8LUT = ColorModel.getLinearRGB16TosRGB8LUT();
            fromsRGB8LUT16 = ColorModel.getsRGB8ToLinearRGB16LUT();
        }
    } else if ((colorSpaceType == ColorSpace.TYPE_GRAY) && (colorSpace instanceof ICC_ColorSpace) && (colorSpace.getMinValue(0) == 0.0f) && (colorSpace.getMaxValue(0) == 1.0f)) {
        // Note that a normalized range of 0.0 - 1.0 for the gray
        // component is required, because usage of these LUTs makes
        // that assumption.
        ICC_ColorSpace ics = (ICC_ColorSpace) colorSpace;
        is_ICCGray_stdScale = true;
        nonStdScale = false;
        fromsRGB8LUT16 = ColorModel.getsRGB8ToLinearRGB16LUT();
        if (ColorModel.isLinearGRAYspace(ics)) {
            is_LinearGray_stdScale = true;
            if (transferType == DataBuffer.TYPE_BYTE) {
                tosRGB8LUT = ColorModel.getGray8TosRGB8LUT(ics);
            } else {
                tosRGB8LUT = ColorModel.getGray16TosRGB8LUT(ics);
            }
        } else {
            if (transferType == DataBuffer.TYPE_BYTE) {
                tosRGB8LUT = ColorModel.getGray8TosRGB8LUT(ics);
                fromLinearGray16ToOtherGray8LUT = ColorModel.getLinearGray16ToOtherGray8LUT(ics);
            } else {
                tosRGB8LUT = ColorModel.getGray16TosRGB8LUT(ics);
                fromLinearGray16ToOtherGray16LUT = ColorModel.getLinearGray16ToOtherGray16LUT(ics);
            }
        }
    } else if (needScaleInit) {
        // if transferType is byte, ushort, int, or short and we
        // don't already know the ColorSpace has minVlaue == 0.0f and
        // maxValue == 1.0f for all components, we need to check that
        // now and setup the min[] and diffMinMax[] arrays if necessary.
        nonStdScale = false;
        for (int i = 0; i < numColorComponents; i++) {
            if ((colorSpace.getMinValue(i) != 0.0f) || (colorSpace.getMaxValue(i) != 1.0f)) {
                nonStdScale = true;
                break;
            }
        }
        if (nonStdScale) {
            min = new float[numColorComponents];
            diffMinMax = new float[numColorComponents];
            for (int i = 0; i < numColorComponents; i++) {
                min[i] = colorSpace.getMinValue(i);
                diffMinMax[i] = colorSpace.getMaxValue(i) - min[i];
            }
        }
    }
}
Also used : ICC_ColorSpace(java.awt.color.ICC_ColorSpace)

Example 5 with ICC_ColorSpace

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

the class ColorModel method getLinearGray16ToOtherGray16LUT.

/*
     * Return a short LUT that converts 16-bit gray values in the CS_GRAY
     * linear gray ColorSpace to the appropriate 16-bit value in the
     * grayCS ColorSpace.  Cache references to any computed LUT in a Map.
     */
static short[] getLinearGray16ToOtherGray16LUT(ICC_ColorSpace grayCS) {
    if (lg16Toog16Map != null) {
        short[] lg16Toog16LUT = lg16Toog16Map.get(grayCS);
        if (lg16Toog16LUT != null) {
            return lg16Toog16LUT;
        }
    }
    short[] tmp = new short[65536];
    for (int i = 0; i <= 65535; i++) {
        tmp[i] = (short) i;
    }
    ColorTransform[] transformList = new ColorTransform[2];
    PCMM mdl = CMSManager.getModule();
    ICC_ColorSpace lgCS = (ICC_ColorSpace) ColorSpace.getInstance(ColorSpace.CS_GRAY);
    transformList[0] = mdl.createTransform(lgCS.getProfile(), ColorTransform.Any, ColorTransform.In);
    transformList[1] = mdl.createTransform(grayCS.getProfile(), ColorTransform.Any, ColorTransform.Out);
    ColorTransform t = mdl.createTransform(transformList);
    short[] lg16Toog16LUT = t.colorConvert(tmp, null);
    if (lg16Toog16Map == null) {
        lg16Toog16Map = Collections.synchronizedMap(new WeakHashMap<ICC_ColorSpace, short[]>(2));
    }
    lg16Toog16Map.put(grayCS, lg16Toog16LUT);
    return lg16Toog16LUT;
}
Also used : ColorTransform(sun.java2d.cmm.ColorTransform) ICC_ColorSpace(java.awt.color.ICC_ColorSpace) PCMM(sun.java2d.cmm.PCMM) WeakHashMap(java.util.WeakHashMap)

Aggregations

ICC_ColorSpace (java.awt.color.ICC_ColorSpace)25 ColorSpace (java.awt.color.ColorSpace)9 ICC_Profile (java.awt.color.ICC_Profile)9 WeakHashMap (java.util.WeakHashMap)8 ColorTransform (sun.java2d.cmm.ColorTransform)8 PCMM (sun.java2d.cmm.PCMM)8 IndexColorModel (java.awt.image.IndexColorModel)4 CMMException (java.awt.color.CMMException)3 BufferedImage (java.awt.image.BufferedImage)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 IIOException (javax.imageio.IIOException)3 TIFFField (it.geosolutions.imageio.plugins.tiff.TIFFField)2 Point (java.awt.Point)2 ColorConvertOp (java.awt.image.ColorConvertOp)2 ComponentColorModel (java.awt.image.ComponentColorModel)2 BitsPerComponentBox (it.geosolutions.imageio.plugins.jp2k.box.BitsPerComponentBox)1 ChannelDefinitionBox (it.geosolutions.imageio.plugins.jp2k.box.ChannelDefinitionBox)1 ColorSpecificationBox (it.geosolutions.imageio.plugins.jp2k.box.ColorSpecificationBox)1 ComponentMappingBox (it.geosolutions.imageio.plugins.jp2k.box.ComponentMappingBox)1