001/* Copyright (C) 2004, 2005  Free Software Foundation
002
003This file is part of GNU Classpath.
004
005GNU Classpath is free software; you can redistribute it and/or modify
006it under the terms of the GNU General Public License as published by
007the Free Software Foundation; either version 2, or (at your option)
008any later version.
009
010GNU Classpath is distributed in the hope that it will be useful, but
011WITHOUT ANY WARRANTY; without even the implied warranty of
012MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013General Public License for more details.
014
015You should have received a copy of the GNU General Public License
016along with GNU Classpath; see the file COPYING.  If not, write to the
017Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
01802110-1301 USA.
019
020Linking this library statically or dynamically with other modules is
021making a combined work based on this library.  Thus, the terms and
022conditions of the GNU General Public License cover the whole
023combination.
024
025As a special exception, the copyright holders of this library give you
026permission to link this library with independent modules to produce an
027executable, regardless of the license terms of these independent
028modules, and to copy and distribute the resulting executable under
029terms of your choice, provided that you also meet, for each linked
030independent module, the terms and conditions of the license of that
031module.  An independent module is a module which is not derived from
032or based on this library.  If you modify this library, you may extend
033this exception to your version of the library, but you are not
034obligated to do so.  If you do not wish to do so, delete this
035exception statement from your version. */
036
037package java.awt.image;
038
039/* This is one of several classes that are nearly identical. Maybe we
040   should have a central template and generate all these files. This
041   is one of the cases where templates or macros would have been
042   useful to have in Java.
043
044   This file has been created using search-replace. My only fear is
045   that these classes will grow out-of-sync as of a result of changes
046   that are not propagated to the other files. As always, mirroring
047   code is a maintenance nightmare.  */
048
049/**
050 * A {@link DataBuffer} that uses an array of <code>double</code> primitives
051 * to represent each of its banks. 
052 * 
053 * @since 1.4
054 *
055 * @author Rolf W. Rasmussen (rolfwr@ii.uib.no)
056 * @author Sascha Brawer (brawer@dandelis.ch)
057 */
058public final class DataBufferDouble
059  extends DataBuffer
060{
061  private double[] data;
062  private double[][] bankData;
063  
064  /**
065   * Creates a new data buffer with a single data bank containing the 
066   * specified number of <code>double</code> elements.
067   * 
068   * @param size the number of elements in the data bank.
069   */
070  public DataBufferDouble(int size)
071  {
072    super(TYPE_DOUBLE, size, 1, 0);
073    bankData = new double[1][];
074    data = new double[size];
075    bankData[0] = data;
076  }
077
078  /**
079   * Creates a new data buffer with the specified number of data banks, 
080   * each containing the specified number of <code>double</code> elements.
081   * 
082   * @param size the number of elements in the data bank.
083   * @param numBanks the number of data banks.
084   */
085  public DataBufferDouble(int size, int numBanks)
086  {
087    super(TYPE_DOUBLE, size, numBanks);
088    bankData = new double[numBanks][size];
089    data = bankData[0];
090  }
091
092  /**
093   * Creates a new data buffer backed by the specified data bank.
094   * <p>
095   * Note: there is no exception when <code>dataArray</code> is 
096   * <code>null</code>, but in that case an exception will be thrown
097   * later if you attempt to access the data buffer.
098   * 
099   * @param dataArray the data bank.
100   * @param size the number of elements in the data bank.
101   */
102  public DataBufferDouble(double[] dataArray, int size)
103  {
104    super(TYPE_DOUBLE, size, 1, 0);
105    bankData = new double[1][];
106    data = dataArray;
107    bankData[0] = data;
108  }
109    
110  /**
111   * Creates a new data buffer backed by the specified data bank, with
112   * the specified offset to the first element.
113   * <p>
114   * Note: there is no exception when <code>dataArray</code> is 
115   * <code>null</code>, but in that case an exception will be thrown
116   * later if you attempt to access the data buffer.
117   * 
118   * @param dataArray the data bank.
119   * @param size the number of elements in the data bank.
120   * @param offset the offset to the first element in the array.
121   */
122  public DataBufferDouble(double[] dataArray, int size, int offset)
123  {
124    super(TYPE_DOUBLE, size, 1, offset);
125    bankData = new double[1][];
126    data = dataArray;
127    bankData[0] = data;
128  }
129
130  /**
131   * Creates a new data buffer backed by the specified data banks.
132   * 
133   * @param dataArray the data banks.
134   * @param size the number of elements in the data bank.
135   * 
136   * @throws NullPointerException if <code>dataArray</code> is 
137   *         <code>null</code>.
138   */
139  public DataBufferDouble(double[][] dataArray, int size)
140  {
141    super(TYPE_DOUBLE, size, dataArray.length);
142    bankData = dataArray;
143    data = bankData[0];
144  }
145
146  /**
147   * Creates a new data buffer backed by the specified data banks, with
148   * the specified offsets to the first element in each bank.
149   * 
150   * @param dataArray the data banks.
151   * @param size the number of elements in the data bank.
152   * @param offsets the offsets to the first element in each data bank.
153   * 
154   * @throws NullPointerException if <code>dataArray</code> is 
155   *         <code>null</code>.
156   */
157  public DataBufferDouble(double[][] dataArray, int size, int[] offsets)
158  {
159    super(TYPE_DOUBLE, size, dataArray.length, offsets);
160    bankData = dataArray;
161    data = bankData[0];
162  }
163
164  /**
165   * Returns the first data bank.
166   * 
167   * @return The first data bank.
168   */
169  public double[] getData()
170  {
171    return data;
172  }
173    
174  /**
175   * Returns a data bank.
176   * 
177   * @param bank the bank index.
178   * @return A data bank.
179   */
180  public double[] getData(int bank)
181  {
182    return bankData[bank];
183  }
184    
185  /**
186   * Returns the array underlying this <code>DataBuffer</code>.
187   * 
188   * @return The data banks.
189   */
190  public double[][] getBankData()
191  {
192    return bankData;
193  }
194  
195  /**
196   * Returns an element from the first data bank.  The offset (specified in
197   * the constructor) is added to <code>i</code> before accessing the 
198   * underlying data array.
199   * 
200   * @param i the element index.
201   * @return The element.
202   */
203  public int getElem(int i)
204  {
205    return (int) data[i+offset];
206  }
207
208  /**
209   * Returns an element from a particular data bank.  The offset (specified in
210   * the constructor) is added to <code>i</code> before accessing the 
211   * underlying data array.
212   * 
213   * @param bank the bank index.
214   * @param i the element index.
215   * @return The element.
216   */
217  public int getElem(int bank, int i)
218  {
219    return (int) bankData[bank][i+offsets[bank]];
220  }
221
222  /**
223   * Sets an element in the first data bank.  The offset (specified in the
224   * constructor) is added to <code>i</code> before updating the underlying
225   * data array.
226   * 
227   * @param i the element index.
228   * @param val the new element value.
229   */
230  public void setElem(int i, int val)
231  {
232    data[i+offset] = val;
233  }
234
235  /**
236   * Sets an element in a particular data bank.  The offset (specified in the
237   * constructor) is added to <code>i</code> before updating the underlying
238   * data array.
239   * 
240   * @param bank the data bank index.
241   * @param i the element index.
242   * @param val the new element value.
243   */
244  public void setElem(int bank, int i, int val)
245  {
246    bankData[bank][i+offsets[bank]] = val;
247  }
248
249  public float getElemFloat(int i)
250  {
251    return (float) data[i+offset];
252  }
253    
254  public float getElemFloat(int bank, int i)
255  {
256    return (float) bankData[bank][i+offsets[bank]];
257  }
258
259  public void setElemFloat(int i, float val)
260  {
261    data[i+offset] = val;
262  }
263
264  public void setElemFloat(int bank, int i, float val)
265  {
266    bankData[bank][i+offsets[bank]] = val;
267  }
268
269  public double getElemDouble(int i)
270  {
271    return data[i + offset];
272  }
273    
274  public double getElemDouble(int bank, int i)
275  {
276    return bankData[bank][i + offsets[bank]];
277  }
278
279  public void setElemDouble(int i, double val)
280  {
281    data[i + offset] = val;
282  }
283
284  public void setElemDouble(int bank, int i, double val)
285  {
286    bankData[bank][i + offsets[bank]] = val;
287  }
288}