📄 bufferedimage.java
字号:
return colorModel; } /** * Returns the {@link WritableRaster}. * @return the <code>WriteableRaster</code> of this * <code>BufferedImage</code>. */ public WritableRaster getRaster() { return raster; } /** * Returns a <code>WritableRaster</code> representing the alpha * channel for <code>BufferedImage</code> objects * with <code>ColorModel</code> objects that support a separate * spatial alpha channel, such as <code>ComponentColorModel</code> and * <code>DirectColorModel</code>. Returns <code>null</code> if there * is no alpha channel associated with the <code>ColorModel</code> in * this image. This method assumes that for all * <code>ColorModel</code> objects other than * <code>IndexColorModel</code>, if the <code>ColorModel</code> * supports alpha, there is a separate alpha channel * which is stored as the last band of image data. * If the image uses an <code>IndexColorModel</code> that * has alpha in the lookup table, this method returns * <code>null</code> since there is no spatially discrete alpha * channel. This method creates a new * <code>WritableRaster</code>, but shares the data array. * @return a <code>WritableRaster</code> or <code>null</code> if this * <code>BufferedImage</code> has no alpha channel associated * with its <code>ColorModel</code>. */ public WritableRaster getAlphaRaster() { return colorModel.getAlphaRaster(raster); } /** * Returns an integer pixel in the default RGB color model * (TYPE_INT_ARGB) and default sRGB colorspace. Color * conversion takes place if this default model does not match * the image <code>ColorModel</code>. There are only 8-bits of * precision for each color component in the returned data when using * this method. * @param x, y the coordinates of the pixel from which to get * the pixel in the default RGB color model and sRGB * color space * @return an integer pixel in the default RGB color model and * default sRGB colorspace. * @see #setRGB(int, int, int) * @see #setRGB(int, int, int, int, int[], int, int) */ public int getRGB(int x, int y) { return colorModel.getRGB(raster.getDataElements(x, y, null)); } /** * Returns an array of integer pixels in the default RGB color model * (TYPE_INT_ARGB) and default sRGB color space, * from a portion of the image data. Color conversion takes * place if the default model does not match the image * <code>ColorModel</code>. There are only 8-bits of precision for * each color component in the returned data when * using this method. With a specified coordinate (x, y) in the * image, the ARGB pixel can be accessed in this way: * <pre> * pixel = rgbArray[offset + (y-startY)*scansize + (x-startX)]; * </pre> * @param startX, startY the starting coordinates * @param w width of region * @param h height of region * @param rgbArray if not <code>null</code>, the rgb pixels are * written here * @param offset offset into the <code>rgbArray</code> * @param scansize scanline stride for the <code>rgbArray</code> * @return array of RGB pixels. * @exception <code>IllegalArgumentException</code> if an unknown * datatype is specified * @see #setRGB(int, int, int) * @see #setRGB(int, int, int, int, int[], int, int) */ public int[] getRGB(int startX, int startY, int w, int h, int[] rgbArray, int offset, int scansize) { int yoff = offset; int off; Object data; int nbands = raster.getNumBands(); int dataType = raster.getDataBuffer().getDataType(); switch (dataType) { case DataBuffer.TYPE_BYTE: data = new byte[nbands]; break; case DataBuffer.TYPE_USHORT: data = new short[nbands]; break; case DataBuffer.TYPE_INT: data = new int[nbands]; break; case DataBuffer.TYPE_FLOAT: data = new float[nbands]; break; case DataBuffer.TYPE_DOUBLE: data = new double[nbands]; break; default: throw new IllegalArgumentException("Unknown data buffer type: "+ dataType); } if (rgbArray == null) { rgbArray = new int[offset+h*scansize]; } for (int y = startY; y < startY+h; y++, yoff+=scansize) { off = yoff; for (int x = startX; x < startX+w; x++) { rgbArray[off++] = colorModel.getRGB(raster.getDataElements(x, y, data)); } } return rgbArray; } /** * Sets a pixel in this <code>BufferedImage</code> to the specified * RGB value. The pixel is assumed to be in the default RGB color * model, TYPE_INT_ARGB, and default sRGB color space. For images * with an <code>IndexColorModel</code>, the index with the nearest * color is chosen. * @param x, y the coordinates of the pixel to set * @param rgb the RGB value * @see #getRGB(int, int) * @see #getRGB(int, int, int, int, int[], int, int) */ public synchronized void setRGB(int x, int y, int rgb) { raster.setDataElements(x, y, colorModel.getDataElements(rgb, null)); } /** * Sets an array of integer pixels in the default RGB color model * (TYPE_INT_ARGB) and default sRGB color space, * into a portion of the image data. Color conversion takes place * if the default model does not match the image * <code>ColorModel</code>. There are only 8-bits of precision for * each color component in the returned data when * using this method. With a specified coordinate (x, y) in the * this image, the ARGB pixel can be accessed in this way: * <pre> * pixel = rgbArray[offset + (y-startY)*scansize + (x-startX)]; * </pre> * WARNING: No dithering takes place. * * @param startX, startY the starting coordinates * @param w width of the region * @param h height of the region * @param rgbArray the rgb pixels * @param offset offset into the <code>rgbArray</code> * @param scansize scanline stride for the <code>rgbArray</code> * @see #getRGB(int, int) * @see #getRGB(int, int, int, int, int[], int, int) */ public void setRGB(int startX, int startY, int w, int h, int[] rgbArray, int offset, int scansize) { int yoff = offset; int off; Object pixel = null; for (int y = startY; y < startY+h; y++, yoff+=scansize) { off = yoff; for (int x = startX; x < startX+w; x++) { pixel = colorModel.getDataElements(rgbArray[off++], pixel); raster.setDataElements(x, y, pixel); } } } /** * Returns the width of the <code>BufferedImage</code>. * @return the width of this <code>BufferedImage</code> */ public int getWidth() { return raster.getWidth(); } /** * Returns the height of the <code>BufferedImage</code>. * @return the height of this <code>BufferedImage</code> */ public int getHeight() { return raster.getHeight(); } /** * Returns the width of the <code>BufferedImage</code>. * @param observer ignored * @return the width of this <code>BufferedImage</code> */ public int getWidth(ImageObserver observer) { return raster.getWidth(); } /** * Returns the height of the <code>BufferedImage</code>. * @param observer ignored * @return the height of this <code>BufferedImage</code> */ public int getHeight(ImageObserver observer) { return raster.getHeight(); } /** * Returns the object that produces the pixels for the image. * @return the {@link ImageProducer} that is used to produce the * pixels for this image. * @see ImageProducer */ public ImageProducer getSource() { if (osis == null) { osis = new OffScreenImageSource(this); } return osis; } /** * Returns a property of the image by name. Individual property names * are defined by the various image formats. If a property is not * defined for a particular image, this method returns the * <code>UndefinedProperty</code> field. If the properties * for this image are not yet known, then this method returns * <code>null</code> and the <code>ImageObserver</code> object is * notified later. The property name "comment" should be used to * store an optional comment that can be presented to the user as a * description of the image, its source, or its author. * @param name the property name * @param observer the <code>ImageObserver</code> that receives * notification regarding image information * @return an {@link Object} that is the property referred to by the * specified <code>name</code> or <code>null</code> if the * properties of this image are not yet known. * @see ImageObserver * @see java.awt.Image#UndefinedProperty */ public Object getProperty(String name, ImageObserver observer) { return getProperty(name); } /** * Returns a property of the image by name. * @param name the property name * @return an <code>Object</code> that is the property referred to by * the specified <code>name</code>. */ public Object getProperty(String name) { if (properties == null) { return null; } Object o = properties.get(name); if (o == null) { o = java.awt.Image.UndefinedProperty; } return o; } /** * Flushes all resources being used to cache optimization information. * The underlying pixel data is unaffected. */ public void flush() { } /** * This method returns a {@link Graphics2D}, but is here * for backwards compatibility. {@link #createGraphics() createGraphics} is more * convenient, since it is declared to return a * <code>Graphics2D</code>. * @return a <code>Graphics2D</code>, which can be used to draw into * this image. */ public java.awt.Graphics getGraphics() { return createGraphics(); } /** * Creates a <code>Graphics2D</code>, which can be used to draw into * this <code>BufferedImage</code>. * @return a <code>Graphics2D</code>, used for drawing into this * image. */ public Graphics2D createGraphics() { GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); return env.createGraphics(this); } /** * Returns a subimage defined by a specified rectangular region. * The returned <code>BufferedImage</code> shares the same * data array as the original image. * @param x, y the coordinates of the upper-left corner of the * specified rectangular region * @param w the width of the specified rectangular region * @param h the height of the specified rectangular region * @return a <code>BufferedImage</code> that is the subimage of this * <code>BufferedImage</code>. * @exception <code>RasterFormatException</code> if the specified * area is not contained within this <code>BufferedImage</code>. */ public BufferedImage getSubimage (int x, int y, int w, int h) { return new BufferedImage (colorModel, raster.createWritableChild(x, y, w, h, 0, 0, null), colorModel.isAlphaPremultiplied(), properties); } /** * Returns whether or not the alpha has been premultiplied. It * returns <code>true</code> if there is no alpha since the * default alpha is OPAQUE. * @return <code>true</code> if the alpha has been premultiplied; * <code>false</code> otherwise. */ public boolean isAlphaPremultiplied() { return colorModel.isAlphaPremultiplied(); } /** * Forces the data to match the state specified in the * <code>isAlphaPremultiplied</code> variable. It may multiply or * divide the color raster data by alpha, or do nothing if the data is * in the correct state. * @param isAlphaPremultiplied <code>true</code> if the alpha has been * premultiplied; <code>false</code> otherwise. */ public void coerceData (boolean isAlphaPremultiplied) { if (colorModel.hasAlpha() && colorModel.isAlphaPremultiplied() != isAlphaPremultiplied) { // Make the color model do the conversion colorModel = colorModel.coerceData (raster, isAlphaPremultiplied); } } /** * Returns a <code>String</code> representation of this * <code>BufferedImage</code> object and its values. * @return a <code>String</code> representing this * <code>BufferedImage</code>. */ public String toString() { return new String("BufferedImage@"+Integer.toHexString(hashCode()) +": type = "+imageType +" "+colorModel+" "+raster); } /** * Returns a {@link Vector} of {@link RenderedImage} objects that are * the immediate sources, not the sources of these immediate sources, * of image data for this <code>BufferedImage</code>. This * method returns <code>null</code> if the <code>BufferedImage</code> * has no information about its immediate sources. It returns an * empty <code>Vector</code> if the <code>BufferedImage</code> has no * immediate sources. * @return a <code>Vector</code> containing immediate sources of * this <code>BufferedImage</code> object's image date, or * <code>null</code> if this <code>BufferedImage</code> has
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -