BufferedImagepublic class BufferedImage extends Image implements Transparency, WritableRenderedImageThe BufferedImage class describes an Image which contains a buffer of image
data and includes a ColorModel and a Raster for this data. This class
provides methods for obtaining and setting the Raster and for manipulating
the ColorModel parameters. |
Fields Summary |
---|
public static final int | TYPE_CUSTOMThe Constant TYPE_CUSTOM indicates that Image type is unknown. | public static final int | TYPE_INT_RGBThe Constant TYPE_INT_RGB indicates an image with 8 bit RGB color
components, it has a DirectColorModel without alpha. | public static final int | TYPE_INT_ARGBThe Constant TYPE_INT_ARGB indicates an image with 8 bit RGBA color
components, it has a DirectColorModel with alpha. | public static final int | TYPE_INT_ARGB_PREThe Constant TYPE_INT_ARGB_PRE indicates an image with 8 bit RGBA color
components, it has a DirectColorModel with alpha, and image data is
pre-multiplied by alpha. | public static final int | TYPE_INT_BGRThe Constant TYPE_INT_BGR indicates an image with 8 bit RGB color
components, BGR color model (with the colors Blue, Green, and Red). There
is no alpha. The image has a DirectColorModel. | public static final int | TYPE_3BYTE_BGRThe Constant TYPE_3BYTE_BGR indicates an image with 8 bit RGB color
components, BGR color model (with the colors Blue, Green, and Red stored
in 3 bytes). There is no alpha. The image has a ComponentColorModel. | public static final int | TYPE_4BYTE_ABGRThe Constant TYPE_4BYTE_ABGR indicates an image with 8 bit RGBA color
components stored in 3 bytes and 1 byte of alpha. It has a
ComponentColorModel with alpha. | public static final int | TYPE_4BYTE_ABGR_PREThe Constant TYPE_4BYTE_ABGR_PRE indicates an image with 8 bit RGBA color
components stored in 3 bytes and 1 byte for alpha. The image has a
ComponentColorModel with alpha. The color data is pre-multiplied with
alpha. | public static final int | TYPE_USHORT_565_RGBThe Constant TYPE_USHORT_565_RGB indicates an image with 565 RGB color
components (5-bits red, 6-bits green, 5-bits blue) with no alpha. This
image has a DirectColorModel. | public static final int | TYPE_USHORT_555_RGBThe Constant TYPE_USHORT_555_RGB indicates an image with 555 RGB color
components (5-bits red, 5-bits green, 5-bits blue) with no alpha. This
image has a DirectColorModel. | public static final int | TYPE_BYTE_GRAYThe Constant TYPE_BYTE_GRAY indicates a unsigned byte image. This image
has a ComponentColorModel with a CS_GRAY ColorSpace. | public static final int | TYPE_USHORT_GRAYThe Constant TYPE_USHORT_GRAY indicates an unsigned short image. This
image has a ComponentColorModel with a CS_GRAY ColorSpace. | public static final int | TYPE_BYTE_BINARYThe Constant TYPE_BYTE_BINARY indicates an opaque byte-packed 1, 2 or 4
bit image. The image has an IndexColorModel without alpha. | public static final int | TYPE_BYTE_INDEXEDThe Constant TYPE_BYTE_INDEXED indicates an indexed byte image. | private static final int | ALPHA_MASKThe Constant ALPHA_MASK. | private static final int | RED_MASKThe Constant RED_MASK. | private static final int | GREEN_MASKThe Constant GREEN_MASK. | private static final int | BLUE_MASKThe Constant BLUE_MASK. | private static final int | RED_BGR_MASKThe Constant RED_BGR_MASK. | private static final int | GREEN_BGR_MASKThe Constant GREEN_BGR_MASK. | private static final int | BLUE_BGR_MASKThe Constant BLUE_BGR_MASK. | private static final int | RED_565_MASKThe Constant RED_565_MASK. | private static final int | GREEN_565_MASKThe Constant GREEN_565_MASK. | private static final int | BLUE_565_MASKThe Constant BLUE_565_MASK. | private static final int | RED_555_MASKThe Constant RED_555_MASK. | private static final int | GREEN_555_MASKThe Constant GREEN_555_MASK. | private static final int | BLUE_555_MASKThe Constant BLUE_555_MASK. | private ColorModel | cmThe cm. | private final WritableRaster | rasterThe raster. | private final int | imageTypeThe image type. | private Hashtable | propertiesThe properties. | private final org.apache.harmony.awt.gl.ImageSurface | imageSurfThe image surf. |
Constructors Summary |
---|
public BufferedImage(ColorModel cm, WritableRaster raster, boolean isRasterPremultiplied, Hashtable properties)Instantiates a new BufferedImage with the specified ColorModel, and
WritableRaster objects. The Raster data can be be divided or multiplied
by alpha. It depends on the alphaPremultiplied state in the ColorModel.
if (!cm.isCompatibleRaster(raster)) {
// awt.4D=The raster is incompatible with this ColorModel
throw new IllegalArgumentException(Messages.getString("awt.4D")); //$NON-NLS-1$
}
if (raster.getMinX() != 0 || raster.getMinY() != 0) {
// awt.228=minX or minY of this raster not equal to zero
throw new IllegalArgumentException(Messages.getString("awt.228")); //$NON-NLS-1$
}
this.cm = cm;
this.raster = raster;
this.properties = properties;
coerceData(isRasterPremultiplied);
imageType = Surface.getType(cm, raster);
imageSurf = createImageSurface(imageType);
| public BufferedImage(int width, int height, int imageType, IndexColorModel cm)Instantiates a new BufferedImage with the specified width, height
predefined image type (TYPE_BYTE_BINARY or TYPE_BYTE_INDEXED) and the
specified IndexColorModel.
switch (imageType) {
case TYPE_BYTE_BINARY:
if (cm.hasAlpha()) {
// awt.227=This image type can't have alpha
throw new IllegalArgumentException(Messages.getString("awt.227")); //$NON-NLS-1$
}
int pixel_bits = 0;
int mapSize = cm.getMapSize();
if (mapSize <= 2) {
pixel_bits = 1;
} else if (mapSize <= 4) {
pixel_bits = 2;
} else if (mapSize <= 16) {
pixel_bits = 4;
} else {
// awt.221=The imageType is TYPE_BYTE_BINARY and the color
// map has more than 16 entries
throw new IllegalArgumentException(Messages.getString("awt.221")); //$NON-NLS-1$
}
raster = Raster.createPackedRaster(DataBuffer.TYPE_BYTE, width, height, 1,
pixel_bits, null);
break;
case TYPE_BYTE_INDEXED:
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height, 1,
null);
break;
default:
// awt.222=The imageType is not TYPE_BYTE_BINARY or
// TYPE_BYTE_INDEXED
throw new IllegalArgumentException(Messages.getString("awt.222")); //$NON-NLS-1$
}
if (!cm.isCompatibleRaster(raster)) {
// awt.223=The imageType is not compatible with ColorModel
throw new IllegalArgumentException(Messages.getString("awt.223")); //$NON-NLS-1$
}
this.cm = cm;
this.imageType = imageType;
imageSurf = createImageSurface(imageType);
| public BufferedImage(int width, int height, int imageType)Instantiates a new BufferedImage with the specified width, height and
predefined image type.
switch (imageType) {
case TYPE_INT_RGB:
cm = new DirectColorModel(24, RED_MASK, GREEN_MASK, BLUE_MASK);
raster = cm.createCompatibleWritableRaster(width, height);
break;
case TYPE_INT_ARGB:
cm = ColorModel.getRGBdefault();
raster = cm.createCompatibleWritableRaster(width, height);
break;
case TYPE_INT_ARGB_PRE:
cm = new DirectColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), 32, RED_MASK,
GREEN_MASK, BLUE_MASK, ALPHA_MASK, true, DataBuffer.TYPE_INT);
raster = cm.createCompatibleWritableRaster(width, height);
break;
case TYPE_INT_BGR:
cm = new DirectColorModel(24, RED_BGR_MASK, GREEN_BGR_MASK, BLUE_BGR_MASK);
raster = cm.createCompatibleWritableRaster(width, height);
break;
case TYPE_3BYTE_BGR: {
int bits[] = {
8, 8, 8
};
int bandOffsets[] = {
2, 1, 0
};
cm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), bits,
false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height,
width * 3, 3, bandOffsets, null);
}
break;
case TYPE_4BYTE_ABGR: {
int bits[] = {
8, 8, 8, 8
};
int bandOffsets[] = {
3, 2, 1, 0
};
cm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), bits,
true, false, Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE);
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height,
width * 4, 4, bandOffsets, null);
}
break;
case TYPE_4BYTE_ABGR_PRE: {
int bits[] = {
8, 8, 8, 8
};
int bandOffsets[] = {
3, 2, 1, 0
};
cm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), bits,
true, true, Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE);
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height,
width * 4, 4, bandOffsets, null);
}
break;
case TYPE_USHORT_565_RGB:
cm = new DirectColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), 16,
RED_565_MASK, GREEN_565_MASK, BLUE_565_MASK, 0, false,
DataBuffer.TYPE_USHORT);
raster = cm.createCompatibleWritableRaster(width, height);
break;
case TYPE_USHORT_555_RGB:
cm = new DirectColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), 15,
RED_555_MASK, GREEN_555_MASK, BLUE_555_MASK, 0, false,
DataBuffer.TYPE_USHORT);
raster = cm.createCompatibleWritableRaster(width, height);
break;
case TYPE_BYTE_GRAY: {
int bits[] = {
8
};
cm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_GRAY), bits,
false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
raster = cm.createCompatibleWritableRaster(width, height);
}
break;
case TYPE_USHORT_GRAY: {
int bits[] = {
16
};
cm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_GRAY), bits,
false, false, Transparency.OPAQUE, DataBuffer.TYPE_USHORT);
raster = cm.createCompatibleWritableRaster(width, height);
}
break;
case TYPE_BYTE_BINARY: {
int colorMap[] = {
0, 0xffffff
};
cm = new IndexColorModel(1, 2, colorMap, 0, false, -1, DataBuffer.TYPE_BYTE);
raster = Raster.createPackedRaster(DataBuffer.TYPE_BYTE, width, height, 1, 1, null);
}
break;
case TYPE_BYTE_INDEXED: {
int colorMap[] = 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) {
colorMap[i] = (r << 16) | (g << 8) | b;
i++;
}
}
}
int gray = 0x12;
for (; i < 256; i++, gray += 6) {
colorMap[i] = (gray << 16) | (gray << 8) | gray;
}
cm = new IndexColorModel(8, 256, colorMap, 0, false, -1, DataBuffer.TYPE_BYTE);
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height, 1,
null);
}
break;
default:
// awt.224=Unknown image type
throw new IllegalArgumentException(Messages.getString("awt.224")); //$NON-NLS-1$
}
this.imageType = imageType;
imageSurf = createImageSurface(imageType);
|
Methods Summary |
---|
public void | addTileObserver(java.awt.image.TileObserver to)
| public void | coerceData(boolean isAlphaPremultiplied)Coerces the data to achieve the state which is specified by the
isAlphaPremultiplied variable.
if (cm.hasAlpha() && cm.isAlphaPremultiplied() != isAlphaPremultiplied) {
cm = cm.coerceData(raster, isAlphaPremultiplied);
}
| public java.awt.image.WritableRaster | copyData(java.awt.image.WritableRaster outRaster)
if (outRaster == null) {
outRaster = Raster.createWritableRaster(raster.getSampleModel(), new Point(raster
.getSampleModelTranslateX(), raster.getSampleModelTranslateY()));
}
int w = outRaster.getWidth();
int h = outRaster.getHeight();
int minX = outRaster.getMinX();
int minY = outRaster.getMinY();
Object data = null;
data = raster.getDataElements(minX, minY, w, h, data);
outRaster.setDataElements(minX, minY, w, h, data);
return outRaster;
| public java.awt.Graphics2D | createGraphics()Creates the Graphics2D object which allows to draw into this
BufferedImage.
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
// return ge.createGraphics(this);
// ???AWT hack, FIXME
// return AndroidGraphics2D.getInstance();
// throw new RuntimeException("Not implemented!");
return null;
| private org.apache.harmony.awt.gl.ImageSurface | createImageSurface(int type)Creates the image surface.
return new ImageSurface(getColorModel(), getRaster(), type);
| public void | flush()
imageSurf.dispose();
| public java.awt.image.WritableRaster | getAlphaRaster()Gets a WritableRaster object which contains the alpha channel of
BufferedImage object with ColorModel objects that supports a separate
alpha channel such as ComponentColorModel or DirectColorModel.
return cm.getAlphaRaster(raster);
| public java.awt.image.ColorModel | getColorModel()
return cm;
| public java.awt.image.Raster | getData()
int w = raster.getWidth();
int h = raster.getHeight();
int minX = raster.getMinX();
int minY = raster.getMinY();
WritableRaster outr = Raster.createWritableRaster(raster.getSampleModel(), new Point(raster
.getSampleModelTranslateX(), raster.getSampleModelTranslateY()));
Object data = null;
data = raster.getDataElements(minX, minY, w, h, data);
outr.setDataElements(minX, minY, w, h, data);
return outr;
| public java.awt.image.Raster | getData(java.awt.Rectangle rect)
int minX = rect.x;
int minY = rect.y;
int w = rect.width;
int h = rect.height;
SampleModel sm = raster.getSampleModel();
SampleModel nsm = sm.createCompatibleSampleModel(w, h);
WritableRaster outr = Raster.createWritableRaster(nsm, rect.getLocation());
Object data = null;
data = raster.getDataElements(minX, minY, w, h, data);
outr.setDataElements(minX, minY, w, h, data);
return outr;
| public java.awt.Graphics | getGraphics()
return createGraphics();
| public int | getHeight(java.awt.image.ImageObserver observer)
return raster.getHeight();
| public int | getHeight()
return raster.getHeight();
| org.apache.harmony.awt.gl.ImageSurface | getImageSurface()Gets the image surface.
return imageSurf;
| public int | getMinTileX()
return 0;
| public int | getMinTileY()
return 0;
| public int | getMinX()
return raster.getMinX();
| public int | getMinY()
return raster.getMinY();
| public int | getNumXTiles()
return 1;
| public int | getNumYTiles()
return 1;
| public java.lang.Object | getProperty(java.lang.String name, java.awt.image.ImageObserver observer)
return getProperty(name);
| public java.lang.Object | getProperty(java.lang.String name)
if (name == null) {
// awt.225=Property name is null
throw new NullPointerException(Messages.getString("awt.225")); //$NON-NLS-1$
}
if (properties == null) {
return Image.UndefinedProperty;
}
Object property = properties.get(name);
if (property == null) {
property = Image.UndefinedProperty;
}
return property;
| public java.lang.String[] | getPropertyNames()
if (properties == null) {
return null;
}
Vector<String> v = new Vector<String>();
for (Enumeration<?> e = properties.keys(); e.hasMoreElements();) {
try {
v.add((String)e.nextElement());
} catch (ClassCastException ex) {
}
}
int size = v.size();
if (size > 0) {
String names[] = new String[size];
for (int i = 0; i < size; i++) {
names[i] = v.elementAt(i);
}
return names;
}
return null;
| public int[] | getRGB(int startX, int startY, int w, int h, int[] rgbArray, int offset, int scansize)Gets an array of colors in the TYPE_INT_ARGB color model and default sRGB
color space of the specified area of this BufferedImage. The result array
is composed by the following algorithm:
pixel = rgbArray[offset + (y-startY)*scansize + (x-startX)]
if (rgbArray == null) {
rgbArray = new int[offset + h * scansize];
}
int off = offset;
for (int y = startY; y < startY + h; y++, off += scansize) {
int i = off;
for (int x = startX; x < startX + w; x++, i++) {
rgbArray[i] = cm.getRGB(raster.getDataElements(x, y, null));
}
}
return rgbArray;
| public int | getRGB(int x, int y)Gets a color in the TYPE_INT_ARGB color model and default sRGB color
space of the specified pixel.
return cm.getRGB(raster.getDataElements(x, y, null));
| public java.awt.image.WritableRaster | getRaster()Gets the WritableRaster of this BufferedImage.
return raster;
| public java.awt.image.SampleModel | getSampleModel()
return raster.getSampleModel();
| public java.awt.image.ImageProducer | getSource()
return new BufferedImageSource(this, properties);
| public java.util.Vector | getSources()
return null;
| public java.awt.image.BufferedImage | getSubimage(int x, int y, int w, int h)Gets the rectangular area of this BufferedImage as a subimage.
WritableRaster wr = raster.createWritableChild(x, y, w, h, 0, 0, null);
return new BufferedImage(cm, wr, cm.isAlphaPremultiplied(), properties);
| public java.awt.image.Raster | getTile(int tileX, int tileY)
if (tileX == 0 && tileY == 0) {
return raster;
}
// awt.226=Both tileX and tileY are not equal to 0
throw new ArrayIndexOutOfBoundsException(Messages.getString("awt.226")); //$NON-NLS-1$
| public int | getTileGridXOffset()
return raster.getSampleModelTranslateX();
| public int | getTileGridYOffset()
return raster.getSampleModelTranslateY();
| public int | getTileHeight()
return raster.getHeight();
| public int | getTileWidth()
return raster.getWidth();
| public int | getTransparency()
return cm.getTransparency();
| public int | getType()Gets the image type.
return imageType;
| public int | getWidth(java.awt.image.ImageObserver observer)
return raster.getWidth();
| public int | getWidth()
return raster.getWidth();
| public java.awt.image.WritableRaster | getWritableTile(int tileX, int tileY)
return raster;
| public java.awt.Point[] | getWritableTileIndices()
Point points[] = new Point[1];
points[0] = new Point(0, 0);
return points;
| public boolean | hasTileWriters()
return true;
| public boolean | isAlphaPremultiplied()Returns true if alpha is pre-multiplied, false if alpha is not
pre-multiplied or there is no alpha.
return cm.isAlphaPremultiplied();
| public boolean | isTileWritable(int tileX, int tileY)
if (tileX == 0 && tileY == 0) {
return true;
}
// awt.226=Both tileX and tileY are not equal to 0
throw new ArrayIndexOutOfBoundsException(Messages.getString("awt.226")); //$NON-NLS-1$
| public void | releaseWritableTile(int tileX, int tileY)
| public void | removeTileObserver(java.awt.image.TileObserver to)
| public void | setData(java.awt.image.Raster r)
Rectangle from = r.getBounds();
Rectangle to = raster.getBounds();
Rectangle intersection = to.intersection(from);
int minX = intersection.x;
int minY = intersection.y;
int w = intersection.width;
int h = intersection.height;
Object data = null;
data = r.getDataElements(minX, minY, w, h, data);
raster.setDataElements(minX, minY, w, h, data);
| public void | setRGB(int startX, int startY, int w, int h, int[] rgbArray, int offset, int scansize)Sets RGB values from the specified array to the specified BufferedImage
area. The pixels are in the default RGB color model (TYPE_INT_ARGB) and
default sRGB color space.
int off = offset;
for (int y = startY; y < startY + h; y++, off += scansize) {
int i = off;
for (int x = startX; x < startX + w; x++, i++) {
raster.setDataElements(x, y, cm.getDataElements(rgbArray[i], null));
}
}
| public synchronized void | setRGB(int x, int y, int rgb)Sets a the specified RGB value to the specified pixel of this
BufferedImage. The pixel should be in the default RGB color model
(TYPE_INT_ARGB) and default sRGB color space.
raster.setDataElements(x, y, cm.getDataElements(rgb, null));
| public java.lang.String | toString()Returns the string representation of this BufferedImage object.
return "BufferedImage@" + Integer.toHexString(hashCode()) + //$NON-NLS-1$
": type = " + imageType + " " + cm + " " + raster; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
|