Home || Architecture || Video Search || Visual Search || Scripts || Applications || Important Messages || OGL || Src

Array2dTem.h

Go to the documentation of this file.
00001 #ifndef Impala_Core_Array_Array2dTem_h
00002 #define Impala_Core_Array_Array2dTem_h
00003 
00004 #include <sstream>
00005 #include "Core/Array/Pattern/PtrFunc.h"
00006 #include "Core/Array/Element/VecTem.h"
00007 #include "Core/Array/Element/TypeString.h"
00008 #include "Core/Array/ArraySystem.h"
00009 #include "Basis/Std.h"
00010 #include "Basis/String.h"
00011 
00012 #ifdef PX_HORUS_USED
00013 #include "Core/Array/Pattern/PxStateTrans.h"
00014 #else
00015 #define SEQ_CREAT   0
00016 #define SEQ_VALID   0
00017 #endif
00018 
00019 namespace Impala
00020 {
00021 namespace Core
00022 {
00023 namespace Array
00024 {
00025 
00026 
00027 template <class StorT, int elemSize, class ArithT>
00028 class Array2dTem
00029 {
00030 public:
00031 
00032     typedef StorT      StorType;
00033     static const int   ElemSize() { return elemSize; }
00034     typedef ArithT     ArithType;
00035 
00036     typedef Element::VecTem<StorT,elemSize> VecType;
00037 
00038     // if data != 0 it should contain w * h * ElemSize elements
00039     // note that the parameters specify core size, not total array size
00040     Array2dTem(Int64 cw, Int64 ch, Int64 bw, Int64 bh, StorType* data = 0,
00041                bool isWrapper = false, bool isPartial = false)
00042     {
00043         mCW = cw;
00044         mCH = ch;
00045         mBW = bw;
00046         mBH = bh;
00047         Int64 w = cw + 2*bw;
00048         Int64 h = ch + 2*bh;
00049         mDataSize = w * h * elemSize;
00050         if (data == 0) 
00051         {
00052             mData = ArraySystem::Instance().Allocate(mDataSize, StorType());
00053 //          mState = SEQ_CREAT;                 // PxHorus extension
00054             mState = SEQ_VALID;                 // PxHorus extension
00055         }
00056         else
00057         {
00058             mData = data;
00059             mState = SEQ_VALID;                 // PxHorus extension
00060             if (! isWrapper)
00061                 ArraySystem::Instance().External(mDataSize, mData);
00062         }
00063         mIsWrapper = isWrapper;
00064         mIsPartial = isPartial;                 // PxHorus extension
00065         mPartArray = 0;                         // PxHorus extension
00066     }
00067 
00068     Array2dTem()
00069     {
00070         mCW = 0;
00071         mCH = 0;
00072         mBW = 0;
00073         mBH = 0;
00074         mDataSize = 0;
00075         mData = 0;
00076         mIsWrapper = false;
00077         mState = SEQ_CREAT;                     // PxHorus extension
00078         mIsPartial = false;                     // PxHorus extension
00079         mPartArray = 0;                         // PxHorus extension
00080     }
00081 
00082     ~Array2dTem()
00083     {
00084         if (mData && !mIsWrapper) 
00085         {
00086             ArraySystem::Instance().Deallocate(mData, mDataSize);
00087             mData = 0;
00088         }
00089         if (mPartArray)                         // PxHorus extension
00090         {
00091             delete mPartArray;
00092             mPartArray = 0;
00093         }
00094     }
00095 
00096     virtual String 
00097     GetTypeName()
00098     {
00099         std::ostringstream s;
00100         s << "Array(" << Element::TypeString<StorT>(0) << "," << elemSize << ")";
00101         return s.str();
00102     }
00103 
00104     Int64
00105     W() const
00106     {
00107         return mCW + 2*mBW;
00108     }
00109 
00110     Int64
00111     BW() const
00112     {
00113         return mBW;
00114     }
00115 
00116     Int64
00117     CW() const
00118     {
00119         return mCW;
00120     }
00121 
00122     Int64
00123     H() const
00124     {
00125         return mCH + 2*mBH;
00126     }
00127 
00128     Int64
00129     BH() const
00130     {
00131         return mBH;
00132     }
00133 
00134     Int64
00135     CH() const
00136     {
00137         return mCH;
00138     }
00139 
00140     // const pointer access
00141     const StorType*
00142     PB(Int64 x=0, Int64 y=0) const
00143     {
00144         return mData + Inc(x, y);
00145     }
00146 
00147     const StorType*
00148     CPB(Int64 x=0, Int64 y=0) const
00149     {
00150         return mData + Inc(BW() + x, BH() + y);
00151     }
00152 
00153     const StorType*
00154     CPE(Int64 x=0, Int64 y=0) const
00155     {
00156         return mData + Inc(BW() + CW() - 1 + x, BH() + CH() - 1 + y);
00157     }
00158 
00159     // non const pointer access
00160     StorType*
00161     PB(Int64 x=0, Int64 y=0)
00162     {
00163         return mData + Inc(x, y);
00164     }
00165 
00166     StorType*
00167     CPB(Int64 x=0, Int64 y=0)
00168     {
00169         return mData + Inc(BW() + x, BH() + y);
00170     }
00171 
00172     StorType*
00173     CPE(Int64 x=0, Int64 y=0)
00174     {
00175         return mData + Inc(BW() + CW() - 1 + x, BH() + CH() - 1 + y);
00176     }
00177 
00178     // value access, scalar only!
00179     StorType
00180     Val(Int64 x, Int64 y) const
00181     {
00182         return *CPB(x, y);
00183     }
00184 
00185     // proper value access
00186     ArithType
00187     Value(Int64 x, Int64 y)
00188     {
00189         return Pattern::PtrRead(CPB(x, y), ArithType());
00190     }
00191 
00192     void
00193     SetValue(ArithType value, Int64 x, Int64 y)
00194     {
00195         //Pattern::PtrWrite(static_cast<ArithType*>(CPB(x, y)), value);
00196         Pattern::PtrWrite(CPB(x, y), value);
00197     }
00198 
00199     // Returns a vector "around" the element at (x,y).
00200     // The vector is a wrapper so it behaves as a reference to the element!
00201     VecType
00202     Elem(Int64 x, Int64 y)
00203     {
00204         return VecType(CPB(x, y), true);
00205     }
00206 
00207     void
00208     WriteTo(std::ostream& os)
00209     {
00210         os << Element::TypeString<StorT>(0) << " " << mDataSize << "\n"
00211            << mCW << " " << mCH << " "
00212            << mBW << " " << mBH << "\n";
00213         for(Int64 i=0 ; i<mDataSize ; i++)
00214             os << mData[i] << " ";
00215         os << "\n";
00216     }
00217 
00218     void
00219     WriteTo(std::ostream& os, Int64 w, Int64 h)
00220     {
00221         WriteTo(os,0,0,w,h);
00222     }
00223     
00224     void
00225     WriteTo(std::ostream& os, Int64 startx, Int64 starty, Int64 w, Int64 h)
00226     {
00227         if(w == -1 && startx == 0)
00228             w = CW();
00229         if(h == -1 && starty == 0)
00230             h = CH();
00231         if(startx+w > W()-BW())
00232             w = W()-startx-BW();
00233         if(starty+h > H()-BH())
00234             h = H()-starty-BH();
00235         os << "partial array dump from ("
00236            << startx << "," << starty << ") size ("
00237            << w << "," << h << ")\n";
00238         Int64 x,y;
00239         for(y=0 ; y<h ; ++y)
00240         {
00241             for(x=0 ; x<w ; ++x)
00242                 os << Pattern::PtrRead(CPB(x+startx, y+starty), ArithType()) << " ";
00243             os << "\n";
00244         }
00245     }
00246 
00247     void FindNaN(CString name)
00248     {
00249         Int64 count=0;
00250         Int64 y;
00251         for(y=0 ; y<H() ; ++y)
00252         {
00253             Int64 x;
00254             for(x=0 ; x<W() ; ++x)
00255             {
00256                 Int64 i;
00257                 for(i=0 ; i<ElemSize() ; ++i)
00258                 {
00259                     StorType e = mData[i + ElemSize()*(x + W()*y)];
00260                     if(e != e) 
00261                     {
00262                         if(count==0)
00263                             std::cout << name << " contains nan! first @ (" << x << "," << y
00264                                       << ") elem" << i << "((debug:" << i + ElemSize()*(x + W()*y) << "))";
00265                         count++;
00266                     }
00267                 } 
00268             }
00269         }
00270         if(count > 0)
00271             std::cout << ", " << count << " of " << W()*H()*ElemSize() << " in total" << std::endl;
00272     }
00277     void GrowBorder(Int64 amount)
00278     {
00279         if(amount < 0)
00280         {
00281             if(-amount > mBH || -amount > mBW)
00282             {
00283                 std::cerr 
00284                     << "WARNING: [Array2dTem::GrowBorder] negative amount > border" 
00285                     << std::endl;
00286                 return;
00287             }
00288             if(2*amount > mCH || 2*amount > mCW)
00289             {
00290                 std::cerr 
00291                     << "WARNING: [Array2dTem::GrowBorder] amount > half of image " 
00292                     << std::endl;
00293                 return;
00294             }
00295         }
00296 
00297         mBW += amount;
00298         mBH += amount;
00299         mCW -= 2*amount;
00300         mCH -= 2*amount;
00301     }
00302     
00303 
00304     Int64         mCW;
00305     Int64         mCH;
00306     Int64         mBW;
00307     Int64         mBH;
00308     StorType*   mData;
00309     size_t      mDataSize;
00310     bool        mIsWrapper;
00311     int         mState;                     // PxHorus extension
00312     bool        mIsPartial;                 // PxHorus extension
00313     Array2dTem* mPartArray;                 // PxHorus extension
00314 
00315 private:
00316 
00317     Int64
00318     Inc(Int64 x, Int64 y) const
00319     {
00320         return y * W() * ElemSize() + x * ElemSize();
00321     }
00322 
00323 };
00324 
00325 // we want bytes to be printed as numbers, not chars
00326 inline std::ostream&
00327 operator<< (std::ostream& os, UInt8 v)
00328 {
00329     return os << int(v);
00330 }
00331 
00332 template<class ArrayT>
00333 inline std::ostream&
00334 ArrayPrintElem(std::ostream& os, ArrayT* array, typename ArrayT::StorType* ptr)
00335 {
00336     for (int i=0 ; i<array->ElemSize() ; i++)
00337     {
00338         if ((i == 0) && (array->ElemSize() > 1))
00339             os << "(";
00340         os << *ptr++;
00341         if (i+1 < array->ElemSize())
00342             os << ",";
00343         if ((i+1 == array->ElemSize()) && (array->ElemSize() > 1))
00344             os << ")";
00345     }
00346     return os;
00347 }
00348 
00349 template<class ArrayT>
00350 ArrayT*
00351 ArrayCreate(Int64 cw, Int64 ch, Int64 bw = 0, Int64 bh = 0,
00352             typename ArrayT::StorType* data = 0, bool isWrapper = false)
00353 {
00354     return new ArrayT(cw, ch, bw, bh, data, isWrapper);
00355 }
00356 
00357 template<class DstArrayT, class SrcArrayT>
00358 DstArrayT*
00359 ArrayClone(SrcArrayT* src, Int64 minimalBW = 0, Int64 minimalBH = 0)
00360 {
00361     Int64 bw = Impala::Max(src->BW(), minimalBW);
00362     Int64 bh = Impala::Max(src->BH(), minimalBH);
00363     return new DstArrayT(src->CW(), src->CH(), bw, bh);
00364 }
00365 
00366 } // namespace Array
00367 } // namespace Core
00368 } // namespace Impala
00369 
00370 
00371 #endif

Generated on Fri Mar 19 09:30:43 2010 for ImpalaSrc by  doxygen 1.5.1