DataBufferFloat.java 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /* Copyright (C) 2004, 2005 Free Software Foundation
  2. This file is part of GNU Classpath.
  3. GNU Classpath is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. GNU Classpath is distributed in the hope that it will be useful, but
  8. WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with GNU Classpath; see the file COPYING. If not, write to the
  13. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  14. 02110-1301 USA.
  15. Linking this library statically or dynamically with other modules is
  16. making a combined work based on this library. Thus, the terms and
  17. conditions of the GNU General Public License cover the whole
  18. combination.
  19. As a special exception, the copyright holders of this library give you
  20. permission to link this library with independent modules to produce an
  21. executable, regardless of the license terms of these independent
  22. modules, and to copy and distribute the resulting executable under
  23. terms of your choice, provided that you also meet, for each linked
  24. independent module, the terms and conditions of the license of that
  25. module. An independent module is a module which is not derived from
  26. or based on this library. If you modify this library, you may extend
  27. this exception to your version of the library, but you are not
  28. obligated to do so. If you do not wish to do so, delete this
  29. exception statement from your version. */
  30. package java.awt.image;
  31. /* This is one of several classes that are nearly identical. Maybe we
  32. should have a central template and generate all these files. This
  33. is one of the cases where templates or macros would have been
  34. useful to have in Java.
  35. This file has been created using search-replace. My only fear is
  36. that these classes will grow out-of-sync as of a result of changes
  37. that are not propagated to the other files. As always, mirroring
  38. code is a maintenance nightmare. */
  39. /**
  40. * A {@link DataBuffer} that uses an array of <code>float</code> primitives
  41. * to represent each of its banks.
  42. *
  43. * @author Rolf W. Rasmussen (rolfwr@ii.uib.no)
  44. * @author Sascha Brawer (brawer@dandelis.ch)
  45. */
  46. public final class DataBufferFloat
  47. extends DataBuffer
  48. {
  49. private float[] data;
  50. private float[][] bankData;
  51. /**
  52. * Creates a new data buffer with a single data bank containing the
  53. * specified number of <code>float</code> elements.
  54. *
  55. * @param size the number of elements in the data bank.
  56. */
  57. public DataBufferFloat(int size)
  58. {
  59. super(TYPE_FLOAT, size, 1, 0);
  60. bankData = new float[1][];
  61. data = new float[size];
  62. bankData[0] = data;
  63. }
  64. /**
  65. * Creates a new data buffer with the specified number of data banks,
  66. * each containing the specified number of <code>float</code> elements.
  67. *
  68. * @param size the number of elements in the data bank.
  69. * @param numBanks the number of data banks.
  70. */
  71. public DataBufferFloat(int size, int numBanks)
  72. {
  73. super(TYPE_FLOAT, size, numBanks);
  74. bankData = new float[numBanks][size];
  75. data = bankData[0];
  76. }
  77. /**
  78. * Creates a new data buffer backed by the specified data bank.
  79. * <p>
  80. * Note: there is no exception when <code>dataArray</code> is
  81. * <code>null</code>, but in that case an exception will be thrown
  82. * later if you attempt to access the data buffer.
  83. *
  84. * @param dataArray the data bank.
  85. * @param size the number of elements in the data bank.
  86. */
  87. public DataBufferFloat(float[] dataArray, int size)
  88. {
  89. super(TYPE_FLOAT, size, 1, 0);
  90. bankData = new float[1][];
  91. data = dataArray;
  92. bankData[0] = data;
  93. }
  94. /**
  95. * Creates a new data buffer backed by the specified data bank, with
  96. * the specified offset to the first element.
  97. * <p>
  98. * Note: there is no exception when <code>dataArray</code> is
  99. * <code>null</code>, but in that case an exception will be thrown
  100. * later if you attempt to access the data buffer.
  101. *
  102. * @param dataArray the data bank.
  103. * @param size the number of elements in the data bank.
  104. * @param offset the offset to the first element in the array.
  105. */
  106. public DataBufferFloat(float[] dataArray, int size, int offset)
  107. {
  108. super(TYPE_FLOAT, size, 1, offset);
  109. bankData = new float[1][];
  110. data = dataArray;
  111. bankData[0] = data;
  112. }
  113. /**
  114. * Creates a new data buffer backed by the specified data banks.
  115. *
  116. * @param dataArray the data banks.
  117. * @param size the number of elements in the data bank.
  118. *
  119. * @throws NullPointerException if <code>dataArray</code> is
  120. * <code>null</code>.
  121. */
  122. public DataBufferFloat(float[][] dataArray, int size)
  123. {
  124. super(TYPE_FLOAT, size, dataArray.length);
  125. bankData = dataArray;
  126. data = bankData[0];
  127. }
  128. /**
  129. * Creates a new data buffer backed by the specified data banks, with
  130. * the specified offsets to the first element in each bank.
  131. *
  132. * @param dataArray the data banks.
  133. * @param size the number of elements in the data bank.
  134. * @param offsets the offsets to the first element in each data bank.
  135. *
  136. * @throws NullPointerException if <code>dataArray</code> is
  137. * <code>null</code>.
  138. */
  139. public DataBufferFloat(float[][] dataArray, int size, int[] offsets)
  140. {
  141. super(TYPE_FLOAT, size, dataArray.length, offsets);
  142. bankData = dataArray;
  143. data = bankData[0];
  144. }
  145. /**
  146. * Returns the first data bank.
  147. *
  148. * @return The first data bank.
  149. */
  150. public float[] getData()
  151. {
  152. return data;
  153. }
  154. /**
  155. * Returns a data bank.
  156. *
  157. * @param bank the bank index.
  158. * @return A data bank.
  159. */
  160. public float[] getData(int bank)
  161. {
  162. return bankData[bank];
  163. }
  164. /**
  165. * Returns the array underlying this <code>DataBuffer</code>.
  166. *
  167. * @return The data banks.
  168. */
  169. public float[][] getBankData()
  170. {
  171. return bankData;
  172. }
  173. /**
  174. * Returns an element from the first data bank. The offset (specified in
  175. * the constructor) is added to <code>i</code> before accessing the
  176. * underlying data array.
  177. *
  178. * @param i the element index.
  179. * @return The element.
  180. */
  181. public int getElem(int i)
  182. {
  183. return (int) data[i+offset];
  184. }
  185. /**
  186. * Returns an element from a particular data bank. The offset (specified in
  187. * the constructor) is added to <code>i</code> before accessing the
  188. * underlying data array.
  189. *
  190. * @param bank the bank index.
  191. * @param i the element index.
  192. * @return The element.
  193. */
  194. public int getElem(int bank, int i)
  195. {
  196. return (int) bankData[bank][i+offsets[bank]];
  197. }
  198. /**
  199. * Sets an element in the first data bank. The offset (specified in the
  200. * constructor) is added to <code>i</code> before updating the underlying
  201. * data array.
  202. *
  203. * @param i the element index.
  204. * @param val the new element value.
  205. */
  206. public void setElem(int i, int val)
  207. {
  208. data[i+offset] = val;
  209. }
  210. /**
  211. * Sets an element in a particular data bank. The offset (specified in the
  212. * constructor) is added to <code>i</code> before updating the underlying
  213. * data array.
  214. *
  215. * @param bank the data bank index.
  216. * @param i the element index.
  217. * @param val the new element value.
  218. */
  219. public void setElem(int bank, int i, int val)
  220. {
  221. bankData[bank][i+offsets[bank]] = val;
  222. }
  223. public float getElemFloat(int i)
  224. {
  225. return data[i+offset];
  226. }
  227. public float getElemFloat(int bank, int i)
  228. {
  229. return bankData[bank][i+offsets[bank]];
  230. }
  231. public void setElemFloat(int i, float val)
  232. {
  233. data[i+offset] = val;
  234. }
  235. public void setElemFloat(int bank, int i, float val)
  236. {
  237. bankData[bank][i+offsets[bank]] = val;
  238. }
  239. public double getElemDouble(int i)
  240. {
  241. return getElemFloat(i);
  242. }
  243. public double getElemDouble(int bank, int i)
  244. {
  245. return getElemFloat(bank, i);
  246. }
  247. public void setElemDouble(int i, double val)
  248. {
  249. setElemFloat(i, (float) val);
  250. }
  251. public void setElemDouble(int bank, int i, double val)
  252. {
  253. setElemFloat(bank, i, (float) val);
  254. }
  255. }