Skip to content

Commit

Permalink
Add new scale_mask & scale_delta helpers (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
emotion3459 authored Nov 13, 2024
1 parent b1e7a73 commit 96f8ff7
Showing 1 changed file with 54 additions and 1 deletion.
55 changes: 54 additions & 1 deletion vstools/utils/scale.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 96f8ff7

Please sign in to comment.