473,400 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 software developers and data experts.

Wrapping c functions

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
Jul 19 '05 #1
2 2050
Glenn Pierce wrote:
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
da***@dalkescientific.com

Jul 19 '05 #2
Andrew Dalke <da***@dalkescientific.com> writes:
Glenn Pierce wrote:
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.


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.)

--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Roy Smith | last post by:
I've got a C library with about 50 calls in it that I want to wrap in Python. I know I could use some tool like SWIG, but that will give me a too-literal translation; I want to make some...
5
by: nimdez | last post by:
Hi, I am working on an existing code base in which a lot of data displayed to the user is formatted in tables. Most tables are printed row-by-row using printf() with "%s" print conversion...
2
by: dam_fool_2003 | last post by:
My understanding about wrapping is that we add utilities to a lib function for our specific use. So days ago in the c.l.c I saw a function def as: void * xmalloc (size_t size) { register void...
1
by: John Lee | last post by:
Hi, I understand we could use p/Invoke to access most of the C/C++ DLL but there are some cases the functions can only be accessed by C/C++ so it would be good if I could create a .NET class to...
5
by: Lee Crabtree | last post by:
More fun wrapping unmanaged code... I have a class heirarchy that I need to expose to C#, so I need to wrap all the classes. That's not a big deal, except for the base class, which is abstract....
10
by: Tom the Canuck | last post by:
What would be the best way to proceed? Should I make a pure virtual class and then derive from that? I want the base class to have functions defined so that I don't have to do the work all over...
6
by: Gerhard Prilmeier | last post by:
Hello, I have an unmanaged C++ API that uses virtual functions, like this: class A { public: virtual void handleMe(){} };
3
by: gabriel.becedillas | last post by:
Hi, I'm having problems wrapping a hierarchy of classes, actually having problems wrapping the base class. I don't need to use the WrapClass mechanism since I don't want to override classes in...
8
by: Anish Chapagain | last post by:
Hi!! I tried wrapping a simple C code suing SWIG to Python, but am having problem, my .c file is, Step 1: example.c -------------- double val=3.0; int fact(int n)
5
by: Charlie | last post by:
Hi All, I am new to using swig/C++/python. I got some problem with function pointers. I posted in swig-user, but got no response. So I forwarded it here. You help is greatly appreciated. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.