Merge branch 'genete_static_values'
[synfig.git] / synfig-core / src / modules / mod_filter / lumakey.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file lumakey.cpp
3 **      \brief Implementation of the "Luma Key" layer
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007 Chris Moore
10 **
11 **      This package is free software; you can redistribute it and/or
12 **      modify it under the terms of the GNU General Public License as
13 **      published by the Free Software Foundation; either version 2 of
14 **      the License, or (at your option) any later version.
15 **
16 **      This package is distributed in the hope that it will be useful,
17 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **      General Public License for more details.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include "lumakey.h"
34
35 #include <synfig/string.h>
36 #include <synfig/time.h>
37 #include <synfig/context.h>
38 #include <synfig/paramdesc.h>
39 #include <synfig/renddesc.h>
40 #include <synfig/surface.h>
41 #include <synfig/value.h>
42 #include <synfig/valuenode.h>
43 #include <synfig/segment.h>
44
45 #endif
46
47 using namespace synfig;
48 using namespace std;
49 using namespace etl;
50
51 /* === M A C R O S ========================================================= */
52
53 /* === G L O B A L S ======================================================= */
54
55 SYNFIG_LAYER_INIT(LumaKey);
56 SYNFIG_LAYER_SET_NAME(LumaKey,"lumakey");
57 SYNFIG_LAYER_SET_LOCAL_NAME(LumaKey,N_("Luma Key"));
58 SYNFIG_LAYER_SET_CATEGORY(LumaKey,N_("Filters"));
59 SYNFIG_LAYER_SET_VERSION(LumaKey,"0.1");
60 SYNFIG_LAYER_SET_CVS_ID(LumaKey,"$Id$");
61
62 /* === P R O C E D U R E S ================================================= */
63
64 /* === M E T H O D S ======================================================= */
65
66 LumaKey::LumaKey():
67         Layer_Composite (1.0,Color::BLEND_STRAIGHT)
68 {
69         set_blend_method(Color::BLEND_STRAIGHT);
70         Layer::Vocab voc(get_param_vocab());
71         Layer::fill_static(voc);
72 }
73
74
75 bool
76 LumaKey::set_param(const String &param, const ValueBase &value)
77 {
78         return Layer_Composite::set_param(param,value);
79 }
80
81 ValueBase
82 LumaKey::get_param(const String &param)const
83 {
84         EXPORT_NAME();
85         EXPORT_VERSION();
86
87         return Layer_Composite::get_param(param);
88 }
89
90 Layer::Vocab
91 LumaKey::get_param_vocab()const
92 {
93         Layer::Vocab ret(Layer_Composite::get_param_vocab());
94
95 /*      ret.push_back(ParamDesc("color")
96                 .set_local_name(_("Color"))
97                 .set_description(_("Color of checkers"))
98         );
99         ret.push_back(ParamDesc("pos")
100                 .set_local_name(_("Offset"))
101         );
102         ret.push_back(ParamDesc("size")
103                 .set_local_name(_("Size"))
104                 .set_description(_("Size of checkers"))
105                 .set_origin("pos")
106         );
107 */
108         return ret;
109 }
110
111 synfig::Layer::Handle
112 LumaKey::hit_check(synfig::Context context, const synfig::Point &getpos)const
113 {
114         return context.hit_check(getpos);
115 }
116
117 Color
118 LumaKey::get_color(Context context, const Point &getpos)const
119 {
120         const Color color(context.get_color(getpos));
121
122         if(get_amount()==0.0)
123                 return color;
124
125         Color ret(color);
126         ret.set_a(ret.get_y()*ret.get_a());
127         ret.set_y(1);
128
129         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
130                 return ret;
131
132         return Color::blend(ret,color,get_amount(),get_blend_method());
133 }
134
135 bool
136 LumaKey::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
137 {
138         SuperCallback supercb(cb,0,9500,10000);
139
140         if(!context.accelerated_render(surface,quality,renddesc,&supercb))
141                 return false;
142
143         int x,y;
144
145         Surface::pen pen(surface->begin());
146
147         for(y=0;y<renddesc.get_h();y++,pen.inc_y(),pen.dec_x(x))
148                 for(x=0;x<renddesc.get_w();x++,pen.inc_x())
149                 {
150                         Color tmp(pen.get_value());
151                         tmp.set_a(tmp.get_y()*tmp.get_a());
152                         tmp.set_y(1);
153                         pen.put_value(tmp);
154                 }
155
156         // Mark our progress as finished
157         if(cb && !cb->amount_complete(10000,10000))
158                 return false;
159
160         return true;
161 }
162
163 Rect
164 LumaKey::get_bounding_rect(Context context)const
165 {
166         if(is_disabled())
167                 return Rect::zero();
168
169         return context.get_full_bounding_rect();
170 }