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