📄 directcolormodel.java
字号:
/* * @(#)DirectColorModel.java 1.77 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.color.ColorSpace;import java.awt.Transparency;/** * The <code>DirectColorModel</code> class is a <code>ColorModel</code> * class that 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 type ColorSpace.TYPE_RGB. * In addition, for each component of the ColorSpace, the minimum * normalized component value obtained via the <code>getMinValue()</code> * method of ColorSpace must be 0.0, and the maximum value obtained via * the <code>getMaxValue()</code> method must be 1.0 (these min/max * values are typical for RGB spaces). * There must be three color samples in the pixel values and there can * be a single alpha sample. 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 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: * <ul> * <li> Masks are identified by indices running from 0 through 2 * if no alpha is present, or 3 if an alpha is present. * <li> The first three indices refer to color samples; * index 0 corresponds to red, index 1 to green, and index 2 to blue. * <li> Index 3 corresponds to the alpha sample, if present. * </ul> * <p> * 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 <code>DirectColorModel</code> is * typically used with image data which uses masks to define packed * samples. For example, a <code>DirectColorModel</code> can be used in * conjunction with a <code>SinglePixelPackedSampleModel</code> to * construct a {@link BufferedImage}. Normally the masks used by the * {@link SampleModel} and the <code>ColorModel</code> would be the * same. However, if they are different, the color interpretation * of pixel data will be done according to the masks of the * <code>ColorModel</code>. * <p> * 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 <code>IllegalArgumentException</code> * due to an invalid pixel value. * <p> * This color model is similar to an X11 TrueColor visual. * The default RGB ColorModel specified by the * {@link ColorModel#getRGBdefault() getRGBdefault} method is a * <code>DirectColorModel</code> with the following parameters: * <pre> * 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 * </pre> * <p> * 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 ColorModel * @see ColorSpace * @see SinglePixelPackedSampleModel * @see BufferedImage * @see ColorModel#getRGBdefault * * @version 10 Feb 1997 */public class DirectColorModel extends PackedColorModel { private int red_mask; private int green_mask; private int blue_mask; private int alpha_mask; private int red_offset; private int green_offset; private int blue_offset; private int alpha_offset; private int red_scale; private int green_scale; private int blue_scale; private int alpha_scale; private boolean is_LinearRGB; private int lRGBprecision; private byte[] tosRGB8LUT; private byte[] fromsRGB8LUT8; private short[] fromsRGB8LUT16; /** * Constructs a <code>DirectColorModel</code> from the specified masks * that indicate which bits in an <code>int</code> pixel representation * contain the red, green and blue color samples. As pixel values do not * contain alpha information, all pixels are treated as opaque, which * means that 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 <code>int</code> pixel representation. * The <code>ColorSpace</code> is the default sRGB space. The * transparency value is Transparency.OPAQUE. The transfer type * is the smallest of DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT, * or DataBuffer.TYPE_INT that can hold a single pixel. * @param bits the number of bits in the pixel values; for example, * the sum of the number of bits in the masks. * @param rmask specifies a mask indicating which bits in an * integer pixel contain the red component * @param gmask specifies a mask indicating which bits in an * integer pixel contain the green component * @param bmask specifies a mask indicating which bits in an * integer pixel contain the blue component * */ public DirectColorModel(int bits, int rmask, int gmask, int bmask) { this(bits, rmask, gmask, bmask, 0); } /** * Constructs a <code>DirectColorModel</code> from the specified masks * that indicate which bits in an <code>int</code> pixel representation * contain the red, green and blue color samples and the alpha sample, * if present. If <code>amask</code> is 0, pixel values do not contain * alpha information and all pixels are treated as opaque, which means * that 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 <code>int</code> pixel representation. Alpha, if present, is not * premultiplied. The <code>ColorSpace</code> is the default sRGB space. * The transparency value is Transparency.OPAQUE if no alpha is * present, or Transparency.TRANSLUCENT otherwise. The transfer type * is the smallest of DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT, * or DataBuffer.TYPE_INT that can hold a single pixel. * @param bits the number of bits in the pixel values; for example, * the sum of the number of bits in the masks. * @param rmask specifies a mask indicating which bits in an * integer pixel contain the red component * @param gmask specifies a mask indicating which bits in an * integer pixel contain the green component * @param bmask specifies a mask indicating which bits in an * integer pixel contain the blue component * @param amask specifies a mask indicating which bits in an * integer pixel contain the alpha component */ public DirectColorModel(int bits, int rmask, int gmask, int bmask, int amask) { super (ColorSpace.getInstance(ColorSpace.CS_sRGB), bits, rmask, gmask, bmask, amask, false, amask == 0 ? Transparency.OPAQUE : Transparency.TRANSLUCENT, ColorModel.getDefaultTransferType(bits)); setFields(); } /** * Constructs a <code>DirectColorModel</code> from the specified * parameters. Color components are in the specified * <code>ColorSpace</code>, which must be of type ColorSpace.TYPE_RGB * and have minimum normalized component values which are all 0.0 * and maximum values which are all 1.0. * The masks specify which bits in an <code>int</code> pixel * representation contain the red, green and blue color samples and * the alpha sample, if present. If <code>amask</code> is 0, pixel * values do not contain alpha information and all pixels are treated * as opaque, which means that 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 <code>int</code> pixel * representation. If there is alpha, the <code>boolean</code> * <code>isAlphaPremultiplied</code> specifies how to interpret * color and alpha samples in pixel values. If the <code>boolean</code> * is <code>true</code>, color samples are assumed to have been * multiplied by the alpha sample. The transparency value is * 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. * @param space the specified <code>ColorSpace</code> * @param bits the number of bits in the pixel values; for example, * the sum of the number of bits in the masks. * @param rmask specifies a mask indicating which bits in an * integer pixel contain the red component * @param gmask specifies a mask indicating which bits in an * integer pixel contain the green component * @param bmask specifies a mask indicating which bits in an * integer pixel contain the blue component * @param amask specifies a mask indicating which bits in an * integer pixel contain the alpha component * @param isAlphaPremultiplied <code>true</code> if color samples are * premultiplied by the alpha sample; <code>false</code> otherwise * @param transferType the type of array used to represent pixel values * @throws IllegalArgumentException if <code>space</code> is not a * TYPE_RGB space or if the min/max normalized component * values are not 0.0/1.0. */ public DirectColorModel(ColorSpace space, int bits, int rmask, int gmask, int bmask, int amask, boolean isAlphaPremultiplied, int transferType) { super (space, bits, rmask, gmask, bmask, amask, isAlphaPremultiplied, amask == 0 ? Transparency.OPAQUE : Transparency.TRANSLUCENT, transferType); if (ColorModel.isLinearRGBspace(colorSpace)) { is_LinearRGB = true; if (maxBits <= 8) { lRGBprecision = 8; tosRGB8LUT = ColorModel.getLinearRGB8TosRGB8LUT(); fromsRGB8LUT8 = ColorModel.getsRGB8ToLinearRGB8LUT(); } else { lRGBprecision = 16; tosRGB8LUT = ColorModel.getLinearRGB16TosRGB8LUT(); fromsRGB8LUT16 = ColorModel.getsRGB8ToLinearRGB16LUT(); } } else if (!is_sRGB) { for (int i = 0; i < 3; i++) { // super constructor checks that space is TYPE_RGB // check here that min/max are all 0.0/1.0 if ((space.getMinValue(i) != 0.0f) || (space.getMaxValue(i) != 1.0f)) { throw new IllegalArgumentException( "Illegal min/max RGB component value"); } } } setFields(); } /** * Returns the mask indicating which bits in an <code>int</code> pixel * representation contain the red color component. * @return the mask, which indicates which bits of the <code>int</code> * pixel representation contain the red color sample. */ final public int getRedMask() { return maskArray[0]; } /** * Returns the mask indicating which bits in an <code>int</code> pixel * representation contain the green color component. * @return the mask, which indicates which bits of the <code>int</code> * pixel representation contain the green color sample. */ final public int getGreenMask() { return maskArray[1]; } /** * Returns the mask indicating which bits in an <code>int</code> pixel * representation contain the blue color component. * @return the mask, which indicates which bits of the <code>int</code> * pixel representation contain the blue color sample. */ final public int getBlueMask() { return maskArray[2]; } /** * Returns the mask indicating which bits in an <code>int</code> pixel * representation contain the alpha component. * @return the mask, which indicates which bits of the <code>int</code> * pixel representation contain the alpha sample. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -