From 1da1f454eb225812c6786b99311455b9cc745cf9 Mon Sep 17 00:00:00 2001
From: louis <louis@poweris.moe>
Date: Thu, 15 Apr 2021 13:27:49 -0400
Subject: [PATCH] vivy: tv: 03

---
 Vivy/03/03.vpy                | 43 +++++++++++++++++++++++++++++++++++
 Vivy/vivy_common/dehardsub.py |  9 +++++---
 2 files changed, 49 insertions(+), 3 deletions(-)
 create mode 100644 Vivy/03/03.vpy

diff --git a/Vivy/03/03.vpy b/Vivy/03/03.vpy
new file mode 100644
index 0000000..505400a
--- /dev/null
+++ b/Vivy/03/03.vpy
@@ -0,0 +1,43 @@
+import vapoursynth as vs
+
+from typing import List
+
+import os
+import sys
+sys.path.append("..")
+
+from vivy_common import (HardsubSign, Range, bounded_dehardsub, antialias, deband, denoise,  # noqa: E402
+                         finalize, fsrcnnx_rescale, letterbox_edgefix, source)
+
+core = vs.core
+
+
+EPNUM: int = int(os.path.basename(os.path.splitext(__file__)[0]))
+SIGNS_RU: List[HardsubSign] = [
+    HardsubSign((1371, 3157), ((293, 872), (1323, 162)), highpass=20000),
+    HardsubSign((3572, 3688), ((236, 860), (821, 103)), refframe=3671),
+    HardsubSign((4040, 4105), ((937, 18), (811, 338))),
+    HardsubSign((4040, 4105), ((132, 671), (384, 211))),
+    HardsubSign((8135, 8259), ((583, 65), (744, 80))),
+    HardsubSign((9572, 9596), ((573, 74), (768, 79))),
+    HardsubSign((21874, 21942), ((532, 131), (445, 228)), highpass=2000),
+    HardsubSign((29968, 31084), ((293, 872), (1323, 162)), highpass=20000),
+    HardsubSign((30002, 30792), ((302, 73), (1328, 142)), highpass=2000, expand=10),
+    HardsubSign((31727, 31879), ((293, 872), (1323, 162)), highpass=20000),
+    HardsubSign((32540, 32651), ((293, 872), (1323, 162)), highpass=20000),
+    HardsubSign((33948, 34044), ((267, 857), (1067, 104)), refframe=34030),
+]
+NOSCALE: List[Range] = []
+NOAA: List[Range] = []
+LETTERBOX: List[Range] = [(0, 432)]
+
+waka, ref = source(EPNUM)
+
+src = bounded_dehardsub(waka, ref, SIGNS_RU)
+rescale = fsrcnnx_rescale(src, NOSCALE)
+den = denoise(rescale)
+deb = deband(den)
+aa = antialias(deb, NOAA)
+edgefix = letterbox_edgefix(aa, LETTERBOX)
+final = finalize(edgefix)
+final.set_output()
diff --git a/Vivy/vivy_common/dehardsub.py b/Vivy/vivy_common/dehardsub.py
index 26abf85..4cc44fa 100644
--- a/Vivy/vivy_common/dehardsub.py
+++ b/Vivy/vivy_common/dehardsub.py
@@ -33,14 +33,16 @@ class HardsubSign():
     bound: Optional[BoundingBox]
     refframe: Optional[int]
     highpass: int
+    expand: int
 
     def __init__(self,
                  range: Range,
                  bound: Union[BoundingBox, Tuple[Tuple[int, int], Tuple[int, int]], None],
-                 refframe: Optional[int] = None, highpass: int = 5000):
+                 refframe: Optional[int] = None, highpass: int = 5000, expand: int = 8):
         self.range = range
         self.refframe = refframe
         self.highpass = highpass
+        self.expand = expand
         if bound is None:
             self.bound = None
         elif isinstance(bound, BoundingBox):
@@ -50,9 +52,10 @@ class HardsubSign():
 
     def _hardsub_mask(self, hrdsb: vs.VideoNode, ref: vs.VideoNode) -> vs.VideoNode:
         if self.refframe is not None:
-            mask = kgf.hardsubmask_fades(hrdsb[self.refframe], ref[self.refframe], highpass=self.highpass)
+            mask = kgf.hardsubmask_fades(hrdsb[self.refframe], ref[self.refframe],
+                                         highpass=self.highpass, expand_n=self.expand)
         else:
-            mask = kgf.hardsubmask_fades(hrdsb, ref, highpass=self.highpass)
+            mask = kgf.hardsubmask_fades(hrdsb, ref, highpass=self.highpass, expand_n=self.expand)
 
         assert isinstance(mask, vs.VideoNode)
         return mask