-
Notifications
You must be signed in to change notification settings - Fork 4
/
various.py
163 lines (107 loc) · 4 KB
/
various.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from __future__ import annotations
from math import log, sqrt
from typing import Any
from vstools import CustomValueError, to_singleton
from .fmtconv import FmtConv
from .placebo import Placebo
__all__ = [
'Box',
'BlackMan',
'BlackManMinLobe',
'Sinc',
'Gaussian',
'EwaBicubic',
'EwaJinc',
'EwaLanczos',
'EwaGinseng',
'EwaHann',
'EwaHannSoft',
'EwaRobidoux',
'EwaRobidouxSharp',
]
class Box(FmtConv):
"""fmtconv's box resizer."""
_kernel = 'box'
class BlackMan(FmtConv):
"""fmtconv's blackman resizer."""
_kernel = 'blackman'
class BlackManMinLobe(FmtConv):
"""fmtconv's blackmanminlobe resizer."""
_kernel = 'blackmanminlobe'
class Sinc(FmtConv):
"""fmtconv's sinc resizer."""
_kernel = 'sinc'
class Gaussian(FmtConv):
"""fmtconv's gaussian resizer."""
_kernel = 'gaussian'
def __init__(self, sigma: float = 0.5, taps: int = 2, **kwargs: Any) -> None:
"""
Sigma is imagemagick's sigma scaling.
This will internally be scaled to fmtc's curve.
You can specify "curve" to override sigma and specify the original `a1` value.
"""
if 'curve' in kwargs:
a1 = kwargs.pop('curve')
if a1 is not None:
if a1 < 1.0 or a1 > 100.0:
raise CustomValueError("curve must be in range 1-100! (inclusive)")
else:
a1 = self.sigma.to_fmtc(sigma) # type: ignore
low, up = self.sigma.from_fmtc(100), self.sigma.from_fmtc(1) # type: ignore
if a1 < 1.0 or a1 > 100.0:
raise CustomValueError(f"sigma must be in range {low:.4f}-{up:.4f}! (inclusive)")
super().__init__(taps, a1=a1, **kwargs)
@to_singleton
class sigma:
def from_fmtc(self, curve: float) -> float:
if not curve:
return 0.0
return sqrt(1.0 / (2.0 * (curve / 10.0) * log(2)))
def to_fmtc(self, sigma: float) -> float:
if not sigma:
return 0.0
return 10 / (2 * log(2) * (sigma ** 2))
def from_libplacebo(self, sigma: float) -> float:
if not sigma:
return 0.0
return sqrt(sigma / 4)
def to_libplacebo(self, sigma: float) -> float:
if not sigma:
return 0.0
return 4 * (sigma ** 2)
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:
from .bicubic import Bicubic
radius = Bicubic(b, c).kernel_radius
super().__init__(radius, b, c, **kwargs)
class EwaLanczos(Placebo):
_kernel = 'ewa_lanczos'
def __init__(self, taps: float = 3.2383154841662362076499, **kwargs: Any) -> None:
super().__init__(taps, None, None, **kwargs)
class EwaJinc(Placebo):
_kernel = 'ewa_jinc'
def __init__(self, taps: float = 3.2383154841662362076499, **kwargs: Any) -> None:
super().__init__(taps, None, None, **kwargs)
class EwaGinseng(Placebo):
_kernel = 'ewa_ginseng'
def __init__(self, taps: float = 3.2383154841662362076499, **kwargs: Any) -> None:
super().__init__(taps, None, None, **kwargs)
class EwaHann(Placebo):
_kernel = 'ewa_hann'
def __init__(self, taps: float = 3.2383154841662362076499, **kwargs: Any) -> None:
super().__init__(taps, None, None, **kwargs)
class EwaHannSoft(Placebo):
_kernel = 'haasnsoft'
def __init__(self, taps: float = 3.2383154841662362076499, **kwargs: Any) -> None:
super().__init__(taps, None, None, **kwargs)
class EwaRobidoux(Placebo):
_kernel = 'ewa_robidoux'
def __init__(self, **kwargs: Any) -> None:
super().__init__(None, None, None, **kwargs)
class EwaRobidouxSharp(Placebo):
_kernel = 'ewa_robidouxsharp'
def __init__(self, **kwargs: Any) -> None:
super().__init__(None, None, None, **kwargs)