| 1 |
/*========================================================================= |
| 2 |
* |
| 3 |
* Copyright NumFOCUS |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* https://www.apache.org/licenses/LICENSE-2.0.txt |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
*=========================================================================*/ |
| 18 |
#include "QuickView.h" |
| 19 |
|
| 20 |
#include "itkRescaleIntensityImageFilter.h" |
| 21 |
#include "itkVectorRescaleIntensityImageFilter.h" |
| 22 |
#include "itkRGBToVectorImageAdaptor.h" |
| 23 |
#include "itkFlipImageFilter.h" |
| 24 |
|
| 25 |
#include "vtkVersion.h" |
| 26 |
|
| 27 |
#include "vtkRenderWindowInteractor.h" |
| 28 |
#include "vtkImageMapper3D.h" |
| 29 |
#include "vtkImageActor.h" |
| 30 |
#include "vtkActor2D.h" |
| 31 |
#include "vtkInteractorStyleImage.h" |
| 32 |
#include "vtkRenderer.h" |
| 33 |
#include "vtkCamera.h" |
| 34 |
#include "vtkTextProperty.h" |
| 35 |
#include "vtkTextMapper.h" |
| 36 |
|
| 37 |
#include "vtkCaptureScreen.h" |
| 38 |
#include "vtkPNGWriter.h" |
| 39 |
#include "vtkJPEGWriter.h" |
| 40 |
#include "vtkBMPWriter.h" |
| 41 |
#include "vtkTIFFWriter.h" |
| 42 |
|
| 43 |
#include "itkImageToVTKImageFilter.h" |
| 44 |
|
| 45 |
|
| 46 |
using UnsignedCharRGBImageType = itk::Image<itk::RGBPixel<unsigned char>, 2>; |
| 47 |
using FloatRGBImageType = itk::Image<itk::RGBPixel<float>, 2>; |
| 48 |
|
| 49 |
using UnsignedCharImageType = itk::Image<unsigned char, 2>; |
| 50 |
using CharImageType = itk::Image<char, 2>; |
| 51 |
using UnsignedShortImageType = itk::Image<unsigned short, 2>; |
| 52 |
using ShortImageType = itk::Image<short, 2>; |
| 53 |
using UnsignedIntImageType = itk::Image<unsigned int, 2>; |
| 54 |
using IntImageType = itk::Image<int, 2>; |
| 55 |
using UnsignedLongImageType = itk::Image<unsigned long, 2>; |
| 56 |
using LongImageType = itk::Image<long, 2>; |
| 57 |
using FloatImageType = itk::Image<float, 2>; |
| 58 |
using DoubleImageType = itk::Image<double, 2>; |
| 59 |
|
| 60 |
template void ITKVtkGlue_EXPORT |
| 61 |
QuickView::AddImage<CharImageType>(CharImageType * image, bool FlipVertical, std::string Description); |
| 62 |
template void ITKVtkGlue_EXPORT |
| 63 |
QuickView::AddImage<UnsignedShortImageType>(UnsignedShortImageType * image, bool FlipVertical, std::string Description); |
| 64 |
template void ITKVtkGlue_EXPORT |
| 65 |
QuickView::AddImage<ShortImageType>(ShortImageType * image, bool FlipVertical, std::string Description); |
| 66 |
template void ITKVtkGlue_EXPORT |
| 67 |
QuickView::AddImage<UnsignedIntImageType>(UnsignedIntImageType * image, bool FlipVertical, std::string Description); |
| 68 |
template void ITKVtkGlue_EXPORT |
| 69 |
QuickView::AddImage<IntImageType>(IntImageType * image, bool FlipVertical, std::string Description); |
| 70 |
template void ITKVtkGlue_EXPORT |
| 71 |
QuickView::AddImage<UnsignedLongImageType>(UnsignedLongImageType * image, bool FlipVertical, std::string Description); |
| 72 |
template void ITKVtkGlue_EXPORT |
| 73 |
QuickView::AddImage<LongImageType>(LongImageType * image, bool FlipVertical, std::string Description); |
| 74 |
template void ITKVtkGlue_EXPORT |
| 75 |
QuickView::AddImage<FloatImageType>(FloatImageType * image, bool FlipVertical, std::string Description); |
| 76 |
template void ITKVtkGlue_EXPORT |
| 77 |
QuickView::AddImage<DoubleImageType>(DoubleImageType * image, bool FlipVertical, std::string Description); |
| 78 |
|
| 79 |
template <> |
| 80 |
void ITKVtkGlue_EXPORT |
| 81 |
QuickView::AddImage<UnsignedCharImageType>(UnsignedCharImageType * image, bool FlipVertical, std::string Description) |
| 82 |
{ |
| 83 |
if (FlipVertical) |
| 84 |
{ |
| 85 |
using FlipFilterType = itk::FlipImageFilter<UnsignedCharImageType>; |
| 86 |
FlipFilterType::Pointer flipper = FlipFilterType::New(); |
| 87 |
bool flipAxes[3] = { false, true, false }; |
| 88 |
flipper = FlipFilterType::New(); |
| 89 |
flipper->SetFlipAxes(flipAxes); |
| 90 |
flipper->SetInput(image); |
| 91 |
flipper->Update(); |
| 92 |
ImageInfo myImage(flipper->GetOutput(), Description); |
| 93 |
this->Images.push_back(myImage); |
| 94 |
} |
| 95 |
else |
| 96 |
{ |
| 97 |
ImageInfo myImage(image, Description); |
| 98 |
this->Images.push_back(myImage); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
template <typename TImage> |
| 103 |
void ITK_TEMPLATE_EXPORT |
| 104 |
QuickView::AddImage(TImage * image, bool FlipVertical, std::string Description) |
| 105 |
{ |
| 106 |
using rescaleFilterType = itk::RescaleIntensityImageFilter<TImage, UnsignedCharImageType>; |
| 107 |
|
| 108 |
typename rescaleFilterType::Pointer rescaler = rescaleFilterType::New(); |
| 109 |
rescaler->SetOutputMinimum(0); |
| 110 |
rescaler->SetOutputMaximum(255); |
| 111 |
rescaler->SetInput(image); |
| 112 |
rescaler->Update(); |
| 113 |
|
| 114 |
this->AddImage(rescaler->GetOutput(), FlipVertical, Description); |
| 115 |
} |
| 116 |
|
| 117 |
template <> |
| 118 |
void ITKVtkGlue_EXPORT |
| 119 |
QuickView::AddImage<UnsignedCharRGBImageType>(UnsignedCharRGBImageType * image, |
| 120 |
bool FlipVertical, |
| 121 |
std::string Description) |
| 122 |
{ |
| 123 |
if (FlipVertical) |
| 124 |
{ |
| 125 |
using FlipFilterType = itk::FlipImageFilter<UnsignedCharRGBImageType>; |
| 126 |
FlipFilterType::Pointer flipper = FlipFilterType::New(); |
| 127 |
bool flipAxes[3] = { false, true, false }; |
| 128 |
flipper = FlipFilterType::New(); |
| 129 |
flipper->SetFlipAxes(flipAxes); |
| 130 |
flipper->SetInput(image); |
| 131 |
flipper->Update(); |
| 132 |
RGBImageInfo myImage(flipper->GetOutput(), Description); |
| 133 |
this->RGBImages.push_back(myImage); |
| 134 |
} |
| 135 |
else |
| 136 |
{ |
| 137 |
RGBImageInfo myImage(image, Description); |
| 138 |
this->RGBImages.push_back(myImage); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
template <> |
| 143 |
void ITKVtkGlue_EXPORT |
| 144 |
QuickView::AddRGBImage<UnsignedCharRGBImageType>(UnsignedCharRGBImageType * image, |
| 145 |
bool FlipVertical, |
| 146 |
std::string Description) |
| 147 |
{ |
| 148 |
if (FlipVertical) |
| 149 |
{ |
| 150 |
using FlipFilterType = itk::FlipImageFilter<UnsignedCharRGBImageType>; |
| 151 |
FlipFilterType::Pointer flipper = FlipFilterType::New(); |
| 152 |
bool flipAxes[3] = { false, true, false }; |
| 153 |
flipper = FlipFilterType::New(); |
| 154 |
flipper->SetFlipAxes(flipAxes); |
| 155 |
flipper->SetInput(image); |
| 156 |
flipper->Update(); |
| 157 |
RGBImageInfo myImage(flipper->GetOutput(), Description); |
| 158 |
this->RGBImages.push_back(myImage); |
| 159 |
} |
| 160 |
else |
| 161 |
{ |
| 162 |
RGBImageInfo myImage(image, Description); |
| 163 |
this->RGBImages.push_back(myImage); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
template <> |
| 168 |
void ITKVtkGlue_EXPORT |
| 169 |
QuickView::AddRGBImage<FloatRGBImageType>(FloatRGBImageType * image, bool FlipVertical, std::string Description) |
| 170 |
{ |
| 171 |
using AdaptorType = itk::RGBToVectorImageAdaptor<FloatRGBImageType>; |
| 172 |
AdaptorType::Pointer adaptor = AdaptorType::New(); |
| 173 |
adaptor->SetImage(image); |
| 174 |
|
| 175 |
using rescaleFilterType = itk::VectorRescaleIntensityImageFilter<AdaptorType, UnsignedCharRGBImageType>; |
| 176 |
rescaleFilterType::Pointer rescaler = rescaleFilterType::New(); |
| 177 |
rescaler->SetOutputMaximumMagnitude(255); |
| 178 |
rescaler->SetInput(adaptor); |
| 179 |
rescaler->Update(); |
| 180 |
this->AddRGBImage(rescaler->GetOutput(), FlipVertical, Description); |
| 181 |
} |
| 182 |
|
| 183 |
template <> |
| 184 |
void ITKVtkGlue_EXPORT |
| 185 |
QuickView::AddImage<FloatRGBImageType>(FloatRGBImageType * image, bool FlipVertical, std::string Description) |
| 186 |
{ |
| 187 |
using AdaptorType = itk::RGBToVectorImageAdaptor<FloatRGBImageType>; |
| 188 |
AdaptorType::Pointer adaptor = AdaptorType::New(); |
| 189 |
adaptor->SetImage(image); |
| 190 |
|
| 191 |
using rescaleFilterType = itk::VectorRescaleIntensityImageFilter<AdaptorType, UnsignedCharRGBImageType>; |
| 192 |
rescaleFilterType::Pointer rescaler = rescaleFilterType::New(); |
| 193 |
rescaler->SetOutputMaximumMagnitude(255); |
| 194 |
rescaler->SetInput(adaptor); |
| 195 |
rescaler->Update(); |
| 196 |
this->AddRGBImage(rescaler->GetOutput(), FlipVertical, Description); |
| 197 |
} |
| 198 |
|
| 199 |
void |
| 200 |
QuickView::Visualize(bool interact) |
| 201 |
{ |
| 202 |
unsigned int rendererSize = this->m_ViewPortSize; |
| 203 |
unsigned int numberOfImages = this->Images.size() + this->RGBImages.size(); |
| 204 |
unsigned int numberOfRows = (numberOfImages - 1) / this->m_NumberOfColumns + 1; |
| 205 |
unsigned int numberOfColumns = numberOfImages / (numberOfRows) + 1; |
| 206 |
if (numberOfColumns > numberOfImages) |
| 207 |
{ |
| 208 |
numberOfColumns = numberOfImages; |
| 209 |
} |
| 210 |
|
| 211 |
// Setup the render window and interactor |
| 212 |
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New(); |
| 213 |
renderWindow->SetSize(rendererSize * numberOfColumns, rendererSize * numberOfRows); |
| 214 |
|
| 215 |
vtkSmartPointer<vtkRenderWindowInteractor> interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New(); |
| 216 |
interactor->SetRenderWindow(renderWindow); |
| 217 |
|
| 218 |
// Render all of the images |
| 219 |
std::vector<double *> viewports; |
| 220 |
|
| 221 |
using ConnectorType = itk::ImageToVTKImageFilter<itk::Image<unsigned char, 2>>; |
| 222 |
using RGBConnectorType = itk::ImageToVTKImageFilter<itk::Image<itk::RGBPixel<unsigned char>, 2>>; |
| 223 |
std::vector<ConnectorType::Pointer> |
| 224 |
connectors; // Force the connectors to persist (not lose scope) after each iteration of the loop |
| 225 |
std::vector<RGBConnectorType::Pointer> |
| 226 |
RGBconnectors; // Force the connectors to persist after each iteration of the loop |
| 227 |
|
| 228 |
double background[6] = { .4, .5, .6, .6, .5, .4 }; |
| 229 |
|
| 230 |
vtkSmartPointer<vtkCamera> sharedCamera = vtkSmartPointer<vtkCamera>::New(); |
| 231 |
|
| 232 |
for (unsigned int row = 0; row < numberOfRows; ++row) |
| 233 |
{ |
| 234 |
for (unsigned int col = 0; col < numberOfColumns; ++col) |
| 235 |
{ |
| 236 |
size_t i = row * numberOfColumns + col; |
| 237 |
// (xmin, ymin, xmax, ymax) |
| 238 |
double viewport[4] = { static_cast<double>(col) * rendererSize / (numberOfColumns * rendererSize), |
| 239 |
static_cast<double>(numberOfRows - (row + 1)) * rendererSize / |
| 240 |
(numberOfRows * rendererSize), |
| 241 |
static_cast<double>(col + 1) * rendererSize / (numberOfColumns * rendererSize), |
| 242 |
static_cast<double>(numberOfRows - row) * rendererSize / (numberOfRows * rendererSize) }; |
| 243 |
viewports.push_back(viewport); |
| 244 |
|
| 245 |
// Grayscale images |
| 246 |
if (i < this->Images.size()) |
| 247 |
{ |
| 248 |
ConnectorType::Pointer connector = ConnectorType::New(); |
| 249 |
connectors.push_back(connector); |
| 250 |
connector->SetInput(this->Images[i].m_Image); |
| 251 |
|
| 252 |
vtkSmartPointer<vtkImageActor> actor = vtkSmartPointer<vtkImageActor>::New(); |
| 253 |
#if VTK_MAJOR_VERSION <= 5 |
| 254 |
actor->SetInput(connector->GetOutput()); |
| 255 |
#else |
| 256 |
connector->Update(); |
| 257 |
actor->GetMapper()->SetInputData(connector->GetOutput()); |
| 258 |
#endif |
| 259 |
|
| 260 |
// Setup renderer |
| 261 |
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New(); |
| 262 |
renderWindow->AddRenderer(renderer); |
| 263 |
renderer->SetViewport(viewports[i]); |
| 264 |
renderer->SetBackground(background); |
| 265 |
if (m_ShareCamera) |
| 266 |
{ |
| 267 |
renderer->SetActiveCamera(sharedCamera); |
| 268 |
} |
| 269 |
else |
| 270 |
{ |
| 271 |
vtkSmartPointer<vtkCamera> aCamera = vtkSmartPointer<vtkCamera>::New(); |
| 272 |
renderer->SetActiveCamera(aCamera); |
| 273 |
} |
| 274 |
std::rotate(background, background + 1, background + 6); |
| 275 |
|
| 276 |
if (!this->Images[i].m_Description.empty()) |
| 277 |
{ |
| 278 |
vtkSmartPointer<vtkTextProperty> textProperty = vtkSmartPointer<vtkTextProperty>::New(); |
| 279 |
textProperty->SetFontSize(10); |
| 280 |
textProperty->SetFontFamilyToCourier(); |
| 281 |
textProperty->SetJustificationToCentered(); |
| 282 |
|
| 283 |
vtkSmartPointer<vtkTextMapper> textMapper = vtkSmartPointer<vtkTextMapper>::New(); |
| 284 |
textMapper->SetTextProperty(textProperty); |
| 285 |
textMapper->SetInput(this->Images[i].m_Description.c_str()); |
| 286 |
|
| 287 |
vtkSmartPointer<vtkActor2D> textActor = vtkSmartPointer<vtkActor2D>::New(); |
| 288 |
textActor->SetMapper(textMapper); |
| 289 |
textActor->SetPosition(rendererSize / 2, 16); |
| 290 |
renderer->AddActor(textActor); |
| 291 |
} |
| 292 |
if (m_Interpolate) |
| 293 |
{ |
| 294 |
actor->InterpolateOn(); |
| 295 |
} |
| 296 |
else |
| 297 |
{ |
| 298 |
actor->InterpolateOff(); |
| 299 |
} |
| 300 |
renderer->AddActor(actor); |
| 301 |
renderer->ResetCamera(); |
| 302 |
} |
| 303 |
// RGB Images |
| 304 |
else if (i >= this->Images.size() && i < numberOfImages) |
| 305 |
{ |
| 306 |
unsigned int j = row * numberOfColumns + col - this->Images.size(); |
| 307 |
RGBConnectorType::Pointer connector = RGBConnectorType::New(); |
| 308 |
RGBconnectors.push_back(connector); |
| 309 |
connector->SetInput(this->RGBImages[j].m_Image); |
| 310 |
|
| 311 |
vtkSmartPointer<vtkImageActor> actor = vtkSmartPointer<vtkImageActor>::New(); |
| 312 |
#if VTK_MAJOR_VERSION <= 5 |
| 313 |
actor->SetInput(connector->GetOutput()); |
| 314 |
#else |
| 315 |
connector->Update(); |
| 316 |
actor->GetMapper()->SetInputData(connector->GetOutput()); |
| 317 |
#endif |
| 318 |
|
| 319 |
// Setup renderer |
| 320 |
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New(); |
| 321 |
renderWindow->AddRenderer(renderer); |
| 322 |
renderer->SetViewport(viewports[i]); |
| 323 |
renderer->SetBackground(background); |
| 324 |
if (m_ShareCamera) |
| 325 |
{ |
| 326 |
renderer->SetActiveCamera(sharedCamera); |
| 327 |
} |
| 328 |
else |
| 329 |
{ |
| 330 |
vtkSmartPointer<vtkCamera> aCamera = vtkSmartPointer<vtkCamera>::New(); |
| 331 |
renderer->SetActiveCamera(aCamera); |
| 332 |
} |
| 333 |
std::rotate(background, background + 1, background + 6); |
| 334 |
|
| 335 |
if (!this->RGBImages[j].m_Description.empty()) |
| 336 |
{ |
| 337 |
vtkSmartPointer<vtkTextProperty> textProperty = vtkSmartPointer<vtkTextProperty>::New(); |
| 338 |
textProperty->SetFontSize(10); |
| 339 |
textProperty->SetFontFamilyToCourier(); |
| 340 |
textProperty->SetJustificationToCentered(); |
| 341 |
|
| 342 |
vtkSmartPointer<vtkTextMapper> textMapper = vtkSmartPointer<vtkTextMapper>::New(); |
| 343 |
textMapper->SetTextProperty(textProperty); |
| 344 |
textMapper->SetInput(this->RGBImages[j].m_Description.c_str()); |
| 345 |
|
| 346 |
vtkSmartPointer<vtkActor2D> textActor = vtkSmartPointer<vtkActor2D>::New(); |
| 347 |
textActor->SetMapper(textMapper); |
| 348 |
textActor->SetPosition(rendererSize / 2, 16); |
| 349 |
renderer->AddActor(textActor); |
| 350 |
} |
| 351 |
|
| 352 |
if (m_Interpolate) |
| 353 |
{ |
| 354 |
actor->InterpolateOn(); |
| 355 |
} |
| 356 |
else |
| 357 |
{ |
| 358 |
actor->InterpolateOff(); |
| 359 |
} |
| 360 |
renderer->AddActor(actor); |
| 361 |
renderer->ResetCamera(); |
| 362 |
} |
| 363 |
else |
| 364 |
{ |
| 365 |
// Fill empty viewports |
| 366 |
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New(); |
| 367 |
renderWindow->AddRenderer(renderer); |
| 368 |
renderer->SetViewport(viewports[i]); |
| 369 |
renderer->SetBackground(background); |
| 370 |
continue; |
| 371 |
} |
| 372 |
} |
| 373 |
} |
| 374 |
renderWindow->Render(); |
| 375 |
|
| 376 |
if (m_Snapshot) |
| 377 |
{ |
| 378 |
std::string filename; |
| 379 |
std::stringstream temp; |
| 380 |
temp << m_SnapshotPath << m_SnapshotPrefix << m_Counter << "."; |
| 381 |
filename = temp.str(); |
| 382 |
filename.append(m_SnapshotExtension); |
| 383 |
|
| 384 |
if (m_SnapshotExtension == "png") |
| 385 |
{ |
| 386 |
vtkCaptureScreen<vtkPNGWriter> capture(renderWindow); |
| 387 |
capture(filename); |
| 388 |
} |
| 389 |
if ((m_SnapshotExtension == "jpg") || (m_SnapshotExtension == "jpeg")) |
| 390 |
{ |
| 391 |
vtkCaptureScreen<vtkJPEGWriter> capture(renderWindow); |
| 392 |
capture(filename); |
| 393 |
} |
| 394 |
if (m_SnapshotExtension == "bmp") |
| 395 |
{ |
| 396 |
vtkCaptureScreen<vtkBMPWriter> capture(renderWindow); |
| 397 |
capture(filename); |
| 398 |
} |
| 399 |
if ((m_SnapshotExtension == "tif") || (m_SnapshotExtension == "tiff")) |
| 400 |
{ |
| 401 |
vtkCaptureScreen<vtkTIFFWriter> capture(renderWindow); |
| 402 |
capture(filename); |
| 403 |
} |
| 404 |
m_Counter++; |
| 405 |
} |
| 406 |
|
| 407 |
vtkSmartPointer<vtkInteractorStyleImage> style = vtkSmartPointer<vtkInteractorStyleImage>::New(); |
| 408 |
interactor->SetInteractorStyle(style); |
| 409 |
|
| 410 |
if (interact) |
| 411 |
{ |
| 412 |
interactor->Start(); |
| 413 |
} |
| 414 |
} |