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>
68 # ifndef MLN_INCLUDE_ONLY
75 void read_point(std::ifstream& file, P&
p)
77 for (
unsigned i = 0; i < P::dim; ++i)
84 void load_header(Image<I>& ima, std::ifstream& info_file,
88 std::string file_type;
89 info_file >> file_type;
90 if (file_type !=
"milena/raw")
92 std::cerr <<
"io::raw::load - Error: invalid file type. '"
94 <<
"' is NOT a valid milena/raw info file!"
103 info_file.read(dev_null, 5);
108 typedef mln_site(I) P;
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
121 for (
unsigned i = 0; i < dim; ++i)
129 info_file.read(dev_null, 11);
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))
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)) <<
"'!"
145 info_file.read(dev_null, 10);
147 read_point<P>(info_file, pmin);
151 info_file.read(dev_null, 14);
153 read_point<P>(info_file, pmax);
156 mln_concrete(I) result(
box<P>(pmin, pmax));
158 mln_assertion(
exact(ima).is_valid());
162 template <typename I>
164 void load_data(Image<I>& ima_, std::ifstream& file)
166 I& ima =
exact(ima_);
169 unsigned data_size =
sizeof (mln_value(I)) +
sizeof (mln_value(I)) % 2;
171 mln_box_runstart_piter(I) p(ima.domain());
174 pixel<I> src(ima, p);
175 file.read((
char*) (&src.val()), p.run_length() * data_size);
184 template <
typename I>
185 void load(Image<I>& ima,
const std::string& filename)
187 mln_trace(
"mln::io::raw::load");
189 std::ifstream file(filename.c_str());
192 std::cerr <<
"io::raw::load - error: cannot open file '"
197 std::string info_filename = filename +
".info";
198 std::ifstream info_file(info_filename.c_str());
201 std::cerr <<
"io::raw::load - error: cannot open file '"
202 << info_filename <<
"'!";
207 internal::load_header(ima, info_file, info_filename);
208 internal::load_data(ima, file);
210 mln_postcondition(
exact(ima).is_valid());
218 # endif // ! MLN_INCLUDE_ONLY
226 #endif // ! MLN_IO_RAW_LOAD_HH