1 /* DataBufferUShort.java --
2    Copyright (C) 2000, 2002, 2004, 2005 Free Software Foundation
3 
4 This file is part of GNU Classpath.
5 
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10 
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20 
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25 
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37 
38 package java.awt.image;
39 
40 /* This is one of several classes that are nearly identical. Maybe we
41    should have a central template and generate all these files. This
42    is one of the cases where templates or macros would have been
43    useful to have in Java.
44 
45    This file has been created using search-replace. My only fear is
46    that these classes will grow out-of-sync as of a result of changes
47    that are not propagated to the other files. As always, mirroring
48    code is a maintenance nightmare.  */
49 
50 /**
51  * A {@link DataBuffer} that uses an array of <code>short</code> primitives
52  * to represent each of its banks.
53  *
54  * @author Rolf W. Rasmussen (rolfwr@ii.uib.no)
55  */
56 public final class DataBufferUShort extends DataBuffer
57 {
58   private short[] data;
59   private short[][] bankData;
60 
61   /**
62    * Creates a new data buffer with a single data bank containing the
63    * specified number of <code>short</code> elements.
64    *
65    * @param size the number of elements in the data bank.
66    */
DataBufferUShort(int size)67   public DataBufferUShort(int size)
68   {
69     super(TYPE_USHORT, size, 1, 0);
70     bankData = new short[1][];
71     data = new short[size];
72     bankData[0] = data;
73   }
74 
75   /**
76    * Creates a new data buffer with the specified number of data banks,
77    * each containing the specified number of <code>short</code> elements.
78    *
79    * @param size the number of elements in the data bank.
80    * @param numBanks the number of data banks.
81    */
DataBufferUShort(int size, int numBanks)82   public DataBufferUShort(int size, int numBanks)
83   {
84     super(TYPE_USHORT, size, numBanks);
85     bankData = new short[numBanks][size];
86     data = bankData[0];
87   }
88 
89   /**
90    * Creates a new data buffer backed by the specified data bank.
91    *
92    * @param dataArray the data bank.
93    * @param size the number of elements in the data bank.
94    *
95    * @throws NullPointerException if dataArray is null
96    */
DataBufferUShort(short[] dataArray, int size)97   public DataBufferUShort(short[] dataArray, int size)
98   {
99     super(TYPE_USHORT, size, 1, 0);
100     if (dataArray == null)
101       throw new NullPointerException();
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    *
111    * @param dataArray the data bank.
112    * @param size the number of elements in the data bank.
113    * @param offset the offset to the first element in the array.
114    *
115    * @throws NullPointerException if dataArray is null
116    */
DataBufferUShort(short[] dataArray, int size, int offset)117   public DataBufferUShort(short[] dataArray, int size, int offset)
118   {
119     super(TYPE_USHORT, size, 1, offset);
120     if (dataArray == null)
121       throw new NullPointerException();
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    */
DataBufferUShort(short[][] dataArray, int size)136   public DataBufferUShort(short[][] dataArray, int size)
137   {
138     super(TYPE_USHORT, 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    */
DataBufferUShort(short[][] dataArray, int size, int[] offsets)154   public DataBufferUShort(short[][] dataArray, int size, int[] offsets)
155   {
156     super(TYPE_USHORT, 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    */
getData()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    */
getData(int bank)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    */
getBankData()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    */
getElem(int i)200   public int getElem(int i)
201   {
202     return data[i+offset] & 0xffff; // get unsigned short as int
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    */
getElem(int bank, int i)214   public int getElem(int bank, int i)
215   {
216     // get unsigned short as int
217     return bankData[bank][i+offsets[bank]] & 0xffff;
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    */
setElem(int i, int val)228   public void setElem(int i, int val)
229   {
230     data[i+offset] = (short) 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    */
setElem(int bank, int i, int val)242   public void setElem(int bank, int i, int val)
243   {
244     bankData[bank][i+offsets[bank]] = (short) val;
245   }
246 }
247