From 9ac0d4323d2ffca53b6fb1ff59d4da6f8a22b46f Mon Sep 17 00:00:00 2001 From: Vadim Pisarevsky Date: Wed, 4 Apr 2012 12:52:14 +0000 Subject: [PATCH] make Mat::Mat(CvMat* m) return empty matrix when m is NULL; added utility Mat::initEmpty() method to replace duplicated matrix initializations in different constructors --- modules/core/include/opencv2/core/core.hpp | 3 + modules/core/include/opencv2/core/mat.hpp | 65 +++++++------------- modules/core/src/matrix.cpp | 71 ++++++++++++++++------ 3 files changed, 77 insertions(+), 62 deletions(-) diff --git a/modules/core/include/opencv2/core/core.hpp b/modules/core/include/opencv2/core/core.hpp index 39ab7737a..555c2b3be 100644 --- a/modules/core/include/opencv2/core/core.hpp +++ b/modules/core/include/opencv2/core/core.hpp @@ -1944,6 +1944,9 @@ public: MSize size; MStep step; + +protected: + void initEmpty(); }; diff --git a/modules/core/include/opencv2/core/mat.hpp b/modules/core/include/opencv2/core/mat.hpp index 427952d6d..4bd1bb7a6 100644 --- a/modules/core/include/opencv2/core/mat.hpp +++ b/modules/core/include/opencv2/core/mat.hpp @@ -55,53 +55,55 @@ namespace cv //////////////////////////////// Mat //////////////////////////////// -inline Mat::Mat() - : flags(0), dims(0), rows(0), cols(0), data(0), refcount(0), - datastart(0), dataend(0), datalimit(0), allocator(0), size(&rows) +inline void Mat::initEmpty() { + flags = MAGIC_VAL; + dims = rows = cols = 0; + data = datastart = dataend = datalimit = 0; + refcount = 0; + allocator = 0; +} + +inline Mat::Mat() : size(&rows) +{ + initEmpty(); } -inline Mat::Mat(int _rows, int _cols, int _type) - : flags(0), dims(0), rows(0), cols(0), data(0), refcount(0), - datastart(0), dataend(0), datalimit(0), allocator(0), size(&rows) +inline Mat::Mat(int _rows, int _cols, int _type) : size(&rows) { + initEmpty(); create(_rows, _cols, _type); } -inline Mat::Mat(int _rows, int _cols, int _type, const Scalar& _s) - : flags(0), dims(0), rows(0), cols(0), data(0), refcount(0), - datastart(0), dataend(0), datalimit(0), allocator(0), size(&rows) +inline Mat::Mat(int _rows, int _cols, int _type, const Scalar& _s) : size(&rows) { + initEmpty(); create(_rows, _cols, _type); *this = _s; } -inline Mat::Mat(Size _sz, int _type) - : flags(0), dims(0), rows(0), cols(0), data(0), refcount(0), - datastart(0), dataend(0), datalimit(0), allocator(0), size(&rows) +inline Mat::Mat(Size _sz, int _type) : size(&rows) { + initEmpty(); create( _sz.height, _sz.width, _type ); } -inline Mat::Mat(Size _sz, int _type, const Scalar& _s) - : flags(0), dims(0), rows(0), cols(0), data(0), refcount(0), - datastart(0), dataend(0), datalimit(0), allocator(0), size(&rows) +inline Mat::Mat(Size _sz, int _type, const Scalar& _s) : size(&rows) { + initEmpty(); create(_sz.height, _sz.width, _type); *this = _s; } -inline Mat::Mat(int _dims, const int* _sz, int _type) - : flags(0), dims(0), rows(0), cols(0), data(0), refcount(0), - datastart(0), dataend(0), datalimit(0), allocator(0), size(&rows) +inline Mat::Mat(int _dims, const int* _sz, int _type) : size(&rows) { + initEmpty(); create(_dims, _sz, _type); } -inline Mat::Mat(int _dims, const int* _sz, int _type, const Scalar& _s) - : flags(0), dims(0), rows(0), cols(0), data(0), refcount(0), - datastart(0), dataend(0), datalimit(0), allocator(0), size(&rows) +inline Mat::Mat(int _dims, const int* _sz, int _type, const Scalar& _s) : size(&rows) { + initEmpty(); create(_dims, _sz, _type); *this = _s; } @@ -169,27 +171,6 @@ inline Mat::Mat(Size _sz, int _type, void* _data, size_t _step) } -inline Mat::Mat(const CvMat* m, bool copyData) - : flags(MAGIC_VAL + (m->type & (CV_MAT_TYPE_MASK|CV_MAT_CONT_FLAG))), - dims(2), rows(m->rows), cols(m->cols), data(m->data.ptr), refcount(0), - datastart(m->data.ptr), allocator(0), size(&rows) -{ - if( !copyData ) - { - size_t esz = CV_ELEM_SIZE(m->type), minstep = cols*esz, _step = m->step; - if( _step == 0 ) - _step = minstep; - datalimit = datastart + _step*rows; - dataend = datalimit - _step + minstep; - step[0] = _step; step[1] = esz; - } - else - { - data = datastart = dataend = 0; - Mat(m->rows, m->cols, m->type, m->data.ptr, m->step).copyTo(*this); - } -} - template inline Mat::Mat(const 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), diff --git a/modules/core/src/matrix.cpp b/modules/core/src/matrix.cpp index 4fb901db2..ebacaa4f4 100644 --- a/modules/core/src/matrix.cpp +++ b/modules/core/src/matrix.cpp @@ -262,10 +262,9 @@ void Mat::deallocate() } -Mat::Mat(const Mat& m, const Range& rowRange, const Range& colRange) - : flags(0), dims(0), rows(0), cols(0), data(0), refcount(0), - datastart(0), dataend(0), datalimit(0), allocator(0), size(&rows) +Mat::Mat(const Mat& m, const Range& rowRange, const Range& colRange) : size(&rows) { + initEmpty(); CV_Assert( m.dims >= 2 ); if( m.dims > 2 ) { @@ -336,21 +335,19 @@ 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|CV_MAT_TYPE(_type)), dims(0), - rows(0), cols(0), data((uchar*)_data), refcount(0), - datastart((uchar*)_data), dataend((uchar*)_data), datalimit((uchar*)_data), - allocator(0), size(&rows) +Mat::Mat(int _dims, const int* _sizes, int _type, void* _data, const size_t* _steps) : size(&rows) { + initEmpty(); + flags |= CV_MAT_TYPE(_type); + data = datastart = (uchar*)_data; setSize(*this, _dims, _sizes, _steps, true); finalizeHdr(*this); } -Mat::Mat(const Mat& m, const Range* ranges) - : flags(m.flags), dims(0), rows(0), cols(0), data(0), refcount(0), - datastart(0), dataend(0), datalimit(0), allocator(0), size(&rows) +Mat::Mat(const Mat& m, const Range* ranges) : size(&rows) { + initEmpty(); int i, d = m.dims; CV_Assert(ranges); @@ -374,12 +371,13 @@ Mat::Mat(const Mat& m, const Range* ranges) } -Mat::Mat(const CvMatND* m, bool copyData) - : flags(MAGIC_VAL|CV_MAT_TYPE(m->type)), dims(0), rows(0), cols(0), - data((uchar*)m->data.ptr), refcount(0), - datastart((uchar*)m->data.ptr), allocator(0), - size(&rows) +Mat::Mat(const CvMatND* m, bool copyData) : size(&rows) { + initEmpty(); + if( !m ) + return; + data = datastart = m->data.ptr; + flags |= CV_MAT_TYPE(m->type); int _sizes[CV_MAX_DIM]; size_t _steps[CV_MAX_DIM]; @@ -434,12 +432,45 @@ Mat Mat::diag(int d) const return m; } + - -Mat::Mat(const IplImage* img, bool copyData) - : flags(MAGIC_VAL), dims(2), rows(0), cols(0), - data(0), refcount(0), datastart(0), dataend(0), allocator(0), size(&rows) +Mat::Mat(const CvMat* m, bool copyData) : size(&rows) { + initEmpty(); + + if( !m ) + return; + + if( !copyData ) + { + flags = MAGIC_VAL + (m->type & (CV_MAT_TYPE_MASK|CV_MAT_CONT_FLAG)); + dims = 2; + rows = m->rows; + cols = m->cols; + data = datastart = m->data.ptr; + size_t esz = CV_ELEM_SIZE(m->type), minstep = cols*esz, _step = m->step; + if( _step == 0 ) + _step = minstep; + datalimit = datastart + _step*rows; + dataend = datalimit - _step + minstep; + step[0] = _step; step[1] = esz; + } + else + { + data = datastart = dataend = 0; + Mat(m->rows, m->cols, m->type, m->data.ptr, m->step).copyTo(*this); + } +} + + +Mat::Mat(const IplImage* img, bool copyData) : size(&rows) +{ + initEmpty(); + + if( !img ) + return; + + dims = 2; CV_DbgAssert(CV_IS_IMAGE(img) && img->imageData != 0); int depth = IPL2CV_DEPTH(img->depth);