Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
Core/src/IO/mitkRawImageFileReader.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 "mitkRawImageFileReader.h"
18 #include "mitkIOConstants.h"
19 #include "mitkIOMimeTypes.h"
20 #include "mitkITKImageImport.h"
21 #include "mitkImageCast.h"
22 
23 #include <itkImage.h>
24 #include <itkImageFileReader.h>
25 #include <itkRawImageIO.h>
26 
28  : AbstractFileReader(CustomMimeType(IOMimeTypes::RAW_MIMETYPE()), "ITK raw image reader")
29 {
30  Options defaultOptions;
31 
33 
34  std::vector<std::string> pixelEnum;
35  pixelEnum.push_back(IOConstants::PIXEL_TYPE_UCHAR());
36  pixelEnum.push_back(IOConstants::PIXEL_TYPE_CHAR());
37  pixelEnum.push_back(IOConstants::PIXEL_TYPE_USHORT());
38  pixelEnum.push_back(IOConstants::PIXEL_TYPE_SHORT());
39  pixelEnum.push_back(IOConstants::PIXEL_TYPE_UINT());
40  pixelEnum.push_back(IOConstants::PIXEL_TYPE_INT());
41  pixelEnum.push_back(IOConstants::PIXEL_TYPE_FLOAT());
42  pixelEnum.push_back(IOConstants::PIXEL_TYPE_DOUBLE());
43  defaultOptions[IOConstants::PIXEL_TYPE_ENUM()] = pixelEnum;
44 
45  defaultOptions[IOConstants::DIMENSION()] = std::string("3");
46  std::vector<std::string> dimEnum;
47  dimEnum.push_back("2");
48  dimEnum.push_back("3");
49  defaultOptions[IOConstants::DIMENSION_ENUM()] = dimEnum;
50 
52  std::vector<std::string> endianEnum;
53  endianEnum.push_back(IOConstants::ENDIANNESS_LITTLE());
54  endianEnum.push_back(IOConstants::ENDIANNESS_BIG());
55  defaultOptions[IOConstants::ENDIANNESS_ENUM()] = endianEnum;
56 
57  defaultOptions[IOConstants::SIZE_X()] = 0;
58  defaultOptions[IOConstants::SIZE_Y()] = 0;
59  defaultOptions[IOConstants::SIZE_Z()] = 0;
60  // defaultOptions[IOConstants::SIZE_T()] = 0;
61 
62  this->SetDefaultOptions(defaultOptions);
63 
64  this->RegisterService();
65 }
66 
68  : AbstractFileReader(other)
69 {
70 }
71 
72 std::vector<itk::SmartPointer<mitk::BaseData>> mitk::RawImageFileReaderService::Read()
73 {
74  std::vector<mitk::BaseData::Pointer> result;
75 
76  const std::string path = this->GetLocalFileName();
77 
78  const Options options = this->GetOptions();
79 
80  const std::string dimensionality = options.find(IOConstants::DIMENSION())->second.ToString();
81  const std::string pixelType = options.find(IOConstants::PIXEL_TYPE())->second.ToString();
82 
83  EndianityType endianity =
84  options.find(IOConstants::ENDIANNESS())->second.ToString() == IOConstants::ENDIANNESS_LITTLE() ? LITTLE : BIG;
85  int dimensions[4];
86  dimensions[0] = us::any_cast<int>(options.find(IOConstants::SIZE_X())->second);
87  dimensions[1] = us::any_cast<int>(options.find(IOConstants::SIZE_Y())->second);
88  dimensions[2] = us::any_cast<int>(options.find(IOConstants::SIZE_Z())->second);
89  dimensions[3] = 0; // us::any_cast<int>(options.find(IOConstants::SIZE_T())->second);
90 
91  // check file dimensionality and pixel type and perform reading according to it
92  if (dimensionality == "2")
93  {
94  if (pixelType == IOConstants::PIXEL_TYPE_CHAR())
95  result.push_back(TypedRead<signed char, 2>(path, endianity, dimensions));
96  else if (pixelType == IOConstants::PIXEL_TYPE_UCHAR())
97  result.push_back(TypedRead<unsigned char, 2>(path, endianity, dimensions));
98  else if (pixelType == IOConstants::PIXEL_TYPE_SHORT())
99  result.push_back(TypedRead<signed short int, 2>(path, endianity, dimensions));
100  else if (pixelType == IOConstants::PIXEL_TYPE_USHORT())
101  result.push_back(TypedRead<unsigned short int, 2>(path, endianity, dimensions));
102  else if (pixelType == IOConstants::PIXEL_TYPE_UINT())
103  result.push_back(TypedRead<unsigned int, 2>(path, endianity, dimensions));
104  else if (pixelType == IOConstants::PIXEL_TYPE_INT())
105  result.push_back(TypedRead<signed int, 2>(path, endianity, dimensions));
106  else if (pixelType == IOConstants::PIXEL_TYPE_FLOAT())
107  result.push_back(TypedRead<float, 2>(path, endianity, dimensions));
108  else if (pixelType == IOConstants::PIXEL_TYPE_DOUBLE())
109  result.push_back(TypedRead<double, 2>(path, endianity, dimensions));
110  else
111  {
112  MITK_INFO << "Error while reading raw file: Dimensionality or pixel type not supported or not properly set"
113  << std::endl;
114  }
115  }
116  else if (dimensionality == "3")
117  {
118  if (pixelType == IOConstants::PIXEL_TYPE_CHAR())
119  result.push_back(TypedRead<signed char, 3>(path, endianity, dimensions));
120  else if (pixelType == IOConstants::PIXEL_TYPE_UCHAR())
121  result.push_back(TypedRead<unsigned char, 3>(path, endianity, dimensions));
122  else if (pixelType == IOConstants::PIXEL_TYPE_SHORT())
123  result.push_back(TypedRead<signed short int, 3>(path, endianity, dimensions));
124  else if (pixelType == IOConstants::PIXEL_TYPE_USHORT())
125  result.push_back(TypedRead<unsigned short int, 3>(path, endianity, dimensions));
126  else if (pixelType == IOConstants::PIXEL_TYPE_UINT())
127  result.push_back(TypedRead<unsigned int, 3>(path, endianity, dimensions));
128  else if (pixelType == IOConstants::PIXEL_TYPE_INT())
129  result.push_back(TypedRead<signed int, 3>(path, endianity, dimensions));
130  else if (pixelType == IOConstants::PIXEL_TYPE_FLOAT())
131  result.push_back(TypedRead<float, 3>(path, endianity, dimensions));
132  else if (pixelType == IOConstants::PIXEL_TYPE_DOUBLE())
133  result.push_back(TypedRead<double, 3>(path, endianity, dimensions));
134  else
135  {
136  MITK_INFO << "Error while reading raw file: Dimensionality or pixel type not supported or not properly set"
137  << std::endl;
138  }
139  }
140  else
141  {
142  MITK_INFO << "Error while reading raw file: Dimensionality not supported" << std::endl;
143  }
144 
145  return result;
146 }
147 
148 template <typename TPixel, unsigned int VImageDimensions>
149 mitk::BaseData::Pointer mitk::RawImageFileReaderService::TypedRead(const std::string &path,
150  EndianityType endianity,
151  int *size)
152 {
153  typedef itk::Image<TPixel, VImageDimensions> ImageType;
154  typedef itk::ImageFileReader<ImageType> ReaderType;
155  typedef itk::RawImageIO<TPixel, VImageDimensions> IOType;
156 
157  typename ReaderType::Pointer reader = ReaderType::New();
158  typename IOType::Pointer io = IOType::New();
159 
160  io->SetFileDimensionality(VImageDimensions);
161 
162  for (unsigned short int dim = 0; dim < VImageDimensions; ++dim)
163  {
164  io->SetDimensions(dim, size[dim]);
165  }
166 
167  if (endianity == LITTLE)
168  {
169  io->SetByteOrderToLittleEndian();
170  }
171  else if (endianity == BIG)
172  {
173  io->SetByteOrderToBigEndian();
174  }
175  else
176  {
177  MITK_INFO << "Warning: endianity not properly set. Resulting image might be incorrect";
178  }
179 
180  reader->SetImageIO(io);
181  reader->SetFileName(path);
182 
183  try
184  {
185  reader->Update();
186  }
187  catch (itk::ExceptionObject &err)
188  {
189  MITK_ERROR << "An error occurred during the raw image reading process: ";
190  MITK_INFO << err << std::endl;
191  }
192 
194  mitk::CastToMitkImage(reader->GetOutput(), image);
195  image->SetVolume(reader->GetOutput()->GetBufferPointer());
196  return image.GetPointer();
197 }
198 
199 mitk::RawImageFileReaderService *mitk::RawImageFileReaderService::Clone() const
200 {
201  return new RawImageFileReaderService(*this);
202 }
itk::SmartPointer< Self > Pointer
The IOMimeTypes class.
static std::string PIXEL_TYPE_USHORT()
static std::string PIXEL_TYPE_DOUBLE()
#define MITK_INFO
Definition: mitkLogMacros.h:22
static std::string DIMENSION_ENUM()
static std::string ENDIANNESS_ENUM()
#define MITK_ERROR
Definition: mitkLogMacros.h:24
ValueType * any_cast(Any *operand)
Definition: usAny.h:377
std::map< std::string, us::Any > Options
Options for reading or writing data.
Definition: mitkIFileIO.h:73
static std::string PIXEL_TYPE_CHAR()
static std::string ENDIANNESS_BIG()
static std::string SIZE_Z()
The CustomMimeType class represents a custom mime-type which may be registered as a service object...
static std::string PIXEL_TYPE_UINT()
map::core::discrete::Elements< 3 >::InternalImageType ImageType
void SetDefaultOptions(const Options &defaultOptions)
us::ServiceRegistration< IFileReader > RegisterService(us::ModuleContext *context=us::GetModuleContext())
static std::string PIXEL_TYPE_INT()
static std::string DIMENSION()
static Pointer New()
static std::string SIZE_X()
static std::string ENDIANNESS_LITTLE()
void CastToMitkImage(const itk::SmartPointer< ItkOutputImageType > &itkimage, itk::SmartPointer< mitk::Image > &mitkoutputimage)
Cast an itk::Image (with a specific type) to an mitk::Image.
Definition: mitkImageCast.h:78
Base class for creating mitk::BaseData objects from files or streams.
static std::string PIXEL_TYPE_SHORT()
static std::string ENDIANNESS()
virtual std::vector< itk::SmartPointer< BaseData > > Read() override
Reads a path or stream and creates a list of BaseData objects.
static std::string PIXEL_TYPE_FLOAT()
static std::string SIZE_Y()
static std::string PIXEL_TYPE_UCHAR()
static std::string PIXEL_TYPE_ENUM()
static std::string PIXEL_TYPE()
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.