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