multipixelpackedsamplemodel.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 598 行 · 第 1/2 页

JAVA
598
字号
	 */
	public void setSample(int x, int y, int b, int s, DataBuffer data) {
		// 'b' must be 0
		if ((x < 0) || (y < 0) || (x >= width) || (y >= height) || (b != 0)) {
			throw new ArrayIndexOutOfBoundsException("Coordinate out of bounds!");
		}
		int bitnum = dataBitOffset + x * pixelBitStride;
		int index = y * scanlineStride + (bitnum / dataElementSize);
		int shift = dataElementSize - (bitnum & (dataElementSize - 1)) - pixelBitStride;
		int element = data.getElem(index);
		element &= ~(bitMask << shift);
		element |= (s & bitMask) << shift;
		data.setElem(index, element);
	}

	/** 
	 * Returns data for a single pixel in a primitive array of type
	 * TransferType.  For a <code>MultiPixelPackedSampleModel</code>, 
	 * the array has one element, and the type is the smallest of
	 * DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT, or DataBuffer.TYPE_INT
	 * that can hold a single pixel.  Generally, <code>obj</code>
	 * should be passed in as <code>null</code>, so that the 
	 * <code>Object</code> is created automatically and is the
	 * correct 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>MultiPixelPackedSampleModel</code> 
	 * <code>mppsm1</code>, to <code>DataBuffer</code> <code>db2</code>,
	 * whose storage layout is described by
	 * <code>MultiPixelPackedSampleModel</code> <code>mppsm2</code>.
	 * The transfer is generally more efficient than using
	 * <code>getPixel</code> or <code>setPixel</code>.
	 * <pre>
	 * 	     MultiPixelPackedSampleModel mppsm1, mppsm2;
	 *	     DataBufferInt db1, db2;
	 * 	     mppsm2.setDataElements(x, y, mppsm1.getDataElements(x, y, null,
	 *                              db1), db2);
	 * </pre>
	 * Using <code>getDataElements</code> or <code>setDataElements</code> 
	 * to transfer between two <code>DataBuffer/SampleModel</code> pairs
	 * is legitimate if the <code>SampleModels</code> have the same number
	 * of bands, corresponding bands have the same number of
	 * bits per sample, and the TransferTypes are the same.
	 * <p>
	 * If <code>obj</code> is not <code>null</code>, it should be a
	 * primitive array of type TransferType.  Otherwise, a 
	 * <code>ClassCastException</code> is thrown.  An
	 * <code>ArrayIndexOutOfBoundsException</code> is 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,&nbsp;y coordinates of the pixel location.
	 * @param obj a primitive array in which to return the pixel data or
	 *		<code>null</code>.
	 * @param data the <code>DataBuffer</code> containing the image data.
	 * @return an <code>Object</code> containing data for the specified
	 *	pixel.
	 * @exception ClassCastException if <code>obj</code> is not a 
	 *	primitive array of type TransferType or is not <code>null</code>
	 * @exception ArrayIndexOutOfBoundsException if the coordinates are
	 * not in bounds, or if <code>obj</code> is not <code>null</code> or 
	 * not large enough to hold the pixel data
	 * @see #setDataElements(int, int, Object, DataBuffer)
	 */
	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 bitnum = dataBitOffset + x * pixelBitStride;
		int shift = dataElementSize - (bitnum & (dataElementSize - 1)) - pixelBitStride;
		int element = 0;

		switch (type) {

			case DataBuffer.TYPE_BYTE :

				byte[] bdata;

				if (obj == null)
					bdata = new byte[1];
				else
					bdata = (byte[]) obj;

				element = data.getElem(y * scanlineStride + bitnum / dataElementSize);
				bdata[0] = (byte) ((element >> shift) & bitMask);

				obj = bdata;
				break;

			case DataBuffer.TYPE_USHORT :

				short[] sdata;

				if (obj == null)
					sdata = new short[1];
				else
					sdata = (short[]) obj;

				element = data.getElem(y * scanlineStride + bitnum / dataElementSize);
				sdata[0] = (short) ((element >> shift) & bitMask);

				obj = sdata;
				break;

			case DataBuffer.TYPE_INT :

				int[] idata;

				if (obj == null)
					idata = new int[1];
				else
					idata = (int[]) obj;

				element = data.getElem(y * scanlineStride + bitnum / dataElementSize);
				idata[0] = (element >> shift) & bitMask;

				obj = idata;
				break;
		}

		return obj;
	}

	/**
	 * Returns the specified single band pixel in the first element
	 * of an <code>int</code> array.
	 * <code>ArrayIndexOutOfBoundsException</code> is thrown if the
	 * coordinates are not in bounds.
	 * @param x,&nbsp;y the coordinates of the pixel location
	 * @param iArray the array containing the pixel to be returned or
	 * 	<code>null</code>
	 * @param data the <code>DataBuffer</code> where image data is stored
	 * @return an array containing the specified pixel.
	 * @exception ArrayIndexOutOfBoundsException if the coordinates
	 *	are not in bounds
	 * @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 = iArray;
		} else {
			pixels = new int[numBands];
		}
		int bitnum = dataBitOffset + x * pixelBitStride;
		int element = data.getElem(y * scanlineStride + bitnum / dataElementSize);
		int shift = dataElementSize - (bitnum & (dataElementSize - 1)) - pixelBitStride;
		pixels[0] = (element >> shift) & bitMask;
		return pixels;
	}

	/** 
	 * Sets the data for a single pixel in the specified 
	 * <code>DataBuffer</code> from a primitive array of type
	 * TransferType.  For a <code>MultiPixelPackedSampleModel</code>,
	 * only the first element of the array holds valid data,
	 * and the type must be the smallest of
	 * DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT, or DataBuffer.TYPE_INT
	 * that can hold a single pixel.
	 * <p>
	 * The following code illustrates transferring data for one pixel from
	 * <code>DataBuffer</code> <code>db1</code>, whose storage layout is
	 * described by <code>MultiPixelPackedSampleModel</code> 
	 * <code>mppsm1</code>, to <code>DataBuffer</code> <code>db2</code>,
	 * whose storage layout is described by
	 * <code>MultiPixelPackedSampleModel</code> <code>mppsm2</code>.
	 * The transfer is generally more efficient than using
	 * <code>getPixel</code> or <code>setPixel</code>.
	 * <pre>
	 * 	     MultiPixelPackedSampleModel mppsm1, mppsm2;
	 *	     DataBufferInt db1, db2;
	 * 	     mppsm2.setDataElements(x, y, mppsm1.getDataElements(x, y, null,
	 *                              db1), db2);
	 * </pre>
	 * Using <code>getDataElements</code> or <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 TransferTypes are the same.
	 * <p>
	 * <code>obj</code> must be a primitive array of type TransferType.
	 * Otherwise, a <code>ClassCastException</code> is thrown.  An
	 * <code>ArrayIndexOutOfBoundsException</code> is 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 <code>DataBuffer</code> 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 bitnum = dataBitOffset + x * pixelBitStride;
		int index = y * scanlineStride + (bitnum / dataElementSize);
		int shift = dataElementSize - (bitnum & (dataElementSize - 1)) - pixelBitStride;
		int element = data.getElem(index);
		element &= ~(bitMask << shift);

		switch (type) {

			case DataBuffer.TYPE_BYTE :

				byte[] barray = (byte[]) obj;
				element |= (((barray[0]) & 0xff) & bitMask) << shift;
				data.setElem(index, element);
				break;

			case DataBuffer.TYPE_USHORT :

				short[] sarray = (short[]) obj;
				element |= (((sarray[0]) & 0xffff) & bitMask) << shift;
				data.setElem(index, element);
				break;

			case DataBuffer.TYPE_INT :

				int[] iarray = (int[]) obj;
				element |= (iarray[0] & bitMask) << shift;
				data.setElem(index, element);
				break;
		}
	}

	/**
	 * Sets a pixel in the <code>DataBuffer</code> using an
	 * <code>int</code> array for input.
	 * <code>ArrayIndexOutOfBoundsException</code> is thrown if
	 * the coordinates are not in bounds.
	 * @param x,&nbsp;y the coordinates of the pixel location
	 * @param iArray the input pixel in an <code>int</code> array
	 * @param data the <code>DataBuffer</code> 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 bitnum = dataBitOffset + x * pixelBitStride;
		int index = y * scanlineStride + (bitnum / dataElementSize);
		int shift = dataElementSize - (bitnum & (dataElementSize - 1)) - pixelBitStride;
		int element = data.getElem(index);
		element &= ~(bitMask << shift);
		element |= (iArray[0] & bitMask) << shift;
		data.setElem(index, element);
	}

	public boolean equals(Object o) {
		if ((o == null) || !(o instanceof MultiPixelPackedSampleModel)) {
			return false;
		}

		MultiPixelPackedSampleModel that = (MultiPixelPackedSampleModel) o;
		return this.width == that.width
			&& this.height == that.height
			&& this.numBands == that.numBands
			&& this.dataType == that.dataType
			&& this.pixelBitStride == that.pixelBitStride
			&& this.bitMask == that.bitMask
			&& this.pixelsPerDataElement == that.pixelsPerDataElement
			&& this.dataElementSize == that.dataElementSize
			&& this.dataBitOffset == that.dataBitOffset
			&& 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;
		hash ^= pixelBitStride;
		hash <<= 8;
		hash ^= bitMask;
		hash <<= 8;
		hash ^= pixelsPerDataElement;
		hash <<= 8;
		hash ^= dataElementSize;
		hash <<= 8;
		hash ^= dataBitOffset;
		hash <<= 8;
		hash ^= scanlineStride;
		return hash;
	}
}

⌨️ 快捷键说明

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