File: vtkSMAnimationSceneImageWriter.cxx

package info (click to toggle)
paraview 5.4.1%2Bdfsg4-3.1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 218,616 kB
  • sloc: cpp: 2,331,508; ansic: 322,365; python: 111,051; xml: 79,203; tcl: 47,013; yacc: 4,877; java: 4,438; perl: 3,238; sh: 2,920; lex: 1,908; f90: 748; makefile: 273; pascal: 228; objc: 83; fortran: 31
file content (276 lines) | stat: -rw-r--r-- 7,814 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*=========================================================================

  Program:   ParaView
  Module:    vtkSMAnimationSceneImageWriter.cxx

  Copyright (c) Kitware, Inc.
  All rights reserved.
  See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
#include "vtkSMAnimationSceneImageWriter.h"

#include "vtkErrorCode.h"
#include "vtkGenericMovieWriter.h"
#include "vtkImageData.h"
#include "vtkImageWriter.h"
#include "vtkJPEGWriter.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkPNGWriter.h"
#include "vtkPVConfig.h"
#include "vtkSMAnimationScene.h"
#include "vtkTIFFWriter.h"
#include "vtkToolkits.h"

#ifdef VTK_USE_MPEG2_ENCODER
#include "vtkMPEG2Writer.h"
#endif

#include <algorithm>
#include <string>
#include <vtksys/SystemTools.hxx>

#ifdef _WIN32
#include "vtkAVIWriter.h"
#endif
#ifdef PARAVIEW_ENABLE_FFMPEG
#include "vtkFFMPEGWriter.h"
#endif

#include "vtkIOMovieConfigure.h" // for VTK_HAS_OGGTHEORA_SUPPORT
#ifdef VTK_HAS_OGGTHEORA_SUPPORT
#include "vtkOggTheoraWriter.h"
#endif

//-----------------------------------------------------------------------------
vtkSMAnimationSceneImageWriter::vtkSMAnimationSceneImageWriter()
  : Quality(100)
  , FileCount(0)
  , ErrorCode(vtkErrorCode::NoError)
  , FrameRate(1.0)
  , MovieWriterStarted(false)
{
}

//-----------------------------------------------------------------------------
vtkSMAnimationSceneImageWriter::~vtkSMAnimationSceneImageWriter()
{
}

//-----------------------------------------------------------------------------
bool vtkSMAnimationSceneImageWriter::SaveInitialize(int startCount)
{
  // Create writers.
  if (!this->CreateWriter())
  {
    return false;
  }

  if (this->MovieWriter)
  {
    this->MovieWriter->SetFileName(this->FileName);
  }

  // Animation scene call render on each tick. We override that render call
  // since it's a waste of rendering, the code to save the images will call
  // render anyways.
  this->AnimationScene->SetOverrideStillRender(1);

  this->FileCount = startCount;
  return true;
}

//-----------------------------------------------------------------------------
bool vtkSMAnimationSceneImageWriter::SaveFrame(double vtkNotUsed(time))
{
  vtkSmartPointer<vtkImageData> frame = this->CaptureFrame();
  if (!frame)
  {
    // skip empty frames.
    return true;
  }
  if (this->ImageWriter)
  {
    char number[1024];
    sprintf(number, ".%04d", this->FileCount);
    std::string filename = this->Prefix;
    filename = filename + number + this->Suffix;
    this->ImageWriter->SetInputData(frame);
    this->ImageWriter->SetFileName(filename.c_str());
    this->ImageWriter->Write();
    this->ImageWriter->SetInputData(0);
    this->ErrorCode = this->ImageWriter->GetErrorCode();
    this->FileCount =
      (this->ErrorCode == vtkErrorCode::NoError) ? this->FileCount + 1 : this->FileCount;
  }
  else if (this->MovieWriter)
  {
    this->MovieWriter->SetInputData(frame);
    if (!this->MovieWriterStarted)
    {
      this->MovieWriter->Start();
      this->MovieWriterStarted = true;
    }
    this->MovieWriter->Write();
    this->MovieWriter->SetInputData(0);

    int alg_error = this->MovieWriter->GetErrorCode();
    int movie_error = this->MovieWriter->GetError();

    if (movie_error && !alg_error)
    {
      // An error that the moviewriter caught, without setting any error code.
      // vtkGenericMovieWriter::GetStringFromErrorCode will result in
      // Unassigned Error. If this happens the Writer should be changed to set
      // a meaningful error code.

      this->ErrorCode = vtkErrorCode::UserError;
    }
    else
    {
      // if 0, then everything went well

      //< userError, means a vtkAlgorithm error (see vtkErrorCode.h)
      //= userError, means an unknown Error (Unassigned error)
      //> userError, means a vtkGenericMovieWriter error

      this->ErrorCode = alg_error;
    }
  }
  return this->ErrorCode == vtkErrorCode::NoError;
}

//-----------------------------------------------------------------------------
bool vtkSMAnimationSceneImageWriter::SaveFinalize()
{
  this->AnimationScene->SetOverrideStillRender(0);

  // TODO: If save failed, we must remove the partially
  // written files.
  if (this->MovieWriter && this->MovieWriterStarted)
  {
    this->MovieWriter->End();
  }

  this->MovieWriter = NULL;
  this->ImageWriter = NULL;
  return true;
}

//-----------------------------------------------------------------------------
bool vtkSMAnimationSceneImageWriter::CreateWriter()
{
  this->MovieWriterStarted = false;
  this->ImageWriter = NULL;
  this->MovieWriter = NULL;

  vtkSmartPointer<vtkImageWriter> iwriter;
  vtkSmartPointer<vtkGenericMovieWriter> mwriter;

  std::string extension = vtksys::SystemTools::GetFilenameLastExtension(this->FileName);
  if (extension == ".jpg" || extension == ".jpeg")
  {
    iwriter = vtkSmartPointer<vtkJPEGWriter>::New();
  }
  else if (extension == ".tif" || extension == ".tiff")
  {
    iwriter = vtkSmartPointer<vtkTIFFWriter>::New();
  }
  else if (extension == ".png")
  {
    int pngQuality = (9 * (100 - this->Quality)) / 100;
    vtkNew<vtkPNGWriter> pngwriter;
    pngwriter->SetCompressionLevel(pngQuality);

    iwriter = pngwriter.Get();
  }
#ifdef VTK_USE_MPEG2_ENCODER
  else if (extension == ".mpeg" || extension == ".mpg")
  {
    mwriter = vtkSmartPointer<vtkMPEG2Writer>::New();
  }
#endif
#ifdef PARAVIEW_ENABLE_FFMPEG
  else if (extension == ".avi")
  {
    vtkNew<vtkFFMPEGWriter> aviwriter;
    double quality = 3 * this->Quality / 100.0;
    if (quality > 2)
    {
      aviwriter->SetCompression(0);
    }
    else
    {
      aviwriter->SetCompression(1);
      aviwriter->SetQuality(static_cast<int>(quality));
    }
    aviwriter->SetRate(static_cast<int>(this->GetFrameRate()));
    mwriter = aviwriter.Get();
  }
#endif
#ifdef _WIN32
  else if (extension == ".avi")
  {
    vtkNew<vtkAVIWriter> avi;
    avi->SetQuality((2 * this->Quality) / 100);
    avi->SetRate(static_cast<int>(this->GetFrameRate()));
    // Also available are IYUV and I420, but these are ~10x larger than MSVC.
    // No other encoder seems to be available on a stock Windows 7 install.
    avi->SetCompressorFourCC("MSVC");
    mwriter = avi.Get();
  }
#else
#endif
#ifdef VTK_HAS_OGGTHEORA_SUPPORT
  else if (extension == ".ogv" || extension == ".ogg")
  {
    vtkNew<vtkOggTheoraWriter> ogvwriter;
    ogvwriter->SetQuality((2 * this->Quality) / 100);
    ogvwriter->SetRate(static_cast<int>(this->GetFrameRate()));
    mwriter = ogvwriter.Get();
  }
#endif
  else
  {
    vtkErrorMacro("Unknown extension " << extension);
    return false;
  }

  if (iwriter)
  {
    this->ImageWriter = iwriter;

    std::string filename = this->FileName;
    std::string::size_type dot_pos = filename.rfind(".");
    if (dot_pos != std::string::npos)
    {
      this->Prefix = filename.substr(0, dot_pos);
      this->Suffix = filename.substr(dot_pos);
    }
    else
    {
      this->Prefix = this->FileName;
      this->Suffix = "";
    }
  }
  if (mwriter)
  {
    this->MovieWriter = mwriter;
  }

  return (this->ImageWriter != NULL || this->MovieWriter != NULL);
}

//-----------------------------------------------------------------------------
void vtkSMAnimationSceneImageWriter::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
  os << indent << "Quality: " << this->Quality << endl;
  os << indent << "ErrorCode: " << this->ErrorCode << endl;
  os << indent << "FrameRate: " << this->FrameRate << endl;
}