remove whitespace at eol
[goodguy/history.git] / cinelerra-5.1 / plugins / chromakeyhsv / chromakey.sl
1 uniform sampler2D tex;
2 uniform float red;
3 uniform float green;
4 uniform float blue;
5 uniform float in_slope;
6 uniform float out_slope;
7 uniform float tolerance;
8 uniform float tolerance_in;
9 uniform float tolerance_out;
10 uniform float sat;
11 uniform float min_s;
12 uniform float min_s_in;
13 uniform float min_s_out;
14 uniform float min_v;
15 uniform float min_v_in;
16 uniform float min_v_out;
17 uniform float max_v;
18 uniform float max_v_in;
19 uniform float max_v_out;
20 uniform float spill_threshold;
21 uniform float spill_amount;
22 uniform float alpha_offset;
23 uniform float hue_key;
24 uniform float saturation_key;
25 uniform float value_key;
26
27 void main()
28 {
29         vec4 color = texture2D(tex, gl_TexCoord[0].st);
30 /* Contribution to alpha from each component */
31         float alpha_value = 1.0;
32         float alpha_value_max = 1.0;
33         float alpha_hue = 1.0;
34         float alpha_saturation = 1.0;
35         bool has_match = true;
36         vec4 color2;
37
38 /* Convert to HSV */
39         color2 = yuv_to_rgb(color);
40         color2 = rgb_to_hsv(color2);
41
42 /* Hue wrap */
43         if(color2.r <= hue_key - tolerance_in * 180.0)
44                 color2.r += 360;
45         else
46         if(color2.r >= hue_key + tolerance_in * 180.0)
47                 color2.r -= 360;
48
49 /* Hue is completely out of range */
50         if (tolerance == 0.0)
51             alpha_hue = 1.0;
52         else
53         if (abs(color2.r - hue_key) < tolerance_in * 180.0)
54                 alpha_hue = 0.0;
55         else
56         if ((out_slope != 0.0 ) && (abs(color2.r - hue_key) < tolerance * 180.0))
57 /* If using slope, scale alpha between 0 and 1 / 2 */
58                 alpha_hue = abs(color2.r - hue_key) / tolerance / 360.0;
59         else
60         if (abs(color2.r - hue_key) < tolerance_out * 180.0)
61 /* If no slope, scale alpha between 1/2 and 1 */
62                 alpha_hue = abs(color2.r - hue_key) / tolerance_out / 360.0;
63         else
64                 has_match = false;
65
66 /* Test saturation */
67         if (has_match)
68         {
69                 if (min_s == 0.0)
70                         alpha_saturation = 0.0;
71                 else
72                 if ( color2.g - sat >= min_s_in )
73                         alpha_saturation = 0.0;
74                 else
75                 if ((out_slope != 0.0) && ( color2.g - sat > min_s ) )
76                         alpha_saturation = (color2.g - sat - min_s) / (min_s * 2.0);
77                 else
78                 if ( color2.g - sat > min_s_out )
79                         alpha_saturation = (color2.g - sat - min_s_out) / (min_s_out * 2.0);
80                 else
81                         has_match = false;
82         }
83
84 /* Test value over minimum */
85         if (has_match)
86         {
87             if (min_v == 0.0)
88                         alpha_value = 0.0;
89                 else
90                 if ( color2.b >= min_v_in )
91                         alpha_value = 0.0;
92                 else
93                 if ((out_slope != 0.0) && ( color2.b > min_v ) )
94                         alpha_value = (color2.b - min_v) / (min_v * 2.0);
95                 else
96                 if ( color2.b > min_v_out )
97                         alpha_value = (color2.b - min_v_out) / (min_v_out * 2.0);
98                 else
99                         has_match = false;
100         }
101
102 /* Test value under maximum */
103         if (has_match)
104         {
105             if (max_v == 0.0)
106                         alpha_value_max = 1.0;
107             else
108                 if (color2.b <= max_v_in)
109                         alpha_value_max = 0.0;
110             else
111                 if ((out_slope != 0.0) && (color2.b < max_v))
112                         alpha_value_max = (color2.b - max_v) / (max_v * 2.0);
113             else
114                 if (color2.b < max_v_out)
115                         alpha_value_max = (color2.b - max_v_out) / (max_v_out * 2.0);
116             else
117                         has_match = false;
118         }
119
120 /* Take largest component as the alpha */
121         if (has_match)
122                 color2.a = max (max (alpha_hue, alpha_value), max (alpha_saturation, alpha_value_max));
123
124 /* Spill light processing */
125         if ((abs(color2.r - hue_key) < spill_threshold * 180.0) ||
126             ((abs(color2.r - hue_key) > 360.0) &&
127             (abs(color2.r - hue_key) - 360.0 < spill_threshold * 180.0)))
128         {
129 /* Modify saturation based on hue contribution */
130             color2.g = color2.g *
131                         spill_amount *
132                         abs(color2.r - hue_key) /
133                         (spill_threshold * 180.0);
134
135 /* convert back to native colormodel */
136                 color2 = hsv_to_rgb(color2);
137                 color.rgb = rgb_to_yuv(color2).rgb;
138         }
139
140
141 /* Convert mask into image */
142         gl_FragColor = show_mask(color, color2);
143 }
144
145