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
00027 #ifndef MLN_IO_RAW_LOAD_HH
00028 # define MLN_IO_RAW_LOAD_HH
00029
00033
00034 # include <iostream>
00035 # include <fstream>
00036
00037 # include <mln/core/concept/image.hh>
00038 # include <mln/core/routine/initialize.hh>
00039 # include <mln/core/box_runstart_piter.hh>
00040 # include <mln/core/pixel.hh>
00041 # include <mln/data/memcpy_.hh>
00042
00043 namespace mln
00044 {
00045
00046 namespace io
00047 {
00048
00049 namespace raw
00050 {
00051
00061
00062 template <typename I>
00063 void load(Image<I>& ima_, const std::string& filename);
00064
00065
00066 # ifndef MLN_INCLUDE_ONLY
00067
00068 namespace internal
00069 {
00070
00071 template <typename P>
00072 inline
00073 void read_point(std::ifstream& file, P& p)
00074 {
00075 for (unsigned i = 0; i < P::dim; ++i)
00076 file >> p[i];
00077 }
00078
00079
00080 template <typename I>
00081 inline
00082 void load_header(Image<I>& ima, std::ifstream& info_file,
00083 const std::string& filename)
00084 {
00085
00086 std::string file_type;
00087 info_file >> file_type;
00088 if (file_type != "milena/raw")
00089 {
00090 std::cerr << "io::raw::load - Error: invalid file type. '"
00091 << filename
00092 << "' is NOT a valid milena/raw info file!"
00093 << std::endl;
00094 abort();
00095 }
00096
00097 char dev_null[255];
00098
00099
00100
00101 info_file.read(dev_null, 5);
00102
00103 unsigned dim;
00104 info_file >> dim;
00105
00106 typedef mln_site(I) P;
00107 if (P::dim != dim)
00108 {
00109 std::cerr << "io::raw::load - Error: invalid image dimension. '"
00110 << filename << "' is a " << dim << "-D image "
00111 << "but you try to load it into a " << P::dim
00112 << "-D image!"
00113 << std::endl;
00114 abort();
00115 }
00116
00117
00118 std::string tmp;
00119 for (unsigned i = 0; i < dim; ++i)
00120 info_file >> tmp;
00121
00122 char c;
00123 info_file.get(c);
00124
00125
00126
00127 info_file.read(dev_null, 11);
00128
00129 char value_type[255];
00130 info_file.getline(value_type, 255);
00131 if (mln_trait_value_name(mln_value(I)) != std::string(value_type))
00132 {
00133 std::cerr << "io::raw::load - Error: invalid image value type. '"
00134 << filename << "' is an image of '" << value_type
00135 << "' but you try to load it into an image of '"
00136 << mln_trait_value_name(mln_value(I)) << "'!"
00137 << std::endl;
00138 abort();
00139 }
00140
00141
00142
00143 info_file.read(dev_null, 10);
00144 P pmin;
00145 read_point<P>(info_file, pmin);
00146
00147
00148
00149 info_file.read(dev_null, 14);
00150 P pmax;
00151 read_point<P>(info_file, pmax);
00152 std::cout << pmax << std::endl;
00153
00154
00155 mln_concrete(I) result(box<P>(pmin, pmax));
00156 initialize(ima, result);
00157 mln_assertion(exact(ima).is_valid());
00158 }
00159
00160
00161 template <typename I>
00162 inline
00163 void load_data(Image<I>& ima_, std::ifstream& file)
00164 {
00165 I& ima = exact(ima_);
00166
00167
00168 unsigned data_size = sizeof (mln_value(I)) + sizeof (mln_value(I)) % 2;
00169
00170 mln_box_runstart_piter(I) p(ima.domain());
00171 for_all(p)
00172 {
00173 pixel<I> src(ima, p);
00174 file.read((char*) (&src.val()), p.run_length() * data_size);
00175 }
00176
00177 }
00178
00179 }
00180
00181
00182
00183 template <typename I>
00184 void load(Image<I>& ima, const std::string& filename)
00185 {
00186 trace::entering("mln::io::raw::load");
00187
00188 std::ifstream file(filename.c_str());
00189 if (! file)
00190 {
00191 std::cerr << "io::raw::load - error: cannot open file '"
00192 << filename << "'!";
00193 abort();
00194 }
00195
00196 std::string info_filename = filename + ".info";
00197 std::ifstream info_file(info_filename.c_str());
00198 if (! info_file)
00199 {
00200 std::cerr << "io::raw::load - error: cannot open file '"
00201 << info_filename << "'!";
00202 abort();
00203 }
00204
00205
00206 internal::load_header(ima, info_file, info_filename);
00207 internal::load_data(ima, file);
00208
00209 mln_postcondition(exact(ima).is_valid());
00210
00211 file.close();
00212 info_file.close();
00213
00214 trace::exiting("mln::io::raw::load");
00215 }
00216
00217
00218 # endif // ! MLN_INCLUDE_ONLY
00219
00220 }
00221
00222 }
00223
00224 }
00225
00226 #endif // ! MLN_IO_RAW_LOAD_HH