# Path: third_party/xiuminglib/xiuminglib/imprt.py
# def preset_import(name, assert_success=False):
#     """A unified importer for both regular and ``google3`` modules, according
#     to specified presets/profiles (e.g., ignoring ``ModuleNotFoundError``).
#     """
#     if name in ('cv2', 'opencv'):
#         try:
#             # BUILD dep:
#             # "//third_party/py/cvx2",
#             from cvx2 import latest as mod
#             # Or
#             # BUILD dep:
#             # "//third_party/OpenCVX:cvx2",
#             # from google3.third_party.OpenCVX import cvx2 as cv2
#         except ModuleNotFoundError:
#             mod = import_module_404ok('cv2')
# 
#     elif name in ('tf', 'tensorflow'):
#         mod = import_module_404ok('tensorflow')
# 
#     elif name == 'gfile':
#         # BUILD deps:
#         # "//pyglib:gfile",
#         # "//file/colossus/cns",
#         mod = import_module_404ok('google3.pyglib.gfile')
# 
#     elif name == 'video_api':
#         # BUILD deps:
#         # "//learning/deepmind/video/python:video_api",
#         mod = import_module_404ok(
#             'google3.learning.deepmind.video.python.video_api')
# 
#     elif name in ('bpy', 'bmesh', 'OpenEXR', 'Imath'):
#         # BUILD deps:
#         # "//third_party/py/Imath",
#         # "//third_party/py/OpenEXR",
#         mod = import_module_404ok(name)
# 
#     elif name in ('Vector', 'Matrix', 'Quaternion'):
#         mod = import_module_404ok('mathutils')
#         mod = _get_module_class(mod, name)
# 
#     elif name == 'BVHTree':
#         mod = import_module_404ok('mathutils.bvhtree')
#         mod = _get_module_class(mod, name)
# 
#     else:
#         raise NotImplementedError(name)
# 
#     if assert_success:
#         assert mod is not None, "Failed in importing '%s'" % name
# 
#     return mod
# 
# Path: third_party/xiuminglib/xiuminglib/os.py
# def makedirs(directory, rm_if_exists=False):
#     """Wraps :func:`os.makedirs` to support removing the directory if it
#     alread exists.
# 
#     Google Colossus-compatible: it tries to use ``gfile`` first for speed. This
#     will fail if Blaze is not used, in which case it then falls back to using
#     ``fileutil`` CLI as external process calls.
# 
#     Args:
#         directory (str)
#         rm_if_exists (bool, optional): Whether to remove the directory (and
#             its contents) if it already exists.
#     """
#     def exists_cns_cli(directory):
#         cmd = 'fileutil test -d %s' % directory
#         retcode, _, _ = call(cmd, quiet=True)
#         if retcode == 0:
#             return True
#         if retcode == 1:
#             return False
#         raise ValueError(retcode)
# 
#     def mkdir_cns_cli(directory):
#         cmd = 'fileutil mkdir -p %s' % directory
#         _call_assert_success(cmd, quiet=True)
# 
#     if _is_cnspath(directory):
#         # Is a CNS path
#         gfile = preset_import('gfile')
#         if gfile is None:
#             exists_func = exists_cns_cli
#             mkdir_func = mkdir_cns_cli
#         else:
#             exists_func = gfile.Exists
#             mkdir_func = gfile.MakeDirs
#     else:
#         # Is just a regular local path
#         exists_func = exists
#         mkdir_func = os.makedirs
# 
#     # Do the job
#     if exists_func(directory):
#         if rm_if_exists:
#             rm(directory)
#             mkdir_func(directory)
#             logger.info("Removed and then remade:\n\t%s", directory)
#     else:
#         mkdir_func(directory)
# 
# def open_file(path, mode):
#     """Opens a file.
# 
#     Supports Google Colossus if ``gfile`` can be imported.
# 
#     Args:
#         path (str): Path to open.
#         mode (str): ``'r'``, ``'rb'``, ``'w'``, or ``'wb'``.
# 
#     Returns:
#         File handle that can be used as a context.
#     """
#     gfile = preset_import('gfile')
# 
#     open_func = open if gfile is None else gfile.Open
#     handle = open_func(path, mode)
# 
#     return handle
# 
# Path: third_party/xiuminglib/xiuminglib/log.py
# def get_logger(level=None):
#     """Creates a logger for functions in the library.
# 
#     Args:
#         level (str, optional): Logging level. Defaults to ``logging.INFO``.
# 
#     Returns:
#         logging.Logger: Logger created.
#     """
#     if level is None:
#         level = logging.INFO
#     logging.basicConfig(level=level)
#     logger = logging.getLogger()
#     return logger


from os.path import dirname
from ..imprt import preset_import
from ..os import makedirs, open_file
from ..log import get_logger
import numpy as np
    """Reads an HDR map from disk.

    Args:
        path (str): Path to the .hdr file.

    Returns:
        numpy.ndarray: Loaded (float) HDR map with RGB channels in order.
    """
    cv2 = preset_import('cv2', assert_success=True)

    with open_file(path, 'rb') as h:
        buffer_ = np.fromstring(h.read(), np.uint8)
    bgr = cv2.imdecode(buffer_, cv2.IMREAD_UNCHANGED)
    rgb = cv2.cvtColor(bgr, cv2.COLOR_BGR2RGB)

    logger.debug("HDR map read from:\n\t%s", path)

    return rgb


def write(rgb, outpath):
    r"""Writes a ``float32`` array as an HDR map to disk.

    Args:
        rgb (numpy.ndarray): ``float32`` RGB array.
        outpath (str): Output path.

    Writes
        - The resultant HDR map.
    """

# Path: third_party/xiuminglib/xiuminglib/imprt.py
# def preset_import(name, assert_success=False):
#     """A unified importer for both regular and ``google3`` modules, according
#     to specified presets/profiles (e.g., ignoring ``ModuleNotFoundError``).
#     """
#     if name in ('cv2', 'opencv'):
#         try:
#             # BUILD dep:
#             # "//third_party/py/cvx2",
#             from cvx2 import latest as mod
#             # Or
#             # BUILD dep:
#             # "//third_party/OpenCVX:cvx2",
#             # from google3.third_party.OpenCVX import cvx2 as cv2
#         except ModuleNotFoundError:
#             mod = import_module_404ok('cv2')
# 
#     elif name in ('tf', 'tensorflow'):
#         mod = import_module_404ok('tensorflow')
# 
#     elif name == 'gfile':
#         # BUILD deps:
#         # "//pyglib:gfile",
#         # "//file/colossus/cns",
#         mod = import_module_404ok('google3.pyglib.gfile')
# 
#     elif name == 'video_api':
#         # BUILD deps:
#         # "//learning/deepmind/video/python:video_api",
#         mod = import_module_404ok(
#             'google3.learning.deepmind.video.python.video_api')
# 
#     elif name in ('bpy', 'bmesh', 'OpenEXR', 'Imath'):
#         # BUILD deps:
#         # "//third_party/py/Imath",
#         # "//third_party/py/OpenEXR",
#         mod = import_module_404ok(name)
# 
#     elif name in ('Vector', 'Matrix', 'Quaternion'):
#         mod = import_module_404ok('mathutils')
#         mod = _get_module_class(mod, name)
# 
#     elif name == 'BVHTree':
#         mod = import_module_404ok('mathutils.bvhtree')
#         mod = _get_module_class(mod, name)
# 
#     else:
#         raise NotImplementedError(name)
# 
#     if assert_success:
#         assert mod is not None, "Failed in importing '%s'" % name
# 
#     return mod
# 
# Path: third_party/xiuminglib/xiuminglib/os.py
# def makedirs(directory, rm_if_exists=False):
#     """Wraps :func:`os.makedirs` to support removing the directory if it
#     alread exists.
# 
#     Google Colossus-compatible: it tries to use ``gfile`` first for speed. This
#     will fail if Blaze is not used, in which case it then falls back to using
#     ``fileutil`` CLI as external process calls.
# 
#     Args:
#         directory (str)
#         rm_if_exists (bool, optional): Whether to remove the directory (and
#             its contents) if it already exists.
#     """
#     def exists_cns_cli(directory):
#         cmd = 'fileutil test -d %s' % directory
#         retcode, _, _ = call(cmd, quiet=True)
#         if retcode == 0:
#             return True
#         if retcode == 1:
#             return False
#         raise ValueError(retcode)
# 
#     def mkdir_cns_cli(directory):
#         cmd = 'fileutil mkdir -p %s' % directory
#         _call_assert_success(cmd, quiet=True)
# 
#     if _is_cnspath(directory):
#         # Is a CNS path
#         gfile = preset_import('gfile')
#         if gfile is None:
#             exists_func = exists_cns_cli
#             mkdir_func = mkdir_cns_cli
#         else:
#             exists_func = gfile.Exists
#             mkdir_func = gfile.MakeDirs
#     else:
#         # Is just a regular local path
#         exists_func = exists
#         mkdir_func = os.makedirs
# 
#     # Do the job
#     if exists_func(directory):
#         if rm_if_exists:
#             rm(directory)
#             mkdir_func(directory)
#             logger.info("Removed and then remade:\n\t%s", directory)
#     else:
#         mkdir_func(directory)
# 
# def open_file(path, mode):
#     """Opens a file.
# 
#     Supports Google Colossus if ``gfile`` can be imported.
# 
#     Args:
#         path (str): Path to open.
#         mode (str): ``'r'``, ``'rb'``, ``'w'``, or ``'wb'``.
# 
#     Returns:
#         File handle that can be used as a context.
#     """
#     gfile = preset_import('gfile')
# 
#     open_func = open if gfile is None else gfile.Open
#     handle = open_func(path, mode)
# 
#     return handle
# 
# Path: third_party/xiuminglib/xiuminglib/log.py
# def get_logger(level=None):
#     """Creates a logger for functions in the library.
# 
#     Args:
#         level (str, optional): Logging level. Defaults to ``logging.INFO``.
# 
#     Returns:
#         logging.Logger: Logger created.
#     """
#     if level is None:
#         level = logging.INFO
#     logging.basicConfig(level=level)
#     logger = logging.getLogger()
#     return logger


# Path: third_party/xiuminglib/xiuminglib/io/hdr.py
from os.path import dirname
from ..imprt import preset_import
from ..os import makedirs, open_file
from ..log import get_logger
import numpy as np

    """Reads an HDR map from disk.

    Args:
        path (str): Path to the .hdr file.

    Returns:
        numpy.ndarray: Loaded (float) HDR map with RGB channels in order.
    """
    cv2 = preset_import('cv2', assert_success=True)

    with open_file(path, 'rb') as h:
        buffer_ = np.fromstring(h.read(), np.uint8)
    bgr = cv2.imdecode(buffer_, cv2.IMREAD_UNCHANGED)
    rgb = cv2.cvtColor(bgr, cv2.COLOR_BGR2RGB)

    logger.debug("HDR map read from:\n\t%s", path)

    return rgb


def write(rgb, outpath):
    r"""Writes a ``float32`` array as an HDR map to disk.

    Args:
        rgb (numpy.ndarray): ``float32`` RGB array.
        outpath (str): Output path.

    Writes
        - The resultant HDR map.
    """

    cv2 = preset_import('cv2', assert_success=True)