Source for java.awt.image.DataBufferFloat

   1: /* Copyright (C) 2004, 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>float</code> primitives
  51:  * to represent each of its banks. 
  52:  * 
  53:  * @author Rolf W. Rasmussen (rolfwr@ii.uib.no)
  54:  * @author Sascha Brawer (brawer@dandelis.ch)
  55:  */
  56: public final class DataBufferFloat
  57:   extends DataBuffer
  58: {
  59:   private float[] data;
  60:   private float[][] bankData;
  61:   
  62:   /**
  63:    * Creates a new data buffer with a single data bank containing the 
  64:    * specified number of <code>float</code> elements.
  65:    * 
  66:    * @param size the number of elements in the data bank.
  67:    */
  68:   public DataBufferFloat(int size)
  69:   {
  70:     super(TYPE_FLOAT, size, 1, 0);
  71:     bankData = new float[1][];
  72:     data = new float[size];
  73:     bankData[0] = data;
  74:   }
  75: 
  76:   /**
  77:    * Creates a new data buffer with the specified number of data banks, 
  78:    * each containing the specified number of <code>float</code> elements.
  79:    * 
  80:    * @param size the number of elements in the data bank.
  81:    * @param numBanks the number of data banks.
  82:    */
  83:   public DataBufferFloat(int size, int numBanks)
  84:   {
  85:     super(TYPE_FLOAT, size, numBanks);
  86:     bankData = new float[numBanks][size];
  87:     data = bankData[0];
  88:   }
  89: 
  90:   /**
  91:    * Creates a new data buffer backed by the specified data bank.
  92:    * <p>
  93:    * Note: there is no exception when <code>dataArray</code> is 
  94:    * <code>null</code>, but in that case an exception will be thrown
  95:    * later if you attempt to access the data buffer.
  96:    * 
  97:    * @param dataArray the data bank.
  98:    * @param size the number of elements in the data bank.
  99:    */
 100:   public DataBufferFloat(float[] dataArray, int size)
 101:   {
 102:     super(TYPE_FLOAT, size, 1, 0);
 103:     bankData = new float[1][];
 104:     data = dataArray;
 105:     bankData[0] = data;
 106:   }
 107:     
 108:   /**
 109:    * Creates a new data buffer backed by the specified data bank, with
 110:    * the specified offset to the first element.
 111:    * <p>
 112:    * Note: there is no exception when <code>dataArray</code> is 
 113:    * <code>null</code>, but in that case an exception will be thrown
 114:    * later if you attempt to access the data buffer.
 115:    * 
 116:    * @param dataArray the data bank.
 117:    * @param size the number of elements in the data bank.
 118:    * @param offset the offset to the first element in the array.
 119:    */
 120:   public DataBufferFloat(float[] dataArray, int size, int offset)
 121:   {
 122:     super(TYPE_FLOAT, size, 1, offset);
 123:     bankData = new float[1][];
 124:     data = dataArray;
 125:     bankData[0] = data;
 126:   }
 127: 
 128:   /**
 129:    * Creates a new data buffer backed by the specified data banks.
 130:    * 
 131:    * @param dataArray the data banks.
 132:    * @param size the number of elements in the data bank.
 133:    * 
 134:    * @throws NullPointerException if <code>dataArray</code> is 
 135:    *         <code>null</code>.
 136:    */
 137:   public DataBufferFloat(float[][] dataArray, int size)
 138:   {
 139:     super(TYPE_FLOAT, size, dataArray.length);
 140:     bankData = dataArray;
 141:     data = bankData[0];
 142:   }
 143: 
 144:   /**
 145:    * Creates a new data buffer backed by the specified data banks, with
 146:    * the specified offsets to the first element in each bank.
 147:    * 
 148:    * @param dataArray the data banks.
 149:    * @param size the number of elements in the data bank.
 150:    * @param offsets the offsets to the first element in each data bank.
 151:    * 
 152:    * @throws NullPointerException if <code>dataArray</code> is 
 153:    *         <code>null</code>.
 154:    */
 155:   public DataBufferFloat(float[][] dataArray, int size, int[] offsets)
 156:   {
 157:     super(TYPE_FLOAT, size, dataArray.length, offsets);
 158:     bankData = dataArray;
 159:     data = bankData[0];
 160:   }
 161: 
 162:   /**
 163:    * Returns the first data bank.
 164:    * 
 165:    * @return The first data bank.
 166:    */
 167:   public float[] getData()
 168:   {
 169:     return data;
 170:   }
 171:     
 172:   /**
 173:    * Returns a data bank.
 174:    * 
 175:    * @param bank the bank index.
 176:    * @return A data bank.
 177:    */
 178:   public float[] getData(int bank)
 179:   {
 180:     return bankData[bank];
 181:   }
 182:     
 183:   /**
 184:    * Returns the array underlying this <code>DataBuffer</code>.
 185:    * 
 186:    * @return The data banks.
 187:    */
 188:   public float[][] getBankData()
 189:   {
 190:     return bankData;
 191:   }
 192:   
 193:   /**
 194:    * Returns an element from the first data bank.  The offset (specified in
 195:    * the constructor) is added to <code>i</code> before accessing the 
 196:    * underlying data array.
 197:    * 
 198:    * @param i the element index.
 199:    * @return The element.
 200:    */
 201:   public int getElem(int i)
 202:   {
 203:     return (int) data[i+offset];
 204:   }
 205: 
 206:   /**
 207:    * Returns an element from a particular data bank.  The offset (specified in
 208:    * the constructor) is added to <code>i</code> before accessing the 
 209:    * underlying data array.
 210:    * 
 211:    * @param bank the bank index.
 212:    * @param i the element index.
 213:    * @return The element.
 214:    */
 215:   public int getElem(int bank, int i)
 216:   {
 217:     return (int) bankData[bank][i+offsets[bank]];
 218:   }
 219: 
 220:   /**
 221:    * Sets an element in the first data bank.  The offset (specified in the
 222:    * constructor) is added to <code>i</code> before updating the underlying
 223:    * data array.
 224:    * 
 225:    * @param i the element index.
 226:    * @param val the new element value.
 227:    */
 228:   public void setElem(int i, int val)
 229:   {
 230:     data[i+offset] = val;
 231:   }
 232: 
 233:   /**
 234:    * Sets an element in a particular data bank.  The offset (specified in the
 235:    * constructor) is added to <code>i</code> before updating the underlying
 236:    * data array.
 237:    * 
 238:    * @param bank the data bank index.
 239:    * @param i the element index.
 240:    * @param val the new element value.
 241:    */
 242:   public void setElem(int bank, int i, int val)
 243:   {
 244:     bankData[bank][i+offsets[bank]] = val;
 245:   }
 246: 
 247:   public float getElemFloat(int i)
 248:   {
 249:     return data[i+offset];
 250:   }
 251:     
 252:   public float getElemFloat(int bank, int i)
 253:   {
 254:     return bankData[bank][i+offsets[bank]];
 255:   }
 256: 
 257:   public void setElemFloat(int i, float val)
 258:   {
 259:     data[i+offset] = val;
 260:   }
 261: 
 262:   public void setElemFloat(int bank, int i, float val)
 263:   {
 264:     bankData[bank][i+offsets[bank]] = val;
 265:   }
 266: 
 267:   public double getElemDouble(int i)
 268:   {
 269:     return getElemFloat(i);
 270:   }
 271:     
 272:   public double getElemDouble(int bank, int i)
 273:   {
 274:     return getElemFloat(bank, i);
 275:   }
 276: 
 277:   public void setElemDouble(int i, double val)
 278:   {
 279:     setElemFloat(i, (float) val);
 280:   }
 281: 
 282:   public void setElemDouble(int bank, int i, double val)
 283:   {
 284:     setElemFloat(bank, i, (float) val);
 285:   }
 286: }