2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG (www.casparcg.com).
\r
6 * CasparCG is free software: you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation, either version 3 of the License, or
\r
9 * (at your option) any later version.
\r
11 * CasparCG is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
\r
19 * Author: Robert Nagy, ronag89@gmail.com
\r
24 static std::string get_adjustement_glsl()
\r
28 "\n ** Contrast, saturation, brightness "
\r
29 "\n ** Code of this function is from TGM's shader pack "
\r
30 "\n ** http://irrlicht.sourceforge.net/phpBB2/viewtopic.php?t=21057 "
\r
33 "\n vec3 ContrastSaturationBrightness(vec3 color, float brt, float sat, float con) "
\r
35 "\n const float AvgLumR = 0.5; "
\r
36 "\n const float AvgLumG = 0.5; "
\r
37 "\n const float AvgLumB = 0.5; "
\r
39 "\n const vec3 LumCoeff = vec3(0.2125, 0.7154, 0.0721); "
\r
41 "\n vec3 AvgLumin = vec3(AvgLumR, AvgLumG, AvgLumB); "
\r
42 "\n vec3 brtColor = color * brt; "
\r
43 "\n vec3 intensity = vec3(dot(brtColor, LumCoeff)); "
\r
44 "\n vec3 satColor = mix(intensity, brtColor, sat); "
\r
45 "\n vec3 conColor = mix(AvgLumin, satColor, con); "
\r
46 "\n return conColor; "
\r
50 "\n ** Gamma correction "
\r
51 "\n ** Details: http://blog.mouaif.org/2009/01/22/photoshop-gamma-correction-shader/ "
\r
54 "\n#define GammaCorrection(color, gamma) pow(color, vec3(1.0 / gamma)) \n "
\r
57 "\n ** Levels control (input (+gamma), output) "
\r
58 "\n ** Details: http://blog.mouaif.org/2009/01/28/levels-control-shader/ "
\r
61 "\n#define LevelsControlInputRange(color, minInput, maxInput) min(max(color - vec3(minInput), vec3(0.0)) / (vec3(maxInput) - vec3(minInput)), vec3(1.0)) \n "
\r
62 "\n#define LevelsControlInput(color, minInput, gamma, maxInput) GammaCorrection(LevelsControlInputRange(color, minInput, maxInput), gamma) \n "
\r
63 "\n#define LevelsControlOutputRange(color, minOutput, maxOutput) mix(vec3(minOutput), vec3(maxOutput), color) \n "
\r
64 "\n#define LevelsControl(color, minInput, gamma, maxInput, minOutput, maxOutput) LevelsControlOutputRange(LevelsControlInput(color, minInput, gamma, maxInput), minOutput, maxOutput) \n "
\r
68 static std::string get_blend_glsl()
\r
70 static std::string glsl =
\r
72 "\n ** Photoshop & misc math "
\r
73 "\n ** Blending modes, RGB/HSL/Contrast/Desaturate, levels control "
\r
75 "\n ** Romain Dura | Romz "
\r
76 "\n ** Blog: http://blog.mouaif.org "
\r
77 "\n ** Post: http://blog.mouaif.org/?p=94 "
\r
82 "\n ** Desaturation "
\r
85 "\n vec4 Desaturate(vec3 color, float Desaturation) "
\r
87 "\n vec3 grayXfer = vec3(0.3, 0.59, 0.11); "
\r
88 "\n vec3 gray = vec3(dot(grayXfer, color)); "
\r
89 "\n return vec4(mix(color, gray, Desaturation), 1.0); "
\r
94 "\n ** Hue, saturation, luminance "
\r
97 "\n vec3 RGBToHSL(vec3 color) "
\r
101 "\n float fmin = min(min(color.r, color.g), color.b); "
\r
102 "\n float fmax = max(max(color.r, color.g), color.b); "
\r
103 "\n float delta = fmax - fmin; "
\r
105 "\n hsl.z = (fmax + fmin) / 2.0; "
\r
107 "\n if (delta == 0.0) "
\r
114 "\n if (hsl.z < 0.5) "
\r
115 "\n hsl.y = delta / (fmax + fmin); "
\r
117 "\n hsl.y = delta / (2.0 - fmax - fmin); "
\r
119 "\n float deltaR = (((fmax - color.r) / 6.0) + (delta / 2.0)) / delta; "
\r
120 "\n float deltaG = (((fmax - color.g) / 6.0) + (delta / 2.0)) / delta; "
\r
121 "\n float deltaB = (((fmax - color.b) / 6.0) + (delta / 2.0)) / delta; "
\r
123 "\n if (color.r == fmax ) "
\r
124 "\n hsl.x = deltaB - deltaG; "
\r
125 "\n else if (color.g == fmax) "
\r
126 "\n hsl.x = (1.0 / 3.0) + deltaR - deltaB; "
\r
127 "\n else if (color.b == fmax) "
\r
128 "\n hsl.x = (2.0 / 3.0) + deltaG - deltaR; "
\r
130 "\n if (hsl.x < 0.0) "
\r
131 "\n hsl.x += 1.0; "
\r
132 "\n else if (hsl.x > 1.0) "
\r
133 "\n hsl.x -= 1.0; "
\r
139 "\n float HueToRGB(float f1, float f2, float hue) "
\r
141 "\n if (hue < 0.0) "
\r
143 "\n else if (hue > 1.0) "
\r
146 "\n if ((6.0 * hue) < 1.0) "
\r
147 "\n res = f1 + (f2 - f1) * 6.0 * hue; "
\r
148 "\n else if ((2.0 * hue) < 1.0) "
\r
150 "\n else if ((3.0 * hue) < 2.0) "
\r
151 "\n res = f1 + (f2 - f1) * ((2.0 / 3.0) - hue) * 6.0; "
\r
157 "\n vec3 HSLToRGB(vec3 hsl) "
\r
161 "\n if (hsl.y == 0.0) "
\r
162 "\n rgb = vec3(hsl.z); "
\r
167 "\n if (hsl.z < 0.5) "
\r
168 "\n f2 = hsl.z * (1.0 + hsl.y); "
\r
170 "\n f2 = (hsl.z + hsl.y) - (hsl.y * hsl.z); "
\r
172 "\n float f1 = 2.0 * hsl.z - f2; "
\r
174 "\n rgb.r = HueToRGB(f1, f2, hsl.x + (1.0/3.0)); "
\r
175 "\n rgb.g = HueToRGB(f1, f2, hsl.x); "
\r
176 "\n rgb.b= HueToRGB(f1, f2, hsl.x - (1.0/3.0)); "
\r
186 "\n ** Float blending modes "
\r
187 "\n ** Adapted from here: http://www.nathanm.com/photoshop-blending-math/ "
\r
188 "\n ** But I modified the HardMix (wrong condition), Overlay, SoftLight, ColorDodge, ColorBurn, VividLight, PinLight (inverted layers) ones to have correct results "
\r
191 "\n#define BlendLinearDodgef BlendAddf \n "
\r
192 "\n#define BlendLinearBurnf BlendSubstractf \n "
\r
193 "\n#define BlendAddf(base, blend) min(base + blend, 1.0) \n "
\r
194 "\n#define BlendSubstractf(base, blend) max(base + blend - 1.0, 0.0) \n "
\r
195 "\n#define BlendLightenf(base, blend) max(blend, base) \n "
\r
196 "\n#define BlendDarkenf(base, blend) min(blend, base) \n "
\r
197 "\n#define BlendLinearLightf(base, blend) (blend < 0.5 ? BlendLinearBurnf(base, (2.0 * blend)) : BlendLinearDodgef(base, (2.0 * (blend - 0.5)))) \n "
\r
198 "\n#define BlendScreenf(base, blend) (1.0 - ((1.0 - base) * (1.0 - blend))) \n "
\r
199 "\n#define BlendOverlayf(base, blend) (base < 0.5 ? (2.0 * base * blend) : (1.0 - 2.0 * (1.0 - base) * (1.0 - blend))) \n "
\r
200 "\n#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))) \n "
\r
201 "\n#define BlendColorDodgef(base, blend) ((blend == 1.0) ? blend : min(base / (1.0 - blend), 1.0)) \n "
\r
202 "\n#define BlendColorBurnf(base, blend) ((blend == 0.0) ? blend : max((1.0 - ((1.0 - base) / blend)), 0.0)) \n "
\r
203 "\n#define BlendVividLightf(base, blend) ((blend < 0.5) ? BlendColorBurnf(base, (2.0 * blend)) : BlendColorDodgef(base, (2.0 * (blend - 0.5)))) \n "
\r
204 "\n#define BlendPinLightf(base, blend) ((blend < 0.5) ? BlendDarkenf(base, (2.0 * blend)) : BlendLightenf(base, (2.0 *(blend - 0.5)))) \n "
\r
205 "\n#define BlendHardMixf(base, blend) ((BlendVividLightf(base, blend) < 0.5) ? 0.0 : 1.0) \n "
\r
206 "\n#define BlendReflectf(base, blend) ((blend == 1.0) ? blend : min(base * base / (1.0 - blend), 1.0)) \n "
\r
210 "\n ** Vector3 blending modes "
\r
213 "\n#define Blend(base, blend, funcf) vec3(funcf(base.r, blend.r), funcf(base.g, blend.g), funcf(base.b, blend.b)) \n "
\r
215 "\n#define BlendNormal(base, blend) (blend) \n "
\r
216 "\n#define BlendLighten BlendLightenf \n "
\r
217 "\n#define BlendDarken BlendDarkenf \n "
\r
218 "\n#define BlendMultiply(base, blend) (base * blend) \n "
\r
219 "\n#define BlendAverage(base, blend) ((base + blend) / 2.0) \n "
\r
220 "\n#define BlendAdd(base, blend) min(base + blend, vec3(1.0)) \n "
\r
221 "\n#define BlendSubstract(base, blend) max(base + blend - vec3(1.0), vec3(0.0)) \n "
\r
222 "\n#define BlendDifference(base, blend) abs(base - blend) \n "
\r
223 "\n#define BlendNegation(base, blend) (vec3(1.0) - abs(vec3(1.0) - base - blend)) \n "
\r
224 "\n#define BlendExclusion(base, blend) (base + blend - 2.0 * base * blend) \n "
\r
225 "\n#define BlendScreen(base, blend) Blend(base, blend, BlendScreenf) \n "
\r
226 "\n#define BlendOverlay(base, blend) Blend(base, blend, BlendOverlayf) \n "
\r
227 "\n#define BlendSoftLight(base, blend) Blend(base, blend, BlendSoftLightf) \n "
\r
228 "\n#define BlendHardLight(base, blend) BlendOverlay(blend, base) \n "
\r
229 "\n#define BlendColorDodge(base, blend) Blend(base, blend, BlendColorDodgef) \n "
\r
230 "\n#define BlendColorBurn(base, blend) Blend(base, blend, BlendColorBurnf) \n "
\r
231 "\n#define BlendLinearDodge BlendAdd \n "
\r
232 "\n#define BlendLinearBurn BlendSubstract \n "
\r
233 "\n#define BlendLinearLight(base, blend) Blend(base, blend, BlendLinearLightf) \n "
\r
234 "\n#define BlendVividLight(base, blend) Blend(base, blend, BlendVividLightf) \n "
\r
235 "\n#define BlendPinLight(base, blend) Blend(base, blend, BlendPinLightf) \n "
\r
236 "\n#define BlendHardMix(base, blend) Blend(base, blend, BlendHardMixf) \n "
\r
237 "\n#define BlendReflect(base, blend) Blend(base, blend, BlendReflectf) \n "
\r
238 "\n#define BlendGlow(base, blend) BlendReflect(blend, base) \n "
\r
239 "\n#define BlendPhoenix(base, blend) (min(base, blend) - max(base, blend) + vec3(1.0)) \n "
\r
240 "\n#define BlendOpacity(base, blend, F, O) (F(base, blend) * O + blend * (1.0 - O)) \n "
\r
243 "\n vec3 BlendHue(vec3 base, vec3 blend) "
\r
245 "\n vec3 baseHSL = RGBToHSL(base); "
\r
246 "\n return HSLToRGB(vec3(RGBToHSL(blend).r, baseHSL.g, baseHSL.b)); "
\r
249 "\n vec3 BlendSaturation(vec3 base, vec3 blend) "
\r
251 "\n vec3 baseHSL = RGBToHSL(base); "
\r
252 "\n return HSLToRGB(vec3(baseHSL.r, RGBToHSL(blend).g, baseHSL.b)); "
\r
255 "\n vec3 BlendColor(vec3 base, vec3 blend) "
\r
257 "\n vec3 blendHSL = RGBToHSL(blend); "
\r
258 "\n return HSLToRGB(vec3(blendHSL.r, blendHSL.g, RGBToHSL(base).b)); "
\r
261 "\n vec3 BlendLuminosity(vec3 base, vec3 blend) "
\r
263 "\n vec3 baseHSL = RGBToHSL(base); "
\r
264 "\n return HSLToRGB(vec3(baseHSL.r, baseHSL.g, RGBToHSL(blend).b)); "
\r