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 / DataBuffer.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  9.0 KB  |  292 lines

  1. /*
  2.  * @(#)DataBuffer.java    1.8 98/03/18
  3.  *
  4.  * Copyright 1997 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. /* ****************************************************************
  16.  ******************************************************************
  17.  ******************************************************************
  18.  *** COPYRIGHT (c) Eastman Kodak Company, 1997
  19.  *** As  an unpublished  work pursuant to Title 17 of the United
  20.  *** States Code.  All rights reserved.
  21.  ******************************************************************
  22.  ******************************************************************
  23.  ******************************************************************/
  24.  
  25. package java.awt.image;
  26.  
  27. /**
  28.  * This class exists to wrap one or more data arrays.  Each data array in
  29.  * the DataBuffer is referred to as a bank.  Accessor methods for getting
  30.  * and setting elements of the DataBuffer's banks exist with and without
  31.  * a bank specifier.  The without bank specifiers use the default 0th
  32.  * bank.  The DataBuffer can optionally take an offset per bank, so that
  33.  * data in an existing array can be used even if the interesting data
  34.  * doesn't start at array location zero.  Getting or setting the 0th
  35.  * element of a bank, uses the (0+offset)th element of the array.  The
  36.  * size field specifies how much of the data array is available for
  37.  * use.  The size + offset fields for a given bank should never be longer
  38.  * than the array length of the associatied data array.  Generally, an
  39.  * object of class DataBuffer will be cast down to one of it's subclasses
  40.  * for improved speed.
  41.  */
  42.  
  43. public abstract class DataBuffer
  44. {
  45.     
  46.     /** Tag for byte data. */
  47.     public static final int BYTE_DATA  = 0;
  48.  
  49.     /** Tag for short data. */
  50.     public static final int SHORT_DATA = 1;
  51.  
  52.     /** Tag for int data. */
  53.     public static final int INT_DATA   = 2;
  54.  
  55.     /** Tag for long data. */
  56.     public static final int LONG_DATA  = 3;
  57.  
  58.     /** Tag for byte data. */
  59.     public static final int FLOAT_DATA  = 4;
  60.  
  61.     /** Tag for byte data. */
  62.     public static final int DOUBLE_DATA  = 5;
  63.  
  64.     /** The datatype of this DataBuffer. */
  65.     protected int dataType;
  66.  
  67.     /** The number of banks (or arrays in this DataBuffer). */
  68.     protected int banks;
  69.  
  70.     /** Offset into default (first) bank from which to get the first element. */
  71.     protected int offset;
  72.  
  73.     /** Usable size of the a bank. */
  74.     protected int size;
  75.  
  76.     /** Offsets into all banks. */
  77.     protected int offsets[];
  78.  
  79.     /** Size of the data types indexed by DataType tags defined above. */
  80.     private static final int dataTypeSize[] = {8,16,32};
  81.  
  82.     /** Size of the data type, given a datatype tag. */
  83.     public static int sizeOf(int type) {
  84.         return dataTypeSize[type];
  85.     }
  86.  
  87.     /** 
  88.      * Converts an input array of some primitive datatype (byte/short/int)
  89.      * into an integer array.  If the input array is of type int[], it
  90.      * will be returned as is.  Otherwise, an integer array will be 
  91.      * created and elements promoted to ints and copied.  Masks will be
  92.      * used to prevent sign extension on byte values.  Returns null
  93.      * if obj is not of type byte[], short[] or int[].
  94.      */
  95.     public static int[] toIntArray(Object obj) {
  96.        if (obj instanceof int[]) {
  97.            return (int[])obj;
  98.        } else if (obj == null) {
  99.            return null;
  100.        } else if (obj instanceof short[]) {
  101.            short sdata[] = (short[])obj;
  102.            int idata[] = new int[sdata.length];
  103.            for (int i = 0; i < sdata.length; i++) {
  104.                idata[i] = (int)sdata[i];
  105.            }
  106.            return idata;
  107.        } else if (obj instanceof byte[]) {
  108.            byte bdata[] = (byte[])obj;
  109.            int idata[] = new int[bdata.length];
  110.            for (int i = 0; i < bdata.length; i++) {
  111.                idata[i] = 0xff & (int)bdata[i]; 
  112.            }
  113.            return idata;
  114.        }
  115.        return null;
  116.     }
  117.        
  118.     /**
  119.      *  Constructs a DataBuffer containing one bank of the specified
  120.      *  data type and size.
  121.      */
  122.     protected DataBuffer(int dataType, int size) {
  123.         this.dataType = dataType;
  124.         this.banks = 1;
  125.         this.size = size;
  126.         this.offset = 0;
  127.         this.offsets = new int[1];  // init to 0 by new
  128.     }
  129.  
  130.     /**
  131.      *  Constructs a DataBuffer containing the specified number of 
  132.      *  banks.  Each bank has the specified size and an offset of 0. 
  133.      */
  134.     protected DataBuffer(int dataType, int size, int numBanks) {
  135.         this.dataType = dataType;
  136.         this.banks = numBanks;
  137.         this.size = size;
  138.         this.offset = 0;
  139.         this.offsets = new int[banks]; // init to 0 by new
  140.     }
  141.  
  142.     /**
  143.      *  Constructs a DataBuffer which contains the specified number
  144.      *  of banks.  Each bank has the specified datatype, size and offset.
  145.      */
  146.     protected DataBuffer(int dataType, int size, int numBanks, int offset) {
  147.         this.dataType = dataType;
  148.         this.banks = numBanks;
  149.         this.size = size;
  150.         this.offset = 0;
  151.         this.offsets = new int[numBanks];
  152.         for (int i = 0; i < numBanks; i++) {
  153.             this.offsets[i] = offset;
  154.         }
  155.     }
  156.  
  157.     /**
  158.      *  Constructs a DataBuffer which contains the specified number
  159.      *  of banks.  Each bank has the specified datatype and size.  The
  160.      *  offset for each bank corresponds to it's respective entry in
  161.      *  the offsets array.
  162.      */
  163.     protected DataBuffer(int dataType, int size, int numBanks, int offsets[]) {
  164.         if (numBanks != offsets.length) {
  165.             throw new ArrayIndexOutOfBoundsException("Number of banks" +
  166.                  " does not match number of bank offsets");
  167.         }
  168.         this.dataType = dataType;
  169.         this.banks = numBanks;
  170.         this.size = size;
  171.         this.offset = offsets[0];
  172.         this.offsets = (int[])offsets.clone();
  173.     }
  174.  
  175.     /**  This returns the data type of this DataBuffer.  */
  176.     public int getDataType() {
  177.         return dataType;
  178.     }
  179.   
  180.     /**  Returns the size of the default bank in array elements. */
  181.     public int getSize() {
  182.         return size;
  183.     }
  184.  
  185.     /** Returns the offset of the default bank in array elements.  */
  186.     public int getOffset() {
  187.         return offset;
  188.     }
  189.  
  190.     /** Returns the offsets of all the banks in array elements. */
  191.     public int[] getOffsets() {
  192.         return (int[])offsets.clone();
  193.     }
  194.  
  195.     /** Returns the number of banks in this DataBuffer. */
  196.     public int getNumBanks() {
  197.         return banks;
  198.     }
  199.  
  200.     /**
  201.      * Returns the requested data array element from the first (default) bank
  202.      * as an integer.
  203.      */
  204.     public int getElem(int i) {
  205.         return getElem(0,i);
  206.     }
  207.  
  208.     /**
  209.      * Returns the requested data array element from the specified bank
  210.      * as an integer.
  211.      */
  212.     public abstract int getElem(int bank, int i);
  213.       
  214.     /**
  215.      * Sets the requested data array element in the first (default) bank 
  216.      * from the given integer.
  217.      */
  218.     public void  setElem(int i, int val) {
  219.         setElem(0,i,val);
  220.     }
  221.  
  222.     /**
  223.      * Sets the requested data array element in the specified bank 
  224.      * from the given integer.
  225.      */
  226.     public abstract void setElem(int bank, int i, int val);
  227.  
  228.     /**
  229.      * Returns the requested data array element from the first (default) bank
  230.      * as a float.
  231.      */
  232.     public float getElemFloat(int i) {
  233.         return (float)getElem(i);
  234.     }
  235.  
  236.     /**
  237.      * Returns the requested data array element from the specified bank
  238.      * as a float.
  239.      */
  240.     public float getElemFloat(int bank, int i) {
  241.         return (float)getElem(bank,i);
  242.     }
  243.  
  244.     /**
  245.      * Sets the requested data array element in the first (default) bank
  246.      * from the given float.
  247.      */ 
  248.     public void setElemFloat(int i, float val) {
  249.         setElem(i,(int)val);
  250.     }
  251.  
  252.     /**
  253.      * Sets the requested data array element in the specified bank
  254.      * from the given float.
  255.      */
  256.     public void setElemFloat(int bank, int i, float val) {
  257.         setElem(bank,i,(int)val);
  258.     }
  259.  
  260.     /**
  261.      * Returns the requested data array element from the first (default) bank
  262.      * as a double.
  263.      */
  264.     public double getElemDouble(int i) {
  265.         return (double)getElem(i);
  266.     }
  267.  
  268.     /**
  269.      * Returns the requested data array element from the specified bank
  270.      * as a double.
  271.      */
  272.     public double getElemDouble(int bank, int i) {
  273.         return (double)getElem(bank,i);
  274.     }
  275.  
  276.     /**
  277.      * Sets the requested data array element in the first (default) bank
  278.      * from the given double.
  279.      */
  280.     public void setElemDouble(int i, double val) {
  281.         setElem(i,(int)val);
  282.     }
  283.  
  284.     /**
  285.      * Sets the requested data array element in the specified bank
  286.      * from the given double.
  287.      */
  288.     public void setElemDouble(int bank, int i, double val) { 
  289.         setElem(bank,i,(int)val);
  290.     }
  291.  }
  292.