I am using python ctypes and libc to interface with a vendor-provided DLL file. The purpose of the DLL file is to acquire an image from a camera.
The image acquisition appears to run without error; the issue I am having is accessing the data.
The image acquisition function takes a ctypes.c_void_p as an argument for the image data.
simplified as follows:
"""
typedef struct AvailableData
{
void* initial_readout;
int64 readout_count;
} imageData;
"""
class AvailableData(ctypes.Structure):
_fields_ = [("initial_readout", ctypes.c_void_p),
("readout_count", ctypes.c_longlong)]
"""
Prototype
Acquire(
CamHandle camera,
int64 readout_count,
int readout_time_out,
AvailableData* available,
AcquisitionErrorsMask* errors );
"""
>>> imageData = AvailableData()
>>> Acquire.argtypes = CamHandle, ctypes.c_longlong, ctypes.c_int,
ctypes.POINTER(AvailableData), ctypes.POINTER(AcquisitionErrorsMask)
>>> Acquire.restype = ctypes.c_void_p
>>> status = Acquire(camera, readout_count, readout_time_out, imageData, errors)
I do not fully understand exactly what the function is doing, because after I run the function, imageData.initial_readout
appears to be a type 'long' (not even a ctypes.c_long: just 'long'). However, it also has a value associated with it. I'm assuming this is a starting address of where the data is stored.
>>> type(imageData.initial_readout)
<type 'long'>
>>> imageData.initial_readout
81002560L
My current approach for accessing the data is to use libc.fopen, libc.fwrite, libc.fclose as follows:
>>> libc = ctypes.cdll.msvcrt
>>> fopen = libc.fopen
>>> fwrite = libc.fwrite
>>> fclose = libc.fclose
>>> fopen.argtypes = ctypes.c_char_p, ctypes.c_char_p
>>> fopen.restype = ctypes.c_void_p
>>> fopen.restype = ctypes.c_void_p
>>> fwrite.argtypes = ctypes.c_void_p, ctypes.c_size_t, ctypes.c_size_t, ctypes.c_void_p
>>> fwrite.restype = ctypes.c_size_t
>>> fclose = libc.fclose
>>> fclose.argtypes = ctypes.c_void_p,
>>> fclose.restype = ctypes.c_int
>>> fp = fopen('output3.raw', 'wb')
>>> print 'fwrite returns: ',fwrite(ctypes.c_void_p(imageData.initial_readout), readoutstride.value, 1, fp)
fwrite returns: 0
>>> fclose(fp)
where readoutstride = 2097152
corresponding to a 1024x1024 array of 16 bit pixels.
The file "output3.raw" shows up in windows explorer, however, it has 0 kbytes and when I try to open it with (e.g. with an imag viewer) it says the file is empty.
I see that fwrite
returns a value of 0 (but should return a value of 1)
If you have any ideas as to what I'm doing wrong here, I appreciate it immensely. Thank you in advance.
Specify argtypes
, restype
of the functions.
import ctypes
libc = ctypes.windll.msvcrt
fopen = libc.fopen
fopen.argtypes = ctypes.c_char_p, ctypes.c_char_p,
fopen.restype = ctypes.c_void_p
fwrite = libc.fwrite
fwrite.argtypes = ctypes.c_void_p, ctypes.c_size_t, ctypes.c_size_t, ctypes.c_void_p
fwrite.restype = ctypes.c_size_t
fclose = libc.fclose
fclose.argtypes = ctypes.c_void_p,
fclose.restype = ctypes.c_int
fp = fopen('output3.raw', 'wb')
fwrite('Hello', 5, 1, fp)
fclose(fp)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With