I have a C function which controls a camera, I am trying to send the image buffer to Python via Ctypes to be used in a GUI.
I can grab the buffer, but I am stuck on basically how to get this to work, I suppose I could look into numpy-c api but it looks very confusing.
Thus far I have tried the following:
Create a C array (I guess it is by pointer reference), something like:
UINT16 * MyFunction()
{
...grab image... and return Buffer
size = 2088 * 2080 * sizeof( UINT16);
array = (BYTE*) malloc(size);
API_Write_to_buffer(Buffer, 0, array, size);
API_Free_Buffer(Buffer);
return array;
}
I can try to get the return of the array in python:
Data = MyFunction()
array = (c_ubyte * size).from_address(addressof(Data.contents))
where MyFunction() looks like the following:
def MyFunction():
MyFunction= lib.MyFunction
MyFunction.restype = POINTER(c_ubyte)
img_arr = MyFunction()
return img_arr
Another option is to read line by line using pointers:
for(i=0;i<Width;i++)
{
Ptr = (UINT16*)(bufferPointer + i*bufferWidth);
for(j=0;j<Height;j++)
{
...
}
}
UPDATE:
It turns out I need to assign a pointer to my array, but it is a UINT16 in C. When I try to get the array into a numpy array, python crashes. I can get this as a return from the wrapper function:
def Grab(nframes):
Grab= 2112 * 2088 * 8 (size of frame width * height * pixelDepth)
Grab= lib.MyFunction
Grab.argtypes = [c_uint32]
Grab.restype = POINTER(c_uint16 * array_length)
r = Grab(nframes)
return r
Calling the function looks like this:
Data = Grab(1)
print Data
Which returns this:
<__main__.LP_c_ushort_Array_4409856 object at 0x000000000436FE48>