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