forked from darktable-org/darktable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsoften.cl
More file actions
257 lines (192 loc) · 6.6 KB
/
Copy pathsoften.cl
File metadata and controls
257 lines (192 loc) · 6.6 KB
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
/*
This file is part of darktable,
copyright (c) 2012 ulrich pegelow.
darktable is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
darktable is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with darktable. If not, see <http://www.gnu.org/licenses/>.
*/
const sampler_t sampleri = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST;
const sampler_t samplerf = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_LINEAR;
float4 RGB_2_HSL(const float4 RGB)
{
float H, S, L;
// assumes that each channel is scaled to [0; 1]
float R = RGB.x;
float G = RGB.y;
float B = RGB.z;
float var_Min = fmin(R, fmin(G, B));
float var_Max = fmax(R, fmax(G, B));
float del_Max = var_Max - var_Min;
L = (var_Max + var_Min) / 2.0f;
if (del_Max == 0.0f)
{
H = 0.0f;
S = 0.0f;
}
else
{
if (L < 0.5f) S = del_Max / (var_Max + var_Min);
else S = del_Max / (2.0f - var_Max - var_Min);
float del_R = (((var_Max - R) / 6.0f) + (del_Max / 2.0f)) / del_Max;
float del_G = (((var_Max - G) / 6.0f) + (del_Max / 2.0f)) / del_Max;
float del_B = (((var_Max - B) / 6.0f) + (del_Max / 2.0f)) / del_Max;
if (R == var_Max) H = del_B - del_G;
else if (G == var_Max) H = (1.0f / 3.0f) + del_R - del_B;
else if (B == var_Max) H = (2.0f / 3.0f) + del_G - del_R;
if (H < 0.0f) H += 1.0f;
if (H > 1.0f) H -= 1.0f;
}
return (float4)(H, S, L, RGB.w);
}
float Hue_2_RGB(float v1, float v2, float vH)
{
if (vH < 0.0f) vH += 1.0f;
if (vH > 1.0f) vH -= 1.0f;
if ((6.0f * vH) < 1.0f) return (v1 + (v2 - v1) * 6.0f * vH);
if ((2.0f * vH) < 1.0f) return (v2);
if ((3.0f * vH) < 2.0f) return (v1 + (v2 - v1) * ((2.0f / 3.0f) - vH) * 6.0f);
return (v1);
}
float4 HSL_2_RGB(const float4 HSL)
{
float R, G, B;
float H = HSL.x;
float S = HSL.y;
float L = HSL.z;
float var_1, var_2;
if (S == 0.0f)
{
R = B = G = L;
}
else
{
if (L < 0.5f) var_2 = L * (1.0f + S);
else var_2 = (L + S) - (S * L);
var_1 = 2.0f * L - var_2;
R = Hue_2_RGB(var_1, var_2, H + (1.0f / 3.0f));
G = Hue_2_RGB(var_1, var_2, H);
B = Hue_2_RGB(var_1, var_2, H - (1.0f / 3.0f));
}
// returns RGB scaled to [0; 1] for each channel
return (float4)(R, G, B, HSL.w);
}
/* first step for soften module: generate overexposed image */
kernel void
soften_overexposed(read_only image2d_t in, write_only image2d_t out, const int width, const int height,
const float saturation, const float brightness)
{
const int x = get_global_id(0);
const int y = get_global_id(1);
if(x >= width || y >= height) return;
float4 pixel = read_imagef(in, sampleri, (int2)(x, y));
float4 hsl = RGB_2_HSL(pixel);
hsl.y = clamp(hsl.y * saturation, 0.0f, 1.0f);
hsl.z = clamp(hsl.z * brightness, 0.0f, 1.0f);
pixel = HSL_2_RGB(hsl);
write_imagef (out, (int2)(x, y), pixel);
}
/* horizontal gaussian blur */
kernel void
soften_hblur(read_only image2d_t in, write_only image2d_t out, global const float *m, const int rad,
const int width, const int height, const int blocksize, local float4 *buffer)
{
const int lid = get_local_id(0);
const int lsz = get_local_size(0);
const int x = get_global_id(0);
const int y = get_global_id(1);
float4 pixel = (float4)0.0f;
if(y >= height) return;
/* read pixel and fill center part of buffer */
pixel = read_imagef(in, sampleri, (int2)(x, y));
buffer[rad + lid] = pixel;
/* left wing of buffer */
for(int n=0; n <= rad/lsz; n++)
{
const int l = mad24(n, lsz, lid + 1);
if(l > rad) continue;
const int xx = mad24((int)get_group_id(0), lsz, -l);
buffer[rad - l] = read_imagef(in, sampleri, (int2)(xx, y));
}
/* right wing of buffer */
for(int n=0; n <= rad/lsz; n++)
{
const int r = mad24(n, lsz, lsz - lid);
if(r > rad) continue;
const int xx = mad24((int)get_group_id(0), lsz, lsz - 1 + r);
buffer[rad + lsz - 1 + r] = read_imagef(in, sampleri, (int2)(xx, y));
}
barrier(CLK_LOCAL_MEM_FENCE);
if(x >= width) return;
buffer += lid + rad;
m += rad;
float4 sum = (float4)0.0f;
for (int i=-rad; i<=rad; i++)
{
sum += buffer[i] * m[i];
}
pixel = sum;
write_imagef (out, (int2)(x, y), pixel);
}
/* vertical gaussian blur */
kernel void
soften_vblur(read_only image2d_t in, write_only image2d_t out, global const float *m, const int rad,
const int width, const int height, const int blocksize, local float4 *buffer)
{
const int lid = get_local_id(1);
const int lsz = get_local_size(1);
const int x = get_global_id(0);
const int y = get_global_id(1);
float4 pixel = (float4)0.0f;
if(x >= width) return;
/* read pixel and fill center part of buffer */
pixel = read_imagef(in, sampleri, (int2)(x, y));
buffer[rad + lid] = pixel;
/* left wing of buffer */
for(int n=0; n <= rad/lsz; n++)
{
const int l = mad24(n, lsz, lid + 1);
if(l > rad) continue;
const int yy = mad24((int)get_group_id(1), lsz, -l);
buffer[rad - l] = read_imagef(in, sampleri, (int2)(x, yy));
}
/* right wing of buffer */
for(int n=0; n <= rad/lsz; n++)
{
const int r = mad24(n, lsz, lsz - lid);
if(r > rad) continue;
const int yy = mad24((int)get_group_id(1), lsz, lsz - 1 + r);
buffer[rad + lsz - 1 + r] = read_imagef(in, sampleri, (int2)(x, yy));
}
barrier(CLK_LOCAL_MEM_FENCE);
if(y >= height) return;
buffer += lid + rad;
m += rad;
float4 sum = (float4)0.0f;
for (int i=-rad; i<=rad; i++)
{
sum += buffer[i] * m[i];
}
pixel = sum;
write_imagef (out, (int2)(x, y), pixel);
}
/* final step for soften module */
kernel void
soften_mix(read_only image2d_t in_a, read_only image2d_t in_b, write_only image2d_t out, const int width, const int height,
const float amount)
{
const int x = get_global_id(0);
const int y = get_global_id(1);
if(x >= width || y >= height) return;
float4 original = read_imagef(in_a, sampleri, (int2)(x, y));
float4 processed = read_imagef(in_b, sampleri, (int2)(x, y));
float4 pixel = original * (1.0f - amount) + clamp(processed, (float4)0.0f, (float4)1.0f) * amount;
pixel.w = original.w;
write_imagef (out, (int2)(x, y), pixel);
}