1 /* === S Y N F I G ========================================================= */
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
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.
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.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
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>
45 /* === M A C R O S ========================================================= */
47 using namespace synfig;
51 /* === G L O B A L S ======================================================= */
53 SYNFIG_LAYER_INIT(Noise);
54 SYNFIG_LAYER_SET_NAME(Noise,"noise");
55 SYNFIG_LAYER_SET_LOCAL_NAME(Noise,N_("Noise Gradient"));
56 SYNFIG_LAYER_SET_CATEGORY(Noise,N_("Gradients"));
57 SYNFIG_LAYER_SET_VERSION(Noise,"0.0");
58 SYNFIG_LAYER_SET_CVS_ID(Noise,"$Id$");
60 /* === P R O C E D U R E S ================================================= */
62 /* === M E T H O D S ======================================================= */
66 gradient(Color::black(), Color::white())
68 smooth=Random::SMOOTH_COSINE;
72 random.set_seed(time(NULL));
74 displacement=Vector(1,1);
75 do_displacement=false;
82 Noise::color_func(const Point &point, float pixel_size,Context /*context*/)const
86 float x(point[0]/size[0]*(1<<detail));
87 float y(point[1]/size[1]*(1<<detail));
90 if(super_sample&&pixel_size)
92 x2=(point[0]+pixel_size)/size[0]*(1<<detail);
93 y2=(point[1]+pixel_size)/size[1]*(1<<detail);
99 Random::SmoothType smooth((!speed && Noise::smooth == Random::SMOOTH_SPLINE) ? Random::SMOOTH_FAST_SPLINE : Noise::smooth);
108 for(i=0;i<detail;i++)
110 amount=random(smooth,0+(detail-i)*5,x,y,ftime)+amount*0.5;
111 if(amount<-1)amount=-1;if(amount>1)amount=1;
113 if(super_sample&&pixel_size)
115 amount2=random(smooth,0+(detail-i)*5,x2,y,ftime)+amount2*0.5;
116 if(amount2<-1)amount2=-1;if(amount2>1)amount2=1;
118 amount3=random(smooth,0+(detail-i)*5,x,y2,ftime)+amount3*0.5;
119 if(amount3<-1)amount3=-1;if(amount3>1)amount3=1;
123 amount2=abs(amount2);
124 amount3=abs(amount3);
133 alpha=random(smooth,3+(detail-i)*5,x,y,ftime)+alpha*0.5;
134 if(alpha<-1)alpha=-1;if(alpha>1)alpha=1;
150 amount=amount/2.0f+0.5f;
151 alpha=alpha/2.0f+0.5f;
153 if(super_sample&&pixel_size)
155 amount2=amount2/2.0f+0.5f;
156 amount3=amount3/2.0f+0.5f;
160 if(super_sample && pixel_size)
161 ret=gradient(amount,max(amount3,max(amount,amount2))-min(amount3,min(amount,amount2)));
163 ret=gradient(amount);
166 ret.set_a(ret.get_a()*(alpha));
172 Noise::calc_supersample(const synfig::Point &/*x*/, float /*pw*/,float /*ph*/)const
178 Noise::set_time(synfig::Context context, synfig::Time t)const
185 Noise::set_time(synfig::Context context, synfig::Time t, const synfig::Point &point)const
188 context.set_time(t,point);
191 synfig::Layer::Handle
192 Noise::hit_check(synfig::Context context, const synfig::Point &point)const
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);
204 Noise::set_param(const String & param, const ValueBase &value)
206 if(param=="seed" && value.same_type_as(int()))
208 random.set_seed(value.get(int()));
218 IMPORT(super_sample);
220 return Layer_Composite::set_param(param,value);
224 Noise::get_param(const String & param)const
227 return random.get_seed();
235 EXPORT(super_sample);
240 return Layer_Composite::get_param(param);
244 Noise::get_param_vocab()const
246 Layer::Vocab ret(Layer_Composite::get_param_vocab());
248 ret.push_back(ParamDesc("gradient")
249 .set_local_name(_("Gradient"))
251 ret.push_back(ParamDesc("seed")
252 .set_local_name(_("Random Seed"))
254 ret.push_back(ParamDesc("size")
255 .set_local_name(_("Size"))
257 ret.push_back(ParamDesc("smooth")
258 .set_local_name(_("Interpolation"))
259 .set_description(_("What type of interpolation to use"))
261 .add_enum_value(Random::SMOOTH_DEFAULT, "nearest", _("Nearest Neighbor"))
262 .add_enum_value(Random::SMOOTH_LINEAR, "linear", _("Linear"))
263 .add_enum_value(Random::SMOOTH_COSINE, "cosine", _("Cosine"))
264 .add_enum_value(Random::SMOOTH_SPLINE, "spline", _("Spline"))
265 .add_enum_value(Random::SMOOTH_CUBIC, "cubic", _("Cubic"))
267 ret.push_back(ParamDesc("detail")
268 .set_local_name(_("Detail"))
270 ret.push_back(ParamDesc("speed")
271 .set_local_name(_("Animation Speed"))
273 ret.push_back(ParamDesc("turbulent")
274 .set_local_name(_("Turbulent"))
276 ret.push_back(ParamDesc("do_alpha")
277 .set_local_name(_("Do Alpha"))
279 ret.push_back(ParamDesc("super_sample")
280 .set_local_name(_("Super Sampling"))
287 Noise::get_color(Context context, const Point &point)const
289 const Color color(color_func(point,0,context));
291 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
294 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
298 Noise::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
300 SuperCallback supercb(cb,0,9500,10000);
302 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
304 surface->set_wh(renddesc.get_w(),renddesc.get_h());
308 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
317 Surface::pen pen(surface->begin());
318 const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
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);
327 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
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));
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()));
340 // Mark our progress as finished
341 if(cb && !cb->amount_complete(10000,10000))