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 / shade.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file shade.cpp
3 **      \brief Implementation of the "Shade" layer
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2008 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 "shade.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 #include <cstring>
46 #include <ETL/pen>
47 #include <ETL/misc>
48
49 #endif
50
51 using namespace synfig;
52 using namespace etl;
53 using namespace std;
54
55 /*#define TYPE_BOX                      0
56 #define TYPE_FASTGUASSIAN       1
57 #define TYPE_CROSS                      2
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_Shade);
65 SYNFIG_LAYER_SET_NAME(Layer_Shade,"shade");
66 SYNFIG_LAYER_SET_LOCAL_NAME(Layer_Shade,N_("Shade"));
67 SYNFIG_LAYER_SET_CATEGORY(Layer_Shade,N_("Stylize"));
68 SYNFIG_LAYER_SET_VERSION(Layer_Shade,"0.2");
69 SYNFIG_LAYER_SET_CVS_ID(Layer_Shade,"$Id$");
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_Shade::Layer_Shade():
80         Layer_Composite (0.75,Color::BLEND_BEHIND),
81         size(0.1,0.1),
82         type(Blur::FASTGAUSSIAN),
83         color(Color::black()),
84         origin(0.2,-0.2),
85         invert(false)
86 {
87 }
88
89 bool
90 Layer_Shade::set_param(const String &param, const ValueBase &value)
91 {
92         IMPORT_PLUS(size,clamp(size));
93         IMPORT(type);
94         IMPORT_PLUS(color, { if (color.get_a() == 0) { if (converted_blend_) {
95                                         set_blend_method(Color::BLEND_ALPHA_OVER);
96                                         color.set_a(1); } else transparent_color_ = true; } });
97         IMPORT(origin);
98         IMPORT(invert);
99
100         IMPORT_AS(origin,"offset");
101
102         return Layer_Composite::set_param(param,value);
103 }
104
105 ValueBase
106 Layer_Shade::get_param(const String &param)const
107 {
108         EXPORT(size);
109         EXPORT(type);
110         EXPORT(color);
111         EXPORT(origin);
112         EXPORT(invert);
113
114         EXPORT_NAME();
115         EXPORT_VERSION();
116
117         return Layer_Composite::get_param(param);
118 }
119
120 Color
121 Layer_Shade::get_color(Context context, const Point &pos)const
122 {
123         Point blurpos = Blur(size,type)(pos);
124
125         if(get_amount()==0.0)
126                 return context.get_color(pos);
127
128         Color shade(color);
129
130         if(!invert)
131                 shade.set_a(context.get_color(blurpos-origin).get_a());
132         else
133                 shade.set_a(1.0f-context.get_color(blurpos-origin).get_a());
134
135         return Color::blend(shade,context.get_color(pos),get_amount(),get_blend_method());
136 }
137
138 bool
139 Layer_Shade::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
140 {
141         int x,y;
142
143         const int       w = renddesc.get_w(),
144                                 h = renddesc.get_h();
145         const Real      pw = renddesc.get_pw(),
146                                 ph = renddesc.get_ph();
147
148         RendDesc        workdesc(renddesc);
149         Surface         worksurface;
150         etl::surface<float> blurred;
151
152         //expand the working surface to accommodate the blur
153
154         //the expanded size = 1/2 the size in each direction rounded up
155         int     halfsizex = (int) (abs(size[0]*.5/pw) + 3),
156                 halfsizey = (int) (abs(size[1]*.5/ph) + 3);
157
158         int origin_u(-round_to_int(origin[0]/pw)),origin_v(-round_to_int(origin[1]/ph));
159
160         int origin_w(w+abs(origin_u)),origin_h(h+abs(origin_v));
161
162         workdesc.set_subwindow(
163                 origin_u<0?origin_u:0,
164                 origin_v<0?origin_v:0,
165                 (origin_u>0?origin_u:0)+w,
166                 (origin_v>0?origin_v:0)+h
167         );
168
169         if(quality >= 10)
170         {
171                 halfsizex=1;
172                 halfsizey=1;
173         }
174         else if (quality == 9)
175         {
176                 halfsizex/=4;
177                 halfsizey/=4;
178         }
179
180         //expand by 1/2 size in each direction on either side
181         switch(type)
182         {
183                 case Blur::DISC:
184                 case Blur::BOX:
185                 case Blur::CROSS:
186                 {
187                         workdesc.set_subwindow(-max(1,halfsizex),-max(1,halfsizey),origin_w+2*max(1,halfsizex),origin_h+2*max(1,halfsizey));
188                         break;
189                 }
190                 case Blur::FASTGAUSSIAN:
191                 {
192                         if(quality < 4)
193                         {
194                                 halfsizex*=2;
195                                 halfsizey*=2;
196                         }
197                         workdesc.set_subwindow(-max(1,halfsizex),-max(1,halfsizey),origin_w+2*max(1,halfsizex),origin_h+2*max(1,halfsizey));
198                         break;
199                 }
200                 case Blur::GAUSSIAN:
201                 {
202                 #define GAUSSIAN_ADJUSTMENT             (0.05)
203                         Real    pw = (Real)workdesc.get_w()/(workdesc.get_br()[0]-workdesc.get_tl()[0]);
204                         Real    ph = (Real)workdesc.get_h()/(workdesc.get_br()[1]-workdesc.get_tl()[1]);
205
206                         pw=pw*pw;
207                         ph=ph*ph;
208
209                         halfsizex = (int)(abs(pw)*size[0]*GAUSSIAN_ADJUSTMENT+0.5);
210                         halfsizey = (int)(abs(ph)*size[1]*GAUSSIAN_ADJUSTMENT+0.5);
211
212                         halfsizex = (halfsizex + 1)/2;
213                         halfsizey = (halfsizey + 1)/2;
214                         workdesc.set_subwindow( -halfsizex, -halfsizey, origin_w+2*halfsizex, origin_h+2*halfsizey );
215
216                         break;
217                 }
218         }
219 #define SCALE_FACTOR    (64.0)
220         if(/*quality>9 || */size[0]<=pw*SCALE_FACTOR)
221         {
222                 SuperCallback stageone(cb,0,5000,10000);
223                 SuperCallback stagetwo(cb,5000,10000,10000);
224
225                 //callbacks depend on how long the blur takes
226                 if(size[0] || size[1])
227                 {
228                         if(type == Blur::DISC)
229                         {
230                                 stageone = SuperCallback(cb,0,5000,10000);
231                                 stagetwo = SuperCallback(cb,5000,10000,10000);
232                         }
233                         else
234                         {
235                                 stageone = SuperCallback(cb,0,9000,10000);
236                                 stagetwo = SuperCallback(cb,9000,10000,10000);
237                         }
238                 }
239                 else
240                 {
241                         stageone = SuperCallback(cb,0,9999,10000);
242                         stagetwo = SuperCallback(cb,9999,10000,10000);
243                 }
244
245
246
247                 //render the background onto the expanded surface
248                 if(!context.accelerated_render(&worksurface,quality,workdesc,&stageone))
249                         return false;
250
251                 // Copy over the alpha
252                 blurred.set_wh(worksurface.get_w(),worksurface.get_h());
253                 for(int j=0;j<worksurface.get_h();j++)
254                         for(int i=0;i<worksurface.get_w();i++)
255                         {
256                                 blurred[j][i]=worksurface[j][i].get_a();
257                         }
258
259                 //blur the image
260                 Blur(size,type,&stagetwo)(blurred,workdesc.get_br()-workdesc.get_tl(),blurred);
261
262                 //be sure the surface is of the correct size
263                 surface->set_wh(renddesc.get_w(),renddesc.get_h());
264
265                 int u = halfsizex-(origin_u<0?origin_u:0), v = halfsizey-(origin_v<0?origin_v:0);
266                 for(y=0;y<renddesc.get_h();y++,v++)
267                 {
268                         u = halfsizex-(origin_u<0?origin_u:0);
269                         for(x=0;x<renddesc.get_w();x++,u++)
270                         {
271                                 Color a(color);
272
273                                 if(!invert)
274                                         a.set_a(blurred.linear_sample(origin_u+(float)u,origin_v+(float)v));
275                                 else
276                                         a.set_a(1.0f-blurred.linear_sample(origin_u+(float)u,origin_v+(float)v));
277
278                                 if(a.get_a() || get_blend_method()==Color::BLEND_STRAIGHT)
279                                 {
280                                         (*surface)[y][x]=Color::blend(a,worksurface[v][u],get_amount(),get_blend_method());
281                                 }
282                                 else (*surface)[y][x] = worksurface[v][u];
283                         }
284                 }
285         }
286         else
287         {
288
289                 SuperCallback stageone(cb,0,5000,10000);
290                 SuperCallback stagetwo(cb,5000,10000,10000);
291
292                 //callbacks depend on how long the blur takes
293                 if(size[0] || size[1])
294                 {
295                         if(type == Blur::DISC)
296                         {
297                                 stageone = SuperCallback(cb,0,5000,10000);
298                                 stagetwo = SuperCallback(cb,5000,10000,10000);
299                         }
300                         else
301                         {
302                                 stageone = SuperCallback(cb,0,9000,10000);
303                                 stagetwo = SuperCallback(cb,9000,10000,10000);
304                         }
305                 }
306                 else
307                 {
308                         stageone = SuperCallback(cb,0,9999,10000);
309                         stagetwo = SuperCallback(cb,9999,10000,10000);
310                 }
311
312                 int fw(floor_to_int(abs(size[0]/(pw*SCALE_FACTOR)))+1);
313                 int fh(floor_to_int(abs(size[1]/(ph*SCALE_FACTOR)))+1);
314                 int tmpw(round_to_int((float)workdesc.get_w()/fw)),tmph(round_to_int((float)workdesc.get_h()/fh));
315
316                 workdesc.clear_flags();
317                 workdesc.set_wh(tmpw,tmph);
318                 //synfig::info("fw: %d, fh: %d",fw,fh);
319
320                 //render the blur fodder
321                 if(!context.accelerated_render(&worksurface,quality,workdesc,&stageone))
322                         return false;
323
324                 //render the background
325                 if(!context.accelerated_render(surface,quality,renddesc,&stageone))
326                         return false;
327
328                 // Copy over the alpha
329                 blurred.set_wh(worksurface.get_w(),worksurface.get_h());
330                 for(int j=0;j<worksurface.get_h();j++)
331                         for(int i=0;i<worksurface.get_w();i++)
332                                 blurred[j][i]=worksurface[j][i].get_a();
333
334                 //blur the image
335                 Blur(size,type,&stagetwo)(blurred,workdesc.get_br()-workdesc.get_tl(),blurred);
336
337
338                 int u = halfsizex-(origin_u<0?origin_u:0), v = halfsizey-(origin_v<0?origin_v:0);
339                 for(y=0;y<renddesc.get_h();y++,v++)
340                 {
341                         u = halfsizex-(origin_u<0?origin_u:0);
342                         for(x=0;x<renddesc.get_w();x++,u++)
343                         {
344                                 Color a(color);
345
346                                 if(!invert)
347                                         a.set_a(blurred.linear_sample(((float)origin_u+(float)u)/(float)fw,((float)origin_v+(float)v)/(float)fh));
348                                 else
349                                         a.set_a(1.0f-blurred.linear_sample(((float)origin_u+(float)u)/fw,((float)origin_v+(float)v)/(float)fh));
350
351                                 if(a.get_a() || get_blend_method()==Color::BLEND_STRAIGHT)
352                                 {
353                                         (*surface)[y][x]=Color::blend(a,(*surface)[y][x],get_amount(),get_blend_method());
354                                 }
355                         }
356                 }
357         }
358
359
360         if(cb && !cb->amount_complete(10000,10000))
361         {
362                 //if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
363                 return false;
364         }
365
366         return true;
367 }
368
369 Layer::Vocab
370 Layer_Shade::get_param_vocab(void)const
371 {
372         Layer::Vocab ret(Layer_Composite::get_param_vocab());
373
374         ret.push_back(ParamDesc("color")
375                 .set_local_name(_("Color"))
376         );
377         ret.push_back(ParamDesc("origin")
378                 .set_local_name(_("Origin"))
379         );
380         ret.push_back(ParamDesc("size")
381                 .set_local_name(_("Size"))
382                 .set_description(_("Size of Shade"))
383                 .set_is_distance()
384                 .set_origin("origin")
385         );
386         ret.push_back(ParamDesc("type")
387                 .set_local_name(_("Type"))
388                 .set_description(_("Type of blur to use"))
389                 .set_hint("enum")
390                 .add_enum_value(Blur::BOX,"box",_("Box Blur"))
391                 .add_enum_value(Blur::FASTGAUSSIAN,"fastgaussian",_("Fast Gaussian Blur"))
392                 .add_enum_value(Blur::CROSS,"cross",_("Cross-Hatch Blur"))
393                 .add_enum_value(Blur::GAUSSIAN,"gaussian",_("Gaussian Blur"))
394                 .add_enum_value(Blur::DISC,"disc",_("Disc Blur"))
395         );
396
397         ret.push_back(ParamDesc("invert")
398                 .set_local_name(_("Invert"))
399         );
400
401         return ret;
402 }
403
404 Rect
405 Layer_Shade::get_full_bounding_rect(Context context)const
406 {
407         if(is_disabled())
408                 return context.get_full_bounding_rect();
409
410         if(invert)
411                 return Rect::full_plane();
412
413         Rect under(context.get_full_bounding_rect());
414
415         if(Color::is_onto(get_blend_method()))
416                 return under;
417
418         Rect bounds((under+origin).expand_x(size[0]).expand_y(size[1]));
419
420         if(is_solid_color())
421                 return bounds;
422
423         return bounds|under;
424 }