00001 #ifndef IMG_SCALAR_H_
00002 #define IMG_SCALAR_H_
00003
00009 #include "img/img_base.h"
00010
00011 namespace img {
00012
00020 template<typename ScalarType>
00021 inline ScalarType clampValue(ScalarType value, ScalarType minval=ScalarType(0.0), ScalarType maxval=ScalarType(255.0))
00022 {
00023 STATIC_FLOAT_OR_DOUBLE_TYPECHECK(ScalarType);
00024 if(value < minval) return minval;
00025 if(value > maxval) return maxval;
00026 return value;
00027 }
00028
00034 template<typename ScalarType>
00035 inline int valueAsInt(ScalarType value)
00036 {
00037 STATIC_FLOAT_OR_DOUBLE_TYPECHECK(ScalarType);
00038 return static_cast<int>(floor(static_cast<double>(value)+0.5f));
00039 }
00040
00048 template<typename ScalarType>
00049 inline ScalarType max3(ScalarType a, ScalarType b, ScalarType c)
00050 {
00051 STATIC_FLOAT_OR_DOUBLE_TYPECHECK(ScalarType);
00052 if ( b<=a && c<=a ) return a;
00053 if ( a<=b && c<=b ) return b;
00054 return c;
00055 }
00056
00064 template<typename ScalarType>
00065 inline ScalarType min3(ScalarType a, ScalarType b, ScalarType c)
00066 {
00067 STATIC_FLOAT_OR_DOUBLE_TYPECHECK(ScalarType);
00068 if ( a<=b && a<=c ) return a;
00069 if ( b<=a && b<=c ) return b;
00070 return c;
00071 }
00072
00080 template<typename ScalarType>
00081 inline ScalarType median3(ScalarType a, ScalarType b, ScalarType c)
00082 {
00083 STATIC_FLOAT_OR_DOUBLE_TYPECHECK(ScalarType);
00084 if ( (b<=a && a<=c) || (c<=a && a<=b) ) return a;
00085 if ( (a<=b && b<=c) || (c<=b && b<=a) ) return b;
00086 return c;
00087 }
00088
00096 template<typename ScalarType>
00097 inline bool almostEqual(ScalarType a, ScalarType b, ScalarType EPSILON=ScalarType(10e-5))
00098 {
00099 STATIC_FLOAT_OR_DOUBLE_TYPECHECK(ScalarType);
00100 ScalarType diff=a-b;
00101 if (diff<0)
00102 diff *= ScalarType(-1);
00103 return diff < EPSILON;
00104 }
00105
00106 }
00107
00108 #endif