Example #1
0
def test_convert_rgb_y_gamma(tmpdir):
    def to_srgb(value):
        if value <= 0.0031308:
            return 12.92 * value
        return 1.055 * (value**(1.0 / 2.4)) - 0.055

    # Tests RGBA(float64) -> Y (uint8_t, linear) conversion
    b1 = Bitmap(Bitmap.PixelFormat.RGBA, Struct.Type.Float64, [3, 1])
    b2 = np.array(b1, copy=False)
    b2[:] = [[[1, 0, 0, 1], [0, 1, 0, 0.5], [0, 0, 1, 0]]]
    b3 = np.array(b1.convert(Bitmap.PixelFormat.Y, Struct.Type.UInt8,
                             False)).ravel()
    assert np.allclose(b3, [0.212671 * 255, 0.715160 * 255, 0.072169 * 255],
                       atol=1)

    # Tests RGBA(float64) -> Y (uint8_t, gamma) conversion
    b1 = Bitmap(Bitmap.PixelFormat.RGBA, Struct.Type.Float64, [3, 1])
    b2 = np.array(b1, copy=False)
    b2[:] = [[[1, 0, 0, 1], [0, 1, 0, 0.5], [0, 0, 1, 0]]]
    b3 = np.array(b1.convert(Bitmap.PixelFormat.Y, Struct.Type.UInt8,
                             True)).ravel()
    assert np.allclose(b3, [
        to_srgb(0.212671) * 255,
        to_srgb(0.715160) * 255,
        to_srgb(0.072169) * 255
    ],
                       atol=1)
Example #2
0
def render(scene, write_to):
    from mitsuba.core import Bitmap, Struct

    success = scene.integrator().render(scene, scene.sensors()[0])
    assert success

    film = scene.sensors()[0].film()
    bitmap = film.bitmap(raw=False)

    if not bitmap.pixel_format() == Bitmap.PixelFormat.MultiChannel:
        bitmap.convert(Bitmap.PixelFormat.RGB, Struct.Type.UInt8,
                       True).write(write_to)
    elif bitmap.channel_count() == 16:
        # Stokes output, rather specialized for 'integrator_stokes_cbox' scene atm.
        data_np = np.array(bitmap, copy=False).astype(np.float)
        s0 = data_np[:, :, 4]
        z = np.zeros(s0.shape)
        s1 = np.dstack([
            np.maximum(0, -data_np[:, :, 7]),
            np.maximum(0, data_np[:, :, 7]), z
        ])
        s2 = np.dstack([
            np.maximum(0, -data_np[:, :, 10]),
            np.maximum(0, data_np[:, :, 10]), z
        ])
        s3 = np.dstack([
            np.maximum(0, -data_np[:, :, 13]),
            np.maximum(0, data_np[:, :, 13]), z
        ])
        Bitmap(s0).convert(Bitmap.PixelFormat.RGB, Struct.Type.UInt8,
                           True).write(write_to)
        Bitmap(s1).convert(Bitmap.PixelFormat.RGB, Struct.Type.UInt8,
                           True).write(write_to.replace('.jpg', '_s1.jpg'))
        Bitmap(s3).convert(Bitmap.PixelFormat.RGB, Struct.Type.UInt8,
                           True).write(write_to.replace('.jpg', '_s3.jpg'))
        Bitmap(s2).convert(Bitmap.PixelFormat.RGB, Struct.Type.UInt8,
                           True).write(write_to.replace('.jpg', '_s2.jpg'))
    else:
        for name, b in bitmap.split():
            if name == '<root>':
                continue

            # normalize depth map
            if name == 'depth.y':
                data_np = np.array(b, copy=False)
                min_val = np.min(data_np)
                max_val = np.max(data_np)
                data_np = (data_np - min_val) / (max_val - min_val)
                b = Bitmap(data_np, Bitmap.PixelFormat.Y)

            pixel_format = b.pixel_format()
            if not pixel_format == Bitmap.PixelFormat.XYZ:
                pixel_format = Bitmap.PixelFormat.RGB

            f_name = write_to if name == 'image' else write_to.replace(
                '.jpg', '_%s.jpg' % name)
            b.convert(pixel_format, Struct.Type.UInt8, True).write(f_name)
Example #3
0
def test15_test_dither():
    from mitsuba.core import Bitmap
    import numpy.linalg as la

    b = Bitmap(Bitmap.PixelFormat.Y, Struct.Type.Float32, [10, 256])
    value = np.linspace(0, 1 / 255.0, 10)
    np.array(b, copy=False)[:, :, 0] = np.tile(value, (256, 1))
    b = b.convert(Bitmap.PixelFormat.Y, Struct.Type.UInt8, False)
    b = b.convert(Bitmap.PixelFormat.Y, Struct.Type.Float32, False)
    err = la.norm(np.mean(np.array(b, copy=False), axis=(0, 2)) - value)
    assert(err < 5e-4)
Example #4
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 #5
0
def test_convert_rgb_y(tmpdir):
    # Tests RGBA(float64) -> Y (float32) conversion
    b1 = Bitmap(Bitmap.PixelFormat.RGBA, Struct.Type.Float64, [3, 1])
    b2 = np.array(b1, copy=False)
    b2[:] = [[[1, 0, 0, 1], [0, 1, 0, 0.5], [0, 0, 1, 0]]]
    b3 = np.array(b1.convert(Bitmap.PixelFormat.Y, Struct.Type.Float32,
                             False)).ravel()
    assert np.allclose(b3, [0.212671, 0.715160, 0.072169])
Example #6
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 #7
0
def test_premultiply_alpha(tmpdir):
    # Tests RGBA(float64) -> Y (float32) conversion
    b1 = Bitmap(Bitmap.PixelFormat.RGBA, Struct.Type.Float64, [3, 1])
    assert b1.premultiplied_alpha()
    b1.set_premultiplied_alpha(False)
    assert not b1.premultiplied_alpha()

    b2 = np.array(b1, copy=False)
    b2[:] = [[[1, 0, 0, 1], [0, 1, 0, 0.5], [0, 0, 1, 0]]]

    # Premultiply
    b3 = np.array(
        b1.convert(Bitmap.PixelFormat.RGBA, Struct.Type.Float32, False,
                   Bitmap.AlphaTransform.Premultiply)).ravel()
    assert np.allclose(
        b3, [1.0, 0.0, 0.0, 1.0, 0.0, 0.5, 0.0, 0.5, 0.0, 0.0, 0.0, 0.0])

    # Unpremultiply
    b1.set_premultiplied_alpha(True)
    b3 = np.array(
        b1.convert(Bitmap.PixelFormat.RGBA, Struct.Type.Float32, False,
                   Bitmap.AlphaTransform.Unpremultiply)).ravel()
    assert np.allclose(
        b3, [1.0, 0.0, 0.0, 1.0, 0.0, 2, 0.0, 0.5, 0.0, 0.0, 0.0, 0.0])
Example #8
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 #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')
Example #10
0
def xyz_to_rgb_bmp(arr):
    from mitsuba.core import Bitmap, Struct
    xyz_bmp = Bitmap(arr, Bitmap.PixelFormat.XYZ)
    return xyz_bmp.convert(Bitmap.PixelFormat.RGB, Struct.Type.Float32, False)
Example #11
0
    'measured_subtraction/goldpaper/goldpaper0',
    'measured_subtraction/goldpaper/goldpaper0_subtracting_dE'
]

tonemap = []
for i in range(2):
    tonemap.append(add_cardboard_names[i])
    tonemap.append(add_goldpaper_names[i])
    tonemap.append(sub_cardboard_names[i])
    tonemap.append(sub_goldpaper_names[i])

for img in tonemap:
    path = img + ".exr"
    bitmap = Bitmap(path)
    bitmap.convert(Bitmap.PixelFormat.RGB, Struct.Type.UInt8,
                   srgb_gamma=True).write(
                       os.path.join("", path.replace('.exr', '.jpg')))


def create_dE(names):
    os.system('vips im_dE_fromdisp %s.jpg %s.jpg %s.png screen' %
              (names[0], names[1], names[2]))


create_dE(add_cardboard_names)
create_dE(add_goldpaper_names)
create_dE(sub_cardboard_names)
create_dE(sub_goldpaper_names)


def create_mapped_dE(names, maximum):