def thread_func(start, end): try: mlsl.grayify(img, img_type, src_col_offset, src_row_offset + start, src_col_stride, src_row_stride, src_chan_stride, dst_col_offset, dst_row_offset + start, dst_col_stride, dst_row_stride, cols, end-start, chans, weights, gamma, output) finally: done.release()
def grayify(img, img_type, src_col_offset, src_row_offset, src_col_stride, src_row_stride, src_chan_stride, dst_col_offset, dst_row_offset, dst_col_stride, dst_row_stride, cols, rows, chans, weights, gamma, output=None, threads=2): if threads < 1: raise Error('the argument threads must be positive') if threads < 2: return mlsl.grayify(img, img_type, src_col_offset, src_row_offset, src_col_stride, src_row_stride, src_chan_stride, dst_col_offset, dst_row_offset, dst_col_stride, dst_row_stride, cols, rows, chans, weights, gamma, output) if threads > rows: threads = rows # create output buffer output_is_array = False if output is None: output_size = (struct.calcsize('f') + dst_col_stride*(dst_col_offset+cols-1) + dst_row_stride*(dst_row_offset+rows-1)) output = array.array('f', output_size*'\x00') output_is_array = True done = threading.Semaphore(0) def thread_func(start, end): try: mlsl.grayify(img, img_type, src_col_offset, src_row_offset + start, src_col_stride, src_row_stride, src_chan_stride, dst_col_offset, dst_row_offset + start, dst_col_stride, dst_row_stride, cols, end-start, chans, weights, gamma, output) finally: done.release() starts = [int(round(1.0*x*rows/threads)) for x in range(threads)] starts.append(cols) for j in range(len(starts)-1): start = starts[j] end = starts[j+1] thread.start_new_thread(thread_func, (start,end)) for j in range(len(starts)-1): done.acquire() if output_is_array: output = output.tostring() return output