Merge branch 'genete_static_values'
[synfig.git] / synfig-core / src / modules / mod_filter / colorcorrect.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file colorcorrect.cpp
3 **      \brief Implementation of the "Color Correct" layer
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include "colorcorrect.h"
33 #include <synfig/string.h>
34 #include <synfig/time.h>
35 #include <synfig/context.h>
36 #include <synfig/paramdesc.h>
37 #include <synfig/renddesc.h>
38 #include <synfig/surface.h>
39 #include <synfig/value.h>
40 #include <synfig/valuenode.h>
41
42 #endif
43
44 /* === U S I N G =========================================================== */
45
46 using namespace etl;
47 using namespace std;
48 using namespace synfig;
49
50 /* === G L O B A L S ======================================================= */
51
52 SYNFIG_LAYER_INIT(Layer_ColorCorrect);
53 SYNFIG_LAYER_SET_NAME(Layer_ColorCorrect,"colorcorrect");
54 SYNFIG_LAYER_SET_LOCAL_NAME(Layer_ColorCorrect,N_("Color Correct"));
55 SYNFIG_LAYER_SET_CATEGORY(Layer_ColorCorrect,N_("Filters"));
56 SYNFIG_LAYER_SET_VERSION(Layer_ColorCorrect,"0.1");
57 SYNFIG_LAYER_SET_CVS_ID(Layer_ColorCorrect,"$Id$");
58
59 /* === P R O C E D U R E S ================================================= */
60
61 /* === M E T H O D S ======================================================= */
62
63 /* === E N T R Y P O I N T ================================================= */
64
65 Layer_ColorCorrect::Layer_ColorCorrect():
66         hue_adjust(Angle::zero()),
67         brightness(0),
68         contrast(1.0),
69         exposure(0.0)
70 {
71         Layer::Vocab voc(get_param_vocab());
72         Layer::fill_static(voc);
73 }
74
75 inline Color
76 Layer_ColorCorrect::correct_color(const Color &in)const
77 {
78         Color ret(in);
79         Real brightness((this->brightness-0.5)*this->contrast+0.5);
80
81         if(gamma.get_gamma_r()!=1.0)
82         {
83                 if(ret.get_r() < 0)
84                 {
85                         ret.set_r(-gamma.r_F32_to_F32(-ret.get_r()));
86                 }else
87                 {
88                         ret.set_r(gamma.r_F32_to_F32(ret.get_r()));
89                 }
90         }
91         if(gamma.get_gamma_g()!=1.0)
92         {
93                 if(ret.get_g() < 0)
94                 {
95                         ret.set_g(-gamma.g_F32_to_F32(-ret.get_g()));
96                 }else
97                 {
98                         ret.set_g(gamma.g_F32_to_F32(ret.get_g()));
99                 }
100         }
101         if(gamma.get_gamma_b()!=1.0)
102         {
103                 if(ret.get_b() < 0)
104                 {
105                         ret.set_b(-gamma.b_F32_to_F32(-ret.get_b()));
106                 }else
107                 {
108                         ret.set_b(gamma.b_F32_to_F32(ret.get_b()));
109                 }
110         }
111
112         assert(!isnan(ret.get_r()));
113         assert(!isnan(ret.get_g()));
114         assert(!isnan(ret.get_b()));
115
116         if(exposure!=0.0)
117         {
118                 const float factor(exp(exposure));
119                 ret.set_r(ret.get_r()*factor);
120                 ret.set_g(ret.get_g()*factor);
121                 ret.set_b(ret.get_b()*factor);
122         }
123
124         // Adjust Contrast
125         if(contrast!=1.0)
126         {
127                 ret.set_r(ret.get_r()*contrast);
128                 ret.set_g(ret.get_g()*contrast);
129                 ret.set_b(ret.get_b()*contrast);
130         }
131
132         if(brightness)
133         {
134                 // Adjust R Channel Brightness
135                 if(ret.get_r()>-brightness)
136                         ret.set_r(ret.get_r()+brightness);
137                 else if(ret.get_r()<brightness)
138                         ret.set_r(ret.get_r()-brightness);
139                 else
140                         ret.set_r(0);
141
142                 // Adjust G Channel Brightness
143                 if(ret.get_g()>-brightness)
144                         ret.set_g(ret.get_g()+brightness);
145                 else if(ret.get_g()<brightness)
146                         ret.set_g(ret.get_g()-brightness);
147                 else
148                         ret.set_g(0);
149
150                 // Adjust B Channel Brightness
151                 if(ret.get_b()>-brightness)
152                         ret.set_b(ret.get_b()+brightness);
153                 else if(ret.get_b()<brightness)
154                         ret.set_b(ret.get_b()-brightness);
155                 else
156                         ret.set_b(0);
157         }
158
159         // Return the color, adjusting the hue if necessary
160         if(!!hue_adjust)
161                 return ret.rotate_uv(hue_adjust);
162         else
163                 return ret;
164 }
165
166 bool
167 Layer_ColorCorrect::set_param(const String & param, const ValueBase &value)
168 {
169         IMPORT(hue_adjust);
170         IMPORT(brightness);
171         IMPORT(contrast);
172         IMPORT(exposure);
173
174         if(param=="gamma" && value.get_type()==ValueBase::TYPE_REAL)
175         {
176                 gamma.set_gamma(1.0/value.get(Real()));
177                 set_param_static(param, value.get_static());
178                 return true;
179         }
180         return false;
181 }
182
183 ValueBase
184 Layer_ColorCorrect::get_param(const String &param)const
185 {
186         EXPORT(hue_adjust);
187         EXPORT(brightness);
188         EXPORT(contrast);
189         EXPORT(exposure);
190
191         if(param=="gamma")
192         {
193                 ValueBase ret(1.0/gamma.get_gamma());
194                 ret.set_static(get_param_static(param));
195                 return ret;
196         }
197
198         EXPORT_NAME();
199         EXPORT_VERSION();
200
201         return ValueBase();
202 }
203
204 Layer::Vocab
205 Layer_ColorCorrect::get_param_vocab()const
206 {
207         Layer::Vocab ret;
208
209         ret.push_back(ParamDesc("hue_adjust")
210                 .set_local_name(_("Hue Adjust"))
211         );
212
213         ret.push_back(ParamDesc("brightness")
214                 .set_local_name(_("Brightness"))
215         );
216
217         ret.push_back(ParamDesc("contrast")
218                 .set_local_name(_("Contrast"))
219         );
220
221         ret.push_back(ParamDesc("exposure")
222                 .set_local_name(_("Exposure Adjust"))
223         );
224
225         ret.push_back(ParamDesc("gamma")
226                 .set_local_name(_("Gamma Adjustment"))
227         );
228
229         return ret;
230 }
231
232 Color
233 Layer_ColorCorrect::get_color(Context context, const Point &pos)const
234 {
235         return correct_color(context.get_color(pos));
236 }
237
238 bool
239 Layer_ColorCorrect::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
240 {
241         SuperCallback supercb(cb,0,9500,10000);
242
243         if(!context.accelerated_render(surface,quality,renddesc,&supercb))
244                 return false;
245
246         int x,y;
247
248         Surface::pen pen(surface->begin());
249
250         for(y=0;y<renddesc.get_h();y++,pen.inc_y(),pen.dec_x(x))
251                 for(x=0;x<renddesc.get_w();x++,pen.inc_x())
252                         pen.put_value(correct_color(pen.get_value()));
253
254         // Mark our progress as finished
255         if(cb && !cb->amount_complete(10000,10000))
256                 return false;
257
258         return true;
259 }
260
261 Rect
262 Layer_ColorCorrect::get_full_bounding_rect(Context context)const
263 {
264         return context.get_full_bounding_rect();
265 }