001 /*
002 * Copyright 1997-2000 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 short quantity. The lookup table contains short
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 ByteLookupTable
040 * @see LookupOp
041 * @version 10 Feb 1997
042 */
043 public class ShortLookupTable extends LookupTable {
044
045 /**
046 * Constants
047 */
048
049 short data[][];
050
051 /**
052 * Constructs a ShortLookupTable object from an array of short
053 * arrays representing a lookup table for each
054 * band. The offset will be subtracted from the 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 short arrays representing a lookup
061 * table for each band
062 */
063 public ShortLookupTable(int offset, short data[][]) {
064 super (offset, data.length);
065 numComponents = data.length;
066 numEntries = data[0].length;
067 this .data = new short[numComponents][];
068 // Allocate the array and copy the data reference
069 for (int i = 0; i < numComponents; i++) {
070 this .data[i] = data[i];
071 }
072 }
073
074 /**
075 * Constructs a ShortLookupTable object from an array
076 * of shorts representing a lookup table for each
077 * band. The offset will be subtracted from the input
078 * values before indexing into the array. The
079 * data array is stored as a reference.
080 * @param offset the value subtracted from the input values
081 * before indexing into the arrays
082 * @param data an array of shorts
083 */
084 public ShortLookupTable(int offset, short data[]) {
085 super (offset, data.length);
086 numComponents = 1;
087 numEntries = data.length;
088 this .data = new short[1][];
089 this .data[0] = data;
090 }
091
092 /**
093 * Returns the lookup table data by reference. If this ShortLookupTable
094 * was constructed using a single short array, the length of the returned
095 * array is one.
096 * @return ShortLookupTable data array.
097 */
098 public final short[][] getTable() {
099 return data;
100 }
101
102 /**
103 * Returns an array of samples of a pixel, translated with the lookup
104 * table. The source and destination array can be the same array.
105 * Array <code>dst</code> is returned.
106 *
107 * @param src the source array.
108 * @param dst the destination array. This array must be at least as
109 * long as <code>src</code>. If <code>dst</code> is
110 * <code>null</code>, a new array will be allocated having the
111 * same length as <code>src</code>.
112 * @return the array <code>dst</code>, an <code>int</code> array of
113 * samples.
114 * @exception ArrayIndexOutOfBoundsException if <code>src</code> is
115 * longer than <code>dst</code> or if for any element
116 * <code>i</code> of <code>src</code>,
117 * <code>(src[i]&0xffff)-offset</code> is either less than
118 * zero or greater than or equal to the length of the
119 * lookup table for any band.
120 */
121 public int[] lookupPixel(int[] src, int[] dst) {
122 if (dst == null) {
123 // Need to alloc a new destination array
124 dst = new int[src.length];
125 }
126
127 if (numComponents == 1) {
128 // Apply one LUT to all channels
129 for (int i = 0; i < src.length; i++) {
130 int s = (src[i] & 0xffff) - offset;
131 if (s < 0) {
132 throw new ArrayIndexOutOfBoundsException("src[" + i
133 + "]-offset is " + "less than zero");
134 }
135 dst[i] = (int) data[0][s];
136 }
137 } else {
138 for (int i = 0; i < src.length; i++) {
139 int s = (src[i] & 0xffff) - offset;
140 if (s < 0) {
141 throw new ArrayIndexOutOfBoundsException("src[" + i
142 + "]-offset is " + "less than zero");
143 }
144 dst[i] = (int) data[i][s];
145 }
146 }
147 return dst;
148 }
149
150 /**
151 * Returns an array of samples of a pixel, translated with the lookup
152 * table. The source and destination array can be the same array.
153 * Array <code>dst</code> is returned.
154 *
155 * @param src the source array.
156 * @param dst the destination array. This array must be at least as
157 * long as <code>src</code>. If <code>dst</code> is
158 * <code>null</code>, a new array will be allocated having the
159 * same length as <code>src</code>.
160 * @return the array <code>dst</code>, an <code>int</code> array of
161 * samples.
162 * @exception ArrayIndexOutOfBoundsException if <code>src</code> is
163 * longer than <code>dst</code> or if for any element
164 * <code>i</code> of <code>src</code>,
165 * <code>(src[i]&0xffff)-offset</code> is either less than
166 * zero or greater than or equal to the length of the
167 * lookup table for any band.
168 */
169 public short[] lookupPixel(short[] src, short[] dst) {
170 if (dst == null) {
171 // Need to alloc a new destination array
172 dst = new short[src.length];
173 }
174
175 if (numComponents == 1) {
176 // Apply one LUT to all channels
177 for (int i = 0; i < src.length; i++) {
178 int s = (src[i] & 0xffff) - offset;
179 if (s < 0) {
180 throw new ArrayIndexOutOfBoundsException("src[" + i
181 + "]-offset is " + "less than zero");
182 }
183 dst[i] = data[0][s];
184 }
185 } else {
186 for (int i = 0; i < src.length; i++) {
187 int s = (src[i] & 0xffff) - offset;
188 if (s < 0) {
189 throw new ArrayIndexOutOfBoundsException("src[" + i
190 + "]-offset is " + "less than zero");
191 }
192 dst[i] = data[i][s];
193 }
194 }
195 return dst;
196 }
197
198 }
|