Example #1
0
def write_bitmap(filename, data, resolution, write_async=True):
    """
    Write the linearized RGB image in `data` to a PNG/EXR/.. file with
    resolution `resolution`.
    """
    import numpy as np
    from mitsuba.core import Bitmap, Struct

    if type(data).__name__ == 'Tensor':
        data = data.detach().cpu()

    data = np.array(data.numpy())
    data = data.reshape(*resolution, -1)
    bitmap = Bitmap(data)
    if filename.endswith('.png') or \
       filename.endswith('.jpg') or \
       filename.endswith('.jpeg'):
        bitmap = bitmap.convert(Bitmap.PixelFormat.RGB,
                                Struct.Type.UInt8, True)
    quality = 0 if filename.endswith('png') else -1

    if write_async:
        bitmap.write_async(filename, quality=quality)
    else:
        bitmap.write(filename, quality=quality)
Example #2
0
def test_read_write_ppm(tmpdir):
    np.random.seed(12345)

    b = Bitmap(Bitmap.PixelFormat.RGB, Struct.Type.UInt8, [10, 20])
    ref = np.uint8(np.random.random((20, 10, 3)) * 255)
    np.array(b, copy=False)[:] = ref[...]
    tmp_file = os.path.join(str(tmpdir), "out.ppm")
    b.write(tmp_file)
    b2 = Bitmap(tmp_file)
    assert np.abs(np.mean(np.array(b2) - ref)) == 0
    os.remove(tmp_file)
Example #3
0
def test_read_write_hdr(tmpdir):
    np.random.seed(12345)

    b = Bitmap(Bitmap.PixelFormat.RGB, Struct.Type.Float32, [10, 20])
    ref = np.float32(np.random.random((20, 10, 3)))
    np.array(b, copy=False)[:] = ref[...]
    tmp_file = os.path.join(str(tmpdir), "out.hdr")
    b.write(tmp_file)
    b2 = Bitmap(tmp_file)
    assert np.abs(np.mean(np.array(b2) - ref)) < 1e-2
    os.remove(tmp_file)
Example #4
0
def test_read_write_png(tmpdir):
    np.random.seed(12345)
    tmp_file = os.path.join(str(tmpdir), "out.png")

    b = Bitmap(Bitmap.PixelFormat.Y, Struct.Type.UInt8, [10, 10])
    ref = np.uint8(np.random.random((10, 10)) * 255)
    np.array(b, copy=False)[:] = ref[..., np.newaxis]
    b.write(tmp_file)
    b2 = Bitmap(tmp_file)
    assert np.sum(np.abs(np.float32(np.array(b2)[:, :, 0]) - ref)) == 0

    b = Bitmap(Bitmap.PixelFormat.RGBA, Struct.Type.UInt8, [10, 10])
    ref = np.uint8(np.random.random((10, 10, 4)) * 255)
    np.array(b, copy=False)[:] = ref
    b.write(tmp_file)
    b2 = Bitmap(tmp_file)
    assert np.sum(np.abs(np.float32(np.array(b2)) - ref)) == 0

    os.remove(tmp_file)
Example #5
0
def test_read_convert_yc(tmpdir):
    # Tests reading & upsampling a luminance/chroma image
    b = Bitmap(find_resource('resources/data/tests/bitmap/XYZ_YC.exr'))
    # Tests float16 XYZ -> float32 RGBA conversion
    b = b.convert(Bitmap.PixelFormat.RGBA, Struct.Type.Float32, False)
    ref = [0.36595437, 0.27774358, 0.11499051, 1.]
    # Tests automatic Bitmap->NumPy conversion
    assert np.allclose(np.mean(b, axis=(0, 1)), ref, atol=1e-3)
    tmp_file = os.path.join(str(tmpdir), "out.exr")
    # Tests bitmap resampling filters
    rfilter = mitsuba.core.xml.load_string(
        "<rfilter version='2.0.0' type='box'/>")
    b = b.resample(
        [1, 1], rfilter,
        (FilterBoundaryCondition.Zero, FilterBoundaryCondition.Zero))
    # Tests OpenEXR bitmap writing
    b.write(tmp_file)
    b = Bitmap(tmp_file)
    os.remove(tmp_file)
    assert np.allclose(np.mean(b, axis=(0, 1)), ref, atol=1e-3)
Example #6
0
def test_read_write_jpeg(tmpdir):
    np.random.seed(12345)
    tmp_file = os.path.join(str(tmpdir), "out.jpg")

    b = Bitmap(Bitmap.PixelFormat.Y, Struct.Type.UInt8, [10, 10])
    ref = np.uint8(np.random.random((10, 10)) * 255)
    np.array(b, copy=False)[:] = ref[..., np.newaxis]
    b.write(tmp_file, quality=50)
    b2 = Bitmap(tmp_file)
    assert np.sum(np.abs(np.float32(np.array(b2)[:, :, 0]) -
                         ref)) / (10 * 10 * 255) < 0.07

    b = Bitmap(Bitmap.PixelFormat.RGB, Struct.Type.UInt8, [10, 10])
    ref = np.uint8(np.random.random((10, 10, 3)) * 255)
    np.array(b, copy=False)[:] = ref
    b.write(tmp_file, quality=100)
    b2 = Bitmap(tmp_file)
    assert np.sum(
        np.abs(np.float32(np.array(b2)) - ref)) / (3 * 10 * 10 * 255) < 0.2

    os.remove(tmp_file)
Example #7
0
def imaging(blocks, film_size, aovs=False, invalid_sample=False):
    """Imaging result with aovs"""

    label = ['result']
    if aovs:
        label.append('scatter')
        label.append('non_scatter')

    if invalid_sample:
        label.append('invalid')

    for i in label:
        xyzaw_np = np.array(blocks[i].data()).reshape(
            [film_size[1], film_size[0], 5])

        bmp = Bitmap(xyzaw_np, Bitmap.PixelFormat.XYZAW)
        bmp = bmp.convert(Bitmap.PixelFormat.RGB,
                          Struct.Type.Float32,
                          srgb_gamma=False)
        bmp.write(i + '.exr')
        bmp.convert(Bitmap.PixelFormat.RGB, Struct.Type.UInt8,
                    srgb_gamma=True).write(i + '.jpg')
Example #8
0
def test_read_write_complex_exr(tmpdir):
    # Tests reading and writing of complex multi-channel images with custom properties
    b1 = Bitmap(Bitmap.PixelFormat.MultiChannel, Struct.Type.Float32, [4, 5],
                6)
    a = b1.struct_()
    for i in range(6):
        a[i].name = "my_ch_%i" % i
    b2 = np.array(b1, copy=False)
    meta = b1.metadata()
    meta["str_prop"] = "value"
    meta["int_prop"] = 15
    meta["dbl_prop"] = 30.0
    meta["vec3_prop"] = [1.0, 2.0, 3.0]
    # meta["mat_prop"] = np.arange(16, dtype=float_dtype).reshape((4, 4)) + np.eye(4, dtype=float_dtype) # TODO py::implicitly_convertible<py::array, Transform4f>() doesn't seem to work in transform_v.cpp

    assert b2.shape == (5, 4, 6)
    assert b2.dtype == np.float32
    b2[:] = np.arange(4 * 5 * 6).reshape((5, 4, 6))
    tmp_file = os.path.join(str(tmpdir), "out.exr")
    b1.write(tmp_file)

    b3 = Bitmap(tmp_file)
    os.remove(tmp_file)
    meta = b3.metadata()
    meta.remove_property("generatedBy")
    meta.remove_property("pixelAspectRatio")
    meta.remove_property("screenWindowWidth")
    assert b3 == b1
    b2[0, 0, 0] = 3
    assert b3 != b1
    b2[0, 0, 0] = 0
    assert b3 == b1
    assert str(b3) == str(b1)
    meta["str_prop"] = "value2"
    assert b3 != b1
    assert str(b3) != str(b1)
Example #9
0
                                               sample3=0)

# Intersect rays with the scene geometry
surface_interaction = scene.ray_intersect(rays)

# Given intersection, compute the final pixel values as the depth t
# of the sampled surface interaction
result = surface_interaction.t

# Set to zero if no intersection was found
result[~surface_interaction.is_valid()] = 0

block = ImageBlock(film.crop_size(),
                   channel_count=5,
                   filter=film.reconstruction_filter(),
                   border=False)
block.clear()
# ImageBlock expects RGB values (Array of size (n, 3))
block.put(position_sample, rays.wavelengths, Vector3f(result, result, result),
          1)

# Write out the result from the ImageBlock
# Internally, ImageBlock stores values in XYZAW format
# (color XYZ, alpha value A and weight W)
xyzaw_np = block.data().reshape([film_size[1], film_size[0], 5])

# We then create a Bitmap from these values and save it out as EXR file
bmp = Bitmap(xyzaw_np, Bitmap.PixelFormat.XYZAW)
bmp = bmp.convert(Bitmap.PixelFormat.Y, Struct.Type.Float32, srgb_gamma=False)
bmp.write('depth.exr')
dragon_c = scene.shapes()[6].bbox().center()
c = Vector3f(dragon_c)
d = c - surface_interaction.p
result = ek.norm(d)
max_dist = ek.hmax(result)
result /= max_dist
result = 0.3 * (1.0 - result**(2.0))

result[~surface_interaction.is_valid()] = 0

block = ImageBlock(film.crop_size(),
                   channel_count=5,
                   filter=film.reconstruction_filter(),
                   border=False)
block.clear()
# ImageBlock expects RGB values (Array of size (n, 3))
block.put(pos, rays.wavelengths, Vector3f(result, result, result), 1)

# Write out the result from the ImageBlock
# Internally, ImageBlock stores values in XYZAW format
# (color XYZ, alpha value A and weight W)

xyzaw_np = np.array(block.data()).reshape([film_size[1], film_size[0], 5])

# We then create a Bitmap from these values and save it out as EXR file
bmp = Bitmap(xyzaw_np, Bitmap.PixelFormat.XYZAW)
bmp = bmp.convert(Bitmap.PixelFormat.RGBA,
                  Struct.Type.Float32,
                  srgb_gamma=False)
bmp.write('distance.exr')