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
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
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;
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 }
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 }
00130
00131 }
00132
00133 }
00134
00135
00136 #endif // ! MLN_IO_PBM_SAVE_HH