1 /* === S Y N F I G ========================================================= */
3 ** \brief Implementation of the "Noise Gradient" layer
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007 Chris Moore
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.
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.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
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>
46 /* === M A C R O S ========================================================= */
48 using namespace synfig;
52 /* === G L O B A L S ======================================================= */
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$");
61 /* === P R O C E D U R E S ================================================= */
63 /* === M E T H O D S ======================================================= */
67 gradient(Color::black(), Color::white())
69 smooth=Random::SMOOTH_COSINE;
73 random.set_seed(time(NULL));
75 displacement=Vector(1,1);
76 do_displacement=false;
83 Noise::color_func(const Point &point, float pixel_size,Context /*context*/)const
87 float x(point[0]/size[0]*(1<<detail));
88 float y(point[1]/size[1]*(1<<detail));
91 if(super_sample&&pixel_size)
93 x2=(point[0]+pixel_size)/size[0]*(1<<detail);
94 y2=(point[1]+pixel_size)/size[1]*(1<<detail);
100 Random::SmoothType smooth((!speed && Noise::smooth == Random::SMOOTH_SPLINE) ? Random::SMOOTH_FAST_SPLINE : Noise::smooth);
109 for(i=0;i<detail;i++)
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;
114 if(super_sample&&pixel_size)
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;
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;
124 amount2=abs(amount2);
125 amount3=abs(amount3);
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;
151 amount=amount/2.0f+0.5f;
152 alpha=alpha/2.0f+0.5f;
154 if(super_sample&&pixel_size)
156 amount2=amount2/2.0f+0.5f;
157 amount3=amount3/2.0f+0.5f;
161 if(super_sample && pixel_size)
162 ret=gradient(amount,max(amount3,max(amount,amount2))-min(amount3,min(amount,amount2)));
164 ret=gradient(amount);
167 ret.set_a(ret.get_a()*(alpha));
173 Noise::calc_supersample(const synfig::Point &/*x*/, float /*pw*/,float /*ph*/)const
179 Noise::set_time(synfig::Context context, synfig::Time t)const
186 Noise::set_time(synfig::Context context, synfig::Time t, const synfig::Point &point)const
189 context.set_time(t,point);
192 synfig::Layer::Handle
193 Noise::hit_check(synfig::Context context, const synfig::Point &point)const
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);
205 Noise::set_param(const String & param, const ValueBase &value)
207 if(param=="seed" && value.same_type_as(int()))
209 random.set_seed(value.get(int()));
219 IMPORT(super_sample);
221 return Layer_Composite::set_param(param,value);
225 Noise::get_param(const String & param)const
228 return random.get_seed();
236 EXPORT(super_sample);
241 return Layer_Composite::get_param(param);
245 Noise::get_param_vocab()const
247 Layer::Vocab ret(Layer_Composite::get_param_vocab());
249 ret.push_back(ParamDesc("gradient")
250 .set_local_name(_("Gradient"))
252 ret.push_back(ParamDesc("seed")
253 .set_local_name(_("Random Seed"))
255 ret.push_back(ParamDesc("size")
256 .set_local_name(_("Size"))
258 ret.push_back(ParamDesc("smooth")
259 .set_local_name(_("Interpolation"))
260 .set_description(_("What type of interpolation to use"))
262 .add_enum_value(Random::SMOOTH_DEFAULT, "nearest", _("Nearest Neighbor"))
263 .add_enum_value(Random::SMOOTH_LINEAR, "linear", _("Linear"))
264 .add_enum_value(Random::SMOOTH_COSINE, "cosine", _("Cosine"))
265 .add_enum_value(Random::SMOOTH_SPLINE, "spline", _("Spline"))
266 .add_enum_value(Random::SMOOTH_CUBIC, "cubic", _("Cubic"))
268 ret.push_back(ParamDesc("detail")
269 .set_local_name(_("Detail"))
271 ret.push_back(ParamDesc("speed")
272 .set_local_name(_("Animation Speed"))
274 ret.push_back(ParamDesc("turbulent")
275 .set_local_name(_("Turbulent"))
277 ret.push_back(ParamDesc("do_alpha")
278 .set_local_name(_("Do Alpha"))
280 ret.push_back(ParamDesc("super_sample")
281 .set_local_name(_("Super Sampling"))
288 Noise::get_color(Context context, const Point &point)const
290 const Color color(color_func(point,0,context));
292 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
295 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
299 Noise::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
301 SuperCallback supercb(cb,0,9500,10000);
303 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
305 surface->set_wh(renddesc.get_w(),renddesc.get_h());
309 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
318 Surface::pen pen(surface->begin());
319 const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
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);
328 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
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));
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()));
341 // Mark our progress as finished
342 if(cb && !cb->amount_complete(10000,10000))