|
| 1 | +// This file is part of OpenCV project. |
| 2 | +// It is subject to the license terms in the LICENSE file found in the top-level directory |
| 3 | +// of this distribution and at http://opencv.org/license.html. |
| 4 | +// |
| 5 | +// Copyright (C) 2021 Intel Corporation |
| 6 | + |
| 7 | +#ifndef OPENCV_GAPI_PYTHON_BRIDGE_HPP |
| 8 | +#define OPENCV_GAPI_PYTHON_BRIDGE_HPP |
| 9 | + |
| 10 | +#include <opencv2/gapi.hpp> |
| 11 | +#include <opencv2/gapi/garg.hpp> |
| 12 | +#include <opencv2/gapi/gopaque.hpp> |
| 13 | + |
| 14 | +#define ID(T, E) T |
| 15 | +#define ID_(T, E) ID(T, E), |
| 16 | + |
| 17 | +#define WRAP_ARGS(T, E, G) \ |
| 18 | + G(T, E) |
| 19 | + |
| 20 | +#define SWITCH(type, LIST_G, HC) \ |
| 21 | + switch(type) { \ |
| 22 | + LIST_G(HC, HC) \ |
| 23 | + default: \ |
| 24 | + GAPI_Assert(false && "Unsupported type"); \ |
| 25 | + } |
| 26 | + |
| 27 | +#define GARRAY_TYPE_LIST_G(G, G2) \ |
| 28 | +WRAP_ARGS(bool , cv::gapi::ArgType::CV_BOOL, G) \ |
| 29 | +WRAP_ARGS(int , cv::gapi::ArgType::CV_INT, G) \ |
| 30 | +WRAP_ARGS(double , cv::gapi::ArgType::CV_DOUBLE, G) \ |
| 31 | +WRAP_ARGS(float , cv::gapi::ArgType::CV_FLOAT, G) \ |
| 32 | +WRAP_ARGS(std::string , cv::gapi::ArgType::CV_STRING, G) \ |
| 33 | +WRAP_ARGS(cv::Point , cv::gapi::ArgType::CV_POINT, G) \ |
| 34 | +WRAP_ARGS(cv::Point2f , cv::gapi::ArgType::CV_POINT2F, G) \ |
| 35 | +WRAP_ARGS(cv::Size , cv::gapi::ArgType::CV_SIZE, G) \ |
| 36 | +WRAP_ARGS(cv::Rect , cv::gapi::ArgType::CV_RECT, G) \ |
| 37 | +WRAP_ARGS(cv::Scalar , cv::gapi::ArgType::CV_SCALAR, G) \ |
| 38 | +WRAP_ARGS(cv::Mat , cv::gapi::ArgType::CV_MAT, G) \ |
| 39 | +WRAP_ARGS(cv::GMat , cv::gapi::ArgType::CV_GMAT, G2) |
| 40 | + |
| 41 | +#define GOPAQUE_TYPE_LIST_G(G, G2) \ |
| 42 | +WRAP_ARGS(bool , cv::gapi::ArgType::CV_BOOL, G) \ |
| 43 | +WRAP_ARGS(int , cv::gapi::ArgType::CV_INT, G) \ |
| 44 | +WRAP_ARGS(double , cv::gapi::ArgType::CV_DOUBLE, G) \ |
| 45 | +WRAP_ARGS(float , cv::gapi::ArgType::CV_FLOAT, G) \ |
| 46 | +WRAP_ARGS(std::string , cv::gapi::ArgType::CV_STRING, G) \ |
| 47 | +WRAP_ARGS(cv::Point , cv::gapi::ArgType::CV_POINT, G) \ |
| 48 | +WRAP_ARGS(cv::Point2f , cv::gapi::ArgType::CV_POINT2F, G) \ |
| 49 | +WRAP_ARGS(cv::Size , cv::gapi::ArgType::CV_SIZE, G) \ |
| 50 | +WRAP_ARGS(cv::Rect , cv::gapi::ArgType::CV_RECT, G2) \ |
| 51 | + |
| 52 | +namespace cv { |
| 53 | +namespace gapi { |
| 54 | + |
| 55 | +// NB: cv.gapi.CV_BOOL in python |
| 56 | +enum ArgType { |
| 57 | + CV_BOOL, |
| 58 | + CV_INT, |
| 59 | + CV_DOUBLE, |
| 60 | + CV_FLOAT, |
| 61 | + CV_STRING, |
| 62 | + CV_POINT, |
| 63 | + CV_POINT2F, |
| 64 | + CV_SIZE, |
| 65 | + CV_RECT, |
| 66 | + CV_SCALAR, |
| 67 | + CV_MAT, |
| 68 | + CV_GMAT, |
| 69 | +}; |
| 70 | + |
| 71 | +} // namespace gapi |
| 72 | + |
| 73 | +namespace detail { |
| 74 | + |
| 75 | +template <template <typename> class Wrapper, typename T> |
| 76 | +struct WrapType { using type = Wrapper<T>; }; |
| 77 | + |
| 78 | +template <template <typename> class T, typename... Types> |
| 79 | +using MakeVariantType = cv::util::variant<typename WrapType<T, Types>::type...>; |
| 80 | + |
| 81 | +template<typename T> struct ArgTypeTraits; |
| 82 | + |
| 83 | +#define DEFINE_TYPE_TRAITS(T, E) \ |
| 84 | +template <> \ |
| 85 | +struct ArgTypeTraits<T> { \ |
| 86 | + static constexpr const cv::gapi::ArgType type = E; \ |
| 87 | +}; \ |
| 88 | + |
| 89 | +GARRAY_TYPE_LIST_G(DEFINE_TYPE_TRAITS, DEFINE_TYPE_TRAITS) |
| 90 | + |
| 91 | +} // namespace detail |
| 92 | + |
| 93 | +class GAPI_EXPORTS_W_SIMPLE GOpaqueT |
| 94 | +{ |
| 95 | +public: |
| 96 | + using Storage = cv::detail::MakeVariantType<cv::GOpaque, GOPAQUE_TYPE_LIST_G(ID_, ID)>; |
| 97 | + |
| 98 | + template<typename T> |
| 99 | + GOpaqueT(cv::GOpaque<T> arg) : m_type(cv::detail::ArgTypeTraits<T>::type), m_arg(arg) { }; |
| 100 | + |
| 101 | + GAPI_WRAP GOpaqueT(gapi::ArgType type) : m_type(type) |
| 102 | + { |
| 103 | + |
| 104 | +#define HC(T, K) case K: \ |
| 105 | + m_arg = cv::GOpaque<T>(); \ |
| 106 | + break; |
| 107 | + |
| 108 | + SWITCH(type, GOPAQUE_TYPE_LIST_G, HC) |
| 109 | +#undef HC |
| 110 | + } |
| 111 | + |
| 112 | + cv::detail::GOpaqueU strip() { |
| 113 | +#define HC(T, K) case Storage:: index_of<cv::GOpaque<T>>(): \ |
| 114 | + return cv::util::get<cv::GOpaque<T>>(m_arg).strip(); \ |
| 115 | + |
| 116 | + SWITCH(m_arg.index(), GOPAQUE_TYPE_LIST_G, HC) |
| 117 | +#undef HC |
| 118 | + |
| 119 | + GAPI_Assert(false); |
| 120 | + } |
| 121 | + |
| 122 | +GAPI_WRAP gapi::ArgType type() { return m_type; } |
| 123 | + |
| 124 | +private: |
| 125 | + gapi::ArgType m_type; |
| 126 | + Storage m_arg; |
| 127 | +}; |
| 128 | + |
| 129 | +class GAPI_EXPORTS_W_SIMPLE GArrayT |
| 130 | +{ |
| 131 | +public: |
| 132 | + using Storage = cv::detail::MakeVariantType<cv::GArray, GARRAY_TYPE_LIST_G(ID_, ID)>; |
| 133 | + |
| 134 | + template<typename T> |
| 135 | + GArrayT(cv::GArray<T> arg) : m_type(cv::detail::ArgTypeTraits<T>::type), m_arg(arg) { }; |
| 136 | + |
| 137 | + GAPI_WRAP GArrayT(gapi::ArgType type) : m_type(type) |
| 138 | + { |
| 139 | + |
| 140 | +#define HC(T, K) case K: \ |
| 141 | + m_arg = cv::GArray<T>(); \ |
| 142 | + break; |
| 143 | + |
| 144 | + SWITCH(type, GARRAY_TYPE_LIST_G, HC) |
| 145 | +#undef HC |
| 146 | + } |
| 147 | + |
| 148 | + cv::detail::GArrayU strip() { |
| 149 | +#define HC(T, K) case Storage:: index_of<cv::GArray<T>>(): \ |
| 150 | + return cv::util::get<cv::GArray<T>>(m_arg).strip(); \ |
| 151 | + |
| 152 | + SWITCH(m_arg.index(), GARRAY_TYPE_LIST_G, HC) |
| 153 | +#undef HC |
| 154 | + |
| 155 | + GAPI_Assert(false); |
| 156 | + } |
| 157 | + |
| 158 | + GAPI_WRAP gapi::ArgType type() { return m_type; } |
| 159 | + |
| 160 | +private: |
| 161 | + gapi::ArgType m_type; |
| 162 | + Storage m_arg; |
| 163 | +}; |
| 164 | + |
| 165 | +} // namespace cv |
| 166 | + |
| 167 | +#endif // OPENCV_GAPI_PYTHON_BRIDGE_HPP |
0 commit comments