# ImageIteratorDemo

VTKExamples/Cxx/ImageData/ImageIteratorDemo

### Description

vtkImageIterator is an efficient way to access the regions of a vtkImageData. A span in the vrkImageData is a continuous segment of pixels. The NextSpan() method increments the iterator to the next continuous segment.

### Code

ImageIteratorDemo.cxx

    #include <vtkSmartPointer.h>
    #include <vtkImageData.h>
    #include <vtkImageIterator.h>

    #include <vtkImageViewer2.h>
    #include <vtkImageActor.h>
    #include <vtkRenderWindow.h>
    #include <vtkInteractorStyleImage.h>
    #include <vtkRenderWindowInteractor.h>
    #include <vtkRenderer.h>
    #include <vtkNamedColors.h>

    int main(int, char *[])
    {
      // Create an image data
      vtkSmartPointer<vtkImageData> imageData =
          vtkSmartPointer<vtkImageData>::New();

      // Specify the size of the image data
      imageData->SetDimensions(100,200,30);
      imageData->AllocateScalars(VTK_UNSIGNED_CHAR,3);

      // Fill every entry of the image data with a color
      int* dims = imageData->GetDimensions();

      unsigned char *ptr = static_cast<unsigned char *>(imageData->GetScalarPointer(0, 0, 0));
      unsigned char r, g, b, a;
      vtkSmartPointer<vtkNamedColors> colors =
        vtkSmartPointer<vtkNamedColors>::New();
      colors->GetColor("Banana", r, g, b, a);
      for (int z=0; z<dims[2]; z++)
      {
        for (int y=0; y<dims[1]; y++)
        {
          for (int x=0; x<dims[0]; x++)
          {
            *ptr++ = r;
            *ptr++ = g;
            *ptr++ = b;
          }
        }
      }

      // Define the extent to be modified
      int extent[6];
      extent[0] = 20; extent[1] = 50;
      extent[2] = 30; extent[3] = 60;
      extent[4] = 10; extent[5] = 20;

      // Set the entries in the region to another color
      colors->GetColor("Tomato", r, g, b, a);
      vtkImageIterator<unsigned char> it(imageData, extent);
      unsigned int counter = 0;
      while(!it.IsAtEnd())
      {
        unsigned char *valIt = it.BeginSpan();
        unsigned char *valEnd = it.EndSpan();
        while (valIt != valEnd)
        {
          // Increment for each component
          *valIt++ = r;
          *valIt++ = g;
          *valIt++ = b;
        }
        it.NextSpan();
        ++counter;
      }
      std::cout << "# of spans: " << counter << std::endl;

      std::cout << "Increments: "
                << imageData->GetIncrements()[0] << ", "
                << imageData->GetIncrements()[1] << ", "
                << imageData->GetIncrements()[2] << std::endl;
      vtkIdType incX, incY, incZ;
      imageData->GetContinuousIncrements(extent, incX, incY, incZ);
      std::cout << "ContinuousIncrements: "
                << incX << ", " << incY << ", " << incZ << std::endl;

      // Visualize
      vtkSmartPointer<vtkImageViewer2> imageViewer =
        vtkSmartPointer<vtkImageViewer2>::New();
      imageViewer->SetInputData( imageData );

      vtkSmartPointer<vtkInteractorStyleImage> style =
        vtkSmartPointer<vtkInteractorStyleImage>::New();
      style->SetInteractionModeToImageSlicing();

      vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
      renderWindowInteractor->SetInteractorStyle(style);
      imageViewer->SetupInteractor(renderWindowInteractor);
      imageViewer->SetSlice((extent[5] - extent[4]) / 2 + extent[4]);

      imageViewer->GetRenderer()->SetBackground(colors->GetColor3d("Slate_grey").GetData());
      imageViewer->GetImageActor()->InterpolateOff();

      imageViewer->Render();
      imageViewer->GetRenderer()->ResetCamera();
      imageViewer->Render();

      renderWindowInteractor->Start();

      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 2.8)

    PROJECT(ImageIteratorDemo)

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

    add_executable(ImageIteratorDemo MACOSX_BUNDLE ImageIteratorDemo.cxx )

    target_link_libraries(ImageIteratorDemo ${VTK_LIBRARIES})

### Download and Build ImageIteratorDemo

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

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

    ./ImageIteratorDemo

WINDOWS USERS

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