forked from jedypod/gamut-compress
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGamutCompress.fuse
297 lines (240 loc) · 9.42 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
--[[--
-----------------------------
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.
Power
Adjust the exponent of the compression function. 1.0f = Reinhard.
Higher values have C2 continuity and result in a more
"aggressive" curve, which preserves more color purity / "saturation".
Be wary of high values if accurate inversion is important to you.
Shadow Rolloff
Reduce gamut compression in dark areas below specified value.
Helps reduce invertability issues in negative values from grain.
Make sure there is only shadow grain below the threshold you specify.
Max Distance
Per color component control to specify what distance will be compressed to the gamut boundary.
For example, a value of cyan=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("threshold_c", "threshold_c", {
LINKID_DataType = "Number",
INPID_InputControl = "SliderControl",
INP_Default = 0.2,
INP_MinAllowed = 0.0001,
INP_MaxScale = 0.6,
})
InThresholdG = self:AddInput("threshold_m", "threshold_m", {
LINKID_DataType = "Number",
INPID_InputControl = "SliderControl",
INP_Default = 0.2,
INP_MinAllowed = 0.0001,
INP_MaxScale = 0.6,
})
InThresholdB = self:AddInput("threshold_y", "threshold_y", {
LINKID_DataType = "Number",
INPID_InputControl = "SliderControl",
INP_Default = 0.2,
INP_MinAllowed = 0.0001,
INP_MaxScale = 0.6,
})
InPower = self:AddInput("power", "power", {
LINKID_DataType = "Number",
INPID_InputControl = "SliderControl",
INP_Default = 1.2,
INP_MinAllowed = 1.0,
INP_MaxScale = 5.0,
})
InShdRolloff = self:AddInput("shd rolloff", "shd rolloff", {
LINKID_DataType = "Number",
INPID_InputControl = "SliderControl",
INP_Default = 0.0,
INP_MinAllowed = 0.0,
INP_MaxScale = 0.03,
})
self:BeginControlNest("max distance limits", "max distance limits", true, {})
InCyan = self:AddInput("cyan", "cyan", {
LINKID_DataType = "Number",
INPID_InputControl = "SliderControl",
INP_Default = 0.09,
INP_MinAllowed = 0.0,
INP_MaxScale = 1.0,
})
InMagenta = self:AddInput("magenta", "magenta", {
LINKID_DataType = "Number",
INPID_InputControl = "SliderControl",
INP_Default = 0.24,
INP_MinAllowed = 0.0,
INP_MaxScale = 1.0,
})
InYellow = self:AddInput("yellow", "yellow", {
LINKID_DataType = "Number",
INPID_InputControl = "SliderControl",
INP_Default = 0.12,
INP_MinAllowed = 0.0,
INP_MaxScale = 1.0,
})
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
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 = node:GetParamBlock(SolidParams)
params.threshold_c = InThresholdR:GetValue(req).Value
params.threshold_m = InThresholdG:GetValue(req).Value
params.threshold_y = InThresholdB:GetValue(req).Value
params.power = InPower:GetValue(req).Value
params.shd_rolloff = InShdRolloff:GetValue(req).Value
params.cyan = InCyan:GetValue(req).Value
params.magenta = InMagenta:GetValue(req).Value
params.yellow = 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 threshold_c;
float threshold_m;
float threshold_y;
float power;
float shd_rolloff;
float cyan;
float magenta;
float yellow;
int invert;
int srcCompOrder;
]]
SolidKernel = [[
// calculate compressed distance
__DEVICE__ float compress(float x, float l, float t, float p, bool invert) {
float cdist;
// power(p) compression function plot https://www.desmos.com/calculator/54aytu7hek
float s = (l-t)/_powf(_powf((1.0f-t)/(l-t),-p)-1.0f,1.0f/p); // calc y=1 intersect
if (l < 1.0001f) {
return x; // disable compression, avoid nan
}
if (x < t) {
cdist = x;
}
else {
if (invert == 0) {
cdist = t+s*((x-t)/s)/(_powf(1.0f+_powf((x-t)/s,p),1.0f/p)); // compress
} else {
if (x > (t + s)) {
cdist = x; // avoid singularity
}
cdist = t+s*_powf(-(_powf((x-t)/s,p)/(_powf((x-t)/s,p)-1.0f)),1.0f/p); // uncompress
}
}
return cdist;
}
__KERNEL__ void SolidKernel(__CONSTANTREF__ SolidParams *params, __TEXTURE2D__ src, __TEXTURE2D_WRITE__ dst)
{
DEFINE_KERNEL_ITERATORS_XY(x, y);
float4 rgb = _tex2DVecN(src, x, y, params->srcCompOrder);
// thr is the percentage of the core gamut to protect: the complement of threshold.
float3 thr = make_float3(
1.0f-_fmaxf(0.0001f, params->threshold_c),
1.0f-_fmaxf(0.0001f, params->threshold_m),
1.0f-_fmaxf(0.0001f, params->threshold_y));
// lim is the max distance from the gamut boundary that will be compressed
// 0 is a no-op, 1 will compress colors from a distance of 2.0 from achromatic to the gamut boundary
// if method is Reinhard, use the limit as-is
float3 lim;
lim = make_float3(params->cyan+1.0f, params->magenta+1.0f, params->yellow+1.0f);
// achromatic axis
float ach = _fmaxf(rgb.x, _fmaxf(rgb.y, rgb.z));
// achromatic shadow rolloff
float ach_shd;
if (params->shd_rolloff < 0.004f) {
// disable shadow rolloff functionality.
// values below 0.004 cause strange behavior, actually increasing distance in some cases.
// if ach < 0.0 and shd_rolloff is disabled, take absolute value. This preserves negative components after compression.
ach_shd = _fabs(ach);
} else {
// lift ach below threshold using a tanh compression function.
// this reduces large distance values in shadow grain, which can cause differences when inverting.
ach_shd = 1.0f-((1.0f-ach)<(1.0f-params->shd_rolloff)?(1.0f-ach):(1.0f-params->shd_rolloff)+params->shd_rolloff*_tanhf((((1.0f-ach)-(1.0f-params->shd_rolloff))/params->shd_rolloff)));
}
// distance from the achromatic axis for each color component aka inverse rgb ratios
float3 dist;
dist.x = ach_shd == 0.0f ? 0.0f : (ach-rgb.x)/ach_shd;
dist.y = ach_shd == 0.0f ? 0.0f : (ach-rgb.y)/ach_shd;
dist.z = ach_shd == 0.0f ? 0.0f : (ach-rgb.z)/ach_shd;
// compress distance with user controlled parameterized shaper function
float3 cdist = make_float3(
compress(dist.x, lim.x, thr.x, params->power, params->invert),
compress(dist.y, lim.y, thr.y, params->power, params->invert),
compress(dist.z, lim.z, thr.z, params->power, params->invert));
// recalculate rgb from compressed distance and achromatic
// effectively this scales each color component relative to achromatic axis by the compressed distance
float4 crgb = make_float4(
ach-cdist.x*ach_shd,
ach-cdist.y*ach_shd,
ach-cdist.z*ach_shd,
rgb.w);
_tex2DVec4Write(dst, x, y, crgb);
}
]]