26 #ifndef MLN_IO_PNM_LOAD_HH
27 # define MLN_IO_PNM_LOAD_HH
38 # include <mln/core/image/image2d.hh>
40 # include <mln/value/int_u8.hh>
41 # include <mln/value/rgb.hh>
43 # include <mln/io/pnm/load_header.hh>
44 # include <mln/io/pnm/max_component.hh>
45 # include <mln/io/pnm/macros.hh>
47 # include <mln/metal/is_a.hh>
59 # ifndef MLN_INCLUDE_ONLY
62 void load_ascii_value(std::ifstream& file, I& ima);
65 void load_ascii_builtin(std::ifstream& file, I& ima);
74 load_ascii_dispatch(std::ifstream& file, I& ima,
const metal::bool_<true>&)
76 load_ascii_value(file, ima);
82 load_ascii_dispatch(std::ifstream& file, I& ima,
const metal::bool_<false>&)
84 load_ascii_builtin(file, ima);
91 template <
unsigned int n>
93 void read_value(std::ifstream& file, value::rgb<n>& v)
95 typedef typename value::int_u<n>::enc E;
98 file.read((
char*)(&c),
sizeof(E));
100 file.read((
char*)(&c),
sizeof(E));
102 file.read((
char*)(&c),
sizeof(E));
109 void read_value(std::ifstream& file, value::Scalar<V>& v)
111 typedef typename V::enc E;
114 file.read((
char*)(&c),
sizeof(E));
119 template <
typename V>
121 void read_value(std::ifstream& file, V& v)
124 file.read((
char*)(&c),
sizeof(V));
129 template <
typename V>
131 void load_raw_2d_uncontiguous(std::ifstream& file, image2d<V>& ima)
140 for (p.row() = min_row; p.row() <=
max_row; ++p.row())
142 read_value(file, ima(p));
146 template <
typename I>
148 void load_raw_2d_contiguous(std::ifstream& file, I& ima)
151 typedef mln_value(I) V;
152 const mln_deduce(I, site, coord)
153 min_row = geom::min_row(ima),
154 max_row = geom::max_row(ima);
156 std::
size_t len = geom::ncols(ima) * sizeof(V);
157 for (p.row() = min_row; p.row() <= max_row; ++p.row())
158 file.read((
char*)(&ima(p)), len);
162 template <typename I>
164 void load_ascii_value(std::ifstream& file, I& ima)
166 mln_equiv(mln_value_(I)) c;
167 mln_fwd_piter(I) p(ima.domain());
176 template <
typename I>
178 void load_ascii_builtin(std::ifstream& file, I& ima)
180 mln_fwd_piter(I) p(ima.domain());
196 template <
typename I>
200 typedef mln_value(I) V;
202 load_raw_2d_contiguous(file, ima);
204 load_raw_2d_uncontiguous(file, ima);
208 template <
typename V>
212 trace::entering(
"mln::io::pnm::load");
214 std::ifstream file(filename.c_str());
217 std::cerr <<
"error: file '" << filename
224 read_header(static_cast<char>(type_ - 3), type_, file, type,
225 nrows, ncols, maxval);
229 std::cerr <<
"error: file '" << filename
230 <<
"' cannot be loaded into this type of image"
233 std::cerr <<
"input image have " << maxval
234 <<
" as maximum value while the destination's one is "
244 if (type == (type_ - 3))
245 pnm::internal::load_ascii_dispatch(file, ima, mlc_is_a(V,
mln::Value)());
247 trace::exiting(
"mln::io::pnm::load");
255 template <
typename I>
259 const std::string& filename)
261 trace::entering(
"mln::io::pnm::load");
263 std::ifstream file(filename.c_str());
266 std::cerr <<
"error: file '" << filename
271 I& ima = exact(ima_);
276 read_header(static_cast<char>(type_ - 3), type_, file, type,
277 nrows, ncols, maxval);
281 std::cerr <<
"error: file '" << filename
282 <<
"' cannot be loaded into this type of image"
285 std::cerr <<
"input image have " << maxval
286 <<
" as maximum value while the destination's one is "
296 if (type == (type_ - 3))
297 pnm::internal::load_ascii_dispatch(file, ima, mlc_is_a(mln_value(I),
mln::Value)());
299 trace::exiting(
"mln::io::pnm::load");
302 # endif // ! MLN_INCLUDE_ONLY
311 #endif // ! MLN_IO_PNM_LOAD_HH