Search in sources :

Example 56 with ColorSpace

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

the class CMYKJPEGImageReader method createRGBImageFromYCCK.

/**
 * Creates a buffered image from a raster in the YCCK color space, converting
 * the colors to RGB using the provided CMYK ICC_Profile.
 *
 * @param ycckRaster 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.
 * @throws NullPointerException.
 */
public static BufferedImage createRGBImageFromYCCK(Raster ycckRaster, ICC_Profile cmykProfile) {
    BufferedImage image;
    if (cmykProfile != null) {
        ycckRaster = convertYCCKtoCMYK(ycckRaster);
        image = createRGBImageFromCMYK(ycckRaster, cmykProfile);
    } else {
        int w = ycckRaster.getWidth(), h = ycckRaster.getHeight();
        int[] rgb = new int[w * h];
        int[] Y = ycckRaster.getSamples(0, 0, w, h, 0, (int[]) null);
        int[] Cb = ycckRaster.getSamples(0, 0, w, h, 1, (int[]) null);
        int[] Cr = ycckRaster.getSamples(0, 0, w, h, 2, (int[]) null);
        int[] K = ycckRaster.getSamples(0, 0, w, h, 3, (int[]) null);
        float vr, vg, vb;
        for (int i = 0, imax = Y.length; i < imax; i++) {
            float k = K[i], y = Y[i], cb = Cb[i], cr = Cr[i];
            vr = y + 1.402f * (cr - 128) - k;
            vg = y - 0.34414f * (cb - 128) - 0.71414f * (cr - 128) - k;
            vb = y + 1.772f * (cb - 128) - k;
            rgb[i] = (0xff & (vr < 0.0f ? 0 : vr > 255.0f ? 0xff : (int) (vr + 0.5f))) << 16 | (0xff & (vg < 0.0f ? 0 : vg > 255.0f ? 0xff : (int) (vg + 0.5f))) << 8 | (0xff & (vb < 0.0f ? 0 : vb > 255.0f ? 0xff : (int) (vb + 0.5f)));
        }
        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 : ColorSpace(java.awt.color.ColorSpace) ICC_ColorSpace(java.awt.color.ICC_ColorSpace)

Example 57 with ColorSpace

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

the class JPEGImageIO method createRGBImageFromInvertedYCCK.

/**
 * Creates a buffered image from a raster in the inverted YCCK color space,
 * converting the colors to RGB using the provided CMYK ICC_Profile.
 *
 * @param ycckRaster 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 createRGBImageFromInvertedYCCK(Raster ycckRaster, ICC_Profile cmykProfile) {
    BufferedImage image;
    if (cmykProfile != null) {
        ycckRaster = convertInvertedYCCKToCMYK(ycckRaster);
        image = createRGBImageFromCMYK(ycckRaster, cmykProfile);
    } else {
        int w = ycckRaster.getWidth(), h = ycckRaster.getHeight();
        int[] rgb = new int[w * h];
        // PixelInterleavedSampleModel pix;
        // if (Adobe_APP14 and transform==2) then YCCK else CMYK
        int[] Y = ycckRaster.getSamples(0, 0, w, h, 0, (int[]) null);
        int[] Cb = ycckRaster.getSamples(0, 0, w, h, 1, (int[]) null);
        int[] Cr = ycckRaster.getSamples(0, 0, w, h, 2, (int[]) null);
        int[] K = ycckRaster.getSamples(0, 0, w, h, 3, (int[]) null);
        float vr, vg, vb;
        for (int i = 0, imax = Y.length; i < imax; i++) {
            float k = 255 - K[i], y = 255 - Y[i], cb = 255 - Cb[i], cr = 255 - Cr[i];
            vr = y + 1.402f * (cr - 128) - k;
            vg = y - 0.34414f * (cb - 128) - 0.71414f * (cr - 128) - k;
            vb = y + 1.772f * (cb - 128) - k;
            rgb[i] = (0xff & (vr < 0.0f ? 0 : vr > 255.0f ? 0xff : (int) (vr + 0.5f))) << 16 | (0xff & (vg < 0.0f ? 0 : vg > 255.0f ? 0xff : (int) (vg + 0.5f))) << 8 | (0xff & (vb < 0.0f ? 0 : vb > 255.0f ? 0xff : (int) (vb + 0.5f)));
        }
        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)

Example 58 with ColorSpace

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

the class JPEGImageIO method createRGBImageFromYCCK.

/**
 * Creates a buffered image from a raster in the YCCK color space, converting
 * the colors to RGB using the provided CMYK ICC_Profile.
 *
 * @param ycckRaster 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.
 * @throws NullPointerException.
 */
public static BufferedImage createRGBImageFromYCCK(Raster ycckRaster, ICC_Profile cmykProfile) {
    BufferedImage image;
    if (cmykProfile != null) {
        ycckRaster = convertYCCKtoCMYK(ycckRaster);
        image = createRGBImageFromCMYK(ycckRaster, cmykProfile);
    } else {
        int w = ycckRaster.getWidth(), h = ycckRaster.getHeight();
        int[] rgb = new int[w * h];
        int[] Y = ycckRaster.getSamples(0, 0, w, h, 0, (int[]) null);
        int[] Cb = ycckRaster.getSamples(0, 0, w, h, 1, (int[]) null);
        int[] Cr = ycckRaster.getSamples(0, 0, w, h, 2, (int[]) null);
        int[] K = ycckRaster.getSamples(0, 0, w, h, 3, (int[]) null);
        float vr, vg, vb;
        for (int i = 0, imax = Y.length; i < imax; i++) {
            float k = K[i], y = Y[i], cb = Cb[i], cr = Cr[i];
            vr = y + 1.402f * (cr - 128) - k;
            vg = y - 0.34414f * (cb - 128) - 0.71414f * (cr - 128) - k;
            vb = y + 1.772f * (cb - 128) - k;
            rgb[i] = (0xff & (vr < 0.0f ? 0 : vr > 255.0f ? 0xff : (int) (vr + 0.5f))) << 16 | (0xff & (vg < 0.0f ? 0 : vg > 255.0f ? 0xff : (int) (vg + 0.5f))) << 8 | (0xff & (vb < 0.0f ? 0 : vb > 255.0f ? 0xff : (int) (vb + 0.5f)));
        }
        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)

Example 59 with ColorSpace

use of java.awt.color.ColorSpace in project BoofCV by lessthanoptimal.

the class ConvertBufferedImage method extractBuffered.

/**
 * Creates a new BufferedImage that internally uses the same data as the provided
 * {@link InterleavedU8}.  If 3 bands then the image will be of type TYPE_3BYTE_BGR
 * or if 1 band TYPE_BYTE_GRAY.
 *
 * @param img Input image who's data will be wrapped by the returned BufferedImage.
 * @return BufferedImage which shared data with the input image.
 */
public static BufferedImage extractBuffered(InterleavedU8 img) {
    if (img.isSubimage())
        throw new IllegalArgumentException("Sub-images are not supported for this operation");
    final int width = img.width;
    final int height = img.height;
    final int numBands = img.numBands;
    // wrap the byte array
    DataBuffer bufferByte = new DataBufferByte(img.data, width * height * numBands, 0);
    ColorModel colorModel;
    int[] bOffs = null;
    if (numBands == 3) {
        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
        int[] nBits = { 8, 8, 8 };
        bOffs = new int[] { 2, 1, 0 };
        colorModel = new ComponentColorModel(cs, nBits, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
    } else if (numBands == 1) {
        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
        int[] nBits = { 8 };
        bOffs = new int[] { 0 };
        colorModel = new ComponentColorModel(cs, nBits, false, true, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
    } else {
        throw new IllegalArgumentException("Only 1 or 3 bands supported");
    }
    // Create a raster using the sample model and data buffer
    WritableRaster raster = Raster.createInterleavedRaster(bufferByte, width, height, img.stride, numBands, bOffs, new Point(0, 0));
    return new BufferedImage(colorModel, raster, false, null);
}
Also used : ColorSpace(java.awt.color.ColorSpace)

Example 60 with ColorSpace

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

the class JPEGImageWriter method getSrcCSType.

private int getSrcCSType(ColorModel cm) {
    int retval = JPEG.JCS_UNKNOWN;
    if (cm != null) {
        boolean alpha = cm.hasAlpha();
        ColorSpace cs = cm.getColorSpace();
        switch(cs.getType()) {
            case ColorSpace.TYPE_GRAY:
                retval = JPEG.JCS_GRAYSCALE;
                break;
            case ColorSpace.TYPE_RGB:
                if (alpha) {
                    retval = JPEG.JCS_RGBA;
                } else {
                    retval = JPEG.JCS_RGB;
                }
                break;
            case ColorSpace.TYPE_YCbCr:
                if (alpha) {
                    retval = JPEG.JCS_YCbCrA;
                } else {
                    retval = JPEG.JCS_YCbCr;
                }
                break;
            case ColorSpace.TYPE_3CLR:
                if (cs == JPEG.JCS.getYCC()) {
                    if (alpha) {
                        retval = JPEG.JCS_YCCA;
                    } else {
                        retval = JPEG.JCS_YCC;
                    }
                }
            case ColorSpace.TYPE_CMYK:
                retval = JPEG.JCS_CMYK;
                break;
        }
    }
    return retval;
}
Also used : ColorSpace(java.awt.color.ColorSpace) ICC_ColorSpace(java.awt.color.ICC_ColorSpace)

Aggregations

ColorSpace (java.awt.color.ColorSpace)86 ColorModel (java.awt.image.ColorModel)33 BufferedImage (java.awt.image.BufferedImage)30 ComponentColorModel (java.awt.image.ComponentColorModel)29 ICC_ColorSpace (java.awt.color.ICC_ColorSpace)24 IndexColorModel (java.awt.image.IndexColorModel)17 SampleModel (java.awt.image.SampleModel)17 WritableRaster (java.awt.image.WritableRaster)13 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)11 ColorConvertOp (java.awt.image.ColorConvertOp)10 DirectColorModel (java.awt.image.DirectColorModel)10 Point (java.awt.Point)8 DataBuffer (java.awt.image.DataBuffer)8 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)8 DataBufferByte (java.awt.image.DataBufferByte)6 SinglePixelPackedSampleModel (java.awt.image.SinglePixelPackedSampleModel)6 IIOException (javax.imageio.IIOException)5 Test (org.junit.Test)5 Rectangle (java.awt.Rectangle)4 ComponentSampleModel (java.awt.image.ComponentSampleModel)4