Wrapping c functions

G

Glenn Pierce

Hi I have been trying to wrap a c library called FreeImage for python
and am having trouble with a couple of functions.

One function is

FreeImage_Load which takes parameters (enum, const char*, int)

I have wrapped the function with the following code
static PyObject *
freeimage_load(PyObject *self, PyObject *args)
{
int format, flags;
char* filename = "None";
FIBITMAP *dib = NULL;

if (!PyArg_ParseTuple(args, "isi", &format, filename, &flags))
return NULL;

dib = FreeImage_Load(format, filename, flags);

return Py_BuildValue("o", dib);
}

However I am getting a segmentation fault at PyArg_ParseTuple.
Also I have little Idea what to return from the function. FIBITMAP * is
an opaque pointer
that I pass to other FreeImage functions, I pretty certain
Py_BuildValue("o", dib) is wrong.

There is also the function
unsigned char *FreeImage_GetBits(FIBITMAP *dib);

I and not sure how to return the bits to python.

I would probably like to display an image with the gtk function

drawable.draw_rgb_image(*gc*, *x*, *y*, *width*, *height*, *dith*, *rgb_buf*, *rowstride*)

here the RGB Image data is packed in a string as a
sequence of 8-bit RGB pixel triplets


but I have no idea how to get from unsigned char * to that.

Any advice would be greatly appreciated.

Thanks
 
A

Andrew Dalke

Glenn said:
if (!PyArg_ParseTuple(args, "isi", &format, filename, &flags))
return NULL;

Shouldn't that be &filename ? See
http://docs.python.org/ext/parseTuple.html
for examples.

dib = FreeImage_Load(format, filename, flags);
Also I have little Idea what to return from the function. FIBITMAP * is
an opaque pointer
that I pass to other FreeImage functions, I pretty certain
Py_BuildValue("o", dib) is wrong.

If it's truly opaque and you trust your use of the code you can
cast it to an integer, use the integer in the Python code, and
at the Python/C interface cast the integer back to a pointer.
Of course if it no longer exists you'll get a segfault.

If you want more type safety you can use the SWIG approach and
encode the pointers as a string, with type information and
pointer included.

Or use the Python extension API to make a new type.

Searching the archives I see people have tried to write a Python/
FreeImage interface using SWIG.


Andrew
(e-mail address removed)
 
D

David M. Cooke

Andrew Dalke said:
Shouldn't that be &filename ? See
http://docs.python.org/ext/parseTuple.html
for examples.




If it's truly opaque and you trust your use of the code you can
cast it to an integer, use the integer in the Python code, and
at the Python/C interface cast the integer back to a pointer.
Of course if it no longer exists you'll get a segfault.

If you want more type safety you can use the SWIG approach and
encode the pointers as a string, with type information and
pointer included.

Better yet, use a CObject. That way, a destructor can be added so as
to not leak memory. Type info could be included in the desc field.

return PyCObject_FromVoidPtr(dib, NULL)

(the NULL can be replaced with a routine that will free the image.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top