Skip to content

Commit 121fa04

Browse files
committed
srgb support added
1 parent 5ea1c72 commit 121fa04

File tree

1 file changed

+80
-6
lines changed

1 file changed

+80
-6
lines changed

modules/imgproc/test/test_color.cpp

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class CV_ColorCvtBaseTest : public cvtest::ArrayTest
8787
int fwd_code, inv_code;
8888
bool test_cpp;
8989
int hue_range;
90+
bool srgb;
9091
};
9192

9293

@@ -108,6 +109,7 @@ CV_ColorCvtBaseTest::CV_ColorCvtBaseTest( bool _custom_inv_transform, bool _allo
108109
test_cpp = false;
109110
hue_range = 0;
110111
blue_idx = 0;
112+
srgb = false;
111113
inplace = false;
112114
}
113115

@@ -146,6 +148,7 @@ void CV_ColorCvtBaseTest::get_test_array_types_and_sizes( int test_case_idx,
146148

147149
cn = (cvtest::randInt(rng) & 1) + 3;
148150
blue_idx = cvtest::randInt(rng) & 1 ? 2 : 0;
151+
srgb = (cvtest::randInt(rng) & 1) != 0;
149152

150153
types[INPUT][0] = CV_MAKETYPE(depth, cn);
151154
types[OUTPUT][0] = types[REF_OUTPUT][0] = CV_MAKETYPE(depth, 3);
@@ -1004,6 +1007,16 @@ void CV_ColorXYZTest::convert_row_abc2bgr_32f_c3( const float* src_row, float* d
10041007

10051008

10061009
//// rgb <=> L*a*b*
1010+
static inline float applyGamma(float x)
1011+
{
1012+
return x <= 0.04045f ? x*(1.f/12.92f) : (float)std::pow((double)(x + 0.055)*(1./1.055), 2.4);
1013+
}
1014+
1015+
static inline float applyInvGamma(float x)
1016+
{
1017+
return x <= 0.0031308 ? x*12.92f : (float)(1.055*std::pow((double)x, 1./2.4) - 0.055);
1018+
}
1019+
10071020
class CV_ColorLabTest : public CV_ColorCvtBaseTest
10081021
{
10091022
public:
@@ -1026,10 +1039,20 @@ void CV_ColorLabTest::get_test_array_types_and_sizes( int test_case_idx, vector<
10261039
{
10271040
CV_ColorCvtBaseTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
10281041

1029-
if( blue_idx == 0 )
1030-
fwd_code = CV_LBGR2Lab, inv_code = CV_Lab2LBGR;
1042+
if(srgb)
1043+
{
1044+
if( blue_idx == 0 )
1045+
fwd_code = CV_BGR2Lab, inv_code = CV_Lab2BGR;
1046+
else
1047+
fwd_code = CV_RGB2Lab, inv_code = CV_Lab2RGB;
1048+
}
10311049
else
1032-
fwd_code = CV_LRGB2Lab, inv_code = CV_Lab2LRGB;
1050+
{
1051+
if( blue_idx == 0 )
1052+
fwd_code = CV_LBGR2Lab, inv_code = CV_Lab2LBGR;
1053+
else
1054+
fwd_code = CV_LRGB2Lab, inv_code = CV_Lab2LRGB;
1055+
}
10331056
}
10341057

10351058

@@ -1065,6 +1088,16 @@ void CV_ColorLabTest::convert_row_bgr2abc_32f_c3(const float* src_row, float* ds
10651088
float G = src_row[x + 1];
10661089
float B = src_row[x];
10671090

1091+
R = std::min(std::max(R, 0.f), 1.f);
1092+
G = std::min(std::max(G, 0.f), 1.f);
1093+
B = std::min(std::max(B, 0.f), 1.f);
1094+
if (srgb)
1095+
{
1096+
R = applyGamma(R);
1097+
G = applyGamma(G);
1098+
B = applyGamma(B);
1099+
}
1100+
10681101
float X = (R * M[0] + G * M[1] + B * M[2]) / Xn;
10691102
float Y = R * M[3] + G * M[4] + B * M[5];
10701103
float Z = (R * M[6] + G * M[7] + B * M[8]) / Zn;
@@ -1139,6 +1172,16 @@ void CV_ColorLabTest::convert_row_abc2bgr_32f_c3( const float* src_row, float* d
11391172
float G = M[3] * X + M[4] * Y + M[5] * Z;
11401173
float B = M[6] * X + M[7] * Y + M[8] * Z;
11411174

1175+
R = std::min(std::max(R, 0.f), 1.f);
1176+
G = std::min(std::max(G, 0.f), 1.f);
1177+
B = std::min(std::max(B, 0.f), 1.f);
1178+
if (srgb)
1179+
{
1180+
R = applyInvGamma(R);
1181+
G = applyInvGamma(G);
1182+
B = applyInvGamma(B);
1183+
}
1184+
11421185
dst_row[x] = B;
11431186
dst_row[x + 1] = G;
11441187
dst_row[x + 2] = R;
@@ -1169,10 +1212,20 @@ void CV_ColorLuvTest::get_test_array_types_and_sizes( int test_case_idx, vector<
11691212
{
11701213
CV_ColorCvtBaseTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
11711214

1172-
if( blue_idx == 0 )
1173-
fwd_code = CV_LBGR2Luv, inv_code = CV_Luv2LBGR;
1215+
if(srgb)
1216+
{
1217+
if( blue_idx == 0 )
1218+
fwd_code = CV_BGR2Luv, inv_code = CV_Luv2BGR;
1219+
else
1220+
fwd_code = CV_RGB2Luv, inv_code = CV_Luv2RGB;
1221+
}
11741222
else
1175-
fwd_code = CV_LRGB2Luv, inv_code = CV_Luv2LRGB;
1223+
{
1224+
if( blue_idx == 0 )
1225+
fwd_code = CV_LBGR2Luv, inv_code = CV_Luv2LBGR;
1226+
else
1227+
fwd_code = CV_LRGB2Luv, inv_code = CV_Luv2LRGB;
1228+
}
11761229
}
11771230

11781231

@@ -1224,6 +1277,16 @@ void CV_ColorLuvTest::convert_row_bgr2abc_32f_c3( const float* src_row, float* d
12241277
float g = src_row[j+1];
12251278
float b = src_row[j];
12261279

1280+
r = std::min(std::max(r, 0.f), 1.f);
1281+
g = std::min(std::max(g, 0.f), 1.f);
1282+
b = std::min(std::max(b, 0.f), 1.f);
1283+
if( srgb )
1284+
{
1285+
r = applyGamma(r);
1286+
g = applyGamma(g);
1287+
b = applyGamma(b);
1288+
}
1289+
12271290
float X = r*M[0] + g*M[1] + b*M[2];
12281291
float Y = r*M[3] + g*M[4] + b*M[5];
12291292
float Z = r*M[6] + g*M[7] + b*M[8];
@@ -1309,6 +1372,17 @@ void CV_ColorLuvTest::convert_row_abc2bgr_32f_c3( const float* src_row, float* d
13091372
float g = M[3]*X + M[4]*Y + M[5]*Z;
13101373
float b = M[6]*X + M[7]*Y + M[8]*Z;
13111374

1375+
r = std::min(std::max(r, 0.f), 1.f);
1376+
g = std::min(std::max(g, 0.f), 1.f);
1377+
b = std::min(std::max(b, 0.f), 1.f);
1378+
1379+
if( srgb )
1380+
{
1381+
r = applyInvGamma(r);
1382+
g = applyInvGamma(g);
1383+
b = applyInvGamma(b);
1384+
}
1385+
13121386
dst_row[j] = b;
13131387
dst_row[j+1] = g;
13141388
dst_row[j+2] = r;

0 commit comments

Comments
 (0)