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