DGtal  1.5.beta
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 
54  template<typename I,typename F>
55  bool
56  ITKWriter<I,F>::exportITK(const std::string& filename, const I& aImage, const Functor& aFunctor)
57  {
58  return ITKWriter<I,F>::exportITK(filename, aImage, SpacingType::diagonal(1), aFunctor);
59  }
60 
61  template<typename I,typename F>
62  bool
63  ITKWriter<I,F>::exportITK(const std::string& filename, const I& aImage, const SpacingType &anImgSpacing,
64  const Functor& aFunctor)
65  {
66  typedef typename Image::Domain Domain;
67  const Domain& domain = aImage.domain();
68 
69  typedef ConstImageAdapter<Image, Domain, functors::Identity, ValueOut, Functor> AdaptedImage;
70  const functors::Identity identityFunctor{};
71  const AdaptedImage adapted(aImage, domain, identityFunctor, aFunctor);
72 
73  typedef ImageContainerByITKImage<Domain, ValueOut> DGtalITKImage;
74  DGtalITKImage itk_image(aImage.domain());
75 
76  std::copy(adapted.constRange().begin(), adapted.constRange().end(), itk_image.range().outputIterator());
77 
78  typedef itk::ImageFileWriter<typename DGtalITKImage::ITKImage> ITKImageWriter;
79  typename ITKImageWriter::Pointer writer = ITKImageWriter::New();
80  try
81  {
82  itk_image.setImageSpacing(anImgSpacing);
83  writer->SetFileName(filename);
84  writer->SetInput(itk_image.getITKImagePointer());
85  writer->Update();
86  }
87  catch (itk::ExceptionObject &e)
88  {
89  trace.error() << e;
90  throw IOException();
91  return false;
92  }
93  return true;
94  }
95 
96  template <typename TDomain, typename TValue, typename TFunctor>
97  bool
98  ITKWriter< ImageContainerByITKImage<TDomain, TValue>, TFunctor >::exportITK(const std::string& filename,
99  const ImageContainerByITKImage<TDomain, TValue>& aImage,[[maybe_unused]] const TFunctor& aFunctor)
100  {
101 
102  typedef typename Image::Domain Domain;
103 
104  typedef ImageContainerByITKImage<Domain, ValueOut> DGtalITKImage;
105  typedef itk::ImageFileWriter<typename DGtalITKImage::ITKImage> ITKImageWriter;
106  typename ITKImageWriter::Pointer writer = ITKImageWriter::New();
107  try
108  {
109  writer->SetFileName(filename);
110  writer->SetInput(aImage.getITKImagePointer());
111  writer->Update();
112  }
113  catch (itk::ExceptionObject &e)
114  {
115  trace.error() << e;
116  throw IOException();
117  return false;
118  }
119  return true;
120  }
121 
122 
123 }//namespace
124