27 #ifndef MLN_IO_PNM_LOAD_HH
28 # define MLN_IO_PNM_LOAD_HH
39 # include <mln/core/image/image2d.hh>
41 # include <mln/value/int_u8.hh>
42 # include <mln/value/rgb.hh>
44 # include <mln/io/pnm/load_header.hh>
45 # include <mln/io/pnm/max_component.hh>
46 # include <mln/io/pnm/macros.hh>
48 # include <mln/metal/is_a.hh>
49 # include <mln/metal/bool.hh>
61 # ifndef MLN_INCLUDE_ONLY
65 load_ascii_value(std::ifstream& file, I& ima,
metal::true_ is_scalar);
69 load_ascii_value(std::ifstream& file, I& ima,
metal::false_ is_scalar);
73 void load_ascii_builtin(std::ifstream& file, I& ima);
82 load_ascii_dispatch(std::ifstream& file, I& ima,
83 const metal::bool_<true>&)
85 load_ascii_value(file, ima,
86 mlc_bool( mln_dim(mln_value(I)) == 1 ) () );
92 load_ascii_dispatch(std::ifstream& file, I& ima,
93 const metal::bool_<false>&)
95 load_ascii_builtin(file, ima);
102 template <
unsigned int n>
104 void read_value(std::ifstream& file, value::rgb<n>& v)
106 typedef typename value::int_u<n>::enc E;
109 file.read((
char*)(&c),
sizeof(E));
111 file.read((
char*)(&c),
sizeof(E));
113 file.read((
char*)(&c),
sizeof(E));
120 void read_value(std::ifstream& file, value::Scalar<V>& v)
122 typedef typename V::enc E;
125 file.read((
char*)(&c),
sizeof(E));
130 template <
typename V>
132 void read_value(std::ifstream& file, V& v)
135 file.read((
char*)(&c),
sizeof(V));
140 template <
typename V>
142 void load_raw_2d_uncontiguous(std::ifstream& file, image2d<V>& ima)
153 read_value(file, ima(p));
157 template <
typename I>
159 void load_raw_2d_contiguous(std::ifstream& file, I& ima)
162 typedef mln_value(I) V;
164 min_row = geom::min_row(ima),
167 std::
size_t len = geom::
ncols(ima) * sizeof(V);
168 for (p.row() = min_row; p.row() <=
max_row; ++p.row())
169 file.read((
char*)(&ima(p)), len);
173 template <typename I>
176 load_ascii_value(std::ifstream& file, I& ima, metal::false_)
178 enum { dim = mln_dim(mln_value(I)) };
179 typedef mln_equiv(mln_value_(I)) E;
180 typedef mln_equiv(trait::value_<E>::comp)
T;
184 mln_fwd_piter(I) p(ima.domain());
187 for (
int i = 0; i<dim; ++i) {
196 template <
typename I>
199 load_ascii_value(std::ifstream& file, I& ima,
metal::true_)
201 mln_equiv(mln_value_(I)) c;
203 mln_fwd_piter(I) p(ima.domain());
213 template <
typename I>
215 void load_ascii_builtin(std::ifstream& file, I& ima)
217 mln_fwd_piter(I) p(ima.domain());
233 template <
typename I>
235 void load_raw_2d(std::ifstream& file, I& ima)
237 typedef mln_value(I) V;
239 load_raw_2d_contiguous(file, ima);
241 load_raw_2d_uncontiguous(file, ima);
245 template <typename V>
247 image2d<V>
load(
char type_, const std::
string&
filename)
249 mln_trace(
"mln::io::pnm::load");
251 std::ifstream file(filename.c_str());
254 std::cerr <<
"error: file '" << filename
261 read_header(static_cast<char>(type_ - 3), type_, file, type,
262 nrows, ncols, maxval);
266 std::cerr <<
"error: file '" << filename
267 <<
"' cannot be loaded into this type of image"
270 std::cerr <<
"input image have " << maxval
271 <<
" as maximum value while the destination's one is "
277 image2d<V> ima(nrows, ncols);
279 load_raw_2d(file, ima);
281 if (type == (type_ - 3))
282 pnm::internal::load_ascii_dispatch(file, ima, mlc_is_a(V,
mln::Value)());
291 template <
typename I>
293 void load(
char type_,
295 const std::string& filename)
297 mln_trace(
"mln::io::pnm::load");
299 std::ifstream file(filename.c_str());
302 std::cerr <<
"error: file '" << filename
307 I& ima =
exact(ima_);
312 read_header(static_cast<char>(type_ - 3), type_, file, type,
313 nrows, ncols, maxval);
317 std::cerr <<
"error: file '" << filename
318 <<
"' cannot be loaded into this type of image"
321 std::cerr <<
"input image have " << maxval
322 <<
" as maximum value while the destination's one is "
330 load_raw_2d(file, ima);
332 if (type == (type_ - 3))
333 pnm::internal::load_ascii_dispatch(file, ima, mlc_is_a(mln_value(I),
mln::Value)());
337 # endif // ! MLN_INCLUDE_ONLY
346 #endif // ! MLN_IO_PNM_LOAD_HH