00001 #ifndef Impala_Core_Stream_RgbDataSrcWindow_h
00002 #define Impala_Core_Stream_RgbDataSrcWindow_h
00003
00004 #include "Core/Stream/RgbDataSrc.h"
00005 #include "Core/Array/Arrays.h"
00006 #include "Core/Array/MakeFromData.h"
00007 #include "Core/Array/Set.h"
00008 #include "Core/Array/Rgb2Ooo.h"
00009 #include "Core/Array/GaussDerivative.h"
00010
00011 namespace Impala
00012 {
00013 namespace Core
00014 {
00015 namespace Stream
00016 {
00017
00018
00019
00020 class WindowPrepCopyToVec3Real64
00021 {
00022 public:
00023
00024 typedef Array::Array2dVec3Real64 ArrayType;
00025
00026 void
00027 DoIt(Array::Array2dVec3Real64*& dst, Array::Array2dVec3UInt8* src)
00028 {
00029 Set(dst, src);
00030 }
00031
00032 };
00033
00034
00035 class WindowPrepRgb2Ooo
00036 {
00037 public:
00038
00039 typedef Array::Array2dVec3Real64 ArrayType;
00040
00041 void
00042 DoIt(Array::Array2dVec3Real64*& dst, Array::Array2dVec3UInt8* src)
00043 {
00044 Rgb2Ooo(dst, src);
00045 }
00046
00047 };
00048
00049
00050 class WindowPrepGaussDerivative
00051 {
00052 public:
00053
00054 typedef Array::Array2dVec3Real64 ArrayType;
00055
00056 WindowPrepGaussDerivative(double sigma, int orderDerivx, int orderDerivy,
00057 double truncation)
00058 {
00059 mSigma = sigma;
00060 mOrderDerivx = orderDerivx;
00061 mOrderDerivy = orderDerivy;
00062 mTruncation = truncation;
00063 }
00064
00065 void
00066 DoIt(Array::Array2dVec3Real64*& dst, Array::Array2dVec3UInt8* src)
00067 {
00068 Array::Array2dVec3Real64* srcV = 0;
00069 Set(srcV, src);
00070 GaussDerivativeVec3(dst, srcV, mSigma, mOrderDerivx, mOrderDerivy,
00071 mTruncation);
00072 delete srcV;
00073 }
00074
00075 private:
00076
00077 double mSigma;
00078 int mOrderDerivx;
00079 int mOrderDerivy;
00080 double mTruncation;
00081
00082 };
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101 template<class PrepT>
00102 class RgbDataSrcWindow : public RgbDataSrc
00103 {
00104 public:
00105
00106 typedef typename PrepT::ArrayType ArrayT;
00107 typedef Array::Array2dVec3UInt8 Array2dVec3UInt8;
00108
00109 RgbDataSrcWindow(RgbDataSrc* src, int windowSize, PrepT* prep = 0,
00110 bool controlSrc = false, bool centre = false)
00111 : RgbDataSrc(src->GetType(), src->GetName())
00112 {
00113
00114 mFrameWidth = src->FrameWidth();
00115 mFrameHeight = src->FrameHeight();
00116 mLastFrame = src->LastFrame();
00117
00118
00119 mSrc = src;
00120 mWindowSize = windowSize;
00121 mControlSrc = controlSrc;
00122 mCentre = centre;
00123 ILOG_DEBUG("window size " << mWindowSize << ", centre " << mCentre);
00124 mPrep = prep;
00125 mWindowStart = -1;
00126 mRgbBufSize = src->FrameWidth() * src->FrameHeight() * 3;
00127 mWindow = new unsigned char*[windowSize];
00128 mWindowAr = new ArrayT*[windowSize];
00129 for (int i=0 ; i<windowSize ; i++)
00130 {
00131 mWindow[i] = new unsigned char[mRgbBufSize];
00132 mWindowAr[i] = 0;
00133 if (mPrep)
00134 mWindowAr[i] = Array::ArrayCreate<ArrayT>(src->FrameWidth(),
00135 src->FrameHeight());
00136 }
00137 }
00138
00139 virtual
00140 ~RgbDataSrcWindow()
00141 {
00142 for (int i=0 ; i<mWindowSize ; i++)
00143 {
00144 if (mWindow[i])
00145 delete mWindow[i];
00146 if (mWindowAr[i])
00147 delete mWindowAr[i];
00148 }
00149 delete [] mWindow;
00150 delete [] mWindowAr;
00151 if (mControlSrc)
00152 delete mSrc;
00153 }
00154
00155 int
00156 WindowSize()
00157 {
00158 return mWindowSize;
00159 }
00160
00161 int
00162 FrameWidth()
00163 {
00164 return mSrc->FrameWidth();
00165 }
00166
00167 int
00168 FrameHeight()
00169 {
00170 return mSrc->FrameHeight();
00171 }
00172
00173
00174 int
00175 FrameNr()
00176 {
00177 if (mCentre)
00178 return mWindowStart + mWindowSize/2;
00179 return mWindowStart + mWindowSize - 1;
00180 }
00181
00182 int
00183 LastFrame()
00184 {
00185 if (mCentre)
00186 return mSrc->LastFrame() - mWindowSize/2;
00187 return mSrc->LastFrame();
00188 }
00189
00190 RgbDataSrc*
00191 Src()
00192 {
00193 return mSrc;
00194 }
00195
00196
00197 unsigned char*
00198 DataPtr()
00199 {
00200 if (mCentre)
00201 return mWindow[mWindowSize/2];
00202 return mWindow[mWindowSize-1];
00203 }
00204
00205
00206 ArrayT*
00207 DataArray()
00208 {
00209 if (mCentre)
00210 return mWindowAr[mWindowSize/2];
00211 return mWindowAr[mWindowSize-1];
00212 }
00213
00214
00215 unsigned char*
00216 DataPtrWindow(int idx)
00217 {
00218 return mWindow[idx];
00219 }
00220
00221
00222 ArrayT*
00223 DataArrayWindow(int idx)
00224 {
00225 return mWindowAr[idx];
00226 }
00227
00228 virtual bool
00229 Valid() const
00230 {
00231 return (mSrc != 0) && mSrc->Valid();
00232 }
00233
00234 bool
00235 NextFrame(int inc = 1)
00236 {
00237 if ((inc == 1) && (mWindowStart != -1))
00238 return Advance();
00239 else
00240 return GotoFrame(FrameNr() + inc);
00241 }
00242
00243 bool
00244 GotoFrame(int frameNr)
00245 {
00246 ILOG_DEBUG(" window goto frame " << frameNr);
00247
00248 if (mSrc->TheEnd())
00249 return true;
00250
00251 if (mCentre)
00252 {
00253 if (frameNr < mWindowSize/2)
00254 frameNr = mWindowSize/2;
00255 if (frameNr >= mSrc->LastFrame() - mWindowSize/2)
00256 frameNr = mSrc->LastFrame() - mWindowSize/2;
00257 }
00258 else
00259 {
00260 if (frameNr < mWindowSize - 1)
00261 frameNr = mWindowSize - 1;
00262 if (frameNr >= mSrc->LastFrame() - 1)
00263 frameNr = mSrc->LastFrame() - 1;
00264 }
00265
00266 if (mCentre)
00267 return JumpStart(frameNr - mWindowSize/2);
00268 else
00269 return JumpStart(frameNr - (mWindowSize - 1));
00270 }
00271
00272 bool
00273 TheEnd()
00274 {
00275 return mSrc->TheEnd();
00276 }
00277
00278 bool
00279 FollowSrc()
00280 {
00281 return Advance();
00282 }
00283
00284 protected:
00285
00286
00287 bool
00288 ReadFrameData()
00289 {
00290 const int frameNr = RgbDataSrc::FrameNr();
00291 ILOG_DEBUG(" reading frame data for " << frameNr);
00292 bool rc = false;
00293 if (frameNr == RgbDataSrcWindow::FrameNr() + 1)
00294 rc = Advance();
00295 else
00296 rc = GotoFrame(frameNr);
00297 mData = RgbDataSrcWindow::DataPtr();
00298 return rc;
00299 }
00300
00301 private:
00302
00303 bool
00304 JumpStart(int start)
00305 {
00306 ILOG_DEBUG(" jumpstart " << start);
00307
00308 mWindowStart = start;
00309 if (start == -1)
00310 return true;
00311
00312 if (mSrc->FrameNr() != mWindowStart)
00313 {
00314 ILOG_DEBUG(" telling slave to goto " << mWindowStart);
00315 if (!mSrc->GotoFrame(mWindowStart))
00316 return false;
00317 ILOG_DEBUG(" slave went to " << mSrc->FrameNr());
00318 }
00319
00320 for (int i=0 ; i<mWindowSize ; i++)
00321 {
00322 memcpy(mWindow[i], mSrc->DataPtr(), mRgbBufSize);
00323 if (mPrep)
00324 {
00325 Array2dVec3UInt8* srcWrap = Array::ArrayCreate<Array2dVec3UInt8>
00326 (mSrc->FrameWidth(), mSrc->FrameHeight(), 0, 0,
00327 mSrc->DataPtr(), true);
00328 mPrep->DoIt(mWindowAr[i], srcWrap);
00329 delete srcWrap;
00330 }
00331 if (!mSrc->NextFrame())
00332 return false;
00333 }
00334 return true;
00335 }
00336
00337 bool
00338 Advance()
00339 {
00340 mWindowStart++;
00341 ILOG_DEBUG(" advance to window start " << mWindowStart);
00342 unsigned char* tmp = mWindow[0];
00343 ArrayT* tmpAr = mWindowAr[0];
00344 for (int i=0 ; i<mWindowSize-1 ; i++)
00345 {
00346 mWindow[i] = mWindow[i+1];
00347 mWindowAr[i] = mWindowAr[i+1];
00348 }
00349 mWindow[mWindowSize-1] = tmp;
00350 mWindowAr[mWindowSize-1] = tmpAr;
00351
00352 memcpy(mWindow[mWindowSize-1], mSrc->DataPtr(), mRgbBufSize);
00353 if (mPrep)
00354 {
00355 Array2dVec3UInt8* srcWrap = Array::ArrayCreate<Array2dVec3UInt8>
00356 (mSrc->FrameWidth(), mSrc->FrameHeight(), 0, 0, mSrc->DataPtr(),
00357 true);
00358 mPrep->DoIt(mWindowAr[mWindowSize-1], srcWrap);
00359 delete srcWrap;
00360 }
00361
00362 bool rc = true;
00363 if (mControlSrc)
00364 {
00365 ILOG_DEBUG(" advancing slave src");
00366 rc = mSrc->NextFrame();
00367 }
00368 return rc;
00369 }
00370
00371 RgbDataSrc* mSrc;
00372 bool mControlSrc;
00373 bool mCentre;
00374 int mRgbBufSize;
00375 int mWindowSize;
00376 int mWindowStart;
00377 unsigned char** mWindow;
00378 ArrayT** mWindowAr;
00379 PrepT* mPrep;
00380
00381 ILOG_VAR_DECL;
00382
00383 };
00384
00385 ILOG_VAR_INIT_TEMPL_1(RgbDataSrcWindow, PrepT, Impala.Core.Stream);
00386
00387 }
00388 }
00389 }
00390
00391 #endif