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;
79 Layer::Vocab voc(get_param_vocab());
80 Layer::fill_static(voc);
86 Noise::color_func(const Point &point, float pixel_size,Context /*context*/)const
90 float x(point[0]/size[0]*(1<<detail));
91 float y(point[1]/size[1]*(1<<detail));
94 if(super_sample&&pixel_size)
96 x2=(point[0]+pixel_size)/size[0]*(1<<detail);
97 y2=(point[1]+pixel_size)/size[1]*(1<<detail);
102 time=speed*curr_time;
103 RandomNoise::SmoothType smooth((!speed && Noise::smooth == RandomNoise::SMOOTH_SPLINE) ? RandomNoise::SMOOTH_FAST_SPLINE : Noise::smooth);
112 for(i=0;i<detail;i++)
114 amount=random(smooth,0+(detail-i)*5,x,y,ftime)+amount*0.5;
115 if(amount<-1)amount=-1;if(amount>1)amount=1;
117 if(super_sample&&pixel_size)
119 amount2=random(smooth,0+(detail-i)*5,x2,y,ftime)+amount2*0.5;
120 if(amount2<-1)amount2=-1;if(amount2>1)amount2=1;
122 amount3=random(smooth,0+(detail-i)*5,x,y2,ftime)+amount3*0.5;
123 if(amount3<-1)amount3=-1;if(amount3>1)amount3=1;
127 amount2=abs(amount2);
128 amount3=abs(amount3);
137 alpha=random(smooth,3+(detail-i)*5,x,y,ftime)+alpha*0.5;
138 if(alpha<-1)alpha=-1;if(alpha>1)alpha=1;
154 amount=amount/2.0f+0.5f;
155 alpha=alpha/2.0f+0.5f;
157 if(super_sample&&pixel_size)
159 amount2=amount2/2.0f+0.5f;
160 amount3=amount3/2.0f+0.5f;
164 if(super_sample && pixel_size)
165 ret=gradient(amount,max(amount3,max(amount,amount2))-min(amount3,min(amount,amount2)));
167 ret=gradient(amount);
170 ret.set_a(ret.get_a()*(alpha));
176 Noise::calc_supersample(const synfig::Point &/*x*/, float /*pw*/,float /*ph*/)const
182 Noise::set_time(synfig::Context context, synfig::Time t)const
189 Noise::set_time(synfig::Context context, synfig::Time t, const synfig::Point &point)const
192 context.set_time(t,point);
195 synfig::Layer::Handle
196 Noise::hit_check(synfig::Context context, const synfig::Point &point)const
198 if(get_blend_method()==Color::BLEND_STRAIGHT && get_amount()>=0.5)
199 return const_cast<Noise*>(this);
200 if(get_amount()==0.0)
201 return context.hit_check(point);
202 if(color_func(point,0,context).get_a()>0.5)
203 return const_cast<Noise*>(this);
208 Noise::set_param(const String & param, const ValueBase &value)
210 if(param=="seed" && value.same_type_as(int()))
212 random.set_seed(value.get(int()));
213 set_param_static(param, value.get_static());
223 IMPORT(super_sample);
225 return Layer_Composite::set_param(param,value);
229 Noise::get_param(const String & param)const
233 ValueBase ret(random.get_seed());
234 ret.set_static(get_param_static(param));
244 EXPORT(super_sample);
249 return Layer_Composite::get_param(param);
253 Noise::get_param_vocab()const
255 Layer::Vocab ret(Layer_Composite::get_param_vocab());
257 ret.push_back(ParamDesc("gradient")
258 .set_local_name(_("Gradient"))
260 ret.push_back(ParamDesc("seed")
261 .set_local_name(_("RandomNoise Seed"))
263 ret.push_back(ParamDesc("size")
264 .set_local_name(_("Size"))
266 ret.push_back(ParamDesc("smooth")
267 .set_local_name(_("Interpolation"))
268 .set_description(_("What type of interpolation to use"))
270 .add_enum_value(RandomNoise::SMOOTH_DEFAULT, "nearest", _("Nearest Neighbor"))
271 .add_enum_value(RandomNoise::SMOOTH_LINEAR, "linear", _("Linear"))
272 .add_enum_value(RandomNoise::SMOOTH_COSINE, "cosine", _("Cosine"))
273 .add_enum_value(RandomNoise::SMOOTH_SPLINE, "spline", _("Spline"))
274 .add_enum_value(RandomNoise::SMOOTH_CUBIC, "cubic", _("Cubic"))
276 ret.push_back(ParamDesc("detail")
277 .set_local_name(_("Detail"))
279 ret.push_back(ParamDesc("speed")
280 .set_local_name(_("Animation Speed"))
282 ret.push_back(ParamDesc("turbulent")
283 .set_local_name(_("Turbulent"))
285 ret.push_back(ParamDesc("do_alpha")
286 .set_local_name(_("Do Alpha"))
288 ret.push_back(ParamDesc("super_sample")
289 .set_local_name(_("Super Sampling"))
296 Noise::get_color(Context context, const Point &point)const
298 const Color color(color_func(point,0,context));
300 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
303 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
307 Noise::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
309 SuperCallback supercb(cb,0,9500,10000);
311 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
313 surface->set_wh(renddesc.get_w(),renddesc.get_h());
317 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
326 Surface::pen pen(surface->begin());
327 const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
329 Point tl(renddesc.get_tl());
330 const int w(surface->get_w());
331 const int h(surface->get_h());
332 float supersampleradius((abs(pw)+abs(ph))*0.5f);
336 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
338 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
339 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
340 pen.put_value(color_func(pos,supersampleradius,context));
344 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
345 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
346 pen.put_value(Color::blend(color_func(pos,supersampleradius,context),pen.get_value(),get_amount(),get_blend_method()));
349 // Mark our progress as finished
350 if(cb && !cb->amount_complete(10000,10000))