diff --git a/modules/python/src2/cv2.cpp b/modules/python/src2/cv2.cpp index 03fd912104..bf59d40a4f 100644 --- a/modules/python/src2/cv2.cpp +++ b/modules/python/src2/cv2.cpp @@ -27,6 +27,80 @@ # define CV_PYTHON_TYPE_HEAD_INIT() PyObject_HEAD_INIT(&PyType_Type) 0, #endif +#define CV_PY_TO_CLASS(TYPE) \ +template<> bool pyopencv_to(PyObject* dst, Ptr& src, const char* name); \ + \ +template<> \ +bool pyopencv_to(PyObject* dst, TYPE& src, const char* name) \ +{ \ + if (!dst || dst == Py_None) \ + return true; \ + Ptr ptr; \ + \ + if (!pyopencv_to(dst, ptr, name)) return false; \ + src = *ptr; \ + return true; \ +} + +#define CV_PY_FROM_CLASS(TYPE) \ +template<> PyObject* pyopencv_from(const Ptr& src); \ + \ +template<> \ +PyObject* pyopencv_from(const TYPE& src) \ +{ \ + Ptr ptr(new TYPE()); \ + \ + *ptr = src; \ + return pyopencv_from(ptr); \ +} + +#define CV_PY_TO_CLASS_PTR(TYPE) \ +template<> bool pyopencv_to(PyObject* dst, Ptr& src, const char* name); \ + \ +template<> \ +bool pyopencv_to(PyObject* dst, TYPE*& src, const char* name) \ +{ \ + if (!dst || dst == Py_None) \ + return true; \ + Ptr ptr; \ + \ + if (!pyopencv_to(dst, ptr, name)) return false; \ + src = ptr; \ + return true; \ +} + +#define CV_PY_FROM_CLASS_PTR(TYPE) \ +template<> PyObject* pyopencv_from(const Ptr& src); \ + \ +static PyObject* pyopencv_from(TYPE*& src) \ +{ \ + return pyopencv_from(Ptr(src)); \ +} + +#define CV_PY_TO_ENUM(TYPE) \ +template<> bool pyopencv_to(PyObject* dst, std::underlying_type::type& src, const char* name); \ + \ +template<> \ +bool pyopencv_to(PyObject* dst, TYPE& src, const char* name) \ +{ \ + if (!dst || dst == Py_None) \ + return true; \ + std::underlying_type::type underlying; \ + \ + if (!pyopencv_to(dst, underlying, name)) return false; \ + src = static_cast(underlying); \ + return true; \ +} + +#define CV_PY_FROM_ENUM(TYPE) \ +template<> PyObject* pyopencv_from(const std::underlying_type::type& src); \ + \ +template<> \ +PyObject* pyopencv_from(const TYPE& src) \ +{ \ + return pyopencv_from(static_cast::type>(src)); \ +} + #include "pyopencv_generated_include.h" #include "opencv2/core/types_c.h" @@ -735,12 +809,31 @@ bool pyopencv_to(PyObject* o, UMat& um, const char* name) } template<> -PyObject* pyopencv_from(const UMat& m) { +PyObject* pyopencv_from(const UMat& m) +{ PyObject *o = PyObject_CallObject((PyObject *) &cv2_UMatWrapperType, NULL); *((cv2_UMatWrapperObject *) o)->um = m; return o; } +template<> +bool pyopencv_to(PyObject* obj, void*& ptr, const char* name) +{ + (void)name; + if (!obj || obj == Py_None) + return true; + + if (!PyLong_Check(obj)) + return false; + ptr = PyLong_AsVoidPtr(obj); + return ptr != NULL && !PyErr_Occurred(); +} + +static PyObject* pyopencv_from(void*& ptr) +{ + return PyLong_FromVoidPtr(ptr); +} + static bool pyopencv_to(PyObject *o, Scalar& s, const ArgInfo info) { if(!o || o == Py_None) @@ -843,6 +936,30 @@ bool pyopencv_to(PyObject* obj, int& value, const char* name) return value != -1 || !PyErr_Occurred(); } +#if defined (_M_AMD64) || defined (__x86_64__) +template<> +PyObject* pyopencv_from(const unsigned int& value) +{ + return PyLong_FromUnsignedLong(value); +} + +template<> + +bool pyopencv_to(PyObject* obj, unsigned int& value, const char* name) +{ + (void)name; + if(!obj || obj == Py_None) + return true; + if(PyInt_Check(obj)) + value = (unsigned int)PyInt_AsLong(obj); + else if(PyLong_Check(obj)) + value = (unsigned int)PyLong_AsLong(obj); + else + return false; + return value != (unsigned int)-1 || !PyErr_Occurred(); +} +#endif + template<> PyObject* pyopencv_from(const uchar& value) {