From 68f527267bdeb6179d0b37665b29b4412d69794a Mon Sep 17 00:00:00 2001 From: nhlsm Date: Sat, 15 Aug 2020 02:21:23 +0900 Subject: [PATCH] Merge pull request #18080 from nhlsm:improve-mat-operator-assign-scalar * improve Mat::operator=(Scalar) * touch * remove trailing whitespace * TEST: check if old code pass test or not * remove CV_Error * remove warning * fix: is -> Scalar * 1) Mat *mat -> Mat &mat 2) return bool, add output param * add comment --- modules/core/src/copy.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/modules/core/src/copy.cpp b/modules/core/src/copy.cpp index 48440ef265..7f4329df78 100644 --- a/modules/core/src/copy.cpp +++ b/modules/core/src/copy.cpp @@ -414,6 +414,29 @@ void Mat::copyTo( OutputArray _dst, InputArray _mask ) const copymask(ptrs[0], 0, ptrs[2], 0, ptrs[1], 0, sz, &esz); } + +static bool can_apply_memset(const Mat &mat, const Scalar &s, int &fill_value) +{ + // check if depth is 1 byte. + switch (mat.depth()) + { + case CV_8U: fill_value = saturate_cast( s.val[0] ); break; + case CV_8S: fill_value = saturate_cast( s.val[0] ); break; + default: return false; + } + + // check if all element is same. + const int64* is = (const int64*)&s.val[0]; + switch (mat.channels()) + { + case 1: return true; + case 2: return (is[0] == is[1]); + case 3: return (is[0] == is[1] && is[1] == is[2]); + case 4: return (is[0] == is[1] && is[1] == is[2] && is[2] == is[3]); + default: return false; + } +} + Mat& Mat::operator = (const Scalar& s) { CV_INSTRUMENT_REGION(); @@ -434,6 +457,14 @@ Mat& Mat::operator = (const Scalar& s) } else { + int fill_value = 0; + if ( can_apply_memset(*this, s, fill_value) ) + { + for (size_t i = 0; i < it.nplanes; i++, ++it) + memset(dptr, fill_value, elsize); + return *this; + } + if( it.nplanes > 0 ) { double scalar[12];