proto-1.0 246: Add new I/O implementations and facades

Index: ChangeLog from Damien Thivolle <damien@lrde.epita.fr> * oln/io/pnm_bin.hh: New. I/O functions for writing and reading a binary PNM file. * oln/io/pnm_read.hh: New. Facade to PNM input methods. * oln/io/read_image_pnm.hh: Remove. * oln/io/vectorial.hh: New. Read to and write from vectorial images. * oln/io/write_image_pnm_2d.hh: Remove. * oln/io/write_image.hh: Adapt to the new I/O modeling. * oln/io/write_image_pnm_decl.hh: Remove. * oln/io/pnm_real.hh: New. I/O functions for writing and reading a grey scale PNM file. * oln/io/utils.hh: Move pnm functions and structures to pnm_header.hh. * oln/io/pnm_header.hh: New. Contain functions and structures to read and write a PNM header. * oln/io/pnm_write.hh: New. Facade to PNM output methods. * oln/io/scalar.hh: New. Read to and write from scalar images. * oln/io/write_image_pnm.hh: Remove. * oln/io/pnm_vect.hh: New. I/O functions for writing and reading a rgb PNM file. * oln/io/read_image_pnm_2d.hh: Remove. * oln/io/read_image.hh: Adapt to the new I/O modeling. * oln/io/read_image_pnm_decl.hh: Remove. pnm_bin.hh | 215 ++++++++++++++++++++++++++++++++++++++++++++++++ pnm_header.hh | 129 ++++++++++++++++++++++++++++ pnm_read.hh | 198 ++++++++++++++++++++++++++++++++++++++++++++ pnm_real.hh | 169 +++++++++++++++++++++++++++++++++++++ pnm_vect.hh | 169 +++++++++++++++++++++++++++++++++++++ pnm_write.hh | 165 ++++++++++++++++++++++++++++++++++++ read_image.hh | 6 - read_image_pnm.hh | 157 ----------------------------------- read_image_pnm_2d.hh | 166 ------------------------------------- read_image_pnm_decl.hh | 57 ------------ scalar.hh | 78 +++++++++++++++++ utils.hh | 98 --------------------- vectorial.hh | 88 +++++++++++++++++++ write_image.hh | 5 - write_image_pnm.hh | 173 -------------------------------------- write_image_pnm_2d.hh | 153 ---------------------------------- write_image_pnm_decl.hh | 57 ------------ 17 files changed, 1217 insertions(+), 866 deletions(-) Index: oln/io/pnm_write.hh --- oln/io/pnm_write.hh (revision 0) +++ oln/io/pnm_write.hh (revision 0) @@ -0,0 +1,165 @@ +// 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_PNM_WRITE_HH +# define OLN_IO_PNM_WRITE_HH + +# include <iostream> + +# include <ntg/core/macros.hh> + +# include <oln/core/abstract/image.hh> + +# include <oln/io/scalar.hh> +# include <oln/io/vectorial.hh> + +# include <oln/io/pnm_real.hh> +# include <oln/io/pnm_bin.hh> +# include <oln/io/pnm_vect.hh> + +namespace oln { + + + namespace io { + + + namespace pnm { + + + namespace impl { + + template <typename V, typename I> + void write(const ntg::real_value<V>&, + const abstract::image<I>& ima, + std::ostream& ostr, + const std::string& ext) + { + typedef oln_type_of(I, grid) grid_type; + + if (ext == "pgm") + { + if (ntg_max_val(V) - ntg_min_val(V) < 256) + { + typedef write_real<char, grid_type> writer_type; + writer_type pnm_writer(ostr); + write_scalar<I, writer_type> writer(ima, pnm_writer); + + writer.run(); + } + else + { + typedef write_real<short, grid_type> writer_type; + writer_type pnm_writer(ostr); + write_scalar<I, writer_type> writer(ima, pnm_writer); + + writer.run(); + } + } + else + std::cerr << "io error: extension mismatch data type" << std::endl; + } + + template <typename V, typename I> + void write(const ntg::enum_value<V>&, + const abstract::image<I>& ima, + std::ostream& ostr, + const std::string& ext) + { + typedef oln_type_of(I, grid) grid_type; + + if (ext == "pbm") + { + assert(ntg_max_val(V) == 1 && ntg_min_val(V) == 0); + + typedef write_bin<grid_type> writer_type; + writer_type pnm_writer(ostr); + write_scalar<I, writer_type> writer(ima, pnm_writer); + + writer.run(); + } + else + std::cerr << "io error: extension mismatch data type" << std::endl; + } + + template <typename V, typename I> + void write(const ntg::vect_value<V>&, + const abstract::image<I>& ima, + std::ostream& ostr, + const std::string& ext) + { + typedef oln_type_of(I, grid) grid_type; + + if (ext == "ppm") + { + assert(ntg_nb_comp(V) == 3); + + if (ntg_max_val(ntg_comp_type(V)) - + ntg_min_val(ntg_comp_type(V)) < 256) + { + typedef write_vect<char, grid_type> writer_type; + writer_type pnm_writer(ostr); + write_vectorial<I, writer_type> writer(ima, pnm_writer); + + writer.run(); + } + else + { + typedef write_vect<short, grid_type> writer_type; + writer_type pnm_writer(ostr); + write_vectorial<I, writer_type> writer(ima, pnm_writer); + + writer.run(); + } + } + else + std::cerr << "io error: extension mismatch data type" << std::endl; + } + + + } + + template <typename I> + void write(const abstract::image<I>& ima, + std::ostream& ostr, + const std::string& ext) + { + typedef oln_type_of(I, value) value_type; + typedef ntg_type(value_type) ntg_value_type; + + impl::write(ntg_value_type(), ima.exact(), ostr, ext); + } + + + } + + + } + + +} + +# endif // ! OLN_IO_PNM_WRITE_HH Index: oln/io/vectorial.hh --- oln/io/vectorial.hh (revision 0) +++ oln/io/vectorial.hh (revision 0) @@ -0,0 +1,88 @@ +// 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 filek 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_VECTORIAL_HH +# define OLENA_IO_VECTORIAL_HH + +# include <oln/core/abstract/image.hh> +# include <oln/canvas/io.hh> + +namespace oln { + + + namespace io { + + + template <typename I, typename F> + struct read_vectorial + : public canvas::read_to_oln<I, F, read_vectorial<I, F> > + { + typedef oln_type_of(I, value) value_type; + typedef ntg_comp_type(value_type) comp_type; + typedef canvas::read_to_oln<I, F, read_vectorial<I, F> > super_type; + + void impl_read_point() + { + value_type tmp; + + for (unsigned i = 0; i < ntg_nb_comp(value_type); ++i) + tmp[i] = (ntg_storage_type(comp_type))this->reader.read_value(); + + this->output[this->reader.point()] = tmp; + } + + read_vectorial(F& reader) : + super_type(reader) {} + + }; + + template <typename I, typename F> + struct write_vectorial : + public canvas::write_from_oln<I, F, write_vectorial<I, F> > + { + typedef oln_type_of(I, value) value_type; + typedef canvas::write_from_oln<I, F, write_vectorial<I, F> > super_type; + + void impl_write_point() + { + value_type tmp = this->input[this->writer.point()]; + + for (unsigned i = 0; i < ntg_nb_comp(value_type); ++i) + this->writer.write_value(tmp[i]); + } + + write_vectorial(const abstract::image<I>& input, F& writer) : + super_type(input, writer) {} + + }; + + } + + +} + +#endif // ! OLENA_IO_VECTORIAL_HH Index: oln/io/scalar.hh --- oln/io/scalar.hh (revision 0) +++ oln/io/scalar.hh (revision 0) @@ -0,0 +1,78 @@ +// 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 filek 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_SCALAR_HH +# define OLENA_IO_SCALAR_HH + +# include <oln/core/abstract/image.hh> +# include <oln/canvas/io.hh> + +namespace oln { + + + namespace io { + + + template <typename I, typename F> + struct read_scalar : public canvas::read_to_oln<I, F, read_scalar<I, F> > + { + typedef oln_type_of(I, value) value_type; + typedef canvas::read_to_oln<I, F, read_scalar<I, F> > super_type; + + void impl_read_point() + { + this->output[this->reader.point()] = + (ntg_storage_type(value_type))this->reader.read_value(); + } + + read_scalar(F& reader) : + super_type(reader) {} + + }; + + template <typename I, typename F> + struct write_scalar : + public canvas::write_from_oln<I, F, write_scalar<I, F> > + { + typedef canvas::write_from_oln<I, F, write_scalar<I, F> > super_type; + + void impl_write_point() + { + this->writer.write_value(this->input[this->writer.point()]); + } + + write_scalar(const abstract::image<I>& input, F& writer) : + super_type(input, writer) {} + + }; + + } + + +} + +#endif // ! OLENA_IO_SCALAR_HH Index: oln/io/pnm_real.hh --- oln/io/pnm_real.hh (revision 0) +++ oln/io/pnm_real.hh (revision 0) @@ -0,0 +1,169 @@ +// 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_PNM_REAL_HH +# define OLN_IO_PNM_REAL_HH + +# include <iostream> + +# include <oln/canvas/io.hh> +# include <oln/io/pnm_header.hh> +# include <oln/core/2d/grid2d.hh> + + + +namespace oln { + + namespace io { + + namespace pnm { + + + template <typename T, typename G> + struct read_real {}; + + template <typename T, typename G> + struct write_real {}; + + template <typename T> + struct read_real<T, oln::grid2d> + : public canvas::read_from_file<T, grid2d, read_real<T, grid2d> > + { + typedef oln_grd_type_of(grid2d, size) size_type; + typedef oln_grd_type_of(grid2d, point) point_type; + + unsigned impl_npoints() + { + return hdr.cols * hdr.rows; + } + + size_type impl_size() + { + return size_type(hdr.rows, hdr.cols); + } + + T impl_read_value() + { + T v; + + istr.read((char*)&v, sizeof (T)); + return v; + } + + void impl_next_point() + { + precondition(p.row() < hdr.rows && p.col() < hdr.cols); + ++p.col(); + if (p.col() >= hdr.cols) + { + p.col() = 0; + ++p.row(); + } + } + + const point_type& impl_point() + { + return p; + } + + read_real(std::istream& istr, const header& hdr) : + istr(istr), + hdr(hdr) + { + p.col() = 0; + p.row() = 0; + } + + std::istream& istr; + const header& hdr; + point_type p; + + }; + + + template <typename T> + struct write_real<T, oln::grid2d> + : public canvas::write_to_file<T, grid2d, write_real<T, grid2d> > + { + typedef oln_grd_type_of(grid2d, size) size_type; + typedef oln_grd_type_of(grid2d, point) point_type; + + void set_size(const size_type& size) + { + nrows = size.nrows(); + ncols = size.ncols(); + } + + void impl_write_value(T& v) + { + ostr.write((char*)&v, sizeof (T)); + } + + void impl_next_point() + { + precondition(p.row() < nrows && p.col() < ncols); + ++p.col(); + if (p.col() >= ncols) + { + p.col() = 0; + ++p.row(); + } + } + + bool impl_write_header() + { + return pnm::write_header(ostr, "P5", ncols, nrows, + ntg_max_val(ntg_type(T)) - + ntg_min_val(ntg_type(T))); + } + + const point_type& impl_point() + { + return p; + } + + write_real(std::ostream& ostr) : + ostr(ostr) + { + p.col() = 0; + p.row() = 0; + } + + std::ostream& ostr; + point_type p; + int nrows, ncols; + + }; + + + } + + } + +} + +#endif // ! OLN_IO_PNM_REAL_HH Index: oln/io/write_image_pnm_decl.hh --- oln/io/write_image_pnm_decl.hh (revision 245) +++ oln/io/write_image_pnm_decl.hh (working copy) @@ -1,57 +0,0 @@ -// 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_PNM_DECL_HH -# define OLN_IO_WRITE_IMAGE_PNM_DECL_HH - -namespace oln { - - - namespace io { - - - namespace impl { - - template <typename I, typename S> - struct write_pnm_vect {}; - - template <typename I, typename S> - struct write_pnm_real {}; - - template <typename I, typename S> - struct write_pnm_bin {}; - - - } - - - } - - -} - -#endif // ! OLN_IO_WRITE_IMAGE_PNM_DECL_HH Index: oln/io/read_image_pnm.hh --- oln/io/read_image_pnm.hh (revision 245) +++ oln/io/read_image_pnm.hh (working copy) @@ -1,157 +0,0 @@ - // 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_PNM_HH -# define OLN_IO_READ_IMAGE_PNM_HH - -# include <iostream> -# include <string> - -# include <oln/io/read_image_pnm_decl.hh> -# include <oln/io/read_image_pnm_2d.hh> -# include <oln/core/abstract/image.hh> -# include <oln/io/utils.hh> - -namespace oln { - - - namespace io { - - - namespace impl { - - template <typename V, typename I> - void read_pnm_(const ntg::real_value<V>&, - abstract::image<I>& ima, - std::istream& istr, - const internal::pnm_header &hdr) - { - typedef oln_type_of(I, size) size_type; - - if (hdr.type == "P5") - { - read_pnm_real<I, size_type> tmp(istr, hdr); - - tmp.run(); - tmp.get_output(ima.exact()); - } - else - std::cerr << "io error: pnm type mismatch data type" << std::endl; - } - - template <typename V, typename I> - void read_pnm_(const ntg::enum_value<V>&, - abstract::image<I>& ima, - std::istream& istr, - const internal::pnm_header &hdr) - { - typedef oln_type_of(I, size) size_type; - - if (hdr.type == "P4") - { - read_pnm_bin<I, size_type> tmp(istr, hdr); - - tmp.run(); - tmp.get_output(ima.exact()); - } - else - std::cerr << "io error: pnm type mismatch data type" << std::endl; - } - - template <typename V, typename I> - void read_pnm_(const ntg::vect_value<V>&, - abstract::image<I>& ima, - std::istream& istr, - const internal::pnm_header &hdr) - { - typedef oln_type_of(I, size) size_type; - - if (hdr.type == "P6") - { - read_pnm_vect<I, size_type> tmp(istr, hdr); - - tmp.run(); - tmp.get_output(ima.exact()); - } - else - std::cerr << "io error: pnm type mismatch data type" << std::endl; - } - - template <typename V, typename I> - void read_pnm(const ntg::value<V>& v, - abstract::image<I>& ima, - std::istream& istr, - const internal::pnm_header &hdr) - { - read_pnm_(v.exact(), ima.exact(), istr, hdr); - } - - } - - - void check_hdr_ext(const internal::pnm_header& hdr, - const std::string& ext) - { - if ((ext == "pbm" && hdr.type != "P4") || - (ext == "pgm" && hdr.type != "P5") || - (ext == "ppm" && hdr.type != "P6")) - std::cerr << "io warning: extension mismatch pnm type." << std::cerr; - } - - - - template <typename I> - void read_pnm(abstract::image<I>& ima, - std::istream& istr, - const std::string& ext) - { - typedef oln_type_of(I, value) value_type; - typedef ntg_type(value_type) ntg_value_type; - internal::pnm_header hdr; - - if (!internal::read_pnm_header(istr, hdr)) - { - std::cerr << "io error: invalid pnm header." << std::cerr; - return; - } - - if (hdr.type == "P1" || hdr.type == "P2" || hdr.type == "P3") - std::cerr << "io error: ascii pnm format not supported." << std::endl; - - check_hdr_ext(hdr, ext); - - impl::read_pnm(ntg_value_type(), ima, istr, hdr); - - } - - - } - - -} - -#endif // ! OLN_IO_READ_IMAGE_PNM_HH Index: oln/io/write_image_pnm.hh --- oln/io/write_image_pnm.hh (revision 245) +++ oln/io/write_image_pnm.hh (working copy) @@ -1,173 +0,0 @@ -// 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_PNM_HH -# define OLN_IO_WRITE_IMAGE_PNM_HH - -# include <oln/io/write_image_pnm_decl.hh> -# include <oln/io/write_image_pnm_2d.hh> - -namespace oln { - - - namespace io { - - - namespace impl { - - - template <typename V, typename I> - void write_pnm_(const ntg::real_value<V>&, - const abstract::image<I>& ima, - std::ostream& istr, - const std::string& ext) - { - typedef oln_type_of(I, size) size_type; - - if (ext == "pgm") - { - write_pnm_real<I, size_type> tmp(ima, istr); - - tmp.run(); - } - else - std::cerr << "io error: extension mismatch data type" << std::endl; - } - - template <typename V, typename I> - void write_pnm_(const ntg::enum_value<V>&, - const abstract::image<I>& ima, - std::ostream& ostr, - const std::string& ext) - { - typedef oln_type_of(I, size) size_type; - - if (ext == "pbm") - { - write_pnm_bin<I, size_type> tmp(ima, ostr); - - tmp.run(); - } - else - std::cerr << "io error: extension mismatch data type" << std::endl; - } - - template <typename V, typename I> - void write_pnm_(const ntg::vect_value<V>&, - const abstract::image<I>& ima, - std::ostream& ostr, - const std::string& ext) - { - typedef oln_type_of(I, size) size_type; - precondition(ntg_nb_comp(V) == 3); - - if (ext == "ppm") - { - write_pnm_vect<I, size_type> tmp(ima, ostr); - - tmp.run(); - } - else - std::cerr << "io error: extension mismatch data type" << std::endl; - } - - template <typename V, typename I> - void write_pnm(const ntg::value<V>& v, - const abstract::image<I>& ima, - std::ostream& ostr, - const std::string& ext) - { - write_pnm_(v.exact(), ima.exact(), ostr, ext); - } - - } - - - template <typename V> - std::string get_pnm_type(const ntg::enum_value<V>&) - { - return "P4"; - } - - template <typename V> - std::string get_pnm_type(const ntg::real_value<V>&) - { - return "P5"; - } - - template <typename V> - std::string get_pnm_type(const ntg::vect_value<V>&) - { - return "P6"; - } - - template <typename V> - int get_max_val(const ntg::value<V>&) - { - return ntg_max_val(V) - ntg_min_val(V); - } - - template <typename V> - int get_max_val(const ntg::vect_value<V>&) - { - return ntg_max_val(ntg_comp_type(V)) - ntg_min_val(ntg_comp_type(V)); - } - - - template <typename I> - void write_pnm(const abstract::image<I>& ima, - std::ostream& ostr, - const std::string& ext) - { - typedef oln_type_of(I, value) value_type; - typedef ntg_type(value_type) ntg_value_type; - int max_val = get_max_val(ntg_value_type()); - - if (max_val <= 65535) - { - internal::write_pnm_header(ostr, get_pnm_type(ntg_value_type()), - ima.exact().size().ncols(), - ima.exact().size().nrows(), - max_val); - - impl::write_pnm(ntg_value_type(), ima.exact(), ostr, ext); - } - else - std::cerr << "io error: data type too large" << std::endl; - - - } - - - } - - -} - - - -# endif // ! OLN_IO_WRITE_IMAGE_PNM_HH Index: oln/io/pnm_bin.hh --- oln/io/pnm_bin.hh (revision 0) +++ oln/io/pnm_bin.hh (revision 0) @@ -0,0 +1,215 @@ +// 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_PNM_BIN_HH +# define OLN_IO_PNM_BIN_HH + +# include <iostream> + +# include <oln/canvas/io.hh> +# include <oln/io/pnm_header.hh> +# include <oln/core/2d/grid2d.hh> + + + +namespace oln { + + namespace io { + + namespace pnm { + + + template <typename G> + struct read_bin {}; + + template <typename G> + struct write_bin {}; + + template <> + struct read_bin<oln::grid2d> + : public canvas::read_from_file<bool, grid2d, read_bin<grid2d> > + { + + // FIXME: these macros should work +// typedef oln_grd_type_of(grid2d, size) size_type; +// typedef oln_grd_type_of(grid2d, point) point_type; + + typedef size2d size_type; + typedef point2d point_type; + + unsigned impl_npoints() + { + return hdr.cols * hdr.rows; + } + + size_type impl_size() + { + return size_type(hdr.rows, hdr.cols); + } + + void extra_work() + { + offset = -1; + } + + bool impl_read_value() + { + if (offset == -1) + { + this->istr.read(&v, 1); + offset = 7; + } + if ((int)(v & (1<<offset--)) == 0) + return false; + else + return true; + } + + void impl_next_point() + { + precondition(p.row() < hdr.rows && p.col() < hdr.cols); + ++p.col(); + if (p.col() >= hdr.cols) + { + p.col() = 0; + ++p.row(); + extra_work(); + } + } + + const point_type& impl_point() + { + return p; + } + + read_bin(std::istream& istr, const header& hdr) : + istr(istr), + hdr(hdr), + offset(-1) + { + p.col() = 0; + p.row() = 0; + + } + + std::istream& istr; + const header& hdr; + point_type p; + + char v; + int offset; + + }; + + + template <> + struct write_bin<oln::grid2d> + : public canvas::write_to_file<bool, grid2d, write_bin<grid2d> > + { + // FIXME: these macros should work +// typedef oln_grd_type_of(grid2d, size) size_type; +// typedef oln_grd_type_of(grid2d, point) point_type; + + typedef size2d size_type; + typedef point2d point_type; + + void set_size(const size_type& size) + { + nrows = size.nrows(); + ncols = size.ncols(); + } + + void extra_work() + { + if (offset != 7) + { + this->ostr.write(&v, 1); + offset = 7; + v = 0; + } + } + + void impl_write_value(bool& b) + { + if (offset == -1) + { + this->ostr.write(&v, 1); + offset = 7; + v = 0; + } + if (b == true) + v |= 1 << offset; + offset--; + } + + void impl_next_point() + { + precondition(p.row() < nrows && p.col() < ncols); + ++p.col(); + if (p.col() >= ncols) + { + p.col() = 0; + ++p.row(); + extra_work(); + } + } + + bool impl_write_header() + { + return pnm::write_header(ostr, "P4", ncols, nrows, 0); + } + + const point_type& impl_point() + { + return p; + } + + write_bin(std::ostream& ostr) : + ostr(ostr) + { + p.col() = 0; + p.row() = 0; + offset = 7; + } + + std::ostream& ostr; + point_type p; + int nrows, ncols; + + int offset; + char v; + + }; + + + } + + } + +} + +#endif // ! OLN_IO_PNM_BIN_HH Index: oln/io/read_image.hh --- oln/io/read_image.hh (revision 245) +++ oln/io/read_image.hh (working copy) @@ -34,11 +34,13 @@ # include <mlc/box.hh> # include <ntg/core/macros.hh> +# include <oln/core/abstract/image.hh> -# include <oln/io/read_image_pnm.hh> +# include <oln/io/pnm_read.hh> # include <oln/io/utils.hh> # include <oln/io/gz_stream.hh> + namespace oln { namespace io { @@ -71,7 +73,7 @@ { if (ext == "pgm" || ext == "pbm" || ext == "ppm" || ext == "pnm") - read_pnm(ima, istr, ext); + pnm::read(ima, istr, ext); else std::cout << "io error: format not supported." << std::endl; Index: oln/io/pnm_vect.hh --- oln/io/pnm_vect.hh (revision 0) +++ oln/io/pnm_vect.hh (revision 0) @@ -0,0 +1,169 @@ +// 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_PNM_VECT_HH +# define OLN_IO_PNM_VECT_HH + +# include <iostream> + +# include <oln/canvas/io.hh> +# include <oln/io/pnm_header.hh> +# include <oln/core/2d/grid2d.hh> + + + +namespace oln { + + namespace io { + + namespace pnm { + + + template <typename T, typename G> + struct read_vect {}; + + template <typename T, typename G> + struct write_vect {}; + + template <typename T> + struct read_vect<T, oln::grid2d> + : public canvas::read_from_file<T, grid2d, read_vect<T, grid2d> > + { + typedef oln_grd_type_of(grid2d, size) size_type; + typedef oln_grd_type_of(grid2d, point) point_type; + + unsigned impl_npoints() + { + return hdr.cols * hdr.rows; + } + + size_type impl_size() + { + return size_type(hdr.rows, hdr.cols); + } + + T impl_read_value() + { + T v; + + istr.read((char*)&v, sizeof (T)); + return v; + } + + void impl_next_point() + { + precondition(p.row() < hdr.rows && p.col() < hdr.cols); + ++p.col(); + if (p.col() >= hdr.cols) + { + p.col() = 0; + ++p.row(); + } + } + + const point_type& impl_point() + { + return p; + } + + read_vect(std::istream& istr, const header& hdr) : + istr(istr), + hdr(hdr) + { + p.col() = 0; + p.row() = 0; + } + + std::istream& istr; + const header& hdr; + point_type p; + + }; + + + template <typename T> + struct write_vect<T, oln::grid2d> + : public canvas::write_to_file<T, grid2d, write_vect<T, grid2d> > + { + typedef oln_grd_type_of(grid2d, size) size_type; + typedef oln_grd_type_of(grid2d, point) point_type; + + void set_size(const size_type& size) + { + nrows = size.nrows(); + ncols = size.ncols(); + } + + void impl_write_value(T& v) + { + ostr.write((char*)&v, sizeof (T)); + } + + void impl_next_point() + { + precondition(p.row() < nrows && p.col() < ncols); + ++p.col(); + if (p.col() >= ncols) + { + p.col() = 0; + ++p.row(); + } + } + + bool impl_write_header() + { + return pnm::write_header(ostr, "P6", ncols, nrows, + ntg_max_val(ntg_type(T)) - + ntg_min_val(ntg_type(T))); + } + + const point_type& impl_point() + { + return p; + } + + write_vect(std::ostream& ostr) : + ostr(ostr) + { + p.col() = 0; + p.row() = 0; + } + + std::ostream& ostr; + point_type p; + int nrows, ncols; + + }; + + + } + + } + +} + +#endif // ! OLN_IO_PNM_VECT_HH Index: oln/io/pnm_header.hh --- oln/io/pnm_header.hh (revision 0) +++ oln/io/pnm_header.hh (revision 0) @@ -0,0 +1,129 @@ +// 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_PNM_HEADER_HH +# define OLN_IO_PNM_HEADER_HH + +namespace oln { + + + namespace io { + + + namespace pnm { + + + struct header + { + int max_val; + int rows; + int cols; + std::string type; + }; + + bool read_header(std::istream& istr, + header& hdr) + { + std::getline(istr, hdr.type); + + hdr.max_val = 1; + + // skip comments + while (istr.peek() == '#') + { + std::string line; + std::getline(istr, line); + } + + // read size + istr >> hdr.cols; + // skip comments + while (istr.peek() == '#') + { + std::string line; + std::getline(istr, line); + } + + + istr >> hdr.rows; + // skip comments + while (istr.peek() == '#') + { + std::string line; + std::getline(istr, line); + } + + if (hdr.cols <= 0 || hdr.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'! + if (istr.get() != '\n') return false; + // extract or skip maxvalue + if (hdr.type != "P1" && hdr.type != "P4") + { + istr >> hdr.max_val; + if (hdr.max_val > 65535 || + istr.get() != '\n' || + hdr.max_val <= 0) + return false; + } + return true; + } + + bool write_header(std::ostream& ostr, + const std::string& type, + int ncols, + int nrows, + int max_val) + { + if (max_val > 65535) + return false; + + ostr << type << std::endl + << "# Olena 1.0" << std::endl + << ncols << " " << nrows << std::endl; + if (type != "P1" && type != "P4") + ostr << max_val << std::endl; + return true; + } + + + } + + + } + + +} + +#endif // ! OLN_IO_PNM_HEADER_HH Index: oln/io/write_image.hh --- oln/io/write_image.hh (revision 245) +++ oln/io/write_image.hh (working copy) @@ -31,8 +31,7 @@ # include <string> # include <oln/io/gz_stream.hh> -# include <oln/io/utils.hh> -# include <oln/io/write_image_pnm.hh> +# include <oln/io/pnm_write.hh> namespace oln { @@ -45,7 +44,7 @@ { if (ext == "pgm" || ext == "pbm" || ext == "ppm" || ext == "pnm") - write_pnm(ima, ostr, ext); + pnm::write(ima, ostr, ext); else std::cout << "io error: format not supported." << std::endl; Index: oln/io/read_image_pnm_decl.hh --- oln/io/read_image_pnm_decl.hh (revision 245) +++ oln/io/read_image_pnm_decl.hh (working copy) @@ -1,57 +0,0 @@ - // 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_PNM_DECL_HH -# define OLN_IO_READ_IMAGE_PNM_DECL_HH - -namespace oln { - - - namespace io { - - - namespace impl { - - - template <typename I, typename S> - struct read_pnm_vect {}; - - template <typename I, typename S> - struct read_pnm_real {}; - - template <typename I, typename S> - struct read_pnm_bin {}; - - } - - - } - - -} - -#endif // ! OLN_IO_READ_IMAGE_PNM_DECL_HH Index: oln/io/write_image_pnm_2d.hh --- oln/io/write_image_pnm_2d.hh (revision 245) +++ oln/io/write_image_pnm_2d.hh (working copy) @@ -1,153 +0,0 @@ -// 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_PNM_2D_HH -# define OLN_IO_WRITE_IMAGE_PNM_2D_HH - -# include <ntg/all.hh> - -# include <oln/canvas/io.hh> - -# include <oln/core/2d/image2d.hh> -# include <oln/io/write_image_pnm_decl.hh> - -namespace oln { - - - namespace io { - - - namespace impl { - - - template <typename I> - struct write_pnm_vect<I, oln::size2d> - : public canvas::write_2d<I, write_pnm_vect<I, oln::size2d> > - { - typedef canvas::write_2d<I, write_pnm_vect<I, oln::size2d> > super_type; - typedef oln_type_of(I, value) value_type; - typedef ntg_comp_type(value_type) comp_type; - typedef oln_io_type(ntg_nbits(ntg_comp_type(value_type))) v_type; - - void impl_write_point() - { - value_type c = this->input[this->p]; - for (unsigned i = 0; i < ntg_nb_comp(value_type); ++i) - { - v_type v; - v = c[i]; - this->ostr.write((char*)&v, sizeof (v_type)); - } - } - - write_pnm_vect(const abstract::image<I>& input, - std::ostream& ostr) : - super_type(input, ostr) - {} - - }; - - template <typename I> - struct write_pnm_real<I, oln::size2d> - : public canvas::write_2d<I, write_pnm_real<I, oln::size2d> > - { - typedef canvas::write_2d<I, write_pnm_real<I, oln::size2d> > super_type; - typedef oln_type_of(I, value) value_type; - typedef ntg_type(value_type) ntg_value_type; - typedef oln_io_type(ntg_nbits(ntg_value_type)) v_type; - - - void impl_write_point() - { - v_type v; - v = this->input[this->p]; - this->ostr.write((char*)&v, sizeof (v_type)); - } - - write_pnm_real(const abstract::image<I>& input, - std::ostream& ostr) : - super_type(input, ostr) - {} - - }; - - template <typename I> - struct write_pnm_bin<I, oln::size2d> - : public canvas::write_2d<I, write_pnm_bin<I, oln::size2d> > - { - typedef canvas::write_2d<I, write_pnm_bin<I, oln::size2d> > super_type; - typedef oln_type_of(I, value) value_type; - typedef ntg_type(value_type) ntg_value_type; - typedef oln_io_type(ntg_nbits(ntg_value_type)) v_type; - - int offset; - char v; - - void impl_extra_work() - { - if (offset != 7) - { - this->ostr.write(&v, 1); - offset = 7; - v = 0; - } - } - - void impl_write_point() - { - if (offset == -1) - { - this->ostr.write(&v, 1); - offset = 7; - v = 0; - } - if (this->input[this->p] == value_type(0)) - v |= 1 << offset; - offset--; - } - - write_pnm_bin(const abstract::image<I>& input, - std::ostream& ostr) : - super_type(input, ostr), - offset(7), - v(0) - { - } - - }; - - - - } - - - } - - -} - -#endif // ! OLN_IO_WRITE_IMAGE_PNM_2D_HH Index: oln/io/pnm_read.hh --- oln/io/pnm_read.hh (revision 0) +++ oln/io/pnm_read.hh (revision 0) @@ -0,0 +1,198 @@ +// 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_PNM_HH +# define OLN_IO_PNM_HH + +# include <iostream> +# include <string> + +# include <oln/core/abstract/image.hh> + +# include <oln/io/scalar.hh> +# include <oln/io/vectorial.hh> +# include <oln/io/pnm_header.hh> + +# include <oln/io/pnm_real.hh> +# include <oln/io/pnm_bin.hh> +# include <oln/io/pnm_vect.hh> + +namespace oln { + + + namespace io { + + + namespace pnm { + + + namespace impl { + + template <typename V, typename I> + void read(const ntg::real_value<V>&, + oln::abstract::image<I>& ima, + std::istream& istr, + const header &hdr) + { + typedef oln_type_of(I, grid) grid_type; + + if (hdr.type == "P5") + { + assert(ntg_max_val(V) - ntg_min_val(V) <= hdr.max_val); + + if (hdr.max_val < 256) + { + typedef read_real<char, grid_type> reader_type; + reader_type pnm_reader(istr, hdr); + read_scalar<I, reader_type> reader(pnm_reader); + + reader.run(); + reader.assign(ima); + } + else + { + typedef read_real<short, grid_type> reader_type; + reader_type pnm_reader(istr, hdr); + read_scalar<I, reader_type> reader(pnm_reader); + + reader.run(); + reader.assign(ima); + } + } + else + std::cerr << "io error: pnm type mismatch data type" << std::endl; + } + + template <typename V, typename I> + void read(const ntg::enum_value<V>&, + abstract::image<I>& ima, + std::istream& istr, + const header &hdr) + { + typedef oln_type_of(I, grid) grid_type; + + if (hdr.type == "P4") + { + assert(ntg_max_val(V) == 1 && ntg_min_val(V) == 0); + + typedef read_bin<grid_type> reader_type; + reader_type pnm_reader(istr, hdr); + read_scalar<I, reader_type> reader(pnm_reader); + + reader.run(); + reader.assign(ima); + } + else + std::cerr << "io error: pnm type mismatch data type" << std::endl; + } + + template <typename V, typename I> + void read(const ntg::vect_value<V>&, + abstract::image<I>& ima, + std::istream& istr, + const header &hdr) + { + typedef oln_type_of(I, grid) grid_type; + + assert(ntg_nb_comp(V) == 3); + if (hdr.type == "P6") + { + assert(ntg_max_val(ntg_comp_type(V)) - + ntg_min_val(ntg_comp_type(V)) <= hdr.max_val); + + if (hdr.max_val < 256) + { + typedef read_vect<char, grid_type> reader_type; + reader_type pnm_reader(istr, hdr); + read_vectorial<I, reader_type> reader(pnm_reader); + + reader.run(); + reader.assign(ima); + } + else + { + typedef read_vect<short, grid_type> reader_type; + reader_type pnm_reader(istr, hdr); + read_vectorial<I, reader_type> reader(pnm_reader); + + reader.run(); + reader.assign(ima); + } + } + else + std::cerr << "io error: pnm type mismatch data type" << std::endl; + } + + + } + + + void check_hdr_ext(const header& hdr, + const std::string& ext) + { + if ((ext == "pbm" && hdr.type != "P4") || + (ext == "pgm" && hdr.type != "P5") || + (ext == "ppm" && hdr.type != "P6")) + std::cerr << "io warning: extension mismatch pnm type." << std::cerr; + } + + + + template <typename I> + void read(abstract::image<I>& ima, + std::istream& istr, + const std::string& ext) + { + typedef oln_type_of(I, value) value_type; + typedef ntg_type(value_type) ntg_value_type; + header hdr; + + if (!read_header(istr, hdr)) + { + std::cerr << "io error: invalid pnm header." << std::cerr; + return; + } + + if (hdr.type == "P1" || hdr.type == "P2" || hdr.type == "P3") + std::cerr << "io error: ascii pnm format not supported." + << std::endl; + + check_hdr_ext(hdr, ext); + + impl::read(ntg_value_type(), ima.exact(), istr, hdr); + } + + + } + + + } + + +} + +#endif // ! OLN_IO_PNM_HH Index: oln/io/read_image_pnm_2d.hh --- oln/io/read_image_pnm_2d.hh (revision 245) +++ oln/io/read_image_pnm_2d.hh (working copy) @@ -1,166 +0,0 @@ -// 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_PNM_2D_HH -# define OLN_IO_READ_IMAGE_PNM_2D_HH - -# include <ntg/all.hh> - -# include <oln/canvas/io.hh> - -# include <oln/core/2d/image2d.hh> -# include <oln/io/read_image_pnm_decl.hh> - -namespace oln { - - - namespace io { - - - namespace impl { - - - template <typename I> - struct read_pnm_vect<I, oln::size2d> - : public canvas::read_2d<I, read_pnm_vect<I, oln::size2d> > - { - typedef canvas::read_2d<I, read_pnm_vect<I, oln::size2d> > super_type; - typedef oln_type_of(I, value) value_type; - typedef ntg_comp_type(value_type) comp_type; - typedef oln_io_type(ntg_nbits(ntg_comp_type(value_type))) v_type; - - void impl_read_point() - { - value_type c; - for (unsigned i = 0; i < ntg_nb_comp(value_type); ++i) - { - v_type v; - this->istr.read((char*)&v, sizeof (v_type)); - c[i] = (ntg_storage_type(ntg_comp_type(value_type)))v; - } - this->output[this->p] = c; - } - - void impl_preconditions() - { - precondition(ntg_max_val(comp_type) - ntg_min_val(comp_type) - <= this->hdr.max_val); - precondition(ntg_nb_comp(value_type) == 3); - } - - read_pnm_vect(std::istream& istr, - const internal::pnm_header& hdr) : - super_type(istr, hdr) - {} - - }; - - template <typename I> - struct read_pnm_real<I, oln::size2d> - : public canvas::read_2d<I, read_pnm_real<I, oln::size2d> > - { - typedef canvas::read_2d<I, read_pnm_real<I, oln::size2d> > super_type; - typedef oln_type_of(I, value) value_type; - typedef ntg_type(value_type) ntg_value_type; - typedef oln_io_type(ntg_nbits(ntg_value_type)) v_type; - - - void impl_read_point() - { - v_type v; - this->istr.read((char*)&v, sizeof (v_type)); - this->output[this->p] = (ntg_storage_type(value_type))v; - } - - void impl_preconditions() - { - precondition(ntg_max_val(value_type) - ntg_min_val(value_type) - <= this->hdr.max_val); - } - - read_pnm_real(std::istream& istr, - const internal::pnm_header& hdr) : - super_type(istr, hdr) - {} - - }; - - template <typename I> - struct read_pnm_bin<I, oln::size2d> - : public canvas::read_2d<I, read_pnm_bin<I, oln::size2d> > - { - typedef canvas::read_2d<I, read_pnm_bin<I, oln::size2d> > super_type; - typedef oln_type_of(I, value) value_type; - typedef ntg_type(value_type) ntg_value_type; - typedef oln_io_type(ntg_nbits(ntg_value_type)) v_type; - - char v; - int offset; - - void impl_extra_work() - { - offset = -1; - } - - void impl_read_point() - { - if (offset == -1) - { - this->istr.read(&v, 1); - offset = 7; - } - if ((int)(v & (1<<offset--)) == 0) - this->output[this->p] = 1; - else - this->output[this->p] = 0; - } - - void impl_preconditions() - { - precondition(ntg_max_val(value_type) - ntg_min_val(value_type) - <= this->hdr.max_val); - } - - read_pnm_bin(std::istream& istr, - const internal::pnm_header& hdr) : - super_type(istr, hdr), - offset(-1) - {} - - }; - - - - } - - - } - - -} - -#endif // ! OLN_IO_READ_IMAGE_PNM_2D_HH Index: oln/io/utils.hh --- oln/io/utils.hh (revision 245) +++ oln/io/utils.hh (working copy) @@ -60,104 +60,6 @@ } - struct pnm_header - { - int max_val; - int rows; - int cols; - std::string type; - }; - - bool read_pnm_header(std::istream& istr, - internal::pnm_header& hdr) - { - std::getline(istr, hdr.type); - - hdr.max_val = 1; - - // skip comments - while (istr.peek() == '#') - { - std::string line; - std::getline(istr, line); - } - - // read size - istr >> hdr.cols; - // skip comments - while (istr.peek() == '#') - { - std::string line; - std::getline(istr, line); - } - - - istr >> hdr.rows; - // skip comments - while (istr.peek() == '#') - { - std::string line; - std::getline(istr, line); - } - - if (hdr.cols <= 0 || hdr.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'! - if (istr.get() != '\n') return false; - // extract or skip maxvalue - if (hdr.type != "P1" && hdr.type != "P4") - { - istr >> hdr.max_val; - if (hdr.max_val > 65535 || - istr.get() != '\n' || - hdr.max_val <= 0) - return false; - } - return true; - } - - bool write_pnm_header(std::ostream& ostr, - const std::string& type, - int ncols, - int nrows, - int max_val) - { - if (max_val > 65535) - return false; - - ostr << type << std::endl - << "# Olena 1.0" << std::endl - << ncols << " " << nrows << std::endl; - if (type != "P1" && type != "P4") - ostr << max_val << std::endl; - return true; - } - - template <bool b> - struct pnm_io_helper_bool - { - typedef unsigned char type; - }; - - template <> - struct pnm_io_helper_bool<false> - { - typedef unsigned short type; - }; - - template <unsigned N> - struct pnm_io_helper - { - typedef typename pnm_io_helper_bool<N <= 8>::type type; - }; - } // end of namespace internal } // end of namespace io
participants (1)
-
Damien Thivolle