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