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