📄 bufferedimage.java
字号:
/* * @(#)BufferedImage.java 1.89 03/01/23 * * Copyright 2003 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package java.awt.image;import java.awt.Transparency;import java.awt.color.ColorSpace;import java.awt.Graphics2D;import java.awt.GraphicsEnvironment;import java.awt.geom.Rectangle2D;import java.awt.geom.Point2D;import java.awt.Point;import java.awt.Rectangle;import java.util.Hashtable;import java.util.Vector;import sun.awt.image.BytePackedRaster;import sun.awt.image.ShortComponentRaster;import sun.awt.image.ByteComponentRaster;import sun.awt.image.IntegerComponentRaster;import sun.awt.image.OffScreenImageSource;/** * * The <code>BufferedImage</code> subclass describes an {@link * java.awt.Image Image} with an accessible buffer of image data. * A <code>BufferedImage</code> is comprised of a {@link ColorModel} and a * {@link Raster} of image data. * The number and types of bands in the {@link SampleModel} of the * <code>Raster</code> must match the number and types required by the * <code>ColorModel</code> to represent its color and alpha components. * All <code>BufferedImage</code> objects have an upper left corner * coordinate of (0, 0). Any <code>Raster</code> used to construct a * <code>BufferedImage</code> must therefore have minX=0 and minY=0. * @see ColorModel * @see Raster * @see WritableRaster * @version 10 Feb 1997 */public class BufferedImage extends java.awt.Image implements WritableRenderedImage{ int imageType = TYPE_CUSTOM; ColorModel colorModel; WritableRaster raster; OffScreenImageSource osis; Hashtable properties; boolean isAlphaPremultiplied;// If true, alpha has been premultiplied in // color channels sun.java2d.SurfaceData sData; /** * Image Type Constants */ /** * Image type is not recognized so it must be a customized * image. This type is only used as a return value for the getType() * method. */ public static final int TYPE_CUSTOM = 0; /** * Represents an image with 8-bit RGB color components packed into * integer pixels. The image has a {@link DirectColorModel} without * alpha. */ public static final int TYPE_INT_RGB = 1; /** * Represents an image with 8-bit RGBA color components packed into * integer pixels. The image has a <code>DirectColorModel</code> * with alpha. The color data in this image is considered not to be * premultiplied with alpha. When this type is used as the * <code>imageType</code> argument to a <code>BufferedImage</code> * constructor, the created image is consistent with images * created in the JDK1.1 and earlier releases. */ public static final int TYPE_INT_ARGB = 2; /** * Represents an image with 8-bit RGBA color components packed into * integer pixels. The image has a <code>DirectColorModel</code> * with alpha. The color data in this image is considered to be * premultiplied with alpha. */ public static final int TYPE_INT_ARGB_PRE = 3; /** * Represents an image with 8-bit RGB color components, corresponding * to a Windows- or Solaris- style BGR color model, with the colors * Blue, Green, and Red packed into integer pixels. There is no alpha. * The image has a {@link DirectColorModel}. */ public static final int TYPE_INT_BGR = 4; /** * Represents an image with 8-bit RGB color components, corresponding * to a Windows-style BGR color model) with the colors Blue, Green, * and Red stored in 3 bytes. There is no alpha. The image has a * <code>ComponentColorModel</code>. */ public static final int TYPE_3BYTE_BGR = 5; /** * Represents an image with 8-bit RGBA color components with the colors * Blue, Green, and Red stored in 3 bytes and 1 byte of alpha. The * image has a <code>ComponentColorModel</code> with alpha. The * color data in this image is considered not to be premultiplied with * alpha. The byte data is interleaved in a single * byte array in the order A, B, G, R * from lower to higher byte addresses within each pixel. */ public static final int TYPE_4BYTE_ABGR = 6; /** * Represents an image with 8-bit RGBA color components with the colors * Blue, Green, and Red stored in 3 bytes and 1 byte of alpha. The * image has a <code>ComponentColorModel</code> with alpha. The color * data in this image is considered to be premultiplied with alpha. * The byte data is interleaved in a single byte array in the order * A, B, G, R from lower to higher byte addresses within each pixel. */ public static final int TYPE_4BYTE_ABGR_PRE = 7; /** * Represents an image with 5-6-5 RGB color components (5-bits red, * 6-bits green, 5-bits blue) with no alpha. This image has * a <code>DirectColorModel</code>. */ public static final int TYPE_USHORT_565_RGB = 8; /** * Represents an image with 5-5-5 RGB color components (5-bits red, * 5-bits green, 5-bits blue) with no alpha. This image has * a <code>DirectColorModel</code>. */ public static final int TYPE_USHORT_555_RGB = 9; /** * Represents a unsigned byte grayscale image, non-indexed. This * image has a <code>ComponentColorModel</code> with a CS_GRAY * {@link ColorSpace}. */ public static final int TYPE_BYTE_GRAY = 10; /** * Represents an unsigned short grayscale image, non-indexed). This * image has a <code>ComponentColorModel</code> with a CS_GRAY * <code>ColorSpace</code>. */ public static final int TYPE_USHORT_GRAY = 11; /** * Represents an opaque byte-packed 1, 2, or 4 bit image. The * image has an {@link IndexColorModel} without alpha. When this * type is used as the <code>imageType</code> argument to the * <code>BufferedImage</code> constructor that takes an * <code>imageType</code> argument but no <code>ColorModel</code> * argument, a 1-bit image is created with an * <code>IndexColorModel</code> with two colors in the default * sRGB <code>ColorSpace</code>: {0, 0, 0} and * {255, 255, 255}. * * <p> Images with 2 or 4 bits per pixel may be constructed via * the <code>BufferedImage</code> constructor that takes a * <code>ColorModel</code> argument by supplying a * <code>ColorModel</code> with an appropriate map size. * * <p> Images with 8 bits per pixel should use the image types * <code>TYPE_BYTE_INDEXED</code> or <code>TYPE_BYTE_GRAY</code> * depending on their <code>ColorModel</code>. */ public static final int TYPE_BYTE_BINARY = 12; /** * Represents an indexed byte image. When this type is used as the * <code>imageType</code> argument to the <code>BufferedImage</code> * constructor that takes an <code>imageType</code> argument * but no <code>ColorModel</code> argument, an * <code>IndexColorModel</code> is created with * a 256-color 6/6/6 color cube palette with the rest of the colors * from 216-255 populated by grayscale values in the * default sRGB ColorSpace. */ public static final int TYPE_BYTE_INDEXED = 13; private static final int DCM_RED_MASK = 0x00ff0000; private static final int DCM_GREEN_MASK = 0x0000ff00; private static final int DCM_BLUE_MASK = 0x000000ff; private static final int DCM_ALPHA_MASK = 0xff000000; private static final int DCM_565_RED_MASK = 0xf800; private static final int DCM_565_GRN_MASK = 0x07E0; private static final int DCM_565_BLU_MASK = 0x001F; private static final int DCM_555_RED_MASK = 0x7C00; private static final int DCM_555_GRN_MASK = 0x03E0; private static final int DCM_555_BLU_MASK = 0x001F; private static final int DCM_BGR_RED_MASK = 0x0000ff; private static final int DCM_BGR_GRN_MASK = 0x00ff00; private static final int DCM_BGR_BLU_MASK = 0xff0000; static private native void initIDs(); static { ColorModel.loadLibraries(); initIDs(); } /** * Constructs a <code>BufferedImage</code> of one of the predefined * image types. The <code>ColorSpace</code> for the image is the * default sRGB space. * @param width width of the created image * @param height height of the created image * @param imageType type of the created image * @see ColorSpace * @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_USHORT_GRAY * @see #TYPE_BYTE_BINARY * @see #TYPE_BYTE_INDEXED * @see #TYPE_USHORT_565_RGB * @see #TYPE_USHORT_555_RGB */ public BufferedImage(int width, int height, int imageType) { switch (imageType) { case TYPE_INT_RGB: { colorModel = new DirectColorModel(24, 0x00ff0000, // Red 0x0000ff00, // Green 0x000000ff, // Blue 0x0 // Alpha ); raster = colorModel.createCompatibleWritableRaster(width, height); } break; case TYPE_INT_ARGB: { colorModel = ColorModel.getRGBdefault(); raster = colorModel.createCompatibleWritableRaster(width, height); } break; case TYPE_INT_ARGB_PRE: { colorModel = new DirectColorModel( ColorSpace.getInstance(ColorSpace.CS_sRGB), 32, 0x00ff0000,// Red 0x0000ff00,// Green 0x000000ff,// Blue 0xff000000,// Alpha true, // Alpha Premultiplied DataBuffer.TYPE_INT ); raster = colorModel.createCompatibleWritableRaster(width, height); } break; case TYPE_INT_BGR: { colorModel = new DirectColorModel(24, 0x000000ff, // Red 0x0000ff00, // Green 0x00ff0000 // Blue ); raster = colorModel.createCompatibleWritableRaster(width, height); } break; case TYPE_3BYTE_BGR: { ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB); int[] nBits = {8, 8, 8}; int[] bOffs = {2, 1, 0}; colorModel = new ComponentColorModel(cs, nBits, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE); raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height, width*3, 3, bOffs, null); } break; case TYPE_4BYTE_ABGR: { ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB); int[] nBits = {8, 8, 8, 8}; int[] bOffs = {3, 2, 1, 0}; colorModel = new ComponentColorModel(cs, nBits, true, false, Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE); raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height, width*4, 4, bOffs, null); } break; case TYPE_4BYTE_ABGR_PRE: { ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB); int[] nBits = {8, 8, 8, 8}; int[] bOffs = {3, 2, 1, 0}; colorModel = new ComponentColorModel(cs, nBits, true, true, Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE); raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height, width*4, 4, bOffs, null); } break; case TYPE_BYTE_GRAY: { ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY); int[] nBits = {8}; colorModel = new ComponentColorModel(cs, nBits, false, true, Transparency.OPAQUE, DataBuffer.TYPE_BYTE); raster = colorModel.createCompatibleWritableRaster(width, height); } break; case TYPE_USHORT_GRAY: { ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY); int[] nBits = {16}; colorModel = new ComponentColorModel(cs, nBits, false, true, Transparency.OPAQUE, DataBuffer.TYPE_USHORT); raster = colorModel.createCompatibleWritableRaster(width, height); } break; case TYPE_BYTE_BINARY: { byte[] arr = {(byte)0, (byte)0xff}; colorModel = new IndexColorModel(1, 2, arr, arr, arr); raster = Raster.createPackedRaster(DataBuffer.TYPE_BYTE, width, height, 1, 1, null); } break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -