Initial Stable Commit
[synfig.git] / synfig-core / trunk / src / modules / mod_filter / blur.cpp
1 /* === S I N F G =========================================================== */
2 /*!     \file blur.cpp
3 **      \brief Template Header
4 **
5 **      $Id: blur.cpp,v 1.2 2005/01/24 03:08:17 darco Exp $
6 **
7 **      \legal
8 **      Copyright (c) 2002 Robert B. Quattlebaum Jr.
9 **
10 **      This software and associated documentation
11 **      are CONFIDENTIAL and PROPRIETARY property of
12 **      the above-mentioned copyright holder.
13 **
14 **      You may not copy, print, publish, or in any
15 **      other way distribute this software without
16 **      a prior written agreement with
17 **      the copyright holder.
18 **      \endlegal
19 */
20 /* ========================================================================= */
21
22 /* === H E A D E R S ======================================================= */
23
24 #ifdef USING_PCH
25 #       include "pch.h"
26 #else
27 #ifdef HAVE_CONFIG_H
28 #       include <config.h>
29 #endif
30
31 #include "blur.h"
32
33 #include <sinfg/string.h>
34 #include <sinfg/time.h>
35 #include <sinfg/context.h>
36 #include <sinfg/paramdesc.h>
37 #include <sinfg/renddesc.h>
38 #include <sinfg/surface.h>
39 #include <sinfg/value.h>
40 #include <sinfg/valuenode.h>
41 #include <sinfg/segment.h>
42
43 #include <cstring>
44 #include <ETL/pen>
45
46 #endif
47
48 using namespace sinfg;
49 using namespace etl;
50 using namespace std;
51
52 /*#define TYPE_BOX                      0
53 #define TYPE_FASTGUASSIAN       1
54 #define TYPE_FASTGAUSSIAN       1
55 #define TYPE_CROSS                      2
56 #define TYPE_GUASSIAN           3
57 #define TYPE_GAUSSIAN           3
58 #define TYPE_DISC                       4
59 */
60
61 /* -- G L O B A L S --------------------------------------------------------- */
62
63 SINFG_LAYER_INIT(Blur_Layer);
64 SINFG_LAYER_SET_NAME(Blur_Layer,"blur");
65 SINFG_LAYER_SET_LOCAL_NAME(Blur_Layer,_("Blur"));
66 SINFG_LAYER_SET_CATEGORY(Blur_Layer,_("Blurs"));
67 SINFG_LAYER_SET_VERSION(Blur_Layer,"0.2");
68 SINFG_LAYER_SET_CVS_ID(Blur_Layer,"$Id: blur.cpp,v 1.2 2005/01/24 03:08:17 darco Exp $");
69
70 /* -- F U N C T I O N S ----------------------------------------------------- */
71
72 inline void clamp(sinfg::Vector &v)
73 {
74         if(v[0]<0.0)v[0]=0.0;
75         if(v[1]<0.0)v[1]=0.0;
76 }
77
78 Blur_Layer::Blur_Layer():
79         Layer_Composite (1.0,Color::BLEND_STRAIGHT),
80         size(0.1,0.1),
81         type(Blur::FASTGAUSSIAN)
82 {
83 }
84
85 bool
86 Blur_Layer::set_param(const String &param, const ValueBase &value)
87 {
88         IMPORT_PLUS(size,clamp(size));
89         IMPORT(type);
90
91         return Layer_Composite::set_param(param,value);
92 }
93
94 ValueBase
95 Blur_Layer::get_param(const String &param)const
96 {
97         EXPORT(size);
98         EXPORT(type);
99         
100         EXPORT_NAME();
101         EXPORT_VERSION();
102                 
103         return Layer_Composite::get_param(param);       
104 }
105
106 Color
107 Blur_Layer::get_color(Context context, const Point &pos)const
108 {
109         Point blurpos = Blur(size,type)(pos);
110         
111         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
112                 return context.get_color(blurpos);
113
114         if(get_amount()==0.0)
115                 return context.get_color(pos);
116
117         return Color::blend(context.get_color(blurpos),context.get_color(pos),get_amount(),get_blend_method());
118 }
119
120 bool
121 Blur_Layer::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
122 {
123         int x,y;
124         SuperCallback stageone(cb,0,5000,10000);
125         SuperCallback stagetwo(cb,5000,10000,10000);
126         
127         const int       w = renddesc.get_w(),
128                                 h = renddesc.get_h();
129         const Real      pw = renddesc.get_pw(),
130                                 ph = renddesc.get_ph();
131         
132         RendDesc        workdesc(renddesc);
133         Surface         worksurface,blurred;
134                 
135         //callbacks depend on how long the blur takes
136         if(size[0] || size[1])
137         {
138                 if(type == Blur::DISC)
139                 {
140                         stageone = SuperCallback(cb,0,5000,10000);
141                         stagetwo = SuperCallback(cb,5000,10000,10000);  
142                 }
143                 else
144                 {
145                         stageone = SuperCallback(cb,0,9000,10000);
146                         stagetwo = SuperCallback(cb,9000,10000,10000);  
147                 }
148         }
149         else
150         {
151                 stageone = SuperCallback(cb,0,9999,10000);
152                 stagetwo = SuperCallback(cb,9999,10000,10000);  
153         }
154         
155         //expand the working surface to accommodate the blur
156         
157         //the expanded size = 1/2 the size in each direction rounded up
158         int     halfsizex = (int) (abs(size[0]*.5/pw) + 3),
159                 halfsizey = (int) (abs(size[1]*.5/ph) + 3);
160                 
161         //expand by 1/2 size in each direction on either side
162         switch(type)
163         {
164                 case Blur::DISC:
165                 case Blur::BOX:
166                 case Blur::CROSS:
167                 {
168                         workdesc.set_subwindow(-max(1,halfsizex),-max(1,halfsizey),w+2*max(1,halfsizex),h+2*max(1,halfsizey));
169                         break;
170                 }
171                 case Blur::FASTGAUSSIAN:
172                 {
173                         if(quality < 4)
174                         {
175                                 halfsizex*=2;
176                                 halfsizey*=2;
177                         }
178                         workdesc.set_subwindow(-max(1,halfsizex),-max(1,halfsizey),w+2*max(1,halfsizex),h+2*max(1,halfsizey));
179                         break;
180                 }
181                 case Blur::GAUSSIAN:
182                 {
183                 #define GAUSSIAN_ADJUSTMENT             (0.05)
184                         Real    pw = (Real)workdesc.get_w()/(workdesc.get_br()[0]-workdesc.get_tl()[0]);
185                         Real    ph = (Real)workdesc.get_h()/(workdesc.get_br()[1]-workdesc.get_tl()[1]);
186                         
187                         pw=pw*pw;
188                         ph=ph*ph;
189
190                         halfsizex = (int)(abs(pw)*size[0]*GAUSSIAN_ADJUSTMENT+0.5);
191                         halfsizey = (int)(abs(ph)*size[1]*GAUSSIAN_ADJUSTMENT+0.5);
192
193                         halfsizex = (halfsizex + 1)/2;
194                         halfsizey = (halfsizey + 1)/2;
195                         workdesc.set_subwindow( -halfsizex, -halfsizey, w+2*halfsizex, h+2*halfsizey );
196                                                 
197                         break;
198                 }
199         }
200         
201         //render the background onto the expanded surface
202         if(!context.accelerated_render(&worksurface,quality,workdesc,&stageone))
203                 return false;
204
205         //blur the image
206         Blur(size,type,&stagetwo)(worksurface,workdesc.get_br()-workdesc.get_tl(),blurred);
207         
208         //be sure the surface is of the correct size
209         surface->set_wh(renddesc.get_w(),renddesc.get_h());
210         
211         {
212                 Surface::pen pen(surface->begin());
213                 worksurface.blit_to(pen,halfsizex,halfsizey,renddesc.get_w(),renddesc.get_h());
214         }
215         {
216                 Surface::alpha_pen pen(surface->begin());
217                 pen.set_alpha(get_amount());
218                 pen.set_blend_method(get_blend_method());
219                 blurred.blit_to(pen,halfsizex,halfsizey,renddesc.get_w(),renddesc.get_h());
220         }
221         if(cb && !cb->amount_complete(10000,10000))
222         {
223                 //if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
224                 return false;
225         }
226                 
227         return true;
228 }
229         
230 Layer::Vocab
231 Blur_Layer::get_param_vocab(void)const
232 {
233         Layer::Vocab ret(Layer_Composite::get_param_vocab());
234
235         ret.push_back(ParamDesc("size")
236                 .set_local_name(_("Size"))
237                 .set_description(_("Size of Blur"))
238         );
239         ret.push_back(ParamDesc("type")
240                 .set_local_name(_("Type"))
241                 .set_description(_("Type of blur to use"))
242                 .set_hint("enum")
243                 .add_enum_value(Blur::BOX,"box",_("Box Blur"))
244                 .add_enum_value(Blur::FASTGAUSSIAN,"fastgaussian",_("Fast Gaussian Blur"))
245                 .add_enum_value(Blur::CROSS,"cross",_("Cross-Hatch Blur"))
246                 .add_enum_value(Blur::GAUSSIAN,"gaussian",_("Gaussian Blur"))
247                 .add_enum_value(Blur::DISC,"disc",_("Disc Blur"))
248         );
249         
250         return ret;
251 }
252
253 Rect
254 Blur_Layer::get_full_bounding_rect(Context context)const
255 {
256         if(is_disabled() || Color::is_onto(get_blend_method()))
257                 return context.get_full_bounding_rect();
258
259         Rect bounds(context.get_full_bounding_rect().expand_x(size[0]).expand_y(size[1]));
260         
261         return bounds;
262 }