From a15da07a790aef6819ac720ba23dbd9dc52d6d18 Mon Sep 17 00:00:00 2001 From: emotion3459 <176516814+emotion3459@users.noreply.github.com> Date: Mon, 14 Oct 2024 18:32:24 -0400 Subject: [PATCH] Update value scaling --- vsmasktools/edge/_abstract.py | 3 ++- vsmasktools/edge_funcs.py | 9 +++++---- vsmasktools/hardsub.py | 15 +++++++-------- vsmasktools/morpho.py | 8 ++++---- vsmasktools/spat_funcs.py | 7 ++++--- 5 files changed, 22 insertions(+), 20 deletions(-) diff --git a/vsmasktools/edge/_abstract.py b/vsmasktools/edge/_abstract.py index c8e5ea8..1d71ae1 100644 --- a/vsmasktools/edge/_abstract.py +++ b/vsmasktools/edge/_abstract.py @@ -183,7 +183,8 @@ def _mask( peak = get_peak_value(clip) hthr = 1.0 if hthr is None else hthr - lthr, hthr = scale_value(lthr, 32, clip, ColorRange.FULL), scale_value(hthr, 32, clip, ColorRange.FULL) + lthr = scale_value(lthr, 32, clip, range_out=ColorRange.FULL) + hthr = scale_value(hthr, 32, clip, range_out=ColorRange.FULL) discard_planes = False if isinstance(planes, tuple): diff --git a/vsmasktools/edge_funcs.py b/vsmasktools/edge_funcs.py index 32af75d..7504fb0 100644 --- a/vsmasktools/edge_funcs.py +++ b/vsmasktools/edge_funcs.py @@ -41,7 +41,7 @@ def ringing_mask( assert check_variable(clip, ringing_mask) thmi, thma, thlimi, thlima = ( - scale_value(t, 32, clip, ColorRange.FULL) for t in [thmi, thma, thlimi, thlima] + scale_value(t, 32, clip, range_out=ColorRange.FULL) for t in [thmi, thma, thlimi, thlima] ) edgemask = normalize_mask(credit_mask, plane(clip, 0), **kwargs).std.Limiter() @@ -67,7 +67,8 @@ def luma_mask(clip: vs.VideoNode, thr_lo: float, thr_hi: float, invert: bool = T lo, hi = (peak, 0) if invert else (0, peak) inv_pre, inv_post = (peak, '-') if invert else ('', '') - thr_lo, thr_hi = scale_value(thr_lo, 32, clip, ColorRange.FULL), scale_value(thr_hi, 32, clip, ColorRange.FULL) + thr_lo = scale_value(thr_lo, 32, clip, range_out=ColorRange.FULL) + thr_hi = scale_value(thr_hi, 32, clip, range_out=ColorRange.FULL) return norm_expr( get_y(clip), @@ -82,7 +83,7 @@ def luma_credit_mask( edge_mask = normalize_mask(edgemask, y, **kwargs) - credit_mask = norm_expr([edge_mask, y], f'y {scale_value(thr, 32, y, ColorRange.FULL)} > y 0 ? x min') + credit_mask = norm_expr([edge_mask, y], f'y {scale_value(thr, 32, y, range_out=ColorRange.FULL)} > y 0 ? x min') if not draft: credit_mask = Morpho.maximum(credit_mask, iterations=4) @@ -134,7 +135,7 @@ def _prefilter(self, clip: vs.VideoNode, **kwargs: Any) -> vs.VideoNode: if self is self.CLAHE: # type: ignore limit, tile = kwargs.get('limit', 0.0305), kwargs.get('tile', 5) - return depth(depth(clip, 16).ehist.CLAHE(scale_value(limit, 32, 16, ColorRange.FULL), tile), clip) + return depth(depth(clip, 16).ehist.CLAHE(scale_value(limit, 32, 16, range_out=ColorRange.FULL), tile), clip) return clip diff --git a/vsmasktools/hardsub.py b/vsmasktools/hardsub.py index fb7c20f..0dce249 100644 --- a/vsmasktools/hardsub.py +++ b/vsmasktools/hardsub.py @@ -11,7 +11,7 @@ from vssource import IMWRI, Indexer from vstools import ( ColorRange, CustomOverflowError, FileNotExistsError, FilePathType, FrameRangeN, FrameRangesN, Matrix, VSFunction, - check_variable, core, depth, fallback, get_neutral_value, get_neutral_values, get_y, iterate, normalize_ranges, + check_variable, core, depth, fallback, get_peak_value, get_lowest_value, get_neutral_value, get_neutral_values, get_y, iterate, normalize_ranges, replace_ranges, scale_value, vs, vs_object ) @@ -130,7 +130,7 @@ def get_progressive_dehardsub( assert masks[-1].format is not None - thr = scale_value(self.bin_thr, 32, masks[-1], ColorRange.FULL) + thr = scale_value(self.bin_thr, 32, masks[-1], range_out=ColorRange.FULL) for p in partials: masks.append( @@ -179,7 +179,7 @@ def _mask(self, clip: vs.VideoNode, ref: vs.VideoNode, **kwargs: Any) -> vs.Vide for x in (clip, ref) ) - highpass = scale_value(self.highpass, 32, clip, ColorRange.FULL) + highpass = scale_value(self.highpass, 32, clip, range_out=ColorRange.FULL) mask = norm_expr( [clipedge, refedge], f'x y - {highpass} < 0 {ExprToken.RangeMax} ?' @@ -238,17 +238,16 @@ def _mask(self, clip: vs.VideoNode, ref: vs.VideoNode, **kwargs: Any) -> vs.Vide expand_n = fallback(self.expand, clip.width // 200) - y_range = scale_value(219, 8, clip, ColorRange.FULL) if clip.format.sample_type == vs.INTEGER else 1 - uv_range = scale_value(224, 8, clip, ColorRange.FULL) if clip.format.sample_type == vs.INTEGER else 1 - offset = scale_value(16, 8, clip, ColorRange.FULL) if clip.format.sample_type == vs.INTEGER else 0 + y_range = get_peak_value(clip) - get_lowest_value(clip) + uv_range = get_peak_value(clip, chroma=True) - get_lowest_value(clip, chroma=True) uv_abs = ' abs ' if clip.format.sample_type == vs.FLOAT else f' {get_neutral_value(clip)} - abs ' yexpr = f'x y - abs {y_range * 0.7} > 255 0 ?' uv_thr = uv_range * 0.8 uvexpr = f'x {uv_abs} {uv_thr} < y {uv_abs} {uv_thr} < and 255 0 ?' - upper = y_range * 0.8 + offset - lower = y_range * 0.2 + offset + upper = scale_value(0.8, 32, clip) + lower = scale_value(0.2, 32, clip) mindiff = y_range * 0.1 difexpr = f'x {upper} > x {lower} < or x y - abs {mindiff} > and 255 0 ?' diff --git a/vsmasktools/morpho.py b/vsmasktools/morpho.py index ab56839..7758b07 100644 --- a/vsmasktools/morpho.py +++ b/vsmasktools/morpho.py @@ -112,7 +112,7 @@ def _morpho_xx_imum( matrix = ExprList(interleave_arr(matrix, op * matrix.mlength, 2)) if thr is not None: - matrix.append('x', scale_value(thr, 32, src, ColorRange.FULL), ExprOp.SUB, ExprOp.MAX) + matrix.append('x', scale_value(thr, 32, src, range_out=ColorRange.FULL), ExprOp.SUB, ExprOp.MAX) if multiply is not None: matrix.append(multiply, ExprOp.MUL) @@ -150,7 +150,7 @@ def _mm_func( if not self._fast: if thr is not None: - kwargs.update(threshold=scale_value(thr, 32, src, ColorRange.FULL)) + kwargs.update(threshold=scale_value(thr, 32, src, range_out=ColorRange.FULL)) if multiply is not None: orig_mm_func = mm_func @@ -211,7 +211,7 @@ def _xxflate( expr.append('x', ExprOp.MAX if inflate else ExprOp.MIN) if thr is not None: - thr = scale_value(thr, 32, src, ColorRange.FULL) + thr = scale_value(thr, 32, src, range_out=ColorRange.FULL) limit = ['x', thr, ExprOp.ADD] if inflate else ['x', thr, ExprOp.SUB, ExprToken.RangeMin, ExprOp.MAX] expr.append(limit, ExprOp.MIN if inflate else ExprOp.MAX) @@ -375,7 +375,7 @@ def binarize( ) -> vs.VideoNode: midthr, lowval, highval = ( thr and list( - scale_value(t, 32, src, ColorRange.FULL, chroma=i != 0) + scale_value(t, 32, src, range_out=ColorRange.FULL) for i, t in enumerate(to_arr(thr)) ) for thr in (midthr, lowval, highval) ) diff --git a/vsmasktools/spat_funcs.py b/vsmasktools/spat_funcs.py index 1122cf2..e1c00e9 100644 --- a/vsmasktools/spat_funcs.py +++ b/vsmasktools/spat_funcs.py @@ -149,7 +149,7 @@ def flat_mask(src: vs.VideoNode, radius: int = 5, thr: float = 0.011, gauss: boo blur = gauss_blur(luma, radius * 0.361083333) if gauss else box_blur(luma, radius) - mask = depth(luma, 8).abrz.AdaptiveBinarize(depth(blur, 8), scale_value(thr, 32, 8, ColorRange.FULL)) + mask = depth(luma, 8).abrz.AdaptiveBinarize(depth(blur, 8), scale_value(thr, 32, 8, range_out=ColorRange.FULL)) return depth(mask, luma, dither_type=DitherType.NONE, range_in=ColorRange.FULL, range_out=ColorRange.FULL) @@ -161,7 +161,8 @@ def texture_mask( points: list[tuple[bool, float]] = [(False, 1.75), (True, 2.5), (True, 5), (False, 10)] ) -> vs.VideoNode: levels = [x for x, _ in points] - _points = [scale_value(x, 8, clip, ColorRange.FULL) for _, x in points] + _points = [scale_value(x, 8, clip, ColorRange.FULL, ColorRange.FULL) for _, x in points] + thr = scale_value(thr, 8, 32, ColorRange.FULL, ColorRange.FULL) qm, peak = len(points), get_peak_value(clip) @@ -170,7 +171,7 @@ def texture_mask( emask = clip.std.Prewitt() rm_txt = ExprOp.MIN(rmask, ( - Morpho.minimum(Morpho.binarize(emask, scale_value(thr, 8, 32, ColorRange.FULL), 1.0, 0), iterations=it) + Morpho.minimum(Morpho.binarize(emask, thr, 1.0, 0), iterations=it) for thr, it in stages ))