def arr_vulkan_layout_to_arr_normal_layout(vk_arr: ndarray_type.ndarray(), normal_arr: ndarray_type.ndarray()): static_assert(len(normal_arr.shape) == 2) w = normal_arr.shape[0] h = normal_arr.shape[1] for i, j in ndrange(w, h): normal_arr[i, j] = vk_arr[(h - 1 - j) * w + i]
def ndarray_matrix_to_ext_arr(ndarray: ndarray_type.ndarray(), arr: ndarray_type.ndarray(), layout_is_aos: template(), as_vector: template()): for I in grouped(ndarray): for p in static(range(ndarray[I].n)): for q in static(range(ndarray[I].m)): if static(as_vector): if static(layout_is_aos): arr[I, p] = ndarray[I][p] else: arr[p, I] = ndarray[I][p] else: if static(layout_is_aos): arr[I, p, q] = ndarray[I][p, q] else: arr[p, q, I] = ndarray[I][p, q]
def ext_arr_to_matrix(arr: ndarray_type.ndarray(), mat: template(), as_vector: template()): for I in grouped(mat): for p in static(range(mat.n)): for q in static(range(mat.m)): if static(as_vector): mat[I][p] = arr[I, p] else: mat[I][p, q] = arr[I, p, q]
def vector_to_fast_image(img: template(), out: ndarray_type.ndarray()): # FIXME: Why is ``for i, j in img:`` slower than: for i, j in ndrange(*img.shape): r, g, b = 0, 0, 0 color = img[i, img.shape[1] - 1 - j] if static(img.dtype in [f16, f32, f64]): r, g, b = min(255, max(0, int(color * 255))) else: static_assert(img.dtype == u8) r, g, b = color idx = j * img.shape[0] + i # We use i32 for |out| since OpenGL and Metal doesn't support u8 types if static(get_os_name() != 'osx'): out[idx] = (r << 16) + (g << 8) + b else: # What's -16777216? # # On Mac, we need to set the alpha channel to 0xff. Since Mac's GUI # is big-endian, the color is stored in ABGR order, and we need to # add 0xff000000, which is -16777216 in I32's legit range. (Albeit # the clarity, adding 0xff000000 doesn't work.) alpha = -16777216 out[idx] = (b << 16) + (g << 8) + r + alpha
def tensor_to_image(tensor: template(), arr: ndarray_type.ndarray()): for I in grouped(tensor): t = ops.cast(tensor[I], f32) arr[I, 0] = t arr[I, 1] = t arr[I, 2] = t
def ndarray_to_ext_arr(ndarray: ndarray_type.ndarray(), arr: ndarray_type.ndarray()): for I in grouped(ndarray): arr[I] = ndarray[I]
def tensor_to_ext_arr(tensor: template(), arr: ndarray_type.ndarray()): for I in grouped(tensor): arr[I] = tensor[I]
def fill_ndarray_matrix(ndarray: ndarray_type.ndarray(), val: template()): for I in grouped(ndarray): ndarray[I].fill(val)
def fill_ndarray(ndarray: ndarray_type.ndarray(), val: template()): for I in grouped(ndarray): ndarray[I] = val
def ext_arr_to_ndarray(arr: ndarray_type.ndarray(), ndarray: ndarray_type.ndarray()): for I in grouped(ndarray): ndarray[I] = arr[I]
def ndarray_to_ndarray(ndarray: ndarray_type.ndarray(), other: ndarray_type.ndarray()): for I in grouped(ndarray): ndarray[I] = other[I]
def ext_arr_to_tensor(arr: ndarray_type.ndarray(), tensor: template()): for I in grouped(tensor): tensor[I] = arr[I]
def vector_to_image(mat: template(), arr: ndarray_type.ndarray()): for I in grouped(mat): for p in static(range(mat.n)): arr[I, p] = ops.cast(mat[I][p], f32) if static(mat.n <= 2): arr[I, 2] = 0