DGtal  1.0.0
ITKWriter.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 ITKWriter.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 #pragma GCC diagnostic ignored "-Wpedantic"
35 #endif
36 #if defined(__clang__)
37 #pragma clang diagnostic push
38 #pragma clang diagnostic ignored "-Wdocumentation"
39 #endif
40 #include <itkImageFileWriter.h>
41 #if defined(__clang__)
42 #pragma clang diagnostic pop
43 #endif
44 #if defined(__GNUG__)
45 #pragma GCC diagnostic pop
46 #endif
47 
48 ///////////////////////////////////////////////////////////////////////////////
49 // IMPLEMENTATION of inline methods.
50 ///////////////////////////////////////////////////////////////////////////////
51 
52 namespace DGtal {
53  template<typename I,typename F>
54  bool
55  ITKWriter<I,F>::exportITK(const std::string& filename, const I& aImage, const Functor& aFunctor)
56  {
57  typedef typename Image::Domain Domain;
58  const Domain& domain = aImage.domain();
59 
60  typedef ConstImageAdapter<Image, Domain, functors::Identity, ValueOut, Functor> AdaptedImage;
61  const functors::Identity identityFunctor{};
62  const AdaptedImage adapted(aImage, domain, identityFunctor, aFunctor);
63 
64  typedef ImageContainerByITKImage<Domain, ValueOut> DGtalITKImage;
65  DGtalITKImage itk_image(aImage.domain());
66 
67  std::copy(adapted.constRange().begin(), adapted.constRange().end(), itk_image.range().outputIterator());
68 
69  typedef itk::ImageFileWriter<typename DGtalITKImage::ITKImage> ITKImageWriter;
70  typename ITKImageWriter::Pointer writer = ITKImageWriter::New();
71 
72  try
73  {
74  writer->SetFileName(filename);
75  writer->SetInput(itk_image.getITKImagePointer());
76  writer->Update();
77  }
78  catch (itk::ExceptionObject &e)
79  {
80  trace.error() << e;
81  throw IOException();
82  return false;
83  }
84 
85  return true;
86  }
87 
88 }//namespace