Skip to content

Commit

Permalink
Add kernel_size properties
Browse files Browse the repository at this point in the history
  • Loading branch information
LightArrowsEXE committed Nov 11, 2023
1 parent 0c01f66 commit 198d54f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 8 deletions.
4 changes: 4 additions & 0 deletions vskernels/kernels/bicubic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
14 changes: 14 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,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)
9 changes: 9 additions & 0 deletions vskernels/kernels/resize.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 Any

from vstools import core, vs
Expand All @@ -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):
"""
Expand All @@ -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)
25 changes: 22 additions & 3 deletions vskernels/kernels/spline.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

import re
from math import ceil, isqrt
from typing import Any

from vstools import core
Expand All @@ -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."""

Expand All @@ -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.
Expand All @@ -37,7 +56,7 @@ class Spline16(ZimgComplexKernel):
descale_function = core.lazy.descale.Despline16


class Spline36(ZimgComplexKernel):
class Spline36(ZimgComplexKernel, _SplineKernelSize):
"""
Built-in spline36 resizer.
Expand All @@ -50,7 +69,7 @@ class Spline36(ZimgComplexKernel):
descale_function = core.lazy.descale.Despline36


class Spline64(ZimgComplexKernel):
class Spline64(ZimgComplexKernel, _SplineKernelSize):
"""
Built-in spline64 resizer.
Expand Down
7 changes: 2 additions & 5 deletions vskernels/kernels/various.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 198d54f

Please sign in to comment.