Skip to content

ImageTransparency

vtk-examples/Cxx/Images/ImageTransparency


Description

Question

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

Code

ImageTransparency.cxx

#include <vtkImageActor.h>
#include <vtkImageData.h>
#include <vtkImageMapToColors.h>
#include <vtkImageMapper3D.h>
#include <vtkInteractorStyleImage.h>
#include <vtkJPEGReader.h>
#include <vtkLookupTable.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPointData.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>

#include <array>

namespace {
void CreateColorImage(vtkImageData* image, std::string const& colorNameunsigned,
                      unsigned char const alpha);
}

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

  vtkNew<vtkImageData> image;
  // Use this to control the transparency [0 .. 255]
  unsigned char alpha = 50;
  CreateColorImage(image, "Gold", alpha);

  // Create actor
  vtkNew<vtkImageActor> imageActor;
  imageActor->GetMapper()->SetInputData(image);

  // Visualize
  vtkNew<vtkRenderer> renderer;
  renderer->AddActor(imageActor);
  renderer->SetBackground(colors->GetColor3d("MidnightBlue").GetData());
  renderer->ResetCamera();

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

  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
  vtkNew<vtkInteractorStyleImage> style;

  renderWindowInteractor->SetInteractorStyle(style);

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

  return EXIT_SUCCESS;
}

namespace {
void CreateColorImage(vtkImageData* image, std::string const& colorName,
                      unsigned char const alpha)
{
  vtkNew<vtkNamedColors> colors;

  std::array<unsigned char, 4> drawColor{0, 0, 0};
  auto color = colors->GetColor4ub(colorName).GetData();
  for (auto i = 0; i < 4; ++i)
  {
    drawColor[i] = color[i];
  }

  unsigned int dim = 10;

  // Specify the size of the image data
  image->SetDimensions(dim, dim, 1);
  image->AllocateScalars(VTK_UNSIGNED_CHAR, 4);

  int* dims = image->GetDimensions();

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

  for (unsigned char y = 0; y < dims[1]; y++)
  {
    for (unsigned char x = 0; x < dims[0]; x++)
    {
      auto pixel =
          static_cast<unsigned char*>(image->GetScalarPointer(x, y, 0));
      for (auto i = 0; i < 4; ++i)
      {
        pixel[i] = drawColor[i];
      }
      if (x < 5)
      {
        pixel[3] = alpha;
      }
      else
      {
        pixel[3] = 255;
      }
    }
  }

  image->Modified();
}
} // namespace```
###CMakeLists.txt
```cmake
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(ImageTransparency)

find_package(VTK COMPONENTS 
  vtkCommonColor
  vtkCommonCore
  vtkCommonDataModel
  vtkIOImage
  vtkImagingCore
  vtkInteractionStyle
  vtkRenderingContextOpenGL2
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingGL2PSOpenGL2
  vtkRenderingOpenGL2
  QUIET
)

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

Download and Build ImageTransparency

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

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

./ImageTransparency

WINDOWS USERS

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