1 // SALOME VTKViewer : build VTK viewer into Salome desktop
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
29 #include "VVTK_Recorder.h"
31 #include "VVTK_ImageWriter.h"
32 #include "VVTK_ImageWriterMgr.h"
34 #include <vtkObjectFactory.h>
35 #include <vtkObject.h>
36 #include <vtkCallbackCommand.h>
37 #include <vtkRenderWindow.h>
38 #include <vtkTimerLog.h>
39 #include <vtkWindowToImageFilter.h>
40 #include <vtkJPEGWriter.h>
41 #include <vtkImageData.h>
48 #include <qapplication.h>
49 #include <qfileinfo.h>
51 #include "utilities.h"
54 static int MYDEBUG = 0;
56 static int MYDEBUG = 0;
62 //----------------------------------------------------------------------------
65 GetNameJPEG(const std::string& thePreffix,
70 ostringstream aStream;
71 aStream<<thePreffix<<"_"<<setw(6)<<setfill('0')<<theIndex<<".jpeg";
72 theName = aStream.str();
76 //----------------------------------------------------------------------------
77 vtkCxxRevisionMacro(VVTK_Recorder,"$Revision$");
78 vtkStandardNewMacro(VVTK_Recorder);
81 //----------------------------------------------------------------------------
85 myState(VVTK_Recorder_Stop),
88 myProgressiveMode(true),
89 myUseSkippedFrames(true),
91 myCommand(vtkCallbackCommand::New()),
96 myFilter(vtkWindowToImageFilter::New()),
97 myWriterMgr(new VVTK_ImageWriterMgr),
99 myNameAVIMaker("jpeg2yuv")
101 myCommand->SetClientData(this);
102 myCommand->SetCallback(VVTK_Recorder::ProcessEvents);
106 //----------------------------------------------------------------------------
116 //----------------------------------------------------------------------------
119 ::CheckExistAVIMaker()
123 ostringstream aStream;
124 aStream<<"which "<<myNameAVIMaker<<" >& /dev/null";
125 std::string anAVIMakeCheck = aStream.str();
126 int iErr = system(anAVIMakeCheck.c_str());
132 //----------------------------------------------------------------------------
135 ::SetName(const char* theName)
141 VVTK_Recorder::Name() const
143 return myName.c_str();
147 //----------------------------------------------------------------------------
150 ::SetNbFPS(const double theNbFPS)
163 //----------------------------------------------------------------------------
166 ::SetQuality(int theQuality)
168 myQuality = theQuality;
179 //----------------------------------------------------------------------------
182 ::SetRenderWindow(vtkRenderWindow* theRenderWindow)
184 myRenderWindow = theRenderWindow;
191 return myRenderWindow;
195 //----------------------------------------------------------------------------
198 ::SetProgressiveMode(bool theProgressiveMode)
200 myProgressiveMode = theProgressiveMode;
205 ::GetProgressiveMode() const
207 return myProgressiveMode;
211 //----------------------------------------------------------------------------
214 ::SetUseSkippedFrames(bool theUseSkippedFrames)
216 myUseSkippedFrames = theUseSkippedFrames;
221 ::UseSkippedFrames() const
223 return myUseSkippedFrames;
227 //----------------------------------------------------------------------------
230 ::ErrorStatus() const
232 return myErrorStatus;
243 //----------------------------------------------------------------------------
246 ::ProcessEvents(vtkObject* vtkNotUsed(theObject),
247 unsigned long theEvent,
249 void* vtkNotUsed(theCallData))
251 if(vtkObject* anObj = reinterpret_cast<vtkObject*>(theClientData)){
252 if(VVTK_Recorder* aSelf = dynamic_cast<VVTK_Recorder*>(anObj)){
253 if(theEvent==vtkCommand::EndEvent){
254 if(aSelf->State() == VVTK_Recorder::VVTK_Recorder_Record){
263 //----------------------------------------------------------------------------
268 if(myState == VVTK_Recorder_Stop){
270 myState = VVTK_Recorder_Record;
271 myFilter->SetInput(myRenderWindow);
273 myNbWrittenFrames = 0;
274 myRenderWindow->RemoveObserver(myCommand);
275 myRenderWindow->AddObserver(vtkCommand::EndEvent,
278 myRenderWindow->Render();
284 //----------------------------------------------------------------------------
289 QApplication::setOverrideCursor( Qt::waitCursor );
291 if(myState == VVTK_Recorder_Record){
297 if(myUseSkippedFrames)
300 myFrameIndexes.clear();
304 myState = VVTK_Recorder_Stop;
307 QApplication::restoreOverrideCursor();
311 //----------------------------------------------------------------------------
316 myPaused = myPaused ? 0 : 1;
317 if(myPaused && !myFrameIndexes.empty()){
318 myFrameIndexes.back() *= -1;
319 if(MYDEBUG) cout<<"VVTK_Recorder::Pause - myFrameIndexes.back() = "<<myFrameIndexes.back()<<endl;
324 //----------------------------------------------------------------------------
327 GetFrameIndex(double theStartTime,
330 double aTimeNow = vtkTimerLog::GetCurrentTime();
331 double aDelta = aTimeNow - theStartTime;
332 return int(aDelta*theFPS);
342 if(myFrameIndex < 0){
344 myTimeStart = vtkTimerLog::GetCurrentTime();
346 int aFrameIndex = GetFrameIndex(myTimeStart,myNbFPS);
347 if(aFrameIndex <= myFrameIndex)
350 // If there was a "pause" we correct the myTimeStart
351 int aLastFrameIndex = myFrameIndexes.back();
352 if(aLastFrameIndex < 0){
353 myFrameIndexes.back() = abs(myFrameIndexes.back());
354 double aPauseTime = fabs(aFrameIndex - myFrameIndex - 1) / myNbFPS;
356 cout<<"VVTK_Recorder::DoRecord - aFrameIndex = "<<aFrameIndex<<
357 "; aPauseTime = "<<aPauseTime<<endl;
358 myTimeStart += aPauseTime;
361 aFrameIndex = GetFrameIndex(myTimeStart,myNbFPS);
362 if(aFrameIndex <= myFrameIndex)
365 myFrameIndex = aFrameIndex;
368 myFrameIndexes.push_back(myFrameIndex);
369 if(MYDEBUG) cout<<"VVTK_Recorder::DoRecord - myFrameIndex = "<<myFrameIndex<<endl;
371 myRenderWindow->RemoveObserver(myCommand);
372 myFilter->Modified();
375 GetNameJPEG(myName,myFrameIndex,aName);
379 vtkImageData *anImageData = vtkImageData::New();
380 anImageData->DeepCopy(myFilter->GetOutput());
382 myWriterMgr->StartImageWriter(anImageData,aName,myProgressiveMode,myQuality);
385 myRenderWindow->AddObserver(vtkCommand::EndEvent,
391 //----------------------------------------------------------------------------
396 vtkImageData *anImageData = myFilter->GetOutput();
402 anImageData->UpdateInformation();
403 int *anExtent = anImageData->GetWholeExtent();
404 anImageData->SetUpdateExtent(anExtent[0], anExtent[1],
405 anExtent[2], anExtent[3],
407 anImageData->UpdateData();
411 //----------------------------------------------------------------------------
418 if(myFrameIndexes.size() < 2)
421 size_t anId = 0, anEnd = myFrameIndexes.size() - 1;
422 for(; anId < anEnd; anId++){
423 int aStartIndex = myFrameIndexes[anId];
427 int aFinishIndex = abs(myFrameIndexes[anId + 1]);
428 if(aStartIndex + 1 == aFinishIndex)
431 std::string anInitialName;
432 std::ostringstream aStream;
433 GetNameJPEG(myName,aStartIndex,anInitialName);
434 for(int anIndex = aStartIndex + 1; anIndex < aFinishIndex; anIndex++){
436 std::string anCurrentName;
437 GetNameJPEG(myName,anIndex,anCurrentName);
438 aStream<<"ln -s "<< anInitialName<<" "<<anCurrentName<<";";
439 if(anIndex + 1 < aFinishIndex)
443 std::string aString(aStream.str());
444 system(aString.c_str());
445 if(MYDEBUG) cout<<"VVTK_Recorder::AddSkippedFrames - "<<aString<<endl;
450 //----------------------------------------------------------------------------
456 std::ostringstream aStream;
457 aStream<<myNameAVIMaker<<
460 //" -f "<<int(myNbFPS)<<" "<<
461 " -f "<<myNbFPS<<" "<<
462 " -n "<<myNbWrittenFrames<<" "<<
463 " -j "<<myName<<"_\%06d.jpeg "<<
467 std::string aString(aStream.str());
468 myErrorStatus = system(aString.c_str());
470 if(MYDEBUG) cout<<"VVTK_Recorder::MakeFileAVI - "<<aString<<endl;
472 QFileInfo aFileInfo(myName.c_str());
473 QString aDirPath = aFileInfo.dirPath(TRUE);
474 QString aBaseName = aFileInfo.fileName();
476 QString("(cd ") + aDirPath +
478 " | egrep '" + aBaseName + "_[0-9]*.jpeg'" +
483 QString("rm ") + aDirPath + "/" + aBaseName + "*.jpeg";
485 if(MYDEBUG) cout<<"VVTK_Recorder::MakeFileAVI - "<<aCommand.latin1()<<endl;
486 system(aCommand.latin1());