From 98d678c2d20f54233814255c79afab06fe6f05a7 Mon Sep 17 00:00:00 2001 From: Alexander Smorkalov Date: Mon, 22 May 2023 15:28:57 +0300 Subject: [PATCH] Added check that YUYV input of cvtColor has even width. --- modules/imgproc/src/color.simd_helpers.hpp | 6 +++++- modules/imgproc/src/color_yuv.dispatch.cpp | 2 +- modules/imgproc/test/test_cvtyuv.cpp | 11 ++++++++++- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/modules/imgproc/src/color.simd_helpers.hpp b/modules/imgproc/src/color.simd_helpers.hpp index 343491f2c6..3d457d5d14 100644 --- a/modules/imgproc/src/color.simd_helpers.hpp +++ b/modules/imgproc/src/color.simd_helpers.hpp @@ -72,7 +72,7 @@ struct Set enum SizePolicy { - TO_YUV, FROM_YUV, NONE + TO_YUV, FROM_YUV, FROM_UYVY, NONE }; template< typename VScn, typename VDcn, typename VDepth, SizePolicy sizePolicy = NONE > @@ -104,6 +104,10 @@ struct CvtHelper CV_Assert( sz.width % 2 == 0 && sz.height % 3 == 0); dstSz = Size(sz.width, sz.height * 2 / 3); break; + case FROM_UYVY: + CV_Assert( sz.width % 2 == 0); + dstSz = sz; + break; case NONE: default: dstSz = sz; diff --git a/modules/imgproc/src/color_yuv.dispatch.cpp b/modules/imgproc/src/color_yuv.dispatch.cpp index cac4fa1b41..559005e07f 100644 --- a/modules/imgproc/src/color_yuv.dispatch.cpp +++ b/modules/imgproc/src/color_yuv.dispatch.cpp @@ -354,7 +354,7 @@ void cvtColorYUV2BGR(InputArray _src, OutputArray _dst, int dcn, bool swapb, boo void cvtColorOnePlaneYUV2BGR( InputArray _src, OutputArray _dst, int dcn, bool swapb, int uidx, int ycn) { - CvtHelper< Set<2>, Set<3, 4>, Set > h(_src, _dst, dcn); + CvtHelper< Set<2>, Set<3, 4>, Set, FROM_UYVY > h(_src, _dst, dcn); hal::cvtOnePlaneYUVtoBGR(h.src.data, h.src.step, h.dst.data, h.dst.step, h.src.cols, h.src.rows, dcn, swapb, uidx, ycn); diff --git a/modules/imgproc/test/test_cvtyuv.cpp b/modules/imgproc/test/test_cvtyuv.cpp index 2a2f72ffe2..cb49baab0a 100644 --- a/modules/imgproc/test/test_cvtyuv.cpp +++ b/modules/imgproc/test/test_cvtyuv.cpp @@ -724,4 +724,13 @@ INSTANTIATE_TEST_CASE_P(cvt422, Imgproc_ColorYUV, (int)COLOR_YUV2RGBA_YUY2, (int)COLOR_YUV2BGRA_YUY2, (int)COLOR_YUV2RGBA_YVYU, (int)COLOR_YUV2BGRA_YVYU, (int)COLOR_YUV2GRAY_UYVY, (int)COLOR_YUV2GRAY_YUY2)); -}} // namespace +} + +TEST(cvtColorUYVY, size_issue_21035) +{ + Mat input = Mat::zeros(1, 1, CV_8UC2); + Mat output; + EXPECT_THROW(cv::cvtColor(input, output, cv::COLOR_YUV2BGR_UYVY), cv::Exception); +} + +} // namespace