Skip to content

Commit

Permalink
Add kernel_radius properties (#17)
Browse files Browse the repository at this point in the history
* Add kernel_size properties

* flake8

* Placebo._kernel_size: Use fallback, fix typing

* Change @Property to @stgpytools.classproperty

* Switch classproperty for inject_self.property

* Remove hack for spline

* Add kernel_size to Point

* Add kernel_size to base classes

* Add kernel_size to BicubicAuto

* Add abstract_kernels

Fix #45

* More code cleanup

* Fix Placebo default for kernel_size

* Remove unused import

* Fix EwaBicubic's radius

* Add kernel_size to FmtConv

* Add _static_kernel_size for kernel with static sizes

* Rename kernel_size to kernel_radius

* Fix typo

---------

Co-authored-by: Setsugennoao <[email protected]>
  • Loading branch information
LightArrowsEXE and Setsugennoao authored Nov 11, 2023
1 parent 0bac0fb commit d467b80
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 5 deletions.
24 changes: 24 additions & 0 deletions vskernels/kernels/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@
]


def _default_kernel_radius(cls, self) -> int:
if hasattr(self, '_static_kernel_radius'):
return ceil(self._static_kernel_radius)

try:
return super(cls, self).kernel_radius
except AttributeError:
...

raise NotImplementedError


class BaseScaler:
@staticmethod
def from_param(
Expand Down Expand Up @@ -120,6 +132,10 @@ def multi(

return self.scale(clip, dst_width, dst_height, shift, **kwargs)

@inject_self.property
def kernel_radius(self) -> int:
return _default_kernel_radius(__class__, self)


class Descaler(vs_object):
@inject_self.cached
Expand Down Expand Up @@ -165,6 +181,10 @@ def ensure_obj(
) -> Descaler:
return BaseScaler.ensure_obj(cls, Descaler, descaler, UnknownDescalerError, [], func_except) # type: ignore

@inject_self.property
def kernel_radius(self) -> int:
return _default_kernel_radius(__class__, self)


class Resampler(vs_object):
@inject_self.cached
Expand All @@ -186,6 +206,10 @@ def ensure_obj(
) -> Resampler:
return BaseScaler.ensure_obj(cls, Resampler, resampler, UnknownDescalerError, [], func_except) # type: ignore

@inject_self.property
def kernel_radius(self) -> int:
return _default_kernel_radius(__class__, self)


class Kernel(Scaler, Descaler, Resampler):
"""
Expand Down
12 changes: 11 additions & 1 deletion vskernels/kernels/bicubic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from math import acos, asinh, cos, sqrt
from typing import TYPE_CHECKING, Any

from vstools import CustomValueError, core, vs
from vstools import CustomValueError, core, vs, inject_self

from .complex import ComplexKernel

Expand Down Expand Up @@ -68,6 +68,12 @@ 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)

@inject_self.property
def kernel_radius(self) -> int:
if (self.b, self.c) == (0, 0):
return 1
return 2


class BSpline(Bicubic):
"""Bicubic b=1, c=0"""
Expand Down Expand Up @@ -265,3 +271,7 @@ def _get_bc_args(self) -> tuple[float, float]:
autoc = (self.target - self.b) / 2

return autob, autoc

@inject_self.property
def kernel_radius(self) -> int:
return Bicubic(*self._get_bc_args()).kernel_radius
12 changes: 11 additions & 1 deletion vskernels/kernels/fmtconv.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from __future__ import annotations

from math import ceil
from typing import Any, Callable, overload

from vstools import VideoFormatT, VSFunction, core, inject_self, vs

from .abstract import Resampler
from .complex import ComplexScaler
from .bicubic import Bicubic
from .complex import ComplexScaler

__all__ = [
'FmtConv'
Expand Down Expand Up @@ -167,3 +168,12 @@ def _shift(shift_top: float | list[float] = 0.0, shift_left: float | list[float]
shifts_left = shifts_left[:n_planes]

return _shift(shifts_top, shifts_left)

@inject_self.property
def kernel_radius(self) -> int:
taps_hv = self.kwargs.get('taps_h', self.kwargs.get('taps_v', None))

if taps_hv is None:
taps_hv = self.taps

return ceil(taps_hv)
13 changes: 13 additions & 0 deletions vskernels/kernels/placebo.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -82,3 +83,15 @@ def get_params_args(
antiring=self.antiring, cutoff=self.cutoff,
lut_entries=self.lut_entries, trc=curve.value_libplacebo
)

@inject_self.property
def kernel_radius(self) -> int:
from .bicubic import Bicubic

if self.taps:
return ceil(self.taps)

if self.b or self.c:
return Bicubic(self.b, self.c).kernel_radius

return 2
9 changes: 8 additions & 1 deletion vskernels/kernels/resize.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from __future__ import annotations

from math import ceil
from typing import Any

from vstools import core, vs
from vstools import core, vs, inject_self

from .zimg import ZimgComplexKernel

Expand All @@ -17,13 +18,15 @@ class Point(ZimgComplexKernel):
"""Built-in point resizer."""

scale_function = resample_function = descale_function = core.lazy.resize.Point
_static_kernel_radius = 1


class Bilinear(ZimgComplexKernel):
"""Built-in bilinear resizer."""

scale_function = resample_function = core.lazy.resize.Bilinear
descale_function = core.lazy.descale.Debilinear
_static_kernel_radius = 1


class Lanczos(ZimgComplexKernel):
Expand Down Expand Up @@ -51,3 +54,7 @@ def get_params_args(
if is_descale:
return args | dict(taps=self.taps)
return args | dict(filter_param_a=self.taps)

@inject_self.property
def kernel_radius(self) -> int:
return ceil(self.taps)
5 changes: 4 additions & 1 deletion vskernels/kernels/spline.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

from vstools import core

from .zimg import ZimgComplexKernel
from .fmtconv import FmtConv
from .zimg import ZimgComplexKernel

__all__ = [
'Spline',
Expand Down Expand Up @@ -35,6 +35,7 @@ class Spline16(ZimgComplexKernel):

scale_function = resample_function = core.lazy.resize.Spline16
descale_function = core.lazy.descale.Despline16
_static_kernel_radius = 2


class Spline36(ZimgComplexKernel):
Expand All @@ -48,6 +49,7 @@ class Spline36(ZimgComplexKernel):

scale_function = resample_function = core.lazy.resize.Spline36
descale_function = core.lazy.descale.Despline36
_static_kernel_radius = 3


class Spline64(ZimgComplexKernel):
Expand All @@ -61,3 +63,4 @@ class Spline64(ZimgComplexKernel):

scale_function = resample_function = core.lazy.resize.Spline64
descale_function = core.lazy.descale.Despline64
_static_kernel_radius = 4
4 changes: 3 additions & 1 deletion vskernels/kernels/various.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ def __init__(self, b: float = 0.0, c: float = 0.5, radius: int | None = None, **
radius = kwargs.pop('taps', radius)

if radius is None:
radius = 1 if (b, c) == (0, 0) else 2
from .bicubic import Bicubic

radius = Bicubic(b, c).kernel_radius

super().__init__(radius, b, c, **kwargs)

Expand Down

0 comments on commit d467b80

Please sign in to comment.