📄 bufferedimage.java
字号:
case TYPE_BYTE_INDEXED: { // Create a 6x6x6 color cube int[] cmap = new int[256]; int i=0; for (int r=0; r < 256; r += 51) { for (int g=0; g < 256; g += 51) { for (int b=0; b < 256; b += 51) { cmap[i++] = (r<<16)|(g<<8)|b; } } } // And populate the rest of the cmap with gray values int grayIncr = 256/(256-i); // The gray ramp will be between 18 and 252 int gray = grayIncr*3; for (; i < 256; i++) { cmap[i] = (gray<<16)|(gray<<8)|gray; gray += grayIncr; } colorModel = new IndexColorModel(8, 256, cmap, 0, false, -1, DataBuffer.TYPE_BYTE); raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height, 1, null); } break; case TYPE_USHORT_565_RGB: { colorModel = new DirectColorModel(16, DCM_565_RED_MASK, DCM_565_GRN_MASK, DCM_565_BLU_MASK ); raster = colorModel.createCompatibleWritableRaster(width, height); } break; case TYPE_USHORT_555_RGB: { colorModel = new DirectColorModel(15, DCM_555_RED_MASK, DCM_555_GRN_MASK, DCM_555_BLU_MASK ); raster = colorModel.createCompatibleWritableRaster(width, height); } break; default: throw new IllegalArgumentException ("Unknown image type " + imageType); } this.imageType = imageType; } /** * Constructs a <code>BufferedImage</code> of one of the predefined * image types: * TYPE_BYTE_BINARY or TYPE_BYTE_INDEXED. * * <p> If the image type is TYPE_BYTE_BINARY, the number of * entries in the color model is used to determine whether the * image should have 1, 2, or 4 bits per pixel. If the color model * has 1 or 2 entries, the image will have 1 bit per pixel. If it * has 3 or 4 entries, the image with have 2 bits per pixel. If * it has between 5 and 16 entries, the image will have 4 bits per * pixel. Otherwise, an IllegalArgumentException will be thrown. * * @param width width of the created image * @param height height of the created image * @param imageType type of the created image * @param cm <code>IndexColorModel</code> of the created image * @throws IllegalArgumentException if the imageType is not * TYPE_BYTE_BINARY or TYPE_BYTE_INDEXED or if the imageType is * TYPE_BYTE_BINARY and the color map has more than 16 entries. * @see #TYPE_BYTE_BINARY * @see #TYPE_BYTE_INDEXED */ public BufferedImage (int width, int height, int imageType, IndexColorModel cm) { if (cm.hasAlpha() && cm.isAlphaPremultiplied()) { throw new IllegalArgumentException("This image types do not have "+ "premultiplied alpha."); } switch(imageType) { case TYPE_BYTE_BINARY: int bits; // Will be set below int mapSize = cm.getMapSize(); if (mapSize <= 2) { bits = 1; } else if (mapSize <= 4) { bits = 2; } else if (mapSize <= 16) { bits = 4; } else { throw new IllegalArgumentException ("Color map for TYPE_BYTE_BINARY " + "must have no more than 16 entries"); } raster = Raster.createPackedRaster(DataBuffer.TYPE_BYTE, width, height, 1, bits, null); break; case TYPE_BYTE_INDEXED: raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height, 1, null); break; default: throw new IllegalArgumentException("Invalid image type (" + imageType+"). Image type must"+ " be either TYPE_BYTE_BINARY or "+ " TYPE_BYTE_INDEXED"); } if (!cm.isCompatibleRaster(raster)) { throw new IllegalArgumentException("Incompatible image type and IndexColorModel"); } colorModel = cm; this.imageType = imageType; } /** * Constructs a new <code>BufferedImage</code> with a specified * <code>ColorModel</code> and <code>Raster</code>. If the number and * types of bands in the <code>SampleModel</code> of the * <code>Raster</code> do not match the number and types required by * the <code>ColorModel</code> to represent its color and alpha * components, a {@link RasterFormatException} is thrown. This * method can multiply or divide the color <code>Raster</code> data by * alpha to match the <code>alphaPremultiplied</code> state * in the <code>ColorModel</code>. Properties for this * <code>BufferedImage</code> can be established by passing * in a {@link Hashtable} of <code>String</code>/<code>Object</code> * pairs. * @param cm <code>ColorModel</code> for the new image * @param raster <code>Raster</code> for the image data * @param isRasterPremultiplied if <code>true</code>, the data in * the raster has been premultiplied with alpha. * @param properties <code>Hashtable</code> of * <code>String</code>/<code>Object</code> pairs. * @exception <code>RasterFormatException</code> if the number and * types of bands in the <code>SampleModel</code> of the * <code>Raster</code> do not match the number and types required by * the <code>ColorModel</code> to represent its color and alpha * components. * @exception <code>IllegalArgumentException</code> if * <code>raster</code> is incompatible with <code>cm</code> * @see ColorModel * @see Raster * @see WritableRaster *//* * * FOR NOW THE CODE WHICH DEFINES THE RASTER TYPE IS DUPLICATED BY DVF * SEE THE METHOD DEFINERASTERTYPE @ RASTEROUTPUTMANAGER * */ public BufferedImage (ColorModel cm, WritableRaster raster, boolean isRasterPremultiplied, Hashtable properties) { if (!cm.isCompatibleRaster(raster)) { throw new IllegalArgumentException("Raster "+raster+ " is incompatible with ColorModel "+ cm); } if ((raster.minX != 0) || (raster.minY != 0)) { throw new IllegalArgumentException("Raster "+raster+ " has minX or minY not equal to zero: " + raster.minX + " " + raster.minY); } colorModel = cm; this.raster = raster; this.properties = properties; int numBands = raster.getNumBands(); boolean isAlphaPre = cm.isAlphaPremultiplied(); ColorSpace cs; // Force the raster data alpha state to match the premultiplied // state in the color model coerceData(isRasterPremultiplied); SampleModel sm = raster.getSampleModel(); cs = cm.getColorSpace(); int csType = cs.getType(); if (csType != ColorSpace.TYPE_RGB) { if (csType == ColorSpace.TYPE_GRAY && cm instanceof ComponentColorModel) { // Check if this might be a child raster (fix for bug 4240596) if (sm instanceof ComponentSampleModel && ((ComponentSampleModel)sm).getPixelStride() != numBands) { imageType = TYPE_CUSTOM; } else if (raster instanceof ByteComponentRaster && raster.getNumBands() == 1 && cm.getComponentSize(0) == 8 && ((ByteComponentRaster)raster).getPixelStride() == 1) { imageType = TYPE_BYTE_GRAY; } else if (raster instanceof ShortComponentRaster && raster.getNumBands() == 1 && cm.getComponentSize(0) == 16 && ((ShortComponentRaster)raster).getPixelStride() == 1) { imageType = TYPE_USHORT_GRAY; } } else { imageType = TYPE_CUSTOM; } return; } if ((raster instanceof IntegerComponentRaster) && (numBands == 3 || numBands == 4)) { IntegerComponentRaster iraster = (IntegerComponentRaster) raster; // Check if the raster params and the color model // are correct int pixSize = cm.getPixelSize(); if (iraster.getPixelStride() == 1 && cm instanceof DirectColorModel && (pixSize == 32 || pixSize == 24)) { // Now check on the DirectColorModel params DirectColorModel dcm = (DirectColorModel) cm; int rmask = dcm.getRedMask(); int gmask = dcm.getGreenMask(); int bmask = dcm.getBlueMask(); if (rmask == DCM_RED_MASK && gmask == DCM_GREEN_MASK && bmask == DCM_BLUE_MASK) { if (dcm.getAlphaMask() == DCM_ALPHA_MASK) { imageType = (isAlphaPre ? TYPE_INT_ARGB_PRE : TYPE_INT_ARGB); } else { // No Alpha if (!dcm.hasAlpha()) { imageType = TYPE_INT_RGB; } } } // if (dcm.getRedMask() == DCM_RED_MASK && else if (rmask == DCM_BGR_RED_MASK && gmask == DCM_BGR_GRN_MASK && bmask == DCM_BGR_BLU_MASK) { if (!dcm.hasAlpha()) { imageType = TYPE_INT_BGR; } } // if (rmask == DCM_BGR_RED_MASK && } // if (iraster.getPixelStride() == 1 } // ((raster instanceof IntegerComponentRaster) && else if ((cm instanceof IndexColorModel) && (numBands == 1) && (!cm.hasAlpha() || !isAlphaPre)) { IndexColorModel icm = (IndexColorModel) cm; int pixSize = icm.getPixelSize(); if (raster instanceof BytePackedRaster) { imageType = TYPE_BYTE_BINARY; } // if (raster instanceof BytePackedRaster) else if (raster instanceof ByteComponentRaster) { ByteComponentRaster braster = (ByteComponentRaster) raster; if (braster.getPixelStride() == 1 && pixSize <= 8) { imageType = TYPE_BYTE_INDEXED; } } } // else if (cm instanceof IndexColorModel) && (numBands == 1)) else if ((raster instanceof ShortComponentRaster) && (cm instanceof DirectColorModel) && (numBands == 3) && !cm.hasAlpha()) { DirectColorModel dcm = (DirectColorModel) cm; if (dcm.getRedMask() == DCM_565_RED_MASK) { if (dcm.getGreenMask() == DCM_565_GRN_MASK && dcm.getBlueMask() == DCM_565_BLU_MASK) { imageType = TYPE_USHORT_565_RGB; } } else if (dcm.getRedMask() == DCM_555_RED_MASK) { if (dcm.getGreenMask() == DCM_555_GRN_MASK && dcm.getBlueMask() == DCM_555_BLU_MASK) { imageType = TYPE_USHORT_555_RGB; } } } // else if ((cm instanceof IndexColorModel) && (numBands == 1)) else if ((raster instanceof ByteComponentRaster) && (cm instanceof ComponentColorModel) && (raster.getSampleModel() instanceof PixelInterleavedSampleModel) && (numBands == 3 || numBands == 4)) { ComponentColorModel ccm = (ComponentColorModel) cm; PixelInterleavedSampleModel csm = (PixelInterleavedSampleModel)raster.getSampleModel(); ByteComponentRaster braster = (ByteComponentRaster) raster; int[] offs = csm.getBandOffsets(); if (ccm.getNumComponents() != numBands) { throw new RasterFormatException("Number of components in "+ "ColorModel ("+ ccm.getNumComponents()+ ") does not match # in "+ " Raster ("+numBands+")"); } int[] nBits = ccm.getComponentSize(); boolean is8bit = true; for (int i=0; i < numBands; i++) { if (nBits[i] != 8) { is8bit = false; break; } } if (is8bit && offs[0] == numBands-1 && offs[1] == numBands-2 && offs[2] == numBands-3) { if (numBands == 3) { imageType = TYPE_3BYTE_BGR; } else if (offs[3] == 0) { imageType = (isAlphaPre ? TYPE_4BYTE_ABGR_PRE : TYPE_4BYTE_ABGR); } } } // else if ((raster instanceof ByteComponentRaster) && } /** * Returns the image type. If it is not one of the known types, * TYPE_CUSTOM is returned. * @return the image type of this <code>BufferedImage</code>. * @see #TYPE_INT_RGB * @see #TYPE_INT_ARGB * @see #TYPE_INT_ARGB_PRE * @see #TYPE_INT_BGR * @see #TYPE_3BYTE_BGR * @see #TYPE_4BYTE_ABGR * @see #TYPE_4BYTE_ABGR_PRE * @see #TYPE_BYTE_GRAY * @see #TYPE_BYTE_BINARY * @see #TYPE_BYTE_INDEXED * @see #TYPE_USHORT_GRAY * @see #TYPE_USHORT_565_RGB * @see #TYPE_USHORT_555_RGB * @see #TYPE_CUSTOM */ public int getType() { return imageType; } /** * Returns the <code>ColorModel</code>. * @return the <code>ColorModel</code> of this * <code>BufferedImage</code>. */ public ColorModel getColorModel() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -