From bfc4eb31cb09d74b96a4ec21ac6eb95225d85e94 Mon Sep 17 00:00:00 2001 From: Vladislav Sovrasov Date: Fri, 5 May 2017 14:41:46 +0300 Subject: [PATCH] imgproc: fix BGRA2BGRA conversion --- modules/imgproc/src/color.cpp | 12 ++++++------ modules/imgproc/test/test_color.cpp | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/modules/imgproc/src/color.cpp b/modules/imgproc/src/color.cpp index f07b1f65e0..ecf8f46109 100644 --- a/modules/imgproc/src/color.cpp +++ b/modules/imgproc/src/color.cpp @@ -688,7 +688,7 @@ template struct RGB2RGB for( int i = 0; i < n; i += 4 ) { _Tp t0 = src[i], t1 = src[i+1], t2 = src[i+2], t3 = src[i+3]; - dst[i] = t2; dst[i+1] = t1; dst[i+2] = t0; dst[i+3] = t3; + dst[i+bidx] = t0; dst[i+1] = t1; dst[i+(bidx^2)] = t2; dst[i+3] = t3; } } } @@ -802,25 +802,25 @@ template<> struct RGB2RGB for ( ; i <= n - 64; i += 64 ) { uint8x16x4_t v_src = vld4q_u8(src + i), v_dst; - v_dst.val[0] = v_src.val[2]; + v_dst.val[0] = v_src.val[bidx]; v_dst.val[1] = v_src.val[1]; - v_dst.val[2] = v_src.val[0]; + v_dst.val[2] = v_src.val[bidx^2]; v_dst.val[3] = v_src.val[3]; vst4q_u8(dst + i, v_dst); } for ( ; i <= n - 32; i += 32 ) { uint8x8x4_t v_src = vld4_u8(src + i), v_dst; - v_dst.val[0] = v_src.val[2]; + v_dst.val[0] = v_src.val[bidx]; v_dst.val[1] = v_src.val[1]; - v_dst.val[2] = v_src.val[0]; + v_dst.val[2] = v_src.val[bidx^2]; v_dst.val[3] = v_src.val[3]; vst4_u8(dst + i, v_dst); } for ( ; i < n; i += 4) { uchar t0 = src[i], t1 = src[i+1], t2 = src[i+2], t3 = src[i+3]; - dst[i] = t2; dst[i+1] = t1; dst[i+2] = t0; dst[i+3] = t3; + dst[i+bidx] = t0; dst[i+1] = t1; dst[i+(bidx^2)] = t2; dst[i+3] = t3; } } } diff --git a/modules/imgproc/test/test_color.cpp b/modules/imgproc/test/test_color.cpp index 2af2990752..75ae3ddddf 100644 --- a/modules/imgproc/test/test_color.cpp +++ b/modules/imgproc/test/test_color.cpp @@ -2190,3 +2190,28 @@ TEST(ImgProc_Bayer2RGBA, accuracy) } } } + +TEST(ImgProc_BGR2RGBA, regression_8696) +{ + Mat src(Size(80, 10), CV_8UC4); + src.setTo(Scalar(255, 0, 200, 100)); + + Mat dst; + cvtColor(src, dst, COLOR_BGR2BGRA); + + EXPECT_DOUBLE_EQ(norm(dst - src, NORM_INF), 0.); +} + +TEST(ImgProc_BGR2RGBA, 3ch24ch) +{ + Mat src(Size(80, 10), CV_8UC3); + src.setTo(Scalar(200, 0, 200)); + + Mat dst; + cvtColor(src, dst, COLOR_BGR2BGRA); + + Mat expected(Size(80, 10), CV_8UC4); + expected.setTo(Scalar(80, 0, 200, 255)); + + EXPECT_DOUBLE_EQ(norm(expected - dst, NORM_INF), 0.); +}