Source for java.awt.image.DataBufferUShort

   1: /*
   2:  * @(#)DataBufferUShort.java    1.13 03/12/19
   3:  *
   4:  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
   5:  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
   6:  */
   7: 
   8: /* ****************************************************************
   9:  ******************************************************************
  10:  ******************************************************************
  11:  *** COPYRIGHT (c) Eastman Kodak Company, 1997
  12:  *** As  an unpublished  work pursuant to Title 17 of the United
  13:  *** States Code.  All rights reserved.
  14:  ******************************************************************
  15:  ******************************************************************
  16:  ******************************************************************/
  17: 
  18: package java.awt.image;
  19: 
  20: /**
  21:  * This class extends <CODE>DataBuffer</CODE> and stores data internally as
  22:  * shorts.  Values stored in the short array(s) of this <CODE>DataBuffer</CODE>
  23:  * are treated as unsigned values.
  24:  */
  25: 
  26: 
  27: public final class DataBufferUShort extends DataBuffer
  28:	  {
  29:    /** The default data bank. */
  30:    short data[];
  31:
  32:    /** All data banks */
  33:    short bankdata[][];
  34:
  35:    /**
  36:     * Constructs an unsigned-short based <CODE>DataBuffer</CODE> with a single bank and the
  37:     * specified size.
  38:     *
  39:     * @param size The size of the <CODE>DataBuffer</CODE>.
  40:     */
  41:	      public DataBufferUShort(int size) {
  42:        super(TYPE_USHORT,size);
  43:        data = new short[size];
  44:        bankdata = new short[1][];
  45:        bankdata[0] = data;
  46:    }
  47:
  48:    /**
  49:     * Constructs an unsigned-short based <CODE>DataBuffer</CODE> with the specified number of
  50:     * banks, all of which are the specified size.
  51:     *
  52:     * @param size The size of the banks in the <CODE>DataBuffer</CODE>.
  53:     * @param numBanks The number of banks in the a<CODE>DataBuffer</CODE>.
  54:    */
  55:	      public DataBufferUShort(int size, int numBanks) {
  56:        super(TYPE_USHORT,size,numBanks);
  57:        bankdata = new short[numBanks][];
  58:	          for (int i= 0; i < numBanks; i++) {
  59:            bankdata[i] = new short[size];
  60:        }
  61:        data = bankdata[0];
  62:    }
  63:
  64:    /**
  65:     * Constructs an unsigned-short based <CODE>DataBuffer</CODE> with a single bank
  66:     * using the specified array.
  67:     * Only the first <CODE>size</CODE> elements should be used by accessors of
  68:     * this <CODE>DataBuffer</CODE>.  <CODE>dataArray</CODE> must be large enough to
  69:     * hold <CODE>size</CODE> elements.
  70:     *
  71:     * @param dataArray The unsigned-short array for the <CODE>DataBuffer</CODE>.
  72:     * @param size The size of the <CODE>DataBuffer</CODE> bank.
  73:     */
  74:	      public DataBufferUShort(short dataArray[], int size) {
  75:        super(TYPE_USHORT,size);
  76:	          if (dataArray == null) {
  77:            throw new NullPointerException("dataArray is null");
  78:        }
  79:        data = dataArray;
  80:        bankdata = new short[1][];
  81:        bankdata[0] = data;
  82:    }
  83:
  84:    /**
  85:     * Constructs an unsigned-short based <CODE>DataBuffer</CODE> with a single bank
  86:     * using the specified array, size, and offset.  <CODE>dataArray</CODE> must have at
  87:     * least <CODE>offset</CODE> + <CODE>size</CODE> elements.  Only elements 
  88:     * <CODE>offset</CODE> through <CODE>offset</CODE> + <CODE>size</CODE> - 1 should 
  89:     * be used by accessors of this <CODE>DataBuffer</CODE>.
  90:     *
  91:     * @param dataArray The unsigned-short array for the <CODE>DataBuffer</CODE>.
  92:     * @param size The size of the <CODE>DataBuffer</CODE> bank.
  93:     * @param offset The offset into the <CODE>dataArray</CODE>.
  94:     */
  95:	      public DataBufferUShort(short dataArray[], int size, int offset) {
  96:        super(TYPE_USHORT,size,1,offset);
  97:	          if (dataArray == null) {
  98:            throw new NullPointerException("dataArray is null");
  99:        }
 100:	          if ((size+offset) > dataArray.length) {
 101:            throw new IllegalArgumentException("Length of dataArray is less "+
 102:                                               " than size+offset.");
 103:        }
 104:        data = dataArray;
 105:        bankdata = new short[1][];
 106:        bankdata[0] = data;
 107:    }
 108:
 109:    /**
 110:     * Constructs an unsigned-short based <CODE>DataBuffer</CODE> with the specified arrays.
 111:     * The number of banks will be equal to <CODE>dataArray.length</CODE>.
 112:     * Only the first <CODE>size</CODE> elements of each array should be used by
 113:     * accessors of this <CODE>DataBuffer</CODE>.
 114:     *
 115:     * @param dataArray The unsigned-short arrays for the <CODE>DataBuffer</CODE>.
 116:     * @param size The size of the banks in the <CODE>DataBuffer</CODE>.
 117:     */
 118:	      public DataBufferUShort(short dataArray[][], int size) {
 119:        super(TYPE_USHORT,size,dataArray.length);
 120:	          if (dataArray == null) {
 121:            throw new NullPointerException("dataArray is null");
 122:        }
 123:	          for (int i=0; i < dataArray.length; i++) {
 124:	              if (dataArray[i] == null) {
 125:                throw new NullPointerException("dataArray["+i+"] is null");
 126:            }
 127:        }
 128:                
 129:        bankdata = (short[][]) dataArray.clone();
 130:        data = bankdata[0];
 131:    }
 132:
 133:    /**
 134:     * Constructs an unsigned-short based <CODE>DataBuffer</CODE> with specified arrays,
 135:     * size, and offsets.
 136:     * The number of banks is equal to <CODE>dataArray.length</CODE>.  Each array must
 137:     * be at least as large as <CODE>size</CODE> + the corresponding offset.   There must
 138:     * be an entry in the offset array for each <CODE>dataArray</CODE> entry.  For each
 139:     * bank, only elements <CODE>offset</CODE> through 
 140:     * <CODE>offset</CODE> + <CODE>size</CODE> - 1 should be
 141:     * used by accessors of this <CODE>DataBuffer</CODE>.
 142:     *
 143:     * @param dataArray The unsigned-short arrays for the <CODE>DataBuffer</CODE>.
 144:     * @param size The size of the banks in the <CODE>DataBuffer</CODE>.
 145:     * @param offsets The offsets into each array.
 146:     */
 147:	      public DataBufferUShort(short dataArray[][], int size, int offsets[]) {
 148:        super(TYPE_USHORT,size,dataArray.length,offsets);
 149:	          if (dataArray == null) {
 150:            throw new NullPointerException("dataArray is null");
 151:        }
 152:	          for (int i=0; i < dataArray.length; i++) {
 153:	              if (dataArray[i] == null) {
 154:                throw new NullPointerException("dataArray["+i+"] is null");
 155:            }
 156:	              if ((size+offsets[i]) > dataArray[i].length) {
 157:                throw new IllegalArgumentException("Length of dataArray["+i+
 158:                                                   "] is less than size+"+
 159:                                                   "offsets["+i+"].");
 160:            }
 161:                                                  
 162:        }
 163:        bankdata = (short[][]) dataArray.clone();
 164:        data = bankdata[0];
 165:    }
 166:
 167:    /** 
 168:     * Returns the default (first) unsigned-short data array. 
 169:     *    
 170:     * @return The first unsigned-short data array.
 171:     */
 172:	      public short[] getData() {
 173:        return data;
 174:    }
 175:
 176:    /** 
 177:     * Returns the data array for the specified bank.    
 178:     *   
 179:     * @param bank The bank whose data array you want to get. 
 180:     * @return The data array for the specified bank.
 181:     */
 182:	      public short[] getData(int bank) {
 183:        return bankdata[bank];
 184:    }
 185:
 186:    /** 
 187:     * Returns the data arrays for all banks. 
 188:     * @return All of the data arrays.
 189:     */
 190:	      public short[][] getBankData() {
 191:        return (short[][]) bankdata.clone();
 192:    }
 193:
 194:    /**
 195:     * Returns the requested data array element from the first (default) bank.
 196:     * 
 197:     * @param i The data array element you want to get.
 198:     * @return The requested data array element as an integer.
 199:     * @see #setElem(int, int)
 200:     * @see #setElem(int, int, int)
 201:     */
 202:	      public int getElem(int i) {
 203:        return (int)(data[i+offset]&0xffff);
 204:    }
 205:
 206:    /**
 207:     * Returns the requested data array element from the specified bank.
 208:     * 
 209:     * @param bank The bank from which you want to get a data array element.
 210:     * @param i The data array element you want to get.
 211:     * @return The requested data array element as an integer.
 212:     * @see #setElem(int, int)
 213:     * @see #setElem(int, int, int)
 214:     */
 215:	      public int getElem(int bank, int i) {
 216:        return (int)(bankdata[bank][i+offsets[bank]]&0xffff);
 217:    }
 218:
 219:    /**
 220:     * Sets the requested data array element in the first (default) bank
 221:     * to the specified value.
 222:     *
 223:     * @param i The data array element you want to set.
 224:     * @param val The integer value to which you want to set the data array element.
 225:     * @see #getElem(int)
 226:     * @see #getElem(int, int)
 227:     */
 228:	      public void setElem(int i, int val) {
 229:        data[i+offset] = (short)(val&0xffff);
 230:    }
 231:
 232:    /**
 233:     * Sets the requested data array element in the specified bank
 234:     * from the given integer.
 235:     * @param bank The bank in which you want to set the data array element.
 236:     * @param i The data array element you want to set.
 237:     * @param val The integer value to which you want to set the specified data array element.
 238:     * @see #getElem(int)
 239:     * @see #getElem(int, int)
 240:     */
 241:	      public void setElem(int bank, int i, int val) {
 242:        bankdata[bank][i+offsets[bank]] = (short)(val&0xffff);
 243:    }
 244:}