Skip to content

Commit

Permalink
partly fixed support for 0D/scalar UMat's
Browse files Browse the repository at this point in the history
  • Loading branch information
vpisarev committed Jun 16, 2023
1 parent a8d1402 commit c5665e8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
4 changes: 1 addition & 3 deletions modules/core/include/opencv2/core/mat.inl.hpp
Expand Up @@ -3427,9 +3427,7 @@ bool UMat::isSubmatrix() const
inline
size_t UMat::elemSize() const
{
size_t res = dims > 0 ? step.p[dims - 1] : 0;
CV_DbgAssert(res != 0);
return res;
return CV_ELEM_SIZE(flags);
}

inline
Expand Down
6 changes: 3 additions & 3 deletions modules/core/src/copy.cpp
Expand Up @@ -336,15 +336,15 @@ void Mat::copyTo( OutputArray _dst ) const
_dst.create( dims, size.p, type(), -1, allowTransposed );
UMat dst = _dst.getUMat();
CV_Assert(dst.u != NULL);
size_t i, sz[CV_MAX_DIM] = {0}, dstofs[CV_MAX_DIM], esz = elemSize();
CV_Assert(dims > 0 && dims < CV_MAX_DIM);
size_t i, sz[CV_MAX_DIM] = {1}, dstofs[CV_MAX_DIM] = {0}, esz = elemSize();
CV_Assert(dims >= 0 && dims < CV_MAX_DIM);
for( i = 0; i < (size_t)dims; i++ )
sz[i] = size.p[i];
int lastdim = dims >= 1 ? dims-1 : 0;
sz[lastdim] *= esz;
dst.ndoffset(dstofs);
dstofs[lastdim] *= esz;
dst.u->currAllocator->upload(dst.u, data, dims, sz, dstofs, dst.step.p, step.p);
dst.u->currAllocator->upload(dst.u, data, std::max(dims, 1), sz, dstofs, dst.step.p, step.p);
return;
}

Expand Down
14 changes: 8 additions & 6 deletions modules/core/src/umatrix.cpp
Expand Up @@ -1189,12 +1189,14 @@ void UMat::copyTo(OutputArray _dst) const
return;
}

size_t i, sz[CV_MAX_DIM] = {0}, srcofs[CV_MAX_DIM], dstofs[CV_MAX_DIM], esz = elemSize();
for( i = 0; i < (size_t)dims; i++ )
size_t sz[CV_MAX_DIM] = {1}, srcofs[CV_MAX_DIM]={0}, dstofs[CV_MAX_DIM]={0};
size_t esz = elemSize();
int i, d = std::max(dims, 1);
for( i = 0; i < d; i++ )
sz[i] = size.p[i];
sz[dims-1] *= esz;
sz[d-1] *= esz;
ndoffset(srcofs);
srcofs[dims-1] *= esz;
srcofs[d-1] *= esz;

_dst.create( dims, size.p, type() );
if( _dst.isUMat() )
Expand All @@ -1208,13 +1210,13 @@ void UMat::copyTo(OutputArray _dst) const
{
dst.ndoffset(dstofs);
dstofs[dims-1] *= esz;
u->currAllocator->copy(u, dst.u, dims, sz, srcofs, step.p, dstofs, dst.step.p, false);
u->currAllocator->copy(u, dst.u, d, sz, srcofs, step.p, dstofs, dst.step.p, false);
return;
}
}

Mat dst = _dst.getMat();
u->currAllocator->download(u, dst.ptr(), dims, sz, srcofs, step.p, dst.step.p);
u->currAllocator->download(u, dst.ptr(), d, sz, srcofs, step.p, dst.step.p);
}

void UMat::copyTo(OutputArray _dst, InputArray _mask) const
Expand Down
12 changes: 12 additions & 0 deletions modules/core/test/test_umat.cpp
Expand Up @@ -1452,4 +1452,16 @@ TEST(UMat, exceptions_refcounts_issue_20594)
umat1.u->handle = original_handle;
}

TEST(UMat, copy_scalar)
{
Mat m(0, nullptr, CV_32F), m2;
m.at<float>(0) = 5;
UMat um;
m.copyTo(um);
um.copyTo(m2);
EXPECT_EQ(0, m2.dims);
EXPECT_EQ(1, m2.cols);
EXPECT_EQ(5.f, m2.at<float>(0));
}

} } // namespace opencv_test::ocl

0 comments on commit c5665e8

Please sign in to comment.