# RescaleAnImage

VTKExamples/Cxx/ImageProcessing/RescaleAnImage

### Code

RescaleAnImage.cxx

    #include <vtkImageData.h>
    #include <vtkImageShiftScale.h>
    #include <vtkMath.h>
    #include <vtkSmartPointer.h>

    static void CreateImage(vtkImageData* const image);

    int main(int, char *[])
    {
      vtkSmartPointer<vtkImageData> image = vtkSmartPointer<vtkImageData>::New();
      CreateImage(image);

      vtkSmartPointer<vtkImageShiftScale> shiftScaleFilter =
        vtkSmartPointer<vtkImageShiftScale>::New();
      shiftScaleFilter->SetOutputScalarTypeToUnsignedChar();
      shiftScaleFilter->SetInputData(image);
      shiftScaleFilter->SetShift(-1.0f * image->GetScalarRange()[0]); // brings the lower bound to 0
      float oldRange = image->GetScalarRange()[1] - image->GetScalarRange()[0];
      std::cout << "Old range: [" << image->GetScalarRange()[0] << ", " << image->GetScalarRange()[1] << "]" << std::endl;
      std::cout << "Old range magnitude: " << oldRange << std::endl;
      float newRange = 255; // We want the output [0,255]

      shiftScaleFilter->SetScale(newRange/oldRange);
      shiftScaleFilter->Update();

      std::cout << "New range: [" << shiftScaleFilter->GetOutput()->GetScalarRange()[1] << ", " << shiftScaleFilter->GetOutput()->GetScalarRange()[1] << "]" << std::endl;
      return EXIT_SUCCESS;
    }

    void CreateImage(vtkImageData* const image)
    {
      // Specify the size of the image data
      image->SetDimensions(2,3,1);
      image->AllocateScalars(VTK_DOUBLE,1);

      int* dims = image->GetDimensions();

      std::cout << "Dims: " << " x: " << dims[0] << " y: " << dims[1] << " z: " << dims[2] << std::endl;

      for (int z = 0; z < dims[2]; z++)
      {
        for (int y = 0; y < dims[1]; y++)
        {
          for (int x = 0; x < dims[0]; x++)
          {
            double* pixel = static_cast<double*>(image->GetScalarPointer(x,y,z));
            pixel[0] =vtkMath::Random(0.0,2000.0);
          }
        }
      }
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(RescaleAnImage)

    find_package(VTK COMPONENTS
      vtkCommonCore
      vtkCommonDataModel
      vtkImagingCore QUIET)
    if (NOT VTK_FOUND)
      message("Skipping RescaleAnImage: ${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(RescaleAnImage MACOSX_BUNDLE RescaleAnImage.cxx )
      target_link_libraries(RescaleAnImage PRIVATE ${VTK_LIBRARIES})
    else ()
      # include all components
      add_executable(RescaleAnImage MACOSX_BUNDLE RescaleAnImage.cxx )
      target_link_libraries(RescaleAnImage PRIVATE ${VTK_LIBRARIES})
      # vtk_module_autoinit is needed
      vtk_module_autoinit(
        TARGETS RescaleAnImage
        MODULES ${VTK_LIBRARIES}
        )
    endif ()

### Download and Build RescaleAnImage

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

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

    ./RescaleAnImage

WINDOWS USERS

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