File indexing completed on 2025-01-18 09:36:55
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef BOOST_GIL_EXTENSION_DYNAMIC_IMAGE_ANY_IMAGE_HPP
0010 #define BOOST_GIL_EXTENSION_DYNAMIC_IMAGE_ANY_IMAGE_HPP
0011
0012 #include <boost/gil/extension/dynamic_image/any_image_view.hpp>
0013
0014 #include <boost/gil/image.hpp>
0015 #include <boost/gil/detail/mp11.hpp>
0016
0017 #include <boost/config.hpp>
0018 #include <boost/variant2/variant.hpp>
0019
0020 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
0021 #pragma warning(push)
0022 #pragma warning(disable:4512)
0023 #endif
0024
0025 namespace boost { namespace gil {
0026
0027 namespace detail {
0028
0029 template <typename T>
0030 using get_view_t = typename T::view_t;
0031
0032 template <typename Images>
0033 using images_get_views_t = mp11::mp_transform<get_view_t, Images>;
0034
0035 template <typename T>
0036 using get_const_view_t = typename T::const_view_t;
0037
0038 template <typename Images>
0039 using images_get_const_views_t = mp11::mp_transform<get_const_view_t, Images>;
0040
0041 struct recreate_image_fnobj
0042 {
0043 using result_type = void;
0044 point<std::ptrdiff_t> const& _dimensions;
0045 unsigned _alignment;
0046
0047 recreate_image_fnobj(point<std::ptrdiff_t> const& dims, unsigned alignment)
0048 : _dimensions(dims), _alignment(alignment)
0049 {}
0050
0051 template <typename Image>
0052 result_type operator()(Image& img) const { img.recreate(_dimensions,_alignment); }
0053 };
0054
0055 template <typename AnyView>
0056 struct any_image_get_view
0057 {
0058 using result_type = AnyView;
0059 template <typename Image>
0060 result_type operator()(Image& img) const
0061 {
0062 return result_type(view(img));
0063 }
0064 };
0065
0066 template <typename AnyConstView>
0067 struct any_image_get_const_view
0068 {
0069 using result_type = AnyConstView;
0070 template <typename Image>
0071 result_type operator()(Image const& img) const { return result_type{const_view(img)}; }
0072 };
0073
0074 }
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087 template <typename ...Images>
0088 class any_image : public variant2::variant<Images...>
0089 {
0090 using parent_t = variant2::variant<Images...>;
0091
0092 public:
0093 using view_t = mp11::mp_rename<detail::images_get_views_t<any_image>, any_image_view>;
0094 using const_view_t = mp11::mp_rename<detail::images_get_const_views_t<any_image>, any_image_view>;
0095 using x_coord_t = std::ptrdiff_t;
0096 using y_coord_t = std::ptrdiff_t;
0097 using point_t = point<std::ptrdiff_t>;
0098
0099 using parent_t::parent_t;
0100
0101 any_image& operator=(any_image const& img)
0102 {
0103 parent_t::operator=((parent_t const&)img);
0104 return *this;
0105 }
0106
0107 template <typename Image>
0108 any_image& operator=(Image const& img)
0109 {
0110 parent_t::operator=(img);
0111 return *this;
0112 }
0113
0114 template <typename ...OtherImages>
0115 any_image& operator=(any_image<OtherImages...> const& img)
0116 {
0117 parent_t::operator=((typename variant2::variant<OtherImages...> const&)img);
0118 return *this;
0119 }
0120
0121 void recreate(point_t const& dims, unsigned alignment=1)
0122 {
0123 variant2::visit(detail::recreate_image_fnobj(dims, alignment), *this);
0124 }
0125
0126 void recreate(x_coord_t width, y_coord_t height, unsigned alignment=1)
0127 {
0128 recreate({ width, height }, alignment);
0129 }
0130
0131 std::size_t num_channels() const
0132 {
0133 return variant2::visit(detail::any_type_get_num_channels(), *this);
0134 }
0135
0136 point_t dimensions() const
0137 {
0138 return variant2::visit(detail::any_type_get_dimensions(), *this);
0139 }
0140
0141 x_coord_t width() const { return dimensions().x; }
0142 y_coord_t height() const { return dimensions().y; }
0143 };
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153 template <typename ...Images>
0154 BOOST_FORCEINLINE
0155 auto view(any_image<Images...>& img) -> typename any_image<Images...>::view_t
0156 {
0157 using view_t = typename any_image<Images...>::view_t;
0158 return variant2::visit(detail::any_image_get_view<view_t>(), img);
0159 }
0160
0161
0162
0163 template <typename ...Images>
0164 BOOST_FORCEINLINE
0165 auto const_view(any_image<Images...> const& img) -> typename any_image<Images...>::const_view_t
0166 {
0167 using view_t = typename any_image<Images...>::const_view_t;
0168 return variant2::visit(detail::any_image_get_const_view<view_t>(), img);
0169 }
0170
0171
0172 }}
0173
0174 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
0175 #pragma warning(pop)
0176 #endif
0177
0178 #endif