00001
00002
00003
00004
00005
00006 #ifndef Impala_Application_SDash_VideoJpgCamera_h
00007 #define Impala_Application_SDash_VideoJpgCamera_h
00008
00009 #include <time.h>
00010
00011 #ifndef Impala_Core_Array_Arrays_h
00012 #include "Core/Array/Arrays.h"
00013 #endif
00014
00015 #ifndef Impala_Core_Array_ReadJpg_h
00016 #include "Core/Array/ReadJpg.h"
00017 #endif
00018
00019 #ifndef Impala_Visualisation_RgbOglImage_h
00020 #include "Visualization/RgbOglImage.h"
00021 #endif
00022
00023 #ifndef OglGui_Window_h
00024 #include "OglGui/Window.h"
00025 #endif
00026
00027 #include "Visualization/Video.h"
00028
00029 namespace Impala {
00030 namespace Application {
00031 namespace SDash {
00032
00033
00034 class VideoJpgCamera : public OglGui::Window
00035 {
00036 typedef Impala::Core::Array::Array2dVec3UInt8 Array2dVec3UInt8;
00037 typedef OglGui::Window Window;
00038 public:
00039
00040 VideoJpgCamera(int x, int y, int w, int h, Visualization::Video& video, bool doLoop = true):
00041 Window(x, y, w, h), mVideo(&video)
00042 {
00043 Init(w, h, doLoop);
00044 }
00045
00046 VideoJpgCamera(Window* parent, int w, int h, Visualization::Video& video, bool doLoop = true):
00047 Window(parent, w, h), mVideo(&video)
00048 {
00049 Init(w, h, doLoop);
00050 }
00051
00052 VideoJpgCamera(Window* parent,int x,int y,int w,int h, Visualization::Video& video, bool doLoop = true):
00053 Window(parent, x, y, w, h), mVideo(&video)
00054 {
00055 Init(w, h, doLoop);
00056 }
00057
00058
00059 float TimePerFrame() { return mTimePerFrame; }
00060 int FrameNr() { return mFrameNr; }
00061
00062 void OpenVideo(Visualization::Video& video)
00063 {
00064 mVideo = &video;
00065 mTimePerFrame = mVideo->GetTimePerFrame();
00066 mFirstFrameNr = mVideo->GetNrOfFirstFrame();
00067 mLastFrameNr = mVideo->GetNrOfLastFrame();
00068
00069 if (mOglView)
00070 viewSys.SetImage(mOglView,0);
00071 mArray = 0;
00072 OGLIMAGE* im = OglImageReadFirstFrame();
00073 IsPower2(mImW, &mPower2W);
00074 IsPower2(mImH, &mPower2H);
00075 im->noTexScaling = 1;
00076
00077 if (!mOglView)
00078 {
00079 mOglView = viewSys.View2D(mOglWnd, NULL, 0, 0, W(), H());
00080 viewSys.SetColor(mOglView, OGL_BG, 0x400000ff);
00081 viewSys.SetColor(mOglView, OGL_BORDER, oglWHITE);
00082 viewSys.SetTexturing(mOglView, true);
00083 viewSys.SetTags(mOglView, FlexViewTags & ~deletableTag);
00084 viewSys.SetTags(mOglView, selectedTag);
00085 }
00086
00087 if (mOglView)
00088 viewSys.SetImage(mOglView, im);
00089 oglSys.ReleaseOglImage(im);
00090 }
00091
00092 void Play(int startTimeOffset = 0)
00093 {
00094 mStartTime = clock();
00095 mStartTimeOffset = startTimeOffset;
00096 mPlaying = true;
00097 oglSys.SetAlwaysDraw(mOglWnd, true);
00098 }
00099
00100 OGLVIEW* OglView()
00101 {
00102 return mOglView;
00103 }
00104
00105 void FitViewToWindow(bool mode) { mFitViewToWindow = mode; }
00106 bool FitViewToWindow() { return mFitViewToWindow; }
00107 void ScaleImageToView(bool mode) { mScaleImageToView = mode; }
00108 bool ScaleImageToView() { return mScaleImageToView; }
00109
00110 virtual void InitDisplayFunc()
00111 {
00112 if (mFitViewToWindow && mOglView)
00113 viewSys.SetDimensions(mOglView, 2, 2, W()-4, H()-4);
00114
00115 if (mOglView && mScaleImageToView && mOglView->im)
00116 {
00117 float zX = W()/(float)mImW;
00118 float zY = H()/(float)mImH;
00119 if (mOglView->texturing && mOglView->im->noTexScaling)
00120 {
00121 zX = (W()/(float)mImW)*(mPower2W/(float)mImW);
00122 zY = (H()/(float)mImH)*(mPower2H/(float)mImH);
00123 }
00124 viewSys.SetZoom(mOglView, zX, zY);
00125 }
00126 Window::InitDisplayFunc();
00127 }
00128
00129 virtual void DisplayFunc()
00130 {
00131 clock_t cpuTime = clock();
00132
00133 if (mPlaying && mOglView){
00134 float elapsedTime = ((cpuTime-mStartTime) / (float) CLOCKS_PER_SEC) + (float) mStartTimeOffset;
00135 int nFrameNr = elapsedTime / mTimePerFrame;
00136 if ((mFrameNr != nFrameNr) && ((mFrameNr < mLastFrameNr) || mDoLoop))
00137 {
00138 mFrameNr = nFrameNr-1;
00139 NextFrameIntoOglImage(mOglView->im );
00140 }
00141 }
00142 if (mOglView && mOglView->rotZ<0)
00143 mOglView->rotZ += 30;
00144 Window::DisplayFunc();
00145 }
00146
00147
00148
00149 OGLIMAGE* OglImageReadFirstFrame()
00150 {
00151 mFrameNr = mFirstFrameNr;
00152 mVideo->GetFramePath(mFrameNr, mNameBuf);
00153 ReadJpg(mArray,mNameBuf);
00154
00155 mImW = mArray->CW();
00156 mImH = mArray->CH();
00157
00158 return Impala::Visualization::RgbOglImage::OglImage(mArray);
00159 }
00160
00161 void NextFrameIntoOglImage(OGLIMAGE *oglIm)
00162 {
00163 mFrameNr++;
00164
00165
00166 mFrameNr = ((mFrameNr - mFirstFrameNr) % mVideo->GetTotalNrOfFrames()) + mFirstFrameNr;
00167 if (mArray)
00168 delete mArray;
00169 mArray = 0;
00170 mVideo->GetFramePath(mFrameNr, mNameBuf);
00171 ReadJpg(mArray,mNameBuf);
00172 #ifdef PRINT_JPG_READ
00173 printf("Read:%s\n",mNameBuf);
00174 #endif
00175 oglIm->imageHandle = mArray;
00176 oglIm->changed = 1;
00177 }
00178
00179 private:
00180
00181 void Init(int w, int h, bool doLoop)
00182 {
00183 mArray = 0;
00184
00185 mOglView = 0;
00186 mPlaying = 0;
00187 mDoLoop = doLoop;
00188 mFitViewToWindow = true;
00189 mScaleImageToView = true;
00190
00191 OpenVideo(*mVideo);
00192 }
00193
00194 char mNameBuf[2048];
00195 Array2dVec3UInt8* mArray;
00196
00197 Visualization::Video* mVideo;
00198 OGLVIEW* mOglView;
00199
00200 float mTimePerFrame;
00201 clock_t mStartTime;
00202 int mStartTimeOffset;
00203 int mFrameNr;
00204 int mFirstFrameNr;
00205 int mLastFrameNr;
00206
00207 int mImW, mImH;
00208 int mPower2W, mPower2H;
00209
00210 bool mPlaying;
00211 bool mDoLoop;
00212 bool mFitViewToWindow;
00213 bool mScaleImageToView;
00214 };
00215
00216 }
00217 }
00218 }
00219
00220 #endif