Add several usefull sequencer-plugins:
[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         
123         if (!ibuf1) return;
124
125         dest= (char *) out->rect;
126         src1= (char *) ibuf1->rect;
127
128         for (y = 0; y < 256; y++) {
129                 float v = 1.0 * y / 255;
130                 v += cast->setup_y;
131                 v *= cast->gain_y;
132                 v = pow(v, cast->gamma_y);
133                 if ( v > 1.0) {
134                         v = 1.0;
135                 } else if (v < 0.0) {
136                         v = 0.0;
137                 }
138                 gamma_table[y] = v * 255;
139         }
140
141         for (y = 0; y < 256; y++) {
142                 float v = 1.0;
143                 v *= cast->master_sat;
144                 if (y < cast->lo_thres * 255) {
145                         v *= cast->sat_shadows;
146                 } else if (y > cast->hi_thres * 255) {
147                         v *= cast->sat_highlights;
148                 } else {
149                         v *= cast->sat_midtones;
150                 }
151                 uv_table[y] = v;
152         }
153
154
155         for (y = 0; y < height; y++) {
156                 for (x = 0; x < width; x++) {
157                         float fac;
158                         rgb[0]= (float)src1[0]/255.0;
159                         rgb[1]= (float)src1[1]/255.0;
160                         rgb[2]= (float)src1[2]/255.0;
161                         rgb_to_yuv(rgb, yuv);
162
163                         yuv[0] = gamma_table[(int) (yuv[0] * 255.0)] / 255.0;
164                         fac = uv_table[(int) (255.0 * yuv[0])];
165
166                         yuv[1] = yuv[1] * fac;
167                         yuv[2] = yuv[2] * fac;
168                         if (yuv[1] > 1.0) {
169                                 yuv[1] = 1.0;
170                         }
171                         if (yuv[1] < -1.0) {
172                                 yuv[1] = -1.0;
173                         }
174                         if (yuv[2] > 1.0) {
175                                 yuv[2] = 1.0;
176                         }
177                         if (yuv[2] < -1.0) {
178                                 yuv[2] = -1.0;
179                         }
180                         yuv_to_rgb(yuv, rgb);
181                         
182                         *dest++ = rgb[0]*255.0;
183                         *dest++ = rgb[1]*255.0;
184                         *dest++ = rgb[2]*255.0;
185                         dest++;
186
187                         src1 += 4;
188                 }
189         }
190
191         if (cast->debug) {
192                 dest= (char *) out->rect;
193                 for (c = 0; c < 10; c++) {
194                         x = 0;
195                         for (y = 0; y < 256; y++) {
196                                 char val = gamma_table[y];
197                                 while (x < y * width / 255) {
198                                         *dest++ = val;
199                                         *dest++ = val;
200                                         *dest++ = val;
201                                         dest++;
202                                         x++;
203                                 }
204                         }
205                 }
206                 for (c = 0; c < 10; c++) {
207                         x = 0;
208                         for (y = 0; y < 256; y++) {
209                                 char val = uv_table[y] * 255.0/10.0;
210                                 while (x < y * width / 255) {
211                                         *dest++ = val;
212                                         *dest++ = val;
213                                         *dest++ = val;
214                                         dest++;
215                                         x++;
216                                 }
217                         }
218                 }
219         }
220 }