9 #ifndef BOOST_GIL_COLOR_BASE_HPP
10 #define BOOST_GIL_COLOR_BASE_HPP
12 #include <boost/gil/utilities.hpp>
13 #include <boost/gil/concepts.hpp>
14 #include <boost/gil/detail/mp11.hpp>
16 #include <boost/assert.hpp>
17 #include <boost/config.hpp>
19 #include <type_traits>
21 namespace boost {
namespace gil {
24 template <
typename P> P* memunit_advanced(
const P* p, std::ptrdiff_t diff);
27 template <
int K,
typename ColorBase>
29 ->
typename std::enable_if
31 !std::is_const<ColorBase>::value,
32 typename kth_semantic_element_reference_type<ColorBase, K>::type
36 template <
int K,
typename ColorBase>
37 auto semantic_at_c(
const ColorBase& p) ->
typename kth_semantic_element_const_reference_type<ColorBase,K>::type;
40 template <
typename ColorBase>
struct element_reference_type;
41 template <
typename ColorBase>
struct element_const_reference_type;
42 template <
typename ColorBase,
int K>
struct kth_element_type;
43 template <
typename ColorBase,
int K>
struct kth_element_type<const ColorBase,K> :
public kth_element_type<ColorBase,K> {};
44 template <
typename ColorBase,
int K>
struct kth_element_reference_type;
45 template <
typename ColorBase,
int K>
struct kth_element_reference_type<const ColorBase,K> :
public kth_element_reference_type<ColorBase,K> {};
46 template <
typename ColorBase,
int K>
struct kth_element_const_reference_type;
47 template <
typename ColorBase,
int K>
struct kth_element_const_reference_type<const ColorBase,K> :
public kth_element_const_reference_type<ColorBase,K> {};
51 template <
typename DstLayout,
typename SrcLayout,
int K>
52 struct mapping_transform : mp11::mp_at
54 typename SrcLayout::channel_mapping_t,
55 typename detail::type_to_index
57 typename DstLayout::channel_mapping_t,
58 std::integral_constant<int, K>
69 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
71 #pragma warning(disable:4512)
77 template <
typename Element,
typename Layout>
78 struct homogeneous_color_base<Element, Layout, 1>
80 using layout_t = Layout;
85 typename =
typename std::enable_if<!std::is_reference<U>::value>::type
87 homogeneous_color_base() : v0_{} {}
89 explicit homogeneous_color_base(Element v) : v0_(v) {}
91 template <
typename E2,
typename L2>
92 homogeneous_color_base(homogeneous_color_base<E2, L2, 1>
const& c)
93 : v0_(gil::at_c<0>(c))
96 auto at(std::integral_constant<int, 0>)
100 auto at(std::integral_constant<int, 0>)
const
106 operator Element()
const {
return v0_; }
115 template <
typename Element,
typename Layout>
116 struct homogeneous_color_base<Element, Layout, 2>
118 using layout_t = Layout;
122 typename U = Element,
123 typename =
typename std::enable_if<!std::is_reference<U>::value>::type
125 homogeneous_color_base() : v0_{}, v1_{} {}
127 explicit homogeneous_color_base(Element v) : v0_(v), v1_(v) {}
129 homogeneous_color_base(Element v0, Element v1) : v0_(v0), v1_(v1) {}
131 template <
typename E2,
typename L2>
132 homogeneous_color_base(homogeneous_color_base<E2, L2, 2>
const& c)
133 : v0_(
gil::at_c<mapping_transform<Layout, L2, 0>::value>(c))
134 , v1_(
gil::at_c<mapping_transform<Layout, L2, 1>::value>(c))
138 template <
typename E2,
typename L2>
139 homogeneous_color_base(homogeneous_color_base<E2, L2, 2>& c)
140 : v0_(
gil::at_c<mapping_transform<Layout, L2, 0>::value>(c))
141 , v1_(
gil::at_c<mapping_transform<Layout, L2, 1>::value>(c))
145 template <
typename P>
146 homogeneous_color_base(P* p,
bool)
147 : v0_(&semantic_at_c<0>(*p))
148 , v1_(&semantic_at_c<1>(*p))
152 template <
typename Ptr>
153 homogeneous_color_base(Ptr
const& ptr, std::ptrdiff_t diff)
154 : v0_(*memunit_advanced(semantic_at_c<0>(ptr), diff))
155 , v1_(*memunit_advanced(semantic_at_c<1>(ptr), diff))
158 template <
typename Ref>
161 return Ref(*semantic_at_c<0>(*
this), *semantic_at_c<1>(*
this));
164 auto at(std::integral_constant<int, 0>)
168 auto at(std::integral_constant<int, 0>)
const
172 auto at(std::integral_constant<int, 1>)
176 auto at(std::integral_constant<int, 1>)
const
181 auto at_c_dynamic(std::size_t i)
const -> Element
197 template <
typename Element,
typename Layout>
198 struct homogeneous_color_base<Element, Layout, 3>
200 using layout_t = Layout;
204 typename U = Element,
205 typename =
typename std::enable_if<!std::is_reference<U>::value>::type
207 homogeneous_color_base() : v0_{}, v1_{}, v2_{} {}
209 explicit homogeneous_color_base(Element v) : v0_(v), v1_(v), v2_(v) {}
211 homogeneous_color_base(Element v0, Element v1, Element v2)
212 : v0_(v0), v1_(v1), v2_(v2)
215 template <
typename E2,
typename L2>
216 homogeneous_color_base(homogeneous_color_base<E2, L2, 3>
const& c)
217 : v0_(
gil::at_c<mapping_transform<Layout, L2, 0>::value>(c))
218 , v1_(
gil::at_c<mapping_transform<Layout, L2, 1>::value>(c))
219 , v2_(
gil::at_c<mapping_transform<Layout, L2, 2>::value>(c))
223 template <
typename E2,
typename L2>
224 homogeneous_color_base(homogeneous_color_base<E2, L2, 3>& c)
225 : v0_(
gil::at_c<mapping_transform<Layout, L2, 0>::value>(c))
226 , v1_(
gil::at_c<mapping_transform<Layout, L2, 1>::value>(c))
227 , v2_(
gil::at_c<mapping_transform<Layout, L2, 2>::value>(c))
231 template <
typename P>
232 homogeneous_color_base(P* p,
bool)
233 : v0_(&semantic_at_c<0>(*p))
234 , v1_(&semantic_at_c<1>(*p))
235 , v2_(&semantic_at_c<2>(*p))
239 template <
typename Ptr>
240 homogeneous_color_base(Ptr
const& ptr, std::ptrdiff_t diff)
241 : v0_(*memunit_advanced(semantic_at_c<0>(ptr), diff))
242 , v1_(*memunit_advanced(semantic_at_c<1>(ptr), diff))
243 , v2_(*memunit_advanced(semantic_at_c<2>(ptr), diff))
246 template <
typename Ref>
250 *semantic_at_c<0>(*
this),
251 *semantic_at_c<1>(*
this),
252 *semantic_at_c<2>(*
this));
255 auto at(std::integral_constant<int, 0>)
259 auto at(std::integral_constant<int, 0>)
const
263 auto at(std::integral_constant<int, 1>)
267 auto at(std::integral_constant<int, 1>)
const
271 auto at(std::integral_constant<int, 2>)
275 auto at(std::integral_constant<int, 2>)
const
280 auto at_c_dynamic(std::size_t i)
const -> Element
299 template <
typename Element,
typename Layout>
300 struct homogeneous_color_base<Element, Layout, 4>
302 using layout_t = Layout;
306 typename U = Element,
307 typename =
typename std::enable_if<!std::is_reference<U>::value>::type
309 homogeneous_color_base() : v0_{}, v1_{}, v2_{}, v3_{} {}
311 explicit homogeneous_color_base(Element v) : v0_(v), v1_(v), v2_(v), v3_(v) {}
313 homogeneous_color_base(Element v0, Element v1, Element v2, Element v3)
314 : v0_(v0), v1_(v1), v2_(v2), v3_(v3)
317 template <
typename E2,
typename L2>
318 homogeneous_color_base(homogeneous_color_base<E2, L2, 4>
const& c)
319 : v0_(
gil::at_c<mapping_transform<Layout, L2, 0>::value>(c))
320 , v1_(
gil::at_c<mapping_transform<Layout, L2, 1>::value>(c))
321 , v2_(
gil::at_c<mapping_transform<Layout, L2, 2>::value>(c))
322 , v3_(
gil::at_c<mapping_transform<Layout, L2, 3>::value>(c))
326 template <
typename E2,
typename L2>
327 homogeneous_color_base(homogeneous_color_base<E2, L2, 4>& c)
328 : v0_(
gil::at_c<mapping_transform<Layout, L2, 0>::value>(c))
329 , v1_(
gil::at_c<mapping_transform<Layout, L2, 1>::value>(c))
330 , v2_(
gil::at_c<mapping_transform<Layout, L2, 2>::value>(c))
331 , v3_(
gil::at_c<mapping_transform<Layout, L2, 3>::value>(c))
335 template <
typename P>
336 homogeneous_color_base(P * p,
bool)
337 : v0_(&semantic_at_c<0>(*p))
338 , v1_(&semantic_at_c<1>(*p))
339 , v2_(&semantic_at_c<2>(*p))
340 , v3_(&semantic_at_c<3>(*p))
344 template <
typename Ptr>
345 homogeneous_color_base(Ptr
const& ptr, std::ptrdiff_t diff)
346 : v0_(*memunit_advanced(semantic_at_c<0>(ptr), diff))
347 , v1_(*memunit_advanced(semantic_at_c<1>(ptr), diff))
348 , v2_(*memunit_advanced(semantic_at_c<2>(ptr), diff))
349 , v3_(*memunit_advanced(semantic_at_c<3>(ptr), diff))
352 template <
typename Ref>
356 *semantic_at_c<0>(*
this),
357 *semantic_at_c<1>(*
this),
358 *semantic_at_c<2>(*
this),
359 *semantic_at_c<3>(*
this));
362 auto at(std::integral_constant<int, 0>)
366 auto at(std::integral_constant<int, 0>)
const
370 auto at(std::integral_constant<int, 1>)
374 auto at(std::integral_constant<int, 1>)
const
378 auto at(std::integral_constant<int, 2>)
382 auto at(std::integral_constant<int, 2>)
const
386 auto at(std::integral_constant<int, 3>)
390 auto at(std::integral_constant<int, 3>)
const
395 auto at_c_dynamic(std::size_t i)
const -> Element
416 template <
typename Element,
typename Layout>
417 struct homogeneous_color_base<Element, Layout, 5>
419 using layout_t = Layout;
423 typename U = Element,
424 typename =
typename std::enable_if<!std::is_reference<U>::value>::type
426 homogeneous_color_base()
427 : v0_{}, v1_{}, v2_{}, v3_{}, v4_{}
430 explicit homogeneous_color_base(Element v)
431 : v0_(v), v1_(v), v2_(v), v3_(v), v4_(v)
434 homogeneous_color_base(Element v0, Element v1, Element v2, Element v3, Element v4)
435 : v0_(v0), v1_(v1), v2_(v2), v3_(v3), v4_(v4)
438 template <
typename E2,
typename L2>
439 homogeneous_color_base(homogeneous_color_base<E2, L2, 5>
const& c)
440 : v0_(
gil::at_c<mapping_transform<Layout, L2, 0>::value>(c))
441 , v1_(
gil::at_c<mapping_transform<Layout, L2, 1>::value>(c))
442 , v2_(
gil::at_c<mapping_transform<Layout, L2, 2>::value>(c))
443 , v3_(
gil::at_c<mapping_transform<Layout, L2, 3>::value>(c))
444 , v4_(
gil::at_c<mapping_transform<Layout, L2, 4>::value>(c))
448 template <
typename E2,
typename L2>
449 homogeneous_color_base(homogeneous_color_base<E2, L2, 5>& c)
450 : v0_(
gil::at_c<mapping_transform<Layout, L2, 0>::value>(c))
451 , v1_(
gil::at_c<mapping_transform<Layout, L2, 1>::value>(c))
452 , v2_(
gil::at_c<mapping_transform<Layout, L2, 2>::value>(c))
453 , v3_(
gil::at_c<mapping_transform<Layout, L2, 3>::value>(c))
454 , v4_(
gil::at_c<mapping_transform<Layout, L2, 4>::value>(c))
458 template <
typename P>
459 homogeneous_color_base(P* p,
bool)
460 : v0_(&semantic_at_c<0>(*p))
461 , v1_(&semantic_at_c<1>(*p))
462 , v2_(&semantic_at_c<2>(*p))
463 , v3_(&semantic_at_c<3>(*p))
464 , v4_(&semantic_at_c<4>(*p))
468 template <
typename Ptr>
469 homogeneous_color_base(Ptr
const& ptr, std::ptrdiff_t diff)
470 : v0_(*memunit_advanced(semantic_at_c<0>(ptr), diff))
471 , v1_(*memunit_advanced(semantic_at_c<1>(ptr), diff))
472 , v2_(*memunit_advanced(semantic_at_c<2>(ptr), diff))
473 , v3_(*memunit_advanced(semantic_at_c<3>(ptr), diff))
474 , v4_(*memunit_advanced(semantic_at_c<4>(ptr), diff))
478 auto at(std::integral_constant<int, 0>)
482 auto at(std::integral_constant<int, 0>)
const
486 auto at(std::integral_constant<int, 1>)
490 auto at(std::integral_constant<int, 1>)
const
494 auto at(std::integral_constant<int, 2>)
498 auto at(std::integral_constant<int, 2>)
const
502 auto at(std::integral_constant<int, 3>)
506 auto at(std::integral_constant<int, 3>)
const
510 auto at(std::integral_constant<int, 4>)
514 auto at(std::integral_constant<int, 4>)
const
518 template <
typename Ref>
522 *semantic_at_c<0>(*
this),
523 *semantic_at_c<1>(*
this),
524 *semantic_at_c<2>(*
this),
525 *semantic_at_c<3>(*
this),
526 *semantic_at_c<4>(*
this));
530 auto at_c_dynamic(std::size_t i)
const -> Element
550 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
564 template <
typename Element,
typename Layout,
int K>
565 auto dynamic_at_c(homogeneous_color_base<Element,Layout,K>& cb, std::size_t i)
569 return (gil_reinterpret_cast<Element*>(&cb))[i];
572 template <
typename Element,
typename Layout,
int K>
573 auto dynamic_at_c(homogeneous_color_base<Element, Layout, K>
const& cb, std::size_t i)
576 homogeneous_color_base<Element, Layout, K>
580 return (gil_reinterpret_cast_c<const Element*>(&cb))[i];
583 template <
typename Element,
typename Layout,
int K>
584 auto dynamic_at_c(homogeneous_color_base<Element&, Layout, K>
const& cb, std::size_t i)
585 ->
typename element_reference_type
587 homogeneous_color_base<Element&, Layout, K>
591 return cb.at_c_dynamic(i);
594 template <
typename Element,
typename Layout,
int K>
596 homogeneous_color_base<Element const&, Layout, K>
const& cb, std::size_t i)
597 ->
typename element_const_reference_type
599 homogeneous_color_base<Element const&, Layout, K>
603 return cb.at_c_dynamic(i);
608 template <
typename Element,
typename Layout,
int K1,
int K>
609 struct kth_element_type<detail::homogeneous_color_base<Element, Layout, K1>, K>
611 using type = Element;
614 template <
typename Element,
typename Layout,
int K1,
int K>
615 struct kth_element_reference_type<detail::homogeneous_color_base<Element, Layout, K1>, K>
616 : std::add_lvalue_reference<Element>
619 template <
typename Element,
typename Layout,
int K1,
int K>
620 struct kth_element_const_reference_type
622 detail::homogeneous_color_base<Element, Layout, K1>,
625 : std::add_lvalue_reference<typename std::add_const<Element>::type>
630 template <
int K,
typename E,
typename L,
int N>
632 auto at_c(detail::homogeneous_color_base<E, L, N>& p)
633 ->
typename std::add_lvalue_reference<E>::type
635 return p.at(std::integral_constant<int, K>());
640 template <
int K,
typename E,
typename L,
int N>
642 auto at_c(
const detail::homogeneous_color_base<E, L, N>& p)
643 ->
typename std::add_lvalue_reference<typename std::add_const<E>::type>::type
645 return p.at(std::integral_constant<int, K>());
653 template <
typename T>
654 void operator()(T& x, T& y)
const
663 template <
typename E,
typename L,
int N>
666 detail::homogeneous_color_base<E, L, N>& x,
667 detail::homogeneous_color_base<E, L, N>& y)
669 static_for_each(x, y, detail::swap_fn());
auto semantic_at_c(const ColorBase &p) -> typename kth_semantic_element_const_reference_type< ColorBase, K >::type
A constant accessor to the K-th semantic element of a color base.
Definition: color_base_algorithm.hpp:133
auto at_c(const detail::homogeneous_color_base< E, L, N > &p) -> typename std::add_lvalue_reference< typename std::add_const< E >::type >::type
Provides constant access to the K-th element, in physical order.
Definition: color_base.hpp:642
void swap(boost::gil::packed_channel_reference< BF, FB, NB, M > const x, R &y)
swap for packed_channel_reference
Definition: channel.hpp:583
defined(BOOST_NO_CXX17_HDR_MEMORY_RESOURCE)
Definition: algorithm.hpp:36
Specifies the return type of the constant element accessor at_c of a homogeneous color base.
Definition: color_base_algorithm.hpp:235
Specifies the return type of the mutable element accessor at_c of a homogeneous color base.
Definition: color_base_algorithm.hpp:230