290N/A/*
3909N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
290N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
290N/A *
290N/A * This code is free software; you can redistribute it and/or modify it
290N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
290N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
290N/A *
290N/A * This code is distributed in the hope that it will be useful, but WITHOUT
290N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
290N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
290N/A * version 2 for more details (a copy is included in the LICENSE file that
290N/A * accompanied this code).
290N/A *
290N/A * You should have received a copy of the GNU General Public License version
290N/A * 2 along with this work; if not, write to the Free Software Foundation,
290N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
290N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
290N/A */
290N/A
290N/A/* ****************************************************************
290N/A ******************************************************************
290N/A ******************************************************************
290N/A *** COPYRIGHT (c) Eastman Kodak Company, 1997
290N/A *** As an unpublished work pursuant to Title 17 of the United
290N/A *** States Code. All rights reserved.
290N/A ******************************************************************
290N/A ******************************************************************
290N/A ******************************************************************/
290N/A
290N/Apackage java.awt.image;
290N/A
290N/Aimport java.util.Arrays;
290N/A
290N/A/**
290N/A * This class represents pixel data packed such that the N samples which make
290N/A * up a single pixel are stored in a single data array element, and each data
290N/A * data array element holds samples for only one pixel.
290N/A * This class supports
290N/A * {@link DataBuffer#TYPE_BYTE TYPE_BYTE},
290N/A * {@link DataBuffer#TYPE_USHORT TYPE_USHORT},
290N/A * {@link DataBuffer#TYPE_INT TYPE_INT} data types.
290N/A * All data array elements reside
290N/A * in the first bank of a DataBuffer. Accessor methods are provided so
290N/A * that the image data can be manipulated directly. Scanline stride is the
290N/A * number of data array elements between a given sample and the corresponding
290N/A * sample in the same column of the next scanline. Bit masks are the masks
290N/A * required to extract the samples representing the bands of the pixel.
290N/A * Bit offsets are the offsets in bits into the data array
290N/A * element of the samples representing the bands of the pixel.
290N/A * <p>
290N/A * The following code illustrates extracting the bits of the sample
290N/A * representing band <code>b</code> for pixel <code>x,y</code>
290N/A * from DataBuffer <code>data</code>:
290N/A * <pre>
290N/A * int sample = data.getElem(y * scanlineStride + x);
290N/A * sample = (sample & bitMasks[b]) >>> bitOffsets[b];
290N/A * </pre>
290N/A */
290N/A
290N/Apublic class SinglePixelPackedSampleModel extends SampleModel
290N/A{
290N/A /** Bit masks for all bands of the image data. */
290N/A private int bitMasks[];
290N/A
290N/A /** Bit Offsets for all bands of the image data. */
290N/A private int bitOffsets[];
290N/A
290N/A /** Bit sizes for all the bands of the image data. */
290N/A private int bitSizes[];
290N/A
290N/A /** Maximum bit size. */
290N/A private int maxBitSize;
290N/A
290N/A /** Line stride of the region of image data described by this
290N/A * SinglePixelPackedSampleModel.
290N/A */
290N/A private int scanlineStride;
290N/A
290N/A private static native void initIDs();
290N/A static {
290N/A ColorModel.loadLibraries();
290N/A initIDs();
290N/A }
290N/A
290N/A /**
290N/A * Constructs a SinglePixelPackedSampleModel with bitMasks.length bands.
290N/A * Each sample is stored in a data array element in the position of
290N/A * its corresponding bit mask. Each bit mask must be contiguous and
3266N/A * masks must not overlap. Bit masks exceeding data type capacity are
3266N/A * truncated.
290N/A * @param dataType The data type for storing samples.
290N/A * @param w The width (in pixels) of the region of the
290N/A * image data described.
290N/A * @param h The height (in pixels) of the region of the
290N/A * image data described.
290N/A * @param bitMasks The bit masks for all bands.
290N/A * @throws IllegalArgumentException if <code>dataType</code> is not
290N/A * either <code>DataBuffer.TYPE_BYTE</code>,
290N/A * <code>DataBuffer.TYPE_USHORT</code>, or
290N/A * <code>DataBuffer.TYPE_INT</code>
290N/A */
290N/A public SinglePixelPackedSampleModel(int dataType, int w, int h,
290N/A int bitMasks[]) {
290N/A this(dataType, w, h, w, bitMasks);
290N/A if (dataType != DataBuffer.TYPE_BYTE &&
290N/A dataType != DataBuffer.TYPE_USHORT &&
290N/A dataType != DataBuffer.TYPE_INT) {
290N/A throw new IllegalArgumentException("Unsupported data type "+
290N/A dataType);
290N/A }
290N/A }
290N/A
290N/A /**
290N/A * Constructs a SinglePixelPackedSampleModel with bitMasks.length bands
290N/A * and a scanline stride equal to scanlineStride data array elements.
290N/A * Each sample is stored in a data array element in the position of
290N/A * its corresponding bit mask. Each bit mask must be contiguous and
3266N/A * masks must not overlap. Bit masks exceeding data type capacity are
3266N/A * truncated.
290N/A * @param dataType The data type for storing samples.
290N/A * @param w The width (in pixels) of the region of
290N/A * image data described.
290N/A * @param h The height (in pixels) of the region of
290N/A * image data described.
290N/A * @param scanlineStride The line stride of the image data.
290N/A * @param bitMasks The bit masks for all bands.
290N/A * @throws IllegalArgumentException if <code>w</code> or
290N/A * <code>h</code> is not greater than 0
290N/A * @throws IllegalArgumentException if any mask in
290N/A * <code>bitMask</code> is not contiguous
290N/A * @throws IllegalArgumentException if <code>dataType</code> is not
290N/A * either <code>DataBuffer.TYPE_BYTE</code>,
290N/A * <code>DataBuffer.TYPE_USHORT</code>, or
290N/A * <code>DataBuffer.TYPE_INT</code>
290N/A */
290N/A public SinglePixelPackedSampleModel(int dataType, int w, int h,
290N/A int scanlineStride, int bitMasks[]) {
290N/A super(dataType, w, h, bitMasks.length);
290N/A if (dataType != DataBuffer.TYPE_BYTE &&
290N/A dataType != DataBuffer.TYPE_USHORT &&
290N/A dataType != DataBuffer.TYPE_INT) {
290N/A throw new IllegalArgumentException("Unsupported data type "+
290N/A dataType);
290N/A }
290N/A this.dataType = dataType;
290N/A this.bitMasks = (int[]) bitMasks.clone();
290N/A this.scanlineStride = scanlineStride;
290N/A
290N/A this.bitOffsets = new int[numBands];
290N/A this.bitSizes = new int[numBands];
290N/A
3266N/A int maxMask = (int)((1L << DataBuffer.getDataTypeSize(dataType)) - 1);
3266N/A
290N/A this.maxBitSize = 0;
290N/A for (int i=0; i<numBands; i++) {
290N/A int bitOffset = 0, bitSize = 0, mask;
3266N/A this.bitMasks[i] &= maxMask;
3266N/A mask = this.bitMasks[i];
290N/A if (mask != 0) {
290N/A while ((mask & 1) == 0) {
290N/A mask = mask >>> 1;
290N/A bitOffset++;
290N/A }
290N/A while ((mask & 1) == 1) {
290N/A mask = mask >>> 1;
290N/A bitSize++;
290N/A }
290N/A if (mask != 0) {
290N/A throw new IllegalArgumentException("Mask "+bitMasks[i]+
290N/A " must be contiguous");
290N/A }
290N/A }
290N/A bitOffsets[i] = bitOffset;
290N/A bitSizes[i] = bitSize;
290N/A if (bitSize > maxBitSize) {
290N/A maxBitSize = bitSize;
290N/A }
290N/A }
290N/A }
290N/A
290N/A /**
290N/A * Returns the number of data elements needed to transfer one pixel
290N/A * via the getDataElements and setDataElements methods.
290N/A * For a SinglePixelPackedSampleModel, this is one.
290N/A */
290N/A public int getNumDataElements() {
290N/A return 1;
290N/A }
290N/A
290N/A /**
290N/A * Returns the size of the buffer (in data array elements)
290N/A * needed for a data buffer that matches this
290N/A * SinglePixelPackedSampleModel.
290N/A */
290N/A private long getBufferSize() {
290N/A long size = scanlineStride * (height-1) + width;
290N/A return size;
290N/A }
290N/A
290N/A /**
290N/A * Creates a new SinglePixelPackedSampleModel with the specified
290N/A * width and height. The new SinglePixelPackedSampleModel will have the
290N/A * same storage data type and bit masks as this
290N/A * SinglePixelPackedSampleModel.
290N/A * @param w the width of the resulting <code>SampleModel</code>
290N/A * @param h the height of the resulting <code>SampleModel</code>
290N/A * @return a <code>SinglePixelPackedSampleModel</code> with the
290N/A * specified width and height.
290N/A * @throws IllegalArgumentException if <code>w</code> or
290N/A * <code>h</code> is not greater than 0
290N/A */
290N/A public SampleModel createCompatibleSampleModel(int w, int h) {
290N/A SampleModel sampleModel = new SinglePixelPackedSampleModel(dataType, w, h,
290N/A bitMasks);
290N/A return sampleModel;
290N/A }
290N/A
290N/A /**
290N/A * Creates a DataBuffer that corresponds to this
290N/A * SinglePixelPackedSampleModel. The DataBuffer's data type and size
290N/A * will be consistent with this SinglePixelPackedSampleModel. The
290N/A * DataBuffer will have a single bank.
290N/A */
290N/A public DataBuffer createDataBuffer() {
290N/A DataBuffer dataBuffer = null;
290N/A
290N/A int size = (int)getBufferSize();
290N/A switch (dataType) {
290N/A case DataBuffer.TYPE_BYTE:
290N/A dataBuffer = new DataBufferByte(size);
290N/A break;
290N/A case DataBuffer.TYPE_USHORT:
290N/A dataBuffer = new DataBufferUShort(size);
290N/A break;
290N/A case DataBuffer.TYPE_INT:
290N/A dataBuffer = new DataBufferInt(size);
290N/A break;
290N/A }
290N/A return dataBuffer;
290N/A }
290N/A
290N/A /** Returns the number of bits per sample for all bands. */
290N/A public int[] getSampleSize() {
3266N/A return bitSizes.clone();
290N/A }
290N/A
290N/A /** Returns the number of bits per sample for the specified band. */
290N/A public int getSampleSize(int band) {
3266N/A return bitSizes[band];
290N/A }
290N/A
290N/A /** Returns the offset (in data array elements) of pixel (x,y).
290N/A * The data element containing pixel <code>x,y</code>
290N/A * can be retrieved from a DataBuffer <code>data</code> with a
290N/A * SinglePixelPackedSampleModel <code>sppsm</code> as:
290N/A * <pre>
290N/A * data.getElem(sppsm.getOffset(x, y));
290N/A * </pre>
290N/A * @param x the X coordinate of the specified pixel
290N/A * @param y the Y coordinate of the specified pixel
290N/A * @return the offset of the specified pixel.
290N/A */
290N/A public int getOffset(int x, int y) {
290N/A int offset = y * scanlineStride + x;
290N/A return offset;
290N/A }
290N/A
290N/A /** Returns the bit offsets into the data array element representing
290N/A * a pixel for all bands.
290N/A * @return the bit offsets representing a pixel for all bands.
290N/A */
290N/A public int [] getBitOffsets() {
290N/A return (int[])bitOffsets.clone();
290N/A }
290N/A
290N/A /** Returns the bit masks for all bands.
290N/A * @return the bit masks for all bands.
290N/A */
290N/A public int [] getBitMasks() {
290N/A return (int[])bitMasks.clone();
290N/A }
290N/A
290N/A /** Returns the scanline stride of this SinglePixelPackedSampleModel.
290N/A * @return the scanline stride of this
290N/A * <code>SinglePixelPackedSampleModel</code>.
290N/A */
290N/A public int getScanlineStride() {
290N/A return scanlineStride;
290N/A }
290N/A
290N/A /**
290N/A * This creates a new SinglePixelPackedSampleModel with a subset of the
290N/A * bands of this SinglePixelPackedSampleModel. The new
290N/A * SinglePixelPackedSampleModel can be used with any DataBuffer that the
290N/A * existing SinglePixelPackedSampleModel can be used with. The new
290N/A * SinglePixelPackedSampleModel/DataBuffer combination will represent
290N/A * an image with a subset of the bands of the original
290N/A * SinglePixelPackedSampleModel/DataBuffer combination.
290N/A * @exception RasterFormatException if the length of the bands argument is
290N/A * greater than the number of bands in
290N/A * the sample model.
290N/A */
290N/A public SampleModel createSubsetSampleModel(int bands[]) {
290N/A if (bands.length > numBands)
290N/A throw new RasterFormatException("There are only " +
290N/A numBands +
290N/A " bands");
290N/A int newBitMasks[] = new int[bands.length];
290N/A for (int i=0; i<bands.length; i++)
290N/A newBitMasks[i] = bitMasks[bands[i]];
290N/A
290N/A return new SinglePixelPackedSampleModel(this.dataType, width, height,
290N/A this.scanlineStride, newBitMasks);
290N/A }
290N/A
290N/A /**
290N/A * Returns data for a single pixel in a primitive array of type
290N/A * TransferType. For a SinglePixelPackedSampleModel, the array will
290N/A * have one element, and the type will be the same as the storage
290N/A * data type. Generally, obj
290N/A * should be passed in as null, so that the Object will be created
290N/A * automatically and will be of the right primitive data type.
290N/A * <p>
290N/A * The following code illustrates transferring data for one pixel from
290N/A * DataBuffer <code>db1</code>, whose storage layout is described by
290N/A * SinglePixelPackedSampleModel <code>sppsm1</code>, to
290N/A * DataBuffer <code>db2</code>, whose storage layout is described by
290N/A * SinglePixelPackedSampleModel <code>sppsm2</code>.
290N/A * The transfer will generally be more efficient than using
290N/A * getPixel/setPixel.
290N/A * <pre>
290N/A * SinglePixelPackedSampleModel sppsm1, sppsm2;
290N/A * DataBufferInt db1, db2;
290N/A * sppsm2.setDataElements(x, y, sppsm1.getDataElements(x, y, null,
290N/A * db1), db2);
290N/A * </pre>
290N/A * Using getDataElements/setDataElements to transfer between two
290N/A * DataBuffer/SampleModel pairs is legitimate if the SampleModels have
290N/A * the same number of bands, corresponding bands have the same number of
290N/A * bits per sample, and the TransferTypes are the same.
290N/A * <p>
290N/A * If obj is non-null, it should be a primitive array of type TransferType.
290N/A * Otherwise, a ClassCastException is thrown. An
290N/A * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
290N/A * not in bounds, or if obj is non-null and is not large enough to hold
290N/A * the pixel data.
290N/A * @param x The X coordinate of the pixel location.
290N/A * @param y The Y coordinate of the pixel location.
290N/A * @param obj If non-null, a primitive array in which to return
290N/A * the pixel data.
290N/A * @param data The DataBuffer containing the image data.
290N/A * @return the data for the specified pixel.
290N/A * @see #setDataElements(int, int, Object, DataBuffer)
290N/A */
290N/A public Object getDataElements(int x, int y, Object obj, DataBuffer data) {
290N/A // Bounds check for 'b' will be performed automatically
290N/A if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
290N/A throw new ArrayIndexOutOfBoundsException
290N/A ("Coordinate out of bounds!");
290N/A }
290N/A
290N/A int type = getTransferType();
290N/A
290N/A switch(type) {
290N/A
290N/A case DataBuffer.TYPE_BYTE:
290N/A
290N/A byte[] bdata;
290N/A
290N/A if (obj == null)
290N/A bdata = new byte[1];
290N/A else
290N/A bdata = (byte[])obj;
290N/A
290N/A bdata[0] = (byte)data.getElem(y * scanlineStride + x);
290N/A
290N/A obj = (Object)bdata;
290N/A break;
290N/A
290N/A case DataBuffer.TYPE_USHORT:
290N/A
290N/A short[] sdata;
290N/A
290N/A if (obj == null)
290N/A sdata = new short[1];
290N/A else
290N/A sdata = (short[])obj;
290N/A
290N/A sdata[0] = (short)data.getElem(y * scanlineStride + x);
290N/A
290N/A obj = (Object)sdata;
290N/A break;
290N/A
290N/A case DataBuffer.TYPE_INT:
290N/A
290N/A int[] idata;
290N/A
290N/A if (obj == null)
290N/A idata = new int[1];
290N/A else
290N/A idata = (int[])obj;
290N/A
290N/A idata[0] = data.getElem(y * scanlineStride + x);
290N/A
290N/A obj = (Object)idata;
290N/A break;
290N/A }
290N/A
290N/A return obj;
290N/A }
290N/A
290N/A /**
290N/A * Returns all samples in for the specified pixel in an int array.
290N/A * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
290N/A * not in bounds.
290N/A * @param x The X coordinate of the pixel location.
290N/A * @param y The Y coordinate of the pixel location.
290N/A * @param iArray If non-null, returns the samples in this array
290N/A * @param data The DataBuffer containing the image data.
290N/A * @return all samples for the specified pixel.
290N/A * @see #setPixel(int, int, int[], DataBuffer)
290N/A */
290N/A public int [] getPixel(int x, int y, int iArray[], DataBuffer data) {
290N/A if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
290N/A throw new ArrayIndexOutOfBoundsException
290N/A ("Coordinate out of bounds!");
290N/A }
290N/A int pixels[];
290N/A if (iArray == null) {
290N/A pixels = new int [numBands];
290N/A } else {
290N/A pixels = iArray;
290N/A }
290N/A
290N/A int value = data.getElem(y * scanlineStride + x);
290N/A for (int i=0; i<numBands; i++) {
290N/A pixels[i] = (value & bitMasks[i]) >>> bitOffsets[i];
290N/A }
290N/A return pixels;
290N/A }
290N/A
290N/A /**
290N/A * Returns all samples for the specified rectangle of pixels in
290N/A * an int array, one sample per array element.
290N/A * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
290N/A * not in bounds.
290N/A * @param x The X coordinate of the upper left pixel location.
290N/A * @param y The Y coordinate of the upper left pixel location.
290N/A * @param w The width of the pixel rectangle.
290N/A * @param h The height of the pixel rectangle.
290N/A * @param iArray If non-null, returns the samples in this array.
290N/A * @param data The DataBuffer containing the image data.
290N/A * @return all samples for the specified region of pixels.
290N/A * @see #setPixels(int, int, int, int, int[], DataBuffer)
290N/A */
290N/A public int[] getPixels(int x, int y, int w, int h,
290N/A int iArray[], DataBuffer data) {
3812N/A int x1 = x + w;
3812N/A int y1 = y + h;
3812N/A
3812N/A if (x < 0 || x >= width || w > width || x1 < 0 || x1 > width ||
3812N/A y < 0 || y >= height || h > height || y1 < 0 || y1 > height)
3812N/A {
290N/A throw new ArrayIndexOutOfBoundsException
290N/A ("Coordinate out of bounds!");
290N/A }
290N/A int pixels[];
290N/A if (iArray != null) {
290N/A pixels = iArray;
290N/A } else {
290N/A pixels = new int [w*h*numBands];
290N/A }
290N/A int lineOffset = y*scanlineStride + x;
290N/A int dstOffset = 0;
290N/A
290N/A for (int i = 0; i < h; i++) {
290N/A for (int j = 0; j < w; j++) {
290N/A int value = data.getElem(lineOffset+j);
290N/A for (int k=0; k < numBands; k++) {
290N/A pixels[dstOffset++] =
290N/A ((value & bitMasks[k]) >>> bitOffsets[k]);
290N/A }
290N/A }
290N/A lineOffset += scanlineStride;
290N/A }
290N/A return pixels;
290N/A }
290N/A
290N/A /**
290N/A * Returns as int the sample in a specified band for the pixel
290N/A * located at (x,y).
290N/A * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
290N/A * not in bounds.
290N/A * @param x The X coordinate of the pixel location.
290N/A * @param y The Y coordinate of the pixel location.
290N/A * @param b The band to return.
290N/A * @param data The DataBuffer containing the image data.
290N/A * @return the sample in a specified band for the specified
290N/A * pixel.
290N/A * @see #setSample(int, int, int, int, DataBuffer)
290N/A */
290N/A public int getSample(int x, int y, int b, DataBuffer data) {
290N/A // Bounds check for 'b' will be performed automatically
290N/A if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
290N/A throw new ArrayIndexOutOfBoundsException
290N/A ("Coordinate out of bounds!");
290N/A }
290N/A int sample = data.getElem(y * scanlineStride + x);
290N/A return ((sample & bitMasks[b]) >>> bitOffsets[b]);
290N/A }
290N/A
290N/A /**
290N/A * Returns the samples for a specified band for the specified rectangle
290N/A * of pixels in an int array, one sample per array element.
290N/A * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
290N/A * not in bounds.
290N/A * @param x The X coordinate of the upper left pixel location.
290N/A * @param y The Y coordinate of the upper left pixel location.
290N/A * @param w The width of the pixel rectangle.
290N/A * @param h The height of the pixel rectangle.
290N/A * @param b The band to return.
290N/A * @param iArray If non-null, returns the samples in this array.
290N/A * @param data The DataBuffer containing the image data.
290N/A * @return the samples for the specified band for the specified
290N/A * region of pixels.
290N/A * @see #setSamples(int, int, int, int, int, int[], DataBuffer)
290N/A */
290N/A public int[] getSamples(int x, int y, int w, int h, int b,
290N/A int iArray[], DataBuffer data) {
290N/A // Bounds check for 'b' will be performed automatically
290N/A if ((x < 0) || (y < 0) || (x + w > width) || (y + h > height)) {
290N/A throw new ArrayIndexOutOfBoundsException
290N/A ("Coordinate out of bounds!");
290N/A }
290N/A int samples[];
290N/A if (iArray != null) {
290N/A samples = iArray;
290N/A } else {
290N/A samples = new int [w*h];
290N/A }
290N/A int lineOffset = y*scanlineStride + x;
290N/A int dstOffset = 0;
290N/A
290N/A for (int i = 0; i < h; i++) {
290N/A for (int j = 0; j < w; j++) {
290N/A int value = data.getElem(lineOffset+j);
290N/A samples[dstOffset++] =
290N/A ((value & bitMasks[b]) >>> bitOffsets[b]);
290N/A }
290N/A lineOffset += scanlineStride;
290N/A }
290N/A return samples;
290N/A }
290N/A
290N/A /**
290N/A * Sets the data for a single pixel in the specified DataBuffer from a
290N/A * primitive array of type TransferType. For a
290N/A * SinglePixelPackedSampleModel, only the first element of the array
290N/A * will hold valid data, and the type of the array must be the same as
290N/A * the storage data type of the SinglePixelPackedSampleModel.
290N/A * <p>
290N/A * The following code illustrates transferring data for one pixel from
290N/A * DataBuffer <code>db1</code>, whose storage layout is described by
290N/A * SinglePixelPackedSampleModel <code>sppsm1</code>,
290N/A * to DataBuffer <code>db2</code>, whose storage layout is described by
290N/A * SinglePixelPackedSampleModel <code>sppsm2</code>.
290N/A * The transfer will generally be more efficient than using
290N/A * getPixel/setPixel.
290N/A * <pre>
290N/A * SinglePixelPackedSampleModel sppsm1, sppsm2;
290N/A * DataBufferInt db1, db2;
290N/A * sppsm2.setDataElements(x, y, sppsm1.getDataElements(x, y, null,
290N/A * db1), db2);
290N/A * </pre>
290N/A * Using getDataElements/setDataElements to transfer between two
290N/A * DataBuffer/SampleModel pairs is legitimate if the SampleModels have
290N/A * the same number of bands, corresponding bands have the same number of
290N/A * bits per sample, and the TransferTypes are the same.
290N/A * <p>
290N/A * obj must be a primitive array of type TransferType. Otherwise,
290N/A * a ClassCastException is thrown. An
290N/A * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
290N/A * not in bounds, or if obj is not large enough to hold the pixel data.
290N/A * @param x The X coordinate of the pixel location.
290N/A * @param y The Y coordinate of the pixel location.
290N/A * @param obj A primitive array containing pixel data.
290N/A * @param data The DataBuffer containing the image data.
290N/A * @see #getDataElements(int, int, Object, DataBuffer)
290N/A */
290N/A public void setDataElements(int x, int y, Object obj, DataBuffer data) {
290N/A if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
290N/A throw new ArrayIndexOutOfBoundsException
290N/A ("Coordinate out of bounds!");
290N/A }
290N/A
290N/A int type = getTransferType();
290N/A
290N/A switch(type) {
290N/A
290N/A case DataBuffer.TYPE_BYTE:
290N/A
290N/A byte[] barray = (byte[])obj;
290N/A data.setElem(y*scanlineStride+x, ((int)barray[0])&0xff);
290N/A break;
290N/A
290N/A case DataBuffer.TYPE_USHORT:
290N/A
290N/A short[] sarray = (short[])obj;
290N/A data.setElem(y*scanlineStride+x, ((int)sarray[0])&0xffff);
290N/A break;
290N/A
290N/A case DataBuffer.TYPE_INT:
290N/A
290N/A int[] iarray = (int[])obj;
290N/A data.setElem(y*scanlineStride+x, iarray[0]);
290N/A break;
290N/A }
290N/A }
290N/A
290N/A /**
290N/A * Sets a pixel in the DataBuffer using an int array of samples for input.
290N/A * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
290N/A * not in bounds.
290N/A * @param x The X coordinate of the pixel location.
290N/A * @param y The Y coordinate of the pixel location.
290N/A * @param iArray The input samples in an int array.
290N/A * @param data The DataBuffer containing the image data.
290N/A * @see #getPixel(int, int, int[], DataBuffer)
290N/A */
290N/A public void setPixel(int x, int y,
290N/A int iArray[],
290N/A DataBuffer data) {
290N/A if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
290N/A throw new ArrayIndexOutOfBoundsException
290N/A ("Coordinate out of bounds!");
290N/A }
290N/A int lineOffset = y * scanlineStride + x;
290N/A int value = data.getElem(lineOffset);
290N/A for (int i=0; i < numBands; i++) {
290N/A value &= ~bitMasks[i];
290N/A value |= ((iArray[i] << bitOffsets[i]) & bitMasks[i]);
290N/A }
290N/A data.setElem(lineOffset, value);
290N/A }
290N/A
290N/A /**
290N/A * Sets all samples for a rectangle of pixels from an int array containing
290N/A * one sample per array element.
290N/A * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
290N/A * not in bounds.
290N/A * @param x The X coordinate of the upper left pixel location.
290N/A * @param y The Y coordinate of the upper left pixel location.
290N/A * @param w The width of the pixel rectangle.
290N/A * @param h The height of the pixel rectangle.
290N/A * @param iArray The input samples in an int array.
290N/A * @param data The DataBuffer containing the image data.
290N/A * @see #getPixels(int, int, int, int, int[], DataBuffer)
290N/A */
290N/A public void setPixels(int x, int y, int w, int h,
290N/A int iArray[], DataBuffer data) {
3812N/A int x1 = x + w;
3812N/A int y1 = y + h;
3812N/A
3812N/A if (x < 0 || x >= width || w > width || x1 < 0 || x1 > width ||
3812N/A y < 0 || y >= height || h > height || y1 < 0 || y1 > height)
3812N/A {
290N/A throw new ArrayIndexOutOfBoundsException
290N/A ("Coordinate out of bounds!");
290N/A }
290N/A
290N/A int lineOffset = y*scanlineStride + x;
290N/A int srcOffset = 0;
290N/A
290N/A for (int i = 0; i < h; i++) {
290N/A for (int j = 0; j < w; j++) {
290N/A int value = data.getElem(lineOffset+j);
290N/A for (int k=0; k < numBands; k++) {
290N/A value &= ~bitMasks[k];
290N/A int srcValue = iArray[srcOffset++];
290N/A value |= ((srcValue << bitOffsets[k])
290N/A & bitMasks[k]);
290N/A }
290N/A data.setElem(lineOffset+j, value);
290N/A }
290N/A lineOffset += scanlineStride;
290N/A }
290N/A }
290N/A
290N/A /**
290N/A * Sets a sample in the specified band for the pixel located at (x,y)
290N/A * in the DataBuffer using an int for input.
290N/A * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
290N/A * not in bounds.
290N/A * @param x The X coordinate of the pixel location.
290N/A * @param y The Y coordinate of the pixel location.
290N/A * @param b The band to set.
290N/A * @param s The input sample as an int.
290N/A * @param data The DataBuffer containing the image data.
290N/A * @see #getSample(int, int, int, DataBuffer)
290N/A */
290N/A public void setSample(int x, int y, int b, int s,
290N/A DataBuffer data) {
290N/A // Bounds check for 'b' will be performed automatically
290N/A if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
290N/A throw new ArrayIndexOutOfBoundsException
290N/A ("Coordinate out of bounds!");
290N/A }
290N/A int value = data.getElem(y*scanlineStride + x);
290N/A value &= ~bitMasks[b];
290N/A value |= (s << bitOffsets[b]) & bitMasks[b];
290N/A data.setElem(y*scanlineStride + x,value);
290N/A }
290N/A
290N/A /**
290N/A * Sets the samples in the specified band for the specified rectangle
290N/A * of pixels from an int array containing one sample per array element.
290N/A * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
290N/A * not in bounds.
290N/A * @param x The X coordinate of the upper left pixel location.
290N/A * @param y The Y coordinate of the upper left pixel location.
290N/A * @param w The width of the pixel rectangle.
290N/A * @param h The height of the pixel rectangle.
290N/A * @param b The band to set.
290N/A * @param iArray The input samples in an int array.
290N/A * @param data The DataBuffer containing the image data.
290N/A * @see #getSamples(int, int, int, int, int, int[], DataBuffer)
290N/A */
290N/A public void setSamples(int x, int y, int w, int h, int b,
290N/A int iArray[], DataBuffer data) {
290N/A // Bounds check for 'b' will be performed automatically
290N/A if ((x < 0) || (y < 0) || (x + w > width) || (y + h > height)) {
290N/A throw new ArrayIndexOutOfBoundsException
290N/A ("Coordinate out of bounds!");
290N/A }
290N/A int lineOffset = y*scanlineStride + x;
290N/A int srcOffset = 0;
290N/A
290N/A for (int i = 0; i < h; i++) {
290N/A for (int j = 0; j < w; j++) {
290N/A int value = data.getElem(lineOffset+j);
290N/A value &= ~bitMasks[b];
290N/A int sample = iArray[srcOffset++];
290N/A value |= ((int)sample << bitOffsets[b]) & bitMasks[b];
290N/A data.setElem(lineOffset+j,value);
290N/A }
290N/A lineOffset += scanlineStride;
290N/A }
290N/A }
290N/A
290N/A public boolean equals(Object o) {
290N/A if ((o == null) || !(o instanceof SinglePixelPackedSampleModel)) {
290N/A return false;
290N/A }
290N/A
290N/A SinglePixelPackedSampleModel that = (SinglePixelPackedSampleModel)o;
290N/A return this.width == that.width &&
290N/A this.height == that.height &&
290N/A this.numBands == that.numBands &&
290N/A this.dataType == that.dataType &&
290N/A Arrays.equals(this.bitMasks, that.bitMasks) &&
290N/A Arrays.equals(this.bitOffsets, that.bitOffsets) &&
290N/A Arrays.equals(this.bitSizes, that.bitSizes) &&
290N/A this.maxBitSize == that.maxBitSize &&
290N/A this.scanlineStride == that.scanlineStride;
290N/A }
290N/A
290N/A // If we implement equals() we must also implement hashCode
290N/A public int hashCode() {
290N/A int hash = 0;
290N/A hash = width;
290N/A hash <<= 8;
290N/A hash ^= height;
290N/A hash <<= 8;
290N/A hash ^= numBands;
290N/A hash <<= 8;
290N/A hash ^= dataType;
290N/A hash <<= 8;
290N/A for (int i = 0; i < bitMasks.length; i++) {
290N/A hash ^= bitMasks[i];
290N/A hash <<= 8;
290N/A }
290N/A for (int i = 0; i < bitOffsets.length; i++) {
290N/A hash ^= bitOffsets[i];
290N/A hash <<= 8;
290N/A }
290N/A for (int i = 0; i < bitSizes.length; i++) {
290N/A hash ^= bitSizes[i];
290N/A hash <<= 8;
290N/A }
290N/A hash ^= maxBitSize;
290N/A hash <<= 8;
290N/A hash ^= scanlineStride;
290N/A return hash;
290N/A }
290N/A}