in seqeffects.c I removed and extra ; and then did a lot of formatting
[blender.git] / release / plugins / sequence / color-correction-hsv.c
1 /*
2  * Color Correction Plugin (YUV Version) 0.01
3  *
4  * Copyright (c) 2005 Peter Schlaile
5  *
6  * This program 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 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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  */
17
18 #include "math.h"
19 #include "plugin.h"
20 #include <stdio.h>
21
22 char name[]= "Color Correction";
23
24 VarStruct varstr[]= {
25         { NUMSLI|FLO, "St Y:", 0.0,     -1.0,   1.0, "Setup Y"}, 
26         { NUMSLI|FLO, "Gn Y:",  1.0,    0.0,    10.0,"Gain Y"},
27         { NUMSLI|FLO, "Ga Y:", 1.0,     0.0,    10.0, "Gamma Y"},
28
29         { NUMSLI|FLO, "Lo S:",  1.0,    0.0,    10.0,"Saturation Shadows"},
30         { NUMSLI|FLO, "Md S:",  1.0,    0.0,    10.0,"Saturation Midtones"},
31         { NUMSLI|FLO, "Hi S:",  1.0,    0.0,    10.0,"Saturation Highlights"},
32
33         { NUMSLI|FLO, "MA S:",  1.0,    0.0,    10.0,"Master Saturation"}, 
34
35         { NUMSLI|FLO, "Lo T:",  0.25, 0.0, 1.0,
36           "Saturation Shadow Thres"}, 
37         { NUMSLI|FLO, "Hi T:",  0.75, 0.0, 1.0,
38           "Saturation Highlights Thres"}, 
39         { TOG|INT,      "Debug", 0.0,   0.0,    1.0,   
40           "Show curves as overlay."}, 
41 };
42
43 typedef struct Cast {
44         float setup_y;
45         float gain_y;
46         float gamma_y;
47         float sat_shadows;
48         float sat_midtones;
49         float sat_highlights;
50
51         float master_sat;
52         float lo_thres;
53         float hi_thres;
54         int debug;
55 } Cast;
56
57 float cfra;
58
59 void plugin_seq_doit(Cast *, float, float, int, int, ImBuf *, ImBuf *, ImBuf *, ImBuf *);
60
61 int plugin_seq_getversion(void) { return B_PLUGIN_VERSION;}
62 void plugin_but_changed(int but) {}
63 void plugin_init() {}
64
65 void plugin_getinfo(PluginInfo *info) {
66         info->name= name;
67         info->nvars= sizeof(varstr)/sizeof(VarStruct);
68         info->cfra= &cfra;
69
70         info->varstr= varstr;
71
72         info->init= plugin_init;
73         info->seq_doit= (SeqDoit) plugin_seq_doit;
74         info->callback= plugin_but_changed;
75 }
76
77 static void hsv_to_rgb (double  h, double  s, double  v,
78                         double *r, double *g, double *b)
79 {
80         int i;
81         double f, w, q, t;
82
83         if (s == 0.0)
84                 s = 0.000001;
85         
86         if (h == -1.0)
87         {
88                 *r = v;
89                 *g = v;
90                 *b = v;
91         }
92         else
93         {
94                 if (h == 360.0)
95                         h = 0.0;
96                 h = h / 60.0;
97                 i = (int) h;
98                 f = h - i;
99                 w = v * (1.0 - s);
100                 q = v * (1.0 - (s * f));
101                 t = v * (1.0 - (s * (1.0 - f)));
102                 
103                 switch (i)
104                 {
105                 case 0:
106                         *r = v;
107                         *g = t;
108                         *b = w;
109                         break;
110                 case 1:
111                         *r = q;
112                         *g = v;
113                         *b = w;
114                         break;
115                 case 2:
116                         *r = w;
117                         *g = v;
118                         *b = t;
119                         break;
120                 case 3:
121                         *r = w;
122                         *g = q;
123                         *b = v;
124                         break;
125                 case 4:
126                         *r = t;
127                         *g = w;
128                         *b = v;
129                         break;
130                 case 5:
131                         *r = v;
132                         *g = w;
133                         *b = q;
134                         break;
135                 }
136         }
137 }
138
139 static void rgb_to_hsv (double  r, double  g, double  b,
140                         double *h, double *s, double *v)
141 {
142         double max, min, delta;
143
144         max = r;
145         if (g > max)
146                 max = g;
147         if (b > max)
148                 max = b;
149         
150         min = r;
151         if (g < min)
152                 min = g;
153         if (b < min)
154                 min = b;
155         
156         *v = max;
157         
158         if (max != 0.0)
159                 *s = (max - min) / max;
160         else
161                 *s = 0.0;
162         
163         if (*s == 0.0)
164                 *h = -1.0;
165         else
166         {
167                 delta = max - min;
168                 
169                 if (r == max)
170                         *h = (g - b) / delta;
171                 else if (g == max)
172                         *h = 2.0 + (b - r) / delta;
173                 else if (b == max)
174                         *h = 4.0 + (r - g) / delta;
175                 
176                 *h = *h * 60.0;
177                 
178                 if (*h < 0.0)
179                         *h = *h + 360;
180         }
181 }
182
183 void plugin_seq_doit(Cast *cast, float facf0, float facf1, int width, 
184         int height, ImBuf *ibuf1, ImBuf *ibuf2, ImBuf *out, ImBuf *use) {
185         char *dest, *src1, *src2;
186         int x, y, c;
187         double gamma_table[256];
188         double uv_table[256];
189         
190         if (!ibuf1) return;
191
192         dest= (char *) out->rect;
193         src1= (char *) ibuf1->rect;
194
195         for (y = 0; y < 256; y++) {
196                 float v = 1.0 * y / 255;
197                 v += cast->setup_y;
198                 v *= cast->gain_y;
199                 v = pow(v, cast->gamma_y);
200                 if ( v > 1.0) {
201                         v = 1.0;
202                 } else if (v < 0.0) {
203                         v = 0.0;
204                 }
205                 gamma_table[y] = v * 255;
206         }
207
208         for (y = 0; y < 256; y++) {
209                 float v = 1.0;
210                 v *= cast->master_sat;
211                 if (y < cast->lo_thres * 255) {
212                         v *= cast->sat_shadows;
213                 } else if (y > cast->hi_thres * 255) {
214                         v *= cast->sat_highlights;
215                 } else {
216                         v *= cast->sat_midtones;
217                 }
218                 uv_table[y] = v;
219         }
220
221
222         for (y = 0; y < height; y++) {
223                 for (x = 0; x < width; x++) {
224                         double h,s,v,r,g,b;
225                         double fac;
226
227                         rgb_to_hsv((double) src1[0]/255.0,
228                                    (double) src1[1]/255.0,
229                                    (double) src1[2]/255.0,
230                                    &h, &s, &v);
231                         v = gamma_table[(int) (v * 255.0)] / 255.0;
232
233                         fac = uv_table[(int) (255.0 * v)];
234
235                         s *= fac;
236                         if (s >= 1.0) {
237                                 s = 1.0;
238                         }
239                         hsv_to_rgb(h,s,v, &r, &g, &b);
240                         
241                         *dest++ = r*255.0;
242                         *dest++ = g*255.0;
243                         *dest++ = b*255.0;
244                         dest++;
245
246                         src1 += 4;
247                 }
248         }
249
250         if (cast->debug) {
251                 dest= (char *) out->rect;
252                 for (c = 0; c < 10; c++) {
253                         x = 0;
254                         for (y = 0; y < 256; y++) {
255                                 char val = gamma_table[y];
256                                 while (x < y * width / 255) {
257                                         *dest++ = val;
258                                         *dest++ = val;
259                                         *dest++ = val;
260                                         dest++;
261                                         x++;
262                                 }
263                         }
264                 }
265                 for (c = 0; c < 10; c++) {
266                         x = 0;
267                         for (y = 0; y < 256; y++) {
268                                 char val = uv_table[y] * 255.0/10.0;
269                                 while (x < y * width / 255) {
270                                         *dest++ = val;
271                                         *dest++ = val;
272                                         *dest++ = val;
273                                         dest++;
274                                         x++;
275                                 }
276                         }
277                 }
278         }
279 }