Source for java.awt.image.DataBufferInt

   1: /* Copyright (C) 2000, 2002, 2005  Free Software Foundation
   2: 
   3: This file is part of GNU Classpath.
   4: 
   5: GNU Classpath is free software; you can redistribute it and/or modify
   6: it under the terms of the GNU General Public License as published by
   7: the Free Software Foundation; either version 2, or (at your option)
   8: any later version.
   9: 
  10: GNU Classpath is distributed in the hope that it will be useful, but
  11: WITHOUT ANY WARRANTY; without even the implied warranty of
  12: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13: General Public License for more details.
  14: 
  15: You should have received a copy of the GNU General Public License
  16: along with GNU Classpath; see the file COPYING.  If not, write to the
  17: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18: 02110-1301 USA.
  19: 
  20: Linking this library statically or dynamically with other modules is
  21: making a combined work based on this library.  Thus, the terms and
  22: conditions of the GNU General Public License cover the whole
  23: combination.
  24: 
  25: As a special exception, the copyright holders of this library give you
  26: permission to link this library with independent modules to produce an
  27: executable, regardless of the license terms of these independent
  28: modules, and to copy and distribute the resulting executable under
  29: terms of your choice, provided that you also meet, for each linked
  30: independent module, the terms and conditions of the license of that
  31: module.  An independent module is a module which is not derived from
  32: or based on this library.  If you modify this library, you may extend
  33: this exception to your version of the library, but you are not
  34: obligated to do so.  If you do not wish to do so, delete this
  35: exception statement from your version. */
  36: 
  37: package java.awt.image;
  38: 
  39: /* This is one of several classes that are nearly identical. Maybe we
  40:    should have a central template and generate all these files. This
  41:    is one of the cases where templates or macros would have been
  42:    useful to have in Java.
  43: 
  44:    This file has been created using search-replace. My only fear is
  45:    that these classes will grow out-of-sync as of a result of changes
  46:    that are not propagated to the other files. As always, mirroring
  47:    code is a maintenance nightmare.  */
  48: 
  49: /**
  50:  * A {@link DataBuffer} that uses an array of <code>int</code> primitives
  51:  * to represent each of its banks.
  52:  *
  53:  * @author Rolf W. Rasmussen (rolfwr@ii.uib.no)
  54:  */
  55: public final class DataBufferInt extends DataBuffer
  56: {
  57:   private int[] data;
  58:   private int[][] bankData;
  59: 
  60:   /**
  61:    * Creates a new data buffer with a single data bank containing the
  62:    * specified number of <code>int</code> elements.
  63:    *
  64:    * @param size the number of elements in the data bank.
  65:    */
  66:   public DataBufferInt(int size)
  67:   {
  68:     super(TYPE_INT, size, 1, 0);
  69:     bankData = new int[1][];
  70:     data = new int[size];
  71:     bankData[0] = data;
  72:   }
  73: 
  74:   /**
  75:    * Creates a new data buffer with the specified number of data banks,
  76:    * each containing the specified number of <code>int</code> elements.
  77:    *
  78:    * @param size the number of elements in the data bank.
  79:    * @param numBanks the number of data banks.
  80:    */
  81:   public DataBufferInt(int size, int numBanks)
  82:   {
  83:     super(TYPE_INT, size, numBanks);
  84:     bankData = new int[numBanks][size];
  85:     data = bankData[0];
  86:   }
  87: 
  88:   /**
  89:    * Creates a new data buffer backed by the specified data bank.
  90:    * <p>
  91:    * Note: there is no exception when <code>dataArray</code> is
  92:    * <code>null</code>, but in that case an exception will be thrown
  93:    * later if you attempt to access the data buffer.
  94:    *
  95:    * @param dataArray the data bank.
  96:    * @param size the number of elements in the data bank.
  97:    */
  98:   public DataBufferInt(int[] dataArray, int size)
  99:   {
 100:     super(TYPE_INT, size, 1, 0);
 101:     bankData = new int[1][];
 102:     data = dataArray;
 103:     bankData[0] = data;
 104:   }
 105: 
 106:   /**
 107:    * Creates a new data buffer backed by the specified data bank, with
 108:    * the specified offset to the first element.
 109:    * <p>
 110:    * Note: there is no exception when <code>dataArray</code> is
 111:    * <code>null</code>, but in that case an exception will be thrown
 112:    * later if you attempt to access the data buffer.
 113:    *
 114:    * @param dataArray the data bank.
 115:    * @param size the number of elements in the data bank.
 116:    * @param offset the offset to the first element in the array.
 117:    */
 118:   public DataBufferInt(int[] dataArray, int size, int offset)
 119:   {
 120:     super(TYPE_INT, size, 1, offset);
 121:     bankData = new int[1][];
 122:     data = dataArray;
 123:     bankData[0] = data;
 124:   }
 125: 
 126:   /**
 127:    * Creates a new data buffer backed by the specified data banks.
 128:    *
 129:    * @param dataArray the data banks.
 130:    * @param size the number of elements in the data bank.
 131:    *
 132:    * @throws NullPointerException if <code>dataArray</code> is
 133:    *         <code>null</code>.
 134:    */
 135:   public DataBufferInt(int[][] dataArray, int size)
 136:   {
 137:     super(TYPE_INT, size, dataArray.length);
 138:     bankData = dataArray;
 139:     data = bankData[0];
 140:   }
 141: 
 142:   /**
 143:    * Creates a new data buffer backed by the specified data banks, with
 144:    * the specified offsets to the first element in each bank.
 145:    *
 146:    * @param dataArray the data banks.
 147:    * @param size the number of elements in the data bank.
 148:    * @param offsets the offsets to the first element in each data bank.
 149:    *
 150:    * @throws NullPointerException if <code>dataArray</code> is
 151:    *         <code>null</code>.
 152:    */
 153:   public DataBufferInt(int[][] dataArray, int size, int[] offsets)
 154:   {
 155:     super(TYPE_INT, size, dataArray.length, offsets);
 156:     bankData = dataArray;
 157:     data = bankData[0];
 158:   }
 159: 
 160:   /**
 161:    * Returns the first data bank.
 162:    *
 163:    * @return The first data bank.
 164:    */
 165:   public int[] getData()
 166:   {
 167:     return data;
 168:   }
 169: 
 170:   /**
 171:    * Returns a data bank.
 172:    *
 173:    * @param bank the bank index.
 174:    * @return A data bank.
 175:    */
 176:   public int[] getData(int bank)
 177:   {
 178:     return bankData[bank];
 179:   }
 180: 
 181:   /**
 182:    * Returns the array underlying this <code>DataBuffer</code>.
 183:    *
 184:    * @return The data banks.
 185:    */
 186:   public int[][] getBankData()
 187:   {
 188:     return bankData;
 189:   }
 190: 
 191:   /**
 192:    * Returns an element from the first data bank.  The <code>offset</code> is
 193:    * added to the specified index before accessing the underlying data array.
 194:    *
 195:    * @param i the element index.
 196:    * @return The element.
 197:    */
 198:   public int getElem(int i)
 199:   {
 200:     return data[i+offset];
 201:   }
 202: 
 203:   /**
 204:    * Returns an element from a particular data bank.  The <code>offset</code>
 205:    * is added to the specified index before accessing the underlying data
 206:    * array.
 207:    *
 208:    * @param bank the bank index.
 209:    * @param i the element index.
 210:    * @return The element.
 211:    */
 212:   public int getElem(int bank, int i)
 213:   {
 214:     // get unsigned int as int
 215:     return bankData[bank][i+offsets[bank]];
 216:   }
 217: 
 218:   /**
 219:    * Sets an element in the first data bank.  The offset (specified in the
 220:    * constructor) is added to <code>i</code> before updating the underlying
 221:    * data array.
 222:    *
 223:    * @param i the element index.
 224:    * @param val the new element value.
 225:    */
 226:   public void setElem(int i, int val)
 227:   {
 228:     data[i+offset] = val;
 229:   }
 230: 
 231:   /**
 232:    * Sets an element in a particular data bank.  The offset (specified in the
 233:    * constructor) is added to <code>i</code> before updating the underlying
 234:    * data array.
 235:    *
 236:    * @param bank the data bank index.
 237:    * @param i the element index.
 238:    * @param val the new element value.
 239:    */
 240:   public void setElem(int bank, int i, int val)
 241:   {
 242:     bankData[bank][i+offsets[bank]] = val;
 243:   }
 244: }