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