Fixed another instance of the bug which was causing the plant layer to break. When...
[synfig.git] / synfig-core / trunk / 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 temp_smooth(smooth);
88         int smooth((!speed && temp_smooth==3)?5:temp_smooth);
89
90         {
91                 Vector vect(0,0);
92                 for(i=0;i<detail;i++)
93                 {
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;
96
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;
99
100                         if(turbulent)
101                         {
102                                 vect[0]=abs(vect[0]);
103                                 vect[1]=abs(vect[1]);
104                         }
105
106                         x/=2.0f;
107                         y/=2.0f;
108                 }
109
110                 if(!turbulent)
111                 {
112                         vect[0]=vect[0]/2.0f+0.5f;
113                         vect[1]=vect[1]/2.0f+0.5f;
114                 }
115                 vect[0]=(vect[0]-0.5f)*displacement[0];
116                 vect[1]=(vect[1]-0.5f)*displacement[1];
117
118                 ret=context.get_color(point+vect);
119         }
120         return ret;
121 }
122
123 inline float
124 NoiseDistort::calc_supersample(const synfig::Point &x, float pw,float ph)const
125 {
126         return 0.0f;
127 }
128
129 void
130 NoiseDistort::set_time(synfig::Context context, synfig::Time t)const
131 {
132         curr_time=t;
133         context.set_time(t);
134 }
135
136 void
137 NoiseDistort::set_time(synfig::Context context, synfig::Time t, const synfig::Point &point)const
138 {
139         curr_time=t;
140         context.set_time(t,point);
141 }
142
143 synfig::Layer::Handle
144 NoiseDistort::hit_check(synfig::Context context, const synfig::Point &point)const
145 {
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);
152         return false;
153 }
154
155 bool
156 NoiseDistort::set_param(const String & param, const ValueBase &value)
157 {
158         if(param=="seed" && value.same_type_as(int()))
159         {
160                 random.set_seed(value.get(int()));
161                 return true;
162         }
163         IMPORT(size);
164         IMPORT(speed);
165         IMPORT(smooth);
166         IMPORT(detail);
167         IMPORT(turbulent);
168         IMPORT(displacement);
169         return Layer_Composite::set_param(param,value);
170 }
171
172 ValueBase
173 NoiseDistort::get_param(const String & param)const
174 {
175         if(param=="seed")
176                 return random.get_seed();
177         EXPORT(size);
178         EXPORT(speed);
179         EXPORT(smooth);
180         EXPORT(detail);
181         EXPORT(displacement);
182         EXPORT(turbulent);
183
184         EXPORT_NAME();
185         EXPORT_VERSION();
186
187         return Layer_Composite::get_param(param);
188 }
189
190 Layer::Vocab
191 NoiseDistort::get_param_vocab()const
192 {
193         Layer::Vocab ret(Layer_Composite::get_param_vocab());
194
195         ret.push_back(ParamDesc("displacement")
196                 .set_local_name(_("Displacement"))
197         );
198
199         ret.push_back(ParamDesc("size")
200                 .set_local_name(_("Size"))
201         );
202         ret.push_back(ParamDesc("seed")
203                 .set_local_name(_("Random Seed"))
204         );
205         ret.push_back(ParamDesc("smooth")
206                 .set_local_name(_("Interpolation"))
207                 .set_description(_("What type of interpolation to use"))
208                 .set_hint("enum")
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"))
214         );
215         ret.push_back(ParamDesc("detail")
216                 .set_local_name(_("Detail"))
217         );
218         ret.push_back(ParamDesc("speed")
219                 .set_local_name(_("Animation Speed"))
220         );
221         ret.push_back(ParamDesc("turbulent")
222                 .set_local_name(_("Turbulent"))
223         );
224
225         return ret;
226 }
227
228 Color
229 NoiseDistort::get_color(Context context, const Point &point)const
230 {
231         const Color color(color_func(point,0,context));
232
233         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
234                 return color;
235         else
236                 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
237 }
238
239 Rect
240 NoiseDistort::get_bounding_rect(Context context)const
241 {
242         if(is_disabled())
243                 return Rect::zero();
244
245         if(Color::is_onto(get_blend_method()))
246                 return context.get_full_bounding_rect();
247
248         Rect bounds(context.get_full_bounding_rect().expand_x(displacement[0]).expand_y(displacement[1]));
249
250         return bounds;
251 }
252
253 /*
254 bool
255 NoiseDistort::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
256 {
257         SuperCallback supercb(cb,0,9500,10000);
258
259         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
260         {
261                 surface->set_wh(renddesc.get_w(),renddesc.get_h());
262         }
263         else
264         {
265                 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
266                         return false;
267                 if(get_amount()==0)
268                         return true;
269         }
270
271
272         int x,y;
273
274         Surface::pen pen(surface->begin());
275         const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
276         Point pos;
277         Point tl(renddesc.get_tl());
278         const int w(surface->get_w());
279         const int h(surface->get_h());
280
281         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
282         {
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));
286         }
287         else
288         {
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()));
292         }
293
294         // Mark our progress as finished
295         if(cb && !cb->amount_complete(10000,10000))
296                 return false;
297
298         return true;
299 }
300 */