26 #ifndef MLN_IO_RAW_SAVE_HH
27 # define MLN_IO_RAW_SAVE_HH
38 # include <mln/core/concept/image.hh>
39 # include <mln/core/box_runstart_piter.hh>
40 # include <mln/core/pixel.hh>
41 # include <mln/data/memcpy_.hh>
42 # include <mln/trait/value_.hh>
65 void save(
const Image<I>& ima_,
const std::string& filename);
69 # ifndef MLN_INCLUDE_ONLY
77 void save_header(
const I& ima, std::ofstream& file)
80 file <<
"milena/raw" << std::endl;
83 typedef mln_site(I) P;
84 file << "dim: " << P::dim << std::endl;
87 typedef algebra::vec<P::dim,
unsigned> vec_t;
88 vec_t size = ima.domain().pmax() - ima.domain().pmin();
89 for (
unsigned i = 0; i < P::dim - 1; ++i)
90 file << size[i] + 1 << " ";
91 file << size[P::dim - 1] + 1 << std::endl;
95 file << "
data type: " << mln_trait_value_name(mln_value(I))
100 for (
unsigned i = 0; i < P::dim - 1; ++i)
101 file << ima.domain().pmin()[i] << " ";
102 file << ima.domain().pmin()[P::dim - 1] << std::endl;
105 file << "bottom right: ";
106 for (
unsigned i = 0; i < P::dim - 1; ++i)
107 file << ima.domain().pmax()[i] << " ";
108 file << ima.domain().pmax()[P::dim - 1] << std::endl;
112 template <typename I>
114 void save_data(I& ima, std::ofstream& file)
118 data_size =
sizeof (mln_value(I)) +
sizeof (mln_value(I)) % 2;
120 mln_box_runstart_piter(I) p(ima.domain());
123 pixel<I> src(ima, p);
124 file.write((
char*) (&src.val()), p.run_length() * data_size);
134 template <
typename I>
137 trace::entering(
"mln::io::raw::save");
139 mlc_bool(mln_site_(I)::dim == 2 || mln_site_(I)::dim == 3)::check();
141 const I& ima = exact(ima_);
143 std::ofstream file(filename.c_str());
146 std::cerr <<
"error: cannot open file '" << filename <<
"'!";
150 std::string info_filename = filename +
".info";
151 std::ofstream info_file(info_filename.c_str());
154 std::cerr <<
"error: cannot open file '" << info_filename <<
"'!";
159 internal::save_header(ima, info_file);
160 internal::save_data(ima, file);
165 trace::exiting(
"mln::io::raw::save");
169 # endif // ! MLN_INCLUDE_ONLY
177 #endif // ! MLN_IO_RAW_SAVE_HH