java.awt.image

Class IndexColorModel

public class IndexColorModel extends ColorModel

Color model similar to pseudo visual in X11.

This color model maps linear pixel values to actual RGB and alpha colors. Thus, pixel values are indexes into the color map. Each color component is an 8-bit unsigned value.

The IndexColorModel supports a map of valid pixels, allowing the representation of holes in the the color map. The valid map is represented as a {@link BigInteger} where each bit indicates the validity of the map entry with the same index.

Colors can have alpha components for transparency support. If alpha component values aren't given, color values are opaque. The model also supports a reserved pixel value to represent completely transparent colors, no matter what the actual color component values are.

IndexColorModel supports anywhere from 1 to 16 bit index values. The allowed transfer types are {@link DataBuffer#TYPE_BYTE} and {@link DataBuffer#TYPE_USHORT}.
Constructor Summary
IndexColorModel(int bits, int size, byte[] reds, byte[] greens, byte[] blues)
Creates a new indexed color model for size color elements with no alpha component.
IndexColorModel(int bits, int size, byte[] reds, byte[] greens, byte[] blues, int trans)
Creates a new indexed color model for size color elements.
IndexColorModel(int bits, int size, byte[] reds, byte[] greens, byte[] blues, byte[] alphas)
Creates a new indexed color model for size color elements including alpha.
IndexColorModel(int bits, int size, byte[] cmap, int start, boolean hasAlpha)
Creates a new indexed color model using the color components in cmap.
IndexColorModel(int bits, int size, byte[] cmap, int start, boolean hasAlpha, int trans)
Construct an IndexColorModel from an array of red, green, blue, and optional alpha components.
IndexColorModel(int bits, int size, int[] cmap, int start, boolean hasAlpha, int trans, int transferType)
Construct an IndexColorModel from an array of size packed colors.
IndexColorModel(int bits, int size, int[] cmap, int start, int transferType, BigInteger validBits)
Construct an IndexColorModel using a colormap with holes.
Method Summary
BufferedImageconvertToIntDiscrete(Raster raster, boolean forceARGB)
Construct a {@link BufferedImage} with rgb pixel values from a {@link Raster}.
SampleModelcreateCompatibleSampleModel(int w, int h)
Creates a {@link SampleModel} that is compatible to this color model.
intgetAlpha(int pixel)
Returns the alpha component of the color in the lookup table for the given pixel value.
voidgetAlphas(byte[] a)
Fills the supplied array with the alpha component of each color in the lookup table.
intgetBlue(int pixel)
Returns the blue component of the color in the lookup table for the given pixel value.
voidgetBlues(byte[] b)
Fills the supplied array with the blue component of each color in the lookup table.
intgetGreen(int pixel)
Returns the green component of the color in the lookup table for the given pixel value.
voidgetGreens(byte[] g)
Fills the supplied array with the green component of each color in the lookup table.
intgetMapSize()
Returns the size of the color lookup table.
intgetRed(int pixel)
Returns the red component of the color in the lookup table for the given pixel value.
voidgetReds(byte[] r)
Fills the supplied array with the red component of each color in the lookup table.
intgetRGB(int pixel)
Get the RGB color value of the given pixel using the default RGB color model.
voidgetRGBs(int[] rgb)
Get the RGB color values of all pixels in the map using the default RGB color model.
intgetTransparentPixel()
Get the index of the transparent color in this color model.
BigIntegergetValidPixels()
Returns a binary value ({@link BigInteger}) where each bit represents an entry in the color lookup table.
booleanisValid(int pixel)
Return true if the lookup table contains valid data for pixel, and false otherwise.
booleanisValid()
Return true if all pixels are valid, false otherwise.

Constructor Detail

IndexColorModel

public IndexColorModel(int bits, int size, byte[] reds, byte[] greens, byte[] blues)
Creates a new indexed color model for size color elements with no alpha component. Each array must contain at least size elements. For each array, the i-th color is described by reds[i], greens[i] and blues[i].

Parameters: bits the number of bits needed to represent size colors. size the number of colors in the color map. reds the red component of all colors. greens the green component of all colors. blues the blue component of all colors.

Throws: IllegalArgumentException if bits < 1 or bits > 16. NullPointerException if any of the arrays is null. ArrayIndexOutOfBoundsException if size is greater than the length of the component arrays.

IndexColorModel

public IndexColorModel(int bits, int size, byte[] reds, byte[] greens, byte[] blues, int trans)
Creates a new indexed color model for size color elements. Each array must contain at least size elements. For each array, the i-th color is described by reds[i], greens[i] and blues[i]. All the colors are opaque except for the transparent color.

Parameters: bits the number of bits needed to represent size colors size the number of colors in the color map reds the red component of all colors greens the green component of all colors blues the blue component of all colors trans the index of the transparent color (use -1 for no transparent color).

Throws: IllegalArgumentException if bits < 1 or bits > 16. NullPointerException if any of the arrays is null. ArrayIndexOutOfBoundsException if size is greater than the length of the component arrays.

IndexColorModel

public IndexColorModel(int bits, int size, byte[] reds, byte[] greens, byte[] blues, byte[] alphas)
Creates a new indexed color model for size color elements including alpha. Each array must contain at least size elements. For each array, the i-th color is described by reds[i], greens[i], blues[i] and alphas[i].

Parameters: bits the number of bits needed to represent size colors. size the number of colors in the color map. reds the red component of all colors. greens the green component of all colors. blues the blue component of all colors. alphas the alpha component of all colors (null permitted).

Throws: IllegalArgumentException if bits < 1 or bits > 16. NullPointerException if reds, greens or blues is null. ArrayIndexOutOfBoundsException if size is greater than the length of the component arrays.

IndexColorModel

public IndexColorModel(int bits, int size, byte[] cmap, int start, boolean hasAlpha)
Creates a new indexed color model using the color components in cmap. If hasAlpha is true then cmap contains an alpha component after each of the red, green and blue components.

Parameters: bits the number of bits needed to represent size colors size the number of colors in the color map cmap packed color components start the offset of the first color component in cmap hasAlpha cmap has alpha values

Throws: IllegalArgumentException if bits < 1, bits > 16, or size < 1. NullPointerException if cmap is null.

IndexColorModel

public IndexColorModel(int bits, int size, byte[] cmap, int start, boolean hasAlpha, int trans)
Construct an IndexColorModel from an array of red, green, blue, and optional alpha components. The component values are interleaved as RGB(A).

Parameters: bits the number of bits needed to represent size colors size the number of colors in the color map cmap interleaved color components start the offset of the first color component in cmap hasAlpha cmap has alpha values trans the index of the transparent color

Throws: IllegalArgumentException if bits < 1, bits > 16, or size < 1. NullPointerException if cmap is null.

IndexColorModel

public IndexColorModel(int bits, int size, int[] cmap, int start, boolean hasAlpha, int trans, int transferType)
Construct an IndexColorModel from an array of size packed colors. Each int element contains 8-bit red, green, blue, and optional alpha values packed in order. If hasAlpha is false, then all the colors are opaque except for the transparent color.

Parameters: bits the number of bits needed to represent size colors size the number of colors in the color map cmap packed color components start the offset of the first color component in cmap hasAlpha cmap has alpha values trans the index of the transparent color transferType {@link DataBuffer#TYPE_BYTE} or {@link DataBuffer#TYPE_USHORT}.

Throws: IllegalArgumentException if bits < 1, bits > 16, or size < 1. IllegalArgumentException if transferType is something other than {@link DataBuffer#TYPE_BYTE} or {@link DataBuffer#TYPE_USHORT}.

IndexColorModel

public IndexColorModel(int bits, int size, int[] cmap, int start, int transferType, BigInteger validBits)
Construct an IndexColorModel using a colormap with holes.

The IndexColorModel is built from the array of ints defining the colormap. Each element contains red, green, blue, and alpha components. The ColorSpace is sRGB. The transparency value is automatically determined.

This constructor permits indicating which colormap entries are valid, using the validBits argument. Each entry in cmap is valid if the corresponding bit in validBits is set.

Parameters: bits the number of bits needed to represent size colors. size the number of colors in the color map. cmap packed color components. start the offset of the first color component in cmap. transferType {@link DataBuffer#TYPE_BYTE} or {@link DataBuffer#TYPE_USHORT}. validBits a map of the valid entries in cmap.

Throws: IllegalArgumentException if bits < 1, bits > 16, or size < 1. IllegalArgumentException if transferType is something other than {@link DataBuffer#TYPE_BYTE} or {@link DataBuffer#TYPE_USHORT}.

Method Detail

convertToIntDiscrete

public BufferedImage convertToIntDiscrete(Raster raster, boolean forceARGB)
Construct a {@link BufferedImage} with rgb pixel values from a {@link Raster}. Constructs a new BufferedImage in which each pixel is an RGBA int from a Raster with index-valued pixels. If this model has no alpha component or transparent pixel, the type of the new BufferedImage is TYPE_INT_RGB. Otherwise the type is TYPE_INT_ARGB. If forceARGB is true, the type is forced to be TYPE_INT_ARGB no matter what.

Parameters: raster The source of pixel values. forceARGB True if type must be TYPE_INT_ARGB.

Returns: New BufferedImage with RBGA int pixel values.

createCompatibleSampleModel

public SampleModel createCompatibleSampleModel(int w, int h)
Creates a {@link SampleModel} that is compatible to this color model. This will be a {@link MultiPixelPackedSampleModel} for bits/pixel of 1, 2 or 4, or a {@link ComponentColorModel} for the other cases.

Parameters: w the width of the sample model to create h the height of the sample model to create

Returns: a compatible sample model

getAlpha

public final int getAlpha(int pixel)
Returns the alpha component of the color in the lookup table for the given pixel value. If no alpha channel was specified when the color model was created, then 255 is returned for all pixels except the transparent pixel (if one is defined - see {@link #getTransparentPixel()}) which returns an alpha of 0.

Parameters: pixel the pixel lookup value.

Returns: The alpha component of the color in the lookup table (in the range 0 to 255).

Throws: ArrayIndexOutOfBoundsException if pixel is negative.

getAlphas

public final void getAlphas(byte[] a)
Fills the supplied array with the alpha component of each color in the lookup table. If the model has a transparent pixel specified, the alpha for that pixel will be 0.

Parameters: a an array that is at least as large as {@link #getMapSize()}.

Throws: NullPointerException if a is null. ArrayIndexOutOfBoundsException if a has less than {@link #getMapSize()} elements.

getBlue

public final int getBlue(int pixel)
Returns the blue component of the color in the lookup table for the given pixel value.

Parameters: pixel the pixel lookup value.

Returns: The blue component of the color in the lookup table.

Throws: ArrayIndexOutOfBoundsException if pixel is negative.

getBlues

public final void getBlues(byte[] b)
Fills the supplied array with the blue component of each color in the lookup table.

Parameters: b an array that is at least as large as {@link #getMapSize()}.

Throws: NullPointerException if b is null. ArrayIndexOutOfBoundsException if b has less than {@link #getMapSize()} elements.

getGreen

public final int getGreen(int pixel)
Returns the green component of the color in the lookup table for the given pixel value.

Parameters: pixel the pixel lookup value.

Returns: The green component of the color in the lookup table.

Throws: ArrayIndexOutOfBoundsException if pixel is negative.

getGreens

public final void getGreens(byte[] g)
Fills the supplied array with the green component of each color in the lookup table.

Parameters: g an array that is at least as large as {@link #getMapSize()}.

Throws: NullPointerException if g is null. ArrayIndexOutOfBoundsException if g has less than {@link #getMapSize()} elements.

getMapSize

public final int getMapSize()
Returns the size of the color lookup table.

Returns: The size of the color lookup table.

getRed

public final int getRed(int pixel)
Returns the red component of the color in the lookup table for the given pixel value.

Parameters: pixel the pixel lookup value.

Returns: The red component of the color in the lookup table.

Throws: ArrayIndexOutOfBoundsException if pixel is negative.

getReds

public final void getReds(byte[] r)
Fills the supplied array with the red component of each color in the lookup table.

Parameters: r an array that is at least as large as {@link #getMapSize()}.

Throws: NullPointerException if r is null. ArrayIndexOutOfBoundsException if r has less than {@link #getMapSize()} elements.

getRGB

public final int getRGB(int pixel)
Get the RGB color value of the given pixel using the default RGB color model.

Parameters: pixel the pixel lookup value.

Returns: The RGB color value.

Throws: ArrayIndexOutOfBoundsException if pixel is negative.

getRGBs

public final void getRGBs(int[] rgb)
Get the RGB color values of all pixels in the map using the default RGB color model.

Parameters: rgb The destination array.

getTransparentPixel

public final int getTransparentPixel()
Get the index of the transparent color in this color model.

Returns: The index of the color that is considered transparent, or -1 if there is no transparent color.

getValidPixels

public BigInteger getValidPixels()
Returns a binary value ({@link BigInteger}) where each bit represents an entry in the color lookup table. If the bit is on, the entry is valid.

Returns: The binary value.

isValid

public boolean isValid(int pixel)
Return true if the lookup table contains valid data for pixel, and false otherwise.

Parameters: pixel the pixel value used to index the color lookup table.

Returns: true if pixel is valid, false otherwise.

isValid

public boolean isValid()
Return true if all pixels are valid, false otherwise.

Returns: true if all pixels are valid, false otherwise.