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_PFM_SAVE_HH
00027 # define MLN_IO_PFM_SAVE_HH
00028 
00033 
00034 # include <iostream>
00035 # include <fstream>
00036 
00037 # include <mln/core/alias/point2d.hh>
00038 # include <mln/geom/size2d.hh>
00039 # include <mln/metal/equal.hh>
00040 # include <mln/metal/bexpr.hh>
00041 
00042 
00043 namespace mln
00044 {
00045 
00046   namespace io
00047   {
00048 
00049     namespace pfm
00050     {
00051 
00057       template <typename I>
00058       void save(const Image<I>& ima, const std::string& filename);
00059 
00060 # ifndef MLN_INCLUDE_ONLY
00061 
00062       namespace impl
00063       {
00064 
00065         template <typename I>
00066         inline
00067         void save_header_(const I& ima, std::ofstream& file)
00068         {
00069           file <<  geom::nrows(ima) << ' ' << geom::nrows(ima) << std::endl
00070                << "float" << std::endl;
00071         }
00072 
00073         template <typename I>
00074         inline
00075         void save_(const Image<I>& ima_, const std::string& filename)
00076         {
00077           const I& ima = exact(ima_);
00078 
00079           std::ofstream file(filename.c_str());
00080           save_header_(ima, file);
00081 
00082           unsigned int ncols = geom::ncols(ima);
00083 
00084           const mln_deduce(I, site, coord)
00085             min_row = geom::min_row(ima),
00086             max_row = geom::max_row(ima);
00087 
00088           
00089           point2d p(point2d(0, 0));
00090           for (p.row() = min_row; p.row() <= max_row; ++p.row())
00091             file.write((const char*)(&(ima(p))),
00092                        sizeof(float) * ncols);
00093           file.close();
00094 
00095         }
00096 
00097       } 
00098 
00099       template <typename I>
00100       inline
00101       void save(const Image<I>& ima, const std::string& filename)
00102       {
00103         trace::entering("mln::io::pfm::save");
00104         mln::metal::equal<mln_value(I), float >::check();
00105         impl::save_(exact(ima), filename);
00106         trace::exiting("mln::io::pfm::save");
00107       }
00108 
00109 # endif // ! MLN_INCLUDE_ONLY
00110 
00111     } 
00112 
00113   } 
00114 
00115 } 
00116 
00117 
00118 #endif // ! MLN_IO_PFM_SAVE_HH