📄 componentsamplemodel.java
字号:
public DataBuffer createDataBuffer() { DataBuffer dataBuffer = null; int size = (int)getBufferSize(); switch (dataType) { case DataBuffer.TYPE_BYTE: dataBuffer = new DataBufferByte(size, numBanks); break; case DataBuffer.TYPE_USHORT: dataBuffer = new DataBufferUShort(size, numBanks); break; case DataBuffer.TYPE_SHORT: dataBuffer = new DataBufferShort(size, numBanks); break; case DataBuffer.TYPE_INT: dataBuffer = new DataBufferInt(size, numBanks); break; case DataBuffer.TYPE_FLOAT: dataBuffer = new DataBufferFloat(size, numBanks); break; case DataBuffer.TYPE_DOUBLE: dataBuffer = new DataBufferDouble(size, numBanks); break; } return dataBuffer; } /** Gets the offset for the first band of pixel (x,y). * A sample of the first band can be retrieved from a * <code>DataBuffer</code> * <code>data</code> with a <code>ComponentSampleModel</code> * <code>csm</code> as * <pre> * data.getElem(csm.getOffset(x, y)); * </pre> * @param x, y the location of the pixel * @return the offset for the first band of the specified pixel. */ public int getOffset(int x, int y) { int offset = y*scanlineStride + x*pixelStride + bandOffsets[0]; return offset; } /** Gets the offset for band b of pixel (x,y). * A sample of band <code>b</code> can be retrieved from a * <code>DataBuffer</code> <code>data</code> * with a <code>ComponentSampleModel</code> <code>csm</code> as * <pre> * data.getElem(csm.getOffset(x, y, b)); * </pre> * @param x, y the location of the specified pixel * @param b the specified band * @return the offset for the specified band of the specified pixel. */ public int getOffset(int x, int y, int b) { int offset = y*scanlineStride + x*pixelStride + bandOffsets[b]; return offset; } /** Returns the number of bits per sample for all bands. * @return an array containing the number of bits per sample * for all bands, where each element in the array * represents a band. */ public final int[] getSampleSize() { int sampleSize[] = new int [numBands]; int sizeInBits = getSampleSize(0); for (int i=0; i<numBands; i++) sampleSize[i] = sizeInBits; return sampleSize; } /** Returns the number of bits per sample for the specified band. * @param band the specified band * @return the number of bits per sample for the specified band. */ public final int getSampleSize(int band) { return DataBuffer.getDataTypeSize(dataType); } /** Returns the bank indices for all bands. * @return the bank indices for all bands. */ public final int [] getBankIndices() { return (int[]) bankIndices.clone(); } /** Returns the band offset for all bands. * @return the band offsets for all bands. */ public final int [] getBandOffsets() { return (int[])bandOffsets.clone(); } /** Returns the scanline stride of this ComponentSampleModel. * @return the scanline stride of this <code>ComponentSampleModel</code>. */ public final int getScanlineStride() { return scanlineStride; } /** Returns the pixel stride of this ComponentSampleModel. * @return the pixel stride of this <code>ComponentSampleModel</code>. */ public final int getPixelStride() { return pixelStride; } /** * Returns the number of data elements needed to transfer a pixel * with the * {@link #getDataElements(int, int, Object, DataBuffer) } and * {@link #setDataElements(int, int, Object, DataBuffer) } * methods. * For a <code>ComponentSampleModel</code>, this is identical to the * number of bands. * @return the number of data elements needed to transfer a pixel with * the <code>getDataElements</code> and * <code>setDataElements</code> methods. * @see java.awt.image.SampleModel#getNumDataElements * @see #getNumBands */ public final int getNumDataElements() { return getNumBands(); } /** * Returns data for a single pixel in a primitive array of type * <code>TransferType</code>. For a <code>ComponentSampleModel</code>, * this is the same as the data type, and samples are returned * one per array element. Generally, <code>obj</code> should * be passed in as <code>null</code>, so that the <code>Object</code> * is created automatically and is the right primitive data type. * <p> * The following code illustrates transferring data for one pixel from * <code>DataBuffer</code> <code>db1</code>, whose storage layout is * described by <code>ComponentSampleModel</code> <code>csm1</code>, * to <code>DataBuffer</code> <code>db2</code>, whose storage layout * is described by <code>ComponentSampleModel</code> <code>csm2</code>. * The transfer is usually more efficient than using * <code>getPixel</code> and <code>setPixel</code>. * <pre> * ComponentSampleModel csm1, csm2; * DataBufferInt db1, db2; * csm2.setDataElements(x, y, * csm1.getDataElements(x, y, null, db1), db2); * </pre> * * Using <code>getDataElements</code> and <code>setDataElements</code> * to transfer between two <code>DataBuffer/SampleModel</code> * pairs is legitimate if the <code>SampleModel</code> objects have * the same number of bands, corresponding bands have the same number of * bits per sample, and the <code>TransferType</code>s are the same. * <p> * If <code>obj</code> is not <code>null</code>, it should be a * primitive array of type <code>TransferType</code>. * Otherwise, a <code>ClassCastException</code> is thrown. An * <code>ArrayIndexOutOfBoundsException</code> might be thrown if the * coordinates are not in bounds, or if <code>obj</code> is not * <code>null</code> and is not large enough to hold * the pixel data. * * @param x, y the coordinates of the pixel location * @param obj if non-<code>null</code>, a primitive array * in which to return the pixel data * @param data the <code>DataBuffer</code> containing the image data * @return the data of the specified pixel * @see #setDataElements(int, int, Object, DataBuffer) * * @throws NullPointerException if data is null. * @throws ArrayIndexOutOfBoundsException if the coordinates are * not in bounds, or if obj is too small to hold the ouput. */ public Object getDataElements(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*pixelStride; switch(type) { case DataBuffer.TYPE_BYTE: byte[] bdata; if (obj == null) bdata = new byte[numDataElems]; else bdata = (byte[])obj; for (int i=0; i<numDataElems; i++) { bdata[i] = (byte)data.getElem(bankIndices[i], pixelOffset + bandOffsets[i]); } obj = (Object)bdata; break; case DataBuffer.TYPE_USHORT: case DataBuffer.TYPE_SHORT: short[] sdata; if (obj == null) sdata = new short[numDataElems]; else sdata = (short[])obj; for (int i=0; i<numDataElems; i++) { sdata[i] = (short)data.getElem(bankIndices[i], pixelOffset + bandOffsets[i]); } obj = (Object)sdata; break; case DataBuffer.TYPE_INT: int[] idata; if (obj == null) idata = new int[numDataElems]; else idata = (int[])obj; for (int i=0; i<numDataElems; i++) { idata[i] = data.getElem(bankIndices[i], pixelOffset + bandOffsets[i]); } obj = (Object)idata; break; case DataBuffer.TYPE_FLOAT: float[] fdata; if (obj == null) fdata = new float[numDataElems]; else fdata = (float[])obj; for (int i=0; i<numDataElems; i++) { fdata[i] = data.getElemFloat(bankIndices[i], pixelOffset + bandOffsets[i]); } obj = (Object)fdata; break; case DataBuffer.TYPE_DOUBLE: double[] ddata; if (obj == null) ddata = new double[numDataElems]; else ddata = (double[])obj; for (int i=0; i<numDataElems; i++) { ddata[i] = data.getElemDouble(bankIndices[i], pixelOffset + bandOffsets[i]); } obj = (Object)ddata; break; } return obj; } /** * Returns all samples for the specified pixel in an int array, * one sample per array element. * An <code>ArrayIndexOutOfBoundsException</code> might be thrown if * the coordinates are not in bounds. * @param x, y The coordinates of the pixel location * @param iArray If non-null, returns the samples in this array * @param data The DataBuffer containing the image data * @return the samples of the specified pixel. * @see #setPixel(int, int, int[], DataBuffer) * * @throws NullPointerException if data is null. * @throws ArrayIndexOutOfBoundsException if the coordinates are * not in bounds, or if iArray is too small to hold the output. */ 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 = iArray; } else { pixels = new int [numBands]; } int pixelOffset = y*scanlineStride + x*pixelStride; for (int i=0; i<numBands; i++) { pixels[i] = data.getElem(bankIndices[i], pixelOffset + bandOffsets[i]); } return pixels; } /** * Returns all samples for the specified rectangle of pixels in * an int array, one sample per array element. * An <code>ArrayIndexOutOfBoundsException</code> might 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 If non-null, returns the samples in this array * @param data The DataBuffer containing the image data * @return the samples of the pixels within the specified region. * @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*pixelStride; int srcOffset = 0; for (int i = 0; i < h; i++) { int pixelOffset = lineOffset; for (int j = 0; j < w; j++) { for (int k=0; k < numBands; k++) { pixels[srcOffset++] = data.getElem(bankIndices[k], pixelOffset + bandOffsets[k]); } pixelOffset += pixelStride; } lineOffset += scanlineStride; } return pixels; } /** * Returns as int the sample in a specified band for the pixel * located at (x,y). * An <code>ArrayIndexOutOfBoundsException</code> might be thrown if * the coordinates are not in bounds. * @param x, y the coordinates of the pixel location * @param b the band to return * @param data the <code>DataBuffer</code> 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(bankIndices[b], y*scanlineStride + x*pixelStride + bandOffsets[b]); return sample; } /** * Returns the sample in a specified band * for the pixel located at (x,y) as a float. * An <code>ArrayIndexOutOfBoundsException</code> might be * thrown if the coordinates are not in bounds. * @param x, y The coordinates of the pixel location * @param b The band to return * @param data The DataBuffer containing the image data * @return a float value representing the sample in the specified * band for the specified pixel. */ public float getSampleFloat(int x, int y, int b, DataBuffer data) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -