Simplified the ntreeUpdate function by getting rid of the dependency list and limitin...
[blender-staging.git] / release / plugins / sequence / gamma.c
1 /*
2  * Gamma Correction Plugin (RGB 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 "util.h"
21 #include <stdio.h>
22
23 #define alpha_epsilon 0.0001f
24 char name[]= "Gamma Correction";
25
26 VarStruct varstr[]= {
27         { NUMSLI|FLO, "St M:", 0.0,     -1.0,   1.0, "Setup Main"}, 
28         { NUMSLI|FLO, "Gn M:",  1.0,    0.0,    10.0,"Gain Main"},
29         { NUMSLI|FLO, "Ga M:", 1.0,     0.0,    10.0, "Gamma Main"},
30
31         { NUMSLI|FLO, "St R:", 0.0,     -1.0,   1.0, "Setup Red"}, 
32         { NUMSLI|FLO, "Gn R:",  1.0,    0.0,    10.0,"Gain Red"},
33         { NUMSLI|FLO, "Ga R:", 1.0,     0.0,    10.0, "Gamma Red"},
34
35         { NUMSLI|FLO, "St G:", 0.0,     -1.0,   1.0, "Setup Green"}, 
36         { NUMSLI|FLO, "Gn G:",  1.0,    0.0,    10.0,"Gain Green"},
37         { NUMSLI|FLO, "Ga G:", 1.0,     0.0,    10.0, "Gamma Green"},
38
39         { NUMSLI|FLO, "St B:", 0.0,     -1.0,   1.0, "Setup Blue"}, 
40         { NUMSLI|FLO, "Gn B:",  1.0,    0.0,    10.0,"Gain Blue"},
41         { NUMSLI|FLO, "Ga B:", 1.0,     0.0,    10.0, "Gamma Blue"},
42 };
43
44 typedef struct Cast {
45         float setup_m;
46         float gain_m;
47         float gamma_m;
48
49         float setup_r;
50         float gain_r;
51         float gamma_r;
52
53         float setup_g;
54         float gain_g;
55         float gamma_g;
56
57         float setup_b;
58         float gain_b;
59         float gamma_b;
60 } Cast;
61
62 float cfra;
63
64 void plugin_seq_doit(Cast *, float, float, int, int, ImBuf *, ImBuf *, ImBuf *, ImBuf *);
65
66 int plugin_seq_getversion(void) { return B_PLUGIN_VERSION; }
67 void plugin_but_changed(int but) {}
68 void plugin_init() {}
69
70 void plugin_getinfo(PluginInfo *info)
71 {
72         info->name= name;
73         info->nvars= sizeof(varstr)/sizeof(VarStruct);
74         info->cfra= &cfra;
75
76         info->varstr= varstr;
77
78         info->init= plugin_init;
79         info->seq_doit= (SeqDoit) plugin_seq_doit;
80         info->callback= plugin_but_changed;
81 }
82
83 static void make_gamma_table(float setup, float gain, float gamma,
84                              unsigned char * table)
85 {
86         int y;
87
88         for (y = 0; y < 256; y++) {
89                 float v = 1.0 * y / 255;
90                 v += setup;
91                 v *= gain;
92                 v = pow(v, gamma);
93                 if ( v > 1.0) {
94                         v = 1.0;
95                 } else if (v < 0.0) {
96                         v = 0.0;
97                 }
98                 table[y] = v * 255;
99         }
100
101 }
102
103
104 void plugin_seq_doit(Cast *cast, float facf0, float facf1, int width, 
105         int height, ImBuf *ibuf1, ImBuf *ibuf2, ImBuf *out, ImBuf *use) {
106         if (!out->rect_float)
107         {
108                 unsigned char *dest, *src1, *src2;
109                 int x, y, c;
110                 unsigned char gamma_table_m[256];
111                 unsigned char gamma_table_r[256];
112                 unsigned char gamma_table_g[256];
113                 unsigned char gamma_table_b[256];
114                 
115                 if (!ibuf1) return;
116
117                 dest= (unsigned char *) out->rect;
118                 src1= (unsigned char *) ibuf1->rect;
119
120                 make_gamma_table(cast->setup_m, cast->gain_m, cast->gamma_m,
121                                  gamma_table_m);
122                 make_gamma_table(cast->setup_r, cast->gain_r, cast->gamma_r,
123                                  gamma_table_r);
124                 make_gamma_table(cast->setup_g, cast->gain_g, cast->gamma_g,
125                                  gamma_table_g);
126                 make_gamma_table(cast->setup_b, cast->gain_b, cast->gamma_b,
127                                  gamma_table_b);
128
129                 for (y = 0; y < height; y++) {
130                         for (x = 0; x < width; x++) {
131                                 *dest++ = gamma_table_r[gamma_table_m[*src1++]];
132                                 *dest++ = gamma_table_g[gamma_table_m[*src1++]];
133                                 *dest++ = gamma_table_b[gamma_table_m[*src1++]];
134                                 dest++; src1++;
135                         }
136                 }
137         }
138         else
139         {
140                 float *i=ibuf1->rect_float;
141                 float *o=out->rect_float;
142                 unsigned int size=width*height;
143                 unsigned int k;
144                 float val_r[3]={cast->setup_r,cast->gain_r,cast->gamma_r};
145                 float val_g[3]={cast->setup_g,cast->gain_g,cast->gamma_g};
146                 float val_b[3]={cast->setup_b,cast->gain_b,cast->gamma_b};
147                 float *vals[3]={val_r,val_g,val_b};
148                 for (k=0;k<size;++k)
149                 {
150                         if (cast->gamma_m!=1.f || cast->setup_m!=0.f || cast->gain_m!=1.f)
151                         {
152                                 float alpha=CLAMP(i[3],0.f,1.f);
153                                 if (alpha>alpha_epsilon) {
154                                         int l;
155                                         for (l=0;l<3;++l)
156                                         {
157                                                 float *val=vals[l];
158                                                 o[l]=i[l]/alpha;
159                                                 o[l]=pow((o[l]+cast->setup_m)*cast->gain_m,cast->gamma_m);
160                                                 if (val[2]!=1.f || val[0]!=0.f || val[1]!=1.f)
161                                                 {
162                                                         o[l]=pow((o[l]+val[0])*val[1],val[2]);
163                                                 }
164                                                 o[l]*=alpha;
165                                                 o[l]=CLAMP(o[l],0.f,1.f);
166                                         }
167                                 } else {
168                                         o[0]=o[1]=o[2]=0.0;
169                                 }
170                                 o[3]=1.0;
171                         }
172                         else
173                         {
174                                 int l;
175                                 for (l=0;l<3;++l)
176                                         o[l]=CLAMP(i[l],0.f,1.f);
177                                 o[3]=1.0;
178                         }
179                         i+=4;
180                         o+=4;
181                 }
182         }
183 }