transform_pixel_iterator.hpp 3.42 KB
Newer Older
1
2
3
4
#ifndef TRANSFORMED_PIXEL_ITERATOR_HPP
# define TRANSFORMED_PIXEL_ITERATOR_HPP

# include <type_traits>
5
# include <mln/core/iterator/iterator_base.hpp>
6
7
8
9
10
11
12
13

namespace mln
{

  template <typename UnaryFunction, typename PixelIterator, typename Image>
  struct transformed_pixel;

  template <typename UnaryFunction, typename PixelIterator, typename Image>
Edwin Carlinet's avatar
Edwin Carlinet committed
14
  struct transform_pixel_iterator;
15
16
17
18
19
20
21
22
23
24


  /***********************/
  /*** Implementation ****/
  /***********************/

  template <typename UnaryFunction, typename PixelIterator, typename Image>
  struct transformed_pixel
  {
  private:
25
    typedef typename PixelIterator::value_type Pixel;
26
27

  public:
28
29
30
31
32
    typedef typename std::result_of<const UnaryFunction(typename Pixel::reference)>::type   reference;
    typedef typename std::remove_reference<reference>::type                   value_type;
    typedef Image                                                           image_type;
    typedef typename Pixel::point_type                                        point_type;
    typedef typename Pixel::site_type                                         site_type;
33
34
35

    transformed_pixel() = default;

36
37
    transformed_pixel(const UnaryFunction& fun, const PixelIterator& x, Image& ima)
      : x_ (x), fun_ (fun), ima_(&ima)
38
39
40
41
42
    {
    }

    template <typename PixelIterator2, typename Image2>
    transformed_pixel(const transformed_pixel<UnaryFunction, PixelIterator2, Image2>& other,
43
                      typename std::enable_if< std::is_convertible<PixelIterator2, PixelIterator>::value and
Edwin Carlinet's avatar
Edwin Carlinet committed
44
					       std::is_convertible<Image2*, Image*>::value >::type* = NULL)
45
      : x_ (other.x_), fun_ (other.fun_), ima_ (other.ima_)
46
47
48
    {
    }

49
50
51
52
    point_type point() const
    {
      return x_->point();
    }
53

54
    point_type site() const
55
    {
56
      return x_->site();
57
58
    }

59
    reference val() const
60
    {
61
      return fun_(x_->val());
62
63
64
65
66
67
68
    }

    image_type& image() const
    {
      return *ima_;
    }

69
70
71
  private:
    template <typename, typename, typename>
    friend class transform_pixel_iterator;
72

73
74
    template <typename, typename, typename>
    friend class transformed_pixel;
75

76
77
78
    PixelIterator x_;
    UnaryFunction fun_;
    Image* ima_;
79
80
81
82
  };


  template <typename UnaryFunction, typename PixelIterator, typename Image>
83
84
85
  struct transform_pixel_iterator
    : iterator_base< transform_pixel_iterator<UnaryFunction, PixelIterator, Image>,
                     const transformed_pixel<UnaryFunction, PixelIterator, Image> >
86
  {
87
88
  private:
    typedef transformed_pixel<UnaryFunction, PixelIterator, Image> pixel_t;
89

90
91
  public:
    transform_pixel_iterator() = default;
92

93
94
    transform_pixel_iterator(const UnaryFunction& fun, const PixelIterator& x, Image& ima)
      : px_(fun, x, ima)
95
96
97
    {
    }

Edwin Carlinet's avatar
Edwin Carlinet committed
98
99
100
101
102
103
104
105
106
107
    template <typename PixelIterator2, typename Image2>
    transform_pixel_iterator(const transform_pixel_iterator<UnaryFunction, PixelIterator2, Image2>& other,
			     typename std::enable_if<std::is_convertible<
			     typename transform_pixel_iterator<UnaryFunction, PixelIterator2, Image2>::pixel_t,
			     pixel_t>::value>::type* = NULL)
      : px_ (other.px_)
    {
    }


108
109
110
    void init() { px_.x_.init(); }
    void next() { px_.x_.next(); }
    bool finished() const { return px_.x_.finished(); }
111

112
    const pixel_t& dereference() const { return px_; }
113
114

  private:
Edwin Carlinet's avatar
Edwin Carlinet committed
115
116
117
    template <typename, typename, typename>
    friend struct transform_pixel_iterator;

118
    pixel_t px_;
119
120
121
122
123
124
  };

}


#endif // ! TRANSFORMED_PIXEL_ITERATOR_HPP