📄 writableraster.java
字号:
* primitive array of type TransferType. For image data supported by * the Java 2D(tm) API, this will be one of DataBuffer.TYPE_BYTE, * DataBuffer.TYPE_USHORT, DataBuffer.TYPE_INT, DataBuffer.TYPE_SHORT, * DataBuffer.TYPE_FLOAT, or DataBuffer.TYPE_DOUBLE. Data in the array * may be in a packed format, thus increasing efficiency for data * transfers. * An ArrayIndexOutOfBoundsException may be thrown if the coordinates are * not in bounds, or if inData is not large enough to hold the pixel data. * However, explicit bounds checking is not guaranteed. * A ClassCastException will be thrown if the input object is not null * and references anything other than an array of TransferType. * @see java.awt.image.SampleModel#setDataElements(int, int, Object, DataBuffer) * @param x The X coordinate of the pixel location. * @param y The Y coordinate of the pixel location. * @param inData An object reference to an array of type defined by * getTransferType() and length getNumDataElements() * containing the pixel data to place at x,y. * * @throws ArrayIndexOutOfBoundsException if the coordinates are not * in bounds, or if inData is too small to hold the input. */ public void setDataElements(int x, int y, Object inData) { sampleModel.setDataElements(x-sampleModelTranslateX, y-sampleModelTranslateY, inData, dataBuffer); } /** * Sets the data for a rectangle of pixels from an input Raster. * The input Raster must be compatible with this WritableRaster * in that they must have the same number of bands, corresponding bands * must have the same number of bits per sample, the TransferTypes * and NumDataElements must be the same, and the packing used by * the getDataElements/setDataElements must be identical. * An ArrayIndexOutOfBoundsException may be thrown if the coordinates are * not in bounds. * However, explicit bounds checking is not guaranteed. * @param x The X coordinate of the pixel location. * @param y The Y coordinate of the pixel location. * @param inRaster Raster containing data to place at x,y. * * @throws NullPointerException if inRaster is null. * @throws ArrayIndexOutOfBoundsException if the coordinates are not * in bounds. */ public void setDataElements(int x, int y, Raster inRaster) { int dstOffX = x+inRaster.getMinX(); int dstOffY = y+inRaster.getMinY(); int width = inRaster.getWidth(); int height = inRaster.getHeight(); if ((dstOffX < this.minX) || (dstOffY < this.minY) || (dstOffX + width > this.minX + this.width) || (dstOffY + height > this.minY + this.height)) { throw new ArrayIndexOutOfBoundsException ("Coordinate out of bounds!"); } int srcOffX = inRaster.getMinX(); int srcOffY = inRaster.getMinY(); Object tdata = null; for (int startY=0; startY < height; startY++) { tdata = inRaster.getDataElements(srcOffX, srcOffY+startY, width, 1, tdata); setDataElements(dstOffX, dstOffY+startY, width, 1, tdata); } } /** * Sets the data for a rectangle of pixels from a * primitive array of type TransferType. For image data supported by * the Java 2D API, this will be one of DataBuffer.TYPE_BYTE, * DataBuffer.TYPE_USHORT, DataBuffer.TYPE_INT, DataBuffer.TYPE_SHORT, * DataBuffer.TYPE_FLOAT, or DataBuffer.TYPE_DOUBLE. Data in the array * may be in a packed format, thus increasing efficiency for data * transfers. * An ArrayIndexOutOfBoundsException may be thrown if the coordinates are * not in bounds, or if inData is not large enough to hold the pixel data. * However, explicit bounds checking is not guaranteed. * A ClassCastException will be thrown if the input object is not null * and references anything other than an array of TransferType. * @see java.awt.image.SampleModel#setDataElements(int, int, int, int, Object, DataBuffer) * @param x The X coordinate of the upper left pixel location. * @param y The Y coordinate of the upper left pixel location. * @param w Width of the pixel rectangle. * @param h Height of the pixel rectangle. * @param inData An object reference to an array of type defined by * getTransferType() and length w*h*getNumDataElements() * containing the pixel data to place between x,y and * x+w-1, y+h-1. * * @throws NullPointerException if inData is null. * @throws ArrayIndexOutOfBoundsException if the coordinates are not * in bounds, or if inData is too small to hold the input. */ public void setDataElements(int x, int y, int w, int h, Object inData) { sampleModel.setDataElements(x-sampleModelTranslateX, y-sampleModelTranslateY, w,h,inData,dataBuffer); } /** * Copies pixels from Raster srcRaster to this WritableRaster. Each pixel * in srcRaster is copied to the same x,y address in this raster, unless * the address falls outside the bounds of this raster. srcRaster * must have the same number of bands as this WritableRaster. The * copy is a simple copy of source samples to the corresponding destination * samples. * <p> * If all samples of both source and destination Rasters are of * integral type and less than or equal to 32 bits in size, then calling * this method is equivalent to executing the following code for all * <code>x,y</code> addresses valid in both Rasters. * <pre> * Raster srcRaster; * WritableRaster dstRaster; * for (int b = 0; b < srcRaster.getNumBands(); b++) { * dstRaster.setSample(x, y, b, srcRaster.getSample(x, y, b)); * } * </pre> * Thus, when copying an integral type source to an integral type * destination, if the source sample size is greater than the destination * sample size for a particular band, the high order bits of the source * sample are truncated. If the source sample size is less than the * destination size for a particular band, the high order bits of the * destination are zero-extended or sign-extended depending on whether * srcRaster's SampleModel treats the sample as a signed or unsigned * quantity. * <p> * When copying a float or double source to an integral type destination, * each source sample is cast to the destination type. When copying an * integral type source to a float or double destination, the source * is first converted to a 32-bit int (if necessary), using the above * rules for integral types, and then the int is cast to float or * double. * <p> * @param srcRaster The Raster from which to copy pixels. * * @throws NullPointerException if srcRaster is null. */ public void setRect(Raster srcRaster) { setRect(0,0,srcRaster); } /** * Copies pixels from Raster srcRaster to this WritableRaster. * For each (x, y) address in srcRaster, the corresponding pixel * is copied to address (x+dx, y+dy) in this WritableRaster, * unless (x+dx, y+dy) falls outside the bounds of this raster. * srcRaster must have the same number of bands as this WritableRaster. * The copy is a simple copy of source samples to the corresponding * destination samples. For details, see * {@link WritableRaster#setRect(Raster)}. * * @param dx The X translation factor from src space to dst space * of the copy. * @param dy The Y translation factor from src space to dst space * of the copy. * @param srcRaster The Raster from which to copy pixels. * * @throws NullPointerException if srcRaster is null. */ public void setRect(int dx, int dy, Raster srcRaster) { int width = srcRaster.getWidth(); int height = srcRaster.getHeight(); int srcOffX = srcRaster.getMinX(); int srcOffY = srcRaster.getMinY(); int dstOffX = dx+srcOffX; int dstOffY = dy+srcOffY; // Clip to this raster if (dstOffX < this.minX) { int skipX = this.minX - dstOffX; width -= skipX; srcOffX += skipX; dstOffX = this.minX; } if (dstOffY < this.minY) { int skipY = this.minY - dstOffY; height -= skipY; srcOffY += skipY; dstOffY = this.minY; } if (dstOffX+width > this.minX+this.width) { width = this.minX + this.width - dstOffX; } if (dstOffY+height > this.minY+this.height) { height = this.minY + this.height - dstOffY; } if (width <= 0 || height <= 0) { return; } switch (srcRaster.getSampleModel().getDataType()) { case DataBuffer.TYPE_BYTE: case DataBuffer.TYPE_SHORT: case DataBuffer.TYPE_USHORT: case DataBuffer.TYPE_INT: int[] iData = null; for (int startY=0; startY < height; startY++) { // Grab one scanline at a time iData = srcRaster.getPixels(srcOffX, srcOffY+startY, width, 1, iData); setPixels(dstOffX, dstOffY+startY, width, 1, iData); } break; case DataBuffer.TYPE_FLOAT: float[] fData = null; for (int startY=0; startY < height; startY++) { fData = srcRaster.getPixels(srcOffX, srcOffY+startY, width, 1, fData); setPixels(dstOffX, dstOffY+startY, width, 1, fData); } break; case DataBuffer.TYPE_DOUBLE: double[] dData = null; for (int startY=0; startY < height; startY++) { // Grab one scanline at a time dData = srcRaster.getPixels(srcOffX, srcOffY+startY, width, 1, dData); setPixels(dstOffX, dstOffY+startY, width, 1, dData); } break; } } /** * Sets a pixel in the DataBuffer using an int array of samples for input. * An ArrayIndexOutOfBoundsException may be thrown if the coordinates are * not in bounds. * However, explicit bounds checking is not guaranteed. * @param x The X coordinate of the pixel location. * @param y The Y coordinate of the pixel location. * @param iArray The input samples in a int array. *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -