Skip to content

Commit

Permalink
Fix mismatched formats when using bound mask (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ichunjo authored Dec 2, 2024
1 parent 160ee61 commit 34dcf0c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
11 changes: 5 additions & 6 deletions vsmasktools/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,6 @@ def get_mask(self, clip: vs.VideoNode, ref: vs.VideoNode, **kwargs: Any) -> vs.V
assert check_variable(clip, self.get_mask)
assert check_variable(ref, self.get_mask)

if self.bound:
bm = self.bound.get_mask(ref, **kwargs)

if self.blur:
bm = box_blur(bm, 5, 5)

if self.refframes:
hm = ref.std.BlankClip(
format=ref.format.replace(color_family=vs.GRAY, subsampling_h=0, subsampling_w=0).id, keep=True
Expand All @@ -100,6 +94,11 @@ def get_mask(self, clip: vs.VideoNode, ref: vs.VideoNode, **kwargs: Any) -> vs.V
)

if self.bound:
bm = self.bound.get_mask(hm, **kwargs)

if self.blur:
bm = box_blur(bm, 5, 5)

return norm_expr([hm, bm], f'y {ExprOp.clamp(c="x")} 0 ?')

return hm.std.Limiter()
Expand Down
2 changes: 1 addition & 1 deletion vsmasktools/hardsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def _mask(self, clip: vs.VideoNode, ref: vs.VideoNode, **kwargs: Any) -> vs.Vide
[clipedge, refedge], f'x y - {highpass} < 0 {ExprToken.RangeMax} ?'
).std.Median()

return Morpho.inflate(Morpho.expand(mask, self.expand, mode=self.expand_mode), iterations=4)
return max_planes(Morpho.inflate(Morpho.expand(mask, self.expand, mode=self.expand_mode), iterations=4))


class HardsubSign(HardsubMask):
Expand Down
32 changes: 24 additions & 8 deletions vsmasktools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from vstools import (
CustomValueError, FrameRangeN, FrameRangesN, FuncExceptT, P, check_ref_clip, check_variable,
check_variable_format, core, depth, flatten, get_lowest_values, get_peak_values, insert_clip,
normalize_ranges, replace_ranges, split, vs
normalize_ranges, plane, replace_ranges, split, vs
)

from .abstract import GeneralMask
Expand Down Expand Up @@ -123,18 +123,34 @@ def squaremask(
color=get_lowest_values(mask_format),
keep=True
)

mask = norm_expr(
base_clip, _get_region_expr(
clip, offset_x, clip.width - width - offset_x, offset_y, clip.height - height - offset_y,
exprs = [
_get_region_expr(
base_clip, offset_x, clip.width - width - offset_x, offset_y, clip.height - height - offset_y,
'range_max x' if invert else 'x range_max'
), force_akarin=func
)
)
]

if mask_format.num_planes > 1:
for i in range(1, mask_format.num_planes):
p = plane(base_clip, i)
ratio_x = p.width / base_clip.width
ratio_y = p.height / base_clip.height
exprs.append(
_get_region_expr(
p,
int(offset_x * ratio_x), int((clip.width - width - offset_x) * ratio_x),
int(offset_y * ratio_y), int((clip.height - height - offset_y) * ratio_y),
'range_max x' if invert else 'x range_max'
)
)

mask = norm_expr(base_clip, tuple(exprs), force_akarin=func)
else:
base_clip = clip.std.BlankClip(width, height, mask_format.id, 1, color=get_peak_values(mask_format), keep=True)

mask = base_clip.std.AddBorders(
offset_x, clip.width - width - offset_x, offset_y, clip.height - height - offset_y
offset_x, clip.width - width - offset_x, offset_y, clip.height - height - offset_y,
[0] * mask_format.num_planes
)
if invert:
mask = mask.std.Invert()
Expand Down

0 comments on commit 34dcf0c

Please sign in to comment.