27 #ifndef MLN_IO_RAW_SAVE_HH
28 # define MLN_IO_RAW_SAVE_HH
39 # include <mln/core/concept/image.hh>
40 # include <mln/core/box_runstart_piter.hh>
41 # include <mln/core/pixel.hh>
42 # include <mln/data/memcpy_.hh>
43 # include <mln/trait/value_.hh>
68 void save(
const Image<I>& ima_,
const std::string&
filename);
72 # ifndef MLN_INCLUDE_ONLY
80 void save_header(
const I& ima, std::ofstream& file)
83 file <<
"milena/raw" << std::endl;
86 typedef mln_site(I) P;
87 file << "dim: " << P::dim << std::endl;
90 typedef algebra::vec<P::dim,
unsigned> vec_t;
91 vec_t size = ima.domain().pmax() - ima.domain().pmin();
92 for (
unsigned i = 0; i < P::dim - 1; ++i)
93 file << size[i] + 1 << " ";
94 file << size[P::dim - 1] + 1 << std::endl;
98 file << "
data type: " << mln_trait_value_name(mln_value(I))
102 file << "top
left: ";
103 for (
unsigned i = 0; i < P::dim - 1; ++i)
104 file << ima.domain().pmin()[i] << " ";
105 file << ima.domain().pmin()[P::dim - 1] << std::endl;
108 file << "bottom
right: ";
109 for (
unsigned i = 0; i < P::dim - 1; ++i)
110 file << ima.domain().pmax()[i] << " ";
111 file << ima.domain().pmax()[P::dim - 1] << std::endl;
115 template <typename I>
117 void save_data(I& ima, std::ofstream& file)
121 data_size =
sizeof (mln_value(I)) +
sizeof (mln_value(I)) % 2;
123 mln_box_runstart_piter(I)
p(ima.domain());
126 pixel<I> src(ima, p);
127 file.write((
char*) (&src.val()), p.run_length() * data_size);
137 template <
typename I>
138 void save(
const Image<I>& ima_,
const std::string&
filename)
140 mln_trace(
"mln::io::raw::save");
142 mlc_bool(mln_site_(I)::dim == 2 || mln_site_(I)::dim == 3)::check();
144 const I& ima =
exact(ima_);
146 std::ofstream file(filename.c_str());
149 std::cerr <<
"error: cannot open file '" << filename <<
"'!";
153 std::string info_filename = filename +
".info";
154 std::ofstream info_file(info_filename.c_str());
157 std::cerr <<
"error: cannot open file '" << info_filename <<
"'!";
162 internal::save_header(ima, info_file);
163 internal::save_data(ima, file);
171 # endif // ! MLN_INCLUDE_ONLY
179 #endif // ! MLN_IO_RAW_SAVE_HH