View difference between Paste ID: z11d5qzv and NEyPaAig
SHOW: | | - or go back to the newest paste.
1
################################################################################################
2
###                                                                                          ###
3
###                       LimitedSharpenFaster MOD : function LSFmod()                       ###
4
###                                                                                          ###
5
###                                Modded Version by LaTo INV.                               ###
6
###                                                                                          ###
7
###                                  v1.9 - 05 October 2009                                  ###
8
###                                                                                          ###
9
###                            another Modded Version by A.SONY.                             ###
10
###                                                                                          ###
11-
###                                   v2.16 - 20 July 2019                                   ###
11+
###                                    v2.17 - 30 Aug 2019                                   ###
12
###                                                                                          ###
13
################################################################################################
14
###
15
### +-----------+
16
### | CHANGELOG |
17
### +-----------+
18
###
19
### v2.17 : - fix upscale, i.e. dest_x or dest_y are larger than the input clip
20
###
21
### v2.12 : - HBD in avs+
22
###        - some fixes
23
###
24
### v1.9 : - tweaked settings
25
###        - default preset is now defaults="fast" /!\
26
###
27
### v1.8 : - changed preblur to allow more tweaking (bool->string)
28
###        - tweaked settings
29
###        - cleaned the code
30
###        - updated documentation
31
###
32
### v1.7 : - changed Smethod=4 to "source"
33
###
34
### v1.6 : - added preblur option
35
###        - added new Smethod=4
36
###
37
### v1.5 : - fixed LUT expression (thanks to Didée)
38
###        - changed Smethod to Smethod+secure
39
###
40
### v1.4 : - changed defaults="new" to defaults="slow" & defaults="fast"
41
###        - added show parameter
42
###        - cleaned a little the code
43
###
44
### v1.3 : - changed a little Smethod=3&5 (same effect, but more precise)
45
###        - added new calculation for soft (soft=-2) [default on]
46
###        - added warning about bad settings (no more silent)
47
###        - updated the documentation
48
###
49
### v1.2 : - added new Lmode<0 (limit with repair)
50
###        - added 2 new Smode (unsharp masking)
51
###        - changed Smode order: now old Smode3-4 is new Smode3-4 to avoid mistake
52
###
53
### v1.1 : - fixed a bug with dest_x!=ox or dest_y!=oy
54
###        - replaced Lfactor by over/undershoot2
55
###
56
### v1.0 : - deleted old Smode(1-4), added new Smode(1-3) & Smethod(1-5)
57
###        - added parameters for nonlinear sharpening (S2zp,S2pwr,S2dmpLo,S2dmpHi)
58
###        - corrected the nonlinear formula
59
###        - added new Lmode 2 & 4 + fixed Lmode 0
60
###        - added faster edgemask
61
###        - added soothe temporal stabilization, 2 parameters: soothe & keep
62
###        - replaced lanczosresize by spline36resize
63
###        - moved "strength" parameter (first place)
64
###        - deleted wide, special and exborder
65
###        - changed some code (cosmetic)
66
###        - added "defaults" parameter (to switch between original and modded version)
67
###        - added documentation
68
###
69
###
70
###
71
### +--------------+
72
### | DEPENDENCIES |
73
### +--------------+
74
###
75
### -> Masktools    (v2.2.15 or up)
76
### -> Removegrain  (v1.0pre or up)
77
### -> Warpsharp    (2003.11.03 or up) [for Smode=1]
78
### -> Variableblur (v0.30 or up)      [for Smode=2]
79
### -> if you use avs+ then make sure it's it the last one
80
###
81
###
82
###
83
###
84
### +-------+
85
### | USAGE |
86
### +-------+
87
###
88
### LSFmod( strength, Smode, Smethod, kernel, 
89
###         preblur, secure, source,
90
###         Szrp, Spwr, SdmpLo, SdmpHi,
91
###         Lmode, overshoot, undershoot, overshoot2, undershoot2,
92
###         soft, soothe, keep,
93
###         edgemode, edgemaskHQ,
94
###         ss_x, ss_y, dest_x, dest_y,
95
###         show, screenW, screenH,
96
###         defaults )
97
###
98
###
99
###
100
### +---------+
101
### | GENERAL |
102
### +---------+
103
###
104
### strength [int]
105
### --------------
106
### Strength of the sharpening
107
###
108
### Smode [int: 1,2,3,4,5]
109
### ----------------------
110
### Sharpen mode:
111
###    =1 : Unsharp masking (from warpsharp)
112
###    =2 : Unsharp masking (from variableblur) 
113
###    =3 : Range sharpening
114
###    =4 : Nonlinear sharpening (original  version)
115
###    =5 : Nonlinear sharpening (corrected version)
116
###
117
### Smethod [int: 1,2,3]
118
### --------------------
119
### Sharpen method: (only used in Smode=3,4,5)
120
###    =1 : 3x3 kernel
121
###    =2 : Min/Max
122
###    =3 : Min/Max + 3x3 kernel
123
###
124
### kernel [int: 11,12,19,20]
125
### -------------------------
126
### Kernel used in Smethod=1&3
127
### In strength order: + 19 > 12 >> 20 > 11 -
128
###
129
###
130
###
131
### +---------+
132
### | SPECIAL |
133
### +---------+
134
###
135
### preblur [string: "ON","OFF",...]
136
### --------------------------------
137
### Mode to avoid noise sharpening & ringing (only used in Smode=3,4,5)
138
### "ON" is sufficient to prevent ringing, but to prevent noise sharpening you should set your own denoiser
139
###    Usage:   LSFmod(preblur="YourFavoriteDenoiser()")
140
###    Example: LSFmod(preblur="FFT3Dfilter(sigma=4,plane=0)")
141
###
142
###
143
### secure [bool]
144
### -------------
145
### Mode to avoid banding & oil painting (or face wax) effect of sharpening
146
### (only used in Smode=3,4,5)
147
###
148
### source [clip]
149
### -------------
150
### If source is defined, LSFmod doesn't sharp more a denoised clip than this source clip
151
### In this mode, you can safely set Lmode=0 & PP=off
152
###    Usage:   denoised.LSFmod(source=source)
153
###    Example: last.FFT3Dfilter().LSFmod(source=last,Lmode=0,soft=0)
154
###
155
###
156
###
157
### +----------------------+
158
### | NONLINEAR SHARPENING |
159
### +----------------------+
160
###
161
### Szrp [int]
162
### ----------
163
### Zero Point:
164
###    - differences below Szrp are amplified (overdrive sharpening)
165
###    - differences above Szrp are reduced   (reduced sharpening)
166
###
167
### Spwr [int]
168
### ----------
169
### Power: exponent for sharpener
170
###
171
### SdmpLo [int]
172
### ------------
173
### Damp Low: reduce sharpening for small changes [0:disable]
174
###
175
### SdmpHi [int]
176
### ------------
177
### Damp High: reduce sharpening for big changes [0:disable]
178
###
179
###
180
###
181
### +----------+
182
### | LIMITING |
183
### +----------+
184
###
185
### Lmode [int: ...,0,1,2,3,4]
186
### --------------------------
187
### Limit mode:
188
###    <0 : Limit with repair (ex: Lmode=-1 --> repair(1), Lmode=-5 --> repair(5)...)
189
###    =0 : No limit
190
###    =1 : Limit to over/undershoot
191
###    =2 : Limit to over/undershoot on edges and no limit on not-edges
192
###    =3 : Limit to zero on edges and to over/undershoot on not-edges
193
###    =4 : Limit to over/undershoot on edges and to over/undershoot2 on not-edges
194
###
195
### overshoot [int]
196
### ---------------
197
### Limit for pixels that get brighter during sharpening
198
###
199
### undershoot [int]
200
### ----------------
201
### Limit for pixels that get darker during sharpening
202
###
203
### overshoot2 [int]
204
### ----------------
205
### Same as overshoot, only for Lmode=4
206
###
207
### undershoot2 [int]
208
### -----------------
209
### Same as undershoot, only for Lmode=4
210
###
211
###
212
###
213
### +-----------------+
214
### | POST-PROCESSING |
215
### +-----------------+
216
###
217
### soft [int: -2,-1,0...100]
218
### -------------------------
219
### Soft the sharpening effect (-1 = old autocalculate, -2 = new autocalculate)
220
###
221
### soothe [bool]
222
### -------------
223
###    =true  : Enable soothe temporal stabilization
224
###    =false : Disable soothe temporal stabilization
225
###
226
### keep [int: 0...100]
227
### -------------------
228
### Minimum percent of the original sharpening to keep (only with soothe=true)
229
###
230
###
231
###
232
### +-------+
233
### | EDGES |
234
### +-------+
235
###
236
### edgemode [int: -1,0,1,2]
237
### ------------------------
238
###    =-1 : Show edgemask
239
###    = 0 : Sharpening all
240
###    = 1 : Sharpening only edges
241
###    = 2 : Sharpening only not-edges
242
###
243
### edgemaskHQ [bool]
244
### -----------------
245
###    =true  : Original edgemask
246
###    =false : Faster edgemask
247
###
248
###
249
###
250
### +------------+
251
### | UPSAMPLING |
252
### +------------+
253
###
254
### ss_x ; ss_y [float]
255
### -------------------
256
### Supersampling factor (reduce aliasing on edges)
257
###
258
### dest_x ; dest_y [int]
259
### ---------------------
260
### Output resolution after sharpening (avoid a resizing step)
261
###
262
###
263
###
264
### +-------+
265
### | DEBUG |
266
### +-------+
267
###
268
### show [bool]
269
### -----------
270
### Show debug clip & informations
271
###
272
### screenW ; screenH [int]
273
### -----------------------
274
### Screen resolution (for show clip)
275
###
276
###
277
###
278
### +----------+
279
### | SETTINGS |
280
### +----------+
281
###
282
### defaults [string: "old" or "slow" or "fast"]
283
### --------------------------------------------
284
###    = "old"  : Reset settings to original version (output will be THE SAME AS LSF)
285
###    = "slow" : Enable SLOW modded version settings
286
###    = "fast" : Enable FAST modded version settings
287
###  --> /!\ [default:"fast"]
288
###
289
###
290
### defaults="old" :  - strength    = 100
291
### ----------------  - Smode       = 3
292
###                   - Smethod     = Smode==3?2:1
293
###                   - kernel      = 11
294
###
295
###                   - preblur     = "OFF"
296
###                   - secure      = false
297
###                   - source      = undefined
298
###
299
###                   - Szrp        = 16
300
###                   - Spwr        = 2
301
###                   - SdmpLo      = strength/25
302
###                   - SdmpHi      = 0
303
###
304
###                   - Lmode       = 1
305
###                   - overshoot   = 1
306
###                   - undershoot  = overshoot
307
###                   - overshoot2  = overshoot*2
308
###                   - undershoot2 = overshoot2
309
###
310
###                   - soft        = 0
311
###                   - soothe      = false
312
###                   - keep        = 25
313
###
314
###                   - edgemode    = 0
315
###                   - edgemaskHQ  = true
316
###
317
###                   - ss_x        = Smode==3?1.50:1.25
318
###                   - ss_y        = ss_x
319
###                   - dest_x      = ox
320
###                   - dest_y      = oy
321
###
322
###                   - show        = false
323
###                   - screenW     = 1280
324
###                   - screenH     = 1024
325
###
326
###
327
### defaults="slow" : - strength    = 100
328
### ----------------- - Smode       = 5
329
###                   - Smethod     = 3
330
###                   - kernel      = 11
331
###
332
###                   - preblur     = "OFF"
333
###                   - secure      = true
334
###                   - source      = undefined
335
###
336
###                   - Szrp        = 16
337
###                   - Spwr        = 4
338
###                   - SdmpLo      = 4
339
###                   - SdmpHi      = 48
340
###
341
###                   - Lmode       = 4
342
###                   - overshoot   = strength/100
343
###                   - undershoot  = overshoot
344
###                   - overshoot2  = overshoot*2
345
###                   - undershoot2 = overshoot2
346
###
347
###                   - soft        = -2
348
###                   - soothe      = true
349
###                   - keep        = 20
350
###
351
###                   - edgemode    = 0
352
###                   - edgemaskHQ  = true
353
###
354
###                   - ss_x        = 1.50
355
###                   - ss_y        = ss_x
356
###                   - dest_x      = ox
357
###                   - dest_y      = oy
358
###
359
###                   - show        = false
360
###                   - screenW     = 1280
361
###                   - screenH     = 1024
362
###
363
###
364
### defaults="fast" : - strength    = 100
365
### ----------------- - Smode       = 3
366
###                   - Smethod     = 2
367
###                   - kernel      = 11
368
###
369
###                   - preblur     = "OFF"
370
###                   - secure      = true
371
###                   - source      = undefined
372
###
373
###                   - Szrp        = 16
374
###                   - Spwr        = 4
375
###                   - SdmpLo      = 4
376
###                   - SdmpHi      = 48
377
###
378
###                   - Lmode       = 1
379
###                   - overshoot   = strength/100
380
###                   - undershoot  = overshoot
381
###                   - overshoot2  = overshoot*2
382
###                   - undershoot2 = overshoot2
383
###
384
###                   - soft        = 0
385
###                   - soothe      = true
386
###                   - keep        = 20
387
###
388
###                   - edgemode    = 0
389
###                   - edgemaskHQ  = false
390
###
391
###                   - ss_x        = 1.25
392
###                   - ss_y        = ss_x
393
###                   - dest_x      = ox
394
###                   - dest_y      = oy
395
###
396
###                   - show        = false
397
###                   - screenW     = 1280
398
###                   - screenH     = 1024
399
###
400
################################################################################################
401
402
function LSFmod( clip inclip, float "strength", int "Smode", int "Smethod", int "kernel",
403
\                string "preblur", bool "secure", clip "source",
404
\                int "Szrp", int "Spwr", float "SdmpLo", float "SdmpHi",
405
\                int "Lmode", int "overshoot", int "undershoot", int "overshoot2", int "undershoot2",
406
\                int "soft", bool "soothe", int "keep",
407
\                int "edgemode", bool "edgemaskHQ",
408
\                float "ss_x", float "ss_y", int "dest_x", int "dest_y",
409
\                bool "show", int "screenW", int "screenH",
410
\                string "defaults" )
411
{
412
413
sisphbd = AvsPlusVersionNumber > 2294
414
415-
input = sislumaonly ? inclip : sisphbd ? inclip.converttoy() : inclip.converttoy8()
415+
416
version  = "v2.17"
417
defaults = default(defaults,"fast")
418
num      = defaults=="old" ? 0 : defaults=="slow" ? 1 : defaults=="fast" ? 2 : 3
419
420
ox = inclip.width
421
oy = inclip.height
422
423
Assert(num         != 3      ? true : false, chr(10) + """Defaults must be "old" or "slow" or "fast" !""" + chr(10))
424-
version  = "v2.1"
424+
425
strength    = default( strength,    Select(num,                100 ,                100 ,                100 ) )
426
Smode       = default( Smode,       Select(num,                  3 ,                  5 ,                  3 ) )
427
Smethod     = default( Smethod,     Select(num,       Smode==3?2:1 ,                  3 ,                  2 ) )
428-
ox = input.width
428+
429-
oy = input.height
429+
430
preblur     = default( preblur,     Select(num,              "OFF" ,              "OFF" ,              "OFF" ) )
431
secure      = default( secure,      Select(num,              false ,               true ,               true ) )
432
433
Szrp        = default( Szrp,        Select(num,                 16 ,                 16 ,                 16 ) )
434
Spwr        = default( Spwr,        Select(num,                  2 ,                  4 ,                  4 ) )
435
SdmpLo      = default( SdmpLo,      Select(num,        strength/25 ,                  4 ,                  4 ) )
436
SdmpHi      = default( SdmpHi,      Select(num,                  0 ,                 48 ,                 48 ) )
437
438
Lmode       = default( Lmode,       Select(num,                  1 ,                  4 ,                  1 ) )
439
overshoot   = default( overshoot,   Select(num,                  1 ,       strength/100 ,       strength/100 ) )
440
undershoot  = default( undershoot,  Select(num,          overshoot ,          overshoot ,          overshoot ) )
441
overshoot2  = default( overshoot2,  Select(num,        overshoot*2 ,        overshoot*2 ,        overshoot*2 ) )
442
undershoot2 = default( undershoot2, Select(num,         overshoot2 ,         overshoot2 ,         overshoot2 ) )
443
444
soft        = default( soft,        Select(num,                  0 ,                 -2 ,                  0 ) )
445
soothe      = default( soothe,      Select(num,              false ,               true ,               true ) )
446
keep        = default( keep,        Select(num,                 25 ,                 20 ,                 20 ) )
447
448
edgemode    = default( edgemode,    Select(num,                  0 ,                  0 ,                  0 ) )
449
edgemaskHQ  = default( edgemaskHQ,  Select(num,               true ,               true ,              false ) )
450
451
ss_x        = default( ss_x,        Select(num, Smode==3?1.50:1.25 ,               1.50 ,               1.25 ) )
452
ss_y        = default( ss_y,        Select(num,               ss_x ,               ss_x ,               ss_x ) )
453
dest_x      = default( dest_x,      Select(num,                 ox ,                 ox ,                 ox ) )
454
dest_y      = default( dest_y,      Select(num,                 oy ,                 oy ,                 oy ) )
455
456
show        = default( show,        Select(num,              false ,              false ,              false ) )
457
screenW     = default( screenW,     Select(num,               1280 ,               1280 ,               1280 ) )
458
screenH     = default( screenH,     Select(num,               1024 ,               1024 ,               1024 ) )
459
460
Assert( ( strength >= 0 )                                                ? true : false, chr(10) + "'strength' have not a correct value! [>=0]" + chr(10))
461
Assert( ( Smode >= 1 && Smode <= 5 )                                     ? true : false, chr(10) + "'Smode' have not a correct value! [1,2,3,4,5]" + chr(10))
462
Assert( ( Smethod >= 1 && Smethod <= 4 )                                 ? true : false, chr(10) + "'Smethod' have not a correct value! [1,2,3,4]" + chr(10))
463
Assert( ( kernel == 19 || kernel == 20 || kernel == 11 || kernel == 12 ) ? true : false, chr(10) + "'kernel' have not a correct value! [19,20,11,12]" + chr(10))
464
Assert( ( Szrp >= 1 && Szrp <= 255 )                                     ? true : false, chr(10) + "'Szrp' have not a correct value! [1...255]" + chr(10))
465
Assert( ( Spwr >= 1 )                                                    ? true : false, chr(10) + "'Spwr' have not a correct value! [>=1]" + chr(10))
466
Assert( ( SdmpLo >= 0 && SdmpLo <= 255 )                                 ? true : false, chr(10) + "'SdmpLo' have not a correct value! [0...255]" + chr(10))
467
Assert( ( SdmpHi >= 0 && SdmpHi <= 255 )                                 ? true : false, chr(10) + "'SdmpHi' have not a correct value! [0...255]" + chr(10))
468
Assert( ( Lmode <= 4 )                                                   ? true : false, chr(10) + "'Lmode' have not a correct value! [...-1,0,1,2,3,4]" + chr(10))
469
Assert( ( overshoot >= 0 && overshoot <= 255 )                           ? true : false, chr(10) + "'overshoot' have not a correct value! [0...255]" + chr(10))
470
Assert( ( undershoot >= 0 && undershoot <= 255 )                         ? true : false, chr(10) + "'undershoot' have not a correct value! [0...255]" + chr(10))
471
Assert( ( overshoot2 >= 0 && overshoot2 <= 255 )                         ? true : false, chr(10) + "'overshoot2' have not a correct value! [0...255]" + chr(10))
472
Assert( ( undershoot2 >= 0 && undershoot2 <= 255 )                       ? true : false, chr(10) + "'undershoot2' have not a correct value! [0...255]" + chr(10))
473
Assert( ( soft >= -2 && soft <= 100 )                                    ? true : false, chr(10) + "'soft' have not a correct value! [-2,-1,0,1...100]" + chr(10))
474
Assert( ( keep >= 0 && keep <= 100 )                                     ? true : false, chr(10) + "'keep' have not a correct value! [0...100]" + chr(10))
475
Assert( ( edgemode >= -1 && edgemode <= 2 )                              ? true : false, chr(10) + "'edgemode' have not a correct value! [-1,0,1,2]" + chr(10))
476
Assert( ( ss_x >= 1.0 )                                                  ? true : false, chr(10) + "'ss_x' have not a correct value! [>=1.0]" + chr(10))
477
Assert( ( ss_y >= 1.0 )                                                  ? true : false, chr(10) + "'ss_y' have not a correct value! [>=1.0]" + chr(10))
478
479
soft = soft!=-1  ? soft : sqrt( (((ss_x+ss_y)/2.0-1.0)*100.0) ) * 10
480
soft = soft!=-2  ? soft : int( (1.0+(2.0/(ss_x+ss_y))) * sqrt(strength) )
481
soft = soft<=100 ? soft : 100
482
483
xxs  = round(ox*ss_x/8)*8
484
yys  = round(oy*ss_y/8)*8
485
486
str  = float(strength)/100.0
487
488
sislumaonly = sisphbd ? inclip.isy() : VersionNumber() < 2.6 ? true : inclip.isy8()
489
 
490
input = (dest_x != ox || dest_y != oy) || sislumaonly ? inclip : sisphbd ? inclip.converttoy() : inclip.converttoy8()
491
492
input = defined(source) ? input.pointresize(width(input),height(input)+8,0,-4,width(input),height(input)+8.0001) : input
493
494
source = defined(source) ? sislumaonly ? source : sisphbd ? source.converttoy() : source.converttoy8() : source
495
496
source = defined(source) ? source.pointresize(width(source),height(source)+8,0,-4,width(source),height(source)+8.0001) : source
497
498
Assert(!isYUY2(input), chr(10) + "not work with an YUY2 clip !" + chr(10))
499
500
### SHARP
501
tmp  = ss_x > 1.0 || ss_y > 1.0 ? input.spline36resize(xxs,yys) : input
502
pre  = preblur=="OFF" ? tmp
503
\    : preblur=="ON"  ? VersionNumber() < 2.6 ? tmp.mt_makediff( mt_lutxy(mt_makediff(tmp,tmp.removegrain(11,-1),U=1,V=1)
504
\                                       			,mt_makediff(tmp,tmp.removegrain(4,-1),U=1,V=1)
505
\                                       			,"x 128 - y 128 - * 0 < 128 x 128 - abs y 128 - abs < x y ? ?",U=1,V=1) ,U=1,V=1) \
506
					      : tmp.mt_makediff( mt_lutxy(mt_makediff(tmp,tmp.removegrain(11,-1),U=1,V=1)
507
\                                       			,mt_makediff(tmp,tmp.removegrain(4,-1),U=1,V=1)
508
\                                       			,"x range_half - y range_half - * 0 < range_half x range_half - abs y range_half - abs < x y ? ?",use_expr=2,U=1,V=1) ,U=1,V=1)
509
\    : eval("tmp."+preblur)
510
511
dark_limit   = pre.mt_inpand(U=1,V=1)
512
bright_limit = pre.mt_expand(U=1,V=1)
513
minmaxavg    = mt_average(dark_limit,bright_limit,U=1,V=1)
514
515
method = Smethod==1   ? pre.removegrain(kernel,-1)
516
\      : Smethod==2   ? minmaxavg
517
\      :                minmaxavg.removegrain(kernel,-1)
518
519
method = secure==true ? VersionNumber() < 2.6 ? method.mt_lutxy(pre,"x y < x 1 + x y > x 1 - x ? ?",U=1,V=1) \
520
					      : method.mt_lutxy(pre,"x y < x 1 scalef + x y > x 1 scalef - x ? ?",use_expr=2,U=1,V=1)
521
\      :                method
522
523
method = preblur=="OFF" ? method : tmp.mt_makediff(mt_makediff(pre,method,U=1,V=1),U=1,V=1)
524
525
normsharp = Smode==1 ? tmp.unsharpmask(strength=int(str*100),radius=round(1.0+((ss_x+ss_y)/2.0)),threshold=0)
526
\         : Smode==2 ? tmp.unsharp(strength=str,vary=((ss_x+ss_y)/2.0),varc=1,border=3,U=1,V=1)
527
\         : Smode==3 ? VersionNumber() < 2.6 ? mt_lutxy(tmp, method, yexpr="x x y - "+string(str)+" * +",U=1,V=1) \
528
					     : mt_lutxy(tmp, method, yexpr="x x y - "+string(str)+" * +",use_expr=2,clamp_float=true,U=1,V=1)
529
\         : Smode==4 ? VersionNumber() < 2.6 ? mt_lutxy(tmp, method, yexpr="x y == x x x y - abs "+string(Szrp)+" / 1 "+string(Spwr)+" / ^ "+string(Szrp)+" * "+string(str)+" * x y - 2 ^ x y - 2 ^ "
530
\                                                   			    +string(SdmpLo)+" + / * x y - x y - abs / * 1 "+string(SdmpHi)+" 0 == 0 x y - abs "+string(SdmpHi)+" / 4 ^ ? + / + ?",U=1,V=1) \
531
					     : mt_lutxy(tmp, method, yexpr="x y == x x x y - abs "+string(Szrp) +" scalef / 1 "+string(Spwr)+" / ^ "+string(Szrp) +" scalef * "+string(str)+" * x y - 2 ^ x y - 2 ^ "
532
\                                                   			    +string(SdmpLo)+" scalef scalef + / * x y - x y - abs / * 1 "+string(SdmpHi)+" scalef 0 == 0 x y - abs "+string(SdmpHi)+" scalef / 4 ^ ? + / + ?",use_expr=2,U=1,V=1)
533
\         :            VersionNumber() < 2.6 ? mt_lutxy(tmp, method, yexpr="x y == x x x y - abs "+string(Szrp)+" / 1 "+string(Spwr)+" / ^ "+string(Szrp)+" * "+string(str)+" * x y - x y - abs / * x y - 2 ^ "
534
\                                                   			    +string(Szrp)+" 2 ^ "+string(SdmpLo)+" + * x y - 2 ^ "+string(SdmpLo)+" + "+string(Szrp)+" 2 ^ * / * 1 "
535
\                                                   			    +string(SdmpHi)+" 0 == 0 "+string(Szrp)+" "+string(SdmpHi)+" / 4 ^ ? + 1 "+string(SdmpHi)+" 0 == 0 x y - abs "+string(SdmpHi)+" / 4 ^ ? + / * + ?",U=1,V=1) \
536
					     : mt_lutxy(tmp, method, yexpr="x y == x x x y - abs "+string(Szrp)+" / 1 "+string(Spwr)+" / ^ "+string(Szrp)+" * "+string(str)+" * x y - x y - abs / * x y - 2 ^ "
537
\                                                   			    +string(Szrp)+" 2 ^ "+string(SdmpLo)+" + * x y - 2 ^ "+string(SdmpLo)+" + "+string(Szrp)+" 2 ^ * / * 1 "
538
\                                                   			    +string(SdmpHi)+" 0 == 0 "+string(Szrp)+" "+string(SdmpHi)+" / 4 ^ ? + 1 "+string(SdmpHi)+" 0 == 0 x y - abs "+string(SdmpHi)+" / 4 ^ ? + / * + ?",U=1,V=1,use_expr=2,scale_inputs="allf")
539
540
541
### LIMIT
542
normal = mt_clamp(normsharp, bright_limit, dark_limit, overshoot,  undershoot,  U=1, V=1)
543
second = mt_clamp(normsharp, bright_limit, dark_limit, overshoot2, undershoot2, U=1, V=1)
544
zero   = mt_clamp(normsharp, bright_limit, dark_limit, 0,          0,           U=1, V=1)
545
546
edge  = edgemaskHQ ? mt_logic( tmp.mt_edge(thY1=0,thY2=255,"8 16 8 0 0 0 -8 -16 -8 4",U=1,V=1)
547
\                             ,tmp.mt_edge(thY1=0,thY2=255,"8 0 -8 16 0 -16 8 0 -8 4",U=1,V=1)
548
\                             ,"max",U=1,V=1)
549
\     :              tmp.mt_edge(thY1=0,thY2=255,mode="min/max",U=1,V=1)
550
551
edge  = edgemaskHQ ? VersionNumber() < 2.6 ? edge.mt_lut("x 128 / 0.86 ^ 255 *",U=1,V=1) \
552
					   : edge.mt_lut("x range_half / 0.86 ^ range_max *",use_expr=2,scale_inputs="floatf",U=1,V=1)
553
\     :              VersionNumber() < 2.6 ? edge.mt_lut("x 32 / 0.86 ^ range_max *",U=1,V=1) \
554
					   : edge.mt_lut("x 32 scalef / 0.86 ^ range_max *",use_expr=2,scale_inputs="floatf",U=1,V=1)
555
556
limit1 = Lmode<0  ? normsharp.repair(tmp,abs(Lmode),-1)
557
\      : Lmode==0 ? normsharp
558
\      : Lmode==1 ? normal
559
\      : Lmode==2 ? mt_merge(normsharp, normal, edge.mt_inflate(U=1,V=1), U=1, V=1)
560
\      : Lmode==3 ? mt_merge(normal,    zero,   edge.mt_inflate(U=1,V=1), U=1, V=1) 
561
\      :            mt_merge(second,    normal, edge.mt_inflate(U=1,V=1), U=1, V=1)
562
563
limit2 = edgemode==0 ? limit1
564
\      : edgemode==1 ? mt_merge(tmp,limit1,edge.mt_inflate(U=1,V=1).mt_inflate(U=1,V=1).removegrain(11,-1),U=1,V=1)
565
\      :               mt_merge(limit1,tmp,edge.mt_inflate(U=1,V=1).mt_inflate(U=1,V=1).removegrain(11,-1),U=1,V=1)
566
567
568
### SOFT
569
sharpdiff = mt_makediff(tmp,limit2,U=1,V=1)
570
sharpdiff = VersionNumber() < 2.6 ? mt_lutxy(sharpdiff,sharpdiff.removegrain(19,-1),
571
\                    			     "x 128 - abs y 128 - abs > y "+string(soft)+" * x "+string(100-soft)+" * + 100 / x ?",U=1,V=1) \
572
				  : mt_lutxy(sharpdiff,sharpdiff.removegrain(19,-1),
573
\                    			     "x range_half - abs y range_half - abs > y "+string(soft)+" * x "+string(100-soft)+" * + 100 / x ?",use_expr=2,clamp_float=true,U=1,V=1)
574
575
PP1 = soft==0 ? limit2 : mt_makediff(tmp,sharpdiff,U=1,V=1)
576
577
578
### SOOTHE
579
diff  = mt_makediff(tmp,PP1,U=1,V=1)
580
diff2 = diff.temporalsoften(1,255,0,32,2)
581
diff3 = VersionNumber() < 2.6 ? mt_lutxy(diff,diff2,"x 128 - y 128 - * 0 < x 128 - 100 / "+string(keep)+
582
\                           			    " * 128 + x 128 - abs y 128 - abs > x "+string(keep)+" * y 100 "+string(keep)+" - * + 100 / x ? ?",U=1,V=1) \
583
			      : mt_lutxy(diff,diff2,"x range_half - y range_half - * 0 < x range_half - 100 / "+string(keep)+
584
\                           			    " * range_half + x range_half - abs y range_half - abs > x "+string(keep)+" * y 100 "+string(keep)+" - * + 100 / x ? ?",use_expr=2,clamp_float=true,U=1,V=1)
585
586
PP2 = soothe ? mt_makediff(tmp,diff3,U=1,V=1) : PP1
587
588
589
### OUTPUT
590
out = (dest_x != ox || dest_y != oy) ? tmp.mergeluma(PP2).spline36resize(dest_x,dest_y)
591
\   : (ss_x > 1.0 || ss_y > 1.0)     ? input.mergeluma(PP2.spline36resize(dest_x,dest_y))
592
\   :                                  input.mergeluma(PP2)
593
594
src = defined(source)==false         ? blankclip
595
\   : (dest_x != ox || dest_y != oy) ? source.spline36resize(dest_x,dest_y)
596
\   :                                  source
597
in  = (dest_x != ox || dest_y != oy) ? input.spline36resize(dest_x,dest_y)
598
\   :                                  input
599
600
shrpD  = mt_makediff(in,out,U=1,V=1)
601
shrpL  = VersionNumber() < 2.6 ? shrpD.repair(mt_makediff(in,src,U=1,V=1),1,-1,-1).mt_lutxy(shrpD,"x 128 - abs y 128 - abs < x y ?",U=1,V=1) \
602
			       : shrpD.repair(mt_makediff(in,src,U=1,V=1),1,-1,-1).mt_lutxy(shrpD,"x range_half - abs y range_half - abs < x y ?",use_expr=1,U=1,V=1)
603
output = defined(source) ? mt_makediff(in,shrpL,U=2,V=2)
604
\      :                   out
605
606
output = edgemode!=-1    ? output
607
\      :                   edge.spline36resize(dest_x,dest_y).greyscale()
608
609
610
### SHOW
611
   function LSFM_Show(clip input, clip output, string "version", string "defaults",
612
   \                  float "strength", int "Smode", int "Smethod", int "kernel",
613
   \                  string "preblur", bool "secure", string "source",
614
   \                  int "Szrp", int "Spwr", float "SdmpLo", float "SdmpHi",
615
   \                  int "Lmode", int "overshoot", int "undershoot", int "overshoot2", int "undershoot2",
616
   \                  int "soft", bool "soothe", int "keep",
617
   \                  int "edgemode", bool "edgemaskHQ",
618
   \                  float "ss_x", float "ss_y", int "dest_x", int "dest_y",
619
   \                  bool "show", int "screenW", int "screenH")
620
   {
621
   resoW = screenW - 384
622
   resoH = screenH
623
624
   inW   = input.width()
625
   inH   = input.height()
626
   outW  = round( resoW / 4 ) * 4
627
   outH  = round( (outW * inH) / (4 * inW) ) * 4
628
   scale = resoH - ( 2 * outH )
629
630
   i = input.spline36resize(outW,outH)
631
   i = scale > 0 ? i.addborders(0,scale/4,0,scale/4) : i
632
   i = scale < 0 ? i.crop(0,-scale/4,0,scale/4) : i
633
634
   o = output.spline36resize(outW,outH)
635
   o = scale > 0 ? o.addborders(0,scale/4,0,scale/4) : o
636
   o = scale < 0 ? o.crop(0,-scale/4,0,scale/4) : o
637
638
   e = blankclip(input, width=384, height=resoH)
639
   e = e.SubTitle("LSFmod " + string(version)                ,text_color=$FFFFFF,font="COURIER NEW",size=24,x=40,y=40)
640
   e = e.SubTitle("DEFAULTS      = " + string(defaults)      ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=100)
641
642
   e = e.SubTitle("GENERAL:"                                 ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=150)
643
   e = e.SubTitle("--------"                                 ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=160)
644
   e = e.SubTitle("strength      = " + string(strength)      ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=180)
645
   e = e.SubTitle("Smode         = " + string(Smode)         ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=190)
646
   e = e.SubTitle("Smethod       = " + string(Smethod)       ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=200)
647
   e = e.SubTitle("kernel        = " + string(kernel)        ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=210)
648
649
   e = e.SubTitle("SPECIAL:"                                 ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=250)
650
   e = e.SubTitle("--------"                                 ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=260)
651
   e = e.SubTitle("preblur       = " + string(preblur)       ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=280)
652
   e = e.SubTitle("secure        = " + string(secure)        ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=290)
653
   e = e.SubTitle("source        = " + string(source)        ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=300)
654
655
   e = e.SubTitle("NONLINEAR SHARPENING:"                    ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=340)
656
   e = e.SubTitle("---------------------"                    ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=350)
657
   e = e.SubTitle("Szrp          = " + string(Szrp)          ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=370)
658
   e = e.SubTitle("Spwr          = " + string(Spwr)          ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=380)
659
   e = e.SubTitle("SdmpLo        = " + string(SdmpLo)        ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=390)
660
   e = e.SubTitle("SdmpHi        = " + string(SdmpHi)        ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=400)
661
662
   e = e.SubTitle("LIMITING:"                                ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=440)
663
   e = e.SubTitle("---------"                                ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=450)
664
   e = e.SubTitle("Lmode         = " + string(Lmode)         ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=470)
665
   e = e.SubTitle("overshoot     = " + string(overshoot)     ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=480)
666
   e = e.SubTitle("undershoot    = " + string(undershoot)    ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=490)
667
   e = e.SubTitle("overshoot2    = " + string(overshoot2)    ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=500)
668
   e = e.SubTitle("undershoot2   = " + string(undershoot2)   ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=510)
669
670
   e = e.SubTitle("POST-PROCESSING:"                         ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=550)
671
   e = e.SubTitle("----------------"                         ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=560)
672
   e = e.SubTitle("soft          = " + string(soft)          ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=580)
673
   e = e.SubTitle("soothe        = " + string(soothe)        ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=590)
674
   e = e.SubTitle("keep          = " + string(keep)          ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=600)
675
676
   e = e.SubTitle("EDGES:"                                   ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=640)
677
   e = e.SubTitle("------"                                   ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=650)
678
   e = e.SubTitle("edgemode      = " + string(edgemode)      ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=670)
679
   e = e.SubTitle("edgemaskHQ    = " + string(edgemaskHQ)    ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=680)
680
681
   e = e.SubTitle("UPSAMPLING:"                              ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=720)
682
   e = e.SubTitle("-----------"                              ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=730)
683
   e = e.SubTitle("ss_x          = " + string(ss_x)          ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=750)
684
   e = e.SubTitle("ss_y          = " + string(ss_y)          ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=760)
685
   e = e.SubTitle("dest_x        = " + string(dest_x)        ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=770)
686
   e = e.SubTitle("dest_y        = " + string(dest_y)        ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=780)
687
688
   e = e.SubTitle("DEBUG:"                                   ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=820)
689
   e = e.SubTitle("------"                                   ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=830)
690
   e = e.SubTitle("show          = " + string(show)          ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=850)
691
   e = e.SubTitle("screenW       = " + string(screenW)       ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=860)
692
   e = e.SubTitle("screenH       = " + string(screenH)       ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=870)
693
694
   debug = stackhorizontal( e ,
695
   \                        stackvertical( i.subtitle("Input",text_color=$FFFFFF,size=20,x=40,y=20) ,
696
   \                                       o.subtitle("Output",text_color=$FFFFFF,size=20,x=40,y=20) )
697
   \                      )
698
699
   return debug
700
   }
701
702-
output = sislumaonly ? output : sisphbd ? CombinePlanes(output,inclip,planes="YUV",sample_clip=inclip) : ytouv(inclip.utoy8(),inclip.vtoy8(),output)
702+
703
704
output = (dest_x != ox || dest_y != oy) || sislumaonly ? output : sisphbd ? CombinePlanes(output,inclip,planes="YUV",sample_clip=inclip) : ytouv(inclip.utoy8(),inclip.vtoy8(),output)
705
706
output = show==false ? output
707
\                    : LSFM_Show(inclip, output, version, defaults,
708
\                                strength, Smode, Smethod, kernel,
709
\                                preblur, secure, defined(source)?"defined":"undefined",
710
\                                Szrp, Spwr, SdmpLo, SdmpHi,
711
\                                Lmode, overshoot, undershoot, overshoot2, undershoot2,
712
\                                soft, soothe, keep,
713
\                                edgemode, edgemaskHQ,
714
\                                ss_x, ss_y, dest_x, dest_y,
715
\                                show, screenW, screenH)
716
717
return output
718
}