Copyright Derek O'Reilly, Dundalk Institute of Technology (DkIT), Dundalk, Co. Louth, Ireland.
ColorConvertOp changes the colour model. It can be used to convert an image to black and white.
Using a ConvolveOp is similar the various other BufferedImageOp classes. In all cases, we can apply a filter to a source to generate a destination. The other BufferedImageOp classes are explained in further detail in these other sections of the notes (AffineTransformOp, ConvolveOp, LookupOp & RescaleOp).
ColorConvertDemo Example (Run Applet)
import java.awt.*;
import java.awt.color.ColorSpace;
import java.awt.image.*;
import javax.swing.*;
public class ColorConvertDemo extends JApplet
{
@Override
public void init()
{
this.setContentPane(new View());
}
public class View extends JPanel
{
private final Image image = new ImageIcon(getClass().getClassLoader().getResource("images/koala.jpg")).getImage();
public View()
{
super();
}
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
// place the original file image into a buffered image
final BufferedImage srcBufferedImg = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
final Graphics2D SrcG = srcBufferedImg.createGraphics();
SrcG.drawImage(this.image, 0, 0, getWidth(), getHeight(), this);
// Do the colorConvert
final ColorSpace grayScale = ColorSpace.getInstance(ColorSpace.CS_GRAY);
final ColorConvertOp colorConvert = new ColorConvertOp(grayScale, null);
final BufferedImage destBufferedImg = colorConvert.filter(srcBufferedImg, null);
// draw the destination buffered image onto the applet's panel
g.drawImage(destBufferedImg, 0, 0, getWidth(), getHeight(), this);
}
}
}
Copyright Derek O' Reilly, Dundalk Institute of Technology (DkIT), Dundalk, Co. Louth, Ireland.