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