Source for java.awt.image.ShortLookupTable

   1: /* ShortLookupTable.java -- Java class for a pixel translation table.
   2:    Copyright (C) 2004, 2005  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   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)
   9: any later version.
  10: 
  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.
  15: 
  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
  19: 02110-1301 USA.
  20: 
  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
  24: combination.
  25: 
  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. */
  37: 
  38: 
  39: package java.awt.image;
  40: 
  41: /**
  42:  * ShortLookupTable 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.
  46:  *
  47:  * @author Jerry Quinn (jlquinn@optonline.net)
  48:  * @version 1.0
  49:  */
  50: public class ShortLookupTable extends LookupTable
  51: {
  52:   // Array of translation tables.
  53:   private short data[][];
  54: 
  55:   /**
  56:    * Creates a new <code>ShortLookupTable</code> instance.
  57:    *
  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
  60:    * components.
  61:    * 
  62:    * @param offset Offset to be subtracted.
  63:    * @param data Array of lookup tables.
  64:    * @exception IllegalArgumentException if offset &lt; 0 or data.length &lt; 1.
  65:    */
  66:   public ShortLookupTable(int offset, short[][] data)
  67:     throws IllegalArgumentException
  68:   {
  69:     super(offset, data.length);
  70:     this.data = data;
  71:   }
  72: 
  73:   /**
  74:    * Creates a new <code>ShortLookupTable</code> instance.
  75:    *
  76:    * Offset is subtracted from pixel values when looking up in the translation
  77:    * table.  The same table is applied to all pixel components.
  78:    * 
  79:    * @param offset Offset to be subtracted.
  80:    * @param data Lookup table for all components.
  81:    * @exception IllegalArgumentException if offset &lt; 0.
  82:    */
  83:   public ShortLookupTable(int offset, short[] data)
  84:     throws IllegalArgumentException
  85:   {
  86:     super(offset, 1);
  87:     this.data = new short[][] {data};
  88:   }
  89: 
  90:   /** Return the lookup tables. */
  91:   public final short[][] getTable()
  92:   {
  93:     return data;
  94:   }
  95: 
  96:   /**
  97:    * Return translated values for a pixel.
  98:    *
  99:    * For each value in the pixel src, use the value minus offset as an index
 100:    * in the component array and copy the value there to the output for the
 101:    * component.  If dest is null, the output is a new array, otherwise the
 102:    * translated values are written to dest.  Dest can be the same array as
 103:    * src.
 104:    *
 105:    * For example, if the pixel src is [2, 4, 3], and offset is 1, the output
 106:    * is [comp1[1], comp2[3], comp3[2]], where comp1, comp2, and comp3 are the
 107:    * translation arrays.
 108:    *
 109:    * @param src Component values of a pixel.
 110:    * @param dst Destination array for values, or null.
 111:    * @return Translated values for the pixel.
 112:    */
 113:   public int[] lookupPixel(int[] src, int[] dst)
 114:     throws ArrayIndexOutOfBoundsException
 115:   {
 116:     if (dst == null)
 117:       dst = new int[src.length];
 118: 
 119:     if (data.length == 1)
 120:       for (int i=0; i < src.length; i++)
 121:     dst[i] = data[0][src[i] - offset];
 122:     else
 123:       for (int i=0; i < src.length; i++)
 124:     dst[i] = data[i][src[i] - offset];
 125:       
 126:     return dst;
 127:   }
 128: 
 129:   /**
 130:    * Return translated values for a pixel.
 131:    *
 132:    * For each value in the pixel src, use the value minus offset as an index
 133:    * in the component array and copy the value there to the output for the
 134:    * component.  If dest is null, the output is a new array, otherwise the
 135:    * translated values are written to dest.  Dest can be the same array as
 136:    * src.
 137:    *
 138:    * For example, if the pixel src is [2, 4, 3], and offset is 1, the output
 139:    * is [comp1[1], comp2[3], comp3[2]], where comp1, comp2, and comp3 are the
 140:    * translation arrays.
 141:    *
 142:    * @param src Component values of a pixel.
 143:    * @param dst Destination array for values, or null.
 144:    * @return Translated values for the pixel.
 145:    */
 146:   public short[] lookupPixel(short[] src, short[] dst)
 147:     throws ArrayIndexOutOfBoundsException
 148:   {
 149:     if (dst == null)
 150:       dst = new short[src.length];
 151: 
 152:     if (data.length == 1)
 153:       for (int i=0; i < src.length; i++)
 154:     dst[i] = data[0][((int)src[i]) - offset];
 155:     else
 156:       for (int i=0; i < src.length; i++)
 157:     dst[i] = data[i][((int)src[i]) - offset];
 158:       
 159:     return dst;
 160: 
 161:   }
 162: }