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