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