From fc85f3644d8084e86e76a0583d96de36cecf0985 Mon Sep 17 00:00:00 2001 From: Andrey Golubev Date: Thu, 1 Aug 2019 19:30:05 +0300 Subject: [PATCH] Merge pull request #15190 from andrey-golubev:fluid_invalid_roi G-API: verify y offset and height of Fluid output ROIs (#15190) * G-API: validate GFluidOutputRoi y and height * Rearrange tested ROIs --- .../gapi/src/backends/fluid/gfluidbackend.cpp | 12 ++++-- modules/gapi/test/gapi_fluid_test.cpp | 39 ++++++++++++++++++- 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/modules/gapi/src/backends/fluid/gfluidbackend.cpp b/modules/gapi/src/backends/fluid/gfluidbackend.cpp index 10bf244f5d..1e10783e52 100644 --- a/modules/gapi/src/backends/fluid/gfluidbackend.cpp +++ b/modules/gapi/src/backends/fluid/gfluidbackend.cpp @@ -565,16 +565,20 @@ void cv::gimpl::GFluidExecutable::initBufferRois(std::vector& readStarts, auto id = m_id_map.at(d.rc); readStarts[id] = 0; - if (out_rois[idx] == gapi::own::Rect{}) + const auto& out_roi = out_rois[idx]; + if (out_roi == gapi::own::Rect{}) { rois[id] = gapi::own::Rect{ 0, 0, desc.size.width, desc.size.height }; } else { + GAPI_Assert(out_roi.height > 0); + GAPI_Assert(out_roi.y + out_roi.height <= desc.size.height); + // Only slices are supported at the moment - GAPI_Assert(out_rois[idx].x == 0); - GAPI_Assert(out_rois[idx].width == desc.size.width); - rois[id] = out_rois[idx]; + GAPI_Assert(out_roi.x == 0); + GAPI_Assert(out_roi.width == desc.size.width); + rois[id] = out_roi; } nodesToVisit.push(nh); diff --git a/modules/gapi/test/gapi_fluid_test.cpp b/modules/gapi/test/gapi_fluid_test.cpp index f2d8432c3f..949ef2739e 100644 --- a/modules/gapi/test/gapi_fluid_test.cpp +++ b/modules/gapi/test/gapi_fluid_test.cpp @@ -752,7 +752,8 @@ INSTANTIATE_TEST_CASE_P(Fluid, NV12RoiTest, ,std::make_pair(cv::Size{1920, 1080}, cv::Rect{0, 710, 1920, 270}) )); -TEST(Fluid, UnusedNodeOutputCompileTest) { +TEST(Fluid, UnusedNodeOutputCompileTest) +{ cv::GMat in; cv::GMat a, b, c, d; std::tie(a, b, c, d) = cv::gapi::split4(in); @@ -767,7 +768,8 @@ TEST(Fluid, UnusedNodeOutputCompileTest) { cv::compile_args(cv::gapi::core::fluid::kernels()))); } -TEST(Fluid, UnusedNodeOutputReshapeTest) { +TEST(Fluid, UnusedNodeOutputReshapeTest) +{ const auto test_size = cv::Size(8, 8); const auto get_compile_args = [] () { return cv::compile_args(cv::gapi::core::fluid::kernels()); }; @@ -791,4 +793,37 @@ TEST(Fluid, UnusedNodeOutputReshapeTest) { ASSERT_NO_THROW(compiled(in_mat, out_mat)); } +TEST(Fluid, InvalidROIs) +{ + cv::GMat in; + cv::GMat out = cv::gapi::add(in, in); + + cv::Mat in_mat(cv::Size(8, 8), CV_8UC3); + cv::Mat out_mat = in_mat.clone(); + cv::randu(in_mat, cv::Scalar::all(0), cv::Scalar::all(100)); + + std::vector invalid_rois = + { + cv::Rect(1, 0, 0, 0), + cv::Rect(0, 1, 0, 0), + cv::Rect(0, 0, 1, 0), + cv::Rect(0, 0, 0, 1), + cv::Rect(0, 0, out_mat.cols, 0), + cv::Rect(0, 0, 0, out_mat.rows), + cv::Rect(0, out_mat.rows, out_mat.cols, out_mat.rows), + cv::Rect(out_mat.cols, 0, out_mat.cols, out_mat.rows), + }; + + const auto compile_args = [] (cv::Rect roi) { + return cv::compile_args(cv::gapi::core::fluid::kernels(), GFluidOutputRois{{to_own(roi)}}); + }; + + for (const auto& roi : invalid_rois) + { + cv::GComputation comp(cv::GIn(in), cv::GOut(out)); + EXPECT_THROW(comp.apply(cv::gin(in_mat), cv::gout(out_mat), compile_args(roi)), + std::exception); + } +} + } // namespace opencv_test