1 /* === S Y N F I G ========================================================= */
2 /*! \file colorcorrect.cpp
3 ** \brief Implementation of the "Color Correct" layer
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
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.
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.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
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>
44 /* === U S I N G =========================================================== */
48 using namespace synfig;
50 /* === G L O B A L S ======================================================= */
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$");
59 /* === P R O C E D U R E S ================================================= */
61 /* === M E T H O D S ======================================================= */
63 /* === E N T R Y P O I N T ================================================= */
65 Layer_ColorCorrect::Layer_ColorCorrect():
66 hue_adjust(Angle::zero()),
74 Layer_ColorCorrect::correct_color(const Color &in)const
77 Real brightness((this->brightness-0.5)*this->contrast+0.5);
79 if(gamma.get_gamma_r()!=1.0)
83 ret.set_r(-gamma.r_F32_to_F32(-ret.get_r()));
86 ret.set_r(gamma.r_F32_to_F32(ret.get_r()));
89 if(gamma.get_gamma_g()!=1.0)
93 ret.set_g(-gamma.g_F32_to_F32(-ret.get_g()));
96 ret.set_g(gamma.g_F32_to_F32(ret.get_g()));
99 if(gamma.get_gamma_b()!=1.0)
103 ret.set_b(-gamma.b_F32_to_F32(-ret.get_b()));
106 ret.set_b(gamma.b_F32_to_F32(ret.get_b()));
110 assert(!isnan(ret.get_r()));
111 assert(!isnan(ret.get_g()));
112 assert(!isnan(ret.get_b()));
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);
125 ret.set_r(ret.get_r()*contrast);
126 ret.set_g(ret.get_g()*contrast);
127 ret.set_b(ret.get_b()*contrast);
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);
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);
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);
157 // Return the color, adjusting the hue if necessary
159 return ret.rotate_uv(hue_adjust);
165 Layer_ColorCorrect::set_param(const String & param, const ValueBase &value)
172 if(param=="gamma" && value.get_type()==ValueBase::TYPE_REAL)
174 gamma.set_gamma(1.0/value.get(Real()));
181 Layer_ColorCorrect::get_param(const String ¶m)const
189 return 1.0/gamma.get_gamma();
198 Layer_ColorCorrect::get_param_vocab()const
202 ret.push_back(ParamDesc("hue_adjust")
203 .set_local_name(_("Hue Adjust"))
206 ret.push_back(ParamDesc("brightness")
207 .set_local_name(_("Brightness"))
210 ret.push_back(ParamDesc("contrast")
211 .set_local_name(_("Contrast"))
214 ret.push_back(ParamDesc("exposure")
215 .set_local_name(_("Exposure Adjust"))
218 ret.push_back(ParamDesc("gamma")
219 .set_local_name(_("Gamma Adjustment"))
226 Layer_ColorCorrect::get_color(Context context, const Point &pos)const
228 return correct_color(context.get_color(pos));
232 Layer_ColorCorrect::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
234 SuperCallback supercb(cb,0,9500,10000);
236 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
241 Surface::pen pen(surface->begin());
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()));
247 // Mark our progress as finished
248 if(cb && !cb->amount_complete(10000,10000))
255 Layer_ColorCorrect::get_full_bounding_rect(Context context)const
257 return context.get_full_bounding_rect();