Source for gnu.java.awt.Buffers

   1: /* Buffers.java --
   2:    Copyright (C) 2000, 2002, 2004  Free Software Foundation
   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 gnu.java.awt;
  40: 
  41: import java.awt.image.DataBuffer;
  42: import java.awt.image.DataBufferByte;
  43: import java.awt.image.DataBufferDouble;
  44: import java.awt.image.DataBufferFloat;
  45: import java.awt.image.DataBufferInt;
  46: import java.awt.image.DataBufferShort;
  47: import java.awt.image.DataBufferUShort;
  48: 
  49: /** 
  50:  * Utility class for creating and accessing data buffers of arbitrary
  51:  * data types.
  52:  */
  53: public final class Buffers
  54: {
  55:   /**
  56:    * Create a data buffer of a particular type.
  57:    *
  58:    * @param dataType the desired data type of the buffer.
  59:    * @param data an array containing data, or null
  60:    * @param size the size of the data buffer bank
  61:    */
  62:   public static DataBuffer createBuffer(int dataType, Object data,
  63:                     int size)
  64:   {
  65:     if (data == null) return createBuffer(dataType, size, 1);
  66: 
  67:     return createBufferFromData(dataType, data, size);
  68:   }
  69: 
  70: 
  71:   /**
  72:    * Create a data buffer of a particular type.
  73:    *
  74:    * @param dataType the desired data type of the buffer.
  75:    * @param size the size of the data buffer bank
  76:    */
  77:   public static DataBuffer createBuffer(int dataType, int size) {
  78:     return createBuffer(dataType, size, 1);
  79:   }
  80: 
  81:   /**
  82:    * Create a data buffer of a particular type.
  83:    *
  84:    * @param dataType the desired data type of the buffer.
  85:    * @param size the size of the data buffer bank
  86:    * @param numBanks the number of banks the buffer should have
  87:    */
  88:   public static DataBuffer createBuffer(int dataType, int size, int numBanks)
  89:   {
  90:     switch (dataType)
  91:       {
  92:       case DataBuffer.TYPE_BYTE:
  93:     return new DataBufferByte(size, numBanks);
  94:       case DataBuffer.TYPE_SHORT:
  95:     return new DataBufferShort(size, numBanks);
  96:       case DataBuffer.TYPE_USHORT:
  97:     return new DataBufferUShort(size, numBanks);
  98:       case DataBuffer.TYPE_INT:
  99:     return new DataBufferInt(size, numBanks);
 100:       case DataBuffer.TYPE_FLOAT:
 101:     return new DataBufferFloat(size, numBanks);
 102:       case DataBuffer.TYPE_DOUBLE:
 103:     return new DataBufferDouble(size, numBanks);
 104:       default:
 105:     throw new UnsupportedOperationException();
 106:       }
 107:   }
 108:   
 109:   /**
 110:    * Create a data buffer of a particular type.
 111:    *
 112:    * @param dataType the desired data type of the buffer
 113:    * @param data an array containing the data
 114:    * @param size the size of the data buffer bank
 115:    */
 116:   public static DataBuffer createBufferFromData(int dataType, Object data,
 117:                         int size)
 118:   {
 119:     switch (dataType)
 120:       {
 121:       case DataBuffer.TYPE_BYTE:
 122:     return new DataBufferByte((byte[]) data, size);
 123:       case DataBuffer.TYPE_SHORT:
 124:     return new DataBufferShort((short[]) data, size);
 125:       case DataBuffer.TYPE_USHORT:
 126:     return new DataBufferUShort((short[]) data, size);
 127:       case DataBuffer.TYPE_INT:
 128:     return new DataBufferInt((int[]) data, size);
 129:       case DataBuffer.TYPE_FLOAT:
 130:     return new DataBufferFloat((float[]) data, size);
 131:       case DataBuffer.TYPE_DOUBLE:
 132:     return new DataBufferDouble((double[]) data, size);
 133:       default:
 134:     throw new UnsupportedOperationException();
 135:       }
 136:   }
 137: 
 138:   /** 
 139:    * Return the data array of a data buffer, regardless of the data
 140:    * type.
 141:    *
 142:    * @return an array of primitive values. The actual array type
 143:    * depends on the data type of the buffer.
 144:    */
 145:   public static Object getData(DataBuffer buffer)
 146:   {
 147:     if (buffer instanceof DataBufferByte)
 148:       return ((DataBufferByte) buffer).getData();
 149: 
 150:     if (buffer instanceof DataBufferShort)
 151:       return ((DataBufferShort) buffer).getData();
 152: 
 153:     if (buffer instanceof DataBufferUShort)
 154:       return ((DataBufferUShort) buffer).getData();
 155: 
 156:     if (buffer instanceof DataBufferInt)
 157:       return ((DataBufferInt) buffer).getData();
 158: 
 159:     if (buffer instanceof DataBufferFloat)
 160:       return ((DataBufferFloat) buffer).getData();
 161: 
 162:     if (buffer instanceof DataBufferDouble)
 163:       return ((DataBufferDouble) buffer).getData();
 164: 
 165:     throw new ClassCastException("Unknown data buffer type");
 166:   }
 167: 
 168:     
 169:   /**
 170:    * Copy data from array contained in data buffer, much like
 171:    * System.arraycopy. Create a suitable destination array if the
 172:    * given destination array is null.
 173:    */
 174:   public static Object getData(DataBuffer src, int srcOffset,
 175:                    Object dest,  int destOffset,
 176:                    int length)
 177:   {
 178:     Object from;
 179:     if (src instanceof DataBufferByte)
 180:       {
 181:     from = ((DataBufferByte) src).getData();
 182:     if (dest == null) dest = new byte[length+destOffset];
 183:       }
 184:     else if (src instanceof DataBufferShort)
 185:       {
 186:     from = ((DataBufferShort) src).getData();
 187:     if (dest == null) dest = new short[length+destOffset];
 188:       }
 189:     else if (src instanceof DataBufferUShort)
 190:       {
 191:     from = ((DataBufferUShort) src).getData();
 192:     if (dest == null) dest = new short[length+destOffset];
 193:       }
 194:     else if (src instanceof DataBufferInt)
 195:       {
 196:     from = ((DataBufferInt) src).getData();
 197:     if (dest == null) dest = new int[length+destOffset];
 198:       }
 199:     else if (src instanceof DataBufferFloat)
 200:       {
 201:     from = ((DataBufferFloat) src).getData();
 202:     if (dest == null) dest = new float[length+destOffset];
 203:       }
 204:     else if (src instanceof DataBufferDouble)
 205:       {
 206:     from = ((DataBufferDouble) src).getData();
 207:     if (dest == null) dest = new double[length+destOffset];
 208:       }
 209:     else
 210:       {
 211:     throw new ClassCastException("Unknown data buffer type");
 212:       }
 213:     
 214:     System.arraycopy(from, srcOffset, dest, destOffset, length);
 215:     return dest;
 216:   }
 217:   
 218:   /**
 219:    * @param bits the width of a data element measured in bits
 220:    *
 221:    * @return the smallest data type that can store data elements of
 222:    * the given number of bits, without any truncation.
 223:    */
 224:   public static int smallestAppropriateTransferType(int bits)
 225:   {
 226:     if (bits <= 8)
 227:       {
 228:     return DataBuffer.TYPE_BYTE;
 229:       }
 230:     else if (bits <= 16)
 231:       {
 232:     return DataBuffer.TYPE_USHORT;
 233:       } 
 234:     else if (bits <= 32)
 235:       {
 236:     return DataBuffer.TYPE_INT;
 237:       }
 238:     else
 239:       {
 240:     return DataBuffer.TYPE_UNDEFINED;
 241:       }
 242:   }
 243: }