From effbeb19bc3a3d7d1abb453f5d042c5a1b62c525 Mon Sep 17 00:00:00 2001 From: Rostislav Khlebnikov Date: Thu, 15 Oct 2015 20:42:56 +0100 Subject: [PATCH] Fixed a crash for loading images with active scalars not set. --- Modules/Core/src/IO/mitkImageVtkLegacyIO.cpp | 23 ++++++++++++++++++++++- Modules/Core/src/IO/mitkImageVtkXmlIO.cpp | 23 ++++++++++++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/Modules/Core/src/IO/mitkImageVtkLegacyIO.cpp b/Modules/Core/src/IO/mitkImageVtkLegacyIO.cpp index a24fd7e..a0204fb 100644 --- a/Modules/Core/src/IO/mitkImageVtkLegacyIO.cpp +++ b/Modules/Core/src/IO/mitkImageVtkLegacyIO.cpp @@ -25,6 +25,8 @@ See LICENSE.txt or http://www.mitk.org for details. #include #include #include +#include +#include namespace mitk { @@ -44,9 +46,28 @@ std::vector ImageVtkLegacyIO::Read() if ( reader->GetOutput() != NULL ) { + auto pointData = reader->GetOutput()->GetPointData(); + + if (pointData->GetScalars() == nullptr) + { + for (int i = 0; i < pointData->GetNumberOfArrays(); ++i) + { + if (pointData->GetArray(i)->GetNumberOfComponents() == 1) + { + pointData->SetActiveAttribute(i, vtkDataSetAttributes::SCALARS); + break; + } + } + } + + if (pointData->GetScalars() == nullptr) + { + mitkThrow() << "mitkImageVtkXmlIO error: could not find scalars in the image."; + } + mitk::Image::Pointer output = mitk::Image::New(); output->Initialize(reader->GetOutput()); - output->SetVolume(reader->GetOutput()->GetScalarPointer()); + output->SetVolume(pointData->GetScalars()->GetVoidPointer(0)); std::vector result; result.push_back(output.GetPointer()); return result; diff --git a/Modules/Core/src/IO/mitkImageVtkXmlIO.cpp b/Modules/Core/src/IO/mitkImageVtkXmlIO.cpp index 881f70b..3da6aa9 100644 --- a/Modules/Core/src/IO/mitkImageVtkXmlIO.cpp +++ b/Modules/Core/src/IO/mitkImageVtkXmlIO.cpp @@ -25,6 +25,8 @@ See LICENSE.txt or http://www.mitk.org for details. #include #include #include +#include +#include namespace mitk { @@ -69,9 +71,28 @@ std::vector ImageVtkXmlIO::Read() if (reader->GetOutput() != NULL) { + auto pointData = reader->GetOutput()->GetPointData(); + + if (pointData->GetScalars() == nullptr) + { + for (int i = 0; i < pointData->GetNumberOfArrays(); ++i) + { + if (pointData->GetArray(i)->GetNumberOfComponents() == 1) + { + pointData->SetActiveAttribute(i, vtkDataSetAttributes::SCALARS); + break; + } + } + } + + if (pointData->GetScalars() == nullptr) + { + mitkThrow() << "mitkImageVtkXmlIO error: could not find scalars in the image."; + } + mitk::Image::Pointer output = mitk::Image::New(); output->Initialize(reader->GetOutput()); - output->SetVolume(reader->GetOutput()->GetScalarPointer()); + output->SetVolume(pointData->GetScalars()->GetVoidPointer(0)); std::vector result; result.push_back(output.GetPointer()); return result; -- 1.8.4.msysgit.0