$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
pfm/save.hh
1 // Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
26 #ifndef MLN_IO_PFM_SAVE_HH
27 # define MLN_IO_PFM_SAVE_HH
28 
33 
34 # include <iostream>
35 # include <fstream>
36 
37 # include <mln/core/alias/point2d.hh>
38 # include <mln/geom/size2d.hh>
39 # include <mln/metal/equal.hh>
40 # include <mln/metal/bexpr.hh>
41 
42 
43 namespace mln
44 {
45 
46  namespace io
47  {
48 
49  namespace pfm
50  {
51 
57  template <typename I>
58  void save(const Image<I>& ima, const std::string& filename);
59 
60 # ifndef MLN_INCLUDE_ONLY
61 
62  namespace impl
63  {
64 
65  template <typename I>
66  inline
67  void save_header_(const I& ima, std::ofstream& file)
68  {
69  file << geom::nrows(ima) << ' ' << geom::nrows(ima) << std::endl
70  << "float" << std::endl;
71  }
72 
73  template <typename I>
74  inline
75  void save_(const Image<I>& ima_, const std::string& filename)
76  {
77  const I& ima = exact(ima_);
78 
79  std::ofstream file(filename.c_str());
80  save_header_(ima, file);
81 
82  unsigned int ncols = geom::ncols(ima);
83 
84  const mln_deduce(I, site, coord)
85  min_row = geom::min_row(ima),
86  max_row = geom::max_row(ima);
87 
88  //image2d<float>& ima_ = const_cast< image2d<float>& >(ima);
89  point2d p(point2d(0, 0));
90  for (p.row() = min_row; p.row() <= max_row; ++p.row())
91  file.write((const char*)(&(ima(p))),
92  sizeof(float) * ncols);
93  file.close();
94 
95  }
96 
97  } // end of namespace mln::io::impl
98 
99  template <typename I>
100  inline
101  void save(const Image<I>& ima, const std::string& filename)
102  {
103  mln_trace("mln::io::pfm::save");
104  mln::metal::equal<mln_value(I), float >::check();
105  impl::save_(exact(ima), filename);
106  }
107 
108 # endif // ! MLN_INCLUDE_ONLY
109 
110  } // end of namespace mln::pfm
111 
112  } // end of namespace mln::io
113 
114 } // end of namespace mln
115 
116 
117 #endif // ! MLN_IO_PFM_SAVE_HH