zip_image.hpp 10.2 KB
Newer Older
1
2
3
4
5
#ifndef MLN_CORE_IMAGE_BASE_HPP
# warning "This file should be included only by image_base.hpp"
# include <mln/core/image_base.hpp>
#endif

edwin's avatar
edwin committed
6
7
8
#ifndef MLN_CORE_IMAGE_ZIP_IMAGE_HPP
# define MLN_CORE_IMAGE_ZIP_IMAGE_HPP

9
10
11
# include <boost/mpl/and.hpp>
# include <boost/tuple/tuple.hpp>

edwin's avatar
edwin committed
12
13
14
15
# include <mln/core/assert.hpp>
# include <mln/core/image_base.hpp>
# include <mln/core/iterator/pixel_iterator.hpp>

16
17
# include <mln/core/image/zip_image_value_iterator.hpp>

edwin's avatar
edwin committed
18
19
20
21
22
23
24
25
26
27
28
# include <boost/iterator/zip_iterator.hpp>
# include <boost/range/iterator_range.hpp>



namespace mln {

  template <typename... Images>
  struct zip_image;

  template <typename... I>
29
  zip_image<I...> imzip(I&&... images);
edwin's avatar
edwin committed
30
31

  // template <typename... I>
32
33
34
35
36
37
38
39
40
41
42
43
  // zip_image<I...> imzip(I&... images);

}

namespace boost
{
  // template <int N, typename... Images>
  // get(const zip_image<Images...>& ima);

  // template <int N, typename... Images>
  // get(zip_image<Images...>& ima);
}
edwin's avatar
edwin committed
44
45
46
47
48

/******************************************/
/****              Traits              ****/
/******************************************/

49
50
51
namespace mln
{

edwin's avatar
edwin committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  namespace internal
  {
    using namespace boost::detail::tuple_impl_specific;

    template <typename ImageTuple>
    struct zip_image_accessibility
    {
      typedef typename tuple_meta_transform<ImageTuple, image_meta_accessibility>::type accessibility_tuple;
      typedef typename tuple_meta_accumulate<accessibility_tuple, boost::mpl::and_<boost::mpl::_1, boost::mpl::_2>, std::true_type>::type type;
    };

    struct meta_common_type
    {
      template <typename T1, typename T2> struct apply : std::common_type<T1, T2> {};
    };

    template <typename ImageTuple>
    struct zip_image_category
    {
      typedef typename tuple_meta_transform<ImageTuple, image_meta_category>::type category_tuple;
      typedef typename tuple_meta_accumulate<category_tuple, meta_common_type, raw_image_tag>::type type;
    };

  }


  template <typename... Images>
  struct image_traits< zip_image<Images...> >
  {
  private:
    typedef boost::tuple<Images...> ImageTuple;

  public:
85
86
    typedef typename std::common_type<typename internal::zip_image_category<ImageTuple>::type,
				      bidirectional_image_tag>::type		 category;
edwin's avatar
edwin committed
87
88
89
90
91
92
93
94
95
    typedef typename internal::zip_image_accessibility<ImageTuple>::type accessible;
  };



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

96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
  template <typename... I>
  zip_image<I...>
  imzip(I&&... images)
  {
    return zip_image<I...> (std::forward<I>(images)...);
  }

  // template <typename... I>
  // zip_image<I...>
  // imzip(I&... images)
  // {
  //   return zip_image<I...> (images...);
  // }


edwin's avatar
edwin committed
111
112
113
114
115
116
117
118
119
120

  // Some helpers
  namespace internal
  {

    template <typename Point>
    struct get_image_operator_call
    {
      get_image_operator_call(const Point& p) : p_ (p) {}

121
      template <class Image> struct apply { typedef typename Image::reference type; };
edwin's avatar
edwin committed
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
      template <class Image> struct apply<Image&> { typedef typename Image::reference type; };
      template <class Image> struct apply<const Image&> { typedef typename Image::const_reference type; };

      template <typename Image>
      typename Image::const_reference
      operator() (const Image& ima) const { return ima(p_); }

      template <typename Image>
      typename Image::reference
      operator() (Image& ima) const { return ima(p_); }

    private:
      const Point& p_;
    };

137
138
139
140
    struct constify
    {
      template <typename T> struct apply { typedef const typename std::remove_reference<T>::type& type; };
      //template <typename T> struct apply<T&> { typedef const T& type; };
edwin's avatar
edwin committed
141

142
143
144
145
      template <typename T>
      const T&
      operator() (T& x) const {return static_cast<const T&> (x); }
    };
edwin's avatar
edwin committed
146

147
148
149
150
151
152
153
154
155
    struct meta_add_reference
    {
      template <typename T> struct apply { typedef typename std::remove_reference<T>::type&  type; };

      template <typename T>
      typename std::remove_const<T>::type &
      operator() (T& x) const {return const_cast<typename std::remove_const<T>::type&>( x ); }
    };

156
  }
edwin's avatar
edwin committed
157
158
159
160
161
162
163
164
165
166
167
168
169
170


  // template <typename... I>
  // zip_image<I...>
  // imzip(const Image<I>&... images)
  // {
  //   return zip_image<I...> (exact<I>(images)...);
  // }



  template <typename... Images>
  struct zip_image : mln::Image< zip_image<Images...> >
  {
171
  private:
edwin's avatar
edwin committed
172
    static_assert(sizeof...(Images) > 0, "Must have at least one image as parameter.");
173
174
175
176
    typedef zip_image<Images...> E;

    BOOST_CONCEPT_ASSERT((IterableImage<E>));

edwin's avatar
edwin committed
177
178

    typedef boost::tuple<Images...> ImageTuple;
179
180
181
    typedef typename internal::tuple_meta_transform<ImageTuple, internal::meta_add_reference>::type ImageTupleRef;
    typedef typename internal::tuple_meta_transform<ImageTuple, internal::constify>::type ConstImageTupleRef;
    typedef typename boost::tuples::element<0, ImageTuple>::type Image;
edwin's avatar
edwin committed
182
183
184
185
    typedef typename std::decay<Image>::type VImage;
    typedef boost::tuple<Images...> images_t;

  public:
186
187
    typedef ImageTuple image_tuple_t;

edwin's avatar
edwin committed
188
189
    typedef typename VImage::domain_type         domain_type;
    typedef typename VImage::point_type          point_type;
190
    typedef typename VImage::point_type          site_type;
edwin's avatar
edwin committed
191

192
    typedef typename internal::tuple_meta_transform<ImageTuple, image_meta_value>::type            value_type;
edwin's avatar
edwin committed
193
194
195
196
197
198
199
200
201
202
203
204
    typedef typename internal::tuple_meta_transform<ImageTuple, image_meta_reference>::type        reference;
    typedef typename internal::tuple_meta_transform<ImageTuple, image_meta_const_reference>::type  const_reference;
    typedef value_type*										   pointer;
    typedef const value_type*									   const_pointer;

    // typedef boost::zip_iterator<typename internal::tuple_meta_transform<ImageTuple, image_meta_value_iterator>::type>         value_iterator;
    // typedef boost::zip_iterator<typename internal::tuple_meta_transform<ImageTuple, image_meta_const_value_iterator>::type>   const_value_iterator;


    // typedef boost::iterator_range<value_iterator> value_range;
    // typedef boost::iterator_range<const_value_iterator> const_value_range;

205
206
    typedef zip_image_value_range<typename internal::zip_image_category<ImageTuple>::type, ImageTupleRef>		      value_range;
    typedef zip_image_value_range<typename internal::zip_image_category<ImageTuple>::type, ConstImageTupleRef>           const_value_range;
207
208
    typedef typename value_range::iterator							 value_iterator;
    typedef typename const_value_range::iterator						 const_value_iterator;
edwin's avatar
edwin committed
209

210
211
    typedef zip_image_pixel_range<typename internal::zip_image_category<ImageTuple>::type, ImageTupleRef, E>		 pixel_range;
    typedef zip_image_pixel_range<typename internal::zip_image_category<ImageTuple>::type, ConstImageTupleRef, const E>     const_pixel_range;
212
213
214
215
    typedef typename pixel_range::iterator							 pixel_iterator;
    typedef typename const_pixel_range::iterator						 const_pixel_iterator;
    typedef typename pixel_iterator::value_type                                                  pixel_type;
    typedef typename const_pixel_iterator::value_type                                            const_pixel_type;
edwin's avatar
edwin committed
216
217
218
219

    /// \brief Constructor
    /// \todo Check that all domain are equals
    /// \todo Add concept checking
220
    explicit zip_image(Images&&... imas) :
edwin's avatar
edwin committed
221
222
223
224
      images_ (std::forward<Images>(imas)...)
    {
    }

225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
    zip_image(zip_image&& other)
    : images_ (std::move(other.images_))
    {
    }

    zip_image(const zip_image& other)
    : images_ (other.images_)
    {
    }

    zip_image(zip_image& other)
    : images_ (other.images_)
    {
    }

240
241
242
243
244
245
    // zip_image(const Images&... imas) :
    //   images_ (imas...)
    // {
    // }


edwin's avatar
edwin committed
246
247
248
249
250
251
252
    const domain_type& domain() const
    {
      return boost::get<0>(images_).domain();
    }

    value_range values()
    {
253
      return value_range(internal::tuple_transform(images_, internal::meta_add_reference ()));
edwin's avatar
edwin committed
254
255
256
257
258
259
260
261
262
      // using namespace boost::detail::tuple_impl_specific;
      // auto w = tuple_transform(images_, internal::get_image_value_range ());
      // value_iterator begin_(tuple_transform(w, internal::get_image_value_iterator_begin ()));
      // value_iterator end_(tuple_transform(w, internal::get_image_value_iterator_end ()));
      // return value_range(begin_, end_);
    }

    const_value_range values() const
    {
263
      return const_value_range(internal::tuple_transform(images_, internal::constify ()));
edwin's avatar
edwin committed
264
265
266
267
268
269
270
271
272
      // using namespace boost::detail::tuple_impl_specific;
      // auto w = tuple_transform(images_, internal::get_image_value_range ());
      // const_value_iterator begin_(tuple_transform(w, internal::get_image_value_iterator_begin ()));
      // const_value_iterator end_(tuple_transform(w, internal::get_image_value_iterator_end ()));
      // return const_value_range(begin_, end_);
    }

    pixel_range pixels()
    {
273
      return pixel_range(internal::tuple_transform(images_, internal::meta_add_reference ()), *this);
274
275
276
277
      // auto x = this->values();
      // pixel_iterator begin_(std::begin(this->domain()), std::begin(x), *this);
      // pixel_iterator end_(std::end(this->domain()), std::end(x), *this);
      // return pixel_range(begin_, end_);
edwin's avatar
edwin committed
278
279
280
281
    }

    const_pixel_range pixels() const
    {
282
283
284
285
286
      return const_pixel_range(internal::tuple_transform(images_, internal::constify ()), *this);
      // auto x = this->values();
      // const_pixel_iterator begin_(std::begin(this->domain()), std::begin(x),*this);
      // const_pixel_iterator end_(std::end(this->domain()), std::end(x), *this);
      // return const_pixel_range(begin_, end_);
edwin's avatar
edwin committed
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
    }

    template <typename dummy = void>
    typename std::enable_if<image_accessibility<E>::type::value, reference>::type
    operator() (const point_type& p)
    {
      return internal::tuple_transform(images_, internal::get_image_operator_call<point_type> (p));
    }

    template <typename dummy = void>
    typename std::enable_if<image_accessibility<E>::type::value, const_reference>::type
    operator() (const point_type& p) const
    {
      return internal::tuple_transform(images_, internal::get_image_operator_call<point_type> (p));
    }

303
304
305
306

    boost::tuple<Images...>& images()             { return images_; }
    const boost::tuple<Images...>& images() const { return images_; }

edwin's avatar
edwin committed
307
308
309
310
311
312
313
314
315
316
317
  private:
    boost::tuple<Images...> images_;
  };




} // end of namespace mln


#endif //!MLN_CORE_IMAGE_ZIP_IMAGE_HPP