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

📄 componentcolormodel.java

📁 JAVA基本类源代码,大家可以学习学习!
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
                lowVal = getNormalizedComponents(spixel, null, 0);                for (int i = 0; i < numColorComponents; i++) {                    spixel[i] = 32767;                }                highVal = getNormalizedComponents(spixel, null, 0);            }            break;        default:            lowVal = highVal = null;  // to keep the compiler from complaining            break;        }        nonStdScale = false;        for (int i = 0; i < numColorComponents; i++) {            if ((lowVal[i] != 0.0f) || (highVal[i] != 1.0f)) {                nonStdScale = true;                break;            }        }        if (nonStdScale) {            noUnnorm = true;            is_sRGB_stdScale = false;            is_LinearRGB_stdScale = false;            is_LinearGray_stdScale = false;            is_ICCGray_stdScale = false;            compOffset = new float[numColorComponents];            compScale = new float[numColorComponents];            for (int i = 0; i < numColorComponents; i++) {                compOffset[i] = lowVal[i];                compScale[i] = 1.0f / (highVal[i] - lowVal[i]);            }        }    }    private int getRGBComponent(int pixel, int idx) {        if (numComponents > 1) {            throw new                IllegalArgumentException("More than one component per pixel");        }        if (signed) {            throw new                IllegalArgumentException("Component value is signed");        }        if (needScaleInit) {            initScale();        }        // Since there is only 1 component, there is no alpha        // Normalize the pixel in order to convert it        Object opixel = null;        switch (transferType) {        case DataBuffer.TYPE_BYTE:            {                byte[] bpixel = { (byte) pixel };                opixel = bpixel;            }            break;        case DataBuffer.TYPE_USHORT:            {                short[] spixel = { (short) pixel };                opixel = spixel;            }            break;        case DataBuffer.TYPE_INT:            {                int[] ipixel = { pixel };                opixel = ipixel;            }            break;        }        float[] norm = getNormalizedComponents(opixel, null, 0);        float[] rgb = colorSpace.toRGB(norm);        return (int) (rgb[idx] * 255.0f + 0.5f);    }    /**     * Returns the red color component for the specified pixel, scaled     * from 0 to 255 in the default RGB ColorSpace, sRGB.  A color conversion     * is done if necessary.  The pixel value is specified as an int.     * The returned value will be a non pre-multiplied value.      * If the alpha is premultiplied, this method divides     * it out before returning the value (if the alpha value is 0,     * the red value will be 0).     *     * @param pixel The pixel from which you want to get the red color component.     *     * @return The red color component for the specified pixel, as an int.     *     * @throws IllegalArgumentException If there is more than     * one component in this <CODE>ColorModel</CODE>.      * @throws IllegalArgumentException If the component value for this     * <CODE>ColorModel</CODE> is signed     */    public int getRed(int pixel) {        return getRGBComponent(pixel, 0);    }    /**     * Returns the green color component for the specified pixel, scaled     * from 0 to 255 in the default RGB ColorSpace, sRGB.  A color conversion     * is done if necessary.  The pixel value is specified as an int.     * The returned value will be a non     * pre-multiplied value. If the alpha is premultiplied, this method     * divides it out before returning the value (if the alpha value is 0,     * the green value will be 0).     *     * @param pixel The pixel from which you want to get the green color component.     *     * @return The green color component for the specified pixel, as an int.     *     * @throws IllegalArgumentException If there is more than     * one component in this <CODE>ColorModel</CODE>.      * @throws IllegalArgumentException If the component value for this     * <CODE>ColorModel</CODE> is signed     */    public int getGreen(int pixel) {        return getRGBComponent(pixel, 1);    }    /**     * Returns the blue color component for the specified pixel, scaled     * from 0 to 255 in the default RGB ColorSpace, sRGB.  A color conversion     * is done if necessary.  The pixel value is specified as an int.     * The returned value will be a non     * pre-multiplied value. If the alpha is premultiplied, this method     * divides it out before returning the value (if the alpha value is 0,     * the blue value will be 0).     *     * @param pixel The pixel from which you want to get the blue color component.     *     * @return The blue color component for the specified pixel, as an int.     *     * @throws IllegalArgumentException If there is more than     * one component in this <CODE>ColorModel</CODE>.      * @throws IllegalArgumentException If the component value for this     * <CODE>ColorModel</CODE> is signed     */    public int getBlue(int pixel) {        return getRGBComponent(pixel, 2);    }    /**     * Returns the alpha component for the specified pixel, scaled     * from 0 to 255.   The pixel value is specified as an int.     *     * @param pixel The pixel from which you want to get the alpha component.     *     * @return The alpha component for the specified pixel, as an int.     *     * @throws IllegalArgumentException If there is more than     * one component in this <CODE>ColorModel</CODE>.      * @throws IllegalArgumentException If the component value for this     * <CODE>ColorModel</CODE> is signed     */    public int getAlpha(int pixel) {        if (supportsAlpha == false) {            return 255;        }        if (numComponents > 1) {            throw new                IllegalArgumentException("More than one component per pixel");        }        if (signed) {            throw new                IllegalArgumentException("Component value is signed");        }        return (int) ((((float) pixel) / ((1<<nBits[0])-1)) * 255.0f + 0.5f);    }    /**     * Returns the color/alpha components of the pixel in the default     * RGB color model format.  A color conversion is done if necessary.     * The returned value will be in a non pre-multiplied format. If     * the alpha is premultiplied, this method divides it out of the     * color components (if the alpha value is 0, the color values will be 0).     *     * @param pixel The pixel from which you want to get the color/alpha components.     *     * @return The color/alpha components for the specified pixel, as an int.     *     * @throws IllegalArgumentException If there is more than     * one component in this <CODE>ColorModel</CODE>.      * @throws IllegalArgumentException If the component value for this     * <CODE>ColorModel</CODE> is signed     */    public int getRGB(int pixel) {        if (numComponents > 1) {            throw new                IllegalArgumentException("More than one component per pixel");        }        if (signed) {            throw new                IllegalArgumentException("Component value is signed");        }	return (getAlpha(pixel) << 24)	    | (getRed(pixel) << 16)	    | (getGreen(pixel) << 8)	    | (getBlue(pixel) << 0);    }    private int extractComponent(Object inData, int idx, int precision) {        // Extract component idx from inData.  The precision argument        // should be either 8 or 16.  If it's 8, this method will return        // an 8-bit value.  If it's 16, this method will return a 16-bit        // value for transferTypes other than TYPE_BYTE.  For TYPE_BYTE,        // an 8-bit value will be returned.        // This method maps the input value corresponding to a        // normalized ColorSpace component value of 0.0 to 0, and the        // input value corresponding to a normalized ColorSpace        // component value of 1.0 to 2^n - 1 (where n is 8 or 16), so        // it is appropriate only for ColorSpaces with min/max component        // values of 0.0/1.0.  This will be true for sRGB, the built-in        // Linear RGB and Linear Gray spaces, and any other ICC grayscale        // spaces for which we have precomputed LUTs.        boolean needAlpha = (supportsAlpha && isAlphaPremultiplied);        int alp = 0;        int comp;        switch (transferType) {            // Note: we do no clamping of the pixel data here - we            // assume that the data is scaled properly            case DataBuffer.TYPE_SHORT: {                short sdata[] = (short[]) inData;                float scalefactor = (float) ((1 << precision) - 1);                if (needAlpha) {                    short s = sdata[numColorComponents];                    if (s != (short) 0) {                        return (int) ((((float) sdata[idx]) /                                       ((float) s)) * scalefactor + 0.5f);                    } else {                        return 0;                    }                } else {                    return (int) ((sdata[idx] / 32767.0f) * scalefactor + 0.5f);                }            }            case DataBuffer.TYPE_FLOAT: {                float fdata[] = (float[]) inData;                float scalefactor = (float) ((1 << precision) - 1);                if (needAlpha) {                    float f = fdata[numColorComponents];                    if (f != 0.0f) {                        return (int) (((fdata[idx] / f) * scalefactor) + 0.5f);                    } else {                        return 0;                    }                } else {                    return (int) (fdata[idx] * scalefactor + 0.5f);                }            }            case DataBuffer.TYPE_DOUBLE: {                double ddata[] = (double[]) inData;                double scalefactor = (double) ((1 << precision) - 1);                if (needAlpha) {                    double d = ddata[numColorComponents];                    if (d != 0.0) {                        return (int) (((ddata[idx] / d) * scalefactor) + 0.5);                    } else {                        return 0;                    }                } else {                    return (int) (ddata[idx] * scalefactor + 0.5);                }            }            case DataBuffer.TYPE_BYTE:               byte bdata[] = (byte[])inData;               comp = bdata[idx] & 0xff;               precision = 8;               if (needAlpha) {                   alp = bdata[numColorComponents] & 0xff;               }            break;            case DataBuffer.TYPE_USHORT:               short usdata[] = (short[])inData;               comp = usdata[idx]&0xffff;

⌨️ 快捷键说明

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