• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List

save.hh

00001 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE)
00002 //
00003 // This file is part of Olena.
00004 //
00005 // Olena is free software: you can redistribute it and/or modify it under
00006 // the terms of the GNU General Public License as published by the Free
00007 // Software Foundation, version 2 of the License.
00008 //
00009 // Olena is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Olena.  If not, see <http://www.gnu.org/licenses/>.
00016 //
00017 // As a special exception, you may use this file as part of a free
00018 // software project without restriction.  Specifically, if other files
00019 // instantiate templates or use macros or inline functions from this
00020 // file, or you compile this file and link it with other files to produce
00021 // an executable, this file does not by itself cause the resulting
00022 // executable to be covered by the GNU General Public License.  This
00023 // exception does not however invalidate any other reasons why the
00024 // executable file might be covered by the GNU General Public License.
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   // Fwd decl.
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; // In pbm, '0' means 'white' so 'object', thus 'true'!
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       } // end of namespace mln::io::impl
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     } // end of namespace mln::pbm
00129 
00130   } // end of namespace mln::io
00131 
00132 } // end of namespace mln
00133 
00134 
00135 #endif // ! MLN_IO_PBM_SAVE_HH

Generated on Tue Oct 4 2011 15:24:23 for Milena (Olena) by  doxygen 1.7.1