Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
raw/save.hh
1 // Copyright (C) 2010 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_RAW_SAVE_HH
27 # define MLN_IO_RAW_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 raw
52  {
53 
63  //
64  template <typename I>
65  void save(const Image<I>& ima_, const std::string& filename);
66 
67 
68 
69 # ifndef MLN_INCLUDE_ONLY
70 
71 
72  namespace internal
73  {
74 
75  template <typename I>
76  inline
77  void save_header(const I& ima, std::ofstream& file)
78  {
79  // Milena's file type
80  file << "milena/raw" << std::endl;
81 
82  // Dimension
83  typedef mln_site(I) P;
84  file << "dim: " << P::dim << std::endl;
85 
86  // Image size.
87  typedef algebra::vec<P::dim, unsigned> vec_t;
88  vec_t size = ima.domain().pmax() - ima.domain().pmin();
89  for (unsigned i = 0; i < P::dim - 1; ++i)
90  file << size[i] + 1 << " ";
91  file << size[P::dim - 1] + 1 << std::endl;
92 
93  // Value type name
94  // WARNING: value type name limited to 255 characters...
95  file << "data type: " << mln_trait_value_name(mln_value(I))
96  << std::endl;
97 
98  // Pmin
99  file << "top left: ";
100  for (unsigned i = 0; i < P::dim - 1; ++i)
101  file << ima.domain().pmin()[i] << " ";
102  file << ima.domain().pmin()[P::dim - 1] << std::endl;
103 
104  // Pmax
105  file << "bottom right: ";
106  for (unsigned i = 0; i < P::dim - 1; ++i)
107  file << ima.domain().pmax()[i] << " ";
108  file << ima.domain().pmax()[P::dim - 1] << std::endl;
109  }
110 
111 
112  template <typename I>
113  inline
114  void save_data(I& ima, std::ofstream& file)
115  {
116  // Handle padding.
117  unsigned
118  data_size = sizeof (mln_value(I)) + sizeof (mln_value(I)) % 2;
119 
120  mln_box_runstart_piter(I) p(ima.domain());
121  for_all(p)
122  {
123  pixel<I> src(ima, p);
124  file.write((char*) (&src.val()), p.run_length() * data_size);
125  }
126  }
127 
128  } // end of namespace mln::io::raw::internal
129 
130 
131 
132  // Facade
133 
134  template <typename I>
135  void save(const Image<I>& ima_, const std::string& filename)
136  {
137  trace::entering("mln::io::raw::save");
138 
139  mlc_bool(mln_site_(I)::dim == 2 || mln_site_(I)::dim == 3)::check();
140 
141  const I& ima = exact(ima_);
142 
143  std::ofstream file(filename.c_str());
144  if (! file)
145  {
146  std::cerr << "error: cannot open file '" << filename << "'!";
147  abort();
148  }
149 
150  std::string info_filename = filename + ".info";
151  std::ofstream info_file(info_filename.c_str());
152  if (! info_file)
153  {
154  std::cerr << "error: cannot open file '" << info_filename << "'!";
155  abort();
156  }
157 
158 
159  internal::save_header(ima, info_file);
160  internal::save_data(ima, file);
161 
162  info_file.close();
163  file.close();
164 
165  trace::exiting("mln::io::raw::save");
166  }
167 
168 
169 # endif // ! MLN_INCLUDE_ONLY
170 
171  } // end of namespace mln::io::raw
172 
173  } // end of namespace mln::io
174 
175 } // end of namespace mln
176 
177 #endif // ! MLN_IO_RAW_SAVE_HH