27 #ifndef MLN_IO_DUMP_LOAD_HH
28 # define MLN_IO_DUMP_LOAD_HH
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>
63 # ifndef MLN_INCLUDE_ONLY
70 void read_point(std::ifstream& file, P&
p)
73 file.read(tmp,
sizeof (P));
74 p = *(P*)(
void*)(&tmp);
80 void load_header(Image<I>& ima, std::ifstream& file,
84 std::string file_type;
86 if (file_type !=
"milena/dump")
88 std::cerr <<
"io::dump::load - Error: invalid file type. '"
90 <<
"' is NOT a valid milena/dump file!"
98 typedef mln_site(I) P;
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
111 for (
unsigned i = 0; i < dim; ++i)
119 char value_type[255];
120 file.getline(value_type, 255);
121 if (mln_trait_value_name(mln_value(I)) != std::string(value_type))
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)) <<
"'!"
139 read_point<P>(file, pmin);
143 read_point<P>(file, pmax);
146 mln_concrete(I) result(
box<P>(pmin, pmax));
148 mln_assertion(
exact(ima).is_valid());
152 template <typename I>
154 void load_data(Image<I>& ima_, std::ifstream& file)
156 I& ima =
exact(ima_);
159 unsigned data_size =
sizeof (mln_value(I)) +
sizeof (mln_value(I)) % 2;
161 mln_box_runstart_piter(I) p(ima.domain());
164 pixel<I> src(ima, p);
165 file.read((
char*) (&src.val()), p.run_length() * data_size);
174 template <
typename I>
175 void load(Image<I>& ima,
const std::string& filename)
177 mln_trace(
"mln::io::dump::load");
179 std::ifstream file(filename.c_str());
182 std::cerr <<
"io::dump::load - Error: cannot open file '"
188 internal::load_header(ima, file, filename);
189 internal::load_data(ima, file);
191 mln_postcondition(
exact(ima).is_valid());
196 # endif // ! MLN_INCLUDE_ONLY
204 #endif // ! MLN_IO_DUMP_LOAD_HH