fill.hpp 2.41 KB
Newer Older
Edwin Carlinet's avatar
Edwin Carlinet committed
1
#pragma once
edwin.carlinet's avatar
edwin.carlinet committed
2

Michaël Roynard's avatar
Michaël Roynard committed
3
#include <mln/core/image/image.hpp>
4
5
6
#include <mln/core/rangev3/rows.hpp>

#include <algorithm>
Edwin Carlinet's avatar
Edwin Carlinet committed
7
8
9
#include <range/v3/algorithm/fill.hpp>

/// \file
edwin.carlinet's avatar
edwin.carlinet committed
10

Michaël Roynard's avatar
Michaël Roynard committed
11
12
namespace mln
{
edwin.carlinet's avatar
edwin.carlinet committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26

  /// \brief \p fill assigns the value \p val to every element of the image \p
  /// ima.
  /// \ingroup Algorithms
  ///
  /// \param[out] output The output image.
  /// \param val The value to assign.
  ///
  /// \return The image.
  ///
  /// \tparam OutputImage is a model of the Writable Forward Image.
  /// \tparam Value must be convertible to Image's value type.
  /// \ingroup algorithms
  template <typename OutputImage, typename Value>
Edwin Carlinet's avatar
Edwin Carlinet committed
27
  [[deprecated]] OutputImage& fill(Image<OutputImage>& output, const Value& val);
edwin.carlinet's avatar
edwin.carlinet committed
28
29
30
31

  /// \overload
  /// \ingroup Algorithms
  template <typename OutputImage, typename Value>
Edwin Carlinet's avatar
Edwin Carlinet committed
32
  [[deprecated]] OutputImage&& fill(Image<OutputImage>&& output, const Value& val);
edwin.carlinet's avatar
edwin.carlinet committed
33

34
35
  namespace experimental
  {
Edwin Carlinet's avatar
Edwin Carlinet committed
36
37
38
39
40
41
42
43
44
45
    /// \brief Assigns the value \p val to every element of the image \p ima.
    ///
    /// \ingroup Algorithms
    ///
    /// \param[out] f The output image.
    /// \param v The value to assign.
    ///
    ///
    /// \tparam OutputImage is a model of the Writable Forward Image.
    /// \tparam Value must be convertible to Image's value type.
46
    template <class OutputImage, class Value>
Edwin Carlinet's avatar
Edwin Carlinet committed
47
48
    void fill(OutputImage f, const Value& v);
  } // namespace experimental
49

Michaël Roynard's avatar
Michaël Roynard committed
50
51
52
  /******************************************/
  /****          Implementation          ****/
  /******************************************/
edwin.carlinet's avatar
edwin.carlinet committed
53

54
55
56
57
58
59
  namespace impl
  {
    template <typename I, typename V>
    void fill(I& ima, const V& v)
    {
      mln_viter(pin, ima);
Michaël Roynard's avatar
Michaël Roynard committed
60
      mln_forall (pin)
Edwin Carlinet's avatar
Edwin Carlinet committed
61
        *pin = v;
62
    }
Edwin Carlinet's avatar
Edwin Carlinet committed
63
  } // namespace impl
64

edwin.carlinet's avatar
edwin.carlinet committed
65
  template <typename OutputImage, typename Value>
Michaël Roynard's avatar
Michaël Roynard committed
66
  OutputImage&& fill(Image<OutputImage>&& output_, const Value& val)
edwin.carlinet's avatar
edwin.carlinet committed
67
  {
68
69
    fill(output_, val);
    return move_exact(output_);
edwin.carlinet's avatar
edwin.carlinet committed
70
71
72
  }

  template <typename OutputImage, typename Value>
Michaël Roynard's avatar
Michaël Roynard committed
73
  OutputImage& fill(Image<OutputImage>& output_, const Value& val)
edwin.carlinet's avatar
edwin.carlinet committed
74
75
  {
    OutputImage& output = exact(output_);
76
    impl::fill(output, val);
edwin.carlinet's avatar
edwin.carlinet committed
77
78
79
    return output;
  }

80
  namespace experimental
81
  {
82
    template <class OutputImage, class Value>
Edwin Carlinet's avatar
Edwin Carlinet committed
83
    void fill(OutputImage f, const Value& v)
84
    {
Edwin Carlinet's avatar
Edwin Carlinet committed
85
      static_assert(mln::is_a<OutputImage, Image>());
86

87
88
      auto&& vals = f.new_values();
      for (auto row : ranges::rows(vals))
89
90
        ::ranges::fill(row, v);
    }
Edwin Carlinet's avatar
Edwin Carlinet committed
91
  } // namespace experimental
edwin.carlinet's avatar
edwin.carlinet committed
92

Edwin Carlinet's avatar
Edwin Carlinet committed
93
} // namespace mln