KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > image > DataBuffer


1 /*
2  * @(#)DataBuffer.java 1.29 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 /* ****************************************************************
9  ******************************************************************
10  ******************************************************************
11  *** COPYRIGHT (c) Eastman Kodak Company, 1997
12  *** As an unpublished work pursuant to Title 17 of the United
13  *** States Code. All rights reserved.
14  ******************************************************************
15  ******************************************************************
16  ******************************************************************/

17
18 package java.awt.image;
19
20 /**
21  * This class exists to wrap one or more data arrays. Each data array in
22  * the DataBuffer is referred to as a bank. Accessor methods for getting
23  * and setting elements of the DataBuffer's banks exist with and without
24  * a bank specifier. The methods without a bank specifier use the default 0th
25  * bank. The DataBuffer can optionally take an offset per bank, so that
26  * data in an existing array can be used even if the interesting data
27  * doesn't start at array location zero. Getting or setting the 0th
28  * element of a bank, uses the (0+offset)th element of the array. The
29  * size field specifies how much of the data array is available for
30  * use. Size + offset for a given bank should never be greater
31  * than the length of the associated data array. The data type of
32  * a data buffer indicates the type of the data array(s) and may also
33  * indicate additional semantics, e.g. storing unsigned 8-bit data
34  * in elements of a byte array. The data type may be TYPE_UNDEFINED
35  * or one of the types defined below. Other types may be added in
36  * the future. Generally, an object of class DataBuffer will be cast down
37  * to one of its data type specific subclasses to access data type specific
38  * methods for improved performance. Currently, the Java 2D(tm) API
39  * image classes use TYPE_BYTE, TYPE_USHORT, TYPE_INT, TYPE_SHORT,
40  * TYPE_FLOAT, and TYPE_DOUBLE DataBuffers to store image data.
41  * @see java.awt.image.Raster
42  * @see java.awt.image.SampleModel
43  */

44
45 public abstract class DataBuffer
46 {
47
48     /** Tag for unsigned byte data. */
49     public static final int TYPE_BYTE = 0;
50
51     /** Tag for unsigned short data. */
52     public static final int TYPE_USHORT = 1;
53  
54     /** Tag for signed short data. Placeholder for future use. */
55     public static final int TYPE_SHORT = 2;
56
57     /** Tag for int data. */
58     public static final int TYPE_INT = 3;
59
60     /** Tag for float data. Placeholder for future use. */
61     public static final int TYPE_FLOAT = 4;
62
63     /** Tag for double data. Placeholder for future use. */
64     public static final int TYPE_DOUBLE = 5;
65
66     /** Tag for undefined data. */
67     public static final int TYPE_UNDEFINED = 32;
68  
69     /** The data type of this DataBuffer. */
70     protected int dataType;
71  
72     /** The number of banks in this DataBuffer. */
73     protected int banks;
74
75     /** Offset into default (first) bank from which to get the first element. */
76     protected int offset;
77  
78     /** Usable size of all banks. */
79     protected int size;
80
81     /** Offsets into all banks. */
82     protected int offsets[];
83
84     /** Size of the data types indexed by DataType tags defined above. */
85     private static final int dataTypeSize[] = {8,16,16,32,32,64};
86  
87     /** Returns the size (in bits) of the data type, given a datatype tag.
88       * @param type the value of one of the defined datatype tags
89       * @return the size of the data type
90       * @throws IllegalArgumentException if <code>type</code> is less than
91       * zero or greater than {@link #TYPE_DOUBLE}
92       */

93     public static int getDataTypeSize(int type) {
94         if (type < TYPE_BYTE || type > TYPE_DOUBLE) {
95             throw new IllegalArgumentException JavaDoc("Unknown data type "+type);
96         }
97         return dataTypeSize[type];
98     }
99
100     /**
101      * Constructs a DataBuffer containing one bank of the specified
102      * data type and size.
103      * @param dataType the data type of this <code>DataBuffer</code>
104      * @param size the size of the banks
105      */

106     protected DataBuffer(int dataType, int size) {
107         this.dataType = dataType;
108         this.banks = 1;
109         this.size = size;
110         this.offset = 0;
111         this.offsets = new int[1]; // init to 0 by new
112
}
113
114     /**
115      * Constructs a DataBuffer containing the specified number of
116      * banks. Each bank has the specified size and an offset of 0.
117      * @param dataType the data type of this <code>DataBuffer</code>
118      * @param size the size of the banks
119      * @param numBanks the number of banks in this
120      * <code>DataBuffer</code>
121      */

122     protected DataBuffer(int dataType, int size, int numBanks) {
123         this.dataType = dataType;
124         this.banks = numBanks;
125         this.size = size;
126         this.offset = 0;
127         this.offsets = new int[banks]; // init to 0 by new
128
}
129
130     /**
131      * Constructs a DataBuffer that contains the specified number
132      * of banks. Each bank has the specified datatype, size and offset.
133      * @param dataType the data type of this <code>DataBuffer</code>
134      * @param size the size of the banks
135      * @param numBanks the number of banks in this
136      * <code>DataBuffer</code>
137      * @param offset the offset for each bank
138      */

139     protected DataBuffer(int dataType, int size, int numBanks, int offset) {
140         this.dataType = dataType;
141         this.banks = numBanks;
142         this.size = size;
143         this.offset = offset;
144         this.offsets = new int[numBanks];
145         for (int i = 0; i < numBanks; i++) {
146             this.offsets[i] = offset;
147         }
148     }
149
150     /**
151      * Constructs a DataBuffer which contains the specified number
152      * of banks. Each bank has the specified datatype and size. The
153      * offset for each bank is specified by its respective entry in
154      * the offsets array.
155      * @param dataType the data type of this <code>DataBuffer</code>
156      * @param size the size of the banks
157      * @param numBanks the number of banks in this
158      * <code>DataBuffer</code>
159      * @param offsets an array containing an offset for each bank.
160      * @throws ArrayIndexOutOfBoundsException if <code>numBanks</code>
161      * does not equal the length of <code>offsets</code>
162      */

163     protected DataBuffer(int dataType, int size, int numBanks, int offsets[]) {
164         if (numBanks != offsets.length) {
165             throw new ArrayIndexOutOfBoundsException JavaDoc("Number of banks" +
166                  " does not match number of bank offsets");
167         }
168         this.dataType = dataType;
169         this.banks = numBanks;
170         this.size = size;
171         this.offset = offsets[0];
172         this.offsets = (int[])offsets.clone();
173     }
174  
175     /** Returns the data type of this DataBuffer.
176      * @return the data type of this <code>DataBuffer</code>.
177      */

178     public int getDataType() {
179         return dataType;
180     }
181   
182     /** Returns the size (in array elements) of all banks.
183      * @return the size of all banks.
184      */

185     public int getSize() {
186         return size;
187     }
188
189     /** Returns the offset of the default bank in array elements.
190      * @return the offset of the default bank.
191      */

192     public int getOffset() {
193         return offset;
194     }
195  
196     /** Returns the offsets (in array elements) of all the banks.
197      * @return the offsets of all banks.
198      */

199     public int[] getOffsets() {
200         return (int[])offsets.clone();
201     }
202
203     /** Returns the number of banks in this DataBuffer.
204      * @return the number of banks.
205      */

206     public int getNumBanks() {
207         return banks;
208     }
209
210     /**
211      * Returns the requested data array element from the first (default) bank
212      * as an integer.
213      * @param i the index of the requested data array element
214      * @return the data array element at the specified index.
215      * @see #setElem(int, int)
216      * @see #setElem(int, int, int)
217      */

218     public int getElem(int i) {
219         return getElem(0,i);
220     }
221
222     /**
223      * Returns the requested data array element from the specified bank
224      * as an integer.
225      * @param bank the specified bank
226      * @param i the index of the requested data array element
227      * @return the data array element at the specified index from the
228      * specified bank at the specified index.
229      * @see #setElem(int, int)
230      * @see #setElem(int, int, int)
231      */

232     public abstract int getElem(int bank, int i);
233
234     /**
235      * Sets the requested data array element in the first (default) bank
236      * from the given integer.
237      * @param i the specified index into the data array
238      * @param val the data to set the element at the specified index in
239      * the data array
240      * @see #getElem(int)
241      * @see #getElem(int, int)
242      */

243     public void setElem(int i, int val) {
244         setElem(0,i,val);
245     }
246
247     /**
248      * Sets the requested data array element in the specified bank
249      * from the given integer.
250      * @param bank the specified bank
251      * @param i the specified index into the data array
252      * @param val the data to set the element in the specified bank
253      * at the specified index in the data array
254      * @see #getElem(int)
255      * @see #getElem(int, int)
256      */

257     public abstract void setElem(int bank, int i, int val);
258
259     /**
260      * Returns the requested data array element from the first (default) bank
261      * as a float. The implementation in this class is to cast getElem(i)
262      * to a float. Subclasses may override this method if another
263      * implementation is needed.
264      * @param i the index of the requested data array element
265      * @return a float value representing the data array element at the
266      * specified index.
267      * @see #setElemFloat(int, float)
268      * @see #setElemFloat(int, int, float)
269      */

270     public float getElemFloat(int i) {
271         return (float)getElem(i);
272     }
273
274     /**
275      * Returns the requested data array element from the specified bank
276      * as a float. The implementation in this class is to cast
277      * {@link #getElem(int, int)}
278      * to a float. Subclasses can override this method if another
279      * implementation is needed.
280      * @param bank the specified bank
281      * @param i the index of the requested data array element
282      * @return a float value representing the data array element from the
283      * specified bank at the specified index.
284      * @see #setElemFloat(int, float)
285      * @see #setElemFloat(int, int, float)
286      */

287     public float getElemFloat(int bank, int i) {
288         return (float)getElem(bank,i);
289     }
290
291     /**
292      * Sets the requested data array element in the first (default) bank
293      * from the given float. The implementation in this class is to cast
294      * val to an int and call {@link #setElem(int, int)}. Subclasses
295      * can override this method if another implementation is needed.
296      * @param i the specified index
297      * @param val the value to set the element at the specified index in
298      * the data array
299      * @see #getElemFloat(int)
300      * @see #getElemFloat(int, int)
301      */

302     public void setElemFloat(int i, float val) {
303         setElem(i,(int)val);
304     }
305
306     /**
307      * Sets the requested data array element in the specified bank
308      * from the given float. The implementation in this class is to cast
309      * val to an int and call {@link #setElem(int, int)}. Subclasses can
310      * override this method if another implementation is needed.
311      * @param bank the specified bank
312      * @param i the specified index
313      * @param val the value to set the element in the specified bank at
314      * the specified index in the data array
315      * @see #getElemFloat(int)
316      * @see #getElemFloat(int, int)
317      */

318     public void setElemFloat(int bank, int i, float val) {
319         setElem(bank,i,(int)val);
320     }
321
322     /**
323      * Returns the requested data array element from the first (default) bank
324      * as a double. The implementation in this class is to cast
325      * {@link #getElem(int)}
326      * to a double. Subclasses can override this method if another
327      * implementation is needed.
328      * @param i the specified index
329      * @return a double value representing the element at the specified
330      * index in the data array.
331      * @see #setElemDouble(int, double)
332      * @see #setElemDouble(int, int, double)
333      */

334     public double getElemDouble(int i) {
335         return (double)getElem(i);
336     }
337
338     /**
339      * Returns the requested data array element from the specified bank as
340      * a double. The implementation in this class is to cast getElem(bank, i)
341      * to a double. Subclasses may override this method if another
342      * implementation is needed.
343      * @param bank the specified bank
344      * @param i the specified index
345      * @return a double value representing the element from the specified
346      * bank at the specified index in the data array.
347      * @see #setElemDouble(int, double)
348      * @see #setElemDouble(int, int, double)
349      */

350     public double getElemDouble(int bank, int i) {
351         return (double)getElem(bank,i);
352     }
353
354     /**
355      * Sets the requested data array element in the first (default) bank
356      * from the given double. The implementation in this class is to cast
357      * val to an int and call {@link #setElem(int, int)}. Subclasses can
358      * override this method if another implementation is needed.
359      * @param i the specified index
360      * @param val the value to set the element at the specified index
361      * in the data array
362      * @see #getElemDouble(int)
363      * @see #getElemDouble(int, int)
364      */

365     public void setElemDouble(int i, double val) {
366         setElem(i,(int)val);
367     }
368
369     /**
370      * Sets the requested data array element in the specified bank
371      * from the given double. The implementation in this class is to cast
372      * val to an int and call {@link #setElem(int, int)}. Subclasses can
373      * override this method if another implementation is needed.
374      * @param bank the specified bank
375      * @param i the specified index
376      * @param val the value to set the element in the specified bank
377      * at the specified index of the data array
378      * @see #getElemDouble(int)
379      * @see #getElemDouble(int, int)
380      */

381     public void setElemDouble(int bank, int i, double val) {
382         setElem(bank,i,(int)val);
383     }
384
385     static int[] toIntArray(Object JavaDoc obj) {
386         if (obj instanceof int[]) {
387             return (int[])obj;
388         } else if (obj == null) {
389             return null;
390         } else if (obj instanceof short[]) {
391             short sdata[] = (short[])obj;
392             int idata[] = new int[sdata.length];
393             for (int i = 0; i < sdata.length; i++) {
394                 idata[i] = (int)sdata[i] & 0xffff;
395             }
396             return idata;
397         } else if (obj instanceof byte[]) {
398             byte bdata[] = (byte[])obj;
399             int idata[] = new int[bdata.length];
400             for (int i = 0; i < bdata.length; i++) {
401                 idata[i] = 0xff & (int)bdata[i];
402             }
403             return idata;
404         }
405         return null;
406     }
407  }
408
Popular Tags