Example #1
0
def test_image_io_vector(resx, resy, comp, ext, dt):
    shape = (resx, resy)
    pixel = np.random.rand(*shape, comp).astype(ti.to_numpy_type(dt))
    pixel_t = ti.Vector.field(comp, dt, shape)
    pixel_t.from_numpy(pixel)
    fn = make_temp_file(suffix='.' + ext)
    ti.imwrite(pixel_t, fn)
    pixel_r = (ti.imread(fn).astype(ti.to_numpy_type(dt)) + 0.5) / 256.0
    assert np.allclose(pixel_r, pixel, atol=2e-2)
    os.remove(fn)
Example #2
0
def test_image_io_uint(resx, resy, comp, ext, dt):
    shape = (resx, resy)
    np_type = ti.to_numpy_type(dt)
    # When saving to disk, pixel data will be truncated into 8 bits.
    # Be careful here if you want lossless saving.
    np_max = np.iinfo(np_type).max // 256
    pixel = np.random.randint(256, size=(*shape, comp), dtype=np_type) * np_max
    pixel_t = ti.Vector.field(comp, dt, shape)
    pixel_t.from_numpy(pixel)
    fn = make_temp_file(suffix='.' + ext)
    ti.imwrite(pixel_t, fn)
    pixel_r = ti.imread(fn).astype(np_type) * np_max
    assert (pixel_r == pixel).all()
    os.remove(fn)
Example #3
0
def test_without_init():
    # We want to check if Taichi works well without ``ti.init()``.
    # But in test ``ti.init()`` will always be called in last ``@ti.all_archs``.
    # So we have to create a new Taichi instance, i.e. test in a sandbox.
    content = '''
import taichi as ti
assert ti.cfg.arch == ti.cpu

x = ti.var(ti.i32, (2, 3))
assert x.shape == (2, 3)

x[1, 2] = 4
assert x[1, 2] == 4
'''
    filename = make_temp_file()
    with open(filename, 'w') as f:
        f.write(content)
    assert os.system(f'{sys.executable} {filename}') == 0
Example #4
0
def test_image_io(resx, resy, comp, ext, is_field, dt):
    if comp != 1:
        shape = (resx, resy, comp)
    else:
        shape = (resx, resy)
    if is_field:
        pixel_t = ti.field(dt, shape)
    pixel = np.random.randint(256, size=shape, dtype=ti.to_numpy_type(dt))
    if is_field:
        pixel_t.from_numpy(pixel)
    fn = make_temp_file(suffix='.' + ext)
    if is_field:
        ti.imwrite(pixel_t, fn)
    else:
        ti.imwrite(pixel, fn)
    pixel_r = ti.imread(fn)
    if comp == 1:
        # from (resx, resy, 1) to (resx, resy)
        pixel_r = pixel_r.reshape((resx, resy))
    assert (pixel_r == pixel).all()
    os.remove(fn)
Example #5
0
def test_save_image_without_window(dtype):
    n = 255
    pixels = ti.field(dtype=dtype, shape=(n, n, 3))

    @ti.kernel
    def paint(c: dtype):
        for i, j, k in pixels:
            pixels[i, j, k] = c

    gui = ti.GUI("Test", res=(n, n), show_gui=False)
    for i in [0, 32, 64, 128, 255]:
        if dtype is ti.u8:
            paint(i)
        else:
            paint(i * 1.0 / n)
        gui.set_image(pixels)
        image_path = make_temp_file(suffix='.png')
        gui.show(image_path)
        image = ti.imread(image_path)
        delta = (image - i).sum()
        assert delta == 0, "Expected image difference to be 0 but got {} instead.".format(
            delta)