Initial Stable Commit
[synfig.git] / synfig-core / trunk / src / modules / mod_filter / colorcorrect.cpp
1 /* === S I N F 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 Robert B. Quattlebaum Jr.
9 **
10 **      This software and associated documentation
11 **      are CONFIDENTIAL and PROPRIETARY property of
12 **      the above-mentioned copyright holder.
13 **
14 **      You may not copy, print, publish, or in any
15 **      other way distribute this software without
16 **      a prior written agreement with
17 **      the copyright holder.
18 **      \endlegal
19 */
20 /* ========================================================================= */
21
22 /* === H E A D E R S ======================================================= */
23
24 #ifdef USING_PCH
25 #       include "pch.h"
26 #else
27 #ifdef HAVE_CONFIG_H
28 #       include <config.h>
29 #endif
30
31 #include "colorcorrect.h"
32 #include <sinfg/string.h>
33 #include <sinfg/time.h>
34 #include <sinfg/context.h>
35 #include <sinfg/paramdesc.h>
36 #include <sinfg/renddesc.h>
37 #include <sinfg/surface.h>
38 #include <sinfg/value.h>
39 #include <sinfg/valuenode.h>
40
41 #endif
42
43 /* === U S I N G =========================================================== */
44
45 using namespace etl;
46 using namespace std;
47 using namespace sinfg;
48
49 /* === G L O B A L S ======================================================= */
50
51 SINFG_LAYER_INIT(Layer_ColorCorrect);
52 SINFG_LAYER_SET_NAME(Layer_ColorCorrect,"colorcorrect");
53 SINFG_LAYER_SET_LOCAL_NAME(Layer_ColorCorrect,_("Color Correct"));
54 SINFG_LAYER_SET_CATEGORY(Layer_ColorCorrect,_("Filters"));
55 SINFG_LAYER_SET_VERSION(Layer_ColorCorrect,"0.1");
56 SINFG_LAYER_SET_CVS_ID(Layer_ColorCorrect,"$Id: colorcorrect.cpp,v 1.3 2005/01/24 05:00:18 darco Exp $");
57
58 /* === P R O C E D U R E S ================================================= */
59
60 /* === M E T H O D S ======================================================= */
61
62 /* === E N T R Y P O I N T ================================================= */
63
64 Layer_ColorCorrect::Layer_ColorCorrect():
65         hue_adjust(Angle::zero()),
66         brightness(0),
67         contrast(1.0),
68         exposure(0.0)
69 {
70 }
71
72 inline Color
73 Layer_ColorCorrect::correct_color(const Color &in)const
74 {
75         Color ret(in);
76         Real brightness((this->brightness-0.5)*this->contrast+0.5);
77         
78         if(gamma.get_gamma_r()!=1.0)
79         {
80                 if(ret.get_r() < 0)
81                 {
82                         ret.set_r(-gamma.r_F32_to_F32(-ret.get_r()));
83                 }else
84                 {
85                         ret.set_r(gamma.r_F32_to_F32(ret.get_r()));                     
86                 }                       
87         }
88         if(gamma.get_gamma_g()!=1.0)
89         {
90                 if(ret.get_g() < 0)
91                 {
92                         ret.set_g(-gamma.g_F32_to_F32(-ret.get_g()));
93                 }else
94                 {
95                         ret.set_g(gamma.g_F32_to_F32(ret.get_g()));                     
96                 }                       
97         }
98         if(gamma.get_gamma_b()!=1.0)
99         {
100                 if(ret.get_b() < 0)
101                 {
102                         ret.set_b(-gamma.b_F32_to_F32(-ret.get_b()));
103                 }else
104                 {
105                         ret.set_b(gamma.b_F32_to_F32(ret.get_b()));                     
106                 }                       
107         }
108         
109         assert(!isnan(ret.get_r()));
110         assert(!isnan(ret.get_g()));
111         assert(!isnan(ret.get_b()));
112         
113         if(exposure!=0.0)
114         {
115                 const float factor(exp(exposure));
116                 ret.set_r(ret.get_r()*factor);
117                 ret.set_g(ret.get_g()*factor);
118                 ret.set_b(ret.get_b()*factor);
119         }
120         
121         // Adjust Contrast
122         if(contrast!=1.0)
123         {
124                 ret.set_r(ret.get_r()*contrast);
125                 ret.set_g(ret.get_g()*contrast);
126                 ret.set_b(ret.get_b()*contrast);
127         }
128         
129         if(brightness)
130         {
131                 // Adjust R Channel Brightness
132                 if(ret.get_r()>-brightness)
133                         ret.set_r(ret.get_r()+brightness);
134                 else if(ret.get_r()<brightness)
135                         ret.set_r(ret.get_r()-brightness);
136                 else
137                         ret.set_r(0);
138         
139                 // Adjust G Channel Brightness
140                 if(ret.get_g()>-brightness)
141                         ret.set_g(ret.get_g()+brightness);
142                 else if(ret.get_g()<brightness)
143                         ret.set_g(ret.get_g()-brightness);
144                 else
145                         ret.set_g(0);
146         
147                 // Adjust B Channel Brightness
148                 if(ret.get_b()>-brightness)
149                         ret.set_b(ret.get_b()+brightness);
150                 else if(ret.get_b()<brightness)
151                         ret.set_b(ret.get_b()-brightness);
152                 else
153                         ret.set_b(0);
154         }
155         
156         // Return the color, adjusting the hue if necessary
157         if(!!hue_adjust)
158                 return ret.rotate_uv(hue_adjust);
159         else
160                 return ret;
161 }
162         
163 bool
164 Layer_ColorCorrect::set_param(const String & param, const ValueBase &value)
165 {
166         IMPORT(hue_adjust);
167         IMPORT(brightness);
168         IMPORT(contrast);
169         IMPORT(exposure);
170
171         if(param=="gamma" && value.get_type()==ValueBase::TYPE_REAL)
172         {
173                 gamma.set_gamma(1.0/value.get(Real()));
174                 return true;
175         }               
176         return false;   
177 }
178
179 ValueBase
180 Layer_ColorCorrect::get_param(const String &param)const
181 {
182         EXPORT(hue_adjust);
183         EXPORT(brightness);
184         EXPORT(contrast);
185         EXPORT(exposure);
186
187         if(param=="gamma")
188                 return 1.0/gamma.get_gamma();
189
190         EXPORT_NAME();
191         EXPORT_VERSION();
192                 
193         return ValueBase();     
194 }
195
196 Layer::Vocab
197 Layer_ColorCorrect::get_param_vocab()const
198 {
199         Layer::Vocab ret;
200         
201         ret.push_back(ParamDesc("hue_adjust")
202                 .set_local_name(_("Hue Adjust"))
203         );
204
205         ret.push_back(ParamDesc("brightness")
206                 .set_local_name(_("Brightness"))
207         );
208
209         ret.push_back(ParamDesc("contrast")
210                 .set_local_name(_("Contrast"))
211         );
212
213         ret.push_back(ParamDesc("exposure")
214                 .set_local_name(_("Exposure Adjust"))
215         );
216
217         ret.push_back(ParamDesc("gamma")
218                 .set_local_name(_("Gamma Adjustment"))
219         );
220         
221         return ret;
222 }
223
224 Color
225 Layer_ColorCorrect::get_color(Context context, const Point &pos)const
226 {
227         return correct_color(context.get_color(pos));
228 }
229         
230 bool
231 Layer_ColorCorrect::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
232 {
233         SuperCallback supercb(cb,0,9500,10000);
234
235         if(!context.accelerated_render(surface,quality,renddesc,&supercb))
236                 return false;
237
238         int x,y;
239
240         Surface::pen pen(surface->begin());
241
242         for(y=0;y<renddesc.get_h();y++,pen.inc_y(),pen.dec_x(x))
243                 for(x=0;x<renddesc.get_w();x++,pen.inc_x())
244                         pen.put_value(correct_color(pen.get_value()));
245
246         // Mark our progress as finished
247         if(cb && !cb->amount_complete(10000,10000))
248                 return false;
249
250         return true;
251 }
252
253 Rect
254 Layer_ColorCorrect::get_full_bounding_rect(Context context)const
255 {
256         return context.get_full_bounding_rect();
257 }