Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_07_rc2 / src / modules / mod_filter / blur.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file mod_filter/blur.cpp
3 **      \brief Template Header
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 "blur.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
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(Blur_Layer);
65 SYNFIG_LAYER_SET_NAME(Blur_Layer,"blur");
66 SYNFIG_LAYER_SET_LOCAL_NAME(Blur_Layer,_("Blur"));
67 SYNFIG_LAYER_SET_CATEGORY(Blur_Layer,_("Blurs"));
68 SYNFIG_LAYER_SET_VERSION(Blur_Layer,"0.2");
69 SYNFIG_LAYER_SET_CVS_ID(Blur_Layer,"$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 Blur_Layer::Blur_Layer():
80         Layer_Composite (1.0,Color::BLEND_STRAIGHT),
81         size(0.1,0.1),
82         type(Blur::FASTGAUSSIAN)
83 {
84 }
85
86 bool
87 Blur_Layer::set_param(const String &param, const ValueBase &value)
88 {
89         IMPORT_PLUS(size,clamp(size));
90         IMPORT(type);
91
92         return Layer_Composite::set_param(param,value);
93 }
94
95 ValueBase
96 Blur_Layer::get_param(const String &param)const
97 {
98         EXPORT(size);
99         EXPORT(type);
100
101         EXPORT_NAME();
102         EXPORT_VERSION();
103
104         return Layer_Composite::get_param(param);
105 }
106
107 Color
108 Blur_Layer::get_color(Context context, const Point &pos)const
109 {
110         Point blurpos = Blur(size,type)(pos);
111
112         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
113                 return context.get_color(blurpos);
114
115         if(get_amount()==0.0)
116                 return context.get_color(pos);
117
118         return Color::blend(context.get_color(blurpos),context.get_color(pos),get_amount(),get_blend_method());
119 }
120
121 bool
122 Blur_Layer::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
123 {
124         // int x,y;
125         SuperCallback stageone(cb,0,5000,10000);
126         SuperCallback stagetwo(cb,5000,10000,10000);
127
128         const int       w = renddesc.get_w(),
129                                 h = renddesc.get_h();
130         const Real      pw = renddesc.get_pw(),
131                                 ph = renddesc.get_ph();
132
133         RendDesc        workdesc(renddesc);
134         Surface         worksurface,blurred;
135
136         //callbacks depend on how long the blur takes
137         if(size[0] || size[1])
138         {
139                 if(type == Blur::DISC)
140                 {
141                         stageone = SuperCallback(cb,0,5000,10000);
142                         stagetwo = SuperCallback(cb,5000,10000,10000);
143                 }
144                 else
145                 {
146                         stageone = SuperCallback(cb,0,9000,10000);
147                         stagetwo = SuperCallback(cb,9000,10000,10000);
148                 }
149         }
150         else
151         {
152                 stageone = SuperCallback(cb,0,9999,10000);
153                 stagetwo = SuperCallback(cb,9999,10000,10000);
154         }
155
156         //expand the working surface to accommodate the blur
157
158         //the expanded size = 1/2 the size in each direction rounded up
159         int     halfsizex = (int) (abs(size[0]*.5/pw) + 3),
160                 halfsizey = (int) (abs(size[1]*.5/ph) + 3);
161
162         //expand by 1/2 size in each direction on either side
163         switch(type)
164         {
165                 case Blur::DISC:
166                 case Blur::BOX:
167                 case Blur::CROSS:
168                 {
169                         workdesc.set_subwindow(-max(1,halfsizex),-max(1,halfsizey),w+2*max(1,halfsizex),h+2*max(1,halfsizey));
170                         break;
171                 }
172                 case Blur::FASTGAUSSIAN:
173                 {
174                         if(quality < 4)
175                         {
176                                 halfsizex*=2;
177                                 halfsizey*=2;
178                         }
179                         workdesc.set_subwindow(-max(1,halfsizex),-max(1,halfsizey),w+2*max(1,halfsizex),h+2*max(1,halfsizey));
180                         break;
181                 }
182                 case Blur::GAUSSIAN:
183                 {
184                 #define GAUSSIAN_ADJUSTMENT             (0.05)
185                         Real    pw = (Real)workdesc.get_w()/(workdesc.get_br()[0]-workdesc.get_tl()[0]);
186                         Real    ph = (Real)workdesc.get_h()/(workdesc.get_br()[1]-workdesc.get_tl()[1]);
187
188                         pw=pw*pw;
189                         ph=ph*ph;
190
191                         halfsizex = (int)(abs(pw)*size[0]*GAUSSIAN_ADJUSTMENT+0.5);
192                         halfsizey = (int)(abs(ph)*size[1]*GAUSSIAN_ADJUSTMENT+0.5);
193
194                         halfsizex = (halfsizex + 1)/2;
195                         halfsizey = (halfsizey + 1)/2;
196                         workdesc.set_subwindow( -halfsizex, -halfsizey, w+2*halfsizex, h+2*halfsizey );
197
198                         break;
199                 }
200         }
201
202         //render the background onto the expanded surface
203         if(!context.accelerated_render(&worksurface,quality,workdesc,&stageone))
204                 return false;
205
206         //blur the image
207         Blur(size,type,&stagetwo)(worksurface,workdesc.get_br()-workdesc.get_tl(),blurred);
208
209         //be sure the surface is of the correct size
210         surface->set_wh(renddesc.get_w(),renddesc.get_h());
211
212         {
213                 Surface::pen pen(surface->begin());
214                 worksurface.blit_to(pen,halfsizex,halfsizey,renddesc.get_w(),renddesc.get_h());
215         }
216         {
217                 Surface::alpha_pen pen(surface->begin());
218                 pen.set_alpha(get_amount());
219                 pen.set_blend_method(get_blend_method());
220                 blurred.blit_to(pen,halfsizex,halfsizey,renddesc.get_w(),renddesc.get_h());
221         }
222         if(cb && !cb->amount_complete(10000,10000))
223         {
224                 //if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
225                 return false;
226         }
227
228         return true;
229 }
230
231 Layer::Vocab
232 Blur_Layer::get_param_vocab(void)const
233 {
234         Layer::Vocab ret(Layer_Composite::get_param_vocab());
235
236         ret.push_back(ParamDesc("size")
237                 .set_local_name(_("Size"))
238                 .set_description(_("Size of Blur"))
239         );
240         ret.push_back(ParamDesc("type")
241                 .set_local_name(_("Type"))
242                 .set_description(_("Type of blur to use"))
243                 .set_hint("enum")
244                 .add_enum_value(Blur::BOX,"box",_("Box Blur"))
245                 .add_enum_value(Blur::FASTGAUSSIAN,"fastgaussian",_("Fast Gaussian Blur"))
246                 .add_enum_value(Blur::CROSS,"cross",_("Cross-Hatch Blur"))
247                 .add_enum_value(Blur::GAUSSIAN,"gaussian",_("Gaussian Blur"))
248                 .add_enum_value(Blur::DISC,"disc",_("Disc Blur"))
249         );
250
251         return ret;
252 }
253
254 Rect
255 Blur_Layer::get_full_bounding_rect(Context context)const
256 {
257         if(is_disabled() || Color::is_onto(get_blend_method()))
258                 return context.get_full_bounding_rect();
259
260         Rect bounds(context.get_full_bounding_rect().expand_x(size[0]).expand_y(size[1]));
261
262         return bounds;
263 }