DataBufferInt.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* Copyright (C) 2000, 2002, 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>int</code> primitives
  41. * to represent each of its banks.
  42. *
  43. * @author Rolf W. Rasmussen (rolfwr@ii.uib.no)
  44. */
  45. public final class DataBufferInt extends DataBuffer
  46. {
  47. private int[] data;
  48. private int[][] bankData;
  49. /**
  50. * Creates a new data buffer with a single data bank containing the
  51. * specified number of <code>int</code> elements.
  52. *
  53. * @param size the number of elements in the data bank.
  54. */
  55. public DataBufferInt(int size)
  56. {
  57. super(TYPE_INT, size, 1, 0);
  58. bankData = new int[1][];
  59. data = new int[size];
  60. bankData[0] = data;
  61. }
  62. /**
  63. * Creates a new data buffer with the specified number of data banks,
  64. * each containing the specified number of <code>int</code> elements.
  65. *
  66. * @param size the number of elements in the data bank.
  67. * @param numBanks the number of data banks.
  68. */
  69. public DataBufferInt(int size, int numBanks)
  70. {
  71. super(TYPE_INT, size, numBanks);
  72. bankData = new int[numBanks][size];
  73. data = bankData[0];
  74. }
  75. /**
  76. * Creates a new data buffer backed by the specified data bank.
  77. * <p>
  78. * Note: there is no exception when <code>dataArray</code> is
  79. * <code>null</code>, but in that case an exception will be thrown
  80. * later if you attempt to access the data buffer.
  81. *
  82. * @param dataArray the data bank.
  83. * @param size the number of elements in the data bank.
  84. */
  85. public DataBufferInt(int[] dataArray, int size)
  86. {
  87. super(TYPE_INT, size, 1, 0);
  88. bankData = new int[1][];
  89. data = dataArray;
  90. bankData[0] = data;
  91. }
  92. /**
  93. * Creates a new data buffer backed by the specified data bank, with
  94. * the specified offset to the first element.
  95. * <p>
  96. * Note: there is no exception when <code>dataArray</code> is
  97. * <code>null</code>, but in that case an exception will be thrown
  98. * later if you attempt to access the data buffer.
  99. *
  100. * @param dataArray the data bank.
  101. * @param size the number of elements in the data bank.
  102. * @param offset the offset to the first element in the array.
  103. */
  104. public DataBufferInt(int[] dataArray, int size, int offset)
  105. {
  106. super(TYPE_INT, size, 1, offset);
  107. bankData = new int[1][];
  108. data = dataArray;
  109. bankData[0] = data;
  110. }
  111. /**
  112. * Creates a new data buffer backed by the specified data banks.
  113. *
  114. * @param dataArray the data banks.
  115. * @param size the number of elements in the data bank.
  116. *
  117. * @throws NullPointerException if <code>dataArray</code> is
  118. * <code>null</code>.
  119. */
  120. public DataBufferInt(int[][] dataArray, int size)
  121. {
  122. super(TYPE_INT, size, dataArray.length);
  123. bankData = dataArray;
  124. data = bankData[0];
  125. }
  126. /**
  127. * Creates a new data buffer backed by the specified data banks, with
  128. * the specified offsets to the first element in each bank.
  129. *
  130. * @param dataArray the data banks.
  131. * @param size the number of elements in the data bank.
  132. * @param offsets the offsets to the first element in each data bank.
  133. *
  134. * @throws NullPointerException if <code>dataArray</code> is
  135. * <code>null</code>.
  136. */
  137. public DataBufferInt(int[][] dataArray, int size, int[] offsets)
  138. {
  139. super(TYPE_INT, size, dataArray.length, offsets);
  140. bankData = dataArray;
  141. data = bankData[0];
  142. }
  143. /**
  144. * Returns the first data bank.
  145. *
  146. * @return The first data bank.
  147. */
  148. public int[] getData()
  149. {
  150. return data;
  151. }
  152. /**
  153. * Returns a data bank.
  154. *
  155. * @param bank the bank index.
  156. * @return A data bank.
  157. */
  158. public int[] getData(int bank)
  159. {
  160. return bankData[bank];
  161. }
  162. /**
  163. * Returns the array underlying this <code>DataBuffer</code>.
  164. *
  165. * @return The data banks.
  166. */
  167. public int[][] getBankData()
  168. {
  169. return bankData;
  170. }
  171. /**
  172. * Returns an element from the first data bank. The <code>offset</code> is
  173. * added to the specified index before accessing the underlying data array.
  174. *
  175. * @param i the element index.
  176. * @return The element.
  177. */
  178. public int getElem(int i)
  179. {
  180. return data[i+offset];
  181. }
  182. /**
  183. * Returns an element from a particular data bank. The <code>offset</code>
  184. * is added to the specified index before accessing the underlying data
  185. * array.
  186. *
  187. * @param bank the bank index.
  188. * @param i the element index.
  189. * @return The element.
  190. */
  191. public int getElem(int bank, int i)
  192. {
  193. // get unsigned int as int
  194. return bankData[bank][i+offsets[bank]];
  195. }
  196. /**
  197. * Sets an element in the first data bank. The offset (specified in the
  198. * constructor) is added to <code>i</code> before updating the underlying
  199. * data array.
  200. *
  201. * @param i the element index.
  202. * @param val the new element value.
  203. */
  204. public void setElem(int i, int val)
  205. {
  206. data[i+offset] = val;
  207. }
  208. /**
  209. * Sets an element in a particular data bank. The offset (specified in the
  210. * constructor) is added to <code>i</code> before updating the underlying
  211. * data array.
  212. *
  213. * @param bank the data bank index.
  214. * @param i the element index.
  215. * @param val the new element value.
  216. */
  217. public void setElem(int bank, int i, int val)
  218. {
  219. bankData[bank][i+offsets[bank]] = val;
  220. }
  221. }