Milena (Olena)
User documentation 2.0a Id
|
00001 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 00002 // 2011 EPITA Research and Development Laboratory (LRDE) 00003 // 00004 // This file is part of Olena. 00005 // 00006 // Olena is free software: you can redistribute it and/or modify it under 00007 // the terms of the GNU General Public License as published by the Free 00008 // Software Foundation, version 2 of the License. 00009 // 00010 // Olena is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 // General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Olena. If not, see <http://www.gnu.org/licenses/>. 00017 // 00018 // As a special exception, you may use this file as part of a free 00019 // software project without restriction. Specifically, if other files 00020 // instantiate templates or use macros or inline functions from this 00021 // file, or you compile this file and link it with other files to produce 00022 // an executable, this file does not by itself cause the resulting 00023 // executable to be covered by the GNU General Public License. This 00024 // exception does not however invalidate any other reasons why the 00025 // executable file might be covered by the GNU General Public License. 00026 00027 #ifndef MLN_IO_PBM_SAVE_HH 00028 # define MLN_IO_PBM_SAVE_HH 00029 00034 00035 # include <iostream> 00036 # include <fstream> 00037 00038 # include <mln/geom/size2d.hh> 00039 # include <mln/metal/equal.hh> 00040 # include <mln/metal/bexpr.hh> 00041 00042 # include <mln/io/pnm/save.hh> 00043 00044 00045 namespace mln 00046 { 00047 00048 // Fwd decl. 00049 namespace value { 00050 template <unsigned> struct int_u; 00051 template <unsigned> struct int_u_sat; 00052 } 00053 00054 00055 namespace io 00056 { 00057 00058 namespace pbm 00059 { 00060 00066 template <typename I> 00067 void save(const Image<I>& ima, const std::string& filename); 00068 00069 00070 # ifndef MLN_INCLUDE_ONLY 00071 00072 namespace impl 00073 { 00074 00075 template <typename I> 00076 inline 00077 void save_(const Image<I>& ima_, const std::string& filename) 00078 { 00079 const I& ima = exact(ima_); 00080 std::ofstream file(filename.c_str()); 00081 00082 io::pnm::save_header(PBM, ima, filename, file); 00083 00084 def::coord 00085 ncols = static_cast<def::coord>(geom::ncols(ima)), 00086 col = 0, 00087 stride = 0; 00088 unsigned char c = 0; 00089 00090 mln_fwd_piter(I) p(ima.domain()); 00091 for_all(p) 00092 { 00093 c = static_cast<unsigned char>(c << 1); 00094 if (ima(p) == false) 00095 ++c; // In pbm, '0' means 'white' so 'object', thus 'true'! 00096 if (++col >= ncols) 00097 { 00098 c = static_cast<unsigned char>(c << (8 - stride - 1)); 00099 file << c; 00100 c = 0; 00101 col = stride = 0; 00102 } 00103 else 00104 if (++stride >= 8) 00105 { 00106 file << c; 00107 c = 0; 00108 stride = 0; 00109 } 00110 } 00111 mln_postcondition(stride == 0); 00112 } 00113 00114 } // end of namespace mln::io::impl 00115 00116 00117 template <typename I> 00118 inline 00119 void save(const Image<I>& ima, const std::string& filename) 00120 { 00121 trace::entering("mln::io::pbm::save"); 00122 mln::metal::equal<mln_value(I), bool >::check(); 00123 impl::save_(exact(ima), filename); 00124 trace::exiting("mln::io::pbm::save"); 00125 } 00126 00127 # endif // ! MLN_INCLUDE_ONLY 00128 00129 } // end of namespace mln::pbm 00130 00131 } // end of namespace mln::io 00132 00133 } // end of namespace mln 00134 00135 00136 #endif // ! MLN_IO_PBM_SAVE_HH