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

📄 directcolormodel.java

📁 JAVA基本类源代码,大家可以学习学习!
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
            // overflows nBits, so we need to clamp.            if (red > ((1<<nBits[0]) - 1)) {                red = (1<<nBits[0]) - 1;            }            if (grn > ((1<<nBits[1]) - 1)) {                grn = (1<<nBits[1]) - 1;            }            if (blu > ((1<<nBits[2]) - 1)) {                blu = (1<<nBits[2]) - 1;            }        }        intpixel[0] |= (red << maskOffsets[0]) |                       (grn << maskOffsets[1]) |                       (blu << maskOffsets[2]);        switch (transferType) {            case DataBuffer.TYPE_BYTE: {               byte bdata[];               if (pixel == null) {                   bdata = new byte[1];               } else {                   bdata = (byte[])pixel;               }               bdata[0] = (byte)(0xff&intpixel[0]);               return bdata;            }            case DataBuffer.TYPE_USHORT:{               short sdata[];               if (pixel == null) {                   sdata = new short[1];               } else {                   sdata = (short[])pixel;               }               sdata[0] = (short)(intpixel[0]&0xffff);               return sdata;            }            case DataBuffer.TYPE_INT:               return intpixel;        }        throw new UnsupportedOperationException("This method has not been "+                 "implemented for transferType " + transferType);    }    /**     * Returns an array of unnormalized color/alpha components given a pixel     * in this <code>ColorModel</code>.  The pixel value is specified as an      * <code>int</code>.  If the <code>components</code> array is      * <code>null</code>, a new array is allocated.  The     * <code>components</code> array is returned.  Color/alpha components are     * stored in the <code>components</code> array starting at      * <code>offset</code>, even if the array is allocated by this method.       * An <code>ArrayIndexOutOfBoundsException</code> is thrown if the      * <code>components</code> array is not <code>null</code> and is not large     * enough to hold all the color and alpha components, starting at      * <code>offset</code>.     * @param pixel the specified pixel     * @param components the array to receive the color and alpha     * components of the specified pixel     * @param offset the offset into the <code>components</code> array at     * which to start storing the color and alpha components     * @return an array containing the color and alpha components of the     * specified pixel starting at the specified offset.     */    final public int[] getComponents(int pixel, int[] components, int offset) {        if (components == null) {            components = new int[offset+numComponents];        }        for (int i=0; i < numComponents; i++) {            components[offset+i] = (pixel & maskArray[i]) >>> maskOffsets[i];        }        return components;    }        /**     * Returns an array of unnormalized color/alpha components given a pixel     * in this <code>ColorModel</code>.  The pixel value is specified by an      * array of data elements of type <code>transferType</code> passed in as      * an object reference.  If <code>pixel</code> is not a primitive array     * of type <code>transferType</code>, a <code>ClassCastException</code>     * is thrown.  An <code>ArrayIndexOutOfBoundsException</code> is     * thrown if <code>pixel</code> is not large enough to hold a     * pixel value for this <code>ColorModel</code>.  If the      * <code>components</code> array is <code>null</code>, a new     * array is allocated.  The <code>components</code> array is returned.     * Color/alpha components are stored in the <code>components</code> array      * starting at <code>offset</code>, even if the array is allocated by      * this method.  An <code>ArrayIndexOutOfBoundsException</code>     * is thrown if the <code>components</code> array is not      * <code>null</code> and is not large enough to hold all the color and      * alpha components, starting at <code>offset</code>.     * Since <code>DirectColorModel</code> can be subclassed, subclasses     * inherit the implementation of this method and if they don't     * override it then they throw an exception if they use an unsupported     * <code>transferType</code>.     * @param pixel the specified pixel     * @param components the array to receive the color and alpha     *        components of the specified pixel     * @param offset the offset into the <code>components</code> array at     *        which to start storing the color and alpha components     * @return an array containing the color and alpha components of the     * specified pixel starting at the specified offset.     * @exception ClassCastException if <code>pixel</code>      *  is not a primitive array of type <code>transferType</code>     * @exception ArrayIndexOutOfBoundsException if     *  <code>pixel</code> is not large enough to hold a pixel value     *  for this <code>ColorModel</code>, or if <code>components</code>     *  is not <code>null</code> and is not large enough to hold all the     *  color and alpha components, starting at <code>offset</code>     * @exception UnsupportedOperationException if this     *            <code>transferType</code> is not supported by this     *            color model      */    final public int[] getComponents(Object pixel, int[] components,                                     int offset) {        int intpixel=0;        switch (transferType) {     	    case DataBuffer.TYPE_BYTE:               byte bdata[] = (byte[])pixel;               intpixel = bdata[0] & 0xff;            break;            case DataBuffer.TYPE_USHORT:               short sdata[] = (short[])pixel;               intpixel = sdata[0] & 0xffff;            break;            case DataBuffer.TYPE_INT:               int idata[] = (int[])pixel;               intpixel = idata[0];            break;            default:               throw new UnsupportedOperationException("This method has not been "+                   "implemented for transferType " + transferType);        }        return getComponents(intpixel, components, offset);    }        /**     * Creates a <code>WritableRaster</code> with the specified width and     * height that has a data layout (<code>SampleModel</code>) compatible      * with this <code>ColorModel</code>.       * @param w the width to apply to the new <code>WritableRaster</code>     * @param h the height to apply to the new <code>WritableRaster</code>     * @return a <code>WritableRaster</code> object with the specified     * width and height.     * @throws IllegalArgumentException if <code>w</code> or <code>h</code>     *         is less than or equal to zero       * @see WritableRaster     * @see SampleModel     */    final public WritableRaster createCompatibleWritableRaster (int w,                                                                int h) {        if ((w <= 0) || (h <= 0)) {            throw new IllegalArgumentException("Width (" + w + ") and height (" + h +                                                ") cannot be <= 0");        }        int[] bandmasks;        if (supportsAlpha) {            bandmasks = new int[4];            bandmasks[3] = alpha_mask;        }        else {            bandmasks = new int[3];        }        bandmasks[0] = red_mask;        bandmasks[1] = green_mask;        bandmasks[2] = blue_mask;                if (pixel_bits > 16) {	    return Raster.createPackedRaster(DataBuffer.TYPE_INT,                                             w,h,bandmasks,null);         }        else if (pixel_bits > 8) {	    return Raster.createPackedRaster(DataBuffer.TYPE_USHORT,                                             w,h,bandmasks,null);         }        else {	    return Raster.createPackedRaster(DataBuffer.TYPE_BYTE,                                             w,h,bandmasks,null);         }    }    /**     * Returns a pixel value represented as an <code>int</code> in this      * <code>ColorModel</code>, given an array of unnormalized color/alpha      * components.   An <code>ArrayIndexOutOfBoundsException</code> is      * thrown if the <code>components</code> array is     * not large enough to hold all the color and alpha components, starting     * at <code>offset</code>.     * @param components an array of unnormalized color and alpha     * components     * @param offset the index into <code>components</code> at which to     * begin retrieving the color and alpha components     * @return an <code>int</code> pixel value in this     * <code>ColorModel</code> corresponding to the specified components.       * @exception <code>ArrayIndexOutOfBoundsException</code> if     *  the <code>components</code> array is not large enough to      *	hold all of the color and alpha components starting at     *	<code>offset</code>      */    public int getDataElement(int[] components, int offset) {        int pixel = 0;        for (int i=0; i < numComponents; i++) {            pixel |= ((components[offset+i]<<maskOffsets[i])&maskArray[i]);        }        return pixel;    }        /**     * Returns a data element array representation of a pixel in this     * <code>ColorModel</code>, given an array of unnormalized color/alpha      * components.     * This array can then be passed to the <code>setDataElements</code>      * method of a <code>WritableRaster</code> object.     * An <code>ArrayIndexOutOfBoundsException</code> is thrown if the      * <code>components</code> array     * is not large enough to hold all the color and alpha components,     * starting at offset.  If the <code>obj</code> variable is      * <code>null</code>, a new array is allocated.  If <code>obj</code> is      * not <code>null</code>, it must be a primitive array     * of type <code>transferType</code>; otherwise, a      * <code>ClassCastException</code> is thrown.     * An <code>ArrayIndexOutOfBoundsException</code> is thrown if      * <code>obj</code> is not large enough to hold a pixel value for this      * <code>ColorModel</code>.     * Since <code>DirectColorModel</code> can be subclassed, subclasses     * inherit the implementation of this method and if they don't     * override it then they throw an exception if they use an unsupported     * <code>transferType</code>.     * @param components an array of unnormalized color and alpha     * components     * @param offset the index into <code>components</code> at which to     * begin retrieving color and alpha components     * @param obj the <code>Object</code> representing an array of color     * and alpha components     * @return an <code>Object</code> representing an array of color and     * alpha components.     * @exception <code>ClassCastException</code> if <code>obj</code>     *  is not a primitive array of type <code>transferType</code>     * @exception <code>ArrayIndexOutOfBoundsException</code> if     *  <code>obj</code> is not large enough to hold a pixel value     *  for this <code>ColorModel</code> or the <code>components</code>     *	array is not large enough to hold all of the color and alpha     *	components starting at <code>offset</code>      * @exception UnsupportedOperationException if this     *            <code>transferType</code> is not supported by this     *            color model     * @see WritableRaster#setDataElements     * @see SampleModel#setDataElements     */    public Object getDataElements(int[] components, int offset, Object obj) {        int pixel = 0;        for (int i=0; i < numComponents; i++) {            pixel |= ((components[offset+i]<<maskOffsets[i])&maskArray[i]);        }        switch (transferType) {            case DataBuffer.TYPE_BYTE:               if (obj instanceof byte[]) {                   byte bdata[] = (byte[])obj;                   bdata[0] = (byte)(pixel&0xff);                   return bdata;               } else {                   byte bdata[] = {(byte)(pixel&0xff)};                   return bdata;               }            case DataBuffer.TYPE_USHORT:               if (obj instanceof short[]) {                   short sdata[] = (short[])obj;                   sdata[0] = (short)(pixel&0xffff);                   return sdata;               } else {                   short sdata[] = {(short)(pixel&0xffff)};                   return sdata;               }            case DataBuffer.TYPE_INT:               if (obj instanceof int[]) {                   int idata[] = (int[])obj;                   idata[0] = pixel;                   return idata;

⌨️ 快捷键说明

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