Java Reference
In-Depth Information
11.9.5 Color conversion
The
ColorConvertOp
filter changes an image from one color space to another.
A common requirement in image processing is to change to gray scale:
ColorSpace gray
-
space =
ColorSpace.getInstance (ColorSpace.CS
-
GRAY);
ColorConvertOp convert
-
to
-
gray
-
op = new ColorConvertOp
(gray
-
space, null);
BufferedImage gray
-
img = convert
-
to
-
gray
-
op.filter
(source
-
image, null);
The
java.awt.color.ColorSpace
class offers a number of options includ-
ing
TYPE
-
CMYK
and
TYPE
-
HSV
.
11.9.6 Custom filters
Yo u can implement the
BufferedImageOp
interface to create your own custom
filters. In addition to the
filter()
method, there are four other methods in the
interface that must be implemented. The following
RotateOp
example illustrates
the basics of creating a filter. (See the topic by Knudsen [1] and the other references
[2,3] for more details about creating filters.)
The
filter()
method offers the option of using an existing
BufferedImage
object passed via the second parameter to receive the out-
put of the filter. If this reference is null, a
BufferedImage
must be created and
it must possess the same dimensions as the source image and also have a simi-
lar raster and color model. The
createCompatible DestImage()
method
does this job of making a suitable destination image. For the
BufferedImage
constructor it uses a color model either passed as a parameter or from the source.
It also gets a raster suitable for this color model and it checks to see if the alpha
transparency factor pre-multiplies the color components.
The
getBounds2D()
method returns the bounds object obtained from the
source image. The
getPoint2D()
method, which asks for the point in the
destination image that corresponds to the given point in the source image, just
returns the same point as in the source image since in this filter the dimensions
are unchanged. There are no
RenderingHints
provided for displaying the
filter output image.
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;
/** Shift the color components with the filter. **/
public class
RotateOp
implements BufferedImageOp
{
Search WWH ::
Custom Search