View difference between Paste ID: ctN9A4gF and E7ffbKLi
SHOW: | | - or go back to the newest paste.
1
# FineDehalo
2-
# 1.1 mod7.1
2+
# 1.1 mod7.2
3
#
4
# This program is free software. It comes without any warranty, to
5
# the extent permitted by applicable law. You can redistribute it
6
# and/or modify it under the terms of the Do What The Fuck You Want
7
# To Public License, Version 2, as published by Sam Hocevar.
8
9
Function FineDehalo (clip src, float "rx", float "ry", int "thmi", int "thma", int "thlimi", int "thlima", float "darkstr", float "brightstr", int "showmask", float "contra", bool "excl", float "edgeproc", val "exdehalo", clip "exedgesm")
10
{
11
	rx        = Default (rx,          2)
12
	ry        = Default (ry,         rx)
13
	thmi      = Default (thmi,       80)
14
	thma      = Default (thma,      128)
15
	thlimi    = Default (thlimi,     50)
16
	thlima    = Default (thlima,    100)
17
	darkstr   = Default (darkstr,   1.0)
18
	brightstr = Default (brightstr, 1.0)
19
	showmask  = Default (showmask,    0)
20
	contra    = Default (contra,    0.0)
21
	excl      = Default (excl,     true)
22
	edgeproc  = Default (edgeproc,  0.0)
23
24
	rx_i = Round (rx)
25
	ry_i = Round (ry)
26
27
	src
28
29
30
	### Dehaloing ###
31
32
	dehaloed = defined(exdehalo) ? isclip(exdehalo) ? exdehalo : eval("last." + exdehalo) : DeHalo_alpha (rx=rx, ry=ry, darkstr=darkstr, brightstr=brightstr)
33
34
	# Contrasharpening
35
	dehaloed =   (contra > 0)
36
\	           ? dehaloed.FineDehalo_contrasharp (src, contra)
37
\	           : dehaloed
38
39
	edgesm = defined(exedgesm) ? exedgesm : FineDehaloedges(rx=rx, ry=ry, thmi=thmi, thma=thma, thlimi=thlimi, thlima=thlima, showmask=showmask, excl=excl, edgeproc=edgeproc)
40
41
	### Masking ###
42
43
		  (showmask != 0) ? edgesm
44
	\	:                   mt_merge (last, dehaloed, edgesm, y=3, u=2, v=2)
45
}
46
47
# level == 1.0 : normal contrasharp
48
Function FineDehalo_contrasharp (clip dehaloed, clip src, float level)
49
{
50
	bb  = dehaloed.RemoveGrain (11, -1)
51
	bb2 = bb.Repair (bb.Repair (bb.Medianblur (2, -256, -256), 1), 1)
52
	xd  = mt_makediff (bb, bb2)
53
	xd  = xd.mt_lut ("x 128 - 2.49 * "+String(level)+" * 128 +")
54
	xdd = mt_lutxy (
55
\		xd,
56
\		mt_makediff (src, dehaloed),
57
\		"x 128 - y 128 - * 0 < 128 x 128 - abs y 128 - abs < x y ? ?"
58
\	)
59
60
	dehaloed.mt_adddiff (xdd, y=3, u=2, v=2)
61
}
62
63
# moved FineDehalo edges mask in stand-alone Function
64
Function FineDehaloedges (clip src, float "rx", float "ry", int "thmi", int "thma", int "thlimi", int "thlima", int "showmask", bool "excl", float "edgeproc")
65
{
66
	rx        = Default (rx,          2)
67
	ry        = Default (ry,         rx)
68
	thmi      = Default (thmi,       80)
69
	thma      = Default (thma,      128)
70
	thlimi    = Default (thlimi,     50)
71
	thlima    = Default (thlima,    100)
72
	showmask  = Default (showmask,    0)
73
	excl      = Default (excl,     true)
74
	edgeproc  = Default (edgeproc,  0.0)
75
76
	rx_i = Round (rx)
77
	ry_i = Round (ry)
78
79
	src
80
81
	### Main edges ###
82
83
	# Basic edge detection, thresholding will be applied later.
84
	edges = mt_edge (mode="prewitt", thY1=0, thY2=255)
85
86
	# Keeps only the sharpest edges (line edges)
87
	strong = edges.mt_lut (expr="x "+String(thmi)+" - "+String(thma-thmi)+" / 255 *")
88
89
	# Extends them to include the potential halos
90
	large = strong.mt_expand_multi (sw=rx_i, sh=ry_i)
91
92
93
	### Exclusion zones ###
94
95
	# When two edges are close from each other (both edges of a single
96
	# line or multiple parallel color bands), the halo removal
97
	# oversmoothes them or makes seriously bleed the bands, producing
98
	# annoying artifacts. Therefore we have to produce a mask to exclude
99
	# these zones from the halo removal.
100
101
	# Includes more edges than previously, but ignores simple details
102
	light = edges.mt_lut (expr="x "+String(thlimi)+" - "+String(thlima-thlimi)+" / 255 *")
103
104
	# To build the exclusion zone, we make grow the edge mask, then shrink
105
	# it to its original shape. During the growing stage, close adjacent
106
	# edge masks will join and merge, forming a solid area, which will
107
	# remain solid even after the shrinking stage.
108
109
	# Mask growing
110
	shrink = light.mt_expand_multi (sw=rx_i, sh=ry_i, mode="ellipse")
111
112
	# At this point, because the mask was made of a shades of grey, we may
113
	# end up with large areas of dark grey after shrinking. To avoid this,
114
	# we amplify and saturate the mask here (actually we could even
115
	# binarize it).
116
	shrink = shrink.mt_lut ("x 4 *")
117
118
	# Mask shrinking
119
	shrink = shrink.mt_inpand_multi (sw=rx_i, sh=ry_i, mode="ellipse")
120
121
	# This mask is almost binary, which will produce distinct
122
	# discontinuities once applied. Then we have to smooth it.
123
	shrink = shrink.RemoveGrain (20, -1)
124
	shrink = shrink.RemoveGrain (20, -1)
125
126
127
	### Final mask building ###
128
129
	# Previous mask may be a bit weak on the pure edge side, so we ensure
130
	# that the main edges are really excluded. We do not want them to be
131
	# smoothed by the halo removal.
132
	shr_med = (excl) ? mt_logic (strong, shrink, mode="max") : strong
133
134
	# Substracts masks and amplifies the difference to be sure we get 255
135
	# on the areas to be processed.
136
	outside = mt_lutxy (large, shr_med, "x y - 2 *")
137
138
	# If edge processing is required, adds the edgemask
139
	ep_str  = "x y "+String(edgeproc * 0.66)+" * +"
140
	outside = (edgeproc > 0) ? mt_lutxy (outside, strong, ep_str) : outside
141
142
	# Smooth again and amplify to grow the mask a bit, otherwise the halo
143
	# parts sticking to the edges could be missed.
144
	outside.RemoveGrain (20, -1).mt_lut ("x 2 *")
145
146
	  (showmask == 1) ? outside.GreyScale ()
147
\	: (showmask == 2) ? shrink.GreyScale ()
148
\	: (showmask == 3) ? edges.GreyScale ()
149
\	: (showmask == 4) ? strong.GreyScale ()
150
\	:                   last
151
}
152
153
154
155
# Try to remove 2nd order halos.
156
Function FineDehalo2 (clip src, string "hconv", string "vconv", int "showmask")
157
{
158
	hconv    = Default (hconv, "-1 -2 0 0 40 0 0 -2 -1")
159
	vconv    = Default (vconv, "-2 -1 0 0 40 0 0 -1 -2")
160
	showmask = Default (showmask, 0)
161
162
	src
163
	fix_h = mt_convolution (horizontal="1", vertical=vconv, y=3, u=2, v=2)
164
	fix_v = mt_convolution (horizontal=hconv, vertical="1", y=3, u=2, v=2)
165
	edges_h = mt_edge (mode="1 2 1 0 0 0 -1 -2 -1", thY1=0, thY2=255)
166
	edges_v = mt_edge (mode="1 0 -1 2 0 -2 1 0 -1", thY1=0, thY2=255)
167
	mask_h = edges_h	#.mt_lut (expr="x 2 *")
168
	mask_v = edges_v	#.mt_lut (expr="x 2 *")
169
	temp_h = mt_lutxy (mask_h, mask_v, expr="x 3 * y -")
170
	temp_v = mt_lutxy (mask_v, mask_h, expr="x 3 * y -")
171
	mask_h = temp_h
172
	mask_v = temp_v
173
174
	mask_h = mask_h.FineDehalo2_grow_mask ("vertical")
175
	mask_v = mask_v.FineDehalo2_grow_mask ("horizontal")
176
177
	src
178
	mt_merge (last, fix_h, mask_h, y=3, u=2, v=2)
179
	mt_merge (last, fix_v, mask_v, y=3, u=2, v=2)
180
181
	  (showmask == 1) ? mt_logic (mask_h, mask_v, mode="max").GreyScale ()
182
\	:                   last
183
}
184
185
Function FineDehalo2_grow_mask (clip mask, string mode)
186
{
187
	Assert ((mode == "horizontal" || mode == "vertical"), "Wrong mode")
188
189
	mask
190
	mt_expand (mode=mode).mt_inpand (mode=mode)
191
	mask_1 = mt_expand (mode=mode)
192
	mask_2 = mask_1.mt_expand (mode=mode).mt_expand (mode=mode)
193
	mt_lutxy (mask_2, mask_1, expr="x y -")
194
	RemoveGrain (12, -1).mt_lut (expr="x 1.8 *")
195
}
196
197
198
# new Functions added to the mod ver.
199
200
201-
Function FineDehaloanalog (clip src, int "radius", string "exdehalo", clip "fexedgesm", clip "linesm", bool "remasked", float "darkstr", float "brightstr")
201+
Function FineDehaloanalog (clip src, int "dhhradius", float "arx", float "ary", string "exdehalo", clip "fexedgesm", clip "linesm", bool "remasked", float "darkstr", float "brightstr")
202
{
203
rx       = Default (arx,          2) # DeHalo_alpha rx
204-
rad      = Default(radius,      2)
204+
ry       = Default (ary,         rx) # DeHalo_alpha ry
205
edgm     = Defined(linesm) ? linesm : src.slinesm()
206
rad      = Default(dhhradius,      2)
207-
exdehalo = Default(exdehalo, "VHSHaloremover("+String(rad)+","+String(rad)+",200,100,"+String(blurmix)+")")
207+
208-
useldh   = !defined(darkstr) && !defined(brightstr) && !defined(radius)
208+
209
	rx_i = Round (rx)
210
	ry_i = Round (ry)
211-
lightdh = DeHalo_alpha_mt(darkstr=0.2,brightstr=0.8)
211+
exdehalo = Default(exdehalo, "VHSHaloremover("+String(rx_i)+","+String(ry_i)+",200,100,"+String(blurmix)+")")
212-
FDedges = Defined(fexedgesm) ? fexedgesm : lightdh.FineDehaloedges()
212+
useldh   = !defined(darkstr) && !defined(brightstr) && !defined(arx) && !defined(ary)
213
214-
                     FineDehalo(rad,rad,exdehalo=useldh ? lightdh : undefined(),darkstr=darkstr,brightstr=brightstr,exedgesm=FDedges)
214+
215
lightdh = DeHalo_alpha_mt(rx=rx_i,ry=ry_i,darkstr=0.2,brightstr=0.8)
216-
hfil = alphadh.FineDehalo(rad,rad,exdehalo=DeHaloHmod(rad,exdehalo=exdehalo,extmask=edgm), exedgesm=FDedges)
216+
FDedges = Defined(fexedgesm) ? fexedgesm : lightdh.FineDehaloedges(rx=rx_i,ry=ry_i)
217
alphadh = remasked ? lightdh : \ 
218
                     FineDehalo(rx=rx,ry=ry,exdehalo=useldh ? lightdh : \
219
                                                              undefined(),darkstr=darkstr,brightstr=brightstr,exedgesm=FDedges)
220
221
hfil = alphadh.FineDehalo(rx=rx,ry=ry,exdehalo=DeHaloHmod(rad,exdehalo=exdehalo,extmask=edgm), exedgesm=FDedges)
222
RM2  = remasked ? DR_Radius_dhh(edgm.mt_inflate(),rad,0).mt_inflate() : nop()
223
224
DeRinging = mt_Merge(hfil, src, edgm.mt_inflate(155,155),u=remasked ? 1 : 4,v=remasked ? 1 : 4)
225
remasked ? mt_Merge(src, DeRinging, RM2, u=2, v=2) : DeRinging
226
}
227
228
# filtering with black borders by A.SONY
229
Function filtering_wbb (clip src, string "filter", int "oneborder", clip "linesm", clip "clip4lines")
230
{
231
oneborder = Default (oneborder, 2)
232
filter    = Default (filter, defined(linesm) ? "yahr3.TBilateral(5,5,0.9,0.9,5,5,0.7,chroma=false)" : \
233
                                             """FineDehaloanalog(exdehalo="VHSHaloremover(2,2,200,100,0.5).yahr2(32)")""")
234
src
235
AddBorders(oneborder,oneborder,oneborder,oneborder)
236
eval(filter)
237
Crop(oneborder, oneborder, -oneborder, -oneborder, align=true)
238
defined(linesm) ? mt_Merge(last, defined(clip4lines) ? clip4lines : src, linesm.mt_inflate(155,155)) : last
239
}
240
241
# black lines mask by A.SONY
242
Function slinesm (clip i, int "trh", float "trh2", bool "analog", bool "autogain", val "edgesm", bool "noedges")
243
{
244
analog     = Default(analog,   true)
245
autogain   = Default(autogain, true)
246
noedges    = Default(noedges,  false)
247
trh        = Default(trh,  255)
248
trh2       = Default(trh2, 170)
249
250
i          = autogain ? i.ColorYUV(autogain=true) : i
251
252
edgesm     = !noedges ? defined(edgesm) ? isclip(edgesm) ? edgesm : eval("i." + edgesm) : i.Camembert_dhh() : nop()
253
254
LineDarkenclip = trh==0 ? i : i.FastLineDarkenMOD3_dhh(trh).blur(0.5).FastLineDarkenMOD3_dhh(250,1,250,-2)
255
256
lut4dark   = analog ? LineDarkenclip.mt_lut("x "+String(trh2/2.46)+" < 255 x "+String(trh2)+" > 0 255 x "+String(trh2/2.46)+" - 255 "+String(trh2)+" "+String(trh2/2.46)+" - / * - ? ?",u=1,v=1) : 
257
                    \ LineDarkenclip.mt_binarize(70, mode="0 255")
258
259
noedges ? lut4dark : 
260
        \ mt_merge(i.mt_edge(mode="min/max", thY1=255, thY2=255), edgesm, lut4dark)
261
}