KickJava   Java API By Example, From Geeks To Geeks.

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


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
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 byte quantity. The lookup table contains byte
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 ShortLookupTable
23  * @see LookupOp
24  * @version 10 Feb 1997
25  */

26 public class ByteLookupTable extends LookupTable JavaDoc {
27
28     /**
29      * Constants
30      */

31  
32     byte data[][];
33     
34     /**
35      * Constructs a ByteLookupTable object from an array of byte
36      * arrays representing a lookup table for each
37      * band. The offset will be subtracted from 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 byte arrays representing a lookup
44      * table for each band
45      * @throws IllegalArgumentException if <code>offset</code> is
46      * is less than 0 or if the length of <code>data</code>
47      * is less than 1
48      */

49     public ByteLookupTable(int offset, byte data[][]) {
50         super(offset,data.length);
51         numComponents = data.length;
52         numEntries = data[0].length;
53         this.data = new byte[numComponents][];
54         // Allocate the array and copy the data reference
55
for (int i=0; i < numComponents; i++) {
56             this.data[i] = data[i];
57         }
58     }
59
60     /**
61      * Constructs a ByteLookupTable object from an array
62      * of bytes representing a lookup table to be applied to all
63      * bands. The offset will be subtracted from input
64      * values before indexing into the array.
65      * The data array is stored as a reference.
66      * @param offset the value subtracted from the input values
67      * before indexing into the array
68      * @param data an array of bytes
69      * @throws IllegalArgumentException if <code>offset</code> is
70      * is less than 0 or if the length of <code>data</code>
71      * is less than 1
72      */

73     public ByteLookupTable(int offset, byte data[]) {
74         super(offset,data.length);
75         numComponents = 1;
76         numEntries = data.length;
77         this.data = new byte[1][];
78         this.data[0] = data;
79     }
80
81     /**
82      * Returns the lookup table data by reference. If this ByteLookupTable
83      * was constructed using a single byte array, the length of the returned
84      * array is one.
85      * @return the data array of this <code>ByteLookupTable</code>.
86      */

87     public final byte[][] getTable(){
88         return data;
89     }
90
91     /**
92      * Returns an array of samples of a pixel, translated with the lookup
93      * table. The source and destination array can be the same array.
94      * Array <code>dst</code> is returned.
95      *
96      * @param src the source array.
97      * @param dst the destination array. This array must be at least as
98      * long as <code>src</code>. If <code>dst</code> is
99      * <code>null</code>, a new array will be allocated having the
100      * same length as <code>src</code>.
101      * @return the array <code>dst</code>, an <code>int</code> array of
102      * samples.
103      * @exception ArrayIndexOutOfBoundsException if <code>src</code> is
104      * longer than <code>dst</code> or if for any element
105      * <code>i</code> of <code>src</code>,
106      * <code>src[i]-offset</code> is either less than zero or
107      * greater than or equal to the length of the lookup table
108      * for any band.
109      */

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

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