001 /*
002 * Copyright 1997-1999 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.awt.image;
027
028 /**
029 * This class defines a lookup table object. The output of a
030 * lookup operation using an object of this class is interpreted
031 * as an unsigned byte quantity. The lookup table contains byte
032 * data arrays for one or more bands (or components) of an image,
033 * and it contains an offset which will be subtracted from the
034 * input values before indexing the arrays. This allows an array
035 * smaller than the native data size to be provided for a
036 * constrained input. If there is only one array in the lookup
037 * table, it will be applied to all bands.
038 *
039 * @see ShortLookupTable
040 * @see LookupOp
041 * @version 10 Feb 1997
042 */
043 public class ByteLookupTable extends LookupTable {
044
045 /**
046 * Constants
047 */
048
049 byte data[][];
050
051 /**
052 * Constructs a ByteLookupTable object from an array of byte
053 * arrays representing a lookup table for each
054 * band. The offset will be subtracted from input
055 * values before indexing into the arrays. The number of
056 * bands is the length of the data argument. The
057 * data array for each band is stored as a reference.
058 * @param offset the value subtracted from the input values
059 * before indexing into the arrays
060 * @param data an array of byte arrays representing a lookup
061 * table for each band
062 * @throws IllegalArgumentException if <code>offset</code> is
063 * is less than 0 or if the length of <code>data</code>
064 * is less than 1
065 */
066 public ByteLookupTable(int offset, byte data[][]) {
067 super (offset, data.length);
068 numComponents = data.length;
069 numEntries = data[0].length;
070 this .data = new byte[numComponents][];
071 // Allocate the array and copy the data reference
072 for (int i = 0; i < numComponents; i++) {
073 this .data[i] = data[i];
074 }
075 }
076
077 /**
078 * Constructs a ByteLookupTable object from an array
079 * of bytes representing a lookup table to be applied to all
080 * bands. The offset will be subtracted from input
081 * values before indexing into the array.
082 * The data array is stored as a reference.
083 * @param offset the value subtracted from the input values
084 * before indexing into the array
085 * @param data an array of bytes
086 * @throws IllegalArgumentException if <code>offset</code> is
087 * is less than 0 or if the length of <code>data</code>
088 * is less than 1
089 */
090 public ByteLookupTable(int offset, byte data[]) {
091 super (offset, data.length);
092 numComponents = 1;
093 numEntries = data.length;
094 this .data = new byte[1][];
095 this .data[0] = data;
096 }
097
098 /**
099 * Returns the lookup table data by reference. If this ByteLookupTable
100 * was constructed using a single byte array, the length of the returned
101 * array is one.
102 * @return the data array of this <code>ByteLookupTable</code>.
103 */
104 public final byte[][] getTable() {
105 return data;
106 }
107
108 /**
109 * Returns an array of samples of a pixel, translated with the lookup
110 * table. The source and destination array can be the same array.
111 * Array <code>dst</code> is returned.
112 *
113 * @param src the source array.
114 * @param dst the destination array. This array must be at least as
115 * long as <code>src</code>. If <code>dst</code> is
116 * <code>null</code>, a new array will be allocated having the
117 * same length as <code>src</code>.
118 * @return the array <code>dst</code>, an <code>int</code> array of
119 * samples.
120 * @exception ArrayIndexOutOfBoundsException if <code>src</code> is
121 * longer than <code>dst</code> or if for any element
122 * <code>i</code> of <code>src</code>,
123 * <code>src[i]-offset</code> is either less than zero or
124 * greater than or equal to the length of the lookup table
125 * for any band.
126 */
127 public int[] lookupPixel(int[] src, int[] dst) {
128 if (dst == null) {
129 // Need to alloc a new destination array
130 dst = new int[src.length];
131 }
132
133 if (numComponents == 1) {
134 // Apply one LUT to all bands
135 for (int i = 0; i < src.length; i++) {
136 int s = src[i] - offset;
137 if (s < 0) {
138 throw new ArrayIndexOutOfBoundsException("src[" + i
139 + "]-offset is " + "less than zero");
140 }
141 dst[i] = (int) data[0][s];
142 }
143 } else {
144 for (int i = 0; i < src.length; i++) {
145 int s = src[i] - offset;
146 if (s < 0) {
147 throw new ArrayIndexOutOfBoundsException("src[" + i
148 + "]-offset is " + "less than zero");
149 }
150 dst[i] = (int) data[i][s];
151 }
152 }
153 return dst;
154 }
155
156 /**
157 * Returns an array of samples of a pixel, translated with the lookup
158 * table. The source and destination array can be the same array.
159 * Array <code>dst</code> is returned.
160 *
161 * @param src the source array.
162 * @param dst the destination array. This array must be at least as
163 * long as <code>src</code>. If <code>dst</code> is
164 * <code>null</code>, a new array will be allocated having the
165 * same length as <code>src</code>.
166 * @return the array <code>dst</code>, an <code>int</code> array of
167 * samples.
168 * @exception ArrayIndexOutOfBoundsException if <code>src</code> is
169 * longer than <code>dst</code> or if for any element
170 * <code>i</code> of <code>src</code>,
171 * <code>(src[i]&0xff)-offset</code> is either less than
172 * zero or greater than or equal to the length of the
173 * lookup table for any band.
174 */
175 public byte[] lookupPixel(byte[] src, byte[] dst) {
176 if (dst == null) {
177 // Need to alloc a new destination array
178 dst = new byte[src.length];
179 }
180
181 if (numComponents == 1) {
182 // Apply one LUT to all bands
183 for (int i = 0; i < src.length; i++) {
184 int s = (src[i] & 0xff) - offset;
185 if (s < 0) {
186 throw new ArrayIndexOutOfBoundsException("src[" + i
187 + "]-offset is " + "less than zero");
188 }
189 dst[i] = data[0][s];
190 }
191 } else {
192 for (int i = 0; i < src.length; i++) {
193 int s = (src[i] & 0xff) - offset;
194 if (s < 0) {
195 throw new ArrayIndexOutOfBoundsException("src[" + i
196 + "]-offset is " + "less than zero");
197 }
198 dst[i] = data[i][s];
199 }
200 }
201 return dst;
202 }
203
204 }
|