Programming in Java Advanced Imaging
C H A P T E R5 |
Color Space |
THIS chapter describes the JAI color space, transparency, and the color conversion operators. JAI follows the Java AWT color model.
5.1 Introduction
Digital images, specifically digital color images, come in several different forms. The form is often dictated by the means by which the image was acquired or by the image's intended use.One of the more basic types of color image is RGB, for the three primary colors (red, green, and blue). RGB images are sometimes acquired by a color scanner or video camera. These devices incorporate three sensors that are spectrally sensitive to light in the red, green, and blue portions of the spectrum. The three separate red, green, and blue values can be made to directly drive red, green, and blue light guns in a CRT. This type of color system is called an additive linear RGB color system, as the sum of the three full color values produces white.
Printed color images are based on a subtractive color process in which cyan, magenta, and yellow (CMY) dyes are deposited onto paper. The amount of dye deposited is subtractively proportional to the amount of each red, blue, and green color value. The sum of the three CMY color values produce black.
The black produced by a CMY color system often falls short of being a true black. To produce a more accurate black in printed images, black is often added as a fourth color component. This is known as the CMYK color system and is commonly used in the printing industry.
The amount of light generated by the red, blue, and green phosphors of a CRT is not linear. To achieve good display quality, the red, blue, and green values must be adjusted - a process known as gamma correction. In computer systems, gamma correction often takes place in the frame buffer, where the RGB values are passed through lookup tables that are set with the necessary compensation values.
In television transmission systems, the red, blue, and green gamma-corrected color video signals are not transmitted directly. Instead, a linear transformation between the RGB components is performed to produce a luminance signal and a pair of chrominance signals. The luminance signal conveys color brightness levels. The two chrominance signals convey the color hue and saturation. This color system is called YCC (or, more specifically, YCbCr).
Another significant color space standard for JAI is CIEXYZ. This is a widely-used, device-independent color standard developed by the Commission Internationale de l'Éclairage (CIE). The CIEXYZ standard is based on color-matching experiments on human observers.
5.2 Color Management
JAI uses three primary classes for the management of color:
ColorModel
- describes a particular way that pixel values are mapped to colors. AColorModel
is typically associated with anImage
orBufferedImage
and provides the information necessary to correctly interpret pixel values.ColorModel
is defined in thejava.awt.image
package.
ColorSpace
- represents a system for measuring colors, typically using three separate values or components. TheColorSpace
class contains methods for converting between the original color space and one of two standard color spaces, CIEXYZ and RGB.ColorSpace
is defined in thejava.awt.color
package.
Color
- a fixed color, defined in terms of its components in a particularColorSpace
.Color
is defined in thejava.awt
package.5.2.1 Color Models
AColorModel
is used to interpret pixel data in an image. This includes:
- Mapping components in the bands of an image to components of a particular color space
- Extracting pixel components from packed pixel data
- Retrieving multiple components from a single band using masks
To determine the color value of a particular pixel in an image, you need to know how the color information is encoded in each pixel. The
- Converting pixel data through a lookup table
ColorModel
associated with an image encapsulates the data and methods necessary for translating a pixel value to and from its constituent color components.JAI supports five color models:
DirectColorModel
- works with pixel values that represent RGB color and alpha information as separate samples and that pack all samples for a single pixel into a single int, short, or byte quantity. This class can be used only with ColorSpaces of typeColorSpace.TYPE_RGB
.
IndexColorModel
- works with pixel values consisting of a single sample that is an index into a fixed colormap in the default sRGB ColorSpace. The colormap specifies red, green, blue, and optional alpha components corresponding to each index.
ComponentColorModel
- can handle an arbitraryColorSpace
and an array of color components to match theColorSpace
. This model can be used to represent most color models on most types ofGraphicsDevices
.
PackedColorModel
- a base class for models that represent pixel values in which the color components are embedded directly in the bits of an integer pixel. APackedColorModel
stores the packing information that describes how color and alpha components are extracted from the channel. TheDirectColorModel
is aPackedColorModel
.The following sample code shows the construction of a
FloatDoubleColorModel
- works with pixel values that represent color and alpha information as separate samples, using float or double elements.ComponentColorModel
for an RGB color model.
// Create an RGB color model int[] bits = { 8, 8, 8 }; ColorModel colorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), bits, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
The following sample code shows the construction of a
ComponentColorModel
for a grayscale color model.
// Create a grayscale color model. ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY); int bits[] = new int[] {8}; ColorModel cm = new ComponentColorModel(cs, bits, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
The following sample code shows the construction of a
FloatDoubleColorModel
for a linear RGB color model.
ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB); int[] bits = new int[3]; bits[0] = bits[1] = bits[2] = 32; ColorModel cm = new FloatDoubleColorModel(colorSpace, false, false, Transparency.OPAQUE, DataBuffer.TYPE_FLOAT);
API:java.awt.image.ComponentColorModel
- ComponentColorModel(ColorSpace colorSpace, int[] bits, boolean hasAlpha, boolean isAlphaPremultiplied, int transparency, int transferType)
- constructs a
ComponentColorModel
from the specified parameters.
Parameters: colorSpace
The ColorSpace
associated with this color model. See Section 5.2.2, "Color Space."
bits
The number of significant bits per component.
hasAlpha
If true, this color model supports alpha.
isAlphaPremultiplied
If true, alpha is premultiplied.
transparency
Specifies what alpha values can be represented by this color model. See Section 5.3, "Transparency."
transferType
Specifies the type of primitive array used to represent pixel values. One of DataBuffer.TYPE_BYTE
,DataBuffer.TYPE_INT
,DataBuffer.TYPE_SHORT
,DataBuffer.TYPE_USHORT
,DataBuffer.TYPE_DOUBLE
, orDataBuffer.TYPE_FLOAT
API:javax.media.jai.FloatDoubleColorModel
- FloatDoubleColorModel(ColorSpace colorSpace, boolean hasAlpha, boolean isAlphaPremultiplied, int transparency, int transferType)
- constructs a
FloatDoubleColorModel
from the specified parameters.
Parameters: colorSpace
The ColorSpace
associated with this color model. See Section 5.2.2, "Color Space."
hasAlpha
If true, this color model supports alpha.
isAlphaPremultiplied
If true, alpha is premultiplied.
transparency
Specifies what alpha values can be represented by this color model. See Section 5.3, "Transparency."
transferType
Specifies the type of primitive array used to represent pixel values. One of DataBuffer.TYPE_FLOAT
orDataBuffer.TYPE_DOUBLE
.
5.2.2 Color Space
TheColorSpace
class represents a system for measuring colors, typically using three or more separate numeric values. For example, RGB and CMYK are color spaces. AColorSpace
object serves as a color space tag that identifies the specific color space of aColor
object or, through aColorModel
object, of anImage
,BufferedImage
, orGraphicsConfiguration
.
ColorSpace
provides methods that transformColors
in a specific color space to and fromsRGB
and to and from a well-definedCIEXYZ
color space. AllColorSpace
objects must be able to map a color from the represented color space intosRGB
and transform ansRGB
color into the represented color space.Table 5-1 lists the variables used to refer to color spaces (such as
CS_sRGB
andCS_CIEXYZ
) and to color space types (such asTYPE_RGB
andTYPE_CMYK
).
Conversion between Java color spaces is simplified by a set of methods that map a color from a represented color space to either sRGB or CIEXYZ and transform a sRGB or CIEXYZ color space to the represented color space. There are four methods:
- The
toRGB
method transforms aColor
in the represented color space to aColor
in sRGB.
- The
toCIEXYZ
method transforms aColor
in the represented color space to aColor
in CIEXYZ.
- The
fromRGB
method takes aColor
in sRGB and transforms into the represented color space.The sRGB (which stands for "standard" RGB) color space is provided as a convenience to programmers, since many applications are primarily concerned with RGB images. Defining a standard RGB color space makes writing such applications easier. The
- The
fromCIEXYZ
method takes aColor
in CIEXYZ and transforms into the represented color space.toRGB
andfromRGB
methods are provided so that developers can easily retrieve colors in this standard space. However, the sRGB color space is not intended for use with highly accurate color correction or conversions.The sRGB color space is somewhat limited in that it cannot represent every color in the full gamut (spectrum of representable colors) of CIEXYZ color. If a color is specified in some space that has a different gammut than sRGB, using sRGB as an intermediate color space results in a loss of information. The CIEXYZ color space is used as an intermediate color space to avoid any loss of color quality. The CIEXYZ color space is known as the conversion space for this reason. The
toCIEXYZ
andfromCIEXYZ
methods support conversions between any two color spaces at a reasonably high degree of accuracy, one color at a time.
API:java.awt.color.ColorSpace
- abstract float[] toRGB(float[] colorvalue)
- transforms a color value assumed to be in this
ColorSpace
into a value in the defaultCS_sRGB
color space.
Parameter: colorvalue
A float array with length of at least the number of components in this ColorSpace
.
- abstract float[] fromRGB(float[] rgbvalue)
- transforms a color value assumed to be in the default
CS_sRGB
color space into thisColorSpace
.
Parameter: rgbvalue
A float array with length of at least 3.
- abstract float[] toCIEXYZ(float[] colorvalue)
- transforms a color value assumed to be in this
ColorSpace
into theCS_CIEXYZ
conversion color space.
- abstract float[] fromCIEXYZ(float[] colorvalue)
- transforms a color value assumed to be in the
CS_CIEXYZ
conversion color space into thisColorSpace
.
- static ColorSpace getInstance(int colorspace)
- returns a ColorSpace representing one of the specific predefined color spaces.
Parameter: colorSpace
A specific color space identified by one of the predefined class constants (e.g., CS_sRGB
,CS_LINEAR_RGB
,CS_CIEXYZ
,CS_GRAY
, orCS_PYCC
).
- int getType()
- returns the color space type of this
ColorSpace
(for exampleTYPE_RGB
,TYPE_XYZ
, etc.).5.2.3 ICC Profile and ICC Color Space
TheColorSpace
class is an abstract class. It is expected that particular implementations of subclasses ofColorSpace
will support high performance conversion based on underlying platform color management systems. TheICC_ColorSpace
class is one such implementation provided in the base AWT. Developers can define their own subclasses to represent arbitrary color spaces, as long as the appropriate "to" and "from" conversion methods are implemented. However, most developers can simply use the defaultsRGB
color space or color spaces that are represented by commonly-available ICC profiles, such as profiles for monitors and printers or profiles embedded in image data.
The
ICC_ColorSpace
class is based on ICC profile data as represented by theICC_Profile
class. TheICC_Profile
class is a representation of color profile data for device-independent and device-dependent color spaces based on the ICC Profile Format Specification, Version 3.4, August 15, 1997, from the International Color Consortium. ICC profiles describe an input space and a connection space, and define how to map between them.The
ICC_Profile
class has two subclasses that correspond to the specific color types:
ICC_ProfileRGB
, which representsTYPE_RGB
color spaces
ICC_ProfileGray
, which representsTYPE_GRAY
color spaces5.3 Transparency
Just as images can have color, they can also have transparency. Transparency defines the specular transmission of light through transparent materials, such as glass, or the lack of transparency for completely opaque objects. The amount of transparency is specified by an alpha () value. An alpha value of 0.0 specifies complete translucency; an alpha value of 1.0 specifies complete opacity.Images can carry transparency information, known as the alpha channel, for each pixel in the image. The alpha value is particularly important when colors overlap. The alpha value specifies how much of the previously-rendered color should show through.
The Java
Transparency
interface defines the common transparency modes for implementing classes. Table 5-2 lists the variables used to specify transparency.
Transparency is specified as part of the color model (see Section 5.2.1, "Color Models").
5.4 Color Conversion
TheColorConvert
operation performs a pixel-by-pixel color conversion of the data in a rendered or renderable source image. The data are treated as having no alpha channel, i.e., all bands are color bands. The color space of the source image is specified by theColorSpace
object of the source imageColorModel
which must not be null.JAI does not attempt to verify that the
ColorModel
of the destination image is consistent with theColorSpace
parameter. To ensure that this is the case, a compatibleColorModel
must be provided via anImageLayout
in theRenderingHints
(see Section 3.7.3, "Rendering Hints").
Integral data are assumed to occupy the full range of the respective data type; floating point data are assumed to be normalized to the range [0.0,1.0]. By default, the destination image bounds, data type, and number of bands are the same as those of the source image.
The
ColorConvert
operation takes one parameter:
Parameters Type Description colorSpace ColorSpace The destination color space.
For information on color space, see Section 5.2.2, "Color Space."
Listing 5-1 shows a code sample for a
ColorConvert
operation.
Listing 5-1 Example ColorConvert Operation
// Read the image from the specified file name. RenderedOp src = JAI.create("fileload", fileName); // Create the ParameterBlock. ParameterBlock pb = new ParameterBlock(); pb.addSource(src).add(colorSpace); // Perform the color conversion. RenderedOp dst = JAI.create("ColorConvert", pb);
5.5 Non-standard Linear Color Conversion (BandCombine)
In JAI, theBandCombine
operation performs a linear color conversion between color spaces other than those listed in Table 5-1. TheBandCombine
operation computes a set of arbitrary linear combinations of the bands of a rendered or renderable source image, using a specified matrix. The matrix must have dimension (# of source bands plus one) by (# of desired destination bands).The
BandCombine
operation takes one parameter:
Parameter Type Description matrix double The matrix specifying the band combination.
As an example, assume the three-band source image and the matrix shown in Figure 5-1. The equation to calculate the value of the destination pixel in this example would be:
Figure 5-1 Band Combine Example In this example, the number of columns in the matrix is equal to the number of bands in the source image. The number of rows in the matrix must equal the number of bands in the destination image. For a destination image with three bands, the values in the second row of the matrix would be used to calculate the values in the second band of the destination image and the values in the third row would be used to calculate the values in the third band.
If the result of the computation underflows or overflows the minimum or maximum value supported by the destination image, it will be clamped to the minimum or maximum value, respectively.
Listing 5-2 shows a code sample for a
BandCombine
operation.
Listing 5-2 Example BandCombine Operation
// Create the matrix. // Invert center band. double[][] matrix = { { 1.0D, 0.0D, 0.0D, 0.0D }, { 0.0D, -1.0D, 0.0D, 255.0D }, { 0.0D, 0.0D, 1.0D, 0.0D }, }; // Identity. double[][] matrix = { { 1.0D, 0.0D, 0.0D, 0.0D }, { 0.0D, 1.0D, 0.0D, 0.0D }, { 0.0D, 0.0D, 1.0D, 0.0D }, }; // Luminance stored into red band (3 band). double[][] matrix = { { .114D, 0.587D, 0.299D, 0.0D }, { .000D, 0.000D, 0.000D, 0.0D }, { .000D, 0.000D, 0.000D, 0.0D } }; // Luminance (single band output). double[][] matrix = { { .114D, 0.587D, 0.299D, 0.0D } }; // Create the ParameterBlock. ParameterBlock pb = new ParameterBlock(); pb.addSource(src_image); pb.add(matrix); // Perform the band combine operation. dst = (PlanarImage)JAI.create("bandcombine", pb, null);
Programming in Java Advanced Imaging
Copyright © 1999, Sun Microsystems, Inc. All rights reserved.
Casa de Bender