tweak to color balance after talking with colin and testing other software, lift...
[blender.git] / source / blender / nodes / intern / CMP_nodes / CMP_colorbalance.c
1 /**
2  * 
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (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  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2006 Blender Foundation.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): Matt Ebb.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30
31 #include "../CMP_util.h"
32
33
34 /* ******************* Color Balance ********************************* */
35 static bNodeSocketType cmp_node_colorbalance_in[]={
36         {SOCK_VALUE, 1, "Fac",  1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
37         {SOCK_RGBA,1,"Image", 0.8f, 0.8f, 0.8f, 1.0f, 0.0f, 1.0f},
38         {-1,0,""}
39 };
40
41 static bNodeSocketType cmp_node_colorbalance_out[]={
42         {SOCK_RGBA,0,"Image", 0.0f, 0.0f, 1.0f, 1.0f, -1.0f, 1.0f},
43         {-1,0,""}
44 };
45
46 DO_INLINE float colorbalance_cdl(float in, float offset, float power, float slope)
47 {
48         float x = in * slope + offset;
49         
50         /* prevent NaN */
51         if (x < 0.f) x = 0.f;
52         
53         //powf(in * slope + offset, power)
54         return powf(x, 1.f/power);
55 }
56
57 /* note: lift_lgg is just 2-lift */
58 DO_INLINE float colorbalance_lgg(float in, float lift_lgg, float gamma, float gain)
59 {       
60         float x= (((in - 1.0f) * lift_lgg) + 1.0f) * gain;
61
62         /* prevent NaN */
63         if (x < 0.f) x = 0.f;
64         
65         return powf(x, (1.f/gamma));
66 }
67
68 static void do_colorbalance_cdl(bNode *node, float* out, float *in)
69 {
70         NodeColorBalance *n= (NodeColorBalance *)node->storage;
71         
72         out[0] = colorbalance_cdl(in[0], n->lift[0], n->gamma[0], n->gain[0]);
73         out[1] = colorbalance_cdl(in[1], n->lift[1], n->gamma[1], n->gain[1]);
74         out[2] = colorbalance_cdl(in[2], n->lift[2], n->gamma[2], n->gain[2]);
75         out[3] = in[3];
76 }
77
78 static void do_colorbalance_cdl_fac(bNode *node, float* out, float *in, float *fac)
79 {
80         NodeColorBalance *n= (NodeColorBalance *)node->storage;
81         const float mfac= 1.0f - *fac;
82         
83         out[0] = mfac*in[0] + *fac * colorbalance_cdl(in[0], n->lift[0], n->gamma[0], n->gain[0]);
84         out[1] = mfac*in[1] + *fac * colorbalance_cdl(in[1], n->lift[1], n->gamma[1], n->gain[1]);
85         out[2] = mfac*in[2] + *fac * colorbalance_cdl(in[2], n->lift[2], n->gamma[2], n->gain[2]);
86         out[3] = in[3];
87 }
88
89 static void do_colorbalance_lgg(bNode *node, float* out, float *in)
90 {
91         NodeColorBalance *n= (NodeColorBalance *)node->storage;
92
93         out[0] = colorbalance_lgg(in[0], n->lift_lgg[0], n->gamma[0], n->gain[0]);
94         out[1] = colorbalance_lgg(in[1], n->lift_lgg[1], n->gamma[1], n->gain[1]);
95         out[2] = colorbalance_lgg(in[2], n->lift_lgg[2], n->gamma[2], n->gain[2]);
96         out[3] = in[3];
97 }
98
99 static void do_colorbalance_lgg_fac(bNode *node, float* out, float *in, float *fac)
100 {
101         NodeColorBalance *n= (NodeColorBalance *)node->storage;
102         const float mfac= 1.0f - *fac;
103
104         out[0] = mfac*in[0] + *fac * colorbalance_lgg(in[0], n->lift_lgg[0], n->gamma[0], n->gain[0]);
105         out[1] = mfac*in[1] + *fac * colorbalance_lgg(in[1], n->lift_lgg[1], n->gamma[1], n->gain[1]);
106         out[2] = mfac*in[2] + *fac * colorbalance_lgg(in[2], n->lift_lgg[2], n->gamma[2], n->gain[2]);
107         out[3] = in[3];
108 }
109
110 static void node_composit_exec_colorbalance(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
111 {
112         CompBuf *cbuf= in[1]->data;
113         CompBuf *stackbuf;
114         
115         /* stack order input:  fac, image */
116         /* stack order output: image */
117         if(out[0]->hasoutput==0) return;
118         
119         if(in[0]->vec[0] == 0.f && in[0]->data == NULL) {
120                 out[0]->data = pass_on_compbuf(cbuf);
121                 return;
122         }
123
124         {
125                 NodeColorBalance *n= (NodeColorBalance *)node->storage;
126                 int c;
127
128                 copy_v3_v3(n->lift_lgg, n->lift);
129
130                 for (c = 0; c < 3; c++) {
131                         /* tweak to give more subtle results
132                          * values above 1.0 are scaled */
133                         if(n->lift_lgg[c] > 1.0f)
134                                 n->lift_lgg[c] = pow(n->lift_lgg[c] - 1.0f, 2.0f) + 1.0f;
135
136                         n->lift_lgg[c] = 2.0f - n->lift_lgg[c];
137                 }
138         }
139
140         if (cbuf) {
141                 stackbuf= alloc_compbuf(cbuf->x, cbuf->y, CB_RGBA, 1); /* create output based on image input */
142                         
143                 if (node->custom1 == 0) {
144                         /* lift gamma gain */
145                         if ((in[0]->data==NULL) && (in[0]->vec[0] >= 1.f)) {
146                                 composit1_pixel_processor(node, stackbuf, in[1]->data, in[1]->vec, do_colorbalance_lgg, CB_RGBA);
147                         }
148                         else {
149                                 composit2_pixel_processor(node, stackbuf, in[1]->data, in[1]->vec, in[0]->data, in[0]->vec, do_colorbalance_lgg_fac, CB_RGBA, CB_VAL);
150                         }
151                 } else {
152                         /* offset/power/slope : ASC-CDL */
153                         if ((in[0]->data==NULL) && (in[0]->vec[0] >= 1.f)) {
154                                 composit1_pixel_processor(node, stackbuf, in[1]->data, in[1]->vec, do_colorbalance_cdl, CB_RGBA);
155                         }
156                         else {
157                                 composit2_pixel_processor(node, stackbuf, in[1]->data, in[1]->vec, in[0]->data, in[0]->vec, do_colorbalance_cdl_fac, CB_RGBA, CB_VAL);
158                         }
159                         
160                 }
161
162                 out[0]->data=stackbuf;
163         }
164 }
165
166 static void node_composit_init_colorbalance(bNode *node)
167 {
168         NodeColorBalance *n= node->storage= MEM_callocN(sizeof(NodeColorBalance), "node colorbalance");
169
170         n->lift[0] = n->lift[1] = n->lift[2] = 1.0f;
171         n->gamma[0] = n->gamma[1] = n->gamma[2] = 1.0f;
172         n->gain[0] = n->gain[1] = n->gain[2] = 1.0f;
173 }
174
175 bNodeType cmp_node_colorbalance={
176         /* *next,*prev */       NULL, NULL,
177         /* type code   */       CMP_NODE_COLORBALANCE,
178         /* name        */       "Color Balance",
179         /* width+range */       400, 200, 400,
180         /* class+opts  */       NODE_CLASS_OP_COLOR, NODE_OPTIONS,
181         /* input sock  */       cmp_node_colorbalance_in,
182         /* output sock */       cmp_node_colorbalance_out,
183         /* storage     */       "NodeColorBalance",
184         /* execfunc    */       node_composit_exec_colorbalance,
185         /* butfunc     */       NULL,
186         /* initfunc    */       node_composit_init_colorbalance,
187         /* freestoragefunc    */        node_free_standard_storage,
188         /* copystoragefunc    */        node_copy_standard_storage,
189         /* id          */       NULL
190 };
191