Java Platform 1.2

java.awt.image
Class DirectColorModel

java.lang.Object
  |
  +--java.awt.image.ColorModel
        |
        +--java.awt.image.PackedColorModel
              |
              +--java.awt.image.DirectColorModel

public class DirectColorModel
extends PackedColorModel

A ColorModel class that works with pixel values which represent RGB color and alpha information as separate samples and which pack all samples for a single pixel into a single int, short, or byte quantity. This class can be used only with ColorSpaces of type ColorSpace.TYPE_RGB. There must be three color samples in the pixel values and there may be a single alpha sample. For those methods which use a primitive array pixel representation of type transferType, the array length is always one. Color and alpha samples are stored in the single element of the array in bits indicated by bit masks. Each bit mask must be contiguous and masks must not overlap. The same masks apply to the single int pixel representation used by other methods. The correspondence of masks and color/alpha samples is as follows. Masks are identified by indices running from 0 through 2, if no alpha is present, or 3. The first three indices refer to color samples; index 0 corresponds to red, index 1 to green, and index 2 to blue. Index 3 corresponds to the alpha sample, if present. The transfer types supported are DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT, and DataBuffer.TYPE_INT.

The translation from pixel values to color/alpha components for display or processing purposes is a one-to-one correspondence of samples to components. A DirectColorModel is typically used with image data which uses masks to define packed samples. For example, a DirectColorModel can be used in conjunction with a SinglePixelPackedSampleModel to construct a BufferedImage. Normally the masks used by the SampleModel and the ColorModel would be the same. However, if they are different, the color interpretation of pixel data will be done according to the masks of the ColorModel.

A single int pixel representation is valid for all objects of this class, since it is always possible to represent pixel values used with this class in a single int. Therefore, methods which use this representation will not throw an IllegalArgumentException due to an invalid pixel value.

This color model is similar to an X11 TrueColor visual. The default RGB ColorModel specified by the ColorModel.getRGBdefault method is a DirectColorModel with the following parameters:

 Number of bits:        32
 Red mask:              0x00ff0000
 Green mask:            0x0000ff00
 Blue mask:             0x000000ff
 Alpha mask:            0xff000000
 Color space:           sRGB
 isAlphaPremultiplied:  False
 Transparency:          Transparency.TRANSLUCENT
 transferType:          DataBuffer.TYPE_INT
 

Many of the methods in this class are final. This is because the underlying native graphics code makes assumptions about the layout and operation of this class and those assumptions are reflected in the implementations of the methods here that are marked final. You can subclass this class for other reasons, but you cannot override or modify the behavior of those methods.

See Also:
ColorModel, ColorSpace, SinglePixelPackedSampleModel, BufferedImage, ColorModel.getRGBdefault()

Fields inherited from class java.awt.image.ColorModel
pixel_bits, transferType
 
Constructor Summary
DirectColorModel(ColorSpace space, int bits, int rmask, int gmask, int bmask, int amask, boolean isAlphaPremultiplied, int transferType)
          Constructs a DirectColorModel from the specified parameters.
DirectColorModel(int bits, int rmask, int gmask, int bmask)
          Constructs a DirectColorModel from the given masks specifying which bits in an int pixel representation contain the red, green and blue color samples.
DirectColorModel(int bits, int rmask, int gmask, int bmask, int amask)
          Constructs a DirectColorModel from the given masks specifying which bits in an int pixel representation contain the red, green and blue color samples and the alpha sample, if present.
 
Method Summary
 ColorModel coerceData(WritableRaster raster, boolean isAlphaPremultiplied)
          Forces the Raster data to match the state specified in the isAlphaPremultiplied variable, assuming the data is currently correctly described by this ColorModel.
 WritableRaster createCompatibleWritableRaster(int w, int h)
          Creates a WritableRaster with the specified width and height that has a data layout (SampleModel) compatible with this ColorModel.
 int getAlpha(int pixel)
          Returns the alpha component for the specified pixel, scaled from 0 to 255.
 int getAlpha(Object inData)
          Returns the alpha component for the specified pixel, scaled from 0 to 255.
 int getAlphaMask()
          Returns the mask indicating which bits in an int pixel representation contain the alpha component.
 int getBlue(int pixel)
          Returns the blue color component for the specified pixel, scaled from 0 to 255 in the default RGB ColorSpace, sRGB.
 int getBlue(Object inData)
          Returns the blue color component for the specified pixel, scaled from 0 to 255 in the default RGB ColorSpace, sRGB.
 int getBlueMask()
          Returns the mask indicating which bits in an int pixel representation contain the blue color component.
 int[] getComponents(int pixel, int[] components, int offset)
          Returns an array of unnormalized color/alpha components given a pixel in this ColorModel.
 int[] getComponents(Object pixel, int[] components, int offset)
          Returns an array of unnormalized color/alpha components given a pixel in this ColorModel.
 int getDataElement(int[] components, int offset)
          Returns a pixel value represented as an int in this ColorModel, given an array of unnormalized color/alpha components.
 Object getDataElements(int[] components, int offset, Object obj)
          Returns a data element array representation of a pixel in this ColorModel, given an array of unnormalized color/alpha components.
 Object getDataElements(int rgb, Object pixel)
          Returns a data element array representation of a pixel in this ColorModel, given an integer pixel representation in the default RGB color model.
 int getGreen(int pixel)
          Returns the green color component for the specified pixel, scaled from 0 to 255 in the default RGB ColorSpace, sRGB.
 int getGreen(Object inData)
          Returns the green color component for the specified pixel, scaled from 0 to 255 in the default RGB ColorSpace, sRGB.
 int getGreenMask()
          Returns the mask indicating which bits in an int pixel representation contain the green color component.
 int getRed(int pixel)
          Returns the red color component for the specified pixel, scaled from 0 to 255 in the default RGB ColorSpace, sRGB.
 int getRed(Object inData)
          Returns the red color component for the specified pixel, scaled from 0 to 255 in the default RGB ColorSpace, sRGB.
 int getRedMask()
          Returns the mask indicating which bits in an int pixel representation contain the red color component.
 int getRGB(int pixel)
          Returns the color/alpha components of the pixel in the default RGB color model format.
 int getRGB(Object inData)
          Returns the color/alpha components for the specified pixel in the default RGB color model format.
 boolean isCompatibleRaster(Raster raster)
          Returns true if raster is compatible with this ColorModel and false if it is not.
 String toString()
          Returns a String that represents this DirectColorModel.
 
Methods inherited from class java.awt.image.PackedColorModel
createCompatibleSampleModel, equals, getAlphaRaster, getMask, getMasks, isCompatibleSampleModel
 
Methods inherited from class java.awt.image.ColorModel
finalize, getColorSpace, getComponentSize, getComponentSize, getNormalizedComponents, getNumColorComponents, getNumComponents, getPixelSize, getRGBdefault, getTransparency, getUnnormalizedComponents, hasAlpha, isAlphaPremultiplied
 
Methods inherited from class java.lang.Object
clone, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DirectColorModel

public DirectColorModel(int bits,
                        int rmask,
                        int gmask,
                        int bmask)
Constructs a DirectColorModel from the given masks specifying which bits in an int pixel representation contain the red, green and blue color samples. Pixel values do not contain alpha information, so all pixels will be treated as opaque (alpha = 1.0). All of the bits in each mask must be contiguous and fit in the specified number of least significant bits of an int pixel representation. The ColorSpace will be the default sRGB space. The transparency value will be Transparency.OPAQUE. The transfer type will be the smallest of DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT, or DataBuffer.TYPE_INT that can hold a single pixel.

DirectColorModel

public DirectColorModel(int bits,
                        int rmask,
                        int gmask,
                        int bmask,
                        int amask)
Constructs a DirectColorModel from the given masks specifying which bits in an int pixel representation contain the red, green and blue color samples and the alpha sample, if present. If amask is 0, pixel values do not contain alpha information, so all pixels will be treated as opaque (alpha = 1.0). All of the bits in each mask must be contiguous and fit in the specified number of least significant bits of an int pixel representation. Alpha, if present, will not be premultiplied. The ColorSpace will be the default sRGB space. The transparency value will be Transparency.OPAQUE, if no alpha is present, or Transparency.TRANSLUCENT otherwise. The transfer type will be the smallest of DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT, or DataBuffer.TYPE_INT that can hold a single pixel.

DirectColorModel

public DirectColorModel(ColorSpace space,
                        int bits,
                        int rmask,
                        int gmask,
                        int bmask,
                        int amask,
                        boolean isAlphaPremultiplied,
                        int transferType)
Constructs a DirectColorModel from the specified parameters. Color components will be in the specified ColorSpace, which must be of type ColorSpace.TYPE_RGB. The masks specify which bits in an int pixel representation contain the red, green and blue color samples and the alpha sample, if present. If amask is 0, pixel values do not contain alpha information, so all pixels will be treated as opaque (alpha = 1.0). All of the bits in each mask must be contiguous and fit in the specified number of least significant bits of an int pixel representation. If there is alpha, the boolean isAlphaPremultiplied specifies how to interpret color and alpha samples in pixel values. If the boolean is true, color samples are assumed to have been multiplied by the alpha sample. The transparency value will be Transparency.OPAQUE, if no alpha is present, or Transparency.TRANSLUCENT otherwise. The transfer type is the type of primitive array used to represent pixel values and must be one of DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT, or DataBuffer.TYPE_INT.
Method Detail

getRedMask

public final int getRedMask()
Returns the mask indicating which bits in an int pixel representation contain the red color component.

getGreenMask

public final int getGreenMask()
Returns the mask indicating which bits in an int pixel representation contain the green color component.

getBlueMask

public final int getBlueMask()
Returns the mask indicating which bits in an int pixel representation contain the blue color component.

getAlphaMask

public final int getAlphaMask()
Returns the mask indicating which bits in an int pixel representation contain the alpha component.

getRed

public final int getRed(int pixel)
Returns the red color component for the specified pixel, scaled from 0 to 255 in the default RGB ColorSpace, sRGB. A color conversion is done if necessary. The pixel value is specified as an int. The returned value will be a non pre-multiplied value, i.e. if the alpha is premultiplied, this method will divide it out before returning the value (if the alpha value is 0, the red value will be 0).
Overrides:
getRed in class ColorModel

getGreen

public final int getGreen(int pixel)
Returns the green color component for the specified pixel, scaled from 0 to 255 in the default RGB ColorSpace, sRGB. A color conversion is done if necessary. The pixel value is specified as an int. The returned value will be a non pre-multiplied value, i.e. if the alpha is premultiplied, this method will divide it out before returning the value (if the alpha value is 0, the green value will be 0).
Overrides:
getGreen in class ColorModel

getBlue

public final int getBlue(int pixel)
Returns the blue color component for the specified pixel, scaled from 0 to 255 in the default RGB ColorSpace, sRGB. A color conversion is done if necessary. The pixel value is specified as an int. The returned value will be a non pre-multiplied value, i.e. if the alpha is premultiplied, this method will divide it out before returning the value (if the alpha value is 0, the blue value will be 0).
Overrides:
getBlue in class ColorModel

getAlpha

public final int getAlpha(int pixel)
Returns the alpha component for the specified pixel, scaled from 0 to 255. The pixel value is specified as an int.
Overrides:
getAlpha in class ColorModel

getRGB

public final int getRGB(int pixel)
Returns the color/alpha components of the pixel in the default RGB color model format. A color conversion is done if necessary. The pixel value is specified as an int. The returned value will be in a non pre-multiplied format, i.e. if the alpha is premultiplied, this method will divide it out of the color components (if the alpha value is 0, the color values will be 0).
Overrides:
getRGB in class ColorModel
See Also:
ColorModel.getRGBdefault()

getRed

public int getRed(Object inData)
Returns the red color component for the specified pixel, scaled from 0 to 255 in the default RGB ColorSpace, sRGB. A color conversion is done if necessary. The pixel value is specified by an array of data elements of type transferType passed in as an object reference. The returned value will be a non pre-multiplied value, i.e. if the alpha is premultiplied, this method will divide it out before returning the value (if the alpha value is 0, the red value will be 0). If inData is not a primitive array of type transferType, a ClassCastException is thrown. An ArrayIndexOutOfBoundsException is thrown if inData is not large enough to hold a pixel value for this ColorModel.
Overrides:
getRed in class ColorModel

getGreen

public int getGreen(Object inData)
Returns the green color component for the specified pixel, scaled from 0 to 255 in the default RGB ColorSpace, sRGB. A color conversion is done if necessary. The pixel value is specified by an array of data elements of type transferType passed in as an object reference. The returned value will be a non pre-multiplied value, i.e. if the alpha is premultiplied, this method will divide it out before returning the value (if the alpha value is 0, the green value will be 0). If inData is not a primitive array of type transferType, a ClassCastException is thrown. An ArrayIndexOutOfBoundsException is thrown if inData is not large enough to hold a pixel value for this ColorModel.
Overrides:
getGreen in class ColorModel

getBlue

public int getBlue(Object inData)
Returns the blue color component for the specified pixel, scaled from 0 to 255 in the default RGB ColorSpace, sRGB. A color conversion is done if necessary. The pixel value is specified by an array of data elements of type transferType passed in as an object reference. The returned value will be a non pre-multiplied value, i.e. if the alpha is premultiplied, this method will divide it out before returning the value (if the alpha value is 0, the blue value will be 0). If inData is not a primitive array of type transferType, a ClassCastException is thrown. An ArrayIndexOutOfBoundsException is thrown if inData is not large enough to hold a pixel value for this ColorModel.
Overrides:
getBlue in class ColorModel

getAlpha

public int getAlpha(Object inData)
Returns the alpha component for the specified pixel, scaled from 0 to 255. The pixel value is specified by an array of data elements of type transferType passed in as an object reference. If inData is not a primitive array of type transferType, a ClassCastException is thrown. An ArrayIndexOutOfBoundsException is thrown if inData is not large enough to hold a pixel value for this ColorModel.
Overrides:
getAlpha in class ColorModel

getRGB

public int getRGB(Object inData)
Returns the color/alpha components for the specified pixel in the default RGB color model format. A color conversion is done if necessary. The pixel value is specified by an array of data elements of type transferType passed in as an object reference. If inData is not a primitive array of type transferType, a ClassCastException is thrown. An ArrayIndexOutOfBoundsException is thrown if inData is not large enough to hold a pixel value for this ColorModel. The returned value will be in a non pre-multiplied format, i.e. if the alpha is premultiplied, this method will divide it out of the color components (if the alpha value is 0, the color values will be 0).
Overrides:
getRGB in class ColorModel
See Also:
ColorModel.getRGBdefault()

getDataElements

public Object getDataElements(int rgb,
                              Object pixel)
Returns a data element array representation of a pixel in this ColorModel, given an integer pixel representation in the default RGB color model. This array can then be passed to the setDataElements method of a WritableRaster object. If the pixel variable is null, a new array will be allocated. If pixel is not null, it must be a primitive array of type transferType; otherwise, a ClassCastException is thrown. An ArrayIndexOutOfBoundsException is thrown if pixel is not large enough to hold a pixel value for this ColorModel. The pixel array will be returned.
Overrides:
getDataElements in class ColorModel
See Also:
WritableRaster.setDataElements(int, int, java.lang.Object), SampleModel.setDataElements(int, int, java.lang.Object, java.awt.image.DataBuffer)

getComponents

public final int[] getComponents(int pixel,
                                 int[] components,
                                 int offset)
Returns an array of unnormalized color/alpha components given a pixel in this ColorModel. The pixel value is specified as an int. If the components array is null, a new array will be allocated. The components array will be returned. Color/alpha components are stored in the components array starting at offset (even if the array is allocated by this method). An ArrayIndexOutOfBoundsException is thrown if the components array is not null and is not large enough to hold all the color and alpha components (starting at offset).
Overrides:
getComponents in class ColorModel

getComponents

public final int[] getComponents(Object pixel,
                                 int[] components,
                                 int offset)
Returns an array of unnormalized color/alpha components given a pixel in this ColorModel. The pixel value is specified by an array of data elements of type transferType passed in as an object reference. If pixel is not a primitive array of type transferType, a ClassCastException is thrown. An ArrayIndexOutOfBoundsException is thrown if pixel is not large enough to hold a pixel value for this ColorModel. If the components array is null, a new array will be allocated. The components array will be returned. Color/alpha components are stored in the components array starting at offset (even if the array is allocated by this method). An ArrayIndexOutOfBoundsException is thrown if the components array is not null and is not large enough to hold all the color and alpha components (starting at offset).
Overrides:
getComponents in class ColorModel

createCompatibleWritableRaster

public final WritableRaster createCompatibleWritableRaster(int w,
                                                           int h)
Creates a WritableRaster with the specified width and height that has a data layout (SampleModel) compatible with this ColorModel.
Overrides:
createCompatibleWritableRaster in class ColorModel
See Also:
WritableRaster, SampleModel

getDataElement

public int getDataElement(int[] components,
                          int offset)
Returns a pixel value represented as an int in this ColorModel, given an array of unnormalized color/alpha components. An ArrayIndexOutOfBoundsException is thrown if the components array is not large enough to hold all the color and alpha components (starting at offset).
Overrides:
getDataElement in class ColorModel

getDataElements

public Object getDataElements(int[] components,
                              int offset,
                              Object obj)
Returns a data element array representation of a pixel in this ColorModel, given an array of unnormalized color/alpha components. This array can then be passed to the setDataElements method of a WritableRaster object. An ArrayIndexOutOfBoundsException is thrown if the components array is not large enough to hold all the color and alpha components (starting at offset). If the obj variable is null, a new array will be allocated. If obj is not null, it must be a primitive array of type transferType; otherwise, a ClassCastException is thrown. An ArrayIndexOutOfBoundsException is thrown if obj is not large enough to hold a pixel value for this ColorModel.
Overrides:
getDataElements in class ColorModel
See Also:
WritableRaster.setDataElements(int, int, java.lang.Object), SampleModel.setDataElements(int, int, java.lang.Object, java.awt.image.DataBuffer)

coerceData

public final ColorModel coerceData(WritableRaster raster,
                                   boolean isAlphaPremultiplied)
Forces the Raster data to match the state specified in the isAlphaPremultiplied variable, assuming the data is currently correctly described by this ColorModel. It may multiply or divide the color Raster data by alpha, or do nothing if the data is in the correct state. If the data needs to be coerced, this method will also return an instance of this ColorModel with the isAlphaPremultiplied flag set appropriately.
Overrides:
coerceData in class ColorModel

isCompatibleRaster

public boolean isCompatibleRaster(Raster raster)
Returns true if raster is compatible with this ColorModel and false if it is not.
Overrides:
isCompatibleRaster in class ColorModel

toString

public String toString()
Returns a String that represents this DirectColorModel.
Returns:
a String representing this DirectColorModel.
Overrides:
toString in class ColorModel

Java Platform 1.2

Submit a bug or feature Version 1.2 of Java Platform API Specification
Java is a trademark or registered trademark of Sun Microsystems, Inc. in the US and other countries.
Copyright 1993-1998 Sun Microsystems, Inc. 901 San Antonio Road,
Palo Alto, California, 94303, U.S.A. All Rights Reserved.