home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-20 | 5.5 KB | 161 lines |
- /*
- * @(#)ByteLookupTable.java 1.20 98/03/18
- *
- * Copyright 1997, 1998 by Sun Microsystems, Inc.,
- * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
- * All rights reserved.
- *
- * This software is the confidential and proprietary information
- * of Sun Microsystems, Inc. ("Confidential Information"). You
- * shall not disclose such Confidential Information and shall use
- * it only in accordance with the terms of the license agreement
- * you entered into with Sun.
- */
-
- package java.awt.image;
-
-
- /**
- * This class defines a lookup table object. The lookup table
- * contains byte data for one or more tile channels or image components
- * (for example, separate arrays for R, G, and B),
- * and it contains an offset which will be subtracted from the
- * input value before indexing the array. This allows an array
- * smaller than the native data size to be provided for a
- * constrained input. If there is only one array in the lookup
- * table, it will be applied to all channels.
- * The byte data is expected to be unsigned data so it will be ANDed
- * with 0xff before it is used to index into the lookup table.
- *
- * @see ShortLookupTable
- * @see LookupOp
- * @version 10 Feb 1997
- */
- public class ByteLookupTable extends LookupTable {
-
- /**
- * Constants
- */
-
- byte data[][];
-
- /**
- * Create a LookupTable object from an array of byte
- * arrays representing a lookup table for each tile
- * channel. The offset will be subtracted from the input
- * value before indexing into the arrays. The number of
- * components is the length of the data argument. The
- * data array for each component stored as a reference.
- */
- public ByteLookupTable(int offset, byte data[][]) {
- super(offset,data.length);
- numComponents = data.length;
- numEntries = data[0].length;
- this.data = new byte[numComponents][];
- // Allocate the array and copy the data reference
- for (int i=0; i < numComponents; i++) {
- this.data[i] = data[i];
- }
- }
-
- /**
- * Create a LookupTable object from an array
- * of bytes representing a lookup table for each tile
- * channel. The offset will be subtracted from the input
- * value before indexing into the array. The number of
- * components is one. The data array will be stored internally.
- */
- public ByteLookupTable(int offset, byte data[]) {
- super(offset,data.length);
- numComponents = 1;
- numEntries = data.length;
- this.data = new byte[1][];
- this.data[0] = data;
- }
-
- /**
- * Returns the lookup table data.
- * @return LookupTable data array.
- */
- public byte[][] getDataStorage(){
- return data;
- }
-
- /**
- * Returns an array of components of a pixel, translated with the lookup
- * table. The source and destination can be equal. If dst is null,
- * a new array will be allocated. The dst array is returned.
- * @return an int array of components.
- */
- public int[] lookupPixel(int[] src, int[] dst){
- if (dst == null) {
- // Need to alloc a new destination array
- dst = new int[src.length];
- }
-
- if (numComponents == 1) {
- // Apply one LUT to all channels
- for (int i=0; i < src.length; i++) {
- int s = src[i] - offset;
- if (s < 0) {
- throw new ArrayIndexOutOfBoundsException("src["+i+
- "]-offset is "+
- "less than zero");
- }
- dst[i] = (int) data[0][s];
- }
- }
- else {
- for (int i=0; i < src.length; i++) {
- int s = src[i] - offset;
- if (s < 0) {
- throw new ArrayIndexOutOfBoundsException("src["+i+
- "]-offset is "+
- "less than zero");
- }
- dst[i] = (int) data[i][s];
- }
- }
- return dst;
- }
-
- /**
- * Returns an array of components of a pixel, translated with the lookup
- * table. The source and destination can be equal. If dst is null,
- * a new array will be allocated. The dst array is returned.
- * @return a byte array of components.
- */
- public byte[] lookupPixel(byte[] src, byte[] dst){
- if (dst == null) {
- // Need to alloc a new destination array
- dst = new byte[src.length];
- }
-
- if (numComponents == 1) {
- // Apply one LUT to all channels
- for (int i=0; i < src.length; i++) {
- int s = (src[i]&0xff) - offset;
- if (s < 0) {
- throw new ArrayIndexOutOfBoundsException("src["+i+
- "]-offset is "+
- "less than zero");
- }
- dst[i] = data[0][s];
- }
- }
- else {
- for (int i=0; i < src.length; i++) {
- int s = (src[i]&0xff) - offset;
- if (s < 0) {
- throw new ArrayIndexOutOfBoundsException("src["+i+
- "]-offset is "+
- "less than zero");
- }
- dst[i] = data[i][s];
- }
- }
- return dst;
- }
-
- }
-