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 2 2044
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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....
|
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...
|
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(){}
};
|
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...
|
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)
|
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.
...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM).
In this month's session, the creator of the excellent VBE...
|
by: MeoLessi9 |
last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: Aftab Ahmad |
last post by:
Hello Experts!
I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
|
by: Aftab Ahmad |
last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below.
Dim IE As Object
Set IE =...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: marcoviolo |
last post by:
Dear all,
I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
| |