Guest User

Bindings_cvMatData.h

a guest
Sep 26th, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.34 KB | None | 0 0
  1. #ifndef __SOFAOR_BINDING_CVMATDATA_H__
  2. #define __SOFAOR_BINDING_CVMATDATA_H__
  3.  
  4. #include <SofaPython/PythonMacros.h>
  5. #include "cvMat.h"
  6.  
  7. SP_DECLARE_CLASS_TYPE(cvMatData)
  8.  
  9. using namespace sofa::core::objectmodel;
  10. using namespace sofa::defaulttype;
  11.  
  12. static PyObject* cvMatData_getPtr(PyObject* self, PyObject* /*args*/)
  13. {
  14.   Data<sofaor::common::cvMat>* data =
  15.       sofa::py::unwrap<Data<sofaor::common::cvMat> >(self);
  16.   sofaor::common::cvMat& image =
  17.       *data->beginEdit();  // where should be the endedit?
  18.  
  19.   PyObject* imgdata = PyLong_FromVoidPtr((void*)image.data);
  20.   PyObject* shape = PyTuple_New(4);
  21.   PyTuple_SetItem(shape, 0, PyInt_FromSsize_t(image.rows));        // rows
  22.   PyTuple_SetItem(shape, 1, PyInt_FromSsize_t(image.cols));        // cols
  23.   PyTuple_SetItem(shape, 2, PyInt_FromSsize_t(image.channels()));  // channels
  24.   PyTuple_SetItem(shape, 3, PyInt_FromSsize_t(image.depth()));     // depth
  25.  
  26.   // output = tuple( list(pointers), shape tuple, type name)
  27.   PyObject* res = PyTuple_New(3);
  28.  
  29.   // the data pointer
  30.   PyTuple_SetItem(res, 0, imgdata);
  31.  
  32.   // the shape
  33.   PyTuple_SetItem(res, 1, shape);
  34.  
  35.   // the type name
  36.   PyTuple_SetItem(
  37.       res, 2, PyString_FromString(DataTypeName<sofaor::common::cvMat>::name()));
  38.  
  39.   return res;
  40. }
  41.  
  42. static PyObject* cvMatData_getData(PyObject* self, PyObject* /*args*/)
  43. {
  44.   Data<sofaor::common::cvMat>* data =
  45.       sofa::py::unwrap<Data<sofaor::common::cvMat> >(self);
  46.   sofaor::common::cvMat& image =
  47.       *data->beginEdit();  // where should be the endedit?
  48.  
  49.   PyObject* imgdata = PyString_FromStringAndSize(
  50.       (char*)image.data, image.total() * image.elemSize());
  51.   PyObject* shape = PyTuple_New(4);
  52.   PyTuple_SetItem(shape, 0, PyInt_FromSsize_t(image.rows));        // rows
  53.   PyTuple_SetItem(shape, 1, PyInt_FromSsize_t(image.cols));        // cols
  54.   PyTuple_SetItem(shape, 2, PyInt_FromSsize_t(image.channels()));  // channels
  55.   PyTuple_SetItem(shape, 3, PyInt_FromSsize_t(image.depth()));     // depth
  56.  
  57.   // output = tuple( list(pointers), shape tuple, type name)
  58.   PyObject* res = PyTuple_New(3);
  59.  
  60.   // the data pointer
  61.   PyTuple_SetItem(res, 0, imgdata);
  62.  
  63.   // the shape
  64.   PyTuple_SetItem(res, 1, shape);
  65.  
  66.   // the type name
  67.   PyTuple_SetItem(
  68.       res, 2, PyString_FromString(DataTypeName<sofaor::common::cvMat>::name()));
  69.   data->endEdit();
  70.   return res;
  71. }
  72.  
  73. SP_CLASS_METHODS_BEGIN(cvMatData)
  74. SP_CLASS_METHOD_DOC(cvMatData, getPtr,
  75.                     "retrieves a Tuple containing:\n 1. The pointer to "
  76.                     "Mat::data (as a numpy array)\n 2. The dimensions of the "
  77.                     "matrix (rows, cols, channels, depth\n 3. The name of the "
  78.                     "Sofa type ('cvMat').")
  79. SP_CLASS_METHOD_DOC(cvMatData, getData,
  80.                     "retrieves a Tuple containing:\n 1. a deep copy of the "
  81.                     "Mat::data byte array (as a numpy array)\n 2. The "
  82.                     "dimensions of the matrix (rows, cols, channels, depth\n "
  83.                     "3. The name of the Sofa type ('cvMat').")
  84. SP_CLASS_METHODS_END
  85.  
  86. // eventual attributes
  87. SP_CLASS_ATTRS_BEGIN(cvMatData)
  88. // SP_CLASS_ATTR(cvMatData, paramName)
  89. SP_CLASS_ATTRS_END
  90.  
  91. SP_CLASS_TYPE_PTR_ATTR(cvMatData,
  92.                        sofa::core::objectmodel::Data<sofaor::common::cvMat>,
  93.                        Data)
  94.  
  95. #endif  // __SOFAOR_BINDING_CVMATDATA_H__
Advertisement
Add Comment
Please, Sign In to add comment