Select interior faces, access from the mesh select menu
[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;
186         int x, y, c;
187         double gamma_table[256];
188         double uv_table[256];
189         float *destf = out->rect_float;
190         float *src1f = ibuf1->rect_float;
191         
192         if (!ibuf1) return;
193
194         dest= (char *) out->rect;
195         src1= (char *) ibuf1->rect;
196
197         for (y = 0; y < 256; y++) {
198                 float v = 1.0 * y / 255;
199                 v += cast->setup_y;
200                 v *= cast->gain_y;
201                 v = pow(v, cast->gamma_y);
202                 if ( v > 1.0) {
203                         v = 1.0;
204                 } else if (v < 0.0) {
205                         v = 0.0;
206                 }
207                 gamma_table[y] = v * 255;
208         }
209
210         for (y = 0; y < 256; y++) {
211                 float v = 1.0;
212                 v *= cast->master_sat;
213                 if (y < cast->lo_thres * 255) {
214                         v *= cast->sat_shadows;
215                 } else if (y > cast->hi_thres * 255) {
216                         v *= cast->sat_highlights;
217                 } else {
218                         v *= cast->sat_midtones;
219                 }
220                 uv_table[y] = v;
221         }
222
223
224         for (y = 0; y < height; y++) {
225                 for (x = 0; x < width; x++) {
226                         double h,s,v,r,g,b;
227                         double fac;
228
229                         if (ibuf1->rect_float) rgb_to_hsv(src1f[0], src1f[1],
230                                 src1f[2],&h,&s,&v);
231                         else rgb_to_hsv((double) src1[0]/255.0,
232                                    (double) src1[1]/255.0,
233                                    (double) src1[2]/255.0,
234                                    &h, &s, &v);
235                         v = gamma_table[(int) (v * 255.0)] / 255.0;
236
237                         fac = uv_table[(int) (255.0 * v)];
238
239                         s *= fac;
240                         if (s >= 1.0) {
241                                 s = 1.0;
242                         }
243                         hsv_to_rgb(h,s,v, &r, &g, &b);
244                         
245                         if (out->rect_float) {
246                                 destf[0] = r;
247                                 destf[1] = g;
248                                 destf[2] = b;
249                                 destf = destf + 4;
250                                 src1f +=4;
251                         } else {
252                                 dest[0] = r*255.0;
253                                 dest[1] = g*255.0;
254                                 dest[2] = b*255.0;
255                                 dest += 4;
256                         }
257
258                         src1 += 4;
259                 }
260         }
261
262         if (cast->debug) {
263                 dest= (char *) out->rect;
264                 for (c = 0; c < 10; c++) {
265                         x = 0;
266                         for (y = 0; y < 256; y++) {
267                                 char val = gamma_table[y];
268                                 while (x < y * width / 255) {
269                                         *dest++ = val;
270                                         *dest++ = val;
271                                         *dest++ = val;
272                                         dest++;
273                                         x++;
274                                 }
275                         }
276                 }
277                 for (c = 0; c < 10; c++) {
278                         x = 0;
279                         for (y = 0; y < 256; y++) {
280                                 char val = uv_table[y] * 255.0/10.0;
281                                 while (x < y * width / 255) {
282                                         *dest++ = val;
283                                         *dest++ = val;
284                                         *dest++ = val;
285                                         dest++;
286                                         x++;
287                                 }
288                         }
289                 }
290         }
291 }