Search in sources :

Example 76 with ColorSpace

use of java.awt.color.ColorSpace in project acs-aem-commons by Adobe-Consulting-Services.

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 77 with ColorSpace

use of java.awt.color.ColorSpace in project acs-aem-commons by Adobe-Consulting-Services.

the class CMYKJPEGImageReader method createRGBAImageFromRGBA.

/**
 * Creates a buffered image from a raster in the RGBA 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 rgbaRaster A raster with (at least) 4 bands of samples.
 * @param rgbaProfile 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 createRGBAImageFromRGBA(Raster rgbaRaster, ICC_Profile rgbaProfile) {
    BufferedImage image;
    int w = rgbaRaster.getWidth();
    int h = rgbaRaster.getHeight();
    // ICC_Profile currently not supported
    rgbaProfile = null;
    if (rgbaProfile != null) {
        ColorSpace rgbaCS = new ICC_ColorSpace(rgbaProfile);
        image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        WritableRaster rgbRaster = image.getRaster();
        ColorSpace rgbCS = image.getColorModel().getColorSpace();
        ColorConvertOp cmykToRgb = new ColorConvertOp(rgbaCS, rgbCS, null);
        cmykToRgb.filter(rgbaRaster, rgbRaster);
    } else {
        int[] rgb = new int[w * h];
        int[] R = rgbaRaster.getSamples(0, 0, w, h, 0, (int[]) null);
        int[] G = rgbaRaster.getSamples(0, 0, w, h, 1, (int[]) null);
        int[] B = rgbaRaster.getSamples(0, 0, w, h, 2, (int[]) null);
        int[] A = rgbaRaster.getSamples(0, 0, w, h, 3, (int[]) null);
        for (int i = 0, imax = R.length; i < imax; i++) {
            rgb[i] = A[i] << 24 | R[i] << 16 | G[i] << 8 | B[i];
        }
        Raster rgbRaster = Raster.createPackedRaster(new DataBufferInt(rgb, rgb.length), w, h, w, new int[] { 0xff0000, 0xff00, 0xff, 0xff000000 }, null);
        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
        ColorModel cm = new DirectColorModel(cs, 32, 0xff0000, 0xff00, 0xff, 0x0ff000000, 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 78 with ColorSpace

use of java.awt.color.ColorSpace in project Java8 by huhuhuHR.

the class OperateImage method grayImage.

/**
 * 图片灰化操作
 *
 * @param srcImage
 *            读取图片路径
 * @param toPath
 *            写入灰化后的图片路径
 * @param imageFormat
 *            图片写入格式
 */
public void grayImage(String srcImage, String toPath, String imageFormat) {
    try {
        BufferedImage src = ImageIO.read(new File(srcImage));
        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
        ColorConvertOp op = new ColorConvertOp(cs, null);
        src = op.filter(src, null);
        ImageIO.write(src, imageFormat, new File(toPath));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : ColorConvertOp(java.awt.image.ColorConvertOp) ColorSpace(java.awt.color.ColorSpace) BufferedImage(java.awt.image.BufferedImage)

Example 79 with ColorSpace

use of java.awt.color.ColorSpace in project fmv by f-agu.

the class DominantColor method parse.

// ***************************************************
/**
 * @param value
 * @return
 * @throws IOException
 */
static Color parse(String value) throws IOException {
    Matcher matcher = PATTERN.matcher(value);
    if (!matcher.find()) {
        throw new IOException("Data not matches a color pattern: " + value);
    }
    ColorSpace colorSpace = parseColorSpace(matcher.group(1));
    String values = matcher.group(2);
    String[] components = values.split(",");
    float[] floatComponents = new float[components.length];
    for (int i = 0; i < components.length; ++i) {
        floatComponents[i] = Float.valueOf(components[i]) / 255F;
    }
    return new Color(colorSpace, floatComponents, 1);
}
Also used : Matcher(java.util.regex.Matcher) ColorSpace(java.awt.color.ColorSpace) Color(java.awt.Color) IOException(java.io.IOException)

Example 80 with ColorSpace

use of java.awt.color.ColorSpace in project nutz by nutzam.

the class Images method createJPEG4.

/**
 * Java's ImageIO can't process 4-component images and Java2D can't apply
 * AffineTransformOp either, so convert raster data to RGB. Technique due to
 * MArk Stephens. Free for any use.
 */
private static BufferedImage createJPEG4(Raster raster) {
    int w = raster.getWidth();
    int h = raster.getHeight();
    byte[] rgb = new byte[w * h * 3];
    float[] Y = raster.getSamples(0, 0, w, h, 0, (float[]) null);
    float[] Cb = raster.getSamples(0, 0, w, h, 1, (float[]) null);
    float[] Cr = raster.getSamples(0, 0, w, h, 2, (float[]) null);
    float[] K = raster.getSamples(0, 0, w, h, 3, (float[]) null);
    for (int i = 0, imax = Y.length, base = 0; i < imax; i++, base += 3) {
        float k = 220 - K[i], y = 255 - Y[i], cb = 255 - Cb[i], cr = 255 - Cr[i];
        double val = y + 1.402 * (cr - 128) - k;
        val = (val - 128) * .65f + 128;
        rgb[base] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff : (byte) (val + 0.5);
        val = y - 0.34414 * (cb - 128) - 0.71414 * (cr - 128) - k;
        val = (val - 128) * .65f + 128;
        rgb[base + 1] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff : (byte) (val + 0.5);
        val = y + 1.772 * (cb - 128) - k;
        val = (val - 128) * .65f + 128;
        rgb[base + 2] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff : (byte) (val + 0.5);
    }
    raster = Raster.createInterleavedRaster(new DataBufferByte(rgb, rgb.length), w, h, w * 3, 3, new int[] { 0, 1, 2 }, null);
    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    ColorModel cm = new ComponentColorModel(cs, false, true, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
    return new BufferedImage(cm, (WritableRaster) raster, true, null);
}
Also used : ColorSpace(java.awt.color.ColorSpace) ComponentColorModel(java.awt.image.ComponentColorModel) ColorModel(java.awt.image.ColorModel) ComponentColorModel(java.awt.image.ComponentColorModel) DataBufferByte(java.awt.image.DataBufferByte) BufferedImage(java.awt.image.BufferedImage)

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