def __init__(self): self.__dll__ = ctypes.windll.LoadLibrary('tests/demo_dll.dll') filter_func_type = ctypes.WINFUNCTYPE(ctypes.c_int16, ctypes.POINTER(image_data)) filter_func_type.memsync = [{ 'p': [0, 'data'], 'l': ([0, 'width'], [0, 'height']), 'f': 'lambda x, y: x * y', 't': 'c_int16' }] self.__apply_filter_to_image__ = self.__dll__.apply_filter_to_image self.__apply_filter_to_image__.argtypes = (ctypes.POINTER(image_data), ctypes.POINTER(image_data), filter_func_type) self.__apply_filter_to_image__.memsync = [{ 'p': [0, 'data'], 'l': ([0, 'width'], [0, 'height']), 'f': 'lambda x, y: x * y', 't': 'c_int16' }, { 'p': [1, 'data'], 'l': ([1, 'width'], [1, 'height']), 'f': 'lambda x, y: x * y', 't': 'c_int16' }] @filter_func_type def filter_edge_detection(in_buffer): filter_matrix = [[0, 1, 0], [1, -4, 1], [0, 1, 0]] width = in_buffer.contents.width height = in_buffer.contents.height assert width == 3 and height == 3 in_matrix = self.array_to_matrix( ctypes.cast(in_buffer.contents.data, ctypes.POINTER(ctypes.c_int16 * (width * height))).contents[:], width, height) out_value = 0 for matrix_line, filter_line in zip(in_matrix, filter_matrix): out_value += sum( [a * b for a, b in zip(matrix_line, filter_line)]) return out_value self.__filter_edge_detection__ = filter_edge_detection
def __init__(self): self.__dll__ = ctypes.windll.LoadLibrary('tests/demo_dll.dll') conveyor_belt = ctypes.WINFUNCTYPE(ctypes.c_int16, ctypes.c_int16) self.__use_optional_callback__ = self.__dll__.use_optional_callback_a self.__use_optional_callback__.argtypes = (ctypes.c_int16, conveyor_belt) self.__use_optional_callback__.restype = ctypes.c_int16 @conveyor_belt def process_data(in_data): return in_data**2 self.__process_data__ = process_data
def __init__(self): self.__dll__ = ctypes.windll.LoadLibrary('tests/demo_dll.dll') conveyor_belt = ctypes.WINFUNCTYPE(ctypes.c_int16, ctypes.c_int16) self.__sum_elements_from_callback__ = self.__dll__.sum_elements_from_callback self.__sum_elements_from_callback__.argtypes = (ctypes.c_int16, conveyor_belt) self.__sum_elements_from_callback__.restype = ctypes.c_int16 self.DATA = [1, 6, 8, 4, 9, 7, 4, 2, 5, 2] @conveyor_belt def get_data(index): print((index, self.DATA[index])) return self.DATA[index] self.__get_data__ = get_data
import ctypes else: raise # TODO unsupported platform # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ # RUN # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ if __name__ == '__main__': DATA = [1, 6, 8, 4, 9, 7, 4, 2, 5, 2] conveyor_belt = ctypes.WINFUNCTYPE(ctypes.c_int16, ctypes.c_int16) @conveyor_belt def get_data(index): print((index, DATA[index])) return DATA[index] dll = ctypes.windll.LoadLibrary('demo_dll.dll') sum_elements_from_callback = dll.sum_elements_from_callback sum_elements_from_callback.argtypes = (ctypes.c_int16, conveyor_belt) sum_elements_from_callback.restype = ctypes.c_int16 test_sum = sum_elements_from_callback(len(DATA), get_data) print(('sum', 48, test_sum)) # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++