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