📄 indexcolormodel.java
字号:
nArray(8, 4), // bits for each channel ColorSpace.getInstance(ColorSpace.CS_sRGB), // sRGB true, // has alpha false, // not premultiplied TRANSLUCENT, transferType); if (transferType != DataBuffer.TYPE_BYTE && transferType != DataBuffer.TYPE_USHORT) throw new IllegalArgumentException(); if (bits > 16) throw new IllegalArgumentException("bits > 16"); if (size < 1) throw new IllegalArgumentException("size < 1"); map_size = size; opaque = !hasAlpha; if (0 <= trans && trans < size) this.trans = trans; rgb = new int[size]; if (!hasAlpha) for (int i = 0; i < size; i++) rgb[i] = cmap[i + start] | 0xff000000; else System.arraycopy(cmap, start, rgb, 0, size); // Generate a bigint with 1's for every pixel validBits = validBits.setBit(size).subtract(BigInteger.ONE); } /** * Construct an IndexColorModel using a colormap with holes. * <br><br> * The IndexColorModel is built from the array of ints defining the * colormap. Each element contains red, green, blue, and alpha * components. The ColorSpace is sRGB. The transparency value is * automatically determined. * <br><br> * This constructor permits indicating which colormap entries are valid, * using the validBits argument. Each entry in cmap is valid if the * corresponding bit in validBits is set. * * @param bits the number of bits needed to represent <code>size</code> * colors. * @param size the number of colors in the color map. * @param cmap packed color components. * @param start the offset of the first color component in <code>cmap</code>. * @param transferType {@link DataBuffer#TYPE_BYTE} or * {@link DataBuffer#TYPE_USHORT}. * @param validBits a map of the valid entries in <code>cmap</code>. * @throws IllegalArgumentException if bits < 1, bits > 16, or size * < 1. * @throws IllegalArgumentException if transferType is something other than * {@link DataBuffer#TYPE_BYTE} or {@link DataBuffer#TYPE_USHORT}. */ public IndexColorModel(int bits, int size, int[] cmap, int start, int transferType, BigInteger validBits) { super(bits, // total bits, sRGB, four channels nArray(8, 4), // bits for each channel ColorSpace.getInstance(ColorSpace.CS_sRGB), // sRGB true, // has alpha false, // not premultiplied TRANSLUCENT, transferType); if (transferType != DataBuffer.TYPE_BYTE && transferType != DataBuffer.TYPE_USHORT) throw new IllegalArgumentException(); if (bits > 16) throw new IllegalArgumentException("bits > 16"); if (size < 1) throw new IllegalArgumentException("size < 1"); map_size = size; opaque = false; this.trans = -1; this.validBits = validBits; rgb = new int[size]; if (!hasAlpha) for (int i = 0; i < size; i++) rgb[i] = cmap[i + start] | 0xff000000; else System.arraycopy(cmap, start, rgb, 0, size); } /** * Returns the size of the color lookup table. * * @return The size of the color lookup table. */ public final int getMapSize() { return map_size; } /** * Get the index of the transparent color in this color model. * * @return The index of the color that is considered transparent, or -1 if * there is no transparent color. */ public final int getTransparentPixel() { return trans; } /** * Fills the supplied array with the red component of each color in the * lookup table. * * @param r an array that is at least as large as {@link #getMapSize()}. * @throws NullPointerException if <code>r</code> is <code>null</code>. * @throws ArrayIndexOutOfBoundsException if <code>r</code> has less * than {@link #getMapSize()} elements. */ public final void getReds(byte[] r) { int i; for (i = 0; i < map_size; i++) r[i] = (byte) ((0x00FF0000 & rgb[i]) >> 16); } /** * Fills the supplied array with the green component of each color in the * lookup table. * * @param g an array that is at least as large as {@link #getMapSize()}. * @throws NullPointerException if <code>g</code> is <code>null</code>. * @throws ArrayIndexOutOfBoundsException if <code>g</code> has less * than {@link #getMapSize()} elements. */ public final void getGreens(byte[] g) { int i; for (i = 0; i < map_size; i++) g[i] = (byte) ((0x0000FF00 & rgb[i]) >> 8); } /** * Fills the supplied array with the blue component of each color in the * lookup table. * * @param b an array that is at least as large as {@link #getMapSize()}. * @throws NullPointerException if <code>b</code> is <code>null</code>. * @throws ArrayIndexOutOfBoundsException if <code>b</code> has less * than {@link #getMapSize()} elements. */ public final void getBlues(byte[] b) { int i; for (i = 0; i < map_size; i++) b[i] = (byte) (0x000000FF & rgb[i]); } /** * Fills the supplied array with the alpha component of each color in the * lookup table. If the model has a transparent pixel specified, the alpha * for that pixel will be 0. * * @param a an array that is at least as large as {@link #getMapSize()}. * @throws NullPointerException if <code>a</code> is <code>null</code>. * @throws ArrayIndexOutOfBoundsException if <code>a</code> has less * than {@link #getMapSize()} elements. */ public final void getAlphas(byte[] a) { int i; for (i = 0; i < map_size; i++) if (i == trans) a[i] = (byte) 0; else a[i] = (byte) ((0xFF000000 & rgb[i]) >> 24); } /** * Returns the red component of the color in the lookup table for the * given pixel value. * * @param pixel the pixel lookup value. * * @return The red component of the color in the lookup table. * @throws ArrayIndexOutOfBoundsException if <code>pixel</code> is negative. */ public final int getRed(int pixel) { if (pixel < map_size) return (0x00FF0000 & rgb[pixel]) >> 16; return 0; } /** * Returns the green component of the color in the lookup table for the * given pixel value. * * @param pixel the pixel lookup value. * * @return The green component of the color in the lookup table. * @throws ArrayIndexOutOfBoundsException if <code>pixel</code> is negative. */ public final int getGreen(int pixel) { if (pixel < map_size) return (0x0000FF00 & rgb[pixel]) >> 8; return 0; } /** * Returns the blue component of the color in the lookup table for the * given pixel value. * * @param pixel the pixel lookup value. * * @return The blue component of the color in the lookup table. * @throws ArrayIndexOutOfBoundsException if <code>pixel</code> is negative. */ public final int getBlue(int pixel) { if (pixel < map_size) return 0x000000FF & rgb[pixel]; return 0; } /** * Returns the alpha component of the color in the lookup table for the * given pixel value. If no alpha channel was specified when the color model * was created, then 255 is returned for all pixels except the transparent * pixel (if one is defined - see {@link #getTransparentPixel()}) which * returns an alpha of 0. * * @param pixel the pixel lookup value. * * @return The alpha component of the color in the lookup table (in the * range 0 to 255). * @throws ArrayIndexOutOfBoundsException if <code>pixel</code> is negative. */ public final int getAlpha(int pixel) { if (opaque && pixel != trans) return 255; if ((pixel == trans && trans != -1) || pixel >= map_size) return 0; return (0xFF000000 & rgb[pixel]) >> 24; } /** * Get the RGB color value of the given pixel using the default * RGB color model. * * @param pixel the pixel lookup value. * @return The RGB color value. * @throws ArrayIndexOutOfBoundsException if <code>pixel</code> is negative. */ public final int getRGB(int pixel) { if (pixel >= 0 && pixel < map_size) return rgb[pixel]; return 0; } /** * Get the RGB color values of all pixels in the map using the default * RGB color model. * * @param rgb The destination array. */ public final void getRGBs(int[] rgb) { System.arraycopy(this.rgb, 0, rgb, 0, map_size); } /** * Return <code>true</code> if the lookup table contains valid data for * <code>pixel</code>, and <code>false</code> otherwise. * * @param pixel the pixel value used to index the color lookup table. * @return <code>true</code> if <code>pixel</code> is valid, * <code>false</code> otherwise. */ public boolean isValid(int pixel) { if (pixel >= 0) return validBits.testBit(pixel); return false; } /** * Return <code>true</code> if all pixels are valid, <code>false</code> * otherwise. * * @return <code>true</code> if all pixels are valid, <code>false</code> * otherwise. */ public boolean isValid() { // Generate a bigint with 1's for every pixel BigInteger allbits = new BigInteger("0"); allbits = allbits.setBit(map_size); allbits = allbits.subtract(new BigInteger("1")); return allbits.equals(validBits); } /** * Returns a binary value ({@link BigInteger}) where each bit represents an * entry in the color lookup table. If the bit is on, the entry is valid. * * @return The binary value. */ public BigInteger getValidPixels() { return validBits; } /** * Construct a {@link BufferedImage} with rgb pixel values from a * {@link Raster}. * * Constructs a new BufferedImage in which each pixel is an RGBA int from * a Raster with index-valued pixels. If this model has no alpha component * or transparent pixel, the type of the new BufferedImage is TYPE_INT_RGB. * Otherwise the type is TYPE_INT_ARGB. If forceARGB is true, the type is * forced to be TYPE_INT_ARGB no matter what. * * @param raster The source of pixel values. * @param forceARGB True if type must be TYPE_INT_ARGB. * @return New BufferedImage with RBGA int pixel values. */ public BufferedImage convertToIntDiscrete(Raster raster, boolean forceARGB) { int type = forceARGB ? BufferedImage.TYPE_INT_ARGB : ((opaque && trans == -1) ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB); // FIXME: assuming that raster has only 1 band since pixels are supposed // to be int indexes. // FIXME: it would likely be more efficient to fetch a complete array, // but it would take much more memory. // FIXME: I'm not sure if transparent pixels or alpha values need special // handling here. BufferedImage im = new BufferedImage(raster.width, raster.height, type); for (int x = raster.minX; x < raster.width + raster.minX; x++) for (int y = raster.minY; y < raster.height + raster.minY; y++) im.setRGB(x, y, rgb[raster.getSample(x, y, 0)]); return im; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -