Skip to content

RenderLargeImage

Repository source: RenderLargeImage

Description

This example renders a high resolution image. The image can be much larger than the window on the screen. It is useful if you need an image for a large poster, or you can down sample the image to produce a high quality anti-aliased image.

The example takes up to three arguments. The first, an input polydata, is required, as is the second, a .png file to hold the high res image. An optional third argument specifies the magnification of the high res image (default 4).

Question

If you have a question about this example, please use the VTK Discourse Forum

Code

RenderLargeImage.cxx

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPNGWriter.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderLargeImage.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkXMLPolyDataReader.h>

#include <iostream>
#include <string>

int main(int argc, char* argv[])
{
  if (argc < 3)
  {
    std::cerr << "Usage: " << argv[0]
              << " Input(.vtp) Output(.png) [Magnification]" << std::endl;
    std::cerr << "e.g. Bunny.vtp Bunny.png 4" << std::endl;
    return EXIT_FAILURE;
  }
  int magnification = 4;
  if (argc == 4)
  {
    magnification = atoi(argv[3]);
  }

  vtkNew<vtkNamedColors> colors;

  vtkNew<vtkXMLPolyDataReader> reader;
  reader->SetFileName(argv[1]);

  vtkNew<vtkPolyDataMapper> mapper;
  mapper->SetInputConnection(reader->GetOutputPort());

  vtkNew<vtkActor> actor;
  actor->SetMapper(mapper);
  actor->GetProperty()->SetColor(colors->GetColor3d("Tan").GetData());

  vtkNew<vtkRenderer> renderer;
  vtkNew<vtkRenderWindow> renderWindow;

  vtkNew<vtkRenderWindowInteractor> interactor;
  interactor->SetRenderWindow(renderWindow);

  renderWindow->AddRenderer(renderer);
  renderWindow->SetWindowName("RenderLargeImage");

  renderer->AddActor(actor);

  // Let the renderer compute good position and focal point.
  renderer->GetActiveCamera()->Azimuth(30);
  renderer->GetActiveCamera()->Elevation(30);
  renderer->ResetCamera();
  renderer->GetActiveCamera()->Dolly(1.4);
  renderer->ResetCameraClippingRange();
  renderer->SetBackground(colors->GetColor3d("SteelBlue").GetData());

  renderWindow->SetSize(640, 480);
  renderWindow->Render();

  std::cout
      << "Interact with image to get desired view and then press 'e' or 'q'"
      << std::endl;

  interactor->Start();

  std::cout << "Generating large image size: "
            << renderWindow->GetSize()[0] * magnification << " by "
            << renderWindow->GetSize()[1] * magnification << std::endl;

  vtkNew<vtkRenderLargeImage> renderLarge;
  renderLarge->SetInput(renderer);
  renderLarge->SetMagnification(magnification);

  std::cout << "Saving image in " << argv[2] << std::endl;
  vtkNew<vtkPNGWriter> writer;
  writer->SetFileName(argv[2]);
  writer->SetInputConnection(renderLarge->GetOutputPort());
  writer->Write();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(RenderLargeImage)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  FiltersHybrid
  IOImage
  IOXML
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "RenderLargeImage: Unable to find the VTK build folder.")
endif()

# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
add_executable(RenderLargeImage MACOSX_BUNDLE RenderLargeImage.cxx )
  target_link_libraries(RenderLargeImage PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS RenderLargeImage
  MODULES ${VTK_LIBRARIES}
)

Download and Build RenderLargeImage

Click here to download RenderLargeImage and its CMakeLists.txt file. Once the tarball RenderLargeImage.tar has been downloaded and extracted,

cd RenderLargeImage/build

If VTK is installed:

cmake ..

If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:

cmake -DVTK_DIR:PATH=/home/me/vtk_build ..

Build the project:

make

and run it:

./RenderLargeImage

WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.