1 /* === S I N F G =========================================================== */
5 ** $Id: distort.cpp,v 1.1.1.1 2005/01/04 01:23:14 darco Exp $
8 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
10 ** This software and associated documentation
11 ** are CONFIDENTIAL and PROPRIETARY property of
12 ** the above-mentioned copyright holder.
14 ** You may not copy, print, publish, or in any
15 ** other way distribute this software without
16 ** a prior written agreement with
17 ** the copyright holder.
20 /* ========================================================================= */
22 /* === H E A D E R S ======================================================= */
33 #include <sinfg/string.h>
34 #include <sinfg/time.h>
35 #include <sinfg/context.h>
36 #include <sinfg/paramdesc.h>
37 #include <sinfg/renddesc.h>
38 #include <sinfg/surface.h>
39 #include <sinfg/value.h>
40 #include <sinfg/valuenode.h>
44 /* === M A C R O S ========================================================= */
46 using namespace sinfg;
50 /* === G L O B A L S ======================================================= */
52 SINFG_LAYER_INIT(NoiseDistort);
53 SINFG_LAYER_SET_NAME(NoiseDistort,"noise_distort");
54 SINFG_LAYER_SET_LOCAL_NAME(NoiseDistort,_("Noise Distort"));
55 SINFG_LAYER_SET_CATEGORY(NoiseDistort,_("Distortions"));
56 SINFG_LAYER_SET_VERSION(NoiseDistort,"0.0");
57 SINFG_LAYER_SET_CVS_ID(NoiseDistort,"$Id: distort.cpp,v 1.1.1.1 2005/01/04 01:23:14 darco Exp $");
59 /* === P R O C E D U R E S ================================================= */
61 /* === M E T H O D S ======================================================= */
63 NoiseDistort::NoiseDistort():
66 set_blend_method(Color::BLEND_STRAIGHT);
70 random.set_seed(time(NULL));
72 displacement=Vector(0.25,0.25);
76 NoiseDistort::color_func(const Point &point, float supersample,Context context)const
80 float x(point[0]/size[0]*(1<<detail));
81 float y(point[1]/size[1]*(1<<detail));
86 int smooth((!speed && smooth==3)?5:smooth);
92 vect[0]=random(smooth,0+(detail-i)*5,x,y,time)+vect[0]*0.5;
93 vect[1]=random(smooth,1+(detail-i)*5,x,y,time)+vect[1]*0.5;
95 if(vect[0]<-1)vect[0]=-1;if(vect[0]>1)vect[0]=1;
96 if(vect[1]<-1)vect[1]=-1;if(vect[1]>1)vect[1]=1;
100 vect[0]=abs(vect[0]);
101 vect[1]=abs(vect[1]);
110 vect[0]=vect[0]/2.0f+0.5f;
111 vect[1]=vect[1]/2.0f+0.5f;
113 vect[0]=(vect[0]-0.5f)*displacement[0];
114 vect[1]=(vect[1]-0.5f)*displacement[1];
116 ret=context.get_color(point+vect);
122 NoiseDistort::calc_supersample(const sinfg::Point &x, float pw,float ph)const
128 NoiseDistort::set_time(sinfg::Context context, sinfg::Time t)const
135 NoiseDistort::set_time(sinfg::Context context, sinfg::Time t, const sinfg::Point &point)const
138 context.set_time(t,point);
142 NoiseDistort::hit_check(sinfg::Context context, const sinfg::Point &point)const
144 if(get_blend_method()==Color::BLEND_STRAIGHT && get_amount()>=0.5)
145 return const_cast<NoiseDistort*>(this);
146 if(get_amount()==0.0)
147 return context.hit_check(point);
148 if(color_func(point,0,context).get_a()>0.5)
149 return const_cast<NoiseDistort*>(this);
154 NoiseDistort::set_param(const String & param, const ValueBase &value)
156 if(param=="seed" && value.same_as(int()))
158 random.set_seed(value.get(int()));
166 IMPORT(displacement);
167 return Layer_Composite::set_param(param,value);
171 NoiseDistort::get_param(const String & param)const
174 return random.get_seed();
179 EXPORT(displacement);
185 return Layer_Composite::get_param(param);
189 NoiseDistort::get_param_vocab()const
191 Layer::Vocab ret(Layer_Composite::get_param_vocab());
193 ret.push_back(ParamDesc("displacement")
194 .set_local_name(_("Displacement"))
197 ret.push_back(ParamDesc("size")
198 .set_local_name(_("Size"))
200 ret.push_back(ParamDesc("seed")
201 .set_local_name(_("Random Seed"))
203 ret.push_back(ParamDesc("smooth")
204 .set_local_name(_("Interpolation"))
205 .set_description(_("What type of interpolation to use"))
207 .add_enum_value(0,"nearest",_("Nearest Neighbor"))
208 .add_enum_value(1,"linear",_("Linear"))
209 .add_enum_value(2,"cosine",_("Cosine"))
210 .add_enum_value(3,"spline",_("Spline"))
211 .add_enum_value(4,"cubic",_("Cubic"))
213 ret.push_back(ParamDesc("detail")
214 .set_local_name(_("Detail"))
216 ret.push_back(ParamDesc("speed")
217 .set_local_name(_("Animation Speed"))
219 ret.push_back(ParamDesc("turbulent")
220 .set_local_name(_("Turbulent"))
227 NoiseDistort::get_color(Context context, const Point &point)const
229 const Color color(color_func(point,0,context));
231 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
234 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
238 NoiseDistort::get_bounding_rect(Context context)const
243 if(Color::is_onto(get_blend_method()))
244 return context.get_full_bounding_rect();
246 Rect bounds(context.get_full_bounding_rect().expand_x(displacement[0]).expand_y(displacement[1]));
253 NoiseDistort::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
255 SuperCallback supercb(cb,0,9500,10000);
257 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
259 surface->set_wh(renddesc.get_w(),renddesc.get_h());
263 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
272 Surface::pen pen(surface->begin());
273 const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
275 Point tl(renddesc.get_tl());
276 const int w(surface->get_w());
277 const int h(surface->get_h());
279 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
281 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
282 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
283 pen.put_value(color_func(pos,calc_supersample(pos,pw,ph),context));
287 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
288 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
289 pen.put_value(Color::blend(color_func(pos,calc_supersample(pos,pw,ph),context),pen.get_value(),get_amount(),get_blend_method()));
292 // Mark our progress as finished
293 if(cb && !cb->amount_complete(10000,10000))