colormodel.java

来自「This is a resource based on j2me embedde」· Java 代码 · 共 443 行 · 第 1/2 页

JAVA
443
字号
/* * @(#)ColorModel.java	1.38 06/10/10 * * Copyright  1990-2008 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER *  * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation.  *  * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt).  *  * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA  *  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions.  * */package java.awt.image;import java.awt.Transparency;import java.awt.color.ColorSpace;/** * A class that encapsulates the methods for translating from pixel values * to alpha, red, green, and blue color components for an image.  This * class is abstract. * * @see IndexColorModel * @see DirectColorModel * * @version	1.22 02/20/02 * @author 	Jim Graham */public abstract class ColorModel implements Transparency {    private int pData;    protected int pixel_bits;    int transparency = Transparency.TRANSLUCENT;    int nBits[];            boolean supportsAlpha = true;    boolean isAlphaPremultiplied = false;    int numComponents = -1;    int numColorComponents = -1;    ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);    /**     * Data type of the array used to represent pixel values.     */    private int transferType;            private static ColorModel RGBdefault;    /**     * Return a ColorModel which describes the default format for     * integer RGB values used throughout the AWT image interfaces.     * 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     */    public static ColorModel getRGBdefault() {        if (RGBdefault == null) {            RGBdefault = new DirectColorModel(32,                        0x00ff0000,	// Red                        0x0000ff00,	// Green                        0x000000ff,	// Blue                        0xff000000	// Alpha                    );        }        return RGBdefault;    }    /**     * Returns whether or not alpha is supported in this     * <code>ColorModel</code>.     * @return <code>true</code> if alpha is supported in this     * <code>ColorModel</code>; <code>false</code> otherwise.     */    final public boolean hasAlpha() {        return supportsAlpha;    }    /**     * Returns whether or not the alpha has been premultiplied in the     * pixel values to be translated by this <code>ColorModel</code>.     * If the boolean is <code>true</code>, this <code>ColorModel</code>     * is to be used to interpret pixel values in which color and alpha     * information are represented as separate spatial bands, and color     * samples are assumed to have been multiplied by the     * alpha sample.     * @return <code>true</code> if the alpha values are premultiplied     *		in the pixel values to be translated by this     *		<code>ColorModel</code>; <code>false</code> otherwise.     */    final public boolean isAlphaPremultiplied() {        return isAlphaPremultiplied;    }    /**     * Constructs a ColorModel which describes a pixel of the specified     * number of bits.     */    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;        // TODO: 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     * separate spatial bands.  If the <code>boolean</code>     * is <code>true</code>, color samples are assumed to have been     * multiplied by the alpha sample.  The <code>transparency</code>     * 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.  Note that the bits array contains the number of     * significant bits per color/alpha component after the translation     * from pixel values.  For example, for an     * <code>IndexColorModel</code> with <code>pixel_bits</code> equal to     * 16, the bits array might have four elements with each element set     * to 8.     * @param pixel_bits the number of bits in the pixel values     * @param bits array that specifies the number of significant bits     *		per color and alpha component     * @param cspace the specified <code>ColorSpace</code>     * @param hasAlpha <code>true</code> if alpha information is present;     *		<code>false</code> otherwise     * @param isAlphaPremultiplied <code>true</code> if color samples are     *		assumed to be premultiplied by the alpha samples;     *		<code>false</code> otherwise     * @param transparency what alpha values can be represented by this     *		color model     * @param transferType the type of the array used to represent pixel     *		values     * @throws IllegalArgumentException if the length of     *		the bit array is less than the number of color or alpha     *		components in this <code>ColorModel</code>, or if the     *          transparency is not a valid value.     * @throws IllegalArgumentException if the sum of the number     * 		of bits in <code>bits</code> is less than 1 or if     *          any of the elements in <code>bits</code> is less than 0.     * @see java.awt.Transparency     */    protected ColorModel(int pixel_bits, int[] bits, ColorSpace cspace,        boolean hasAlpha,        boolean isAlphaPremultiplied,        int transparency,        int transferType) {        colorSpace = cspace;        numColorComponents = cspace.getNumComponents();        numComponents = numColorComponents + (hasAlpha ? 1 : 0);        supportsAlpha = hasAlpha;        if (bits.length < numComponents) {            throw new IllegalArgumentException("Number of color/alpha " +                    "components should be " +                    numComponents +                    " but length of bits array is " +                    bits.length);        }        // 4186669        if (transparency < Transparency.OPAQUE ||            transparency > Transparency.TRANSLUCENT) {            throw new IllegalArgumentException("Unknown transparency: " +                    transparency);        }        if (supportsAlpha == false) {            this.isAlphaPremultiplied = false;            this.transparency = Transparency.OPAQUE;        } else {            this.isAlphaPremultiplied = isAlphaPremultiplied;            this.transparency = transparency;        }        nBits = (int[]) bits.clone();        this.pixel_bits = pixel_bits;        if (pixel_bits <= 0) {            throw new IllegalArgumentException("Number of pixel bits must " +                    "be > 0");        }        // Check for bits < 0        //maxBits = 0;        int maxBits = 0;        for (int i = 0; i < bits.length; i++) {            // bug 4304697            if (bits[i] < 0) {                throw new                    IllegalArgumentException("Number of bits must be >= 0");            }            if (maxBits < bits[i]) {                maxBits = bits[i];            }        }        // Make sure that we don't have all 0-bit components        if (maxBits == 0) {            throw new IllegalArgumentException("There must be at least " +                    "one component with > 0 " +                    "pixel bits.");        }        // Save the transfer type        this.transferType = transferType;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?