Medical Imaging Interaction Toolkit  2016.11.0
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,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include "mitkImageVtkXmlIO.h"
18 
19 #include "mitkIOMimeTypes.h"
20 #include "mitkImage.h"
22 
23 #include <vtkErrorCode.h>
24 #include <vtkImageData.h>
25 #include <vtkSmartPointer.h>
26 #include <vtkXMLImageDataReader.h>
27 #include <vtkXMLImageDataWriter.h>
28 
29 namespace mitk
30 {
31  class VtkXMLImageDataReader : public ::vtkXMLImageDataReader
32  {
33  public:
34  static VtkXMLImageDataReader *New() { return new VtkXMLImageDataReader(); }
35  vtkTypeMacro(VtkXMLImageDataReader, vtkXMLImageDataReader)
36 
37  void SetStream(std::istream *is)
38  {
39  this->Stream = is;
40  }
41  std::istream *GetStream() const { return this->Stream; }
42  };
43 
44  class VtkXMLImageDataWriter : public ::vtkXMLImageDataWriter
45  {
46  public:
47  static VtkXMLImageDataWriter *New() { return new VtkXMLImageDataWriter(); }
48  vtkTypeMacro(VtkXMLImageDataWriter, vtkXMLImageDataWriter)
49 
50  void SetStream(std::ostream *os)
51  {
52  this->Stream = os;
53  }
54  std::ostream *GetStream() const { return this->Stream; }
55  };
56 
58  : AbstractFileIO(Image::GetStaticNameOfClass(), IOMimeTypes::VTK_IMAGE_MIMETYPE(), "VTK XML Image")
59  {
60  this->RegisterService();
61  }
62 
63  std::vector<BaseData::Pointer> ImageVtkXmlIO::Read()
64  {
65  vtkSmartPointer<VtkXMLImageDataReader> reader = vtkSmartPointer<VtkXMLImageDataReader>::New();
66  if (this->GetInputStream())
67  {
68  reader->SetStream(this->GetInputStream());
69  }
70  else
71  {
72  reader->SetFileName(this->GetInputLocation().c_str());
73  }
74  reader->Update();
75 
76  if (reader->GetOutput() != NULL)
77  {
79  output->Initialize(reader->GetOutput());
80  output->SetVolume(reader->GetOutput()->GetScalarPointer());
81  std::vector<BaseData::Pointer> result;
82  result.push_back(output.GetPointer());
83  return result;
84  }
85  else
86  {
87  mitkThrow() << "vtkXMLImageDataReader error: " << vtkErrorCode::GetStringFromErrorCode(reader->GetErrorCode());
88  }
89  }
90 
92  {
94  return Unsupported;
95  if (this->GetInputStream() == NULL)
96  {
97  // check if the xml vtk reader can handle the file
98  vtkSmartPointer<VtkXMLImageDataReader> xmlReader = vtkSmartPointer<VtkXMLImageDataReader>::New();
99  if (xmlReader->CanReadFile(this->GetInputLocation().c_str()) != 0)
100  {
101  return Supported;
102  }
103  return Unsupported;
104  }
105  // in case of an input stream, VTK does not seem to have methods for
106  // validating it
107  return Supported;
108  }
109 
111  {
113 
114  const Image *input = dynamic_cast<const Image *>(this->GetInput());
115 
116  vtkSmartPointer<VtkXMLImageDataWriter> writer = vtkSmartPointer<VtkXMLImageDataWriter>::New();
117  if (this->GetOutputStream())
118  {
119  writer->SetStream(this->GetOutputStream());
120  }
121  else
122  {
123  writer->SetFileName(this->GetOutputLocation().c_str());
124  }
125 
126  ImageVtkReadAccessor vtkReadAccessor(Image::ConstPointer(input), NULL, input->GetVtkImageData());
127  writer->SetInputData(const_cast<vtkImageData *>(vtkReadAccessor.GetVtkImageData()));
128 
129  if (writer->Write() == 0 || writer->GetErrorCode() != 0)
130  {
131  mitkThrow() << "vtkXMLImageDataWriter error: " << vtkErrorCode::GetStringFromErrorCode(writer->GetErrorCode());
132  }
133  }
134 
136  {
138  return Unsupported;
139  const Image *input = static_cast<const Image *>(this->GetInput());
140  if (input->GetDimension() == 3)
141  return Supported;
142  else if (input->GetDimension() < 3)
143  return PartiallySupported;
144  return Unsupported;
145  }
146 
147  ImageVtkXmlIO *ImageVtkXmlIO::IOClone() const { return new ImageVtkXmlIO(*this); }
148 }
The IOMimeTypes class.
virtual ConfidenceLevel GetReaderConfidenceLevel() const override
virtual std::istream * GetInputStream() const override
Get the input stream.
STL namespace.
DataCollection - Class to facilitate loading/accessing structured data.
virtual vtkImageData * GetVtkImageData(int t=0, int n=0)
Get a volume at a specific time t of channel n as a vtkImageData.
Definition: mitkImage.cpp:221
virtual ConfidenceLevel GetReaderConfidenceLevel() const override
std::pair< us::ServiceRegistration< IFileReader >, us::ServiceRegistration< IFileWriter > > RegisterService(us::ModuleContext *context=us::GetModuleContext())
#define mitkThrow()
virtual const BaseData * GetInput() const override
Get the input data set via SetInput().
Image class for storing images.
Definition: mitkImage.h:76
static const char * GetStaticNameOfClass()
static Pointer New()
virtual ConfidenceLevel GetWriterConfidenceLevel() const override
virtual 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...
virtual std::ostream * GetOutputStream() const override
Get the output stream.
virtual std::string GetOutputLocation() const override
Get the current output location.
ConfidenceLevel
A confidence level describing the confidence of the reader or writer in handling the given data...
Definition: mitkIFileIO.h:49
virtual ConfidenceLevel GetWriterConfidenceLevel() const override
virtual std::vector< BaseData::Pointer > Read() override
Reads a path or stream and creates a list of BaseData objects.
virtual std::string GetInputLocation() const override
Get the current input location.
unsigned int GetDimension() const
Get dimension of the image.
Definition: mitkImage.cpp:110
Abstract class for implementing a reader and writer.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.