27 #ifndef MLN_IO_RAW_LOAD_HH
28 # define MLN_IO_RAW_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 void load(Image<I>& ima_,
const std::string& filename);
66 # ifndef MLN_INCLUDE_ONLY
73 void read_point(std::ifstream& file, P& p)
75 for (
unsigned i = 0; i < P::dim; ++i)
82 void load_header(Image<I>& ima, std::ifstream& info_file,
83 const std::string& filename)
86 std::string file_type;
87 info_file >> file_type;
88 if (file_type !=
"milena/raw")
90 std::cerr <<
"io::raw::load - Error: invalid file type. '"
92 <<
"' is NOT a valid milena/raw info file!"
101 info_file.read(dev_null, 5);
106 typedef mln_site(I) P;
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
119 for (
unsigned i = 0; i < dim; ++i)
127 info_file.read(dev_null, 11);
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))
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)) <<
"'!"
143 info_file.read(dev_null, 10);
145 read_point<P>(info_file, pmin);
149 info_file.read(dev_null, 14);
151 read_point<P>(info_file, pmax);
152 std::cout << pmax << std::endl;
155 mln_concrete(I) result(box<P>(pmin, pmax));
157 mln_assertion(exact(ima).is_valid());
161 template <typename I>
163 void load_data(Image<I>& ima_, std::ifstream& file)
165 I& ima = exact(ima_);
168 unsigned data_size =
sizeof (mln_value(I)) +
sizeof (mln_value(I)) % 2;
170 mln_box_runstart_piter(I) p(ima.domain());
173 pixel<I> src(ima, p);
174 file.read((
char*) (&src.val()), p.run_length() * data_size);
183 template <
typename I>
186 trace::entering(
"mln::io::raw::load");
188 std::ifstream file(filename.c_str());
191 std::cerr <<
"io::raw::load - error: cannot open file '"
196 std::string info_filename = filename +
".info";
197 std::ifstream info_file(info_filename.c_str());
200 std::cerr <<
"io::raw::load - error: cannot open file '"
201 << info_filename <<
"'!";
206 internal::load_header(ima, info_file, info_filename);
207 internal::load_data(ima, file);
209 mln_postcondition(exact(ima).is_valid());
214 trace::exiting(
"mln::io::raw::load");
218 # endif // ! MLN_INCLUDE_ONLY
226 #endif // ! MLN_IO_RAW_LOAD_HH