1 /* ByteLookupTable.java -- Java class for a pixel translation table.
2 Copyright (C) 2004, 2005, 2006, Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.awt.image;
42 * ByteLookupTable represents translation arrays for pixel values. It wraps
43 * one or more data arrays for each layer (or component) in an image, such as
44 * Alpha, R, G, and B. When doing translation, the offset is subtracted from
45 * the pixel values to allow a subset of an array to be used.
47 * @author Jerry Quinn (jlquinn@optonline.net)
50 public class ByteLookupTable extends LookupTable
52 // Array of translation tables.
53 private byte data[][];
56 * Creates a new <code>ByteLookupTable</code> instance.
58 * Offset is subtracted from pixel values when looking up in the translation
59 * tables. If data.length is one, the same table is applied to all pixel
62 * @param offset Offset to be subtracted.
63 * @param data Array of lookup tables (<code>null</code> not permitted).
64 * @exception IllegalArgumentException if offset < 0 or data.length < 1.
66 public ByteLookupTable(int offset, byte[][] data)
67 throws IllegalArgumentException
69 super(offset, data.length);
71 // tests show that Sun's implementation creates a new array to store the
72 // references from the incoming 'data' array - not sure why, but we'll
73 // match that behaviour just in case it matters...
74 this.data = new byte[data.length][];
75 for (int i = 0; i < data.length; i++)
76 this.data[i] = data[i];
80 * Creates a new <code>ByteLookupTable</code> instance.
82 * Offset is subtracted from pixel values when looking up in the translation
83 * table. The same table is applied to all pixel components.
85 * @param offset Offset to be subtracted.
86 * @param data Lookup table for all components (<code>null</code> not
88 * @exception IllegalArgumentException if offset < 0.
90 public ByteLookupTable(int offset, byte[] data)
91 throws IllegalArgumentException
95 throw new NullPointerException("Null 'data' argument.");
96 this.data = new byte[][] {data};
100 * Return the lookup tables.
104 public final byte[][] getTable()
110 * Return translated values for a pixel.
112 * For each value in the pixel src, use the value minus offset as an index
113 * in the component array and copy the value there to the output for the
114 * component. If dest is null, the output is a new array, otherwise the
115 * translated values are written to dest. Dest can be the same array as
118 * For example, if the pixel src is [2, 4, 3], and offset is 1, the output
119 * is [comp1[1], comp2[3], comp3[2]], where comp1, comp2, and comp3 are the
120 * translation arrays.
122 * @param src Component values of a pixel.
123 * @param dst Destination array for values, or null.
124 * @return Translated values for the pixel.
126 public int[] lookupPixel(int[] src, int[] dst)
127 throws ArrayIndexOutOfBoundsException
130 dst = new int[src.length];
132 if (data.length == 1)
133 for (int i=0; i < src.length; i++)
134 dst[i] = data[0][src[i] - offset];
136 for (int i=0; i < src.length; i++)
137 dst[i] = data[i][src[i] - offset];
143 * Return translated values for a pixel.
145 * For each value in the pixel src, use the value minus offset as an index
146 * in the component array and copy the value there to the output for the
147 * component. If dest is null, the output is a new array, otherwise the
148 * translated values are written to dest. Dest can be the same array as
151 * For example, if the pixel src is [2, 4, 3], and offset is 1, the output
152 * is [comp1[1], comp2[3], comp3[2]], where comp1, comp2, and comp3 are the
153 * translation arrays.
155 * @param src Component values of a pixel.
156 * @param dst Destination array for values, or null.
157 * @return Translated values for the pixel.
159 public byte[] lookupPixel(byte[] src, byte[] dst)
160 throws ArrayIndexOutOfBoundsException
163 dst = new byte[src.length];
165 if (data.length == 1)
166 for (int i=0; i < src.length; i++)
167 dst[i] = data[0][((int)src[i]) - offset];
169 for (int i=0; i < src.length; i++)
170 dst[i] = data[i][((int)src[i]) - offset];