Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
dump/save.hh
1 // Copyright (C) 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_DUMP_SAVE_HH
27 # define MLN_IO_DUMP_SAVE_HH
28 
34 
35 # include <iostream>
36 # include <fstream>
37 
38 # include <mln/core/concept/image.hh>
39 # include <mln/core/box_runstart_piter.hh>
40 # include <mln/core/pixel.hh>
41 # include <mln/data/memcpy_.hh>
42 # include <mln/trait/value_.hh>
43 
44 
45 namespace mln
46 {
47 
48  namespace io
49  {
50 
51  namespace dump
52  {
53 
58  //
59  template <typename I>
60  void save(const Image<I>& ima_, const std::string& filename);
61 
62 
63 
64 # ifndef MLN_INCLUDE_ONLY
65 
66 
67  namespace internal
68  {
69 
70  template <typename I>
71  inline
72  void save_header(const I& ima,
73  std::ofstream& file)
74  {
75  // Milena's file type
76  file << "milena/dump" << std::endl;
77 
78  // Dimension
79  typedef mln_site(I) P;
80  file << P::dim << std::endl;
81 
82  // Image size.
83  typedef algebra::vec<P::dim, unsigned> vec_t;
84  vec_t size = ima.domain().pmax() - ima.domain().pmin();
85  for (unsigned i = 0; i < P::dim - 1; ++i)
86  file << size[i] + 1 << " ";
87  file << size[P::dim - 1] + 1 << std::endl;
88 
89  // Value type name
90  // WARNING: value type name limited to 255 characters...
91  file << mln_trait_value_name(mln_value(I)) << std::endl;
92 
93  // Empty line - may be used for a new information.
94  file << std::endl;
95 
96  // Empty line - may be used for a new information.
97  file << std::endl;
98 
99  // Pmin
100  mln_site(I) p = ima.domain().pmin();
101  file.write((char*) (&p), sizeof (P));
102 
103  // Pmax
104  p = ima.domain().pmax();
105  file.write((char*) (&p), sizeof (P));
106  }
107 
108 
109  template <typename I>
110  inline
111  void save_data(I& ima, std::ofstream& file)
112  {
113  // Handle padding.
114  unsigned data_size = sizeof (mln_value(I)) + sizeof (mln_value(I)) % 2;
115 
116  mln_box_runstart_piter(I) p(ima.domain());
117  for_all(p)
118  {
119  pixel<I> src(ima, p);
120  file.write((char*) (&src.val()), p.run_length() * data_size);
121  }
122  }
123 
124  } // end of namespace mln::io::dump::internal
125 
126 
127 
128  // Facade
129 
130  template <typename I>
131  void save(const Image<I>& ima_, const std::string& filename)
132  {
133  trace::entering("mln::io::dump::save");
134 
135  const I& ima = exact(ima_);
136 
137  std::ofstream file(filename.c_str());
138  if (! file)
139  {
140  std::cerr << "error: cannot open file '" << filename << "'!";
141  abort();
142  }
143 
144  internal::save_header(ima, file);
145  internal::save_data(ima, file);
146 
147 
148  file.close();
149 
150  trace::exiting("mln::io::dump::save");
151  }
152 
153 
154 # endif // ! MLN_INCLUDE_ONLY
155 
156  } // end of namespace mln::io::dump
157 
158  } // end of namespace mln::io
159 
160 } // end of namespace mln
161 
162 #endif // ! MLN_IO_DUMP_SAVE_HH