save.hh 3.58 KB
Newer Older
1
// Copyright (C) 2009, 2010, 2013 EPITA Research and Development Laboratory
Guillaume Lazzara's avatar
Guillaume Lazzara committed
2
// (LRDE)
3
//
4
// This file is part of Olena.
5
//
6
7
8
9
10
// 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,
11
12
13
14
15
// 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
16
// along with Olena.  If not, see <http://www.gnu.org/licenses/>.
17
18
//
// As a special exception, you may use this file as part of a free
19
// software project without restriction.  Specifically, if other files
20
// instantiate templates or use macros or inline functions from this
21
22
23
24
25
// 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.
26
27
28
29

#ifndef MLN_IO_PLOT_SAVE_HH
# define MLN_IO_PLOT_SAVE_HH

30
/// \file
31
32
33
///
/// Define a routine which saves in a plot format.

Guillaume Lazzara's avatar
Guillaume Lazzara committed
34
35
# include <iostream>
# include <fstream>
36
37
38
39
40
41
42
43
44
45
46
47
48
# include <mln/core/image/image1d.hh>
# include <mln/util/array.hh>


namespace mln
{

  namespace io
  {

    namespace plot
    {

49
50
51
      /*! \brief Save a Milena 1D image in a plot file.
	  \param[in] ima A reference to the image to save.
	  \param[out] filename The output file.  */
52
      template <typename I>
Guillaume Lazzara's avatar
Guillaume Lazzara committed
53
      void save(const image1d<I>& ima, const std::string& filename);
54
55
56
57
58
59

      /*! \brief Save a Milena array in a plot file.
          \param[in] arr A reference to the array to save.
	  \param[out] filename The output file.
	  \param[in] start_value The start index value of the plot
	             (optional).  */
Guillaume Lazzara's avatar
Guillaume Lazzara committed
60
      template <typename T>
61
62
      void save(const util::array<T>& arr, const std::string& filename,
		int start_value = 0);
63

Guillaume Lazzara's avatar
Guillaume Lazzara committed
64
65
66
      /// \overload
      template <typename T>
      void save(const histo::array<T>& arr, const std::string& filename);
67
68
69
70

# ifndef MLN_INCLUDE_ONLY


Guillaume Lazzara's avatar
Guillaume Lazzara committed
71
72
73

      // FIXME: Make it generic.
      template <typename T>
74
      inline
Guillaume Lazzara's avatar
Guillaume Lazzara committed
75
      void save(const image1d<T>& ima, const std::string& filename)
76
77
78
79
      {
	trace::entering("mln::io::plot::save");

	std::ofstream file_out(filename.c_str());
80
81
82
	unsigned end = ima.bbox().pmax().ind();
	for (unsigned i = ima.bbox().pmin().ind(); i <= end; ++i)
	  file_out << i << " " << ima.at_(i) << std::endl;
83
84
85
86

	trace::exiting("mln::io::plot::save");
      }

Guillaume Lazzara's avatar
Guillaume Lazzara committed
87
      template <typename T>
88
      inline
Guillaume Lazzara's avatar
Guillaume Lazzara committed
89
      void save(const util::array<T>& arr, const std::string& filename,
90
		int start_value)
91
92
93
94
      {
	trace::entering("mln::io::plot::save");

	std::ofstream file_out(filename.c_str());
95
	for (unsigned i = 0; i < arr.nelements(); ++i)
96
	  file_out << start_value + i << " " << arr[i] << std::endl;
97
98
99
100

	trace::exiting("mln::io::plot::save");
      }

Guillaume Lazzara's avatar
Guillaume Lazzara committed
101
102
103
104
105
106
107
108
109
110
111
112
113
      template <typename T>
      inline
      void save(const histo::array<T>& arr, const std::string& filename)
      {
	trace::entering("mln::io::plot::save");

	std::ofstream file_out(filename.c_str());
	for (unsigned i = 0; i < arr.nvalues(); ++i)
	  file_out << i << " " << arr[i] << std::endl;

	trace::exiting("mln::io::plot::save");
      }

114
115
116
117
118
119
120
121
122
123
124

# endif // ! MLN_INCLUDE_ONLY

    } // end of namespace mln::io::plot

  } // end of namespace mln::io

} // end of namespace mln


#endif // ! MLN_IO_PLOT_SAVE_HH