📄 bandedsamplemodel.java
字号:
* BandedSampleModel <code>bsm2</code>. * The transfer will generally be more efficient than using * getPixel/setPixel. * <pre> * BandedSampleModel bsm1, bsm2; * DataBufferInt db1, db2; * bsm2.setDataElements(x, y, bsm1.getDataElements(x, y, null, db1), * db2); * </pre> * Using getDataElements/setDataElements to transfer between two * DataBuffer/SampleModel pairs is legitimate if the SampleModels have * the same number of bands, corresponding bands have the same number of * bits per sample, and the TransferTypes are the same. * <p> * obj must be a primitive array of type TransferType. Otherwise, * a ClassCastException is thrown. An * ArrayIndexOutOfBoundsException may be thrown if the coordinates are * not in bounds, or if obj is not large enough to hold the pixel data. * @param x, y The coordinates of the pixel location * @param obj If non-null, returns the primitive array in this * object * @param data The DataBuffer containing the image data * @see #getDataElements(int, int, Object, DataBuffer) */ public void setDataElements(int x, int y, Object obj, DataBuffer data) { if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) { throw new ArrayIndexOutOfBoundsException ("Coordinate out of bounds!"); } int type = getTransferType(); int numDataElems = getNumDataElements(); int pixelOffset = y*scanlineStride + x; switch(type) { case DataBuffer.TYPE_BYTE: byte[] barray = (byte[])obj; for (int i=0; i<numDataElems; i++) { data.setElem(bankIndices[i], pixelOffset + bandOffsets[i], barray[i] & 0xff); } break; case DataBuffer.TYPE_USHORT: case DataBuffer.TYPE_SHORT: short[] sarray = (short[])obj; for (int i=0; i<numDataElems; i++) { data.setElem(bankIndices[i], pixelOffset + bandOffsets[i], sarray[i] & 0xffff); } break; case DataBuffer.TYPE_INT: int[] iarray = (int[])obj; for (int i=0; i<numDataElems; i++) { data.setElem(bankIndices[i], pixelOffset + bandOffsets[i], iarray[i]); } break; case DataBuffer.TYPE_FLOAT: float[] farray = (float[])obj; for (int i=0; i<numDataElems; i++) { data.setElemFloat(bankIndices[i], pixelOffset + bandOffsets[i], farray[i]); } break; case DataBuffer.TYPE_DOUBLE: double[] darray = (double[])obj; for (int i=0; i<numDataElems; i++) { data.setElemDouble(bankIndices[i], pixelOffset + bandOffsets[i], darray[i]); } break; } } /** * Sets a pixel in the DataBuffer using an int array of samples for input. * ArrayIndexOutOfBoundsException may be thrown if the coordinates are * not in bounds. * @param x, y The coordinates of the pixel location * @param iArray The input samples in an int array * @param data The DataBuffer containing the image data * @see #getPixel(int, int, int[], DataBuffer) */ public void setPixel(int x, int y, int iArray[], DataBuffer data) { if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) { throw new ArrayIndexOutOfBoundsException ("Coordinate out of bounds!"); } int pixelOffset = y*scanlineStride + x; for (int i=0; i<numBands; i++) { data.setElem(bankIndices[i], pixelOffset + bandOffsets[i], iArray[i]); } } /** * Sets all samples for a rectangle of pixels from an int array containing * one sample per array element. * ArrayIndexOutOfBoundsException may be thrown if the coordinates are * not in bounds. * @param x, y The coordinates of the upper left pixel location * @param w The width of the pixel rectangle * @param h The height of the pixel rectangle * @param iArray The input samples in an int array * @param data The DataBuffer containing the image data * @see #getPixels(int, int, int, int, int[], DataBuffer) */ public void setPixels(int x, int y, int w, int h, int iArray[], DataBuffer data) { if ((x < 0) || (y < 0) || (x + w > width) || (y + h > height)) { throw new ArrayIndexOutOfBoundsException ("Coordinate out of bounds!"); } for (int k = 0; k < numBands; k++) { int lineOffset = y*scanlineStride + x + bandOffsets[k]; int srcOffset = k; int bank = bankIndices[k]; for (int i = 0; i < h; i++) { int pixelOffset = lineOffset; for (int j = 0; j < w; j++) { data.setElem(bank, pixelOffset++, iArray[srcOffset]); srcOffset += numBands; } lineOffset += scanlineStride; } } } /** * Sets a sample in the specified band for the pixel located at (x,y) * in the DataBuffer using an int for input. * ArrayIndexOutOfBoundsException may be thrown if the coordinates are * not in bounds. * @param x, y The coordinates of the pixel location * @param b The band to set * @param s The input sample as an int * @param data The DataBuffer containing the image data * @see #getSample(int, int, int, DataBuffer) */ public void setSample(int x, int y, int b, int s, DataBuffer data) { // Bounds check for 'b' will be performed automatically if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) { throw new ArrayIndexOutOfBoundsException ("Coordinate out of bounds!"); } data.setElem(bankIndices[b], y*scanlineStride + x + bandOffsets[b], s); } /** * Sets a sample in the specified band for the pixel located at (x,y) * in the DataBuffer using a float for input. * ArrayIndexOutOfBoundsException may be thrown if the coordinates are * not in bounds. * @param x, y The coordinates of the pixel location * @param b The band to set * @param s The input sample as a float * @param data The DataBuffer containing the image data * @see #getSample(int, int, int, DataBuffer) */ public void setSample(int x, int y, int b, float s , DataBuffer data) { // Bounds check for 'b' will be performed automatically if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) { throw new ArrayIndexOutOfBoundsException ("Coordinate out of bounds!"); } data.setElemFloat(bankIndices[b], y*scanlineStride + x + bandOffsets[b], s); } /** * Sets a sample in the specified band for the pixel located at (x,y) * in the DataBuffer using a double for input. * ArrayIndexOutOfBoundsException may be thrown if the coordinates are * not in bounds. * @param x, y The coordinates of the pixel location * @param b The band to set * @param s The input sample as a double * @param data The DataBuffer containing the image data * @see #getSample(int, int, int, DataBuffer) */ public void setSample(int x, int y, int b, double s, DataBuffer data) { // Bounds check for 'b' will be performed automatically if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) { throw new ArrayIndexOutOfBoundsException ("Coordinate out of bounds!"); } data.setElemDouble(bankIndices[b], y*scanlineStride + x + bandOffsets[b], s); } /** * Sets the samples in the specified band for the specified rectangle * of pixels from an int array containing one sample per data array element. * ArrayIndexOutOfBoundsException may be thrown if the coordinates are * not in bounds. * @param x, y The coordinates of the upper left pixel location * @param w The width of the pixel rectangle * @param h The height of the pixel rectangle * @param b The band to set * @param iArray The input sample array * @param data The DataBuffer containing the image data * @see #getSamples(int, int, int, int, int, int[], DataBuffer) */ public void setSamples(int x, int y, int w, int h, int b, int iArray[], DataBuffer data) { // Bounds check for 'b' will be performed automatically if ((x < 0) || (y < 0) || (x + w > width) || (y + h > height)) { throw new ArrayIndexOutOfBoundsException ("Coordinate out of bounds!"); } int lineOffset = y*scanlineStride + x + bandOffsets[b]; int srcOffset = 0; int bank = bankIndices[b]; for (int i = 0; i < h; i++) { int sampleOffset = lineOffset; for (int j = 0; j < w; j++) { data.setElem(bank, sampleOffset++, iArray[srcOffset++]); } lineOffset += scanlineStride; } } private static int[] createOffsetArray(int numBands) { int[] bandOffsets = new int[numBands]; for (int i=0; i < numBands; i++) { bandOffsets[i] = 0; } return bandOffsets; } private static int[] createIndicesArray(int numBands) { int[] bankIndices = new int[numBands]; for (int i=0; i < numBands; i++) { bankIndices[i] = i; } return bankIndices; } // Differentiate hash code from other ComponentSampleModel subclasses public int hashCode() { return super.hashCode() ^ 0x2; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -