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