26 #ifndef MLN_IO_DUMP_LOAD_HH
27 # define MLN_IO_DUMP_LOAD_HH
36 # include <mln/core/concept/image.hh>
37 # include <mln/core/routine/initialize.hh>
38 # include <mln/core/box_runstart_piter.hh>
39 # include <mln/core/pixel.hh>
40 # include <mln/data/memcpy_.hh>
56 void load(Image<I>& ima_,
const std::string& filename);
59 # ifndef MLN_INCLUDE_ONLY
66 void read_point(std::ifstream& file, P& p)
69 file.read(tmp,
sizeof (P));
70 p = *(P*)(
void*)(&tmp);
76 void load_header(Image<I>& ima, std::ifstream& file,
77 const std::string& filename)
80 std::string file_type;
82 if (file_type !=
"milena/dump")
84 std::cerr <<
"io::dump::load - Error: invalid file type. '"
86 <<
"' is NOT a valid milena/dump file!"
94 typedef mln_site(I) P;
97 std::cerr <<
"io::dump::load - Error: invalid image dimension. '"
98 << filename <<
"' is a " << dim <<
"-D image "
99 <<
"but you try to load it into a " << P::dim
107 for (
unsigned i = 0; i < dim; ++i)
115 char value_type[255];
116 file.getline(value_type, 255);
117 if (mln_trait_value_name(mln_value(I)) != std::string(value_type))
119 std::cerr <<
"io::dump::load - Error: invalid image value type. '"
120 << filename <<
"' is an image of '" << value_type
121 <<
"' but you try to load it into an image of '"
122 << mln_trait_value_name(mln_value(I)) <<
"'!"
135 read_point<P>(file, pmin);
139 read_point<P>(file, pmax);
142 mln_concrete(I) result(box<P>(pmin, pmax));
144 mln_assertion(exact(ima).is_valid());
148 template <typename I>
150 void load_data(Image<I>& ima_, std::ifstream& file)
152 I& ima = exact(ima_);
155 unsigned data_size =
sizeof (mln_value(I)) +
sizeof (mln_value(I)) % 2;
157 mln_box_runstart_piter(I) p(ima.domain());
160 pixel<I> src(ima, p);
161 file.read((
char*) (&src.val()), p.run_length() * data_size);
170 template <
typename I>
173 trace::entering(
"mln::io::dump::load");
175 std::ifstream file(filename.c_str());
178 std::cerr <<
"io::dump::load - Error: cannot open file '"
184 internal::load_header(ima, file, filename);
185 internal::load_data(ima, file);
187 mln_postcondition(exact(ima).is_valid());
189 trace::exiting(
"mln::io::dump::load");
193 # endif // ! MLN_INCLUDE_ONLY
201 #endif // ! MLN_IO_DUMP_LOAD_HH