$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
dump/load.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_LOAD_HH
28 # define MLN_IO_DUMP_LOAD_HH
29 
33 
34 # include <iostream>
35 # include <fstream>
36 
37 # include <mln/core/concept/image.hh>
38 # include <mln/core/routine/initialize.hh>
39 # include <mln/core/box_runstart_piter.hh>
40 # include <mln/core/pixel.hh>
41 # include <mln/data/memcpy_.hh>
42 
43 namespace mln
44 {
45 
46  namespace io
47  {
48 
49  namespace dump
50  {
51 
59  template <typename I>
60  void load(Image<I>& ima_, const std::string& filename);
61 
62 
63 # ifndef MLN_INCLUDE_ONLY
64 
65  namespace internal
66  {
67 
68  template <typename P>
69  inline
70  void read_point(std::ifstream& file, P& p)
71  {
72  char tmp[sizeof (P)];
73  file.read(tmp, sizeof (P));
74  p = *(P*)(void*)(&tmp);
75  }
76 
77 
78  template <typename I>
79  inline
80  void load_header(Image<I>& ima, std::ifstream& file,
81  const std::string& filename)
82  {
83  // Milena's file type ?
84  std::string file_type;
85  file >> file_type;
86  if (file_type != "milena/dump")
87  {
88  std::cerr << "io::dump::load - Error: invalid file type. '"
89  << filename
90  << "' is NOT a valid milena/dump file!"
91  << std::endl;
92  abort();
93  }
94 
95  // Dimension ?
96  unsigned dim;
97  file >> dim;
98  typedef mln_site(I) P;
99  if (P::dim != dim)
100  {
101  std::cerr << "io::dump::load - Error: invalid image dimension. '"
102  << filename << "' is a " << dim << "-D image "
103  << "but you try to load it into a " << P::dim
104  << "-D image!"
105  << std::endl;
106  abort();
107  }
108 
109  // Size information - Skip it, useless.
110  std::string tmp;
111  for (unsigned i = 0; i < dim; ++i)
112  file >> tmp;
113  // Skipping endline.
114  char c;
115  file.get(c);
116 
117  // Value type name ?
118  // WARNING: value type name limited to 255 characters...
119  char value_type[255];
120  file.getline(value_type, 255);
121  if (mln_trait_value_name(mln_value(I)) != std::string(value_type))
122  {
123  std::cerr << "io::dump::load - Error: invalid image value type. '"
124  << filename << "' is an image of '" << value_type
125  << "' but you try to load it into an image of '"
126  << mln_trait_value_name(mln_value(I)) << "'!"
127  << std::endl;
128  abort();
129  }
130 
131  // Empty line - may be used for a new information.
132  file.get(c);
133 
134  // Empty line - may be used for a new information.
135  file.get(c);
136 
137  // Pmin
138  P pmin;
139  read_point<P>(file, pmin);
140 
141  // Pmax
142  P pmax;
143  read_point<P>(file, pmax);
144 
145  // Initialize the image buffer.
146  mln_concrete(I) result(box<P>(pmin, pmax));
147  initialize(ima, result);
148  mln_assertion(exact(ima).is_valid());
149  }
150 
151 
152  template <typename I>
153  inline
154  void load_data(Image<I>& ima_, std::ifstream& file)
155  {
156  I& ima = exact(ima_);
157 
158  // Handle padding.
159  unsigned data_size = sizeof (mln_value(I)) + sizeof (mln_value(I)) % 2;
160 
161  mln_box_runstart_piter(I) p(ima.domain());
162  for_all(p)
163  {
164  pixel<I> src(ima, p);
165  file.read((char*) (&src.val()), p.run_length() * data_size);
166  }
167 
168  }
169 
170  } // end of namespace mln::io::dump::internal
171 
172 
173 
174  template <typename I>
175  void load(Image<I>& ima, const std::string& filename)
176  {
177  mln_trace("mln::io::dump::load");
178 
179  std::ifstream file(filename.c_str());
180  if (! file)
181  {
182  std::cerr << "io::dump::load - Error: cannot open file '"
183  << filename << "'!"
184  << std::endl;
185  abort();
186  }
187 
188  internal::load_header(ima, file, filename);
189  internal::load_data(ima, file);
190 
191  mln_postcondition(exact(ima).is_valid());
192 
193  }
194 
195 
196 # endif // ! MLN_INCLUDE_ONLY
197 
198  } // end of namespace mln::io::dump
199 
200  } // end of namespace mln::io
201 
202 } // end of namespace mln
203 
204 #endif // ! MLN_IO_DUMP_LOAD_HH