1. /*
  2. * @(#)DataBufferUShort.java 1.6 01/11/29
  3. *
  4. * Copyright 2002 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. data = dataArray;
  68. bankdata = new short[1][];
  69. bankdata[0] = data;
  70. }
  71. /**
  72. * Constructs an unsigned-short based <CODE>DataBuffer</CODE> with a single bank
  73. * using the specified array, size, and offset. <CODE>dataArray</CODE> must have at
  74. * least <CODE>offset</CODE> + <CODE>size</CODE> elements. Only elements
  75. * <CODE>offset</CODE> through <CODE>offset</CODE> + <CODE>size</CODE> - 1 should
  76. * be used by accessors of this <CODE>DataBuffer</CODE>.
  77. *
  78. * @param dataArray The unsigned-short array for the <CODE>DataBuffer</CODE>.
  79. * @param size The size of the <CODE>DataBuffer</CODE> bank.
  80. * @param offset The offset into the <CODE>dataArray</CODE>.
  81. */
  82. public DataBufferUShort(short dataArray[], int size, int offset) {
  83. super(TYPE_USHORT,size,1,offset);
  84. data = dataArray;
  85. bankdata = new short[1][];
  86. bankdata[0] = data;
  87. }
  88. /**
  89. * Constructs an unsigned-short based <CODE>DataBuffer</CODE> with the specified arrays.
  90. * The number of banks will be equal to <CODE>dataArray.length</CODE>.
  91. * Only the first <CODE>size</CODE> elements of each array should be used by
  92. * accessors of this <CODE>DataBuffer</CODE>.
  93. *
  94. * @param dataArray The unsigned-short arrays for the <CODE>DataBuffer</CODE>.
  95. * @param size The size of the banks in the <CODE>DataBuffer</CODE>.
  96. */
  97. public DataBufferUShort(short dataArray[][], int size) {
  98. super(TYPE_USHORT,size,dataArray.length);
  99. bankdata = dataArray;
  100. data = bankdata[0];
  101. }
  102. /**
  103. * Constructs an unsigned-short based <CODE>DataBuffer</CODE> with specified arrays,
  104. * size, and offsets.
  105. * The number of banks is equal to <CODE>dataArray.length</CODE>. Each array must
  106. * be at least as large as <CODE>size</CODE> + the corresponding offset. There must
  107. * be an entry in the offset array for each <CODE>dataArray</CODE> entry. For each
  108. * bank, only elements <CODE>offset</CODE> through
  109. * <CODE>offset</CODE> + <CODE>size</CODE> - 1 should be
  110. * used by accessors of this <CODE>DataBuffer</CODE>.
  111. *
  112. * @param dataArray The unsigned-short arrays for the <CODE>DataBuffer</CODE>.
  113. * @param size The size of the banks in the <CODE>DataBuffer</CODE>.
  114. * @param offsets The offsets into each array.
  115. */
  116. public DataBufferUShort(short dataArray[][], int size, int offsets[]) {
  117. super(TYPE_USHORT,size,dataArray.length,offsets);
  118. bankdata = dataArray;
  119. data = bankdata[0];
  120. }
  121. /**
  122. * Returns the default (first) unsigned-short data array.
  123. *
  124. * @return The first unsigned-short data array.
  125. */
  126. public short[] getData() {
  127. return data;
  128. }
  129. /**
  130. * Returns the data array for the specified bank.
  131. *
  132. * @param bank The bank whose data array you want to get.
  133. * @return The data array for the specified bank.
  134. */
  135. public short[] getData(int bank) {
  136. return bankdata[bank];
  137. }
  138. /**
  139. * Returns the data arrays for all banks.
  140. * @return All of the data arrays.
  141. */
  142. public short[][] getBankData() {
  143. return bankdata;
  144. }
  145. /**
  146. * Returns the requested data array element from the first (default) bank.
  147. *
  148. * @param i The data array element you want to get.
  149. * @return The requested data array element as an integer.
  150. */
  151. public int getElem(int i) {
  152. return (int)(data[i+offset]&0xffff);
  153. }
  154. /**
  155. * Returns the requested data array element from the specified bank
  156. *
  157. * @param bank The bank from which you want to get a data array element.
  158. * @param i The data array element you want to get.
  159. * @return The requested data array element as an integer.
  160. */
  161. public int getElem(int bank, int i) {
  162. return (int)(bankdata[bank][i+offsets[bank]]&0xffff);
  163. }
  164. /**
  165. * Sets the requested data array element in the first (default) bank
  166. * to the specified value.
  167. *
  168. * @param i The data array element you want to set.
  169. * @param val The integer value to which you want to set the data array element.
  170. */
  171. public void setElem(int i, int val) {
  172. data[i+offset] = (short)(val&0xffff);
  173. }
  174. /**
  175. * Sets the requested data array element in the specified bank
  176. * from the given integer.
  177. * @param bank The bank in which you want to set the data array element.
  178. * @param i The data array element you want to set.
  179. * @param val The integer value to which you want to set the specified data array element.
  180. */
  181. public void setElem(int bank, int i, int val) {
  182. bankdata[bank][i+offsets[bank]] = (short)(val&0xffff);
  183. }
  184. }