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