
* headers.mk: add new headers to distribution. * mln/io/raw/all.hh: include new headers. * mln/io/raw/load.hh, * mln/io/raw/save.hh: new io routines. Dump/load Olena images into a file. * tests/io/Makefile.am, * tests/io/raw/Makefile.am, * tests/io/raw/pbm.cc, * tests/io/raw/raw.cc: add new tests. * tests/unit_test/Makefile.am, * tests/unit_test/mln_io_raw_all.cc, * tests/unit_test/mln_io_raw_load.cc, * tests/unit_test/mln_io_raw_save.cc: add new unit tests. --- milena/ChangeLog | 22 +++++ milena/headers.mk | 3 + milena/mln/io/raw/all.hh | 51 ++++++++++ milena/mln/io/raw/load.hh | 143 +++++++++++++++++++++++++++++ milena/mln/io/raw/save.hh | 127 +++++++++++++++++++++++++ milena/tests/io/Makefile.am | 3 +- milena/tests/io/{ => raw}/Makefile.am | 14 +-- milena/tests/io/raw/pbm.cc | 62 +++++++++++++ milena/tests/io/raw/raw.cc | 94 +++++++++++++++++++ milena/tests/unit_test/Makefile.am | 6 + milena/tests/unit_test/mln_io_raw_all.cc | 11 ++ milena/tests/unit_test/mln_io_raw_load.cc | 11 ++ milena/tests/unit_test/mln_io_raw_save.cc | 11 ++ 13 files changed, 549 insertions(+), 9 deletions(-) create mode 100644 milena/mln/io/raw/all.hh create mode 100644 milena/mln/io/raw/load.hh create mode 100644 milena/mln/io/raw/save.hh copy milena/tests/io/{ => raw}/Makefile.am (54%) create mode 100644 milena/tests/io/raw/pbm.cc create mode 100644 milena/tests/io/raw/raw.cc create mode 100644 milena/tests/unit_test/mln_io_raw_all.cc create mode 100644 milena/tests/unit_test/mln_io_raw_load.cc create mode 100644 milena/tests/unit_test/mln_io_raw_save.cc diff --git a/milena/ChangeLog b/milena/ChangeLog index baa1882..41dcd8d 100644 --- a/milena/ChangeLog +++ b/milena/ChangeLog @@ -1,5 +1,27 @@ 2009-01-23 Guillaume Lazzara <z@lrde.epita.fr> + Add io::raw::save and io::raw::load routines. + + * headers.mk: add new headers to distribution. + + * mln/io/raw/all.hh: include new headers. + + * mln/io/raw/load.hh, + * mln/io/raw/save.hh: new io routines. Dump/load Olena images into a + file. + + * tests/io/Makefile.am, + * tests/io/raw/Makefile.am, + * tests/io/raw/pbm.cc, + * tests/io/raw/raw.cc: add new tests. + + * tests/unit_test/Makefile.am, + * tests/unit_test/mln_io_raw_all.cc, + * tests/unit_test/mln_io_raw_load.cc, + * tests/unit_test/mln_io_raw_save.cc: add new unit tests. + +2009-01-23 Guillaume Lazzara <z@lrde.epita.fr> + Use extract::* routines in tutorial examples. * milena/doc/tutorial/figures/tuto4_genericity_and_algorithms-10.ppm diff --git a/milena/headers.mk b/milena/headers.mk index 65d98e5..74a3002 100644 --- a/milena/headers.mk +++ b/milena/headers.mk @@ -985,6 +985,9 @@ mln/io/ppm/save.hh \ mln/io/pfm/load.hh \ mln/io/pfm/all.hh \ mln/io/pfm/save.hh \ +mln/io/raw/load.hh \ +mln/io/raw/all.hh \ +mln/io/raw/save.hh \ mln/io/all.hh \ mln/io/pbm/load.hh \ mln/io/pbm/all.hh \ diff --git a/milena/mln/io/raw/all.hh b/milena/mln/io/raw/all.hh new file mode 100644 index 0000000..c10fa1a --- /dev/null +++ b/milena/mln/io/raw/all.hh @@ -0,0 +1,51 @@ +// Copyright (C) 2007 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, 51 Franklin Street, Fifth Floor, +// 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 MLN_IO_PGM_ALL_HH +# define MLN_IO_PGM_ALL_HH + +/*! \file mln/io/pgm/all.hh + * + * \brief File that includes all pgm io materials. + */ + + +namespace mln +{ + + namespace io + { + /// Namespace of pgm input/output handling. + namespace pgm {} + } + +} + +# include <mln/io/pgm/load.hh> +# include <mln/io/pgm/save.hh> + +#endif // ! MLN_IO_PGM_ALL_HH diff --git a/milena/mln/io/raw/load.hh b/milena/mln/io/raw/load.hh new file mode 100644 index 0000000..d6e9077 --- /dev/null +++ b/milena/mln/io/raw/load.hh @@ -0,0 +1,143 @@ +// Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE) +// +// 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, 51 Franklin Street, Fifth Floor, +// 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 MLN_IO_RAW_LOAD_HH +# define MLN_IO_RAW_LOAD_HH + +/// \file mln/io/raw/load.hh +/// +/// Load a Milena image dumped into a file. + +# include <iostream> +# include <fstream> + +# include <mln/core/concept/image.hh> +# include <mln/core/routine/initialize.hh> +# include <mln/core/box_runstart_piter.hh> +# include <mln/core/pixel.hh> +# include <mln/data/memcpy_.hh> + +namespace mln +{ + + namespace io + { + + namespace raw + { + + /// Load a Milena image by dumped into a file. + /// + /// \param[in,out] ima_ The image to load. + /// \param[in] filename the destination. + template <typename I> + void load(Image<I>& ima_, const std::string& filename); + + +# ifndef MLN_INCLUDE_ONLY + + namespace internal + { + + template <typename P> + inline + void read_point(std::ifstream& file, P& p) + { + char tmp[sizeof (P)]; + file.read(tmp, sizeof (P)); + p = *(P*)(void*)(&tmp); + } + + + template <typename I> + inline + void load_header(Image<I>& ima, std::ifstream& file) + { + unsigned dim; + file >> dim; + + typedef mln_site(I) P; + mln_assertion(P::dim == dim); + + P pmin, pmax; + read_point<P>(file, pmin); + read_point<P>(file, pmax); + + mln_concrete(I) result(box<P>(pmin, pmax)); + initialize(ima, result); + } + + + template <typename I> + inline + void load_data(Image<I>& ima_, std::ifstream& file) + { + I& ima = exact(ima_); + + unsigned data_size = sizeof (mln_value(I)) + sizeof (mln_value(I)) % 2; + + mln_box_runstart_piter(I) p(ima.domain()); + for_all(p) + { + pixel<I> src(ima, p); + file.read((char*) (&src.val()), p.run_length() * data_size); + } + + } + + } // end of namespace mln::io::raw::internal + + + + template <typename I> + void load(Image<I>& ima, const std::string& filename) + { + trace::entering("mln::io::raw::load"); + + std::ifstream file(filename.c_str()); + if (! file) + { + std::cerr << "error: cannot open file '" << filename << "'!"; + abort(); + } + + internal::load_header(ima, file); + internal::load_data(ima, file); + + trace::exiting("mln::io::raw::load"); + } + + +# endif // ! MLN_INCLUDE_ONLY + + } // end of namespace mln::io::raw + + } // end of namespace mln::io + +} // end of namespace mln + +#endif // ! MLN_IO_RAW_LOAD_HH diff --git a/milena/mln/io/raw/save.hh b/milena/mln/io/raw/save.hh new file mode 100644 index 0000000..83b8fab --- /dev/null +++ b/milena/mln/io/raw/save.hh @@ -0,0 +1,127 @@ +// Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE) +// +// 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, 51 Franklin Street, Fifth Floor, +// 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 MLN_IO_RAW_SAVE_HH +# define MLN_IO_RAW_SAVE_HH + +/// \file mln/io/raw/save.hh +/// +/// Save a Milena image by dumping its data to a file. + +# include <iostream> +# include <fstream> + +# include <mln/core/concept/image.hh> +# include <mln/core/box_runstart_piter.hh> +# include <mln/core/pixel.hh> +# include <mln/data/memcpy_.hh> + +namespace mln +{ + + namespace io + { + + namespace raw + { + + /// Save a Milena image by dumping its data to a file. + /// + /// \param[in] ima_ The image to save. + /// \param[in] filename the destination. + template <typename I> + void save(const Image<I>& ima_, const std::string& filename); + + +# ifndef MLN_INCLUDE_ONLY + + namespace internal + { + + template <typename I> + inline + void save_header(const I& ima, + std::ofstream& file) + { + typedef mln_site(I) P; + file << P::dim; + + mln_site(I) p = ima.domain().pmin(); + file.write((char*) (&p), sizeof (P)); + + p = ima.domain().pmax(); + file.write((char*) (&p), sizeof (P)); + } + + + template <typename I> + inline + void save_data(I& ima, std::ofstream& file) + { + unsigned data_size = sizeof (mln_value(I)) + sizeof (mln_value(I)) % 2; + + mln_box_runstart_piter(I) p(ima.domain()); + for_all(p) + { + pixel<I> src(ima, p); + file.write((char*) (&src.val()), p.run_length() * data_size); + } + } + + } // end of namespace mln::io::raw::internal + + + template <typename I> + void save(const Image<I>& ima_, const std::string& filename) + { + trace::entering("mln::io::raw::save"); + + const I& ima = exact(ima_); + + std::ofstream file(filename.c_str()); + if (! file) + { + std::cerr << "error: cannot open file '" << filename << "'!"; + abort(); + } + + internal::save_header(ima, file); + internal::save_data(ima, file); + + trace::exiting("mln::io::raw::save"); + } + + +# endif // ! MLN_INCLUDE_ONLY + + } // end of namespace mln::io::raw + + } // end of namespace mln::io + +} // end of namespace mln + +#endif // ! MLN_IO_RAW_SAVE_HH diff --git a/milena/tests/io/Makefile.am b/milena/tests/io/Makefile.am index 36d7797..8895b09 100644 --- a/milena/tests/io/Makefile.am +++ b/milena/tests/io/Makefile.am @@ -9,4 +9,5 @@ SUBDIRS = \ pfm \ pgm \ pnm \ - ppm + ppm \ + raw diff --git a/milena/tests/io/Makefile.am b/milena/tests/io/raw/Makefile.am similarity index 54% copy from milena/tests/io/Makefile.am copy to milena/tests/io/raw/Makefile.am index 36d7797..2d3d1c0 100644 --- a/milena/tests/io/Makefile.am +++ b/milena/tests/io/raw/Makefile.am @@ -2,11 +2,9 @@ include $(top_srcdir)/milena/tests/tests.mk -SUBDIRS = \ - fits \ - off \ - pbm \ - pfm \ - pgm \ - pnm \ - ppm +check_PROGRAMS = \ + raw + +raw_SOURCES = raw.cc + +TESTS = $(check_PROGRAMS) diff --git a/milena/tests/io/raw/pbm.cc b/milena/tests/io/raw/pbm.cc new file mode 100644 index 0000000..3ccc040 --- /dev/null +++ b/milena/tests/io/raw/pbm.cc @@ -0,0 +1,62 @@ +// Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE) +// +// 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, 51 Franklin Street, Fifth Floor, +// 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. + +/// \file tests/io/raw/raw.cc +/// +/// Test on mln::io::raw::. + +#include <mln/core/image/image2d.hh> +#include <mln/io/pbm/load.hh> +#include <mln/io/raw/load.hh> +#include <mln/io/raw/save.hh> + +#include <mln/level/compare.hh> + +#include "tests/data.hh" + + +int main() +{ + using namespace mln; + + image2d<bool> pic = io::pbm::load(MLN_IMG_DIR "/picasso.pbm"); + io::raw::save(pic, "pic.raw"); +// image2d<bool> pic2 = io::raw::load("pic.raw"); + +// mln_assertion(pic == pic2); +} + + +// sample binary image to test with xv, imview, and display +// to bring into the fore the binary image bug with raw raw. + +// | | | | - +// | | - - - +// | - - - - +// - - - - - +// - - - - - +// - - - - - diff --git a/milena/tests/io/raw/raw.cc b/milena/tests/io/raw/raw.cc new file mode 100644 index 0000000..193b125 --- /dev/null +++ b/milena/tests/io/raw/raw.cc @@ -0,0 +1,94 @@ +// Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE) +// +// 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, 51 Franklin Street, Fifth Floor, +// 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. + +/// \file tests/io/raw/raw.cc +/// +/// Test on mln::io::raw::*. + +#include <mln/core/image/image2d.hh> +#include <mln/io/raw/load.hh> +#include <mln/io/raw/save.hh> + +#include <mln/level/compare.hh> + +#include <mln/value/int_u8.hh> +#include <mln/value/rgb8.hh> + +#include "tests/data.hh" + +#include <mln/debug/println.hh> + +int main() +{ + using namespace mln; + + /// Value: bool + { + bool data[4] = { 0, 1, + 1, 0 }; + image2d<bool> pic = make::image2d(data); + + io::raw::save(pic, "pic.raw"); + + image2d<bool> pic2; + io::raw::load(pic2, "pic.raw"); + + mln_assertion(pic.domain() == pic2.domain()); + mln_assertion(pic == pic2); + } + + /// Value: int_u8 + { + value::int_u8 data[4] = { 5, 1, + 1, 9 }; + image2d<value::int_u8> pic = make::image2d(data); + io::raw::save(pic, "pic.raw"); + + image2d<value::int_u8> pic2; + io::raw::load(pic2, "pic.raw"); + + mln_assertion(pic.domain() == pic2.domain()); + mln_assertion(pic == pic2); + } + + /// Value: rgb8 + { + using value::rgb8; + value::rgb8 data[4] = { rgb8(2,4,5), rgb8(1,23,255), + rgb8(64,41,150), rgb8(23,53,49) }; + image2d<value::rgb8> pic = make::image2d(data); + io::raw::save(pic, "pic.raw"); + + image2d<value::rgb8> pic2; + io::raw::load(pic2, "pic.raw"); + + mln_assertion(pic.domain() == pic2.domain()); + mln_assertion(pic == pic2); + } + +} + diff --git a/milena/tests/unit_test/Makefile.am b/milena/tests/unit_test/Makefile.am index 315f1b3..0475cc4 100644 --- a/milena/tests/unit_test/Makefile.am +++ b/milena/tests/unit_test/Makefile.am @@ -951,6 +951,9 @@ mln_io_ppm_save \ mln_io_pfm_load \ mln_io_pfm_all \ mln_io_pfm_save \ +mln_io_raw_load \ +mln_io_raw_all \ +mln_io_raw_save \ mln_io_all \ mln_io_pbm_load \ mln_io_pbm_all \ @@ -1974,6 +1977,9 @@ mln_io_ppm_save_SOURCES = mln_io_ppm_save.cc mln_io_pfm_load_SOURCES = mln_io_pfm_load.cc mln_io_pfm_all_SOURCES = mln_io_pfm_all.cc mln_io_pfm_save_SOURCES = mln_io_pfm_save.cc +mln_io_raw_load_SOURCES = mln_io_raw_load.cc +mln_io_raw_all_SOURCES = mln_io_raw_all.cc +mln_io_raw_save_SOURCES = mln_io_raw_save.cc mln_io_all_SOURCES = mln_io_all.cc mln_io_pbm_load_SOURCES = mln_io_pbm_load.cc mln_io_pbm_all_SOURCES = mln_io_pbm_all.cc diff --git a/milena/tests/unit_test/mln_io_raw_all.cc b/milena/tests/unit_test/mln_io_raw_all.cc new file mode 100644 index 0000000..40875ba --- /dev/null +++ b/milena/tests/unit_test/mln_io_raw_all.cc @@ -0,0 +1,11 @@ +// Unit test for mln/io/raw/all.hh. +// Generated by ./build_unit_test.sh, do not modify. + +// Include the file twice, so we detect missing inclusion guards. +#include <mln/io/raw/all.hh> +#include <mln/io/raw/all.hh> + +int main() +{ + // Nothing. +} diff --git a/milena/tests/unit_test/mln_io_raw_load.cc b/milena/tests/unit_test/mln_io_raw_load.cc new file mode 100644 index 0000000..11f9ba6 --- /dev/null +++ b/milena/tests/unit_test/mln_io_raw_load.cc @@ -0,0 +1,11 @@ +// Unit test for mln/io/raw/load.hh. +// Generated by ./build_unit_test.sh, do not modify. + +// Include the file twice, so we detect missing inclusion guards. +#include <mln/io/raw/load.hh> +#include <mln/io/raw/load.hh> + +int main() +{ + // Nothing. +} diff --git a/milena/tests/unit_test/mln_io_raw_save.cc b/milena/tests/unit_test/mln_io_raw_save.cc new file mode 100644 index 0000000..3ead995 --- /dev/null +++ b/milena/tests/unit_test/mln_io_raw_save.cc @@ -0,0 +1,11 @@ +// Unit test for mln/io/raw/save.hh. +// Generated by ./build_unit_test.sh, do not modify. + +// Include the file twice, so we detect missing inclusion guards. +#include <mln/io/raw/save.hh> +#include <mln/io/raw/save.hh> + +int main() +{ + // Nothing. +} -- 1.5.6.5