📄 packedcolormodel.java
字号:
* <code>int</code> pixel * representation contain the color or alpha samples. */ final public int[] getMasks() { return (int[]) maskArray.clone(); } /* * A utility function to compute the mask offset and scalefactor, * store these and the mask in instance arrays, and verify that * the mask fits in the specified pixel size. */ private void DecomposeMask(int mask, int idx, String componentName) { int off = 0; int count = nBits[idx]; // Store the mask maskArray[idx] = mask; // Now find the shift if (mask != 0) { while ((mask & 1) == 0) { mask >>>= 1; off++; } } if (off + count > pixel_bits) { throw new IllegalArgumentException(componentName + " mask "+ Integer.toHexString(maskArray[idx])+ " overflows pixel (expecting "+ pixel_bits+" bits"); } maskOffsets[idx] = off; if (count == 0) { // High enough to scale any 0-ff value down to 0.0, but not // high enough to get Infinity when scaling back to pixel bits scaleFactors[idx] = 256.0f; } else { scaleFactors[idx] = 255.0f / ((1 << count) - 1); } } /** * 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 (in pixels) of the region of the image data * described * @param h the height (in pixels) of the region of the image data * described * @return the newly created <code>SampleModel</code>. * @throws IllegalArgumentException if <code>w</code> or * <code>h</code> is not greater than 0 * @see SampleModel */ public SampleModel createCompatibleSampleModel(int w, int h) { return new SinglePixelPackedSampleModel(transferType, w, h, maskArray); } /** * 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) { if (! (sm instanceof SinglePixelPackedSampleModel)) { return false; } // Must have the same number of components if (numComponents != sm.getNumBands()) { return false; } // Transfer type must be the same if (sm.getTransferType() != transferType) { return false; } SinglePixelPackedSampleModel sppsm = (SinglePixelPackedSampleModel) sm; // Now compare the specific masks int[] bitMasks = sppsm.getBitMasks(); if (bitMasks.length != maskArray.length) { return false; } for (int i=0; i < bitMasks.length; i++) { if (bitMasks[i] != maskArray[i]) { return false; } } return true; } /** * Returns a {@link WritableRaster} representing the alpha channel of * an image, extracted from the input <code>WritableRaster</code>. * This method assumes that <code>WritableRaster</code> objects * associated with this <code>ColorModel</code> store the alpha band, * if present, as the last band of image data. Returns <code>null</code> * if there is no separate spatial alpha channel associated with this * <code>ColorModel</code>. This method creates a new * <code>WritableRaster</code>, but shares the data array. * @param raster a <code>WritableRaster</code> containing an image * @return a <code>WritableRaster</code> that represents the alpha * channel of the image contained in <code>raster</code>. */ public WritableRaster getAlphaRaster(WritableRaster raster) { if (hasAlpha() == false) { return null; } int x = raster.getMinX(); int y = raster.getMinY(); int[] band = new int[1]; band[0] = raster.getNumBands() - 1; return raster.createWritableChild(x, y, raster.getWidth(), raster.getHeight(), x, y, band); } /** * Tests if the specified <code>Object</code> is an instance * of <code>PackedColorModel</code> and equals this * <code>PackedColorModel</code>. * @param obj the <code>Object</code> to test for equality * @return <code>true</code> if the specified <code>Object</code> * is an instance of <code>PackedColorModel</code> and equals this * <code>PackedColorModel</code>; <code>false</code> otherwise. */ public boolean equals(Object obj) { if (!(obj instanceof PackedColorModel)) { return false; } if (!super.equals(obj)) { return false; } PackedColorModel cm = (PackedColorModel) obj; int numC = cm.getNumComponents(); if (numC != numComponents) { return false; } for(int i=0; i < numC; i++) { if (maskArray[i] != cm.getMask(i)) { return false; } } return true; } private final static int[] createBitsArray(int[]colorMaskArray, int alphaMask) { int numColors = colorMaskArray.length; int numAlpha = (alphaMask == 0 ? 0 : 1); int[] arr = new int[numColors+numAlpha]; for (int i=0; i < numColors; i++) { arr[i] = countBits(colorMaskArray[i]); if (arr[i] < 0) { throw new IllegalArgumentException("Noncontiguous color mask (" + Integer.toHexString(colorMaskArray[i])+ "at index "+i); } } if (alphaMask != 0) { arr[numColors] = countBits(alphaMask); if (arr[numColors] < 0) { throw new IllegalArgumentException("Noncontiguous alpha mask (" + Integer.toHexString(alphaMask)); } } return arr; } private final static int[] createBitsArray(int rmask, int gmask, int bmask, int amask) { int[] arr = new int[3 + (amask == 0 ? 0 : 1)]; arr[0] = countBits(rmask); arr[1] = countBits(gmask); arr[2] = countBits(bmask); if (arr[0] < 0) { throw new IllegalArgumentException("Noncontiguous red mask (" + Integer.toHexString(rmask)); } else if (arr[1] < 0) { throw new IllegalArgumentException("Noncontiguous green mask (" + Integer.toHexString(gmask)); } else if (arr[2] < 0) { throw new IllegalArgumentException("Noncontiguous blue mask (" + Integer.toHexString(bmask)); } if (amask != 0) { arr[3] = countBits(amask); if (arr[3] < 0) { throw new IllegalArgumentException("Noncontiguous alpha mask (" + Integer.toHexString(amask)); } } return arr; } private final static int countBits(int mask) { int count = 0; if (mask != 0) { while ((mask & 1) == 0) { mask >>>= 1; } while ((mask & 1) == 1) { mask >>>= 1; count++; } } if (mask != 0) { return -1; } return count; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -