Can I pass Image.GetDataBuffer() to c++?

Hi,

Is it possible to use wx.Image.GetDataBuffer() from a c++ extension? If so, is it possible to use it from swig?
I am just learning swig and have not built a python extension in c or c++ before.

What I want is to recover the mutable char* from the wxWidgets.API.

--John

John C. Femiani wrote:

Hi,

Is it possible to use wx.Image.GetDataBuffer() from a c++ extension? If so, is it possible to use it from swig?
I am just learning swig and have not built a python extension in c or c++ before.

What I want is to recover the mutable char* from the wxWidgets.API.

There are two ways to approach this. The first would be to just pass the wx.Image object to your extension. That means that your extension would have to be able to be linked with wxWidgets and deal with the wxPython/SWIG API for type checking, etc. But if you're doing that anyway then just passing the image would probably be simplest and most flexible (you could then use the rest of the wxImage API from your extension code if needed, etc.)

The other approach is to pass the return value of GetDataBuffer like you suggest above. What you'll get in the extension code will be a generic PyObject pointer, but it will really point to a PyBuffer object so you can use the PyBuffer_* API functions to get the data pointer and buffer length.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Robin Dunn wrote:

John C. Femiani wrote:

Hi,

Is it possible to use wx.Image.GetDataBuffer() from a c++ extension? If so, is it possible to use it from swig?
I am just learning swig and have not built a python extension in c or c++ before.

What I want is to recover the mutable char* from the wxWidgets.API.

There are two ways to approach this. The first would be to just pass the wx.Image object to your extension. That means that your extension would have to be able to be linked with wxWidgets and deal with the wxPython/SWIG API for type checking, etc. But if you're doing that anyway then just passing the image would probably be simplest and most flexible (you could then use the rest of the wxImage API from your extension code if needed, etc.)

The other approach is to pass the return value of GetDataBuffer like you suggest above. What you'll get in the extension code will be a generic PyObject pointer, but it will really point to a PyBuffer object so you can use the PyBuffer_* API functions to get the data pointer and buffer length.

Well, I am hoping to start using python as an 'integration' language. Ideally I would like to mix wx, PIL, and NumPy stuff with my own C++ image code. I would rather not have the C++ extension be aware that it was even dealing with wx, so the second approach sounds better to me. I will read up on the PyBuffer API. The fact that it can have multiple segments scares me, but hopefully these wxImage buffers are contiguous anyhow.

-- John

John C. Femiani wrote:

Well, I am hoping to start using python as an 'integration' language. Ideally I would like to mix wx, PIL, and NumPy stuff with my own C++ image code. I would rather not have the C++ extension be aware that it was even dealing with wx, so the second approach sounds better to me. I will read up on the PyBuffer API. The fact that it can have multiple segments scares me, but hopefully these wxImage buffers are contiguous anyhow.

I don't know what your plans are with regard to python versions, but in Python 3 (and 2.6/) there will be a new buffer protocol, inspired by numpy, that will be well suited to this kind of thing, you might want to take a look at it:

Hopefully, it will get integrated into future versions of wxPython and PIL, and certainly numpy.

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Chris Barker wrote:

John C. Femiani wrote:

Well, I am hoping to start using python as an 'integration' language. Ideally I would like to mix wx, PIL, and NumPy stuff with my own C++ image code. I would rather not have the C++ extension be aware that it was even dealing with wx, so the second approach sounds better to me. I will read up on the PyBuffer API. The fact that it can have multiple segments scares me, but hopefully these wxImage buffers are contiguous anyhow.

I don't know what your plans are with regard to python versions, but in Python 3 (and 2.6/) there will be a new buffer protocol, inspired by numpy, that will be well suited to this kind of thing, you might want to take a look at it:

PEP 3118 – Revising the buffer protocol | peps.python.org

Hopefully, it will get integrated into future versions of wxPython and PIL, and certainly numpy.

Fantastic! Thanks, I like the explanation/rationale of the existing system and I look forward to the new system.

--John