Unverified Commit f21bde4d authored by Alexander Alekhin's avatar Alexander Alekhin Committed by GitHub

Merge pull request #16046 from alalek:issue_15990

* core: disable invalid constructors in C API by default

- C API objects will lose their default initializers through constructors

* samples: stop using of C API
parent 986c5084
...@@ -44,8 +44,10 @@ ...@@ -44,8 +44,10 @@
#ifndef OPENCV_CORE_TYPES_H #ifndef OPENCV_CORE_TYPES_H
#define OPENCV_CORE_TYPES_H #define OPENCV_CORE_TYPES_H
#if !defined(__OPENCV_BUILD) && !defined(CV__DISABLE_C_API_CTORS) #ifdef CV__ENABLE_C_API_CTORS // invalid C API ctors (must be removed)
#define CV__ENABLE_C_API_CTORS // enable C API ctors (must be removed) #if defined(_WIN32) && !defined(CV__SKIP_MESSAGE_MALFORMED_C_API_CTORS)
#error "C API ctors don't work on Win32: https://github.com/opencv/opencv/issues/15990"
#endif
#endif #endif
//#define CV__VALIDATE_UNUNITIALIZED_VARS 1 // C++11 & GCC only //#define CV__VALIDATE_UNUNITIALIZED_VARS 1 // C++11 & GCC only
......
...@@ -17,10 +17,9 @@ static void help() ...@@ -17,10 +17,9 @@ static void help()
"./image [image-name Default: lena.jpg]\n" << endl; "./image [image-name Default: lena.jpg]\n" << endl;
} }
// enable/disable use of mixed API in the code below. //#define USE_LEGACY_C_API 1 // not working with modern OpenCV
#define DEMO_MIXED_API_USE 1
#ifdef DEMO_MIXED_API_USE #ifdef USE_LEGACY_C_API
# include <opencv2/highgui/highgui_c.h> # include <opencv2/highgui/highgui_c.h>
# include <opencv2/imgcodecs/imgcodecs_c.h> # include <opencv2/imgcodecs/imgcodecs_c.h>
#endif #endif
...@@ -34,7 +33,7 @@ int main( int argc, char** argv ) ...@@ -34,7 +33,7 @@ int main( int argc, char** argv )
return 0; return 0;
} }
string imagename = parser.get<string>("@image"); string imagename = parser.get<string>("@image");
#if DEMO_MIXED_API_USE #ifdef USE_LEGACY_C_API
//! [iplimage] //! [iplimage]
Ptr<IplImage> iplimg(cvLoadImage(imagename.c_str())); // Ptr<T> is safe ref-counting pointer class Ptr<IplImage> iplimg(cvLoadImage(imagename.c_str())); // Ptr<T> is safe ref-counting pointer class
if(!iplimg) if(!iplimg)
...@@ -94,7 +93,7 @@ int main( int argc, char** argv ) ...@@ -94,7 +93,7 @@ int main( int argc, char** argv )
const double brightness_gain = 0; const double brightness_gain = 0;
const double contrast_gain = 1.7; const double contrast_gain = 1.7;
#if DEMO_MIXED_API_USE #ifdef USE_LEGACY_C_API
// it's easy to pass the new matrices to the functions that only work with IplImage or CvMat: // it's easy to pass the new matrices to the functions that only work with IplImage or CvMat:
// step 1) - convert the headers, data will not be copied // step 1) - convert the headers, data will not be copied
IplImage cv_planes_0 = planes[0], cv_noise = noise; IplImage cv_planes_0 = planes[0], cv_noise = noise;
...@@ -121,7 +120,7 @@ int main( int argc, char** argv ) ...@@ -121,7 +120,7 @@ int main( int argc, char** argv )
// this is counterpart for cvNamedWindow // this is counterpart for cvNamedWindow
namedWindow("image with grain", WINDOW_AUTOSIZE); namedWindow("image with grain", WINDOW_AUTOSIZE);
#if DEMO_MIXED_API_USE #ifdef USE_LEGACY_C_API
// this is to demonstrate that img and iplimg really share the data - the result of the above // this is to demonstrate that img and iplimg really share the data - the result of the above
// processing is stored in img and thus in iplimg too. // processing is stored in img and thus in iplimg too.
cvShowImage("image with grain", iplimg); cvShowImage("image with grain", iplimg);
......
...@@ -19,21 +19,21 @@ static void help( char* progName) ...@@ -19,21 +19,21 @@ static void help( char* progName)
<< progName << " [image-name Default: ../data/lena.jpg]" << endl << endl; << progName << " [image-name Default: ../data/lena.jpg]" << endl << endl;
} }
//! [start] //#define USE_LEGACY_C_API 1 // not working with modern OpenCV
// comment out the define to use only the latest C++ API
#define DEMO_MIXED_API_USE
#ifdef DEMO_MIXED_API_USE #ifdef DEMO_MIXED_API_USE
# include <opencv2/highgui/highgui_c.h> # include <opencv2/highgui/highgui_c.h>
# include <opencv2/imgcodecs/imgcodecs_c.h> # include <opencv2/imgcodecs/imgcodecs_c.h>
#endif #endif
//! [start]
int main( int argc, char** argv ) int main( int argc, char** argv )
{ {
help(argv[0]); help(argv[0]);
const char* imagename = argc > 1 ? argv[1] : "../data/lena.jpg"; const char* imagename = argc > 1 ? argv[1] : "../data/lena.jpg";
#ifdef DEMO_MIXED_API_USE #ifdef USE_LEGACY_C_API
Ptr<IplImage> IplI(cvLoadImage(imagename)); // Ptr<T> is a safe ref-counting pointer class Ptr<IplImage> IplI(cvLoadImage(imagename)); // Ptr<T> is a safe ref-counting pointer class
if(!IplI) if(!IplI)
{ {
...@@ -101,7 +101,7 @@ int main( int argc, char** argv ) ...@@ -101,7 +101,7 @@ int main( int argc, char** argv )
const double brightness_gain = 0; const double brightness_gain = 0;
const double contrast_gain = 1.7; const double contrast_gain = 1.7;
#ifdef DEMO_MIXED_API_USE #ifdef USE_LEGACY_C_API
// To pass the new matrices to the functions that only work with IplImage or CvMat do: // To pass the new matrices to the functions that only work with IplImage or CvMat do:
// step 1) Convert the headers (tip: data will not be copied). // step 1) Convert the headers (tip: data will not be copied).
// step 2) call the function (tip: to pass a pointer do not forget unary "&" to form pointers) // step 2) call the function (tip: to pass a pointer do not forget unary "&" to form pointers)
...@@ -133,7 +133,7 @@ int main( int argc, char** argv ) ...@@ -133,7 +133,7 @@ int main( int argc, char** argv )
namedWindow("image with grain", WINDOW_AUTOSIZE); // use this to create images namedWindow("image with grain", WINDOW_AUTOSIZE); // use this to create images
#ifdef DEMO_MIXED_API_USE #ifdef USE_LEGACY_C_API
// this is to demonstrate that I and IplI really share the data - the result of the above // this is to demonstrate that I and IplI really share the data - the result of the above
// processing is stored in I and thus in IplI too. // processing is stored in I and thus in IplI too.
cvShowImage("image with grain", IplI); cvShowImage("image with grain", IplI);
......
...@@ -131,8 +131,8 @@ int main(int,char**) ...@@ -131,8 +131,8 @@ int main(int,char**)
{ {
//! [C-API conversion] //! [C-API conversion]
Mat img = imread("image.jpg"); Mat img = imread("image.jpg");
IplImage img1 = img; IplImage img1 = cvIplImage(img);
CvMat m = img; CvMat m = cvMat(img);
//! [C-API conversion] //! [C-API conversion]
CV_UNUSED(img1); CV_UNUSED(img1);
CV_UNUSED(m); CV_UNUSED(m);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment