Medical Imaging Interaction Toolkit  2018.4.99-6aa36ba9
Medical Imaging Interaction Toolkit
LegacyIO/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 "mitkImageCast.h"
15 
16 #include <itkImage.h>
17 #include <itkImageFileReader.h>
18 #include <itkRawImageIO.h>
19 
20 mitk::RawImageFileReader::RawImageFileReader() : m_FileName(""), m_FilePrefix(""), m_FilePattern("")
21 {
22 }
23 
25 {
26 }
27 
28 void mitk::RawImageFileReader::SetDimensions(unsigned int i, unsigned int dim)
29 {
30  if (i > 2)
31  return;
32 
33  this->Modified(); // TODO: this order (first modified, then set the variable) is intended??
34  m_Dimensions[i] = dim;
35 }
36 
37 unsigned int mitk::RawImageFileReader::GetDimensions(unsigned int i) const
38 {
39  if (i > 2)
40  return 0;
41 
42  return m_Dimensions[i];
43 }
44 
45 bool mitk::RawImageFileReader::CanReadFile(const std::string filename,
46  const std::string filePrefix,
47  const std::string filePattern)
48 {
49  // First check the extension
50  if (filename == "")
51  return false;
52 
53  // check if image is serie
54  if (filePattern != "" && filePrefix != "")
55  return false;
56 
57  return true;
58 }
59 
61 {
62  mitk::Image::Pointer output = this->GetOutput();
63 
64  if (this->GetOutput() == nullptr)
65  {
66  MITK_INFO << "Error" << std::endl;
67  }
68 
69  // Check to see if we can read the file given the name or prefix
70  if (m_FileName == "")
71  {
72  itkWarningMacro(<< "File Type not supported!");
73  return;
74  }
75 
76  // check file dimensionality and pixel type and perform reading according to it
77  if (m_Dimensionality == 2)
78  {
79  if (m_PixelType == SCHAR)
80  TypedGenerateData<signed char, 2>();
81  else if (m_PixelType == UCHAR)
82  TypedGenerateData<unsigned char, 2>();
83  else if (m_PixelType == SSHORT)
84  TypedGenerateData<signed short int, 2>();
85  else if (m_PixelType == USHORT)
86  TypedGenerateData<unsigned short int, 2>();
87  else if (m_PixelType == UINT)
88  TypedGenerateData<unsigned int, 2>();
89  else if (m_PixelType == SINT)
90  TypedGenerateData<signed int, 2>();
91  else if (m_PixelType == FLOAT)
92  TypedGenerateData<float, 2>();
93  else if (m_PixelType == DOUBLE)
94  TypedGenerateData<double, 2>();
95  else
96  {
97  MITK_INFO << "Error while reading raw file: Dimensionality or pixel type not supported or not properly set"
98  << std::endl;
99  return;
100  }
101  }
102  else if (m_Dimensionality == 3)
103  {
104  if (m_PixelType == SCHAR)
105  TypedGenerateData<signed char, 3>();
106  else if (m_PixelType == UCHAR)
107  TypedGenerateData<unsigned char, 3>();
108  else if (m_PixelType == SSHORT)
109  TypedGenerateData<signed short int, 3>();
110  else if (m_PixelType == USHORT)
111  TypedGenerateData<unsigned short int, 3>();
112  else if (m_PixelType == UINT)
113  TypedGenerateData<unsigned int, 3>();
114  else if (m_PixelType == SINT)
115  TypedGenerateData<signed int, 3>();
116  else if (m_PixelType == FLOAT)
117  TypedGenerateData<float, 3>();
118  else if (m_PixelType == DOUBLE)
119  TypedGenerateData<double, 3>();
120  else
121  {
122  MITK_INFO << "Error while reading raw file: Dimensionality or pixel type not supported or not properly set"
123  << std::endl;
124  return;
125  }
126  }
127  else
128  {
129  MITK_INFO << "Error while reading raw file: Dimensionality not supported" << std::endl;
130  return;
131  }
132 
133  MITK_INFO << "...reading raw finished!" << std::endl;
134 }
135 
136 template <typename TPixel, unsigned int VImageDimensions>
138 {
139  mitk::Image::Pointer output = this->GetOutput();
140 
141  if (this->GetOutput() == nullptr)
142  {
143  MITK_INFO << "Error" << std::endl;
144  }
145 
146  MITK_INFO << "loading " << m_FileName << " via itk::ImageIOFactory... " << std::endl;
147 
148  // Check to see if we can read the file given the name or prefix
149  if (m_FileName == "")
150  {
151  itkWarningMacro(<< "File Type not supported!");
152  return;
153  }
154 
155  typedef itk::Image<TPixel, VImageDimensions> ImageType;
156  typedef itk::ImageFileReader<ImageType> ReaderType;
157  typedef itk::RawImageIO<TPixel, VImageDimensions> IOType;
158 
159  typename ReaderType::Pointer reader = ReaderType::New();
160  typename IOType::Pointer io = IOType::New();
161 
162  io->SetFileDimensionality(VImageDimensions);
163 
164  for (unsigned short int dim = 0; dim < VImageDimensions; ++dim)
165  {
166  io->SetDimensions(dim, m_Dimensions[dim]);
167  }
168 
169  if (m_Endianity == LITTLE)
170  {
171  io->SetByteOrderToLittleEndian();
172  }
173  else if (m_Endianity == BIG)
174  {
175  io->SetByteOrderToBigEndian();
176  }
177  else
178  {
179  MITK_INFO << "Warning: endianity not properly set. Resulting image might be incorrect";
180  }
181 
182  reader->SetImageIO(io);
183  reader->SetFileName(m_FileName.c_str());
184 
185  try
186  {
187  reader->Update();
188  }
189  catch (itk::ExceptionObject &err)
190  {
191  MITK_ERROR << "An error occurred during the raw image reading process: ";
192  MITK_INFO << err << std::endl;
193  }
194 
196  mitk::CastToMitkImage(reader->GetOutput(), image);
197  output->Initialize(image);
198  output->SetVolume(reader->GetOutput()->GetBufferPointer());
199 }
void SetDimensions(unsigned int i, unsigned int dim)
#define MITK_INFO
Definition: mitkLogMacros.h:18
itk::Image< unsigned char, 3 > ImageType
#define MITK_ERROR
Definition: mitkLogMacros.h:20
static bool CanReadFile(const std::string filename, const std::string filePrefix, const std::string filePattern)
unsigned int GetDimensions(unsigned int i) const
mitk::Image::Pointer image
static Pointer New()
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
OutputType * GetOutput()
Get the output data of this image source object.