📄 indexcolormodel.java
字号:
/* * @(#)IndexColorModel.java 1.92 03/01/23 * * Copyright 2003 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package java.awt.image;import java.awt.Transparency;import java.awt.color.ColorSpace;import java.math.BigInteger;/** * The <code>IndexColorModel</code> class is a <code>ColorModel</code> * class that works with pixel values consisting of a * single sample that is an index into a fixed colormap in the default * sRGB color space. The colormap specifies red, green, blue, and * optional alpha components corresponding to each index. All components * are represented in the colormap as 8-bit unsigned integral values. * Some constructors allow the caller to specify "holes" in the colormap * by indicating which colormap entries are valid and which represent * unusable colors via the bits set in a <code>BigInteger</code> object. * This color model is similar to an X11 PseudoColor visual. * <p> * Some constructors provide a means to specify an alpha component * for each pixel in the colormap, while others either provide no * such means or, in some cases, a flag to indicate whether the * colormap data contains alpha values. If no alpha is supplied to * the constructor, an opaque alpha component (alpha = 1.0) is * assumed for each entry. * An optional transparent pixel value can be supplied that indicates a * completely transparent pixel, regardless of any alpha component * supplied or assumed for that pixel value. * Note that the color components in the colormap of an * <code>IndexColorModel</code> objects are never pre-multiplied with * the alpha components. * <p> * <a name="transparency"> * The transparency of an <code>IndexColorModel</code> object is * determined by examining the alpha components of the colors in the * colormap and choosing the most specific value after considering * the optional alpha values and any transparent index specified. * The transparency value is <code>Transparency.OPAQUE</code> * only if all valid colors in * the colormap are opaque and there is no valid transparent pixel. * If all valid colors * in the colormap are either completely opaque (alpha = 1.0) or * completely transparent (alpha = 0.0), which typically occurs when * a valid transparent pixel is specified, * the value is <code>Transparency.BITMASK</code>. * Otherwise, the value is <code>Transparency.TRANSLUCENT</code>, indicating * that some valid color has an alpha component that is * neither completely transparent nor completely opaque (0.0 < alpha < 1.0). * </a> * <p> * The index represented by a pixel value is stored in the least * significant <em>n</em> bits of the pixel representations passed to the * methods of this class, where <em>n</em> is the pixel size specified to the * constructor for a particular <code>IndexColorModel</code> object; * <em>n</em> must be between 1 and 16, inclusive. * Higher order bits in pixel representations are assumed to be zero. * For those methods that use a primitive array pixel representation of * type <code>transferType</code>, the array length is always one. * The transfer types supported are <code>DataBuffer.TYPE_BYTE</code> and * <code>DataBuffer.TYPE_USHORT</code>. 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 <code>IllegalArgumentException</code> due to an invalid * pixel value. * <p> * Many of the methods in this class are final. The reason for * this is that 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 reaons, but * you cannot override or modify the behaviour of those methods. * * @see ColorModel * @see ColorSpace * @see DataBuffer * * @version 10 Feb 1997 */public class IndexColorModel extends ColorModel { private int rgb[]; private int map_size; private int transparent_index = -1; private boolean allgrayopaque; private BigInteger validBits; private static int[] opaqueBits = {8, 8, 8}; private static int[] alphaBits = {8, 8, 8, 8}; static private native void initIDs(); static { ColorModel.loadLibraries(); initIDs(); } /** * Constructs an <code>IndexColorModel</code> from the specified * arrays of red, green, and blue components. Pixels described * by this color model all have alpha components of 255 * unnormalized (1.0 normalized), which means they * are fully opaque. All of the arrays specifying the color * components must have at least the specified number of entries. * The <code>ColorSpace</code> is the default sRGB space. * Since there is no alpha information in any of the arguments * to this constructor, the transparency value is always * <code>Transparency.OPAQUE</code>. * The transfer type is the smallest of <code>DataBuffer.TYPE_BYTE</code> * or <code>DataBuffer.TYPE_USHORT</code> that can hold a single pixel. * @param bits the number of bits each pixel occupies * @param size the size of the color component arrays * @param r the array of red color components * @param g the array of green color components * @param b the array of blue color components * @throws IllegalArgumentException if <code>bits</code> is less * than 1 or greater than 16 * @throws IllegalArgumentException if <code>size</code> is less * than 1 */ public IndexColorModel(int bits, int size, byte r[], byte g[], byte b[]) { super(bits, opaqueBits, ColorSpace.getInstance(ColorSpace.CS_sRGB), false, false, OPAQUE, ColorModel.getDefaultTransferType(bits)); if (bits < 1 || bits > 16) { throw new IllegalArgumentException("Number of bits must be between" +" 1 and 16."); } setRGBs(size, r, g, b, null); } /** * Constructs an <code>IndexColorModel</code> from the given arrays * of red, green, and blue components. Pixels described by this color * model all have alpha components of 255 unnormalized * (1.0 normalized), which means they are fully opaque, except * for the indicated transparent pixel. All of the arrays * specifying the color components must have at least the specified * number of entries. * The <code>ColorSpace</code> is the default sRGB space. * The transparency value may be <code>Transparency.OPAQUE</code> or * <code>Transparency.BITMASK</code> depending on the arguments, as * specified in the <a href="#transparency">class description</a> above. * The transfer type is the smallest of <code>DataBuffer.TYPE_BYTE</code> * or <code>DataBuffer.TYPE_USHORT</code> that can hold a * single pixel. * @param bits the number of bits each pixel occupies * @param size the size of the color component arrays * @param r the array of red color components * @param g the array of green color components * @param b the array of blue color components * @param trans the index of the transparent pixel * @throws IllegalArgumentException if <code>bits</code> is less than * 1 or greater than 16 * @throws IllegalArgumentException if <code>size</code> is less than * 1 */ public IndexColorModel(int bits, int size, byte r[], byte g[], byte b[], int trans) { super(bits, opaqueBits, ColorSpace.getInstance(ColorSpace.CS_sRGB), false, false, OPAQUE, ColorModel.getDefaultTransferType(bits)); if (bits < 1 || bits > 16) { throw new IllegalArgumentException("Number of bits must be between" +" 1 and 16."); } setRGBs(size, r, g, b, null); setTransparentPixel(trans); } /** * Constructs an <code>IndexColorModel</code> from the given * arrays of red, green, blue and alpha components. All of the * arrays specifying the components must have at least the specified * number of entries. * The <code>ColorSpace</code> is the default sRGB space. * The transparency value may be any of <code>Transparency.OPAQUE</code>, * <code>Transparency.BITMASK</code>, * or <code>Transparency.TRANSLUCENT</code> * depending on the arguments, as specified * in the <a href="#transparency">class description</a> above. * The transfer type is the smallest of <code>DataBuffer.TYPE_BYTE</code> * or <code>DataBuffer.TYPE_USHORT</code> that can hold a single pixel. * @param bits the number of bits each pixel occupies * @param size the size of the color component arrays * @param r the array of red color components * @param g the array of green color components * @param b the array of blue color components * @param a the array of alpha value components * @throws IllegalArgumentException if <code>bits</code> is less * than 1 or greater than 16 * @throws IllegalArgumentException if <code>size</code> is less * than 1 */ public IndexColorModel(int bits, int size, byte r[], byte g[], byte b[], byte a[]) { super (bits, alphaBits, ColorSpace.getInstance(ColorSpace.CS_sRGB), true, false, TRANSLUCENT, ColorModel.getDefaultTransferType(bits)); if (bits < 1 || bits > 16) { throw new IllegalArgumentException("Number of bits must be between" +" 1 and 16."); } setRGBs (size, r, g, b, a); } /** * Constructs an <code>IndexColorModel</code> from a single * array of interleaved red, green, blue and optional alpha * components. The array must have enough values in it to * fill all of the needed component arrays of the specified * size. The <code>ColorSpace</code> is the default sRGB space. * The transparency value may be any of <code>Transparency.OPAQUE</code>, * <code>Transparency.BITMASK</code>, * or <code>Transparency.TRANSLUCENT</code> * depending on the arguments, as specified * in the <a href="#transparency">class description</a> above. * The transfer type is the smallest of * <code>DataBuffer.TYPE_BYTE</code> or <code>DataBuffer.TYPE_USHORT</code> * that can hold a single pixel. * * @param bits the number of bits each pixel occupies * @param size the size of the color component arrays * @param cmap the array of color components * @param start the starting offset of the first color component * @param hasalpha indicates whether alpha values are contained in * the <code>cmap</code> array * @throws IllegalArgumentException if <code>bits</code> is less * than 1 or greater than 16 * @throws IllegalArgumentException if <code>size</code> is less * than 1 */ public IndexColorModel(int bits, int size, byte cmap[], int start, boolean hasalpha) { this(bits, size, cmap, start, hasalpha, -1); if (bits < 1 || bits > 16) { throw new IllegalArgumentException("Number of bits must be between" +" 1 and 16."); } } /** * Constructs an <code>IndexColorModel</code> from a single array of * interleaved red, green, blue and optional alpha components. The * specified transparent index represents a pixel that is considered * entirely transparent regardless of any alpha value specified * for it. The array must have enough values in it to fill all * of the needed component arrays of the specified size. * The <code>ColorSpace</code> is the default sRGB space. * The transparency value may be any of <code>Transparency.OPAQUE</code>, * <code>Transparency.BITMASK</code>, * or <code>Transparency.TRANSLUCENT</code> * depending on the arguments, as specified * in the <a href="#transparency">class description</a> above. * The transfer type is the smallest of * <code>DataBuffer.TYPE_BYTE</code> or <code>DataBuffer.TYPE_USHORT</code> * that can hold a single pixel. * @param bits the number of bits each pixel occupies * @param size the size of the color component arrays * @param cmap the array of color components * @param start the starting offset of the first color component * @param hasalpha indicates whether alpha values are contained in * the <code>cmap</code> array * @param trans the index of the fully transparent pixel * @throws IllegalArgumentException if <code>bits</code> is less than * 1 or greater than 16 * @throws IllegalArgumentException if <code>size</code> is less than * 1 */ public IndexColorModel(int bits, int size, byte cmap[], int start, boolean hasalpha, int trans) { // REMIND: This assumes the ordering: RGB[A] super(bits, opaqueBits, ColorSpace.getInstance(ColorSpace.CS_sRGB), false, false, OPAQUE, ColorModel.getDefaultTransferType(bits)); if (bits < 1 || bits > 16) { throw new IllegalArgumentException("Number of bits must be between" +" 1 and 16."); } if (size < 1) { throw new IllegalArgumentException("Map size ("+size+ ") must be >= 1"); } map_size = size; rgb = new int[calcRealMapSize(bits, size)]; int j = start; int alpha = 0xff; boolean allgray = true; int transparency = OPAQUE; for (int i = 0; i < size; i++) { int r = cmap[j++] & 0xff; int g = cmap[j++] & 0xff; int b = cmap[j++] & 0xff; allgray = allgray && (r == g) && (g == b); if (hasalpha) { alpha = cmap[j++] & 0xff; if (alpha != 0xff) { if (alpha == 0x00) { if (transparency == OPAQUE) { transparency = BITMASK; } if (transparent_index < 0) { transparent_index = i; } } else { transparency = TRANSLUCENT; } allgray = false; } } rgb[i] = (alpha << 24) | (r << 16) | (g << 8) | b; } this.allgrayopaque = allgray; setTransparency(transparency); setTransparentPixel(trans); } /** * Constructs an <code>IndexColorModel</code> from an array of * ints where each int is comprised of red, green, blue, and * optional alpha components in the default RGB color model format. * The specified transparent index represents a pixel that is considered * entirely transparent regardless of any alpha value specified * for it. The array must have enough values in it to fill all * of the needed component arrays of the specified size. * The <code>ColorSpace</code> is the default sRGB space. * The transparency value may be any of <code>Transparency.OPAQUE</code>, * <code>Transparency.BITMASK</code>, * or <code>Transparency.TRANSLUCENT</code> * depending on the arguments, as specified * in the <a href="#transparency">class description</a> above. * @param bits the number of bits each pixel occupies * @param size the size of the color component arrays * @param cmap the array of color components * @param start the starting offset of the first color component * @param hasalpha indicates whether alpha values are contained in * the <code>cmap</code> array
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -