72dbc766425076f2e1553334cecd2bd91bf803db
[synfig.git] / synfig-core / trunk / 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 **
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,N_("Blur"));
67 SYNFIG_LAYER_SET_CATEGORY(Blur_Layer,N_("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         // don't do anything at quality 10
125         if (quality == 10)
126                 return context.accelerated_render(surface,quality,renddesc,cb);
127
128         // int x,y;
129         SuperCallback stageone(cb,0,5000,10000);
130         SuperCallback stagetwo(cb,5000,10000,10000);
131
132         const int       w = renddesc.get_w(),
133                                 h = renddesc.get_h();
134         const Real      pw = renddesc.get_pw(),
135                                 ph = renddesc.get_ph();
136
137         RendDesc        workdesc(renddesc);
138         Surface         worksurface,blurred;
139
140         //callbacks depend on how long the blur takes
141         if(size[0] || size[1])
142         {
143                 if(type == Blur::DISC)
144                 {
145                         stageone = SuperCallback(cb,0,5000,10000);
146                         stagetwo = SuperCallback(cb,5000,10000,10000);
147                 }
148                 else
149                 {
150                         stageone = SuperCallback(cb,0,9000,10000);
151                         stagetwo = SuperCallback(cb,9000,10000,10000);
152                 }
153         }
154         else
155         {
156                 stageone = SuperCallback(cb,0,9999,10000);
157                 stagetwo = SuperCallback(cb,9999,10000,10000);
158         }
159
160         //expand the working surface to accommodate the blur
161
162         //the expanded size = 1/2 the size in each direction rounded up
163         int     halfsizex = (int) (abs(size[0]*.5/pw) + 3),
164                 halfsizey = (int) (abs(size[1]*.5/ph) + 3);
165
166         //expand by 1/2 size in each direction on either side
167         switch(type)
168         {
169                 case Blur::DISC:
170                 case Blur::BOX:
171                 case Blur::CROSS:
172                 {
173                         workdesc.set_subwindow(-max(1,halfsizex),-max(1,halfsizey),w+2*max(1,halfsizex),h+2*max(1,halfsizey));
174                         break;
175                 }
176                 case Blur::FASTGAUSSIAN:
177                 {
178                         if(quality < 4)
179                         {
180                                 halfsizex*=2;
181                                 halfsizey*=2;
182                         }
183                         workdesc.set_subwindow(-max(1,halfsizex),-max(1,halfsizey),w+2*max(1,halfsizex),h+2*max(1,halfsizey));
184                         break;
185                 }
186                 case Blur::GAUSSIAN:
187                 {
188                 #define GAUSSIAN_ADJUSTMENT             (0.05)
189                         Real    pw = (Real)workdesc.get_w()/(workdesc.get_br()[0]-workdesc.get_tl()[0]);
190                         Real    ph = (Real)workdesc.get_h()/(workdesc.get_br()[1]-workdesc.get_tl()[1]);
191
192                         pw=pw*pw;
193                         ph=ph*ph;
194
195                         halfsizex = (int)(abs(pw)*size[0]*GAUSSIAN_ADJUSTMENT+0.5);
196                         halfsizey = (int)(abs(ph)*size[1]*GAUSSIAN_ADJUSTMENT+0.5);
197
198                         halfsizex = (halfsizex + 1)/2;
199                         halfsizey = (halfsizey + 1)/2;
200                         workdesc.set_subwindow( -halfsizex, -halfsizey, w+2*halfsizex, h+2*halfsizey );
201
202                         break;
203                 }
204         }
205
206         //render the background onto the expanded surface
207         if(!context.accelerated_render(&worksurface,quality,workdesc,&stageone))
208                 return false;
209
210         //blur the image
211         Blur(size,type,&stagetwo)(worksurface,workdesc.get_br()-workdesc.get_tl(),blurred);
212
213         //be sure the surface is of the correct size
214         surface->set_wh(renddesc.get_w(),renddesc.get_h());
215
216         {
217                 Surface::pen pen(surface->begin());
218                 worksurface.blit_to(pen,halfsizex,halfsizey,renddesc.get_w(),renddesc.get_h());
219         }
220         {
221                 Surface::alpha_pen pen(surface->begin());
222                 pen.set_alpha(get_amount());
223                 pen.set_blend_method(get_blend_method());
224                 blurred.blit_to(pen,halfsizex,halfsizey,renddesc.get_w(),renddesc.get_h());
225         }
226         if(cb && !cb->amount_complete(10000,10000))
227         {
228                 //if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
229                 return false;
230         }
231
232         return true;
233 }
234
235 Layer::Vocab
236 Blur_Layer::get_param_vocab(void)const
237 {
238         Layer::Vocab ret(Layer_Composite::get_param_vocab());
239
240         ret.push_back(ParamDesc("size")
241                 .set_local_name(_("Size"))
242                 .set_description(_("Size of Blur"))
243         );
244         ret.push_back(ParamDesc("type")
245                 .set_local_name(_("Type"))
246                 .set_description(_("Type of blur to use"))
247                 .set_hint("enum")
248                 .add_enum_value(Blur::BOX,"box",_("Box Blur"))
249                 .add_enum_value(Blur::FASTGAUSSIAN,"fastgaussian",_("Fast Gaussian Blur"))
250                 .add_enum_value(Blur::CROSS,"cross",_("Cross-Hatch Blur"))
251                 .add_enum_value(Blur::GAUSSIAN,"gaussian",_("Gaussian Blur"))
252                 .add_enum_value(Blur::DISC,"disc",_("Disc Blur"))
253         );
254
255         return ret;
256 }
257
258 Rect
259 Blur_Layer::get_full_bounding_rect(Context context)const
260 {
261         if(is_disabled() || Color::is_onto(get_blend_method()))
262                 return context.get_full_bounding_rect();
263
264         Rect bounds(context.get_full_bounding_rect().expand_x(size[0]).expand_y(size[1]));
265
266         return bounds;
267 }