Index: ChangeLog from Damien Thivolle damien@lrde.epita.fr
* oln/makefile.src: Add new files. * oln/io: New. * oln/io/read_image_2d_pnm.hh: New. 2D image pnm i/o (only raw pgm at the moment). * oln/io/read_image.hh: New. Abstract input routine. * oln/io/utils.hh: New. Tools for i/o manipulations. * oln/core/abstract/op.hh: New. Algorithms abstract class.
core/abstract/op.hh | 75 ++++++++++++++++++++++++ io/read_image.hh | 64 +++++++++++++++++++++ io/read_image_2d_pnm.hh | 140 ++++++++++++++++++++++++++++++++++++++++++++++ io/utils.hh | 146 ++++++++++++++++++++++++++++++++++++++++++++++++ makefile.src | 10 +-- 5 files changed, 430 insertions(+), 5 deletions(-)
Index: oln/makefile.src --- oln/makefile.src (revision 37) +++ oln/makefile.src (working copy) @@ -26,21 +26,21 @@ core/abstract/data_storage.hh \ core/abstract/image.hh \ core/abstract/image_constness.hh \ + core/abstract/image_identity.hh \ core/abstract/image_with_data.hh \ core/abstract/images.hh \ core/abstract/internal/image_impl.hh \ - core/abstract/morpher.hh \ core/abstract/piter.hh \ core/abstract/point.hh \ core/abstract/size.hh \ core/cats.hh \ core/coord.hh \ core/macros.hh \ - core/id_morpher.hh \ core/props.hh \ core/tags.hh \ core/value_box.hh \ fancy/iota.hh \ - fancy/print.hh - - + fancy/print.hh \ + io/read_image.hh \ + io/read_image_2d_pnm.hh \ + io/utils.hh Index: oln/io/read_image_2d_pnm.hh --- oln/io/read_image_2d_pnm.hh (revision 0) +++ oln/io/read_image_2d_pnm.hh (revision 0) @@ -0,0 +1,140 @@ +#ifndef OLN_IO_READ_IMAGE_2D_PNM_HH +# define OLN_IO_READ_IMAGE_2D_PNM_HH + +# include <iostream> +# include <string> + +# include <mlc/box.hh> + +# include <ntg/core/macros.hh> +# include <ntg/real/int_u8.hh> +# include <ntg/real/integer.hh> +# include <ntg/enum/enum.hh> +# include <ntg/color/color.hh> + +# include <oln/core/2d/image2d.hh> +# include <oln/core/macros.hh> +# include <oln/core/abstract/op.hh> + +# include <oln/io/utils.hh> + + +namespace oln { + + template <typename I> + struct image2d; + + namespace io { + + namespace impl { + + + template <typename I> + struct read_image_2d_pgm_raw: + oln::abstract::void_op<read_image_2d_pgm_raw<I> > + { + mlc::box<I> image_; + std::ifstream& istr_; + internal::pnm_info& info_; + + read_image_2d_pgm_raw(I& image, + std::ifstream &istr, + internal::pnm_info &info) : + image_(image), + istr_(istr), + info_(info) + {} + + typedef oln_value_type(I) value_type; + typedef typename mlc::traits<value_type>::encoding_type encoding_type; + + void impl_run() + { + encoding_type c; + + if (info_.max_val > ntg_max_val(value_type)) + { + std::cerr << "Can't load image, data type is not large enough" + << std::endl; + return; + } + c =c ; + point2d p; + oln::image2d<value_type> tmp(info_.rows, info_.cols); + + std::cout << info_.cols << "," << info_.rows << "," + << info_.max_val << std::endl; + + + for (p.row() = 0; p.row() < info_.rows && !istr_.eof(); ++p.row()) + for (p.col() = 0; p.col() < info_.cols && !istr_.eof(); ++p.col()) + { + istr_.read(&c, sizeof (encoding_type)); + tmp[p] = c; + } + istr_.close(); + *image_ = tmp; + + } + }; + + + template <typename I, typename T> + void read(abstract::image2d<I>& ima, + const ntg::integer<T>&, + const std::string& filename, + const std::string& ext) + { + std::ifstream istr; + internal::pnm_info info; + + if (internal::read_pnm_header(istr, info, filename)) + if (ext == "pgm") + if (info.type == "P5") + { + read_image_2d_pgm_raw<I> tmp(ima.exact(), istr, info); + tmp.run(); + } + else + if (info.type == "P2") + std::cerr << "read_image_2d_pgm_ascii not implemented" + << std::endl; + else + std::cerr << "file header (`" << info.type + << "') does not match file extension (`" + << ext << "')" << std::endl; + else + std::cerr << "image data type (`integer') does not match" + << " file extension (`" << ext << "')" << std::endl; + else + std::cerr << "bad header" << std::endl; + + } + + template <typename I, typename T> + void read(abstract::image2d<I>& ima, + const ntg::color<T>&, + const std::string& filename, + const std::string& ext) + { + std::cout << "read for image2d<color>" << std::endl; + } + + template <typename I, typename T> + void read(abstract::image2d<I>& ima, + const ntg::enum_value<T>&, + const std::string& filename, + const std::string& ext) + { + std::cout << "read for image2d<enum>" << std::endl; + } + + + } + + } + +} + + +#endif // ! OLN_IO_READ_IMAGE_2D_PNM_HH Index: oln/io/read_image.hh --- oln/io/read_image.hh (revision 0) +++ oln/io/read_image.hh (revision 0) @@ -0,0 +1,64 @@ +#ifndef OLN_IO_READ_IMAGE_HH +# define OLN_IO_READ_IMAGE_HH + +# include <iostream> +# include <string> + +# include <mlc/box.hh> + +# include <oln/core/macros.hh> + +# include <oln/io/read_image_2d_pnm.hh> +# include <oln/io/utils.hh> + +namespace oln { + + + namespace io { + + struct filename + { + explicit filename(const std::string& str) : + str_(str) + {} + const std::string& get() const + { + return str_; + } + private: + std::string str_; + }; + + filename read(const std::string& str) + { + filename tmp(str); + return tmp; + } + + template <typename I> + void do_read(abstract::image<I>& ima, const filename& name) + { + std::string ext; + const oln_value_type(I) t; + + ext = internal::utils::extension(name.get()); + + if (ext == "pgm" || + ext == "pbm" || + ext == "ppm") + impl::read(ima.exact(), t, name.get(), ext); + else + { + std::cout << "input method for '" + << name.get() + << "' not implemented yet" + << std::endl; + } + + } + + } + +} + +#endif // ! OLN_IO_READ_IMAGE_HH Index: oln/io/utils.hh --- oln/io/utils.hh (revision 0) +++ oln/io/utils.hh (revision 0) @@ -0,0 +1,146 @@ +// Copyright (C) 2001, 2002, 2003, 2004, 2005 EPITA Research and Development Laboratory +// +// This file is part of the Olena Library. This library is free +// software; you can redistribute it and/or modify it under the terms +// of the GNU General Public License version 2 as published by the +// Free Software Foundation. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this library; see the file COPYING. If not, write to +// the Free Software Foundation, 59 Temple Place - Suite 330, Boston, +// MA 02111-1307, USA. +// +// As a special exception, you may use this file as part of a free +// software library without restriction. Specifically, if other files +// instantiate templates or use macros or inline functions from this +// file, or you compile this file and link it with other files to +// produce an executable, this file does not by itself cause the +// resulting executable to be covered by the GNU General Public +// License. This exception does not however invalidate any other +// reasons why the executable file might be covered by the GNU General +// Public License. + +#ifndef OLENA_IO_UTILS_HH +# define OLENA_IO_UTILS_HH + +# include <oln/config/system.hh> + +# include <iostream> +# include <fstream> +# include <string> + +namespace oln { + + namespace io { + + namespace internal { + + /*! + ** \brief Utils for io (get extension of a file). + */ + struct utils + { + + /*! + ** \brief Return the extension of a filename. + ** \arg name The filename. + ** \return The extension (lower case). + */ + static std::string + extension(const std::string& name) + { + std::string ext; + int pos = name.rfind('.'); + if (pos > 0) + { + ext.assign(name, pos + 1, name.size() - pos); + for (std::string::iterator i = ext.begin(); i != ext.end(); ++i) + *i = tolower(*i); + } + return ext; + } + + }; + + + struct pnm_info + { + int max_val; + int rows; + int cols; + std::string type; + }; + + bool read_pnm_header(std::ifstream& istr, + internal::pnm_info& info, + const std::string& name) + { + istr.open(name.c_str(), std::ifstream::in); + + if (!istr.is_open()) + return false; + + std::getline(istr, info.type); + + // skip comments + while (istr.peek() == '#') + { + std::string line; + std::getline(istr, line); + } + + // read size + istr >> info.cols; + // skip comments + while (istr.peek() == '#') + { + std::string line; + std::getline(istr, line); + } + + + istr >> info.rows; + // skip comments + while (istr.peek() == '#') + { + std::string line; + std::getline(istr, line); + } + + if (info.cols <= 0 || info.rows <= 0) return false; + + // skip comments + while (istr.peek() == '#') + { + std::string line; + std::getline(istr, line); + } + + + // FIXME: it can be either '\n', 'whitespace', ..., not only '\n'! + + // extract or skip maxvalue + if (istr.get() != '\n') return false; + if (info.type != "P1" && info.type != "P4") + { + istr >> info.max_val; + if (info.max_val > 65535 || + istr.get() != '\n' || + info.max_val <= 0) + return false; + } + return true; + } + + } // end of namespace internal + + } // end of namespace io + +} // end of namespace oln + +#endif // ! OLENA_IO_UTILS_HH Index: oln/core/abstract/op.hh --- oln/core/abstract/op.hh (revision 0) +++ oln/core/abstract/op.hh (revision 0) @@ -0,0 +1,75 @@ +// Copyright (C) 2005 EPITA Research and Development Laboratory +// +// This file is part of the Olena Library. This library is free +// software; you can redistribute it and/or modify it under the terms +// of the GNU General Public License version 2 as published by the +// Free Software Foundation. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this library; see the file COPYING. If not, write to +// the Free Software Foundation, 59 Temple Place - Suite 330, Boston, +// MA 02111-1307, USA. +// +// As a special exception, you may use this file as part of a free +// software library without restriction. Specifically, if other files +// instantiate templates or use macros or inline functions from this +// file, or you compile this file and link it with other files to +// produce an executable, this file does not by itself cause the +// resulting executable to be covered by the GNU General Public +// License. This exception does not however invalidate any other +// reasons why the executable file might be covered by the GNU General +// Public License. + +#ifndef OLENA_CORE_ABSTRACT_OP_HH +# define OLENA_CORE_ABSTRACT_OP_HH + +# include <oln/core/abstract/image_identity.hh> + +namespace oln { + + namespace abstract { + + template <typename I, typename E> + struct op : public oln::abstract::image_identity<I, E> + { + typedef oln::abstract::image_identity<I, E> super_type; + + op(I& ref): super_type(ref) + { + this->exact_ptr = (E*)(void*)(this); + } + + op() {} + + void run() + { + impl_run(); + } + virtual void impl_run() + { + std::cerr << "oops (nothing)" << std::endl; + } + }; + + + template <typename E> + struct void_op : public mlc::any<E> + { + void_op() {} + + void run() + { + this->exact().impl_run(); + } + }; + + } + +} + +#endif // ! OLENA_CORE_ABSTRACT_OP_HH