PackedColorModel (Java SE 26 & JDK 26)
Description: declaration: module: java.desktop, package: java.awt.image, class: PackedColorModel
Keywords: java.awt.image.PackedColorModel class

# Class PackedColorModel

java.lang.Object
java.awt.image.ColorModel
java.awt.image.PackedColorModel

All Implemented Interfaces:
  `Transparency`

Direct Known Subclasses:
  `DirectColorModel`

public abstract class PackedColorModel extends ColorModel
The `PackedColorModel` class is an abstract `ColorModel` class that works with pixel values which represent 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 with an arbitrary `ColorSpace`. The number of color samples in the pixel values must be the same as the number of color components in the `ColorSpace`. There can be a single alpha sample. The array length is always 1 for those methods that use a primitive array pixel representation of type `transferType`. The transfer types supported are DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT, and DataBuffer.TYPE_INT. 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 `getNumComponents` - 1.
- The first `getNumColorComponents` indices refer to color samples.
- If an alpha sample is present, it corresponds the last index.
- The order of the color indices is specified by the `ColorSpace`. Typically, this reflects the name of the color space type (for example, TYPE_RGB), index 0 corresponds to red, index 1 to green, and index 2 to blue.

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 `PackedColorModel` is typically used with image data that uses masks to define packed samples. For example, a `PackedColorModel` 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 is 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 that use this representation do not throw an `IllegalArgumentException` due to an invalid pixel value.

A subclass of `PackedColorModel` is `DirectColorModel`, which is similar to an X11 TrueColor visual.

See Also:
  - `DirectColorModel`
  - `SinglePixelPackedSampleModel`
  - `BufferedImage`

- ## Field Summary

  ### Fields declared in class ColorModel

  `pixel_bits, transferType`
  Modifier and Type
  Field
  Description
  `protected int`
  `pixel_bits`
  The total number of bits in the pixel.
  `protected int`
  `transferType`
  Data type of the array used to represent pixel values.

  ### Fields declared in interface Transparency

  `BITMASK, OPAQUE, TRANSLUCENT`
  Modifier and Type
  Field
  Description
  `static final int`
  `BITMASK`
  Represents image data that is guaranteed to be either completely opaque, with an alpha value of 1.0, or completely transparent, with an alpha value of 0.0.
  `static final int`
  `OPAQUE`
  Represents image data that is guaranteed to be completely opaque, meaning that all pixels have an alpha value of 1.0.
  `static final int`
  `TRANSLUCENT`
  Represents image data that contains or might contain arbitrary alpha values between and including 0.0 and 1.0.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `PackedColorModel(ColorSpace space, int bits, int[] colorMaskArray, int alphaMask, boolean isAlphaPremultiplied, int trans, int transferType)`
  Constructs a `PackedColorModel` from a color mask array, which specifies which bits in an `int` pixel representation contain each of the color samples, and an alpha mask.
  `PackedColorModel(ColorSpace space, int bits, int rmask, int gmask, int bmask, int amask, boolean isAlphaPremultiplied, int trans, int transferType)`
  Constructs a `PackedColorModel` from the specified masks which indicate which bits in an `int` pixel representation contain the alpha, red, green and blue color samples.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `SampleModel`
  `createCompatibleSampleModel(int w, int h)`
  Creates a `SampleModel` with the specified width and height that has a data layout compatible with this `ColorModel`.
  `boolean`
  `equals(Object obj)`
  Tests if the specified `Object` is an instance of `PackedColorModel` and equals this `PackedColorModel`.
  `WritableRaster`
  `getAlphaRaster(WritableRaster raster)`
  Returns a `WritableRaster` representing the alpha channel of an image, extracted from the input `WritableRaster`.
  `final int`
  `getMask(int index)`
  Returns the mask indicating which bits in a pixel contain the specified color/alpha sample.
  `final int[]`
  `getMasks()`
  Returns a mask array indicating which bits in a pixel contain the color and alpha samples.
  `int`
  `hashCode()`
  Returns the hash code for this PackedColorModel.
  `boolean`
  `isCompatibleSampleModel(SampleModel sm)`
  Checks if the specified `SampleModel` is compatible with this `ColorModel`.

  ### Methods declared in class ColorModel

  `coerceData, createCompatibleWritableRaster, getAlpha, getAlpha, getBlue, getBlue, getColorSpace, getComponents, getComponents, getComponentSize, getComponentSize, getDataElement, getDataElement, getDataElements, getDataElements, getDataElements, getGreen, getGreen, getNormalizedComponents, getNormalizedComponents, getNumColorComponents, getNumComponents, getPixelSize, getRed, getRed, getRGB, getRGB, getRGBdefault, getTransferType, getTransparency, getUnnormalizedComponents, hasAlpha, isAlphaPremultiplied, isCompatibleRaster, toString`
  Modifier and Type
  Method
  Description
  `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`.
  `abstract 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.
  `abstract 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.
  `final ColorSpace`
  `getColorSpace()`
  Returns the `ColorSpace` associated with this `ColorModel`.
  `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[]`
  `getComponentSize()`
  Returns an array of the number of bits per color/alpha component.
  `int`
  `getComponentSize(int componentIdx)`
  Returns the number of bits for the specified color/alpha component.
  `int`
  `getDataElement(float[] normComponents, int normOffset)`
  Returns a pixel value represented as an `int` in this `ColorModel`, given an array of normalized color/alpha components.
  `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(float[] normComponents, int normOffset, Object obj)`
  Returns a data element array representation of a pixel in this `ColorModel`, given an array of normalized 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.
  `abstract 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.
  `float[]`
  `getNormalizedComponents(int[] components, int offset, float[] normComponents, int normOffset)`
  Returns an array of all of the color/alpha components in normalized form, given an unnormalized component array.
  `float[]`
  `getNormalizedComponents(Object pixel, float[] normComponents, int normOffset)`
  Returns an array of all of the color/alpha components in normalized form, given a pixel in this `ColorModel`.
  `int`
  `getNumColorComponents()`
  Returns the number of color components in this `ColorModel`.
  `int`
  `getNumComponents()`
  Returns the number of components, including alpha, in this `ColorModel`.
  `int`
  `getPixelSize()`
  Returns the number of bits per pixel described by this `ColorModel`.
  `abstract 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`
  `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.
  `static ColorModel`
  `getRGBdefault()`
  Returns a `DirectColorModel` that describes the default format for integer RGB values used in many of the methods in the AWT image interfaces for the convenience of the programmer.
  `final int`
  `getTransferType()`
  Returns the transfer type of this `ColorModel`.
  `int`
  `getTransparency()`
  Returns the transparency.
  `int[]`
  `getUnnormalizedComponents(float[] normComponents, int normOffset, int[] components, int offset)`
  Returns an array of all of the color/alpha components in unnormalized form, given a normalized component array.
  `final boolean`
  `hasAlpha()`
  Returns whether or not alpha is supported in this `ColorModel`.
  `final boolean`
  `isAlphaPremultiplied()`
  Returns whether or not the alpha has been premultiplied in the pixel values to be translated by this `ColorModel`.
  `boolean`
  `isCompatibleRaster(Raster raster)`
  Returns `true` if `raster` is compatible with this `ColorModel` and `false` if it is not.
  `String`
  `toString()`
  Returns the `String` representation of the contents of this `ColorModel` object.

  ### Methods declared in class Object

  `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`
  Modifier and Type
  Method
  Description
  `protected Object`
  `clone()`
  Creates and returns a copy of this object.
  `protected void`
  `finalize()`
  Deprecated, for removal: This API element is subject to removal in a future version.
  Finalization is deprecated and subject to removal in a future release.
  `final Class<?>`
  `getClass()`
  Returns the runtime class of this `Object`.
  `final void`
  `notify()`
  Wakes up a single thread that is waiting on this object's monitor.
  `final void`
  `notifyAll()`
  Wakes up all threads that are waiting on this object's monitor.
  `final void`
  `wait()`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted.
  `final void`
  `wait(long timeoutMillis)`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.
  `final void`
  `wait(long timeoutMillis, int nanos)`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

- ## Constructor Details

  - ### PackedColorModel

    public PackedColorModel(ColorSpace space, int bits, int[] colorMaskArray, int alphaMask, boolean isAlphaPremultiplied, int trans, int transferType)
    Constructs a `PackedColorModel` from a color mask array, which specifies which bits in an `int` pixel representation contain each of the color samples, and an alpha mask. Color components are in the specified `ColorSpace`. The length of `colorMaskArray` should be the number of components in the `ColorSpace`. 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 the `alphaMask` is 0, there is no alpha. 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, `trans`, specifies what alpha values can be represented by this color model. The transfer type is the type of primitive array used to represent pixel values.
    Parameters:
      `space` - the specified `ColorSpace`
      `bits` - the number of bits in the pixel values
      `colorMaskArray` - array that specifies the masks representing the bits of the pixel values that represent the color components
      `alphaMask` - specifies the mask representing the bits of the pixel values that represent the alpha component
      `isAlphaPremultiplied` - `true` if color samples are premultiplied by the alpha sample; `false` otherwise
      `trans` - specifies the alpha value that can be represented by this color model
      `transferType` - the type of array used to represent pixel values
    Throws:
      `IllegalArgumentException` - if `bits` is less than 1 or greater than 32

  - ### PackedColorModel

    public PackedColorModel(ColorSpace space, int bits, int rmask, int gmask, int bmask, int amask, boolean isAlphaPremultiplied, int trans, int transferType)
    Constructs a `PackedColorModel` from the specified masks which indicate which bits in an `int` pixel representation contain the alpha, red, green and blue color samples. Color components are in the specified `ColorSpace`, which must be of type ColorSpace.TYPE_RGB. 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 `amask` is 0, there is no alpha. 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, `trans`, specifies what alpha values can be represented by this color model. The transfer type is the type of primitive array used to represent pixel values.
    Parameters:
      `space` - the specified `ColorSpace`
      `bits` - the number of bits in the pixel values
      `rmask` - specifies the mask representing the bits of the pixel values that represent the red color component
      `gmask` - specifies the mask representing the bits of the pixel values that represent the green color component
      `bmask` - specifies the mask representing the bits of the pixel values that represent the blue color component
      `amask` - specifies the mask representing the bits of the pixel values that represent the alpha component
      `isAlphaPremultiplied` - `true` if color samples are premultiplied by the alpha sample; `false` otherwise
      `trans` - specifies the alpha value that can be represented by this color model
      `transferType` - the type of array used to represent pixel values
    Throws:
      `IllegalArgumentException` - if `space` is not a TYPE_RGB space
    See Also:
      - `ColorSpace`

- ## Method Details

  - ### getMask

    public final int getMask(int index)
    Returns the mask indicating which bits in a pixel contain the specified color/alpha sample. For color samples, `index` corresponds to the placement of color sample names in the color space. Thus, an `index` equal to 0 for a CMYK ColorSpace would correspond to Cyan and an `index` equal to 1 would correspond to Magenta. If there is alpha, the alpha `index` would be:

             alphaIndex = numComponents() - 1;

    Parameters:
      `index` - the specified color or alpha sample
    Returns:
      the mask, which indicates which bits of the `int` pixel representation contain the color or alpha sample specified by `index`.
    Throws:
      `ArrayIndexOutOfBoundsException` - if `index` is greater than the number of components minus 1 in this `PackedColorModel` or if `index` is less than zero

  - ### getMasks

    public final int[] getMasks()
    Returns a mask array indicating which bits in a pixel contain the color and alpha samples.
    Returns:
      the mask array , which indicates which bits of the `int` pixel representation contain the color or alpha samples.

  - ### createCompatibleSampleModel

    public SampleModel createCompatibleSampleModel(int w, int h)
    Creates a `SampleModel` with the specified width and height that has a data layout compatible with this `ColorModel`.
    Overrides:
      `createCompatibleSampleModel` in class `ColorModel`
    Parameters:
      `w` - the width (in pixels) of the region of the image data described
      `h` - the height (in pixels) of the region of the image data described
    Returns:
      the newly created `SampleModel`.
    Throws:
      `IllegalArgumentException` - if `w` or `h` is not greater than 0
    See Also:
      - `SampleModel`

  - ### isCompatibleSampleModel

    public boolean isCompatibleSampleModel(SampleModel sm)
    Checks if the specified `SampleModel` is compatible with this `ColorModel`. If `sm` is `null`, this method returns `false`.
    Overrides:
      `isCompatibleSampleModel` in class `ColorModel`
    Parameters:
      `sm` - the specified `SampleModel`, or `null`
    Returns:
      `true` if the specified `SampleModel` is compatible with this `ColorModel`; `false` otherwise.
    See Also:
      - `SampleModel`

  - ### getAlphaRaster

    public WritableRaster getAlphaRaster(WritableRaster raster)
    Returns a `WritableRaster` representing the alpha channel of an image, extracted from the input `WritableRaster`. This method assumes that `WritableRaster` objects associated with this `ColorModel` store the alpha band, if present, as the last band of image data. Returns `null` if there is no separate spatial alpha channel associated with this `ColorModel`. This method creates a new `WritableRaster`, but shares the data array.
    Overrides:
      `getAlphaRaster` in class `ColorModel`
    Parameters:
      `raster` - a `WritableRaster` containing an image
    Returns:
      a `WritableRaster` that represents the alpha channel of the image contained in `raster`.

  - ### equals

    public boolean equals(Object obj)
    Tests if the specified `Object` is an instance of `PackedColorModel` and equals this `PackedColorModel`.
    Overrides:
      `equals` in class `ColorModel`
    Parameters:
      `obj` - the `Object` to test for equality
    Returns:
      `true` if the specified `Object` is an instance of `PackedColorModel` and equals this `PackedColorModel`; `false` otherwise.
    See Also:
      - `Object.hashCode()`
      - `HashMap`

  - ### hashCode

    public int hashCode()
    Returns the hash code for this PackedColorModel.
    Overrides:
      `hashCode` in class `ColorModel`
    Returns:
      a hash code for this PackedColorModel.
    See Also:
      - `Object.equals(java.lang.Object)`
      - `System.identityHashCode(Object)`