DGtal  1.0.0
ITKReader.ih
1 /**
2  * This program is free software: you can redistribute it and/or modify
3  * it under the terms of the GNU Lesser General Public License as
4  * published by the Free Software Foundation, either version 3 of the
5  * License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program. If not, see <http://www.gnu.org/licenses/>.
14  *
15  **/
16 
17 /**
18  * @file
19  * @author Pierre Gueth (\c pierre.gueth@gmail.com )
20  * Laboratoire d'InfoRmatique en Image et Systèmes d'information - LIRIS (CNRS,
21  * UMR 5205), CNRS, France
22  *
23  *
24  * @author Bertrand Kerautret (\c bertrand.kerautret@loria.fr )
25  * LORIA (CNRS, UMR 7503), University of Lorraine, France
26  *
27  * @date 2013/10/28
28  *
29  * Header file for module ITKReader.cpp
30  *
31  * This file is part of the DGtal library.
32  */
33 
34 #include "DGtal/images/ConstImageAdapter.h"
35 #include "DGtal/images/ImageContainerByITKImage.h"
36 #if defined(__GNUG__)
37 #pragma GCC diagnostic push
38 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
39 #endif
40 #if defined(__clang__)
41 #pragma clang diagnostic push
42 #pragma clang diagnostic ignored "-Wdocumentation"
43 #endif
44 #include <itkImageFileReader.h>
45 #if defined(__clang__)
46 #pragma clang diagnostic pop
47 #endif
48 #if defined(__GNUG__)
49 #pragma GCC diagnostic pop
50 #endif
51 
52 namespace DGtal {
53 
54  template <typename I>
55  template <typename TFunctor>
56  I ITKReader<I>::importITK(
57  const std::string & filename,
58  const TFunctor & aFunctor )
59  {
60  typedef typename Image::Domain Domain;
61  typedef itk::ImageIOBase::IOComponentType IOComponentType;
62  BOOST_CONCEPT_ASSERT( (concepts::CUnaryFunctor<TFunctor, ValueOut, Value>));
63  const IOComponentType componentType = getITKComponentType( filename );
64  switch ( componentType )
65  {
66 
67  default:
68  case itk::ImageIOBase::UNKNOWNCOMPONENTTYPE:
69  trace.warning() << "[ITKReader] Unknown and unsupported component type! File will be loaded with UCHAR component type" << std::endl;
70  //fallthrough
71  case itk::ImageIOBase::UCHAR:
72  {
73  typedef ImageContainerByITKImage<Domain, unsigned char> DGtalITKImage;
74  return readDGtalImageFromITKtypes<DGtalITKImage>( filename, aFunctor );
75  }
76  case itk::ImageIOBase::CHAR:
77  {
78  typedef ImageContainerByITKImage<Domain, char> DGtalITKImage;
79  return readDGtalImageFromITKtypes<DGtalITKImage>( filename, aFunctor );
80  }
81  case itk::ImageIOBase::USHORT:
82  {
83  typedef ImageContainerByITKImage<Domain, unsigned short> DGtalITKImage;
84  return readDGtalImageFromITKtypes<DGtalITKImage>( filename, aFunctor );
85  }
86  case itk::ImageIOBase::SHORT:
87  {
88  typedef ImageContainerByITKImage<Domain, short> DGtalITKImage;
89  return readDGtalImageFromITKtypes<DGtalITKImage>( filename, aFunctor );
90  }
91  case itk::ImageIOBase::UINT:
92  {
93  typedef ImageContainerByITKImage<Domain, unsigned int> DGtalITKImage;
94  return readDGtalImageFromITKtypes<DGtalITKImage>( filename, aFunctor );
95  }
96  case itk::ImageIOBase::INT:
97  {
98  typedef ImageContainerByITKImage<Domain, int> DGtalITKImage;
99  return readDGtalImageFromITKtypes<DGtalITKImage>( filename, aFunctor );
100  }
101  case itk::ImageIOBase::ULONG:
102  {
103  typedef ImageContainerByITKImage<Domain, unsigned long> DGtalITKImage;
104  return readDGtalImageFromITKtypes<DGtalITKImage>( filename, aFunctor );
105  }
106  case itk::ImageIOBase::LONG:
107  {
108  typedef ImageContainerByITKImage<Domain, long> DGtalITKImage;
109  return readDGtalImageFromITKtypes<DGtalITKImage>( filename, aFunctor );
110  }
111 #if (ITK_VERSION_MAJOR > 4) || (ITK_VERSION_MAJOR == 4 && ITK_VERSION_MINOR >= 13)
112  case itk::ImageIOBase::ULONGLONG:
113  {
114  typedef ImageContainerByITKImage<Domain, unsigned long long> DGtalITKImage;
115  return readDGtalImageFromITKtypes<DGtalITKImage>( filename, aFunctor );
116  }
117  case itk::ImageIOBase::LONGLONG:
118  {
119  typedef ImageContainerByITKImage<Domain, long long> DGtalITKImage;
120  return readDGtalImageFromITKtypes<DGtalITKImage>( filename, aFunctor );
121  }
122 #endif
123  case itk::ImageIOBase::FLOAT:
124  {
125  typedef ImageContainerByITKImage<Domain, float> DGtalITKImage;
126  return readDGtalImageFromITKtypes<DGtalITKImage>( filename, aFunctor );
127  }
128  case itk::ImageIOBase::DOUBLE:
129  {
130  typedef ImageContainerByITKImage<Domain, double> DGtalITKImage;
131  return readDGtalImageFromITKtypes<DGtalITKImage>( filename, aFunctor );
132  }
133  }
134  }
135 
136  template <typename I>
137  itk::ImageIOBase::IOComponentType
138  ITKReader<I>::getITKComponentType( const std::string & filename )
139  {
140  typedef itk::ImageIOBase::IOComponentType IOComponentType;
141  IOComponentType componentType;
142  try
143  {
144  itk::ImageIOBase::Pointer imageIO = itk::ImageIOFactory::CreateImageIO(
145  filename.c_str(), itk::ImageIOFactory::ReadMode );
146  if (imageIO)
147  {
148  imageIO->SetFileName( filename.c_str() );
149  imageIO->ReadImageInformation();
150  componentType = imageIO->GetComponentType();
151  }
152  }
153  catch ( itk::ExceptionObject & e )
154  {
155  trace.error() << e;
156  throw IOException();
157  }
158  return componentType;
159  }
160 
161  template <typename I>
162  template <typename TypeDGtalImage, typename TFunctor>
163  typename ITKReader<I>::Image ITKReader<I>::readDGtalImageFromITKtypes(
164  const std::string & filename,
165  const TFunctor & aFunctor )
166  {
167 
168  typedef typename TypeDGtalImage::ITKImagePointer ITKImagePointer;
169  typedef typename Image::Domain Domain;
170 
171  ITKImagePointer itk_image = nullptr;
172 
173  try
174  {
175  typedef itk::ImageFileReader<typename TypeDGtalImage::ITKImage>
176  ITKImageReader;
177  typename ITKImageReader::Pointer reader = ITKImageReader::New();
178 
179  reader->SetFileName( filename );
180  reader->Update();
181  reader->GetOutput();
182 
183  itk_image = reader->GetOutput();
184  }
185  catch (itk::ExceptionObject &e)
186  {
187  trace.error() << e;
188  throw IOException();
189  }
190 
191  const TypeDGtalImage dgtal_itk_image( itk_image );
192  const Domain& domain = dgtal_itk_image.domain();
193 
194  typedef ConstImageAdapter<TypeDGtalImage, Domain, functors::Identity, Value,
195  TFunctor>
196  AdaptedImage;
197  const functors::Identity identityFunctor{};
198  const AdaptedImage adapted(dgtal_itk_image, domain, identityFunctor, aFunctor);
199 
200  Image image(domain);
201  std::copy(adapted.constRange().begin(), adapted.constRange().end(), image.range().outputIterator());
202  return image;
203  }
204 
205 }//namespace