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