whitespace only, no functional change mixed tabs/spaces --> tabs.
[blender.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         info->name= name;
72         info->nvars= sizeof(varstr)/sizeof(VarStruct);
73         info->cfra= &cfra;
74
75         info->varstr= varstr;
76
77         info->init= plugin_init;
78         info->seq_doit= (SeqDoit) plugin_seq_doit;
79         info->callback= plugin_but_changed;
80 }
81
82 static void make_gamma_table(float setup, float gain, float gamma,
83                              unsigned char * table)
84 {
85         int y;
86
87         for (y = 0; y < 256; y++) {
88                 float v = 1.0 * y / 255;
89                 v += setup;
90                 v *= gain;
91                 v = pow(v, gamma);
92                 if ( v > 1.0) {
93                         v = 1.0;
94                 } else if (v < 0.0) {
95                         v = 0.0;
96                 }
97                 table[y] = v * 255;
98         }
99
100 }
101
102
103 void plugin_seq_doit(Cast *cast, float facf0, float facf1, int width, 
104         int height, ImBuf *ibuf1, ImBuf *ibuf2, ImBuf *out, ImBuf *use) {
105         if (!out->rect_float)
106         {
107                 unsigned char *dest, *src1, *src2;
108                 int x, y, c;
109                 unsigned char gamma_table_m[256];
110                 unsigned char gamma_table_r[256];
111                 unsigned char gamma_table_g[256];
112                 unsigned char gamma_table_b[256];
113                 
114                 if (!ibuf1) return;
115
116                 dest= (unsigned char *) out->rect;
117                 src1= (unsigned char *) ibuf1->rect;
118
119                 make_gamma_table(cast->setup_m, cast->gain_m, cast->gamma_m,
120                                  gamma_table_m);
121                 make_gamma_table(cast->setup_r, cast->gain_r, cast->gamma_r,
122                                  gamma_table_r);
123                 make_gamma_table(cast->setup_g, cast->gain_g, cast->gamma_g,
124                                  gamma_table_g);
125                 make_gamma_table(cast->setup_b, cast->gain_b, cast->gamma_b,
126                                  gamma_table_b);
127
128                 for (y = 0; y < height; y++) {
129                         for (x = 0; x < width; x++) {
130                                 *dest++ = gamma_table_r[gamma_table_m[*src1++]];
131                                 *dest++ = gamma_table_g[gamma_table_m[*src1++]];
132                                 *dest++ = gamma_table_b[gamma_table_m[*src1++]];
133                                 dest++; src1++;
134                         }
135                 }
136         }
137         else
138         {
139                 float *i=ibuf1->rect_float;
140                 float *o=out->rect_float;
141                 unsigned int size=width*height;
142                 unsigned int k;
143                 float val_r[3]={cast->setup_r,cast->gain_r,cast->gamma_r};
144                 float val_g[3]={cast->setup_g,cast->gain_g,cast->gamma_g};
145                 float val_b[3]={cast->setup_b,cast->gain_b,cast->gamma_b};
146                 float *vals[3]={val_r,val_g,val_b};
147                 for (k=0;k<size;++k)
148                 {
149                         if (cast->gamma_m!=1.f || cast->setup_m!=0.f || cast->gain_m!=1.f)
150                         {
151                                 float alpha=CLAMP(i[3],0.f,1.f);
152                                 if (alpha>alpha_epsilon) {
153                                         int l;
154                                         for (l=0;l<3;++l)
155                                         {
156                                                 float *val=vals[l];
157                                                 o[l]=i[l]/alpha;
158                                                 o[l]=pow((o[l]+cast->setup_m)*cast->gain_m,cast->gamma_m);
159                                                 if (val[2]!=1.f || val[0]!=0.f || val[1]!=1.f)
160                                                 {
161                                                         o[l]=pow((o[l]+val[0])*val[1],val[2]);
162                                                 }
163                                                 o[l]*=alpha;
164                                                 o[l]=CLAMP(o[l],0.f,1.f);
165                                         }
166                                 } else {
167                                         o[0]=o[1]=o[2]=0.0;
168                                 }
169                                 o[3]=1.0;
170                         }
171                         else
172                         {
173                                 int l;
174                                 for (l=0;l<3;++l)
175                                         o[l]=CLAMP(i[l],0.f,1.f);
176                                 o[3]=1.0;
177                         }
178                         i+=4;
179                         o+=4;
180                 }
181         }
182 }