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 ======================================================= */
66 Layer_Composite(1.0,Color::BLEND_COMPOSITE),
68 gradient(Color::black(), Color::white())
70 smooth=RandomNoise::SMOOTH_COSINE;
74 random.set_seed(time(NULL));
76 displacement=Vector(1,1);
77 do_displacement=false;
84 Noise::color_func(const Point &point, float pixel_size,Context /*context*/)const
88 float x(point[0]/size[0]*(1<<detail));
89 float y(point[1]/size[1]*(1<<detail));
92 if(super_sample&&pixel_size)
94 x2=(point[0]+pixel_size)/size[0]*(1<<detail);
95 y2=(point[1]+pixel_size)/size[1]*(1<<detail);
100 time=speed*curr_time;
101 RandomNoise::SmoothType smooth((!speed && Noise::smooth == RandomNoise::SMOOTH_SPLINE) ? RandomNoise::SMOOTH_FAST_SPLINE : Noise::smooth);
110 for(i=0;i<detail;i++)
112 amount=random(smooth,0+(detail-i)*5,x,y,ftime)+amount*0.5;
113 if(amount<-1)amount=-1;if(amount>1)amount=1;
115 if(super_sample&&pixel_size)
117 amount2=random(smooth,0+(detail-i)*5,x2,y,ftime)+amount2*0.5;
118 if(amount2<-1)amount2=-1;if(amount2>1)amount2=1;
120 amount3=random(smooth,0+(detail-i)*5,x,y2,ftime)+amount3*0.5;
121 if(amount3<-1)amount3=-1;if(amount3>1)amount3=1;
125 amount2=abs(amount2);
126 amount3=abs(amount3);
135 alpha=random(smooth,3+(detail-i)*5,x,y,ftime)+alpha*0.5;
136 if(alpha<-1)alpha=-1;if(alpha>1)alpha=1;
152 amount=amount/2.0f+0.5f;
153 alpha=alpha/2.0f+0.5f;
155 if(super_sample&&pixel_size)
157 amount2=amount2/2.0f+0.5f;
158 amount3=amount3/2.0f+0.5f;
162 if(super_sample && pixel_size)
163 ret=gradient(amount,max(amount3,max(amount,amount2))-min(amount3,min(amount,amount2)));
165 ret=gradient(amount);
168 ret.set_a(ret.get_a()*(alpha));
174 Noise::calc_supersample(const synfig::Point &/*x*/, float /*pw*/,float /*ph*/)const
180 Noise::set_time(synfig::Context context, synfig::Time t)const
187 Noise::set_time(synfig::Context context, synfig::Time t, const synfig::Point &point)const
190 context.set_time(t,point);
193 synfig::Layer::Handle
194 Noise::hit_check(synfig::Context context, const synfig::Point &point)const
196 if(get_blend_method()==Color::BLEND_STRAIGHT && get_amount()>=0.5)
197 return const_cast<Noise*>(this);
198 if(get_amount()==0.0)
199 return context.hit_check(point);
200 if(color_func(point,0,context).get_a()>0.5)
201 return const_cast<Noise*>(this);
206 Noise::set_param(const String & param, const ValueBase &value)
208 if(param=="seed" && value.same_type_as(int()))
210 random.set_seed(value.get(int()));
220 IMPORT(super_sample);
222 return Layer_Composite::set_param(param,value);
226 Noise::get_param(const String & param)const
229 return random.get_seed();
237 EXPORT(super_sample);
242 return Layer_Composite::get_param(param);
246 Noise::get_param_vocab()const
248 Layer::Vocab ret(Layer_Composite::get_param_vocab());
250 ret.push_back(ParamDesc("gradient")
251 .set_local_name(_("Gradient"))
253 ret.push_back(ParamDesc("seed")
254 .set_local_name(_("RandomNoise Seed"))
256 ret.push_back(ParamDesc("size")
257 .set_local_name(_("Size"))
259 ret.push_back(ParamDesc("smooth")
260 .set_local_name(_("Interpolation"))
261 .set_description(_("What type of interpolation to use"))
263 .add_enum_value(RandomNoise::SMOOTH_DEFAULT, "nearest", _("Nearest Neighbor"))
264 .add_enum_value(RandomNoise::SMOOTH_LINEAR, "linear", _("Linear"))
265 .add_enum_value(RandomNoise::SMOOTH_COSINE, "cosine", _("Cosine"))
266 .add_enum_value(RandomNoise::SMOOTH_SPLINE, "spline", _("Spline"))
267 .add_enum_value(RandomNoise::SMOOTH_CUBIC, "cubic", _("Cubic"))
269 ret.push_back(ParamDesc("detail")
270 .set_local_name(_("Detail"))
272 ret.push_back(ParamDesc("speed")
273 .set_local_name(_("Animation Speed"))
275 ret.push_back(ParamDesc("turbulent")
276 .set_local_name(_("Turbulent"))
278 ret.push_back(ParamDesc("do_alpha")
279 .set_local_name(_("Do Alpha"))
281 ret.push_back(ParamDesc("super_sample")
282 .set_local_name(_("Super Sampling"))
289 Noise::get_color(Context context, const Point &point)const
291 const Color color(color_func(point,0,context));
293 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
296 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
300 Noise::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
302 SuperCallback supercb(cb,0,9500,10000);
304 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
306 surface->set_wh(renddesc.get_w(),renddesc.get_h());
310 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
319 Surface::pen pen(surface->begin());
320 const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
322 Point tl(renddesc.get_tl());
323 const int w(surface->get_w());
324 const int h(surface->get_h());
325 float supersampleradius((abs(pw)+abs(ph))*0.5f);
329 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
331 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
332 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
333 pen.put_value(color_func(pos,supersampleradius,context));
337 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
338 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
339 pen.put_value(Color::blend(color_func(pos,supersampleradius,context),pen.get_value(),get_amount(),get_blend_method()));
342 // Mark our progress as finished
343 if(cb && !cb->amount_complete(10000,10000))