Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkItkImageFileReader.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 "mitkItkImageFileReader.h"
18 #include "mitkConfig.h"
19 #include "mitkException.h"
20 #include <mitkLocaleSwitch.h>
22 
23 #include <itkImage.h>
24 #include <itkImageFileReader.h>
25 #include <itksys/Directory.hxx>
26 #include <itksys/SystemTools.hxx>
27 //#include <itkImageSeriesReader.h>
28 #include <itkImageFileReader.h>
29 #include <itkImageIOFactory.h>
30 #include <itkImageIORegion.h>
31 #include <itkMetaDataObject.h>
32 //#include <itkImageSeriesReader.h>
33 //#include <itkDICOMImageIO2.h>
34 //#include <itkDICOMSeriesFileNames.h>
35 //#include <itkGDCMImageIO.h>
36 //#include <itkGDCMSeriesFileNames.h>
37 //#include <itkNumericSeriesFileNames.h>
38 
40 {
41  mitk::LocaleSwitch localeSwitch("C");
42  mitk::Image::Pointer image = this->GetOutput();
43 
44  const unsigned int MINDIM = 2;
45  const unsigned int MAXDIM = 4;
46 
47  MITK_INFO("mitkItkImageFileReader") << "loading " << m_FileName << " via itk::ImageIOFactory... " << std::endl;
48 
49  // Check to see if we can read the file given the name or prefix
50  if (m_FileName == "")
51  {
52  mitkThrow() << "Empty filename in mitk::ItkImageFileReader ";
53  return;
54  }
55 
57  itk::ImageIOFactory::CreateImageIO(m_FileName.c_str(), itk::ImageIOFactory::ReadMode);
58  if (imageIO.IsNull())
59  {
60  // itkWarningMacro( << "File Type not supported!" );
61  mitkThrow() << "Could not create itk::ImageIOBase object for filename " << m_FileName;
62  return;
63  }
64 
65  // Got to allocate space for the image. Determine the characteristics of
66  // the image.
67  imageIO->SetFileName(m_FileName.c_str());
68  imageIO->ReadImageInformation();
69 
70  unsigned int ndim = imageIO->GetNumberOfDimensions();
71  if (ndim < MINDIM || ndim > MAXDIM)
72  {
73  itkWarningMacro(<< "Sorry, only dimensions 2, 3 and 4 are supported. The given file has " << ndim
74  << " dimensions! Reading as 4D.");
75  ndim = MAXDIM;
76  }
77 
78  itk::ImageIORegion ioRegion(ndim);
79  itk::ImageIORegion::SizeType ioSize = ioRegion.GetSize();
80  itk::ImageIORegion::IndexType ioStart = ioRegion.GetIndex();
81 
82  unsigned int dimensions[MAXDIM];
83  dimensions[0] = 0;
84  dimensions[1] = 0;
85  dimensions[2] = 0;
86  dimensions[3] = 0;
87 
88  ScalarType spacing[MAXDIM];
89  spacing[0] = 1.0f;
90  spacing[1] = 1.0f;
91  spacing[2] = 1.0f;
92  spacing[3] = 1.0f;
93 
94  Point3D origin;
95  origin.Fill(0);
96 
97  unsigned int i;
98  for (i = 0; i < ndim; ++i)
99  {
100  ioStart[i] = 0;
101  ioSize[i] = imageIO->GetDimensions(i);
102  if (i < MAXDIM)
103  {
104  dimensions[i] = imageIO->GetDimensions(i);
105  spacing[i] = imageIO->GetSpacing(i);
106  if (spacing[i] <= 0)
107  spacing[i] = 1.0f;
108  }
109  if (i < 3)
110  {
111  origin[i] = imageIO->GetOrigin(i);
112  }
113  }
114 
115  ioRegion.SetSize(ioSize);
116  ioRegion.SetIndex(ioStart);
117 
118  MITK_INFO("mitkItkImageFileReader") << "ioRegion: " << ioRegion << std::endl;
119  imageIO->SetIORegion(ioRegion);
120  void *buffer = new unsigned char[imageIO->GetImageSizeInBytes()];
121  imageIO->Read(buffer);
122 
123  image->Initialize(MakePixelType(imageIO), ndim, dimensions);
124  image->SetImportChannel(buffer, 0, Image::ManageMemory);
125 
126  // access direction of itk::Image and include spacing
127  mitk::Matrix3D matrix;
128  matrix.SetIdentity();
129  unsigned int j, itkDimMax3 = (ndim >= 3 ? 3 : ndim);
130  for (i = 0; i < itkDimMax3; ++i)
131  for (j = 0; j < itkDimMax3; ++j)
132  matrix[i][j] = imageIO->GetDirection(j)[i];
133 
134  // re-initialize PlaneGeometry with origin and direction
135  PlaneGeometry *planeGeometry = static_cast<PlaneGeometry *>(image->GetSlicedGeometry(0)->GetPlaneGeometry(0));
136  planeGeometry->SetOrigin(origin);
137  planeGeometry->GetIndexToWorldTransform()->SetMatrix(matrix);
138 
139  // re-initialize SlicedGeometry3D
140  SlicedGeometry3D *slicedGeometry = image->GetSlicedGeometry(0);
141  slicedGeometry->InitializeEvenlySpaced(planeGeometry, image->GetDimension(2));
142  slicedGeometry->SetSpacing(spacing);
143 
144  MITK_INFO("mitkItkImageFileReader") << slicedGeometry->GetCornerPoint(false, false, false);
145  MITK_INFO("mitkItkImageFileReader") << slicedGeometry->GetCornerPoint(true, true, true);
146 
147  // re-initialize TimeGeometry
149  timeGeometry->Initialize(slicedGeometry, image->GetDimension(3));
150  image->SetTimeGeometry(timeGeometry);
151 
152  buffer = nullptr;
153  MITK_INFO("mitkItkImageFileReader") << "number of image components: " << image->GetPixelType().GetNumberOfComponents()
154  << std::endl;
155  // mitk::DataNode::Pointer node = this->GetOutput();
156  // node->SetData( image );
157 
158  // add level-window property
159  // if ( image->GetPixelType().GetNumberOfComponents() == 1 )
160  //{
161  // SetDefaultImageProperties( node );
162  //}
163  MITK_INFO("mitkItkImageFileReader") << "...finished!" << std::endl;
164 }
165 
167  const std::string filePrefix,
168  const std::string filePattern)
169 {
170  // First check the extension
171  if (filename == "")
172  return false;
173 
174  // check if image is serie
175  if (filePattern != "" && filePrefix != "")
176  return false;
177 
178  itk::ImageIOBase::Pointer imageIO =
179  itk::ImageIOFactory::CreateImageIO(filename.c_str(), itk::ImageIOFactory::ReadMode);
180  if (imageIO.IsNull())
181  return false;
182 
183  try
184  {
185  imageIO->SetFileName(filename.c_str());
186  imageIO->ReadImageInformation();
187  itk::MetaDataDictionary imgMetaDictionary = imageIO->GetMetaDataDictionary();
188  std::vector<std::string> imgMetaKeys = imgMetaDictionary.GetKeys();
189  std::vector<std::string>::const_iterator itKey = imgMetaKeys.begin();
190  std::string metaString;
191 
192  for (; itKey != imgMetaKeys.end(); itKey++)
193  {
194  itk::ExposeMetaData<std::string>(imgMetaDictionary, *itKey, metaString);
195  if (itKey->find("modality") != std::string::npos)
196  {
197  if (metaString.find("DWMRI") != std::string::npos)
198  {
199  return false; // DiffusionImageReader should handle this
200  }
201  }
202  }
203  }
204  catch (...)
205  {
206  MITK_INFO("mitkItkImageFileReader") << "Could not read ImageInformation ";
207  }
208 
209  return true;
210 }
211 
212 mitk::ItkImageFileReader::ItkImageFileReader() : m_FileName(""), m_FilePrefix(""), m_FilePattern("")
213 {
214 }
215 
217 {
218 }
itk::SmartPointer< Self > Pointer
void SetSpacing(const mitk::Vector3D &aSpacing, bool enforceSetSpacing=false)
Set the spacing (m_Spacing).
#define MITK_INFO
Definition: mitkLogMacros.h:22
double ScalarType
virtual void InitializeEvenlySpaced(mitk::PlaneGeometry *geometry2D, unsigned int slices)
Completely initialize this instance as evenly-spaced with slices parallel to the provided PlaneGeomet...
MITKCORE_EXPORT mitk::PixelType MakePixelType(vtkImageData *vtkimagedata)
deduct the PixelType for a given vtk image
Convenience class to temporarily change the current locale.
static const std::string filename
#define mitkThrow()
void SetOrigin(const Point3D &origin)
Set the origin, i.e. the upper-left corner of the plane.
Point3D GetCornerPoint(int id) const
Get the position of the corner number id (in world coordinates)
Describes the geometry of a data object consisting of slices.
OutputType * GetOutput()
Get the output data of this image source object.
Describes a two-dimensional, rectangular plane.
virtual void GenerateData() override
A version of GenerateData() specific for image processing filters.
static bool CanReadFile(const std::string filename, const std::string filePrefix, const std::string filePattern)
mitk::AffineTransform3D * GetIndexToWorldTransform()
Get the transformation used to convert from index to world coordinates.