Index: ChangeLog from Damien Thivolle damien@lrde.epita.fr
* oln/makefile.src: Add new files. * oln/io/write_image_2d_pnm.hh: New. pgm output method for 2D images. * oln/io/read_image_2d_pnm.hh: Handle mlc::box correctly. * oln/io/write_image.hh: New. Dispatch for output methods. * oln/io/read_image.hh: Handle mlc::box correctly.. * oln/io/utils.hh: Add pnm_write_header function.
io/read_image.hh | 30 +++++++++++ io/read_image_2d_pnm.hh | 20 +++++-- io/utils.hh | 32 ++++++++++++ io/write_image.hh | 63 ++++++++++++++++++++++++ io/write_image_2d_pnm.hh | 119 +++++++++++++++++++++++++++++++++++++++++++++++ makefile.src | 5 + 6 files changed, 260 insertions(+), 9 deletions(-)
Index: oln/makefile.src --- oln/makefile.src (revision 40) +++ oln/makefile.src (working copy) @@ -30,6 +30,7 @@ core/abstract/image_with_data.hh \ core/abstract/images.hh \ core/abstract/internal/image_impl.hh \ + core/abstract/op.hh \ core/abstract/piter.hh \ core/abstract/point.hh \ core/abstract/size.hh \ @@ -43,4 +44,6 @@ fancy/print.hh \ io/read_image.hh \ io/read_image_2d_pnm.hh \ - io/utils.hh + io/utils.hh \ + io/write_image.hh \ + io/write_image_2d_pnm.hh Index: oln/io/write_image_2d_pnm.hh --- oln/io/write_image_2d_pnm.hh (revision 0) +++ oln/io/write_image_2d_pnm.hh (revision 0) @@ -0,0 +1,119 @@ +// 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 OLN_IO_WRITE_IMAGE_2D_PNM_HH +# define OLN_IO_WRITE_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 { + + namespace io { + + namespace impl { + + template <typename I> + struct write_image_2d_pgm_raw + : public oln::abstract::void_op<write_image_2d_pgm_raw<I> > + { + typedef oln_value_type(I) value_type; + typedef typename mlc::traits<value_type>::encoding_type out_type; + + const I& to_write_; + std::ofstream& ostr_; + + write_image_2d_pgm_raw(const I& to_write, std::ofstream& ostr): + to_write_(to_write), + ostr_(ostr) + {} + + void impl_run() + { + point2d p; + out_type c; + + for (p.row() = 0; p.row() < to_write_.size().nrows(); ++p.row()) + for (p.col() = 0; p.col() < to_write_.size().ncols(); ++p.col()) + { + c = to_write_[p]; + ostr_ << c; + } + ostr_.close(); + } + + }; + + template <typename I, typename T> + void write(const abstract::image2d<I>& to_write, + const ntg::integer<T>&, + const std::string& name, + const std::string& ext) + { + std::ofstream ostr; + point2d p; + size2d s = to_write.size(); + + if (ext == "pgm") + if (internal::write_pnm_header(ostr, name, "P5", + s.ncols(), s.nrows(), + ntg_max_val(T))) + { + write_image_2d_pgm_raw<I> tmp(to_write.exact(), ostr); + tmp.run(); + } + else + std::cerr << "error: unable to write header" << std::endl; + else + std::cerr << "error: image data type (`integer') does not match" + << " file extension (`" << ext << "')" << std::endl; + } + + } + + } + +} + + + +#endif // ! OLN_IO_WRITE_IMAGE_2D_PNM_HH Index: oln/io/read_image_2d_pnm.hh --- oln/io/read_image_2d_pnm.hh (revision 40) +++ oln/io/read_image_2d_pnm.hh (working copy) @@ -48,6 +48,12 @@ typedef oln_value_type(I) value_type; typedef typename mlc::traits<value_type>::encoding_type encoding_type;
+ read_image_2d_pgm_raw<I>& output(I& output) + { + output = *image_; + return *this; + } + void impl_run() { encoding_type c; @@ -58,7 +64,6 @@ << std::endl; return; } - c =c ; point2d p; oln::image2d<value_type> tmp(info_.rows, info_.cols);
@@ -74,7 +79,7 @@ } istr_.close(); *image_ = tmp; - + std::cout << "debug : size = " << image_->size() << std::endl; } };
@@ -94,21 +99,24 @@ { read_image_2d_pgm_raw<I> tmp(ima.exact(), istr, info); tmp.run(); + tmp.output(ima.exact()); } else if (info.type == "P2") - std::cerr << "read_image_2d_pgm_ascii not implemented" + std::cerr << "error: read_image_2d_pgm_ascii not implemented" << std::endl; else - std::cerr << "file header (`" << info.type + std::cerr << "error: file header (`" << info.type << "') does not match file extension (`" << ext << "')" << std::endl; else - std::cerr << "image data type (`integer') does not match" + std::cerr << "error: image data type (`integer') does not match" << " file extension (`" << ext << "')" << std::endl; else - std::cerr << "bad header" << std::endl; + std::cerr << "error: unable to get a valid header" << std::endl; + std::cout << "debug : size = " << ima.size() << std::endl;
+ }
template <typename I, typename T> Index: oln/io/write_image.hh --- oln/io/write_image.hh (revision 0) +++ oln/io/write_image.hh (revision 0) @@ -0,0 +1,63 @@ +// 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 OLN_IO_WRITE_IMAGE_HH +# define OLN_IO_WRITE_IMAGE_HH + +# include <string> + +# include <oln/io/utils.hh> +# include <oln/io/write_image_2d_pnm.hh> + +namespace oln { + + namespace io { + + template <typename E> + void write(const abstract::image<E>& im, const std::string& name) + { + std::string ext; + oln_value_type(E) t; + + ext = internal::utils::extension(name); + + if (ext == "pgm" || + ext == "ppm" || + ext == "pbm") + impl::write(im.exact(), t, name, ext); + else + std::cerr << "error: output method for '" + << name.c_str() + << "' not implemented" + << std::endl; + } + + } + +} + +#endif // ! OLN_IO_WRITE_IMAGE_HH Index: oln/io/read_image.hh --- oln/io/read_image.hh (revision 40) +++ oln/io/read_image.hh (working copy) @@ -1,3 +1,30 @@ +// 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 OLN_IO_READ_IMAGE_HH # define OLN_IO_READ_IMAGE_HH
@@ -13,7 +40,6 @@
namespace oln {
- namespace io {
struct filename @@ -51,7 +77,7 @@ { std::cout << "input method for '" << name.get() - << "' not implemented yet" + << "' not implemented" << std::endl; }
Index: oln/io/utils.hh --- oln/io/utils.hh (revision 40) +++ oln/io/utils.hh (working copy) @@ -137,6 +137,38 @@ return true; }
+ bool write_pnm_header(std::ofstream& ostr, + const std::string& name, + const std::string& type, + int ncols, + int nrows, + int max_val) + { + if (max_val > 65535) + { + std::cerr << "error: can't save " << name + << ", data type too large" + << std::endl; + return false; + } + + ostr.open(name.c_str(), std::ofstream::out); + + if (ostr.is_open() == false) + { + std::cerr << "error: couldn't open " << name << std::endl; + return false; + } + + + ostr << "P5" << std::endl + << "# Olena 1.0" << std::endl + << ncols << " " << nrows << std::endl + << max_val << std::endl; + return true; + } + + } // end of namespace internal
} // end of namespace io