00001 /* 00002 This file is part of matfile. 00003 00004 Copyright (C) 2010-2011 Andrea Arteaga <arteagaa@student.ethz.ch> 00005 00006 matfile is free software: you can redistribute it and/or modify 00007 it under the terms of the GNU Lesser General Public License as published 00008 by the Free Software Foundation, either version 3 of the License, or 00009 (at your option) any later version. 00010 00011 matfile is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU Lesser General Public License for more details. 00015 00016 You should have received a copy of the GNU Lesser General Public License 00017 along with matfile. If not, see <http://www.gnu.org/licenses/>. 00018 */ 00019 00020 #ifndef OPERATIONS_ON_TYPES_HPP_ 00021 #define OPERATIONS_ON_TYPES_HPP_ 00022 00023 00024 inline miINT32_t typeSize(const MATtype& type) 00025 { 00026 switch(type) { 00027 case miINT8: 00028 case miUINT8: 00029 return 1; 00030 case miINT16: 00031 case miUINT16: 00032 return 2; 00033 case miINT32: 00034 case miUINT32: 00035 return 4; 00036 case miINT64: 00037 case miUINT64: 00038 return 8; 00039 case miSINGLE: 00040 return 4; 00041 case miDOUBLE: 00042 return 8; 00043 default: 00044 return 0; 00045 } 00046 } 00047 00048 00049 template<class T> 00050 std::string class_to_string(T c) 00051 { 00052 switch(c) { 00053 case 1: return "mxCELL_CLASS"; 00054 case 2: return "mxSTRUCT_CLASS"; 00055 case 3: return "mxOBJECT_CLASS"; 00056 case 4: return "mxCHAR_CLASS"; 00057 case 5: return "mxSPARSE_CLASS"; 00058 case 6: return "mxDOUBLE_CLASS"; 00059 case 7: return "mxSINGLE_CLASS"; 00060 case 8: return "mxINT8_CLASS"; 00061 case 9: return "mxUINT8_CLASS"; 00062 case 10: return "mxINT16_CLASS"; 00063 case 11: return "mxUINT16_CLASS"; 00064 case 12: return "mxINT32_CLASS"; 00065 case 13: return "mxUINT32_CLASS"; 00066 default: return "unrecognized class"; 00067 } 00068 } 00069 00070 00071 00072 template<class T> inline T read_as(void* p, MATtype type) { 00073 T ret; 00074 switch(type) { 00075 case miINT8: 00076 ret = *reinterpret_cast<miINT8_t*>(p); 00077 break; 00078 case miUINT8: 00079 ret = *reinterpret_cast<miUINT8_t*>(p); 00080 break; 00081 case miINT16: 00082 ret = *reinterpret_cast<miINT16_t*>(p); 00083 break; 00084 case miUINT16: 00085 ret = *reinterpret_cast<miUINT16_t*>(p); 00086 break; 00087 case miINT32: 00088 ret = *reinterpret_cast<miINT32_t*>(p); 00089 break; 00090 case miUINT32: 00091 ret = *reinterpret_cast<miUINT32_t*>(p); 00092 break; 00093 case miSINGLE: 00094 ret = *reinterpret_cast<miSINGLE_t*>(p); 00095 break; 00096 case miDOUBLE: 00097 ret = *reinterpret_cast<miDOUBLE_t*>(p); 00098 break; 00099 } 00100 return ret; 00101 } 00102 00103 template<class T> inline void write_as(const T& v, void *p, MATtype type) 00104 { 00105 switch (type) { 00106 case miINT8: 00107 *reinterpret_cast<miINT8_t*>(p) = v; 00108 break; 00109 case miUINT8: 00110 *reinterpret_cast<miUINT8_t*>(p) = v; 00111 break; 00112 case miINT16: 00113 *reinterpret_cast<miINT16_t*>(p) = v; 00114 break; 00115 case miUINT16: 00116 *reinterpret_cast<miUINT16_t*>(p) = v; 00117 break; 00118 case miINT32: 00119 *reinterpret_cast<miINT32_t*>(p) = v; 00120 break; 00121 case miUINT32: 00122 *reinterpret_cast<miUINT32_t*>(p) = v; 00123 break; 00124 case miSINGLE: 00125 *reinterpret_cast<miSINGLE_t*>(p) = v; 00126 break; 00127 case miDOUBLE: 00128 *reinterpret_cast<miDOUBLE_t*>(p) = v; 00129 break; 00130 } 00131 } 00132 00133 00134 00135 00136 /* Mathematics functions */ 00137 00138 template<class T> inline typename NumTraits<T>::Real mat_real(const T&); 00139 template<> inline miINT8_t mat_real(const miINT8_t& v) { return v; } 00140 template<> inline miUINT8_t mat_real(const miUINT8_t& v) { return v; } 00141 template<> inline miINT16_t mat_real(const miINT16_t& v) { return v; } 00142 template<> inline miUINT16_t mat_real(const miUINT16_t& v) { return v; } 00143 template<> inline miINT32_t mat_real(const miINT32_t& v) { return v; } 00144 template<> inline miUINT32_t mat_real(const miUINT32_t& v) { return v; } 00145 template<> inline miSINGLE_t mat_real(const miSINGLE_t& v) { return v; } 00146 template<> inline miDOUBLE_t mat_real(const miDOUBLE_t& v) { return v; } 00147 template<> inline miINT8_t mat_real(const std::complex<miINT8_t>& v) { return std::real(v); } 00148 template<> inline miUINT8_t mat_real(const std::complex<miUINT8_t>& v) { return std::real(v); } 00149 template<> inline miINT16_t mat_real(const std::complex<miINT16_t>& v) { return std::real(v); } 00150 template<> inline miUINT16_t mat_real(const std::complex<miUINT16_t>& v) { return std::real(v); } 00151 template<> inline miINT32_t mat_real(const std::complex<miINT32_t>& v) { return std::real(v); } 00152 template<> inline miUINT32_t mat_real(const std::complex<miUINT32_t>& v) { return std::real(v); } 00153 template<> inline miSINGLE_t mat_real(const std::complex<miSINGLE_t>& v) { return std::real(v); } 00154 template<> inline miDOUBLE_t mat_real(const std::complex<miDOUBLE_t>& v) { return std::real(v); } 00155 00156 template<class T> inline typename NumTraits<T>::Real mat_imag(const T&); 00157 template<> inline miINT8_t mat_imag(const miINT8_t& v) { return 0; } 00158 template<> inline miUINT8_t mat_imag(const miUINT8_t& v) { return 0; } 00159 template<> inline miINT16_t mat_imag(const miINT16_t& v) { return 0; } 00160 template<> inline miUINT16_t mat_imag(const miUINT16_t& v) { return 0; } 00161 template<> inline miINT32_t mat_imag(const miINT32_t& v) { return 0; } 00162 template<> inline miUINT32_t mat_imag(const miUINT32_t& v) { return 0; } 00163 template<> inline miSINGLE_t mat_imag(const miSINGLE_t& v) { return 0; } 00164 template<> inline miDOUBLE_t mat_imag(const miDOUBLE_t& v) { return 0; } 00165 template<> inline miINT8_t mat_imag(const std::complex<miINT8_t>& v) { return std::imag(v); } 00166 template<> inline miUINT8_t mat_imag(const std::complex<miUINT8_t>& v) { return std::imag(v); } 00167 template<> inline miINT16_t mat_imag(const std::complex<miINT16_t>& v) { return std::imag(v); } 00168 template<> inline miUINT16_t mat_imag(const std::complex<miUINT16_t>& v) { return std::imag(v); } 00169 template<> inline miINT32_t mat_imag(const std::complex<miINT32_t>& v) { return std::imag(v); } 00170 template<> inline miUINT32_t mat_imag(const std::complex<miUINT32_t>& v) { return std::imag(v); } 00171 template<> inline miSINGLE_t mat_imag(const std::complex<miSINGLE_t>& v) { return std::imag(v); } 00172 template<> inline miDOUBLE_t mat_imag(const std::complex<miDOUBLE_t>& v) { return std::imag(v); } 00173 00174 #endif /* OPERATIONS_ON_TYPES_HPP_ */