$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
dump/save.hh
1 // Copyright (C) 2009, 2012 EPITA Research and Development Laboratory
2 // (LRDE)
3 //
4 // This file is part of Olena.
5 //
6 // Olena is free software: you can redistribute it and/or modify it under
7 // the terms of the GNU General Public License as published by the Free
8 // Software Foundation, version 2 of the License.
9 //
10 // Olena is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // As a special exception, you may use this file as part of a free
19 // software project without restriction. Specifically, if other files
20 // instantiate templates or use macros or inline functions from this
21 // file, or you compile this file and link it with other files to produce
22 // an executable, this file does not by itself cause the resulting
23 // executable to be covered by the GNU General Public License. This
24 // exception does not however invalidate any other reasons why the
25 // executable file might be covered by the GNU General Public License.
26 
27 #ifndef MLN_IO_DUMP_SAVE_HH
28 # define MLN_IO_DUMP_SAVE_HH
29 
35 
36 # include <iostream>
37 # include <fstream>
38 
39 # include <mln/core/concept/image.hh>
40 # include <mln/core/box_runstart_piter.hh>
41 # include <mln/core/pixel.hh>
42 # include <mln/data/memcpy_.hh>
43 # include <mln/trait/value_.hh>
44 
45 
46 namespace mln
47 {
48 
49  namespace io
50  {
51 
52  namespace dump
53  {
54 
62  template <typename I>
63  void save(const Image<I>& ima_, const std::string& filename);
64 
65 
66 
67 # ifndef MLN_INCLUDE_ONLY
68 
69 
70  namespace internal
71  {
72 
73  template <typename I>
74  inline
75  void save_header(const I& ima,
76  std::ofstream& file)
77  {
78  // Milena's file type
79  file << "milena/dump" << std::endl;
80 
81  // Dimension
82  typedef mln_site(I) P;
83  file << P::dim << std::endl;
84 
85  // Image size.
86  typedef algebra::vec<P::dim, unsigned> vec_t;
87  vec_t size = ima.domain().pmax() - ima.domain().pmin();
88  for (unsigned i = 0; i < P::dim - 1; ++i)
89  file << size[i] + 1 << " ";
90  file << size[P::dim - 1] + 1 << std::endl;
91 
92  // Value type name
93  // WARNING: value type name limited to 255 characters...
94  file << mln_trait_value_name(mln_value(I)) << std::endl;
95 
96  // Empty line - may be used for a new information.
97  file << std::endl;
98 
99  // Empty line - may be used for a new information.
100  file << std::endl;
101 
102  // Pmin
103  mln_site(I) p = ima.domain().pmin();
104  file.write((char*) (&p), sizeof (P));
105 
106  // Pmax
107  p = ima.domain().pmax();
108  file.write((char*) (&p), sizeof (P));
109  }
110 
111 
112  template <typename I>
113  inline
114  void save_data(I& ima, std::ofstream& file)
115  {
116  // Handle padding.
117  unsigned data_size = sizeof (mln_value(I)) + sizeof (mln_value(I)) % 2;
118 
119  mln_box_runstart_piter(I) p(ima.domain());
120  for_all(p)
121  {
122  pixel<I> src(ima, p);
123  file.write((char*) (&src.val()), p.run_length() * data_size);
124  }
125  }
126 
127  } // end of namespace mln::io::dump::internal
128 
129 
130 
131  // Facade
132 
133  template <typename I>
134  void save(const Image<I>& ima_, const std::string& filename)
135  {
136  mln_trace("mln::io::dump::save");
137 
138  const I& ima = exact(ima_);
139 
140  std::ofstream file(filename.c_str());
141  if (! file)
142  {
143  std::cerr << "error: cannot open file '" << filename << "'!";
144  abort();
145  }
146 
147  internal::save_header(ima, file);
148  internal::save_data(ima, file);
149 
150 
151  file.close();
152 
153  }
154 
155 
156 # endif // ! MLN_INCLUDE_ONLY
157 
158  } // end of namespace mln::io::dump
159 
160  } // end of namespace mln::io
161 
162 } // end of namespace mln
163 
164 #endif // ! MLN_IO_DUMP_SAVE_HH