CONTENTS | PREV | NEXT | Java 2D API |
ColorModel defines methods for translating from image pixel values to color components such as RGB. The awt.image package provides four types of color models:
- PackedColorModel--An abstract ColorModel that represents pixel values that have color components embedded directly in the bits of an integer pixel. A DirectColorModel is a PackedColorModel.
- DirectColorModel--a ColorModel that represents pixel values that have RGB color components embedded directly in the bits of the pixel itself. DirectColorModel model is similar to an X11 TrueColor visual.
- ComponentColorModel--a ColorModel that can handle an arbitrary ColorSpace and an array of color components to match the ColorSpace.
- IndexColorModel--a ColorModel that represents pixel values that are indices into a fixed color map in the ColorModel's ColorSpace.
ComponentColorModel and PackedColorModel are new in JDK 1.2.
SampleModel defines an interface for extracting samples of an image without knowing how the underlying data is stored. Image data is described as a collection of pixels where each pixel consists of a number of samples. A sample is a datum from an x,y location in one band of an image. A band is a spatially-coherent collection of data.The java.awt.image package provides four types of sample models:
- BandedSampleModel--used to extract samples from images that store sample data for each band in a different bank of the DataBuffer.
- ComponentSampleModel--used to extract samples from images that store sample data in separate data array elements in one bank of a DataBuffer.
- MultiPixelPackedSampleModel--used to extract samples from single banded images that store multiple one-sample pixels in one data element.
- SinglePixelPackedSampleModel--used to extract samples from images that store sample data for a single pixel in one data array element in the first bank of a DataBuffer.
A lookup table contains data for one or more channels or image components; for example, separate arrays for R, G, and B, The java.awt package defines two types of lookup tables that extend the abstract LookupTable class, one that contains byte data and one that contains short data (ByteLookupTable and ShortLookupData).
The java.awt.image package provides two key image classes, BufferedImage and Raster. A BufferedImage is an Image that provides access to its image data. It is comprised of a ColorModel and a Raster of image data. The Raster defines the data and data layout in the BufferedImage, representing a rectangular array of pixels.The image package provides a set of classes that define operations on BufferedImage and Raster objects. Each image processing operation is embodied in a class that implements either the BufferedImageOp interface or the RasterOp interface. The operation class defines a filter method that performs the actual image manipulation.
Figure 5-1 illustrates the basic model for Java 2D API image processing:
The operations supported include:
The classes that implement these operations include AffineTransformOp and its subclasses BilinearAffineTransformOp and NearestNeighborAffineTransformOp, BandCombineOp, ColorConvertOp, ConvolveOp, LookupOp, RescaleOp, and ThresholdOp. These classes can be used to geometrically transform, blur, sharpen, enhance contrast, threshold, and color correct images.Figure 5-2 illustrates edge detection and enhancement, an operation that emphasizes sharp changes in intensity within an image. Edge detection is commonly used in medical imaging and mapping applications. Edge detection is used to increase the contrast between adjacent structures in an image, allowing the viewer to discriminate greater detail.
Figure 5-3 demonstrates lookup table manipulation via rescaling and thresholding. Rescaling can increase or decrease the intensity of all points. Rescaling can be used to increase the dynamic range of an otherwise neutral image, bringing out detail in a region that appears neutral or flat. Thresholding can clip ranges of intensities to a specified level.
The image processing classes provided by the Java 2D API include:
Preparing a graphic element offscreen and then copying to the screen can be useful, particularly if the graphic is complex or is used repeatedly. For example, if you want to display a complicated shape several times, you could draw it once into an offscreen buffer and then copy it to different locations in the window. By drawing the graphic once and copying it, you can display the graphics more quickly.The java.awt package facilitates the use of offscreen buffers by letting you draw to an Image object the same way that you draw to a window. All of the Java 2D API rendering features can be used when drawing to an offscreen images. To copy an offscreen drawing to the screen, you simply call drawImage.
1
Offscreen buffers are often used for animation. For example, you could use an offscreen buffer to draw an object once and then move it around in a window. Similarly, you could use an offscreen buffer to provide feedback as a user moves a graphic using the mouse. Instead of redrawing the graphic at every mouse location, you could draw the graphic once to an offscreen buffer, and then copy it to the mouse location as the user drags the mouse.2
Figure 5-4 demonstrates how a program can draw to an offscreen image and then copy that image into a window multiple times. The last time the image is copied, it is transformed. Note that transforming the image instead of redrawing the graphic with the transformation might produce unsatisfactory results.