URL: https://svn.lrde.epita.fr/svn/oln/trunk/milena
ChangeLog:
2009-10-16 Edwin Carlinet <carlinet(a)lrde.epita.fr>
Add AVS field file support.
* mln/io/all.hh: Update.
* mln/io/fld/all.hh: New.
* mln/io/fld/header.hh: New.
* mln/io/fld/load.hh: New.
* mln/io/fld/load_header.hh: New.
* mln/io/fld/max_components.hh: New.
* mln/io/fld/save.hh: New.
* mln/io/fld/write_header.hh: New.
* mln/io/fld: New.
* tests/io/Makefile.am: Add rules.
* tests/io/fld/Makefile.am: New.
* tests/io/fld/fld1d.cc: New.
* tests/io/fld/fld2d.cc: New.
* tests/io/fld/fld3d.cc: New.
* tests/io/fld: New.
---
mln/io/all.hh | 1
mln/io/fld/all.hh | 49 ++++++++
mln/io/fld/header.hh | 94 ++++++++++++++++
mln/io/fld/load.hh | 242 +++++++++++++++++++++++++++++++++++++++++++
mln/io/fld/load_header.hh | 220 +++++++++++++++++++++++++++++++++++++++
mln/io/fld/max_components.hh | 102 ++++++++++++++++++
mln/io/fld/save.hh | 173 ++++++++++++++++++++++++++++++
mln/io/fld/write_header.hh | 129 ++++++++++++++++++++++
tests/io/Makefile.am | 3
tests/io/fld/Makefile.am | 31 +++++
tests/io/fld/fld1d.cc | 57 ++++++++++
tests/io/fld/fld2d.cc | 108 +++++++++++++++++++
tests/io/fld/fld3d.cc | 59 ++++++++++
13 files changed, 1267 insertions(+), 1 deletion(-)
Index: trunk/milena/mln/io/fld/write_header.hh
===================================================================
--- trunk/milena/mln/io/fld/write_header.hh (revision 0)
+++ trunk/milena/mln/io/fld/write_header.hh (revision 4637)
@@ -0,0 +1,129 @@
+// Copyright (C) 2008, 2009 EPITA Research and Development Laboratory (LRDE)
+//
+// This file is part of Olena.
+//
+// Olena is free software: you can redistribute it and/or modify it under
+// the terms of the GNU General Public License as published by the Free
+// Software Foundation, version 2 of the License.
+//
+// Olena 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 Olena. If not, see <http://www.gnu.org/licenses/>.
+//
+// As a special exception, you may use this file as part of a free
+// software project 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_FLD_WRITE_HEADER_HH
+# define MLN_IO_FLD_WRITE_HEADER_HH
+
+///
+/// \brief Write AVS headers in a file.
+///
+///
+///
+
+
+# include <mln/io/fld/header.hh>
+# include <iostream>
+
+namespace mln
+{
+
+ namespace io
+ {
+
+ namespace fld
+ {
+ /// Write the AVS header in a file.
+ ///
+ /// \param file The file to write.
+ /// \param h The AVS header.
+ ///
+ void write_header(std::ostream& file, const fld_header& h);
+
+# ifndef MLN_INCLUDE_ONLY
+
+ inline
+ void
+ write_header(std::ostream& file, const fld_header& h)
+ {
+ file << "# AVS field file" << std::endl;
+ file << "# Generated by Milena 1.0 http://olena.lrde.epita.fr" << std::endl;
+ file << "# EPITA Research and Development Laboratory (LRDE)" << std::endl;
+
+ file << "ndim=" << h.ndim << std::endl;
+ for (int i = 0; i < h.ndim; i++)
+ file << "dim" << (i + 1) << "=" << h.dim[i] << std::endl;
+ file << "nspace=" << h.nspace << std::endl
+ << "veclen=" << h.veclen << std::endl;
+
+ switch (h.data)
+ {
+ case data_type::BYTE :
+ file << "data=byte" << std::endl;
+ break;
+ case data_type::SHORT :
+ file << "data=short" << std::endl;
+ break;
+ case data_type::INTEGER :
+ file << "data=integer" << std::endl;
+ break;
+ case data_type::FLOAT :
+ file << "data=float" << std::endl;
+ break;
+ case data_type::DOUBLE :
+ file << "data=double" << std::endl;
+ break;
+ default:
+ std::cerr << "Data type not supported: abort().";
+ abort();
+ }
+
+ switch (h.field)
+ {
+ case field_type::UNIFORM :
+ file << "field=uniform" << std::endl;
+ break;
+ case field_type::IRREGULAR :
+ file << "field=irregular" << std::endl;
+ break;
+ case field_type::RECTILINEAR :
+ file << "field=rectilinear" << std::endl;
+ break;
+ default:
+ std::cerr << "Field type not suported: abort().";
+ abort();
+ }
+
+ file << "min_ext=";
+ for (int i = 0; i < h.nspace; i++)
+ file << h.min_ext[i] << " ";
+ file << std::endl;
+
+ file << "max_ext=";
+ for (int i = 0; i < h.nspace; i++)
+ file << h.max_ext[i] << " ";
+ file << std::endl;
+
+ file << "\f\f";
+ }
+
+#endif // !MLN_INCLUDE_ONLY
+
+ } // end of namespace mln::io::fld
+
+ } // end of namespace mln::io
+
+} // end of namespace mln
+
+#endif // !MLN_IO_FLD_WRITE_HEADER_HH
Index: trunk/milena/mln/io/fld/load_header.hh
===================================================================
--- trunk/milena/mln/io/fld/load_header.hh (revision 0)
+++ trunk/milena/mln/io/fld/load_header.hh (revision 4637)
@@ -0,0 +1,220 @@
+// Copyright (C) 2008, 2009 EPITA Research and Development Laboratory (LRDE)
+//
+// This file is part of Olena.
+//
+// Olena is free software: you can redistribute it and/or modify it under
+// the terms of the GNU General Public License as published by the Free
+// Software Foundation, version 2 of the License.
+//
+// Olena 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 Olena. If not, see <http://www.gnu.org/licenses/>.
+//
+// As a special exception, you may use this file as part of a free
+// software project 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_FLD_LOAD_HEADER_HH
+# define MLN_IO_FLD_LOAD_HEADER_HH
+
+///
+/// \brief Read AVS header from a file.
+///
+///
+
+# include <mln/io/fld/header.hh>
+# include <cstdlib>
+# include <locale>
+# include <iostream>
+# include <sstream>
+# include <string>
+
+namespace mln
+{
+
+ namespace io
+ {
+
+ namespace fld
+ {
+
+ /// Read the header form an AVS field file.
+ ///
+ /// \param ins The file to read.
+ ///
+ /// \return The header.
+ ///
+ fld_header read_header(std::istream& ins);
+
+# ifndef MLN_INCLUDE_ONLY
+
+ namespace internal
+ {
+ void
+ abort_fld_reader(const char* msg, unsigned line = 0)
+ {
+ std::cerr << "AVS field file reader: " << msg << " on line " << line << std::endl;
+ abort();
+ }
+
+ }
+
+ inline
+ fld_header
+ read_header(std::istream& file)
+ {
+ std::stringstream ins;
+ std::string line_str, lhs, rhs;
+ fld_header header;
+ unsigned line;
+
+ std::getline(file, line_str);
+ line = 1;
+ if (line_str.compare(0, 5, "# AVS"))
+ internal::abort_fld_reader("Invalid format", line);
+
+ while (file.good() && file.peek() != '\f')
+ {
+ std::getline(file, line_str);
+ ++line;
+
+ ins.clear();
+ ins.str(line_str);
+ rhs.clear();
+ lhs.clear();
+
+ { // Parse the line
+ char c = ins.get();
+ while (isspace(c))
+ ins.get(c);
+ if (c == '#') // Comments
+ continue;
+ while (isalnum(c) || c == '_')
+ {
+ lhs.push_back(c);
+ ins.get(c);
+ }
+ while (isspace(c))
+ ins.get(c);
+ if (c != '=')
+ internal::abort_fld_reader("Parse error", line);
+ while (isspace(ins.peek()))
+ ins.ignore();
+ }
+
+ if (lhs == "ndim")
+ {
+ ins >> header.ndim;
+ if (header.ndim < 1)
+ internal::abort_fld_reader("Invalid dimension", line);
+ header.dim = new int[header.ndim];
+ std::fill(header.dim, header.dim + header.ndim, -1);
+ }
+ else if (lhs.compare(0, 3, "dim") == 0)
+ {
+ std::stringstream ss(lhs.substr(3));
+ int dim;
+ ss >> dim;
+ if (dim < 1 || dim > header.ndim)
+ internal::abort_fld_reader("Invalid dimension", line);
+ if (!ss.eof())
+ internal::abort_fld_reader("Parse error", line);
+ ins >> header.dim[dim - 1];
+ if (header.dim[dim - 1] < 1)
+ internal::abort_fld_reader("Invalid dimension", line);
+ }
+ else if (lhs == "nspace")
+ {
+ ins >> header.nspace;
+ if (header.nspace < 1)
+ internal::abort_fld_reader("Invalid space dimension", line);
+ header.min_ext = new float[header.nspace];
+ header.max_ext = new float[header.nspace];
+ }
+ else if (lhs == "veclen")
+ {
+ ins >> header.veclen;
+ if (header.veclen == -1)
+ internal::abort_fld_reader("Invalid vector length", line);
+ }
+ else if (lhs == "data")
+ {
+ ins >> rhs;
+ if (rhs == "byte")
+ header.data = data_type::BYTE;
+ else if (rhs == "short")
+ header.data = data_type::SHORT;
+ else if (rhs == "integer")
+ header.data = data_type::INTEGER;
+ else if (rhs == "float")
+ header.data = data_type::FLOAT;
+ else if (rhs == "double")
+ header.data = data_type::DOUBLE;
+ else
+ internal::abort_fld_reader("Invalid data type", line);
+ }
+ else if (lhs == "field")
+ {
+ ins >> rhs;
+ if (rhs != "uniform")
+ internal::abort_fld_reader("Unhandled field type", line);
+ header.field = field_type::UNIFORM;
+ }
+ else if (lhs == "min_ext")
+ {
+ for (int i = 0; i < header.ndim; ++i)
+ {
+ ins >> header.min_ext[i];
+ if (ins.peek() == ',')
+ ins.ignore();
+ }
+ }
+ else if (lhs == "max_ext")
+ {
+ for (int i = 0; i < header.ndim; ++i)
+ {
+ ins >> header.max_ext[i];
+ if (ins.peek() == ',')
+ ins.ignore();
+ }
+ }
+ else
+ internal::abort_fld_reader("Parse error", line);
+
+ rhs.clear();
+ ins >> rhs;
+ if (!rhs.empty() && rhs[0] != '#')
+ internal::abort_fld_reader("Parse error", line);
+ }
+
+ file.ignore();
+ if (file.get() != '\f')
+ internal::abort_fld_reader("Parse error", line);
+
+ if (header.ndim == -1 || header.nspace == -1 || header.veclen == -1 ||
+ header.data == data_type::UNKNOWN || header.field == field_type::UNKNOWN)
+ internal::abort_fld_reader("Invalid format", line);
+ for (int i = 0; i < header.ndim; ++i)
+ if (header.dim[i] == -1)
+ internal::abort_fld_reader("Invalid format", line);
+ return header;
+ }
+
+# endif // ! MLN_INCLUDE_ONLY
+
+ } // end of namespace mln::io::fld
+
+ } // end of namespace mln::io
+
+} // end of namespace mln
+
+#endif // !MLN_IO_FLD_LOAD_HEADER_HH
Index: trunk/milena/mln/io/fld/all.hh
===================================================================
--- trunk/milena/mln/io/fld/all.hh (revision 0)
+++ trunk/milena/mln/io/fld/all.hh (revision 4637)
@@ -0,0 +1,49 @@
+// Copyright (C) 2007, 2009 EPITA Research and Development Laboratory (LRDE)
+//
+// This file is part of Olena.
+//
+// Olena is free software: you can redistribute it and/or modify it under
+// the terms of the GNU General Public License as published by the Free
+// Software Foundation, version 2 of the License.
+//
+// Olena 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 Olena. If not, see <http://www.gnu.org/licenses/>.
+//
+// As a special exception, you may use this file as part of a free
+// software project 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_FLD_ALL_HH
+# define MLN_IO_FLD_ALL_HH
+
+/// \file
+/// \brief Inclusion of all AVS field file I/O routines.
+
+
+namespace mln
+{
+
+ namespace io
+ {
+ /// Namespace of pgm input/output handling.
+ namespace fld {}
+ }
+
+}
+
+# include <mln/io/fld/load_header.hh>
+# include <mln/io/fld/write_header.hh>
+# include <mln/io/fld/load.hh>
+# include <mln/io/fld/save.hh>
+
+#endif // ! MLN_IO_FLD_ALL_HH
Index: trunk/milena/mln/io/fld/header.hh
===================================================================
--- trunk/milena/mln/io/fld/header.hh (revision 0)
+++ trunk/milena/mln/io/fld/header.hh (revision 4637)
@@ -0,0 +1,94 @@
+// Copyright (C) 2008, 2009 EPITA Research and Development Laboratory (LRDE)
+//
+// This file is part of Olena.
+//
+// Olena is free software: you can redistribute it and/or modify it under
+// the terms of the GNU General Public License as published by the Free
+// Software Foundation, version 2 of the License.
+//
+// Olena 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 Olena. If not, see <http://www.gnu.org/licenses/>.
+//
+// As a special exception, you may use this file as part of a free
+// software project 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_FLD_HEADER_HH
+# define MLN_IO_FLD_HEADER_HH
+
+namespace mln
+{
+
+ namespace io
+ {
+
+ namespace fld
+ {
+
+
+ struct data_type { enum E { UNKNOWN, BYTE, SHORT, INTEGER, FLOAT, DOUBLE }; };
+ struct field_type { enum E { UNKNOWN, UNIFORM, RECTILINEAR, IRREGULAR }; };
+
+ ///
+ /// \brief Define the header structure of an AVS field data file.
+ ///
+ struct fld_header
+ {
+ int ndim; // The number of computational dimensions in the field.
+ int* dim; // The dimension size of each axis.
+ int nspace; // The number of physical coordinates per field element.
+ int veclen; // The number of data values for each field element.
+ data_type::E data; // The primitive data type of all the data values.
+ field_type::E field; // The field type.
+ float* min_ext; // The minimum coordinate value that any member data point occupies in space.
+ float* max_ext; // The maximum coordinate value that any member data point occupies in space.
+ // std::vector<std::string> label; // Not handled.
+ // std::vector<std::string> unit; // Not handled.
+ // void* min_val; // The minimum data value in the field. (Not used)
+ // void* max_val; // The maximum data value in the field. (Not used)
+ // struct {...} variable; // Not handled.
+ // struct {...} coord; // Not handled.
+
+ fld_header();
+ ~fld_header();
+ };
+
+
+# ifndef MLN_INCLUDE_ONLY
+
+ fld_header::fld_header()
+ : ndim (-1),
+ dim (0),
+ nspace (-1),
+ veclen (-1),
+ data (data_type::UNKNOWN),
+ field (field_type::UNKNOWN)
+ {
+ }
+
+ fld_header::~fld_header()
+ {
+ delete [] dim;
+ delete [] max_ext;
+ delete [] min_ext;
+ }
+
+# endif // ! MLN_INCLUDE_ONLY
+
+ } // end of namespace mln::io::fld
+
+ } // end of namespace mln::io
+
+} // end of namespace mln
+
+#endif // !MLN_IO_FLD_HEADER_HH
Index: trunk/milena/mln/io/fld/max_components.hh
===================================================================
--- trunk/milena/mln/io/fld/max_components.hh (revision 0)
+++ trunk/milena/mln/io/fld/max_components.hh (revision 4637)
@@ -0,0 +1,102 @@
+// Copyright (C) 2008, 2009 EPITA Research and Development Laboratory (LRDE)
+//
+// This file is part of Olena.
+//
+// Olena is free software: you can redistribute it and/or modify it under
+// the terms of the GNU General Public License as published by the Free
+// Software Foundation, version 2 of the License.
+//
+// Olena 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 Olena. If not, see <http://www.gnu.org/licenses/>.
+//
+// As a special exception, you may use this file as part of a free
+// software project 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_FLD_MAX_COMPONENTS_HH
+# define MLN_IO_FLD_MAX_COMPONENTS_HH
+
+# include <mln/algebra/vec.hh>
+# include <mln/value/rgb.hh>
+# include <mln/io/fld/header.hh>
+
+namespace mln
+{
+
+ namespace io
+ {
+
+ namespace fld
+ {
+
+ template <typename V>
+ inline
+ unsigned int max_component(const V&);
+
+ template <unsigned n, typename V>
+ inline
+ unsigned int max_component(const algebra::vec<n, V>& v);
+
+ template <unsigned n>
+ inline
+ unsigned int max_component(const value::rgb<n>&);
+
+ inline
+ unsigned int max_component(const fld::data_type::E& t);
+
+# ifndef MLN_INCLUDE_ONLY
+
+ template <typename V>
+ inline
+ unsigned int max_component(const V&)
+ {
+ return mln_max(V);
+ }
+
+
+ template <unsigned n, typename V>
+ inline
+ unsigned int max_component(const algebra::vec<n, V>& v)
+ {
+ return mln_max(V);
+ }
+
+ template <unsigned n>
+ inline
+ unsigned int max_component(const value::rgb<n>&)
+ {
+ return mln_max(mln::value::int_u<n>);
+ }
+
+ inline
+ unsigned int max_component(const fld::data_type::E& t)
+ {
+ switch (t)
+ {
+ case data_type::BYTE: return mln_max(unsigned char);
+ case data_type::SHORT: return mln_max(unsigned short);
+ case data_type::INTEGER: return mln_max(unsigned);
+ case data_type::FLOAT: return mln_max(float);
+ case data_type::DOUBLE: return mln_max(double);
+ default: return 0;
+ }
+ }
+
+# endif // ! MLN_INCLUDE_ONLY
+ }
+
+ }
+
+}
+
+#endif // !MLN_IO_FLD_MAX_COMPONENTS_HH
Index: trunk/milena/mln/io/fld/save.hh
===================================================================
--- trunk/milena/mln/io/fld/save.hh (revision 0)
+++ trunk/milena/mln/io/fld/save.hh (revision 4637)
@@ -0,0 +1,173 @@
+// Copyright (C) 2008, 2009 EPITA Research and Development Laboratory (LRDE)
+//
+// This file is part of Olena.
+//
+// Olena is free software: you can redistribute it and/or modify it under
+// the terms of the GNU General Public License as published by the Free
+// Software Foundation, version 2 of the License.
+//
+// Olena 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 Olena. If not, see <http://www.gnu.org/licenses/>.
+//
+// As a special exception, you may use this file as part of a free
+// software project 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_FLD_SAVE_HH
+# define MLN_IO_FLD_SAVE_HH
+/// \file
+/// \brief Save an image to AVS field file format.
+///
+/// \todo Handle not high speed images.
+
+# include <mln/core/concept/image.hh>
+# include <mln/core/concept/gpoint.hh>
+# include <mln/io/fld/header.hh>
+# include <mln/io/fld/write_header.hh>
+# include <mln/io/fld/max_components.hh>
+
+# include <mln/algebra/vec.hh>
+
+# include <mln/geom/nsites.hh>
+# include <fstream>
+# include <iostream>
+
+namespace mln
+{
+
+ namespace io
+ {
+
+ namespace fld
+ {
+
+ template <typename I>
+ void save(const Image<I>& ima_, const char* filename);
+
+
+# ifndef MLN_INCLUDE_ONLY
+
+ namespace internal
+ {
+ template <typename I>
+ inline
+ void save_data_contiguous(std::ofstream& file, const I& ima)
+ {
+ typedef mln_site(I) P;
+ typedef mln_value(I) V;
+ enum { dim = P::dim };
+
+ P pmin = ima.domain().pmin();
+ P pmax = ima.domain().pmax();
+
+ std::size_t len = pmax[dim - 1] - pmin[dim - 1] + 1;
+ std::size_t n = len * sizeof(V);
+ P p = pmin;
+ if (dim == 1)
+ {
+ file.write((char*)(&ima(p)), n);
+ return;
+ }
+
+ while (true)
+ {
+ file.write((char*)(&ima(p)), n);
+ ++p[dim - 2];
+
+ for (int i = dim - 2; p[i] > pmax[i]; --i)
+ {
+ if (i == 0)
+ return;
+ p[i] = pmin[i];
+ ++p[i - 1];
+ }
+ }
+ }
+
+ template <typename I>
+ inline
+ fld::fld_header make_header(const I& ima)
+ {
+ fld_header hdr;
+ typedef mln_site(I) P;
+ typedef mln_value(I) V;
+ enum { dim = P::dim };
+
+ hdr.ndim = dim;
+ hdr.nspace = dim;
+ hdr.veclen = mln_dim(V);
+ hdr.dim = new int[dim];
+ hdr.min_ext = new float[dim];
+ hdr.max_ext = new float[dim];
+
+ box<P> bbox = geom::bbox(ima);
+ P pmin = bbox.pmin();
+ P pmax = bbox.pmax();
+
+ for (unsigned i = 0; i < dim; i++)
+ {
+ hdr.dim[i] = pmax[i] - pmin[i] + 1;
+ hdr.min_ext[i] = pmin[i];
+ hdr.max_ext[i] = pmax[i];
+ }
+
+ unsigned max_c = max_component(V ());
+ if (max_c == max_component(data_type::BYTE))
+ hdr.data = data_type::BYTE;
+ else if (max_c == max_component(data_type::SHORT))
+ hdr.data = data_type::SHORT;
+ else if (max_c == max_component(data_type::INTEGER))
+ hdr.data = data_type::INTEGER;
+ else if (max_c == max_component(data_type::FLOAT))
+ hdr.data = data_type::FLOAT;
+ else if (max_c == max_component(data_type::DOUBLE))
+ hdr.data = data_type::DOUBLE;
+ else
+ hdr.data = data_type::UNKNOWN;
+
+ hdr.field = field_type::UNIFORM;
+
+ return hdr;
+ }
+
+ } // end of namespace mln::io::fld::internal
+
+ template <typename I>
+ void save(const Image<I>& ima_, const char* filename)
+ {
+ trace::entering("mln::io::fld::save");
+ // For the moment, just the fast version.
+ mlc_is(mln_trait_image_speed(I), trait::image::speed::fastest)::check();
+
+ const I& ima = exact(ima_);
+ mln_precondition(ima.is_valid());
+
+ std::ofstream file(filename);
+ fld_header hdr = internal::make_header(ima);
+
+ write_header(file, hdr);
+ internal::save_data_contiguous(file, ima);
+
+ file.close();
+ trace::exiting("mln::io::fld::save");
+ }
+
+
+# endif // ! MLN_INCLUDE_ONLY
+
+ } // end of namespace mln::io::fld
+
+ } // end of namespace mln::io
+
+} // end of namespace mln
+#endif // !MLN_IO_FLD_SAVE_HH
Index: trunk/milena/mln/io/fld/load.hh
===================================================================
--- trunk/milena/mln/io/fld/load.hh (revision 0)
+++ trunk/milena/mln/io/fld/load.hh (revision 4637)
@@ -0,0 +1,242 @@
+// Copyright (C) 2008, 2009 EPITA Research and Development Laboratory (LRDE)
+//
+// This file is part of Olena.
+//
+// Olena is free software: you can redistribute it and/or modify it under
+// the terms of the GNU General Public License as published by the Free
+// Software Foundation, version 2 of the License.
+//
+// Olena 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 Olena. If not, see <http://www.gnu.org/licenses/>.
+//
+// As a special exception, you may use this file as part of a free
+// software project 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_FLD_LOAD_HH
+# define MLN_IO_FLD_LOAD_HH
+
+/// \file
+///
+/// \brief Load an image from an AVS field file.
+///
+/// \note The current loader does not follow the whole specifications
+/// of the format. Actually, it has the following restrictions:
+/// - the dimension of the field and the space must be the same.
+/// - the number of dimension is limited to 1D, 2D and 3D.
+/// - the data format must be native (float, integer...) (XDR extension is not supported)
+/// - the field must uniform (regular grid).
+/// - dim1, dim2... dimn are parsed but ignored.
+/// - min_ext and max_ext (pmin and pmax of the bbox) are not computed and are compulsory.
+/// - label and unit keyword are not supported.
+/// - external data source ('coord', and 'variable') is not supported.
+///
+/// FIXME: pnm::load uses special implementation if sizeof(int_u8) != 1 ?? what ??
+
+# include <mln/core/concept/image.hh>
+# include <mln/io/fld/header.hh>
+# include <mln/io/fld/load_header.hh>
+# include <mln/io/fld/max_components.hh>
+
+# include <mln/algebra/vec.hh>
+# include <mln/value/rgb.hh>
+# include <mln/value/int_u8.hh>
+
+# include <mln/geom/nsites.hh>
+
+# include <fstream>
+# include <iostream>
+
+namespace mln
+{
+
+ namespace io
+ {
+
+ namespace fld
+ {
+
+ /// Load an image from an AVS field file.
+ ///
+ /// \param[in,out] ima_ The image to load.
+ /// \param[in] filename The path to the AVS file.
+ ///
+ template <typename I>
+ inline
+ void
+ load(Image<I>& ima_, const char* filename);
+
+# ifndef MLN_INCLUDE_ONLY
+
+ namespace internal
+ {
+
+ void
+ abort_load(const char* msg, const char* filename)
+ {
+ std::cerr << "Error: file '" << filename << "'"
+ << "cannot be loaded." << std::endl
+ << "Error description: " << msg << std::endl;
+ abort();
+ }
+
+ // Read a Milena rgb value (sizeof(int_u8) != 1).
+ template <unsigned int n>
+ inline
+ void read_value(std::ifstream& file, value::rgb<n>& v)
+ {
+ typedef typename value::int_u<n>::enc E;
+
+ E c;
+ file.read((char*)(&c), sizeof(E));
+ v.red() = c;
+ file.read((char*)(&c), sizeof(E));
+ v.green() = c;
+ file.read((char*)(&c), sizeof(E));
+ v.blue() = c;
+ }
+
+ // Read a Milena scalar value (sizeof(int_u8) != 1).
+ template <class V>
+ inline
+ void read_value(std::ifstream& file, value::Scalar<V>& v)
+ {
+ typedef typename V::enc E;
+
+ E c;
+ file.read((char*)(&c), sizeof(E));
+ exact(v) = c;
+ }
+
+ // Read a builtin scalar value.
+ template <typename V>
+ inline
+ void read_value(std::ifstream& file, V& v)
+ {
+ V c;
+ file.read((char*)(&c), sizeof(V));
+ v = c;
+ }
+
+ // used when (sizeof(int_u8) != 1)
+ template <typename I>
+ inline
+ void load_raw_uncontiguous(std::ifstream& file, I& ima)
+ {
+ mln_piter(I) p(ima.domain());
+ read_value(file, ima(p));
+ }
+
+ // used in g++ > 2.95
+ template <typename I>
+ inline
+ void load_raw_contiguous(std::ifstream& file, I& ima)
+ {
+ mln_site(I) pmin = ima.domain().pmin();
+ mln_site(I) pmax = ima.domain().pmax();
+
+ typedef mln_site(I) P;
+ enum { dim = P::dim };
+
+ // The first array index varies most quickly (FORTRAN-style).
+ typedef mln_value(I) V;
+
+
+ std::size_t len = pmax[dim - 1] - pmin[dim - 1] + 1;
+ std::size_t n = len * sizeof(V);
+
+ P p = pmin;
+ if (dim == 1)
+ {
+ file.read((char*)(&ima(p)), n);
+ return;
+ }
+
+ while (true)
+ {
+ file.read((char*)(&ima(p)), n);
+ ++p[dim - 2];
+
+ for (int i = dim - 2; p[i] > pmax[i]; --i)
+ {
+ if (i == 0)
+ return;
+ p[i] = pmin[i];
+ ++p[i - 1];
+ }
+ }
+ }
+
+ template <typename I>
+ inline
+ void load_raw(std::ifstream& file, I& ima)
+ {
+ if (sizeof(value::int_u8) == 1)
+ load_raw_contiguous(file, ima);
+ else
+ load_raw_uncontiguous(file, ima);
+ }
+
+ } // end of mln::io::internal
+
+ template <typename I>
+ inline
+ void
+ load(Image<I>& ima_, const char* filename)
+ {
+ trace::entering("mln::io::fld::load");
+
+ std::ifstream file(filename);
+ if (! file)
+ internal::abort_load("Fail to open the file.", filename);
+
+ typedef mln_value(I) V;
+ typedef mln_site(I) P;
+
+ I& ima = exact(ima_);
+ fld_header hder = fld::read_header(file);
+ int nspace = P::dim;
+ int veclen = mln_dim(V);
+
+ if (nspace != hder.nspace)
+ internal::abort_load("The dimension of the input does not match the one from the file.", filename);
+ if (nspace > 3)
+ internal::abort_load("The loader does not handle image dimension greater than three.", filename);
+ if (veclen != hder.veclen)
+ internal::abort_load("The dimension of the value does not match the one from the file.", filename);
+ if (max_component(V ()) != max_component(hder.data))
+ internal::abort_load("The data type of the input mismatches the one from the file.", filename);
+
+ box<mln_site(I)> bbox;
+ for (int i = 0; i < hder.ndim; ++i)
+ {
+ bbox.pmin()[i] = hder.min_ext[i];
+ bbox.pmax()[i] = hder.max_ext[i];
+ }
+
+ ima.init_(bbox);
+ internal::load_raw(file, ima);
+
+ file.close();
+ trace::exiting("mln::io::fld::load");
+ }
+
+# endif // ! MLN_INCLUDE_ONLY
+
+ } // end of namespace mln::io::fld
+
+ } // end of namespace mln::io
+
+} // end of namespace mln
+
+#endif // !MLN_IO_FLD_LOAD_HH
Index: trunk/milena/mln/io/all.hh
===================================================================
--- trunk/milena/mln/io/all.hh (revision 4636)
+++ trunk/milena/mln/io/all.hh (revision 4637)
@@ -57,6 +57,7 @@
# include <mln/io/ppm/all.hh>
# include <mln/io/txt/all.hh>
# include <mln/io/off/all.hh>
+# include <mln/io/fld/all.hh>
/*--------------------------------------------------.
Index: trunk/milena/tests/io/fld/fld2d.cc
===================================================================
--- trunk/milena/tests/io/fld/fld2d.cc (revision 0)
+++ trunk/milena/tests/io/fld/fld2d.cc (revision 4637)
@@ -0,0 +1,108 @@
+// Copyright (C) 2008, 2009 EPITA Research and Development Laboratory (LRDE)
+//
+// This file is part of Olena.
+//
+// Olena is free software: you can redistribute it and/or modify it under
+// the terms of the GNU General Public License as published by the Free
+// Software Foundation, version 2 of the License.
+//
+// Olena 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 Olena. If not, see <http://www.gnu.org/licenses/>.
+//
+// As a special exception, you may use this file as part of a free
+// software project 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.
+
+///
+/// \brief Test fld IO on 2D images.
+///
+
+#include <mln/core/image/image2d.hh>
+
+#include <mln/io/pgm/load.hh>
+#include <mln/io/ppm/load.hh>
+#include <mln/io/fld/load.hh>
+#include <mln/io/fld/save.hh>
+#include <mln/value/int_u8.hh>
+#include <mln/value/int_u16.hh>
+#include <mln/data/compare.hh>
+#include "tests/data.hh"
+
+#include <stdio.h>
+#include <float.h>
+#include <time.h>
+
+int main()
+{
+ using namespace mln;
+ using value::int_u8;
+ using value::int_u16;
+
+ // Test on int_u8.
+ // Veclen = 1, data = byte
+ {
+ image2d<int_u8> ori, test;
+ io::pgm::load(ori, MLN_IMG_DIR "/lena.pgm");
+
+ io::fld::save(ori, "out.fld");
+ io::fld::load(test, "out.fld");
+
+ // Clean output.
+ std::remove("out.fld");
+
+ mln_assertion(ori == test);
+ }
+
+ // Test on RGB 16
+ // Veclen = 3, data = short
+ {
+ image2d<int_u16> ori, test;
+ io::ppm::load(ori, MLN_IMG_DIR "/lena_16.ppm");
+
+ io::fld::save(ori, "out.fld");
+ io::fld::load(test, "out.fld");
+
+ // Clean output.
+ std::remove("out.fld");
+
+ mln_assertion(ori == test);
+ }
+
+ // Test on 32-bits data type
+ // Veclen = 1, data = float
+ {
+ image2d<float> ori, test;
+ box<point2d> domain(8, 9);
+
+ srand(time(NULL));
+ ori.init_(domain);
+ {
+ mln_piter_(image2d<float>) p(domain);
+ for_all(p)
+ ori(p) = random() / RAND_MAX;
+ }
+
+ io::fld::save(ori, "out.fld");
+ io::fld::load(test, "out.fld");
+
+ // Clean output.
+ std::remove("out.fld");
+
+ {
+ mln_piter_(image2d<float>) p(domain);
+ for_all(p)
+ mln_assertion(fabs(ori(p) - test(p)) < FLT_EPSILON);
+ }
+ }
+
+}
Index: trunk/milena/tests/io/fld/fld3d.cc
===================================================================
--- trunk/milena/tests/io/fld/fld3d.cc (revision 0)
+++ trunk/milena/tests/io/fld/fld3d.cc (revision 4637)
@@ -0,0 +1,59 @@
+// Copyright (C) 2008, 2009 EPITA Research and Development Laboratory (LRDE)
+//
+// This file is part of Olena.
+//
+// Olena is free software: you can redistribute it and/or modify it under
+// the terms of the GNU General Public License as published by the Free
+// Software Foundation, version 2 of the License.
+//
+// Olena 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 Olena. If not, see <http://www.gnu.org/licenses/>.
+//
+// As a special exception, you may use this file as part of a free
+// software project 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.
+
+///
+/// \brief Test AVS field file IO with 3D image.
+///
+///
+///
+
+#include <mln/core/image/image3d.hh>
+#include <mln/io/fld/load.hh>
+#include <mln/io/fld/save.hh>
+#include <mln/value/int_u8.hh>
+#include <mln/debug/iota.hh>
+#include <mln/debug/println.hh>
+#include <mln/data/compare.hh>
+
+
+int main()
+{
+ using namespace mln;
+ using value::int_u8;
+
+
+ image3d<int_u8> ori, test;
+ box<point3d> b(8, 9, 10);
+ ori.init_(b);
+ debug::iota(ori);
+
+ io::fld::save(ori, "out.fld");
+ io::fld::load(test, "out.fld");
+
+ // Clean output.
+ std::remove("out.fld");
+
+ mln_assertion(ori == test);
+}
Index: trunk/milena/tests/io/fld/Makefile.am
===================================================================
--- trunk/milena/tests/io/fld/Makefile.am (revision 0)
+++ trunk/milena/tests/io/fld/Makefile.am (revision 4637)
@@ -0,0 +1,31 @@
+# Copyright (C) 2007, 2009 EPITA Research and Development Laboratory (LRDE).
+#
+# This file is part of Olena.
+#
+# Olena is free software: you can redistribute it and/or modify it under
+# the terms of the GNU General Public License as published by the Free
+# Software Foundation, version 2 of the License.
+#
+# Olena 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 Olena. If not, see <http://www.gnu.org/licenses/>.
+#
+
+## Process this file through Automake to create Makefile.in.
+
+include $(top_srcdir)/milena/tests/tests.mk
+
+check_PROGRAMS = \
+ fld2d \
+ fld1d \
+ fld3d
+
+fld2d_SOURCES = fld2d.cc
+fld3d_SOURCES = fld3d.cc
+fld1d_SOURCES = fld1d.cc
+
+TESTS = $(check_PROGRAMS)
\ No newline at end of file
Index: trunk/milena/tests/io/fld/fld1d.cc
===================================================================
--- trunk/milena/tests/io/fld/fld1d.cc (revision 0)
+++ trunk/milena/tests/io/fld/fld1d.cc (revision 4637)
@@ -0,0 +1,57 @@
+// Copyright (C) 2008, 2009 EPITA Research and Development Laboratory (LRDE)
+//
+// This file is part of Olena.
+//
+// Olena is free software: you can redistribute it and/or modify it under
+// the terms of the GNU General Public License as published by the Free
+// Software Foundation, version 2 of the License.
+//
+// Olena 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 Olena. If not, see <http://www.gnu.org/licenses/>.
+//
+// As a special exception, you may use this file as part of a free
+// software project 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.
+
+///
+/// \brief Test AVS field file IO with 1D image.
+///
+///
+///
+
+#include <mln/core/image/image1d.hh>
+#include <mln/io/fld/load.hh>
+#include <mln/io/fld/save.hh>
+#include <mln/value/int_u8.hh>
+#include <mln/debug/iota.hh>
+#include <mln/data/compare.hh>
+
+int main()
+{
+ using namespace mln;
+ using value::int_u8;
+
+
+ image1d<int_u8> ori, test;
+ box<point1d> b(9);
+ ori.init_(b);
+ debug::iota(ori);
+
+ io::fld::save(ori, "out.fld");
+ io::fld::load(test, "out.fld");
+
+ // Clean output.
+ std::remove("out.fld");
+
+ mln_assertion(ori == test);
+}
Index: trunk/milena/tests/io/Makefile.am
===================================================================
--- trunk/milena/tests/io/Makefile.am (revision 4636)
+++ trunk/milena/tests/io/Makefile.am (revision 4637)
@@ -33,7 +33,8 @@
pgms \
pnm \
ppm \
- ppms
+ ppms \
+ fld
## ------------------------------------------------- ##
## I/O routines depending on a third-party library. ##
* green/mln/clustering/kmean3d.hh: Fix mistakes in documentation.
---
trunk/milena/sandbox/ChangeLog | 6 +++
.../milena/sandbox/green/mln/clustering/kmean3d.hh | 33 +++++++++-----------
2 files changed, 21 insertions(+), 18 deletions(-)
diff --git a/trunk/milena/sandbox/ChangeLog b/trunk/milena/sandbox/ChangeLog
index eac8803..ea973fa 100644
--- a/trunk/milena/sandbox/ChangeLog
+++ b/trunk/milena/sandbox/ChangeLog
@@ -1,5 +1,11 @@
2009-10-15 Yann Jacquelet <jacquelet(a)lrde.epita.fr>
+ Correct kmean3d documentation.
+
+ * green/mln/clustering/kmean3d.hh: Fix mistakes in documentation.
+
+2009-10-15 Yann Jacquelet <jacquelet(a)lrde.epita.fr>
+
Make Théo projection for histogram available.
* green/mln/display: New directory.
diff --git a/trunk/milena/sandbox/green/mln/clustering/kmean3d.hh b/trunk/milena/sandbox/green/mln/clustering/kmean3d.hh
index 6aebefc..fb1a8df 100644
--- a/trunk/milena/sandbox/green/mln/clustering/kmean3d.hh
+++ b/trunk/milena/sandbox/green/mln/clustering/kmean3d.hh
@@ -31,11 +31,11 @@
/// \brief Implements the optimized kmean algorithm.
///
/// This algorithm is optimized in the way it proceeds directly with
-/// the greylevel attribute inspite of the pixel attribute. The
+/// the rgb values inspite of the pixel attribute. The
/// algorithm is independant from the image dimension. But, we have to
/// compute one time the histogram. In fact, we move a recurrent cost
-/// to a fix cost in the complexity. This version is very adapted to
-/// images with small quantification.
+/// to a fix cost in the complexity. This version is adapted to
+/// image with small quantification.
#include <limits.h>
#include <iostream>
@@ -86,12 +86,12 @@ namespace mln
{
/// \brief Implements the kmean algorithm in a specific way.
///
- /// This version of the kmean algorithm uses a greyscale image as input,
+ /// This version of the kmean algorithm uses a rgb image as input,
/// temporary images for computations and produces images as result. Images
- /// play the role of matrix or vector in standard statistic algoritm.
+ /// play the role of matrix or vector in standard statistic algorithm.
///
/// T is the type used for computations (float or double).
- /// n is the quantification for the image grayscale.
+ /// n is the quantification for the rgb image.
template <typename T, unsigned n>
struct kmean3d
{
@@ -208,7 +208,7 @@ namespace mln
/// \brief Two ways: Regular greylevel tick or random greylevel value or.
///
/// There is two way to proceed the initialization. First of all, we
- /// divide the greyscale in regular tick and we assigne them to the mean
+ /// divide the rgb space in regular tick and we assigne them to the mean
/// of the centers. Finaly, we can ask random initialization along the
/// greyscale axis. The second process is needed to launch_n_times the
/// kmean and converge to the best descent.
@@ -388,17 +388,17 @@ namespace mln
/// \}
- /// Greylevels description.
+ /// rgb image description.
/// \{
- /// \brief The information are concerned with the greylevel input image.
+ /// \brief The information are concerned with the rgb input image.
///
- /// The group image allow us to decide which greylevel (and of course
+ /// The group image allow us to decide which rgb color (and of course
/// which pixel) is assigned to a center. The distance image give us a
/// clue on how a greylevel could contribute to a center. The summation
- /// over the greylevels of a center give us the within variance.
+ /// over the rgb space of a center give us the within variance.
- t_group_img _group; // g x 1 because dim(t_value) = 1
- t_distance_img _distance; // label x graylevel
+ t_group_img _group; // g x 3 because dim(t_value) = 3
+ t_distance_img _distance; // label x rgb space
/// \}
@@ -1008,8 +1008,6 @@ namespace mln
{
trace::entering("mln::clustering::kmean3d::update_mean");
- /// FIXME VERIFIER QUE L'ON PEUT OBTENIR UNE IMAGE EN NDG SIGNE
-
// avec g le niveau de gris (signed or not signed)
// w[g] la classe de g sous forme d'image
// h[g] l'histogramme de l'image sous forme d'image
@@ -1035,7 +1033,6 @@ namespace mln
for_all(rgb)
{
- // peut être faut-il le decomposer par composantes
_mean[_group(rgb)][0] += rgb.row() * _histo(rgb);
_mean[_group(rgb)][1] += rgb.col() * _histo(rgb);
_mean[_group(rgb)][2] += rgb.sli() * _histo(rgb);
@@ -1275,8 +1272,8 @@ namespace mln
// Debugging code
update_cnv();
- std::cout << "_current_step : " << _current_step << std::endl;
- std::cout << "_within_variance : " << _within_variance << std::endl;
+ //std::cout << "_current_step : " << _current_step << std::endl;
+ //std::cout << "_within_variance : " << _within_variance << std::endl;
++_current_step;
}
--
1.5.6.5
* green/mln/display: New directory.
* green/mln/display/display_histo.hh: New library file.
* green/mln/display/project_histo.hh: New library file.
* green/mln/fun/v2v/log.hh: New library file.
---
trunk/milena/sandbox/ChangeLog | 9 ++
.../sandbox/green/mln/display/display_histo.hh | 92 +++++++++++++++++++
.../sandbox/green/mln/display/project_histo.hh | 97 ++++++++++++++++++++
trunk/milena/sandbox/green/mln/fun/v2v/log.hh | 70 ++++++++++++++
4 files changed, 268 insertions(+), 0 deletions(-)
create mode 100644 trunk/milena/sandbox/green/mln/display/display_histo.hh
create mode 100644 trunk/milena/sandbox/green/mln/display/project_histo.hh
create mode 100644 trunk/milena/sandbox/green/mln/fun/v2v/log.hh
diff --git a/trunk/milena/sandbox/ChangeLog b/trunk/milena/sandbox/ChangeLog
index 2ab7bd8..eac8803 100644
--- a/trunk/milena/sandbox/ChangeLog
+++ b/trunk/milena/sandbox/ChangeLog
@@ -1,3 +1,12 @@
+2009-10-15 Yann Jacquelet <jacquelet(a)lrde.epita.fr>
+
+ Make Th�o projection for histogram available.
+
+ * green/mln/display: New directory.
+ * green/mln/display/display_histo.hh: New library file.
+ * green/mln/display/project_histo.hh: New library file.
+ * green/mln/fun/v2v/log.hh: New library file.
+
2009-10-14 Yann Jacquelet <jacquelet(a)lrde.epita.fr>
Add spaces to conform to the LRDE coding norm.
diff --git a/trunk/milena/sandbox/green/mln/display/display_histo.hh b/trunk/milena/sandbox/green/mln/display/display_histo.hh
new file mode 100644
index 0000000..d34914c
--- /dev/null
+++ b/trunk/milena/sandbox/green/mln/display/display_histo.hh
@@ -0,0 +1,92 @@
+// Copyright (C) 2007 EPITA Research and Development Laboratory (LRDE)
+// Copyright (C) 2008 EPITA Research and Development Laboratory (LRDE)
+// Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE)
+//
+// This file is part of Olena.
+//
+// Olena is free software: you can redistribute it and/or modify it under
+// the terms of the GNU General Public License as published by the Free
+// Software Foundation, version 2 of the License.
+//
+// Olena 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 Olena. If not, see <http://www.gnu.org/licenses/>.
+//
+// As a special exception, you may use this file as part of a free
+// software project 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_DISPLAY_DISPLAY_HISTO_HH
+# define MLN_DISPLAY_DISPLAY_HISTO_HH
+
+# include <mln/data/stretch.hh>
+# include <mln/fun/v2v/log.hh>
+# include <mln/display/project_histo.hh>
+# include <mln/accu/math/sum.hh>
+
+/// \file
+///
+/// \brief Allow the complete visualization of a 3d histogram by projection.
+///
+/// The 3d histogram is projected in red/green space by accumulating around the
+/// the blue dimension (green and blue composantes are being correlated). In
+/// fact, we sum in along the blue direction. Then, we stretch value to feet
+/// pgm format.
+
+
+namespace mln
+{
+
+ namespace display
+ {
+
+ // Forward declaration.
+ image2d<value::int_u8>
+ display_histo3d_unsigned(const image3d<unsigned>& histo);
+
+#ifndef MLN_INCLUDE_ONLY
+
+ /// \brief Allow the visualization of a 3d histogram by projection.
+ ///
+ /// The 3d histogram is projected in red/green space by
+ /// accumulating around the the blue dimension (green and blue
+ /// composantes are being correlated). In fact, we sum in along
+ /// the blue direction. Then, we stretch value to feet pgm
+ /// format.
+ ///
+ /// \parameter[in] histo the histogram in 3d.
+ /// \result return a equivalent 2d image.
+
+
+ image2d<value::int_u8>
+ display_histo3d_unsigned(const image3d<unsigned>& histo)
+ {
+ typedef accu::math::sum<unsigned,unsigned> t_sum;
+ typedef value::int_u8 t_int_u8;
+ typedef fun::v2v::log<float> t_log;
+
+ image2d<unsigned> proj = project_histo<t_sum,2>(histo);
+ image2d<t_int_u8> proj_int = data::stretch(t_int_u8(),
+ data::transform(proj,
+ t_log()));
+ return proj_int;
+ }
+
+#endif // ! MLN_INCLUDE_ONLY
+
+
+ } // end of namespace mln::transform
+
+} // end of namespace mln
+
+
+#endif // ! MLN_DISPLAY_DISPLAY_HISTO_HH
diff --git a/trunk/milena/sandbox/green/mln/display/project_histo.hh b/trunk/milena/sandbox/green/mln/display/project_histo.hh
new file mode 100644
index 0000000..63ea84e
--- /dev/null
+++ b/trunk/milena/sandbox/green/mln/display/project_histo.hh
@@ -0,0 +1,97 @@
+// Copyright (C) 2007 EPITA Research and Development Laboratory (LRDE)
+// Copyright (C) 2008 EPITA Research and Development Laboratory (LRDE)
+// Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE)
+//
+// This file is part of Olena.
+//
+// Olena is free software: you can redistribute it and/or modify it under
+// the terms of the GNU General Public License as published by the Free
+// Software Foundation, version 2 of the License.
+//
+// Olena 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 Olena. If not, see <http://www.gnu.org/licenses/>.
+//
+// As a special exception, you may use this file as part of a free
+// software project 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_DISPLAY_PROJECT_HISTO_HH
+# define MLN_DISPLAY_PROJECT_HISTO_HH
+
+# include <mln/core/image/image2d.hh>
+# include <mln/core/image/image3d.hh>
+# include <mln/core/image/dmorph/unproject_image.hh>
+# include <mln/fun/v2v/projection.hh>
+
+# include <mln/accu/image/init.hh>
+# include <mln/accu/image/take.hh>
+# include <mln/accu/image/to_result.hh>
+
+/// \file
+///
+/// \brief Allow the visualization of 3d histogram.
+/// The 3d histogram is projected in 2d such as the data in that direction
+/// are accumulated to the two others.
+
+namespace mln
+{
+
+ namespace display
+ {
+
+ // Forward declaration.
+ template <typename A, unsigned direction, typename V>
+ image2d<mln_result(A)>
+ project_histo(const image3d<V>& histo);
+
+#ifndef MLN_INCLUDE_ONLY
+
+ /// \brief Allow the visualization of 3d histogram.
+ ///
+ /// The 3d histogram is projected in 2d such as the data in that direction
+ /// are accumulated to the two others.
+ ///
+ /// Parameter A is the type of accumulator, for instance, accu::math::sum.
+ /// Parameter direction is the way of the projection, for instance blue one.
+ /// Parameter V is the value we use to accumulate information.
+ ///
+ /// \prameter[in] the histogram 3d.
+ /// \result the 2d projection of the 3d histogram.
+
+ template <typename A, unsigned direction, typename V>
+ image2d<mln_result(A)>
+ project_histo(const image3d<V>& histo)
+ {
+ typedef fun::v2v::projection<point3d,direction> t_projection;
+
+ image2d<A> histo_accu(histo.nrows(), histo.ncols());
+
+ accu::image::init(histo_accu);
+
+ accu::image::take(unproject(histo_accu,
+ histo.domain(),
+ t_projection()).rw(),
+ histo);
+
+ return accu::image::to_result(histo_accu);
+ }
+
+#endif // ! MLN_INCLUDE_ONLY
+
+
+ } // end of namespace mln::transform
+
+} // end of namespace mln
+
+
+#endif // ! MLN_DISPLAY_PROJECT_HISTO_HH
diff --git a/trunk/milena/sandbox/green/mln/fun/v2v/log.hh b/trunk/milena/sandbox/green/mln/fun/v2v/log.hh
new file mode 100644
index 0000000..d6905fa
--- /dev/null
+++ b/trunk/milena/sandbox/green/mln/fun/v2v/log.hh
@@ -0,0 +1,70 @@
+// Copyright (C) 2007 EPITA Research and Development Laboratory (LRDE)
+// Copyright (C) 2008 EPITA Research and Development Laboratory (LRDE)
+// Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE)
+//
+// This file is part of Olena.
+//
+// Olena is free software: you can redistribute it and/or modify it under
+// the terms of the GNU General Public License as published by the Free
+// Software Foundation, version 2 of the License.
+//
+// Olena 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 Olena. If not, see <http://www.gnu.org/licenses/>.
+//
+// As a special exception, you may use this file as part of a free
+// software project 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_FUN_V2V_LOG_HH
+# define MLN_FUN_V2V_LOG_HH
+
+/// \file
+///
+/// \brief Take the logarithm of each pixel value.
+
+namespace mln
+{
+
+ namespace fun
+ {
+
+ namespace v2v
+ {
+
+ /// \brief Take the logarithm of each pixel value.
+ ///
+ /// \ingroup modfunv2v
+
+ template <typename T>
+ struct log : Function_v2v< log<T> >
+ {
+ typedef T argument;
+ typedef T result;
+
+ result operator()(const argument v) const
+ {
+ mln_precondition(v > -1);
+
+ result tmp = std::log(v+1);
+
+ return tmp;
+ }
+ };
+
+ } // end of namespace mln::fun::v2v
+
+ } // end of namespace mln::fun
+
+} // end of namespace mln
+
+#endif // ! MLN_FUN_V2V_LOG_HH
--
1.5.6.5