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