DGtal  1.5.beta
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 #include <boost/iostreams/filtering_streambuf.hpp>
35 #include <boost/iostreams/copy.hpp>
36 #include <boost/iostreams/filter/zlib.hpp>
37 //////////////////////////////////////////////////////////////////////////////
38 
39 ///////////////////////////////////////////////////////////////////////////////
40 // IMPLEMENTATION of inline methods.
41 ///////////////////////////////////////////////////////////////////////////////
42 
43 
44 namespace DGtal {
45  template<typename I,typename C>
46  bool
47  LongvolWriter<I,C>::exportLongvol(const std::string & filename, const I & aImage, const bool compressed,
48  const Functor & aFunctor)
49  {
50  DGtal::IOException dgtalio;
51 
52  std::ofstream out;
53  typename I::Domain domain = aImage.domain();
54  const typename I::Domain::Point &upBound = domain.upperBound();
55  const typename I::Domain::Point &lowBound = domain.lowerBound();
56  typename I::Domain::Point p = I::Domain::Point::diagonal(1);
57  typename I::Domain::Vector size = (upBound - lowBound) + p;
58  typename I::Domain::Vector center = lowBound + ((upBound - lowBound)/2);
59  typename I::Value val;
60 
61  try
62  {
63  std::stringstream main;
64  out.open(filename.c_str(), std::ios::out | std::ios::binary);
65 
66  //Longvol format
67  out << "Center-X: " << center[0] <<std::endl;
68  out << "Center-Y: " << center[1] <<std::endl;
69  out << "Center-Z: " << center[2] <<std::endl;
70  out << "X: "<< size[0]<<std::endl;
71  out << "Y: "<< size[1]<<std::endl;
72  out << "Z: "<< size[2]<<std::endl;
73  out << "Lvoxel-Size: 4"<<std::endl; //not used in liblongvol but required
74  out << "Alpha-Color: 0"<<std::endl;
75  out << "Lvoxel-Endian: 0"<<std::endl;//not used in liblongvol but required
76  out << "Int-Endian: 0123"<<std::endl;
77  if (compressed)
78  out << "Version: 3"<<std::endl;
79  else
80  out << "Version: 2"<<std::endl;
81  out << "."<<std::endl;
82 
83  out.close();
84  out.open(filename.c_str(),std::ios_base::binary | std::ios_base::app);
85 
86  //We scan the domain
87  for(typename I::Domain::ConstIterator it = domain.begin(), itend=domain.end();
88  it!=itend;
89  ++it)
90  {
91  val = aImage( (*it) );
92  write_word(main,aFunctor(val));
93  }
94 
95  if (compressed)
96  {
97  boost::iostreams::filtering_streambuf<boost::iostreams::input> out_compressed;
98  out_compressed.push(boost::iostreams::zlib_compressor());
99  out_compressed.push( main );
100  boost::iostreams::copy(out_compressed, out);
101  out.close();
102  }
103  else
104  {
105  out << main.str();
106  out.close();
107  }
108 
109 
110  }
111  catch( ... )
112  {
113  std::cout << "LongVol writer IO error on export " << filename << std::endl;
114  throw dgtalio;
115  }
116 
117  return true;
118  }
119 
120  }//namespace