Replace static numpy allocator by function containing static.

That enables the numpy code to be its own library, in case
some users want to (e.g. CLIF library).
pull/25493/head
Vincent Rabaud 1 year ago
parent 2cd330486e
commit 8f7e55a60b
  1. 2
      modules/core/misc/python/pyopencv_umat.hpp
  2. 10
      modules/python/src2/cv2_convert.cpp
  3. 2
      modules/python/src2/cv2_numpy.cpp
  4. 2
      modules/python/src2/cv2_numpy.hpp

@ -28,7 +28,7 @@ static void* cv_UMat_context()
static Mat cv_UMat_get(const UMat* _self) static Mat cv_UMat_get(const UMat* _self)
{ {
Mat m; Mat m;
m.allocator = &g_numpyAllocator; m.allocator = &GetNumpyAllocator();
_self->copyTo(m); _self->copyTo(m);
return m; return m;
} }

@ -56,7 +56,7 @@ bool pyopencv_to(PyObject* o, Mat& m, const ArgInfo& info)
if(!o || o == Py_None) if(!o || o == Py_None)
{ {
if( !m.data ) if( !m.data )
m.allocator = &g_numpyAllocator; m.allocator = &GetNumpyAllocator();
return true; return true;
} }
@ -298,14 +298,14 @@ bool pyopencv_to(PyObject* o, Mat& m, const ArgInfo& info)
#endif #endif
m = Mat(ndims, size, type, PyArray_DATA(oarr), step); m = Mat(ndims, size, type, PyArray_DATA(oarr), step);
m.u = g_numpyAllocator.allocate(o, ndims, size, type, step); m.u = GetNumpyAllocator().allocate(o, ndims, size, type, step);
m.addref(); m.addref();
if( !needcopy ) if( !needcopy )
{ {
Py_INCREF(o); Py_INCREF(o);
} }
m.allocator = &g_numpyAllocator; m.allocator = &GetNumpyAllocator();
return true; return true;
} }
@ -316,9 +316,9 @@ PyObject* pyopencv_from(const cv::Mat& m)
if( !m.data ) if( !m.data )
Py_RETURN_NONE; Py_RETURN_NONE;
cv::Mat temp, *p = (cv::Mat*)&m; cv::Mat temp, *p = (cv::Mat*)&m;
if(!p->u || p->allocator != &g_numpyAllocator) if(!p->u || p->allocator != &GetNumpyAllocator())
{ {
temp.allocator = &g_numpyAllocator; temp.allocator = &GetNumpyAllocator();
ERRWRAP2(m.copyTo(temp)); ERRWRAP2(m.copyTo(temp));
p = &temp; p = &temp;
} }

@ -6,8 +6,6 @@
#include "cv2_numpy.hpp" #include "cv2_numpy.hpp"
#include "cv2_util.hpp" #include "cv2_util.hpp"
NumpyAllocator g_numpyAllocator;
using namespace cv; using namespace cv;
UMatData* NumpyAllocator::allocate(PyObject* o, int dims, const int* sizes, int type, size_t* step) const UMatData* NumpyAllocator::allocate(PyObject* o, int dims, const int* sizes, int type, size_t* step) const

@ -18,7 +18,7 @@ public:
const cv::MatAllocator* stdAllocator; const cv::MatAllocator* stdAllocator;
}; };
extern NumpyAllocator g_numpyAllocator; inline NumpyAllocator& GetNumpyAllocator() {static NumpyAllocator gNumpyAllocator;return gNumpyAllocator;}
//====================================================================================================================== //======================================================================================================================

Loading…
Cancel
Save