1 /* === S Y N F I G ========================================================= */
3 ** \brief Implementation of the "Noise Distort" 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(NoiseDistort);
55 SYNFIG_LAYER_SET_NAME(NoiseDistort,"noise_distort");
56 SYNFIG_LAYER_SET_LOCAL_NAME(NoiseDistort,N_("Noise Distort"));
57 SYNFIG_LAYER_SET_CATEGORY(NoiseDistort,N_("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=RandomNoise::SMOOTH_COSINE;
72 random.set_seed(time(NULL));
74 displacement=Vector(0.25,0.25);
75 Layer::Vocab voc(get_param_vocab());
76 Layer::fill_static(voc);
80 NoiseDistort::color_func(const Point &point, float /*supersample*/,Context context)const
84 float x(point[0]/size[0]*(1<<detail));
85 float y(point[1]/size[1]*(1<<detail));
90 RandomNoise::SmoothType temp_smooth(smooth);
91 RandomNoise::SmoothType smooth((!speed && temp_smooth == RandomNoise::SMOOTH_SPLINE) ? RandomNoise::SMOOTH_FAST_SPLINE : temp_smooth);
97 vect[0]=random(smooth,0+(detail-i)*5,x,y,time)+vect[0]*0.5;
98 vect[1]=random(smooth,1+(detail-i)*5,x,y,time)+vect[1]*0.5;
100 if(vect[0]<-1)vect[0]=-1;if(vect[0]>1)vect[0]=1;
101 if(vect[1]<-1)vect[1]=-1;if(vect[1]>1)vect[1]=1;
105 vect[0]=abs(vect[0]);
106 vect[1]=abs(vect[1]);
115 vect[0]=vect[0]/2.0f+0.5f;
116 vect[1]=vect[1]/2.0f+0.5f;
118 vect[0]=(vect[0]-0.5f)*displacement[0];
119 vect[1]=(vect[1]-0.5f)*displacement[1];
121 ret=context.get_color(point+vect);
127 NoiseDistort::calc_supersample(const synfig::Point &/*x*/, float /*pw*/,float /*ph*/)const
133 NoiseDistort::set_time(synfig::Context context, synfig::Time t)const
140 NoiseDistort::set_time(synfig::Context context, synfig::Time t, const synfig::Point &point)const
143 context.set_time(t,point);
146 synfig::Layer::Handle
147 NoiseDistort::hit_check(synfig::Context context, const synfig::Point &point)const
149 if(get_blend_method()==Color::BLEND_STRAIGHT && get_amount()>=0.5)
150 return const_cast<NoiseDistort*>(this);
151 if(get_amount()==0.0)
152 return context.hit_check(point);
153 if(color_func(point,0,context).get_a()>0.5)
154 return const_cast<NoiseDistort*>(this);
159 NoiseDistort::set_param(const String & param, const ValueBase &value)
161 if(param=="seed" && value.same_type_as(int()))
163 random.set_seed(value.get(int()));
164 set_param_static(param, value.get_static());
172 IMPORT(displacement);
173 return Layer_Composite::set_param(param,value);
177 NoiseDistort::get_param(const String & param)const
181 ValueBase ret(random.get_seed());
182 ret.set_static(get_param_static(param));
189 EXPORT(displacement);
195 return Layer_Composite::get_param(param);
199 NoiseDistort::get_param_vocab()const
201 Layer::Vocab ret(Layer_Composite::get_param_vocab());
203 ret.push_back(ParamDesc("displacement")
204 .set_local_name(_("Displacement"))
207 ret.push_back(ParamDesc("size")
208 .set_local_name(_("Size"))
210 ret.push_back(ParamDesc("seed")
211 .set_local_name(_("RandomNoise Seed"))
213 ret.push_back(ParamDesc("smooth")
214 .set_local_name(_("Interpolation"))
215 .set_description(_("What type of interpolation to use"))
217 .add_enum_value(RandomNoise::SMOOTH_DEFAULT, "nearest", _("Nearest Neighbor"))
218 .add_enum_value(RandomNoise::SMOOTH_LINEAR, "linear", _("Linear"))
219 .add_enum_value(RandomNoise::SMOOTH_COSINE, "cosine", _("Cosine"))
220 .add_enum_value(RandomNoise::SMOOTH_SPLINE, "spline", _("Spline"))
221 .add_enum_value(RandomNoise::SMOOTH_CUBIC, "cubic", _("Cubic"))
223 ret.push_back(ParamDesc("detail")
224 .set_local_name(_("Detail"))
226 ret.push_back(ParamDesc("speed")
227 .set_local_name(_("Animation Speed"))
229 ret.push_back(ParamDesc("turbulent")
230 .set_local_name(_("Turbulent"))
237 NoiseDistort::get_color(Context context, const Point &point)const
239 const Color color(color_func(point,0,context));
241 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
244 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
248 NoiseDistort::get_bounding_rect(Context context)const
253 if(Color::is_onto(get_blend_method()))
254 return context.get_full_bounding_rect();
256 Rect bounds(context.get_full_bounding_rect().expand_x(displacement[0]).expand_y(displacement[1]));
263 NoiseDistort::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
265 SuperCallback supercb(cb,0,9500,10000);
267 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
269 surface->set_wh(renddesc.get_w(),renddesc.get_h());
273 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
282 Surface::pen pen(surface->begin());
283 const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
285 Point tl(renddesc.get_tl());
286 const int w(surface->get_w());
287 const int h(surface->get_h());
289 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
291 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
292 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
293 pen.put_value(color_func(pos,calc_supersample(pos,pw,ph),context));
297 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
298 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
299 pen.put_value(Color::blend(color_func(pos,calc_supersample(pos,pw,ph),context),pen.get_value(),get_amount(),get_blend_method()));
302 // Mark our progress as finished
303 if(cb && !cb->amount_complete(10000,10000))