Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_07 / src / modules / lyr_std / clamp.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file clamp.cpp
3 **      \brief Template Header
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 "clamp.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_Clamp);
53 SYNFIG_LAYER_SET_NAME(Layer_Clamp,"clamp");
54 SYNFIG_LAYER_SET_LOCAL_NAME(Layer_Clamp,_("Clamp"));
55 SYNFIG_LAYER_SET_CATEGORY(Layer_Clamp,_("Filters"));
56 SYNFIG_LAYER_SET_VERSION(Layer_Clamp,"0.2");
57 SYNFIG_LAYER_SET_CVS_ID(Layer_Clamp,"$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_Clamp::Layer_Clamp():
66         invert_negative(false),
67         clamp_ceiling(true),
68         ceiling(1.0f),
69         floor(0.0f)
70 {
71 }
72
73 inline Color
74 Layer_Clamp::clamp_color(const Color &in)const
75 {
76         Color ret(in);
77
78         if(ret.get_a()==0)
79                 return Color::alpha();
80
81         if(invert_negative)
82         {
83                 if(ret.get_a()<floor)
84                         ret=-ret;
85
86                 if(ret.get_r()<floor)
87                 {
88                         ret.set_g(ret.get_g()-ret.get_r());
89                         ret.set_b(ret.get_b()-ret.get_r());
90                         ret.set_r(floor);
91                 }
92                 if(ret.get_g()<floor)
93                 {
94                         ret.set_r(ret.get_r()-ret.get_g());
95                         ret.set_b(ret.get_b()-ret.get_g());
96                         ret.set_g(floor);
97                 }
98                 if(ret.get_b()<floor)
99                 {
100                         ret.set_g(ret.get_g()-ret.get_b());
101                         ret.set_r(ret.get_r()-ret.get_b());
102                         ret.set_b(floor);
103                 }
104         }
105         else
106         {
107                 if(ret.get_r()<floor) ret.set_r(floor);
108                 if(ret.get_g()<floor) ret.set_g(floor);
109                 if(ret.get_b()<floor) ret.set_b(floor);
110                 if(ret.get_a()<floor) ret.set_a(floor);
111         }
112
113         if(clamp_ceiling)
114         {
115                 if(ret.get_r()>ceiling) ret.set_r(ceiling);
116                 if(ret.get_g()>ceiling) ret.set_g(ceiling);
117                 if(ret.get_b()>ceiling) ret.set_b(ceiling);
118                 if(ret.get_a()>ceiling) ret.set_a(ceiling);
119         }
120         return ret;
121 }
122
123 bool
124 Layer_Clamp::set_param(const String & param, const ValueBase &value)
125 {
126         IMPORT(invert_negative);
127         IMPORT(clamp_ceiling);
128         IMPORT(ceiling);
129         IMPORT(floor);
130
131         return false;
132 }
133
134 ValueBase
135 Layer_Clamp::get_param(const String &param)const
136 {
137         EXPORT(invert_negative);
138         EXPORT(clamp_ceiling);
139
140         EXPORT(ceiling);
141         EXPORT(floor);
142
143         EXPORT_NAME();
144         EXPORT_VERSION();
145
146         return ValueBase();
147 }
148
149 Layer::Vocab
150 Layer_Clamp::get_param_vocab()const
151 {
152         Layer::Vocab ret;
153
154         ret.push_back(ParamDesc("invert_negative")
155                 .set_local_name(_("Invert Negative"))
156         );
157
158         ret.push_back(ParamDesc("clamp_ceiling")
159                 .set_local_name(_("Clamp Ceiling"))
160         );
161
162         ret.push_back(ParamDesc("ceiling")
163                 .set_local_name(_("Ceiling"))
164         );
165
166         ret.push_back(ParamDesc("floor")
167                 .set_local_name(_("Floor"))
168         );
169
170         return ret;
171 }
172
173 Color
174 Layer_Clamp::get_color(Context context, const Point &pos)const
175 {
176         return clamp_color(context.get_color(pos));
177 }
178
179 bool
180 Layer_Clamp::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
181 {
182         SuperCallback supercb(cb,0,9500,10000);
183
184         if(!context.accelerated_render(surface,quality,renddesc,&supercb))
185                 return false;
186
187         int x,y;
188
189         Surface::pen pen(surface->begin());
190
191         for(y=0;y<renddesc.get_h();y++,pen.inc_y(),pen.dec_x(x))
192                 for(x=0;x<renddesc.get_w();x++,pen.inc_x())
193                         pen.put_value(clamp_color(pen.get_value()));
194
195         // Mark our progress as finished
196         if(cb && !cb->amount_complete(10000,10000))
197                 return false;
198
199         return true;
200 }
201
202
203 Rect
204 Layer_Clamp::get_full_bounding_rect(Context context)const
205 {
206         return context.get_full_bounding_rect();
207 }