Skip to content

Commit 40428d9

Browse files
authored
Merge pull request opencv#26259 from Kumataro:fix26258
core: C-API cleanup: RNG algorithms in core(4.x) opencv#26259 - replace CV_RAND_UNI and NORMAL to cv::RNG::UNIFORM and cv::RNG::NORMAL. ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake
1 parent 28efc21 commit 40428d9

File tree

5 files changed

+59
-25
lines changed

5 files changed

+59
-25
lines changed

modules/core/src/rand.cpp

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ void RNG::fill( InputOutputArray _mat, int disttype,
409409
(((_param2.rows == 1 || _param2.cols == 1) &&
410410
(_param2.rows + _param2.cols - 1 == cn || _param2.rows + _param2.cols - 1 == 1 ||
411411
(_param1.size() == Size(1, 4) && _param1.type() == CV_64F && cn <= 4))) ||
412-
(_param2.rows == cn && _param2.cols == cn && disttype == NORMAL)));
412+
(_param2.rows == cn && _param2.cols == cn && disttype == RNG::NORMAL)));
413413

414414
Vec2i* ip = 0;
415415
Vec2d* dp = 0;
@@ -421,7 +421,7 @@ void RNG::fill( InputOutputArray _mat, int disttype,
421421
int n1 = (int)_param1.total();
422422
int n2 = (int)_param2.total();
423423

424-
if( disttype == UNIFORM )
424+
if( disttype == RNG::UNIFORM )
425425
{
426426
_parambuf.allocate(cn*8 + n1 + n2);
427427
double* parambuf = _parambuf.data();
@@ -535,7 +535,7 @@ void RNG::fill( InputOutputArray _mat, int disttype,
535535
}
536536
CV_Assert( func != 0 );
537537
}
538-
else if( disttype == CV_RAND_NORMAL )
538+
else if( disttype == RNG::NORMAL )
539539
{
540540
_parambuf.allocate(MAX(n1, cn) + MAX(n2, cn));
541541
double* parambuf = _parambuf.data();
@@ -586,7 +586,7 @@ void RNG::fill( InputOutputArray _mat, int disttype,
586586
float* nbuf = 0;
587587
float* tmpbuf = 0;
588588

589-
if( disttype == UNIFORM )
589+
if( disttype == RNG::UNIFORM )
590590
{
591591
buf.allocate(blockSize*cn*4);
592592
param = (uchar*)(double*)buf.data();
@@ -637,7 +637,7 @@ void RNG::fill( InputOutputArray _mat, int disttype,
637637
{
638638
int len = std::min(total - j, blockSize);
639639

640-
if( disttype == CV_RAND_UNI )
640+
if( disttype == RNG::UNIFORM )
641641
func( ptr, len*cn, &state, param, tmpbuf, smallFlag );
642642
else
643643
{
@@ -753,23 +753,57 @@ void cv::randShuffle( InputOutputArray _dst, double iterFactor, RNG* _rng )
753753

754754
#ifndef OPENCV_EXCLUDE_C_API
755755

756+
// Related with https://github.com/opencv/opencv/issues/26258
757+
// To suppress cast-user-defined warning for casting CvRNG to cv::RNG& with GCC14.
758+
// ( CvRNG is uint64, and cv::RNG has only status member which is uint64. )
759+
760+
#if defined(__GNUC__) && __GNUC__ >= 14
761+
#define CV_IGNORE_CAST_USER_DEFINED_WARNING
762+
#endif
763+
756764
CV_IMPL void
757765
cvRandArr( CvRNG* _rng, CvArr* arr, int disttype, CvScalar param1, CvScalar param2 )
758766
{
759767
cv::Mat mat = cv::cvarrToMat(arr);
768+
769+
#ifdef CV_IGNORE_CAST_USER_DEFINED_WARNING
770+
#pragma GCC diagnostic push
771+
#pragma GCC diagnostic ignored "-Wcast-user-defined"
772+
#endif
773+
760774
// !!! this will only work for current 64-bit MWC RNG !!!
761775
cv::RNG& rng = _rng ? (cv::RNG&)*_rng : cv::theRNG();
776+
777+
#ifdef CV_IGNORE_CAST_USER_DEFINED_WARNING
778+
#pragma GCC diagnostic pop
779+
#endif
780+
762781
rng.fill(mat, disttype == CV_RAND_NORMAL ?
763782
cv::RNG::NORMAL : cv::RNG::UNIFORM, cv::Scalar(param1), cv::Scalar(param2) );
764783
}
765784

766785
CV_IMPL void cvRandShuffle( CvArr* arr, CvRNG* _rng, double iter_factor )
767786
{
768787
cv::Mat dst = cv::cvarrToMat(arr);
788+
789+
#ifdef CV_IGNORE_CAST_USER_DEFINED_WARNING
790+
#pragma GCC diagnostic push
791+
#pragma GCC diagnostic ignored "-Wcast-user-defined"
792+
#endif
793+
769794
cv::RNG& rng = _rng ? (cv::RNG&)*_rng : cv::theRNG();
795+
796+
#ifdef CV_IGNORE_CAST_USER_DEFINED_WARNING
797+
#pragma GCC diagnostic pop
798+
#endif
799+
770800
cv::randShuffle( dst, iter_factor, &rng );
771801
}
772802

803+
#ifdef CV_IGNORE_CAST_USER_DEFINED_WARNING
804+
#undef CV_IGNORE_CAST_USER_DEFINED_WARNING
805+
#endif
806+
773807
#endif // OPENCV_EXCLUDE_C_API
774808

775809

modules/core/test/test_io.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,12 @@ class Core_IOTest : public cvtest::BaseTest
118118
int cn = cvtest::randInt(rng) % 4 + 1;
119119
Mat test_mat(cvtest::randInt(rng)%30+1, cvtest::randInt(rng)%30+1, CV_MAKETYPE(depth, cn));
120120

121-
rng0.fill(test_mat, CV_RAND_UNI, Scalar::all(ranges[depth][0]), Scalar::all(ranges[depth][1]));
121+
rng0.fill(test_mat, RNG::UNIFORM, Scalar::all(ranges[depth][0]), Scalar::all(ranges[depth][1]));
122122
if( depth >= CV_32F )
123123
{
124124
exp(test_mat, test_mat);
125125
Mat test_mat_scale(test_mat.size(), test_mat.type());
126-
rng0.fill(test_mat_scale, CV_RAND_UNI, Scalar::all(-1), Scalar::all(1));
126+
rng0.fill(test_mat_scale, RNG::UNIFORM, Scalar::all(-1), Scalar::all(1));
127127
cv::multiply(test_mat, test_mat_scale, test_mat);
128128
}
129129

@@ -136,12 +136,12 @@ class Core_IOTest : public cvtest::BaseTest
136136
};
137137
MatND test_mat_nd(3, sz, CV_MAKETYPE(depth, cn));
138138

139-
rng0.fill(test_mat_nd, CV_RAND_UNI, Scalar::all(ranges[depth][0]), Scalar::all(ranges[depth][1]));
139+
rng0.fill(test_mat_nd, RNG::UNIFORM, Scalar::all(ranges[depth][0]), Scalar::all(ranges[depth][1]));
140140
if( depth >= CV_32F )
141141
{
142142
exp(test_mat_nd, test_mat_nd);
143143
MatND test_mat_scale(test_mat_nd.dims, test_mat_nd.size, test_mat_nd.type());
144-
rng0.fill(test_mat_scale, CV_RAND_UNI, Scalar::all(-1), Scalar::all(1));
144+
rng0.fill(test_mat_scale, RNG::UNIFORM, Scalar::all(-1), Scalar::all(1));
145145
cv::multiply(test_mat_nd, test_mat_scale, test_mat_nd);
146146
}
147147

modules/core/test/test_mat.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -650,8 +650,8 @@ void Core_ArrayOpTest::run( int /* start_from */)
650650
MatND A(3, sz3, CV_32F), B(3, sz3, CV_16SC4);
651651
CvMatND matA = cvMatND(A), matB = cvMatND(B);
652652
RNG rng;
653-
rng.fill(A, CV_RAND_UNI, Scalar::all(-10), Scalar::all(10));
654-
rng.fill(B, CV_RAND_UNI, Scalar::all(-10), Scalar::all(10));
653+
rng.fill(A, RNG::UNIFORM, Scalar::all(-10), Scalar::all(10));
654+
rng.fill(B, RNG::UNIFORM, Scalar::all(-10), Scalar::all(10));
655655

656656
int idx0[] = {3,4,5}, idx1[] = {0, 9, 7};
657657
float val0 = 130;
@@ -807,7 +807,7 @@ void Core_ArrayOpTest::run( int /* start_from */)
807807
all_vals.resize(nz0);
808808
all_vals2.resize(nz0);
809809
Mat_<double> _all_vals(all_vals), _all_vals2(all_vals2);
810-
rng.fill(_all_vals, CV_RAND_UNI, Scalar(-1000), Scalar(1000));
810+
rng.fill(_all_vals, RNG::UNIFORM, Scalar(-1000), Scalar(1000));
811811
if( depth == CV_32F )
812812
{
813813
Mat _all_vals_f;

modules/core/test/test_rand.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ bool Core_RandTest::check_pdf(const Mat& hist, double scale,
4848
sum += H[i];
4949
CV_Assert( fabs(1./sum - scale) < FLT_EPSILON );
5050

51-
if( dist_type == CV_RAND_UNI )
51+
if( dist_type == RNG::UNIFORM )
5252
{
5353
float scale0 = (float)(1./hsz);
5454
for( i = 0; i < hsz; i++ )
@@ -79,7 +79,7 @@ bool Core_RandTest::check_pdf(const Mat& hist, double scale,
7979
}
8080
realval = chi2;
8181

82-
double chi2_pval = chi2_p95(hsz - 1 - (dist_type == CV_RAND_NORMAL ? 2 : 0));
82+
double chi2_pval = chi2_p95(hsz - 1 - (dist_type == RNG::NORMAL ? 2 : 0));
8383
refval = chi2_pval*0.01;
8484
return realval <= refval;
8585
}
@@ -108,26 +108,26 @@ void Core_RandTest::run( int )
108108
int depth = cvtest::randInt(rng) % (CV_64F+1);
109109
int c, cn = (cvtest::randInt(rng) % 4) + 1;
110110
int type = CV_MAKETYPE(depth, cn);
111-
int dist_type = cvtest::randInt(rng) % (CV_RAND_NORMAL+1);
111+
int dist_type = cvtest::randInt(rng) % (RNG::NORMAL+1);
112112
int i, k, SZ = N/cn;
113113
Scalar A, B;
114114

115115
double eps = 1.e-4;
116116
if (depth == CV_64F)
117117
eps = 1.e-7;
118118

119-
bool do_sphere_test = dist_type == CV_RAND_UNI;
119+
bool do_sphere_test = dist_type == RNG::UNIFORM;
120120
Mat arr[2], hist[4];
121121
int W[] = {0,0,0,0};
122122

123123
arr[0].create(1, SZ, type);
124124
arr[1].create(1, SZ, type);
125-
bool fast_algo = dist_type == CV_RAND_UNI && depth < CV_32F;
125+
bool fast_algo = dist_type == RNG::UNIFORM && depth < CV_32F;
126126

127127
for( c = 0; c < cn; c++ )
128128
{
129129
int a, b, hsz;
130-
if( dist_type == CV_RAND_UNI )
130+
if( dist_type == RNG::UNIFORM )
131131
{
132132
a = (int)(cvtest::randInt(rng) % (_ranges[depth][1] -
133133
_ranges[depth][0])) + _ranges[depth][0];
@@ -188,8 +188,8 @@ void Core_RandTest::run( int )
188188
const uchar* data = arr[0].ptr();
189189
int* H = hist[c].ptr<int>();
190190
int HSZ = hist[c].cols;
191-
double minVal = dist_type == CV_RAND_UNI ? A[c] : A[c] - B[c]*4;
192-
double maxVal = dist_type == CV_RAND_UNI ? B[c] : A[c] + B[c]*4;
191+
double minVal = dist_type == RNG::UNIFORM ? A[c] : A[c] - B[c]*4;
192+
double maxVal = dist_type == RNG::UNIFORM ? B[c] : A[c] + B[c]*4;
193193
double scale = HSZ/(maxVal - minVal);
194194
double delta = -minVal*scale;
195195

@@ -210,7 +210,7 @@ void Core_RandTest::run( int )
210210
H[ival]++;
211211
W[c]++;
212212
}
213-
else if( dist_type == CV_RAND_UNI )
213+
else if( dist_type == RNG::UNIFORM )
214214
{
215215
if( (minVal <= val && val < maxVal) || (depth >= CV_32F && val == maxVal) )
216216
{
@@ -224,14 +224,14 @@ void Core_RandTest::run( int )
224224
}
225225
}
226226

227-
if( dist_type == CV_RAND_UNI && W[c] != SZ )
227+
if( dist_type == RNG::UNIFORM && W[c] != SZ )
228228
{
229229
ts->printf( cvtest::TS::LOG, "Uniform RNG gave values out of the range [%g,%g) on channel %d/%d\n",
230230
A[c], B[c], c, cn);
231231
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
232232
return;
233233
}
234-
if( dist_type == CV_RAND_NORMAL && W[c] < SZ*.90)
234+
if( dist_type == RNG::NORMAL && W[c] < SZ*.90)
235235
{
236236
ts->printf( cvtest::TS::LOG, "Normal RNG gave too many values out of the range (%g+4*%g,%g+4*%g) on channel %d/%d\n",
237237
A[c], B[c], A[c], B[c], c, cn);

modules/imgproc/test/test_imgwarp.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ int CV_WarpAffineTest::prepare_test_case( int test_case_idx )
558558
angle = cvtest::randReal(rng)*360;
559559
scale = ((double)dst.rows/src.rows + (double)dst.cols/src.cols)*0.5;
560560
getRotationMatrix2D(center, angle, scale).convertTo(mat, mat.depth());
561-
rng.fill( tmp, CV_RAND_NORMAL, Scalar::all(1.), Scalar::all(0.01) );
561+
rng.fill( tmp, RNG::NORMAL, Scalar::all(1.), Scalar::all(0.01) );
562562
cv::max(tmp, 0.9, tmp);
563563
cv::min(tmp, 1.1, tmp);
564564
cv::multiply(tmp, mat, mat, 1.);
@@ -673,7 +673,7 @@ int CV_WarpPerspectiveTest::prepare_test_case( int test_case_idx )
673673
float bufer[16];
674674
Mat tmp( 1, 16, CV_32FC1, bufer );
675675

676-
rng.fill( tmp, CV_RAND_NORMAL, Scalar::all(0.), Scalar::all(0.1) );
676+
rng.fill( tmp, RNG::NORMAL, Scalar::all(0.), Scalar::all(0.1) );
677677

678678
for( i = 0; i < 4; i++ )
679679
{

0 commit comments

Comments
 (0)