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_LOAD_HH
00027 # define MLN_IO_PBM_LOAD_HH
00028 
00033 
00034 
00035 # include <iostream>
00036 # include <fstream>
00037 # include <string>
00038 
00039 # include <mln/core/image/image2d.hh>
00040 # include <mln/core/image/image3d.hh>
00041 # include <mln/io/pnm/load_header.hh>
00042 
00043 
00044 namespace mln
00045 {
00046 
00047   namespace io
00048   {
00049 
00050     namespace pbm
00051     {
00052 
00053 
00060       void load(image2d<bool>& ima,
00061                 const std::string& filename);
00062 
00069       image2d<bool> load(const std::string& filename);
00070 
00071 
00072 # ifndef MLN_INCLUDE_ONLY
00073 
00074       namespace internal
00075       {
00076 
00078         template <typename I>
00079         inline
00080         void load_ascii(std::ifstream& file, I& ima)
00081         {
00082           mln_fwd_piter(I) p(ima.domain());
00083           for_all(p)
00084             {
00085               unsigned char value;
00086               file >> value;
00087 
00088               mln_assertion(value == '0' || value == '1');
00089               ima(p) = (value == '0'); 
00090             }
00091         }
00092 
00093 
00095         template <typename I>
00096         inline
00097         void load_raw_2d(std::ifstream& file, I& ima)
00098         {
00099           point2d p = point2d(0, ima.domain().pmin().col());
00100           typedef mln_value(I) V;
00101           const mln_deduce(I, site, coord)
00102             min_row = geom::min_row(ima),
00103             max_row = geom::max_row(ima),
00104             min_col = geom::min_col(ima),
00105             max_col = geom::max_col(ima);
00106 
00107           char c;
00108           int i;
00109           for (p.row() = min_row; p.row() <= max_row; ++p.row())
00110           {
00111             i = 0;
00112             for (p.col() = min_col; p.col() <= max_col; ++p.col())
00113               {
00114                 if (i % 8 == 0)
00115                   file.read((char*)(&c), 1);
00116                 ima(p) = !(c & 128);
00117                 c = static_cast<char>(c * 2);
00118                 ++i;
00119               }
00120           }
00121         }
00122 
00123 
00124       } 
00125 
00126 
00127       inline
00128       image2d<bool> load(const std::string& filename)
00129       {
00130         trace::entering("mln::io::pbm::load");
00131         std::ifstream file(filename.c_str());
00132         if (! file)
00133         {
00134           std::cerr << "error: file '" << filename
00135                     << "' not found!";
00136           abort();
00137         }
00138         char type;
00139         int nrows, ncols;
00140         io::pnm::read_header('1', '4', file, type, nrows, ncols);
00141 
00142         image2d<bool> ima(nrows, ncols);
00143         if (type == '4')
00144           internal::load_raw_2d(file, ima);
00145         else
00146           if (type == '1')
00147             internal::load_ascii(file, ima);
00148 
00149         trace::exiting("mln::io::pbm::load");
00150 
00151         return ima;
00152       }
00153 
00154 
00155       inline
00156       void load(image2d<bool>& ima,
00157                 const std::string& filename)
00158       {
00159         ima = load(filename);
00160       }
00161 
00162 
00163 # endif // ! MLN_INCLUDE_ONLY
00164 
00165     } 
00166 
00167   } 
00168 
00169 } 
00170 
00171 
00172 #endif // ! MLN_IO_PBM_LOAD_HH