cmtkTypes.cxx

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 1997-2009 Torsten Rohlfing
00004 //
00005 //  Copyright 2004-2010 SRI International
00006 //
00007 //  This file is part of the Computational Morphometry Toolkit.
00008 //
00009 //  http://www.nitrc.org/projects/cmtk/
00010 //
00011 //  The Computational Morphometry Toolkit is free software: you can
00012 //  redistribute it and/or modify it under the terms of the GNU General Public
00013 //  License as published by the Free Software Foundation, either version 3 of
00014 //  the License, or (at your option) any later version.
00015 //
00016 //  The Computational Morphometry Toolkit is distributed in the hope that it
00017 //  will be useful, but WITHOUT ANY WARRANTY; without even the implied
00018 //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 //  GNU General Public License for more details.
00020 //
00021 //  You should have received a copy of the GNU General Public License along
00022 //  with the Computational Morphometry Toolkit.  If not, see
00023 //  <http://www.gnu.org/licenses/>.
00024 //
00025 //  $Revision: 2398 $
00026 //
00027 //  $LastChangedDate: 2010-10-05 14:54:37 -0700 (Tue, 05 Oct 2010) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
00030 //
00031 */
00032 
00033 #include <Base/cmtkTypes.h>
00034 
00035 #include <string.h>
00036 
00037 namespace
00038 cmtk
00039 {
00040 
00043 
00044 const char *DataClassString[] = 
00045 { "grey", "label", "unknown",
00046   NULL };
00047 
00048 const char* DataTypeName[] = 
00049 { "byte (8bit unsigned)",
00050   "char (8bit signed)",
00051   "short (16bit signed)",
00052   "ushort (16bit unsigned)",
00053   "int (32bit signed)",
00054   "uint (32bit unsigned)",
00055   "float (32bit)",
00056   "double (64bit)"
00057 };
00058 
00059 DataClass
00060 StringToDataClass( const char *dataClassStr )
00061 {
00062   if ( dataClassStr ) 
00063     {
00064     for ( int idx=0; DataClassString[idx]; ++idx ) 
00065       {
00066       if ( !strcmp( dataClassStr, DataClassString[idx] ) )
00067         return (DataClass) idx;
00068       }
00069     }
00070   
00071   return DATACLASS_UNKNOWN;
00072 }
00073 
00074 const char*
00075 DataClassToString( const DataClass dataClass )
00076 {
00077   return DataClassString[ static_cast<int>( dataClass ) ];
00078 }
00079 
00080 size_t
00081 TypeItemSize ( const ScalarDataType dtype ) 
00082 {
00083   switch (dtype)
00084     {
00085     case TYPE_BYTE: return sizeof(byte);      
00086     case TYPE_CHAR: return sizeof(char);
00087     case TYPE_SHORT: return sizeof(short);
00088     case TYPE_USHORT: return sizeof(unsigned short);
00089     case TYPE_INT: return sizeof(int);
00090     case TYPE_UINT: return sizeof(int);
00091     case TYPE_FLOAT: return sizeof(float);
00092     case TYPE_DOUBLE: return sizeof(double);
00093     default : return 0;
00094     }
00095 }
00096 
00097 ScalarDataType 
00098 SelectDataTypeInteger( const byte itemSize, const bool signBit )
00099 {
00100   if ( signBit ) 
00101     {
00102     switch ( itemSize ) 
00103       {
00104       case 1 : return TYPE_CHAR;
00105       case 2 : return TYPE_SHORT;
00106       case 4 : return TYPE_INT;
00107       default: return TYPE_NONE;
00108       }
00109     } 
00110   else
00111     {
00112     switch ( itemSize ) 
00113       {
00114       case 1 : return TYPE_BYTE;
00115       case 2 : return TYPE_USHORT;
00116       case 4 : return TYPE_INT;
00117       default: return TYPE_NONE;
00118       }
00119     }
00120 }
00121 
00122 ScalarDataType
00123 GetSignedDataType( const ScalarDataType dtype )
00124 {
00125   switch ( dtype ) 
00126     {
00127     case TYPE_BYTE:
00128       return TYPE_CHAR;
00129     case TYPE_USHORT:
00130       return TYPE_SHORT;
00131     case TYPE_UINT:
00132       return TYPE_INT;
00133     default:
00134       return dtype;
00135     }
00136 }
00137 
00138 ScalarDataType
00139 GetDifferenceDataType
00140 ( const ScalarDataType dtype1, const ScalarDataType dtype2 )
00141 {
00142   if ( dtype1 == TYPE_DOUBLE || dtype2 == TYPE_DOUBLE )
00143     return TYPE_DOUBLE;
00144   if ( dtype1 == TYPE_FLOAT || dtype2 == TYPE_FLOAT )
00145     return TYPE_FLOAT;
00146   
00147   if ( dtype1 == TYPE_INT || dtype2 == TYPE_INT )
00148     return TYPE_FLOAT;
00149   if ( dtype1 == TYPE_UINT || dtype2 == TYPE_UINT )
00150     return TYPE_FLOAT;
00151 
00152   if ( dtype1 == TYPE_SHORT || dtype2 == TYPE_SHORT )
00153     return TYPE_INT;
00154   if ( dtype1 == TYPE_USHORT || dtype2 == TYPE_USHORT )
00155     return TYPE_INT;
00156 
00157   if ( dtype1 == TYPE_BYTE || dtype2 == TYPE_BYTE )
00158     return TYPE_SHORT; 
00159   if ( dtype1 == TYPE_CHAR || dtype2 == TYPE_CHAR )
00160     return TYPE_SHORT;
00161 
00162   return TYPE_CHAR;
00163 }
00164 
00165 ScalarDataType
00166 GetUnsignedDataType( const ScalarDataType dtype )
00167 {
00168   switch ( dtype ) 
00169     {
00170     case TYPE_CHAR:
00171       return TYPE_BYTE;
00172     case TYPE_SHORT:
00173       return TYPE_USHORT;
00174     case TYPE_INT:
00175       return TYPE_UINT;
00176     default:
00177       return dtype;
00178     }
00179 }
00180 
00181 } // namespace cmtk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines