27 #ifndef MLN_IO_FLD_LOAD_HH
28 # define MLN_IO_FLD_LOAD_HH
50 # include <mln/core/concept/image.hh>
51 # include <mln/io/fld/header.hh>
52 # include <mln/io/fld/load_header.hh>
53 # include <mln/io/fld/max_components.hh>
55 # include <mln/algebra/vec.hh>
56 # include <mln/value/rgb.hh>
57 # include <mln/value/int_u8.hh>
59 # include <mln/geom/nsites.hh>
61 # include <mln/convert/from_to.hh>
80 load(Image<I>& ima_,
const char* filename);
82 # ifndef MLN_INCLUDE_ONLY
89 abort_load(
const char* msg,
const char* filename)
91 std::cerr <<
"Error: file '" << filename <<
"'"
92 <<
"cannot be loaded." << std::endl
93 <<
"Error description: " << msg << std::endl;
98 template <
unsigned int n>
100 void read_value(std::ifstream& file, value::rgb<n>& v)
102 typedef typename value::int_u<n>::enc E;
105 file.read((
char*)(&c),
sizeof(E));
107 file.read((
char*)(&c),
sizeof(E));
109 file.read((
char*)(&c),
sizeof(E));
116 void read_value(std::ifstream& file, value::Scalar<V>& v)
118 typedef typename V::enc E;
121 file.read((
char*)(&c),
sizeof(E));
126 template <
typename V>
128 void read_value(std::ifstream& file, V& v)
131 file.read((
char*)(&c),
sizeof(V));
136 template <
typename I>
138 void load_raw_uncontiguous(std::ifstream& file, I& ima)
140 mln_piter(I) p(ima.domain());
141 read_value(file, ima(p));
145 template <typename I>
147 void load_raw_contiguous(std::ifstream& file, I& ima)
149 mln_site(I) pmin = ima.domain().pmin();
150 mln_site(I) pmax = ima.domain().pmax();
152 typedef mln_site(I) P;
153 enum { dim = P::dim };
156 typedef mln_value(I) V;
159 std::
size_t len = pmax[dim - 1] - pmin[dim - 1] + 1;
160 std::
size_t n = len * sizeof(V);
165 file.read((
char*)(&ima(p)), n);
171 file.read((
char*)(&ima(p)), n);
174 for (
int i = dim - 2; p[i] > pmax[i]; --i)
184 template <
typename I>
186 void load_raw(std::ifstream& file, I& ima)
189 load_raw_contiguous(file, ima);
191 load_raw_uncontiguous(file, ima);
196 template <
typename I>
201 trace::entering(
"mln::io::fld::load");
203 std::ifstream file(filename);
205 internal::abort_load(
"Fail to open the file.", filename);
207 typedef mln_value(I) V;
208 typedef mln_site(I) P;
210 I& ima = exact(ima_);
213 int veclen = mln_dim(V);
215 if (nspace != hder.nspace)
216 internal::abort_load(
"The dimension of the input does not match the one from the file.", filename);
218 internal::abort_load(
"The loader does not handle image dimension greater than three.", filename);
219 if (veclen != hder.veclen)
220 internal::abort_load(
"The dimension of the value does not match the one from the file.", filename);
221 if (max_component(V ()) != max_component(hder.data))
222 internal::abort_load(
"The data type of the input mismatches the one from the file.", filename);
225 for (
int i = 0; i < hder.ndim; ++i)
227 convert::from_to(hder.min_ext[i], bbox.
pmin()[i]);
228 convert::from_to(hder.max_ext[i], bbox.
pmax()[i]);
232 internal::load_raw(file, ima);
235 trace::exiting(
"mln::io::fld::load");
238 # endif // ! MLN_INCLUDE_ONLY
246 #endif // !MLN_IO_FLD_LOAD_HH