def vert_thread(start, end): "Thread for vertical filtering" try: mlsl.correlate2s(temp, 'f', 0, start, 0, temp_col_stride, temp_row_stride, cols, rows, dst_img_type, dst_byte_offset, dst_col_offset+start, dst_row_offset, dst_col_stride, dst_row_stride, dst_col_total, dst_row_total, end-start, rows, None, 0, kernel_v, kernel_v_offset, min_val, max_val, output=output) finally: done_v.release()
def horiz_thread(start, end): "Thread for horizontal filtering" try: mlsl.correlate2s(src, src_img_type, src_byte_offset, src_col_offset, src_row_offset+start, src_col_stride, src_row_stride, src_col_total, src_row_total, 'f', 0, 0, start, temp_col_stride, temp_row_stride, cols, rows, cols, end-start, kernel_h, kernel_h_offset, None, 0, 0.0, 0.0, output=temp) finally: done_h.release()
def correlate2s(src, src_img_type, src_byte_offset, src_col_offset, src_row_offset, src_col_stride, src_row_stride, src_col_total, src_row_total, dst_img_type, dst_byte_offset, dst_col_offset, dst_row_offset, dst_col_stride, dst_row_stride, dst_col_total, dst_row_total, cols, rows, kernel_h, kernel_h_offset, kernel_v, kernel_v_offset, min_val=0, max_val=0, output=None, threads=2): if threads < 1: raise Error('the argument threads must be positive') if threads < 2: return mlsl.correlate2s(src, src_img_type, src_byte_offset, src_col_offset, src_row_offset, src_col_stride, src_row_stride, src_col_total, src_row_total, dst_img_type, dst_byte_offset, dst_col_offset, dst_row_offset, dst_col_stride, dst_row_stride, dst_col_total, dst_row_total, cols, rows, kernel_h, kernel_h_offset, kernel_v, kernel_v_offset, min_val, max_val, output) if threads > min(cols,rows): threads = min(cols,rows) # create intermediate buffer temp_col_stride = struct.calcsize('f') temp_row_stride = temp_col_stride * (cols|1) temp_size = temp_row_stride*rows temp = array.array('f', '\x00'*temp_size) # create output buffer if there isn't one output_size = (dst_byte_offset + max(dst_col_stride*(dst_col_offset+cols-1),0) + max(dst_row_stride*(dst_row_offset+rows-1),0) + struct.calcsize(dst_img_type)) output_is_array = False if output is None: output = array.array(dst_img_type, '\x00'*output_size) output_is_array = True done_h = threading.Semaphore(0) done_v = threading.Semaphore(0) def horiz_thread(start, end): "Thread for horizontal filtering" try: mlsl.correlate2s(src, src_img_type, src_byte_offset, src_col_offset, src_row_offset+start, src_col_stride, src_row_stride, src_col_total, src_row_total, 'f', 0, 0, start, temp_col_stride, temp_row_stride, cols, rows, cols, end-start, kernel_h, kernel_h_offset, None, 0, 0.0, 0.0, output=temp) finally: done_h.release() def vert_thread(start, end): "Thread for vertical filtering" try: mlsl.correlate2s(temp, 'f', 0, start, 0, temp_col_stride, temp_row_stride, cols, rows, dst_img_type, dst_byte_offset, dst_col_offset+start, dst_row_offset, dst_col_stride, dst_row_stride, dst_col_total, dst_row_total, end-start, rows, None, 0, kernel_v, kernel_v_offset, min_val, max_val, output=output) finally: done_v.release() # horizontal filtering for j in range(threads): start = int(round(1.0*j*rows/threads)) end = int(round(1.0*(j+1)*rows/threads)) thread.start_new_thread(horiz_thread, (start,end)) for j in range(threads): done_h.acquire() # vertical filtering for j in range(threads): start = int(round(1.0*j*cols/threads)) end = int(round(1.0*(j+1)*cols/threads)) thread.start_new_thread(vert_thread, (start,end)) for j in range(threads): done_v.acquire() # return the result if output_is_array: return output.tostring() else: return output