Programming in Java Advanced Imaging
C H A P T E R 5 |
|
Color Space |
THIS chapter describes the JAI color space, transparency, and the color conversion operators. JAI follows the Java AWT color model.
5.1
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.Introduction
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
JAI uses three primary classes for the management of color:Color Management
ColorModel- describes a particular way that pixel values are mapped to colors. AColorModelis typically associated with anImageorBufferedImageand provides the information necessary to correctly interpret pixel values.ColorModelis defined in thejava.awt.imagepackage.
ColorSpace- represents a system for measuring colors, typically using three separate values or components. TheColorSpaceclass contains methods for converting between the original color space and one of two standard color spaces, CIEXYZ and RGB.ColorSpaceis defined in thejava.awt.colorpackage.
Color- a fixed color, defined in terms of its components in a particularColorSpace.Coloris defined in thejava.awtpackage.5.2.1
AColor Models
ColorModelis 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
ColorModelassociated 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 arbitraryColorSpaceand 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. APackedColorModelstores the packing information that describes how color and alpha components are extracted from the channel. TheDirectColorModelis 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.ComponentColorModelfor 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
ComponentColorModelfor 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
FloatDoubleColorModelfor 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
ComponentColorModelfrom the specified parameters.
Parameters: colorSpaceThe ColorSpaceassociated with this color model. See Section 5.2.2, "Color Space."
bitsThe number of significant bits per component.
hasAlphaIf true, this color model supports alpha.
isAlphaPremultipliedIf true, alpha is premultiplied.
transparencySpecifies what alpha values can be represented by this color model. See Section 5.3, "Transparency."
transferTypeSpecifies 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
FloatDoubleColorModelfrom the specified parameters.
Parameters: colorSpaceThe ColorSpaceassociated with this color model. See Section 5.2.2, "Color Space."
hasAlphaIf true, this color model supports alpha.
isAlphaPremultipliedIf true, alpha is premultiplied.
transparencySpecifies what alpha values can be represented by this color model. See Section 5.3, "Transparency."
transferTypeSpecifies the type of primitive array used to represent pixel values. One of DataBuffer.TYPE_FLOATorDataBuffer.TYPE_DOUBLE.
5.2.2
TheColor Space
ColorSpaceclass represents a system for measuring colors, typically using three or more separate numeric values. For example, RGB and CMYK are color spaces. AColorSpaceobject serves as a color space tag that identifies the specific color space of aColorobject or, through aColorModelobject, of anImage,BufferedImage, orGraphicsConfiguration.
ColorSpaceprovides methods that transformColorsin a specific color space to and fromsRGBand to and from a well-definedCIEXYZcolor space. AllColorSpaceobjects must be able to map a color from the represented color space intosRGBand transform ansRGBcolor into the represented color space.Table 5-1 lists the variables used to refer to color spaces (such as
CS_sRGBandCS_CIEXYZ) and to color space types (such asTYPE_RGBandTYPE_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
toRGBmethod transforms aColorin the represented color space to aColorin sRGB.
- The
toCIEXYZmethod transforms aColorin the represented color space to aColorin CIEXYZ.
- The
fromRGBmethod takes aColorin 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
fromCIEXYZmethod takes aColorin CIEXYZ and transforms into the represented color space.toRGBandfromRGBmethods 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
toCIEXYZandfromCIEXYZmethods 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
ColorSpaceinto a value in the defaultCS_sRGBcolor space.
Parameter: colorvalueA 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_sRGBcolor space into thisColorSpace.
Parameter: rgbvalueA float array with length of at least 3.
- abstract float[] toCIEXYZ(float[] colorvalue)
- transforms a color value assumed to be in this
ColorSpaceinto theCS_CIEXYZconversion color space.
- abstract float[] fromCIEXYZ(float[] colorvalue)
- transforms a color value assumed to be in the
CS_CIEXYZconversion color space into thisColorSpace.
- static ColorSpace getInstance(int colorspace)
- returns a ColorSpace representing one of the specific predefined color spaces.
Parameter: colorSpaceA 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
TheICC Profile and ICC Color Space
ColorSpaceclass is an abstract class. It is expected that particular implementations of subclasses ofColorSpacewill support high performance conversion based on underlying platform color management systems. TheICC_ColorSpaceclass 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 defaultsRGBcolor 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_ColorSpaceclass is based on ICC profile data as represented by theICC_Profileclass. TheICC_Profileclass 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_Profileclass has two subclasses that correspond to the specific color types:
ICC_ProfileRGB, which representsTYPE_RGBcolor spaces
ICC_ProfileGray, which representsTYPE_GRAYcolor spaces5.3
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 (Transparency
) 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
Transparencyinterface 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
TheColor Conversion
ColorConvertoperation 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 theColorSpaceobject of the source imageColorModelwhich must not be null.JAI does not attempt to verify that the
ColorModelof the destination image is consistent with theColorSpaceparameter. To ensure that this is the case, a compatibleColorModelmust be provided via anImageLayoutin 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
ColorConvertoperation 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
ColorConvertoperation.
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
In JAI, theNon-standard Linear Color Conversion (BandCombine)
BandCombineoperation performs a linear color conversion between color spaces other than those listed in Table 5-1. TheBandCombineoperation 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
BandCombineoperation 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
BandCombineoperation.
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