Rearrange conditions for export action and fix bug 2956710
[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 }
72
73 bool
74 Layer_MotionBlur::set_param(const String &param, const ValueBase &value)
75 {
76
77         IMPORT(aperture);
78         IMPORT(subsamples_factor);
79         IMPORT(subsampling_type);
80         IMPORT(subsample_start);
81         IMPORT(subsample_end);
82         return Layer_Composite::set_param(param,value);
83 }
84
85 ValueBase
86 Layer_MotionBlur::get_param(const String &param)const
87 {
88         EXPORT(aperture);
89         EXPORT(subsamples_factor);
90         EXPORT(subsampling_type);
91         EXPORT(subsample_start);
92         EXPORT(subsample_end);
93
94         EXPORT_NAME();
95         EXPORT_VERSION();
96
97         return Layer_Composite::get_param(param);
98 }
99
100 void
101 Layer_MotionBlur::set_time(Context context, Time time)const
102 {
103         context.set_time(time);
104         time_cur=time;
105 }
106
107 void
108 Layer_MotionBlur::set_time(Context context, Time time, const Point &pos)const
109 {
110         context.set_time(time,pos);
111         time_cur=time;
112 }
113
114 Color
115 Layer_MotionBlur::get_color(Context context, const Point &pos)const
116 {
117 /*      if(aperture)
118         {
119                 Time time(time_cur);
120                 time+=(Vector::value_type)( (signed)(RAND_MAX/2)-(signed)rand() )/(Vector::value_type)(RAND_MAX) *aperture -aperture*0.5;
121                 context.set_time(time, pos);
122         }
123 */
124         return context.get_color(pos);
125 }
126
127 Layer::Vocab
128 Layer_MotionBlur::get_param_vocab()const
129 {
130         Layer::Vocab ret;
131         //ret=Layer_Composite::get_param_vocab();
132
133         ret.push_back(ParamDesc("aperture")
134                 .set_local_name(_("Aperture"))
135                 .set_description(_("Shutter Time"))
136         );
137
138         ret.push_back(ParamDesc("subsamples_factor")
139                 .set_local_name(_("Subsamples Factor"))
140                 .set_description(_("Multiplies The Number Of Subsamples Rendered"))
141         );
142
143         ret.push_back(ParamDesc("subsampling_type")
144                 .set_local_name(_("Subsampling Type"))
145                 .set_description(_("Curve Type For Weighting Subsamples"))
146                 .set_hint("enum")
147                 .add_enum_value(SUBSAMPLING_CONSTANT,"constant",_("Constant"))
148                 .add_enum_value(SUBSAMPLING_LINEAR,"linear",_("Linear"))
149                 .add_enum_value(SUBSAMPLING_HYPERBOLIC,"hyperbolic",_("Hyperbolic"))
150         );
151
152         ret.push_back(ParamDesc("subsample_start")
153                 .set_local_name(_("Subsample Start Amount"))
154                 .set_description(_("Relative Amount Of The First Subsample, For Linear Weighting"))
155         );
156
157         ret.push_back(ParamDesc("subsample_end")
158                 .set_local_name(_("Subsample End Amount"))
159                 .set_description(_("Relative Amount Of The Last Subsample, For Linear Weighting"))
160         );
161
162         return ret;
163 }
164
165 bool
166 Layer_MotionBlur::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
167 {
168         if(aperture && quality<=10)
169         {
170                 //int x, y;
171                 SuperCallback subimagecb;
172                 int samples=1;
173                 switch(quality)
174                 {
175                         case 1: // Production Quality
176                                 samples=32;
177                                 break;
178                         case 2: // Excellent Quality
179                                 samples=24;
180                                 break;
181                         case 3: // Good Quality
182                                 samples=16;
183                                 break;
184                         case 4: // Moderate Quality
185                                 samples=12;
186                                 break;
187                         case 5: // Draft Quality
188                                 samples=7;
189                                 break;
190                         case 6:
191                                 samples=6;
192                                 break;
193                         case 7:
194                                 samples=5;
195                                 break;
196                         case 8:
197                                 samples=3;
198                                 break;
199                         case 9:
200                                 samples=2;
201                                 break;
202                         case 10: // Rough Quality
203             default:
204                                 samples=1;
205                                 break;
206
207                 }
208
209                 samples *= subsamples_factor;
210
211                 if (samples <= 1) return context.accelerated_render(surface,quality,renddesc,cb);
212
213                 // Only in modes where subsample_start/end matters...
214                 if(subsampling_type == SUBSAMPLING_LINEAR)
215                 {
216                         // We won't render when the scale==0, so we'll use those samples elsewhere
217                         if(subsample_start == 0) samples++;
218                         if(subsample_end == 0) samples++;
219                 }
220
221                 Surface tmp;
222                 int i;
223                 float scale, divisor = 0;
224
225                 surface->set_wh(renddesc.get_w(),renddesc.get_h());
226                 surface->clear();
227
228                 // Render subsamples from time_cur-aperture to time_cur
229                 for(i=0;i<samples;i++)
230                 {
231                         float pos = i/(samples-1.0);
232                         float ipos = 1.0-pos;
233                         switch(subsampling_type)
234                         {
235                                 case SUBSAMPLING_LINEAR:
236                                         scale = ipos*subsample_start + pos*subsample_end;
237                                         break;
238                                 case SUBSAMPLING_HYPERBOLIC:
239                                         scale = 1.0/(samples-i);
240                                         break;
241                                 case SUBSAMPLING_CONSTANT:
242                                 default:
243                                         scale = 1.0; // Weights don't matter for constant overall subsampling.
244                                         break;
245                         }
246                         // Don't bother rendering if scale is zero
247                         if(scale==0)
248                                 continue;
249                         divisor += scale;
250                         subimagecb=SuperCallback(cb,i*(5000/samples),(i+1)*(5000/samples),5000);
251                         context.set_time(time_cur-aperture*ipos);
252                         if(!context.accelerated_render(&tmp,quality,renddesc,&subimagecb))
253                                 return false;
254                         for(int y=0;y<renddesc.get_h();y++)
255                                 for(int x=0;x<renddesc.get_w();x++)
256                                         (*surface)[y][x]+=tmp[y][x].premult_alpha()*scale;
257                 }
258                 for(int y=0;y<renddesc.get_h();y++)
259                         for(int x=0;x<renddesc.get_w();x++)
260                                 (*surface)[y][x]=((*surface)[y][x]/divisor).demult_alpha();
261         }
262         else
263                 return context.accelerated_render(surface,quality,renddesc,cb);
264
265         return true;
266 }