home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-20 | 9.0 KB | 292 lines |
- /*
- * @(#)DataBuffer.java 1.8 98/03/18
- *
- * Copyright 1997 by Sun Microsystems, Inc.,
- * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
- * All rights reserved.
- *
- * This software is the confidential and proprietary information
- * of Sun Microsystems, Inc. ("Confidential Information"). You
- * shall not disclose such Confidential Information and shall use
- * it only in accordance with the terms of the license agreement
- * you entered into with Sun.
- */
-
- /* ****************************************************************
- ******************************************************************
- ******************************************************************
- *** COPYRIGHT (c) Eastman Kodak Company, 1997
- *** As an unpublished work pursuant to Title 17 of the United
- *** States Code. All rights reserved.
- ******************************************************************
- ******************************************************************
- ******************************************************************/
-
- package java.awt.image;
-
- /**
- * This class exists to wrap one or more data arrays. Each data array in
- * the DataBuffer is referred to as a bank. Accessor methods for getting
- * and setting elements of the DataBuffer's banks exist with and without
- * a bank specifier. The without bank specifiers use the default 0th
- * bank. The DataBuffer can optionally take an offset per bank, so that
- * data in an existing array can be used even if the interesting data
- * doesn't start at array location zero. Getting or setting the 0th
- * element of a bank, uses the (0+offset)th element of the array. The
- * size field specifies how much of the data array is available for
- * use. The size + offset fields for a given bank should never be longer
- * than the array length of the associatied data array. Generally, an
- * object of class DataBuffer will be cast down to one of it's subclasses
- * for improved speed.
- */
-
- public abstract class DataBuffer
- {
-
- /** Tag for byte data. */
- public static final int BYTE_DATA = 0;
-
- /** Tag for short data. */
- public static final int SHORT_DATA = 1;
-
- /** Tag for int data. */
- public static final int INT_DATA = 2;
-
- /** Tag for long data. */
- public static final int LONG_DATA = 3;
-
- /** Tag for byte data. */
- public static final int FLOAT_DATA = 4;
-
- /** Tag for byte data. */
- public static final int DOUBLE_DATA = 5;
-
- /** The datatype of this DataBuffer. */
- protected int dataType;
-
- /** The number of banks (or arrays in this DataBuffer). */
- protected int banks;
-
- /** Offset into default (first) bank from which to get the first element. */
- protected int offset;
-
- /** Usable size of the a bank. */
- protected int size;
-
- /** Offsets into all banks. */
- protected int offsets[];
-
- /** Size of the data types indexed by DataType tags defined above. */
- private static final int dataTypeSize[] = {8,16,32};
-
- /** Size of the data type, given a datatype tag. */
- public static int sizeOf(int type) {
- return dataTypeSize[type];
- }
-
- /**
- * Converts an input array of some primitive datatype (byte/short/int)
- * into an integer array. If the input array is of type int[], it
- * will be returned as is. Otherwise, an integer array will be
- * created and elements promoted to ints and copied. Masks will be
- * used to prevent sign extension on byte values. Returns null
- * if obj is not of type byte[], short[] or int[].
- */
- public static int[] toIntArray(Object obj) {
- if (obj instanceof int[]) {
- return (int[])obj;
- } else if (obj == null) {
- return null;
- } else if (obj instanceof short[]) {
- short sdata[] = (short[])obj;
- int idata[] = new int[sdata.length];
- for (int i = 0; i < sdata.length; i++) {
- idata[i] = (int)sdata[i];
- }
- return idata;
- } else if (obj instanceof byte[]) {
- byte bdata[] = (byte[])obj;
- int idata[] = new int[bdata.length];
- for (int i = 0; i < bdata.length; i++) {
- idata[i] = 0xff & (int)bdata[i];
- }
- return idata;
- }
- return null;
- }
-
- /**
- * Constructs a DataBuffer containing one bank of the specified
- * data type and size.
- */
- protected DataBuffer(int dataType, int size) {
- this.dataType = dataType;
- this.banks = 1;
- this.size = size;
- this.offset = 0;
- this.offsets = new int[1]; // init to 0 by new
- }
-
- /**
- * Constructs a DataBuffer containing the specified number of
- * banks. Each bank has the specified size and an offset of 0.
- */
- protected DataBuffer(int dataType, int size, int numBanks) {
- this.dataType = dataType;
- this.banks = numBanks;
- this.size = size;
- this.offset = 0;
- this.offsets = new int[banks]; // init to 0 by new
- }
-
- /**
- * Constructs a DataBuffer which contains the specified number
- * of banks. Each bank has the specified datatype, size and offset.
- */
- protected DataBuffer(int dataType, int size, int numBanks, int offset) {
- this.dataType = dataType;
- this.banks = numBanks;
- this.size = size;
- this.offset = 0;
- this.offsets = new int[numBanks];
- for (int i = 0; i < numBanks; i++) {
- this.offsets[i] = offset;
- }
- }
-
- /**
- * Constructs a DataBuffer which contains the specified number
- * of banks. Each bank has the specified datatype and size. The
- * offset for each bank corresponds to it's respective entry in
- * the offsets array.
- */
- protected DataBuffer(int dataType, int size, int numBanks, int offsets[]) {
- if (numBanks != offsets.length) {
- throw new ArrayIndexOutOfBoundsException("Number of banks" +
- " does not match number of bank offsets");
- }
- this.dataType = dataType;
- this.banks = numBanks;
- this.size = size;
- this.offset = offsets[0];
- this.offsets = (int[])offsets.clone();
- }
-
- /** This returns the data type of this DataBuffer. */
- public int getDataType() {
- return dataType;
- }
-
- /** Returns the size of the default bank in array elements. */
- public int getSize() {
- return size;
- }
-
- /** Returns the offset of the default bank in array elements. */
- public int getOffset() {
- return offset;
- }
-
- /** Returns the offsets of all the banks in array elements. */
- public int[] getOffsets() {
- return (int[])offsets.clone();
- }
-
- /** Returns the number of banks in this DataBuffer. */
- public int getNumBanks() {
- return banks;
- }
-
- /**
- * Returns the requested data array element from the first (default) bank
- * as an integer.
- */
- public int getElem(int i) {
- return getElem(0,i);
- }
-
- /**
- * Returns the requested data array element from the specified bank
- * as an integer.
- */
- public abstract int getElem(int bank, int i);
-
- /**
- * Sets the requested data array element in the first (default) bank
- * from the given integer.
- */
- public void setElem(int i, int val) {
- setElem(0,i,val);
- }
-
- /**
- * Sets the requested data array element in the specified bank
- * from the given integer.
- */
- public abstract void setElem(int bank, int i, int val);
-
- /**
- * Returns the requested data array element from the first (default) bank
- * as a float.
- */
- public float getElemFloat(int i) {
- return (float)getElem(i);
- }
-
- /**
- * Returns the requested data array element from the specified bank
- * as a float.
- */
- public float getElemFloat(int bank, int i) {
- return (float)getElem(bank,i);
- }
-
- /**
- * Sets the requested data array element in the first (default) bank
- * from the given float.
- */
- public void setElemFloat(int i, float val) {
- setElem(i,(int)val);
- }
-
- /**
- * Sets the requested data array element in the specified bank
- * from the given float.
- */
- public void setElemFloat(int bank, int i, float val) {
- setElem(bank,i,(int)val);
- }
-
- /**
- * Returns the requested data array element from the first (default) bank
- * as a double.
- */
- public double getElemDouble(int i) {
- return (double)getElem(i);
- }
-
- /**
- * Returns the requested data array element from the specified bank
- * as a double.
- */
- public double getElemDouble(int bank, int i) {
- return (double)getElem(bank,i);
- }
-
- /**
- * Sets the requested data array element in the first (default) bank
- * from the given double.
- */
- public void setElemDouble(int i, double val) {
- setElem(i,(int)val);
- }
-
- /**
- * Sets the requested data array element in the specified bank
- * from the given double.
- */
- public void setElemDouble(int bank, int i, double val) {
- setElem(bank,i,(int)val);
- }
- }
-