Example usage for java.awt.image ColorConvertOp ColorConvertOp

List of usage examples for java.awt.image ColorConvertOp ColorConvertOp

Introduction

In this page you can find the example usage for java.awt.image ColorConvertOp ColorConvertOp.

Prototype

public ColorConvertOp(ColorSpace srcCspace, ColorSpace dstCspace, RenderingHints hints) 

Source Link

Document

Constructs a new ColorConvertOp from two ColorSpace objects.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    int size = 120;
    int pad = 10;
    BufferedImage bi = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.createGraphics();
    g.setColor(Color.WHITE);//from ww  w .  j  av a  2s.  c om
    g.fillRect(0, 0, size, size);
    g.setColor(Color.YELLOW);
    g.fillOval(pad, pad, size - (2 * pad), size - (2 * pad));
    g.dispose();

    BufferedImage image2 = new BufferedImage(bi.getWidth(), bi.getHeight(), BufferedImage.TYPE_BYTE_GRAY);

    ColorConvertOp op = new ColorConvertOp(bi.getColorModel().getColorSpace(),
            image2.getColorModel().getColorSpace(), null);
    op.filter(bi, image2);
    ImageIO.write(image2, "png", new File("c:/Java_Dev/image2.png"));
}

From source file:ConvertUtil.java

/**
 * Converts the source to 1-bit colour depth (monochrome). No transparency.
 * /* www.  ja v a  2  s . c  o m*/
 * @param src
 *            the source image to convert
 * @return a copy of the source image with a 1-bit colour depth.
 */
public static BufferedImage convert1(BufferedImage src) {
    IndexColorModel icm = new IndexColorModel(1, 2, new byte[] { (byte) 0, (byte) 0xFF },
            new byte[] { (byte) 0, (byte) 0xFF }, new byte[] { (byte) 0, (byte) 0xFF });

    BufferedImage dest = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_BINARY,
            icm);

    ColorConvertOp cco = new ColorConvertOp(src.getColorModel().getColorSpace(),
            dest.getColorModel().getColorSpace(), null);

    cco.filter(src, dest);

    return dest;
}

From source file:ConvertUtil.java

/**
 * Converts the source image to 4-bit colour using the given colour map. No
 * transparency.//from  w  w  w  . jav a2 s . co m
 * 
 * @param src
 *            the source image to convert
 * @param cmap
 *            the colour map, which should contain no more than 16 entries
 *            The entries are in the form RRGGBB (hex).
 * @return a copy of the source image with a 4-bit colour depth, with the
 *         custom colour pallette
 */
public static BufferedImage convert4(BufferedImage src, int[] cmap) {
    IndexColorModel icm = new IndexColorModel(4, cmap.length, cmap, 0, false, Transparency.OPAQUE,
            DataBuffer.TYPE_BYTE);
    BufferedImage dest = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_BINARY,
            icm);
    ColorConvertOp cco = new ColorConvertOp(src.getColorModel().getColorSpace(),
            dest.getColorModel().getColorSpace(), null);
    cco.filter(src, dest);

    return dest;
}

From source file:ConvertUtil.java

/**
 * Converts the source image to 8-bit colour using the default 256-colour
 * palette. No transparency./*from   w  w w. j a v a 2  s  . c  om*/
 * 
 * @param src
 *            the source image to convert
 * @return a copy of the source image with an 8-bit colour depth
 */
public static BufferedImage convert8(BufferedImage src) {
    BufferedImage dest = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_INDEXED);
    ColorConvertOp cco = new ColorConvertOp(src.getColorModel().getColorSpace(),
            dest.getColorModel().getColorSpace(), null);
    cco.filter(src, dest);
    return dest;
}

From source file:ConvertUtil.java

/**
 * Converts the source image to 24-bit colour (RGB). No transparency.
 * //  ww  w.  ja  v  a 2  s .  c o  m
 * @param src
 *            the source image to convert
 * @return a copy of the source image with a 24-bit colour depth
 */
public static BufferedImage convert24(BufferedImage src) {
    BufferedImage dest = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_RGB);
    ColorConvertOp cco = new ColorConvertOp(src.getColorModel().getColorSpace(),
            dest.getColorModel().getColorSpace(), null);
    cco.filter(src, dest);
    return dest;
}

From source file:ConvertUtil.java

/**
 * Converts the source image to 32-bit colour with transparency (ARGB).
 * // w w w  .j  a  v a2  s. co  m
 * @param src
 *            the source image to convert
 * @return a copy of the source image with a 32-bit colour depth.
 */
public static BufferedImage convert32(BufferedImage src) {
    BufferedImage dest = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_ARGB);
    ColorConvertOp cco = new ColorConvertOp(src.getColorModel().getColorSpace(),
            dest.getColorModel().getColorSpace(), null);
    cco.filter(src, dest);
    return dest;
}

From source file:org.gmdev.pdftrick.utils.CustomExtraImgReader.java

/**
 * Convert image from Cmyk to Rgb profile
 * @param cmykRaster//from   w  w w. java2  s .c o  m
 * @param cmykProfile
 * @return The BufferedImage obj
 * @throws IOException
 */
private static BufferedImage convertCmykToRgb(Raster cmykRaster, ICC_Profile cmykProfile) throws IOException {
    if (cmykProfile == null) {
        cmykProfile = ICC_Profile.getInstance(
                CustomExtraImgReader.class.getResourceAsStream(Consts.RESOURCEPATH + Consts.GENERICICCFILE));
    }
    if (cmykProfile.getProfileClass() != ICC_Profile.CLASS_DISPLAY) {
        byte[] profileData = cmykProfile.getData();
        if (profileData[ICC_Profile.icHdrRenderingIntent] == ICC_Profile.icPerceptual) {
            intToBigEndian(ICC_Profile.icSigDisplayClass, profileData, ICC_Profile.icHdrDeviceClass);
            cmykProfile = ICC_Profile.getInstance(profileData);
        }
    }

    ICC_ColorSpace cmykCS = new ICC_ColorSpace(cmykProfile);
    BufferedImage rgbImage = new BufferedImage(cmykRaster.getWidth(), cmykRaster.getHeight(),
            BufferedImage.TYPE_INT_RGB);
    WritableRaster rgbRaster = rgbImage.getRaster();
    ColorSpace rgbCS = rgbImage.getColorModel().getColorSpace();
    ColorConvertOp cmykToRgb = new ColorConvertOp(cmykCS, rgbCS, null);
    cmykToRgb.filter(cmykRaster, rgbRaster);
    return rgbImage;
}

From source file:org.polymap.rhei.batik.engine.svg.Svg2Png.java

public static BufferedImage transform(BufferedImage img, ImageConfiguration imageConfiguration) {
    ColorSpace imgCS = img.getColorModel().getColorSpace();
    ColorSpace grayCS = ColorSpace.getInstance(ColorSpace.CS_GRAY);
    ColorConvertOp cop = new ColorConvertOp(imgCS, grayCS, null);
    BufferedImage finalThresholdImage = cop.filter(img, null);
    finalThresholdImage = shiftHue(finalThresholdImage, imageConfiguration);
    return finalThresholdImage;
}

From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java

public static BufferedImage convertToARGB(BufferedImage img) {
    if (img == null) {
        return null;
    }//from ww w  . java  2 s.co  m

    BufferedImage out = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
    ColorConvertOp cco = new ColorConvertOp(img.getColorModel().getColorSpace(),
            out.getColorModel().getColorSpace(), null);
    cco.filter(img, out);
    return out;
}