diff --git a/modules/core/include/opencv2/core/cuda.inl.hpp b/modules/core/include/opencv2/core/cuda.inl.hpp index 170d0affb..d497f20cc 100644 --- a/modules/core/include/opencv2/core/cuda.inl.hpp +++ b/modules/core/include/opencv2/core/cuda.inl.hpp @@ -595,7 +595,7 @@ namespace cv { inline Mat::Mat(const cuda::GpuMat& m) - : flags(0), dims(0), rows(0), cols(0), data(0), refcount(0), datastart(0), dataend(0), datalimit(0), allocator(0), size(&rows) + : flags(0), dims(0), rows(0), cols(0), data(0), datastart(0), dataend(0), datalimit(0), allocator(0), u(0), size(&rows) { m.download(*this); } diff --git a/modules/core/include/opencv2/core/mat.hpp b/modules/core/include/opencv2/core/mat.hpp index 662da519c..81694b69e 100644 --- a/modules/core/include/opencv2/core/mat.hpp +++ b/modules/core/include/opencv2/core/mat.hpp @@ -874,10 +874,6 @@ public: //! pointer to the data uchar* data; - //! pointer to the reference counter; - // when matrix points to user-allocated data, the pointer is NULL - int* refcount; - //! helper fields used in locateROI and adjustROI uchar* datastart; uchar* dataend; diff --git a/modules/core/include/opencv2/core/mat.inl.hpp b/modules/core/include/opencv2/core/mat.inl.hpp index 15878d8e2..3c49984e7 100644 --- a/modules/core/include/opencv2/core/mat.inl.hpp +++ b/modules/core/include/opencv2/core/mat.inl.hpp @@ -274,13 +274,13 @@ inline _InputOutputArray::_InputOutputArray(const cuda::CudaMem& cuda_mem) inline Mat::Mat() - : flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), refcount(0), datastart(0), dataend(0), + : flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), datastart(0), dataend(0), datalimit(0), allocator(0), u(0), size(&rows) {} inline Mat::Mat(int _rows, int _cols, int _type) - : flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), refcount(0), datastart(0), dataend(0), + : flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), datastart(0), dataend(0), datalimit(0), allocator(0), u(0), size(&rows) { create(_rows, _cols, _type); @@ -288,7 +288,7 @@ Mat::Mat(int _rows, int _cols, int _type) inline Mat::Mat(int _rows, int _cols, int _type, const Scalar& _s) - : flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), refcount(0), datastart(0), dataend(0), + : flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), datastart(0), dataend(0), datalimit(0), allocator(0), u(0), size(&rows) { create(_rows, _cols, _type); @@ -297,7 +297,7 @@ Mat::Mat(int _rows, int _cols, int _type, const Scalar& _s) inline Mat::Mat(Size _sz, int _type) - : flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), refcount(0), datastart(0), dataend(0), + : flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), datastart(0), dataend(0), datalimit(0), allocator(0), u(0), size(&rows) { create( _sz.height, _sz.width, _type ); @@ -305,7 +305,7 @@ Mat::Mat(Size _sz, int _type) inline Mat::Mat(Size _sz, int _type, const Scalar& _s) - : flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), refcount(0), datastart(0), dataend(0), + : flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), datastart(0), dataend(0), datalimit(0), allocator(0), u(0), size(&rows) { create(_sz.height, _sz.width, _type); @@ -314,7 +314,7 @@ Mat::Mat(Size _sz, int _type, const Scalar& _s) inline Mat::Mat(int _dims, const int* _sz, int _type) - : flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), refcount(0), datastart(0), dataend(0), + : flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), datastart(0), dataend(0), datalimit(0), allocator(0), u(0), size(&rows) { create(_dims, _sz, _type); @@ -322,7 +322,7 @@ Mat::Mat(int _dims, const int* _sz, int _type) inline Mat::Mat(int _dims, const int* _sz, int _type, const Scalar& _s) - : flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), refcount(0), datastart(0), dataend(0), + : flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), datastart(0), dataend(0), datalimit(0), allocator(0), u(0), size(&rows) { create(_dims, _sz, _type); @@ -331,12 +331,12 @@ Mat::Mat(int _dims, const int* _sz, int _type, const Scalar& _s) inline Mat::Mat(const Mat& m) - : flags(m.flags), dims(m.dims), rows(m.rows), cols(m.cols), data(m.data), refcount(m.refcount), + : flags(m.flags), dims(m.dims), rows(m.rows), cols(m.cols), data(m.data), datastart(m.datastart), dataend(m.dataend), datalimit(m.datalimit), allocator(m.allocator), u(m.u), size(&rows) { - if( refcount ) - CV_XADD(refcount, 1); + if( u ) + CV_XADD(&u->refcount, 1); if( m.dims <= 2 ) { step[0] = m.step[0]; step[1] = m.step[1]; @@ -351,7 +351,7 @@ Mat::Mat(const Mat& m) inline Mat::Mat(int _rows, int _cols, int _type, void* _data, size_t _step) : flags(MAGIC_VAL + (_type & TYPE_MASK)), dims(2), rows(_rows), cols(_cols), - data((uchar*)_data), refcount(0), datastart((uchar*)_data), dataend(0), datalimit(0), + data((uchar*)_data), datastart((uchar*)_data), dataend(0), datalimit(0), allocator(0), u(0), size(&rows) { size_t esz = CV_ELEM_SIZE(_type); @@ -376,7 +376,7 @@ Mat::Mat(int _rows, int _cols, int _type, void* _data, size_t _step) inline Mat::Mat(Size _sz, int _type, void* _data, size_t _step) : flags(MAGIC_VAL + (_type & TYPE_MASK)), dims(2), rows(_sz.height), cols(_sz.width), - data((uchar*)_data), refcount(0), datastart((uchar*)_data), dataend(0), datalimit(0), + data((uchar*)_data), datastart((uchar*)_data), dataend(0), datalimit(0), allocator(0), u(0), size(&rows) { size_t esz = CV_ELEM_SIZE(_type); @@ -401,7 +401,7 @@ Mat::Mat(Size _sz, int _type, void* _data, size_t _step) template inline Mat::Mat(const std::vector<_Tp>& vec, bool copyData) : flags(MAGIC_VAL | DataType<_Tp>::type | CV_MAT_CONT_FLAG), dims(2), rows((int)vec.size()), - cols(1), data(0), refcount(0), datastart(0), dataend(0), allocator(0), u(0), size(&rows) + cols(1), data(0), datastart(0), dataend(0), allocator(0), u(0), size(&rows) { if(vec.empty()) return; @@ -418,7 +418,7 @@ Mat::Mat(const std::vector<_Tp>& vec, bool copyData) template inline Mat::Mat(const Vec<_Tp, n>& vec, bool copyData) : flags(MAGIC_VAL | DataType<_Tp>::type | CV_MAT_CONT_FLAG), dims(2), rows(n), cols(1), data(0), - refcount(0), datastart(0), dataend(0), allocator(0), u(0), size(&rows) + datastart(0), dataend(0), allocator(0), u(0), size(&rows) { if( !copyData ) { @@ -434,7 +434,7 @@ Mat::Mat(const Vec<_Tp, n>& vec, bool copyData) template inline Mat::Mat(const Matx<_Tp,m,n>& M, bool copyData) : flags(MAGIC_VAL | DataType<_Tp>::type | CV_MAT_CONT_FLAG), dims(2), rows(m), cols(n), data(0), - refcount(0), datastart(0), dataend(0), allocator(0), u(0), size(&rows) + datastart(0), dataend(0), allocator(0), u(0), size(&rows) { if( !copyData ) { @@ -450,7 +450,7 @@ Mat::Mat(const Matx<_Tp,m,n>& M, bool copyData) template inline Mat::Mat(const Point_<_Tp>& pt, bool copyData) : flags(MAGIC_VAL | DataType<_Tp>::type | CV_MAT_CONT_FLAG), dims(2), rows(2), cols(1), data(0), - refcount(0), datastart(0), dataend(0), allocator(0), u(0), size(&rows) + datastart(0), dataend(0), allocator(0), u(0), size(&rows) { if( !copyData ) { @@ -469,7 +469,7 @@ Mat::Mat(const Point_<_Tp>& pt, bool copyData) template inline Mat::Mat(const Point3_<_Tp>& pt, bool copyData) : flags(MAGIC_VAL | DataType<_Tp>::type | CV_MAT_CONT_FLAG), dims(2), rows(3), cols(1), data(0), - refcount(0), datastart(0), dataend(0), allocator(0), u(0), size(&rows) + datastart(0), dataend(0), allocator(0), u(0), size(&rows) { if( !copyData ) { @@ -489,7 +489,7 @@ Mat::Mat(const Point3_<_Tp>& pt, bool copyData) template inline Mat::Mat(const MatCommaInitializer_<_Tp>& commaInitializer) : flags(MAGIC_VAL | DataType<_Tp>::type | CV_MAT_CONT_FLAG), dims(0), rows(0), cols(0), data(0), - refcount(0), datastart(0), dataend(0), allocator(0), u(0), size(&rows) + datastart(0), dataend(0), allocator(0), u(0), size(&rows) { *this = commaInitializer.operator Mat_<_Tp>(); } @@ -507,8 +507,8 @@ Mat& Mat::operator = (const Mat& m) { if( this != &m ) { - if( m.refcount ) - CV_XADD(m.refcount, 1); + if( m.u ) + CV_XADD(&m.u->refcount, 1); release(); flags = m.flags; if( dims <= 2 && m.dims <= 2 ) @@ -525,7 +525,6 @@ Mat& Mat::operator = (const Mat& m) datastart = m.datastart; dataend = m.dataend; datalimit = m.datalimit; - refcount = m.refcount; allocator = m.allocator; u = m.u; } @@ -604,17 +603,16 @@ void Mat::create(Size _sz, int _type) inline void Mat::addref() { - if( refcount ) - CV_XADD(refcount, 1); + if( u ) + CV_XADD(&u->refcount, 1); } inline void Mat::release() { - if( refcount && CV_XADD(refcount, -1) == 1 ) + if( u && CV_XADD(&u->refcount, -1) == 1 ) deallocate(); data = datastart = dataend = datalimit = 0; size.p[0] = 0; - refcount = 0; u = 0; } diff --git a/modules/core/src/matrix.cpp b/modules/core/src/matrix.cpp index 1493e1f7e..a29a1b077 100644 --- a/modules/core/src/matrix.cpp +++ b/modules/core/src/matrix.cpp @@ -205,7 +205,6 @@ void swap( Mat& a, Mat& b ) std::swap(a.rows, b.rows); std::swap(a.cols, b.cols); std::swap(a.data, b.data); - std::swap(a.refcount, b.refcount); std::swap(a.datastart, b.datastart); std::swap(a.dataend, b.dataend); std::swap(a.datalimit, b.datalimit); @@ -397,8 +396,8 @@ void Mat::deallocate() } Mat::Mat(const Mat& m, const Range& _rowRange, const Range& _colRange) - : flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), refcount(0), datastart(0), dataend(0), - datalimit(0), allocator(0), size(&rows) + : flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), datastart(0), dataend(0), + datalimit(0), allocator(0), u(0), size(&rows) { CV_Assert( m.dims >= 2 ); if( m.dims > 2 ) @@ -443,9 +442,9 @@ Mat::Mat(const Mat& m, const Range& _rowRange, const Range& _colRange) Mat::Mat(const Mat& m, const Rect& roi) : flags(m.flags), dims(2), rows(roi.height), cols(roi.width), - data(m.data + roi.y*m.step[0]), refcount(m.refcount), + data(m.data + roi.y*m.step[0]), datastart(m.datastart), dataend(m.dataend), datalimit(m.datalimit), - allocator(m.allocator), size(&rows) + allocator(m.allocator), u(m.u), size(&rows) { CV_Assert( m.dims <= 2 ); flags &= roi.width < m.cols ? ~CONTINUOUS_FLAG : -1; @@ -455,8 +454,8 @@ Mat::Mat(const Mat& m, const Rect& roi) data += roi.x*esz; CV_Assert( 0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows ); - if( refcount ) - CV_XADD(refcount, 1); + if( u ) + CV_XADD(&u->refcount, 1); if( roi.width < m.cols || roi.height < m.rows ) flags |= SUBMATRIX_FLAG; @@ -471,8 +470,8 @@ Mat::Mat(const Mat& m, const Rect& roi) Mat::Mat(int _dims, const int* _sizes, int _type, void* _data, const size_t* _steps) - : flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), refcount(0), datastart(0), dataend(0), - datalimit(0), allocator(0), size(&rows) + : flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), datastart(0), dataend(0), + datalimit(0), allocator(0), u(0), size(&rows) { flags |= CV_MAT_TYPE(_type); data = datastart = (uchar*)_data; @@ -482,8 +481,8 @@ Mat::Mat(int _dims, const int* _sizes, int _type, void* _data, const size_t* _st Mat::Mat(const Mat& m, const Range* ranges) - : flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), refcount(0), datastart(0), dataend(0), - datalimit(0), allocator(0), size(&rows) + : flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), datastart(0), dataend(0), + datalimit(0), allocator(0), u(0), size(&rows) { int i, d = m.dims; @@ -1470,6 +1469,9 @@ int _InputArray::type(int i) const { int k = kind(); + if( k == MATX || k == STD_VECTOR || k == STD_VECTOR_VECTOR || (flags & FIXED_TYPE)) + return CV_MAT_TYPE(flags); + if( k == MAT ) return ((const Mat*)obj)->type(); @@ -1479,9 +1481,6 @@ int _InputArray::type(int i) const if( k == EXPR ) return ((const MatExpr*)obj)->type(); - if( k == MATX || k == STD_VECTOR || k == STD_VECTOR_VECTOR ) - return CV_MAT_TYPE(flags); - if( k == NONE ) return -1; diff --git a/modules/core/src/umatrix.cpp b/modules/core/src/umatrix.cpp index df3b87c02..a18620705 100644 --- a/modules/core/src/umatrix.cpp +++ b/modules/core/src/umatrix.cpp @@ -547,11 +547,10 @@ Mat UMat::getMat(int accessFlags) const u->currAllocator->map(u, accessFlags); CV_Assert(u->data != 0); Mat hdr(dims, size.p, type(), u->data + offset, step.p); - hdr.refcount = &u->refcount; hdr.u = u; hdr.datastart = hdr.data = u->data; hdr.datalimit = hdr.dataend = u->data + u->size; - CV_XADD(hdr.refcount, 1); + CV_XADD(&hdr.u->refcount, 1); return hdr; } diff --git a/modules/python/src2/cv2.cpp b/modules/python/src2/cv2.cpp index ba6dad894..fa075b154 100644 --- a/modules/python/src2/cv2.cpp +++ b/modules/python/src2/cv2.cpp @@ -453,7 +453,7 @@ PyObject* pyopencv_from(const Mat& m) if( !m.data ) Py_RETURN_NONE; Mat temp, *p = (Mat*)&m; - if(!p->refcount || p->allocator != &g_numpyAllocator) + if(!p->u || p->allocator != &g_numpyAllocator) { temp.allocator = &g_numpyAllocator; ERRWRAP2(m.copyTo(temp));