8 #ifndef BOOST_GIL_EXTENSION_DYNAMIC_IMAGE_GIL_REDUCE_HPP 9 #define BOOST_GIL_EXTENSION_DYNAMIC_IMAGE_GIL_REDUCE_HPP 11 #ifdef BOOST_GIL_DOXYGEN_ONLY 12 #undef BOOST_GIL_REDUCE_CODE_BLOAT 15 #ifdef BOOST_GIL_REDUCE_CODE_BLOAT 17 #include <boost/gil/extension/dynamic_image/dynamic_at_c.hpp> 19 #include <boost/gil/metafunctions.hpp> 20 #include <boost/gil/typedefs.hpp> 22 #include <boost/mpl/back.hpp> 23 #include <boost/mpl/insert.hpp> 24 #include <boost/mpl/insert_range.hpp> 25 #include <boost/mpl/long.hpp> 26 #include <boost/mpl/logical.hpp> 27 #include <boost/mpl/range_c.hpp> 28 #include <boost/mpl/vector.hpp> 29 #include <boost/mpl/vector_c.hpp> 30 #include <boost/mpl/transform.hpp> 33 #define GIL_BINARY_REDUCE_LIMIT 226 35 namespace boost {
namespace mpl {
48 template <
typename SrcTypes,
typename DstTypes>
49 struct mapping_vector {};
51 template <
typename SrcTypes,
typename DstTypes,
long K>
52 struct at_c<mapping_vector<SrcTypes,DstTypes>, K>
54 static const std::size_t value=size<DstTypes>::value - order<DstTypes, typename gil::at_c<SrcTypes,K>::type>::type::value +1;
55 using type = size_t<value>;
58 template <
typename SrcTypes,
typename DstTypes>
59 struct size<mapping_vector<SrcTypes,DstTypes>>
61 using type =
typename size<SrcTypes>::type;
62 static const std::size_t value=type::value;
75 template <
typename SFirst, std::
size_t NLeft>
76 struct copy_to_vector_impl {
78 using T =
typename deref<SFirst>::type;
79 using next =
typename next<SFirst>::type;
80 using rest =
typename copy_to_vector_impl<next, NLeft-1>::type;
82 using type =
typename push_front<rest, T>::type;
85 template <
typename SFirst>
86 struct copy_to_vector_impl<SFirst,1>
88 using type = vector<typename deref<SFirst>::type>;
92 template <
typename Src>
95 using type =
typename detail::copy_to_vector_impl<typename begin<Src>::type, size<Src>::value>::type;
99 struct copy_to_vector<set<>>
101 using type = vector0<>;
106 namespace boost {
namespace gil {
130 template <
typename Types,
typename Op>
131 struct unary_reduce_impl {
132 using reduced_t =
typename mpl::transform<Types, detail::reduce<Op, mpl::_1> >::type;
133 using unique_t =
typename mpl::copy<reduced_t, mpl::inserter<mpl::set<>, mpl::insert<mpl::_1,mpl::_2>>>::type;
134 static const bool is_single=mpl::size<unique_t>::value==1;
137 template <typename Types, typename Op, bool IsSingle=unary_reduce_impl<Types,Op>::is_single>
138 struct unary_reduce :
public unary_reduce_impl<Types,Op>
140 using reduced_t =
typename unary_reduce_impl<Types,Op>::reduced_t;
141 using unique_t =
typename unary_reduce_impl<Types,Op>::unique_t;
143 static unsigned short inline map_index(std::size_t index)
145 using indices_t =
typename mpl::mapping_vector<reduced_t, unique_t>;
146 return gil::at_c<indices_t, unsigned short>(index);
148 template <
typename Bits> BOOST_FORCEINLINE
static typename Op::result_type applyc(
const Bits& bits, std::size_t index, Op op) {
149 return apply_operation_basec<unique_t>(bits,map_index(index),op);
152 template <
typename Bits> BOOST_FORCEINLINE
static typename Op::result_type apply(Bits& bits, std::size_t index, Op op) {
153 return apply_operation_base<unique_t>(bits,map_index(index),op);
157 template <
typename Types,
typename Op>
158 struct unary_reduce<Types,Op,true> :
public unary_reduce_impl<Types,Op> {
159 using unique_t =
typename unary_reduce_impl<Types,Op>::unique_t;
160 static unsigned short inline map_index(std::size_t index) {
return 0; }
162 template <
typename Bits> BOOST_FORCEINLINE
static typename Op::result_type applyc(
const Bits& bits, std::size_t index, Op op) {
163 return op(*gil_reinterpret_cast_c<
const typename mpl::front<unique_t>::type*>(&bits));
166 template <
typename Bits> BOOST_FORCEINLINE
static typename Op::result_type apply(Bits& bits, std::size_t index, Op op) {
167 return op(*gil_reinterpret_cast<
typename mpl::front<unique_t>::type*>(&bits));
186 struct pair_generator {
187 template <
typename Vec2>
struct apply
189 using type = std::pair<const typename mpl::at_c<Vec2,0>::type*,
const typename mpl::at_c<Vec2,1>::type*>;
194 template <
typename Unary1,
typename Unary2,
typename Op,
bool IsComplex>
195 struct binary_reduce_impl
198 using vec1_types =
typename mpl::copy_to_vector<typename Unary1::unique_t>::type;
199 using vec2_types =
typename mpl::copy_to_vector<typename Unary2::unique_t>::type;
201 using BIN_TYPES = mpl::cross_vector<mpl::vector2<vec1_types, vec2_types>, pair_generator>;
202 using bin_reduced_t = unary_reduce<BIN_TYPES,Op>;
204 static unsigned short inline map_index(std::size_t index1, std::size_t index2) {
205 unsigned short r1=Unary1::map_index(index1);
206 unsigned short r2=Unary2::map_index(index2);
207 return bin_reduced_t::map_index(r2*mpl::size<vec1_types>::value + r1);
210 using unique_t =
typename bin_reduced_t::unique_t
212 template <
typename Bits1,
typename Bits2>
213 static typename Op::result_type
inline apply(
const Bits1& bits1, std::size_t index1,
const Bits2& bits2, std::size_t index2, Op op) {
214 std::pair<const void*,const void*> pr(&bits1, &bits2);
215 return apply_operation_basec<unique_t>(pr, map_index(index1,index2),op);
220 template <
typename Unary1,
typename Unary2,
typename Op>
221 struct binary_reduce_impl<Unary1,Unary2,Op,true> {
222 template <
typename Bits1,
typename Bits2>
223 static typename Op::result_type
inline apply(
const Bits1& bits1, std::size_t index1,
const Bits2& bits2, std::size_t index2, Op op) {
224 return apply_operation_base<Unary1::unique_t,Unary2::unique_t>(bits1, index1, bits2, index2, op);
230 template <
typename Types1,
typename Types2,
typename Op>
234 using unary1_t = unary_reduce<Types1,Op>;
235 using unary2_t = unary_reduce<Types2,Op>;
237 static const std::size_t CROSS_SIZE = mpl::size<typename unary1_t::unique_t>::value *
238 mpl::size<typename unary2_t::unique_t>::value;
240 using impl = detail::binary_reduce_impl<unary1_t,unary2_t,Op, (CROSS_SIZE>GIL_BINARY_REDUCE_LIMIT)>;
242 template <
typename Bits1,
typename Bits2>
243 static typename Op::result_type
inline apply(
const Bits1& bits1, std::size_t index1,
const Bits2& bits2, std::size_t index2, Op op) {
244 return impl::apply(bits1,index1,bits2,index2,op);
248 template <
typename Types,
typename UnaryOp>
249 BOOST_FORCEINLINE
typename UnaryOp::result_type
apply_operation(variant<Types>& arg, UnaryOp op) {
250 return unary_reduce<Types,UnaryOp>::template apply(arg._bits, arg._index ,op);
253 template <
typename Types,
typename UnaryOp>
254 BOOST_FORCEINLINE
typename UnaryOp::result_type
apply_operation(
const variant<Types>& arg, UnaryOp op) {
255 return unary_reduce<Types,UnaryOp>::template applyc(arg._bits, arg._index ,op);
258 template <
typename Types1,
typename Types2,
typename BinaryOp>
259 BOOST_FORCEINLINE
typename BinaryOp::result_type
apply_operation(
const variant<Types1>& arg1,
const variant<Types2>& arg2, BinaryOp op) {
260 return binary_reduce<Types1,Types2,BinaryOp>::template apply(arg1._bits, arg1._index, arg2._bits, arg2._index, op);
263 #undef GIL_BINARY_REDUCE_LIMIT 268 namespace boost {
namespace mpl {
288 template <
typename VecOfVecs,
typename TypeGen>
289 struct cross_vector {};
293 template <
typename VecOfVecs,
typename TypeGen, std::
size_t K>
294 struct cross_iterator
296 using category = mpl::random_access_iterator_tag;
308 template <
typename VecOfVecs,
typename TypeGen, std::
size_t K>
309 struct deref<cross_iterator<VecOfVecs,TypeGen,K>>
312 using DerefTypes =
typename detail::select_subvector_c<VecOfVecs, K>::type;
314 using type =
typename TypeGen::template apply<DerefTypes>::type;
319 template <
typename VecOfVecs,
typename TypeGen, std::
size_t K>
320 struct next<cross_iterator<VecOfVecs,TypeGen,K>>
322 using type = cross_iterator<VecOfVecs,TypeGen,K+1>;
327 template <
typename VecOfVecs,
typename TypeGen, std::
size_t K>
328 struct prior<cross_iterator<VecOfVecs,TypeGen,K>>
330 using type = cross_iterator<VecOfVecs,TypeGen,K-1>;
335 template <
typename VecOfVecs,
typename TypeGen, std::
size_t K,
typename Distance>
336 struct advance<cross_iterator<VecOfVecs,TypeGen,K>, Distance>
338 using type = cross_iterator<VecOfVecs,TypeGen,K+Distance::value>;
344 template <
typename VecOfVecs,
typename TypeGen, std::
size_t K1, std::
size_t K2>
345 struct distance<cross_iterator<VecOfVecs,TypeGen,K1>, cross_iterator<VecOfVecs,TypeGen,K2>>
347 using type =
size_t<K2-K1>;
355 template <
typename VecOfVecs,
typename TypeGen>
356 struct size<cross_vector<VecOfVecs,TypeGen>>
358 using type =
typename fold<VecOfVecs, size_t<1>, times<_1, size<_2>>>::type;
359 static const std::size_t value=type::value;
364 template <
typename VecOfVecs,
typename TypeGen>
365 struct empty<cross_vector<VecOfVecs,TypeGen>> {
366 using type =
typename empty<VecOfVecs>::type;
371 template <
typename VecOfVecs,
typename TypeGen,
typename K>
372 struct at<cross_vector<VecOfVecs,TypeGen>, K>
375 using KthIterator = cross_iterator<VecOfVecs,TypeGen,K::value>;
377 using type =
typename deref<KthIterator>::type;
382 template <
typename VecOfVecs,
typename TypeGen>
383 struct begin<cross_vector<VecOfVecs,TypeGen>>
385 using type = cross_iterator<VecOfVecs,TypeGen,0>;
390 template <
typename VecOfVecs,
typename TypeGen>
391 struct end<cross_vector<VecOfVecs,TypeGen>>
394 using this_t = cross_vector<VecOfVecs,TypeGen>;
396 using type = cross_iterator<VecOfVecs,TypeGen,size<this_t>::value>;
401 template <
typename VecOfVecs,
typename TypeGen>
402 struct front<cross_vector<VecOfVecs,TypeGen>> {
404 using this_t = cross_vector<VecOfVecs,TypeGen>;
406 using type =
typename deref<typename begin<this_t>::type>::type;
411 template <
typename VecOfVecs,
typename TypeGen>
412 struct back<cross_vector<VecOfVecs,TypeGen>>
415 using this_t = cross_vector<VecOfVecs,TypeGen>;
416 using size =
typename size<this_t>::type;
417 using last_index =
typename minus<size, size_t<1>>::type;
419 using type =
typename at<this_t, last_index>::type;
424 template <
typename VecOfVecs,
typename TypeGen,
typename OPP>
425 struct transform<cross_vector<VecOfVecs,TypeGen>, OPP>
427 using Op =
typename lambda<OPP>::type;
430 template <
typename Elements>
433 using orig_t =
typename TypeGen::template apply<Elements>::type;
434 using type =
typename Op::template apply<orig_t>::type;
437 using type = cross_vector<VecOfVecs, adapter>;
442 namespace boost {
namespace gil {
444 template <
typename Types,
typename T>
struct type_to_index;
445 template <
typename V>
struct view_is_basic;
460 template <
typename Op,
typename T>
473 template <
typename Op,
typename View,
bool IsBasic>
474 struct reduce_view_basic
479 template <
typename Op,
typename Loc>
480 struct reduce<Op, image_view<Loc>>
481 :
public reduce_view_basic<Op,image_view<Loc>,view_is_basic<image_view<Loc>>::value> {};
490 template <
typename Op,
typename Img,
bool IsBasic>
491 struct reduce_image_basic
496 template <
typename Op,
typename V,
typename Alloc>
497 struct reduce<Op, image<V,Alloc>> :
public reduce_image_basic<Op,image<V,Alloc>,image_is_basic<image<V,Alloc>>::value > {};
506 template <
typename Op,
typename V1,
typename V2,
bool AreBasic>
507 struct reduce_views_basic
509 using type = std::pair<const V1*, const V2*>;
512 template <
typename Op,
typename L1,
typename L2>
513 struct reduce<Op,
std::pair<const image_view<L1>*, const image_view<L2>*>>
514 :
public reduce_views_basic<Op,image_view<L1>,image_view<L2>,
515 mpl::and_<view_is_basic<image_view<L1>>, view_is_basic<image_view<L2>>>::value >
525 template <
typename CS>
526 struct reduce_color_space
531 template <>
struct reduce_color_space<lab_t> {
using type = rgb_t; };
532 template <>
struct reduce_color_space<hsb_t> {
using type = rgb_t; };
533 template <>
struct reduce_color_space<cmyk_t> {
using type = rgba_t; };
641 template <
typename SrcLayout,
typename DstLayout>
642 struct reduce_color_layouts
644 using first_t = SrcLayout;
645 using second_t = DstLayout;
654 struct copy_pixels_fn;
668 template <
typename V1,
typename V2,
bool Compatible>
669 struct reduce_copy_pixop_compat
671 using type = error_t;
676 template <
typename V1,
typename V2>
677 struct reduce_copy_pixop_compat<V1,V2,true>
679 using layout1 = layout<typename V1::color_space_t, typename V1::channel_mapping_t>;
680 using layout2 = layout<typename V2::color_space_t, typename V2::channel_mapping_t>;
682 using L1 =
typename reduce_color_layouts<layout1,layout2>::first_t;
683 using L2 =
typename reduce_color_layouts<layout1,layout2>::second_t;
685 using DV1 =
typename derived_view_type<V1, use_default, L1, use_default, use_default, use_default, mpl::false_>::type;
686 using DV2 =
typename derived_view_type<V2, use_default, L2, use_default, use_default, use_default, mpl::true_ >::type;
688 using type = std::pair<const DV1*, const DV2*>;
692 template <
typename V1,
typename V2>
693 struct reduce_views_basic<copy_pixels_fn, V1, V2, true>
694 :
public reduce_copy_pixop_compat<V1, V2, mpl::and_<views_are_compatible<V1,V2>, view_is_mutable<V2>>::value > {
703 struct destructor_op;
704 template <
typename View>
705 struct reduce_view_basic<destructor_op,View,true>
707 using type = gray8_view_t;
716 struct any_type_get_dimensions;
718 template <
typename View>
719 struct reduce_view_basic<any_type_get_dimensions,View,true>
721 using type = gray8_view_t;
724 template <
typename Img>
725 struct reduce_image_basic<any_type_get_dimensions,Img,true>
727 using type = gray8_image_t;
736 struct any_type_get_num_channels;
738 template <
typename View>
739 struct reduce_view_basic<any_type_get_num_channels,View,true>
741 using color_space_t =
typename View::color_space_t::base;
742 using type =
typename view_type<uint8_t,typename reduce_color_space<color_space_t>::type>::type;
745 template <
typename Img>
746 struct reduce_image_basic<any_type_get_num_channels,Img,true>
748 using color_space_t =
typename Img::color_space_t::base;
749 using type =
typename image_type<uint8_t,typename reduce_color_space<color_space_t>::type>::type;
758 template <
typename Sampler,
typename MapFn>
struct resample_pixels_fn;
760 template <
typename S,
typename M,
typename V,
bool IsBasic>
761 struct reduce_view_basic<resample_pixels_fn<S,M>, V, IsBasic> :
public reduce_view_basic<copy_pixels_fn, V, IsBasic> {};
763 template <
typename S,
typename M,
typename V1,
typename V2,
bool IsBasic>
764 struct reduce_views_basic<resample_pixels_fn<S,M>, V1, V2, IsBasic> :
public reduce_views_basic<copy_pixels_fn, V1, V2, IsBasic> {};
774 template <
typename CC>
class copy_and_convert_pixels_fn;
777 template <
typename CC,
typename View,
bool IsBasic>
778 struct reduce_view_basic<copy_and_convert_pixels_fn<CC>, View, IsBasic>
779 :
public derived_view_type<View, use_default, use_default, use_default, use_default, mpl::true_> {
784 template <
typename CC,
typename V1,
typename V2,
bool AreBasic>
785 struct reduce_views_basic<copy_and_convert_pixels_fn<CC>, V1, V2, AreBasic>
787 using Same = is_same<typename V1::pixel_t, typename V2::pixel_t>;
789 using CsR = reduce_color_space<typename V1::color_space_t::base>;
790 using Cs1 =
typename mpl::if_<Same, typename CsR::type, typename V1::color_space_t>::type;
791 using Cs2 =
typename mpl::if_<Same, typename CsR::type, typename V2::color_space_t>::type;
793 using DV1 =
typename derived_view_type<V1, use_default, layout<Cs1, typename V1::channel_mapping_t>, use_default, use_default, mpl::false_>::type;
794 using DV2 =
typename derived_view_type<V2, use_default, layout<Cs2, typename V2::channel_mapping_t>, use_default, use_default, mpl::true_ >::type;
796 using type = std::pair<const DV1*, const DV2*>;
830 #endif // defined(BOOST_GIL_REDUCE_CODE_BLOAT) Definition: algorithm.hpp:30
add_reference< E >::type at_c(detail::homogeneous_color_base< E, L, N > &p)
Provides mutable access to the K-th element, in physical order.
Definition: color_base.hpp:387
Definition: algorithm.hpp:127
BOOST_FORCEINLINE auto apply_operation(variant< Types > &arg, UnaryOp op)
Invokes a generic mutable operation (represented as a unary function object) on a variant...
Definition: apply_operation.hpp:31