00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef CVD_PIXEL_OPERATIONS_H_
00023 #define CVD_PIXEL_OPERATIONS_H_
00024
00025 #include <cmath>
00026 #include <cvd/internal/pixel_traits.h>
00027 #include <cvd/internal/convert_pixel_types.h>
00028 #include <cvd/internal/builtin_components.h>
00029 #include <cvd/internal/rgb_components.h>
00030
00031 namespace CVD {
00032
00033 namespace Pixel {
00034
00035
00036
00037 template <class T, unsigned int N = Component<T>::count> struct operations {
00038 inline static void assign(T & lhs, const T & rhs) { memcpy(&lhs, &rhs, sizeof(T)); }
00039 template <class S> inline static void assign(T & lhs, const S & rhs) { for (unsigned int i=0; i<N; i++) Component<T>::get(lhs,i) = (typename Component<T>::type)Component<S>::get(rhs,i); }
00040 template <class S> inline static void add(T & lhs, const S & rhs) { for (unsigned int i=0;i<N;++i) Component<T>::get(lhs,i) += Component<S>::get(rhs,i); }
00041 template <class S> inline static void subtract(T & lhs, const S & rhs) { for (unsigned int i=0;i<N;++i) Component<T>::get(lhs,i) -= Component<S>::get(rhs,i); }
00042 template <class S> inline static void multiply(T & lhs, const S& rhs) { for (unsigned int i=0;i<N;++i) Component<T>::get(lhs,i) = (typename Component<T>::type)(Component<T>::get(lhs,i)*rhs); }
00043 template <class S> inline static void divide(T & lhs, const S& rhs) { for (unsigned int i=0;i<N;++i) Component<T>::get(lhs,i) = (typename Component<T>::type)(Component<T>::get(lhs,i)/rhs); }
00044 inline static bool equal(const T & a, const T & b) { return memcmp(&a,&b,sizeof(T)) == 0; }
00045 inline static void zero(T & t) { memset(&t, 0, sizeof(T)); }
00046 };
00047
00048 template <class T> struct operations<T,1> {
00049 template <class S> inline static void assign(T& lhs, const S& rhs) { lhs = (T)rhs; }
00050 template <class S> inline static void add(T& lhs, const S& rhs) { lhs += rhs; }
00051 template <class S> inline static void subtract(T& lhs, const S& rhs) { lhs -= rhs; }
00052 template <class S> inline static void multiply(T& lhs, const S& rhs) { lhs = (T)(lhs*rhs); }
00053 template <class S> inline static void divide(T& lhs, const S& rhs) { lhs = (T)(lhs/rhs); }
00054 inline static bool equal(const T& a, const T& b) { return a == b; }
00055 inline static void zero(T& t) { t = T(); }
00056 };
00057
00058 template <class T, unsigned int N=Pixel::Component<T>::count>
00059 struct difference {
00060 typedef typename Pixel::Component<T>::type TComp;
00061 typedef typename Pixel::traits<TComp>::wider_type diff_type;
00062 static inline diff_type avgabs(const T& a, const T& b) {
00063 diff_type sum = diff_type();
00064 for (unsigned int i=0; i<N; i++)
00065 sum += std::abs((diff_type)Pixel::Component<T>::get(a,i) - Pixel::Component<T>::get(b,i));
00066 return sum/N;
00067 }
00068 static inline diff_type avg(const T& a, const T& b) {
00069 diff_type sum = diff_type();
00070 for (unsigned int i=0; i<N; i++)
00071 sum += (diff_type)Pixel::Component<T>::get(a,i) - Pixel::Component<T>::get(b,i);
00072 return sum/N;
00073 }
00074 };
00075
00076 template <class T>
00077 struct difference<T, 1> {
00078 typedef typename Pixel::Component<T>::type TComp;
00079 typedef typename Pixel::traits<TComp>::wider_type diff_type;
00080 static inline diff_type avgabs(const T& a, const T& b) {
00081 return CVD::abs((diff_type)Pixel::Component<T>::get(a,0) - Pixel::Component<T>::get(b,0));
00082 }
00083 static inline diff_type avg(const T& a, const T& b) {
00084 return (diff_type)Pixel::Component<T>::get(a,0) - (diff_type)Pixel::Component<T>::get(b,0);
00085 }
00086 };
00087
00088 };
00089
00090 };
00091
00092 #endif