Medical Imaging Interaction Toolkit  2016.11.0
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,
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 "mitkImageVtkLegacyIO.h"
18 
19 #include "mitkIOMimeTypes.h"
20 #include "mitkImage.h"
22 
23 #include <vtkErrorCode.h>
24 #include <vtkSmartPointer.h>
25 #include <vtkStructuredPoints.h>
26 #include <vtkStructuredPointsReader.h>
27 #include <vtkStructuredPointsWriter.h>
28 
29 namespace mitk
30 {
32  : AbstractFileIO(Image::GetStaticNameOfClass(), IOMimeTypes::VTK_IMAGE_LEGACY_MIMETYPE(), "VTK Legacy Image")
33  {
34  Options defaultOptions;
35  defaultOptions["Save as binary file"] = false;
36  this->SetDefaultWriterOptions(defaultOptions);
37  this->RegisterService();
38  }
39 
40  std::vector<BaseData::Pointer> ImageVtkLegacyIO::Read()
41  {
42  // The legay vtk reader cannot work with input streams
43  const std::string fileName = this->GetLocalFileName();
44  vtkSmartPointer<vtkStructuredPointsReader> reader = vtkSmartPointer<vtkStructuredPointsReader>::New();
45  reader->SetFileName(fileName.c_str());
46  reader->Update();
47 
48  if (reader->GetOutput() != NULL)
49  {
51  output->Initialize(reader->GetOutput());
52  output->SetVolume(reader->GetOutput()->GetScalarPointer());
53  std::vector<BaseData::Pointer> result;
54  result.push_back(output.GetPointer());
55  return result;
56  }
57  else
58  {
59  mitkThrow() << "vtkStructuredPointsReader error: "
60  << vtkErrorCode::GetStringFromErrorCode(reader->GetErrorCode());
61  }
62  }
63 
65  {
67  return Unsupported;
68  vtkSmartPointer<vtkStructuredPointsReader> reader = vtkSmartPointer<vtkStructuredPointsReader>::New();
69  reader->SetFileName(this->GetLocalFileName().c_str());
70  if (reader->IsFileStructuredPoints())
71  {
72  return Supported;
73  }
74  return Unsupported;
75  }
76 
78  {
80 
81  const Image *input = dynamic_cast<const Image *>(this->GetInput());
82 
83  vtkSmartPointer<vtkStructuredPointsWriter> writer = vtkSmartPointer<vtkStructuredPointsWriter>::New();
84 
85  // The legacy vtk image writer cannot write to streams
86  LocalFile localFile(this);
87  writer->SetFileName(localFile.GetFileName().c_str());
88 
89  if (us::any_cast<bool>(GetWriterOption("Save as binary file")))
90  {
91  writer->SetFileTypeToBinary();
92  }
93 
94  ImageVtkReadAccessor vtkReadAccessor(Image::ConstPointer(input), NULL, input->GetVtkImageData());
95  writer->SetInputData(const_cast<vtkImageData *>(vtkReadAccessor.GetVtkImageData()));
96 
97  if (writer->Write() == 0 || writer->GetErrorCode() != 0)
98  {
99  mitkThrow() << "vtkStructuredPointesWriter error: "
100  << vtkErrorCode::GetStringFromErrorCode(writer->GetErrorCode());
101  }
102  }
103 
105  {
107  return Unsupported;
108  const Image *input = static_cast<const Image *>(this->GetInput());
109  if (input->GetDimension() == 3)
110  return Supported;
111  else if (input->GetDimension() < 3)
112  return PartiallySupported;
113  return Unsupported;
114  }
115 
116  ImageVtkLegacyIO *ImageVtkLegacyIO::IOClone() const { return new ImageVtkLegacyIO(*this); }
117 }
The IOMimeTypes class.
virtual 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:73
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
std::string GetLocalFileName() const
Get a local file name for reading.
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()
A local file representation for streams.
virtual 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...
virtual ConfidenceLevel GetReaderConfidenceLevel() const override
virtual ConfidenceLevel GetWriterConfidenceLevel() const override
us::Any GetWriterOption(const std::string &name) const
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
unsigned int GetDimension() const
Get dimension of the image.
Definition: mitkImage.cpp:110
Abstract class for implementing a reader and writer.
void SetDefaultWriterOptions(const Options &defaultOptions)
virtual std::vector< BaseData::Pointer > Read() override
Reads a path or stream and creates a list of BaseData objects.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.