📄 indexcolormodel.java
字号:
* is thrown if the <code>components</code> array is not large * enough to hold all of the color and alpha components starting * at <code>offset</code>. Since * <code>ColorModel</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 transferType. * @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. * @throws ArrayIndexOutOfBoundsException if * the <code>components</code> array is not large enough to * hold all of the color and alpha components starting at * <code>offset</code> * @throws UnsupportedOperationException if <code>transferType</code> * is invalid */ public int getDataElement(int[] components, int offset) { int rgb = (components[offset+0]<<16) | (components[offset+1]<<8) | (components[offset+2]); if (supportsAlpha) { rgb |= (components[offset+3]<<24); } else { rgb |= 0xff000000; } Object inData = getDataElements(rgb, null); int pixel; switch (transferType) { case DataBuffer.TYPE_BYTE: byte bdata[] = (byte[])inData; pixel = bdata[0] & 0xff; break; case DataBuffer.TYPE_USHORT: short sdata[] = (short[])inData; pixel = sdata[0]; break; case DataBuffer.TYPE_INT: int idata[] = (int[])inData; pixel = idata[0]; break; default: throw new UnsupportedOperationException("This method has not been "+ "implemented for transferType " + transferType); } 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 of the * color and alpha components starting at <code>offset</code>. * If the pixel variable is <code>null</code>, a new array * is allocated. If <code>pixel</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 pixel * is not large enough to hold a pixel value for this * <code>ColorModel</code>. * <p> * Since <code>IndexColorModel</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 pixel 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. * @throws ClassCastException if <code>pixel</code> * is not a primitive array of type <code>transferType</code> * @throws ArrayIndexOutOfBoundsException if * <code>pixel</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> * @throws UnsupportedOperationException if <code>transferType</code> * is not one of the supported transer types * @see WritableRaster#setDataElements * @see SampleModel#setDataElements */ public Object getDataElements(int[] components, int offset, Object pixel) { int rgb = (components[offset+0]<<16) | (components[offset+1]<<8) | (components[offset+2]); if (supportsAlpha) { rgb |= (components[offset+3]<<24); } else { rgb &= 0xff000000; } return getDataElements(rgb, pixel); } /** * 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>. This method * only works for color models with 16 or fewer bits per pixel. * <p> * Since <code>IndexColorModel</code> can be subclassed, any * subclass that supports greater than 16 bits per pixel must * override this method. * * @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 UnsupportedOperationException if the number of bits in a * pixel is greater than 16 * @see WritableRaster * @see SampleModel */ public WritableRaster createCompatibleWritableRaster(int w, int h) { WritableRaster raster; if (pixel_bits == 1 || pixel_bits == 2 || pixel_bits == 4) { // TYPE_BINARY raster = Raster.createPackedRaster(DataBuffer.TYPE_BYTE, w, h, 1, pixel_bits, null); } else if (pixel_bits <= 8) { raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, w,h,1,null); } else if (pixel_bits <= 16) { raster = Raster.createInterleavedRaster(DataBuffer.TYPE_USHORT, w,h,1,null); } else { throw new UnsupportedOperationException("This method is not supported "+ " for pixel bits > 16."); } return raster; } /** * Returns <code>true</code> if <code>raster</code> is compatible * with this <code>ColorModel</code> or <code>false</code> if it * is not compatible with this <code>ColorModel</code>. * @param raster the {@link Raster} object to test for compatibility * @return <code>true</code> if <code>raster</code> is compatible * with this <code>ColorModel</code>; <code>false</code> otherwise. * */ public boolean isCompatibleRaster(Raster raster) { int size = raster.getSampleModel().getSampleSize(0); return ((raster.getTransferType() == transferType) && (raster.getNumBands() == 1) && ((1 << size) >= map_size)); } /** * Creates a <code>SampleModel</code> with the specified * width and height that has a data layout compatible with * this <code>ColorModel</code>. * @param w the width to apply to the new <code>SampleModel</code> * @param h the height to apply to the new <code>SampleModel</code> * @return a <code>SampleModel</code> object with the specified * width and height. * @throws IllegalArgumentException if <code>w</code> or * <code>h</code> is not greater than 0 * @see SampleModel */ public SampleModel createCompatibleSampleModel(int w, int h) { int[] off = new int[1]; off[0] = 0; if (pixel_bits == 1 || pixel_bits == 2 || pixel_bits == 4) { return new MultiPixelPackedSampleModel(transferType, w, h, pixel_bits); } else { return new ComponentSampleModel(transferType, w, h, 1, w, off); } } /** * Checks if the specified <code>SampleModel</code> is compatible * with this <code>ColorModel</code>. If <code>sm</code> is * <code>null</code>, this method returns <code>false</code>. * @param sm the specified <code>SampleModel</code>, * or <code>null</code> * @return <code>true</code> if the specified <code>SampleModel</code> * is compatible with this <code>ColorModel</code>; <code>false</code> * otherwise. * @see SampleModel */ public boolean isCompatibleSampleModel(SampleModel sm) { // fix 4238629 if (! (sm instanceof ComponentSampleModel) && ! (sm instanceof MultiPixelPackedSampleModel) ) { return false; } // Transfer type must be the same if (sm.getTransferType() != transferType) { return false; } if (sm.getNumBands() != 1) { return false; } return true; } /** * Returns a new <code>BufferedImage</code> of TYPE_INT_ARGB or * TYPE_INT_RGB that has a <code>Raster</code> with pixel data * computed by expanding the indices in the source <code>Raster</code> * using the color/alpha component arrays of this <code>ColorModel</code>. * If <code>forceARGB</code> is <code>true</code>, a TYPE_INT_ARGB image is * returned regardless of whether or not this <code>ColorModel</code> * has an alpha component array or a transparent pixel. * @param raster the specified <code>Raster</code> * @param forceARGB if <code>true</code>, the returned * <code>BufferedImage</code> is TYPE_INT_ARGB; otherwise it is * TYPE_INT_RGB * @return a <code>BufferedImage</code> created with the specified * <code>Raster</code> * @throws IllegalArgumentException if the raster argument is not * compatible with this IndexColorModel */ public BufferedImage convertToIntDiscrete(Raster raster, boolean forceARGB) { ColorModel cm; if (!isCompatibleRaster(raster)) { throw new IllegalArgumentException("This raster is not compatible" + "with this IndexColorModel."); } if (forceARGB || transparency == TRANSLUCENT) { cm = ColorModel.getRGBdefault(); } else if (transparency == BITMASK) { cm = new DirectColorModel(25, 0xff0000, 0x00ff00, 0x0000ff, 0x1000000); } else { cm = new DirectColorModel(24, 0xff0000, 0x00ff00, 0x0000ff); } int w = raster.getWidth(); int h = raster.getHeight(); WritableRaster discreteRaster = cm.createCompatibleWritableRaster(w, h); Object obj = null; int[] data = null; int rX = raster.getMinX(); int rY = raster.getMinY(); for (int y=0; y < h; y++, rY++) { obj = raster.getDataElements(rX, rY, w, 1, obj); if (obj instanceof int[]) { data = (int[])obj; } else { data = DataBuffer.toIntArray(obj); } for (int x=0; x < w; x++) { data[x] = rgb[data[x]]; } discreteRaster.setDataElements(0, y, w, 1, data); } return new BufferedImage(cm, discreteRaster, false, null); } /** * Returns whether or not the pixel is valid. * @param pixel the specified pixel value * @return <code>true</code> if <code>pixel</code> * is valid; <code>false</code> otherwise. */ public boolean isValid(int pixel) { return ((pixel >= 0 && pixel < map_size) && (validBits == null || validBits.testBit(pixel))); } /** * Returns whether or not all of the pixels are valid. * @return <code>true</code> if all pixels are valid; * <code>false</code> otherwise. */ public boolean isValid() { return (validBits == null); } /** * Returns a <code>BigInteger</code> that indicates the valid/invalid * pixels in the colormap. A bit is valid if the * <code>BigInteger</code> value at that index is set, and is invalid * if the <code>BigInteger</code> value at that index is not set. * The only valid ranges to query in the <code>BigInteger</code> are * between 0 and the map size. * @return a <code>BigInteger</code> indicating the valid/invalid pixels. */ public BigInteger getValidPixels() { if (validBits == null) { return getAllValid(); } else { return validBits; } } /** * Disposes of system resources associated with this * <code>ColorModel</code> once this <code>ColorModel</code> is no * longer referenced. */ public void finalize() { sun.awt.image.BufImgSurfaceData.freeNativeICMData(this); } /** * Returns the <code>String</code> representation of the contents of * this <code>ColorModel</code>object. * @return a <code>String</code> representing the contents of this * <code>ColorModel</code> object. */ public String toString() { return new String("IndexColorModel: #pixelBits = "+pixel_bits + " numComponents = "+numComponents + " color space = "+colorSpace + " transparency = "+transparency + " transIndex = "+transparent_index + " has alpha = "+supportsAlpha + " isAlphaPre = "+isAlphaPremultiplied ); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -