コード例 #1
0
def get_write_function(preset,
                       overwrite_preset=False,
                       out_type=None,
                       out_format=None,
                       input_range=None,
                       output_range=None,
                       out_bit_depth=None,
                       out_cube_size=None,
                       verbose=False):
    """ Get write function from a preset

    Args:
        preset (dict): lut generic and sampling informations

    Returns:
        .write function

    """
    if overwrite_preset:
        if out_type is not None:
            preset[presets.TYPE] = out_type
        if out_format is not None:
            preset[presets.EXT] = out_format
        if input_range is not None:
            preset[presets.IN_RANGE] = input_range
        if output_range is not None:
            preset[presets.OUT_RANGE] = output_range
        if out_bit_depth is not None:
            preset[presets.OUT_BITDEPTH] = out_bit_depth
        if out_cube_size is not None:
            preset[presets.CUBE_SIZE] = out_cube_size

    if (not overwrite_preset
            and (out_type is not None or out_format is not None
                 or input_range is not None or output_range is not None
                 or out_bit_depth is not None or out_cube_size is not None)):
        if verbose:
            print_warning_message(("A preset was specified."
                                   " Default behaviour is to ignore other"
                                   " export options. Use "
                                   "--overwrite-preset, if you want to "
                                   "overwrite preset values that are"
                                   " redefined by other options"))

    typ = preset[presets.TYPE]
    ext, helper = _get_ext_and_helper(preset[presets.EXT], typ)
    # necessary if presets.TYPE was overwrite by overwrite_preset option
    preset[presets.EXT] = ext
    # check
    helper.check_preset(preset)
    return _get_write_function(helper, typ)
コード例 #2
0
def check_range_is_int(arange, message=None):
    """ Check input / output range. Some LUT are int.
        Print a warning or raise an error

    """
    if message is None:
        message = _get_range_int_message(arange)
    if not is_int(arange):
        raise PresetException(message)
    elif arange[1] < BITDEPTH_MAX_VALUE:
        message = ("{0} seems too low !\n"
                   "Please check this, if the LUT isn't what you expected"
                   ).format(message)
        print_warning_message(message)
コード例 #3
0
def check_range_is_float(arange, message=None):
    """ Check output range. Some LUT are float.
        Print a warning or raise an error

    """
    if message is None:
        message = _get_range_float_message(arange)
    if is_int(arange):
        raise PresetException(message)
    elif arange[1] > FLOAT_BOUNDARY:
        message = ("{0} seems too big !\n"
                   "Please check this, if the LUT isn't what you expected"
                   ).format(message)
        print_warning_message(message)
コード例 #4
0
ファイル: lut_presets.py プロジェクト: dkirat/ColorPipe-tools
def check_range_is_int(arange, message=None):
    """ Check input / output range. Some LUT are int.
        Print a warning or raise an error

    """
    if message is None:
        message = _get_range_int_message(arange)
    if not is_int(arange):
        raise PresetException(message)
    elif arange[1] < BITDEPTH_MAX_VALUE:
        message = ("{0} seems too low !\n"
                   "Please check this, if the LUT isn't what you expected"
                   ).format(message)
        print_warning_message(message)
コード例 #5
0
ファイル: lut_presets.py プロジェクト: dkirat/ColorPipe-tools
def check_range_is_float(arange, message=None):
    """ Check output range. Some LUT are float.
        Print a warning or raise an error

    """
    if message is None:
        message = _get_range_float_message(arange)
    if is_int(arange):
        raise PresetException(message)
    elif arange[1] > FLOAT_BOUNDARY:
        message = ("{0} seems too big !\n"
                   "Please check this, if the LUT isn't what you expected"
                   ).format(message)
        print_warning_message(message)
コード例 #6
0
def get_write_function(preset, overwrite_preset=False, out_type=None,
                       out_format=None, input_range=None, output_range=None,
                        out_bit_depth=None, out_cube_size=None, verbose=False):
    """ Get write function from a preset

    Args:
        preset (dict): lut generic and sampling informations

    Returns:
        .write function

    """
    if overwrite_preset:
        if out_type is not None:
            preset[presets.TYPE] = out_type
        if out_format is not None:
            preset[presets.EXT] = out_format
        if input_range is not None:
            preset[presets.IN_RANGE] = input_range
        if output_range is not None:
            preset[presets.OUT_RANGE] = output_range
        if out_bit_depth is not None:
            preset[presets.OUT_BITDEPTH] = out_bit_depth
        if out_cube_size is not None:
            preset[presets.CUBE_SIZE] = out_cube_size

    if (not overwrite_preset
        and (out_type is not None
             or out_format is not None
             or input_range is not None
             or output_range is not None
             or out_bit_depth is not None
             or out_cube_size is not None
             )):
        if verbose:
            print_warning_message(("A preset was specified."
                                   " Default behaviour is to ignore other"
                                   " export options. Use "
                                   "--overwrite-preset, if you want to "
                                   "overwrite preset values that are"
                                   " redefined by other options"))

    typ = preset[presets.TYPE]
    ext, helper = _get_ext_and_helper(preset[presets.EXT], typ)
    # necessary if presets.TYPE was overwrite by overwrite_preset option
    preset[presets.EXT] = ext
    # check
    helper.check_preset(preset)
    return _get_write_function(helper, typ)
コード例 #7
0
 def write_1d_lut(self, process_function, file_path, preset):
     print_warning_message("1D LUT is not supported in Cube format"
                           " --> Switch to 2D LUT.")
     return self.write_2d_lut(process_function, file_path, preset)
コード例 #8
0
ファイル: cube_helper.py プロジェクト: dkirat/ColorPipe-tools
 def write_1d_lut(self, process_function, file_path, preset):
     print_warning_message("1D LUT is not supported in Cube format"
                           " --> Switch to 2D LUT.")
     return self.write_2d_lut(process_function, file_path, preset)
コード例 #9
0
def curve_to_lut(colorspace, gamma, outlutfile, out_type=None, out_format=None,
                 input_range=None, output_range=None, out_bit_depth=None,
                 out_cube_size=None, verbose=False, direction=Direction.ENCODE,
                 preset=None, overwrite_preset=False,
                 process_input_range=False):
    """Export a LUT from a colorspace gradation function

    Args:
        colorspace (str): input colorspace. Mutually exclusive with gamma.
        See list of colorspaces in utils.colorspaces

        gamma (float): input gamma. Mutually exclusive with colorspace.

        out_type (str): 1D, 2D or 3D

        out_format (str): '3dl', 'csp', 'cube', 'lut', 'spi', 'clcc', 'json'...

        outlutfile (str): path to output LUT

    Kwargs:

        input_range ([int/float, int/float]): input range.
        Ex: [0.0, 1.0] or [0, 4095]

        output_range ([int/float, int/float]): output range.
        Ex: [0.0, 1.0] or [0, 4095]

        out_bit_depth (int): output lut bit precision (1D only).
        Ex : 10, 16, 32.

        out_cube_size (int): output cube size (3D only). Ex : 17, 32.

        verbose (bool): print log if true

        direction (Direction): encode or decode

        preset (dict): lut generic and sampling informations

        process_input_range (bool): If true, input range will be computed from
        colorspace gradation functions. Colorspace only"

    """
    # get colorspace function
    if colorspace is None and gamma is None:
        raise AttributeError("A colorspace or a gamma should be specified")
    if colorspace is not None and gamma is not None:
        raise AttributeError("Choose between a colorspace or a gamma")
    elif gamma is not None:
        # gamma mode
        if direction == Direction.DECODE:
            gradation = lambda value: gamma_to_lin(value, gamma)
            title = "Gamma{0}_to_lin".format(gamma)
        else:
            gradation = lambda value: lin_to_gamma(value, gamma)
            title = "Lin_to_gamma{0}".format(gamma)
    else:
        # colorspace mode
        try:
            colorspace_obj = dict(list(COLORSPACES.items()) +
                                  list(PRIVATE_COLORSPACES.items()))[colorspace]
        except KeyError:
            raise CurveToLUTException(("Unsupported {0} "
                                       "Colorspace!").format(colorspace))
        if direction == Direction.DECODE:
            gradation = colorspace_obj.decode_gradation
            title = "{0}_to_lin".format(colorspace)
        else:
            gradation = colorspace_obj.encode_gradation
            title = "Lin_to_{0}".format(colorspace)
    # get preset and write function
    if preset:
        write_function = get_write_function(preset, overwrite_preset,
                                            out_type, out_format,
                                            input_range,
                                            output_range,
                                            out_bit_depth,
                                            out_cube_size,
                                            verbose)
    elif out_type is None or out_format is None:
        raise CurveToLUTException("Specify out_type/out_format or a preset.")
    else:
        preset, write_function = get_preset_and_write_function(out_type,
                                                               out_format,
                                                               input_range,
                                                               output_range,
                                                               out_bit_depth,
                                                               out_cube_size)
    if preset[presets.TYPE] == '3D':
        print_warning_message(("Gradations and gamma functions are 1D / 2D"
                               " transformations. Baking them in a 3D LUT "
                               "may not be efficient. Are you sure ?"))
    # process file output
    if os.path.isdir(outlutfile):
        filename = "{0}{1}".format(title,
                                   preset[presets.EXT])
        outlutfile = os.path.join(outlutfile, filename)
    else:
        try:
            check_extension(outlutfile, preset[presets.EXT])
            outlutfile = outlutfile
        except LUTException as error:
            raise CurveToLUTException(("Directory doesn't exist "
                                       "or {0}").format(error))
    preset[presets.TITLE] = title
    if process_input_range:
        if colorspace:
            preset[presets.IN_RANGE] = get_input_range(colorspace_obj,
                                                       direction,
                                                       8)
        else:
            raise CurveToLUTException(("--process-input-range must be used"
                                       " with --colorspace."))
    if verbose:
        print("{0} will be written in {1}.".format(title, outlutfile))
        print("Final setting:\n{0}".format(presets.string_preset(preset)))
    # write
    message = write_function(gradation, outlutfile, preset)
    if verbose:
        print_success_message(message)
コード例 #10
0
def curve_to_lut(colorspace, gamma, outlutfile, out_type=None, out_format=None,
                 input_range=None, output_range=None, out_bit_depth=None,
                 out_cube_size=None, verbose=False, direction=Direction.ENCODE,
                 preset=None, overwrite_preset=False,
                 process_input_range=False):
    """Export a LUT from a colorspace gradation function

    Args:
        colorspace (str): input colorspace. Mutually exclusive with gamma.
        See list of colorspaces in utils.colorspaces

        gamma (float): input gamma. Mutually exclusive with colorspace.

        out_type (str): 1D, 2D or 3D

        out_format (str): '3dl', 'csp', 'cube', 'lut', 'spi', 'clcc', 'json'...

        outlutfile (str): path to output LUT

    Kwargs:

        input_range ([int/float, int/float]): input range.
        Ex: [0.0, 1.0] or [0, 4095]

        output_range ([int/float, int/float]): output range.
        Ex: [0.0, 1.0] or [0, 4095]

        out_bit_depth (int): output lut bit precision (1D only).
        Ex : 10, 16, 32.

        out_cube_size (int): output cube size (3D only). Ex : 17, 32.

        verbose (bool): print log if true

        direction (Direction): encode or decode

        preset (dict): lut generic and sampling informations

        process_input_range (bool): If true, input range will be computed from
        colorspace gradation functions. Colorspace only"

    """
    # get colorspace function
    if colorspace is None and gamma is None:
        raise AttributeError("A colorspace or a gamma should be specified")
    if colorspace is not None and gamma is not None:
        raise AttributeError("Choose between a colorspace or a gamma")
    elif gamma is not None:
        # gamma mode
        if direction == Direction.DECODE:
            gradation = lambda value: gamma_to_lin(value, gamma)
            title = "Gamma{0}_to_lin".format(gamma)
        else:
            gradation = lambda value: lin_to_gamma(value, gamma)
            title = "Lin_to_gamma{0}".format(gamma)
    else:
        # colorspace mode
        try:
            colorspace_obj = dict(COLORSPACES.items() +
                                  PRIVATE_COLORSPACES.items())[colorspace]
        except KeyError:
            raise CurveToLUTException(("Unsupported {0} "
                                       "Colorspace!").format(colorspace))
        if direction == Direction.DECODE:
            gradation = colorspace_obj.decode_gradation
            title = "{0}_to_lin".format(colorspace)
        else:
            gradation = colorspace_obj.encode_gradation
            title = "Lin_to_{0}".format(colorspace)
    # get preset and write function
    if preset:
        write_function = get_write_function(preset, overwrite_preset,
                                            out_type, out_format,
                                            input_range,
                                            output_range,
                                            out_bit_depth,
                                            out_cube_size,
                                            verbose)
    elif out_type is None or out_format is None:
        raise CurveToLUTException("Specify out_type/out_format or a preset.")
    else:
        preset, write_function = get_preset_and_write_function(out_type,
                                                               out_format,
                                                               input_range,
                                                               output_range,
                                                               out_bit_depth,
                                                               out_cube_size)
    if preset[presets.TYPE] == '3D':
        print_warning_message(("Gradations and gamma functions are 1D / 2D"
                               " transformations. Baking them in a 3D LUT "
                               "may not be efficient. Are you sure ?"))
    # process file output
    if os.path.isdir(outlutfile):
        filename = "{0}{1}".format(title,
                                   preset[presets.EXT])
        outlutfile = os.path.join(outlutfile, filename)
    else:
        try:
            check_extension(outlutfile, preset[presets.EXT])
            outlutfile = outlutfile
        except LUTException as error:
            raise CurveToLUTException(("Directory doesn't exist "
                                       "or {0}").format(error))
    preset[presets.TITLE] = title
    if process_input_range:
        if colorspace:
            preset[presets.IN_RANGE] = get_input_range(colorspace_obj,
                                                       direction,
                                                       8)
        else:
            raise CurveToLUTException(("--process-input-range must be used"
                                       " with --colorspace."))
    if verbose:
        print "{0} will be written in {1}.".format(title, outlutfile)
        print "Final setting:\n{0}".format(presets.string_preset(preset))
    # write
    message = write_function(gradation, outlutfile, preset)
    if verbose:
        print_success_message(message)