gapi_render_tests.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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) 2018 Intel Corporation
  6. #ifndef OPENCV_GAPI_RENDER_TESTS_HPP
  7. #define OPENCV_GAPI_RENDER_TESTS_HPP
  8. #include "gapi_tests_common.hpp"
  9. namespace opencv_test
  10. {
  11. template<typename ...SpecificParams>
  12. struct RenderParams : public Params<SpecificParams...>
  13. {
  14. using common_params_t = std::tuple<cv::Size>;
  15. using specific_params_t = std::tuple<SpecificParams...>;
  16. using params_t = std::tuple<cv::Size, SpecificParams...>;
  17. static constexpr const size_t common_params_size = std::tuple_size<common_params_t>::value;
  18. static constexpr const size_t specific_params_size = std::tuple_size<specific_params_t>::value;
  19. template<size_t I>
  20. static const typename std::tuple_element<I, common_params_t>::type&
  21. getCommon(const params_t& t)
  22. {
  23. static_assert(I < common_params_size, "Index out of range");
  24. return std::get<I>(t);
  25. }
  26. template<size_t I>
  27. static const typename std::tuple_element<I, specific_params_t>::type&
  28. getSpecific(const params_t& t)
  29. {
  30. static_assert(specific_params_size > 0,
  31. "Impossible to call this function: no specific parameters specified");
  32. static_assert(I < specific_params_size, "Index out of range");
  33. return std::get<common_params_size + I>(t);
  34. }
  35. };
  36. template<typename ...SpecificParams>
  37. struct RenderTestBase : public TestWithParam<typename RenderParams<SpecificParams...>::params_t>
  38. {
  39. using AllParams = RenderParams<SpecificParams...>;
  40. // Get common (pre-defined) parameter value by index
  41. template<size_t I>
  42. inline auto getCommonParam() const
  43. -> decltype(AllParams::template getCommon<I>(this->GetParam()))
  44. {
  45. return AllParams::template getCommon<I>(this->GetParam());
  46. }
  47. // Get specific (user-defined) parameter value by index
  48. template<size_t I>
  49. inline auto getSpecificParam() const
  50. -> decltype(AllParams::template getSpecific<I>(this->GetParam()))
  51. {
  52. return AllParams::template getSpecific<I>(this->GetParam());
  53. }
  54. cv::Size sz_ = getCommonParam<0>();
  55. };
  56. template <typename ...Args>
  57. class RenderBGRTestBase : public RenderTestBase<Args...>
  58. {
  59. protected:
  60. void Init(const cv::Size& sz)
  61. {
  62. MatType type = CV_8UC3;
  63. ref_mat.create(sz, type);
  64. gapi_mat.create(sz, type);
  65. cv::randu(ref_mat, cv::Scalar::all(0), cv::Scalar::all(255));
  66. ref_mat.copyTo(gapi_mat);
  67. }
  68. cv::Mat gapi_mat, ref_mat;
  69. };
  70. template <typename ...Args>
  71. class RenderNV12TestBase : public RenderTestBase<Args...>
  72. {
  73. protected:
  74. void Init(const cv::Size& sz)
  75. {
  76. auto create_rand_mats = [](const cv::Size& size, MatType type, cv::Mat& ref_mat, cv::Mat& gapi_mat) {
  77. ref_mat.create(size, type);
  78. cv::randu(ref_mat, cv::Scalar::all(0), cv::Scalar::all(255));
  79. ref_mat.copyTo(gapi_mat);
  80. };
  81. create_rand_mats(sz, CV_8UC1, y_ref_mat , y_gapi_mat);
  82. create_rand_mats(sz / 2, CV_8UC2, uv_ref_mat , uv_gapi_mat);
  83. }
  84. cv::Mat y_ref_mat, uv_ref_mat, y_gapi_mat, uv_gapi_mat;
  85. };
  86. cv::Scalar cvtBGRToYUVC(const cv::Scalar& bgr);
  87. void drawMosaicRef(const cv::Mat& mat, const cv::Rect &rect, int cellSz);
  88. void blendImageRef(cv::Mat& mat,
  89. const cv::Point& org,
  90. const cv::Mat& img,
  91. const cv::Mat& alpha);
  92. #define GAPI_RENDER_TEST_FIXTURE_NV12(Fixture, API, Number, ...) \
  93. struct Fixture : public RenderNV12TestBase API { \
  94. __WRAP_VAARGS(DEFINE_SPECIFIC_PARAMS_##Number(__VA_ARGS__)) \
  95. Fixture() { \
  96. Init(sz_); \
  97. }; \
  98. };
  99. #define GAPI_RENDER_TEST_FIXTURE_BGR(Fixture, API, Number, ...) \
  100. struct Fixture : public RenderBGRTestBase API { \
  101. __WRAP_VAARGS(DEFINE_SPECIFIC_PARAMS_##Number(__VA_ARGS__)) \
  102. Fixture() { \
  103. Init(sz_); \
  104. }; \
  105. };
  106. #define GET_VA_ARGS(...) __VA_ARGS__
  107. #define GAPI_RENDER_TEST_FIXTURES(Fixture, API, Number, ...) \
  108. GAPI_RENDER_TEST_FIXTURE_BGR(RenderBGR##Fixture, GET_VA_ARGS(API), Number, __VA_ARGS__) \
  109. GAPI_RENDER_TEST_FIXTURE_NV12(RenderNV12##Fixture, GET_VA_ARGS(API), Number, __VA_ARGS__) \
  110. GAPI_RENDER_TEST_FIXTURE_NV12(RenderMFrame##Fixture, GET_VA_ARGS(API), Number, __VA_ARGS__) \
  111. using Points = std::vector<cv::Point>;
  112. GAPI_RENDER_TEST_FIXTURES(TestTexts, FIXTURE_API(std::string, cv::Point, double, cv::Scalar), 4, text, org, fs, color)
  113. GAPI_RENDER_TEST_FIXTURES(TestRects, FIXTURE_API(cv::Rect, cv::Scalar, int), 3, rect, color, thick)
  114. GAPI_RENDER_TEST_FIXTURES(TestCircles, FIXTURE_API(cv::Point, int, cv::Scalar, int), 4, center, radius, color, thick)
  115. GAPI_RENDER_TEST_FIXTURES(TestLines, FIXTURE_API(cv::Point, cv::Point, cv::Scalar, int), 4, pt1, pt2, color, thick)
  116. GAPI_RENDER_TEST_FIXTURES(TestMosaics, FIXTURE_API(cv::Rect, int, int), 3, mos, cellsz, decim)
  117. GAPI_RENDER_TEST_FIXTURES(TestImages, FIXTURE_API(cv::Rect, cv::Scalar, double), 3, rect, color, transparency)
  118. GAPI_RENDER_TEST_FIXTURES(TestPolylines, FIXTURE_API(Points, cv::Scalar, int), 3, points, color, thick)
  119. } // opencv_test
  120. #endif //OPENCV_GAPI_RENDER_TESTS_HPP