00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef MLN_IO_RAW_SAVE_HH
00027 # define MLN_IO_RAW_SAVE_HH
00028
00034
00035 # include <iostream>
00036 # include <fstream>
00037
00038 # include <mln/core/concept/image.hh>
00039 # include <mln/core/box_runstart_piter.hh>
00040 # include <mln/core/pixel.hh>
00041 # include <mln/data/memcpy_.hh>
00042 # include <mln/trait/value_.hh>
00043
00044
00045 namespace mln
00046 {
00047
00048 namespace io
00049 {
00050
00051 namespace raw
00052 {
00053
00063
00064 template <typename I>
00065 void save(const Image<I>& ima_, const std::string& filename);
00066
00067
00068
00069 # ifndef MLN_INCLUDE_ONLY
00070
00071
00072 namespace internal
00073 {
00074
00075 template <typename I>
00076 inline
00077 void save_header(const I& ima, std::ofstream& file)
00078 {
00079
00080 file << "milena/raw" << std::endl;
00081
00082
00083 typedef mln_site(I) P;
00084 file << "dim: " << P::dim << std::endl;
00085
00086
00087 typedef algebra::vec<P::dim, unsigned> vec_t;
00088 vec_t size = ima.domain().pmax() - ima.domain().pmin();
00089 for (unsigned i = 0; i < P::dim - 1; ++i)
00090 file << size[i] + 1 << " ";
00091 file << size[P::dim - 1] + 1 << std::endl;
00092
00093
00094
00095 file << "data type: " << mln_trait_value_name(mln_value(I))
00096 << std::endl;
00097
00098
00099 file << "top left: ";
00100 for (unsigned i = 0; i < P::dim - 1; ++i)
00101 file << ima.domain().pmin()[i] << " ";
00102 file << ima.domain().pmin()[P::dim - 1] << std::endl;
00103
00104
00105 file << "bottom right: ";
00106 for (unsigned i = 0; i < P::dim - 1; ++i)
00107 file << ima.domain().pmax()[i] << " ";
00108 file << ima.domain().pmax()[P::dim - 1] << std::endl;
00109 }
00110
00111
00112 template <typename I>
00113 inline
00114 void save_data(I& ima, std::ofstream& file)
00115 {
00116
00117 unsigned
00118 data_size = sizeof (mln_value(I)) + sizeof (mln_value(I)) % 2;
00119
00120 mln_box_runstart_piter(I) p(ima.domain());
00121 for_all(p)
00122 {
00123 pixel<I> src(ima, p);
00124 file.write((char*) (&src.val()), p.run_length() * data_size);
00125 }
00126 }
00127
00128 }
00129
00130
00131
00132
00133
00134 template <typename I>
00135 void save(const Image<I>& ima_, const std::string& filename)
00136 {
00137 trace::entering("mln::io::raw::save");
00138
00139 mlc_bool(mln_site_(I)::dim == 2 || mln_site_(I)::dim == 3)::check();
00140
00141 const I& ima = exact(ima_);
00142
00143 std::ofstream file(filename.c_str());
00144 if (! file)
00145 {
00146 std::cerr << "error: cannot open file '" << filename << "'!";
00147 abort();
00148 }
00149
00150 std::string info_filename = filename + ".info";
00151 std::ofstream info_file(info_filename.c_str());
00152 if (! info_file)
00153 {
00154 std::cerr << "error: cannot open file '" << info_filename << "'!";
00155 abort();
00156 }
00157
00158
00159 internal::save_header(ima, info_file);
00160 internal::save_data(ima, file);
00161
00162 info_file.close();
00163 file.close();
00164
00165 trace::exiting("mln::io::raw::save");
00166 }
00167
00168
00169 # endif // ! MLN_INCLUDE_ONLY
00170
00171 }
00172
00173 }
00174
00175 }
00176
00177 #endif // ! MLN_IO_RAW_SAVE_HH