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(NoiseDistort);
54 SYNFIG_LAYER_SET_NAME(NoiseDistort,"noise_distort");
55 SYNFIG_LAYER_SET_LOCAL_NAME(NoiseDistort,_("Noise Distort"));
56 SYNFIG_LAYER_SET_CATEGORY(NoiseDistort,_("Distortions"));
57 SYNFIG_LAYER_SET_VERSION(NoiseDistort,"0.0");
58 SYNFIG_LAYER_SET_CVS_ID(NoiseDistort,"$Id$");
60 /* === P R O C E D U R E S ================================================= */
62 /* === M E T H O D S ======================================================= */
64 NoiseDistort::NoiseDistort():
67 set_blend_method(Color::BLEND_STRAIGHT);
71 random.set_seed(time(NULL));
73 displacement=Vector(0.25,0.25);
77 NoiseDistort::color_func(const Point &point, float supersample,Context context)const
81 float x(point[0]/size[0]*(1<<detail));
82 float y(point[1]/size[1]*(1<<detail));
87 int smooth((!speed && smooth==3)?5:smooth);
93 vect[0]=random(smooth,0+(detail-i)*5,x,y,time)+vect[0]*0.5;
94 vect[1]=random(smooth,1+(detail-i)*5,x,y,time)+vect[1]*0.5;
96 if(vect[0]<-1)vect[0]=-1;if(vect[0]>1)vect[0]=1;
97 if(vect[1]<-1)vect[1]=-1;if(vect[1]>1)vect[1]=1;
101 vect[0]=abs(vect[0]);
102 vect[1]=abs(vect[1]);
111 vect[0]=vect[0]/2.0f+0.5f;
112 vect[1]=vect[1]/2.0f+0.5f;
114 vect[0]=(vect[0]-0.5f)*displacement[0];
115 vect[1]=(vect[1]-0.5f)*displacement[1];
117 ret=context.get_color(point+vect);
123 NoiseDistort::calc_supersample(const synfig::Point &x, float pw,float ph)const
129 NoiseDistort::set_time(synfig::Context context, synfig::Time t)const
136 NoiseDistort::set_time(synfig::Context context, synfig::Time t, const synfig::Point &point)const
139 context.set_time(t,point);
142 synfig::Layer::Handle
143 NoiseDistort::hit_check(synfig::Context context, const synfig::Point &point)const
145 if(get_blend_method()==Color::BLEND_STRAIGHT && get_amount()>=0.5)
146 return const_cast<NoiseDistort*>(this);
147 if(get_amount()==0.0)
148 return context.hit_check(point);
149 if(color_func(point,0,context).get_a()>0.5)
150 return const_cast<NoiseDistort*>(this);
155 NoiseDistort::set_param(const String & param, const ValueBase &value)
157 if(param=="seed" && value.same_as(int()))
159 random.set_seed(value.get(int()));
167 IMPORT(displacement);
168 return Layer_Composite::set_param(param,value);
172 NoiseDistort::get_param(const String & param)const
175 return random.get_seed();
180 EXPORT(displacement);
186 return Layer_Composite::get_param(param);
190 NoiseDistort::get_param_vocab()const
192 Layer::Vocab ret(Layer_Composite::get_param_vocab());
194 ret.push_back(ParamDesc("displacement")
195 .set_local_name(_("Displacement"))
198 ret.push_back(ParamDesc("size")
199 .set_local_name(_("Size"))
201 ret.push_back(ParamDesc("seed")
202 .set_local_name(_("Random Seed"))
204 ret.push_back(ParamDesc("smooth")
205 .set_local_name(_("Interpolation"))
206 .set_description(_("What type of interpolation to use"))
208 .add_enum_value(0,"nearest",_("Nearest Neighbor"))
209 .add_enum_value(1,"linear",_("Linear"))
210 .add_enum_value(2,"cosine",_("Cosine"))
211 .add_enum_value(3,"spline",_("Spline"))
212 .add_enum_value(4,"cubic",_("Cubic"))
214 ret.push_back(ParamDesc("detail")
215 .set_local_name(_("Detail"))
217 ret.push_back(ParamDesc("speed")
218 .set_local_name(_("Animation Speed"))
220 ret.push_back(ParamDesc("turbulent")
221 .set_local_name(_("Turbulent"))
228 NoiseDistort::get_color(Context context, const Point &point)const
230 const Color color(color_func(point,0,context));
232 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
235 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
239 NoiseDistort::get_bounding_rect(Context context)const
244 if(Color::is_onto(get_blend_method()))
245 return context.get_full_bounding_rect();
247 Rect bounds(context.get_full_bounding_rect().expand_x(displacement[0]).expand_y(displacement[1]));
254 NoiseDistort::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
256 SuperCallback supercb(cb,0,9500,10000);
258 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
260 surface->set_wh(renddesc.get_w(),renddesc.get_h());
264 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
273 Surface::pen pen(surface->begin());
274 const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
276 Point tl(renddesc.get_tl());
277 const int w(surface->get_w());
278 const int h(surface->get_h());
280 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
282 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
283 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
284 pen.put_value(color_func(pos,calc_supersample(pos,pw,ph),context));
288 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
289 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
290 pen.put_value(Color::blend(color_func(pos,calc_supersample(pos,pw,ph),context),pen.get_value(),get_amount(),get_blend_method()));
293 // Mark our progress as finished
294 if(cb && !cb->amount_complete(10000,10000))