[C++-sig] Conversion problem

Nicolas Lelong nico_ml at mgdesign.org
Wed Jan 13 12:17:23 CET 2010


Simon,

it seems that I wrapped the same videoinput library you're using.

I wrapped the getPixels the following way : python script is responsible 
of the memory allocation for pixels buffer (in a correctly sized string).

The getPixels function is wrapped as follows :

namespace {

   bool videoInput_getPixels(videoInput& input, int device_id, 
python::object memory_buffer)
   {
     PyObject* pyObject = memory_buffer.ptr();
     if (PyString_CheckExact(pyObject))
     {
       Py_ssize_t string_size = PyString_Size(pyObject);
       if (string_size >= Py_ssize_t(input.getWidth(device_id)) * 
Py_ssize_t(input.getHeight(device_id)) * Py_ssize_t(3))
       {
         unsigned char* pixels = reinterpret_cast<unsigned 
char*>(PyString_AsString(pyObject));

         return input.getPixels(device_id, pixels, false, false);
       }
     }
     return false;
   }

};

python::class_<videoInput, boost::noncopyable> klass("VideoInput");
klass.def("getPixels", videoInput_getPixels);

This certainly lacks some error checking for the actual 'memory_buffer' 
parameter type, but can give you a clue.

HTH,

Nicolas



More information about the Cplusplus-sig mailing list