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