27 #ifndef MLN_IO_PNM_SAVE_HH
28 # define MLN_IO_PNM_SAVE_HH
38 # include <mln/core/concept/image.hh>
39 # include <mln/core/alias/point2d.hh>
40 # include <mln/value/concept/scalar.hh>
42 # include <mln/value/rgb.hh>
43 # include <mln/value/rgb8.hh>
44 # include <mln/value/int_u8.hh>
46 # include <mln/metal/templated_by.hh>
47 # include <mln/metal/not_equal.hh>
49 # include <mln/io/pnm/save_header.hh>
50 # include <mln/io/pnm/macros.hh>
52 # include <mln/geom/size2d.hh>
72 void save(
char type,
const Image<I>& ima_,
const std::string&
filename);
76 # ifndef MLN_INCLUDE_ONLY
82 template <
unsigned int n>
84 void write_value(std::ofstream& file,
85 const value::rgb<n>& c)
87 typedef typename value::int_u<n>::enc E;
89 E v = c.red().to_enc();
90 file.write((
char*)&v,
sizeof(E));
91 v = c.green().to_enc();
92 file.write((
char*)&v,
sizeof(E));
93 v = c.blue().to_enc();
94 file.write((
char*)&v,
sizeof(E));
100 void write_value(std::ofstream& file,
103 mlc_not_equal(V,
bool)::check();
104 file.write((
char*)(&v),
sizeof(V));
108 template <
typename S>
110 void write_value(std::ofstream& file,
111 const value::Scalar<S>& s)
113 typedef typename S::enc E;
116 file.write((
char*)(&c),
sizeof(E));
120 template <
typename I>
122 void save_data_uncontiguous(std::ofstream& file,
134 write_value(file, ima(p));
139 template <
typename I>
141 void save_data_contiguous(std::ofstream& file,
144 const I& ima =
exact(ima_);
150 std::size_t len =
geom::ncols(ima) *
sizeof(mln_value(I));
152 file.write((
char*)(& ima(p)), len);
157 template <
typename I>
159 void save_data_(std::ofstream& file,
160 trait::image::speed::fastest,
const I& ima)
163 if (
sizeof(value::int_u8) == 1)
164 save_data_contiguous(file, ima);
166 save_data_uncontiguous(file, ima);
170 template <
typename I>
172 void save_data_(std::ofstream& file,
173 trait::image::speed::any,
const I& ima)
175 save_data_uncontiguous(file, ima);
183 template <
typename I>
185 void save(
char type,
const Image<I>& ima_,
const std::string&
filename)
187 mln_trace(
"mln::io::pnm::save");
188 const I& ima =
exact(ima_);
189 std::ofstream file(filename.c_str());
190 io::pnm::save_header(type, ima, filename, file);
192 impl::save_data_(file,
193 mln_trait_image_speed(I)(), ima);
196 # endif // ! MLN_INCLUDE_ONLY
205 #endif // ! MLN_IO_PNM_SAVE_HH