1. /*
  2. * @(#)ByteLookupTable.java 1.29 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. package java.awt.image;
  11. /**
  12. * This class defines a lookup table object. The output of a
  13. * lookup operation using an object of this class is interpreted
  14. * as an unsigned byte quantity. The lookup table contains byte
  15. * data arrays for one or more bands (or components) of an image,
  16. * and it contains an offset which will be subtracted from the
  17. * input values before indexing the arrays. This allows an array
  18. * smaller than the native data size to be provided for a
  19. * constrained input. If there is only one array in the lookup
  20. * table, it will be applied to all bands.
  21. *
  22. * @see ShortLookupTable
  23. * @see LookupOp
  24. * @version 10 Feb 1997
  25. */
  26. public class ByteLookupTable extends LookupTable {
  27. /**
  28. * Constants
  29. */
  30. byte data[][];
  31. /**
  32. * Constructs a ByteLookupTable object from an array of byte
  33. * arrays representing a lookup table for each
  34. * band. The offset will be subtracted from input
  35. * values before indexing into the arrays. The number of
  36. * bands is the length of the data argument. The
  37. * data array for each band is stored as a reference.
  38. * @param offset the value subtracted from the input values
  39. * before indexing into the arrays
  40. * @param data an array of byte arrays representing a lookup
  41. * table for each band
  42. * @throws IllegalArgumentException if <code>offset</code> is
  43. * is less than 0 or if the length of <code>data</code>
  44. * is less than 1
  45. */
  46. public ByteLookupTable(int offset, byte data[][]) {
  47. super(offset,data.length);
  48. numComponents = data.length;
  49. numEntries = data[0].length;
  50. this.data = new byte[numComponents][];
  51. // Allocate the array and copy the data reference
  52. for (int i=0; i < numComponents; i++) {
  53. this.data[i] = data[i];
  54. }
  55. }
  56. /**
  57. * Constructs a ByteLookupTable object from an array
  58. * of bytes representing a lookup table to be applied to all
  59. * bands. The offset will be subtracted from input
  60. * values before indexing into the array.
  61. * The data array is stored as a reference.
  62. * @param offset the value subtracted from the input values
  63. * before indexing into the array
  64. * @param data an array of bytes
  65. * @throws IllegalArgumentException if <code>offset</code> is
  66. * is less than 0 or if the length of <code>data</code>
  67. * is less than 1
  68. */
  69. public ByteLookupTable(int offset, byte data[]) {
  70. super(offset,data.length);
  71. numComponents = 1;
  72. numEntries = data.length;
  73. this.data = new byte[1][];
  74. this.data[0] = data;
  75. }
  76. /**
  77. * Returns the lookup table data by reference. If this ByteLookupTable
  78. * was constructed using a single byte array, the length of the returned
  79. * array is one.
  80. * @return the data array of this <code>ByteLookupTable</code>.
  81. */
  82. public final byte[][] getTable(){
  83. return data;
  84. }
  85. /**
  86. * Returns an array of samples of a pixel, translated with the lookup
  87. * table. The source and destination array can be the same array.
  88. * Array <code>dst</code> is returned.
  89. *
  90. * @param src the source array.
  91. * @param dst the destination array. This array must be at least as
  92. * long as <code>src</code>. If <code>dst</code> is
  93. * <code>null</code>, a new array will be allocated having the
  94. * same length as <code>src</code>.
  95. * @return the array <code>dst</code>, an <code>int</code> array of
  96. * samples.
  97. * @exception ArrayIndexOutOfBoundsException if <code>src</code> is
  98. * longer than <code>dst</code> or if for any element
  99. * <code>i</code> of <code>src</code>,
  100. * <code>src[i]-offset</code> is either less than zero or
  101. * greater than or equal to the length of the lookup table
  102. * for any band.
  103. */
  104. public int[] lookupPixel(int[] src, int[] dst){
  105. if (dst == null) {
  106. // Need to alloc a new destination array
  107. dst = new int[src.length];
  108. }
  109. if (numComponents == 1) {
  110. // Apply one LUT to all bands
  111. for (int i=0; i < src.length; i++) {
  112. int s = src[i] - offset;
  113. if (s < 0) {
  114. throw new ArrayIndexOutOfBoundsException("src["+i+
  115. "]-offset is "+
  116. "less than zero");
  117. }
  118. dst[i] = (int) data[0][s];
  119. }
  120. }
  121. else {
  122. for (int i=0; i < src.length; i++) {
  123. int s = src[i] - offset;
  124. if (s < 0) {
  125. throw new ArrayIndexOutOfBoundsException("src["+i+
  126. "]-offset is "+
  127. "less than zero");
  128. }
  129. dst[i] = (int) data[i][s];
  130. }
  131. }
  132. return dst;
  133. }
  134. /**
  135. * Returns an array of samples of a pixel, translated with the lookup
  136. * table. The source and destination array can be the same array.
  137. * Array <code>dst</code> is returned.
  138. *
  139. * @param src the source array.
  140. * @param dst the destination array. This array must be at least as
  141. * long as <code>src</code>. If <code>dst</code> is
  142. * <code>null</code>, a new array will be allocated having the
  143. * same length as <code>src</code>.
  144. * @return the array <code>dst</code>, an <code>int</code> array of
  145. * samples.
  146. * @exception ArrayIndexOutOfBoundsException if <code>src</code> is
  147. * longer than <code>dst</code> or if for any element
  148. * <code>i</code> of <code>src</code>,
  149. * <code>(src[i]&0xff)-offset</code> is either less than
  150. * zero or greater than or equal to the length of the
  151. * lookup table for any band.
  152. */
  153. public byte[] lookupPixel(byte[] src, byte[] dst){
  154. if (dst == null) {
  155. // Need to alloc a new destination array
  156. dst = new byte[src.length];
  157. }
  158. if (numComponents == 1) {
  159. // Apply one LUT to all bands
  160. for (int i=0; i < src.length; i++) {
  161. int s = (src[i]&0xff) - offset;
  162. if (s < 0) {
  163. throw new ArrayIndexOutOfBoundsException("src["+i+
  164. "]-offset is "+
  165. "less than zero");
  166. }
  167. dst[i] = data[0][s];
  168. }
  169. }
  170. else {
  171. for (int i=0; i < src.length; i++) {
  172. int s = (src[i]&0xff) - offset;
  173. if (s < 0) {
  174. throw new ArrayIndexOutOfBoundsException("src["+i+
  175. "]-offset is "+
  176. "less than zero");
  177. }
  178. dst[i] = data[i][s];
  179. }
  180. }
  181. return dst;
  182. }
  183. }