Search in sources :

Example 16 with ColorSpace

use of java.awt.color.ColorSpace in project commons-utils-core by jiayongming.

the class OperateImage method grayImage.

/**
 * 图片灰化操作
 *
 * @param srcImage    读取图片路径
 * @param toPath      写入灰化后的图片路径
 * @param imageFormat 图片写入格式
 */
public static 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 17 with ColorSpace

use of java.awt.color.ColorSpace in project imagingbook-common by imagingbook.

the class ColorStack method luvToSrgb.

public static void luvToSrgb(ImagePlus luvImg) {
    assert isColorStack(luvImg);
    ColorSpace lcs = new LuvColorSpace();
    ImageStack stack = luvImg.getImageStack();
    float[] lPix = (float[]) stack.getPixels(1);
    float[] aPix = (float[]) stack.getPixels(2);
    float[] bPix = (float[]) stack.getPixels(3);
    float[] lab = new float[3];
    for (int i = 0; i < lPix.length; i++) {
        lab[0] = lPix[i];
        lab[1] = aPix[i];
        lab[2] = bPix[i];
        float[] srgb = lcs.toRGB(lab);
        float rp = srgb[0];
        float gp = srgb[1];
        float bp = srgb[2];
        if (rp < 0)
            rp = 0;
        else if (rp > 1)
            rp = 1;
        if (gp < 0)
            gp = 0;
        else if (gp > 1)
            gp = 1;
        if (bp < 0)
            bp = 0;
        else if (bp > 1)
            bp = 1;
        // R
        lPix[i] = rp;
        // G
        aPix[i] = gp;
        // B
        bPix[i] = bp;
    }
    setType(luvImg, ColorStackType.sRGB);
}
Also used : ImageStack(ij.ImageStack) ColorSpace(java.awt.color.ColorSpace)

Example 18 with ColorSpace

use of java.awt.color.ColorSpace in project imagingbook-common by imagingbook.

the class ColorStack method srgbToLab.

// sRGB <-> Lab -----------------------------------------------------
public static void srgbToLab(ImagePlus srgbImg) {
    assert isColorStack(srgbImg);
    ColorSpace lcs = new LabColorSpace();
    ImageStack stack = srgbImg.getImageStack();
    float[] rPix = (float[]) stack.getPixels(1);
    float[] gPix = (float[]) stack.getPixels(2);
    float[] bPix = (float[]) stack.getPixels(3);
    float[] srgb = new float[3];
    for (int i = 0; i < rPix.length; i++) {
        float rp = rPix[i];
        float gp = gPix[i];
        float bp = bPix[i];
        if (rp < 0)
            rp = 0;
        else if (rp > 1)
            rp = 1;
        if (gp < 0)
            gp = 0;
        else if (gp > 1)
            gp = 1;
        if (bp < 0)
            bp = 0;
        else if (bp > 1)
            bp = 1;
        srgb[0] = rp;
        srgb[1] = gp;
        srgb[2] = bp;
        float[] lab = lcs.fromRGB(srgb);
        rPix[i] = lab[0];
        gPix[i] = lab[1];
        bPix[i] = lab[2];
    }
    setType(srgbImg, ColorStackType.Lab);
}
Also used : ImageStack(ij.ImageStack) ColorSpace(java.awt.color.ColorSpace)

Example 19 with ColorSpace

use of java.awt.color.ColorSpace in project imagingbook-common by imagingbook.

the class LabColorSpace method main.

public static void main(String[] args) {
    int sr = 128;
    int sg = 1;
    int sb = 128;
    System.out.format("Input (sRGB) = %d, %d, %d\n", sr, sg, sb);
    System.out.format("XYZref = %10g, %10g, %10g\n", Xref, Yref, Zref);
    ColorSpace cs = new LabColorSpace();
    // float[] luv = cs.fromCIEXYZ(new float[] {.1f,.5f,.9f});
    float[] lab = cs.fromRGB(new float[] { sr / 255f, sg / 255f, sb / 255f });
    System.out.format("Lab = %8f, %8f, %8f\n", lab[0], lab[2], lab[2]);
    // float[] xyz = cs.toCIEXYZ(luv);
    float[] srgb = cs.toRGB(lab);
    System.out.format("sRGB = %8f, %8f, %8f\n", Math.rint(255 * srgb[0]), Math.rint(255 * srgb[1]), Math.rint(255 * srgb[2]));
}
Also used : ColorSpace(java.awt.color.ColorSpace)

Example 20 with ColorSpace

use of java.awt.color.ColorSpace in project nationgen by elmokki.

the class Drawing method greyscale.

public static BufferedImage greyscale(BufferedImage image, int units) {
    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
    ColorConvertOp op = new ColorConvertOp(cs, null);
    BufferedImage newimage = op.filter(image, null);
    BufferedImage n = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics g = n.getGraphics();
    g.drawImage(newimage, 0, 0, null);
    for (int x = 0; x < image.getWidth(); x++) for (int y = 0; y < image.getHeight(); y++) {
        int clr = image.getRGB(x, y);
        int red = (clr & 0x00ff0000) >> 16;
        int green = (clr & 0x0000ff00) >> 8;
        int blue = clr & 0x000000ff;
        if (red > 247 && blue > 247 && green == 0) {
            n.setRGB(x, y, clr);
        }
    }
    return darken(n, units);
}
Also used : Graphics(java.awt.Graphics) ColorConvertOp(java.awt.image.ColorConvertOp) ColorSpace(java.awt.color.ColorSpace) BufferedImage(java.awt.image.BufferedImage)

Aggregations

ColorSpace (java.awt.color.ColorSpace)74 BufferedImage (java.awt.image.BufferedImage)28 ColorModel (java.awt.image.ColorModel)28 ComponentColorModel (java.awt.image.ComponentColorModel)25 ICC_ColorSpace (java.awt.color.ICC_ColorSpace)22 SampleModel (java.awt.image.SampleModel)15 IndexColorModel (java.awt.image.IndexColorModel)13 WritableRaster (java.awt.image.WritableRaster)12 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)11 ColorConvertOp (java.awt.image.ColorConvertOp)8 DataBuffer (java.awt.image.DataBuffer)8 DirectColorModel (java.awt.image.DirectColorModel)8 Point (java.awt.Point)7 DataBufferByte (java.awt.image.DataBufferByte)6 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)6 IIOException (javax.imageio.IIOException)5 Rectangle (java.awt.Rectangle)4 ComponentSampleModel (java.awt.image.ComponentSampleModel)4 PixelInterleavedSampleModel (java.awt.image.PixelInterleavedSampleModel)4 SinglePixelPackedSampleModel (java.awt.image.SinglePixelPackedSampleModel)4