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