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