from maya.OpenMaya import MImage
import os.path as osp


def convertBumpToNormal(src, dst, scale=1, outputFormat=None, force=False):
    """Creates a new file (dst) that can be used as normal map file from given (src)
     file.

    :param src: Source file used to create normal map.
    :type src: str
    :param dst: Destination file, where to save converted map.
    :type dst: str
    :param scale: Depth normal scale, can vary from -256.0 to 256.0, although typical values range from 1.0 to 10.0.
    :type scale: float
    :param outputFormat: Destination format, if not set, if taken from given dst or src.
    :type outputFormat: str
    :param force: If dst file exists, overwrites it.
    :type force: bool
    """
    image = MImage()
    if not osp.isfile(src):
        raise ValueError("%s if not a file!" % src)
    if osp.isfile(dst) and not force:
        raise Exception("%s already exists, use -force flag to overwrite." % dst)

    image.readFromFile(src)
    image.filter(MImage.kHeightFieldBumpFormat, MImage.kNormalMapBumpFormat, scale)

    outputFormat = (outputFormat or osp.splitext(dst)[1].replace(".", "") or
                    osp.splitext(src)[1].replace(".", ""))
    if not outputFormat:
        image.writeToFile(dst)
    else:
        image.writeToFile(dst, outputFormat)