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