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