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