Medical Imaging Interaction Toolkit  2018.4.99-12ad79a3
Medical Imaging Interaction Toolkit
mitkImageVtkLegacyIO.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include "mitkImageVtkLegacyIO.h"
14 
15 #include "mitkIOMimeTypes.h"
16 #include "mitkImage.h"
18 
19 #include <vtkErrorCode.h>
20 #include <vtkSmartPointer.h>
21 #include <vtkStructuredPoints.h>
22 #include <vtkStructuredPointsReader.h>
23 #include <vtkStructuredPointsWriter.h>
24 
25 namespace mitk
26 {
28  : AbstractFileIO(Image::GetStaticNameOfClass(), IOMimeTypes::VTK_IMAGE_LEGACY_MIMETYPE(), "VTK Legacy Image")
29  {
30  Options defaultOptions;
31  defaultOptions["Save as binary file"] = false;
32  this->SetDefaultWriterOptions(defaultOptions);
33  this->RegisterService();
34  }
35 
36  std::vector<BaseData::Pointer> ImageVtkLegacyIO::Read()
37  {
38  // The legay vtk reader cannot work with input streams
39  const std::string fileName = this->GetLocalFileName();
40  vtkSmartPointer<vtkStructuredPointsReader> reader = vtkSmartPointer<vtkStructuredPointsReader>::New();
41  reader->SetFileName(fileName.c_str());
42  reader->Update();
43 
44  if (reader->GetOutput() != nullptr)
45  {
47  output->Initialize(reader->GetOutput());
48  output->SetVolume(reader->GetOutput()->GetScalarPointer());
49  std::vector<BaseData::Pointer> result;
50  result.push_back(output.GetPointer());
51  return result;
52  }
53  else
54  {
55  mitkThrow() << "vtkStructuredPointsReader error: "
56  << vtkErrorCode::GetStringFromErrorCode(reader->GetErrorCode());
57  }
58  }
59 
61  {
63  return Unsupported;
64  vtkSmartPointer<vtkStructuredPointsReader> reader = vtkSmartPointer<vtkStructuredPointsReader>::New();
65  reader->SetFileName(this->GetLocalFileName().c_str());
66  if (reader->IsFileStructuredPoints())
67  {
68  return Supported;
69  }
70  return Unsupported;
71  }
72 
74  {
76 
77  const auto *input = dynamic_cast<const Image *>(this->GetInput());
78 
79  vtkSmartPointer<vtkStructuredPointsWriter> writer = vtkSmartPointer<vtkStructuredPointsWriter>::New();
80 
81  // The legacy vtk image writer cannot write to streams
82  LocalFile localFile(this);
83  writer->SetFileName(localFile.GetFileName().c_str());
84 
85  if (us::any_cast<bool>(GetWriterOption("Save as binary file")))
86  {
87  writer->SetFileTypeToBinary();
88  }
89 
90  ImageVtkReadAccessor vtkReadAccessor(Image::ConstPointer(input), nullptr, input->GetVtkImageData());
91  writer->SetInputData(const_cast<vtkImageData *>(vtkReadAccessor.GetVtkImageData()));
92 
93  if (writer->Write() == 0 || writer->GetErrorCode() != 0)
94  {
95  mitkThrow() << "vtkStructuredPointesWriter error: "
96  << vtkErrorCode::GetStringFromErrorCode(writer->GetErrorCode());
97  }
98  }
99 
101  {
103  return Unsupported;
104  const auto *input = static_cast<const Image *>(this->GetInput());
105  if (input->GetDimension() == 3)
106  return Supported;
107  else if (input->GetDimension() < 3)
108  return PartiallySupported;
109  return Unsupported;
110  }
111 
112  ImageVtkLegacyIO *ImageVtkLegacyIO::IOClone() const { return new ImageVtkLegacyIO(*this); }
113 }
The IOMimeTypes class.
ConfidenceLevel GetReaderConfidenceLevel() const override
DataCollection - Class to facilitate loading/accessing structured data.
std::map< std::string, us::Any > Options
Options for reading or writing data.
Definition: mitkIFileIO.h:69
us::Any GetWriterOption(const std::string &name) const
std::string GetLocalFileName() const
Get a local file name for reading.
ConfidenceLevel
A confidence level describing the confidence of the reader or writer in handling the given data...
Definition: mitkIFileIO.h:45
std::pair< us::ServiceRegistration< IFileReader >, us::ServiceRegistration< IFileWriter > > RegisterService(us::ModuleContext *context=us::GetModuleContext())
#define mitkThrow()
const BaseData * GetInput() const override
Get the input data set via SetInput().
Image class for storing images.
Definition: mitkImage.h:72
A local file representation for streams.
void Write() override
Write the base data to the specified location or output stream.
static Pointer New()
ImageVtkReadAccessor class provides any image read access which is required by Vtk methods...
ConfidenceLevel GetReaderConfidenceLevel() const override
ConfidenceLevel GetWriterConfidenceLevel() const override
ConfidenceLevel GetWriterConfidenceLevel() const override
Abstract class for implementing a reader and writer.
void SetDefaultWriterOptions(const Options &defaultOptions)
std::vector< BaseData::Pointer > Read() override
Reads a path or stream and creates a list of BaseData objects.