# MetaImageReader

VTKExamples/Cxx/IO/MetaImageReader

### Code

MetaImageReader.cxx

    #include <vtkImageData.h>
    #include <vtkMetaImageReader.h>
    #include <vtkSmartPointer.h>
    #include <vtkInteractorStyleImage.h>
    #include <vtkRenderer.h>
    #include <vtkImageActor.h>
    #include <vtkImageMapper3D.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderWindowInteractor.h>

    int main(int argc, char *argv[])
    {
      std::string inputFilename;

      if(argc < 2)
      {
        std::cerr << "Required arguments: image.mha" << std::endl;
        return EXIT_FAILURE;
      }

      inputFilename = argv[1];

      vtkSmartPointer<vtkMetaImageReader> reader =
        vtkSmartPointer<vtkMetaImageReader>::New();
      reader->SetFileName(inputFilename.c_str());
      reader->Update();

      // Visualize
      vtkSmartPointer<vtkImageActor> actor =
        vtkSmartPointer<vtkImageActor>::New();
      actor->GetMapper()->SetInputConnection(reader->GetOutputPort());

      vtkSmartPointer<vtkRenderer> renderer =
        vtkSmartPointer<vtkRenderer>::New();
      renderer->AddActor(actor);
      renderer->ResetCamera();

      vtkSmartPointer<vtkRenderWindow> renderWindow =
        vtkSmartPointer<vtkRenderWindow>::New();
      renderWindow->AddRenderer(renderer);

      vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
      vtkSmartPointer<vtkInteractorStyleImage> style =
        vtkSmartPointer<vtkInteractorStyleImage>::New();

      renderWindowInteractor->SetInteractorStyle(style);

      renderWindowInteractor->SetRenderWindow(renderWindow);
      renderWindowInteractor->Initialize();

      renderWindowInteractor->Start();

      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 2.8)

    PROJECT(MetaImageReader)

    find_package(VTK REQUIRED)
    include(${VTK_USE_FILE})

    add_executable(MetaImageReader MACOSX_BUNDLE MetaImageReader.cxx )

    target_link_libraries(MetaImageReader ${VTK_LIBRARIES})

### Download and Build MetaImageReader

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

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

    ./MetaImageReader

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