Salome HOME
updated copyright message
[modules/gui.git] / src / SVTK / SVTK_Recorder.cxx
old mode 100755 (executable)
new mode 100644 (file)
index 52b08a1..1be7fe3
@@ -1,24 +1,22 @@
-//  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2023  CEA/DEN, EDF R&D, OPEN CASCADE
 //
-//  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
-//  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
 //
-//  This library is free software; you can redistribute it and/or
-//  modify it under the terms of the GNU Lesser General Public
-//  License as published by the Free Software Foundation; either
-//  version 2.1 of the License.
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
 //
-//  This library is distributed in the hope that it will be useful,
-//  but WITHOUT ANY WARRANTY; without even the implied warranty of
-//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-//  Lesser General Public License for more details.
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-//  You should have received a copy of the GNU Lesser General Public
-//  License along with this library; if not, write to the Free Software
-//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
-//
-//  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
+
 #include "SVTK_Recorder.h"
 
 #include "SVTK_ImageWriter.h"
@@ -36,6 +34,7 @@
 #include <sstream>
 #include <iomanip>
 #include <iostream>
+#include <math.h>
 
 #ifndef WIN32
 #include <unistd.h>
 #include <QFileInfo>
 #include <QDir>
 
-//#include "utilities.h"
-
-#ifdef _DEBUG_
-static int MYDEBUG = 0;
-#else
-static int MYDEBUG = 0;
-#endif
+#include "utilities.h"
 
 
 namespace
@@ -59,43 +52,41 @@ namespace
   //----------------------------------------------------------------------------
   inline
   void
-  GetNameJPEG(const std::string& thePreffix,  
-             const int theIndex,
-             std::string& theName)
+  GetNameJPEG(const std::string& thePreffix,
+              const int theIndex,
+              std::string& theName)
   {
-    using namespace std;
-    ostringstream aStream;
+    std::ostringstream aStream;
     aStream<<thePreffix<<"_"<<setw(6)<<setfill('0')<<theIndex<<".jpeg";
     theName = aStream.str();
   }
 }
 
 //----------------------------------------------------------------------------
-vtkCxxRevisionMacro(SVTK_Recorder,"$Revision$");
-vtkStandardNewMacro(SVTK_Recorder);
+vtkStandardNewMacro(SVTK_Recorder)
 
 
 //----------------------------------------------------------------------------
 SVTK_Recorder
 ::SVTK_Recorder():
-  myRenderWindow(NULL),
   myState(SVTK_Recorder_Stop),
+  myPaused(0),
+  myErrorStatus(0),
+  myPriority(0.0),
+  myTimeStart(0.0),
+  myFrameIndex(0),
+  myNbWrittenFrames(0),
   myNbFPS(5.5),
   myQuality(100),
   myProgressiveMode(true),
   myUseSkippedFrames(true),
-  myErrorStatus(0),
+  myNameAVIMaker("jpeg2yuv"),
   myCommand(vtkCallbackCommand::New()),
-  myPriority(0.0),
-  myTimeStart(0.0),
-  myFrameIndex(0),
-  myPaused(0),
+  myRenderWindow(NULL),
   myFilter(vtkWindowToImageFilter::New()),
-  myWriterMgr(new SVTK_ImageWriterMgr),
-  myNbWrittenFrames(0),
-  myNameAVIMaker("jpeg2yuv")
+  myWriterMgr(new SVTK_ImageWriterMgr)
 {
-  myCommand->SetClientData(this); 
+  myCommand->SetClientData(this);
   myCommand->SetCallback(SVTK_Recorder::ProcessEvents);
 }
 
@@ -116,9 +107,12 @@ SVTK_Recorder
 ::CheckExistAVIMaker()
 {
   myErrorStatus = 0;
-  using namespace std;
-  ostringstream aStream;
-  aStream<<"which "<<myNameAVIMaker<<" >& /dev/null";
+  std::ostringstream aStream;
+#ifndef WIN32
+  aStream<<"which "<<myNameAVIMaker<<" 2> /dev/null";
+#else
+  aStream<<"setlocal & set P2=.;%PATH% & (for %e in (%PATHEXT%) do @for %i in ("<<myNameAVIMaker<<"%e) do @if NOT \"%~$P2:i\"==\"\" exit /b 0) & exit /b 1";
+#endif
   std::string anAVIMakeCheck = aStream.str();
   int iErr = system(anAVIMakeCheck.c_str());
   if(iErr != 0)
@@ -134,7 +128,7 @@ SVTK_Recorder
   myName = theName;
 }
 
-const char* 
+const char*
 SVTK_Recorder::Name() const
 {
   return myName.c_str();
@@ -174,14 +168,14 @@ SVTK_Recorder
 
 
 //----------------------------------------------------------------------------
-void 
+void
 SVTK_Recorder
 ::SetRenderWindow(vtkRenderWindow* theRenderWindow)
 {
   myRenderWindow = theRenderWindow;
 }
 
-vtkRenderWindow* 
+vtkRenderWindow*
 SVTK_Recorder
 ::RenderWindow()
 {
@@ -240,17 +234,17 @@ SVTK_Recorder
 //----------------------------------------------------------------------------
 void
 SVTK_Recorder
-::ProcessEvents(vtkObject* vtkNotUsed(theObject), 
-               unsigned long theEvent,
-               void* theClientData, 
-               void* vtkNotUsed(theCallData))
+::ProcessEvents(vtkObject* vtkNotUsed(theObject),
+                unsigned long theEvent,
+                void* theClientData,
+                void* vtkNotUsed(theCallData))
 {
-  if(vtkObject* anObj = reinterpret_cast<vtkObject*>(theClientData)){ 
+  if(vtkObject* anObj = reinterpret_cast<vtkObject*>(theClientData)){
     if(SVTK_Recorder* aSelf = dynamic_cast<SVTK_Recorder*>(anObj)){
       if(theEvent==vtkCommand::EndEvent){
-       if(aSelf->State() == SVTK_Recorder::SVTK_Recorder_Record){
-         aSelf->DoRecord();
-       }
+        if(aSelf->State() == SVTK_Recorder::SVTK_Recorder_Record){
+          aSelf->DoRecord();
+        }
       }
     }
   }
@@ -285,7 +279,7 @@ SVTK_Recorder
 {
   QApplication::setOverrideCursor( Qt::WaitCursor );
 
-  if(myState == SVTK_Recorder_Record){ 
+  if(myState == SVTK_Recorder_Record){
     if(!myPaused)
       DoRecord();
 
@@ -313,18 +307,20 @@ SVTK_Recorder
   myPaused = myPaused ? 0 : 1;
   if(myPaused && !myFrameIndexes.empty()){
     myFrameIndexes.back() *= -1;
-    if(MYDEBUG) cout<<"SVTK_Recorder::Pause - myFrameIndexes.back() = "<<myFrameIndexes.back()<<endl;
+
+    if(SALOME::VerbosityActivated())
+      cout << "SVTK_Recorder::Pause - myFrameIndexes.back() = " << myFrameIndexes.back() << endl;
   }
 }
 
 
 //----------------------------------------------------------------------------
-inline 
+inline
 int
 GetFrameIndex(double theStartTime,
-             double theFPS)
+              double theFPS)
 {
-  double aTimeNow = vtkTimerLog::GetCurrentTime();
+  double aTimeNow = vtkTimerLog::GetUniversalTime();
   double aDelta = aTimeNow - theStartTime;
   return int(aDelta*theFPS);
 }
@@ -338,7 +334,7 @@ SVTK_Recorder
 
   if(myFrameIndex < 0){
     myFrameIndex = 0;
-    myTimeStart = vtkTimerLog::GetCurrentTime();
+    myTimeStart = vtkTimerLog::GetUniversalTime();
   }else{
     int aFrameIndex = GetFrameIndex(myTimeStart,myNbFPS);
     if(aFrameIndex <= myFrameIndex)
@@ -349,9 +345,11 @@ SVTK_Recorder
     if(aLastFrameIndex < 0){
       myFrameIndexes.back() = abs(myFrameIndexes.back());
       double aPauseTime = fabs((double)(aFrameIndex - myFrameIndex - 1)) / myNbFPS;
-      if(MYDEBUG) 
-       cout<<"SVTK_Recorder::DoRecord - aFrameIndex = "<<aFrameIndex<<
-         "; aPauseTime = "<<aPauseTime<<endl;
+
+      if(SALOME::VerbosityActivated())
+        cout << "SVTK_Recorder::DoRecord - aFrameIndex = " << aFrameIndex <<
+        "; aPauseTime = " << aPauseTime << endl;
+
       myTimeStart += aPauseTime;
     }
 
@@ -363,7 +361,8 @@ SVTK_Recorder
   }
 
   myFrameIndexes.push_back(myFrameIndex);
-  if(MYDEBUG) cout<<"SVTK_Recorder::DoRecord - myFrameIndex = "<<myFrameIndex<<endl;
+  if(SALOME::VerbosityActivated())
+    cout << "SVTK_Recorder::DoRecord - myFrameIndex = " << myFrameIndex << endl;
 
   myRenderWindow->RemoveObserver(myCommand);
   myFilter->Modified();
@@ -373,10 +372,10 @@ SVTK_Recorder
 
   PreWrite();
 
-  vtkImageData *anImageData = vtkImageData::New(); 
+  vtkImageData *anImageData = vtkImageData::New();
   anImageData->DeepCopy(myFilter->GetOutput());
 
-  myWriterMgr->StartImageWriter(anImageData,aName,myProgressiveMode,myQuality);
+  myWriterMgr->StartImageWriter(myFilter,anImageData,aName,myProgressiveMode,myQuality);
   myNbWrittenFrames++;
 
   myRenderWindow->AddObserver(vtkCommand::EndEvent,
@@ -396,12 +395,8 @@ SVTK_Recorder
     myErrorStatus = 20;
     return;
   }
-  anImageData->UpdateInformation();
-  int *anExtent = anImageData->GetWholeExtent();
-  anImageData->SetUpdateExtent(anExtent[0], anExtent[1],
-                              anExtent[2], anExtent[3],
-                              0,0);
-  anImageData->UpdateData();
+  myFilter->UpdateInformation();
+  myFilter->UpdateWholeExtent();
 }
 
 
@@ -432,14 +427,25 @@ SVTK_Recorder
       myNbWrittenFrames++;
       std::string anCurrentName;
       GetNameJPEG(myName,anIndex,anCurrentName);
+  #ifndef WIN32
       aStream<<"ln -s "<< anInitialName<<" "<<anCurrentName<<";";
-      if(anIndex + 1 < aFinishIndex)
-       aStream<<" \\";
-      aStream<<endl;
+  #else
+      aStream<<"COPY /Y "<<QString::fromStdString(anInitialName).replace("/","\\\\").toStdString()<<
+                 " "<<QString::fromStdString(anCurrentName).replace("/","\\\\").toStdString()<<" > NUL";
+  #endif
+      if(anIndex + 1 < aFinishIndex) {
+  #ifndef WIN32
+        aStream<<" \\";
+        aStream<<endl;
+  #else
+        aStream<<" & ";
+  #endif
+      }
     }
     std::string aString(aStream.str());
     system(aString.c_str());
-    if(MYDEBUG) cout<<"SVTK_Recorder::AddSkippedFrames - "<<aString<<endl;
+    if(SALOME::VerbosityActivated())
+      cout << "SVTK_Recorder::AddSkippedFrames - " << aString << endl;
   }
 }
 
@@ -457,25 +463,37 @@ SVTK_Recorder
     //" -f "<<int(myNbFPS)<<" "<<
     " -f "<<myNbFPS<<" "<<
     " -n "<<myNbWrittenFrames<<" "<<
-    " -j "<<myName<<"_\%06d.jpeg "<<
-    "| yuv2lav"<<
-    " -o "<<myName;
-   
+    " -j \""<<myName<<"_%06d.jpeg\" "<<
+    "| yuv2lav"<<" -o \""<<myName<<"\"";
+#ifdef WIN32
+  aStream<<" -f aA";
+#endif
   std::string aString(aStream.str());
   myErrorStatus = system(aString.c_str());
 
-  if(MYDEBUG) cout<<"SVTK_Recorder::MakeFileAVI - "<<aString<<endl;
+  if(SALOME::VerbosityActivated())
+    cout << "SVTK_Recorder::MakeFileAVI - " << aString << endl;
 
   QFileInfo aFileInfo(myName.c_str());
   QString aDirPath = aFileInfo.absoluteDir().path();
   QString aBaseName = aFileInfo.fileName();
-  QString aCommand = 
-    QString("(cd ") + aDirPath + 
+  QString aCommand;
+#ifndef WIN32
+  aCommand = QString("(cd ") + aDirPath +
     "; ls " +
     " | egrep '" + aBaseName + "_[0-9]*.jpeg'" +
     " | xargs rm " +
     ")";
+#else
+  QString tmpFile = QString("_") + aBaseName + "_tempfile";
+  QString diskName = aDirPath.split("/")[0];
+  aCommand = diskName + " && (cd " + aDirPath.replace("/","\\\\") +
+       " && ((dir /b | findstr " + aBaseName + "_[0-9]*.jpeg > " + tmpFile +
+       ") & (for /f %i in (" + tmpFile + ") do (del \"%i\")) & (del " + tmpFile + "))) > NUL";
+#endif
 
-  if(MYDEBUG) cout<<"SVTK_Recorder::MakeFileAVI - "<<(const char*)aCommand.toLatin1()<<endl;
-  system((const char*)aCommand.toLatin1());
+  if(SALOME::VerbosityActivated())
+    cout << "SVTK_Recorder::MakeFileAVI - " << (const char*)aCommand.toUtf8() << endl;
+    
+  system((const char*)aCommand.toUtf8());
 }