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_PBM_SAVE_HH
00027 # define MLN_IO_PBM_SAVE_HH
00028 
00033 
00034 # include <iostream>
00035 # include <fstream>
00036 
00037 # include <mln/geom/size2d.hh>
00038 # include <mln/metal/equal.hh>
00039 # include <mln/metal/bexpr.hh>
00040 
00041 # include <mln/io/pnm/save.hh>
00042 
00043 
00044 namespace mln
00045 {
00046 
00047   
00048   namespace value {
00049     template <unsigned> class int_u;
00050     template <unsigned> class int_u_sat;
00051   }
00052 
00053 
00054   namespace io
00055   {
00056 
00057     namespace pbm
00058     {
00059 
00065       template <typename I>
00066       void save(const Image<I>& ima, const std::string& filename);
00067 
00068 
00069 # ifndef MLN_INCLUDE_ONLY
00070 
00071       namespace impl
00072       {
00073 
00074         template <typename I>
00075         inline
00076         void save_(const Image<I>& ima_, const std::string& filename)
00077         {
00078           const I& ima = exact(ima_);
00079           std::ofstream file(filename.c_str());
00080 
00081           io::pnm::save_header(PBM, ima, filename, file);
00082 
00083           def::coord
00084             ncols = static_cast<def::coord>(geom::ncols(ima)),
00085             col = 0,
00086             stride = 0;
00087           unsigned char c = 0;
00088 
00089           mln_fwd_piter(I) p(ima.domain());
00090           for_all(p)
00091             {
00092               c = static_cast<unsigned char>(c << 1);
00093               if (ima(p) == false)
00094                 ++c; 
00095               if (++col >= ncols)
00096                 {
00097                   c = static_cast<unsigned char>(c << (8 - stride - 1));
00098                   file << c;
00099                   c = 0;
00100                   col = stride = 0;
00101                 }
00102               else
00103                 if (++stride >= 8)
00104                   {
00105                     file << c;
00106                     c = 0;
00107                     stride = 0;
00108                   }
00109             }
00110           mln_postcondition(stride == 0);
00111         }
00112 
00113       } 
00114 
00115 
00116       template <typename I>
00117       inline
00118       void save(const Image<I>& ima, const std::string& filename)
00119       {
00120         trace::entering("mln::io::pbm::save");
00121         mln::metal::equal<mln_value(I), bool >::check();
00122         impl::save_(exact(ima), filename);
00123         trace::exiting("mln::io::pbm::save");
00124       }
00125 
00126 # endif // ! MLN_INCLUDE_ONLY
00127 
00128     } 
00129 
00130   } 
00131 
00132 } 
00133 
00134 
00135 #endif // ! MLN_IO_PBM_SAVE_HH