1926: Make Tikz output able to valuate pixels.

https://svn.lrde.epita.fr/svn/oln/trunk/milena Index: ChangeLog from Alexandre Abraham <abraham@lrde.epita.fr> Make Tikz output able to valuate pixels. * sandbox/abraham/io/tikz/save_header.hh: New (mln::io::tikz::save_header) Write the header of the LaTeX file. * sandbox/abraham/io/tikz/save.hh: (mln::io::tikz::save) Can now take another image in argument to put values on pixels. save.hh | 70 ++++++++++++++++++++++++++++++++++++++------- save_header.hh | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 147 insertions(+), 11 deletions(-) Index: sandbox/abraham/io/tikz/save_header.hh --- sandbox/abraham/io/tikz/save_header.hh (revision 0) +++ sandbox/abraham/io/tikz/save_header.hh (revision 0) @@ -0,0 +1,88 @@ +// Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007 EPITA +// Research and Development Laboratory +// +// This file is part of the Olena Library. This library is free +// software; you can redistribute it and/or modify it under the terms +// of the GNU General Public License version 2 as published by the +// Free Software Foundation. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this library; see the file COPYING. If not, write to +// the Free Software Foundation, 51 Franklin Street, Fifth Floor, +// Boston, MA 02111-1307, USA. +// +// As a special exception, you may use this file as part of a free +// software library without restriction. Specifically, if other files +// instantiate templates or use macros or inline functions from this +// file, or you compile this file and link it with other files to +// produce an executable, this file does not by itself cause the +// resulting executable to be covered by the GNU General Public +// License. This exception does not however invalidate any other +// reasons why the executable file might be covered by the GNU General +// Public License. + +#ifndef MLN_IO_TIKZ_SAVE_HEADER_HH +# define MLN_IO_TIKZ_SAVE_HEADER_HH + +/*! + * \file mln/io/tikz/save_header.hh + * + * \brief Define a function which saves header for TIKZ image. + * + */ + +# include <iostream> +# include <fstream> + +namespace mln +{ + + namespace io + { + +# ifndef MLN_INCLUDE_ONLY + + namespace tikz + { + + template <typename I> + inline + void save_header(const I& ima, + const std::string& filename, + std::ofstream& file) + { + if (! file) + { + std::cerr << "error: cannot open file '" << filename + << "'!"; + abort(); + } + + file << "% Generated by milena 1.0 http://olena.lrde.epita.fr" << std::endl; + file << "% EPITA Research and Development Laboratory (LRDE)" << std::endl; + + file << "\\documentclass[12pt,a4paper]{article}" << std::endl; + file << "\\usepackage{tikz}" << std::endl; + + file << "\\begin{document}" << std::endl; + file << "\\begin{tikzpicture}" << std::endl; + file << "\\tikzstyle{every node}=[transform shape, draw,shape=rectangle, minimum width=1cm, minimum height=1cm]" << std::endl; + + } + + } // end of namespace mln::io::tikz + + +# endif // ! MLN_INCLUDE_ONLY + + } // end of namespace mln::io + +} // end of namespace mln + + +#endif // ! MLN_IO_TIKZ_SAVE_HEADER_HH Index: sandbox/abraham/io/tikz/save.hh --- sandbox/abraham/io/tikz/save.hh (revision 1925) +++ sandbox/abraham/io/tikz/save.hh (working copy) @@ -49,8 +49,8 @@ # include <mln/metal/templated_by.hh> -# include <mln/io/pnm/save_header.hh> -# include <mln/io/pnm/macros.hh> +// # include <mln/io/tikz/save_header.hh> +# include "save_header.hh" # include <mln/geom/size2d.hh> @@ -71,6 +71,15 @@ template <typename I> void save(const Image<I>& ima_, const std::string& filename); + /*! Save a milena image as a tikz image with values. + * + * \param[in] ima_ The image to save. + * \param[in,out] filename the destination. + * \param[in] val_ An Image containing values to print. + */ + template <typename I, typename V> + void save(const Image<I>& ima_, const std::string& filename, const Image<V>& val_); + # ifndef MLN_INCLUDE_ONLY @@ -157,12 +166,38 @@ { file << "\\path (" << p.col() << "," << max_row - p.row() << ") node[fill="; write_value(file, ima(p)); - file << "] (p_" << p.row() << "_" << p.col() << ") { \\scriptsize \\color{"; + file << "] (p_" << p.row() << "_" << p.col() << ") { \\color{"; + file << "black"; + file << "}" << "};" << std::endl; + } + } + + // Save data + template <typename I, typename V> + inline + void save_data_with_values_(std::ofstream& file, + const I& ima, + const V& val) + { + const int + min_row = geom::min_row(ima), + max_row = geom::max_row(ima), + min_col = geom::min_col(ima), + max_col = geom::max_col(ima); + + point2d p; + for (p.row() = min_row; p.row() <= max_row; ++p.row()) + for (p.col() = min_col; p.col() <= max_col; ++p.col()) + { + file << "\\path (" << p.col() << "," << max_row - p.row() << ") node[fill="; + write_value(file, ima(p)); + file << "] (p_" << p.row() << "_" << p.col() << ") { \\color{"; file << "black"; - file << "}" << ima(p) << "};" << std::endl; + file << "}" << val(p) << "};" << std::endl; } } + } // end of namespace mln::io::pnm::impl @@ -174,14 +209,8 @@ { const I& ima = exact(ima_); std::ofstream file(filename.c_str()); - // io::tikz::save_header(ima, file); - - file << "\\documentclass[12pt,a4paper]{article}" << std::endl; - file << "\\usepackage{tikz}" << std::endl; - file << "\\begin{document}" << std::endl; - file << "\\begin{tikzpicture}" << std::endl; - file << "\\tikzstyle{every node}=[draw,shape=rectangle, minimum width=1cm, minimum height=1cm]" << std::endl; + io::tikz::save_header(ima, filename, file); impl::save_data_(file, ima); @@ -190,6 +219,25 @@ file << "\\end{document}" << std::endl; } + template <typename I, typename V> + inline + void save(const Image<I>& ima_, const std::string& filename, const Image<V>& val_) + { + const I& ima = exact(ima_); + const V& val = exact(val_); + std::ofstream file(filename.c_str()); + + io::tikz::save_header(ima, filename, file); + + impl::save_data_with_values_(file, + ima, + val); + + file << "\\end{tikzpicture}" << std::endl; + file << "\\end{document}" << std::endl; + } + + # endif // ! MLN_INCLUDE_ONLY } // end of namespace mln::io::tikz
participants (1)
-
Alexandre Abraham