JPEGWriter
VTKExamples/Cxx/IO/JPEGWriter
Question
If you have a simple question about this example contact us at VTKExamplesProject If your question is more complex and may require extended discussion, please use the VTK Discourse Forum
Code¶
JPEGWriter.cxx
#include <vtkSmartPointer.h> #include <vtkImageCanvasSource2D.h> #include <vtkJPEGWriter.h> int main(int vtkNotUsed(argc), char* vtkNotUsed(argv)[]) { std::string outputFilename = "output.jpg"; // Create a 100x100 image to save into the jpeg file int extent[6] = { 0, 99, 0, 99, 0, 0 }; vtkSmartPointer<vtkImageCanvasSource2D> imageSource = vtkSmartPointer<vtkImageCanvasSource2D>::New(); 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 blue background imageSource->SetDrawColor( 0, 127, 255 ); imageSource->FillBox( extent[0], extent[1], extent[2], extent[3] ); // Paint a 30x30 white square into the image imageSource->SetDrawColor( 255, 255, 255 ); imageSource->FillBox( 40, 70, 20, 50 ); vtkSmartPointer<vtkJPEGWriter> writer = vtkSmartPointer<vtkJPEGWriter>::New(); 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 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.