@@ -87,6 +87,7 @@ class CV_ColorCvtBaseTest : public cvtest::ArrayTest
87
87
int fwd_code, inv_code;
88
88
bool test_cpp;
89
89
int hue_range;
90
+ bool srgb;
90
91
};
91
92
92
93
@@ -108,6 +109,7 @@ CV_ColorCvtBaseTest::CV_ColorCvtBaseTest( bool _custom_inv_transform, bool _allo
108
109
test_cpp = false ;
109
110
hue_range = 0 ;
110
111
blue_idx = 0 ;
112
+ srgb = false ;
111
113
inplace = false ;
112
114
}
113
115
@@ -146,6 +148,7 @@ void CV_ColorCvtBaseTest::get_test_array_types_and_sizes( int test_case_idx,
146
148
147
149
cn = (cvtest::randInt (rng) & 1 ) + 3 ;
148
150
blue_idx = cvtest::randInt (rng) & 1 ? 2 : 0 ;
151
+ srgb = (cvtest::randInt (rng) & 1 ) != 0 ;
149
152
150
153
types[INPUT][0 ] = CV_MAKETYPE (depth, cn);
151
154
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
1004
1007
1005
1008
1006
1009
// // 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
+
1007
1020
class CV_ColorLabTest : public CV_ColorCvtBaseTest
1008
1021
{
1009
1022
public:
@@ -1026,10 +1039,20 @@ void CV_ColorLabTest::get_test_array_types_and_sizes( int test_case_idx, vector<
1026
1039
{
1027
1040
CV_ColorCvtBaseTest::get_test_array_types_and_sizes ( test_case_idx, sizes, types );
1028
1041
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
+ }
1031
1049
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
+ }
1033
1056
}
1034
1057
1035
1058
@@ -1065,6 +1088,16 @@ void CV_ColorLabTest::convert_row_bgr2abc_32f_c3(const float* src_row, float* ds
1065
1088
float G = src_row[x + 1 ];
1066
1089
float B = src_row[x];
1067
1090
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
+
1068
1101
float X = (R * M[0 ] + G * M[1 ] + B * M[2 ]) / Xn;
1069
1102
float Y = R * M[3 ] + G * M[4 ] + B * M[5 ];
1070
1103
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
1139
1172
float G = M[3 ] * X + M[4 ] * Y + M[5 ] * Z;
1140
1173
float B = M[6 ] * X + M[7 ] * Y + M[8 ] * Z;
1141
1174
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
+
1142
1185
dst_row[x] = B;
1143
1186
dst_row[x + 1 ] = G;
1144
1187
dst_row[x + 2 ] = R;
@@ -1169,10 +1212,20 @@ void CV_ColorLuvTest::get_test_array_types_and_sizes( int test_case_idx, vector<
1169
1212
{
1170
1213
CV_ColorCvtBaseTest::get_test_array_types_and_sizes ( test_case_idx, sizes, types );
1171
1214
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
+ }
1174
1222
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
+ }
1176
1229
}
1177
1230
1178
1231
@@ -1224,6 +1277,16 @@ void CV_ColorLuvTest::convert_row_bgr2abc_32f_c3( const float* src_row, float* d
1224
1277
float g = src_row[j+1 ];
1225
1278
float b = src_row[j];
1226
1279
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
+
1227
1290
float X = r*M[0 ] + g*M[1 ] + b*M[2 ];
1228
1291
float Y = r*M[3 ] + g*M[4 ] + b*M[5 ];
1229
1292
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
1309
1372
float g = M[3 ]*X + M[4 ]*Y + M[5 ]*Z;
1310
1373
float b = M[6 ]*X + M[7 ]*Y + M[8 ]*Z;
1311
1374
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
+
1312
1386
dst_row[j] = b;
1313
1387
dst_row[j+1 ] = g;
1314
1388
dst_row[j+2 ] = r;
0 commit comments