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 temp_smooth(smooth);
88 int smooth((!speed && temp_smooth==3)?5:temp_smooth);
94 vect[0]=random(smooth,0+(detail-i)*5,x,y,time)+vect[0]*0.5;
95 vect[1]=random(smooth,1+(detail-i)*5,x,y,time)+vect[1]*0.5;
97 if(vect[0]<-1)vect[0]=-1;if(vect[0]>1)vect[0]=1;
98 if(vect[1]<-1)vect[1]=-1;if(vect[1]>1)vect[1]=1;
102 vect[0]=abs(vect[0]);
103 vect[1]=abs(vect[1]);
112 vect[0]=vect[0]/2.0f+0.5f;
113 vect[1]=vect[1]/2.0f+0.5f;
115 vect[0]=(vect[0]-0.5f)*displacement[0];
116 vect[1]=(vect[1]-0.5f)*displacement[1];
118 ret=context.get_color(point+vect);
124 NoiseDistort::calc_supersample(const synfig::Point &/*x*/, float /*pw*/,float /*ph*/)const
130 NoiseDistort::set_time(synfig::Context context, synfig::Time t)const
137 NoiseDistort::set_time(synfig::Context context, synfig::Time t, const synfig::Point &point)const
140 context.set_time(t,point);
143 synfig::Layer::Handle
144 NoiseDistort::hit_check(synfig::Context context, const synfig::Point &point)const
146 if(get_blend_method()==Color::BLEND_STRAIGHT && get_amount()>=0.5)
147 return const_cast<NoiseDistort*>(this);
148 if(get_amount()==0.0)
149 return context.hit_check(point);
150 if(color_func(point,0,context).get_a()>0.5)
151 return const_cast<NoiseDistort*>(this);
156 NoiseDistort::set_param(const String & param, const ValueBase &value)
158 if(param=="seed" && value.same_type_as(int()))
160 random.set_seed(value.get(int()));
168 IMPORT(displacement);
169 return Layer_Composite::set_param(param,value);
173 NoiseDistort::get_param(const String & param)const
176 return random.get_seed();
181 EXPORT(displacement);
187 return Layer_Composite::get_param(param);
191 NoiseDistort::get_param_vocab()const
193 Layer::Vocab ret(Layer_Composite::get_param_vocab());
195 ret.push_back(ParamDesc("displacement")
196 .set_local_name(_("Displacement"))
199 ret.push_back(ParamDesc("size")
200 .set_local_name(_("Size"))
202 ret.push_back(ParamDesc("seed")
203 .set_local_name(_("Random Seed"))
205 ret.push_back(ParamDesc("smooth")
206 .set_local_name(_("Interpolation"))
207 .set_description(_("What type of interpolation to use"))
209 .add_enum_value(0,"nearest",_("Nearest Neighbor"))
210 .add_enum_value(1,"linear",_("Linear"))
211 .add_enum_value(2,"cosine",_("Cosine"))
212 .add_enum_value(3,"spline",_("Spline"))
213 .add_enum_value(4,"cubic",_("Cubic"))
215 ret.push_back(ParamDesc("detail")
216 .set_local_name(_("Detail"))
218 ret.push_back(ParamDesc("speed")
219 .set_local_name(_("Animation Speed"))
221 ret.push_back(ParamDesc("turbulent")
222 .set_local_name(_("Turbulent"))
229 NoiseDistort::get_color(Context context, const Point &point)const
231 const Color color(color_func(point,0,context));
233 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
236 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
240 NoiseDistort::get_bounding_rect(Context context)const
245 if(Color::is_onto(get_blend_method()))
246 return context.get_full_bounding_rect();
248 Rect bounds(context.get_full_bounding_rect().expand_x(displacement[0]).expand_y(displacement[1]));
255 NoiseDistort::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
257 SuperCallback supercb(cb,0,9500,10000);
259 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
261 surface->set_wh(renddesc.get_w(),renddesc.get_h());
265 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
274 Surface::pen pen(surface->begin());
275 const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
277 Point tl(renddesc.get_tl());
278 const int w(surface->get_w());
279 const int h(surface->get_h());
281 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
283 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
284 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
285 pen.put_value(color_func(pos,calc_supersample(pos,pw,ph),context));
289 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
290 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
291 pen.put_value(Color::blend(color_func(pos,calc_supersample(pos,pw,ph),context),pen.get_value(),get_amount(),get_blend_method()));
294 // Mark our progress as finished
295 if(cb && !cb->amount_complete(10000,10000))