Milena (Olena)
User documentation 2.0a Id
|
00001 // Copyright (C) 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_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'); // In pbm, '0' means 'white' so 'object', thus 'true'! 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 } // end of namespace mln::io::internal 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 } // end of namespace mln::io::pbm 00166 00167 } // end of namespace mln::io 00168 00169 } // end of namespace mln 00170 00171 00172 #endif // ! MLN_IO_PBM_LOAD_HH