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_SAVE_HH
00027 # define MLN_IO_DUMP_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 dump
00052     {
00053 
00058       
00059       template <typename I>
00060       void save(const Image<I>& ima_, const std::string& filename);
00061 
00062 
00063 
00064 # ifndef MLN_INCLUDE_ONLY
00065 
00066 
00067       namespace internal
00068       {
00069 
00070         template <typename I>
00071         inline
00072         void save_header(const I& ima,
00073                          std::ofstream& file)
00074         {
00075           
00076           file << "milena/dump" << std::endl;
00077 
00078           
00079           typedef mln_site(I) P;
00080           file << P::dim << std::endl;
00081 
00082           
00083           typedef algebra::vec<P::dim, unsigned> vec_t;
00084           vec_t size = ima.domain().pmax() - ima.domain().pmin();
00085           for (unsigned i = 0; i < P::dim - 1; ++i)
00086             file << size[i] + 1 << " ";
00087           file << size[P::dim - 1] + 1 << std::endl;
00088 
00089           
00090           
00091           file << mln_trait_value_name(mln_value(I)) << std::endl;
00092 
00093           
00094           file << std::endl;
00095 
00096           
00097           file << std::endl;
00098 
00099           
00100           mln_site(I) p = ima.domain().pmin();
00101           file.write((char*) (&p), sizeof (P));
00102 
00103           
00104           p = ima.domain().pmax();
00105           file.write((char*) (&p), sizeof (P));
00106         }
00107 
00108 
00109         template <typename I>
00110         inline
00111         void save_data(I& ima, std::ofstream& file)
00112         {
00113           
00114           unsigned data_size = sizeof (mln_value(I)) + sizeof (mln_value(I)) % 2;
00115 
00116           mln_box_runstart_piter(I) p(ima.domain());
00117           for_all(p)
00118           {
00119             pixel<I> src(ima, p);
00120             file.write((char*) (&src.val()), p.run_length() * data_size);
00121           }
00122         }
00123 
00124       } 
00125 
00126 
00127 
00128       
00129 
00130       template <typename I>
00131       void save(const Image<I>& ima_, const std::string& filename)
00132       {
00133         trace::entering("mln::io::dump::save");
00134 
00135         const I& ima = exact(ima_);
00136 
00137         std::ofstream file(filename.c_str());
00138         if (! file)
00139         {
00140           std::cerr << "error: cannot open file '" << filename << "'!";
00141           abort();
00142         }
00143 
00144         internal::save_header(ima, file);
00145         internal::save_data(ima, file);
00146 
00147 
00148         file.close();
00149 
00150         trace::exiting("mln::io::dump::save");
00151       }
00152 
00153 
00154 # endif // ! MLN_INCLUDE_ONLY
00155 
00156     } 
00157 
00158   } 
00159 
00160 } 
00161 
00162 #endif // ! MLN_IO_DUMP_SAVE_HH