
https://svn.lrde.epita.fr/svn/oln/trunk/milena Index: ChangeLog from Alexandre Abraham <abraham@lrde.epita.fr> Add beta tikz saving and tests. * sandbox/abraham/tests: New. * sandbox/abraham/tests/tikz.cc: New Basic test for tikz saving. * sandbox/abraham/tests/Makefile: New. * sandbox/abraham/io: New. * sandbox/abraham/io/tikz: New. * sandbox/abraham/io/tikz/save.hh: New (mln::io::tikz::save) Save an image to tikz. * sandbox/abraham/img: New. * sandbox/abraham/img/test.pgm: New. * sandbox/abraham/img/lena.pgm: New. * sandbox/abraham/img/lena.ppm: New. img/lena.pgm | 5 + img/lena.ppm | 5 + io/tikz/save.hh | 202 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/Makefile | 9 ++ tests/tikz.cc | 37 ++++++++++ 5 files changed, 258 insertions(+) Index: sandbox/abraham/tests/tikz.cc --- sandbox/abraham/tests/tikz.cc (revision 0) +++ sandbox/abraham/tests/tikz.cc (revision 0) @@ -0,0 +1,37 @@ +#include <mln/core/image2d.hh> +#include <mln/value/rgb.hh> +#include <mln/value/int_u.hh> +#include <mln/io/ppm/load.hh> +#include <mln/io/pgm/load.hh> + +#include "io/tikz/save.hh" + +int main () { + using namespace mln; + using value::rgb; + + typedef image2d< rgb<8> > image2drgb; + + image2drgb input; + + io::ppm::load(input, "../img/lena.ppm"); + + io::tikz::save(input, "tex/lena.tex"); + + typedef image2d< value::int_u<4> > image2dint4; + typedef image2d< value::int_u<8> > image2dint8; + + image2dint8 input2; + + io::pgm::load(input2, "../img/test.pgm"); + + image2dint4 input3 (input2.domain(), input2.border()); + image2dint8::fwd_piter p(input2.domain()); + + for_all(p) + input3(p) = input2(p); + + io::tikz::save(input3, "tex/test.tex"); + + return 0; +} // int main () Index: sandbox/abraham/tests/Makefile --- sandbox/abraham/tests/Makefile (revision 0) +++ sandbox/abraham/tests/Makefile (revision 0) @@ -0,0 +1,9 @@ +CXXFLAGS=-Wall -W -I ../../.. -g -I ../ + +tikz: tikz.o + g++ ${CXXFLAGS} tikz.o -o tikz + +all: + +tikz.o : tikz.cc +tikz.cc: ../io/tikz/save.hh \ No newline at end of file Index: sandbox/abraham/io/tikz/save.hh --- sandbox/abraham/io/tikz/save.hh (revision 0) +++ sandbox/abraham/io/tikz/save.hh (revision 0) @@ -0,0 +1,202 @@ +// 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_HH +# define MLN_IO_TIKZ_SAVE_HH + +/*! + * \file mln/io/tikz/save.hh + * + * \brief Define a function which saves an image of kind TIKZ into + * given path. + * + */ + +# include <iostream> +# include <fstream> + +# include <mln/core/concept/image.hh> +# include <mln/core/point2d.hh> + +# include <mln/value/graylevel.hh> +# include <mln/value/rgb.hh> +# include <mln/value/int_u.hh> + +# include <mln/metal/templated_by.hh> + +# include <mln/io/pnm/save_header.hh> +# include <mln/io/pnm/macros.hh> + +# include <mln/geom/size2d.hh> + +namespace mln +{ + + namespace io + { + + namespace tikz + { + + /*! Save a milena image as a tikz image. + * + * \param[in] ima_ The image to save. + * \param[in,out] filename the destination. + */ + template <typename I> + void save(const Image<I>& ima_, const std::string& filename); + + +# ifndef MLN_INCLUDE_ONLY + + namespace impl + { + + // write a greylevel value into + template <unsigned int n> + inline + void write_value(std::ofstream& file, + const value::int_u<n>& v) + { + int c = v; + file << "black!" << (int(mln::trait::value_< mln::value::int_u<n> >::max()) - c) * 100 / int(mln::trait::value_< mln::value::int_u<n> >::max()); + } + + // write a rgb value into + template <unsigned int n> + inline + void write_value(std::ofstream& file, + const value::rgb<n>& v) + { + mln::value::int_u<n> cvalues[] = {v.red(), v.green(), v.blue()}; + char *cnames[] = {"red", "green", "blue"}; + short colors[] = {0, 1, 2}, tmp; + + // Sort colors increasing + if (cvalues[colors[0]] > cvalues[colors[1]]) + { + tmp = colors[0]; + colors[0] = colors[1]; + colors[1] = tmp; + } + + if (cvalues[colors[1]] > cvalues[colors[2]]) + { + tmp = colors[1]; + colors[1] = colors[2]; + colors[2] = tmp; + } + + if (cvalues[colors[0]] > cvalues[colors[1]]) + { + tmp = colors[0]; + colors[0] = colors[1]; + colors[1] = tmp; + } + + double c1 = cvalues[colors[1]] - cvalues[colors[0]], + c2 = cvalues[colors[2]] - cvalues[colors[0]]; + + double w = 1 - double(cvalues[colors[0]]) / 255.; + + c1 /= w; + c2 /= w; + + double c = 1 - c1 / 255.; + + c2 -= c1; + c2 /= c; + + double p = 1 - c2 / 255.; + + file << "black!" << int(p * 100) << "!" << cnames[colors[2]] << "!" + << int(c * 100) << "!" << "-" << cnames[colors[0]] << "!" + << int(w * 100) << "!white"; + } + + // Save data + template <typename I> + inline + void save_data_(std::ofstream& file, + const I& ima) + { + 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() << ") { \\scriptsize \\color{"; + file << "black"; + file << "}" << ima(p) << "};" << std::endl; + } + } + + } // end of namespace mln::io::pnm::impl + + + // Facades. + + template <typename I> + inline + void save(const Image<I>& ima_, const std::string& filename) + { + 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; + + impl::save_data_(file, + ima); + + file << "\\end{tikzpicture}" << std::endl; + file << "\\end{document}" << std::endl; + } + +# endif // ! MLN_INCLUDE_ONLY + + } // end of namespace mln::io::tikz + + } // end of namespace mln::io + +} // end of namespace mln + + +#endif // ! MLN_IO_TIKZ_SAVE_HH Index: sandbox/abraham/img/test.pgm Cannot display: file marked as a binary type. svn:mime-type = application/octet-stream Property changes on: sandbox/abraham/img/test.pgm ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Index: sandbox/abraham/img/lena.pgm --- sandbox/abraham/img/lena.pgm (revision 0) +++ sandbox/abraham/img/lena.pgm (revision 0) @@ -0,0 +1,5 @@ +P5 +# CREATOR: GIMP PNM Filter Version 1.1 +16 16 +255 +[kyzyzxrrTXitx}px®S*mSeop¯Áwws,FXQfkv´ÅÊ[88^Srjw©²¹´¹Å.t`Rt{~rpªÀ©nEaQajC=±n.q]Tgl;4®D8UWiH<Y qWHRC]U9FeqIrÆGTJQg:vPL¶ÐBOMo?2W1UÅÌNHFeD/O uX}r¾W_66QX6bÆy[a7/I<Gn¹¡e£]Qv2>C7_| ¥·_jOJ \ No newline at end of file Index: sandbox/abraham/img/lena.ppm --- sandbox/abraham/img/lena.ppm (revision 0) +++ sandbox/abraham/img/lena.ppm (revision 0) @@ -0,0 +1,5 @@ +P6 +# CREATOR: GIMP PNM Filter Version 1.1 +16 16 +255 +ânàkDR¿TVÎb_Ñc`Ïb`Îc`Ìa_Ä\^ÏpjálàrÐ rÁ]^@Qäjá}f«@MÀQUÎ\\ÎacËpqÍ~ËhjÇX\ÊbdÞ~nÜxgæ¢@Q`@ÁVUâ}e£<MºMTÈWY¾ZaÇrtץݻ¯¾otÊ`bÈabÝqe½__b@{5RªANà|i¢:LºOVÀTYÄ`eËvwÛ« ྴßÅ»Êpn GVÖq%Gn&JÈ~y´FNãj§;JÂ]ZÀRWÇagÔ ÚÛ¨Ù±©Û«Û±¥à¿²aD±cnÓ}·HPåk¥:JÁ_[ÒmaÉejÅjv©a}¢`wÔݹ«Ð¡_k}3SÓÐ}·IQæm£9J¶IQÕ}pµThx0]o+YµØ¨Â¦]kf@ªakÔϱFPæp¥=O¿OUµWbo)Vf"Q¸v|ËèÖz2Sn&IË{Ë~Æ}§=Kèq¬?M¨Wgv7gj+YI_Çsv´]nßBY6VAWÎ yÈyÉ-Fçq§Gb}Ezh(T}5SUcÙfjåàÀ[f7SbfÈyÎzìÀ1Hèu>\t:o~AjXhw'FØ[_ÞooÛzy<V:RÈ}rÃ}wã îË¥ -Hçw;]s>r`r.LlA?LÛgjÏishCBUÉ y»|wé¿îÇ=Uäuz7Zp6fXt4YcC9MÔilÙ isFX·mnªbgè·ERRgæsg%Ld&RxBuJnq#D·JSÜjfá}í¾©ko½ysÑ{Íl¢GVTgãpk&M^Jo:ik,S2FÄW[ÞicÞyoæ®ÎU^Út§HP>TjuÜpc!Jj.[m4an%H´GLÙc\ßmfÞsháÜ®¤MX±WW=S8Q \ No newline at end of file