From 96f8ff7aceca44ebe9d78125e043417a60af7662 Mon Sep 17 00:00:00 2001 From: emotion3459 <176516814+emotion3459@users.noreply.github.com> Date: Wed, 13 Nov 2024 13:49:05 -0500 Subject: [PATCH] Add new scale_mask & scale_delta helpers (#161) --- vstools/utils/scale.py | 55 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/vstools/utils/scale.py b/vstools/utils/scale.py index 2525c82..f558fdb 100644 --- a/vstools/utils/scale.py +++ b/vstools/utils/scale.py @@ -10,7 +10,7 @@ from .info import get_depth, get_video_format __all__ = [ - 'scale_value', + 'scale_value', 'scale_mask', 'scale_delta', 'get_lowest_value', 'get_neutral_value', 'get_peak_value', 'get_lowest_values', 'get_neutral_values', 'get_peak_values', @@ -97,6 +97,59 @@ def scale_value( return out_value +def scale_mask( + value: int | float, + input_depth: int | VideoFormatT | HoldsVideoFormatT, + output_depth: int | VideoFormatT | HoldsVideoFormatT +) -> int | float: + """ + Converts the value to the specified bit depth, or bit depth of the clip/format specified. + Intended for mask clips which are always full range. + + :param value: Value to scale. + :param input_depth: Input bit depth, or clip, frame, format from where to get it. + :param output_depth: Output bit depth, or clip, frame, format from where to get it. + + :return: Scaled value. + """ + + return scale_value(value, input_depth, output_depth, ColorRange.FULL, ColorRange.FULL) + + +def scale_delta( + value: int | float, + input_depth: int | VideoFormatT | HoldsVideoFormatT, + output_depth: int | VideoFormatT | HoldsVideoFormatT, + range_in: ColorRangeT | None = None, + range_out: ColorRangeT | None = None, +) -> int | float: + """ + Converts the value to the specified bit depth, or bit depth of the clip/format specified. + Uses the clip's range (if only one clip is passed) for both depths. + Intended for filter thresholds. + + :param value: Value to scale. + :param input_depth: Input bit depth, or clip, frame, format from where to get it. + :param output_depth: Output bit depth, or clip, frame, format from where to get it. + :param range_in: Color range of the input value + :param range_out: Color range of the desired output. + + :return: Scaled value. + """ + + if isinstance(input_depth, vs.VideoNode) != isinstance(output_depth, vs.VideoNode): + if isinstance(input_depth, vs.VideoNode): + clip_range = input_depth + if isinstance(output_depth, vs.VideoNode): + clip_range = output_depth + + clip_range = ColorRange.from_video(clip_range) + range_in = clip_range if range_in is None else range_in + range_out = clip_range if range_out is None else range_out + + return scale_value(value, input_depth, output_depth, range_in, range_out, False) + + def get_lowest_value( clip_or_depth: int | VideoFormatT | HoldsVideoFormatT, chroma: bool = False, range_in: ColorRangeT | None = None, family: vs.ColorFamily | None = None