Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkImageVtkXmlIO.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 "mitkImageVtkXmlIO.h"
14 
15 #include "mitkIOMimeTypes.h"
16 #include "mitkImage.h"
18 
19 #include <vtkErrorCode.h>
20 #include <vtkImageData.h>
21 #include <vtkSmartPointer.h>
22 #include <vtkXMLImageDataReader.h>
23 #include <vtkXMLImageDataWriter.h>
24 
25 namespace mitk
26 {
27  class VtkXMLImageDataReader : public ::vtkXMLImageDataReader
28  {
29  public:
30  static VtkXMLImageDataReader *New() { return new VtkXMLImageDataReader(); }
31  vtkTypeMacro(VtkXMLImageDataReader, vtkXMLImageDataReader)
32 
33  void SetStream(std::istream *is)
34  {
35  this->Stream = is;
36  }
37  std::istream *GetStream() const { return this->Stream; }
38  };
39 
40  class VtkXMLImageDataWriter : public ::vtkXMLImageDataWriter
41  {
42  public:
43  static VtkXMLImageDataWriter *New() { return new VtkXMLImageDataWriter(); }
44  vtkTypeMacro(VtkXMLImageDataWriter, vtkXMLImageDataWriter)
45 
46  void SetStream(std::ostream *os)
47  {
48  this->Stream = os;
49  }
50  std::ostream *GetStream() const { return this->Stream; }
51  };
52 
54  : AbstractFileIO(Image::GetStaticNameOfClass(), IOMimeTypes::VTK_IMAGE_MIMETYPE(), "VTK XML Image")
55  {
56  this->RegisterService();
57  }
58 
59  std::vector<BaseData::Pointer> ImageVtkXmlIO::Read()
60  {
61  vtkSmartPointer<VtkXMLImageDataReader> reader = vtkSmartPointer<VtkXMLImageDataReader>::New();
62  if (this->GetInputStream())
63  {
64  reader->SetStream(this->GetInputStream());
65  }
66  else
67  {
68  reader->SetFileName(this->GetInputLocation().c_str());
69  }
70  reader->Update();
71 
72  if (reader->GetOutput() != nullptr)
73  {
75  output->Initialize(reader->GetOutput());
76  output->SetVolume(reader->GetOutput()->GetScalarPointer());
77  std::vector<BaseData::Pointer> result;
78  result.push_back(output.GetPointer());
79  return result;
80  }
81  else
82  {
83  mitkThrow() << "vtkXMLImageDataReader error: " << vtkErrorCode::GetStringFromErrorCode(reader->GetErrorCode());
84  }
85  }
86 
88  {
90  return Unsupported;
91  if (this->GetInputStream() == nullptr)
92  {
93  // check if the xml vtk reader can handle the file
94  vtkSmartPointer<VtkXMLImageDataReader> xmlReader = vtkSmartPointer<VtkXMLImageDataReader>::New();
95  if (xmlReader->CanReadFile(this->GetInputLocation().c_str()) != 0)
96  {
97  return Supported;
98  }
99  return Unsupported;
100  }
101  // in case of an input stream, VTK does not seem to have methods for
102  // validating it
103  return Supported;
104  }
105 
107  {
109 
110  const auto *input = dynamic_cast<const Image *>(this->GetInput());
111 
112  vtkSmartPointer<VtkXMLImageDataWriter> writer = vtkSmartPointer<VtkXMLImageDataWriter>::New();
113  if (this->GetOutputStream())
114  {
115  writer->SetStream(this->GetOutputStream());
116  }
117  else
118  {
119  writer->SetFileName(this->GetOutputLocation().c_str());
120  }
121 
122  ImageVtkReadAccessor vtkReadAccessor(Image::ConstPointer(input), nullptr, input->GetVtkImageData());
123  writer->SetInputData(const_cast<vtkImageData *>(vtkReadAccessor.GetVtkImageData()));
124 
125  if (writer->Write() == 0 || writer->GetErrorCode() != 0)
126  {
127  mitkThrow() << "vtkXMLImageDataWriter error: " << vtkErrorCode::GetStringFromErrorCode(writer->GetErrorCode());
128  }
129  }
130 
132  {
134  return Unsupported;
135  const auto *input = static_cast<const Image *>(this->GetInput());
136  if (input->GetDimension() == 3)
137  return Supported;
138  else if (input->GetDimension() < 3)
139  return PartiallySupported;
140  return Unsupported;
141  }
142 
143  ImageVtkXmlIO *ImageVtkXmlIO::IOClone() const { return new ImageVtkXmlIO(*this); }
144 }
The IOMimeTypes class.
ConfidenceLevel GetReaderConfidenceLevel() const override
std::istream * GetInputStream() const override
Get the input stream.
DataCollection - Class to facilitate loading/accessing structured data.
ConfidenceLevel GetReaderConfidenceLevel() const override
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
static Pointer New()
ConfidenceLevel GetWriterConfidenceLevel() const override
void Write() override
Write the base data to the specified location or output stream.
ImageVtkReadAccessor class provides any image read access which is required by Vtk methods...
std::ostream * GetOutputStream() const override
Get the output stream.
std::string GetOutputLocation() const override
Get the current output location.
ConfidenceLevel GetWriterConfidenceLevel() const override
std::vector< BaseData::Pointer > Read() override
Reads a path or stream and creates a list of BaseData objects.
std::string GetInputLocation() const override
Get the current input location.
Abstract class for implementing a reader and writer.