Re: void * C array to a Numpy array using Swig



Krish wrote:
Hello People

I hope I am On Topic. Anyways, here is my problem. Any insights would
be really appreciated.

Posting to the numpy-discussion@xxxxxxxxxxxxxxxxxxxxx list would help generate more responses, I think.



I have wrapped a C IO module using SWIG -> Python Module. Suppose the name of the module is "imageio" and the reader function from the file is image_read() which returns an object ( "filled" C structure) with a lot of members.

Let

a = imageio.image_read("testfile.image")

Now a is the object which contains pointer to data of the image. ( void
* pdata). ( a also contains the datatype which I need to typecast to
get the data pointed by pdata). My question is how to access the data
pointed by pdata in *Python*.

Yes, you are right that you need to use typemaps. It's been awhile since I did this kind of thing, but here are some pointers.


1)  Look at Michael Sanner's typemaps at

http://www.scripps.edu/mb/olson/people/sanner/html/Python/NumericTypemaps/

Except for the CHAR version, these should work for NumPy by replacing Numeric/arrayobject.h with numpy/arrayobject.h

2) In full scipy there are typemaps for numpy arrays in cluster/src/swig_num.i

Look here...

http://projects.scipy.org/scipy/scipy/file/trunk/Lib/cluster/src/swig_num.i

This should help you get started with some examples. Typemaps can be a little confusing at first, but they do make your interface a bit nicer.

-Travis

.



Relevant Pages

  • Re: Pocket PC - Pointer Crash
    ... PUSHORT pData; ... I'm de-referencing a pointer who's contents/values I can view without any ... problems in the memory watch window. ... int FOO ...
    (microsoft.public.pocketpc.developer)
  • void * C array to a Numpy array using Swig
    ... I have wrapped a C IO module using SWIG -> Python Module. ... Now a is the object which contains pointer to data of the image. ... get the data pointed by pdata). ...
    (comp.lang.python)
  • Re: Need Help declaring parameter variables for DllImport for c-dl
    ... What you said about the nSize and nLength makes a lot of sense. ... right about the pointer to the encoded data pointed by a pointer in the Blob ... IntPtr pData = Marshal.AllocHGlobal; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: fread choking on binary file
    ... pData, rather than into the memory address occupied by pData ... show how the memory for Image.Print.pData is ... I removed it and now the pointer to the buffer is assigned to ...
    (microsoft.public.vc.language)
  • VB Marshal problem
    ... I've declared a VB callback Sub which is called by an unmanaged DLL (code ... The PData parameter is a pointer to a memory bloc which is allocated ... The RequestFunc should read a string pointed by PData, ...
    (microsoft.public.dotnet.languages.vb)