📄 singlepixelpackedsamplemodel.java
字号:
case DataBuffer.TYPE_INT: int[] idata; if (obj == null) idata = new int[1]; else idata = (int[])obj; idata[0] = data.getElem(y * scanlineStride + x); obj = (Object)idata; break; } return obj; } /** * Returns all samples in for the specified pixel in an int array. * ArrayIndexOutOfBoundsException may be thrown if the coordinates are * not in bounds. * @param x The X coordinate of the pixel location. * @param y The Y coordinate of the pixel location. * @param iArray If non-null, returns the samples in this array * @param data The DataBuffer containing the image data. * @return all samples for the specified pixel. * @see #setPixel(int, int, int[], DataBuffer) */ public int [] getPixel(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 pixels[]; if (iArray == null) { pixels = new int [numBands]; } else { pixels = iArray; } int value = data.getElem(y * scanlineStride + x); for (int i=0; i<numBands; i++) { pixels[i] = (value & bitMasks[i]) >>> bitOffsets[i]; } return pixels; } /** * Returns all samples for the specified rectangle of pixels in * an int array, one sample per array element. * ArrayIndexOutOfBoundsException may be thrown if the coordinates are * not in bounds. * @param x The X coordinate of the upper left pixel location. * @param y The Y coordinate of the upper left pixel location. * @param w The width of the pixel rectangle. * @param h The height of the pixel rectangle. * @param iArray If non-null, returns the samples in this array. * @param data The DataBuffer containing the image data. * @return all samples for the specified region of pixels. * @see #setPixels(int, int, int, int, int[], DataBuffer) */ public int[] getPixels(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!"); } int pixels[]; if (iArray != null) { pixels = iArray; } else { pixels = new int [w*h*numBands]; } int lineOffset = y*scanlineStride + x; int dstOffset = 0; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { int value = data.getElem(lineOffset+j); for (int k=0; k < numBands; k++) { pixels[dstOffset++] = ((value & bitMasks[k]) >>> bitOffsets[k]); } } lineOffset += scanlineStride; } return pixels; } /** * Returns as int the sample in a specified band for the pixel * located at (x,y). * ArrayIndexOutOfBoundsException may be thrown if the coordinates are * not in bounds. * @param x The X coordinate of the pixel location. * @param y The Y coordinate of the pixel location. * @param b The band to return. * @param data The DataBuffer containing the image data. * @return the sample in a specified band for the specified * pixel. * @see #setSample(int, int, int, int, DataBuffer) */ public int getSample(int x, int y, int b, 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!"); } int sample = data.getElem(y * scanlineStride + x); return ((sample & bitMasks[b]) >>> bitOffsets[b]); } /** * Returns the samples for a specified band for the specified rectangle * of pixels in an int array, one sample per array element. * ArrayIndexOutOfBoundsException may be thrown if the coordinates are * not in bounds. * @param x The X coordinate of the upper left pixel location. * @param y The Y coordinate 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 return. * @param iArray If non-null, returns the samples in this array. * @param data The DataBuffer containing the image data. * @return the samples for the specified band for the specified * region of pixels. * @see #setSamples(int, int, int, int, int, int[], DataBuffer) */ public int[] getSamples(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 samples[]; if (iArray != null) { samples = iArray; } else { samples = new int [w*h]; } int lineOffset = y*scanlineStride + x; int dstOffset = 0; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { int value = data.getElem(lineOffset+j); samples[dstOffset++] = ((value & bitMasks[b]) >>> bitOffsets[b]); } lineOffset += scanlineStride; } return samples; } /** * Sets the data for a single pixel in the specified DataBuffer from a * primitive array of type TransferType. For a * SinglePixelPackedSampleModel, only the first element of the array * will hold valid data, and the type of the array must be the same as * the storage data type of the SinglePixelPackedSampleModel. * <p> * The following code illustrates transferring data for one pixel from * DataBuffer <code>db1</code>, whose storage layout is described by * SinglePixelPackedSampleModel <code>sppsm1</code>, * to DataBuffer <code>db2</code>, whose storage layout is described by * SinglePixelPackedSampleModel <code>sppsm2</code>. * The transfer will generally be more efficient than using * getPixel/setPixel. * <pre> * SinglePixelPackedSampleModel sppsm1, sppsm2; * DataBufferInt db1, db2; * sppsm2.setDataElements(x, y, sppsm1.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 The X coordinate of the pixel location. * @param y The Y coordinate of the pixel location. * @param obj A primitive array containing pixel data. * @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(); switch(type) { case DataBuffer.TYPE_BYTE: byte[] barray = (byte[])obj; data.setElem(y*scanlineStride+x, ((int)barray[0])&0xff); break; case DataBuffer.TYPE_USHORT: short[] sarray = (short[])obj; data.setElem(y*scanlineStride+x, ((int)sarray[0])&0xffff); break; case DataBuffer.TYPE_INT: int[] iarray = (int[])obj; data.setElem(y*scanlineStride+x, iarray[0]); 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 The X coordinate of the pixel location. * @param y The Y coordinate 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 lineOffset = y * scanlineStride + x; int value = data.getElem(lineOffset); for (int i=0; i < numBands; i++) { value &= ~bitMasks[i]; value |= ((iArray[i] << bitOffsets[i]) & bitMasks[i]); } data.setElem(lineOffset, value); } /** * 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 The X coordinate of the upper left pixel location. * @param y The Y coordinate 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!"); } int lineOffset = y*scanlineStride + x; int srcOffset = 0; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { int value = data.getElem(lineOffset+j); for (int k=0; k < numBands; k++) { value &= ~bitMasks[k]; int srcValue = iArray[srcOffset++]; value |= ((srcValue << bitOffsets[k]) & bitMasks[k]); } data.setElem(lineOffset+j, value); } 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 The X coordinate of the pixel location. * @param y The Y coordinate 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!"); } int value = data.getElem(y*scanlineStride + x); value &= ~bitMasks[b]; value |= (s << bitOffsets[b]) & bitMasks[b]; data.setElem(y*scanlineStride + x,value); } /** * Sets the samples in the specified band for the specified 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 The X coordinate of the upper left pixel location. * @param y The Y coordinate 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 samples in an int 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; int srcOffset = 0; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { int value = data.getElem(lineOffset+j); value &= ~bitMasks[b]; int sample = iArray[srcOffset++]; value |= ((int)sample << bitOffsets[b]) & bitMasks[b]; data.setElem(lineOffset+j,value); } lineOffset += scanlineStride; } } public boolean equals(Object o) { if ((o == null) || !(o instanceof SinglePixelPackedSampleModel)) { return false; } SinglePixelPackedSampleModel that = (SinglePixelPackedSampleModel)o; return this.width == that.width && this.height == that.height && this.numBands == that.numBands && this.dataType == that.dataType && Arrays.equals(this.bitMasks, that.bitMasks) && Arrays.equals(this.bitOffsets, that.bitOffsets) && Arrays.equals(this.bitSizes, that.bitSizes) && this.maxBitSize == that.maxBitSize && this.scanlineStride == that.scanlineStride; } // If we implement equals() we must also implement hashCode public int hashCode() { int hash = 0; hash = width; hash <<= 8; hash ^= height; hash <<= 8; hash ^= numBands; hash <<= 8; hash ^= dataType; hash <<= 8; for (int i = 0; i < bitMasks.length; i++) { hash ^= bitMasks[i]; hash <<= 8; } for (int i = 0; i < bitOffsets.length; i++) { hash ^= bitOffsets[i]; hash <<= 8; } for (int i = 0; i < bitSizes.length; i++) { hash ^= bitSizes[i]; hash <<= 8; } hash ^= maxBitSize; hash <<= 8; hash ^= scanlineStride; return hash; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -