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