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