1 /* === S Y N F I G ========================================================= */
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(NoiseDistort);
55 SYNFIG_LAYER_SET_NAME(NoiseDistort,"noise_distort");
56 SYNFIG_LAYER_SET_LOCAL_NAME(NoiseDistort,_("Noise Distort"));
57 SYNFIG_LAYER_SET_CATEGORY(NoiseDistort,_("Distortions"));
58 SYNFIG_LAYER_SET_VERSION(NoiseDistort,"0.0");
59 SYNFIG_LAYER_SET_CVS_ID(NoiseDistort,"$Id$");
61 /* === P R O C E D U R E S ================================================= */
63 /* === M E T H O D S ======================================================= */
65 NoiseDistort::NoiseDistort():
68 set_blend_method(Color::BLEND_STRAIGHT);
69 smooth=Random::SMOOTH_COSINE;
72 random.set_seed(time(NULL));
74 displacement=Vector(0.25,0.25);
78 NoiseDistort::color_func(const Point &point, float /*supersample*/,Context context)const
82 float x(point[0]/size[0]*(1<<detail));
83 float y(point[1]/size[1]*(1<<detail));
88 Random::SmoothType temp_smooth(smooth);
89 Random::SmoothType smooth((!speed && temp_smooth == Random::SMOOTH_SPLINE) ? Random::SMOOTH_FAST_SPLINE : temp_smooth);
95 vect[0]=random(smooth,0+(detail-i)*5,x,y,time)+vect[0]*0.5;
96 vect[1]=random(smooth,1+(detail-i)*5,x,y,time)+vect[1]*0.5;
98 if(vect[0]<-1)vect[0]=-1;if(vect[0]>1)vect[0]=1;
99 if(vect[1]<-1)vect[1]=-1;if(vect[1]>1)vect[1]=1;
103 vect[0]=abs(vect[0]);
104 vect[1]=abs(vect[1]);
113 vect[0]=vect[0]/2.0f+0.5f;
114 vect[1]=vect[1]/2.0f+0.5f;
116 vect[0]=(vect[0]-0.5f)*displacement[0];
117 vect[1]=(vect[1]-0.5f)*displacement[1];
119 ret=context.get_color(point+vect);
125 NoiseDistort::calc_supersample(const synfig::Point &/*x*/, float /*pw*/,float /*ph*/)const
131 NoiseDistort::set_time(synfig::Context context, synfig::Time t)const
138 NoiseDistort::set_time(synfig::Context context, synfig::Time t, const synfig::Point &point)const
141 context.set_time(t,point);
144 synfig::Layer::Handle
145 NoiseDistort::hit_check(synfig::Context context, const synfig::Point &point)const
147 if(get_blend_method()==Color::BLEND_STRAIGHT && get_amount()>=0.5)
148 return const_cast<NoiseDistort*>(this);
149 if(get_amount()==0.0)
150 return context.hit_check(point);
151 if(color_func(point,0,context).get_a()>0.5)
152 return const_cast<NoiseDistort*>(this);
157 NoiseDistort::set_param(const String & param, const ValueBase &value)
159 if(param=="seed" && value.same_type_as(int()))
161 random.set_seed(value.get(int()));
169 IMPORT(displacement);
170 return Layer_Composite::set_param(param,value);
174 NoiseDistort::get_param(const String & param)const
177 return random.get_seed();
182 EXPORT(displacement);
188 return Layer_Composite::get_param(param);
192 NoiseDistort::get_param_vocab()const
194 Layer::Vocab ret(Layer_Composite::get_param_vocab());
196 ret.push_back(ParamDesc("displacement")
197 .set_local_name(_("Displacement"))
200 ret.push_back(ParamDesc("size")
201 .set_local_name(_("Size"))
203 ret.push_back(ParamDesc("seed")
204 .set_local_name(_("Random Seed"))
206 ret.push_back(ParamDesc("smooth")
207 .set_local_name(_("Interpolation"))
208 .set_description(_("What type of interpolation to use"))
210 .add_enum_value(Random::SMOOTH_DEFAULT, "nearest", _("Nearest Neighbor"))
211 .add_enum_value(Random::SMOOTH_LINEAR, "linear", _("Linear"))
212 .add_enum_value(Random::SMOOTH_COSINE, "cosine", _("Cosine"))
213 .add_enum_value(Random::SMOOTH_SPLINE, "spline", _("Spline"))
214 .add_enum_value(Random::SMOOTH_CUBIC, "cubic", _("Cubic"))
216 ret.push_back(ParamDesc("detail")
217 .set_local_name(_("Detail"))
219 ret.push_back(ParamDesc("speed")
220 .set_local_name(_("Animation Speed"))
222 ret.push_back(ParamDesc("turbulent")
223 .set_local_name(_("Turbulent"))
230 NoiseDistort::get_color(Context context, const Point &point)const
232 const Color color(color_func(point,0,context));
234 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
237 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
241 NoiseDistort::get_bounding_rect(Context context)const
246 if(Color::is_onto(get_blend_method()))
247 return context.get_full_bounding_rect();
249 Rect bounds(context.get_full_bounding_rect().expand_x(displacement[0]).expand_y(displacement[1]));
256 NoiseDistort::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
258 SuperCallback supercb(cb,0,9500,10000);
260 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
262 surface->set_wh(renddesc.get_w(),renddesc.get_h());
266 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
275 Surface::pen pen(surface->begin());
276 const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
278 Point tl(renddesc.get_tl());
279 const int w(surface->get_w());
280 const int h(surface->get_h());
282 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
284 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
285 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
286 pen.put_value(color_func(pos,calc_supersample(pos,pw,ph),context));
290 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
291 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
292 pen.put_value(Color::blend(color_func(pos,calc_supersample(pos,pw,ph),context),pen.get_value(),get_amount(),get_blend_method()));
295 // Mark our progress as finished
296 if(cb && !cb->amount_complete(10000,10000))