Add "By Layer Default" blend method to the Toolbox Defaults widget.
[synfig.git] / synfig-core / src / modules / mod_noise / noise.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file noise.cpp
3 **      \brief Implementation of the "Noise Gradient" layer
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007 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 "noise.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
44 #endif
45
46 /* === M A C R O S ========================================================= */
47
48 using namespace synfig;
49 using namespace std;
50 using namespace etl;
51
52 /* === G L O B A L S ======================================================= */
53
54 SYNFIG_LAYER_INIT(Noise);
55 SYNFIG_LAYER_SET_NAME(Noise,"noise");
56 SYNFIG_LAYER_SET_LOCAL_NAME(Noise,N_("Noise Gradient"));
57 SYNFIG_LAYER_SET_CATEGORY(Noise,N_("Gradients"));
58 SYNFIG_LAYER_SET_VERSION(Noise,"0.0");
59 SYNFIG_LAYER_SET_CVS_ID(Noise,"$Id$");
60
61 /* === P R O C E D U R E S ================================================= */
62
63 /* === M E T H O D S ======================================================= */
64
65 Noise::Noise():
66         Layer_Composite(1.0,Color::BLEND_COMPOSITE),
67         size(1,1),
68         gradient(Color::black(), Color::white())
69 {
70         smooth=RandomNoise::SMOOTH_COSINE;
71         detail=4;
72         speed=0;
73         do_alpha=false;
74         random.set_seed(time(NULL));
75         turbulent=false;
76         displacement=Vector(1,1);
77         do_displacement=false;
78         super_sample=false;
79 }
80
81
82
83 inline Color
84 Noise::color_func(const Point &point, float pixel_size,Context /*context*/)const
85 {
86         Color ret(0,0,0,0);
87
88         float x(point[0]/size[0]*(1<<detail));
89         float y(point[1]/size[1]*(1<<detail));
90         float x2(0),y2(0);
91
92         if(super_sample&&pixel_size)
93         {
94                 x2=(point[0]+pixel_size)/size[0]*(1<<detail);
95                 y2=(point[1]+pixel_size)/size[1]*(1<<detail);
96         }
97
98         int i;
99         Time time;
100         time=speed*curr_time;
101         RandomNoise::SmoothType smooth((!speed && Noise::smooth == RandomNoise::SMOOTH_SPLINE) ? RandomNoise::SMOOTH_FAST_SPLINE : Noise::smooth);
102
103         float ftime(time);
104
105         {
106                 float amount=0.0f;
107                 float amount2=0.0f;
108                 float amount3=0.0f;
109                 float alpha=0.0f;
110                 for(i=0;i<detail;i++)
111                 {
112                         amount=random(smooth,0+(detail-i)*5,x,y,ftime)+amount*0.5;
113                         if(amount<-1)amount=-1;if(amount>1)amount=1;
114
115                         if(super_sample&&pixel_size)
116                         {
117                                 amount2=random(smooth,0+(detail-i)*5,x2,y,ftime)+amount2*0.5;
118                                 if(amount2<-1)amount2=-1;if(amount2>1)amount2=1;
119
120                                 amount3=random(smooth,0+(detail-i)*5,x,y2,ftime)+amount3*0.5;
121                                 if(amount3<-1)amount3=-1;if(amount3>1)amount3=1;
122
123                                 if(turbulent)
124                                 {
125                                         amount2=abs(amount2);
126                                         amount3=abs(amount3);
127                                 }
128
129                                 x2*=0.5f;
130                                 y2*=0.5f;
131                         }
132
133                         if(do_alpha)
134                         {
135                                 alpha=random(smooth,3+(detail-i)*5,x,y,ftime)+alpha*0.5;
136                                 if(alpha<-1)alpha=-1;if(alpha>1)alpha=1;
137                         }
138
139                         if(turbulent)
140                         {
141                                 amount=abs(amount);
142                                 alpha=abs(alpha);
143                         }
144
145                         x*=0.5f;
146                         y*=0.5f;
147                         //ftime*=0.5f;
148                 }
149
150                 if(!turbulent)
151                 {
152                         amount=amount/2.0f+0.5f;
153                         alpha=alpha/2.0f+0.5f;
154
155                         if(super_sample&&pixel_size)
156                         {
157                                 amount2=amount2/2.0f+0.5f;
158                                 amount3=amount3/2.0f+0.5f;
159                         }
160                 }
161
162                 if(super_sample && pixel_size)
163                         ret=gradient(amount,max(amount3,max(amount,amount2))-min(amount3,min(amount,amount2)));
164                 else
165                         ret=gradient(amount);
166
167                 if(do_alpha)
168                         ret.set_a(ret.get_a()*(alpha));
169         }
170         return ret;
171 }
172
173 inline float
174 Noise::calc_supersample(const synfig::Point &/*x*/, float /*pw*/,float /*ph*/)const
175 {
176         return 0.0f;
177 }
178
179 void
180 Noise::set_time(synfig::Context context, synfig::Time t)const
181 {
182         curr_time=t;
183         context.set_time(t);
184 }
185
186 void
187 Noise::set_time(synfig::Context context, synfig::Time t, const synfig::Point &point)const
188 {
189         curr_time=t;
190         context.set_time(t,point);
191 }
192
193 synfig::Layer::Handle
194 Noise::hit_check(synfig::Context context, const synfig::Point &point)const
195 {
196         if(get_blend_method()==Color::BLEND_STRAIGHT && get_amount()>=0.5)
197                 return const_cast<Noise*>(this);
198         if(get_amount()==0.0)
199                 return context.hit_check(point);
200         if(color_func(point,0,context).get_a()>0.5)
201                 return const_cast<Noise*>(this);
202         return false;
203 }
204
205 bool
206 Noise::set_param(const String & param, const ValueBase &value)
207 {
208         if(param=="seed" && value.same_type_as(int()))
209         {
210                 random.set_seed(value.get(int()));
211                 return true;
212         }
213         IMPORT(size);
214         IMPORT(speed);
215         IMPORT(smooth);
216         IMPORT(detail);
217         IMPORT(do_alpha);
218         IMPORT(gradient);
219         IMPORT(turbulent);
220         IMPORT(super_sample);
221
222         return Layer_Composite::set_param(param,value);
223 }
224
225 ValueBase
226 Noise::get_param(const String & param)const
227 {
228         if(param=="seed")
229                 return random.get_seed();
230         EXPORT(size);
231         EXPORT(speed);
232         EXPORT(smooth);
233         EXPORT(detail);
234         EXPORT(do_alpha);
235         EXPORT(gradient);
236         EXPORT(turbulent)
237         EXPORT(super_sample);
238
239         EXPORT_NAME();
240         EXPORT_VERSION();
241
242         return Layer_Composite::get_param(param);
243 }
244
245 Layer::Vocab
246 Noise::get_param_vocab()const
247 {
248         Layer::Vocab ret(Layer_Composite::get_param_vocab());
249
250         ret.push_back(ParamDesc("gradient")
251                 .set_local_name(_("Gradient"))
252         );
253         ret.push_back(ParamDesc("seed")
254                 .set_local_name(_("RandomNoise Seed"))
255         );
256         ret.push_back(ParamDesc("size")
257                 .set_local_name(_("Size"))
258         );
259         ret.push_back(ParamDesc("smooth")
260                 .set_local_name(_("Interpolation"))
261                 .set_description(_("What type of interpolation to use"))
262                 .set_hint("enum")
263                 .add_enum_value(RandomNoise::SMOOTH_DEFAULT,    "nearest",      _("Nearest Neighbor"))
264                 .add_enum_value(RandomNoise::SMOOTH_LINEAR,     "linear",       _("Linear"))
265                 .add_enum_value(RandomNoise::SMOOTH_COSINE,     "cosine",       _("Cosine"))
266                 .add_enum_value(RandomNoise::SMOOTH_SPLINE,     "spline",       _("Spline"))
267                 .add_enum_value(RandomNoise::SMOOTH_CUBIC,      "cubic",        _("Cubic"))
268         );
269         ret.push_back(ParamDesc("detail")
270                 .set_local_name(_("Detail"))
271         );
272         ret.push_back(ParamDesc("speed")
273                 .set_local_name(_("Animation Speed"))
274         );
275         ret.push_back(ParamDesc("turbulent")
276                 .set_local_name(_("Turbulent"))
277         );
278         ret.push_back(ParamDesc("do_alpha")
279                 .set_local_name(_("Do Alpha"))
280         );
281         ret.push_back(ParamDesc("super_sample")
282                 .set_local_name(_("Super Sampling"))
283         );
284
285         return ret;
286 }
287
288 Color
289 Noise::get_color(Context context, const Point &point)const
290 {
291         const Color color(color_func(point,0,context));
292
293         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
294                 return color;
295         else
296                 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
297 }
298
299 bool
300 Noise::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
301 {
302         SuperCallback supercb(cb,0,9500,10000);
303
304         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
305         {
306                 surface->set_wh(renddesc.get_w(),renddesc.get_h());
307         }
308         else
309         {
310                 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
311                         return false;
312                 if(get_amount()==0)
313                         return true;
314         }
315
316
317         int x,y;
318
319         Surface::pen pen(surface->begin());
320         const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
321         Point pos;
322         Point tl(renddesc.get_tl());
323         const int w(surface->get_w());
324         const int h(surface->get_h());
325         float supersampleradius((abs(pw)+abs(ph))*0.5f);
326         if(quality>=8)
327                 supersampleradius=0;
328
329         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
330         {
331                 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
332                         for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
333                                 pen.put_value(color_func(pos,supersampleradius,context));
334         }
335         else
336         {
337                 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
338                         for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
339                                 pen.put_value(Color::blend(color_func(pos,supersampleradius,context),pen.get_value(),get_amount(),get_blend_method()));
340         }
341
342         // Mark our progress as finished
343         if(cb && !cb->amount_complete(10000,10000))
344                 return false;
345
346         return true;
347 }