⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 componentsamplemodel.java

📁 JAVA基本类源代码,大家可以学习学习!
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        // Bounds check for 'b' will be performed automatically        if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {            throw new ArrayIndexOutOfBoundsException                ("Coordinate out of bounds!");        }        float sample = data.getElemFloat(bankIndices[b],                                         y*scanlineStride + x*pixelStride +                                         bandOffsets[b]);        return sample;    }    /**     * Returns the sample in a specified band     * for a pixel located at (x,y) as a double.     * An <code>ArrayIndexOutOfBoundsException</code> might be      * thrown if the coordinates are not in bounds.     * @param x,&nbsp;y The coordinates of the pixel location     * @param b         The band to return     * @param data      The DataBuffer containing the image data     * @return a double value representing the sample in the specified     * band for the specified pixel.     */    public double getSampleDouble(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!");        }        double sample = data.getElemDouble(bankIndices[b],                                           y*scanlineStride + x*pixelStride +                                           bandOffsets[b]);	return sample;    }    /**     * Returns the samples in a specified band for the specified rectangle     * of pixels in an int array, one sample per data array element.     * An <code>ArrayIndexOutOfBoundsException</code> might be thrown if      * the coordinates are not in bounds.     * @param x,&nbsp;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 return     * @param iArray    if non-<code>null</code>, returns the samples     *                  in this array     * @param data      the <code>DataBuffer</code> containing the image data     * @return the samples in the specified band of the specified pixel     * @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*pixelStride +  bandOffsets[b];        int srcOffset = 0;        for (int i = 0; i < h; i++) {           int sampleOffset = lineOffset;           for (int j = 0; j < w; j++) {              samples[srcOffset++] = data.getElem(bankIndices[b],                                                  sampleOffset);              sampleOffset += pixelStride;           }           lineOffset += scanlineStride;        }        return samples;    }    /**      * Sets the data for a single pixel in the specified      * <code>DataBuffer</code> from a primitive array of type      * <code>TransferType</code>.  For a <code>ComponentSampleModel</code>,     * this is the same as the data type, and samples are transferred     * one per array element.     * <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>     * A <code>ClassCastException</code> is thrown if <code>obj</code> is not     * a primitive array of type <code>TransferType</code>.     * An <code>ArrayIndexOutOfBoundsException</code> might be thrown if     * the coordinates are not in bounds, or if <code>obj</code> is not large      * enough to hold the pixel data.     * @param x,&nbsp;y the coordinates 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();	int numDataElems = getNumDataElements();	int pixelOffset = y*scanlineStride + x*pixelStride;	switch(type) {	case DataBuffer.TYPE_BYTE:	    byte[] barray = (byte[])obj;	    for (int i=0; i<numDataElems; i++) {		data.setElem(bankIndices[i], pixelOffset + bandOffsets[i],			   ((int)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],			   ((int)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 <code>DataBuffer</code> using an int array of      * samples for input.  An <code>ArrayIndexOutOfBoundsException</code>     * might be thrown if the coordinates are     * not in bounds.     * @param x,&nbsp;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*pixelStride;       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.     * An <code>ArrayIndexOutOfBoundsException</code> might be thrown if the      * coordinates are not in bounds.     * @param x,&nbsp;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!");        }        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++) {                 data.setElem(bankIndices[k], pixelOffset + bandOffsets[k],                              iArray[srcOffset++]);              }              pixelOffset += pixelStride;           }           lineOffset += scanlineStride;        }    }    /**     * Sets a sample in the specified band for the pixel located at (x,y)     * in the <code>DataBuffer</code> using an int for input.     * An <code>ArrayIndexOutOfBoundsException</code> might be thrown if the      * coordinates are not in bounds.     * @param x,&nbsp;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*pixelStride + bandOffsets[b], s);    }    /**     * Sets a sample in the specified band for the pixel located at (x,y)     * in the <code>DataBuffer</code> using a float for input.     * An <code>ArrayIndexOutOfBoundsException</code> might be thrown if      * the coordinates are not in bounds.     * @param x,&nbsp;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*pixelStride + bandOffsets[b],                          s);    }    /**     * Sets a sample in the specified band for the pixel located at (x,y)     * in the <code>DataBuffer</code> using a double for input.     * An <code>ArrayIndexOutOfBoundsException</code> might be thrown if      * the coordinates are not in bounds.     * @param x,&nbsp;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*pixelStride + 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.     * An <code>ArrayIndexOutOfBoundsException</code> might be thrown if the      * coordinates are not in bounds.     * @param x,&nbsp;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 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*pixelStride + bandOffsets[b];        int srcOffset = 0;        for (int i = 0; i < h; i++) {           int sampleOffset = lineOffset;           for (int j = 0; j < w; j++) {              data.setElem(bankIndices[b], sampleOffset, iArray[srcOffset++]);              sampleOffset += pixelStride;           }           lineOffset += scanlineStride;        }    }    public boolean equals(Object o) {        if ((o == null) || !(o instanceof ComponentSampleModel)) {            return false;        }        ComponentSampleModel that = (ComponentSampleModel)o;        return this.width == that.width &&            this.height == that.height &&            this.numBands == that.numBands &&            this.dataType == that.dataType &&            Arrays.equals(this.bandOffsets, that.bandOffsets) &&            Arrays.equals(this.bankIndices, that.bankIndices) &&            this.numBands == that.numBands &&            this.numBanks == that.numBanks &&            this.scanlineStride == that.scanlineStride &&            this.pixelStride == that.pixelStride;    }    // 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 < bandOffsets.length; i++) {            hash ^= bandOffsets[i];            hash <<= 8;        }        for (int i = 0; i < bankIndices.length; i++) {            hash ^= bankIndices[i];            hash <<= 8;        }        hash ^= numBands;        hash <<= 8;        hash ^= numBanks;        hash <<= 8;        hash ^= scanlineStride;        hash <<= 8;        hash ^= pixelStride;        return hash;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -