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