提交 377dd042 编写于 作者: A Alexander Alekhin

core: fix .begin()/.end() of empty Mat

上级 8ac33369
...@@ -1304,6 +1304,8 @@ const _Tp& Mat::at(const Vec<int, n>& idx) const ...@@ -1304,6 +1304,8 @@ const _Tp& Mat::at(const Vec<int, n>& idx) const
template<typename _Tp> inline template<typename _Tp> inline
MatConstIterator_<_Tp> Mat::begin() const MatConstIterator_<_Tp> Mat::begin() const
{ {
if (empty())
return MatConstIterator_<_Tp>();
CV_DbgAssert( elemSize() == sizeof(_Tp) ); CV_DbgAssert( elemSize() == sizeof(_Tp) );
return MatConstIterator_<_Tp>((const Mat_<_Tp>*)this); return MatConstIterator_<_Tp>((const Mat_<_Tp>*)this);
} }
...@@ -1311,6 +1313,8 @@ MatConstIterator_<_Tp> Mat::begin() const ...@@ -1311,6 +1313,8 @@ MatConstIterator_<_Tp> Mat::begin() const
template<typename _Tp> inline template<typename _Tp> inline
MatConstIterator_<_Tp> Mat::end() const MatConstIterator_<_Tp> Mat::end() const
{ {
if (empty())
return MatConstIterator_<_Tp>();
CV_DbgAssert( elemSize() == sizeof(_Tp) ); CV_DbgAssert( elemSize() == sizeof(_Tp) );
MatConstIterator_<_Tp> it((const Mat_<_Tp>*)this); MatConstIterator_<_Tp> it((const Mat_<_Tp>*)this);
it += total(); it += total();
...@@ -1320,6 +1324,8 @@ MatConstIterator_<_Tp> Mat::end() const ...@@ -1320,6 +1324,8 @@ MatConstIterator_<_Tp> Mat::end() const
template<typename _Tp> inline template<typename _Tp> inline
MatIterator_<_Tp> Mat::begin() MatIterator_<_Tp> Mat::begin()
{ {
if (empty())
return MatIterator_<_Tp>();
CV_DbgAssert( elemSize() == sizeof(_Tp) ); CV_DbgAssert( elemSize() == sizeof(_Tp) );
return MatIterator_<_Tp>((Mat_<_Tp>*)this); return MatIterator_<_Tp>((Mat_<_Tp>*)this);
} }
...@@ -1327,6 +1333,8 @@ MatIterator_<_Tp> Mat::begin() ...@@ -1327,6 +1333,8 @@ MatIterator_<_Tp> Mat::begin()
template<typename _Tp> inline template<typename _Tp> inline
MatIterator_<_Tp> Mat::end() MatIterator_<_Tp> Mat::end()
{ {
if (empty())
return MatIterator_<_Tp>();
CV_DbgAssert( elemSize() == sizeof(_Tp) ); CV_DbgAssert( elemSize() == sizeof(_Tp) );
MatIterator_<_Tp> it((Mat_<_Tp>*)this); MatIterator_<_Tp> it((Mat_<_Tp>*)this);
it += total(); it += total();
...@@ -2690,6 +2698,7 @@ MatConstIterator::MatConstIterator(const Mat* _m) ...@@ -2690,6 +2698,7 @@ MatConstIterator::MatConstIterator(const Mat* _m)
{ {
if( m && m->isContinuous() ) if( m && m->isContinuous() )
{ {
CV_Assert(!m->empty());
sliceStart = m->ptr(); sliceStart = m->ptr();
sliceEnd = sliceStart + m->total()*elemSize; sliceEnd = sliceStart + m->total()*elemSize;
} }
...@@ -2703,6 +2712,7 @@ MatConstIterator::MatConstIterator(const Mat* _m, int _row, int _col) ...@@ -2703,6 +2712,7 @@ MatConstIterator::MatConstIterator(const Mat* _m, int _row, int _col)
CV_Assert(m && m->dims <= 2); CV_Assert(m && m->dims <= 2);
if( m->isContinuous() ) if( m->isContinuous() )
{ {
CV_Assert(!m->empty());
sliceStart = m->ptr(); sliceStart = m->ptr();
sliceEnd = sliceStart + m->total()*elemSize; sliceEnd = sliceStart + m->total()*elemSize;
} }
...@@ -2717,6 +2727,7 @@ MatConstIterator::MatConstIterator(const Mat* _m, Point _pt) ...@@ -2717,6 +2727,7 @@ MatConstIterator::MatConstIterator(const Mat* _m, Point _pt)
CV_Assert(m && m->dims <= 2); CV_Assert(m && m->dims <= 2);
if( m->isContinuous() ) if( m->isContinuous() )
{ {
CV_Assert(!m->empty());
sliceStart = m->ptr(); sliceStart = m->ptr();
sliceEnd = sliceStart + m->total()*elemSize; sliceEnd = sliceStart + m->total()*elemSize;
} }
......
...@@ -2082,4 +2082,12 @@ TEST(Mat, regression_12943) // memory usage: ~4.5 Gb ...@@ -2082,4 +2082,12 @@ TEST(Mat, regression_12943) // memory usage: ~4.5 Gb
cv::flip(src, dst, 0); cv::flip(src, dst, 0);
} }
TEST(Mat, empty_iterator_16855)
{
cv::Mat m;
EXPECT_NO_THROW(m.begin<uchar>());
EXPECT_NO_THROW(m.end<uchar>());
EXPECT_TRUE(m.begin<uchar>() == m.end<uchar>());
}
}} // namespace }} // namespace
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册