Source code: org/ydp/jai/DataBufferDouble.java
1 /*
2 * The contents of this file are subject to the JAVA ADVANCED IMAGING
3 * SAMPLE INPUT-OUTPUT CODECS AND WIDGET HANDLING SOURCE CODE License
4 * Version 1.0 (the "License"); You may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 * http://www.sun.com/software/imaging/JAI/index.html
7 *
8 * Software distributed under the License is distributed on an "AS IS"
9 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
10 * the License for the specific language governing rights and limitations
11 * under the License.
12 *
13 * The Original Code is JAVA ADVANCED IMAGING SAMPLE INPUT-OUTPUT CODECS
14 * AND WIDGET HANDLING SOURCE CODE.
15 * The Initial Developer of the Original Code is: Sun Microsystems, Inc..
16 * Portions created by: _______________________________________
17 * are Copyright (C): _______________________________________
18 * All Rights Reserved.
19 * Contributor(s): _______________________________________
20 */
21
22 package org.ydp.jai;
23
24 import java.awt.image.DataBuffer;
25
26 /**
27 * An extension of <code>DataBuffer</code> that stores data internally
28 * in <code>double</code> form.
29 *
30 * @see DataBuffer
31 */
32 public class DataBufferDouble extends DataBuffer {
33
34 /** The array of data banks. */
35 protected double bankdata[][];
36
37 /** A reference to the default data bank. */
38 protected double data[];
39
40 /**
41 * Constructs a <code>double</code>-based <code>DataBuffer</code>
42 * with a specified size.
43 *
44 * @param size The number of elements in the <code>DataBuffer</code>.
45 */
46 public DataBufferDouble(int size) {
47 super(TYPE_DOUBLE, size);
48 data = new double[size];
49 bankdata = new double[1][];
50 bankdata[0] = data;
51 }
52
53 /**
54 * Constructs a <code>double</code>-based <code>DataBuffer</code>
55 * with a specified number of banks, all of which are of a
56 * specified size.
57 *
58 * @param size The number of elements in each bank of the
59 * <code>DataBuffer</code>.
60 * @param numBanks The number of banks in the <code>DataBuffer</code>.
61 */
62 public DataBufferDouble(int size, int numBanks) {
63 super(TYPE_DOUBLE, size, numBanks);
64 bankdata = new double[numBanks][];
65 for (int i= 0; i < numBanks; i++) {
66 bankdata[i] = new double[size];
67 }
68 data = bankdata[0];
69 }
70
71 /**
72 * Constructs a <code>double</code>-based <code>DataBuffer</code>
73 * with the specified data array. Only the first
74 * <code>size</code> elements are available for use by this
75 * <code>DataBuffer</code>. The array must be large enough to
76 * hold <code>size</code> elements.
77 *
78 * @param dataArray An array of <code>double</code>s to be used as the
79 * first and only bank of this <code>DataBuffer</code>.
80 * @param size The number of elements of the array to be used.
81 */
82 public DataBufferDouble(double dataArray[], int size) {
83 super(TYPE_DOUBLE, size);
84 data = dataArray;
85 bankdata = new double[1][];
86 bankdata[0] = data;
87 }
88
89 /**
90 * Constructs a <code>double</code>-based <code>DataBuffer</code>
91 * with the specified data array. Only the elements between
92 * <code>offset</code> and <code>offset + size - 1</code> are
93 * available for use by this <code>DataBuffer</code>. The array
94 * must be large enough to hold <code>offset + size</code> elements.
95 *
96 * @param dataArray An array of <code>double</code>s to be used as the
97 * first and only bank of this <code>DataBuffer</code>.
98 * @param size The number of elements of the array to be used.
99 * @param offset The offset of the first element of the array
100 * that will be used.
101 */
102 public DataBufferDouble(double dataArray[], int size, int offset) {
103 super(TYPE_DOUBLE, size, 1, offset);
104 data = dataArray;
105 bankdata = new double[1][];
106 bankdata[0] = data;
107 }
108
109 /**
110 * Constructs a <code>double</code>-based <code>DataBuffer</code>
111 * with the specified data arrays. Only the first
112 * <code>size</code> elements of each array are available for use
113 * by this <code>DataBuffer</code>. The number of banks will be
114 * equal <code>to dataArray.length</code>.
115 *
116 * @param dataArray An array of arrays of <code>double</code>s to be
117 * used as the banks of this <code>DataBuffer</code>.
118 * @param size The number of elements of each array to be used.
119 */
120 public DataBufferDouble(double dataArray[][], int size) {
121 super(TYPE_DOUBLE, size, dataArray.length);
122 bankdata = dataArray;
123 data = bankdata[0];
124 }
125
126 /**
127 * Constructs a <code>double</code>-based <code>DataBuffer</code>
128 * with the specified data arrays, size, and per-bank offsets.
129 * The number of banks is equal to dataArray.length. Each array
130 * must be at least as large as <code>size plus the corresponding
131 * offset. There must be an entry in the <code>offsets</code>
132 * array for each data array.
133 *
134 * @param dataArray An array of arrays of <code>double</code>s to be
135 * used as the banks of this <code>DataBuffer</code>.
136 * @param size The number of elements of each array to be used.
137 * @param offsets An array of integer offsets, one for each bank.
138 */
139 public DataBufferDouble(double dataArray[][], int size, int offsets[]) {
140 super(TYPE_DOUBLE, size, dataArray.length, offsets);
141 bankdata = dataArray;
142 data = bankdata[0];
143 }
144
145 /** Returns the default (first) <code>double</code> data array. */
146 public double[] getData() {
147 return data;
148 }
149
150 /** Returns the data array for the specified bank. */
151 public double[] getData(int bank) {
152 return bankdata[bank];
153 }
154
155 /** Returns the data array for all banks. */
156 public double[][] getBankData() {
157 return bankdata;
158 }
159
160 /**
161 * Returns the requested data array element from the first
162 * (default) bank as an <code>int</code>.
163 *
164 * @param i The desired data array element.
165 *
166 * @return The data entry as an <code>int</code>.
167 */
168 public int getElem(int i) {
169 return (int)(data[i+offset]);
170 }
171
172 /**
173 * Returns the requested data array element from the specified
174 * bank as an <code>int</code>.
175 *
176 * @param bank The bank number.
177 * @param i The desired data array element.
178 *
179 * @return The data entry as an <code>int</code>.
180 */
181 public int getElem(int bank, int i) {
182 return (int)(bankdata[bank][i+offsets[bank]]);
183 }
184
185 /**
186 * Sets the requested data array element in the first (default)
187 * bank to the given <code>int</code>.
188 *
189 * @param i The desired data array element.
190 * @param val The value to be set.
191 */
192 public void setElem(int i, int val) {
193 data[i+offset] = (double)val;
194 }
195
196 /**
197 * Sets the requested data array element in the specified bank
198 * to the given <code>int</code>.
199 *
200 * @param bank The bank number.
201 * @param i The desired data array element.
202 * @param val The value to be set.
203 */
204 public void setElem(int bank, int i, int val) {
205 bankdata[bank][i+offsets[bank]] = (double)val;
206 }
207
208 /**
209 * Returns the requested data array element from the first
210 * (default) bank as a <code>float</code>.
211 *
212 * @param i The desired data array element.
213 *
214 * @return The data entry as a <code>float</code>.
215 */
216 public float getElemFloat(int i) {
217 return (float)data[i+offset];
218 }
219
220 /**
221 * Returns the requested data array element from the specified
222 * bank as a <code>float</code>.
223 *
224 * @param bank The bank number.
225 * @param i The desired data array element.
226 *
227 * @return The data entry as a <code>float</code>.
228 */
229 public float getElemFloat(int bank, int i) {
230 return (float)bankdata[bank][i+offsets[bank]];
231 }
232
233 /**
234 * Sets the requested data array element in the first (default)
235 * bank to the given <code>float</code>.
236 *
237 * @param i The desired data array element.
238 * @param val The value to be set.
239 */
240 public void setElemFloat(int i, float val) {
241 data[i+offset] = (double)val;
242 }
243
244 /**
245 * Sets the requested data array element in the specified bank to
246 * the given <code>float</code>.
247 *
248 * @param bank The bank number.
249 * @param i The desired data array element.
250 * @param val The value to be set.
251 */
252 public void setElemFloat(int bank, int i, float val) {
253 bankdata[bank][i+offsets[bank]] = (double)val;
254 }
255
256 /**
257 * Returns the requested data array element from the first
258 * (default) bank as a <code>double</code>.
259 *
260 * @param i The desired data array element.
261 *
262 * @return The data entry as a <code>double</code>.
263 */
264 public double getElemDouble(int i) {
265 return data[i+offset];
266 }
267
268 /**
269 * Returns the requested data array element from the specified
270 * bank as a <code>double</code>.
271 *
272 * @param bank The bank number.
273 * @param i The desired data array element.
274 *
275 * @return The data entry as a <code>double</code>.
276 */
277 public double getElemDouble(int bank, int i) {
278 return bankdata[bank][i+offsets[bank]];
279 }
280
281 /**
282 * Sets the requested data array element in the first (default)
283 * bank to the given <code>double</code>.
284 *
285 * @param i The desired data array element.
286 * @param val The value to be set.
287 */
288 public void setElemDouble(int i, double val) {
289 data[i+offset] = val;
290 }
291
292 /**
293 * Sets the requested data array element in the specified bank to
294 * the given <code>double</code>.
295 *
296 * @param bank The bank number.
297 * @param i The desired data array element.
298 * @param val The value to be set.
299 */
300 public void setElemDouble(int bank, int i, double val) {
301 bankdata[bank][i+offsets[bank]] = val;
302 }
303 }