-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathstanislav_paskalev.gmic
178 lines (152 loc) · 6.07 KB
/
stanislav_paskalev.gmic
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#@gmic
#----------------------------------------------------------------------
#
# File : stanislav_paskalev.gmic <-- Put your filename here.
# ( G'MIC commands file )
#
# Description : External Filters by Stanislav Paskalev.
#
# Location : If you are using the G'MIC plug-in for GIMP, and made
# a filter update via Internet, a copy of this file
# should be located in the folder :
#
# $HOME/.gmic_def.xxxx (on Unix-based OS).
# %APPDATA/gmic_def.xxxx (on Windows-based OS).
#
# Author : Stanislav Paskalev
# ( https://www.spaskalev.com/ )
#
# License : Public domain
#
#----------------------------------------------------------------------
#@gui _<b>Repair</b>
#@cli unpurple : intensity, min_brightnes, min_red_to_blue, max_red_to_blue, blur_std_deviation, gentle_mode
#@cli : Unpurple removes purple fringing
#@cli : Based on https://github.com/mjambon/purple-fringe by Martin Jambon.
#@cli : This implementation works with RGB and RGBA images in any bit depth.
#@cli : Note: the minimum brightness and intensity are relative to the maximum blue value.
#@cli : Ported to G'MIC by Stanislav Paskalev <[email protected]>
#@cli : $ image.jpg +unpurple
#@cli :
#@gui Unpurple : unpurple, unpurple_preview(0)+
#@gui : Intensity of purple fringe = float(1,0,1)
#@gui : Minimum brightness = float(0,0,1)
#@gui : Minimum red:blue ratio in the fringe = float(0,0,1)
#@gui : Maximum red:blue ratio in the fringe = float(0.33,0,1)
#@gui : Blur standard deviation = int(5,1,10)
#@gui : Gentle mode (overrides minimum brightness and minimun red:blue ratio) = bool(0)
#@gui : Bit depth (hidden) = value(0)
#@gui : sep = separator()
#@gui : Preview Type = choice("Full","Forward Horizontal","Forward Vertical","Backward Horizontal",
#@gui : "Backward Vertical","Duplicate Top","Duplicate Left","Duplicate Bottom","Duplicate Right",
#@gui : "Duplicate Horizontal","Duplicate Vertical","Checkered","Checkered Inverse")
#@gui : Preview Split = point(50,50,0,0,200,200,200,0,10)_0
#@gui : sep = separator()
#@gui : note = note("Unpurple removes purple fringing")
#@gui : note = note("Based on <a href="https://github.com/mjambon/purple-fringe">https://github.com/mjambon/purple-fringe</a> by Martin Jambon.")
#@gui : note = note("This implementation works with RGB and RGBA images in any bit depth.")
#@gui : note = note("Note: the minimum brightness and intensity are relative to the maximum blue value.")
#@gui : note = note("Ported to G'MIC by Stanislav Paskalev <[email protected]>")
unpurple : skip ${1=1},${2=0},${3=0},${4=0.33},${5=5},${6=0},${7=0}
intensity=$1
min_brightness=$2
min_red_to_blue_ratio=$3
max_red_to_blue_ratio=$4
blur_std_variation=$5
gentle=$6
precision=$7
if $gentle
min_brightness=0.8
min_red_to_blue_ratio=0.15
fi
repeat $!
local[$>]
# This filter only works with RGB and RGBA images
if s==3
has_alpha=0
elif s==4
has_alpha=1
else
error "Unsupported number of image channels "{s}
fi
# Split into different images, three for RGB or four for RGBA
split c
# Move alpha in front, if present
if $has_alpha
move. 0
fi
# Store the maximum value from the blue image in order to
# normalize the data for the relative treshold operation
# and then restore it for the subtraction process
if $precision
max_blue={(1<<$precision)-1}
else
# The original code used a fixed constant of 255 for this,
# making it work only with 8-bit images. I can't find a way
# to get the input image bit depth, neither from the gmic
# command line nor from the gmic-qt plugin so making the
# operation relative to the maximum blue value available.
#
# This means that the minimum brightness and intensity
# parameters are now also relative to the image that is
# currently being processed. Caveat emptor.
max_blue.={iM}
fi
# generate a blurred mask from the the blue image, taking
# the selected threshold and intensity into account
+fill. "blue=i/"$max_blue";
threshold="$min_brightness";
grey_level=max(0,(blue-threshold))*1/(1-threshold);
"$intensity"*grey_level"
blur. $blur_std_variation,0,0
# Add the blur mask as a channel and process the subtraction
append[-4,-3,-2,-1] c
fill. "bl=min("$max_blue",("$max_blue"*i3));
# amount of blue and red that would produce a grey if removed
db=max((i2-i1), 0);
dr=max((i0-i1), 0);
# maximum amount of blue that we accept to remove, ignoring red level
mb=min(bl,db);
# amount of red that we will remove, honoring max red:blue
r_diff=min(dr,mb*"$max_red_to_blue_ratio");
# amount of blue that we will remove, honoring min red:blue
b_diff=if("$min_red_to_blue_ratio">0,min(mb,r_diff/"$min_red_to_blue_ratio"),mb);
bl=round(bl,0,-1);
r_diff=round(r_diff,0,-1);
b_diff=round(b_diff,0,-1);
[i0-r_diff,i1,i2-b_diff]"
# Remove the blur mask
split. c
remove.
# Restore any alpha
if $has_alpha
move[0] 4
fi
# Combine and output
append c
done
done
return
unpurple_preview :
gui_split_preview "unpurple $*",${-3--1}
vis_min_brightness=2
vis_min_red_to_blue=2
gentle={$6}
if $gentle
vis_min_brightness=0
vis_min_red_to_blue=0
fi
status "{$1}"\
"{$2}"_$vis_min_brightness\
"{$3}"_$vis_min_red_to_blue\
"{$4}"\
"{$5}"\
"{$6}"\
"{$7}"\
"{$8}"\
"{$9}"
# Local Variables:
# mode: sh
# End:
#
# (End of G'MIC custom commands)