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

AppControlSrc.h

Go to the documentation of this file.
00001 #ifndef Impala_Visualization_AppControlSrc_h
00002 #define Impala_Visualization_AppControlSrc_h
00003 
00004 #include "Persistency/ImageRepository.h"
00005 #include "Basis/Timer.h"
00006 #include "Basis/CmdOptions.h"
00007 #include "Util/Database.h"
00008 #include "Basis/FileName.h"
00009 #include "Core/Array/WritePng.h"
00010 #include "Core/Stream/RgbDataSrc.h"
00011 #include "Visualization/AppControl.h"
00012 
00013 namespace Impala
00014 {
00015 namespace Visualization
00016 {
00017 
00018 
00019 class AppControlSrc : public AppControl
00020 {
00021 public:
00022     typedef Core::Stream::RgbDataSrc RgbDataSrc;
00023 
00024     AppControlSrc(int id) : AppControl(id, false), mTimer(1)
00025     {
00026         CmdOptions& options = CmdOptions::GetInstance();
00027         mSrc = 0;
00028         mSrc2 = 0;
00029         mDone = false;
00030         mHadJump = false;
00031         mStartFrame = options.GetInt("startFrame");
00032         mMarkedFrameBegin = -1;
00033         mMarkedFrameEnd = -1;
00034         mDoContinuous = ! options.GetBool("step");
00035         mStepSize = options.GetInt("stepSize");
00036         mDoLoop = options.GetBool("loop");
00037         mDoOneStep = true;
00038         mFramesDone = 0;
00039         mDelay = options.GetInt("delay");
00040         mDidSrcError = false;
00041     }
00042 
00043     virtual void
00044     HandleActivate()
00045     {
00046         HandleNewFrame();
00047         HandleCycleSrc();
00048     }
00049 
00050     void
00051     SetSrc(RgbDataSrc* src, int startFrame = -1, // -1 indicates default
00052            RgbDataSrc* src2 = 0)
00053     {
00054         mSrc = src;
00055         mSrc2 = src2;
00056         //mDidSrcError = false;
00057         if (!mSrc)
00058             return;
00059         //mTimer.Start();
00060         FpsReset();
00061         if (startFrame == -1)
00062             startFrame = mStartFrame;
00063         if ((startFrame != 0) || (mDelay != 0))
00064         {
00065             mSrc->GotoFrame(startFrame);
00066             if (mSrc2)
00067                 mSrc2->GotoFrame(startFrame + mDelay);
00068         }
00069         else
00070         {
00071             if (mSrc->FrameNr() == -1) // -1 indicates not started yet
00072                 mSrc->NextFrame();
00073             if (mSrc2 && (mSrc2->FrameNr() == -1))
00074                 mSrc2->NextFrame();
00075         }
00076         CheckJumpFrame();
00077     }
00078 
00079     RgbDataSrc*
00080     GetSrc()
00081     {
00082         return mSrc;
00083     }
00084 
00085     RgbDataSrc*
00086     GetSrc2()
00087     {
00088         return mSrc2;
00089     }
00090 
00091     void
00092     Jump(int frameNr)
00093     {
00094         if (!mSrc || frameNr < 0)
00095             return;
00096         if (mSrc2)
00097             frameNr -= mDelay; // hack : mSrc2 is reference point
00098         mSrc->GotoFrame(frameNr);
00099         if (mSrc2)
00100             mSrc2->GotoFrame(frameNr + mDelay);
00101         CheckJumpFrame();
00102     }
00103 
00104     void
00105     SetDoLoop(bool flag)
00106     {
00107         mDoLoop = flag;
00108     }
00109 
00110     void
00111     SetDoContinuous(bool flag)
00112     {
00113         mDoContinuous = flag;
00114     }
00115 
00116     bool GetDoContinuous()
00117     {
00118         return mDoContinuous;
00119     }
00120 
00121     int
00122     GetMarkedFrameBegin()
00123     {
00124         return mMarkedFrameBegin;
00125     }
00126 
00127     void
00128     SetMarkedFrameBegin(int frameNr)
00129     {
00130         mMarkedFrameBegin = frameNr;
00131     }
00132 
00133     int
00134     GetMarkedFrameEnd()
00135     {
00136         return mMarkedFrameEnd;
00137     }
00138 
00139     void
00140     SetMarkedFrameEnd(int frameNr)
00141     {
00142         mMarkedFrameEnd = frameNr;
00143     }
00144 
00145     void
00146     FpsReset()
00147     {
00148         mTimer.Start();
00149         mFramesDone = 0;
00150     }
00151 
00152     std::string
00153     GetFpsString()
00154     {
00155         if (!mSrc)
00156             return std::string("no src");
00157         double timeVal = mTimer.SplitTime();
00158         double fps = (double) mFramesDone / timeVal;
00159         std::ostringstream fpsStream;
00160         fpsStream << "FrameNr = " << mSrc->FrameNr()
00161                   << " (" << mSrc->FrameType() << ")";
00162         if (mSrc2)
00163             fpsStream << ", FrameNr2 = " << mSrc2->FrameNr()
00164                       << " (" << mSrc->FrameType() << ")";
00165         fpsStream << ", did " << mFramesDone << " frames in " << timeVal
00166                   << " sec = " << fps << " fps" << std::ends;
00167         return std::string(fpsStream.str());
00168     }
00169 
00170     // specialization of base class
00171 
00172     virtual void
00173     ComputeCycle(double elapsedTime)
00174     {
00175         if (! mSrc)
00176         {
00177             if (! mDidSrcError)
00178             {
00179                 ILOG_ERROR("AppControlSrc::ComputeCycle : no src");
00180                 mDidSrcError = true;
00181             }
00182             return;
00183         }
00184         if (mDoContinuous || mDoOneStep)
00185         {
00186             if (! mHadJump)  // already moved to current frame
00187             {
00188                 mSrc->NextFrame(mStepSize);
00189                 if (mSrc2)
00190                     mSrc2->NextFrame(mStepSize);
00191             }
00192             mHadJump = false;
00193             HandleNewFrame();
00194             mFramesDone++;
00195             mDoOneStep = false;
00196         }
00197         HandleCycleSrc();
00198     }
00199 
00200     virtual void
00201     KeyEvent(OglGui::Window* src, int c, int state)
00202     {
00203         if (!GetActive())
00204             return;
00205 
00206         if (c == 'x')
00207         {
00208             ExportFrame();
00209         }
00210 
00211         if (c == 's')
00212         {
00213             mDoOneStep = true;
00214             mStepSize = abs(mStepSize);
00215         }
00216         if (c == 'S')
00217         {
00218             mDoOneStep = true;
00219             mStepSize = - abs(mStepSize);
00220         }
00221         if (c == 'a')
00222             HandleDelayUpdate(1);
00223         if (c == 'A')
00224             HandleDelayUpdate(-1);
00225         if (c == 'c')
00226         {
00227             mDoContinuous = true;
00228             FpsReset();
00229         }
00230         if (c == 'C')
00231             mDoContinuous = false;
00232         if (c == 'd')
00233             Jump(mMarkedFrameBegin);
00234         if (c == 'D')
00235             mMarkedFrameBegin = mSrc->FrameNr();
00236         if (c == 'e')
00237             Jump(mMarkedFrameEnd);
00238         if (c == 'E')
00239             mMarkedFrameEnd = mSrc->FrameNr();
00240         if (c == 'Q')
00241         {
00242             ILOG_INFO("Instant exit button pressed.");
00243             mDone = true;
00244             HandleDestroyEvent();
00245         }
00246         if (c == ' ')
00247             mDoContinuous=!mDoContinuous ;
00248         if (c == '<')
00249             Jump(mSrc->FrameNr()-1000);
00250         if (c == '>')
00251             Jump(mSrc->FrameNr()+1000);
00252         if (c == '[')
00253             Jump(mSrc->FrameNr()-100);
00254         if (c == ']')
00255             Jump(mSrc->FrameNr()+100);
00256         if (c == '}' )
00257             Jump(mSrc->FrameNr()+10);
00258         if (c == '{' )
00259             Jump(mSrc->FrameNr()-10);
00260     }
00261 
00262     virtual bool
00263     Done()
00264     {
00265         //if (!mSrc || mDone)
00266         if(mDone)
00267             HandleDestroyEvent();
00268 
00269         if (mDone)
00270             return true;
00271         if (!mSrc)
00272             return false;
00273         if (mSrc->TheEnd())
00274         {
00275             if (mDoLoop)
00276             {
00277                 mSrc->Reset();
00278                 FpsReset();
00279                 CheckJumpFrame();
00280             }
00281             else
00282             {
00283                 mDone = true;
00284             }
00285         }
00286         if (mDone)
00287             HandleDestroyEvent();
00288         return mDone;
00289     }
00290 
00291     // interface to derived classes
00292 
00293     virtual void
00294     HandleNewFrame() = 0;
00295     
00296     virtual void
00297     HandleDestroyEvent()
00298     {
00299     }
00300 
00301     virtual void
00302     HandleCycleSrc()
00303     {
00304     }
00305 
00306 private:
00307 
00308     void
00309     CheckJumpFrame()
00310     {
00311         int nStartRetry = 3;
00312         while ((nStartRetry > 0) && (mSrc->DataPtr() == 0))
00313         {
00314             ILOG_WARN("Skipping empty frame");
00315             nStartRetry--;
00316             mSrc->NextFrame();
00317             if (mSrc2)
00318                 mSrc2->NextFrame();
00319         }
00320         mHadJump = true;
00321         mDoOneStep = true; // to make sure the frame is processed
00322     }
00323 
00324     void
00325     HandleDelayUpdate(int inc)
00326     {
00327         if (!mSrc2)
00328             return;
00329         mDelay += inc;
00330         mSrc2->NextFrame(inc);
00331         mHadJump = true;
00332         mDoOneStep = true; // to make sure the frame is processed
00333     }
00334 
00335     bool
00336     ExportFrame()
00337     {
00338         const int frameNr = mSrc->FrameNr();
00339         if (!mSrc || frameNr < 0)
00340         {
00341             ILOG_ERROR("No valid source or no current frame set (" << mSrc <<
00342                 ", " << frameNr << ")");
00343             return false;
00344         }
00345 
00346         Core::Array::Array2dVec3UInt8* im = 
00347             Core::Array::ArrayCreate<Core::Array::Array2dVec3UInt8>
00348             (mSrc->FrameWidth(), mSrc->FrameHeight(), 0, 0, mSrc->DataPtr(), true);
00349         if (!im)
00350         {
00351             ILOG_ERROR("Couldn't load image for frame nr " << frameNr);
00352             return false;
00353         }
00354 
00355         String fileName = FileNameTail(mSrc->GetName());
00356         fileName += "_frame" + MakeString(frameNr) + ".png";
00357 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00358         Core::Array::WritePng(im, fileName, new Util::Database());
00359 #else // REPOSITORY_USED
00360         Persistency::ImageLocator loc("file:", ".", "", fileName);
00361         Persistency::ImageRepository().Add(loc, im);
00362 #endif // REPOSITORY_USED
00363         delete im;
00364         return true;
00365     }
00366 
00367     RgbDataSrc* mSrc;
00368     bool        mDone;
00369     bool        mHadJump;
00370     int         mStartFrame;
00371     int         mMarkedFrameBegin;
00372     int         mMarkedFrameEnd;
00373     bool        mDoContinuous;
00374     bool        mDoOneStep;
00375     int         mStepSize;
00376     bool        mDoLoop;
00377     int         mFramesDone;
00378     Timer       mTimer;
00379     bool        mDidSrcError;
00380 
00381     RgbDataSrc* mSrc2;
00382     int         mDelay;
00383 
00384     ILOG_VAR_DEC;
00385 };
00386 
00387 ILOG_VAR_INIT(AppControlSrc, Visualization);
00388 
00389 
00390 } // namespace Visualization
00391 } // namespace Impala
00392 
00393 #endif

Generated on Fri Mar 19 09:31:48 2010 for ImpalaSrc by  doxygen 1.5.1