
Index: ChangeLog from Damien Thivolle <damien@lrde.epita.fr> * tests/io/loadsave.hh: Adapt to integre. * tests/core/tests/readonly_image: Likewise. * tests/core/tests/image1d: Likewise. * tests/core/tests/image2d: Likewise. * tests/core/tests/readwrite_image: Likewise. * tests/core/tests/image3d: Likewise. * tests/core/tests/image_identity: Likewise. * oln/fancy/print.hh: Likewise. * oln/basics.hh: Likewise. * oln/basics1d.hh: Likewise. * oln/io/write_image_2d_pnm.hh: Likewise. * oln/io/read_image_2d_pnm.hh: Likewise. * oln/io/utils.hh: Likewise. * oln/core/value_box.hh: Likewise. oln/basics.hh | 7 --- oln/basics1d.hh | 3 - oln/core/value_box.hh | 48 +++++++++++++++++------ oln/fancy/print.hh | 2 oln/io/read_image_2d_pnm.hh | 66 +++++++++++++++++++------------- oln/io/utils.hh | 20 +++++++++ oln/io/write_image_2d_pnm.hh | 80 +++++++++++++++++++++------------------ tests/core/tests/image1d | 2 tests/core/tests/image2d | 2 tests/core/tests/image3d | 2 tests/core/tests/image_identity | 2 tests/core/tests/readonly_image | 8 +-- tests/core/tests/readwrite_image | 8 +-- tests/io/loadsave.hh | 15 +++++-- 14 files changed, 166 insertions(+), 99 deletions(-) Index: tests/io/loadsave.hh --- tests/io/loadsave.hh (revision 59) +++ tests/io/loadsave.hh (working copy) @@ -8,6 +8,7 @@ #include "check.hh" #include "data.hh" + template<typename E> bool compare(const oln::abstract::image<E>& a, const oln::abstract::image<E>& b) @@ -19,9 +20,11 @@ a.size().ncols() != b.size().ncols()) return false; - for (it.start(); !it.is_valid(); it.next()) - if (a[it] != b[it]) - return false; + for_all(it) + { + if (a[it] != b[it]) + return false; + } return true; } @@ -33,12 +36,16 @@ bool fail = false; T im1, im2; + im1 = oln::io::read(name); + + oln::io::write(im1, savename); + im2 = oln::io::read(savename); - if (compare(im1, im2)) + if (compare(im1, im2) == true) std::cout << "OK" << std::endl; else { Index: tests/core/tests/readonly_image --- tests/core/tests/readonly_image (revision 59) +++ tests/core/tests/readonly_image (working copy) @@ -1,6 +1,6 @@ #include <iostream> -#include <ntg/real/int_u8.hh> +#include <ntg/all.hh> #include <oln/basics2d.hh> #include "check.hh" @@ -14,7 +14,7 @@ struct category_type< dummy_image > { typedef cat::image ret; }; template <> - struct props <cat::image, dummy_image> : + struct props <cat::image, dummy_image> : public default_props < cat::image > { typedef size2d size_type; @@ -49,8 +49,8 @@ } // end of namespace test - - + + bool check() { oln::dummy_image ima(3, 3); Index: tests/core/tests/image1d --- tests/core/tests/image1d (revision 59) +++ tests/core/tests/image1d (working copy) @@ -1,7 +1,7 @@ #include <iostream> -#include <ntg/real/int_u8.hh> +#include <ntg/all.hh> #include <oln/basics1d.hh> #include <oln/fancy/iota.hh> Index: tests/core/tests/image2d --- tests/core/tests/image2d (revision 59) +++ tests/core/tests/image2d (working copy) @@ -1,7 +1,7 @@ #include <iostream> -#include <ntg/real/int_u8.hh> +#include <ntg/all.hh> #include <oln/basics2d.hh> #include <oln/fancy/iota.hh> Index: tests/core/tests/readwrite_image --- tests/core/tests/readwrite_image (revision 59) +++ tests/core/tests/readwrite_image (working copy) @@ -1,6 +1,6 @@ #include <iostream> -#include <ntg/real/int_u8.hh> +#include <ntg/all.hh> #include <oln/basics2d.hh> #include "check.hh" @@ -19,7 +19,7 @@ }; template <> - struct set_props <category::image, dummy_image> : + struct set_props <category::image, dummy_image> : public props_of < category::image > { typedef is_a<abstract::readwrite_image> image_constness; @@ -53,8 +53,8 @@ } // end of namespace test - - + + bool check() { oln::dummy_image ima(3, 3); Index: tests/core/tests/image3d --- tests/core/tests/image3d (revision 59) +++ tests/core/tests/image3d (working copy) @@ -1,7 +1,7 @@ #include <iostream> -#include <ntg/real/int_u8.hh> +#include <ntg/all.hh> #include <oln/basics3d.hh> #include <oln/fancy/iota.hh> Index: tests/core/tests/image_identity --- tests/core/tests/image_identity (revision 59) +++ tests/core/tests/image_identity (working copy) @@ -1,6 +1,6 @@ #include <oln/core/abstract/image_identity.hh> #include <oln/core/2d/image2d.hh> -#include <ntg/real/int_u8.hh> +#include <ntg/all.hh> template <typename I> Index: oln/fancy/print.hh --- oln/fancy/print.hh (revision 59) +++ oln/fancy/print.hh (working copy) @@ -33,7 +33,7 @@ # include <string> -# include <ntg/real/int_u8.hh> // FIXME: no coupling like that! +# include <ntg/real/int_u.hh> // FIXME: no coupling like that! # include <oln/core/abstract/image_dimension.hh> # include <oln/core/1d/point1d.hh> Index: oln/basics.hh --- oln/basics.hh (revision 59) +++ oln/basics.hh (working copy) @@ -25,8 +25,8 @@ // reasons why the executable file might be covered by the GNU General // Public License. - + #ifndef OLENA_BASICS_HH # define OLENA_BASICS_HH @@ -48,9 +48,6 @@ // ntg:: -# include <ntg/real/int_u8.hh> - - // oln:: # include <oln/core/coord.hh> @@ -61,7 +58,7 @@ # include <oln/core/abstract/point.hh> # include <oln/core/abstract/images.hh> -// # include <oln/core/abstract/piter.hh> +# include <oln/core/abstract/piter.hh> // # include <oln/core/abstract/niter.hh> Index: oln/basics1d.hh --- oln/basics1d.hh (revision 59) +++ oln/basics1d.hh (working copy) @@ -28,9 +28,6 @@ #ifndef OLENA_BASICS1D_HH # define OLENA_BASICS1D_HH - -# include <oln/basics.hh> - # include <oln/core/1d/size1d.hh> # include <oln/core/1d/point1d.hh> # include <oln/core/1d/image1d.hh> Index: oln/io/write_image_2d_pnm.hh --- oln/io/write_image_2d_pnm.hh (revision 59) +++ oln/io/write_image_2d_pnm.hh (working copy) @@ -34,11 +34,7 @@ # 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 <ntg/all.hh> # include <oln/core/2d/image2d.hh> # include <oln/core/abstract/op.hh> @@ -56,15 +52,20 @@ : public oln::abstract::void_op<write_image_2d_raw<I> > { typedef oln_type_of(I, value) value_type; - typedef ntg_io_type(value_type) io_type; const I& to_write_; std::ostream& ostr_; + int bin_offset; + char bin_v; + write_image_2d_raw(const I& to_write, std::ostream& ostr): to_write_(to_write), ostr_(ostr) - {} + { + bin_offset = 7; + bin_v = 0; + } void impl_run() { @@ -74,51 +75,55 @@ 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]; - b = write_value_type(c); - } + // FIXME: SHOULD NOT BE .value() !!! + b = write_value_type(to_write_[p].value()); while (b == false) b = write_value_type(c); + bin_offset = 7; + bin_v = 0; } - bool write_value_type(const ntg::integer<value_type> &c) + //FIXME: Should work with builtin types. + + template <typename E> + bool write_value_type(const ntg::real_value<E> &c) { - io_type v = c.exact(); - ostr_.write((char *)&v, sizeof(io_type)); + typedef oln_io_type(ntg_nbits(E)) v_type; + + v_type v; + v = ntg::cast::bound<E, v_type>(c.exact()); + ostr_.write((char*)&v, sizeof (v_type)); return true; } - bool write_value_type(const ntg::color<value_type> &c) + template <typename E> + bool write_value_type(const ntg::vect_value<E> &c) { - assert ((ntg_depth(value_type) == 3)); - io_type v; - for (unsigned i = 0; i < 3; i++) { + typedef oln_io_type(ntg_nbits(ntg_comp_type(value_type))) v_type; + + v_type v; v = c[i]; - ostr_.write((char *)(&v), sizeof(io_type)); + ostr_.write((char*)&v, sizeof (v_type)); } return true; } - bool write_value_type(const ntg::enum_value<value_type> &c) + bool write_value_type(const ntg::bin &c) { - assert ((ntg_max_val(value_type) == 1)); - static int offset = 7; - static unsigned char v = 0; bool ret = false; - if (offset == -1) + if (bin_offset == -1) { - ostr_.write((char *)&v, 1); - offset = 7; - v = 0; + ostr_.write(&bin_v, 1); + bin_offset = 7; + bin_v = 0; ret = true; } - if (c == 1) - v |= 1 << offset; - offset--; + if (c == value_type(1)) + bin_v |= 1 << bin_offset; + bin_offset--; return ret; } }; @@ -126,7 +131,7 @@ template <typename I, typename T> void write(const abstract::image2d<I>& to_write, - const ntg::integer<T>&, + const ntg::real_value<T>&, std::ostream& ostr, const std::string& ext) { @@ -134,6 +139,7 @@ point2d p; size2d s = to_write.size(); + precondition(ntg_nbits(T) <= 16); if (ext == "pgm") if (internal::write_pnm_header(ostr, "P5", s.ncols(), s.nrows(), @@ -151,7 +157,7 @@ template <typename I, typename T> void write(const abstract::image2d<I>& to_write, - const ntg::color<T>&, + const ntg::color_value<T>&, std::ostream& ostr, const std::string& ext) { @@ -159,10 +165,12 @@ point2d p; size2d s = to_write.size(); + precondition(ntg_nbits(ntg_comp_type(T)) <= 16); + precondition(ntg_nb_comp(T) == 3); if (ext == "ppm") if (internal::write_pnm_header(ostr, "P6", s.ncols(), s.nrows(), - ntg_max_val(T))) + ntg_max_val(ntg_comp_type(T)))) { write_image_2d_raw<I> tmp(to_write.exact(), ostr); tmp.run(); @@ -174,9 +182,9 @@ << " file extension (`" << ext << "')" << std::endl; } - template <typename I, typename T> + template <typename I> void write(const abstract::image2d<I>& to_write, - const ntg::enum_value<T>&, + const ntg::bin&, std::ostream& ostr, const std::string& ext) { @@ -186,7 +194,7 @@ if (ext == "pbm") if (internal::write_pnm_header(ostr, "P4", s.ncols(), s.nrows(), - ntg_max_val(T))) + 1)) { write_image_2d_raw<I> tmp(to_write.exact(), ostr); tmp.run(); Index: oln/io/read_image_2d_pnm.hh --- oln/io/read_image_2d_pnm.hh (revision 59) +++ oln/io/read_image_2d_pnm.hh (working copy) @@ -32,12 +32,9 @@ # include <string> # include <mlc/box.hh> +# include <mlc/math.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 <ntg/all.hh> # include <oln/core/2d/image2d.hh> # include <oln/core/abstract/op.hh> @@ -83,7 +80,6 @@ typedef oln::abstract::op<I, read_image_2d_raw<I> > super_type; typedef oln_type_of(I, value) value_type; - typedef ntg_io_type(value_type) io_type; mlc::box<I> image_; std::istream& istr_; @@ -105,18 +101,33 @@ return *this; } + template <typename E> + void precond(ntg::real_value<E>& c) + { + precondition(ntg_max_val(value_type) <= info_.max_val); + precondition(info_.type == "P5"); + } + + template <typename E> + void precond(ntg::vect_value<E>& c) + { + precondition(ntg_max_val(ntg_comp_type(value_type)) <= info_.max_val); + precondition(info_.type == "P6"); + precondition(ntg_nb_comp(value_type) == 3); + } + + void precond(ntg::bin& c) + { + precondition(info_.type == "P4"); + } + void impl_run() { value_type c; point2d p; oln::image2d<value_type> tmp(info_.rows, info_.cols); - if (info_.max_val > ntg_max_val(value_type)) - { - std::cerr << "Can't load image, data type is not large enough" - << std::endl; - return; - } + precond(c); for (p.row() = 0; p.row() < info_.rows && !istr_.eof(); ++p.row()) for (p.col() = 0; p.col() < info_.cols && !istr_.eof(); ++p.col()) @@ -127,32 +138,37 @@ *image_ = tmp; } - void read_value_type(ntg::integer<value_type> &c) + //FIXME: Should work with builtin types. + + template <typename E> + void read_value_type(ntg::real_value<E> &c) { - io_type v; - istr_.read(&v, sizeof (io_type)); - c = v; + typedef oln_io_type(ntg_nbits(E)) v_type; + v_type v; + istr_.read((char*)&v, sizeof (v_type)); + c = ntg::cast::force<E, v_type>(v); } - void read_value_type(ntg::color<value_type> &c) + template <typename E> + void read_value_type(ntg::vect_value<E> &c) { - io_type v; - - for (unsigned i = 0; i < ntg_depth(value_type); i++) + for (unsigned i = 0; i < ntg_nb_comp(E); i++) { - istr_.read(&v, sizeof (v)); - c[i] = v; + typedef oln_io_type(ntg_nbits(ntg_comp_type(E))) v_type; + v_type v; + istr_.read((char*)&v, sizeof (v_type)); + c[i] = ntg::cast::force<ntg_comp_type(E), v_type>(v); } } - void read_value_type(ntg::enum_value<value_type> &c) + void read_value_type(ntg::bin &c) { - static io_type v; + static char v; static int offset = -1; if (offset == -1) { - istr_.read((char *)(&v), sizeof (v)); + istr_.read(&v, 1); offset = 7; } if ((int)(v & (1<<offset--)) == 0) Index: oln/io/utils.hh --- oln/io/utils.hh (revision 59) +++ oln/io/utils.hh (working copy) @@ -40,6 +40,7 @@ namespace internal { + /*! ** \brief Utils for io (get extension of a file). */ @@ -148,11 +149,30 @@ return true; } + template <bool b> + struct pnm_io_helper + { + enum { size = 1}; + typedef unsigned char type; + }; + template <> + struct pnm_io_helper<false> + { + enum { size = 2}; + typedef unsigned short type; + }; + + + } // end of namespace internal } // end of namespace io } // end of namespace oln +# define oln_io_type(N) typename oln::io::internal::pnm_io_helper<N <= 8>::type +# define oln_io_type_(N) oln::io::internal::pnm_io_helper<N <= 8>::type + + #endif // ! OLENA_IO_UTILS_HH Index: oln/core/value_box.hh --- oln/core/value_box.hh (revision 59) +++ oln/core/value_box.hh (working copy) @@ -81,24 +81,35 @@ /// op== - bool operator==(const value_box<I>& value) const + bool operator==(const value_box<const I>& rhs) const { - return this->value() == value.value(); + return this->value() == rhs.value(); } template <typename V> - bool operator==(const V& value) const + bool operator==(const V& rhs) const { - return this->value() == value; + return this->value() == rhs; } template <typename V> - bool operator!=(const V& value) const + bool operator!=(const V& rhs) const { - return ! this->operator==(value); + return ! this->operator==(rhs); } + template <typename V> + bool operator<(const V& rhs) const + { + return this->value() < rhs; + } + template <typename II> + bool operator<(const value_box<II>& rhs) const + { + return this->value() < value.value(); + } + /*! \brief op= ** FIXME:... ** \return (*this) @@ -123,7 +134,7 @@ template <typename II> value_box& operator=(const value_box<II>& rhs) { - ima_->set(p_, rhs); // automatic conversion from rhs to value_type + ima_->set(p_, rhs.value()); // automatic conversion from rhs to value_type return *this; } @@ -240,24 +251,35 @@ /// op== - bool operator==(const value_box<const I>& value) const + bool operator==(const value_box<const I>& rhs) const { - return this->value() == value.value(); + return this->value() == rhs.value(); } template <typename V> - bool operator==(const V& value) const + bool operator==(const V& rhs) const { - return this->value() == value; + return this->value() == rhs; } template <typename V> - bool operator!=(const V& value) const + bool operator!=(const V& rhs) const { - return ! this->operator==(value); + return ! this->operator==(rhs); } + template <typename V> + bool operator<(const V& rhs) const + { + return this->value() < rhs; + } + template <typename II> + bool operator<(const value_box<II>& rhs) const + { + return this->value() < rhs.value(); + } + /*! \brief Assignment (op=) is declared but undefined. */