def thread_func(start,end): try: mlsl.correlate2(in_data, in_format, lf_byte_offset, 0, start, x_stride, y_stride, xlen, ylen, dst_img_type, dst_byte_offset, 0, start, dst_col_stride, dst_row_stride, xlen, ylen, xlen, end-start, k, k_h_offset, k_width, k_v_offset, output=output) finally: completed.release()
def thread_func(start, end): try: mlsl.correlate2(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, dst_img_type, dst_byte_offset, dst_col_offset, dst_row_offset+start, dst_col_stride, dst_row_stride, dst_col_total, dst_row_total, cols, end-start, kernel, kernel_h_offset, kernel_width, kernel_v_offset, min_val, max_val, output) finally: done_sema.release()
def work_thread(jobs): try: for (src_byte_offset, dst_byte_offset, kernel) in jobs: k, k_h_offset, k_width, k_v_offset = kernel mlsl.correlate2(in_data, in_format, src_byte_offset, 0, 0, x_stride, y_stride, xlen, ylen, out_format, dst_byte_offset, 0, 0, out_x_stride, out_y_stride, xlen, ylen, xlen, ylen, k, k_h_offset, k_width, k_v_offset, output=out_data) finally: done.release()
def lf_blur_subaperture(in_data, in_align, in_format, in_layout, clen, xlen, ylen, ulen, vlen, u, v, k, k_h_offset, k_width, k_v_offset, dst_img_type, dst_byte_offset, dst_col_stride, dst_row_stride, output=None, threads=2): """ Return a subaperture image (u,v) from given light field correlated with the given kernel """ # calculate light field strides (lf_byte_offset, c_stride, x_stride, y_stride, u_stride, v_stride, full_size) = mlsl.strides(in_format, in_layout, in_align, clen, xlen, ylen, ulen, vlen) if u < 0 or u >= ulen or v < 0 or v >= vlen: raise Error('u or v out of bounds') lf_byte_offset += u_stride * u + v_stride * v if threads < 1: raise Error('the argument threads must be positive') if threads < 2: return mlsl.correlate2(in_data, in_format, lf_byte_offset, 0, 0, x_stride, y_stride, xlen, ylen, dst_img_type, dst_byte_offset, 0, 0, dst_col_stride, dst_row_stride, xlen, ylen, xlen, ylen, k, k_h_offset, k_width, k_v_offset, output=output) if threads > ylen: threads = ylen # create a shared output buffer if needed if output is None: max_col_reach = max(0, (dst_col_stride-1)*xlen) max_row_reach = max(0, (dst_row_stride-1)*ylen) max_reach = dst_byte_offset + max_col_reach + max_row_reach + struct.calcsize(dst_img_type) output = array.array(dst_img_type, '\x00'*max_reach) is_array = True else: is_array = False # create semaphores for task completion completed = threading.Semaphore(0) def thread_func(start,end): try: mlsl.correlate2(in_data, in_format, lf_byte_offset, 0, start, x_stride, y_stride, xlen, ylen, dst_img_type, dst_byte_offset, 0, start, dst_col_stride, dst_row_stride, xlen, ylen, xlen, end-start, k, k_h_offset, k_width, k_v_offset, output=output) finally: completed.release() # start tasks for i in range(threads): start = int(round(1.0*i/threads*ylen)) end = int(round(1.0*(i+1)/threads*ylen)) thread.start_new_thread(thread_func, (start,end)) # wait for task completion for i in range(threads): completed.acquire() if is_array: return output.tostring() else: return output
def correlate2(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, kernel_h_offset, kernel_width, 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.correlate2(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, kernel_h_offset, kernel_width, kernel_v_offset, min_val, max_val, output=output) if threads > rows: threads = rows # 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_sema = threading.Semaphore(0) def thread_func(start, end): try: mlsl.correlate2(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, dst_img_type, dst_byte_offset, dst_col_offset, dst_row_offset+start, dst_col_stride, dst_row_stride, dst_col_total, dst_row_total, cols, end-start, kernel, kernel_h_offset, kernel_width, kernel_v_offset, min_val, max_val, output) finally: done_sema.release() for i in range(threads): start = int(round(1.0*i/threads*rows)) end = int(round(1.0*(i+1)/threads*rows)) thread.start_new_thread(thread_func, (start,end)) for i in range(threads): done_sema.acquire() if output_is_array: return output.tostring() else: return output