⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 indexcolormodel.java

📁 JAVA基本类源代码,大家可以学习学习!
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
     * @param trans	the index of the fully transparent pixel     * @param transferType the data type of the array used to represent     *           pixel values.  The data type must be either      *           <code>DataBuffer.TYPE_BYTE</code> or     *           <code>DataBuffer.TYPE_USHORT</code>.     * @throws IllegalArgumentException if <code>bits</code> is less     *           than 1 or greater than 16     * @throws IllegalArgumentException if <code>size</code> is less     *           than 1     * @throws IllegalArgumentException if <code>transferType</code> is not     *           one of <code>DataBuffer.TYPE_BYTE</code> or     *           <code>DataBuffer.TYPE_USHORT</code>     */    public IndexColorModel(int bits, int size,                           int cmap[], int start,			   boolean hasalpha, int trans, int transferType) {	// REMIND: This assumes the ordering: RGB[A]	super(bits, opaqueBits,              ColorSpace.getInstance(ColorSpace.CS_sRGB),              false, false, OPAQUE,              transferType);        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");        }        if ((transferType != DataBuffer.TYPE_BYTE) &&            (transferType != DataBuffer.TYPE_USHORT)) {            throw new IllegalArgumentException("transferType must be either" +                "DataBuffer.TYPE_BYTE or DataBuffer.TYPE_USHORT");        }	setRGBs(size, cmap, start, hasalpha);        setTransparentPixel(trans);    }    /**     * Constructs an <code>IndexColorModel</code> from an      * <code>int</code> array where each <code>int</code> is      * comprised of red, green, blue, and alpha                 * components in the default RGB color model format.       * 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 must be one of <code>DataBuffer.TYPE_BYTE</code>     * <code>DataBuffer.TYPE_USHORT</code>.     * The <code>BigInteger</code> object specifies the valid/invalid pixels     * in the <code>cmap</code> array.  A pixel is valid if the      * <code>BigInteger</code> value at that index is set, and is invalid     * if the <code>BigInteger</code> bit  at that index is not set.     * @param bits the number of bits each pixel occupies     * @param size the size of the color component array     * @param cmap the array of color components     * @param start the starting offset of the first color component     * @param transferType the specified data type     * @param validBits a <code>BigInteger</code> object.  If a bit is     *    set in the BigInteger, the pixel at that index is valid.     *    If a bit is not set, the pixel at that index     *    is considered invalid.  If null, all pixels are valid.     *    Only bits from 0 to the map size are considered.     * @throws IllegalArgumentException if <code>bits</code> is less     *           than 1 or greater than 16     * @throws IllegalArgumentException if <code>size</code> is less     *           than 1     * @throws IllegalArgumentException if <code>transferType</code> is not     *           one of <code>DataBuffer.TYPE_BYTE</code> or     *           <code>DataBuffer.TYPE_USHORT</code>     *         */    public IndexColorModel(int bits, int size, int cmap[], int start,                           int transferType, BigInteger validBits) {        super (bits, alphaBits,               ColorSpace.getInstance(ColorSpace.CS_sRGB),               true, false, TRANSLUCENT,               transferType);                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");        }        if ((transferType != DataBuffer.TYPE_BYTE) &&            (transferType != DataBuffer.TYPE_USHORT)) {            throw new IllegalArgumentException("transferType must be either" +                "DataBuffer.TYPE_BYTE or DataBuffer.TYPE_USHORT");        }        if (validBits != null) {            // Check to see if it is all valid            for (int i=0; i < size; i++) {                if (!validBits.testBit(i)) {                    this.validBits = validBits;                    break;                }            }        }        	setRGBs(size, cmap, start, true);    }        private void setRGBs(int size, byte r[], byte g[], byte b[], byte a[]) {        if (size < 1) {            throw new IllegalArgumentException("Map size ("+size+                                               ") must be >= 1");        }	map_size = size;	rgb = new int[calcRealMapSize(pixel_bits, size)];	int alpha = 0xff;        int transparency = OPAQUE;	boolean allgray = true;	for (int i = 0; i < size; i++) {	    int rc = r[i] & 0xff;	    int gc = g[i] & 0xff;	    int bc = b[i] & 0xff;	    allgray = allgray && (rc == gc) && (gc == bc);	    if (a != null) {		alpha = a[i] & 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) | (rc << 16) | (gc << 8) | bc;	}	this.allgrayopaque = allgray;	setTransparency(transparency);    }    private void setRGBs(int size, int cmap[], int start, boolean hasalpha) {	map_size = size;	rgb = new int[calcRealMapSize(pixel_bits, size)];	int j = start;        int transparency = OPAQUE;	boolean allgray = true;	BigInteger validBits = this.validBits;	for (int i = 0; i < size; i++, j++) {	    if (validBits != null && !validBits.testBit(i)) {		continue;	    }	    int cmaprgb = cmap[j];	    int r = (cmaprgb >> 16) & 0xff;	    int g = (cmaprgb >>  8) & 0xff;	    int b = (cmaprgb      ) & 0xff;	    allgray = allgray && (r == g) && (g == b);	    if (hasalpha) {		int alpha = cmaprgb >>> 24;		if (alpha != 0xff) {		    if (alpha == 0x00) {			if (transparency == OPAQUE) {			    transparency = BITMASK;			}			if (transparent_index < 0) {			    transparent_index = i;			}		    } else {			transparency = TRANSLUCENT;		    }		    allgray = false;		}	    } else {		cmaprgb |= 0xff000000;	    }	    rgb[i] = cmaprgb;	}	this.allgrayopaque = allgray;	setTransparency(transparency);    }    private int calcRealMapSize(int bits, int size) {	int newSize = Math.max(1 << bits, size);	return Math.max(newSize, 256);    }    private BigInteger getAllValid() {	int numbytes = (map_size+7)/8;	byte[] valid = new byte[numbytes];	java.util.Arrays.fill(valid, (byte)0xff);	valid[0] = (byte)(0xff >>> (numbytes*8 - map_size));	return new BigInteger(1, valid);    }            /**     * Returns the transparency.  Returns either OPAQUE, BITMASK,     * or TRANSLUCENT     * @return the transparency of this <code>IndexColorModel</code>      * @see Transparency#OPAQUE     * @see Transparency#BITMASK     * @see Transparency#TRANSLUCENT     */    public int getTransparency() {        return transparency;    }    /**     * Returns an array of the number of bits for each color/alpha component.     * The array contains the color components in the order red, green,     * blue, followed by the alpha component, if present.     * @return an array containing the number of bits of each color      *         and alpha component of this <code>IndexColorModel</code>     */    public int[] getComponentSize() {        if (nBits == null) {            if (supportsAlpha) {                nBits = new int[4];                nBits[3] = 8;            }            else {                nBits = new int[3];            }            nBits[0] = nBits[1] = nBits[2] = 8;        }        return nBits;    }        /**     * Returns the size of the color/alpha component arrays in this     * <code>IndexColorModel</code>.     * @return the size of the color and alpha component arrays.     */    final public int getMapSize() {        return map_size;    }    /**     * Returns the index of the transparent pixel in this      * <code>IndexColorModel</code> or -1 if there is no transparent pixel.     * @return the index of this <code>IndexColorModel</code> object's     *       transparent pixel, or -1 if there is no such pixel.     */    final public int getTransparentPixel() {	return transparent_index;    }    /**     * Copies the array of red color components into the specified array.       * Only the initial entries of the array as specified by      * {@link #getMapSize() getMapSize} are written.     * @param r the specified array into which the elements of the      *      array of red color components are copied      */    final public void getReds(byte r[]) {	for (int i = 0; i < map_size; i++) {	    r[i] = (byte) (rgb[i] >> 16);	}    }    /**     * Copies the array of green color components into the specified array.       * Only the initial entries of the array as specified by      * <code>getMapSize</code> are written.     * @param g the specified array into which the elements of the      *      array of green color components are copied      */    final public void getGreens(byte g[]) {	for (int i = 0; i < map_size; i++) {	    g[i] = (byte) (rgb[i] >> 8);	}    }    /**     * Copies the array of blue color components into the specified array.       * Only the initial entries of the array as specified by      * <code>getMapSize</code> are written.     * @param b the specified array into which the elements of the      *      array of blue color components are copied      */    final public void getBlues(byte b[]) {        for (int i = 0; i < map_size; i++) {            b[i] = (byte) rgb[i];        }    }    /**     * Copies the array of alpha transparency components into the      * specified array.  Only the initial entries of the array as specified      * by <code>getMapSize</code> are written.     * @param a the specified array into which the elements of the      *      array of alpha components are copied      */    final public void getAlphas(byte a[]) {        for (int i = 0; i < map_size; i++) {            a[i] = (byte) (rgb[i] >> 24);        }    }    /**     * Converts data for each index from the color and alpha component     * arrays to an int in the default RGB ColorModel format and copies     * the resulting 32-bit ARGB values into the specified array.  Only     * the initial entries of the array as specified by      * <code>getMapSize</code> are     * written.     * @param rgb the specified array into which the converted ARGB      *        values from this array of color and alpha components     *        are copied.     */    final public void getRGBs(int rgb[]) {        System.arraycopy(this.rgb, 0, rgb, 0, map_size);    }    private void setTransparentPixel(int trans) {	if (trans >= 0 && trans < map_size) {	    rgb[trans] &= 0x00ffffff;	    transparent_index = trans;	    allgrayopaque = false;	    if (this.transparency == OPAQUE) {		setTransparency(BITMASK);	    }	}    }    private void setTransparency(int transparency) {	if (this.transparency != transparency) {	    this.transparency = transparency;	    if (transparency == OPAQUE) {		supportsAlpha = false;		numComponents = 3;		nBits = opaqueBits;	    } else {		supportsAlpha = true;		numComponents = 4;		nBits = alphaBits;	    }	}

⌨️ 快捷键说明

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