Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: User Interface Design
Expertise: Beginner
Dec 15, 1999

Color Space Transformation

Question:
How do I transform the color space of an image—from its original space to grayscale, for instance?

Answer:
The ColorSpace class is defined in the java.awt.color package. It encapsulates the transformations that convert pixel values from one color space to another, such as RGB to Photo YCC. ColorSpace contains a factory method, called getInstance() that will create one of a set of predefined ColorSpace instances represented by constants, such as CS_sRGB and CS_GRAY. A ColorSpace can be used to define a transformation with the ColorConvertOp class defined in the java.awt.image package.

An actual conversion can be performed by using the filter() method, which will convert a source image to a destination image. The first argument to the method is the source image and the second argument is the destination image. Both of these must be stored as instances of BufferedImage.

The second argument is optional and if set to null, the method will create and return a new BufferedImage containing the converted image. To get a BufferedImage's ColorSpace, you must first get its ColorModel with getColorModel(). ColorModel is also defined in java.awt.image. Different from a ColorSpace, a ColorModel translates a pixel value to its components, such as RGB and alpha values.

You can fetch the ColorSpace from the ColorModel with getColorSpace(). The following example program ties together these elements to convert an image from its source color space to a gray scale color space and displays both images in two different frames.

import java.awt.*;
import java.awt.color.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;

public class ColorConvert {
  public static class ImageCanvas extends Canvas {
    Image image;

    public ImageCanvas(Image image) {
      this.image = image;
    }

    public void paint(Graphics g) {
      update(g);
    }

    public void update(Graphics g) {
      g.drawImage(image, 0, 0, this);
    }
  }


  public static void main(String[] args) {
    ImageIcon icon;
    BufferedImage convertedImage, sourceImage;
    ColorSpace colorSpace;
    ColorConvertOp conversionOp;
    Graphics graphics;
    final Frame originalFrame, convertedFrame;
    WindowAdapter exitListener;
    int width, height;

    if(args.length < 1) {
      System.err.println("Usage: ColorConvert imagefile");
      return;
    }

    icon = new ImageIcon(args[0]);

    width  = icon.getIconWidth();
    height = icon.getIconHeight();

    sourceImage = 
      new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    graphics = sourceImage.getGraphics();
    graphics.drawImage(icon.getImage(), 0, 0, null);

    colorSpace     = ColorSpace.getInstance(ColorSpace.CS_GRAY);
    conversionOp   = new ColorConvertOp(colorSpace, null);
    convertedImage = conversionOp.filter(sourceImage, null);

    originalFrame = new Frame("Source Image");
    originalFrame.add(new ImageCanvas(sourceImage), BorderLayout.CENTER);
    convertedFrame = new Frame("Converted Image");
    convertedFrame.add(new ImageCanvas(convertedImage), BorderLayout.CENTER);


    exitListener = new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        originalFrame.setVisible(false);
        convertedFrame.setVisible(false);
        originalFrame.dispose();
        convertedFrame.dispose();
        System.exit(0);
      }
    };

    originalFrame.addWindowListener(exitListener);
    convertedFrame.addWindowListener(exitListener);

    originalFrame.setSize(width, height);
    convertedFrame.setSize(width, height);

    originalFrame.setVisible(true);
    convertedFrame.setVisible(true);
  }

}
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date