1. /*
  2. * @(#)DataBufferUShort.java 1.13 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. /* ****************************************************************
  8. ******************************************************************
  9. ******************************************************************
  10. *** COPYRIGHT (c) Eastman Kodak Company, 1997
  11. *** As an unpublished work pursuant to Title 17 of the United
  12. *** States Code. All rights reserved.
  13. ******************************************************************
  14. ******************************************************************
  15. ******************************************************************/
  16. package java.awt.image;
  17. /**
  18. * This class extends <CODE>DataBuffer</CODE> and stores data internally as
  19. * shorts. Values stored in the short array(s) of this <CODE>DataBuffer</CODE>
  20. * are treated as unsigned values.
  21. */
  22. public final class DataBufferUShort extends DataBuffer
  23. {
  24. /** The default data bank. */
  25. short data[];
  26. /** All data banks */
  27. short bankdata[][];
  28. /**
  29. * Constructs an unsigned-short based <CODE>DataBuffer</CODE> with a single bank and the
  30. * specified size.
  31. *
  32. * @param size The size of the <CODE>DataBuffer</CODE>.
  33. */
  34. public DataBufferUShort(int size) {
  35. super(TYPE_USHORT,size);
  36. data = new short[size];
  37. bankdata = new short[1][];
  38. bankdata[0] = data;
  39. }
  40. /**
  41. * Constructs an unsigned-short based <CODE>DataBuffer</CODE> with the specified number of
  42. * banks, all of which are the specified size.
  43. *
  44. * @param size The size of the banks in the <CODE>DataBuffer</CODE>.
  45. * @param numBanks The number of banks in the a<CODE>DataBuffer</CODE>.
  46. */
  47. public DataBufferUShort(int size, int numBanks) {
  48. super(TYPE_USHORT,size,numBanks);
  49. bankdata = new short[numBanks][];
  50. for (int i= 0; i < numBanks; i++) {
  51. bankdata[i] = new short[size];
  52. }
  53. data = bankdata[0];
  54. }
  55. /**
  56. * Constructs an unsigned-short based <CODE>DataBuffer</CODE> with a single bank
  57. * using the specified array.
  58. * Only the first <CODE>size</CODE> elements should be used by accessors of
  59. * this <CODE>DataBuffer</CODE>. <CODE>dataArray</CODE> must be large enough to
  60. * hold <CODE>size</CODE> elements.
  61. *
  62. * @param dataArray The unsigned-short array for the <CODE>DataBuffer</CODE>.
  63. * @param size The size of the <CODE>DataBuffer</CODE> bank.
  64. */
  65. public DataBufferUShort(short dataArray[], int size) {
  66. super(TYPE_USHORT,size);
  67. if (dataArray == null) {
  68. throw new NullPointerException("dataArray is null");
  69. }
  70. data = dataArray;
  71. bankdata = new short[1][];
  72. bankdata[0] = data;
  73. }
  74. /**
  75. * Constructs an unsigned-short based <CODE>DataBuffer</CODE> with a single bank
  76. * using the specified array, size, and offset. <CODE>dataArray</CODE> must have at
  77. * least <CODE>offset</CODE> + <CODE>size</CODE> elements. Only elements
  78. * <CODE>offset</CODE> through <CODE>offset</CODE> + <CODE>size</CODE> - 1 should
  79. * be used by accessors of this <CODE>DataBuffer</CODE>.
  80. *
  81. * @param dataArray The unsigned-short array for the <CODE>DataBuffer</CODE>.
  82. * @param size The size of the <CODE>DataBuffer</CODE> bank.
  83. * @param offset The offset into the <CODE>dataArray</CODE>.
  84. */
  85. public DataBufferUShort(short dataArray[], int size, int offset) {
  86. super(TYPE_USHORT,size,1,offset);
  87. if (dataArray == null) {
  88. throw new NullPointerException("dataArray is null");
  89. }
  90. if ((size+offset) > dataArray.length) {
  91. throw new IllegalArgumentException("Length of dataArray is less "+
  92. " than size+offset.");
  93. }
  94. data = dataArray;
  95. bankdata = new short[1][];
  96. bankdata[0] = data;
  97. }
  98. /**
  99. * Constructs an unsigned-short based <CODE>DataBuffer</CODE> with the specified arrays.
  100. * The number of banks will be equal to <CODE>dataArray.length</CODE>.
  101. * Only the first <CODE>size</CODE> elements of each array should be used by
  102. * accessors of this <CODE>DataBuffer</CODE>.
  103. *
  104. * @param dataArray The unsigned-short arrays for the <CODE>DataBuffer</CODE>.
  105. * @param size The size of the banks in the <CODE>DataBuffer</CODE>.
  106. */
  107. public DataBufferUShort(short dataArray[][], int size) {
  108. super(TYPE_USHORT,size,dataArray.length);
  109. if (dataArray == null) {
  110. throw new NullPointerException("dataArray is null");
  111. }
  112. for (int i=0; i < dataArray.length; i++) {
  113. if (dataArray[i] == null) {
  114. throw new NullPointerException("dataArray["+i+"] is null");
  115. }
  116. }
  117. bankdata = (short[][]) dataArray.clone();
  118. data = bankdata[0];
  119. }
  120. /**
  121. * Constructs an unsigned-short based <CODE>DataBuffer</CODE> with specified arrays,
  122. * size, and offsets.
  123. * The number of banks is equal to <CODE>dataArray.length</CODE>. Each array must
  124. * be at least as large as <CODE>size</CODE> + the corresponding offset. There must
  125. * be an entry in the offset array for each <CODE>dataArray</CODE> entry. For each
  126. * bank, only elements <CODE>offset</CODE> through
  127. * <CODE>offset</CODE> + <CODE>size</CODE> - 1 should be
  128. * used by accessors of this <CODE>DataBuffer</CODE>.
  129. *
  130. * @param dataArray The unsigned-short arrays for the <CODE>DataBuffer</CODE>.
  131. * @param size The size of the banks in the <CODE>DataBuffer</CODE>.
  132. * @param offsets The offsets into each array.
  133. */
  134. public DataBufferUShort(short dataArray[][], int size, int offsets[]) {
  135. super(TYPE_USHORT,size,dataArray.length,offsets);
  136. if (dataArray == null) {
  137. throw new NullPointerException("dataArray is null");
  138. }
  139. for (int i=0; i < dataArray.length; i++) {
  140. if (dataArray[i] == null) {
  141. throw new NullPointerException("dataArray["+i+"] is null");
  142. }
  143. if ((size+offsets[i]) > dataArray[i].length) {
  144. throw new IllegalArgumentException("Length of dataArray["+i+
  145. "] is less than size+"+
  146. "offsets["+i+"].");
  147. }
  148. }
  149. bankdata = (short[][]) dataArray.clone();
  150. data = bankdata[0];
  151. }
  152. /**
  153. * Returns the default (first) unsigned-short data array.
  154. *
  155. * @return The first unsigned-short data array.
  156. */
  157. public short[] getData() {
  158. return data;
  159. }
  160. /**
  161. * Returns the data array for the specified bank.
  162. *
  163. * @param bank The bank whose data array you want to get.
  164. * @return The data array for the specified bank.
  165. */
  166. public short[] getData(int bank) {
  167. return bankdata[bank];
  168. }
  169. /**
  170. * Returns the data arrays for all banks.
  171. * @return All of the data arrays.
  172. */
  173. public short[][] getBankData() {
  174. return (short[][]) bankdata.clone();
  175. }
  176. /**
  177. * Returns the requested data array element from the first (default) bank.
  178. *
  179. * @param i The data array element you want to get.
  180. * @return The requested data array element as an integer.
  181. * @see #setElem(int, int)
  182. * @see #setElem(int, int, int)
  183. */
  184. public int getElem(int i) {
  185. return (int)(data[i+offset]&0xffff);
  186. }
  187. /**
  188. * Returns the requested data array element from the specified bank.
  189. *
  190. * @param bank The bank from which you want to get a data array element.
  191. * @param i The data array element you want to get.
  192. * @return The requested data array element as an integer.
  193. * @see #setElem(int, int)
  194. * @see #setElem(int, int, int)
  195. */
  196. public int getElem(int bank, int i) {
  197. return (int)(bankdata[bank][i+offsets[bank]]&0xffff);
  198. }
  199. /**
  200. * Sets the requested data array element in the first (default) bank
  201. * to the specified value.
  202. *
  203. * @param i The data array element you want to set.
  204. * @param val The integer value to which you want to set the data array element.
  205. * @see #getElem(int)
  206. * @see #getElem(int, int)
  207. */
  208. public void setElem(int i, int val) {
  209. data[i+offset] = (short)(val&0xffff);
  210. }
  211. /**
  212. * Sets the requested data array element in the specified bank
  213. * from the given integer.
  214. * @param bank The bank in which you want to set the data array element.
  215. * @param i The data array element you want to set.
  216. * @param val The integer value to which you want to set the specified data array element.
  217. * @see #getElem(int)
  218. * @see #getElem(int, int)
  219. */
  220. public void setElem(int bank, int i, int val) {
  221. bankdata[bank][i+offsets[bank]] = (short)(val&0xffff);
  222. }
  223. }