2 * Gamma Correction Plugin (RGB Version) 0.01
4 * Copyright (c) 2005 Peter Schlaile
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.
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.
23 #define alpha_epsilon 0.0001f
24 char name[]= "Gamma Correction";
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"},
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"},
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"},
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"},
64 void plugin_seq_doit(Cast *, float, float, int, int, ImBuf *, ImBuf *, ImBuf *, ImBuf *);
66 int plugin_seq_getversion(void) { return B_PLUGIN_VERSION; }
67 void plugin_but_changed(int but) {}
70 void plugin_getinfo(PluginInfo *info)
73 info->nvars= sizeof(varstr)/sizeof(VarStruct);
78 info->init= plugin_init;
79 info->seq_doit= (SeqDoit) plugin_seq_doit;
80 info->callback= plugin_but_changed;
83 static void make_gamma_table(float setup, float gain, float gamma,
84 unsigned char * table)
88 for (y = 0; y < 256; y++) {
89 float v = 1.0 * y / 255;
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)
108 unsigned char *dest, *src1, *src2;
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];
117 dest= (unsigned char *) out->rect;
118 src1= (unsigned char *) ibuf1->rect;
120 make_gamma_table(cast->setup_m, cast->gain_m, cast->gamma_m,
122 make_gamma_table(cast->setup_r, cast->gain_r, cast->gamma_r,
124 make_gamma_table(cast->setup_g, cast->gain_g, cast->gamma_g,
126 make_gamma_table(cast->setup_b, cast->gain_b, cast->gamma_b,
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++]];
140 float *i=ibuf1->rect_float;
141 float *o=out->rect_float;
142 unsigned int size=width*height;
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};
150 if (cast->gamma_m!=1.f || cast->setup_m!=0.f || cast->gain_m!=1.f)
152 float alpha=CLAMP(i[3],0.f,1.f);
153 if (alpha>alpha_epsilon) {
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)
162 o[l]=pow((o[l]+val[0])*val[1],val[2]);
165 o[l]=CLAMP(o[l],0.f,1.f);
176 o[l]=CLAMP(i[l],0.f,1.f);