Skip to content

BorderPixelSize

Repository source: BorderPixelSize


Question

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

Code

BorderPixelSize.cxx

#include <vtkImageData.h>
#include <vtkImageProperty.h>
#include <vtkImageSlice.h>
#include <vtkImageSliceMapper.h>
#include <vtkInteractorStyleImage.h>
#include <vtkMinimalStandardRandomSequence.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>

namespace {

void CreateRandomImage(vtkImageData* image, const unsigned int dimension);

}

int main(int, char*[])
{
  vtkNew<vtkNamedColors> colors;

  // Big image.
  vtkNew<vtkImageData> image;
  CreateRandomImage(image, 50);

  vtkNew<vtkImageSliceMapper> imageSliceMapper;
  imageSliceMapper->SetInputData(image);
  imageSliceMapper->BorderOn(); // This line tells the mapper to draw the full
                                // border pixels.
  vtkNew<vtkImageSlice> imageSlice;
  imageSlice->SetMapper(imageSliceMapper);
  imageSlice->GetProperty()->SetInterpolationTypeToNearest();

  vtkNew<vtkRenderer> renderer;
  renderer->AddViewProp(imageSlice);
  renderer->SetBackground(colors->GetColor3d("DarkSlateGray").GetData());
  renderer->ResetCamera();

  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);
  renderWindow->SetWindowName("BorderPixelSize");

  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;

  vtkNew<vtkInteractorStyleImage> style;

  renderWindowInteractor->SetInteractorStyle(style);

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

  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

namespace {

void CreateRandomImage(vtkImageData* image, const unsigned int dimension)
{
  image->SetDimensions(dimension, dimension, 1);
  image->SetOrigin(.5, .5, 0);
  image->AllocateScalars(VTK_UNSIGNED_CHAR, 3);

  unsigned int seed = 0;
  vtkNew<vtkMinimalStandardRandomSequence> randomSequence;
  randomSequence->Initialize(seed);

  for (unsigned int x = 0; x < dimension; x++)
  {
    for (unsigned int y = 0; y < dimension; y++)
    {
      unsigned char* pixel =
          static_cast<unsigned char*>(image->GetScalarPointer(x, y, 0));
      pixel[0] = static_cast<unsigned char>(randomSequence->GetValue() * 255);
      randomSequence->Next();
      pixel[1] = static_cast<unsigned char>(randomSequence->GetValue() * 255);
      randomSequence->Next();
      pixel[2] = static_cast<unsigned char>(randomSequence->GetValue() * 255);
      randomSequence->Next();
    }
  }
  image->Modified();
}

} // namespace

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(BorderPixelSize)

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

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

Download and Build BorderPixelSize

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

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

./BorderPixelSize

WINDOWS USERS

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