Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_03 / synfig-core / src / modules / mod_filter / colorcorrect.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file colorcorrect.cpp
3 **      \brief Template Header
4 **
5 **      $Id: colorcorrect.cpp,v 1.3 2005/01/24 05:00:18 darco Exp $
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,_("Color Correct"));
55 SYNFIG_LAYER_SET_CATEGORY(Layer_ColorCorrect,_("Filters"));
56 SYNFIG_LAYER_SET_VERSION(Layer_ColorCorrect,"0.1");
57 SYNFIG_LAYER_SET_CVS_ID(Layer_ColorCorrect,"$Id: colorcorrect.cpp,v 1.3 2005/01/24 05:00:18 darco Exp $");
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 }
72
73 inline Color
74 Layer_ColorCorrect::correct_color(const Color &in)const
75 {
76         Color ret(in);
77         Real brightness((this->brightness-0.5)*this->contrast+0.5);
78         
79         if(gamma.get_gamma_r()!=1.0)
80         {
81                 if(ret.get_r() < 0)
82                 {
83                         ret.set_r(-gamma.r_F32_to_F32(-ret.get_r()));
84                 }else
85                 {
86                         ret.set_r(gamma.r_F32_to_F32(ret.get_r()));                     
87                 }                       
88         }
89         if(gamma.get_gamma_g()!=1.0)
90         {
91                 if(ret.get_g() < 0)
92                 {
93                         ret.set_g(-gamma.g_F32_to_F32(-ret.get_g()));
94                 }else
95                 {
96                         ret.set_g(gamma.g_F32_to_F32(ret.get_g()));                     
97                 }                       
98         }
99         if(gamma.get_gamma_b()!=1.0)
100         {
101                 if(ret.get_b() < 0)
102                 {
103                         ret.set_b(-gamma.b_F32_to_F32(-ret.get_b()));
104                 }else
105                 {
106                         ret.set_b(gamma.b_F32_to_F32(ret.get_b()));                     
107                 }                       
108         }
109         
110         assert(!isnan(ret.get_r()));
111         assert(!isnan(ret.get_g()));
112         assert(!isnan(ret.get_b()));
113         
114         if(exposure!=0.0)
115         {
116                 const float factor(exp(exposure));
117                 ret.set_r(ret.get_r()*factor);
118                 ret.set_g(ret.get_g()*factor);
119                 ret.set_b(ret.get_b()*factor);
120         }
121         
122         // Adjust Contrast
123         if(contrast!=1.0)
124         {
125                 ret.set_r(ret.get_r()*contrast);
126                 ret.set_g(ret.get_g()*contrast);
127                 ret.set_b(ret.get_b()*contrast);
128         }
129         
130         if(brightness)
131         {
132                 // Adjust R Channel Brightness
133                 if(ret.get_r()>-brightness)
134                         ret.set_r(ret.get_r()+brightness);
135                 else if(ret.get_r()<brightness)
136                         ret.set_r(ret.get_r()-brightness);
137                 else
138                         ret.set_r(0);
139         
140                 // Adjust G Channel Brightness
141                 if(ret.get_g()>-brightness)
142                         ret.set_g(ret.get_g()+brightness);
143                 else if(ret.get_g()<brightness)
144                         ret.set_g(ret.get_g()-brightness);
145                 else
146                         ret.set_g(0);
147         
148                 // Adjust B Channel Brightness
149                 if(ret.get_b()>-brightness)
150                         ret.set_b(ret.get_b()+brightness);
151                 else if(ret.get_b()<brightness)
152                         ret.set_b(ret.get_b()-brightness);
153                 else
154                         ret.set_b(0);
155         }
156         
157         // Return the color, adjusting the hue if necessary
158         if(!!hue_adjust)
159                 return ret.rotate_uv(hue_adjust);
160         else
161                 return ret;
162 }
163         
164 bool
165 Layer_ColorCorrect::set_param(const String & param, const ValueBase &value)
166 {
167         IMPORT(hue_adjust);
168         IMPORT(brightness);
169         IMPORT(contrast);
170         IMPORT(exposure);
171
172         if(param=="gamma" && value.get_type()==ValueBase::TYPE_REAL)
173         {
174                 gamma.set_gamma(1.0/value.get(Real()));
175                 return true;
176         }               
177         return false;   
178 }
179
180 ValueBase
181 Layer_ColorCorrect::get_param(const String &param)const
182 {
183         EXPORT(hue_adjust);
184         EXPORT(brightness);
185         EXPORT(contrast);
186         EXPORT(exposure);
187
188         if(param=="gamma")
189                 return 1.0/gamma.get_gamma();
190
191         EXPORT_NAME();
192         EXPORT_VERSION();
193                 
194         return ValueBase();     
195 }
196
197 Layer::Vocab
198 Layer_ColorCorrect::get_param_vocab()const
199 {
200         Layer::Vocab ret;
201         
202         ret.push_back(ParamDesc("hue_adjust")
203                 .set_local_name(_("Hue Adjust"))
204         );
205
206         ret.push_back(ParamDesc("brightness")
207                 .set_local_name(_("Brightness"))
208         );
209
210         ret.push_back(ParamDesc("contrast")
211                 .set_local_name(_("Contrast"))
212         );
213
214         ret.push_back(ParamDesc("exposure")
215                 .set_local_name(_("Exposure Adjust"))
216         );
217
218         ret.push_back(ParamDesc("gamma")
219                 .set_local_name(_("Gamma Adjustment"))
220         );
221         
222         return ret;
223 }
224
225 Color
226 Layer_ColorCorrect::get_color(Context context, const Point &pos)const
227 {
228         return correct_color(context.get_color(pos));
229 }
230         
231 bool
232 Layer_ColorCorrect::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
233 {
234         SuperCallback supercb(cb,0,9500,10000);
235
236         if(!context.accelerated_render(surface,quality,renddesc,&supercb))
237                 return false;
238
239         int x,y;
240
241         Surface::pen pen(surface->begin());
242
243         for(y=0;y<renddesc.get_h();y++,pen.inc_y(),pen.dec_x(x))
244                 for(x=0;x<renddesc.get_w();x++,pen.inc_x())
245                         pen.put_value(correct_color(pen.get_value()));
246
247         // Mark our progress as finished
248         if(cb && !cb->amount_complete(10000,10000))
249                 return false;
250
251         return true;
252 }
253
254 Rect
255 Layer_ColorCorrect::get_full_bounding_rect(Context context)const
256 {
257         return context.get_full_bounding_rect();
258 }