Skip to content

JPEGWriter

vtk-examples/Cxx/IO/JPEGWriter

Other languages

See (Java), (CSharp)

Question

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

Code

JPEGWriter.cxx

#include <vtkImageCanvasSource2D.h>
#include <vtkJPEGWriter.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkSmartPointer.h>

#include <array>

int main(int vtkNotUsed(argc), char* vtkNotUsed(argv)[])
{
  std::string outputFilename = "output.jpg";

  vtkNew<vtkNamedColors> colors;

  std::array<double, 3> drawColor1{0, 0, 0};
  std::array<double, 3> drawColor2{0, 0, 0};
  auto color1 = colors->GetColor3ub("DeepSkyBlue").GetData();
  auto color2 = colors->GetColor3ub("PaleGoldenrod").GetData();
  for (auto i = 0; i < 3; ++i)
  {
    drawColor1[i] = color1[i];
    drawColor2[i] = color2[i];
  }

  // Create a 100x100 image to save into the jpeg file
  int extent[6] = {0, 99, 0, 99, 0, 0};
  vtkNew<vtkImageCanvasSource2D> imageSource;
  imageSource->SetExtent(extent);
  imageSource->SetScalarTypeToUnsignedChar(); // vtkJPEGWriter only accepts
                                              // unsigned char input
  imageSource->SetNumberOfScalarComponents(
      3); // 3 color channels: Red, Green and Blue

  // Fill the whole image with a bluish background
  imageSource->SetDrawColor(drawColor1.data());
  imageSource->FillBox(extent[0], extent[1], extent[2], extent[3]);

  // Paint a 30x30 yellowish square into the image
  imageSource->SetDrawColor(drawColor2.data());
  imageSource->FillBox(40, 70, 20, 50);

  vtkNew<vtkJPEGWriter> writer;
  writer->SetFileName(outputFilename.c_str());
  writer->SetInputConnection(imageSource->GetOutputPort());
  writer->Write();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(JPEGWriter)

find_package(VTK COMPONENTS 
  vtkCommonColor
  vtkCommonCore
  vtkIOImage
  vtkImagingSources
  QUIET
)

if (NOT VTK_FOUND)
  message("Skipping JPEGWriter: ${VTK_NOT_FOUND_MESSAGE}")
  return ()
endif()
message (STATUS "VTK_VERSION: ${VTK_VERSION}")
if (VTK_VERSION VERSION_LESS "8.90.0")
  # old system
  include(${VTK_USE_FILE})
  add_executable(JPEGWriter MACOSX_BUNDLE JPEGWriter.cxx )
  target_link_libraries(JPEGWriter PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(JPEGWriter MACOSX_BUNDLE JPEGWriter.cxx )
  target_link_libraries(JPEGWriter PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS JPEGWriter
    MODULES ${VTK_LIBRARIES}
    )
endif ()

Download and Build JPEGWriter

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

cd JPEGWriter/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:

./JPEGWriter

WINDOWS USERS

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