home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / awt / image / ByteLookupTable.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  5.5 KB  |  161 lines

  1. /*
  2.  * @(#)ByteLookupTable.java    1.20 98/03/18
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.awt.image;
  16.  
  17.  
  18. /**
  19.  * This class defines a lookup table object.  The lookup table
  20.  * contains byte data for one or more tile channels or image components
  21.  * (for example, separate arrays for R, G, and B),
  22.  * and it contains an offset which will be subtracted from the
  23.  * input value before indexing the array.  This allows an array
  24.  * smaller than the native data size to be provided for a
  25.  * constrained input.  If there is only one array in the lookup
  26.  * table, it will be applied to all channels.
  27.  * The byte data is expected to be unsigned data so it will be ANDed
  28.  * with 0xff before it is used to index into the lookup table.
  29.  * 
  30.  * @see ShortLookupTable
  31.  * @see LookupOp
  32.  * @version 10 Feb 1997
  33.  */
  34. public class ByteLookupTable extends LookupTable {
  35.  
  36.     /**
  37.      * Constants
  38.      */  
  39.  
  40.     byte data[][];
  41.     
  42.     /**
  43.      * Create a LookupTable object from an array of byte
  44.      * arrays representing a lookup table for each tile
  45.      * channel.  The offset will be subtracted from the input
  46.      * value before indexing into the arrays.  The number of
  47.      * components is the length of the data argument.  The
  48.      * data array for each component stored as a reference.
  49.      */
  50.     public ByteLookupTable(int offset, byte data[][]) {
  51.         super(offset,data.length);
  52.         numComponents = data.length;
  53.         numEntries    = data[0].length;
  54.         this.data = new byte[numComponents][];
  55.         // Allocate the array and copy the data reference
  56.         for (int i=0; i < numComponents; i++) {
  57.             this.data[i] = data[i];
  58.         }
  59.     }
  60.  
  61.     /**
  62.      * Create a LookupTable object from an array
  63.      * of bytes representing a lookup table for each tile
  64.      * channel.  The offset will be subtracted from the input
  65.      * value before indexing into the array.  The number of
  66.      * components is one.  The data array will be stored internally.
  67.      */
  68.     public ByteLookupTable(int offset, byte data[]) {
  69.         super(offset,data.length);
  70.         numComponents = 1;
  71.         numEntries    = data.length;
  72.         this.data = new byte[1][];
  73.         this.data[0] = data;
  74.     }
  75.  
  76.     /**
  77.      * Returns the lookup table data.
  78.       * @return LookupTable data array.
  79.      */
  80.     public byte[][] getDataStorage(){
  81.         return data;
  82.     }
  83.  
  84.     /**
  85.      * Returns an array of components of a pixel, translated with the lookup
  86.      * table. The source and destination can be equal.  If dst is null,
  87.      * a new array will be allocated.  The dst array is returned.
  88.      * @return an int array of components.
  89.      */
  90.     public int[] lookupPixel(int[] src, int[] dst){
  91.         if (dst == null) {
  92.             // Need to alloc a new destination array
  93.             dst = new int[src.length];
  94.         }
  95.         
  96.         if (numComponents == 1) {
  97.             // Apply one LUT to all channels
  98.             for (int i=0; i < src.length; i++) {
  99.                 int s = src[i] - offset;
  100.                 if (s < 0) {
  101.                     throw new ArrayIndexOutOfBoundsException("src["+i+
  102.                                                              "]-offset is "+
  103.                                                              "less than zero");
  104.                 }
  105.                 dst[i] = (int) data[0][s];
  106.             }
  107.         }
  108.         else {
  109.             for (int i=0; i < src.length; i++) {
  110.                 int s = src[i] - offset;
  111.                 if (s < 0) {
  112.                     throw new ArrayIndexOutOfBoundsException("src["+i+
  113.                                                              "]-offset is "+
  114.                                                              "less than zero");
  115.                 }
  116.                 dst[i] = (int) data[i][s];
  117.             }
  118.         }
  119.         return dst;
  120.     }
  121.  
  122.     /**
  123.      * Returns an array of components of a pixel, translated with the lookup
  124.      * table. The source and destination can be equal.  If dst is null,
  125.      * a new array will be allocated.  The dst array is returned.
  126.      * @return a byte array of components.
  127.      */
  128.     public byte[] lookupPixel(byte[] src, byte[] dst){
  129.         if (dst == null) {
  130.             // Need to alloc a new destination array
  131.             dst = new byte[src.length];
  132.         }
  133.         
  134.         if (numComponents == 1) {
  135.             // Apply one LUT to all channels
  136.             for (int i=0; i < src.length; i++) {
  137.                 int s = (src[i]&0xff) - offset;
  138.                 if (s < 0) {
  139.                     throw new ArrayIndexOutOfBoundsException("src["+i+
  140.                                                              "]-offset is "+
  141.                                                              "less than zero");
  142.                 }
  143.                 dst[i] = data[0][s];
  144.             }
  145.         }
  146.         else {
  147.             for (int i=0; i < src.length; i++) {
  148.                 int s = (src[i]&0xff) - offset;
  149.                 if (s < 0) {
  150.                     throw new ArrayIndexOutOfBoundsException("src["+i+
  151.                                                              "]-offset is "+
  152.                                                              "less than zero");
  153.                 }
  154.                 dst[i] = data[i][s];
  155.             }
  156.         }
  157.         return dst;
  158.     }
  159.  
  160. }
  161.