def apply_filter_to_image(self, in_image): width = len(in_image[0]) height = len(in_image) in_image_ctypes = image_data() out_image_ctypes = image_data() in_image_ctypes.width = ctypes.c_int16(width) in_image_ctypes.height = ctypes.c_int16(height) in_image_ctypes.data = ctypes.cast( ctypes.pointer((ctypes.c_int16 * (width * height))(*self.matrix_to_array(in_image))), ctypes.POINTER(ctypes.c_int16) ) self.__apply_filter_to_image__( ctypes.pointer(in_image_ctypes), ctypes.pointer(out_image_ctypes), self.__filter_edge_detection__ ) return self.array_to_matrix( ctypes.cast(out_image_ctypes.data, ctypes.POINTER(ctypes.c_int16 * (width * height))).contents[:], width, height )
def __init__(self): self.__dll__ = ctypes.windll.LoadLibrary('tests/demo_dll.dll') # void gauss_elimination(float [3][4] *) self.__gauss_elimination__ = self.__dll__.gauss_elimination self.__gauss_elimination__.argtypes = (ctypes.POINTER( ctypes.c_float * 4 * 3), ctypes.POINTER(ctypes.c_float * 3))
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') # vector3d *distance(vector3d *, vector3d *) self.__vector3d_add__ = self.__dll__.vector3d_add self.__vector3d_add__.argtypes = (ctypes.POINTER(vector3d), ctypes.POINTER(vector3d)) self.__vector3d_add__.restype = ctypes.POINTER(vector3d)
def replace_r(self, in_string, old_letter, new_letter): string_buffer = (ctypes.c_char_p * 1)(in_string.encode('utf-8')) string_buffer_p = ctypes.cast( string_buffer, ctypes.POINTER(ctypes.POINTER(ctypes.c_char))) self.__replace_r__(string_buffer_p, old_letter.encode('utf-8'), new_letter.encode('utf-8')) return string_buffer[:][0].decode('utf-8')
def __init__(self): self.__dll__ = ctypes.windll.LoadLibrary('tests/demo_dll.dll') self.__replace_r__ = self.__dll__.replace_letter_in_null_terminated_string_r self.__replace_r__.argtypes = ( ctypes.POINTER(ctypes.POINTER( ctypes.c_char)), # Generate pointer to char manually ctypes.c_char, ctypes.c_char) self.__replace_r__.memsync = [{'p': [0, -1], 'n': True}]
def square_int_array(self, in_array): in_array_p = ctypes.cast( ctypes.pointer((ctypes.c_int16 * len(in_array))(*in_array)), ctypes.POINTER(ctypes.c_int16)) out_array_p = ctypes.pointer(ctypes.c_void_p()) self.__square_int_array__(in_array_p, out_array_p, ctypes.c_int16(len(in_array))) return ctypes.cast(out_array_p.contents, ctypes.POINTER(ctypes.c_int16 * len(in_array))).contents[:]
def __init__(self): self.__dll__ = ctypes.windll.LoadLibrary('tests/demo_dll.dll') self.__vector3d_add_array__ = self.__dll__.vector3d_add_array self.__vector3d_add_array__.argtypes = (ctypes.POINTER(vector3d), ctypes.c_int16) self.__vector3d_add_array__.restype = ctypes.POINTER(vector3d) self.__vector3d_add_array__.memsync = [{ 'p': [0], 'l': [1], 't': 'vector3d' }]
def __init__(self): self.__dll__ = ctypes.windll.LoadLibrary('tests/demo_dll.dll') # double distance(Point *, Point *) self.distance = self.__dll__.cookbook_distance self.distance.argtypes = (ctypes.POINTER(Point), ctypes.POINTER(Point)) self.distance.restype = ctypes.c_double # double *distance_pointer(Point *, Point *) self.__distance_pointer__ = self.__dll__.cookbook_distance_pointer self.__distance_pointer__.argtypes = (ctypes.POINTER(Point), ctypes.POINTER(Point)) self.__distance_pointer__.restype = ctypes.POINTER(ctypes.c_double)
def __init__(self): # Generate reference on routine in DLL self._call_demo_routine_ = ctypes.windll.LoadLibrary('demo_dll.dll').complex_demo_routine # Define parameter datatypes self._call_demo_routine_.argtypes = [ ctypes.POINTER(ctypes.c_char), ctypes.c_int, ctypes.POINTER(type_test_struct) ] # Define return datatype self._call_demo_routine_.restype = ctypes.c_void_p
def square_int_array_with_struct(self, in_array): in_array_obj = int_array_data() out_array_obj = int_array_data() in_array_obj.data = ctypes.cast( ctypes.pointer((ctypes.c_int16 * len(in_array))(*in_array)), ctypes.POINTER(ctypes.c_int16)) in_array_obj.len = len(in_array) self.__square_int_array_with_struct__(in_array_obj, out_array_obj) return ctypes.cast(out_array_obj.data, ctypes.POINTER(ctypes.c_int16 * len(in_array))).contents[:]
class int_array_data(ctypes.Structure): _fields_ = [ ('data', ctypes.POINTER(ctypes.c_int16)), ('len', ctypes.c_int16) ]
class bubblesort_data(ctypes.Structure): _fields_ = [ ('a', ctypes.POINTER(ctypes.c_float)), ('n', ctypes.c_int) ]
def __init__(self): self.__dll__ = ctypes.windll.LoadLibrary('tests/demo_dll.dll') self.__square_int_array_with_struct__ = self.__dll__.square_int_array_with_struct self.__square_int_array_with_struct__.argtypes = ( ctypes.POINTER(int_array_data), ctypes.POINTER(int_array_data)) self.__square_int_array_with_struct__.memsync = [{ 'p': [0, 'data'], 'l': [0, 'len'], 't': 'c_int16' }, { 'p': [1, 'data'], 'l': [1, 'len'], 't': 'c_int16' }]
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
def __init__(self): self.__dll__ = ctypes.windll.LoadLibrary('tests/demo_dll.dll') # int divide(int, int, int *) self.__divide__ = self.__dll__.cookbook_divide self.__divide__.argtypes = (ctypes.c_int, ctypes.c_int, ctypes.POINTER(ctypes.c_int)) self.__divide__.restype = ctypes.c_int
class image_data(ctypes.Structure): _fields_ = [ ('data', ctypes.POINTER(ctypes.c_int16)), ('width', ctypes.c_int16), ('height', ctypes.c_int16) ]
def bubblesort_segments(self, values, number_of_segments, elements_per_segment): ctypes_float_values = ((ctypes.c_float)*len(values))(*values) ctypes_float_pointer_firstelement = ctypes.cast( ctypes.pointer(ctypes_float_values), ctypes.POINTER(ctypes.c_float) ) self.__bubblesort_segments__(ctypes_float_pointer_firstelement, number_of_segments, elements_per_segment) values[:] = ctypes_float_values[:]
def bubblesort(self, values): ctypes_float_values = ((ctypes.c_float) * len(values))(*values) ctypes_float_pointer_firstelement = ctypes.cast( ctypes.pointer(ctypes_float_values), ctypes.POINTER(ctypes.c_float)) self.__bubblesort__(ctypes_float_pointer_firstelement, len(values)) values[:] = ctypes_float_values[:]
def __init__(self): self.__dll__ = ctypes.windll.LoadLibrary('tests/demo_dll.dll') # void mix_rgb_colors(int8_t [3], int8_t [3], int8_t *) self.__mix_rgb_colors__ = self.__dll__.mix_rgb_colors self.__mix_rgb_colors__.argtypes = (ctypes.c_ubyte * 3, ctypes.c_ubyte * 3, ctypes.POINTER(ctypes.c_ubyte * 3))
def tag_string(self, in_string): in_buffer = ctypes.create_string_buffer(in_string.encode('utf-8')) out_buffer = ctypes.pointer(ctypes.c_void_p()) self.__tag_string__(in_buffer, out_buffer) return ctypes.cast( out_buffer.contents, ctypes.POINTER(ctypes.c_char * (len(in_buffer) + 2)) ).contents[:].decode('utf-8').rstrip('\0')
def __init__(self): self.__dll__ = ctypes.windll.LoadLibrary('tests/demo_dll.dll') self.__fibonacci_sequence__ = self.__dll__.fibonacci_sequence_a self.__fibonacci_sequence__.argtypes = (ctypes.c_int16, ) self.__fibonacci_sequence__.restype = ctypes.POINTER(int_array_data) self.__fibonacci_sequence__.memsync = [{ 'p': ['r', 'data'], 'l': ['r', 'len'], 't': 'c_int16' }]
def __init__(self): self.__dll__ = ctypes.windll.LoadLibrary('tests/demo_dll.dll') self.__bubblesort_struct__ = self.__dll__.bubblesort_struct self.__bubblesort_struct__.memsync = [ # Regular ctypes on Windows should ignore this statement { 'p': [0, 'a'], # "path" to argument containing the pointer 'l': [0, 'n'], # "path" to argument containing the length 't': 'c_float' # type of argument (optional, default char/byte): sizeof(type) * length == bytes } ] self.__bubblesort_struct__.argtypes = (ctypes.POINTER(bubblesort_data),)
def __init__(self): self.__dll__ = ctypes.windll.LoadLibrary('tests/demo_dll.dll') self.__bubblesort_segments__ = self.__dll__.bubblesort_segments self.__bubblesort_segments__.memsync = [ # Regular ctypes on Windows should ignore this statement { 'p': [0], # "path" to argument containing the pointer 'l': ([1], [2]), # "path" to arguments containing information on length 'f': 'lambda x, y: x * y', # function computing length from relevant arguments 't': 'c_float' # type of argument (optional, default char/byte): sizeof(type) * length == bytes } ] self.__bubblesort_segments__.argtypes = (ctypes.POINTER(ctypes.c_float), ctypes.c_int, ctypes.c_int)
def __init__(self): self.__dll__ = ctypes.windll.LoadLibrary('tests/demo_dll.dll') self.__tag_string__ = self.__dll__.tag_string_b self.__tag_string__.argtypes = (ctypes.POINTER(ctypes.c_char), ctypes.c_void_p) self.__tag_string__.memsync = [{ 'p': [0], 'n': True }, { 'p': [1, -1], 'n': True }]
def __init__(self): self.__dll__ = ctypes.windll.LoadLibrary('tests/demo_dll.dll') self.__square_int_array__ = self.__dll__.square_int_array self.__square_int_array__.argtypes = (ctypes.POINTER(ctypes.c_int16), ctypes.c_void_p, ctypes.c_int16) self.__square_int_array__.memsync = [{ 'p': [0], 'l': [2], 't': 'c_int16' }, { 'p': [1, -1], 'l': [2], 't': 'c_int16' }]
def __init__(self): self.__dll__ = ctypes.windll.LoadLibrary('tests/demo_dll.dll') self.__tag_string__ = self.__dll__.tag_string_a self.__tag_string__.argtypes = (ctypes.POINTER(ctypes.c_char), ctypes.c_void_p) self.__tag_string__.memsync = [{ 'p': [0], 'l': ([0], ), 'f': 'lambda x: ctypes.sizeof(x)' }, { 'p': [1, -1], 'l': ([0], ), 'f': 'lambda x: ctypes.sizeof(x) + 2' }]
def __init__(self): self.__dll__ = ctypes.windll.LoadLibrary('tests/demo_dll.dll') self.__sum_elements_from_callback_in_struct__ = self.__dll__.sum_elements_from_callback_in_struct self.__sum_elements_from_callback_in_struct__.argtypes = ( ctypes.POINTER(conveyor_belt_data), ) self.__sum_elements_from_callback_in_struct__.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
def vector3d_add_array(self, v): length = len(v) def dict_from_struct(in_struct): return { key: getattr(in_struct.contents, key) for key in ['x', 'y', 'z'] } v_ctypes = (vector3d * length)() for i in range(length): for key in v[i].keys(): setattr(v_ctypes[i], key, v[i][key]) return dict_from_struct( self.__vector3d_add_array__( ctypes.cast(ctypes.pointer(v_ctypes), ctypes.POINTER(vector3d)), length))
def from_array(self, param): if param.typecode != 'd': raise TypeError('must be an array of doubles') ptr, _ = param.buffer_info() return ctypes.cast(ptr, ctypes.POINTER(ctypes.c_double))