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