KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > image > ShortLookupTable


1 /*
2  * @(#)ShortLookupTable.java 1.28 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
8 package java.awt.image;
9
10
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 JavaDoc {
27
28     /**
29      * Constants
30      */

31  
32     short data[][];
33     
34     /**
35      * Constructs a ShortLookupTable object from an array of short
36      * arrays representing a lookup table for each
37      * band. The offset will be subtracted from the input
38      * values before indexing into the arrays. The number of
39      * bands is the length of the data argument. The
40      * data array for each band is stored as a reference.
41      * @param offset the value subtracted from the input values
42      * before indexing into the arrays
43      * @param data an array of short arrays representing a lookup
44      * table for each band
45      */

46     public ShortLookupTable(int offset, short data[][]) {
47         super(offset,data.length);
48         numComponents = data.length;
49         numEntries = data[0].length;
50         this.data = new short[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     /**
58      * Constructs a ShortLookupTable object from an array
59      * of shorts representing a lookup table for each
60      * band. The offset will be subtracted from the input
61      * values before indexing into the array. The
62      * data array is stored as a reference.
63      * @param offset the value subtracted from the input values
64      * before indexing into the arrays
65      * @param data an array of shorts
66      */

67     public ShortLookupTable(int offset, short data[]) {
68         super(offset,data.length);
69         numComponents = 1;
70         numEntries = data.length;
71         this.data = new short[1][];
72         this.data[0] = data;
73     }
74
75     /**
76      * Returns the lookup table data by reference. If this ShortLookupTable
77      * was constructed using a single short array, the length of the returned
78      * array is one.
79      * @return ShortLookupTable data array.
80      */

81     public final short[][] getTable(){
82         return data;
83     }
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]&0xffff)-offset</code> is either less than
101      * zero or greater than or equal to the length of the
102      * lookup table 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         
110         if (numComponents == 1) {
111             // Apply one LUT to all channels
112
for (int i=0; i < src.length; i++) {
113                 int s = (src[i]&0xffff) - offset;
114                 if (s < 0) {
115                     throw new ArrayIndexOutOfBoundsException JavaDoc("src["+i+
116                                                              "]-offset is "+
117                                                              "less than zero");
118                 }
119                 dst[i] = (int) data[0][s];
120             }
121         }
122         else {
123             for (int i=0; i < src.length; i++) {
124                 int s = (src[i]&0xffff) - offset;
125                 if (s < 0) {
126                     throw new ArrayIndexOutOfBoundsException JavaDoc("src["+i+
127                                                              "]-offset is "+
128                                                              "less than zero");
129                 }
130                 dst[i] = (int) data[i][s];
131             }
132         }
133         return dst;
134     }
135
136     /**
137      * Returns an array of samples of a pixel, translated with the lookup
138      * table. The source and destination array can be the same array.
139      * Array <code>dst</code> is returned.
140      *
141      * @param src the source array.
142      * @param dst the destination array. This array must be at least as
143      * long as <code>src</code>. If <code>dst</code> is
144      * <code>null</code>, a new array will be allocated having the
145      * same length as <code>src</code>.
146      * @return the array <code>dst</code>, an <code>int</code> array of
147      * samples.
148      * @exception ArrayIndexOutOfBoundsException if <code>src</code> is
149      * longer than <code>dst</code> or if for any element
150      * <code>i</code> of <code>src</code>,
151      * <code>(src[i]&0xffff)-offset</code> is either less than
152      * zero or greater than or equal to the length of the
153      * lookup table for any band.
154      */

155     public short[] lookupPixel(short[] src, short[] dst){
156         if (dst == null) {
157             // Need to alloc a new destination array
158
dst = new short[src.length];
159         }
160         
161         if (numComponents == 1) {
162             // Apply one LUT to all channels
163
for (int i=0; i < src.length; i++) {
164                 int s = (src[i]&0xffff) - offset;
165                 if (s < 0) {
166                     throw new ArrayIndexOutOfBoundsException JavaDoc("src["+i+
167                                                              "]-offset is "+
168                                                              "less than zero");
169                 }
170                 dst[i] = data[0][s];
171             }
172         }
173         else {
174             for (int i=0; i < src.length; i++) {
175                 int s = (src[i]&0xffff) - offset;
176                 if (s < 0) {
177                     throw new ArrayIndexOutOfBoundsException JavaDoc("src["+i+
178                                                              "]-offset is "+
179                                                              "less than zero");
180                 }
181                 dst[i] = data[i][s];
182             }
183         }
184         return dst;
185     }
186
187 }
188
Popular Tags