DGtal  0.9.2
VolWriter.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 VolWriter.ih
19  * @author David Coeurjolly (\c david.coeurjolly@liris.cnrs.fr )
20  * Laboratoire d'InfoRmatique en Image et Systèmes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
21  *
22  * @date 2010/07/22
23  *
24  * Implementation of inline methods defined in VolWriter.h
25  *
26  * This file is part of the DGtal library.
27  */
28 
29 
30 //////////////////////////////////////////////////////////////////////////////
31 #include <cstdlib>
32 #include <fstream>
33 #include "DGtal/io/Color.h"
34 //////////////////////////////////////////////////////////////////////////////
35 
36 ///////////////////////////////////////////////////////////////////////////////
37 // IMPLEMENTATION of inline methods.
38 ///////////////////////////////////////////////////////////////////////////////
39 
40 
41 namespace DGtal {
42  template<typename I,typename F>
43  bool
44  VolWriter<I,F>::exportVol(const std::string & filename, const I & aImage,
45  const Functor & aFunctor) throw(DGtal::IOException)
46  {
47  DGtal::IOException dgtalio;
48 
49  std::ofstream out;
50  typename I::Domain domain = aImage.domain();
51  const typename I::Domain::Point &upBound = domain.upperBound();
52  const typename I::Domain::Point &lowBound = domain.lowerBound();
53  typename I::Domain::Point p = I::Domain::Point::diagonal(1);
54  typename I::Domain::Vector size = (upBound - lowBound) + p;
55  typename I::Domain::Vector center = lowBound + ((upBound - lowBound)/2);
56 
57  typename I::Value val;
58 
59  try
60  {
61  out.open(filename.c_str());
62 
63  //Vol format
64  out << "Center-X: " << center[0] <<std::endl;
65  out << "Center-Y: " << center[1] <<std::endl;
66  out << "Center-Z: " << center[2] <<std::endl;
67  out << "X: "<< size[0]<<std::endl;
68  out << "Y: "<< size[1]<<std::endl;
69  out << "Z: "<< size[2]<<std::endl;
70  out << "Voxel-Size: 1"<<std::endl;
71  out << "Alpha-Color: 0"<<std::endl;
72  out << "Voxel-Endian: 0"<<std::endl;
73  out << "Int-Endian: 0123"<<std::endl;
74  out << "Version: 2"<<std::endl;
75  out << "."<<std::endl;
76 
77  //We scan the domain instead of the image because we cannot
78  //trust the image container Iterator
79  for(typename I::Domain::ConstIterator it = domain.begin(), itend=domain.end();
80  it!=itend;
81  ++it)
82  {
83  val = aImage( (*it) );
84  out.put(aFunctor(val));
85  }
86 
87  out.close();
88 
89  }
90  catch( ... )
91  {
92  trace.error() << "Vol writer IO error on export " << filename << std::endl;
93  throw dgtalio;
94  }
95  return true;
96  }
97 
98 }//namespace