From 198d54fd16b9dcbe26bb5ad1313be93cc0d30fd1 Mon Sep 17 00:00:00 2001 From: LightArrowsEXE Date: Sat, 11 Nov 2023 13:49:35 +0100 Subject: [PATCH] Add kernel_size properties --- vskernels/kernels/bicubic.py | 4 ++++ vskernels/kernels/placebo.py | 14 ++++++++++++++ vskernels/kernels/resize.py | 9 +++++++++ vskernels/kernels/spline.py | 25 ++++++++++++++++++++++--- vskernels/kernels/various.py | 7 ++----- 5 files changed, 51 insertions(+), 8 deletions(-) diff --git a/vskernels/kernels/bicubic.py b/vskernels/kernels/bicubic.py index 99de42b..d2d8850 100644 --- a/vskernels/kernels/bicubic.py +++ b/vskernels/kernels/bicubic.py @@ -68,6 +68,10 @@ def get_params_args( return args | dict(b=self.b, c=self.c) return args | dict(filter_param_a=self.b, filter_param_b=self.c) + @property + def kernel_size(self) -> int: + return 1 + ((self.b, self.c) != (0, 0)) + class BSpline(Bicubic): """Bicubic b=1, c=0""" diff --git a/vskernels/kernels/placebo.py b/vskernels/kernels/placebo.py index f779c92..009cfd8 100644 --- a/vskernels/kernels/placebo.py +++ b/vskernels/kernels/placebo.py @@ -1,5 +1,6 @@ from __future__ import annotations +from math import ceil from typing import TYPE_CHECKING, Any from vstools import Transfer, TransferT, core, inject_self, vs @@ -82,3 +83,16 @@ def get_params_args( antiring=self.antiring, cutoff=self.cutoff, lut_entries=self.lut_entries, trc=curve.value_libplacebo ) + + def _kernel_size(self, taps: float | None = None, b: int | None = None, c: int | None = None) -> int + if taps: + return ceil(self.taps) + + if b or c: + return 1 + ((b if b and b != 0 else 0, c if c and c != 0 else 0.5) != (0, 0)) + + return 1 + + @property + def kernel_size(self) -> int: + return self._kernel_size(self.taps, self.b, self.c) diff --git a/vskernels/kernels/resize.py b/vskernels/kernels/resize.py index f1e553f..f36b97d 100644 --- a/vskernels/kernels/resize.py +++ b/vskernels/kernels/resize.py @@ -1,5 +1,6 @@ from __future__ import annotations +from math import ceil from typing import Any from vstools import core, vs @@ -25,6 +26,10 @@ class Bilinear(ZimgComplexKernel): scale_function = resample_function = core.lazy.resize.Bilinear descale_function = core.lazy.descale.Debilinear + @property + def kernel_size(self) -> int: + return 1 + class Lanczos(ZimgComplexKernel): """ @@ -51,3 +56,7 @@ def get_params_args( if is_descale: return args | dict(taps=self.taps) return args | dict(filter_param_a=self.taps) + + @property + def kernel_size(self) -> int: + return ceil(self.taps) diff --git a/vskernels/kernels/spline.py b/vskernels/kernels/spline.py index 64a5fa3..52b6ea9 100644 --- a/vskernels/kernels/spline.py +++ b/vskernels/kernels/spline.py @@ -1,5 +1,7 @@ from __future__ import annotations +import re +from math import ceil, isqrt from typing import Any from vstools import core @@ -15,6 +17,19 @@ ] +class _SplineKernelSize: + """Spline kernel size sub-class.""" + + @property + def kernel_size(self) -> int: + radius = re.search(r'\d+$', self.__class__.__name__) + + if not radius: + return 1 + + return ceil(isqrt(int(radius.group())) / 2) + + class Spline(FmtConv): """fmtconv's spline resizer.""" @@ -23,8 +38,12 @@ class Spline(FmtConv): def __init__(self, taps: int = 2, **kwargs: Any) -> None: super().__init__(taps=taps, **kwargs) + @property + def kernel_size(self) -> int: + return ceil(self.taps) + -class Spline16(ZimgComplexKernel): +class Spline16(ZimgComplexKernel, _SplineKernelSize): """ Built-in spline16 resizer. @@ -37,7 +56,7 @@ class Spline16(ZimgComplexKernel): descale_function = core.lazy.descale.Despline16 -class Spline36(ZimgComplexKernel): +class Spline36(ZimgComplexKernel, _SplineKernelSize): """ Built-in spline36 resizer. @@ -50,7 +69,7 @@ class Spline36(ZimgComplexKernel): descale_function = core.lazy.descale.Despline36 -class Spline64(ZimgComplexKernel): +class Spline64(ZimgComplexKernel, _SplineKernelSize): """ Built-in spline64 resizer. diff --git a/vskernels/kernels/various.py b/vskernels/kernels/various.py index 59fb9ed..ac4820a 100644 --- a/vskernels/kernels/various.py +++ b/vskernels/kernels/various.py @@ -1,6 +1,6 @@ from __future__ import annotations -from math import log, sqrt +from math import ceil, log, sqrt from typing import Any from vstools import CustomValueError, to_singleton @@ -104,10 +104,7 @@ class EwaBicubic(Placebo): _kernel = 'ewa_robidoux' def __init__(self, b: float = 0.0, c: float = 0.5, radius: int | None = None, **kwargs: Any) -> None: - radius = kwargs.pop('taps', radius) - - if radius is None: - radius = 1 if (b, c) == (0, 0) else 2 + radius = self._kernel_size(kwargs.pop('taps', radius), b, c) super().__init__(radius, b, c, **kwargs)