📄 colormodel.java
字号:
/* * @(#)ColorModel.java 1.79 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.awt.color.ICC_ColorSpace;import sun.awt.color.ICC_Transform;import sun.awt.color.CMM;import java.awt.Toolkit;import java.util.Collections;import java.util.Map;import java.util.WeakHashMap;/** * The <code>ColorModel</code> abstract class encapsulates the * methods for translating a pixel value to color components * (for example, red, green, and blue) and an alpha component. * In order to render an image to the screen, a printer, or another * image, pixel values must be converted to color and alpha components. * As arguments to or return values from methods of this class, * pixels are represented as 32-bit ints or as arrays of primitive types. * The number, order, and interpretation of color components for a * <code>ColorModel</code> is specified by its <code>ColorSpace</code>. * A <code>ColorModel</code> used with pixel data that does not include * alpha information treats all pixels as opaque, which is an alpha * value of 1.0. * <p> * This <code>ColorModel</code> class supports two representations of * pixel values. A pixel value can be a single 32-bit int or an * array of primitive types. The Java(tm) Platform 1.0 and 1.1 APIs * represented pixels as single <code>byte</code> or single * <code>int</code> values. For purposes of the <code>ColorModel</code> * class, pixel value arguments were passed as ints. The Java(tm) 2 * Platform API introduced additional classes for representing images. * With {@link BufferedImage} or {@link RenderedImage} * objects, based on {@link Raster} and {@link SampleModel} classes, pixel * values might not be conveniently representable as a single int. * Consequently, <code>ColorModel</code> now has methods that accept * pixel values represented as arrays of primitive types. The primitive * type used by a particular <code>ColorModel</code> object is called its * transfer type. * <p> * <code>ColorModel</code> objects used with images for which pixel values * are not conveniently representable as a single int throw an * {@link IllegalArgumentException} when methods taking a single int pixel * argument are called. Subclasses of <code>ColorModel</code> must * specify the conditions under which this occurs. This does not * occur with {@link DirectColorModel} or {@link IndexColorModel} objects. * <p> * Currently, the transfer types supported by the Java 2D(tm) API are * DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT, DataBuffer.TYPE_INT, * DataBuffer.TYPE_SHORT, DataBuffer.TYPE_FLOAT, and DataBuffer.TYPE_DOUBLE. * Most rendering operations will perform much faster when using ColorModels * and images based on the first three of these types. In addition, some * image filtering operations are not supported for ColorModels and * images based on the latter three types. * The transfer type for a particular <code>ColorModel</code> object is * specified when the object is created, either explicitly or by default. * All subclasses of <code>ColorModel</code> must specify what the * possible transfer types are and how the number of elements in the * primitive arrays representing pixels is determined. * <p> * For <code>BufferedImages</code>, the transfer type of its * <code>Raster</code> and of the <code>Raster</code> object's * <code>SampleModel</code> (available from the * <code>getTransferType</code> methods of these classes) must match that * of the <code>ColorModel</code>. The number of elements in an array * representing a pixel for the <code>Raster</code> and * <code>SampleModel</code> (available from the * <code>getNumDataElements</code> methods of these classes) must match * that of the <code>ColorModel</code>. * <p> * The algorithm used to convert from pixel values to color and alpha * components varies by subclass. For example, there is not necessarily * a one-to-one correspondence between samples obtained from the * <code>SampleModel</code> of a <code>BufferedImage</code> object's * <code>Raster</code> and color/alpha components. Even when * there is such a correspondence, the number of bits in a sample is not * necessarily the same as the number of bits in the corresponding color/alpha * component. Each subclass must specify how the translation from * pixel values to color/alpha components is done. * <p> * Methods in the <code>ColorModel</code> class use two different * representations of color and alpha components - a normalized form * and an unnormalized form. In the normalized form, each component is a * <code>float</code> value between some minimum and maximum values. For * the alpha component, the minimum is 0.0 and the maximum is 1.0. For * color components the minimum and maximum values for each component can * be obtained from the <code>ColorSpace</code> object. These values * will often be 0.0 and 1.0 (e.g. normalized component values for the * default sRGB color space range from 0.0 to 1.0), but some color spaces * have component values with different upper and lower limits. These * limits can be obtained using the <code>getMinValue</code> and * <code>getMaxValue</code> methods of the <code>ColorSpace</code> * class. Normalized color component values are not premultiplied. * All <code>ColorModels</code> must support the normalized form. * <p> * In the unnormalized * form, each component is an unsigned integral value between 0 and * 2<sup>n</sup> - 1, where n is the number of significant bits for a * particular component. If pixel values for a particular * <code>ColorModel</code> represent color samples premultiplied by * the alpha sample, unnormalized color component values are * also premultiplied. The unnormalized form is used only with instances * of <code>ColorModel</code> whose <code>ColorSpace</code> has minimum * component values of 0.0 for all components and maximum values of * 1.0 for all components. * The unnormalized form for color and alpha components can be a convenient * representation for <code>ColorModels</code> whose normalized component * values all lie * between 0.0 and 1.0. In such cases the integral value 0 maps to 0.0 and * the value 2<sup>n</sup> - 1 maps to 1.0. In other cases, such as * when the normalized component values can be either negative or positive, * the unnormalized form is not convenient. Such <code>ColorModel</code> * objects throw an {@link IllegalArgumentException} when methods involving * an unnormalized argument are called. Subclasses of <code>ColorModel</code> * must specify the conditions under which this occurs. * * @see IndexColorModel * @see ComponentColorModel * @see PackedColorModel * @see DirectColorModel * @see java.awt.Image * @see BufferedImage * @see RenderedImage * @see java.awt.color.ColorSpace * @see SampleModel * @see Raster * @see DataBuffer * @version 10 Feb 1997 */public abstract class ColorModel implements Transparency{ private long pData; // Placeholder for data for native functions /** * The total number of bits in the pixel. */ protected int pixel_bits; int nBits[]; int transparency = Transparency.TRANSLUCENT; boolean supportsAlpha = true; boolean isAlphaPremultiplied = false; int numComponents = -1; int numColorComponents = -1; ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB); int colorSpaceType = ColorSpace.TYPE_RGB; int maxBits; boolean is_sRGB = true; /** * Data type of the array used to represent pixel values. */ protected int transferType; /** * This is copied from java.awt.Toolkit since we need the library * loaded in java.awt.image also: * * WARNING: This is a temporary workaround for a problem in the * way the AWT loads native libraries. A number of classes in the * AWT package have a native method, initIDs(), which initializes * the JNI field and method ids used in the native portion of * their implementation. * * Since the use and storage of these ids is done by the * implementation libraries, the implementation of these method is * provided by the particular AWT implementations (for example, * "Toolkit"s/Peer), such as Motif, Microsoft Windows, or Tiny. The * problem is that this means that the native libraries must be * loaded by the java.* classes, which do not necessarily know the * names of the libraries to load. A better way of doing this * would be to provide a separate library which defines java.awt.* * initIDs, and exports the relevant symbols out to the * implementation libraries. * * For now, we know it's done by the implementation, and we assume * that the name of the library is "awt". -br. */ private static boolean loaded = false; static void loadLibraries() { if (!loaded) { java.security.AccessController.doPrivileged( new sun.security.action.LoadLibraryAction("awt")); loaded = true; } } private static native void initIDs(); static { /* ensure that the proper libraries are loaded */ loadLibraries(); initIDs(); } private static ColorModel RGBdefault; /** * Returns a <code>DirectColorModel</code> 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. * The color space is the default {@link ColorSpace}, sRGB. * The format for the RGB values is an integer with 8 bits * each of alpha, red, green, and blue color components ordered * correspondingly from the most significant byte to the least * significant byte, as in: 0xAARRGGBB. Color components are * not premultiplied by the alpha component. This format does not * necessarily represent the native or the most efficient * <code>ColorModel</code> for a particular device or for all images. * It is merely used as a common color model format. * @return a <code>DirectColorModel</code>object describing default * RGB values. */ public static ColorModel getRGBdefault() { if (RGBdefault == null) { RGBdefault = new DirectColorModel(32, 0x00ff0000, // Red 0x0000ff00, // Green 0x000000ff, // Blue 0xff000000 // Alpha ); } return RGBdefault; } /** * Constructs a <code>ColorModel</code> that translates pixels of the * specified number of bits to color/alpha components. The color * space is the default RGB <code>ColorSpace</code>, which is sRGB. * Pixel values are assumed to include alpha information. If color * and alpha information are represented in the pixel value as * separate spatial bands, the color bands are assumed not to be * premultiplied with the alpha value. The transparency type is * java.awt.Transparency.TRANSLUCENT. The transfer type will be the * smallest of DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT, * or DataBuffer.TYPE_INT that can hold a single pixel * (or DataBuffer.TYPE_UNDEFINED if bits is greater * than 32). Since this constructor has no information about the * number of bits per color and alpha component, any subclass calling * this constructor should override any method that requires this * information. * @param bits the number of bits of a pixel * @throws IllegalArgumentException if the number * of bits in <code>bits</code> is less than 1 */ public ColorModel(int bits) { pixel_bits = bits; if (bits < 1) { throw new IllegalArgumentException("Number of bits must be > 0"); } numComponents = 4; numColorComponents = 3; maxBits = bits; // REMIND: make sure transferType is set correctly transferType = ColorModel.getDefaultTransferType(bits); } /** * Constructs a <code>ColorModel</code> that translates pixel values * to color/alpha components. Color components will be in the * specified <code>ColorSpace</code>. <code>pixel_bits</code> is the * number of bits in the pixel values. The bits array * specifies the number of significant bits per color and alpha component. * Its length should be the number of components in the * <code>ColorSpace</code> if there is no alpha information in the * pixel values, or one more than this number if there is alpha * information. <code>hasAlpha</code> indicates whether or not alpha * information is present. The <code>boolean</code> * <code>isAlphaPremultiplied</code> specifies how to interpret pixel * values in which color and alpha information are represented as
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -