Skip to content

Commit bfc4eb3

Browse files
committed
imgproc: fix BGRA2BGRA conversion
1 parent fe4555e commit bfc4eb3

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

modules/imgproc/src/color.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ template<typename _Tp> struct RGB2RGB
688688
for( int i = 0; i < n; i += 4 )
689689
{
690690
_Tp t0 = src[i], t1 = src[i+1], t2 = src[i+2], t3 = src[i+3];
691-
dst[i] = t2; dst[i+1] = t1; dst[i+2] = t0; dst[i+3] = t3;
691+
dst[i+bidx] = t0; dst[i+1] = t1; dst[i+(bidx^2)] = t2; dst[i+3] = t3;
692692
}
693693
}
694694
}
@@ -802,25 +802,25 @@ template<> struct RGB2RGB<uchar>
802802
for ( ; i <= n - 64; i += 64 )
803803
{
804804
uint8x16x4_t v_src = vld4q_u8(src + i), v_dst;
805-
v_dst.val[0] = v_src.val[2];
805+
v_dst.val[0] = v_src.val[bidx];
806806
v_dst.val[1] = v_src.val[1];
807-
v_dst.val[2] = v_src.val[0];
807+
v_dst.val[2] = v_src.val[bidx^2];
808808
v_dst.val[3] = v_src.val[3];
809809
vst4q_u8(dst + i, v_dst);
810810
}
811811
for ( ; i <= n - 32; i += 32 )
812812
{
813813
uint8x8x4_t v_src = vld4_u8(src + i), v_dst;
814-
v_dst.val[0] = v_src.val[2];
814+
v_dst.val[0] = v_src.val[bidx];
815815
v_dst.val[1] = v_src.val[1];
816-
v_dst.val[2] = v_src.val[0];
816+
v_dst.val[2] = v_src.val[bidx^2];
817817
v_dst.val[3] = v_src.val[3];
818818
vst4_u8(dst + i, v_dst);
819819
}
820820
for ( ; i < n; i += 4)
821821
{
822822
uchar t0 = src[i], t1 = src[i+1], t2 = src[i+2], t3 = src[i+3];
823-
dst[i] = t2; dst[i+1] = t1; dst[i+2] = t0; dst[i+3] = t3;
823+
dst[i+bidx] = t0; dst[i+1] = t1; dst[i+(bidx^2)] = t2; dst[i+3] = t3;
824824
}
825825
}
826826
}

modules/imgproc/test/test_color.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2190,3 +2190,28 @@ TEST(ImgProc_Bayer2RGBA, accuracy)
21902190
}
21912191
}
21922192
}
2193+
2194+
TEST(ImgProc_BGR2RGBA, regression_8696)
2195+
{
2196+
Mat src(Size(80, 10), CV_8UC4);
2197+
src.setTo(Scalar(255, 0, 200, 100));
2198+
2199+
Mat dst;
2200+
cvtColor(src, dst, COLOR_BGR2BGRA);
2201+
2202+
EXPECT_DOUBLE_EQ(norm(dst - src, NORM_INF), 0.);
2203+
}
2204+
2205+
TEST(ImgProc_BGR2RGBA, 3ch24ch)
2206+
{
2207+
Mat src(Size(80, 10), CV_8UC3);
2208+
src.setTo(Scalar(200, 0, 200));
2209+
2210+
Mat dst;
2211+
cvtColor(src, dst, COLOR_BGR2BGRA);
2212+
2213+
Mat expected(Size(80, 10), CV_8UC4);
2214+
expected.setTo(Scalar(80, 0, 200, 255));
2215+
2216+
EXPECT_DOUBLE_EQ(norm(expected - dst, NORM_INF), 0.);
2217+
}

0 commit comments

Comments
 (0)