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 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
|
/*=========================================================================
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 "vtkImageIterator.h"
#include "vtkImageWriter.h"
#include "vtkJPEGWriter.h"
#include "vtkObjectFactory.h"
#include "vtkObjectFactory.h"
#include "vtkPNGWriter.h"
#include "vtkPVConfig.h"
#include "vtkSMAnimationScene.h"
#include "vtkSmartPointer.h"
#include "vtkSMIntVectorProperty.h"
#include "vtkSMPropertyHelper.h"
#include "vtkSMRenderViewProxy.h"
#include "vtkSMUtilities.h"
#include "vtkSMViewLayoutProxy.h"
#include "vtkSMViewProxy.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
vtkStandardNewMacro(vtkSMAnimationSceneImageWriter);
vtkCxxSetObjectMacro(vtkSMAnimationSceneImageWriter,
ImageWriter, vtkImageWriter);
vtkCxxSetObjectMacro(vtkSMAnimationSceneImageWriter,
MovieWriter, vtkGenericMovieWriter);
//-----------------------------------------------------------------------------
vtkSMAnimationSceneImageWriter::vtkSMAnimationSceneImageWriter()
{
this->Magnification = 1;
this->ErrorCode = 0;
this->Quality = 2; // 0 = low, 1 = medium, 2 = high
this->Compression = true;
#ifdef PARAVIEW_OGGTHEORA_USE_SUBSAMPLING
this->Subsampling = 1;
#else
this->Subsampling = 0;
#endif
this->ActualSize[0] = this->ActualSize[1] = 0;
this->MovieWriter = 0;
this->ImageWriter = 0;
this->FileCount = 0;
this->Prefix = 0;
this->Suffix = 0;
this->FrameRate = 1.0;
this->BackgroundColor[0] = this->BackgroundColor[1] =
this->BackgroundColor[2] = 0.0;
}
//-----------------------------------------------------------------------------
vtkSMAnimationSceneImageWriter::~vtkSMAnimationSceneImageWriter()
{
this->SetMovieWriter(0);
this->SetImageWriter(0);
this->SetPrefix(0);
this->SetSuffix(0);
}
//-----------------------------------------------------------------------------
bool vtkSMAnimationSceneImageWriter::SaveInitialize(int startCount)
{
// Create writers.
if (!this->CreateWriter())
{
return false;
}
this->UpdateImageSize();
if (this->MovieWriter)
{
this->MovieWriter->SetFileName(this->FileName);
vtkImageData* emptyImage = this->NewFrame();
this->MovieWriter->SetInputData(emptyImage);
emptyImage->Delete();
this->MovieWriter->Start();
}
this->AnimationScene->SetOverrideStillRender(1);
this->FileCount = startCount;
#if !defined(__APPLE__)
// Iterate over all views and enable offscreen rendering. This avoid toggling
// of the offscreen rendering flag on every frame.
unsigned int num_modules = this->AnimationScene->GetNumberOfViewProxies();
for (unsigned int cc=0; cc < num_modules; cc++)
{
vtkSMRenderViewProxy* rmview = vtkSMRenderViewProxy::SafeDownCast(
this->AnimationScene->GetViewProxy(cc));
if (rmview)
{
if (vtkSMPropertyHelper(rmview,
"UseOffscreenRenderingForScreenshots").GetAsInt() == 1)
{
vtkSMPropertyHelper(rmview, "UseOffscreenRendering").Set(1);
rmview->UpdateProperty("UseOffscreenRendering");
}
}
}
#endif
return true;
}
//-----------------------------------------------------------------------------
vtkImageData* vtkSMAnimationSceneImageWriter::NewFrame()
{
vtkImageData* image = vtkImageData::New();
image->SetDimensions(this->ActualSize[0], this->ActualSize[1], 1);
image->AllocateScalars(VTK_UNSIGNED_CHAR, 3);
unsigned char rgb[3];
rgb[0] = 0x0ff & static_cast<int>(this->BackgroundColor[0]*0x0ff);
rgb[1] = 0x0ff & static_cast<int>(this->BackgroundColor[1]*0x0ff);
rgb[2] = 0x0ff & static_cast<int>(this->BackgroundColor[2]*0x0ff);
vtkImageIterator<unsigned char> it(image, image->GetExtent());
while (!it.IsAtEnd())
{
unsigned char* span = it.BeginSpan();
unsigned char* spanEnd = it.EndSpan();
while (spanEnd != span)
{
*span = rgb[0]; span++;
*span = rgb[1]; span++;
*span = rgb[2]; span++;
}
it.NextSpan();
}
return image;
}
//-----------------------------------------------------------------------------
vtkImageData* vtkSMAnimationSceneImageWriter::CaptureViewImage(
vtkSMViewProxy* view, int magnification)
{
if (view)
{
return view->CaptureWindow(magnification);
}
return NULL;
}
//-----------------------------------------------------------------------------
void vtkSMAnimationSceneImageWriter::Merge(vtkImageData* dest, vtkImageData* src)
{
const double* colord = vtkSMViewLayoutProxy::GetMultiViewImageBorderColor();
unsigned char color[3];
color[0] = static_cast<unsigned char>(colord[0] * 0xff);
color[1] = static_cast<unsigned char>(colord[1] * 0xff);
color[2] = static_cast<unsigned char>(colord[2] * 0xff);
vtkSMUtilities::Merge(dest, src,
vtkSMViewLayoutProxy::GetMultiViewImageBorderWidth(), color);
}
//-----------------------------------------------------------------------------
bool vtkSMAnimationSceneImageWriter::SaveFrame(double vtkNotUsed(time))
{
vtkSmartPointer<vtkImageData> combinedImage;
unsigned int num_modules = this->AnimationScene->GetNumberOfViewProxies();
if (num_modules > 1)
{
combinedImage.TakeReference(this->NewFrame());
for (unsigned int cc=0; cc < num_modules; cc++)
{
vtkSMViewProxy* view = this->AnimationScene->GetViewProxy(cc);
vtkImageData* capture = this->CaptureViewImage(view, this->Magnification);
if (capture)
{
this->Merge(combinedImage, capture);
capture->Delete();
}
}
}
else if (num_modules == 1)
{
// If only one view, we speed things up slightly by using the
// captured image directly.
vtkSMViewProxy* view = this->AnimationScene->GetViewProxy(0);
vtkImageData* capture = this->CaptureViewImage(view, this->Magnification);
if (!capture)
{
return false;
}
combinedImage.TakeReference(capture);
}
int errcode = 0;
if (this->ImageWriter)
{
char number[1024];
sprintf(number, ".%04d", this->FileCount);
std::string filename = this->Prefix;
filename = filename + number + this->Suffix;
this->ImageWriter->SetInputData(combinedImage);
this->ImageWriter->SetFileName(filename.c_str());
this->ImageWriter->Write();
this->ImageWriter->SetInputData(0);
errcode = this->ImageWriter->GetErrorCode();
this->FileCount = (!errcode)? this->FileCount + 1 : this->FileCount;
}
else if (this->MovieWriter)
{
this->MovieWriter->SetInputData(combinedImage);
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.
errcode = 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
errcode = alg_error;
}
}
combinedImage = 0;
if (errcode)
{
this->ErrorCode = errcode;
return false;
}
return true;
}
//-----------------------------------------------------------------------------
bool vtkSMAnimationSceneImageWriter::SaveFinalize()
{
this->AnimationScene->SetOverrideStillRender(0);
// TODO: If save failed, we must remove the partially
// written files.
if (this->MovieWriter)
{
this->MovieWriter->End();
this->SetMovieWriter(0);
}
this->SetImageWriter(0);
#if !defined(__APPLE__)
// restore offscreen rendering state.
unsigned int num_modules = this->AnimationScene->GetNumberOfViewProxies();
for (unsigned int cc=0; cc < num_modules; cc++)
{
vtkSMRenderViewProxy* rmview = vtkSMRenderViewProxy::SafeDownCast(
this->AnimationScene->GetViewProxy(cc));
if (rmview)
{
vtkSMPropertyHelper(rmview, "UseOffscreenRendering").Set(0);
rmview->UpdateProperty("UseOffscreenRendering");
}
}
#endif
return true;
}
//-----------------------------------------------------------------------------
bool vtkSMAnimationSceneImageWriter::CreateWriter()
{
this->SetMovieWriter(0);
this->SetImageWriter(0);
vtkImageWriter* iwriter =0;
vtkGenericMovieWriter* mwriter = 0;
std::string extension = vtksys::SystemTools::GetFilenameLastExtension(
this->FileName);
if (extension == ".jpg" || extension == ".jpeg")
{
iwriter = vtkJPEGWriter::New();
}
else if (extension == ".tif" || extension == ".tiff")
{
iwriter = vtkTIFFWriter::New();
}
else if (extension == ".png")
{
iwriter = vtkPNGWriter::New();
}
#ifdef VTK_USE_MPEG2_ENCODER
else if (extension == ".mpeg" || extension == ".mpg")
{
mwriter = vtkMPEG2Writer::New();
}
#endif
# ifdef PARAVIEW_ENABLE_FFMPEG
else if (extension == ".avi")
{
vtkFFMPEGWriter *aviwriter = vtkFFMPEGWriter::New();
aviwriter->SetQuality(this->Quality);
aviwriter->SetCompression(this->Compression);
aviwriter->SetRate(
static_cast<int>(this->GetFrameRate()));
mwriter = aviwriter;
}
# endif
#ifdef _WIN32
else if (extension == ".avi")
{
vtkAVIWriter* avi = vtkAVIWriter::New();
avi->SetQuality(this->Quality);
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;
}
#else
#endif
#ifdef VTK_HAS_OGGTHEORA_SUPPORT
else if (extension == ".ogv" || extension == ".ogg")
{
vtkOggTheoraWriter *ogvwriter = vtkOggTheoraWriter::New();
ogvwriter->SetQuality(this->Quality);
ogvwriter->SetRate(
static_cast<int>(this->GetFrameRate()));
ogvwriter->SetSubsampling(this->GetSubsampling());
mwriter = ogvwriter;
}
#endif
else
{
vtkErrorMacro("Unknown extension " << extension.c_str());
return false;
}
if (iwriter)
{
this->SetImageWriter(iwriter);
iwriter->Delete();
std::string filename = this->FileName;
std::string::size_type dot_pos = filename.rfind(".");
if(dot_pos != std::string::npos)
{
this->SetPrefix(filename.substr(0, dot_pos).c_str());
this->SetSuffix(filename.substr(dot_pos).c_str());
}
else
{
this->SetPrefix(this->FileName);
this->SetSuffix("");
}
}
if (mwriter)
{
this->SetMovieWriter(mwriter);
mwriter->Delete();
}
return true;
}
//-----------------------------------------------------------------------------
void vtkSMAnimationSceneImageWriter::UpdateImageSize()
{
unsigned int num_modules = this->AnimationScene->GetNumberOfViewProxies();
int gui_size[2] = {1, 1};
for (unsigned int cc=0; cc < num_modules; cc++)
{
vtkSMViewProxy* view = this->AnimationScene->GetViewProxy(cc);
if (!view)
{
continue;
}
vtkSMPropertyHelper size(view, "ViewSize");
vtkSMPropertyHelper position(view, "ViewPosition");
if (gui_size[0] < size.GetAsInt(0) + position.GetAsInt(0))
{
gui_size[0] = size.GetAsInt(0) + position.GetAsInt(0);
}
if (gui_size[1] < size.GetAsInt(1) + position.GetAsInt(1))
{
gui_size[1] = size.GetAsInt(1) + position.GetAsInt(1);
}
}
if (num_modules==0)
{
vtkErrorMacro("AnimationScene has no view modules added to it.");
}
gui_size[0] *= this->Magnification;
gui_size[1] *= this->Magnification;
this->SetActualSize(gui_size);
}
//-----------------------------------------------------------------------------
void vtkSMAnimationSceneImageWriter::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "Quality: " << this->Quality << endl;
os << indent << "Compression: " << (this->Compression?"on":"off") << endl;
os << indent << "Magnification: " << this->Magnification << endl;
os << indent << "Subsampling: " << this->Subsampling << endl;
os << indent << "ErrorCode: " << this->ErrorCode << endl;
os << indent << "FrameRate: " << this->FrameRate << endl;
os << indent << "BackgroundColor: " << this->BackgroundColor[0]
<< ", " << this->BackgroundColor[1] << ", " << this->BackgroundColor[2]
<< endl;
}
|