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