gtransform.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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) 2019 Intel Corporation
  6. #ifndef OPENCV_GAPI_GTRANSFORM_HPP
  7. #define OPENCV_GAPI_GTRANSFORM_HPP
  8. #include <functional>
  9. #include <type_traits>
  10. #include <utility>
  11. #include <opencv2/gapi/gcommon.hpp>
  12. #include <opencv2/gapi/util/util.hpp>
  13. #include <opencv2/gapi/garg.hpp>
  14. #include <opencv2/gapi/gtype_traits.hpp>
  15. #include <opencv2/gapi/util/compiler_hints.hpp>
  16. #include <opencv2/gapi/gcomputation.hpp>
  17. namespace cv
  18. {
  19. struct GAPI_EXPORTS GTransform
  20. {
  21. // FIXME: consider another simplified
  22. // class instead of GComputation
  23. using F = std::function<GComputation()>;
  24. std::string description;
  25. F pattern;
  26. F substitute;
  27. GTransform(const std::string& d, const F &p, const F &s) : description(d), pattern(p), substitute(s) {}
  28. };
  29. namespace detail
  30. {
  31. template <typename, typename, typename>
  32. struct TransHelper;
  33. template <typename K, typename... Ins, typename Out>
  34. struct TransHelper<K, std::tuple<Ins...>, Out>
  35. {
  36. template <typename Callable, int... IIs, int... OIs>
  37. static GComputation invoke(Callable f, Seq<IIs...>, Seq<OIs...>)
  38. {
  39. const std::tuple<Ins...> ins;
  40. const auto r = tuple_wrap_helper<Out>::get(f(std::get<IIs>(ins)...));
  41. return GComputation(cv::GIn(std::get<IIs>(ins)...),
  42. cv::GOut(std::get<OIs>(r)...));
  43. }
  44. static GComputation get_pattern()
  45. {
  46. return invoke(K::pattern, typename MkSeq<sizeof...(Ins)>::type(),
  47. typename MkSeq<std::tuple_size<typename tuple_wrap_helper<Out>::type>::value>::type());
  48. }
  49. static GComputation get_substitute()
  50. {
  51. return invoke(K::substitute, typename MkSeq<sizeof...(Ins)>::type(),
  52. typename MkSeq<std::tuple_size<typename tuple_wrap_helper<Out>::type>::value>::type());
  53. }
  54. };
  55. } // namespace detail
  56. template <typename, typename>
  57. class GTransformImpl;
  58. template <typename K, typename R, typename... Args>
  59. class GTransformImpl<K, std::function<R(Args...)>> : public cv::detail::TransHelper<K, std::tuple<Args...>, R>,
  60. public cv::detail::TransformTag
  61. {
  62. public:
  63. // FIXME: currently there is no check that transformations' signatures are unique
  64. // and won't be any intersection in graph compilation stage
  65. using API = K;
  66. static GTransform transformation()
  67. {
  68. return GTransform(K::descr(), &K::get_pattern, &K::get_substitute);
  69. }
  70. };
  71. } // namespace cv
  72. #define G_DESCR_HELPER_CLASS(Class) Class##DescrHelper
  73. #define G_DESCR_HELPER_BODY(Class, Descr) \
  74. namespace detail \
  75. { \
  76. struct G_DESCR_HELPER_CLASS(Class) \
  77. { \
  78. static constexpr const char *descr() { return Descr; }; \
  79. }; \
  80. }
  81. #define GAPI_TRANSFORM(Class, API, Descr) \
  82. G_DESCR_HELPER_BODY(Class, Descr) \
  83. struct Class final : public cv::GTransformImpl<Class, std::function API>, \
  84. public detail::G_DESCR_HELPER_CLASS(Class)
  85. #endif // OPENCV_GAPI_GTRANSFORM_HPP