DGtal 1.3.0
Loading...
Searching...
No Matches
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
52namespace 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, const TFunctor& aFunctor)
100 {
101 typedef typename Image::Domain Domain;
102 const Domain& domain = aImage.domain();
103
104 typedef ConstImageAdapter<Image, Domain, functors::Identity, ValueOut, Functor> AdaptedImage;
105 const functors::Identity identityFunctor{};
106 typedef ImageContainerByITKImage<Domain, ValueOut> DGtalITKImage;
107 typedef itk::ImageFileWriter<typename DGtalITKImage::ITKImage> ITKImageWriter;
108 typename ITKImageWriter::Pointer writer = ITKImageWriter::New();
109 try
110 {
111 writer->SetFileName(filename);
112 writer->SetInput(aImage.getITKImagePointer());
113 writer->Update();
114 }
115 catch (itk::ExceptionObject &e)
116 {
117 trace.error() << e;
118 throw IOException();
119 return false;
120 }
121 return true;
122 }
123
124
125}//namespace
126