e4d5c3b21a0e040e5c851e4a2076dc70ded63111
[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         Layer::Vocab voc(get_param_vocab());
80         Layer::fill_static(voc);
81 }
82
83
84
85 inline Color
86 Noise::color_func(const Point &point, float pixel_size,Context /*context*/)const
87 {
88         Color ret(0,0,0,0);
89
90         float x(point[0]/size[0]*(1<<detail));
91         float y(point[1]/size[1]*(1<<detail));
92         float x2(0),y2(0);
93
94         if(super_sample&&pixel_size)
95         {
96                 x2=(point[0]+pixel_size)/size[0]*(1<<detail);
97                 y2=(point[1]+pixel_size)/size[1]*(1<<detail);
98         }
99
100         int i;
101         Time time;
102         time=speed*curr_time;
103         RandomNoise::SmoothType smooth((!speed && Noise::smooth == RandomNoise::SMOOTH_SPLINE) ? RandomNoise::SMOOTH_FAST_SPLINE : Noise::smooth);
104
105         float ftime(time);
106
107         {
108                 float amount=0.0f;
109                 float amount2=0.0f;
110                 float amount3=0.0f;
111                 float alpha=0.0f;
112                 for(i=0;i<detail;i++)
113                 {
114                         amount=random(smooth,0+(detail-i)*5,x,y,ftime)+amount*0.5;
115                         if(amount<-1)amount=-1;if(amount>1)amount=1;
116
117                         if(super_sample&&pixel_size)
118                         {
119                                 amount2=random(smooth,0+(detail-i)*5,x2,y,ftime)+amount2*0.5;
120                                 if(amount2<-1)amount2=-1;if(amount2>1)amount2=1;
121
122                                 amount3=random(smooth,0+(detail-i)*5,x,y2,ftime)+amount3*0.5;
123                                 if(amount3<-1)amount3=-1;if(amount3>1)amount3=1;
124
125                                 if(turbulent)
126                                 {
127                                         amount2=abs(amount2);
128                                         amount3=abs(amount3);
129                                 }
130
131                                 x2*=0.5f;
132                                 y2*=0.5f;
133                         }
134
135                         if(do_alpha)
136                         {
137                                 alpha=random(smooth,3+(detail-i)*5,x,y,ftime)+alpha*0.5;
138                                 if(alpha<-1)alpha=-1;if(alpha>1)alpha=1;
139                         }
140
141                         if(turbulent)
142                         {
143                                 amount=abs(amount);
144                                 alpha=abs(alpha);
145                         }
146
147                         x*=0.5f;
148                         y*=0.5f;
149                         //ftime*=0.5f;
150                 }
151
152                 if(!turbulent)
153                 {
154                         amount=amount/2.0f+0.5f;
155                         alpha=alpha/2.0f+0.5f;
156
157                         if(super_sample&&pixel_size)
158                         {
159                                 amount2=amount2/2.0f+0.5f;
160                                 amount3=amount3/2.0f+0.5f;
161                         }
162                 }
163
164                 if(super_sample && pixel_size)
165                         ret=gradient(amount,max(amount3,max(amount,amount2))-min(amount3,min(amount,amount2)));
166                 else
167                         ret=gradient(amount);
168
169                 if(do_alpha)
170                         ret.set_a(ret.get_a()*(alpha));
171         }
172         return ret;
173 }
174
175 inline float
176 Noise::calc_supersample(const synfig::Point &/*x*/, float /*pw*/,float /*ph*/)const
177 {
178         return 0.0f;
179 }
180
181 void
182 Noise::set_time(synfig::Context context, synfig::Time t)const
183 {
184         curr_time=t;
185         context.set_time(t);
186 }
187
188 void
189 Noise::set_time(synfig::Context context, synfig::Time t, const synfig::Point &point)const
190 {
191         curr_time=t;
192         context.set_time(t,point);
193 }
194
195 synfig::Layer::Handle
196 Noise::hit_check(synfig::Context context, const synfig::Point &point)const
197 {
198         if(get_blend_method()==Color::BLEND_STRAIGHT && get_amount()>=0.5)
199                 return const_cast<Noise*>(this);
200         if(get_amount()==0.0)
201                 return context.hit_check(point);
202         if(color_func(point,0,context).get_a()>0.5)
203                 return const_cast<Noise*>(this);
204         return false;
205 }
206
207 bool
208 Noise::set_param(const String & param, const ValueBase &value)
209 {
210         if(param=="seed" && value.same_type_as(int()))
211         {
212                 random.set_seed(value.get(int()));
213                 set_param_static(param, value.get_static());
214                 return true;
215         }
216         IMPORT(size);
217         IMPORT(speed);
218         IMPORT(smooth);
219         IMPORT(detail);
220         IMPORT(do_alpha);
221         IMPORT(gradient);
222         IMPORT(turbulent);
223         IMPORT(super_sample);
224
225         return Layer_Composite::set_param(param,value);
226 }
227
228 ValueBase
229 Noise::get_param(const String & param)const
230 {
231         if(param=="seed")
232         {
233                 ValueBase ret(random.get_seed());
234                 ret.set_static(get_param_static(param));
235                 return ret;
236         }
237         EXPORT(size);
238         EXPORT(speed);
239         EXPORT(smooth);
240         EXPORT(detail);
241         EXPORT(do_alpha);
242         EXPORT(gradient);
243         EXPORT(turbulent)
244         EXPORT(super_sample);
245
246         EXPORT_NAME();
247         EXPORT_VERSION();
248
249         return Layer_Composite::get_param(param);
250 }
251
252 Layer::Vocab
253 Noise::get_param_vocab()const
254 {
255         Layer::Vocab ret(Layer_Composite::get_param_vocab());
256
257         ret.push_back(ParamDesc("gradient")
258                 .set_local_name(_("Gradient"))
259         );
260         ret.push_back(ParamDesc("seed")
261                 .set_local_name(_("RandomNoise Seed"))
262         );
263         ret.push_back(ParamDesc("size")
264                 .set_local_name(_("Size"))
265         );
266         ret.push_back(ParamDesc("smooth")
267                 .set_local_name(_("Interpolation"))
268                 .set_description(_("What type of interpolation to use"))
269                 .set_hint("enum")
270                 .add_enum_value(RandomNoise::SMOOTH_DEFAULT,    "nearest",      _("Nearest Neighbor"))
271                 .add_enum_value(RandomNoise::SMOOTH_LINEAR,     "linear",       _("Linear"))
272                 .add_enum_value(RandomNoise::SMOOTH_COSINE,     "cosine",       _("Cosine"))
273                 .add_enum_value(RandomNoise::SMOOTH_SPLINE,     "spline",       _("Spline"))
274                 .add_enum_value(RandomNoise::SMOOTH_CUBIC,      "cubic",        _("Cubic"))
275         );
276         ret.push_back(ParamDesc("detail")
277                 .set_local_name(_("Detail"))
278         );
279         ret.push_back(ParamDesc("speed")
280                 .set_local_name(_("Animation Speed"))
281         );
282         ret.push_back(ParamDesc("turbulent")
283                 .set_local_name(_("Turbulent"))
284         );
285         ret.push_back(ParamDesc("do_alpha")
286                 .set_local_name(_("Do Alpha"))
287         );
288         ret.push_back(ParamDesc("super_sample")
289                 .set_local_name(_("Super Sampling"))
290         );
291
292         return ret;
293 }
294
295 Color
296 Noise::get_color(Context context, const Point &point)const
297 {
298         const Color color(color_func(point,0,context));
299
300         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
301                 return color;
302         else
303                 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
304 }
305
306 bool
307 Noise::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
308 {
309         SuperCallback supercb(cb,0,9500,10000);
310
311         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
312         {
313                 surface->set_wh(renddesc.get_w(),renddesc.get_h());
314         }
315         else
316         {
317                 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
318                         return false;
319                 if(get_amount()==0)
320                         return true;
321         }
322
323
324         int x,y;
325
326         Surface::pen pen(surface->begin());
327         const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
328         Point pos;
329         Point tl(renddesc.get_tl());
330         const int w(surface->get_w());
331         const int h(surface->get_h());
332         float supersampleradius((abs(pw)+abs(ph))*0.5f);
333         if(quality>=8)
334                 supersampleradius=0;
335
336         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
337         {
338                 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
339                         for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
340                                 pen.put_value(color_func(pos,supersampleradius,context));
341         }
342         else
343         {
344                 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
345                         for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
346                                 pen.put_value(Color::blend(color_func(pos,supersampleradius,context),pen.get_value(),get_amount(),get_blend_method()));
347         }
348
349         // Mark our progress as finished
350         if(cb && !cb->amount_complete(10000,10000))
351                 return false;
352
353         return true;
354 }