00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef Impala_Visualization_VideoJpgPlayer_h
00015 #define Impala_Visualization_VideoJpgPlayer_h
00016
00017 #include <time.h>
00018
00019 #ifndef Impala_Core_Array_Arrays_h
00020 #include "Core/Array/Arrays.h"
00021 #endif
00022
00023 #ifndef Impala_Core_Array_ReadJpg_h
00024 #include "Core/Array/ReadJpg.h"
00025 #endif
00026
00027 #ifndef Impala_Visualisation_RgbOglImage_h
00028 #include "Visualization/RgbOglImage.h"
00029 #endif
00030
00031 #ifndef OglGui_Window_h
00032 #include "OglGui/Window.h"
00033 #endif
00034
00035 #ifndef Impala_Visualisation_VideoJpgPlayerListener_h
00036 #include "Visualization/VideoJpgPlayerListener.h"
00037 #endif
00038
00039 #include "Visualization/Video.h"
00040
00041
00042 namespace Impala
00043 {
00044 namespace Visualization
00045 {
00046
00047 class VideoJpgPlayer : public OglGui::Window
00048 {
00049 public:
00050
00051 typedef Impala::Core::Array::Array2dVec3UInt8 Array2dVec3UInt8;
00052 typedef OglGui::Window Window;
00053
00054 VideoJpgPlayer(int x, int y, int w, int h, Video& video):
00055 Window(x, y, w, h), mVideo(&video)
00056 {
00057 Init(w,h);
00058 }
00059
00060 VideoJpgPlayer(Window* parent, int w, int h, Video& video):
00061 Window(parent, w, h), mVideo(&video)
00062 {
00063 Init(w,h);
00064 }
00065
00066 VideoJpgPlayer(Window* parent,int x,int y,int w,int h, Video& video):
00067 Window(parent, x, y, w, h), mVideo(&video)
00068 {
00069 Init(w,h);
00070 }
00071
00072 void SetVideoJpgPlayerListener(VideoJpgPlayerListener* listener,
00073 void* userData)
00074 {
00075 mListener = listener;
00076 mListenerData = userData;
00077 }
00078
00079 void TimePerFrame(float seconds) { mTimePerFrame = seconds; }
00080 float TimePerFrame() { return mTimePerFrame; }
00081 void NoKeyboardCommands(bool mode) { mNoKeyboardCommands = mode; }
00082 bool NoKeyboardCommands() { return mNoKeyboardCommands; }
00083 int FrameNr() { return mFrameNr; }
00084
00085 void OpenVideo(Video& video)
00086 {
00087 mVideo = &video;
00088
00089 if (mOglView)
00090 viewSys.SetImage(mOglView,0);
00091 mArray = 0;
00092
00093 mTimePerFrame = mVideo->GetTimePerFrame();
00094 mFirstFrameNr = mVideo->GetNrOfFirstFrame();
00095 mLastFrameNr = mShowMaxFrameNr = mVideo->GetNrOfLastFrame();
00096 mFrameNr = mFirstFrameNr;
00097 mIncrement = 1;
00098 mNewFrame = false;
00099 mLastReadFrameNr = -999999;
00100
00101 OGLIMAGE* im = OglImageReadFirstFrame();
00102 IsPower2(mImW, &mPower2W);
00103 IsPower2(mImH, &mPower2H);
00104 im->noTexScaling = 1;
00105
00106 if (!mOglView)
00107 {
00108 mOglView = viewSys.View2D(mOglWnd, NULL, 0, 0, W(), H());
00109 viewSys.SetColor(mOglView, OGL_BG, 0x400000ff);
00110 viewSys.SetTexturing(mOglView, true);
00111 viewSys.SetTags(mOglView, FlexViewTags & ~deletableTag);
00112 viewSys.SetTags(mOglView, selectedTag);
00113 }
00114
00115 if (mOglView)
00116 viewSys.SetImage(mOglView, im);
00117 oglSys.ReleaseOglImage(im);
00118 }
00119
00120 void SetShowMaxFrame(int frameNr) { mShowMaxFrameNr = frameNr;}
00121 void SetFirstFrame(int frameNr) { mFirstFrameNr = frameNr; }
00122 void SetLastFrame(int frameNr) { mLastFrameNr = frameNr; }
00123
00124 float GetTimePast(int frameNr)
00125 {
00126 return (float) (frameNr - mFirstFrameNr) * mTimePerFrame;
00127 }
00128
00129 float GetTimePast()
00130 {
00131 return GetTimePast(mFrameNr);
00132 }
00133
00134 void Play()
00135 {
00136 mPlaying = true;
00137 mNormalSpeed = true;
00138 mIncrement = 1;
00139 oglSys.SetAlwaysDraw(mOglWnd, true);
00140 }
00141
00142 void Stop()
00143 {
00144 mPlaying = false;
00145 oglSys.SetAlwaysDraw(mOglWnd, false);
00146 }
00147
00148 void Reset()
00149 {
00150 Stop();
00151 GoToFrame(mFirstFrameNr);
00152 }
00153
00154 void FFwd()
00155 {
00156 Play();
00157 mIncrement = 1;
00158 mNormalSpeed = false;
00159 }
00160
00161 void FBwd()
00162 {
00163 Play();
00164 mIncrement = -1;
00165 mNormalSpeed = false;
00166 }
00167
00168 void NextFrame(int inc=1)
00169 {
00170 GoToFrame(mFrameNr+inc);
00171 }
00172
00173 void GoToFrame(int frameNr)
00174 {
00175 mFrameNr = frameNr;
00176 if (mFrameNr < mFirstFrameNr)
00177 mFrameNr = mFirstFrameNr;
00178 if (mFrameNr > mShowMaxFrameNr)
00179 mFrameNr = mShowMaxFrameNr;
00180 if (mFrameNr > mLastFrameNr)
00181 {
00182 mFrameNr = mLastFrameNr;
00183 if (mLooping)
00184 Reset();
00185 }
00186 if (mLastReadFrameNr != mFrameNr)
00187 ReadFrameIntoOglImage(mOglView->im );
00188 }
00189
00190 bool Playing()
00191 {
00192 return mPlaying;
00193 }
00194
00195 bool FFwding()
00196 {
00197 return mPlaying && !mNormalSpeed && mIncrement > 0;
00198 }
00199 bool FBwding()
00200 {
00201 return mPlaying && !mNormalSpeed && mIncrement < 0;
00202 }
00203
00204 OGLVIEW* OglView()
00205 {
00206 return mOglView;
00207 }
00208
00209 void FitViewToWindow(bool mode) { mFitViewToWindow = mode; }
00210 bool FitViewToWindow() { return mFitViewToWindow; }
00211 void ScaleImageToView(bool mode) { mScaleImageToView = mode; }
00212 bool ScaleImageToView() { return mScaleImageToView; }
00213 void NormalSpeed(bool mode) { mNormalSpeed = mode; }
00214 void SetIncrement(int incr) { mIncrement = incr; }
00215
00216 virtual void InitDisplayFunc()
00217 {
00218 if (mFitViewToWindow && mOglView)
00219 viewSys.SetDimensions(mOglView, 2, 2, W()-4, H()-4);
00220
00221 if (mOglView && mScaleImageToView && mOglView->im)
00222 {
00223 float zX = W()/(float)mImW;
00224 float zY = H()/(float)mImH;
00225 if (mOglView->texturing && mOglView->im->noTexScaling)
00226 {
00227 zX = (W()/(float)mImW)*(mPower2W/(float)mImW);
00228 zY = (H()/(float)mImH)*(mPower2H/(float)mImH);
00229 }
00230 viewSys.SetZoom(mOglView, zX, zY);
00231 }
00232 Window::InitDisplayFunc();
00233 }
00234
00235 virtual void DisplayFunc()
00236 {
00237 double cpuTime = OglClock();
00238
00239 if (mPlaying && mOglView){
00240 if (!mNormalSpeed || (cpuTime-mLastTime) > mTimePerFrame)
00241 {
00242 mLastTime = cpuTime;
00243 NextFrame(mIncrement);
00244 }
00245 }
00246 Window::DisplayFunc();
00247 if (mNewFrame && mListener)
00248 mListener->OnNewFrame(this, mFrameNr, mListenerData);
00249 mNewFrame = false;
00250 }
00251
00252 virtual void MouseFunc(int msg, int btn, int state, int x, int y)
00253 {
00254 if (msg == oglMouseWheelDown)
00255 NextFrame();
00256 if (msg == oglMouseWheelUp)
00257 NextFrame(-1);
00258 Window::MouseFunc(msg,btn,state,x,y);
00259 }
00260
00261 virtual void KeyboardFunc(int c, int state)
00262 {
00263 if (!mNoKeyboardCommands)
00264 {
00265 if (c == 'p')
00266 Play();
00267 if (c == 's')
00268 Stop();
00269 if (c == oglHOME)
00270 Reset();
00271 if (c == 'n')
00272 mNormalSpeed = !mNormalSpeed;
00273 if (c == '+')
00274 NextFrame(1);
00275 if (c == '-')
00276 NextFrame(-1);
00277 if (c == 'S')
00278 mScaleImageToView = !mScaleImageToView;
00279 if (c == 'F')
00280 mFitViewToWindow = !mFitViewToWindow;
00281 }
00282 Window::KeyboardFunc(c, state);
00283 if (mListener)
00284 mListener->OnKey(this, c, state, mListenerData);
00285 }
00286
00287 OGLIMAGE* OglImageReadFirstFrame()
00288 {
00289 mFrameNr = mFirstFrameNr;
00290 mVideo->GetFramePathAnnotated(mFirstFrameNr, mNameBuf);
00291 ReadJpg(mArray,mNameBuf);
00292 mNewFrame = true;
00293
00294 mImW = mArray->CW();
00295 mImH = mArray->CH();
00296
00297 return Impala::Visualization::RgbOglImage::OglImage(mArray);
00298 }
00299
00300
00301 private:
00302
00303 void ReadFrameIntoOglImage(OGLIMAGE *oglIm)
00304 {
00305 if (mFrameNr > mLastFrameNr)
00306 mFrameNr = mLastFrameNr;
00307 if (mFrameNr < mFirstFrameNr)
00308 mFrameNr = mFirstFrameNr;
00309 if (mArray)
00310 delete mArray;
00311 mArray = 0;
00312 mVideo->GetFramePathAnnotated(mFrameNr, mNameBuf);
00313 ReadJpg(mArray,mNameBuf);
00314 mLastReadFrameNr = mFrameNr;
00315 oglIm->imageHandle = mArray;
00316 oglIm->changed = 1;
00317 mNewFrame = true;
00318 }
00319
00320 void Init(int w, int h)
00321 {
00322 mArray = 0;
00323
00324 mListener = 0;
00325
00326 mOglView = 0;
00327 mNoKeyboardCommands = false;
00328 mLooping = false;
00329 mPlaying = 0;
00330 mLastTime = 0;
00331 mNormalSpeed = true;
00332 mFitViewToWindow = true;
00333 mScaleImageToView = true;
00334
00335 OpenVideo(*mVideo);
00336 }
00337
00338 VideoJpgPlayerListener* mListener;
00339 void* mListenerData;
00340
00341 char mNameBuf[2048];
00342 Array2dVec3UInt8* mArray;
00343
00344 Video* mVideo;
00345 OGLVIEW* mOglView;
00346
00347 float mTimePerFrame;
00348 double mLastTime;
00349 bool mNewFrame;
00350
00351 int mLastReadFrameNr;
00352 int mFirstFrameNr;
00353 int mLastFrameNr;
00354 int mShowMaxFrameNr;
00355 int mFrameNr;
00356
00357 int mIncrement;
00358
00359 int mImW, mImH;
00360 int mPower2W, mPower2H;
00361
00362 bool mLooping;
00363 bool mNoKeyboardCommands;
00364 bool mPlaying;
00365 bool mNormalSpeed;
00366 bool mFitViewToWindow;
00367 bool mScaleImageToView;
00368 };
00369
00370 }
00371 }
00372
00373 #endif