Salome HOME
Update from BR_V5_DEV 13Feb2009
[modules/gui.git] / src / SVTK / SVTK_Recorder.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 #include "SVTK_Recorder.h"
23
24 #include "SVTK_ImageWriter.h"
25 #include "SVTK_ImageWriterMgr.h"
26
27 #include <vtkObjectFactory.h>
28 #include <vtkObject.h>
29 #include <vtkCallbackCommand.h>
30 #include <vtkRenderWindow.h>
31 #include <vtkTimerLog.h>
32 #include <vtkWindowToImageFilter.h>
33 #include <vtkJPEGWriter.h>
34 #include <vtkImageData.h>
35
36 #include <sstream>
37 #include <iomanip>
38 #include <iostream>
39
40 #ifndef WIN32
41 #include <unistd.h>
42 #endif
43
44 #include <QApplication>
45 #include <QFileInfo>
46 #include <QDir>
47
48 //#include "utilities.h"
49
50 #ifdef _DEBUG_
51 static int MYDEBUG = 0;
52 #else
53 static int MYDEBUG = 0;
54 #endif
55
56
57 namespace
58 {
59   //----------------------------------------------------------------------------
60   inline
61   void
62   GetNameJPEG(const std::string& thePreffix,  
63               const int theIndex,
64               std::string& theName)
65   {
66     using namespace std;
67     ostringstream aStream;
68     aStream<<thePreffix<<"_"<<setw(6)<<setfill('0')<<theIndex<<".jpeg";
69     theName = aStream.str();
70   }
71 }
72
73 //----------------------------------------------------------------------------
74 vtkCxxRevisionMacro(SVTK_Recorder,"$Revision$");
75 vtkStandardNewMacro(SVTK_Recorder);
76
77
78 //----------------------------------------------------------------------------
79 SVTK_Recorder
80 ::SVTK_Recorder():
81   myRenderWindow(NULL),
82   myState(SVTK_Recorder_Stop),
83   myNbFPS(5.5),
84   myQuality(100),
85   myProgressiveMode(true),
86   myUseSkippedFrames(true),
87   myErrorStatus(0),
88   myCommand(vtkCallbackCommand::New()),
89   myPriority(0.0),
90   myTimeStart(0.0),
91   myFrameIndex(0),
92   myPaused(0),
93   myFilter(vtkWindowToImageFilter::New()),
94   myWriterMgr(new SVTK_ImageWriterMgr),
95   myNbWrittenFrames(0),
96   myNameAVIMaker("jpeg2yuv")
97 {
98   myCommand->SetClientData(this); 
99   myCommand->SetCallback(SVTK_Recorder::ProcessEvents);
100 }
101
102
103 //----------------------------------------------------------------------------
104 SVTK_Recorder
105 ::~SVTK_Recorder()
106 {
107   myCommand->Delete();
108   myFilter->Delete();
109   delete myWriterMgr;
110 }
111
112
113 //----------------------------------------------------------------------------
114 void
115 SVTK_Recorder
116 ::CheckExistAVIMaker()
117 {
118   myErrorStatus = 0;
119   using namespace std;
120   ostringstream aStream;
121   aStream<<"which "<<myNameAVIMaker<<" >& /dev/null";
122   std::string anAVIMakeCheck = aStream.str();
123   int iErr = system(anAVIMakeCheck.c_str());
124   if(iErr != 0)
125     myErrorStatus = 127;
126 }
127
128
129 //----------------------------------------------------------------------------
130 void
131 SVTK_Recorder
132 ::SetName(const char* theName)
133 {
134   myName = theName;
135 }
136
137 const char* 
138 SVTK_Recorder::Name() const
139 {
140   return myName.c_str();
141 }
142
143
144 //----------------------------------------------------------------------------
145 void
146 SVTK_Recorder
147 ::SetNbFPS(const double theNbFPS)
148 {
149   myNbFPS = theNbFPS;
150 }
151
152 double
153 SVTK_Recorder
154 ::NbFPS() const
155 {
156   return myNbFPS;
157 }
158
159
160 //----------------------------------------------------------------------------
161 void
162 SVTK_Recorder
163 ::SetQuality(int theQuality)
164 {
165   myQuality = theQuality;
166 }
167
168 int
169 SVTK_Recorder
170 ::GetQuality() const
171 {
172   return myQuality;
173 }
174
175
176 //----------------------------------------------------------------------------
177 void 
178 SVTK_Recorder
179 ::SetRenderWindow(vtkRenderWindow* theRenderWindow)
180 {
181   myRenderWindow = theRenderWindow;
182 }
183
184 vtkRenderWindow* 
185 SVTK_Recorder
186 ::RenderWindow()
187 {
188   return myRenderWindow;
189 }
190
191
192 //----------------------------------------------------------------------------
193 void
194 SVTK_Recorder
195 ::SetProgressiveMode(bool theProgressiveMode)
196 {
197   myProgressiveMode = theProgressiveMode;
198 }
199
200 bool
201 SVTK_Recorder
202 ::GetProgressiveMode() const
203 {
204   return myProgressiveMode;
205 }
206
207
208 //----------------------------------------------------------------------------
209 void
210 SVTK_Recorder
211 ::SetUseSkippedFrames(bool theUseSkippedFrames)
212 {
213   myUseSkippedFrames = theUseSkippedFrames;
214 }
215
216 bool
217 SVTK_Recorder
218 ::UseSkippedFrames() const
219 {
220   return myUseSkippedFrames;
221 }
222
223
224 //----------------------------------------------------------------------------
225 int
226 SVTK_Recorder
227 ::ErrorStatus() const
228 {
229   return myErrorStatus;
230 }
231
232 int
233 SVTK_Recorder
234 ::State() const
235 {
236   return myState;
237 }
238
239
240 //----------------------------------------------------------------------------
241 void
242 SVTK_Recorder
243 ::ProcessEvents(vtkObject* vtkNotUsed(theObject), 
244                 unsigned long theEvent,
245                 void* theClientData, 
246                 void* vtkNotUsed(theCallData))
247 {
248   if(vtkObject* anObj = reinterpret_cast<vtkObject*>(theClientData)){ 
249     if(SVTK_Recorder* aSelf = dynamic_cast<SVTK_Recorder*>(anObj)){
250       if(theEvent==vtkCommand::EndEvent){
251         if(aSelf->State() == SVTK_Recorder::SVTK_Recorder_Record){
252           aSelf->DoRecord();
253         }
254       }
255     }
256   }
257 }
258
259
260 //----------------------------------------------------------------------------
261 void
262 SVTK_Recorder
263 ::Record()
264 {
265   if(myState == SVTK_Recorder_Stop){
266     if(myRenderWindow){
267       myState = SVTK_Recorder_Record;
268       myFilter->SetInput(myRenderWindow);
269       myFrameIndex = -1;
270       myNbWrittenFrames = 0;
271       myRenderWindow->RemoveObserver(myCommand);
272       myRenderWindow->AddObserver(vtkCommand::EndEvent,
273                                   myCommand,
274                                   myPriority);
275       myRenderWindow->Render();
276     }
277   }
278 }
279
280
281 //----------------------------------------------------------------------------
282 void
283 SVTK_Recorder
284 ::Stop()
285 {
286   QApplication::setOverrideCursor( Qt::WaitCursor );
287
288   if(myState == SVTK_Recorder_Record){ 
289     if(!myPaused)
290       DoRecord();
291
292     myWriterMgr->Stop();
293
294     if(myUseSkippedFrames)
295       AddSkippedFrames();
296
297     myFrameIndexes.clear();
298
299     MakeFileAVI();
300   }
301   myState = SVTK_Recorder_Stop;
302   myPaused = 0;
303
304   QApplication::restoreOverrideCursor();
305 }
306
307
308 //----------------------------------------------------------------------------
309 void
310 SVTK_Recorder
311 ::Pause()
312 {
313   myPaused = myPaused ? 0 : 1;
314   if(myPaused && !myFrameIndexes.empty()){
315     myFrameIndexes.back() *= -1;
316     if(MYDEBUG) cout<<"SVTK_Recorder::Pause - myFrameIndexes.back() = "<<myFrameIndexes.back()<<endl;
317   }
318 }
319
320
321 //----------------------------------------------------------------------------
322 inline 
323 int
324 GetFrameIndex(double theStartTime,
325               double theFPS)
326 {
327   double aTimeNow = vtkTimerLog::GetCurrentTime();
328   double aDelta = aTimeNow - theStartTime;
329   return int(aDelta*theFPS);
330 }
331
332 void
333 SVTK_Recorder
334 ::DoRecord()
335 {
336   if(myPaused)
337     return;
338
339   if(myFrameIndex < 0){
340     myFrameIndex = 0;
341     myTimeStart = vtkTimerLog::GetCurrentTime();
342   }else{
343     int aFrameIndex = GetFrameIndex(myTimeStart,myNbFPS);
344     if(aFrameIndex <= myFrameIndex)
345       return;
346
347     // If there was a "pause" we correct the myTimeStart
348     int aLastFrameIndex = myFrameIndexes.back();
349     if(aLastFrameIndex < 0){
350       myFrameIndexes.back() = abs(myFrameIndexes.back());
351       double aPauseTime = fabs((double)(aFrameIndex - myFrameIndex - 1)) / myNbFPS;
352       if(MYDEBUG) 
353         cout<<"SVTK_Recorder::DoRecord - aFrameIndex = "<<aFrameIndex<<
354           "; aPauseTime = "<<aPauseTime<<endl;
355       myTimeStart += aPauseTime;
356     }
357
358     aFrameIndex = GetFrameIndex(myTimeStart,myNbFPS);
359     if(aFrameIndex <= myFrameIndex)
360       return;
361
362     myFrameIndex = aFrameIndex;
363   }
364
365   myFrameIndexes.push_back(myFrameIndex);
366   if(MYDEBUG) cout<<"SVTK_Recorder::DoRecord - myFrameIndex = "<<myFrameIndex<<endl;
367
368   myRenderWindow->RemoveObserver(myCommand);
369   myFilter->Modified();
370
371   std::string aName;
372   GetNameJPEG(myName,myFrameIndex,aName);
373
374   PreWrite();
375
376   vtkImageData *anImageData = vtkImageData::New(); 
377   anImageData->DeepCopy(myFilter->GetOutput());
378
379   myWriterMgr->StartImageWriter(anImageData,aName,myProgressiveMode,myQuality);
380   myNbWrittenFrames++;
381
382   myRenderWindow->AddObserver(vtkCommand::EndEvent,
383                               myCommand,
384                               myPriority);
385 }
386
387
388 //----------------------------------------------------------------------------
389 void
390 SVTK_Recorder
391 ::PreWrite()
392 {
393   vtkImageData *anImageData = myFilter->GetOutput();
394   //
395   if(!anImageData){
396     myErrorStatus = 20;
397     return;
398   }
399   anImageData->UpdateInformation();
400   int *anExtent = anImageData->GetWholeExtent();
401   anImageData->SetUpdateExtent(anExtent[0], anExtent[1],
402                                anExtent[2], anExtent[3],
403                                0,0);
404   anImageData->UpdateData();
405 }
406
407
408 //----------------------------------------------------------------------------
409 void
410 SVTK_Recorder
411 ::AddSkippedFrames()
412 {
413   myErrorStatus = 0;
414
415   if(myFrameIndexes.size() < 2)
416     return;
417
418   size_t anId = 0, anEnd = myFrameIndexes.size() - 1;
419   for(; anId < anEnd; anId++){
420     int aStartIndex = myFrameIndexes[anId];
421     if(aStartIndex < 0)
422       continue;
423
424     int aFinishIndex = abs(myFrameIndexes[anId + 1]);
425     if(aStartIndex + 1 == aFinishIndex)
426       continue;
427
428     std::string anInitialName;
429     std::ostringstream aStream;
430     GetNameJPEG(myName,aStartIndex,anInitialName);
431     for(int anIndex = aStartIndex + 1; anIndex < aFinishIndex; anIndex++){
432       myNbWrittenFrames++;
433       std::string anCurrentName;
434       GetNameJPEG(myName,anIndex,anCurrentName);
435       aStream<<"ln -s "<< anInitialName<<" "<<anCurrentName<<";";
436       if(anIndex + 1 < aFinishIndex)
437         aStream<<" \\";
438       aStream<<endl;
439     }
440     std::string aString(aStream.str());
441     system(aString.c_str());
442     if(MYDEBUG) cout<<"SVTK_Recorder::AddSkippedFrames - "<<aString<<endl;
443   }
444 }
445
446
447 //----------------------------------------------------------------------------
448 void
449 SVTK_Recorder
450 ::MakeFileAVI()
451 {
452   myErrorStatus = 0;
453   std::ostringstream aStream;
454   aStream<<myNameAVIMaker<<
455     " -I p"<<
456     " -v 0"<<
457     //" -f "<<int(myNbFPS)<<" "<<
458     " -f "<<myNbFPS<<" "<<
459     " -n "<<myNbWrittenFrames<<" "<<
460     " -j "<<myName<<"_\%06d.jpeg "<<
461     "| yuv2lav"<<
462     " -o "<<myName;
463    
464   std::string aString(aStream.str());
465   myErrorStatus = system(aString.c_str());
466
467   if(MYDEBUG) cout<<"SVTK_Recorder::MakeFileAVI - "<<aString<<endl;
468
469   QFileInfo aFileInfo(myName.c_str());
470   QString aDirPath = aFileInfo.absoluteDir().path();
471   QString aBaseName = aFileInfo.fileName();
472   QString aCommand = 
473     QString("(cd ") + aDirPath + 
474     "; ls " +
475     " | egrep '" + aBaseName + "_[0-9]*.jpeg'" +
476     " | xargs rm " +
477     ")";
478
479   if(MYDEBUG) cout<<"SVTK_Recorder::MakeFileAVI - "<<(const char*)aCommand.toLatin1()<<endl;
480   system((const char*)aCommand.toLatin1());
481 }