DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
LongvolWriter.ih
1 
30 
31 #include <cstdlib>
32 #include <fstream>
33 #include "DGtal/io/Color.h"
35 
37 // IMPLEMENTATION of inline methods.
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  ofstream out;
50  typename I::Domain::Vector ext = aImage.extent();
51  typename I::Domain domain = aImage.domain();
52  typename I::Value val;
53 
54  try
55  {
56  out.open(filename.c_str());
57 
58  //Longvol format
59  out << "X: "<< ext[0]<<endl;
60  out << "Y: "<< ext[1]<<endl;
61  out << "Z: "<< ext[2]<<endl;
62  out << "Lvoxel-Size: 4"<<endl; //not used in liblongvol but required
63  out << "Alpha-Color: 0"<<endl;
64  out << "Lvoxel-Endian: 0"<<endl;//not used in liblongvol but required
65  out << "Int-Endian: 0123"<<endl;
66  out << "Version: 2"<<endl;
67  out << "."<<endl;
68 
69  out.close();
70  out.open(filename.c_str(),ios_base::binary | ios_base::app);
71  //We scan the domain instead of the image because we cannot
72  //trust the image container Iterator
73  for(typename I::Domain::ConstIterator it = domain.begin(), itend=domain.end();
74  it!=itend;
75  ++it)
76  {
77  val = aImage( (*it) );
78  write_word(out,aFunctor(val));
79  }
80 
81  out.close();
82 
83  }
84  catch( ... )
85  {
86  std::cout << "LongVol writer IO error on export " << filename << endl;
87  throw dgtalio;
88  }
89 
90  return true;
91  }
92 
93 }//namespace