svn merge -r 16592:16667 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[blender.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         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 rgb_to_yuv(float rgb[3], float yuv[3]) {
78         yuv[0]= 0.299*rgb[0] + 0.587*rgb[1] + 0.114*rgb[2];
79         yuv[1]= 0.492*(rgb[2] - yuv[0]);
80         yuv[2]= 0.877*(rgb[0] - yuv[0]);
81         
82         /* Normalize */
83         yuv[1] /= 0.436;
84         yuv[2] /= 0.615;
85 }
86
87 static void yuv_to_rgb(float yuv[3], float rgb[3]) {
88         yuv[1] *= 0.436;
89         yuv[2] *= 0.615;
90
91         rgb[0] = yuv[2]/0.877 + yuv[0];
92         rgb[2] = yuv[1]/0.492 + yuv[0];
93         rgb[1] = (yuv[0] - 0.299*rgb[0] - 0.114*rgb[2]) / 0.587;
94         if (rgb[0] > 1.0) {
95                 rgb[0] = 1.0;
96         }
97         if (rgb[0] < 0.0) {
98                 rgb[0] = 0.0;
99         }
100         if (rgb[1] > 1.0) {
101                 rgb[1] = 1.0;
102         }
103         if (rgb[1] < 0.0) {
104                 rgb[1] = 0.0;
105         }
106         if (rgb[2] > 1.0) {
107                 rgb[2] = 1.0;
108         }
109         if (rgb[2] < 0.0) {
110                 rgb[2] = 0.0;
111         }
112 }
113
114 void plugin_seq_doit(Cast *cast, float facf0, float facf1, int width, 
115         int height, ImBuf *ibuf1, ImBuf *ibuf2, ImBuf *out, ImBuf *use) {
116         char *dest, *src1, *src2;
117         int x, y, c;
118         float rgb[3];
119         float yuv[3];
120         float gamma_table[256];
121         float uv_table[256];
122         float *destf = out->rect_float;
123         float *src1f = ibuf1->rect_float;
124         
125         if (!ibuf1) return;
126
127         dest= (char *) out->rect;
128         src1= (char *) ibuf1->rect;
129
130         for (y = 0; y < 256; y++) {
131                 float v = 1.0 * y / 255;
132                 v += cast->setup_y;
133                 v *= cast->gain_y;
134                 v = pow(v, cast->gamma_y);
135                 if ( v > 1.0) {
136                         v = 1.0;
137                 } else if (v < 0.0) {
138                         v = 0.0;
139                 }
140                 gamma_table[y] = v * 255;
141         }
142
143         for (y = 0; y < 256; y++) {
144                 float v = 1.0;
145                 v *= cast->master_sat;
146                 if (y < cast->lo_thres * 255) {
147                         v *= cast->sat_shadows;
148                 } else if (y > cast->hi_thres * 255) {
149                         v *= cast->sat_highlights;
150                 } else {
151                         v *= cast->sat_midtones;
152                 }
153                 uv_table[y] = v;
154         }
155
156
157         for (y = 0; y < height; y++) {
158                 for (x = 0; x < width; x++) {
159                         float fac;
160                         if (out->rect_float) {
161                                 rgb[0]= (float)src1f[0]/255.0;
162                                 rgb[1]= (float)src1f[1]/255.0;
163                                 rgb[2]= (float)src1f[2]/255.0;
164                         } else {
165                                 rgb[0]= (float)src1[0]/255.0;
166                                 rgb[1]= (float)src1[1]/255.0;
167                                 rgb[2]= (float)src1[2]/255.0;
168                         }
169                         rgb_to_yuv(rgb, yuv);
170
171                         yuv[0] = gamma_table[(int) (yuv[0] * 255.0)] / 255.0;
172                         fac = uv_table[(int) (255.0 * yuv[0])];
173
174                         yuv[1] = yuv[1] * fac;
175                         yuv[2] = yuv[2] * fac;
176                         if (yuv[1] > 1.0) {
177                                 yuv[1] = 1.0;
178                         }
179                         if (yuv[1] < -1.0) {
180                                 yuv[1] = -1.0;
181                         }
182                         if (yuv[2] > 1.0) {
183                                 yuv[2] = 1.0;
184                         }
185                         if (yuv[2] < -1.0) {
186                                 yuv[2] = -1.0;
187                         }
188                         yuv_to_rgb(yuv, rgb);
189                         
190                         if (out->rect_float) {
191                                 *destf++ = rgb[0];
192                                 *destf++ = rgb[1];
193                                 *destf++ = rgb[2];
194                                 destf++;
195                                 src1f += 4;
196                         } else {
197                                 *dest++ = rgb[0]*255.0;
198                                 *dest++ = rgb[1]*255.0;
199                                 *dest++ = rgb[2]*255.0;
200                                 dest++;
201                                 src1 += 4;
202                         }
203                 }
204         }
205
206         if (cast->debug) {
207                 dest= (char *) out->rect;
208                 for (c = 0; c < 10; c++) {
209                         x = 0;
210                         for (y = 0; y < 256; y++) {
211                                 char val = gamma_table[y];
212                                 while (x < y * width / 255) {
213                                         *dest++ = val;
214                                         *dest++ = val;
215                                         *dest++ = val;
216                                         dest++;
217                                         x++;
218                                 }
219                         }
220                 }
221                 for (c = 0; c < 10; c++) {
222                         x = 0;
223                         for (y = 0; y < 256; y++) {
224                                 char val = uv_table[y] * 255.0/10.0;
225                                 while (x < y * width / 255) {
226                                         *dest++ = val;
227                                         *dest++ = val;
228                                         *dest++ = val;
229                                         dest++;
230                                         x++;
231                                 }
232                         }
233                 }
234         }
235 }