Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_06 / src / modules / mod_noise / distort.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file distort.cpp
3 **      \brief blehh
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
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.
14 **
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.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include "distort.h"
33
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>
42
43 #endif
44
45 /* === M A C R O S ========================================================= */
46
47 using namespace synfig;
48 using namespace std;
49 using namespace etl;
50
51 /* === G L O B A L S ======================================================= */
52
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$");
59
60 /* === P R O C E D U R E S ================================================= */
61
62 /* === M E T H O D S ======================================================= */
63
64 NoiseDistort::NoiseDistort():
65         size(1,1)
66 {
67         set_blend_method(Color::BLEND_STRAIGHT);
68         smooth=2;
69         detail=4;
70         speed=0;
71         random.set_seed(time(NULL));
72         turbulent=false;
73         displacement=Vector(0.25,0.25);
74 }
75
76 inline Color
77 NoiseDistort::color_func(const Point &point, float supersample,Context context)const
78 {
79         Color ret(0,0,0,0);
80
81         float x(point[0]/size[0]*(1<<detail));
82         float y(point[1]/size[1]*(1<<detail));
83
84         int i;
85         Time time;
86         time=speed*curr_time;
87         int smooth((!speed && smooth==3)?5:smooth);
88
89         {
90                 Vector vect(0,0);
91                 for(i=0;i<detail;i++)
92                 {
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;
95
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;
98
99                         if(turbulent)
100                         {
101                                 vect[0]=abs(vect[0]);
102                                 vect[1]=abs(vect[1]);
103                         }
104
105                         x/=2.0f;
106                         y/=2.0f;
107                 }
108
109                 if(!turbulent)
110                 {
111                         vect[0]=vect[0]/2.0f+0.5f;
112                         vect[1]=vect[1]/2.0f+0.5f;
113                 }
114                 vect[0]=(vect[0]-0.5f)*displacement[0];
115                 vect[1]=(vect[1]-0.5f)*displacement[1];
116
117                 ret=context.get_color(point+vect);
118         }
119         return ret;
120 }
121
122 inline float
123 NoiseDistort::calc_supersample(const synfig::Point &x, float pw,float ph)const
124 {
125         return 0.0f;
126 }
127
128 void
129 NoiseDistort::set_time(synfig::Context context, synfig::Time t)const
130 {
131         curr_time=t;
132         context.set_time(t);
133 }
134
135 void
136 NoiseDistort::set_time(synfig::Context context, synfig::Time t, const synfig::Point &point)const
137 {
138         curr_time=t;
139         context.set_time(t,point);
140 }
141
142 synfig::Layer::Handle
143 NoiseDistort::hit_check(synfig::Context context, const synfig::Point &point)const
144 {
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);
151         return false;
152 }
153
154 bool
155 NoiseDistort::set_param(const String & param, const ValueBase &value)
156 {
157         if(param=="seed" && value.same_as(int()))
158         {
159                 random.set_seed(value.get(int()));
160                 return true;
161         }
162         IMPORT(size);
163         IMPORT(speed);
164         IMPORT(smooth);
165         IMPORT(detail);
166         IMPORT(turbulent);
167         IMPORT(displacement);
168         return Layer_Composite::set_param(param,value);
169 }
170
171 ValueBase
172 NoiseDistort::get_param(const String & param)const
173 {
174         if(param=="seed")
175                 return random.get_seed();
176         EXPORT(size);
177         EXPORT(speed);
178         EXPORT(smooth);
179         EXPORT(detail);
180         EXPORT(displacement);
181         EXPORT(turbulent);
182
183         EXPORT_NAME();
184         EXPORT_VERSION();
185
186         return Layer_Composite::get_param(param);
187 }
188
189 Layer::Vocab
190 NoiseDistort::get_param_vocab()const
191 {
192         Layer::Vocab ret(Layer_Composite::get_param_vocab());
193
194         ret.push_back(ParamDesc("displacement")
195                 .set_local_name(_("Displacement"))
196         );
197
198         ret.push_back(ParamDesc("size")
199                 .set_local_name(_("Size"))
200         );
201         ret.push_back(ParamDesc("seed")
202                 .set_local_name(_("Random Seed"))
203         );
204         ret.push_back(ParamDesc("smooth")
205                 .set_local_name(_("Interpolation"))
206                 .set_description(_("What type of interpolation to use"))
207                 .set_hint("enum")
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"))
213         );
214         ret.push_back(ParamDesc("detail")
215                 .set_local_name(_("Detail"))
216         );
217         ret.push_back(ParamDesc("speed")
218                 .set_local_name(_("Animation Speed"))
219         );
220         ret.push_back(ParamDesc("turbulent")
221                 .set_local_name(_("Turbulent"))
222         );
223
224         return ret;
225 }
226
227 Color
228 NoiseDistort::get_color(Context context, const Point &point)const
229 {
230         const Color color(color_func(point,0,context));
231
232         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
233                 return color;
234         else
235                 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
236 }
237
238 Rect
239 NoiseDistort::get_bounding_rect(Context context)const
240 {
241         if(is_disabled())
242                 return Rect::zero();
243
244         if(Color::is_onto(get_blend_method()))
245                 return context.get_full_bounding_rect();
246
247         Rect bounds(context.get_full_bounding_rect().expand_x(displacement[0]).expand_y(displacement[1]));
248
249         return bounds;
250 }
251
252 /*
253 bool
254 NoiseDistort::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
255 {
256         SuperCallback supercb(cb,0,9500,10000);
257
258         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
259         {
260                 surface->set_wh(renddesc.get_w(),renddesc.get_h());
261         }
262         else
263         {
264                 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
265                         return false;
266                 if(get_amount()==0)
267                         return true;
268         }
269
270
271         int x,y;
272
273         Surface::pen pen(surface->begin());
274         const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
275         Point pos;
276         Point tl(renddesc.get_tl());
277         const int w(surface->get_w());
278         const int h(surface->get_h());
279
280         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
281         {
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));
285         }
286         else
287         {
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()));
291         }
292
293         // Mark our progress as finished
294         if(cb && !cb->amount_complete(10000,10000))
295                 return false;
296
297         return true;
298 }
299 */