Fix compilation error of Cycles after recent py node removal
[blender-staging.git] / release / plugins / sequence / color-correction-yuv.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         { NUMSLI|FLO, "Lo T:",  0.25, 0.0, 1.0,
35           "Saturation Shadow Thres"}, 
36         { NUMSLI|FLO, "Hi T:",  0.75, 0.0, 1.0,
37           "Saturation Highlights Thres"}, 
38         { TOG|INT,      "Debug", 0.0,   0.0,    1.0,   
39           "Show curves as overlay"}, 
40 };
41
42 typedef struct Cast {
43         float setup_y;
44         float gain_y;
45         float gamma_y;
46         
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 {
67         info->name= name;
68         info->nvars= sizeof(varstr)/sizeof(VarStruct);
69         info->cfra= &cfra;
70
71         info->varstr= varstr;
72
73         info->init= plugin_init;
74         info->seq_doit= (SeqDoit) plugin_seq_doit;
75         info->callback= plugin_but_changed;
76 }
77
78 static void rgb_to_yuv(float rgb[3], float yuv[3])
79 {
80         yuv[0]= 0.299*rgb[0] + 0.587*rgb[1] + 0.114*rgb[2];
81         yuv[1]= 0.492*(rgb[2] - yuv[0]);
82         yuv[2]= 0.877*(rgb[0] - yuv[0]);
83         
84         /* Normalize */
85         yuv[1] /= 0.436;
86         yuv[2] /= 0.615;
87 }
88
89 static void yuv_to_rgb(float yuv[3], float rgb[3])
90 {
91         yuv[1] *= 0.436;
92         yuv[2] *= 0.615;
93
94         rgb[0] = yuv[2]/0.877 + yuv[0];
95         rgb[2] = yuv[1]/0.492 + yuv[0];
96         rgb[1] = (yuv[0] - 0.299*rgb[0] - 0.114*rgb[2]) / 0.587;
97         if (rgb[0] > 1.0) {
98                 rgb[0] = 1.0;
99         }
100         if (rgb[0] < 0.0) {
101                 rgb[0] = 0.0;
102         }
103         if (rgb[1] > 1.0) {
104                 rgb[1] = 1.0;
105         }
106         if (rgb[1] < 0.0) {
107                 rgb[1] = 0.0;
108         }
109         if (rgb[2] > 1.0) {
110                 rgb[2] = 1.0;
111         }
112         if (rgb[2] < 0.0) {
113                 rgb[2] = 0.0;
114         }
115 }
116
117 void plugin_seq_doit(Cast *cast, float facf0, float facf1, int width, 
118         int height, ImBuf *ibuf1, ImBuf *ibuf2, ImBuf *out, ImBuf *use) {
119         char *dest, *src1, *src2;
120         int x, y, c;
121         float rgb[3];
122         float yuv[3];
123         float gamma_table[256];
124         float uv_table[256];
125         float *destf = out->rect_float;
126         float *src1f;
127         
128         if (!ibuf1) return;
129
130         dest= (char *) out->rect;
131         src1= (char *) ibuf1->rect;
132         src1f= ibuf1->rect_float;
133
134         for (y = 0; y < 256; y++) {
135                 float v = 1.0 * y / 255;
136                 v += cast->setup_y;
137                 v *= cast->gain_y;
138                 v = pow(v, cast->gamma_y);
139                 if ( v > 1.0) {
140                         v = 1.0;
141                 } else if (v < 0.0) {
142                         v = 0.0;
143                 }
144                 gamma_table[y] = v * 255;
145         }
146
147         for (y = 0; y < 256; y++) {
148                 float v = 1.0;
149                 v *= cast->master_sat;
150                 if (y < cast->lo_thres * 255) {
151                         v *= cast->sat_shadows;
152                 } else if (y > cast->hi_thres * 255) {
153                         v *= cast->sat_highlights;
154                 } else {
155                         v *= cast->sat_midtones;
156                 }
157                 uv_table[y] = v;
158         }
159
160
161         for (y = 0; y < height; y++) {
162                 for (x = 0; x < width; x++) {
163                         float fac;
164                         if (out->rect_float) {
165                                 rgb[0]= (float)src1f[0]/255.0;
166                                 rgb[1]= (float)src1f[1]/255.0;
167                                 rgb[2]= (float)src1f[2]/255.0;
168                         } else {
169                                 rgb[0]= (float)src1[0]/255.0;
170                                 rgb[1]= (float)src1[1]/255.0;
171                                 rgb[2]= (float)src1[2]/255.0;
172                         }
173                         rgb_to_yuv(rgb, yuv);
174
175                         yuv[0] = gamma_table[(int) (yuv[0] * 255.0)] / 255.0;
176                         fac = uv_table[(int) (255.0 * yuv[0])];
177
178                         yuv[1] = yuv[1] * fac;
179                         yuv[2] = yuv[2] * fac;
180                         if (yuv[1] > 1.0) {
181                                 yuv[1] = 1.0;
182                         }
183                         if (yuv[1] < -1.0) {
184                                 yuv[1] = -1.0;
185                         }
186                         if (yuv[2] > 1.0) {
187                                 yuv[2] = 1.0;
188                         }
189                         if (yuv[2] < -1.0) {
190                                 yuv[2] = -1.0;
191                         }
192                         yuv_to_rgb(yuv, rgb);
193                         
194                         if (out->rect_float) {
195                                 *destf++ = rgb[0];
196                                 *destf++ = rgb[1];
197                                 *destf++ = rgb[2];
198                                 destf++;
199                                 src1f += 4;
200                         } else {
201                                 *dest++ = rgb[0]*255.0;
202                                 *dest++ = rgb[1]*255.0;
203                                 *dest++ = rgb[2]*255.0;
204                                 dest++;
205                                 src1 += 4;
206                         }
207                 }
208         }
209
210         if (cast->debug) {
211                 dest= (char *) out->rect;
212                 for (c = 0; c < 10; c++) {
213                         x = 0;
214                         for (y = 0; y < 256; y++) {
215                                 char val = gamma_table[y];
216                                 while (x < y * width / 255) {
217                                         *dest++ = val;
218                                         *dest++ = val;
219                                         *dest++ = val;
220                                         dest++;
221                                         x++;
222                                 }
223                         }
224                 }
225                 for (c = 0; c < 10; c++) {
226                         x = 0;
227                         for (y = 0; y < 256; y++) {
228                                 char val = uv_table[y] * 255.0/10.0;
229                                 while (x < y * width / 255) {
230                                         *dest++ = val;
231                                         *dest++ = val;
232                                         *dest++ = val;
233                                         dest++;
234                                         x++;
235                                 }
236                         }
237                 }
238         }
239 }