001    /* Copyright (C) 2004, 2005  Free Software Foundation
002    
003    This file is part of GNU Classpath.
004    
005    GNU Classpath is free software; you can redistribute it and/or modify
006    it under the terms of the GNU General Public License as published by
007    the Free Software Foundation; either version 2, or (at your option)
008    any later version.
009    
010    GNU Classpath is distributed in the hope that it will be useful, but
011    WITHOUT ANY WARRANTY; without even the implied warranty of
012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013    General Public License for more details.
014    
015    You should have received a copy of the GNU General Public License
016    along with GNU Classpath; see the file COPYING.  If not, write to the
017    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
018    02110-1301 USA.
019    
020    Linking this library statically or dynamically with other modules is
021    making a combined work based on this library.  Thus, the terms and
022    conditions of the GNU General Public License cover the whole
023    combination.
024    
025    As a special exception, the copyright holders of this library give you
026    permission to link this library with independent modules to produce an
027    executable, regardless of the license terms of these independent
028    modules, and to copy and distribute the resulting executable under
029    terms of your choice, provided that you also meet, for each linked
030    independent module, the terms and conditions of the license of that
031    module.  An independent module is a module which is not derived from
032    or based on this library.  If you modify this library, you may extend
033    this exception to your version of the library, but you are not
034    obligated to do so.  If you do not wish to do so, delete this
035    exception statement from your version. */
036    
037    package 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>float</code> primitives
051     * to represent each of its banks.
052     *
053     * @author Rolf W. Rasmussen (rolfwr@ii.uib.no)
054     * @author Sascha Brawer (brawer@dandelis.ch)
055     */
056    public final class DataBufferFloat
057      extends DataBuffer
058    {
059      private float[] data;
060      private float[][] bankData;
061    
062      /**
063       * Creates a new data buffer with a single data bank containing the
064       * specified number of <code>float</code> elements.
065       *
066       * @param size the number of elements in the data bank.
067       */
068      public DataBufferFloat(int size)
069      {
070        super(TYPE_FLOAT, size, 1, 0);
071        bankData = new float[1][];
072        data = new float[size];
073        bankData[0] = data;
074      }
075    
076      /**
077       * Creates a new data buffer with the specified number of data banks,
078       * each containing the specified number of <code>float</code> elements.
079       *
080       * @param size the number of elements in the data bank.
081       * @param numBanks the number of data banks.
082       */
083      public DataBufferFloat(int size, int numBanks)
084      {
085        super(TYPE_FLOAT, size, numBanks);
086        bankData = new float[numBanks][size];
087        data = bankData[0];
088      }
089    
090      /**
091       * Creates a new data buffer backed by the specified data bank.
092       * <p>
093       * Note: there is no exception when <code>dataArray</code> is
094       * <code>null</code>, but in that case an exception will be thrown
095       * later if you attempt to access the data buffer.
096       *
097       * @param dataArray the data bank.
098       * @param size the number of elements in the data bank.
099       */
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    }