001/* ShortLookupTable.java -- Java class for a pixel translation table. 002 Copyright (C) 2004, 2005, 2006, Free Software Foundation, Inc. 003 004This file is part of GNU Classpath. 005 006GNU Classpath is free software; you can redistribute it and/or modify 007it under the terms of the GNU General Public License as published by 008the Free Software Foundation; either version 2, or (at your option) 009any later version. 010 011GNU Classpath is distributed in the hope that it will be useful, but 012WITHOUT ANY WARRANTY; without even the implied warranty of 013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014General Public License for more details. 015 016You should have received a copy of the GNU General Public License 017along with GNU Classpath; see the file COPYING. If not, write to the 018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 01902110-1301 USA. 020 021Linking this library statically or dynamically with other modules is 022making a combined work based on this library. Thus, the terms and 023conditions of the GNU General Public License cover the whole 024combination. 025 026As a special exception, the copyright holders of this library give you 027permission to link this library with independent modules to produce an 028executable, regardless of the license terms of these independent 029modules, and to copy and distribute the resulting executable under 030terms of your choice, provided that you also meet, for each linked 031independent module, the terms and conditions of the license of that 032module. An independent module is a module which is not derived from 033or based on this library. If you modify this library, you may extend 034this exception to your version of the library, but you are not 035obligated to do so. If you do not wish to do so, delete this 036exception statement from your version. */ 037 038 039package java.awt.image; 040 041/** 042 * ShortLookupTable represents translation arrays for pixel values. It wraps 043 * one or more data arrays for each layer (or component) in an image, such as 044 * Alpha, R, G, and B. When doing translation, the offset is subtracted from 045 * the pixel values to allow a subset of an array to be used. 046 * 047 * @author Jerry Quinn (jlquinn@optonline.net) 048 * @version 1.0 049 */ 050public class ShortLookupTable extends LookupTable 051{ 052 // Array of translation tables. 053 private short data[][]; 054 055 /** 056 * Creates a new <code>ShortLookupTable</code> instance. 057 * 058 * Offset is subtracted from pixel values when looking up in the translation 059 * tables. If data.length is one, the same table is applied to all pixel 060 * components. 061 * 062 * @param offset Offset to be subtracted. 063 * @param data Array of lookup tables. 064 * @exception IllegalArgumentException if offset < 0 or data.length < 1. 065 */ 066 public ShortLookupTable(int offset, short[][] data) 067 throws IllegalArgumentException 068 { 069 super(offset, data.length); 070 071 // tests show that Sun's implementation creates a new array to store the 072 // references from the incoming 'data' array - not sure why, but we'll 073 // match that behaviour just in case it matters... 074 this.data = new short[data.length][]; 075 for (int i = 0; i < data.length; i++) 076 this.data[i] = data[i]; 077 } 078 079 /** 080 * Creates a new <code>ShortLookupTable</code> instance. 081 * 082 * Offset is subtracted from pixel values when looking up in the translation 083 * table. The same table is applied to all pixel components. 084 * 085 * @param offset Offset to be subtracted. 086 * @param data Lookup table for all components (<code>null</code> not 087 * permitted). 088 * @exception IllegalArgumentException if offset < 0. 089 */ 090 public ShortLookupTable(int offset, short[] data) 091 throws IllegalArgumentException 092 { 093 super(offset, 1); 094 if (data == null) 095 throw new NullPointerException("Null 'data' argument."); 096 this.data = new short[][] {data}; 097 } 098 099 /** 100 * Return the lookup tables. This is a reference to the actual table, so 101 * modifying the contents of the returned array will modify the lookup table. 102 * 103 * @return The lookup table. 104 */ 105 public final short[][] getTable() 106 { 107 return data; 108 } 109 110 /** 111 * Return translated values for a pixel. 112 * 113 * For each value in the pixel src, use the value minus offset as an index 114 * in the component array and copy the value there to the output for the 115 * component. If dest is null, the output is a new array, otherwise the 116 * translated values are written to dest. Dest can be the same array as 117 * src. 118 * 119 * For example, if the pixel src is [2, 4, 3], and offset is 1, the output 120 * is [comp1[1], comp2[3], comp3[2]], where comp1, comp2, and comp3 are the 121 * translation arrays. 122 * 123 * @param src Component values of a pixel. 124 * @param dst Destination array for values, or null. 125 * @return Translated values for the pixel. 126 */ 127 public int[] lookupPixel(int[] src, int[] dst) 128 throws ArrayIndexOutOfBoundsException 129 { 130 if (dst == null) 131 dst = new int[src.length]; 132 133 if (data.length == 1) 134 for (int i = 0; i < src.length; i++) 135 dst[i] = data[0][src[i] - offset]; 136 else 137 for (int i = 0; i < src.length; i++) 138 dst[i] = data[i][src[i] - offset]; 139 140 return dst; 141 } 142 143 /** 144 * Return translated values for a pixel. 145 * 146 * For each value in the pixel src, use the value minus offset as an index 147 * in the component array and copy the value there to the output for the 148 * component. If dest is null, the output is a new array, otherwise the 149 * translated values are written to dest. Dest can be the same array as 150 * src. 151 * 152 * For example, if the pixel src is [2, 4, 3], and offset is 1, the output 153 * is [comp1[1], comp2[3], comp3[2]], where comp1, comp2, and comp3 are the 154 * translation arrays. 155 * 156 * @param src Component values of a pixel. 157 * @param dst Destination array for values, or null. 158 * @return Translated values for the pixel. 159 * 160 */ 161 public short[] lookupPixel(short[] src, short[] dst) 162 throws ArrayIndexOutOfBoundsException 163 { 164 if (dst == null) 165 dst = new short[src.length]; 166 167 if (data.length == 1) 168 for (int i = 0; i < src.length; i++) 169 dst[i] = data[0][((int) src[i]) - offset]; 170 else 171 for (int i = 0; i < src.length; i++) 172 dst[i] = data[i][((int) src[i]) - offset]; 173 174 return dst; 175 176 } 177}