$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
raw/load.hh
1 // Copyright (C) 2010, 2012, 2013 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_LOAD_HH
28 # define MLN_IO_RAW_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 raw
50  {
51 
64  template <typename I>
65  void load(Image<I>& ima_, const std::string& filename);
66 
67 
68 # ifndef MLN_INCLUDE_ONLY
69 
70  namespace internal
71  {
72 
73  template <typename P>
74  inline
75  void read_point(std::ifstream& file, P& p)
76  {
77  for (unsigned i = 0; i < P::dim; ++i)
78  file >> p[i];
79  }
80 
81 
82  template <typename I>
83  inline
84  void load_header(Image<I>& ima, std::ifstream& info_file,
85  const std::string& filename)
86  {
87  // Milena's file type ?
88  std::string file_type;
89  info_file >> file_type;
90  if (file_type != "milena/raw")
91  {
92  std::cerr << "io::raw::load - Error: invalid file type. '"
93  << filename
94  << "' is NOT a valid milena/raw info file!"
95  << std::endl;
96  abort();
97  }
98 
99  char dev_null[255];
100 
101  // Dimension ?
102  // Reading line title "Dim: "
103  info_file.read(dev_null, 5);
104 
105  unsigned dim;
106  info_file >> dim;
107 
108  typedef mln_site(I) P;
109  if (P::dim != dim)
110  {
111  std::cerr << "io::raw::load - Error: invalid image dimension. '"
112  << filename << "' is a " << dim << "-D image "
113  << "but you try to load it into a " << P::dim
114  << "-D image!"
115  << std::endl;
116  abort();
117  }
118 
119  // Size information - Skip it, useless.
120  std::string tmp;
121  for (unsigned i = 0; i < dim; ++i)
122  info_file >> tmp;
123  // Skipping endline.
124  char c;
125  info_file.get(c);
126 
127  // Value type name ?
128  // Reading line title "data type: "
129  info_file.read(dev_null, 11);
130  // WARNING: value type name limited to 255 characters...
131  char value_type[255];
132  info_file.getline(value_type, 255);
133  if (mln_trait_value_name(mln_value(I)) != std::string(value_type))
134  {
135  std::cerr << "io::raw::load - Error: invalid image value type. '"
136  << filename << "' is an image of '" << value_type
137  << "' but you try to load it into an image of '"
138  << mln_trait_value_name(mln_value(I)) << "'!"
139  << std::endl;
140  abort();
141  }
142 
143  // Pmin
144  // Reading line title "top left: "
145  info_file.read(dev_null, 10);
146  P pmin;
147  read_point<P>(info_file, pmin);
148 
149  // Pmax
150  // Reading line title "bottom right: "
151  info_file.read(dev_null, 14);
152  P pmax;
153  read_point<P>(info_file, pmax);
154 
155  // Initialize the image buffer.
156  mln_concrete(I) result(box<P>(pmin, pmax));
157  initialize(ima, result);
158  mln_assertion(exact(ima).is_valid());
159  }
160 
161 
162  template <typename I>
163  inline
164  void load_data(Image<I>& ima_, std::ifstream& file)
165  {
166  I& ima = exact(ima_);
167 
168  // Handle padding.
169  unsigned data_size = sizeof (mln_value(I)) + sizeof (mln_value(I)) % 2;
170 
171  mln_box_runstart_piter(I) p(ima.domain());
172  for_all(p)
173  {
174  pixel<I> src(ima, p);
175  file.read((char*) (&src.val()), p.run_length() * data_size);
176  }
177 
178  }
179 
180  } // end of namespace mln::io::raw::internal
181 
182 
183 
184  template <typename I>
185  void load(Image<I>& ima, const std::string& filename)
186  {
187  mln_trace("mln::io::raw::load");
188 
189  std::ifstream file(filename.c_str());
190  if (! file)
191  {
192  std::cerr << "io::raw::load - error: cannot open file '"
193  << filename << "'!";
194  abort();
195  }
196 
197  std::string info_filename = filename + ".info";
198  std::ifstream info_file(info_filename.c_str());
199  if (! info_file)
200  {
201  std::cerr << "io::raw::load - error: cannot open file '"
202  << info_filename << "'!";
203  abort();
204  }
205 
206 
207  internal::load_header(ima, info_file, info_filename);
208  internal::load_data(ima, file);
209 
210  mln_postcondition(exact(ima).is_valid());
211 
212  file.close();
213  info_file.close();
214 
215  }
216 
217 
218 # endif // ! MLN_INCLUDE_ONLY
219 
220  } // end of namespace mln::io::raw
221 
222  } // end of namespace mln::io
223 
224 } // end of namespace mln
225 
226 #endif // ! MLN_IO_RAW_LOAD_HH