DGtal  0.9.2
LongvolWriter.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 LongvolWriter.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 2011/06/11
23  *
24  * Implementation of inline methods defined in LongvolWriter.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 C>
43  bool
44  LongvolWriter<I,C>::exportLongvol(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  typename I::Value val;
57 
58  try
59  {
60  out.open(filename.c_str());
61 
62  //Longvol format
63  out << "Center-X: " << center[0] <<std::endl;
64  out << "Center-Y: " << center[1] <<std::endl;
65  out << "Center-Z: " << center[2] <<std::endl;
66  out << "X: "<< size[0]<<std::endl;
67  out << "Y: "<< size[1]<<std::endl;
68  out << "Z: "<< size[2]<<std::endl;
69  out << "Lvoxel-Size: 4"<<std::endl; //not used in liblongvol but required
70  out << "Alpha-Color: 0"<<std::endl;
71  out << "Lvoxel-Endian: 0"<<std::endl;//not used in liblongvol but required
72  out << "Int-Endian: 0123"<<std::endl;
73  out << "Version: 2"<<std::endl;
74  out << "."<<std::endl;
75 
76  out.close();
77  out.open(filename.c_str(),std::ios_base::binary | std::ios_base::app);
78  //We scan the domain instead of the image because we cannot
79  //trust the image container Iterator
80  for(typename I::Domain::ConstIterator it = domain.begin(), itend=domain.end();
81  it!=itend;
82  ++it)
83  {
84  val = aImage( (*it) );
85  write_word(out,aFunctor(val));
86  }
87 
88  out.close();
89 
90  }
91  catch( ... )
92  {
93  std::cout << "LongVol writer IO error on export " << filename << std::endl;
94  throw dgtalio;
95  }
96 
97  return true;
98  }
99 
100 }//namespace