IM: im_math_op.h Source File
From IM - An Imaging Tool
im_math_op.h
Go to the documentation of this file.00001 /** \file 00002 * \brief Math Operations 00003 * 00004 * See Copyright Notice in im_lib.h 00005 */ 00006 00007 #ifndef __IM_MATH_OP_H 00008 #define __IM_MATH_OP_H 00009 00010 #include "im_complex.h" 00011 00012 //#define IM_NEARZERO 0.0000001f 00013 //#define IM_NEARINF 10000000 00014 00015 /// Crop value to Byte limit 00016 template <class T> 00017 inline T crop_byte(const T& v) 00018 { 00019 return v <= 0? 0: v <= 255? v: 255; 00020 } 00021 00022 /// Generic Addition with 2 template types 00023 template <class T1, class T2> 00024 inline T1 add_op(const T1& v1, const T2& v2) 00025 { 00026 return v2 + v1; 00027 } 00028 00029 /// Generic Subtraction with 2 template types 00030 template <class T1, class T2> 00031 inline T1 sub_op(const T1& v1, const T2& v2) 00032 { 00033 return v2 - v1; 00034 } 00035 00036 /// Generic Multiplication with 2 template types 00037 template <class T1, class T2> 00038 inline T1 mul_op(const T1& v1, const T2& v2) 00039 { 00040 return v2 * v1; 00041 } 00042 00043 /// Generic Division with 2 template types 00044 template <class T1, class T2> 00045 inline T1 div_op(const T1& v1, const T2& v2) 00046 { 00047 // if (v2 == 0) return (T1)IM_NEARINF; 00048 return v1 / v2; 00049 } 00050 00051 /// Generic Invert 00052 template <class T> 00053 inline T inv_op(const T& v) 00054 { 00055 // if (v == 0) return (T)IM_NEARINF; 00056 return 1/v; 00057 } 00058 00059 /// Generic Difference with 2 template types 00060 template <class T1, class T2> 00061 inline T1 diff_op(const T1& v1, const T2& v2) 00062 { 00063 if (v1 <= v2) 00064 return v2 - v1; 00065 return v1 - v2; 00066 } 00067 00068 /// Generic Minimum with 2 template types 00069 template <class T1, class T2> 00070 inline T1 min_op(const T1& v1, const T2& v2) 00071 { 00072 if (v1 <= v2) 00073 return v1; 00074 return v2; 00075 } 00076 00077 /// Generic Maximum with 2 template types 00078 template <class T1, class T2> 00079 inline T1 max_op(const T1& v1, const T2& v2) 00080 { 00081 if (v1 <= v2) 00082 return v2; 00083 return v1; 00084 } 00085 00086 inline imbyte pow_op(const imbyte& v1, const imbyte& v2) 00087 { 00088 return (imbyte)pow((float)v1, v2); 00089 } 00090 00091 inline imushort pow_op(const imushort& v1, const imushort& v2) 00092 { 00093 return (imushort)pow((float)v1, v2); 00094 } 00095 00096 inline int pow_op(const int& v1, const int& v2) 00097 { 00098 return (int)pow((float)v1, v2); 00099 } 00100 00101 /// Generic Power with 2 template types 00102 template <class T1, class T2> 00103 inline T1 pow_op(const T1& v1, const T2& v2) 00104 { 00105 return (T1)pow(v1, v2); 00106 } 00107 00108 /// Generic Abssolute 00109 template <class T> 00110 inline T abs_op(const T& v) 00111 { 00112 if (v <= 0) 00113 return -1*v; 00114 return v; 00115 } 00116 00117 /// Generic Less 00118 template <class T> 00119 inline T less_op(const T& v) 00120 { 00121 return -1*v; 00122 } 00123 00124 /// Generic Square 00125 template <class T> 00126 inline T sqr_op(const T& v) 00127 { 00128 return v*v; 00129 } 00130 00131 inline int sqrt(const int& C) 00132 { 00133 return (int)sqrt(float(C)); 00134 } 00135 00136 /// Generic Square Root 00137 template <class T> 00138 inline T sqrt_op(const T& v) 00139 { 00140 return (T)sqrt(v); 00141 } 00142 00143 inline int exp(const int& v) 00144 { 00145 return (int)exp((float)v); 00146 } 00147 00148 /// Generic Exponential 00149 template <class T> 00150 inline T exp_op(const T& v) 00151 { 00152 return (T)exp(v); 00153 } 00154 00155 inline int log(const int& v) 00156 { 00157 return (int)log((float)v); 00158 } 00159 00160 /// Generic Logarithm 00161 template <class T> 00162 inline T log_op(const T& v) 00163 { 00164 // if (v <= 0) return (T)IM_NEARINF; 00165 return (T)log(v); 00166 } 00167 00168 // Dummy sin 00169 inline imcfloat sin(const imcfloat& v) 00170 { 00171 return (v); 00172 } 00173 00174 inline int sin(const int& v) 00175 { 00176 return (int)sin((float)v); 00177 } 00178 00179 /// Generic Sine 00180 template <class T> 00181 inline T sin_op(const T& v) 00182 { 00183 return (T)sin(v); 00184 } 00185 00186 inline int cos(const int& v) 00187 { 00188 return (int)cos((float)v); 00189 } 00190 00191 // Dummy cos 00192 inline imcfloat cos(const imcfloat& v) 00193 { 00194 return (v); 00195 } 00196 00197 /// Generic Cosine 00198 template <class T> 00199 inline T cos_op(const T& v) 00200 { 00201 return (T)cos(v); 00202 } 00203 00204 /// Sets a bit in an array 00205 inline void imDataBitSet(imbyte* data, int index, int bit) 00206 { 00207 if (bit) 00208 data[index / 8] |= (0x01 << (7 - (index % 8))); 00209 else 00210 data[index / 8] &= ~(0x01 << (7 - (index % 8))); 00211 } 00212 00213 /// Gets a bit from an array 00214 inline int imDataBitGet(imbyte* data, int index) 00215 { 00216 return (data[index / 8] >> (7 - (index % 8))) & 0x01; 00217 } 00218 00219 #endif