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>
84 # ifndef MLN_INCLUDE_ONLY
91 abort_load(
const char* msg,
const char*
filename)
93 std::cerr <<
"Error: file '" << filename <<
"'"
94 <<
"cannot be loaded." << std::endl
95 <<
"Error description: " << msg << std::endl;
100 template <
unsigned int n>
102 void read_value(std::ifstream& file, value::rgb<n>& v)
104 typedef typename value::int_u<n>::enc E;
107 file.read((
char*)(&c),
sizeof(E));
109 file.read((
char*)(&c),
sizeof(E));
111 file.read((
char*)(&c),
sizeof(E));
118 void read_value(std::ifstream& file, value::Scalar<V>& v)
120 typedef typename V::enc E;
123 file.read((
char*)(&c),
sizeof(E));
128 template <
typename V>
130 void read_value(std::ifstream& file, V& v)
133 file.read((
char*)(&c),
sizeof(V));
138 template <
typename I>
140 void load_raw_uncontiguous(std::ifstream& file, I& ima)
142 mln_piter(I)
p(ima.domain());
143 read_value(file, ima(
p));
147 template <typename I>
149 void load_raw_contiguous(std::ifstream& file, I& ima)
151 mln_site(I) pmin = ima.domain().pmin();
152 mln_site(I) pmax = ima.domain().pmax();
154 typedef mln_site(I) P;
155 enum { dim = P::dim };
158 typedef mln_value(I) V;
161 std::
size_t len = pmax[dim - 1] - pmin[dim - 1] + 1;
162 std::
size_t n = len * sizeof(V);
167 file.read((
char*)(&ima(p)), n);
173 file.read((
char*)(&ima(p)), n);
176 for (
int i = dim - 2; p[i] > pmax[i]; --i)
186 template <
typename I>
188 void load_raw(std::ifstream& file, I& ima)
190 if (
sizeof(value::int_u8) == 1)
191 load_raw_contiguous(file, ima);
193 load_raw_uncontiguous(file, ima);
198 template <
typename I>
203 mln_trace(
"mln::io::fld::load");
205 std::ifstream file(filename);
207 internal::abort_load(
"Fail to open the file.", filename);
209 typedef mln_value(I) V;
210 typedef mln_site(I) P;
212 I& ima =
exact(ima_);
215 int veclen = mln_dim(V);
217 if (nspace != hder.nspace)
218 internal::abort_load("The dimension of the input does not match the one from the file.", filename);
220 internal::abort_load("The loader does not handle image dimension greater than three.", filename);
221 if (veclen != hder.veclen)
222 internal::abort_load("The dimension of the
value does not match the one from the file.", filename);
224 internal::abort_load("The
data type of the input mismatches the one from the file.", filename);
227 for (
int i = 0; i < hder.ndim; ++i)
234 internal::load_raw(file, ima);
239 # endif // ! MLN_INCLUDE_ONLY
247 #endif // !MLN_IO_FLD_LOAD_HH