3508: Make io::dump::* more robust and improve dump format.

* mln/io/dump/load.hh, * mln/io/dump/save.hh: - Add new information in the header. - Add more assertion on value type, dimension and file type. Important note: Old dump files are not compatible with this new format. * mln/trait/value_.hh: add a new macro mln_trait_value_name. * mln/value/builtin/floatings.hh * mln/value/builtin/integers.hh * mln/value/builtin/symbolics.hh * mln/value/int_s.hh * mln/value/int_u.hh * mln/value/label.hh * mln/value/rgb.hh: add name() member to value_<> specialization. It returns the type name as a const char*. * tests/io/dump/dump.cc: add new tests with builtin types. --- milena/ChangeLog | 25 ++++++++++++ milena/mln/io/dump/load.hh | 70 +++++++++++++++++++++++++++++--- milena/mln/io/dump/save.hh | 26 ++++++++++++- milena/mln/trait/value_.hh | 3 +- milena/mln/value/builtin/floatings.hh | 10 ++++- milena/mln/value/builtin/integers.hh | 65 ++++++++++++++++++++++++++---- milena/mln/value/builtin/symbolics.hh | 15 ++++--- milena/mln/value/int_s.hh | 9 ++++- milena/mln/value/int_u.hh | 9 ++++- milena/mln/value/label.hh | 7 +++ milena/mln/value/rgb.hh | 7 +++ milena/tests/io/dump/dump.cc | 28 +++++++++++++- 12 files changed, 246 insertions(+), 28 deletions(-) diff --git a/milena/ChangeLog b/milena/ChangeLog index 199bdd1..bd6d99c 100644 --- a/milena/ChangeLog +++ b/milena/ChangeLog @@ -1,5 +1,30 @@ 2009-03-10 Guillaume Lazzara <lazzara@lrde.epita.fr> + Make io::dump::* more robust and improve dump format. + + * mln/io/dump/load.hh, + * mln/io/dump/save.hh: + - Add new information in the header. + - Add more assertion on value type, dimension and file type. + + Important note: Old dump files are not compatible with this new + format. + + * mln/trait/value_.hh: add a new macro mln_trait_value_name. + + * mln/value/builtin/floatings.hh + * mln/value/builtin/integers.hh + * mln/value/builtin/symbolics.hh + * mln/value/int_s.hh + * mln/value/int_u.hh + * mln/value/label.hh + * mln/value/rgb.hh: add name() member to value_<> specialization. + It returns the type name as a const char*. + + * tests/io/dump/dump.cc: add new tests with builtin types. + +2009-03-10 Guillaume Lazzara <lazzara@lrde.epita.fr> + Various small fixes. * headers.mk: update dist header list. diff --git a/milena/mln/io/dump/load.hh b/milena/mln/io/dump/load.hh index bc6a051..88f60a2 100644 --- a/milena/mln/io/dump/load.hh +++ b/milena/mln/io/dump/load.hh @@ -73,20 +73,74 @@ namespace mln } - template <typename I> + template <typename I> inline - void load_header(Image<I>& ima, std::ifstream& file) + void load_header(Image<I>& ima, std::ifstream& file, + const std::string& filename) { + // Milena's file type ? + std::string file_type; + file >> file_type; + if (file_type != "milena/dump") + { + std::cerr << "io::dump::load - Error: invalid file type. '" + << filename + << "' is NOT a valid milena/dump file!" + << std::endl; + abort(); + } + + // Dimension ? unsigned dim; file >> dim; - typedef mln_site(I) P; - mln_assertion(P::dim == dim); + if (P::dim != dim) + { + std::cerr << "io::dump::load - Error: invalid image dimension. '" + << filename << "' is a " << dim << "-D image " + << "but you try to load it into a " << P::dim + << "-D image!" + << std::endl; + abort(); + } - P pmin, pmax; + // Size information - Skip it, useless. + std::string tmp; + for (unsigned i = 0; i < dim; ++i) + file >> tmp; + // Skipping endline. + char c; + file.get(c); + + // Value type name ? + // WARNING: value type name limited to 255 characters... + char value_type[255]; + file.getline(value_type, 255); + if (mln_trait_value_name(mln_value(I)) != std::string(value_type)) + { + std::cerr << "io::dump::load - Error: invalid image value type. '" + << filename << "' is an image of '" << value_type + << "' but you try to load it into an image of '" + << mln_trait_value_name(mln_value(I)) << "'!" + << std::endl; + abort(); + } + + // Empty line - may be used for a new information. + file.get(c); + + // Empty line - may be used for a new information. + file.get(c); + + // Pmin + P pmin; read_point<P>(file, pmin); + + // Pmax + P pmax; read_point<P>(file, pmax); + // Initialize the image buffer. mln_concrete(I) result(box<P>(pmin, pmax)); initialize(ima, result); mln_assertion(exact(ima).is_valid()); @@ -123,11 +177,13 @@ namespace mln std::ifstream file(filename.c_str()); if (! file) { - std::cerr << "error: cannot open file '" << filename << "'!"; + std::cerr << "io::dump::load - Error: cannot open file '" + << filename << "'!" + << std::endl; abort(); } - internal::load_header(ima, file); + internal::load_header(ima, file, filename); internal::load_data(ima, file); mln_postcondition(exact(ima).is_valid()); diff --git a/milena/mln/io/dump/save.hh b/milena/mln/io/dump/save.hh index a489a20..41d4571 100644 --- a/milena/mln/io/dump/save.hh +++ b/milena/mln/io/dump/save.hh @@ -40,6 +40,7 @@ # include <mln/core/box_runstart_piter.hh> # include <mln/core/pixel.hh> # include <mln/data/memcpy_.hh> +# include <mln/trait/value_.hh> namespace mln { @@ -68,12 +69,35 @@ namespace mln void save_header(const I& ima, std::ofstream& file) { + // Milena's file type + file << "milena/dump" << std::endl; + + // Dimension typedef mln_site(I) P; - file << P::dim; + file << P::dim << std::endl; + + // Image size. + typedef algebra::vec<P::dim, unsigned> vec_t; + vec_t size = ima.domain().pmax() - ima.domain().pmin(); + for (unsigned i = 0; i < P::dim - 1; ++i) + file << size[i] + 1 << " "; + file << size[P::dim - 1] + 1 << std::endl; + + // Value type name + // WARNING: value type name limited to 255 characters... + file << mln_trait_value_name(mln_value(I)) << std::endl; + + // Empty line - may be used for a new information. + file << std::endl; + + // Empty line - may be used for a new information. + file << std::endl; + // Pmin mln_site(I) p = ima.domain().pmin(); file.write((char*) (&p), sizeof (P)); + // Pmax p = ima.domain().pmax(); file.write((char*) (&p), sizeof (P)); } diff --git a/milena/mln/trait/value_.hh b/milena/mln/trait/value_.hh index d03403d..f607604 100644 --- a/milena/mln/trait/value_.hh +++ b/milena/mln/trait/value_.hh @@ -66,7 +66,8 @@ # define mln_sum(V) mln_trait_value_sum(V) # define mln_sum_(V) mln_trait_value_sum_(V) - +/// Give the value type name +# define mln_trait_value_name(V) mln::trait::value_< V >::name() /// FIXME: check that the -1 is correct # define mln_value_quant_from_(C) \ diff --git a/milena/mln/value/builtin/floatings.hh b/milena/mln/value/builtin/floatings.hh index 3fd0b50..c777555 100644 --- a/milena/mln/value/builtin/floatings.hh +++ b/milena/mln/value/builtin/floatings.hh @@ -1,4 +1,4 @@ -// Copyright (C) 2007, 2008 EPITA Research and Development Laboratory +// Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory // (LRDE) // // This file is part of the Olena Library. This library is free @@ -89,6 +89,10 @@ namespace mln } typedef float sum; + + static const char* name() + { return "float"; } + }; template <> @@ -120,6 +124,10 @@ namespace mln } typedef double sum; + + static const char* name() + { return "float"; } + }; } // end of namespace mln::trait diff --git a/milena/mln/value/builtin/integers.hh b/milena/mln/value/builtin/integers.hh index cb4cced..71cfd34 100644 --- a/milena/mln/value/builtin/integers.hh +++ b/milena/mln/value/builtin/integers.hh @@ -1,4 +1,4 @@ -// Copyright (C) 2007, 2008 EPITA Research and Development Laboratory +// Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory // (LRDE) // // This file is part of the Olena Library. This library is free @@ -90,14 +90,61 @@ namespace mln } // end of namespace mln::trait::internal - template <> struct value_< unsigned char > : internal::value_integer_< unsigned char > {}; - template <> struct value_< signed char > : internal::value_integer_< signed char > {}; - template <> struct value_< unsigned short > : internal::value_integer_< unsigned short > {}; - template <> struct value_< signed short > : internal::value_integer_< signed short > {}; - template <> struct value_< unsigned int > : internal::value_integer_< unsigned int > {}; - template <> struct value_< signed int > : internal::value_integer_< signed int > {}; - template <> struct value_< unsigned long > : internal::value_integer_< unsigned long > {}; - template <> struct value_< signed long > : internal::value_integer_< signed long > {}; + template <> struct value_< unsigned char > + : internal::value_integer_< unsigned char > + { + static const char* name() + { return "unsigned char"; } + }; + + template <> struct value_< signed char > + : internal::value_integer_< signed char > + { + static const char* name() + { return "signed char"; } + }; + + template <> struct value_< unsigned short > + : internal::value_integer_< unsigned short > + { + static const char* name() + { return "unsigned short"; } + }; + + template <> struct value_< signed short > + : internal::value_integer_< signed short > + { + static const char* name() + { return "signed short"; } + }; + + template <> struct value_< unsigned int > + : internal::value_integer_< unsigned int > + { + static const char* name() + { return "unsigned int"; } + }; + + template <> struct value_< signed int > + : internal::value_integer_< signed int > + { + static const char* name() + { return "signed int"; } + }; + + template <> struct value_< unsigned long > + : internal::value_integer_< unsigned long > + { + static const char* name() + { return "unsigned long"; } + }; + + template <> struct value_< signed long > + : internal::value_integer_< signed long > + { + static const char* name() + { return "signed long"; } + }; } // end of namespace mln::trait diff --git a/milena/mln/value/builtin/symbolics.hh b/milena/mln/value/builtin/symbolics.hh index 8724c3a..65cdb3c 100644 --- a/milena/mln/value/builtin/symbolics.hh +++ b/milena/mln/value/builtin/symbolics.hh @@ -1,4 +1,4 @@ -// Copyright (C) 2007, 2008 EPITA Research and Development Laboratory +// Copyright (C) 2007, 2008, 2009 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 @@ -28,10 +28,10 @@ #ifndef MLN_VALUE_BUILTIN_SYMBOLICS_HH # define MLN_VALUE_BUILTIN_SYMBOLICS_HH -/*! \file mln/value/builtin/symbolics.hh - * - * \brief Some definitions about built-in symbolic types. - */ +/// \file mln/value/builtin/symbolics.hh +/// +/// Some definitions about built-in symbolic types. + # include <mln/value/concept/built_in.hh> # include <mln/value/concept/symbolic.hh> @@ -53,7 +53,7 @@ namespace mln { template <> - struct value_< bool> + struct value_<bool> { typedef value::nature::symbolic nature; typedef value::kind::binary kind; @@ -63,6 +63,9 @@ namespace mln card = 2 }; typedef value::quant::low quant; + + static const char* name() { return "bool"; } + }; } // end of namespace mln::trait diff --git a/milena/mln/value/int_s.hh b/milena/mln/value/int_s.hh index e30206c..a6d02ca 100644 --- a/milena/mln/value/int_s.hh +++ b/milena/mln/value/int_s.hh @@ -1,4 +1,4 @@ -// Copyright (C) 2007, 2008 EPITA Research and Development Laboratory +// Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory // (LRDE) // // This file is part of the Olena Library. This library is free @@ -89,6 +89,13 @@ namespace mln static const self_ epsilon() { return 0; } typedef float sum; + + static const char* name() + { + static std::string s = std::string("int_s").append(1, n + '0'); + return s.c_str(); + } + }; } // end of namespace mln::trait diff --git a/milena/mln/value/int_u.hh b/milena/mln/value/int_u.hh index ce1993b..db3353e 100644 --- a/milena/mln/value/int_u.hh +++ b/milena/mln/value/int_u.hh @@ -1,4 +1,4 @@ -// Copyright (C) 2007, 2008 EPITA Research and Development Laboratory +// Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory // (LRDE) // // This file is part of the Olena Library. This library is free @@ -95,6 +95,13 @@ namespace mln static const self_ epsilon() { return 0; } typedef float sum; + + static const char* name() + { + static std::string s = std::string("int_u").append(1, n + '0'); + return s.c_str(); + } + }; } // end of namespace mln::trait diff --git a/milena/mln/value/label.hh b/milena/mln/value/label.hh index 53c3d60..51dfa2d 100644 --- a/milena/mln/value/label.hh +++ b/milena/mln/value/label.hh @@ -78,6 +78,13 @@ namespace mln static const self_ min() { return 0; } static const self_ max() { return mlc_pow_int(2, n) - 1; } + + static const char* name() + { + static std::string s = std::string("label_").append(1, n + '0'); + return s.c_str(); + } + }; } // end of namespace trait diff --git a/milena/mln/value/rgb.hh b/milena/mln/value/rgb.hh index 2d13f94..11733cc 100644 --- a/milena/mln/value/rgb.hh +++ b/milena/mln/value/rgb.hh @@ -217,6 +217,13 @@ namespace mln typedef mln_value_quant_from_(card) quant; typedef algebra::vec<3, float> sum; + + static const char* name() + { + static std::string s = std::string("rgb").append(1, n + '0'); + return s.c_str(); + } + }; } // end of namespace trait diff --git a/milena/tests/io/dump/dump.cc b/milena/tests/io/dump/dump.cc index 6099bc9..7c7ba04 100644 --- a/milena/tests/io/dump/dump.cc +++ b/milena/tests/io/dump/dump.cc @@ -67,7 +67,6 @@ int main() 1, 9 }; image2d<value::int_u8> pic = make::image2d(data); io::dump::save(pic, "pic.dump"); - image2d<value::int_u8> pic2; io::dump::load(pic2, "pic.dump"); @@ -75,6 +74,33 @@ int main() mln_assertion(pic == pic2); } + /// Value: unsigned + { + unsigned data[4] = { 5, 1, + 1, 9 }; + image2d<unsigned> pic = make::image2d(data); + io::dump::save(pic, "pic.dump"); + image2d<unsigned> pic2; + io::dump::load(pic2, "pic.dump"); + + mln_assertion(pic.domain() == pic2.domain()); + mln_assertion(pic == pic2); + } + + /// Value: float + { + float data[4] = { 5, 1, + 1, 9 }; + image2d<float> pic = make::image2d(data); + io::dump::save(pic, "pic.dump"); + image2d<float> pic2; + io::dump::load(pic2, "pic.dump"); + + mln_assertion(pic.domain() == pic2.domain()); + mln_assertion(pic == pic2); + } + + /// Value: rgb8 { using value::rgb8; -- 1.5.6.5
participants (1)
-
Guillaume Lazzara