1. /*
  2. * @(#)DataBufferShort.java 1.18 03/01/23
  3. *
  4. * Copyright 2003 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 shorts.
  19. */
  20. public final class DataBufferShort extends DataBuffer
  21. {
  22. /** The default data bank. */
  23. short data[];
  24. /** All data banks */
  25. short bankdata[][];
  26. /**
  27. * Constructs a short-based <CODE>DataBuffer</CODE> with a single bank and the
  28. * specified size.
  29. *
  30. * @param size The size of the <CODE>DataBuffer</CODE>.
  31. */
  32. public DataBufferShort(int size) {
  33. super(TYPE_SHORT,size);
  34. data = new short[size];
  35. bankdata = new short[1][];
  36. bankdata[0] = data;
  37. }
  38. /**
  39. * Constructs a short-based <CODE>DataBuffer</CODE> with the specified number of
  40. * banks all of which are the specified size.
  41. *
  42. * @param size The size of the banks in the <CODE>DataBuffer</CODE>.
  43. * @param numBanks The number of banks in the a<CODE>DataBuffer</CODE>.
  44. */
  45. public DataBufferShort(int size, int numBanks) {
  46. super(TYPE_SHORT,size,numBanks);
  47. bankdata = new short[numBanks][];
  48. for (int i= 0; i < numBanks; i++) {
  49. bankdata[i] = new short[size];
  50. }
  51. data = bankdata[0];
  52. }
  53. /**
  54. * Constructs a short-based <CODE>DataBuffer</CODE> with a single bank using the
  55. * specified array.
  56. * Only the first <CODE>size</CODE> elements should be used by accessors of
  57. * this <CODE>DataBuffer</CODE>. <CODE>dataArray</CODE> must be large enough to
  58. * hold <CODE>size</CODE> elements.
  59. *
  60. * @param dataArray The short array for the <CODE>DataBuffer</CODE>.
  61. * @param size The size of the <CODE>DataBuffer</CODE> bank.
  62. */
  63. public DataBufferShort(short dataArray[], int size) {
  64. super(TYPE_SHORT,size);
  65. data = dataArray;
  66. bankdata = new short[1][];
  67. bankdata[0] = data;
  68. }
  69. /**
  70. * Constructs a short-based <CODE>DataBuffer</CODE> with a single bank using the
  71. * specified array, size, and offset. <CODE>dataArray</CODE> must have at least
  72. * <CODE>offset</CODE> + <CODE>size</CODE> elements. Only elements <CODE>offset</CODE>
  73. * through <CODE>offset</CODE> + <CODE>size</CODE> - 1
  74. * should be used by accessors of this <CODE>DataBuffer</CODE>.
  75. *
  76. * @param dataArray The short array for the <CODE>DataBuffer</CODE>.
  77. * @param size The size of the <CODE>DataBuffer</CODE> bank.
  78. * @param offset The offset into the <CODE>dataArray</CODE>.
  79. */
  80. public DataBufferShort(short dataArray[], int size, int offset) {
  81. super(TYPE_SHORT,size,1,offset);
  82. data = dataArray;
  83. bankdata = new short[1][];
  84. bankdata[0] = data;
  85. }
  86. /**
  87. * Constructs a short-based <CODE>DataBuffer</CODE> with the specified arrays.
  88. * The number of banks will be equal to <CODE>dataArray.length</CODE>.
  89. * Only the first <CODE>size</CODE> elements of each array should be used by
  90. * accessors of this <CODE>DataBuffer</CODE>.
  91. *
  92. * @param dataArray The short arrays for the <CODE>DataBuffer</CODE>.
  93. * @param size The size of the banks in the <CODE>DataBuffer</CODE>.
  94. */
  95. public DataBufferShort(short dataArray[][], int size) {
  96. super(TYPE_SHORT,size,dataArray.length);
  97. bankdata = (short[][]) dataArray.clone();
  98. data = bankdata[0];
  99. }
  100. /**
  101. * Constructs a short-based <CODE>DataBuffer</CODE> with the specified arrays, size,
  102. * and offsets.
  103. * The number of banks is equal to <CODE>dataArray.length</CODE>. Each array must
  104. * be at least as large as <CODE>size</CODE> + the corresponding offset. There must
  105. * be an entry in the offset array for each <CODE>dataArray</CODE> entry. For each
  106. * bank, only elements <CODE>offset</CODE> through
  107. * <CODE>offset</CODE> + <CODE>size</CODE> - 1 should be
  108. * used by accessors of this <CODE>DataBuffer</CODE>.
  109. *
  110. * @param dataArray The short arrays for the <CODE>DataBuffer</CODE>.
  111. * @param size The size of the banks in the <CODE>DataBuffer</CODE>.
  112. * @param offsets The offsets into each array.
  113. */
  114. public DataBufferShort(short dataArray[][], int size, int offsets[]) {
  115. super(TYPE_SHORT,size,dataArray.length,offsets);
  116. bankdata = (short[][]) dataArray.clone();
  117. data = bankdata[0];
  118. }
  119. /**
  120. * Returns the default (first) byte data array.
  121. *
  122. * @return The first short data array.
  123. */
  124. public short[] getData() {
  125. return data;
  126. }
  127. /**
  128. * Returns the data array for the specified bank.
  129. *
  130. * @param bank The bank whose data array you want to get.
  131. * @return The data array for the specified bank.
  132. */
  133. public short[] getData(int bank) {
  134. return bankdata[bank];
  135. }
  136. /**
  137. * Returns the data arrays for all banks.
  138. * @return All of the data arrays.
  139. */
  140. public short[][] getBankData() {
  141. return (short[][]) bankdata.clone();
  142. }
  143. /**
  144. * Returns the requested data array element from the first (default) bank.
  145. *
  146. * @param i The data array element you want to get.
  147. * @return The requested data array element as an integer.
  148. * @see #setElem(int, int)
  149. * @see #setElem(int, int, int)
  150. */
  151. public int getElem(int i) {
  152. return (int)(data[i+offset]);
  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. * @see #setElem(int, int)
  161. * @see #setElem(int, int, int)
  162. */
  163. public int getElem(int bank, int i) {
  164. return (int)(bankdata[bank][i+offsets[bank]]);
  165. }
  166. /**
  167. * Sets the requested data array element in the first (default) bank
  168. * to the specified value.
  169. *
  170. * @param i The data array element you want to set.
  171. * @param val The integer value to which you want to set the data array element.
  172. * @see #getElem(int)
  173. * @see #getElem(int, int)
  174. */
  175. public void setElem(int i, int val) {
  176. data[i+offset] = (short)val;
  177. }
  178. /**
  179. * Sets the requested data array element in the specified bank
  180. * from the given integer.
  181. * @param bank The bank in which you want to set the data array element.
  182. * @param i The data array element you want to set.
  183. * @param val The integer value to which you want to set the specified data array element.
  184. * @see #getElem(int)
  185. * @see #getElem(int, int)
  186. */
  187. public void setElem(int bank, int i, int val) {
  188. bankdata[bank][i+offsets[bank]] = (short)val;
  189. }
  190. }