forked from jedypod/gamut-compress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GamutCompress.fuse
237 lines (182 loc) · 6.86 KB
/
GamutCompress.fuse
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
--[[--
-----------------------------
Gamut Compress is a tool to compress out of gamut colors back into gamut.
Written by Jed Smith with lots of help from the ACES Gamut Mapping Virtual Working Group
https://github.com/jedypod/gamut-compress
https://community.acescentral.com/t/rgb-saturation-gamut-mapping-approach-and-a-comp-vfx-perspective
https://community.acescentral.com/c/aces-development-acesnext/vwg-aces-gamut-mapping-working-group
Initial version of this Fuse written by Jacob Danell. Thank you! :D
------------------
Documenation
Threshold
Percentage of the gamut to affect.
If threshold is 0.2, the inner 80% of the gamut will be unaffected
and out of gamut values will be compressed into the outer 20% of the gamuts color volume.
Max Distance
Per color component control to specify what distance will be compressed to the gamut boundary.
For example, a value of d_c=0.2 will map colors with a distance of
red=1.2 from the achromatic axis to red=1.0, which is the gamut boundary.
Direction
Specifies whether to apply or inverse the gamut compression operation.
-------------------------
Installation
To install this fuse for Blackmagic Fusion Studio, copy the file to your UserData Path Map folder:
Linux:
~/.fusion/BlackmagicDesign/Fusion/Fuses
OSX:
~/Library/Application Support/Blackmagic Design/Fusion
Windows:
%appdata%\Blackmagic Design\Fusion\Fuses
For the Fusion page in Resolve Lite or Resolve Studio use these UserData folders:
Linux:
~/.local/share/DaVinciResolve/Fusion/Fuses
OSX:
~/Library/Application Support/Blackmagic Design/DaVinci Resolve/Fusion
Windows:
%appdata%\Blackmagic Design\DaVinci Resolve\Fusion\Fuses
--]]--
FuRegisterClass("GamutCompress", CT_Tool, {
REGS_Name = "Gamut Compress",
REGS_Category = "Color",
REGS_OpIconString = "",
REGS_OpDescription = "Compress out of gamut colors back into gamut.",
REG_Fuse_NoEdit = false,
REG_Fuse_NoReload = false,
REG_SupportsDoD = false,
})
function Create()
InThresholdR = self:AddInput("thr c", "thr c", {
LINKID_DataType = "Number",
INPID_InputControl = "SliderControl",
INP_Default = 0.15,
INP_MinAllowed = 0.0001,
INP_MaxScale = 0.3,
})
InThresholdG = self:AddInput("thr m", "thr m", {
LINKID_DataType = "Number",
INPID_InputControl = "SliderControl",
INP_Default = 0.15,
INP_MinAllowed = 0.0001,
INP_MaxScale = 0.3,
})
InThresholdB = self:AddInput("thr y", "thr y", {
LINKID_DataType = "Number",
INPID_InputControl = "SliderControl",
INP_Default = 0.15,
INP_MinAllowed = 0.0001,
INP_MaxScale = 0.3,
})
self:BeginControlNest("max distance limits", "max distance limits", true, {})
InCyan = self:AddInput("dist c", "dist c", {
LINKID_DataType = "Number",
INPID_InputControl = "SliderControl",
INP_Default = 0.1,
INP_MinAllowed = 0.0,
INP_MaxScale = 0.4,
})
InMagenta = self:AddInput("dist m", "dist m", {
LINKID_DataType = "Number",
INPID_InputControl = "SliderControl",
INP_Default = 0.2,
INP_MinAllowed = 0.0,
INP_MaxScale = 0.4,
})
InYellow = self:AddInput("dist y", "dist y", {
LINKID_DataType = "Number",
INPID_InputControl = "SliderControl",
INP_Default = 0.1,
INP_MinAllowed = 0.0,
INP_MaxScale = 0.4,
})
self:EndControlNest()
InInvert = self:AddInput("invert", "invert", {
LINKID_DataType = "Number",
INPID_InputControl = "CheckboxControl",
INP_MinAllowed = 0.0,
INP_MaxAllowed = 1.0,
INP_Default = 0.0,
})
InImage = self:AddInput("Input", "Input", {
LINKID_DataType = "Image",
LINK_Main = 1,
})
OutImage = self:AddOutput("Output", "Output", {
LINKID_DataType = "Image",
LINK_Main = 1,
})
end
local lastreqtime = -2
function Process(req)
local src = InImage:GetValue(req)
local dst = Image{ IMG_Like = src, IMG_DeferAlloc = true }
if not req:IsPreCalc() then
local node = DVIPComputeNode(req, "SolidKernel", SolidKernel, "SolidParams", SolidParams)
local params
if (lastreqtime ~= req.Time - 1) then
params = node:GetParamBlock(SolidParams)
end
lastreqtime = req.Time
params.th_c = InThresholdR:GetValue(req).Value
params.th_m = InThresholdG:GetValue(req).Value
params.th_y = InThresholdB:GetValue(req).Value
params.d_c = InCyan:GetValue(req).Value
params.d_m = InMagenta:GetValue(req).Value
params.d_y = InYellow:GetValue(req).Value
params.invert = InInvert:GetValue(req).Value
params.srcCompOrder = src:IsMask() and 1 or 15
node:SetParamBlock(params)
node:AddInput("src", src)
node:AddOutput("dst", dst)
local ok = node:RunSession(req)
if not ok then
dst = nil
end
end
OutImage:Set(req, dst)
end
SolidParams = [[
float th_c;
float th_m;
float th_y;
float d_c;
float d_m;
float d_y;
int invert;
int srcCompOrder;
]]
SolidKernel = [[
__KERNEL__ void SolidKernel(__CONSTANTREF__ SolidParams *params, __TEXTURE2D__ src, __TEXTURE2D_WRITE__ dst)
{
DEFINE_KERNEL_ITERATORS_XY(x, y);
float4 rgba = _tex2DVecN(src, x, y, params->srcCompOrder);
float3 rgb = make_float3(rgba.x, rgba.y, rgba.z);
// Amount of outer gamut to affect
float3 th = 1.0f-make_float3(params->th_c, params->th_m, params->th_y);
// Distance limit: How far beyond the gamut boundary to compress
float3 dl = 1.0f+make_float3(params->d_c, params->d_m, params->d_y);
// Calculate scale so compression function passes through distance limit: (x=dl, y=1)
float3 s;
s.x = (1.0f-th.x)/_sqrtf(_fmaxf(1.001f, dl.x)-1.0f);
s.y = (1.0f-th.y)/_sqrtf(_fmaxf(1.001f, dl.y)-1.0f);
s.z = (1.0f-th.z)/_sqrtf(_fmaxf(1.001f, dl.z)-1.0f);
// Achromatic axis
float ac = _fmaxf(rgb.x, _fmaxf(rgb.y, rgb.z));
// Inverse RGB Ratios: distance from achromatic axis
float3 d = ac == 0.0f ? make_float3(0.0f) : (ac-rgb)/_fabs(ac);
float3 cd; // Compressed distance
// Parabolic compression function: https://www.desmos.com/calculator/nvhp63hmtj
if (params->invert == 0) {
cd.x = d.x<th.x?d.x:s.x*_sqrtf(d.x-th.x+s.x*s.x/4.0f)-s.x*_sqrtf(s.x*s.x/4.0f)+th.x;
cd.y = d.y<th.y?d.y:s.y*_sqrtf(d.y-th.y+s.y*s.y/4.0f)-s.y*_sqrtf(s.y*s.y/4.0f)+th.y;
cd.z = d.z<th.z?d.z:s.z*_sqrtf(d.z-th.z+s.z*s.z/4.0f)-s.z*_sqrtf(s.z*s.z/4.0f)+th.z;
} else {
cd.x = d.x<th.x?d.x:_powf(d.x-th.x+s.x*_sqrtf(s.x*s.x/4.0f),2.0f)/(s.x*s.x)-s.x*s.x/4.0f+th.x;
cd.y = d.y<th.y?d.y:_powf(d.y-th.y+s.y*_sqrtf(s.y*s.y/4.0f),2.0f)/(s.y*s.y)-s.y*s.y/4.0f+th.y;
cd.z = d.z<th.z?d.z:_powf(d.z-th.z+s.z*_sqrtf(s.z*s.z/4.0f),2.0f)/(s.z*s.z)-s.z*s.z/4.0f+th.z;
}
// Inverse RGB Ratios to RGB
rgb = ac-cd*_fabs(ac);
rgba = make_float4(rgb.x, rgb.y, rgb.z, rgba.w);
_tex2DVec4Write(dst, x, y, rgba);
}
]]