]> git.sesse.net Git - casparcg/blob - accelerator/ogl/image/blending_glsl.h
[mixer] Merged fixed from 2.0 where contrast adjustment incorrectly worked on premult...
[casparcg] / accelerator / ogl / image / blending_glsl.h
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 #pragma once
23
24 static std::string get_adjustement_glsl()
25 {
26         return R"shader(
27                         /*
28                         ** Contrast, saturation, brightness
29                         ** Code of this function is from TGM's shader pack
30                         ** http://irrlicht.sourceforge.net/phpBB2/viewtopic.php?t=21057
31                         */
32
33                         vec3 ContrastSaturationBrightness(vec4 color, float brt, float sat, float con)
34                         {
35                                 const float AvgLumR = 0.5;
36                                 const float AvgLumG = 0.5;
37                                 const float AvgLumB = 0.5;
38
39                                 vec3 LumCoeff = is_hd
40                                                 ? vec3(0.0722, 0.7152, 0.2126)
41                                                 : vec3(0.114, 0.587, 0.299);
42
43                                 if (color.a > 0.0)
44                                         color.rgb /= color.a;
45
46                                 vec3 AvgLumin = vec3(AvgLumR, AvgLumG, AvgLumB);
47                                 vec3 brtColor = color.rgb * brt;
48                                 vec3 intensity = vec3(dot(brtColor, LumCoeff));
49                                 vec3 satColor = mix(intensity, brtColor, sat);
50                                 vec3 conColor = mix(AvgLumin, satColor, con);
51
52                                 conColor.rgb *= color.a;
53
54                                 return conColor;
55                         }
56
57                         /*
58                         ** Gamma correction
59                         ** Details: http://blog.mouaif.org/2009/01/22/photoshop-gamma-correction-shader/
60                         */
61                         #define GammaCorrection(color, gamma)                                                           pow(color, vec3(1.0 / gamma))
62
63                         /*
64                         ** Levels control (input (+gamma), output)
65                         ** Details: http://blog.mouaif.org/2009/01/28/levels-control-shader/
66                         */
67
68                         #define LevelsControlInputRange(color, minInput, maxInput)                              min(max(color - vec3(minInput), vec3(0.0)) / (vec3(maxInput) - vec3(minInput)), vec3(1.0))
69                         #define LevelsControlInput(color, minInput, gamma, maxInput)                            GammaCorrection(LevelsControlInputRange(color, minInput, maxInput), gamma)
70                         #define LevelsControlOutputRange(color, minOutput, maxOutput)                   mix(vec3(minOutput), vec3(maxOutput), color)
71                         #define LevelsControl(color, minInput, gamma, maxInput, minOutput, maxOutput)   LevelsControlOutputRange(LevelsControlInput(color, minInput, gamma, maxInput), minOutput, maxOutput)
72         )shader";
73 }
74
75 static std::string get_blend_glsl()
76 {
77         static std::string glsl = R"shader(
78                         /*
79                         ** Photoshop & misc math
80                         ** Blending modes, RGB/HSL/Contrast/Desaturate, levels control
81                         **
82                         ** Romain Dura | Romz
83                         ** Blog: http://blog.mouaif.org
84                         ** Post: http://blog.mouaif.org/?p=94
85                         */
86
87
88                         /*
89                         ** Desaturation
90                         */
91
92                         vec4 Desaturate(vec3 color, float Desaturation)
93                         {
94                                 vec3 grayXfer = vec3(0.3, 0.59, 0.11);
95                                 vec3 gray = vec3(dot(grayXfer, color));
96                                 return vec4(mix(color, gray, Desaturation), 1.0);
97                         }
98
99
100                         /*
101                         ** Hue, saturation, luminance
102                         */
103
104                         vec3 RGBToHSL(vec3 color)
105                         {
106                                 vec3 hsl;
107
108                                 float fmin = min(min(color.r, color.g), color.b);
109                                 float fmax = max(max(color.r, color.g), color.b);
110                                 float delta = fmax - fmin;
111
112                                 hsl.z = (fmax + fmin) / 2.0;
113
114                                 if (delta == 0.0)
115                                 {
116                                         hsl.x = 0.0;
117                                         hsl.y = 0.0;
118                                 }
119                                 else
120                                 {
121                                         if (hsl.z < 0.5)
122                                                 hsl.y = delta / (fmax + fmin);
123                                         else
124                                                 hsl.y = delta / (2.0 - fmax - fmin);
125
126                                         float deltaR = (((fmax - color.r) / 6.0) + (delta / 2.0)) / delta;
127                                         float deltaG = (((fmax - color.g) / 6.0) + (delta / 2.0)) / delta;
128                                         float deltaB = (((fmax - color.b) / 6.0) + (delta / 2.0)) / delta;
129
130                                         if (color.r == fmax )
131                                                 hsl.x = deltaB - deltaG;
132                                         else if (color.g == fmax)
133                                                 hsl.x = (1.0 / 3.0) + deltaR - deltaB;
134                                         else if (color.b == fmax)
135                                                 hsl.x = (2.0 / 3.0) + deltaG - deltaR;
136
137                                         if (hsl.x < 0.0)
138                                                 hsl.x += 1.0;
139                                         else if (hsl.x > 1.0)
140                                                 hsl.x -= 1.0;
141                                 }
142
143                                 return hsl;
144                         }
145
146                         float HueToRGB(float f1, float f2, float hue)
147                         {
148                                 if (hue < 0.0)
149                                         hue += 1.0;
150                                 else if (hue > 1.0)
151                                         hue -= 1.0;
152                                 float res;
153                                 if ((6.0 * hue) < 1.0)
154                                         res = f1 + (f2 - f1) * 6.0 * hue;
155                                 else if ((2.0 * hue) < 1.0)
156                                         res = f2;
157                                 else if ((3.0 * hue) < 2.0)
158                                         res = f1 + (f2 - f1) * ((2.0 / 3.0) - hue) * 6.0;
159                                 else
160                                         res = f1;
161                                 return res;
162                         }
163
164                         vec3 HSLToRGB(vec3 hsl)
165                         {
166                                 vec3 rgb;
167
168                                 if (hsl.y == 0.0)
169                                         rgb = vec3(hsl.z);
170                                 else
171                                 {
172                                         float f2;
173
174                                         if (hsl.z < 0.5)
175                                                 f2 = hsl.z * (1.0 + hsl.y);
176                                         else
177                                                 f2 = (hsl.z + hsl.y) - (hsl.y * hsl.z);
178
179                                         float f1 = 2.0 * hsl.z - f2;
180
181                                         rgb.r = HueToRGB(f1, f2, hsl.x + (1.0/3.0));
182                                         rgb.g = HueToRGB(f1, f2, hsl.x);
183                                         rgb.b= HueToRGB(f1, f2, hsl.x - (1.0/3.0));
184                                 }
185
186                                 return rgb;
187                         }
188
189
190
191
192                         /*
193                         ** Float blending modes
194                         ** Adapted from here: http://www.nathanm.com/photoshop-blending-math/
195                         ** But I modified the HardMix (wrong condition), Overlay, SoftLight, ColorDodge, ColorBurn, VividLight, PinLight (inverted layers) ones to have correct results
196                         */
197
198                         #define BlendLinearDodgef                                       BlendAddf
199                         #define BlendLinearBurnf                                        BlendSubstractf
200                         #define BlendAddf(base, blend)                          min(base + blend, 1.0)
201                         #define BlendSubstractf(base, blend)            max(base + blend - 1.0, 0.0)
202                         #define BlendLightenf(base, blend)              max(blend, base)
203                         #define BlendDarkenf(base, blend)                       min(blend, base)
204                         #define BlendLinearLightf(base, blend)  (blend < 0.5 ? BlendLinearBurnf(base, (2.0 * blend)) : BlendLinearDodgef(base, (2.0 * (blend - 0.5))))
205                         #define BlendScreenf(base, blend)                       (1.0 - ((1.0 - base) * (1.0 - blend)))
206                         #define BlendOverlayf(base, blend)              (base < 0.5 ? (2.0 * base * blend) : (1.0 - 2.0 * (1.0 - base) * (1.0 - blend)))
207                         #define BlendSoftLightf(base, blend)            ((blend < 0.5) ? (2.0 * base * blend + base * base * (1.0 - 2.0 * blend)) : (sqrt(base) * (2.0 * blend - 1.0) + 2.0 * base * (1.0 - blend)))
208                         #define BlendColorDodgef(base, blend)           ((blend == 1.0) ? blend : min(base / (1.0 - blend), 1.0))
209                         #define BlendColorBurnf(base, blend)            ((blend == 0.0) ? blend : max((1.0 - ((1.0 - base) / blend)), 0.0))
210                         #define BlendVividLightf(base, blend)           ((blend < 0.5) ? BlendColorBurnf(base, (2.0 * blend)) : BlendColorDodgef(base, (2.0 * (blend - 0.5))))
211                         #define BlendPinLightf(base, blend)             ((blend < 0.5) ? BlendDarkenf(base, (2.0 * blend)) : BlendLightenf(base, (2.0 *(blend - 0.5))))
212                         #define BlendHardMixf(base, blend)              ((BlendVividLightf(base, blend) < 0.5) ? 0.0 : 1.0)
213                         #define BlendReflectf(base, blend)              ((blend == 1.0) ? blend : min(base * base / (1.0 - blend), 1.0))
214
215
216                         /*
217                         ** Vector3 blending modes
218                         */
219
220                         #define Blend(base, blend, funcf)                       vec3(funcf(base.r, blend.r), funcf(base.g, blend.g), funcf(base.b, blend.b))
221
222                         #define BlendNormal(base, blend)                        (blend)
223                         #define BlendLighten                                            BlendLightenf
224                         #define BlendDarken                                             BlendDarkenf
225                         #define BlendMultiply(base, blend)              (base * blend)
226                         #define BlendAverage(base, blend)                       ((base + blend) / 2.0)
227                         #define BlendAdd(base, blend)                           min(base + blend, vec3(1.0))
228                         #define BlendSubstract(base, blend)             max(base + blend - vec3(1.0), vec3(0.0))
229                         #define BlendDifference(base, blend)            abs(base - blend)
230                         #define BlendNegation(base, blend)              (vec3(1.0) - abs(vec3(1.0) - base - blend))
231                         #define BlendExclusion(base, blend)             (base + blend - 2.0 * base * blend)
232                         #define BlendScreen(base, blend)                        Blend(base, blend, BlendScreenf)
233                         #define BlendOverlay(base, blend)                       Blend(base, blend, BlendOverlayf)
234                         #define BlendSoftLight(base, blend)             Blend(base, blend, BlendSoftLightf)
235                         #define BlendHardLight(base, blend)             BlendOverlay(blend, base)
236                         #define BlendColorDodge(base, blend)            Blend(base, blend, BlendColorDodgef)
237                         #define BlendColorBurn(base, blend)             Blend(base, blend, BlendColorBurnf)
238                         #define BlendLinearDodge                                        BlendAdd
239                         #define BlendLinearBurn                                 BlendSubstract
240                         #define BlendLinearLight(base, blend)           Blend(base, blend, BlendLinearLightf)
241                         #define BlendVividLight(base, blend)            Blend(base, blend, BlendVividLightf)
242                         #define BlendPinLight(base, blend)              Blend(base, blend, BlendPinLightf)
243                         #define BlendHardMix(base, blend)                       Blend(base, blend, BlendHardMixf)
244                         #define BlendReflect(base, blend)                       Blend(base, blend, BlendReflectf)
245                         #define BlendGlow(base, blend)                  BlendReflect(blend, base)
246                         #define BlendPhoenix(base, blend)                       (min(base, blend) - max(base, blend) + vec3(1.0))
247                         #define BlendOpacity(base, blend, F, O)         (F(base, blend) * O + blend * (1.0 - O))
248
249
250                         vec3 BlendHue(vec3 base, vec3 blend)
251                         {
252                                 vec3 baseHSL = RGBToHSL(base);
253                                 return HSLToRGB(vec3(RGBToHSL(blend).r, baseHSL.g, baseHSL.b));
254                         }
255
256                         vec3 BlendSaturation(vec3 base, vec3 blend)
257                         {
258                                 vec3 baseHSL = RGBToHSL(base);
259                                 return HSLToRGB(vec3(baseHSL.r, RGBToHSL(blend).g, baseHSL.b));
260                         }
261
262                         vec3 BlendColor(vec3 base, vec3 blend)
263                         {
264                                 vec3 blendHSL = RGBToHSL(blend);
265                                 return HSLToRGB(vec3(blendHSL.r, blendHSL.g, RGBToHSL(base).b));
266                         }
267
268                         vec3 BlendLuminosity(vec3 base, vec3 blend)
269                         {
270                                 vec3 baseHSL = RGBToHSL(base);
271                                 return HSLToRGB(vec3(baseHSL.r, baseHSL.g, RGBToHSL(blend).b));
272                         }
273
274         )shader";
275
276         return glsl;
277 }
278
279 static std::string get_chroma_glsl()
280 {
281         static std::string glsl = R"shader(
282                 // Chroma keying
283                 // Author: Tim Eves <timseves@googlemail.com>
284                 //
285                 // This implements the Chroma key algorithm described in the paper:
286                 //      'Software Chroma Keying in an Imersive Virtual Environment'
287                 //      by F. van den Bergh & V. Lalioti
288                 // but as a pixel shader algorithm.
289                 //
290                 vec4  grey_xfer  = is_hd
291                                 ? vec4(0.2126, 0.7152, 0.0722, 0)
292                                 : vec4(0.299,  0.587,  0.114, 0);
293
294                 // This allows us to implement the paper's alphaMap curve in software
295                 // rather than a largeish array
296                 float alpha_map(float d)
297                 {
298                     return 1.0 - smoothstep(1.0, chroma_softness, d);
299                 }
300
301                 // http://stackoverflow.com/questions/15095909/from-rgb-to-hsv-in-opengl-glsl
302                 vec3 rgb2hsv(vec3 c)
303                 {
304                         vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
305                         vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
306                         vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
307
308                         float d = q.x - min(q.w, q.y);
309                         float e = 1.0e-10;
310                         return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
311                 }
312
313                 // From the same page
314                 vec3 hsv2rgb(vec3 c)
315                 {
316                         vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
317                         vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
318                         return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
319                 }
320
321                 float AngleDiff(float angle1, float angle2)
322                 {
323                         return 0.5 - abs(abs(angle1 - angle2) - 0.5);
324                 }
325
326                 float AngleDiffDirectional(float angle1, float angle2)
327                 {
328                         float diff = angle1 - angle2;
329
330                         return diff < -0.5
331                                         ? diff + 1.0
332                                         : (diff > 0.5 ? diff - 1.0 : diff);
333                 }
334
335                 float Distance(float actual, float target)
336                 {
337                         return min(0.0, target - actual);
338                 }
339
340                 float ColorDistance(vec3 hsv)
341                 {
342                         float hueDiff                                   = AngleDiff(hsv.x, chroma_target_hue) * 2;
343                         float saturationDiff                    = Distance(hsv.y, chroma_min_saturation);
344                         float brightnessDiff                    = Distance(hsv.z, chroma_min_brightness);
345
346                         float saturationBrightnessScore = max(brightnessDiff, saturationDiff);
347                         float hueScore                                  = hueDiff - chroma_hue_width;
348
349                         return -hueScore * saturationBrightnessScore;
350                 }
351
352                 vec3 supress_spill(vec3 c)
353                 {
354                         float hue               = c.x;
355                         float diff              = AngleDiffDirectional(hue, chroma_target_hue);
356                         float distance  = abs(diff) / chroma_spill_suppress;
357
358                         if (distance < 1)
359                         {
360                                 c.x = diff < 0
361                                                 ? chroma_target_hue - chroma_spill_suppress
362                                                 : chroma_target_hue + chroma_spill_suppress;
363                                 c.y *= min(1.0, distance + chroma_spill_suppress_saturation);
364                         }
365
366                         return c;
367                 }
368
369                 // Key on any color
370                 vec4 ChromaOnCustomColor(vec4 c)
371                 {
372                         vec3 hsv                = rgb2hsv(c.rgb);
373                         float distance  = ColorDistance(hsv);
374                         float d                 = distance * -2.0 + 1.0;
375                     vec4 suppressed     = vec4(hsv2rgb(supress_spill(hsv)), 1.0);
376                         float alpha             = alpha_map(d);
377
378                         suppressed *= alpha;
379
380                         return chroma_show_mask ? vec4(suppressed.a, suppressed.a, suppressed.a, 1) : suppressed;
381                 }
382         )shader";
383
384         return glsl;
385 }