1 /* Copyright (C) 2000, 2002, 2005 Free Software Foundation
3 This file is part of GNU Classpath.
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)
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.
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
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
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. */
37 package java
.awt
.image
;
40 * Class that manages arrays of data elements. A data buffer consists
41 * of one or more banks. A bank is a continuous region of data
44 * @author Rolf W. Rasmussen (rolfwr@ii.uib.no)
46 public abstract class DataBuffer
49 * A constant representing a data type that uses <code>byte</code> primitives
50 * as the storage unit.
52 public static final int TYPE_BYTE
= 0;
55 * A constant representing a data type that uses <code>short</code>
56 * primitives as the storage unit.
58 public static final int TYPE_USHORT
= 1;
61 * A constant representing a data type that uses <code>short</code>
62 * primitives as the storage unit.
64 public static final int TYPE_SHORT
= 2;
67 * A constant representing a data type that uses <code>int</code>
68 * primitives as the storage unit.
70 public static final int TYPE_INT
= 3;
73 * A constant representing a data type that uses <code>float</code>
74 * primitives as the storage unit.
76 public static final int TYPE_FLOAT
= 4;
79 * A constant representing a data type that uses <code>double</code>
80 * primitives as the storage unit.
82 public static final int TYPE_DOUBLE
= 5;
85 * A constant representing an undefined data type.
87 public static final int TYPE_UNDEFINED
= 32;
89 /** The type of the data elements stored in the data buffer. */
90 protected int dataType
;
92 /** The number of banks in this buffer. */
93 protected int banks
= 1;
95 /** Offset into the default (0'th) bank). */
96 protected int offset
; // FIXME: Is offsets[0] always mirrored in offset?
98 /** The size of the banks. */
101 /** Offset into each bank. */
102 protected int[] offsets
;
105 * Creates a new <code>DataBuffer</code> with the specified data type and
106 * size. The <code>dataType</code> should be one of the constants
107 * {@link #TYPE_BYTE}, {@link #TYPE_SHORT}, {@link #TYPE_USHORT},
108 * {@link #TYPE_INT}, {@link #TYPE_FLOAT} and {@link #TYPE_DOUBLE}.
110 * The physical (array-based) storage is allocated by a subclass.
112 * @param dataType the data type.
113 * @param size the number of elements in the buffer.
115 protected DataBuffer(int dataType
, int size
)
117 this(dataType
, size
, 1);
121 * Creates a new <code>DataBuffer</code> with the specified data type,
122 * size and number of banks. The <code>dataType</code> should be one of
123 * the constants {@link #TYPE_BYTE}, {@link #TYPE_SHORT},
124 * {@link #TYPE_USHORT}, {@link #TYPE_INT}, {@link #TYPE_FLOAT} and
125 * {@link #TYPE_DOUBLE}.
127 * The physical (array-based) storage is allocated by a subclass.
129 * @param dataType the data type.
130 * @param size the number of elements in the buffer.
131 * @param numBanks the number of data banks.
133 protected DataBuffer(int dataType
, int size
, int numBanks
) {
134 this(dataType
, size
, numBanks
, 0);
138 * Creates a new <code>DataBuffer</code> with the specified data type,
139 * size and number of banks. An offset (which applies to all banks) is
140 * also specified. The <code>dataType</code> should be one of
141 * the constants {@link #TYPE_BYTE}, {@link #TYPE_SHORT},
142 * {@link #TYPE_USHORT}, {@link #TYPE_INT}, {@link #TYPE_FLOAT} and
143 * {@link #TYPE_DOUBLE}.
145 * The physical (array-based) storage is allocated by a subclass.
147 * @param dataType the data type.
148 * @param size the number of elements in the buffer.
149 * @param numBanks the number of data banks.
150 * @param offset the offset to the first element for all banks.
152 protected DataBuffer(int dataType
, int size
, int numBanks
, int offset
) {
154 this.dataType
= dataType
;
156 this.offset
= offset
;
158 offsets
= new int[ numBanks
];
159 for(int i
= 0; i
< numBanks
; i
++ )
164 * Creates a new <code>DataBuffer</code> with the specified data type,
165 * size and number of banks. An offset (which applies to all banks) is
166 * also specified. The <code>dataType</code> should be one of
167 * the constants {@link #TYPE_BYTE}, {@link #TYPE_SHORT},
168 * {@link #TYPE_USHORT}, {@link #TYPE_INT}, {@link #TYPE_FLOAT} and
169 * {@link #TYPE_DOUBLE}.
171 * The physical (array-based) storage is allocated by a subclass.
173 * @param dataType the data type.
174 * @param size the number of elements in the buffer.
175 * @param numBanks the number of data banks.
176 * @param offsets the offsets to the first element for all banks.
178 * @throws ArrayIndexOutOfBoundsException if
179 * <code>numBanks != offsets.length</code>.
181 protected DataBuffer(int dataType
, int size
, int numBanks
, int[] offsets
) {
182 if (numBanks
!= offsets
.length
)
183 throw new ArrayIndexOutOfBoundsException();
185 this.dataType
= dataType
;
188 this.offsets
= offsets
;
194 * Returns the size (number of bits) of the specified data type. Valid types
195 * are defined by the constants {@link #TYPE_BYTE}, {@link #TYPE_SHORT},
196 * {@link #TYPE_USHORT}, {@link #TYPE_INT}, {@link #TYPE_FLOAT} and
197 * {@link #TYPE_DOUBLE}.
199 * @param dataType the data type.
200 * @return The number of bits for the specified data type.
201 * @throws IllegalArgumentException if <code>dataType < 0</code> or
202 * <code>dataType > TYPE_DOUBLE</code>.
204 public static int getDataTypeSize(int dataType
) {
205 // Maybe this should be a lookup table instead.
219 throw new IllegalArgumentException();
224 * Returns the type of the data elements in the data buffer. Valid types
225 * are defined by the constants {@link #TYPE_BYTE}, {@link #TYPE_SHORT},
226 * {@link #TYPE_USHORT}, {@link #TYPE_INT}, {@link #TYPE_FLOAT} and
227 * {@link #TYPE_DOUBLE}.
231 public int getDataType()
237 * Returns the size of the data buffer.
247 * Returns the element offset for the first data bank.
249 * @return The element offset.
251 public int getOffset()
257 * Returns the offsets for all the data banks used by this
258 * <code>DataBuffer</code>.
260 * @return The offsets.
262 public int[] getOffsets()
266 // is this necessary?
267 offsets
= new int[1];
274 * Returns the number of data banks for this <code>DataBuffer</code>.
275 * @return The number of data banks.
277 public int getNumBanks()
283 * Returns an element from the first data bank. The offset (specified in
284 * the constructor) is added to <code>i</code> before accessing the
285 * underlying data array.
287 * @param i the element index.
288 * @return The element.
290 public int getElem(int i
)
292 return getElem(0, i
);
296 * Returns an element from a particular data bank. The offset (specified in
297 * the constructor) is added to <code>i</code> before accessing the
298 * underlying data array.
300 * @param bank the bank index.
301 * @param i the element index.
302 * @return The element.
304 public abstract int getElem(int bank
, int i
);
307 * Sets an element in the first data bank. The offset (specified in the
308 * constructor) is added to <code>i</code> before updating the underlying
311 * @param i the element index.
312 * @param val the new element value.
314 public void setElem(int i
, int val
)
320 * Sets an element in a particular data bank. The offset (specified in the
321 * constructor) is added to <code>i</code> before updating the underlying
324 * @param bank the data bank index.
325 * @param i the element index.
326 * @param val the new element value.
328 public abstract void setElem(int bank
, int i
, int val
);
331 * Returns an element from the first data bank, converted to a
332 * <code>float</code>. The offset (specified in the constructor) is added
333 * to <code>i</code> before accessing the underlying data array.
335 * @param i the element index.
336 * @return The element.
338 public float getElemFloat(int i
)
344 * Returns an element from a particular data bank, converted to a
345 * <code>float</code>. The offset (specified in the constructor) is
346 * added to <code>i</code> before accessing the underlying data array.
348 * @param bank the bank index.
349 * @param i the element index.
350 * @return The element.
352 public float getElemFloat(int bank
, int i
)
354 return getElem(bank
, i
);
358 * Sets an element in the first data bank. The offset (specified in the
359 * constructor) is added to <code>i</code> before updating the underlying
362 * @param i the element index.
363 * @param val the new element value.
365 public void setElemFloat(int i
, float val
)
367 setElem(i
, (int) val
);
371 * Sets an element in a particular data bank. The offset (specified in the
372 * constructor) is added to <code>i</code> before updating the underlying
375 * @param bank the data bank index.
376 * @param i the element index.
377 * @param val the new element value.
379 public void setElemFloat(int bank
, int i
, float val
)
381 setElem(bank
, i
, (int) val
);
385 * Returns an element from the first data bank, converted to a
386 * <code>double</code>. The offset (specified in the constructor) is added
387 * to <code>i</code> before accessing the underlying data array.
389 * @param i the element index.
390 * @return The element.
392 public double getElemDouble(int i
)
398 * Returns an element from a particular data bank, converted to a
399 * <code>double</code>. The offset (specified in the constructor) is
400 * added to <code>i</code> before accessing the underlying data array.
402 * @param bank the bank index.
403 * @param i the element index.
404 * @return The element.
406 public double getElemDouble(int bank
, int i
)
408 return getElem(bank
, i
);
412 * Sets an element in the first data bank. The offset (specified in the
413 * constructor) is added to <code>i</code> before updating the underlying
416 * @param i the element index.
417 * @param val the new element value.
419 public void setElemDouble(int i
, double val
)
421 setElem(i
, (int) val
);
425 * Sets an element in a particular data bank. The offset (specified in the
426 * constructor) is added to <code>i</code> before updating the underlying
429 * @param bank the data bank index.
430 * @param i the element index.
431 * @param val the new element value.
433 public void setElemDouble(int bank
, int i
, double val
)
435 setElem(bank
, i
, (int) val
);