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.1 2005/04/02 22:07:00 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 /// Generic Power with 2 template types 00088 template <class T1, class T2> 00089 inline T1 pow_op(const T1& v1, const T2& v2) 00090 { 00091 return (T1)pow(v1, v2); 00092 } 00093 00094 /// Generic Abssolute 00095 template <class T> 00096 inline T abs_op(const T& v) 00097 { 00098 if (v <= 0) 00099 return -1*v; 00100 return v; 00101 } 00102 00103 /// Generic Less 00104 template <class T> 00105 inline T less_op(const T& v) 00106 { 00107 return -1*v; 00108 } 00109 00110 /// Generic Square 00111 template <class T> 00112 inline T sqr_op(const T& v) 00113 { 00114 return v*v; 00115 } 00116 00117 inline int sqrt(const int& C) 00118 { 00119 return (int)sqrt(float(C)); 00120 } 00121 00122 /// Generic Square Root 00123 template <class T> 00124 inline T sqrt_op(const T& v) 00125 { 00126 return (T)sqrt(v); 00127 } 00128 00129 inline int exp(const int& v) 00130 { 00131 return (int)exp((float)v); 00132 } 00133 00134 /// Generic Exponential 00135 template <class T> 00136 inline T exp_op(const T& v) 00137 { 00138 return (T)exp(v); 00139 } 00140 00141 inline int log(const int& v) 00142 { 00143 return (int)log((float)v); 00144 } 00145 00146 /// Generic Logarithm 00147 template <class T> 00148 inline T log_op(const T& v) 00149 { 00150 // if (v <= 0) return (T)IM_NEARINF; 00151 return (T)log(v); 00152 } 00153 00154 // Dummy sin 00155 inline imcfloat sin(const imcfloat& v) 00156 { 00157 return (v); 00158 } 00159 00160 inline int sin(const int& v) 00161 { 00162 return (int)sin((float)v); 00163 } 00164 00165 /// Generic Sine 00166 template <class T> 00167 inline T sin_op(const T& v) 00168 { 00169 return (T)sin(v); 00170 } 00171 00172 inline int cos(const int& v) 00173 { 00174 return (int)cos((float)v); 00175 } 00176 00177 // Dummy cos 00178 inline imcfloat cos(const imcfloat& v) 00179 { 00180 return (v); 00181 } 00182 00183 /// Generic Cosine 00184 template <class T> 00185 inline T cos_op(const T& v) 00186 { 00187 return (T)cos(v); 00188 } 00189 00190 /// Sets a bit in an array 00191 inline void imDataBitSet(imbyte* data, int index, int bit) 00192 { 00193 if (bit) 00194 data[index / 8] |= (0x01 << (7 - (index % 8))); 00195 else 00196 data[index / 8] &= ~(0x01 << (7 - (index % 8))); 00197 } 00198 00199 /// Gets a bit from an array 00200 inline int imDataBitGet(imbyte* data, int index) 00201 { 00202 return (data[index / 8] >> (7 - (index % 8))) & 0x01; 00203 } 00204 00205 #endif