Add parameter static option for rest of layers.
[synfig.git] / synfig-core / src / modules / lyr_std / bevel.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file bevel.cpp
3 **      \brief Implementation of the "Bevel" layer
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 "bevel.h"
33
34 #include <synfig/string.h>
35 #include <synfig/time.h>
36 #include <synfig/context.h>
37 #include <synfig/paramdesc.h>
38 #include <synfig/renddesc.h>
39 #include <synfig/surface.h>
40 #include <synfig/value.h>
41 #include <synfig/valuenode.h>
42 #include <synfig/segment.h>
43
44 #include <cstring>
45 #include <ETL/pen>
46 #include <ETL/misc>
47
48 #endif
49
50 using namespace synfig;
51 using namespace etl;
52 using namespace std;
53
54 /*#define TYPE_BOX                      0
55 #define TYPE_FASTGUASSIAN       1
56 #define TYPE_FASTGAUSSIAN       1
57 #define TYPE_CROSS                      2
58 #define TYPE_GUASSIAN           3
59 #define TYPE_GAUSSIAN           3
60 #define TYPE_DISC                       4
61 */
62
63 /* -- G L O B A L S --------------------------------------------------------- */
64
65 SYNFIG_LAYER_INIT(Layer_Bevel);
66 SYNFIG_LAYER_SET_NAME(Layer_Bevel,"bevel");
67 SYNFIG_LAYER_SET_LOCAL_NAME(Layer_Bevel,N_("Bevel"));
68 SYNFIG_LAYER_SET_CATEGORY(Layer_Bevel,N_("Stylize"));
69 SYNFIG_LAYER_SET_VERSION(Layer_Bevel,"0.2");
70 SYNFIG_LAYER_SET_CVS_ID(Layer_Bevel,"$Id$");
71
72 /* -- F U N C T I O N S ----------------------------------------------------- */
73
74 inline void clamp(synfig::Vector &v)
75 {
76         if(v[0]<0.0)v[0]=0.0;
77         if(v[1]<0.0)v[1]=0.0;
78 }
79
80 Layer_Bevel::Layer_Bevel():
81         Layer_Composite (0.75,Color::BLEND_ONTO),
82         softness(0.1),
83         type(Blur::FASTGAUSSIAN),
84         color1(Color::white()),
85         color2(Color::black()),
86         depth(0.2)
87 {
88         angle=Angle::deg(135);
89         calc_offset();
90         use_luma=false;
91         solid=false;
92         Layer::Vocab voc(get_param_vocab());
93         Layer::fill_static(voc);
94 }
95
96 void
97 Layer_Bevel::calc_offset()
98 {
99         offset[0]=Angle::cos(angle).get()*depth;
100         offset[1]=Angle::sin(angle).get()*depth;
101
102         offset45[0]=Angle::cos(angle-Angle::deg(45)).get()*depth*0.707106781;
103         offset45[1]=Angle::sin(angle-Angle::deg(45)).get()*depth*0.707106781;
104 }
105
106 bool
107 Layer_Bevel::set_param(const String &param, const ValueBase &value)
108 {
109         IMPORT_PLUS(softness,softness=softness>0?softness:0);
110         IMPORT(color1);
111         IMPORT(color2);
112         IMPORT_PLUS(depth,calc_offset());
113         IMPORT_PLUS(angle,calc_offset());
114         IMPORT(type);
115         IMPORT(use_luma);
116         IMPORT(solid);
117
118         return Layer_Composite::set_param(param,value);
119 }
120
121 ValueBase
122 Layer_Bevel::get_param(const String &param)const
123 {
124         EXPORT(type);
125         EXPORT(softness);
126         EXPORT(color1);
127         EXPORT(color2);
128         EXPORT(depth);
129         EXPORT(angle);
130         EXPORT(use_luma);
131         EXPORT(solid);
132
133         EXPORT_NAME();
134         EXPORT_VERSION();
135
136         return Layer_Composite::get_param(param);
137 }
138
139 Color
140 Layer_Bevel::get_color(Context context, const Point &pos)const
141 {
142         const Vector size(softness,softness);
143         Point blurpos = Blur(size,type)(pos);
144
145         if(get_amount()==0.0)
146                 return context.get_color(pos);
147
148         Color shade;
149
150         Real hi_alpha(1.0f-context.get_color(blurpos+offset).get_a());
151         Real lo_alpha(1.0f-context.get_color(blurpos-offset).get_a());
152
153         Real shade_alpha(hi_alpha-lo_alpha);
154         if(shade_alpha>0)
155                 shade=color1,shade.set_a(shade_alpha);
156         else
157                 shade=color2,shade.set_a(-shade_alpha);
158
159         return Color::blend(shade,context.get_color(pos),get_amount(),get_blend_method());
160 }
161
162 bool
163 Layer_Bevel::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
164 {
165         int x,y;
166         SuperCallback stageone(cb,0,5000,10000);
167         SuperCallback stagetwo(cb,5000,10000,10000);
168
169         const int       w = renddesc.get_w(),
170                                 h = renddesc.get_h();
171         const Real      pw = renddesc.get_pw(),
172                                 ph = renddesc.get_ph();
173         const Vector size(softness,softness);
174
175         RendDesc        workdesc(renddesc);
176         Surface         worksurface;
177         etl::surface<float> blurred;
178
179         //callbacks depend on how long the blur takes
180         if(size[0] || size[1])
181         {
182                 if(type == Blur::DISC)
183                 {
184                         stageone = SuperCallback(cb,0,5000,10000);
185                         stagetwo = SuperCallback(cb,5000,10000,10000);
186                 }
187                 else
188                 {
189                         stageone = SuperCallback(cb,0,9000,10000);
190                         stagetwo = SuperCallback(cb,9000,10000,10000);
191                 }
192         }
193         else
194         {
195                 stageone = SuperCallback(cb,0,9999,10000);
196                 stagetwo = SuperCallback(cb,9999,10000,10000);
197         }
198
199         //expand the working surface to accommodate the blur
200
201         //the expanded size = 1/2 the size in each direction rounded up
202         int     halfsizex = (int) (abs(size[0]*.5/pw) + 3),
203                 halfsizey = (int) (abs(size[1]*.5/ph) + 3);
204
205         int offset_u(round_to_int(offset[0]/pw)),offset_v(round_to_int(offset[1]/ph));
206         int offset_w(w+abs(offset_u)*2),offset_h(h+abs(offset_v)*2);
207
208         workdesc.set_subwindow(
209                 -abs(offset_u),
210                 -abs(offset_v),
211                 w+abs(offset_u),
212                 h+abs(offset_v)
213         );
214
215         //expand by 1/2 size in each direction on either side
216         switch(type)
217         {
218                 case Blur::DISC:
219                 case Blur::BOX:
220                 case Blur::CROSS:
221                 {
222                         workdesc.set_subwindow(-max(1,halfsizex),-max(1,halfsizey),offset_w+2*max(1,halfsizex),offset_h+2*max(1,halfsizey));
223                         break;
224                 }
225                 case Blur::FASTGAUSSIAN:
226                 {
227                         if(quality < 4)
228                         {
229                                 halfsizex*=2;
230                                 halfsizey*=2;
231                         }
232                         workdesc.set_subwindow(-max(1,halfsizex),-max(1,halfsizey),offset_w+2*max(1,halfsizex),offset_h+2*max(1,halfsizey));
233                         break;
234                 }
235                 case Blur::GAUSSIAN:
236                 {
237                 #define GAUSSIAN_ADJUSTMENT             (0.05)
238                         Real    pw = (Real)workdesc.get_w()/(workdesc.get_br()[0]-workdesc.get_tl()[0]);
239                         Real    ph = (Real)workdesc.get_h()/(workdesc.get_br()[1]-workdesc.get_tl()[1]);
240
241                         pw=pw*pw;
242                         ph=ph*ph;
243
244                         halfsizex = (int)(abs(pw)*size[0]*GAUSSIAN_ADJUSTMENT+0.5);
245                         halfsizey = (int)(abs(ph)*size[1]*GAUSSIAN_ADJUSTMENT+0.5);
246
247                         halfsizex = (halfsizex + 1)/2;
248                         halfsizey = (halfsizey + 1)/2;
249                         workdesc.set_subwindow( -halfsizex, -halfsizey, offset_w+2*halfsizex, offset_h+2*halfsizey );
250
251                         break;
252                 }
253         }
254
255         //render the background onto the expanded surface
256         if(!context.accelerated_render(&worksurface,quality,workdesc,&stageone))
257                 return false;
258
259         // Copy over the alpha
260         blurred.set_wh(worksurface.get_w(),worksurface.get_h());
261         if(!use_luma)
262         {
263                 for(int j=0;j<worksurface.get_h();j++)
264                         for(int i=0;i<worksurface.get_w();i++)
265                         {
266                                 blurred[j][i]=worksurface[j][i].get_a();
267                         }
268         }
269         else
270         {
271                 for(int j=0;j<worksurface.get_h();j++)
272                         for(int i=0;i<worksurface.get_w();i++)
273                         {
274                                 blurred[j][i]=worksurface[j][i].get_a()*worksurface[j][i].get_y();
275                         }
276         }
277
278         //blur the image
279         Blur(size,type,&stagetwo)(blurred,workdesc.get_br()-workdesc.get_tl(),blurred);
280
281         //be sure the surface is of the correct size
282         surface->set_wh(renddesc.get_w(),renddesc.get_h());
283
284         int u = halfsizex+abs(offset_u), v = halfsizey+abs(offset_v);
285         for(y=0;y<renddesc.get_h();y++,v++)
286         {
287                 u = halfsizex+abs(offset_u);
288                 for(x=0;x<renddesc.get_w();x++,u++)
289                 {
290                         Real alpha(0);
291                         Color shade;
292
293                         {
294                                 const float u2(offset[0]/pw),v2(offset[1]/ph);
295                                 alpha+=1.0f-blurred.linear_sample(u2+u,v2+v);
296                         }
297                         {
298                                 const float u2(-offset[0]/pw),v2(-offset[1]/ph);
299                                 alpha-=1.0f-blurred.linear_sample(u2+u,v2+v);
300                         }
301                         {
302                                 const float u2(offset45[0]/pw),v2(offset45[1]/ph);
303                                 alpha+=1.0f-blurred.linear_sample(u2+u,v2+v)*0.5f;
304                         }
305                         {
306                                 const float u2(offset45[1]/ph),v2(-offset45[0]/pw);
307                                 alpha+=1.0f-blurred.linear_sample(u2+u,v2+v)*0.5f;
308                         }
309                         {
310                                 const float u2(-offset45[0]/pw),v2(-offset45[1]/ph);
311                                 alpha-=1.0f-blurred.linear_sample(u2+u,v2+v)*0.5f;
312                         }
313                         {
314                                 const float u2(-offset45[1]/ph),v2(offset45[0]/pw);
315                                 alpha-=1.0f-blurred.linear_sample(u2+u,v2+v)*0.5f;
316                         }
317
318                         if(solid)
319                         {
320                                 alpha/=4.0f;
321                                 alpha+=0.5f;
322                                 shade=Color::blend(color1,color2,alpha,Color::BLEND_STRAIGHT);
323                         }
324                         else
325                         {
326                                 alpha/=2;
327                                 if(alpha>0)
328                                         shade=color1,shade.set_a(shade.get_a()*alpha);
329                                 else
330                                         shade=color2,shade.set_a(shade.get_a()*-alpha);
331                         }
332
333
334
335                         if(shade.get_a())
336                         {
337                                 (*surface)[y][x]=Color::blend(shade,worksurface[v][u],get_amount(),get_blend_method());
338                         }
339                         else (*surface)[y][x] = worksurface[v][u];
340                 }
341         }
342
343         if(cb && !cb->amount_complete(10000,10000))
344         {
345                 //if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
346                 return false;
347         }
348
349         return true;
350 }
351
352 Layer::Vocab
353 Layer_Bevel::get_param_vocab(void)const
354 {
355         Layer::Vocab ret(Layer_Composite::get_param_vocab());
356
357         ret.push_back(ParamDesc("type")
358                 .set_local_name(_("Type"))
359                 .set_description(_("Type of blur to use"))
360                 .set_hint("enum")
361                 .add_enum_value(Blur::BOX,"box",_("Box Blur"))
362                 .add_enum_value(Blur::FASTGAUSSIAN,"fastgaussian",_("Fast Gaussian Blur"))
363                 .add_enum_value(Blur::CROSS,"cross",_("Cross-Hatch Blur"))
364                 .add_enum_value(Blur::GAUSSIAN,"gaussian",_("Gaussian Blur"))
365                 .add_enum_value(Blur::DISC,"disc",_("Disc Blur"))
366         );
367
368         ret.push_back(ParamDesc("color1")
369                 .set_local_name(_("Hi-Color"))
370         );
371         ret.push_back(ParamDesc("color2")
372                 .set_local_name(_("Lo-Color"))
373         );
374         ret.push_back(ParamDesc("angle")
375                 .set_local_name(_("Light Angle"))
376         );
377         ret.push_back(ParamDesc("depth")
378                 .set_is_distance()
379                 .set_local_name(_("Depth of Bevel"))
380         );
381         ret.push_back(ParamDesc("softness")
382                 .set_is_distance()
383                 .set_local_name(_("Softness"))
384         );
385         ret.push_back(ParamDesc("use_luma")
386                 .set_local_name(_("Use Luma"))
387         );
388         ret.push_back(ParamDesc("solid")
389                 .set_local_name(_("Solid"))
390         );
391
392         return ret;
393 }
394
395 Rect
396 Layer_Bevel::get_full_bounding_rect(Context context)const
397 {
398         if(is_disabled())
399                 return context.get_full_bounding_rect();
400
401         Rect under(context.get_full_bounding_rect());
402
403         if(Color::is_onto(get_blend_method()))
404                 return under;
405
406         Rect bounds(under.expand(softness));
407         bounds.expand_x(abs(depth));
408         bounds.expand_y(abs(depth));
409
410         return bounds;
411 }