📄 raster.java
字号:
/* * @(#)Raster.java 1.59 03/01/23 * * Copyright 2003 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. *//* **************************************************************** ****************************************************************** ****************************************************************** *** COPYRIGHT (c) Eastman Kodak Company, 1997 *** As an unpublished work pursuant to Title 17 of the United *** States Code. All rights reserved. ****************************************************************** ****************************************************************** ******************************************************************/package java.awt.image;import java.awt.Rectangle;import java.awt.Point;import sun.awt.image.ByteInterleavedRaster;import sun.awt.image.ShortInterleavedRaster;import sun.awt.image.IntegerInterleavedRaster;import sun.awt.image.ByteBandedRaster;import sun.awt.image.ShortBandedRaster;import sun.awt.image.BytePackedRaster;import sun.awt.image.SunWritableRaster;/** * A class representing a rectangular array of pixels. A Raster * encapsulates a DataBuffer that stores the sample values and a * SampleModel that describes how to locate a given sample value in a * DataBuffer. * <p> * A Raster defines values for pixels occupying a particular * rectangular area of the plane, not necessarily including (0, 0). * The rectangle, known as the Raster's bounding rectangle and * available by means of the getBounds method, is defined by minX, * minY, width, and height values. The minX and minY values define * the coordinate of the upper left corner of the Raster. References * to pixels outside of the bounding rectangle may result in an * exception being thrown, or may result in references to unintended * elements of the Raster's associated DataBuffer. It is the user's * responsibility to avoid accessing such pixels. * <p> * A SampleModel describes how samples of a Raster * are stored in the primitive array elements of a DataBuffer. * Samples may be stored one per data element, as in a * PixelInterleavedSampleModel or BandedSampleModel, or packed several to * an element, as in a SinglePixelPackedSampleModel or * MultiPixelPackedSampleModel. The SampleModel is also * controls whether samples are sign extended, allowing unsigned * data to be stored in signed Java data types such as byte, short, and * int. * <p> * Although a Raster may live anywhere in the plane, a SampleModel * makes use of a simple coordinate system that starts at (0, 0). A * Raster therefore contains a translation factor that allows pixel * locations to be mapped between the Raster's coordinate system and * that of the SampleModel. The translation from the SampleModel * coordinate system to that of the Raster may be obtained by the * getSampleModelTranslateX and getSampleModelTranslateY methods. * <p> * A Raster may share a DataBuffer with another Raster either by * explicit construction or by the use of the createChild and * createTranslatedChild methods. Rasters created by these methods * can return a reference to the Raster they were created from by * means of the getParent method. For a Raster that was not * constructed by means of a call to createTranslatedChild or * createChild, getParent will return null. * <p> * The createTranslatedChild method returns a new Raster that * shares all of the data of the current Raster, but occupies a * bounding rectangle of the same width and height but with a * different starting point. For example, if the parent Raster * occupied the region (10, 10) to (100, 100), and the translated * Raster was defined to start at (50, 50), then pixel (20, 20) of the * parent and pixel (60, 60) of the child occupy the same location in * the DataBuffer shared by the two Rasters. In the first case, (-10, * -10) should be added to a pixel coordinate to obtain the * corresponding SampleModel coordinate, and in the second case (-50, * -50) should be added. * <p> * The translation between a parent and child Raster may be * determined by subtracting the child's sampleModelTranslateX and * sampleModelTranslateY values from those of the parent. * <p> * The createChild method may be used to create a new Raster * occupying only a subset of its parent's bounding rectangle * (with the same or a translated coordinate system) or * with a subset of the bands of its parent. * <p> * All constructors are protected. The correct way to create a * Raster is to use one of the static create methods defined in this * class. These methods create instances of Raster that use the * standard Interleaved, Banded, and Packed SampleModels and that may * be processed more efficiently than a Raster created by combining * an externally generated SampleModel and DataBuffer. * @see java.awt.image.DataBuffer * @see java.awt.image.SampleModel * @see java.awt.image.PixelInterleavedSampleModel * @see java.awt.image.BandedSampleModel * @see java.awt.image.SinglePixelPackedSampleModel * @see java.awt.image.MultiPixelPackedSampleModel */public class Raster { /** * The SampleModel that describes how pixels from this Raster * are stored in the DataBuffer. */ protected SampleModel sampleModel; /** The DataBuffer that stores the image data. */ protected DataBuffer dataBuffer; /** The X coordinate of the upper-left pixel of this Raster. */ protected int minX; /** The Y coordinate of the upper-left pixel of this Raster. */ protected int minY; /** The width of this Raster. */ protected int width; /** The height of this Raster. */ protected int height; /** * The X translation from the coordinate space of the * Raster's SampleModel to that of the Raster. */ protected int sampleModelTranslateX; /** * The Y translation from the coordinate space of the * Raster's SampleModel to that of the Raster. */ protected int sampleModelTranslateY; /** The number of bands in the Raster. */ protected int numBands; /** The number of DataBuffer data elements per pixel. */ protected int numDataElements; /** The parent of this Raster, or null. */ protected Raster parent; static private native void initIDs(); static { ColorModel.loadLibraries(); initIDs(); } /** * Creates a Raster based on a PixelInterleavedSampleModel with the * specified data type, width, height, and number of bands. * * <p> The upper left corner of the Raster is given by the * location argument. If location is null, (0, 0) will be used. * The dataType parameter should be one of the enumerated values * defined in the DataBuffer class. * * <p> Note that interleaved <code>DataBuffer.TYPE_INT</code> * Rasters are not supported. To create a 1-band Raster of type * <code>DataBuffer.TYPE_INT</code>, use * Raster.createPackedRaster(). * <p> The only dataTypes supported currently are TYPE_BYTE * and TYPE_USHORT. * @param dataType the data type for storing samples * @param w the width in pixels of the image data * @param h the height in pixels of the image data * @param bands the number of bands * @param location the upper-left corner of the <code>Raster</code> * @return a WritableRaster object with the specified data type, * width, height and number of bands. * @throws RasterFormatException if <code>w</code> or <code>h</code> * is less than or equal to zero, or computing either * <code>location.x + w</code> or * <code>location.y + h</code> results in integer * overflow */ public static WritableRaster createInterleavedRaster(int dataType, int w, int h, int bands, Point location) { int[] bandOffsets = new int[bands]; for (int i = 0; i < bands; i++) { bandOffsets[i] = i; } return createInterleavedRaster(dataType, w, h, w*bands, bands, bandOffsets, location); } /** * Creates a Raster based on a PixelInterleavedSampleModel with the * specified data type, width, height, scanline stride, pixel * stride, and band offsets. The number of bands is inferred from * bandOffsets.length. * * <p> The upper left corner of the Raster is given by the * location argument. If location is null, (0, 0) will be used. * The dataType parameter should be one of the enumerated values * defined in the DataBuffer class. * * <p> Note that interleaved <code>DataBuffer.TYPE_INT</code> * Rasters are not supported. To create a 1-band Raster of type * <code>DataBuffer.TYPE_INT</code>, use * Raster.createPackedRaster(). * <p> The only dataTypes supported currently are TYPE_BYTE * and TYPE_USHORT. * @param dataType the data type for storing samples * @param w the width in pixels of the image data * @param h the height in pixels of the image data * @param scanlineStride the line stride of the image data * @param pixelStride the pixel stride of the image data * @param bandOffsets the offsets of all bands * @param location the upper-left corner of the <code>Raster</code> * @return a WritableRaster object with the specified data type, * width, height, scanline stride, pixel stride and band * offsets. * @throws RasterFormatException if <code>w</code> or <code>h</code> * is less than or equal to zero, or computing either * <code>location.x + w</code> or * <code>location.y + h</code> results in integer * overflow * @throws IllegalArgumentException if <code>dataType</code> is not * one of the supported data types, which are * <code>DataBuffer.TYPE_BYTE</code>, or * <code>DataBuffer.TYPE_USHORT</code>. */ public static WritableRaster createInterleavedRaster(int dataType, int w, int h, int scanlineStride, int pixelStride, int bandOffsets[], Point location) { DataBuffer d; int bands = bandOffsets.length; int maxBandOff = bandOffsets[0]; for (int i=1; i < bands; i++) { if (bandOffsets[i] > maxBandOff) { maxBandOff = bandOffsets[i]; } } int size = maxBandOff + scanlineStride*(h-1) + pixelStride*(w-1) + 1; switch(dataType) { case DataBuffer.TYPE_BYTE: d = new DataBufferByte(size); break; case DataBuffer.TYPE_USHORT: d = new DataBufferUShort(size); break; default: throw new IllegalArgumentException("Unsupported data type " + dataType); } return createInterleavedRaster(d, w, h, scanlineStride, pixelStride, bandOffsets, location); } /** * Creates a Raster based on a BandedSampleModel with the * specified data type, width, height, and number of bands. * * <p> The upper left corner of the Raster is given by the * location argument. If location is null, (0, 0) will be used. * The dataType parameter should be one of the enumerated values * defined in the DataBuffer class. * * <p> The only dataTypes supported currently are TYPE_BYTE, TYPE_USHORT, * and TYPE_INT. * @param dataType the data type for storing samples * @param w the width in pixels of the image data * @param h the height in pixels of the image data * @param bands the number of bands * @param location the upper-left corner of the <code>Raster</code> * @return a WritableRaster object with the specified data type, * width, height and number of bands. * @throws RasterFormatException if <code>w</code> or <code>h</code> * is less than or equal to zero, or computing either * <code>location.x + w</code> or * <code>location.y + h</code> results in integer * overflow * @throws ArrayIndexOutOfBoundsException if <code>bands</code> * is less than 1
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -