DGtal  0.9.2
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, UMR 5205), CNRS, France
21  *
22  * @date 2013/10/28
23  *
24  * Header file for module ITKReader.cpp
25  *
26  * This file is part of the DGtal library.
27  */
28 
29 #include "DGtal/images/ConstImageAdapter.h"
30 #include "DGtal/images/ImageContainerByITKImage.h"
31 #if defined(__GNUG__)
32 #pragma GCC diagnostic push
33 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
34 #endif
35 #if defined(__clang__)
36 #pragma clang diagnostic push
37 #pragma clang diagnostic ignored "-Wdocumentation"
38 #endif
39 #include <itkImageFileReader.h>
40 #if defined(__clang__)
41 #pragma clang diagnostic pop
42 #endif
43 #if defined(__GNUG__)
44 #endif
45 #pragma GCC diagnostic pop
46 
47 namespace DGtal {
48  template<typename I,typename F>
49  I
50  ITKReader<I,F>::importITK(const std::string& filename, const Functor& aFunctor)
51  throw(DGtal::IOException)
52  {
53  typedef typename Image::Domain Domain;
54 
55  typedef ImageContainerByITKImage<Domain, ValueOut> DGtalITKImage;
56  typedef typename DGtalITKImage::ITKImagePointer ITKImagePointer;
57  ITKImagePointer itk_image = DGtalITKImage::ITKImage::New();
58 
59  try
60  {
61  typedef itk::ImageFileReader<typename DGtalITKImage::ITKImage> ITKImageReader;
62  typename ITKImageReader::Pointer reader = ITKImageReader::New();
63 
64  reader->SetFileName(filename);
65  reader->Update();
66  reader->GetOutput();
67 
68  itk_image = reader->GetOutput();
69  }
70  catch (itk::ExceptionObject &e)
71  {
72  trace.error() << e;
73  throw IOException();
74  }
75 
76  const DGtalITKImage dgtal_itk_image(itk_image);
77  const Domain& domain = dgtal_itk_image.domain();
78 
79  typedef ConstImageAdapter<DGtalITKImage, Domain, functors::Identity, Value, Functor> AdaptedImage;
80  const functors::Identity identityFunctor{};
81  const AdaptedImage adapted(dgtal_itk_image, domain, identityFunctor, aFunctor);
82 
83  Image image(domain);
84  std::copy(adapted.constRange().begin(), adapted.constRange().end(), image.range().outputIterator());
85 
86  return image;
87  }
88 
89 }//namespace