Merge branch 'genete_static_values'
[synfig.git] / synfig-core / src / synfig / layer_motionblur.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file layer_motionblur.cpp
3 **      \brief Implementation of the "Motion Blur" layer
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007, 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 "string.h"
34 #include "layer_motionblur.h"
35 #include "time.h"
36 #include "context.h"
37 #include "paramdesc.h"
38 #include "renddesc.h"
39 #include "surface.h"
40 #include "value.h"
41 #include "valuenode.h"
42 #include "canvas.h"
43
44 #endif
45
46 /* === U S I N G =========================================================== */
47
48 using namespace synfig;
49 using namespace etl;
50 using namespace std;
51
52 /* === G L O B A L S ======================================================= */
53
54 SYNFIG_LAYER_INIT(Layer_MotionBlur);
55 SYNFIG_LAYER_SET_NAME(Layer_MotionBlur,"MotionBlur"); // todo: use motion_blur
56 SYNFIG_LAYER_SET_LOCAL_NAME(Layer_MotionBlur,N_("Motion Blur"));
57 SYNFIG_LAYER_SET_CATEGORY(Layer_MotionBlur,N_("Blurs"));
58 SYNFIG_LAYER_SET_VERSION(Layer_MotionBlur,"0.1");
59 SYNFIG_LAYER_SET_CVS_ID(Layer_MotionBlur,"$Id$");
60
61 /* === M E M B E R S ======================================================= */
62
63 Layer_MotionBlur::Layer_MotionBlur():
64         Layer_Composite         (1.0,Color::BLEND_STRAIGHT),
65         aperture                        (0),
66         subsamples_factor       (1.0),
67         subsampling_type        (SUBSAMPLING_HYPERBOLIC),
68         subsample_start         (0.0),
69         subsample_end           (1.0)
70 {
71         Layer::Vocab voc(get_param_vocab());
72         Layer::fill_static(voc);
73 }
74
75 bool
76 Layer_MotionBlur::set_param(const String &param, const ValueBase &value)
77 {
78
79         IMPORT(aperture);
80         IMPORT(subsamples_factor);
81         IMPORT(subsampling_type);
82         IMPORT(subsample_start);
83         IMPORT(subsample_end);
84         return Layer_Composite::set_param(param,value);
85 }
86
87 ValueBase
88 Layer_MotionBlur::get_param(const String &param)const
89 {
90         EXPORT(aperture);
91         EXPORT(subsamples_factor);
92         EXPORT(subsampling_type);
93         EXPORT(subsample_start);
94         EXPORT(subsample_end);
95
96         EXPORT_NAME();
97         EXPORT_VERSION();
98
99         return Layer_Composite::get_param(param);
100 }
101
102 void
103 Layer_MotionBlur::set_time(Context context, Time time)const
104 {
105         context.set_time(time);
106         time_cur=time;
107 }
108
109 void
110 Layer_MotionBlur::set_time(Context context, Time time, const Point &pos)const
111 {
112         context.set_time(time,pos);
113         time_cur=time;
114 }
115
116 Color
117 Layer_MotionBlur::get_color(Context context, const Point &pos)const
118 {
119 /*      if(aperture)
120         {
121                 Time time(time_cur);
122                 time+=(Vector::value_type)( (signed)(RAND_MAX/2)-(signed)rand() )/(Vector::value_type)(RAND_MAX) *aperture -aperture*0.5;
123                 context.set_time(time, pos);
124         }
125 */
126         return context.get_color(pos);
127 }
128
129 Layer::Vocab
130 Layer_MotionBlur::get_param_vocab()const
131 {
132         Layer::Vocab ret;
133         //ret=Layer_Composite::get_param_vocab();
134
135         ret.push_back(ParamDesc("aperture")
136                 .set_local_name(_("Aperture"))
137                 .set_description(_("Shutter Time"))
138         );
139
140         ret.push_back(ParamDesc("subsamples_factor")
141                 .set_local_name(_("Subsamples Factor"))
142                 .set_description(_("Multiplies The Number Of Subsamples Rendered"))
143         );
144
145         ret.push_back(ParamDesc("subsampling_type")
146                 .set_local_name(_("Subsampling Type"))
147                 .set_description(_("Curve Type For Weighting Subsamples"))
148                 .set_hint("enum")
149                 .add_enum_value(SUBSAMPLING_CONSTANT,"constant",_("Constant"))
150                 .add_enum_value(SUBSAMPLING_LINEAR,"linear",_("Linear"))
151                 .add_enum_value(SUBSAMPLING_HYPERBOLIC,"hyperbolic",_("Hyperbolic"))
152         );
153
154         ret.push_back(ParamDesc("subsample_start")
155                 .set_local_name(_("Subsample Start Amount"))
156                 .set_description(_("Relative Amount Of The First Subsample, For Linear Weighting"))
157         );
158
159         ret.push_back(ParamDesc("subsample_end")
160                 .set_local_name(_("Subsample End Amount"))
161                 .set_description(_("Relative Amount Of The Last Subsample, For Linear Weighting"))
162         );
163
164         return ret;
165 }
166
167 bool
168 Layer_MotionBlur::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
169 {
170         if(aperture && quality<=10)
171         {
172                 //int x, y;
173                 SuperCallback subimagecb;
174                 int samples=1;
175                 switch(quality)
176                 {
177                         case 1: // Production Quality
178                                 samples=32;
179                                 break;
180                         case 2: // Excellent Quality
181                                 samples=24;
182                                 break;
183                         case 3: // Good Quality
184                                 samples=16;
185                                 break;
186                         case 4: // Moderate Quality
187                                 samples=12;
188                                 break;
189                         case 5: // Draft Quality
190                                 samples=7;
191                                 break;
192                         case 6:
193                                 samples=6;
194                                 break;
195                         case 7:
196                                 samples=5;
197                                 break;
198                         case 8:
199                                 samples=3;
200                                 break;
201                         case 9:
202                                 samples=2;
203                                 break;
204                         case 10: // Rough Quality
205             default:
206                                 samples=1;
207                                 break;
208
209                 }
210
211                 samples *= subsamples_factor;
212
213                 if (samples <= 1) return context.accelerated_render(surface,quality,renddesc,cb);
214
215                 // Only in modes where subsample_start/end matters...
216                 if(subsampling_type == SUBSAMPLING_LINEAR)
217                 {
218                         // We won't render when the scale==0, so we'll use those samples elsewhere
219                         if(subsample_start == 0) samples++;
220                         if(subsample_end == 0) samples++;
221                 }
222
223                 Surface tmp;
224                 int i;
225                 float scale, divisor = 0;
226
227                 surface->set_wh(renddesc.get_w(),renddesc.get_h());
228                 surface->clear();
229
230                 // Render subsamples from time_cur-aperture to time_cur
231                 for(i=0;i<samples;i++)
232                 {
233                         float pos = i/(samples-1.0);
234                         float ipos = 1.0-pos;
235                         switch(subsampling_type)
236                         {
237                                 case SUBSAMPLING_LINEAR:
238                                         scale = ipos*subsample_start + pos*subsample_end;
239                                         break;
240                                 case SUBSAMPLING_HYPERBOLIC:
241                                         scale = 1.0/(samples-i);
242                                         break;
243                                 case SUBSAMPLING_CONSTANT:
244                                 default:
245                                         scale = 1.0; // Weights don't matter for constant overall subsampling.
246                                         break;
247                         }
248                         // Don't bother rendering if scale is zero
249                         if(scale==0)
250                                 continue;
251                         divisor += scale;
252                         subimagecb=SuperCallback(cb,i*(5000/samples),(i+1)*(5000/samples),5000);
253                         context.set_time(time_cur-aperture*ipos);
254                         if(!context.accelerated_render(&tmp,quality,renddesc,&subimagecb))
255                                 return false;
256                         for(int y=0;y<renddesc.get_h();y++)
257                                 for(int x=0;x<renddesc.get_w();x++)
258                                         (*surface)[y][x]+=tmp[y][x].premult_alpha()*scale;
259                 }
260                 for(int y=0;y<renddesc.get_h();y++)
261                         for(int x=0;x<renddesc.get_w();x++)
262                                 (*surface)[y][x]=((*surface)[y][x]/divisor).demult_alpha();
263         }
264         else
265                 return context.accelerated_render(surface,quality,renddesc,cb);
266
267         return true;
268 }