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