📄 componentcolormodel.java
字号:
if (needAlpha) { alp = usdata[numColorComponents] & 0xffff; } break; case DataBuffer.TYPE_INT: int idata[] = (int[])inData; comp = idata[idx]; if (needAlpha) { alp = idata[numColorComponents]; } break; default: throw new UnsupportedOperationException("This method has not "+ "been implemented for transferType " + transferType); } if (needAlpha) { if (alp != 0) { float scalefactor = (float) ((1 << precision) - 1); float fcomp = ((float) comp) / ((float) ((1<<nBits[idx]) - 1)); float invalp = ((float) ((1<<nBits[numColorComponents]) - 1)) / ((float) alp); return (int) (fcomp * invalp * scalefactor + 0.5f); } else { return 0; } } else { if (nBits[idx] != precision) { float scalefactor = (float) ((1 << precision) - 1); float fcomp = ((float) comp) / ((float) ((1<<nBits[idx]) - 1)); return (int) (fcomp * scalefactor + 0.5f); } return comp; } } private int getRGBComponent(Object inData, int idx) { if (needScaleInit) { initScale(); } if (is_sRGB_stdScale) { return extractComponent(inData, idx, 8); } else if (is_LinearRGB_stdScale) { int lutidx = extractComponent(inData, idx, 16); return tosRGB8LUT[lutidx] & 0xff; } else if (is_ICCGray_stdScale) { int lutidx = extractComponent(inData, 0, 16); return tosRGB8LUT[lutidx] & 0xff; } // Not CS_sRGB, CS_LINEAR_RGB, or any TYPE_GRAY ICC_ColorSpace float[] norm = getNormalizedComponents(inData, null, 0); // Note that getNormalizedComponents returns non-premultiplied values 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 <CODE>pixel</CODE> value is specified by an array * of data elements of type <CODE>transferType</CODE> passed in as an object * reference. 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). Since * <code>ComponentColorModel</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 inData The pixel from which you want to get the red color component, * specified by an array of data elements of type <CODE>transferType</CODE>. * * @return The red color component for the specified pixel, as an int. * * @throws ClassCastException If <CODE>inData</CODE> is not a primitive array * of type <CODE>transferType</CODE>. * @throws ArrayIndexOutOfBoundsException if <CODE>inData</CODE> is not * large enough to hold a pixel value for this * <CODE>ColorModel</CODE>. * @throws UnsupportedOperationException If the transfer type of * this <CODE>ComponentColorModel</CODE> * is not one of the supported transfer types: * <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>, * <CODE>DataBuffer.TYPE_INT</CODE>, <CODE>DataBuffer.TYPE_SHORT</CODE>, * <CODE>DataBuffer.TYPE_FLOAT</CODE>, or <CODE>DataBuffer.TYPE_DOUBLE</CODE>. */ public int getRed(Object inData) { return getRGBComponent(inData, 0); } /** * Returns the green color component for the specified pixel, scaled * from 0 to 255 in the default RGB <CODE>ColorSpace</CODE>, sRGB. * A color conversion is done if necessary. The <CODE>pixel</CODE> value * is specified by an array of data elements of type <CODE>transferType</CODE> * passed in as an object reference. The returned value is 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). * Since <code>ComponentColorModel</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 inData The pixel from which you want to get the green color component, * specified by an array of data elements of type <CODE>transferType</CODE>. * * @return The green color component for the specified pixel, as an int. * * @throws ClassCastException If <CODE>inData</CODE> is not a primitive array * of type <CODE>transferType</CODE>. * @throws ArrayIndexOutOfBoundsException if <CODE>inData</CODE> is not * large enough to hold a pixel value for this * <CODE>ColorModel</CODE>. * @throws UnsupportedOperationException If the transfer type of * this <CODE>ComponentColorModel</CODE> * is not one of the supported transfer types: * <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>, * <CODE>DataBuffer.TYPE_INT</CODE>, <CODE>DataBuffer.TYPE_SHORT</CODE>, * <CODE>DataBuffer.TYPE_FLOAT</CODE>, or <CODE>DataBuffer.TYPE_DOUBLE</CODE>. */ public int getGreen(Object inData) { return getRGBComponent(inData, 1); } /** * Returns the blue color component for the specified pixel, scaled * from 0 to 255 in the default RGB <CODE>ColorSpace</CODE>, sRGB. * A color conversion is done if necessary. The <CODE>pixel</CODE> value is * specified by an array of data elements of type <CODE>transferType</CODE> * passed in as an object reference. The returned value is 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). * Since <code>ComponentColorModel</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 inData The pixel from which you want to get the blue color component, * specified by an array of data elements of type <CODE>transferType</CODE>. * * @return The blue color component for the specified pixel, as an int. * * @throws ClassCastException If <CODE>inData</CODE> is not a primitive array * of type <CODE>transferType</CODE>. * @throws ArrayIndexOutOfBoundsException if <CODE>inData</CODE> is not * large enough to hold a pixel value for this * <CODE>ColorModel</CODE>. * @throws UnsupportedOperationException If the transfer type of * this <CODE>ComponentColorModel</CODE> * is not one of the supported transfer types: * <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>, * <CODE>DataBuffer.TYPE_INT</CODE>, <CODE>DataBuffer.TYPE_SHORT</CODE>, * <CODE>DataBuffer.TYPE_FLOAT</CODE>, or <CODE>DataBuffer.TYPE_DOUBLE</CODE>. */ public int getBlue(Object inData) { return getRGBComponent(inData, 2); } /** * Returns the alpha component for the specified pixel, scaled from * 0 to 255. The pixel value is specified by an array of data * elements of type <CODE>transferType</CODE> passed in as an * object reference. Since <code>ComponentColorModel</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 inData The pixel from which you want to get the alpha component, * specified by an array of data elements of type <CODE>transferType</CODE>. * * @return The alpha component for the specified pixel, as an int. * * @throws ClassCastException If <CODE>inData</CODE> is not a primitive array * of type <CODE>transferType</CODE>. * @throws ArrayIndexOutOfBoundsException if <CODE>inData</CODE> is not * large enough to hold a pixel value for this * <CODE>ColorModel</CODE>. * @throws UnsupportedOperationException If the transfer type of * this <CODE>ComponentColorModel</CODE> * is not one of the supported transfer types: * <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>, * <CODE>DataBuffer.TYPE_INT</CODE>, <CODE>DataBuffer.TYPE_SHORT</CODE>, * <CODE>DataBuffer.TYPE_FLOAT</CODE>, or <CODE>DataBuffer.TYPE_DOUBLE</CODE>. */ public int getAlpha(Object inData) { if (supportsAlpha == false) { return 255; } int alpha = 0; int aIdx = numColorComponents; switch (transferType) { case DataBuffer.TYPE_SHORT: short sdata[] = (short[])inData; alpha = (int) ((sdata[aIdx] / 32767.0f) * 255.0f + 0.5f); return alpha; case DataBuffer.TYPE_FLOAT: float fdata[] = (float[])inData; alpha = (int) (fdata[aIdx] * 255.0f + 0.5f); return alpha; case DataBuffer.TYPE_DOUBLE: double ddata[] = (double[])inData; alpha = (int) (ddata[aIdx] * 255.0 + 0.5); return alpha; case DataBuffer.TYPE_BYTE: byte bdata[] = (byte[])inData; alpha = bdata[aIdx] & 0xff; break; case DataBuffer.TYPE_USHORT: short usdata[] = (short[])inData; alpha = usdata[aIdx]&0xffff; break; case DataBuffer.TYPE_INT: int idata[] = (int[])inData; alpha = idata[aIdx]; break; default: throw new UnsupportedOperationException("This method has not "+ "been implemented for transferType " + transferType); } if (nBits[aIdx] == 8) { return alpha; } else { return (int) ((((float) alpha) / ((float) ((1 << nBits[aIdx]) - 1))) * 255.0f + 0.5f); } } /** * Returns the color/alpha components for the specified pixel in the * default RGB color model format. A color conversion is done if * necessary. The pixel value is specified by an * array of data elements of type <CODE>transferType</CODE> passed * in as an object reference. * The returned value is 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). * Since <code>ComponentColorModel</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 inData The pixel from which you want to get the color/alpha components, * specified by an array of data elements of type <CODE>transferType</CODE>. * * @return The color/alpha components for the specified pixel, as an int. * * @throws ClassCastException If <CODE>inData</CODE> is not a primitive array * of type <CODE>transferType</CODE>. * @throws ArrayIndexOutOfBoundsException if <CODE>inData</CODE> is not * large enough to hold a pixel value for this * <CODE>ColorModel</CODE>. * @throws UnsupportedOperationException If the transfer type of * this <CODE>ComponentColorModel</CODE> * is not one of the supported transfer types: * <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>, * <CODE>DataBuffer.TYPE_INT</CODE>, <CODE>DataBuffer.TYPE_SHORT</CODE>, * <CODE>DataBuffer.TYPE_FLOAT</CODE>, or <CODE>DataBuffer.TYPE_DOUBLE</CODE>. * @see ColorModel#getRGBdefault */ public int getRGB(Object inData) { if (needScaleInit) { initScale(); } if (is_sRGB_stdScale || is_LinearRGB_stdScale) { return (getAlpha(inData) << 24) | (getRed(inData) << 16) | (getGreen(inData) << 8) | (getBlue(inData)); } else if (colorSpaceType == ColorSpace.TYPE_GRAY) { int gray = getRed(inData); // Red sRGB component should equal // green and blue components
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -