Skip to content

Commit

Permalink
Update value scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
emotion3459 committed Oct 14, 2024
1 parent d6c423b commit a15da07
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 20 deletions.
3 changes: 2 additions & 1 deletion vsmasktools/edge/_abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
9 changes: 5 additions & 4 deletions vsmasktools/edge_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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),
Expand All @@ -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)
Expand Down Expand Up @@ -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

Expand Down
15 changes: 7 additions & 8 deletions vsmasktools/hardsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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} ?'
Expand Down Expand Up @@ -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 ?'
Expand Down
8 changes: 4 additions & 4 deletions vsmasktools/morpho.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
)
Expand Down
7 changes: 4 additions & 3 deletions vsmasktools/spat_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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
))

Expand Down

0 comments on commit a15da07

Please sign in to comment.