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 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 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[:]
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 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 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 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 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 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))
def fibonacci_sequence(self, length): out_array_obj = self.__fibonacci_sequence__(length) return ctypes.cast(out_array_obj.contents.data, ctypes.POINTER(ctypes.c_int16 * length)).contents[:]