Skip to content

HDRReader

Repository source: HDRReader


Description

Demonstrates how to read high-dynamic-range imaging files.

A callback is used to print out the color window (move the mouse horizontally over the image) and color level (move the mouse vertically over the image).

This is based on IO/Image/Testing/Cxx/TestHDRReader.cxx in the VTK source files.

Other languages

See (Python), (PythonicAPI)

Question

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

Code

HDRReader.cxx

#include "vtkHDRReader.h"
#include "vtkImageData.h"
#include "vtkImageViewer.h"
#include "vtkNew.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include <vtkCallbackCommand.h>

namespace {
class ColorCallback : public vtkCallbackCommand
{
public:
  static ColorCallback* New()
  {
    return new ColorCallback;
  }
  // Here we Create a vtkCallbackCommand and reimplement it.
  void Execute(vtkObject* caller, unsigned long evId, void*) override
  {
    // Note the use of reinterpret_cast to cast the caller to the expected type.
    // Just do this to demonstrate who called callback and the event that
    // triggered it.
    // std::cout << interactor->GetClassName() << "  Event Id: " << evId
    //          << std::endl;

    std::cout << "Color window: " << imageViewer->GetColorWindow();
    std::cout << " level: " << imageViewer->GetColorLevel() << std::endl;
  }
  ColorCallback() : imageViewer(nullptr)
  {
  }
  // Set pointers to any clientData or callData here.
  vtkImageViewer* imageViewer;

private:
  ColorCallback(const ColorCallback&) = delete;
  void operator=(const ColorCallback&) = delete;
};

} // namespace

int main(int argc, char* argv[])
{
  if (argc <= 1)
  {
    cout << "Usage: " << argv[0] << " <hdr file>" << endl;
    cout << "For example: Skyboxes/spiaggia_di_mondello_1k.hdr" << std::endl;
    return EXIT_FAILURE;
  }

  std::string filename = argv[1];

  vtkNew<vtkHDRReader> reader;

  // Check the image can be read
  if (!reader->CanReadFile(filename.c_str()))
  {
    cerr << "CanReadFile failed for " << filename.c_str() << "\n";
    return EXIT_FAILURE;
  }

  reader->SetFileName(filename.c_str());
  reader->UpdateInformation();

  // Whole extent
  const int* we = reader->GetDataExtent();
  const int extents[6] = {we[0], we[1], we[2], we[3], 0, 0};
  reader->UpdateExtent(extents);
  // Visualize
  vtkNew<vtkImageViewer> imageViewer;
  imageViewer->SetInputData(reader->GetOutput());

  imageViewer->SetColorWindow(1);
  imageViewer->SetColorLevel(1);
  imageViewer->SetPosition(0, 100);

  vtkNew<ColorCallback> getColorWindow;
  getColorWindow->imageViewer = imageViewer;

  vtkNew<vtkRenderWindowInteractor> iren;
  imageViewer->SetupInteractor(iren);
  imageViewer->GetRenderWindow()->SetWindowName("HDRReader");
  imageViewer->Render();

  iren->AddObserver(vtkCommand::EndInteractionEvent, getColorWindow);

  iren->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(HDRReader)

find_package(VTK COMPONENTS 
  CommonCore
  CommonDataModel
  IOImage
  InteractionImage
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "HDRReader: 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(HDRReader MACOSX_BUNDLE HDRReader.cxx )
  target_link_libraries(HDRReader PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS HDRReader
  MODULES ${VTK_LIBRARIES}
)

Download and Build HDRReader

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

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

./HDRReader

WINDOWS USERS

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