diff --git a/modules/core/include/opencv2/core/types_c.h b/modules/core/include/opencv2/core/types_c.h index 81e986fcd1..c412d3202c 100644 --- a/modules/core/include/opencv2/core/types_c.h +++ b/modules/core/include/opencv2/core/types_c.h @@ -44,6 +44,27 @@ #ifndef OPENCV_CORE_TYPES_H #define OPENCV_CORE_TYPES_H +#define CV__ENABLE_C_API_CTORS // enable C API ctors (must be removed) + +//#define CV__VALIDATE_UNUNITIALIZED_VARS 1 // C++11 & GCC only + +#ifdef __cplusplus + +#ifdef CV__VALIDATE_UNUNITIALIZED_VARS +#pragma GCC diagnostic ignored "-Wmissing-field-initializers" +#define CV_STRUCT_INITIALIZER {0,} +#else +#if defined(__GNUC__) && __GNUC__ == 4 // GCC 4.x warns on "= {}" initialization, fixed in GCC 5.0 +#pragma GCC diagnostic ignored "-Wmissing-field-initializers" +#endif +#define CV_STRUCT_INITIALIZER {} +#endif + +#else +#define CV_STRUCT_INITIALIZER {0} +#endif + + #ifdef HAVE_IPL # ifndef __IPL_H__ # if defined _WIN32 @@ -285,6 +306,11 @@ CV_INLINE double cvRandReal( CvRNG* rng ) #define IPL_BORDER_REFLECT 2 #define IPL_BORDER_WRAP 3 +#ifdef __cplusplus +typedef struct _IplImage IplImage; +CV_EXPORTS _IplImage cvIplImage(const cv::Mat& m); +#endif + /** The IplImage is taken from the Intel Image Processing Library, in which the format is native. OpenCV only supports a subset of possible IplImage formats, as outlined in the parameter list above. @@ -294,9 +320,6 @@ hand, the Intel Image Processing Library processes the area of intersection betw destination images (or ROIs), allowing them to vary independently. */ typedef struct -#ifdef __cplusplus - CV_EXPORTS -#endif _IplImage { int nSize; /**< sizeof(IplImage) */ @@ -330,13 +353,22 @@ _IplImage (not necessarily aligned) - needed for correct deallocation */ -#ifdef __cplusplus +#if defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus) _IplImage() {} - _IplImage(const cv::Mat& m); + _IplImage(const cv::Mat& m) { *this = cvIplImage(m); } #endif } IplImage; +CV_INLINE IplImage cvIplImage() +{ +#if !defined(CV__ENABLE_C_API_CTORS) + IplImage self = CV_STRUCT_INITIALIZER; self.nSize = sizeof(IplImage); return self; +#else + return _IplImage(); +#endif +} + typedef struct _IplTileInfo IplTileInfo; typedef struct _IplROI @@ -460,13 +492,10 @@ typedef struct CvMat int cols; #endif - -#ifdef __cplusplus +#if defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus) CvMat() {} - CvMat(const CvMat& m) { memcpy(this, &m, sizeof(CvMat));} - CvMat(const cv::Mat& m); + CvMat(const cv::Mat& m) { *this = cvMat(m); } #endif - } CvMat; @@ -529,15 +558,8 @@ CV_INLINE CvMat cvMat( int rows, int cols, int type, void* data CV_DEFAULT(NULL) } #ifdef __cplusplus -inline CvMat::CvMat(const cv::Mat& m) -{ - CV_DbgAssert(m.dims <= 2); - *this = cvMat(m.rows, m.dims == 1 ? 1 : m.cols, m.type(), m.data); - step = (int)m.step[0]; - type = (type & ~cv::Mat::CONTINUOUS_FLAG) | (m.flags & cv::Mat::CONTINUOUS_FLAG); -} -inline CvMat cvMat(const cv::Mat& m) +CV_INLINE CvMat cvMat(const cv::Mat& m) { CvMat self; CV_DbgAssert(m.dims <= 2); @@ -546,7 +568,24 @@ inline CvMat cvMat(const cv::Mat& m) self.type = (self.type & ~cv::Mat::CONTINUOUS_FLAG) | (m.flags & cv::Mat::CONTINUOUS_FLAG); return self; } +CV_INLINE CvMat cvMat() +{ +#if !defined(CV__ENABLE_C_API_CTORS) + CvMat self = CV_STRUCT_INITIALIZER; return self; +#else + return CvMat(); #endif +} +CV_INLINE CvMat cvMat(const CvMat& m) +{ +#if !defined(CV__ENABLE_C_API_CTORS) + CvMat self = CV_STRUCT_INITIALIZER; memcpy(&self, &m, sizeof(self)); return self; +#else + return CvMat(m); +#endif +} + +#endif // __cplusplus #define CV_MAT_ELEM_PTR_FAST( mat, row, col, pix_size ) \ @@ -630,13 +669,15 @@ CV_INLINE int cvIplDepth( int type ) #define CV_MAX_DIM 32 +#ifdef __cplusplus +typedef struct CvMatND CvMatND; +CV_EXPORTS CvMatND cvMatND(const cv::Mat& m); +#endif + /** @deprecated consider using cv::Mat instead */ typedef struct -#ifdef __cplusplus - CV_EXPORTS -#endif CvMatND { int type; @@ -661,13 +702,23 @@ CvMatND } dim[CV_MAX_DIM]; -#ifdef __cplusplus +#if defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus) CvMatND() {} - CvMatND(const cv::Mat& m); + CvMatND(const cv::Mat& m) { *this = cvMatND(m); } #endif } CvMatND; + +CV_INLINE CvMatND cvMatND() +{ +#if !(defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus)) + CvMatND self = CV_STRUCT_INITIALIZER; return self; +#else + return CvMatND(); +#endif +} + #define CV_IS_MATND_HDR(mat) \ ((mat) != NULL && (((const CvMatND*)(mat))->type & CV_MAGIC_MASK) == CV_MATND_MAGIC_VAL) @@ -684,11 +735,7 @@ CvMatND; struct CvSet; -typedef struct -#ifdef __cplusplus - CV_EXPORTS -#endif -CvSparseMat +typedef struct CvSparseMat { int type; int dims; @@ -703,13 +750,13 @@ CvSparseMat int size[CV_MAX_DIM]; #ifdef __cplusplus - void copyToSparseMat(cv::SparseMat& m) const; + CV_EXPORTS void copyToSparseMat(cv::SparseMat& m) const; #endif } CvSparseMat; #ifdef __cplusplus - CV_EXPORTS CvSparseMat* cvCreateSparseMat(const cv::SparseMat& m); +CV_EXPORTS CvSparseMat* cvCreateSparseMat(const cv::SparseMat& m); #endif #define CV_IS_SPARSE_MAT_HDR(mat) \ @@ -796,10 +843,23 @@ typedef struct CvRect int width; int height; -#ifdef __cplusplus +#ifdef CV__VALIDATE_UNUNITIALIZED_VARS + CvRect() __attribute__(( warning("Non-initialized variable") )) {}; + template CvRect(const std::initializer_list<_Tp> list) + { + CV_Assert(list.size() == 0 || list.size() == 4); + x = y = width = height = 0; + if (list.size() == 4) + { + x = list.begin()[0]; y = list.begin()[1]; width = list.begin()[2]; height = list.begin()[3]; + } + }; +#elif defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus) CvRect(int _x = 0, int _y = 0, int w = 0, int h = 0): x(_x), y(_y), width(w), height(h) {} template CvRect(const cv::Rect_<_Tp>& r): x(cv::saturate_cast(r.x)), y(cv::saturate_cast(r.y)), width(cv::saturate_cast(r.width)), height(cv::saturate_cast(r.height)) {} +#endif +#ifdef __cplusplus template operator cv::Rect_<_Tp>() const { return cv::Rect_<_Tp>((_Tp)x, (_Tp)y, (_Tp)width, (_Tp)height); } #endif @@ -809,16 +869,16 @@ CvRect; /** constructs CvRect structure. */ CV_INLINE CvRect cvRect( int x, int y, int width, int height ) { - CvRect r; - - r.x = x; - r.y = y; - r.width = width; - r.height = height; - +#if !(defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus)) + CvRect r = {x, y, width, height}; +#else + CvRect r(x, y , width, height); +#endif return r; } - +#ifdef __cplusplus +CV_INLINE CvRect cvRect(const cv::Rect& rc) { return cvRect(rc.x, rc.y, rc.width, rc.height); } +#endif CV_INLINE IplROI cvRectToROI( CvRect rect, int coi ) { @@ -853,26 +913,28 @@ typedef struct CvTermCriteria CV_TERMCRIT_EPS */ int max_iter; double epsilon; - -#ifdef __cplusplus +#if defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus) CvTermCriteria(int _type = 0, int _iter = 0, double _eps = 0) : type(_type), max_iter(_iter), epsilon(_eps) {} CvTermCriteria(const cv::TermCriteria& t) : type(t.type), max_iter(t.maxCount), epsilon(t.epsilon) {} +#endif +#ifdef __cplusplus operator cv::TermCriteria() const { return cv::TermCriteria(type, max_iter, epsilon); } #endif - } CvTermCriteria; CV_INLINE CvTermCriteria cvTermCriteria( int type, int max_iter, double epsilon ) { - CvTermCriteria t; - - t.type = type; - t.max_iter = max_iter; - t.epsilon = (float)epsilon; - +#if !(defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus)) + CvTermCriteria t = { type, max_iter, (float)epsilon}; +#else + CvTermCriteria t(type, max_iter, epsilon); +#endif return t; } +#ifdef __cplusplus +CV_INLINE CvTermCriteria cvTermCriteria(const cv::TermCriteria& t) { return cvTermCriteria(t.type, t.maxCount, t.epsilon); } +#endif /******************************* CvPoint and variants ***********************************/ @@ -882,10 +944,23 @@ typedef struct CvPoint int x; int y; -#ifdef __cplusplus +#ifdef CV__VALIDATE_UNUNITIALIZED_VARS + CvPoint() __attribute__(( warning("Non-initialized variable") )) {} + template CvPoint(const std::initializer_list<_Tp> list) + { + CV_Assert(list.size() == 0 || list.size() == 2); + x = y = 0; + if (list.size() == 2) + { + x = list.begin()[0]; y = list.begin()[1]; + } + }; +#elif defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus) CvPoint(int _x = 0, int _y = 0): x(_x), y(_y) {} template CvPoint(const cv::Point_<_Tp>& pt): x((int)pt.x), y((int)pt.y) {} +#endif +#ifdef __cplusplus template operator cv::Point_<_Tp>() const { return cv::Point_<_Tp>(cv::saturate_cast<_Tp>(x), cv::saturate_cast<_Tp>(y)); } #endif @@ -895,24 +970,39 @@ CvPoint; /** constructs CvPoint structure. */ CV_INLINE CvPoint cvPoint( int x, int y ) { - CvPoint p; - - p.x = x; - p.y = y; - +#if !(defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus)) + CvPoint p = {x, y}; +#else + CvPoint p(x, y); +#endif return p; } - +#ifdef __cplusplus +CV_INLINE CvPoint cvPoint(const cv::Point& pt) { return cvPoint(pt.x, pt.y); } +#endif typedef struct CvPoint2D32f { float x; float y; -#ifdef __cplusplus +#ifdef CV__VALIDATE_UNUNITIALIZED_VARS + CvPoint2D32f() __attribute__(( warning("Non-initialized variable") )) {} + template CvPoint2D32f(const std::initializer_list<_Tp> list) + { + CV_Assert(list.size() == 0 || list.size() == 2); + x = y = 0; + if (list.size() == 2) + { + x = list.begin()[0]; y = list.begin()[1]; + } + }; +#elif defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus) CvPoint2D32f(float _x = 0, float _y = 0): x(_x), y(_y) {} template CvPoint2D32f(const cv::Point_<_Tp>& pt): x((float)pt.x), y((float)pt.y) {} +#endif +#ifdef __cplusplus template operator cv::Point_<_Tp>() const { return cv::Point_<_Tp>(cv::saturate_cast<_Tp>(x), cv::saturate_cast<_Tp>(y)); } #endif @@ -922,11 +1012,11 @@ CvPoint2D32f; /** constructs CvPoint2D32f structure. */ CV_INLINE CvPoint2D32f cvPoint2D32f( double x, double y ) { - CvPoint2D32f p; - - p.x = (float)x; - p.y = (float)y; - +#if !(defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus)) + CvPoint2D32f p = { (float)x, (float)y }; +#else + CvPoint2D32f p((float)x, (float)y); +#endif return p; } @@ -934,7 +1024,11 @@ CV_INLINE CvPoint2D32f cvPoint2D32f( double x, double y ) template CvPoint2D32f cvPoint2D32f(const cv::Point_<_Tp>& pt) { +#if !(defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus)) + CvPoint2D32f p = { (float)pt.x, (float)pt.y }; +#else CvPoint2D32f p((float)pt.x, (float)pt.y); +#endif return p; } #endif @@ -948,10 +1042,11 @@ CV_INLINE CvPoint2D32f cvPointTo32f( CvPoint point ) /** converts CvPoint2D32f to CvPoint. */ CV_INLINE CvPoint cvPointFrom32f( CvPoint2D32f point ) { - CvPoint ipt; - ipt.x = cvRound(point.x); - ipt.y = cvRound(point.y); - +#if !(defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus)) + CvPoint ipt = { cvRound(point.x), cvRound(point.y) }; +#else + CvPoint ipt(cvRound(point.x), cvRound(point.y)); +#endif return ipt; } @@ -962,10 +1057,23 @@ typedef struct CvPoint3D32f float y; float z; -#ifdef __cplusplus +#ifdef CV__VALIDATE_UNUNITIALIZED_VARS + CvPoint3D32f() __attribute__(( warning("Non-initialized variable") )) {} + template CvPoint3D32f(const std::initializer_list<_Tp> list) + { + CV_Assert(list.size() == 0 || list.size() == 3); + x = y = z = 0; + if (list.size() == 3) + { + x = list.begin()[0]; y = list.begin()[1]; z = list.begin()[2]; + } + }; +#elif defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus) CvPoint3D32f(float _x = 0, float _y = 0, float _z = 0): x(_x), y(_y), z(_z) {} template CvPoint3D32f(const cv::Point3_<_Tp>& pt): x((float)pt.x), y((float)pt.y), z((float)pt.z) {} +#endif +#ifdef __cplusplus template operator cv::Point3_<_Tp>() const { return cv::Point3_<_Tp>(cv::saturate_cast<_Tp>(x), cv::saturate_cast<_Tp>(y), cv::saturate_cast<_Tp>(z)); } #endif @@ -975,31 +1083,51 @@ CvPoint3D32f; /** constructs CvPoint3D32f structure. */ CV_INLINE CvPoint3D32f cvPoint3D32f( double x, double y, double z ) { - CvPoint3D32f p; - - p.x = (float)x; - p.y = (float)y; - p.z = (float)z; +#if !(defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus)) + CvPoint3D32f p = { (float)x, (float)y, (float)z }; +#else + CvPoint3D32f p((float)x, (float)y, (float)z); +#endif + return p; +} +#ifdef __cplusplus +template +CvPoint3D32f cvPoint3D32f(const cv::Point3_<_Tp>& pt) +{ +#if !(defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus)) + CvPoint3D32f p = { (float)pt.x, (float)pt.y, (float)pt.z }; +#else + CvPoint3D32f p((float)pt.x, (float)pt.y, (float)pt.z); +#endif return p; } +#endif typedef struct CvPoint2D64f { double x; double y; +#ifdef CV__VALIDATE_UNUNITIALIZED_VARS + CvPoint2D64f() __attribute__(( warning("Non-initialized variable") )) {} + template CvPoint2D64f(const std::initializer_list<_Tp> list) + { + CV_Assert(list.size() == 0 || list.size() == 2); + x = y = 0; + if (list.size() == 2) + { + x = list.begin()[0]; y = list.begin()[1]; + } + }; +#endif } CvPoint2D64f; /** constructs CvPoint2D64f structure.*/ CV_INLINE CvPoint2D64f cvPoint2D64f( double x, double y ) { - CvPoint2D64f p; - - p.x = x; - p.y = y; - + CvPoint2D64f p = { x, y }; return p; } @@ -1009,18 +1137,25 @@ typedef struct CvPoint3D64f double x; double y; double z; +#ifdef CV__VALIDATE_UNUNITIALIZED_VARS + CvPoint3D64f() __attribute__(( warning("Non-initialized variable") )) {} + template CvPoint3D64f(const std::initializer_list<_Tp> list) + { + CV_Assert(list.size() == 0 || list.size() == 3); + x = y = z = 0; + if (list.size() == 3) + { + x = list.begin()[0]; y = list.begin()[1]; z = list.begin()[2]; + } + }; +#endif } CvPoint3D64f; /** constructs CvPoint3D64f structure. */ CV_INLINE CvPoint3D64f cvPoint3D64f( double x, double y, double z ) { - CvPoint3D64f p; - - p.x = x; - p.y = y; - p.z = z; - + CvPoint3D64f p = { x, y, z }; return p; } @@ -1032,10 +1167,23 @@ typedef struct CvSize int width; int height; -#ifdef __cplusplus +#ifdef CV__VALIDATE_UNUNITIALIZED_VARS + CvSize() __attribute__(( warning("Non-initialized variable") )) {} + template CvSize(const std::initializer_list<_Tp> list) + { + CV_Assert(list.size() == 0 || list.size() == 2); + width = 0; height = 0; + if (list.size() == 2) + { + width = list.begin()[0]; height = list.begin()[1]; + } + }; +#elif defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus) CvSize(int w = 0, int h = 0): width(w), height(h) {} template CvSize(const cv::Size_<_Tp>& sz): width(cv::saturate_cast(sz.width)), height(cv::saturate_cast(sz.height)) {} +#endif +#ifdef __cplusplus template operator cv::Size_<_Tp>() const { return cv::Size_<_Tp>(cv::saturate_cast<_Tp>(width), cv::saturate_cast<_Tp>(height)); } #endif @@ -1045,23 +1193,48 @@ CvSize; /** constructs CvSize structure. */ CV_INLINE CvSize cvSize( int width, int height ) { - CvSize s; - - s.width = width; - s.height = height; +#if !(defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus)) + CvSize s = { width, height }; +#else + CvSize s(width, height); +#endif + return s; +} +#ifdef __cplusplus +CV_INLINE CvSize cvSize(const cv::Size& sz) +{ +#if !(defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus)) + CvSize s = { sz.width, sz.height }; +#else + CvSize s(sz.width, sz.height); +#endif return s; } +#endif typedef struct CvSize2D32f { float width; float height; -#ifdef __cplusplus +#ifdef CV__VALIDATE_UNUNITIALIZED_VARS + CvSize2D32f() __attribute__(( warning("Non-initialized variable") )) {} + template CvSize2D32f(const std::initializer_list<_Tp> list) + { + CV_Assert(list.size() == 0 || list.size() == 2); + width = 0; height = 0; + if (list.size() == 2) + { + width = list.begin()[0]; height = list.begin()[1]; + } + }; +#elif defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus) CvSize2D32f(float w = 0, float h = 0): width(w), height(h) {} template CvSize2D32f(const cv::Size_<_Tp>& sz): width(cv::saturate_cast(sz.width)), height(cv::saturate_cast(sz.height)) {} +#endif +#ifdef __cplusplus template operator cv::Size_<_Tp>() const { return cv::Size_<_Tp>(cv::saturate_cast<_Tp>(width), cv::saturate_cast<_Tp>(height)); } #endif @@ -1071,13 +1244,25 @@ CvSize2D32f; /** constructs CvSize2D32f structure. */ CV_INLINE CvSize2D32f cvSize2D32f( double width, double height ) { - CvSize2D32f s; - - s.width = (float)width; - s.height = (float)height; - +#if !(defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus)) + CvSize2D32f s = { (float)width, (float)height }; +#else + CvSize2D32f s((float)width, (float)height); +#endif return s; } +#ifdef __cplusplus +template +CvSize2D32f cvSize2D32f(const cv::Size_<_Tp>& sz) +{ +#if !(defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus)) + CvSize2D32f s = { (float)sz.width, (float)sz.height }; +#else + CvSize2D32f s((float)sz.width, (float)sz.height); +#endif + return s; +} +#endif /** @sa RotatedRect */ @@ -1088,15 +1273,37 @@ typedef struct CvBox2D float angle; /**< Angle between the horizontal axis */ /**< and the first side (i.e. length) in degrees */ -#ifdef __cplusplus +#if defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus) CvBox2D(CvPoint2D32f c = CvPoint2D32f(), CvSize2D32f s = CvSize2D32f(), float a = 0) : center(c), size(s), angle(a) {} CvBox2D(const cv::RotatedRect& rr) : center(rr.center), size(rr.size), angle(rr.angle) {} +#endif +#ifdef __cplusplus operator cv::RotatedRect() const { return cv::RotatedRect(center, size, angle); } #endif } CvBox2D; +#ifdef __cplusplus +CV_INLINE CvBox2D cvBox2D(CvPoint2D32f c = CvPoint2D32f(), CvSize2D32f s = CvSize2D32f(), float a = 0) +{ + CvBox2D self; + self.center = c; + self.size = s; + self.angle = a; + return self; +} +CV_INLINE CvBox2D cvBox2D(const cv::RotatedRect& rr) +{ + CvBox2D self; + self.center = cvPoint2D32f(rr.center); + self.size = cvSize2D32f(rr.size); + self.angle = rr.angle; + return self; +} +#endif + + /** Line iterator state: */ typedef struct CvLineIterator { @@ -1122,7 +1329,19 @@ typedef struct CvSlice { int start_index, end_index; -#if defined(__cplusplus) && !defined(__CUDACC__) +#ifdef CV__VALIDATE_UNUNITIALIZED_VARS + CvSlice() __attribute__(( warning("Non-initialized variable") )) {} + template CvSlice(const std::initializer_list<_Tp> list) + { + CV_Assert(list.size() == 0 || list.size() == 2); + start_index = end_index = 0; + if (list.size() == 2) + { + start_index = list.begin()[0]; end_index = list.begin()[1]; + } + }; +#endif +#if defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus) && !defined(__CUDACC__) CvSlice(int start = 0, int end = 0) : start_index(start), end_index(end) {} CvSlice(const cv::Range& r) { *this = (r.start != INT_MIN && r.end != INT_MAX) ? CvSlice(r.start, r.end) : CvSlice(0, CV_WHOLE_SEQ_END_INDEX); } operator cv::Range() const { return (start_index == 0 && end_index == CV_WHOLE_SEQ_END_INDEX ) ? cv::Range::all() : cv::Range(start_index, end_index); } @@ -1132,13 +1351,21 @@ CvSlice; CV_INLINE CvSlice cvSlice( int start, int end ) { - CvSlice slice; - slice.start_index = start; - slice.end_index = end; - +#if !(defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus)) + CvSlice slice = { start, end }; +#else + CvSlice slice(start, end); +#endif return slice; } +#if defined(__cplusplus) +CV_INLINE CvSlice cvSlice(const cv::Range& r) +{ + CvSlice slice = (r.start != INT_MIN && r.end != INT_MAX) ? cvSlice(r.start, r.end) : cvSlice(0, CV_WHOLE_SEQ_END_INDEX); + return slice; +} +#endif /************************************* CvScalar *****************************************/ @@ -1148,13 +1375,22 @@ typedef struct CvScalar { double val[4]; -#ifdef __cplusplus +#ifdef CV__VALIDATE_UNUNITIALIZED_VARS + CvScalar() __attribute__(( warning("Non-initialized variable") )) {} + CvScalar(const std::initializer_list list) + { + CV_Assert(list.size() == 0 || list.size() == 4); + val[0] = val[1] = val[2] = val[3] = 0; + if (list.size() == 4) + { + val[0] = list.begin()[0]; val[1] = list.begin()[1]; val[2] = list.begin()[2]; val[3] = list.begin()[3]; + } + }; +#elif defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus) CvScalar() {} CvScalar(double d0, double d1 = 0, double d2 = 0, double d3 = 0) { val[0] = d0; val[1] = d1; val[2] = d2; val[3] = d3; } template CvScalar(const cv::Scalar_<_Tp>& s) { val[0] = s.val[0]; val[1] = s.val[1]; val[2] = s.val[2]; val[3] = s.val[3]; } - template - operator cv::Scalar_<_Tp>() const { return cv::Scalar_<_Tp>(cv::saturate_cast<_Tp>(val[0]), cv::saturate_cast<_Tp>(val[1]), cv::saturate_cast<_Tp>(val[2]), cv::saturate_cast<_Tp>(val[3])); } template CvScalar(const cv::Vec<_Tp, cn>& v) { @@ -1163,22 +1399,59 @@ typedef struct CvScalar for( ; i < 4; i++ ) val[i] = 0; } #endif +#ifdef __cplusplus + template + operator cv::Scalar_<_Tp>() const { return cv::Scalar_<_Tp>(cv::saturate_cast<_Tp>(val[0]), cv::saturate_cast<_Tp>(val[1]), cv::saturate_cast<_Tp>(val[2]), cv::saturate_cast<_Tp>(val[3])); } +#endif } CvScalar; CV_INLINE CvScalar cvScalar( double val0, double val1 CV_DEFAULT(0), double val2 CV_DEFAULT(0), double val3 CV_DEFAULT(0)) { +#if !(defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus)) + CvScalar scalar = CV_STRUCT_INITIALIZER; +#else CvScalar scalar; +#endif scalar.val[0] = val0; scalar.val[1] = val1; scalar.val[2] = val2; scalar.val[3] = val3; return scalar; } +#ifdef __cplusplus +CV_INLINE CvScalar cvScalar() +{ +#if !(defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus)) + CvScalar scalar = CV_STRUCT_INITIALIZER; +#else + CvScalar scalar; +#endif + scalar.val[0] = scalar.val[1] = scalar.val[2] = scalar.val[3] = 0; + return scalar; +} +CV_INLINE CvScalar cvScalar(const cv::Scalar& s) +{ +#if !(defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus)) + CvScalar scalar = CV_STRUCT_INITIALIZER; +#else + CvScalar scalar; +#endif + scalar.val[0] = s.val[0]; + scalar.val[1] = s.val[1]; + scalar.val[2] = s.val[2]; + scalar.val[3] = s.val[3]; + return scalar; +} +#endif CV_INLINE CvScalar cvRealScalar( double val0 ) { +#if !(defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus)) + CvScalar scalar = CV_STRUCT_INITIALIZER; +#else CvScalar scalar; +#endif scalar.val[0] = val0; scalar.val[1] = scalar.val[2] = scalar.val[3] = 0; return scalar; @@ -1186,7 +1459,11 @@ CV_INLINE CvScalar cvRealScalar( double val0 ) CV_INLINE CvScalar cvScalarAll( double val0123 ) { +#if !(defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus)) + CvScalar scalar = CV_STRUCT_INITIALIZER; +#else CvScalar scalar; +#endif scalar.val[0] = val0123; scalar.val[1] = val0123; scalar.val[2] = val0123; @@ -1239,7 +1516,7 @@ typedef struct CvSeqBlock { struct CvSeqBlock* prev; /**< Previous sequence block. */ struct CvSeqBlock* next; /**< Next sequence block. */ - int start_index; /**< Index of the first element in the block + */ + int start_index; /**< Index of the first element in the block + */ /**< sequence->first->start_index. */ int count; /**< Number of elements in the block. */ schar* data; /**< Pointer to the first element of the block. */ diff --git a/modules/core/src/matrix_c.cpp b/modules/core/src/matrix_c.cpp index a4efd4ccf3..f9f50fa521 100644 --- a/modules/core/src/matrix_c.cpp +++ b/modules/core/src/matrix_c.cpp @@ -4,20 +4,24 @@ // glue -CvMatND::CvMatND(const cv::Mat& m) +CvMatND cvMatND(const cv::Mat& m) { - cvInitMatNDHeader(this, m.dims, m.size, m.type(), m.data ); + CvMatND self; + cvInitMatNDHeader(&self, m.dims, m.size, m.type(), m.data ); int i, d = m.dims; for( i = 0; i < d; i++ ) - dim[i].step = (int)m.step[i]; - type |= m.flags & cv::Mat::CONTINUOUS_FLAG; + self.dim[i].step = (int)m.step[i]; + self.type |= m.flags & cv::Mat::CONTINUOUS_FLAG; + return self; } -_IplImage::_IplImage(const cv::Mat& m) +_IplImage cvIplImage(const cv::Mat& m) { + _IplImage self; CV_Assert( m.dims <= 2 ); - cvInitImageHeader(this, m.size(), cvIplDepth(m.flags), m.channels()); - cvSetData(this, m.data, (int)m.step[0]); + cvInitImageHeader(&self, cvSize(m.size()), cvIplDepth(m.flags), m.channels()); + cvSetData(&self, m.data, (int)m.step[0]); + return self; } namespace cv { diff --git a/modules/imgproc/include/opencv2/imgproc/types_c.h b/modules/imgproc/include/opencv2/imgproc/types_c.h index 13ffe1b1a3..d3e55f576f 100644 --- a/modules/imgproc/include/opencv2/imgproc/types_c.h +++ b/modules/imgproc/include/opencv2/imgproc/types_c.h @@ -410,7 +410,7 @@ typedef struct CvMoments double mu20, mu11, mu02, mu30, mu21, mu12, mu03; /**< central moments */ double inv_sqrt_m00; /**< m00 != 0 ? 1/sqrt(m00) : 0 */ -#ifdef __cplusplus +#if defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus) CvMoments(){} CvMoments(const cv::Moments& m) { @@ -430,6 +430,36 @@ typedef struct CvMoments } CvMoments; +#ifdef __cplusplus +} // extern "C" + +CV_INLINE CvMoments cvMoments() +{ +#if !defined(CV__ENABLE_C_API_CTORS) + CvMoments self = CV_STRUCT_INITIALIZER; return self; +#else + return CvMoments(); +#endif +} + +CV_INLINE CvMoments cvMoments(const cv::Moments& m) +{ +#if !defined(CV__ENABLE_C_API_CTORS) + double am00 = std::abs(m.m00); + CvMoments self = { + m.m00, m.m10, m.m01, m.m20, m.m11, m.m02, m.m30, m.m21, m.m12, m.m03, + m.mu20, m.mu11, m.mu02, m.mu30, m.mu21, m.mu12, m.mu03, + am00 > DBL_EPSILON ? 1./std::sqrt(am00) : 0 + }; + return self; +#else + return CvMoments(m); +#endif +} + +extern "C" { +#endif // __cplusplus + /** Hu invariants */ typedef struct CvHuMoments {