Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

Sketch.cc

Go to the documentation of this file.
00001 //-*-c++-*-
00002 
00003 #include "Sketch.h"
00004 
00005 namespace DualCoding {
00006 
00007 // These functions must go here instead of in Sketch.h because
00008 // they are not templated.
00009 
00010 #define DEF_MATHOPS_CC(_T1, _T2, _Result) \
00011   DEF_MATHOP_CC( +, _T1, _T2, _Result ) \
00012   DEF_MATHOP_CC( -, _T1, _T2, _Result ) \
00013   DEF_MATHOP_CC( *, _T1, _T2, _Result ) \
00014   DEF_MATHOP_CC( /, _T1, _T2, _Result )
00015 
00016 #define DEF_MATHOP_CC(_Op, _T1, _T2, _Result) \
00017 Sketch<_Result> operator _Op (const Sketch<_T1> &lhs, const Sketch<_T2> &rhs) { \
00018   Sketch<_Result> result(lhs->getName() + #_Op + rhs->getName(), lhs); \
00019   _Result* dest = &(*result.pixels)[0]; \
00020   const _T1* src1 = &(*lhs.pixels)[0]; \
00021   const _T1* end1 = &(*lhs.pixels)[lhs->getNumPixels()]; \
00022   const _T2* src2 = &(*rhs.pixels)[0]; \
00023   while ( src1 != end1 ) \
00024     *dest++ = *src1++ _Op *src2++; \
00025   return result; \
00026 } \
00027 /* continued... */ \
00028 Sketch<_Result> operator _Op (const Sketch<_T1> &lhs, const _T2 value) { \
00029   Sketch<_Result> result(lhs->getName() + #_Op + "scalar", lhs); \
00030   _Result* dest = &(*result.pixels)[0]; \
00031   const _T1* src1 = &(*lhs.pixels)[0]; \
00032   const _T1* end1 = &(*lhs.pixels)[lhs->getNumPixels()]; \
00033   while ( src1 != end1 ) \
00034     *dest++ = *src1++ _Op (_Result)value; \
00035   return result; \
00036 }
00037 
00038 // DEF_MATHOPS(bool, bool, bool) disallowed because valarray<bool> doesn't provide arithmetic
00039 DEF_MATHOPS_CC(bool, uchar, uchar)
00040 DEF_MATHOPS_CC(bool, uint, uint)
00041 DEF_MATHOPS_CC(bool, float, float)
00042 
00043 DEF_MATHOPS_CC(uchar, bool, uchar)
00044 DEF_MATHOPS_CC(uchar, uchar, uchar)
00045 DEF_MATHOPS_CC(uchar, uint, uint)
00046 DEF_MATHOPS_CC(uchar, float, float)
00047 
00048 DEF_MATHOPS_CC(uint, bool, uint)
00049 DEF_MATHOPS_CC(uint, uchar, uint)
00050 DEF_MATHOPS_CC(uint, uint, uint)
00051 DEF_MATHOPS_CC(uint, float, float)
00052 
00053 DEF_MATHOPS_CC(float, bool, float)
00054 DEF_MATHOPS_CC(float, uchar, float)
00055 DEF_MATHOPS_CC(float, uint, float)
00056 DEF_MATHOPS_CC(float, float, float)
00057 
00058 #undef DEF_MATHOPS_CC
00059 #undef DEF_MATHOP_CC
00060 
00061 #define DEF_MATHOPS_INT_CC(_T1) \
00062   DEF_MATHOP_INT_CC( +, _T1) \
00063   DEF_MATHOP_INT_CC( -, _T1) \
00064   DEF_MATHOP_INT_CC( *, _T1) \
00065   DEF_MATHOP_INT_CC( /, _T1)
00066 
00067 #define DEF_MATHOP_INT_CC(_Op, _T1) \
00068 Sketch<_T1> operator _Op (const Sketch<_T1>& lhs, const int value) { \
00069   Sketch<_T1> result(lhs->getName() + #_Op + "scalar", lhs); \
00070   *result.pixels = *lhs.pixels _Op (_T1)(value); \
00071   return result; \
00072 }
00073 
00074 //DEF_MATHOPS_INT(bool, uchar) disallowed because valarray won't mix types
00075 DEF_MATHOPS_INT_CC(uchar)
00076 DEF_MATHOPS_INT_CC(uint)
00077 DEF_MATHOPS_INT_CC(float)
00078 
00079 #undef DEF_MATHOPS_INT_CC
00080 #undef DEF_MATHOP_INT_CC
00081 
00082 // Define a special version of int math ops on Sketch<bool> that converts to Sketch<uchar>
00083 
00084 #define DEF_MATHBOOL_INT_CC(_Op) \
00085 Sketch<uchar> operator _Op (const Sketch<bool>& lhs, const int value) { \
00086   Sketch<uchar> result(lhs->getName() + #_Op + "scalar", lhs); \
00087   uchar* dest = &(*result.pixels)[0]; \
00088   const bool* src1 = &(*lhs.pixels)[0]; \
00089   const bool* end1 = &(*lhs.pixels)[lhs->getNumPixels()]; \
00090   while ( src1 != end1 ) \
00091     *dest++ = *src1++ _Op (uchar)value; \
00092   return result; \
00093 }
00094 
00095 DEF_MATHBOOL_INT_CC( + )
00096 DEF_MATHBOOL_INT_CC( - )
00097 DEF_MATHBOOL_INT_CC( * )
00098 DEF_MATHBOOL_INT_CC( / )
00099 
00100 #undef DEF_MATHBOOL_INT_CC
00101 
00102 template<>
00103 Sketch<bool>::operator Sketch<uchar>() const {
00104   /*
00105     std::cout << "Converting " << this << " '" << getName() << "'"
00106     << " id=" << getId() << ",parent=" << getParentId() << ",refcount=" << data->refcount
00107     << " to Sketch<uchar>\n";
00108   */
00109   Sketch<uchar> converted("uchar("+(*this)->getName()+")", *this);
00110   copyPixels(converted, *this);
00111   return converted;
00112 }
00113 
00114 template<>
00115 Sketch<uchar>::operator Sketch<bool>() const {
00116   /*
00117     std::cout << "Converting " << this << " '" << getName() << "'"
00118     << " id=" << getId() << ",parent=" << getParentId() << ",refcount=" << data->refcount
00119     << " to Sketch<bool>\n";
00120   */
00121   Sketch<bool> converted("bool(" + (*this)->getName() + ")", *this);
00122   copyPixels(converted, *this);
00123   return converted;
00124 }
00125 
00126 Sketch<bool>& operator&= (Sketch<bool>& arg1, Sketch<bool> const& arg2) {
00127   *(arg1.pixels) &= *(arg2.pixels); 
00128   return arg1;
00129 }
00130 
00131 Sketch<bool>& operator|= (Sketch<bool>& arg1, Sketch<bool> const& arg2) {
00132   *(arg1.pixels) |= *(arg2.pixels); 
00133   return arg1;
00134 }
00135 
00136 Sketch<bool>& operator^= (Sketch<bool>& arg1, Sketch<bool> const& arg2) {
00137   *(arg1.pixels) ^= *(arg2.pixels); 
00138   return arg1;
00139 }
00140 
00141 } // namespace

DualCoding 4.0
Generated Thu Nov 22 00:52:36 2007 by Doxygen 1.5.4