From ac2a95d382fc97c3b67879fd5e3c5a1d63a0c92c Mon Sep 17 00:00:00 2001 From: apo Date: Mon, 12 Dec 2005 06:39:26 +0000 Subject: [PATCH] To improve AVI recorder --- src/VVTK/VVTK_ImageWriter.cxx | 212 ++++------- src/VVTK/VVTK_ImageWriter.h | 49 ++- src/VVTK/VVTK_ImageWriterMgr.cxx | 194 ++++------- src/VVTK/VVTK_ImageWriterMgr.h | 50 +-- src/VVTK/VVTK_MainWindow.cxx | 40 +-- src/VVTK/VVTK_Recorder.cxx | 579 +++++++++++++++++-------------- src/VVTK/VVTK_Recorder.h | 160 ++++++--- 7 files changed, 620 insertions(+), 664 deletions(-) diff --git a/src/VVTK/VVTK_ImageWriter.cxx b/src/VVTK/VVTK_ImageWriter.cxx index 16e8e802..a4f48073 100755 --- a/src/VVTK/VVTK_ImageWriter.cxx +++ b/src/VVTK/VVTK_ImageWriter.cxx @@ -25,163 +25,85 @@ // Author : // Module : // $Header$ + #include "VVTK_ImageWriter.h" + +#include + #include -#include #include -// -//=============================================== -// function: VVTK_ImageWriter -// purpose : -//=============================================== -VVTK_ImageWriter::VVTK_ImageWriter() -: QThread() -{ - myName=NULL; - myProgressive=0; - myQuality=0; - myImageData=0; - myConstraint16Flag=true; - myErrorStatus=0; -} -//=============================================== -// function: ~ -// purpose : -//=============================================== -VVTK_ImageWriter::~VVTK_ImageWriter() -{ -} -//=============================================== -// function: SetName -// purpose : -//=============================================== -void VVTK_ImageWriter::SetName(const char *aName) -{ - myName=(char *)aName; -} -//=============================================== -// function: Name -// purpose : -//=============================================== -const char* VVTK_ImageWriter::Name()const -{ - return myName; -} -//=============================================== -// function: SetProgressive -// purpose : -//=============================================== -void VVTK_ImageWriter::SetProgressive(const int aProgressive) -{ - myProgressive=aProgressive; -} -//=============================================== -// function: Progressive -// purpose : -//=============================================== -int VVTK_ImageWriter::Progressive()const -{ - return myProgressive; -} -//=============================================== -// function: SetQuality -// purpose : -//=============================================== -void VVTK_ImageWriter::SetQuality(const int aQuality) -{ - myQuality=aQuality; -} -//=============================================== -// function: Quality -// purpose : -//=============================================== -int VVTK_ImageWriter::Quality()const -{ - return myQuality; -} -//=============================================== -// function: SetImageData -// purpose : -//=============================================== -void VVTK_ImageWriter::SetImageData(vtkImageData* pImageData) -{ - myImageData=pImageData; -} -//=============================================== -// function: ImageData -// purpose : -//=============================================== -vtkImageData* VVTK_ImageWriter::ImageData() -{ - return myImageData; -} -//=============================================== -// function: SetConstraint16Flag -// purpose : -//=============================================== -void VVTK_ImageWriter::SetConstraint16Flag(const bool aFlag) -{ - myConstraint16Flag=aFlag; -} -//=============================================== -// function: Constraint16Flag -// purpose : -//=============================================== -bool VVTK_ImageWriter::Constraint16Flag()const -{ - return myConstraint16Flag; -} -//=============================================== -// function: ErrorStatus -// purpose : -//=============================================== -int VVTK_ImageWriter::ErrorStatus()const +#include +#include + +#ifdef _DEBUG_ +static int MYDEBUG = 0; +#else +static int MYDEBUG = 0; +#endif + + +//---------------------------------------------------------------------------- +VVTK_ImageWriter +::VVTK_ImageWriter(QSemaphore* theSemaphore, + vtkImageData* theImageData, + const std::string& theName, + int theProgressive, + int theQuality): + mySemaphore(theSemaphore), + myImageData(theImageData), + myName(theName), + myProgressive(theProgressive), + myQuality(theQuality), + myConstraint16Flag(true) +{} + +//---------------------------------------------------------------------------- +VVTK_ImageWriter +::~VVTK_ImageWriter() { - return myErrorStatus; + if(MYDEBUG) cout<<"VVTK_ImageWriter::~VVTK_ImageWriter - this = "< anImageClip; // - vtkImageData *pImageData=myImageData; - vtkImageClip *pImageClip=NULL; - // - if (myConstraint16Flag){ + if(myConstraint16Flag){ int uExtent[6]; - unsigned int width, height; - // myImageData->GetUpdateExtent(uExtent); - width =uExtent[1]-uExtent[0]+1; - height=uExtent[3]-uExtent[2]+1; - width =(width/16)*16; - height=(height/16)*16; - uExtent[1]=uExtent[0]+width -1; - uExtent[3]=uExtent[2]+height-1; + unsigned int width = uExtent[1] - uExtent[0] + 1; + unsigned int height = uExtent[3] - uExtent[2] + 1; + width = (width / 16) * 16; + height= (height / 16) * 16; + uExtent[1] = uExtent[0] + width - 1; + uExtent[3] = uExtent[2] + height - 1; // - pImageClip=vtkImageClip::New(); - pImageClip->SetInput(myImageData); - pImageClip->SetOutputWholeExtent(uExtent); - pImageClip->ClipDataOn(); - pImageData=pImageClip->GetOutput(); + anImageClip = vtkImageClip::New(); + anImageClip->Delete(); + + anImageClip->SetInput(myImageData); + anImageClip->SetOutputWholeExtent(uExtent); + anImageClip->ClipDataOn(); + anImageData = anImageClip->GetOutput(); } // - pWriter->WriteToMemoryOff(); - pWriter->SetFileName(myName); - pWriter->SetQuality(myQuality); - pWriter->SetProgressive(myProgressive); - pWriter->SetInput(pImageData); - // - pWriter->Write(); - // - if (myConstraint16Flag){ - pImageClip->Delete(); - } - pWriter->Delete(); - delete myName; + aWriter->WriteToMemoryOff(); + aWriter->SetFileName(myName.c_str()); + aWriter->SetQuality(myQuality); + aWriter->SetProgressive(myProgressive); + aWriter->SetInput(anImageData); + aWriter->Write(); + + aWriter->Delete(); myImageData->Delete(); + + if(MYDEBUG) cout<<"VVTK_ImageWriter::run() - this = "< +#include class vtkImageData; +class QSemaphore; -class VVTK_ImageWriter : public QThread { -public: - VVTK_ImageWriter(); - ~VVTK_ImageWriter(); - - void SetName(const char *aName); - const char *Name()const; - - void SetProgressive(const int aProgressive); - int Progressive()const; - - void SetQuality(const int aQuality); - int Quality()const; - void SetImageData(vtkImageData*); - vtkImageData* ImageData(); +class VVTK_ImageWriter : public QThread +{ +public: + VVTK_ImageWriter(QSemaphore* theSemaphore, + vtkImageData* theImageData, + const std::string& theName, + int theProgressive, + int theQuality); - void SetConstraint16Flag(const bool bFlag); - bool Constraint16Flag()const; - - int ErrorStatus()const; + ~VVTK_ImageWriter(); -protected : - virtual void run(); - void WriteSlice(); + protected: + virtual + void + run(); -protected : - char *myName; + protected: + QSemaphore* mySemaphore; + vtkImageData *myImageData; + std::string myName; int myProgressive; int myQuality; - int myErrorStatus; bool myConstraint16Flag; - - vtkImageData *myImageData; - }; #endif diff --git a/src/VVTK/VVTK_ImageWriterMgr.cxx b/src/VVTK/VVTK_ImageWriterMgr.cxx index 0cf7c4cc..d327fcfd 100755 --- a/src/VVTK/VVTK_ImageWriterMgr.cxx +++ b/src/VVTK/VVTK_ImageWriterMgr.cxx @@ -25,151 +25,73 @@ // Author : // Module : // $Header$ + #include "VVTK_ImageWriterMgr.h" +#include "VVTK_ImageWriter.h" -#include -#include #include -#include "VVTK_ImageWriter.h" -//=============================================== -// function: -// purpose : -//=============================================== -VVTK_ImageWriterMgr::VVTK_ImageWriterMgr() - : QThread() -{ - myStop=0; - myErrorStatus=0; -} -//=============================================== -// function: ~ -// purpose : -//=============================================== -VVTK_ImageWriterMgr::~VVTK_ImageWriterMgr() -{ -} -//=============================================== -// function: Stop -// purpose : -//=============================================== -void VVTK_ImageWriterMgr::Stop() -{ - myStop=1; -} -//=============================================== -// function: StartImageWriter -// purpose : -//=============================================== -void VVTK_ImageWriterMgr::StartImageWriter(const char *aName, - const int aProgressive, - const int aQuality, - vtkImageData *pImageData) +#include + +#include + + +#ifdef _DEBUG_ +static int MYDEBUG = 0; +#else +static int MYDEBUG = 0; +#endif + +//---------------------------------------------------------------------------- +VVTK_ImageWriterMgr +::VVTK_ImageWriterMgr() { - VVTK_ImageWriter *pIW=new VVTK_ImageWriter; - // - myMutex.lock(); - myThreads.push_back(pIW); - myMutex.unlock(); - // - pIW->SetName(aName); - pIW->SetQuality(aQuality); - pIW->SetProgressive(aProgressive); - pIW->SetImageData(pImageData); - pIW->start(); + int aMax = std::numeric_limits::max(); + mySemaphore = new QSemaphore(aMax); + *mySemaphore += aMax; } -//=============================================== -// function: run -// purpose : -//=============================================== -void VVTK_ImageWriterMgr::run() + + +//---------------------------------------------------------------------------- +VVTK_ImageWriterMgr +::~VVTK_ImageWriterMgr() { - while (1) { - if (myErrorStatus){ - return; - } - if(myStop) { - CleanAll(); - break; - } - // - msleep(1000); - CleanPartial(10); - } + Stop(); + delete mySemaphore; } -//=============================================== -// function: CleanPartial -// purpose : -//=============================================== -void VVTK_ImageWriterMgr::CleanPartial(const int aNumber) + + +//---------------------------------------------------------------------------- +void +VVTK_ImageWriterMgr +::StartImageWriter(vtkImageData *theImageData, + const std::string& theName, + const int theProgressive, + const int theQuality) { - void *p; - VVTK_ImageWriter* pIW; - // - std::list aList; - std::list ::iterator aIter; - // - while (myMutex.locked()); - // - myMutex.lock(); - // - aIter=myThreads.begin(); - for (; aIter != myThreads.end() ; aIter++ ){ - void *p=*aIter; - pIW=reinterpret_cast (*aIter); - if (pIW->ErrorStatus()){ - myErrorStatus=10; - return; - } - if (pIW->finished()){ - aList.push_back(pIW); - if(aList.size()>aNumber){ - break; - } - } - } - // - aIter=aList.begin(); - for (; aIter != aList.end(); aIter++ ){ - p=*aIter; - myThreads.remove(p); - pIW=reinterpret_cast (p); - vtkImageData* pImageData=pIW->ImageData(); - delete pIW; - } - myMutex.unlock(); + VVTK_ImageWriter *anImageWriter = + new VVTK_ImageWriter(mySemaphore, + theImageData, + theName, + theProgressive, + theQuality); + myThreads.push_back(anImageWriter); + + anImageWriter->start(); + } -//=============================================== -// function: CleanAll -// purpose : -//=============================================== -void VVTK_ImageWriterMgr::CleanAll() + + +//---------------------------------------------------------------------------- +void +VVTK_ImageWriterMgr +::Stop() { - void *p; - VVTK_ImageWriter* pIW; - // - std::list ::iterator aIter; - // - // clean all - while (myMutex.locked()); - myMutex.lock(); - while (myThreads.size()) { - aIter=myThreads.begin(); - for (; aIter != myThreads.end() ; aIter++ ){ - void *p=*aIter; - pIW=reinterpret_cast (*aIter); - if (pIW->ErrorStatus()){ - myErrorStatus=10; - myStop=0; - return; - } - if (pIW->finished()){ - delete pIW; - myThreads.remove(p); - break; - } - } - } - myMutex.unlock(); - myStop=0; + if(MYDEBUG) cout<<"VVTK_ImageWriterMgr::Stop - *mySemaphore -= "< -#include -// +#include +#include + class QString; class vtkImageData; -class QMutex; -// -class VVTK_ImageWriterMgr : public QThread { -public: +class VVTK_ImageWriter; +class QSemaphore; + +class VVTK_ImageWriterMgr +{ + public: VVTK_ImageWriterMgr(); ~VVTK_ImageWriterMgr(); - void Stop(); - void StartImageWriter(const char *aName, - const int aProgressive, - const int aQuality, - vtkImageData *pImageData); - int ErrorStatus()const; - -protected : - virtual void run(); - void CleanAll(); - void CleanPartial(const int aNumber); - -protected : - int myStop; - int myErrorStatus; - std::list myThreads; - QMutex myMutex; + void + StartImageWriter(vtkImageData *theImageData, + const std::string& theName, + const int theProgressive, + const int theQuality); + + void + Stop(); + + protected: + typedef std::vector TWriterThreads; + TWriterThreads myThreads; + + QSemaphore* mySemaphore; }; + + #endif diff --git a/src/VVTK/VVTK_MainWindow.cxx b/src/VVTK/VVTK_MainWindow.cxx index f71f4056..df26f833 100644 --- a/src/VVTK/VVTK_MainWindow.cxx +++ b/src/VVTK/VVTK_MainWindow.cxx @@ -104,11 +104,10 @@ VVTK_MainWindow myStopAction->addTo( myRecordingToolBar ); connect( myStopAction, SIGNAL( activated() ), this, SLOT( OnStopRecording() ) ); - myRecorder=VVTK_Recorder::New(); + myRecorder = VVTK_Recorder::New(); myRecorder->CheckExistAVIMaker(); - if (myRecorder->ErrorStatus()){ + if(myRecorder->ErrorStatus()) myRecordingToolBar->setEnabled(false); - } } //---------------------------------------------------------------------------- @@ -116,20 +115,25 @@ void VVTK_MainWindow ::Initialize(SVTK_RenderWindowInteractor* theInteractor) { - vtkInteractorStyle* pVIS=theInteractor->GetInteractorStyle(); - SVTK_InteractorStyle *pInteractorStyle=dynamic_cast(pVIS); - if (pInteractorStyle){ - pInteractorStyle->SetControllerIncrement(myControllerIncrement.GetPointer()); - pInteractorStyle->SetControllerOnKeyDown(myControllerOnKeyDown.GetPointer()); + vtkInteractorStyle* aStyle = theInteractor->GetInteractorStyle(); + if(SVTK_InteractorStyle *anInteractorStyle = dynamic_cast(aStyle)){ + anInteractorStyle->SetControllerIncrement(myControllerIncrement.GetPointer()); + anInteractorStyle->SetControllerOnKeyDown(myControllerOnKeyDown.GetPointer()); } + + myRecorder->SetNbFPS(17.3); + myRecorder->SetQuality(100); + myRecorder->SetProgressiveMode(true); + myRecorder->SetUseSkippedFrames(true); + myRecorder->SetRenderWindow(theInteractor->getRenderWindow()); + SVTK_MainWindow::Initialize(theInteractor); } VVTK_MainWindow::~VVTK_MainWindow() { - if (myRecorder){ + if(myRecorder) myRecorder->Delete(); - } } @@ -155,26 +159,16 @@ VVTK_MainWindow //---------------------------------------------------------------------------- void VVTK_MainWindow::OnStartRecording() { - //QString aFileName = "/data/pkv/IMAGES/TMP/abc"; - QString aFileName=QFileDialog::getSaveFileName( getenv( "HOME"), "*.avi", this ); - if( aFileName.isNull() ){ + QString aFileName = QFileDialog::getSaveFileName( getenv( "HOME"), "*.avi", this ); + if(aFileName.isNull()) return; - } - // myStartAction->setEnabled( false ); myPlayAction->setEnabled( false ); myPauseAction->setEnabled( true ); myStopAction->setEnabled( true ); - // - //==== - SVTK_RenderWindowInteractor* pInteractor=GetInteractor(); - vtkRenderWindow* pRenderWindow=pInteractor->getRenderWindow(); - // + myRecorder->SetName(aFileName.latin1()); - myRecorder->SetNbFPS(10); - myRecorder->SetRenderWindow(pRenderWindow); - // myRecorder->Record(); } diff --git a/src/VVTK/VVTK_Recorder.cxx b/src/VVTK/VVTK_Recorder.cxx index 8781e936..f4aab20a 100755 --- a/src/VVTK/VVTK_Recorder.cxx +++ b/src/VVTK/VVTK_Recorder.cxx @@ -40,166 +40,216 @@ #include #include +#include +#include +#include + #include +#include #include +#include "utilities.h" -static - void GetNameJPEG(const char *pName, - const int aIndex, - char *pNameJPEG); -// +#ifdef _DEBUG_ +static int MYDEBUG = 0; +#else +static int MYDEBUG = 0; +#endif + + +namespace +{ + //---------------------------------------------------------------------------- + inline + void + GetNameJPEG(const std::string& thePreffix, + const int theIndex, + std::string& theName) + { + using namespace std; + ostringstream aStream; + aStream<SetClientData(this); myCommand->SetCallback(VVTK_Recorder::ProcessEvents); - // - myPriority=0.; - myTimeStart=0.; - myFrameIndex=0; - myPaused=0; - // - myFilter=vtkWindowToImageFilter::New(); - myWriterMgr=new VVTK_ImageWriterMgr; - myName=new char [512]; - myName[0]=0; - myNbWrittenFrames=0; - // - myNameAVIMaker=new char[32]; - strcpy (myNameAVIMaker, "jpeg2yuv"); } -//=============================================== -// function: ~ -// purpose : -//=============================================== -VVTK_Recorder::~VVTK_Recorder() + + +//---------------------------------------------------------------------------- +VVTK_Recorder +::~VVTK_Recorder() { myCommand->Delete(); myFilter->Delete(); delete myWriterMgr; - delete myName; - delete myNameAVIMaker; } -//=============================================== -// function: CheckExistAVIMaker -// purpose : -//=============================================== -void VVTK_Recorder::CheckExistAVIMaker() + + +//---------------------------------------------------------------------------- +void +VVTK_Recorder +::CheckExistAVIMaker() { - int iErr, iES; - // - myErrorStatus=0; - std::string anAVIMakeCheck("which "); - anAVIMakeCheck += myNameAVIMaker; - anAVIMakeCheck += " >& /dev/null"; - iErr=system(anAVIMakeCheck.c_str()); - iErr=WEXITSTATUS(iErr); - if (iErr==127){ - myErrorStatus=iErr; - } + myErrorStatus = 0; + using namespace std; + ostringstream aStream; + aStream<<"which "<& /dev/null"; + std::string anAVIMakeCheck = aStream.str(); + int iErr = system(anAVIMakeCheck.c_str()); + if(iErr != 0) + myErrorStatus = 127; } -//=============================================== -// function: DoPlay -// purpose : -//=============================================== -void VVTK_Recorder::DoPlay() + + +//---------------------------------------------------------------------------- +void +VVTK_Recorder +::SetName(const char* theName) { + myName = theName; } -//=============================================== -// function: DoStop -// purpose : -//=============================================== -void VVTK_Recorder::DoStop() + +const char* +VVTK_Recorder::Name() const { + return myName.c_str(); } -//=============================================== -// function: SetName -// purpose : -//=============================================== -void VVTK_Recorder::SetName(const char* theName) + + +//---------------------------------------------------------------------------- +void +VVTK_Recorder +::SetNbFPS(const double theNbFPS) { - strcpy(myName, theName); + myNbFPS = theNbFPS; } -//=============================================== -// function: Name -// purpose : -//=============================================== -const char* VVTK_Recorder::Name()const + +double +VVTK_Recorder +::NbFPS() const { - return myName; + return myNbFPS; } -//=============================================== -// function: SetNbFPS -// purpose : -//=============================================== -void VVTK_Recorder::SetNbFPS(const double theNb) + + +//---------------------------------------------------------------------------- +void +VVTK_Recorder +::SetQuality(int theQuality) { - myNbFPS=theNb; + myQuality = theQuality; } -//=============================================== -// function: NbFPS -// purpose : -//=============================================== -double VVTK_Recorder::NbFPS()const + +int +VVTK_Recorder +::GetQuality() const { - return myNbFPS; + return myQuality; } -//=============================================== -// function: SetRenderWindow -// purpose : -//=============================================== -void VVTK_Recorder::SetRenderWindow(vtkRenderWindow* pW) + + +//---------------------------------------------------------------------------- +void +VVTK_Recorder +::SetRenderWindow(vtkRenderWindow* theRenderWindow) { - myRenderWindow=pW; + myRenderWindow = theRenderWindow; } -//=============================================== -// function: RenderWindow -// purpose : -//=============================================== -vtkRenderWindow* VVTK_Recorder::RenderWindow() + +vtkRenderWindow* +VVTK_Recorder +::RenderWindow() { return myRenderWindow; } -//=============================================== -// function: ErrorStatus -// purpose : -//=============================================== -int VVTK_Recorder::ErrorStatus()const + + +//---------------------------------------------------------------------------- +void +VVTK_Recorder +::SetProgressiveMode(bool theProgressiveMode) +{ + myProgressiveMode = theProgressiveMode; +} + +bool +VVTK_Recorder +::GetProgressiveMode() const +{ + return myProgressiveMode; +} + + +//---------------------------------------------------------------------------- +void +VVTK_Recorder +::SetUseSkippedFrames(bool theUseSkippedFrames) +{ + myUseSkippedFrames = theUseSkippedFrames; +} + +bool +VVTK_Recorder +::UseSkippedFrames() const +{ + return myUseSkippedFrames; +} + + +//---------------------------------------------------------------------------- +int +VVTK_Recorder +::ErrorStatus() const { return myErrorStatus; } -//=============================================== -// function: State -// purpose : -//=============================================== -int VVTK_Recorder::State()const + +int +VVTK_Recorder +::State() const { return myState; } -//=============================================== -// function: ProcessEvents -// purpose : -//=============================================== -void VVTK_Recorder::ProcessEvents(vtkObject* vtkNotUsed(theObject), - unsigned long theEvent, - void* theClientData, - void* vtkNotUsed(theCallData)) + + +//---------------------------------------------------------------------------- +void +VVTK_Recorder +::ProcessEvents(vtkObject* vtkNotUsed(theObject), + unsigned long theEvent, + void* theClientData, + void* vtkNotUsed(theCallData)) { - if(vtkObject* anObj=reinterpret_cast(theClientData)){ - if(VVTK_Recorder* aSelf=dynamic_cast(anObj)){ + if(vtkObject* anObj = reinterpret_cast(theClientData)){ + if(VVTK_Recorder* aSelf = dynamic_cast(anObj)){ if(theEvent==vtkCommand::EndEvent){ if(aSelf->State() == VVTK_Recorder::VVTK_Recorder_Record){ aSelf->DoRecord(); @@ -209,21 +259,18 @@ void VVTK_Recorder::ProcessEvents(vtkObject* vtkNotUsed(theObject), } } -//=============================================== -// function: Record -// purpose : -//=============================================== -void VVTK_Recorder::Record() + +//---------------------------------------------------------------------------- +void +VVTK_Recorder +::Record() { - if (myState==VVTK_Recorder_Stop){ - if (myRenderWindow){ - myState=VVTK_Recorder_Record; - // + if(myState == VVTK_Recorder_Stop){ + if(myRenderWindow){ + myState = VVTK_Recorder_Record; myFilter->SetInput(myRenderWindow); - // - myFrameIndex=-1; - myNbWrittenFrames=0; - // + myFrameIndex = -1; + myNbWrittenFrames = 0; myRenderWindow->RemoveObserver(myCommand); myRenderWindow->AddObserver(vtkCommand::EndEvent, myCommand, @@ -232,146 +279,175 @@ void VVTK_Recorder::Record() } } } -//=============================================== -// function: Play -// purpose : -//=============================================== -void VVTK_Recorder::Play() -{ - if (myState==VVTK_Recorder_Stop){ - if (myRenderWindow){ - myState=VVTK_Recorder_Play; - } - } -} -//=============================================== -// function: Stop -// purpose : -//=============================================== -void VVTK_Recorder::Stop() + + +//---------------------------------------------------------------------------- +void +VVTK_Recorder +::Stop() { - if (myState==VVTK_Recorder_Record) { + QApplication::setOverrideCursor( Qt::waitCursor ); + + if(myState == VVTK_Recorder_Record){ + if(!myPaused) + DoRecord(); + myWriterMgr->Stop(); - AddSkippedFrames(); + + if(myUseSkippedFrames) + AddSkippedFrames(); + + myFrameIndexes.clear(); + MakeFileAVI(); } - if (myState==VVTK_Recorder_Play){ - //... - } - myState=VVTK_Recorder_Stop; + myState = VVTK_Recorder_Stop; + myPaused = 0; + + QApplication::restoreOverrideCursor(); } -//=============================================== -// function: Pause -// purpose : -//=============================================== -void VVTK_Recorder::Pause() + + +//---------------------------------------------------------------------------- +void +VVTK_Recorder +::Pause() { - myPaused=myPaused ? 0 : 1; + myPaused = myPaused ? 0 : 1; + if(myPaused && !myFrameIndexes.empty()){ + size_t aLastId = myFrameIndexes.size() - 1; + myFrameIndexes[aLastId] *= -1; + } } -//=============================================== -// function: DoRecord -// purpose : -//=============================================== -void VVTK_Recorder::DoRecord() + + +//---------------------------------------------------------------------------- +void +VVTK_Recorder +::DoRecord() { - if (myPaused){ + if(myPaused) return; - } - // - char *buf, aPrefix[512]; - double aTimeNow, dT; - int aFrameIndex, aQuality, aProgressive; - // - if (myFrameIndex<0) { - myFrameIndex=0; - myTimeStart=vtkTimerLog::GetCurrentTime(); - // - myWriterMgr->start(); - // - } - else { - aTimeNow=vtkTimerLog::GetCurrentTime(); - dT=aTimeNow-myTimeStart; - if (dT<0.){ - //printf(" * DoRecord() dT<0 returned\n"); + + if(myFrameIndex < 0){ + myFrameIndex = 0; + myTimeStart = vtkTimerLog::GetCurrentTime(); + }else{ + double aTimeNow = vtkTimerLog::GetCurrentTime(); + double aDelta = aTimeNow - myTimeStart; + if(aDelta < 0.0) return; - } - aFrameIndex=(int)(dT*myNbFPS); - if (aFrameIndex==myFrameIndex) { + + int aFrameIndex = int(aDelta*myNbFPS); + if(aFrameIndex == myFrameIndex) return; - } - myFrameIndex=aFrameIndex; + + myFrameIndex = aFrameIndex; } - // + + myFrameIndexes.push_back(myFrameIndex); + if(MYDEBUG) cout<<"VVTK_Recorder::DoRecord - myFrameIndex = "<RemoveObserver(myCommand); - // myFilter->Modified(); - // - buf=new char [512]; - GetNameJPEG(myName, myFrameIndex, buf); - //printf(" *buf: %s\n", buf); - // + + std::string aName; + GetNameJPEG(myName,myFrameIndex,aName); + PreWrite(); - //=============== - // - aQuality=95; - aProgressive=1; - vtkImageData *pC=vtkImageData::New(); - pC->DeepCopy(myFilter->GetOutput()); - // - myWriterMgr->StartImageWriter(buf, aProgressive, aQuality, pC); - ++myNbWrittenFrames; - // - //=============== + + vtkImageData *anImageData = vtkImageData::New(); + anImageData->DeepCopy(myFilter->GetOutput()); + + myWriterMgr->StartImageWriter(anImageData,aName,myProgressiveMode,myQuality); + myNbWrittenFrames++; + myRenderWindow->AddObserver(vtkCommand::EndEvent, myCommand, myPriority); } -//=============================================== -// function: PreWrite -// purpose : -//=============================================== -void VVTK_Recorder::PreWrite() + + +//---------------------------------------------------------------------------- +void +VVTK_Recorder +::PreWrite() { - vtkImageData *pImageData=myFilter->GetOutput(); + vtkImageData *anImageData = myFilter->GetOutput(); // - if (!pImageData){ - myErrorStatus=20; + if(!anImageData){ + myErrorStatus = 20; return; } - pImageData->UpdateInformation(); - int *wExtent=pImageData->GetWholeExtent(); - pImageData->SetUpdateExtent(wExtent[0], wExtent[1], - wExtent[2], wExtent[3], - 0,0); - pImageData->UpdateData(); + anImageData->UpdateInformation(); + int *anExtent = anImageData->GetWholeExtent(); + anImageData->SetUpdateExtent(anExtent[0], anExtent[1], + anExtent[2], anExtent[3], + 0,0); + anImageData->UpdateData(); } -//=============================================== -// function: AddSkippedFrames -// purpose : -//=============================================== -void VVTK_Recorder::AddSkippedFrames() + + +//---------------------------------------------------------------------------- +void +VVTK_Recorder +::AddSkippedFrames() { - myErrorStatus=0; - //... + myErrorStatus = 0; + + if(myFrameIndexes.size() < 2) + return; + + size_t anId = 0, anEnd = myFrameIndexes.size() - 1; + for(; anId < anEnd; anId++){ + int aStartIndex = myFrameIndexes[anId]; + if(aStartIndex < 0) + continue; + + int aFinishIndex = abs(myFrameIndexes[anId + 1]); + if(aStartIndex + 1 == aFinishIndex) + continue; + + std::string anInitialName; + std::ostringstream aStream; + GetNameJPEG(myName,aStartIndex,anInitialName); + for(int anIndex = aStartIndex + 1; anIndex < aFinishIndex; anIndex++){ + myNbWrittenFrames++; + std::string anCurrentName; + GetNameJPEG(myName,anIndex,anCurrentName); + aStream<<"ln -s "<< anInitialName<<" "< +#include +#include + #include class vtkRenderWindow; @@ -37,71 +40,124 @@ class vtkCallbackCommand; class vtkWindowToImageFilter; class VVTK_ImageWriterMgr; // -class VVTK_Recorder : public vtkObject { - -protected: +class VVTK_Recorder : public vtkObject +{ + protected: enum State { VVTK_Recorder_Unknown=0, VVTK_Recorder_Record, - VVTK_Recorder_Play, VVTK_Recorder_Stop }; - -public: + + public: static VVTK_Recorder *New(); vtkTypeRevisionMacro(VVTK_Recorder,vtkObject); - // - void SetRenderWindow(vtkRenderWindow* pW); - vtkRenderWindow* RenderWindow(); - // - void SetName(const char *theName); - const char* Name()const; - // - void SetNbFPS(const double theNbFPS); - double NbFPS()const; - // - void Record(); - void Play(); - void Pause(); - void Stop(); - // - int State()const; - int ErrorStatus()const; - - static void ProcessEvents(vtkObject* theObject, - unsigned long theEvent, - void* theClientData, - void* theCallData); - - void CheckExistAVIMaker(); - // + + void + SetRenderWindow(vtkRenderWindow* theRenderWindow); + + vtkRenderWindow* + RenderWindow(); + + void + SetName(const char *theName); + + const char* + Name() const; + + void + SetNbFPS(const double theNbFPS); + + double + NbFPS() const; + + void + SetQuality(int theQuality); + + int + GetQuality() const; + + void + SetProgressiveMode(bool theProgressiveMode); + + bool + GetProgressiveMode() const; + + void + SetUseSkippedFrames(bool theUseSkippedFrames); + + bool + UseSkippedFrames() const; + + void + Record(); + + void + Pause(); + + void + Stop(); + + int + State() const; + + int + ErrorStatus() const; + + void + CheckExistAVIMaker(); + protected : VVTK_Recorder(); + ~VVTK_Recorder(); - void DoRecord(); - void DoPlay(); - void DoStop(); - void MakeFileAVI(); - void AddSkippedFrames(); - void PreWrite(); - // + + void + DoRecord(); + + void + MakeFileAVI(); + + void + AddSkippedFrames(); + + void + PreWrite(); + + static + void + ProcessEvents(vtkObject* theObject, + unsigned long theEvent, + void* theClientData, + void* theCallData); + protected : - int myState; - int myPaused; - double myNbFPS; - int myErrorStatus; - int myFrameIndex; - int myNbWrittenFrames; - float myPriority; - double myTimeStart; - char *myName; - - vtkCallbackCommand *myCommand; - vtkRenderWindow *myRenderWindow; + int myState; + int myPaused; + int myErrorStatus; + + float myPriority; + double myTimeStart; + + int myFrameIndex; + int myNbWrittenFrames; + + double myNbFPS; + int myQuality; + bool myProgressiveMode; + + typedef std::vector TFrameIndexes; + TFrameIndexes myFrameIndexes; + bool myUseSkippedFrames; + + std::string myName; + std::string myNameAVIMaker; + + vtkCallbackCommand *myCommand; + vtkRenderWindow *myRenderWindow; vtkWindowToImageFilter *myFilter; - VVTK_ImageWriterMgr *myWriterMgr; + VVTK_ImageWriterMgr *myWriterMgr; - char *myNameAVIMaker; private: VVTK_Recorder(const VVTK_Recorder&); //Not implemented -- 2.39.2