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