-
Notifications
You must be signed in to change notification settings - Fork 14
/
GamutCompress.dctl
157 lines (139 loc) · 5.68 KB
/
GamutCompress.dctl
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
DEFINE_UI_PARAMS(threshold_r, threshold c, DCTLUI_SLIDER_FLOAT, 0.815, 0.4, 1.0, 0.0)
DEFINE_UI_PARAMS(threshold_g, threshold m, DCTLUI_SLIDER_FLOAT, 0.803, 0.4, 1.0, 0.0)
DEFINE_UI_PARAMS(threshold_b, threshold y, DCTLUI_SLIDER_FLOAT, 0.88, 0.4, 1.0, 0.0)
DEFINE_UI_PARAMS(power, power, DCTLUI_SLIDER_FLOAT, 1.2, 1.0, 3.0, 1.0)
DEFINE_UI_PARAMS(cyan, cyan, DCTLUI_SLIDER_FLOAT, 0.147, 0.0, 1.0, 0.0)
DEFINE_UI_PARAMS(magenta, magenta, DCTLUI_SLIDER_FLOAT, 0.264, 0.0, 1.0, 0.0)
DEFINE_UI_PARAMS(yellow, yellow, DCTLUI_SLIDER_FLOAT, 0.312, 0.0, 1.0, 0.0)
DEFINE_UI_PARAMS(working_colorspace, working space, DCTLUI_COMBO_BOX, 0, {acescct, acescc, acescg}, {acescct, acescc, acescg})
DEFINE_UI_PARAMS(invert, invert, DCTLUI_CHECK_BOX, 0)
DEFINE_UI_PARAMS(overlay, overlay graph, DCTLUI_CHECK_BOX, 0)
// Convert acescg to acescct
__DEVICE__ float lin_to_acescct(float in) {
if (in <= 0.0078125f) {
return 10.5402377416545f * in + 0.0729055341958355f;
} else {
return (_log2f(in) + 9.72f) / 17.52f;
}
}
// Convert acescct to acescg
__DEVICE__ float acescct_to_lin(float in) {
if (in > 0.155251141552511f) {
return _powf( 2.0f, in*17.52f - 9.72f);
} else {
return (in - 0.0729055341958355f) / 10.5402377416545f;
}
}
// Convert acescg to acescc
__DEVICE__ float lin_to_acescc(float in) {
if (in <= 0.0f) {
return -0.3584474886f;
} else if (in < _powf(2.0f, -15.0f)) {
return (_log2f(_powf(2.0f, -16.0f) + in * 0.5f) + 9.72f) / 17.52f;
} else {
return (_log2f(in) + 9.72f) / 17.52f;
}
}
// Convert acescc to acescg
__DEVICE__ float acescc_to_lin(float in) {
if (in < -0.3013698630f) {
return (_powf( 2.0f, in * 17.52f - 9.72f) - _powf( 2.0f, -16.0f)) * 2.0f;
} else if (in < (_log2f(65504.0f)+9.72f)/17.52f) {
return _powf(2.0f, in * 17.52f - 9.72f);
} else {
return 65504.0f;
}
}
// calculate compressed distance
__DEVICE__ float compress(float dist, float lim, float thr, bool invert, float power) {
float cdist, s;
if (dist < thr) {
cdist = dist;
} else {
// power(p) compression function plot https://www.desmos.com/calculator/54aytu7hek
if (lim < 1.0001f) {
return dist; // disable compression, avoid nan
}
s = (lim-thr)/_powf(_powf((1.0f-thr)/(lim-thr),-power)-1.0f,1.0f/power); // calc y=1 intersect
if (invert == 0) {
cdist = thr+s*((dist-thr)/s)/(_powf(1.0f+_powf((dist-thr)/s,power),1.0f/power)); // compress
} else {
if (dist > (thr + s)) {
cdist = dist; // avoid singularity
} else {
cdist = thr+s*_powf(-(_powf((dist-thr)/s,power)/(_powf((dist-thr)/s,power)-1.0f)),1.0f/power); // uncompress
}
}
}
return cdist;
}
__DEVICE__ float3 transform(int p_Width, int p_Height, int p_X, int p_Y, float p_R, float p_G, float p_B)
{
// ^-- this is necessary for the DCTL to work!
// normalised pixel coordinates
float2 pos = make_float2((float)p_X / p_Width, (float)(p_Height - p_Y) / p_Height);
// source pixels
float3 rgb = make_float3(p_R, p_G, p_B);
if (working_colorspace == acescct) {
rgb.x = acescct_to_lin(rgb.x);
rgb.y = acescct_to_lin(rgb.y);
rgb.z = acescct_to_lin(rgb.z);
}
if (working_colorspace == acescc) {
rgb.x = acescc_to_lin(rgb.x);
rgb.y = acescc_to_lin(rgb.y);
rgb.z = acescc_to_lin(rgb.z);
}
// thr is the percentage of the core gamut to protect.
float3 thr = make_float3(
_fminf(0.9999f, threshold_r),
_fminf(0.9999f, threshold_g),
_fminf(0.9999f, threshold_b));
// 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
float3 lim = make_float3(cyan+1.0f, magenta+1.0f, yellow+1.0f);
// achromatic axis
float ach = _fmaxf(rgb.x, _fmaxf(rgb.y, rgb.z));
// distance from the achromatic axis for each color component aka inverse rgb ratios
float3 dist;
dist.x = ach == 0.0f ? 0.0f : (ach-rgb.x)/_fabs(ach);
dist.y = ach == 0.0f ? 0.0f : (ach-rgb.y)/_fabs(ach);
dist.z = ach == 0.0f ? 0.0f : (ach-rgb.z)/_fabs(ach);
// compress distance with user controlled parameterized shaper function
float3 cdist = make_float3(
compress(dist.x, lim.x, thr.x, invert, power),
compress(dist.y, lim.y, thr.y, invert, power),
compress(dist.z, lim.z, thr.z, invert, power));
// recalculate rgb from compressed distance and achromatic
// effectively this scales each color component relative to achromatic axis by the compressed distance
float3 crgb = make_float3(
ach-cdist.x*_fabs(ach),
ach-cdist.y*_fabs(ach),
ach-cdist.z*_fabs(ach));
// Graph overlay method based on one by Paul Dore
// https://github.com/baldavenger/DCTLs/tree/master/ACES%20TOOLS
if (overlay) {
float3 cramp = make_float3(
compress(2.0f * pos.x, lim.x, thr.x, invert, power),
compress(2.0f * pos.x, lim.y, thr.y, invert, power),
compress(2.0f * pos.x, lim.z, thr.z, invert, power));
bool overlay_r = _fabs(2.0f * pos.y - cramp.x) < 0.004f || _fabs(pos.y - 0.5f) < 0.0005f ? true : false;
bool overlay_g = _fabs(2.0f * pos.y - cramp.y) < 0.004f || _fabs(pos.y - 0.5f) < 0.0005f ? true : false;
bool overlay_b = _fabs(2.0f * pos.y - cramp.z) < 0.004f || _fabs(pos.y - 0.5f) < 0.0005f ? true : false;
crgb.x = overlay_g || overlay_b ? 1.0f : crgb.x;
crgb.y = overlay_b || overlay_r ? 1.0f : crgb.y;
crgb.z = overlay_r || overlay_g ? 1.0f : crgb.z;
}
if (working_colorspace == acescct) {
crgb.x = lin_to_acescct(crgb.x);
crgb.y = lin_to_acescct(crgb.y);
crgb.z = lin_to_acescct(crgb.z);
}
if (working_colorspace == acescc) {
crgb.x = lin_to_acescc(crgb.x);
crgb.y = lin_to_acescc(crgb.y);
crgb.z = lin_to_acescc(crgb.z);
}
// write output
return crgb;
}