
* green/mln/io/plot/save_image_sh.hh : New library file. * green/tests/io/plot/save_image_sh : New directory. * green/tests/io/plot/save_image_sh/Makefile : New Makefile. * green/tests/io/plot/save_image_sh/save_image_sh.cc : New unit tests. --- trunk/milena/sandbox/ChangeLog | 14 - .../sandbox/green/mln/io/plot/save_image_sh.hh | 856 ++++++++++++++++++++ .../green/tests/io/plot/save_image_sh/Makefile.am | 148 ++++ .../tests/io/plot/save_image_sh/save_image_sh.cc | 662 +++++++++++++++ 4 files changed, 1666 insertions(+), 14 deletions(-) create mode 100644 trunk/milena/sandbox/green/mln/io/plot/save_image_sh.hh create mode 100644 trunk/milena/sandbox/green/tests/io/plot/save_image_sh/Makefile.am create mode 100644 trunk/milena/sandbox/green/tests/io/plot/save_image_sh/save_image_sh.cc diff --git a/trunk/milena/sandbox/ChangeLog b/trunk/milena/sandbox/ChangeLog index 944d3af..df395ef 100644 --- a/trunk/milena/sandbox/ChangeLog +++ b/trunk/milena/sandbox/ChangeLog @@ -54,20 +54,6 @@ starting from (0,0). Initialize image extension correctly. -2009-09-15 Yann Jacquelet <jacquelet@lrde.epita.fr> - - Fix bug in histo3d_rgb (parameters'order in Point's constructor) - - * green/mln/accu/stat/histo3d_hsl.hh - (take) : Fix point's call. - * green/tests/accu/stat/histo3d_hsl/histo3d_hsl.cc - (conv) : Delete func. - * green/tests/accu/stat/histo3d_hsl/histo3d_hsl.cc - (mean_histo, var_histo2) : Update func. - - * green/mln/io/plot/save_histo_sh.hh : . - * green/mln/io/plot/save_image_sh.hh : New library file. - 2009-09-14 Yann Jacquelet <jacquelet@lrde.epita.fr> Fix bug in histo3d_rgb (parameters'order in Point's constructor) diff --git a/trunk/milena/sandbox/green/mln/io/plot/save_image_sh.hh b/trunk/milena/sandbox/green/mln/io/plot/save_image_sh.hh new file mode 100644 index 0000000..62f6652 --- /dev/null +++ b/trunk/milena/sandbox/green/mln/io/plot/save_image_sh.hh @@ -0,0 +1,856 @@ +// 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_IO_PLOT_SAVE_IMAGE_SH_HH +#define MLN_IO_PLOT_SAVE_IMAGE_SH_HH + +/// \file +/// +/// \brief Define some functions to export to gnuplot format as a script shell. +/// +/// Theses routines are dedicated to image visualization. The aim is to display +/// image whatever the value used as pixel. The behaviour of gnuplot is more +/// like xv than imageMagick. A Gnuplot script shell file is a text dump file +/// with a preambule to let gnuplot interpret data. As a script shell, you can +/// launch it (don't forget the permissions), and every thing is packed in it. +/// The script file call gnuplot in batch mode, the result window persists and +/// that's all. +/// + +#include <fstream> +#include <string> + +#include <mln/trace/entering.hh> +#include <mln/trace/exiting.hh> +#include <mln/core/macros.hh> +#include <mln/core/contract.hh> +#include <mln/core/image/image1d.hh> +#include <mln/core/image/image2d.hh> +#include <mln/core/image/image3d.hh> + +#include <mln/geom/min_row.hh> +#include <mln/geom/max_row.hh> +#include <mln/geom/min_col.hh> +#include <mln/geom/max_col.hh> +#include <mln/value/int_u.hh> +#include <mln/value/int_s.hh> +#include <mln/value/rgb.hh> +#include <mln/value/hsl.hh> +#include <mln/value/hsi.hh> + + +namespace mln +{ + + namespace io + { + + namespace plot + { + + /// \brief Save an image as a gnuplot script shell. + /// + /// Every thing is save. The image could be 1d, 2d or 3d. The value of + /// the pixels could be int_u<n>, int_s<n>, float, double, hsl_f, hsl_d, + /// hsi_f, hsi_d and rgb<n>. + /// + /// \param[in] img the image which contains the data to save. + /// \param[in] filename the name of the unix script shell. + /// \return the status of the opening file operation. + /// + /// The result depends on the permission to save the file with + /// filename parameter as unix path. The script shell file must have the + /// permission to execute (chmod 755). Launch the script shell to call + /// gnuplot in batchmode with fine parameters. + + template <typename I> + bool save_image_sh(const Image<I>& img, const std::string& filename); + + } // end of namespace mln::accu::stat + + } // end of namespace mln::accu + + + namespace io + { + + namespace plot + { + +#ifndef MLN_INCLUDE_ONLY + + + //------------------------------------------------------------------------ + // save_image_sh(const image2d<bool>&, const std::string&) + //------------------------------------------------------------------------ + + inline + bool save_image_sh_dispatch_overload(const image2d< bool >& img, + const std::string& filename) + { + trace::entering("mln::io::plot::save_image_sh<image2d<bool>>"); + mln_precondition(img.is_valid()); + + std::ofstream out(filename.c_str()); + bool result = !out.fail(); + unsigned min_row = geom::min_row(img); + unsigned max_row = geom::max_row(img); + unsigned min_col = geom::min_col(img); + unsigned max_col = geom::max_col(img); + + if (result) + { + mln_piter_(image2d< bool >) p(img.domain()); + + // Output data prelude (terminal X11, image). + out << "#!/bin/sh" << std::endl; + out << "####################################" << std::endl; + out << "# Columns = (x, y, bool) #" << std::endl; + out << "####################################" << std::endl; + out << std::endl; + out << "gnuplot <<EOF" << std::endl; + out << "set terminal x11 persist 1" << std::endl; + out << "set palette gray" << std::endl; + out << "set xrange [" << min_col; + out << ":" << max_col; + out << "]" << std::endl; + out << "set yrange [-" << max_row; + out << ":" << min_row; + out << "]" << std::endl; + out << "plot '-' using (\\$2):(-\\$1):(\\$3) with image" << std::endl; + + // Output data. + for_all(p) + { + out << p.row() << " "; + out << p.col() << " "; + out << img(p) << std::endl; + } + + // Close gnuplot data stream. + out << "e" << std::endl; + out << "EOF" << std::endl; + + out.close(); + } + else + { + std::cerr << "ERROR[mln::io::plot::save_image_sh]:" << filename; + std::cerr << " couldn't be opened !!" << std::endl; + } + + trace::exiting("mln::io::plot::save_image_sh<image2d<bool>>"); + return result; + } + + //------------------------------------------------------------------------ + // save_image_sh(const image2d<float>&, const std::string&) + //------------------------------------------------------------------------ + + inline + bool save_image_sh_dispatch_overload(const image2d<float>& img, + const std::string& filename) + { + trace::entering("mln::io::plot::save_image_sh<image2d<float>>"); + mln_precondition(img.is_valid()); + + std::ofstream out(filename.c_str()); + bool result = !out.fail(); + unsigned min_row = geom::min_row(img); + unsigned max_row = geom::max_row(img); + unsigned min_col = geom::min_col(img); + unsigned max_col = geom::max_col(img); + + if (result) + { + mln_piter_(image2d< float >) p(img.domain()); + + // Output data prelude (terminal X11, image). + out << "#!/bin/sh" << std::endl; + out << "####################################" << std::endl; + out << "# Columns = (x, y, float) #" << std::endl; + out << "####################################" << std::endl; + out << std::endl; + out << "gnuplot <<EOF" << std::endl; + out << "set terminal x11 persist 1" << std::endl; + out << "set palette gray" << std::endl; + out << "set xrange [" << min_col; + out << ":" << max_col; + out << "]" << std::endl; + out << "set yrange [-" << max_row; + out << ":" << min_row; + out << "]" << std::endl; + out << "plot '-' using (\\$2):(-\\$1):(\\$3) with image" << std::endl; + + // Output data. + for_all(p) + { + out << p.row() << " "; + out << p.col() << " "; + out << img(p) << std::endl; + } + + // Close gnuplot data stream. + out << "e" << std::endl; + out << "EOF" << std::endl; + + out.close(); + } + else + { + std::cerr << "ERROR[mln::io::plot::save_image_sh]:" << filename; + std::cerr << " couldn't be opened !!" << std::endl; + } + + trace::exiting("mln::io::plot::save_image_sh<image2d<float>>"); + return result; + } + + //------------------------------------------------------------------------ + // save_image_sh(const image2d<double>&, const std::string&) + //------------------------------------------------------------------------ + + inline + bool save_image_sh_dispatch_overload(const image2d<double>& img, + const std::string& filename) + { + trace::entering("mln::io::plot::save_image_sh<image2d<double>>"); + mln_precondition(img.is_valid()); + + std::ofstream out(filename.c_str()); + bool result = !out.fail(); + unsigned min_row = geom::min_row(img); + unsigned max_row = geom::max_row(img); + unsigned min_col = geom::min_col(img); + unsigned max_col = geom::max_col(img); + + if (result) + { + mln_piter_(image2d< double >) p(img.domain()); + + // Output data prelude (terminal X11, image). + out << "#!/bin/sh" << std::endl; + out << "####################################" << std::endl; + out << "# Columns = (x, y, double) #" << std::endl; + out << "####################################" << std::endl; + out << std::endl; + out << "gnuplot <<EOF" << std::endl; + out << "set terminal x11 persist 1" << std::endl; + out << "set palette gray" << std::endl; + out << "set xrange [" << min_col; + out << ":" << max_col; + out << "]" << std::endl; + out << "set yrange [-" << max_row; + out << ":" << min_row; + out << "]" << std::endl; + out << "plot '-' using (\\$2):(-\\$1):(\\$3) with image" << std::endl; + + // Output data. + for_all(p) + { + out << p.row() << " "; + out << p.col() << " "; + out << img(p) << std::endl; + } + + // Close gnuplot data stream. + out << "e" << std::endl; + out << "EOF" << std::endl; + + out.close(); + } + else + { + std::cerr << "ERROR[mln::io::plot::save_image_sh]:" << filename; + std::cerr << " couldn't be opened !!" << std::endl; + } + + trace::exiting("mln::io::plot::save_image_sh<image2d<double>>"); + return result; + } + + //------------------------------------------------------------------------ + // save_image_sh(const image2d<int_u<n>>&, const std::string&) + //------------------------------------------------------------------------ + + template <unsigned n> + inline + bool save_image_sh_dispatch_overload(const image2d<value::int_u<n> >& img, + const std::string& filename) + { + trace::entering("mln::io::plot::save_image_sh<image2d<int_u<n>>"); + mln_precondition(img.is_valid()); + + std::ofstream out(filename.c_str()); + bool result = !out.fail(); + unsigned min_row = geom::min_row(img); + unsigned max_row = geom::max_row(img); + unsigned min_col = geom::min_col(img); + unsigned max_col = geom::max_col(img); + + if (result) + { + mln_piter(image2d< value::int_u<n> >) p(img.domain()); + + // Output data prelude (terminal X11, image). + out << "#!/bin/sh" << std::endl; + out << "####################################" << std::endl; + out << "# Columns = (x, y, int_u<n>) #" << std::endl; + out << "####################################" << std::endl; + out << std::endl; + out << "gnuplot <<EOF" << std::endl; + out << "set terminal x11 persist 1" << std::endl; + out << "set palette gray" << std::endl; + out << "set xrange [" << min_col; + out << ":" << max_col; + out << "]" << std::endl; + out << "set yrange [-" << max_row; + out << ":" << min_row; + out << "]" << std::endl; + out << "plot '-' using (\\$2):(-\\$1):(\\$3) with image" << std::endl; + + // Output data. + for_all(p) + { + out << p.row() << " "; + out << p.col() << " "; + out << img(p) << std::endl; + } + + // Close gnuplot data stream. + out << "e" << std::endl; + out << "EOF" << std::endl; + + out.close(); + } + else + { + std::cerr << "ERROR[mln::io::plot::save_image_sh]:" << filename; + std::cerr << " couldn't be opened !!" << std::endl; + } + + trace::exiting("mln::io::plot::save_image_sh<image2d<int_u<n>>"); + return result; + } + + //------------------------------------------------------------------------ + // save_image_sh(const image2d<int_s<n>>&, const std::string&) + //------------------------------------------------------------------------ + + template <unsigned n> + inline + bool save_image_sh_dispatch_overload(const image2d<value::int_s<n> >& img, + const std::string& filename) + { + trace::entering("mln::io::plot::save_image_sh<image2d<int_s<n>>"); + mln_precondition(img.is_valid()); + + std::ofstream out(filename.c_str()); + bool result = !out.fail(); + unsigned min_row = geom::min_row(img); + unsigned max_row = geom::max_row(img); + unsigned min_col = geom::min_col(img); + unsigned max_col = geom::max_col(img); + + if (result) + { + mln_piter(image2d< value::int_s<n> >) p(img.domain()); + + // Output data prelude (terminal X11, image). + out << "#!/bin/sh" << std::endl; + out << "####################################" << std::endl; + out << "# Columns = (x, y, int_s<n>) #" << std::endl; + out << "####################################" << std::endl; + out << std::endl; + out << "gnuplot <<EOF" << std::endl; + out << "set terminal x11 persist 1" << std::endl; + out << "set palette gray" << std::endl; + out << "set xrange [" << min_col; + out << ":" << max_col; + out << "]" << std::endl; + out << "set yrange [-" << max_row; + out << ":" << min_row; + out << "]" << std::endl; + out << "plot '-' using 2:(-\\$1):3 with image" << std::endl; + + // Output data. + for_all(p) + { + out << p.row() << " "; + out << p.col() << " "; + out << img(p) << std::endl; + } + + // Close gnuplot data stream. + out << "e" << std::endl; + out << "EOF" << std::endl; + + out.close(); + } + else + { + std::cerr << "ERROR[mln::io::plot::save_image_sh]:" << filename; + std::cerr << " couldn't be opened !!" << std::endl; + } + + trace::exiting("mln::io::plot::save_image_sh<image2d<int_s<n>>"); + return result; + } + + //------------------------------------------------------------------------ + // save_image_sh(const image2d<rgb<n>>&, const std::string&) + //------------------------------------------------------------------------ + + template <unsigned n> + inline + bool save_image_sh_dispatch_overload(const image2d<value::rgb<n> >& img, + const std::string& filename) + { + trace::entering("mln::io::plot::save_image_sh<image2d<rgb<n>>"); + mln_precondition(img.is_valid()); + + std::ofstream out(filename.c_str()); + bool result = !out.fail(); + unsigned min_row = geom::min_row(img); + unsigned max_row = geom::max_row(img); + unsigned min_col = geom::min_col(img); + unsigned max_col = geom::max_col(img); + + if (result) + { + mln_piter(image2d< value::int_u<n> >) p(img.domain()); + + // Output data prelude (terminal X11, image). + out << "#!/bin/sh" << std::endl; + out << "####################################" << std::endl; + out << "# Columns = (x, y, r, g, b) #" << std::endl; + out << "####################################" << std::endl; + out << std::endl; + out << "gnuplot <<EOF" << std::endl; + out << "set terminal x11 persist 1" << std::endl; + out << "set xrange [" << min_col; + out << ":" << max_col; + out << "]" << std::endl; + out << "set yrange [-" << max_row; + out << ":" << min_row; + out << "]" << std::endl; + out << "plot '-' using 2:(-\\$1):3:4:5 with rgbimage" << std::endl; + + // Output data. + for_all(p) + { + out << p.row() << " "; + out << p.col() << " "; + out << img(p).red() << " "; + out << img(p).green() << " "; + out << img(p).blue() << std::endl; + } + + // Close gnuplot data stream. + out << "e" << std::endl; + out << "EOF" << std::endl; + + out.close(); + } + else + { + std::cerr << "ERROR[mln::io::plot::save_image_sh]:" << filename; + std::cerr << " couldn't be opened !!" << std::endl; + } + + trace::exiting("mln::io::plot::save_image_sh<image2d<rgb<n>>"); + return result; + } + + //------------------------------------------------------------------------ + // save_image_sh(const image2d<hsl_<T,T,T>>&, const std::string&) + //------------------------------------------------------------------------ + + template <typename T> + inline + bool save_image_sh(const image2d<mln::value::hsl_<T,T,T> >& img, + const std::string& filename) + { + trace::entering("mln::io::plot::save_image_sh<image2d<hsl>>"); + mln_precondition(img.is_valid()); + + std::ofstream out(filename.c_str()); + bool result = !out.fail(); + unsigned min_row = geom::min_row(img); + unsigned max_row = geom::max_row(img); + unsigned min_col = geom::min_col(img); + unsigned max_col = geom::max_col(img); + + typedef mln::value::hsl_<T,T,T> hsl; + + if (result) + { + mln_piter(image2d< hsl >) p(img.domain()); + + // Output data prelude (terminal X11, image). + out << "#!/bin/sh" << std::endl; + out << "####################################" << std::endl; + out << "# Columns = (x, y, val) #" << std::endl; + out << "####################################" << std::endl; + out << std::endl; + out << "gnuplot <<EOF" << std::endl; + out << "q(l,s) = (l < 0.5)? (l*(1+s)) : (l+s-(l*s))" << std::endl; + out << "p(l,s) = (2.0 * l - q(l,s))" << std::endl; + out << "n(x) = (x < 0)?(x+1) : (x > 1)?(x-1) : (x)"<< std::endl; + out << std::endl; + out << "c(p,q,t) = (t < (1.0/6.0))?(p+(q-p)*6.0*t):\\" << std::endl; + out << " (t < (1.0/2.0))?(q) :\\" << std::endl; + out << " (t < (2.0/3.0))?(p+(q-p)*6.0*((2.0/3.0)-t)):\\" << std::endl; + out << " (p)" << std::endl; + out << std::endl; + out << "r(h,s,l) = c(p(l,s),q(l,s),n(h/360.0+1.0/3.0))"<< std::endl; + out << "g(h,s,l) = c(p(l,s),q(l,s),n(h/360.0))" << std::endl; + out << "b(h,s,l) = c(p(l,s),q(l,s),n(h/360.0-1.0/3.0))"<< std::endl; + out << std::endl; + out << "set terminal x11 persist 1" << std::endl; + out << "set palette gray" << std::endl; + out << "set xrange [" << min_col; + out << ":" << max_col; + out << "]" << std::endl; + out << "set yrange [-" << max_row; + out << ":" << min_row; + out << "]" << std::endl; + out << "plot '-' using 2:(-\\$1):(r(\\$3,\\$4,\\$5)):\\" << std::endl; + out << " (g(\\$3,\\$4,\\$5)):\\" << std::endl; + out << " (b(\\$3,\\$4,\\$5)) with rgbimage"<< std::endl; + + // Output data. + for_all(p) + { + out << p.row() << " "; + out << p.col() << " "; + out << img(p).hue() << " "; + out << img(p).sat() << " "; + out << img(p).lum() << std::endl; + } + + // Close gnuplot data stream. + out << "e" << std::endl; + out << "EOF" << std::endl; + + out.close(); + } + else + { + std::cerr << "ERROR[mln::io::plot::save_image_sh]:" << filename; + std::cerr << " couldn't be opened !!" << std::endl; + } + + trace::exiting("mln::io::plot::save_image_sh<image2d<hsl>>"); + return result; + } + + //------------------------------------------------------------------------ + // save_image_sh(const image2d<hsi_<T,T,T>>&, const std::string&) + //------------------------------------------------------------------------ + + template <typename T> + inline + bool save_image_sh(const image2d<mln::value::hsi_<T,T,T> >& img, + const std::string& filename) + { + trace::entering("mln::io::plot::save_image_sh<2d>vf"); + mln_precondition(img.is_valid()); + + std::ofstream out(filename.c_str()); + bool result = !out.fail(); + unsigned min_row = geom::min_row(img); + unsigned max_row = geom::max_row(img); + unsigned min_col = geom::min_col(img); + unsigned max_col = geom::max_col(img); + + typedef mln::value::hsi_<T,T,T> hsi; + + if (result) + { + mln_piter(image2d< hsi >) p(img.domain()); + + // Output data prelude (terminal X11, image). + out << "#!/bin/sh" << std::endl; + out << "####################################" << std::endl; + out << "# Columns = (x, y, val) #" << std::endl; + out << "####################################" << std::endl; + out << std::endl; + out << "gnuplot <<EOF" << std::endl; + out << "teta(h) = (h/180.0) * pi" << std::endl; + out << "alpha(s,h) = s*cos(teta(h))" << std::endl; + out << "beta(s,h) = s*sin(teta(h))" << std::endl; + out << "n(x) = (x < 0)?(x+1) : (x > 1)?(x-1) : (x)"<< std::endl; + out << std::endl; + out << "c(p,q,t) = (t < (1.0/6.0))?(p+(q-p)*6.0*t):\\" << std::endl; + out << " (t < (1.0/2.0))?(q) :\\" << std::endl; + out << " (t < (2.0/3.0))?(p+(q-p)*6.0*((2.0/3.0)-t)):\\" << std::endl; + out << " (p)" << std::endl; + out << std::endl; + out << "r(h,s,i) = (sqrt(3.0)/3.0) * i \\" << std::endl; + out << " + (2.0/(sqrt(6.0))) * beta(s,h)" << std::endl; + out << "g(h,s,i) = (sqrt(3.0)/3.0) * i \\" << std::endl; + out << " + (2.0/(sqrt(2.0))) * alpha(s,h) \\"<< std::endl; + out << " - (1.0/(sqrt(6.0))) * beta(s,h)" << std::endl; + out << "b(h,s,i) = (sqrt(3.0)/3.0) * i \\" << std::endl; + out << " - (2.0/(sqrt(2.0))) * alpha(s,h) \\"<< std::endl; + out << " - (1.0/(sqrt(6.0))) * beta(s,h)" << std::endl; + out << std::endl; + out << "set terminal x11 persist 1" << std::endl; + out << "set palette gray" << std::endl; + out << "set xrange [" << min_col; + out << ":" << max_col; + out << "]" << std::endl; + out << "set yrange [-" << max_row; + out << ":" << min_row; + out << "]" << std::endl; + out << "plot '-' using 2:(-\\$1):(r(\\$3,\\$4,\\$5)):\\" << std::endl; + out << " (g(\\$3,\\$4,\\$5)):\\" << std::endl; + out << " (b(\\$3,\\$4,\\$5)) with rgbimage"<< std::endl; + + // Output data. + for_all(p) + { + out << p.row() << " "; + out << p.col() << " "; + out << img(p).hue() << " "; + out << img(p).sat() << " "; + out << img(p).inty() << std::endl; + } + + // Close gnuplot data stream. + out << "e" << std::endl; + out << "EOF" << std::endl; + + out.close(); + } + else + { + std::cerr << "ERROR[mln::io::plot::save_image_sh]:" << filename; + std::cerr << " couldn't be opened !!" << std::endl; + } + + trace::exiting("mln::io::plot::save_image_sh<2d>vf"); + return result; + } + + //------------------------------------------------------------------------ + // save_image_sh(const image2d<I>&, const std::string&) + //------------------------------------------------------------------------ + + template <typename I> + inline + bool save_image_sh_dispatch_overload(const image2d<I>& img, + const std::string& filename) + { + trace::entering("mln::io::plot::save_image_sh<image2d<I>>"); + mln_precondition(img.is_valid()); + + std::ofstream out(filename.c_str()); + bool result = !out.fail(); + + if (result) + { + mln_piter(image2d<I>) p(img.domain()); + + // Output data prelude (terminal X11, image). + out << "#!/bin/sh" << std::endl; + out << "####################################" << std::endl; + out << "# Columns = (x, y, builtin) #" << std::endl; + out << "####################################" << std::endl; + out << std::endl; + out << "gnuplot <<EOF" << std::endl; + out << "set terminal x11 persist 1" << std::endl; + out << "set pm3d" << std::endl; + out << "set dgrid3d 20,20,2" << std::endl; + out << "splot '-' with lines" << std::endl; + + // Output data. + for_all(p) + { + out << p.col() << " "; + out << p.row() << " "; + out << img(p) << std::endl; + } + + // Close gnuplot data stream. + out << "e" << std::endl; + out << "EOF" << std::endl; + + out.close(); + } + else + { + std::cerr << "ERROR[mln::io::plot::save_image_sh]:"<< filename; + std::cerr << " couldn't be opened !!" << std::endl; + } + + trace::exiting("mln::io::plot::save_image_sh<image2d<I>>"); + return result; + } + + //------------------------------------------------------------------------ + // save_image_sh(const image1d<I>&, const std::string&) + //------------------------------------------------------------------------ + + template <typename I> + inline + bool save_image_sh_dispatch_overload(const image1d<I>& img, + const std::string& filename) + { + trace::entering("mln::io::plot::save_image_sh<image1d<I>>"); + mln_precondition(img.is_valid()); + + std::ofstream out(filename.c_str()); + bool result = !out.fail(); + + if (result) + { + mln_piter(image1d<I>) p(img.domain()); + + // Output data prelude (terminal X11, impulse). + out << "#!/bin/sh" << std::endl; + out << "##########################" << std::endl; + out << "# Two columns = (x, val) #" << std::endl; + out << "##########################" << std::endl; + out << std::endl; + out << "gnuplot <<EOF" << std::endl; + out << "set terminal x11 persist 1" << std::endl; + out << "plot '-' with impulse" << std::endl; + + // Output data. + for_all(p) + { + out << p.ind() << " "; + out << img(p) << std::endl; + } + + // Close gnuplot data stream. + out << "e" << std::endl; + out << "EOF" << std::endl; + + out.close(); + } + else + { + std::cerr << "ERROR[mln::io::plot::save_image_sh]:"<< filename; + std::cerr << " couldn't be opened !!" << std::endl; + } + + trace::exiting("mln::io::plot::save_image_sh<image1d<I>>"); + return result; + } + + //------------------------------------------------------------------------ + // save_image_sh(const image3d<I>&, const std::string&) + //------------------------------------------------------------------------ + + + template <typename I> + inline + bool save_image_sh(const image3d<I>& img, const std::string& filename) + { + trace::entering("mln::io::plot::save_image_sh<image3d<I>>"); + mln_precondition(img.is_valid()); + + std::ofstream out(filename.c_str()); + bool result = !out.fail(); + + if (result) + { + mln_piter(image3d<I>) p(img.domain()); + + // Output data prelude (terminal X11, pointtype 0). + out << "#!/bin/sh" << std::endl; + out << "####################################" << std::endl; + out << "# Columns = (x, y, z, val) #" << std::endl; + out << "####################################" << std::endl; + out << std::endl; + out << "gnuplot <<EOF" << std::endl; + out << "set terminal x11 persist 1" << std::endl; + out << "splot '-' with points palette pointtype 7" << std::endl; + + // Output data. + for_all(p) + { + out << p.row() << " "; + out << p.col() << " "; + out << p.sli() << " "; + out << img(p) << std::endl; + } + + // Close gnuplot data stream. + out << "e" << std::endl; + out << "EOF" << std::endl; + + out.close(); + } + else + { + std::cerr << "ERROR[mln::io::plot::save_image_sh]:"<< filename; + std::cerr << " couldn't be opened !!" << std::endl; + } + + trace::exiting("mln::io::plot::save_image_sh<image3d<I>>"); + return result; + } + + //------------------------------------------------------------------------ + // Facade. + //------------------------------------------------------------------------ + + template <typename I> + inline + bool save_image_sh_dispatch(const Image<I>& img, + const std::string& filename) + { + return save_image_sh_dispatch_overload(exact(img), filename); + } + + template <typename I> + inline + bool save_image_sh(const Image<I>& img, const std::string& filename) + { + trace::entering("mln::io::plot::save_image_sh"); + + bool result = save_image_sh_dispatch(img, filename); + + trace::exiting("mln::io::plot::save_image_sh"); + return result; + } + + +#endif // ! MLN_INCLUDE_ONLY + + + } // end of namespace mln::accu::stat + + } // end of namespace mln::accu + +} // end of namespace mln + +#endif // ! MLN_IO_PLOT_SAVE_IMAGE_SH_HH diff --git a/trunk/milena/sandbox/green/tests/io/plot/save_image_sh/Makefile.am b/trunk/milena/sandbox/green/tests/io/plot/save_image_sh/Makefile.am new file mode 100644 index 0000000..d970989 --- /dev/null +++ b/trunk/milena/sandbox/green/tests/io/plot/save_image_sh/Makefile.am @@ -0,0 +1,148 @@ +# +# Generic Makefile +# + +######### +# TOOLS # +######### + +INCLUDES= -I$(HOME)/svn/oln/trunk/milena/sandbox/green +CXXFLAGS= -ggdb -O0 -Wall -W -pedantic -ansi -pipe $(INCLUDES) +ECHO= echo +RM= rm +MKDIR= mkdir -p +CP= cp + +SOURCE_PATTERN= green/tests +BUILD__PATTERN= green/build/tests + + +ifeq ($(findstring $(BUILD__PATTERN),$(PWD)), $(BUILD__PATTERN)) +# Case where make is done from build directory. +SOURCE_DIR= $(subst $(BUILD__PATTERN),$(SOURCE_PATTERN),$(PWD)) +BUILD__DIR= $(PWD) +else +# Case where make is done from source directory. +SOURCE_DIR= $(PWD) +BUILD__DIR= $(subst $(SOURCE_PATTERN),$(BUILD__PATTERN),$(PWD)) +endif + +SRC= $(notdir $(wildcard $(SOURCE_DIR)/*.cc)) +OLD= $(notdir $(wildcard $(SOURCE_DIR)/*~)) +OBJ= $(patsubst %.cc,%.o,$(SRC)) +SOURCE_MAKEFILE=Makefile.am +BUILD__MAKEFILE=Makefile +TARGET_FILE= $(notdir $(PWD)) +SOURCE_FILES= $(notdir $(wildcard $(SOURCE_DIR)/*.*)) +BUILD__FILES= $(filter-out $(SRC) $(SOURCE_MAKEFILE), $(SOURCE_FILES)) + +BUILD__F_PATH= $(addprefix $(BUILD__DIR)/,$(BUILD__FILES)) +SOURCE_F_PATH= $(addprefix $(SOURCE_DIR)/,$(SOURCE_FILES)) + +BUILD__M_PATH= $(addprefix $(BUILD__DIR)/,$(BUILD__MAKEFILE)) +SOURCE_M_PATH= $(addprefix $(SOURCE_DIR)/,$(SOURCE_MAKEFILE)) + +TARGET_F_PATH= $(addprefix $(BUILD__DIR)/,$(TARGET_FILE)) +OBJ_F_PATH= $(addprefix $(BUILD__DIR)/,$(OBJ)) +SRC_F_PATH= $(addprefix $(SOURCE_DIR)/,$(SRC)) +OLD_F_PATH= $(addprefix $(SOURCE_DIR)/,$(OLD)) + +############# +# BOOTSTRAP # +############# + + +bootstrap: $(BUILD__DIR) $(BUILD__F_PATH) $(BUILD__M_PATH) + +# Create, if nessary, the destination directory +$(BUILD__DIR): + $(MKDIR) $(BUILD__DIR) + +# Copy, if nessary, all the files, except the Makefile.am +$(BUILD__F_PATH): $(SOURCE_F_PATH) + $(CP) $(addprefix $(SOURCE_DIR)/,$(@F)) $@ + +# Copy if nessary, the Makefile.am into Makefile +$(BUILD__M_PATH): $(SOURCE_M_PATH) + $(CP) $(SOURCE_M_PATH) $(BUILD__M_PATH) + + +####### +# ALL # +####### + +# We assume that the call is done from the build directory. +# With the directive vpath, hidden files are found in the source directory. + +all: $(TARGET_F_PATH) + + +$(TARGET_F_PATH): $(OBJ_F_PATH) + $(LINK.cc) $< $(LOADLIBES) $(LDLIBS) -o $@ + +$(OBJ_F_PATH):$(SRC_F_PATH) + $(COMPILE.cc) $(OUTPUT_OPTION) $< + + +######### +# CLEAN # +######### + +# Force every time the deletion +clean: clean_target clean_obj clean_dst clean_old #clean_make + + +clean_target: + -@$(RM) $(TARGET_F_PATH) &> /dev/null + +clean_obj: + -@$(RM) $(OBJ_F_PATH) &> /dev/null + +clean_dst: + -@$(RM) $(BUILD_F_PATH) &> /dev/null + +clean_make: + -@$(RM) $(BUILD_M_PATH) &> /dev/null + +clean_old: + -@$(RM) $(OLD_F_PATH) &> /dev/null + + +######### +# PRINT # +######### + +print: print_tools print_bootstrap + +print_tools: + @$(ECHO) "HOME = $(HOME)" + @$(ECHO) "INCLUDES = $(INCLUDES)" + @$(ECHO) "CXXFLAGS = $(CXXFLAGS)" + @$(ECHO) "ECHO = $(ECHO)" + @$(ECHO) "RM = $(RM)" + @$(ECHO) "MKDIR = $(MKDIR)" + @$(ECHO) "CP = $(CP)" + @$(ECHO) + +print_bootstrap: + @$(ECHO) "PWD = $(PWD)" + @$(ECHO) "SOURCE_PATTERN = $(SOURCE_PATTERN)" + @$(ECHO) "BUILD__PATTERN = $(BUILD__PATTERN)" + @$(ECHO) "SOURCE_DIR = $(SOURCE_DIR)" + @$(ECHO) "BUILD__DIR = $(BUILD__DIR)" + @$(ECHO) "SOURCE_MAKEFILE = $(SOURCE_MAKEFILE)" + @$(ECHO) "BUILD__MAKEFILE = $(BUILD__MAKEFILE)" + @$(ECHO) "TARGET_FILE = $(TARGET_FILE)" + @$(ECHO) "SOURCE_FILES = $(SOURCE_FILES)" + @$(ECHO) "SOURCE_F_PATH = $(SOURCE_F_PATH)" + @$(ECHO) "BUILD__FILES = $(BUILD__FILES)" + @$(ECHO) "BUILD__F_PATH = $(BUILD__F_PATH)" + @$(ECHO) "BUILD__M_PATH = $(BUILD__M_PATH)" + @$(ECHO) "SOURCE_M_PATH = $(SOURCE_M_PATH)" + @$(ECHO) "SRC = $(SRC)" + @$(ECHO) "OBJ = $(OBJ)" + @$(ECHO) "OLD = $(OLD)" + @$(ECHO) "SRC_F_PATH = $(SRC_F_PATH)" + @$(ECHO) "OBJ_F_PATH = $(OBJ_F_PATH)" + @$(ECHO) "OLD_F_PATH = $(OLD_F_PATH)" + @$(ECHO) diff --git a/trunk/milena/sandbox/green/tests/io/plot/save_image_sh/save_image_sh.cc b/trunk/milena/sandbox/green/tests/io/plot/save_image_sh/save_image_sh.cc new file mode 100644 index 0000000..c9c817b --- /dev/null +++ b/trunk/milena/sandbox/green/tests/io/plot/save_image_sh/save_image_sh.cc @@ -0,0 +1,662 @@ +/// TEST SAVE_IMAGE_SH.HH + +#include <iostream> +#include <limits.h> + +#include <mln/img_path.hh> +#include <mln/value/int_u8.hh> +#include <mln/value/int_u16.hh> +#include <mln/value/int_s8.hh> +#include <mln/value/int_s16.hh> +#include <mln/value/rgb8.hh> +#include <mln/value/rgb16.hh> +#include <mln/value/hsl.hh> +#include <mln/value/hsi.hh> +#include <mln/util/array.hh> +#include <mln/core/macros.hh> +#include <mln/core/image/image1d.hh> +#include <mln/core/image/image2d.hh> +#include <mln/core/image/image3d.hh> + +#include <mln/fun/v2v/rgb_to_hsl.hh> +#include <mln/fun/v2v/rgb_to_hsi.hh> +#include <mln/io/pgm/load.hh> +#include <mln/io/pgm/save.hh> +#include <mln/io/pgms/load.hh> +#include <mln/io/ppm/load.hh> +#include <mln/io/pbm/load.hh> +#include <mln/io/plot/save_image_sh.hh> +#include <mln/io/plot/load.hh> + +#include <mln/data/transform.hh> +#include <mln/literal/colors.hh> +#include <mln/debug/println.hh> + +//------------------------------------------------------------------------------ +// TEST TO SAVE UNSIGNED 8 BITS 1D GRAYSCALE IMAGE +//------------------------------------------------------------------------------ + +mln::image1d<mln::value::int_u8> +convert_array_to_image1d(const mln::util::array<mln::value::int_u8>& array) +{ + typedef mln::value::int_u8 int_u8; + typedef mln::image1d<int_u8> image1d; + typedef mln::util::array<int_u8> array1d; + + image1d result(array.nelements()); + + mln_piter_(image1d) p(result.domain()); + mln_eiter_(array1d) e(array); + + for_all_2(e, p) + { + result(p) = e.element(); + } + + return result; +} + +void test_save_image_sh_1d() +{ + typedef mln::value::int_u8 int_u8; + typedef mln::image1d<int_u8> image1d; + typedef mln::util::array<int_u8> array1d; + + image1d img; + array1d array; + + mln::io::plot::load(array, OLENA_IMG_PATH"/fly.plot"); + img = convert_array_to_image1d(array); + + // Save it were it is impossible to do it !! + bool ref_false = mln::io::plot::save_image_sh(img, "/dev/null/fly.sh"); + + // Save it where we have the full permissions. + bool ref_true = mln::io::plot::save_image_sh(img, "fly1d.sh"); + + mln_assertion(true == ref_true); + mln_assertion(false == ref_false); + std::cout << "save image 1d : ok" << std::endl; +} + +//------------------------------------------------------------------------------ +// Test to save image2d<bool> +//------------------------------------------------------------------------------ + + +void test_save_image2d_bool() +{ + typedef mln::image2d<bool> image2d_bool; + const char* null_lena = "/dev/null/lena2d_bool.sh"; + const char* lena = "lena2d_bool.sh"; + + image2d_bool img_bool; + + mln::io::pbm::load(img_bool, OLENA_IMG_PATH"/lena.pbm"); + + // Save it were it is impossible to do it !! + bool ref_false = mln::io::plot::save_image_sh(img_bool, null_lena); + + // Save it where we have the full permissions. + bool ref_true = mln::io::plot::save_image_sh(img_bool, lena); + + mln_assertion(true == ref_true); + mln_assertion(false == ref_false); + std::cout << "save image2d<bool>: ok" << std::endl; +} + +//------------------------------------------------------------------------------ +// Test to save image2d<int_u8> +//------------------------------------------------------------------------------ + + +void test_save_image2d_int_u8() +{ + typedef mln::value::int_u8 int_u8; + typedef mln::image2d<int_u8> image2d_int_u8; + const char* null_lena = "/dev/null/lena2d_int_u8.sh"; + const char* lena = "lena2d_int_u8.sh"; + + image2d_int_u8 img_int_u8; + + mln::io::pgm::load(img_int_u8, OLENA_IMG_PATH"/lena.pgm"); + + // Save it were it is impossible to do it !! + bool ref_false = mln::io::plot::save_image_sh(img_int_u8, null_lena); + + // Save it where we have the full permissions. + bool ref_true = mln::io::plot::save_image_sh(img_int_u8, lena); + + mln_assertion(true == ref_true); + mln_assertion(false == ref_false); + std::cout << "save image2d<int_u8>: ok" << std::endl; +} + +//------------------------------------------------------------------------------ +// Test to save image2d<int_u16> +//------------------------------------------------------------------------------ + +struct int_u16_to_int_u8 : mln::Function_v2v< int_u16_to_int_u8 > +{ + typedef mln::value::int_u16 argument; + typedef mln::value::int_u8 result; + + result operator()(const argument& c) const + { + result res(c / 256); + + return res; + } +}; + +void test_save_image2d_int_u16() +{ + typedef mln::value::int_u8 int_u8; + typedef mln::value::int_u16 int_u16; + typedef mln::image2d<int_u8> image2d_int_u8; + typedef mln::image2d<int_u16> image2d_int_u16; + const char* null_lena = "/dev/null/lena2d_int_u16.sh"; + const char* lena = "lena2d_int_u16.sh"; + + image2d_int_u8 img_int_u8; + image2d_int_u16 img_int_u16; + + mln::io::pgm::load(img_int_u16, OLENA_IMG_PATH"/lena_16_gnuplot.pgm"); + //img_int_u8 = mln::data::transform(img_int_u16, int_u16_to_int_u8()); + //mln::io::pgm::save(img_int_u8, "lena_8.pgm"); + + // Save it were it is impossible to do it !! + bool ref_false = mln::io::plot::save_image_sh(img_int_u16, null_lena); + + // Save it where we have the full permissions. + bool ref_true = mln::io::plot::save_image_sh(img_int_u16, lena); + + mln_assertion(true == ref_true); + mln_assertion(false == ref_false); + std::cout << "save image2d<int_u16> : ok" << std::endl; +} + + +//------------------------------------------------------------------------------ +// Test to save image2d<int_s8> +//------------------------------------------------------------------------------ + +struct int_u8_to_int_s8 : mln::Function_v2v< int_u8_to_int_s8 > +{ + typedef mln::value::int_u8 argument; + typedef mln::value::int_s8 result; + + result operator()(const argument& c) const + { + argument tmp_int_u8; + result tmp_int_s8; + result res; + + // Note : -127 <= int_s8 <= +127 + if (UCHAR_MAX == c) + { + res = SCHAR_MAX; + } + else if (SCHAR_MAX < c) + { + tmp_int_u8 = c - SCHAR_MAX; + res = static_cast<mln::value::int_s8>(tmp_int_u8); + } + else + { + tmp_int_s8 = static_cast<mln::value::int_s8>(c) - SCHAR_MAX; + res = tmp_int_s8; + } + + return res; + } +}; + + +void test_save_image2d_int_s8() +{ + typedef mln::value::int_s8 int_s8; + typedef mln::value::int_u8 int_u8; + typedef mln::image2d<int_s8> image2d_int_s8; + typedef mln::image2d<int_u8> image2d_int_u8; + const char* null_lena = "/dev/null/lena2d_int_s8.sh"; + const char* lena = "lena2d_int_s8.sh"; + + image2d_int_s8 img_int_s8; + image2d_int_u8 img_int_u8; + + mln::io::pgm::load(img_int_u8, OLENA_IMG_PATH"/lena.pgm"); + img_int_s8 = mln::data::transform(img_int_u8, int_u8_to_int_s8()); + + // Save it were it is impossible to do it !! + bool ref_false = mln::io::plot::save_image_sh(img_int_s8, null_lena); + + // Save it where we have the full permissions. + bool ref_true = mln::io::plot::save_image_sh(img_int_s8, lena); + + mln_assertion(true == ref_true); + mln_assertion(false == ref_false); + std::cout << "save image2d<int_s8>: ok" << std::endl; +} + +//------------------------------------------------------------------------------ +// Test to save image2d<int_s16> +//------------------------------------------------------------------------------ + +struct int_u16_to_int_s16 : mln::Function_v2v< int_u16_to_int_s16 > +{ + typedef mln::value::int_u16 argument; + typedef mln::value::int_s16 result; + + result operator()(const argument& c) const + { + argument tmp_int_u16; + result tmp_int_s16; + result res; + + if (USHRT_MAX == c) + { + res = SHRT_MAX; + } + else if (SHRT_MAX < c) + { + tmp_int_u16 = c - SHRT_MAX; + res = static_cast<mln::value::int_s16>(tmp_int_u16); + } + else + { + tmp_int_s16 = static_cast<mln::value::int_s16>(c) - SHRT_MAX; + res = tmp_int_s16; + } + + return res; + } +}; + +void test_save_image2d_int_s16() +{ + typedef mln::value::int_u16 int_u16; + typedef mln::value::int_s16 int_s16; + typedef mln::image2d<int_s16> image2d_int_s16; + typedef mln::image2d<int_u16> image2d_int_u16; + const char* null_lena = "/dev/null/lena2d_int_s16.sh"; + const char* lena = "lena2d_int_s16.sh"; + + image2d_int_s16 img_int_s16; + image2d_int_u16 img_int_u16; + + mln::io::pgm::load(img_int_u16, OLENA_IMG_PATH"/lena_16_gnuplot.pgm"); + img_int_s16 = mln::data::transform(img_int_u16, int_u16_to_int_s16()); + + // Save it were it is impossible to do it !! + bool ref_false = mln::io::plot::save_image_sh(img_int_s16, null_lena); + + // Save it where we have the full permissions. + bool ref_true = mln::io::plot::save_image_sh(img_int_s16, lena); + + mln_assertion(true == ref_true); + mln_assertion(false == ref_false); + std::cout << "save image2d<int_s16> : ok" << std::endl; +} + + +//------------------------------------------------------------------------------ +// Test to save image2d<float> +//------------------------------------------------------------------------------ + +struct int_u8_to_float : mln::Function_v2v< int_u8_to_float > +{ + typedef mln::value::int_u8 argument; + typedef float result; + + result operator()(const argument& c) const + { + result res = static_cast<float>(c)/3; + + return res; + } +}; + +void test_save_image2d_float() +{ + typedef mln::value::int_u8 int_u8; + typedef mln::image2d<float> image2d_float; + typedef mln::image2d<int_u8> image2d_int_u8; + const char* null_lena = "/dev/null/lena2d_float.sh"; + const char* lena = "lena2d_float.sh"; + + image2d_int_u8 img_int_u8; + image2d_float img_float; + + mln::io::pgm::load(img_int_u8, OLENA_IMG_PATH"/lena.pgm"); + img_float = mln::data::transform(img_int_u8, int_u8_to_float()); + + // Save it were it is impossible to do it !! + bool ref_false = mln::io::plot::save_image_sh(img_float, null_lena); + + // Save it where we have the full permissions. + bool ref_true = mln::io::plot::save_image_sh(img_float, lena); + + mln_assertion(true == ref_true); + mln_assertion(false == ref_false); + std::cout << "save image2d<float> : ok" << std::endl; +} + +//------------------------------------------------------------------------------ +// Test to save image2d<double> +//------------------------------------------------------------------------------ + +struct int_u16_to_double : mln::Function_v2v< int_u16_to_double > +{ + typedef mln::value::int_u16 argument; + typedef double result; + + result operator()(const argument& c) const + { + result res = static_cast<float>(c)/256; + + return res; + } +}; + +void test_save_image2d_double() +{ + typedef mln::value::int_u16 int_u16; + typedef mln::image2d<double> image2d_double; + typedef mln::image2d<int_u16> image2d_int_u16; + const char* null_lena = "/dev/null/lena2d_double.sh"; + const char* lena = "lena2d_double.sh"; + + image2d_int_u16 img_int_u16; + image2d_double img_double; + + mln::io::pgm::load(img_int_u16, OLENA_IMG_PATH"/lena_16_gnuplot.pgm"); + img_double = mln::data::transform(img_int_u16, int_u16_to_double()); + + // Save it were it is impossible to do it !! + bool ref_false = mln::io::plot::save_image_sh(img_double, null_lena); + + // Save it where we have the full permissions. + bool ref_true = mln::io::plot::save_image_sh(img_double, lena); + + mln_assertion(true == ref_true); + mln_assertion(false == ref_false); + std::cout << "save image2d<double> : ok" << std::endl; +} + + + +//------------------------------------------------------------------------------ +// Test to save image2d<rgb8> +//------------------------------------------------------------------------------ + +void test_save_image2d_rgb8() +{ + typedef mln::value::rgb8 rgb8; + typedef mln::image2d<rgb8> image2d_rgb8; + const char* null_lena = "/dev/null/lena2d_rgb8.sh"; + const char* lena = "lena2d_rgb8.sh"; + + image2d_rgb8 img_rgb8; + + mln::io::ppm::load(img_rgb8, OLENA_IMG_PATH"/lena.ppm"); + + // Save it were it is impossible to do it !! + bool ref_false = mln::io::plot::save_image_sh(img_rgb8, null_lena); + + // Save it where we have the full permissions. + bool ref_true = mln::io::plot::save_image_sh(img_rgb8, lena); + + mln_assertion(true == ref_true); + mln_assertion(false == ref_false); + std::cout << "save image2d<rgb8> : ok" << std::endl; +} + +//------------------------------------------------------------------------------ +// Test to save image2d<rgb16> +//------------------------------------------------------------------------------ + +struct rgb16_to_int_u16 : mln::Function_v2v< rgb16_to_int_u16 > +{ + typedef mln::value::rgb16 argument; + typedef mln::value::int_u16 result; + + + result operator()(const argument& c) const + { + result res((c.red() + c.green() + c.blue())/3); + + return res; + } +}; + +void test_save_image2d_rgb16() +{ + typedef mln::value::rgb16 rgb16; + typedef mln::value::int_u16 int_u16; + typedef mln::image2d<rgb16> image2d_rgb16; + typedef mln::image2d<int_u16> image2d_int_u16; + const char* null_lena = "/dev/null/lena2d_rgb16.sh"; + const char* lena = "lena2d_rgb16.sh"; + //const char* lena2 = "lena2d_int_u16.sh"; + + + image2d_rgb16 img_rgb16; + image2d_int_u16 img_int_u16; + + mln::io::ppm::load(img_rgb16, OLENA_IMG_PATH"/lena_16.ppm"); + //img_int_u16 = mln::data::transform(img_rgb16, rgb16_to_int_u16()); + //mln::io::pgm::save(img_int_u16, "lena_16.pgm"); + + // Save it were it is impossible to do it !! + bool ref_false=mln::io::plot::save_image_sh(img_rgb16, null_lena); + + // Save it where we have the full permissions. + bool ref_true = mln::io::plot::save_image_sh(img_rgb16, lena); + // mln::io::plot::save_image_sh(img_int_u16, lena2); + + mln_assertion(true == ref_true); + mln_assertion(false == ref_false); + std::cout << "save image2d<rgb16> : ok" << std::endl; +} + + +//------------------------------------------------------------------------------ +// Test to save image2d<hsl_f> +//------------------------------------------------------------------------------ + +void test_save_image2d_hsl_f() +{ + typedef mln::value::hsl_f hsl_f; + typedef mln::value::rgb8 rgb8; + typedef mln::image2d<hsl_f> image2d_hsl_f; + typedef mln::image2d<rgb8> image2d_rgb8; + typedef mln::fun::v2v::f_rgb_to_hsl_<hsl_f> rgb_to_hsl; + + const char* null_lena = "/dev/null/lena2d_hsl_f.sh"; + const char* lena = "lena2d_hsl_f.sh"; + image2d_hsl_f img_hsl_f; + image2d_rgb8 img_rgb8; + + mln::io::ppm::load(img_rgb8, OLENA_IMG_PATH"/lena.ppm"); + //mln::data::fill(img_rgb8, mln::literal::red); + //mln::data::fill(img_rgb8, mln::literal::green); + //mln::data::fill(img_rgb8, mln::literal::blue); + img_hsl_f = mln::data::transform(img_rgb8, rgb_to_hsl()); + + + // Save it were it is impossible to do it !! + bool ref_false = mln::io::plot::save_image_sh(img_hsl_f, null_lena); + + // Save it where we have the full permissions. + bool ref_true = mln::io::plot::save_image_sh(img_hsl_f, lena); + + mln_assertion(true == ref_true); + mln_assertion(false == ref_false); + std::cout << "save image2d<hsl_f> : ok" << std::endl; +} + +//------------------------------------------------------------------------------ +// Test to save image2d<hsl_d> +//------------------------------------------------------------------------------ + +void test_save_image2d_hsl_d() +{ + typedef mln::value::hsl_d hsl_d; + typedef mln::value::rgb16 rgb16; + typedef mln::image2d<hsl_d> image2d_hsl_d; + typedef mln::image2d<rgb16> image2d_rgb16; + typedef mln::fun::v2v::f_rgb_to_hsl_<hsl_d> rgb_to_hsl; + + const char* null_lena = "/dev/null/lena2d_hsl_d.sh"; + const char* lena = "lena2d_hsl_d.sh"; + image2d_hsl_d img_hsl_d; + image2d_rgb16 img_rgb16; + + mln::io::ppm::load(img_rgb16, OLENA_IMG_PATH"/lena_16.ppm"); + //mln::data::fill(img_rgb16, mln::literal::red); + //mln::data::fill(img_rgb16, mln::literal::green); + //mln::data::fill(img_rgb16, mln::literal::blue); + img_hsl_d = mln::data::transform(img_rgb16, rgb_to_hsl()); + + + // Save it were it is impossible to do it !! + bool ref_false = mln::io::plot::save_image_sh(img_hsl_d, null_lena); + + // Save it where we have the full permissions. + bool ref_true = mln::io::plot::save_image_sh(img_hsl_d, lena); + + mln_assertion(true == ref_true); + mln_assertion(false == ref_false); + std::cout << "save image2d<hsl_d> : ok" << std::endl; +} + + +//------------------------------------------------------------------------------ +// Test to save image2d<hsi_f> +//------------------------------------------------------------------------------ + +void test_save_image2d_hsi_f() +{ + typedef mln::value::hsi_f hsi_f; + typedef mln::value::rgb8 rgb8; + typedef mln::image2d<hsi_f> image2d_hsi_f; + typedef mln::image2d<rgb8> image2d_rgb8; + typedef mln::fun::v2v::f_rgb_to_hsi_<hsi_f> rgb_to_hsi; + + const char* null_lena = "/dev/null/lena2d_hsi_f.sh"; + const char* lena = "lena2d_hsi_f.sh"; + image2d_hsi_f img_hsi_f; + image2d_rgb8 img_rgb8; + + mln::io::ppm::load(img_rgb8, OLENA_IMG_PATH"/lena.ppm"); + //mln::data::fill(img_rgb8, mln::literal::red); + //mln::data::fill(img_rgb8, mln::literal::green); + //mln::data::fill(img_rgb8, mln::literal::blue); + img_hsi_f = mln::data::transform(img_rgb8, rgb_to_hsi()); + + + // Save it were it is impossible to do it !! + bool ref_false = mln::io::plot::save_image_sh(img_hsi_f, null_lena); + + // Save it where we have the full permissions. + bool ref_true = mln::io::plot::save_image_sh(img_hsi_f, lena); + + mln_assertion(true == ref_true); + mln_assertion(false == ref_false); + std::cout << "save image2d<hsi_f> : ok" << std::endl; +} + +//------------------------------------------------------------------------------ +// Test to save image2d<hsi_d> +//------------------------------------------------------------------------------ + +void test_save_image2d_hsi_d() +{ + typedef mln::value::hsi_d hsi_d; + typedef mln::value::rgb16 rgb16; + typedef mln::image2d<hsi_d> image2d_hsi_d; + typedef mln::image2d<rgb16> image2d_rgb16; + typedef mln::fun::v2v::f_rgb_to_hsi_<hsi_d> rgb_to_hsi; + + const char* null_lena = "/dev/null/lena2d_hsi_d.sh"; + const char* lena = "lena2d_hsi_d.sh"; + image2d_hsi_d img_hsi_d; + image2d_rgb16 img_rgb16; + + mln::io::ppm::load(img_rgb16, OLENA_IMG_PATH"/lena_16.ppm"); + //mln::data::fill(img_rgb16, mln::literal::red); + //mln::data::fill(img_rgb16, mln::literal::green); + //mln::data::fill(img_rgb16, mln::literal::blue); + img_hsi_d = mln::data::transform(img_rgb16, rgb_to_hsi()); + + + // Save it were it is impossible to do it !! + bool ref_false = mln::io::plot::save_image_sh(img_hsi_d, null_lena); + + // Save it where we have the full permissions. + bool ref_true = mln::io::plot::save_image_sh(img_hsi_d, lena); + + mln_assertion(true == ref_true); + mln_assertion(false == ref_false); + std::cout << "save image2d<hsi_d> : ok" << std::endl; +} + +//------------------------------------------------------------------------------ +// TEST TO SAVE 8 BITS 3D RGB IMAGE +//------------------------------------------------------------------------------ + +void test_save_image_sh_3d() +{ + typedef mln::value::int_u8 int_u8; + typedef mln::image3d<int_u8> image3d; + typedef mln::util::array<std::string> array1d; + + image3d img; + array1d array; + + for (unsigned i = 0; i < 5; i++) + array.append(OLENA_IMG_PATH"/fly.pgm"); + + mln::io::pgms::load(img, array); + + // Save it were it is impossible to do it !! + bool ref_false = mln::io::plot::save_image_sh(img, "/dev/null/fly3d.sh"); + + // Save it where we have the full permissions. + bool ref_true = mln::io::plot::save_image_sh(img, "fly3d.sh"); + + mln_assertion(true == ref_true); + mln_assertion(false == ref_false); + std::cout << "save image 3d : ok" << std::endl; +} + + +int main() +{ + test_save_image_sh_1d(); + + test_save_image2d_bool(); + + test_save_image2d_int_u8(); + test_save_image2d_int_u16(); + + test_save_image2d_int_s8(); + test_save_image2d_int_s16(); + + test_save_image2d_float(); + test_save_image2d_double(); + + test_save_image2d_rgb8(); + test_save_image2d_rgb16(); + + test_save_image2d_hsl_f(); + test_save_image2d_hsl_d(); + + test_save_image2d_hsi_f(); + test_save_image2d_hsi_d(); + + test_save_image_sh_3d(); + + return 0; +} -- 1.5.6.5