Add parameter static option for rest of layers.
[synfig.git] / synfig-core / src / modules / lyr_std / rotate.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file rotate.cpp
3 **      \brief Implementation of the "Rotate" layer
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2008 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 ** === N O T E S ===========================================================
23 **
24 ** ========================================================================= */
25
26 /* === H E A D E R S ======================================================= */
27
28 #ifdef USING_PCH
29 #       include "pch.h"
30 #else
31 #ifdef HAVE_CONFIG_H
32 #       include <config.h>
33 #endif
34
35 #include "rotate.h"
36 #include <synfig/string.h>
37 #include <synfig/time.h>
38 #include <synfig/context.h>
39 #include <synfig/paramdesc.h>
40 #include <synfig/renddesc.h>
41 #include <synfig/surface.h>
42 #include <synfig/value.h>
43 #include <synfig/valuenode.h>
44 #include <synfig/transform.h>
45 #include <ETL/misc>
46
47 #endif
48
49 /* === M A C R O S ========================================================= */
50
51 /* === G L O B A L S ======================================================= */
52
53 SYNFIG_LAYER_INIT(Rotate);
54 SYNFIG_LAYER_SET_NAME(Rotate,"rotate");
55 SYNFIG_LAYER_SET_LOCAL_NAME(Rotate,N_("Rotate"));
56 SYNFIG_LAYER_SET_CATEGORY(Rotate,N_("Transform"));
57 SYNFIG_LAYER_SET_VERSION(Rotate,"0.1");
58 SYNFIG_LAYER_SET_CVS_ID(Rotate,"$Id$");
59
60 /* === P R O C E D U R E S ================================================= */
61
62 /* === M E T H O D S ======================================================= */
63
64 /* === E N T R Y P O I N T ================================================= */
65
66 Rotate::Rotate():
67         origin  (0,0),
68         amount  (Angle::deg(0)),
69         sin_val (0),
70         cos_val (1)
71 {
72         Layer::Vocab voc(get_param_vocab());
73         Layer::fill_static(voc);
74 }
75
76 Rotate::~Rotate()
77 {
78 }
79
80 bool
81 Rotate::set_param(const String & param, const ValueBase &value)
82 {
83         IMPORT(origin);
84
85         if(param=="amount" && value.same_type_as(amount))
86         {
87                 amount=value.get(amount);
88                 sin_val=Angle::sin(amount).get();
89                 cos_val=Angle::cos(amount).get();
90                 set_param_static(param, value.get_static());
91                 return true;
92         }
93
94         return false;
95 }
96
97 ValueBase
98 Rotate::get_param(const String &param)const
99 {
100         EXPORT(origin);
101         EXPORT(amount);
102
103         EXPORT_NAME();
104         EXPORT_VERSION();
105
106         return ValueBase();
107 }
108
109 Layer::Vocab
110 Rotate::get_param_vocab()const
111 {
112         Layer::Vocab ret;
113
114         ret.push_back(ParamDesc("origin")
115                 .set_local_name(_("Origin"))
116                 .set_description(_("Point where you want the origin to be"))
117         );
118
119         ret.push_back(ParamDesc("amount")
120                 .set_local_name(_("Amount"))
121                 .set_description(_("Amount of rotation"))
122                 .set_origin("origin")
123         );
124
125         return ret;
126 }
127
128 class Rotate_Trans : public Transform
129 {
130         etl::handle<const Rotate> layer;
131 public:
132         Rotate_Trans(const Rotate* x):Transform(x->get_guid()),layer(x) { }
133
134         synfig::Vector perform(const synfig::Vector& x)const
135         {
136                 Point pos(x-layer->origin);
137                 return Point(layer->cos_val*pos[0]-layer->sin_val*pos[1],layer->sin_val*pos[0]+layer->cos_val*pos[1])+layer->origin;
138         }
139
140         synfig::Vector unperform(const synfig::Vector& x)const
141         {
142                 Point pos(x-layer->origin);
143                 return Point(layer->cos_val*pos[0]+layer->sin_val*pos[1],-layer->sin_val*pos[0]+layer->cos_val*pos[1])+layer->origin;
144         }
145 };
146 etl::handle<Transform>
147 Rotate::get_transform()const
148 {
149         return new Rotate_Trans(this);
150 }
151
152 synfig::Layer::Handle
153 Rotate::hit_check(synfig::Context context, const synfig::Point &p)const
154 {
155         Point pos(p-origin);
156         Point newpos(cos_val*pos[0]+sin_val*pos[1],-sin_val*pos[0]+cos_val*pos[1]);
157         newpos+=origin;
158         return context.hit_check(newpos);
159 }
160
161 Color
162 Rotate::get_color(Context context, const Point &p)const
163 {
164         Point pos(p-origin);
165         Point newpos(cos_val*pos[0]+sin_val*pos[1],-sin_val*pos[0]+cos_val*pos[1]);
166         newpos+=origin;
167         return context.get_color(newpos);
168 }
169
170 bool
171 Rotate::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
172 {
173         if(amount.dist(Angle::deg(0))==Angle::deg(0))
174                 return context.accelerated_render(surface,quality,renddesc,cb);
175         if(amount.dist(Angle::deg(180))==Angle::deg(0))
176         {
177                 RendDesc desc(renddesc);
178                 desc.clear_flags();
179                 Point tmp;
180                 tmp=renddesc.get_tl()-origin;
181                 desc.set_tl(Point(-tmp[0],-tmp[1])+origin);
182                 tmp=renddesc.get_br()-origin;
183                 desc.set_br(Point(-tmp[0],-tmp[1])+origin);
184                 return context.accelerated_render(surface,quality,desc,cb);
185         }
186
187         SuperCallback stageone(cb,0,9000,10000);
188         SuperCallback stagetwo(cb,9000,10000,10000);
189
190         if(cb && !cb->amount_complete(0,10000))
191                 return false;
192
193         Point tl(renddesc.get_tl()-origin);
194         Point br(renddesc.get_br()-origin);
195
196         {
197                 Point rot_tl(cos_val*tl[0]+sin_val*tl[1],-sin_val*tl[0]+cos_val*tl[1]);
198                 Point rot_br(cos_val*br[0]+sin_val*br[1],-sin_val*br[0]+cos_val*br[1]);
199                 Point rot_tr(cos_val*br[0]+sin_val*tl[1],-sin_val*br[0]+cos_val*tl[1]);
200                 Point rot_bl(cos_val*tl[0]+sin_val*br[1],-sin_val*tl[0]+cos_val*br[1]);
201                 rot_tl+=origin;
202                 rot_br+=origin;
203                 rot_tr+=origin;
204                 rot_bl+=origin;
205
206                 Point min_point(min(min(min(rot_tl[0],rot_br[0]),rot_tr[0]),rot_bl[0]),min(min(min(rot_tl[1],rot_br[1]),rot_tr[1]),rot_bl[1]));
207                 Point max_point(max(max(max(rot_tl[0],rot_br[0]),rot_tr[0]),rot_bl[0]),max(max(max(rot_tl[1],rot_br[1]),rot_tr[1]),rot_bl[1]));
208
209                 if(tl[0]>br[0])
210                 {
211                         tl[0]=max_point[0];
212                         br[0]=min_point[0];
213                 }
214                 else
215                 {
216                         br[0]=max_point[0];
217                         tl[0]=min_point[0];
218                 }
219                 if(tl[1]>br[1])
220                 {
221                         tl[1]=max_point[1];
222                         br[1]=min_point[1];
223                 }
224                 else
225                 {
226                         br[1]=max_point[1];
227                         tl[1]=min_point[1];
228                 }
229         }
230
231         Real pw=(renddesc.get_w())/(renddesc.get_br()[0]-renddesc.get_tl()[0]);
232         Real ph=(renddesc.get_h())/(renddesc.get_br()[1]-renddesc.get_tl()[1]);
233
234         // we're going to round the canvas size to an integer number of pixels, so round the
235         // tl-br rectangle accordingly - otherwise we see the jittering described in bug 2152666
236         br[0] -= (pw*(br[0]-tl[0]) - round_to_int(pw*(br[0]-tl[0]))) / pw;
237         br[1] -= (ph*(br[1]-tl[1]) - round_to_int(ph*(br[1]-tl[1]))) / ph;
238
239         RendDesc desc(renddesc);
240         desc.clear_flags();
241         //desc.set_flags(RendDesc::PX_ASPECT);
242         desc.set_tl(tl);
243         desc.set_br(br);
244         desc.set_wh(round_to_int(pw*(br[0]-tl[0])),round_to_int(ph*(br[1]-tl[1])));
245
246         //synfig::warning("given window: [%f,%f]-[%f,%f] %dx%d",renddesc.get_tl()[0],renddesc.get_tl()[1],renddesc.get_br()[0],renddesc.get_br()[1],renddesc.get_w(),renddesc.get_h());
247         //synfig::warning("surface to render: [%f,%f]-[%f,%f] %dx%d",desc.get_tl()[0],desc.get_tl()[1],desc.get_br()[0],desc.get_br()[1],desc.get_w(),desc.get_h());
248
249         Surface source;
250         source.set_wh(desc.get_w(),desc.get_h());
251
252         if(!context.accelerated_render(&source,quality,desc,&stageone))
253                 return false;
254
255         surface->set_wh(renddesc.get_w(),renddesc.get_h());
256
257         Surface::pen pen(surface->begin());
258
259         if(quality<=4)
260         {
261                 // CUBIC
262                 int x,y;//,u,v,u2,v2;
263                 Point point,tmp;
264                 for(y=0,point[1]=renddesc.get_tl()[1];y<surface->get_h();y++,pen.inc_y(),pen.dec_x(x),point[1]+=1.0/ph)
265                 {
266                         for(x=0,point[0]=renddesc.get_tl()[0];x<surface->get_w();x++,pen.inc_x(),point[0]+=1.0/pw)
267                         {
268                                 tmp=Point(cos_val*(point[0]-origin[0])+sin_val*(point[1]-origin[1]),-sin_val*(point[0]-origin[0])+cos_val*(point[1]-origin[1])) +origin;
269                                 (*surface)[y][x]=source.cubic_sample((tmp[0]-tl[0])*pw,(tmp[1]-tl[1])*ph);
270                         }
271                         if((y&31)==0 && cb)
272                         {
273                                 if(!stagetwo.amount_complete(y,surface->get_h()))
274                                         return false;
275                         }
276                 }
277         }
278         else
279         if(quality<=6)
280         {
281                 // INTERPOLATION_LINEAR
282                 int x,y;//,u,v,u2,v2;
283                 Point point,tmp;
284                 for(y=0,point[1]=renddesc.get_tl()[1];y<surface->get_h();y++,pen.inc_y(),pen.dec_x(x),point[1]+=1.0/ph)
285                 {
286                         for(x=0,point[0]=renddesc.get_tl()[0];x<surface->get_w();x++,pen.inc_x(),point[0]+=1.0/pw)
287                         {
288                                 tmp=Point(cos_val*(point[0]-origin[0])+sin_val*(point[1]-origin[1]),-sin_val*(point[0]-origin[0])+cos_val*(point[1]-origin[1])) +origin;
289                                 (*surface)[y][x]=source.linear_sample((tmp[0]-tl[0])*pw,(tmp[1]-tl[1])*ph);
290                         }
291                         if((y&31)==0 && cb)
292                         {
293                                 if(!stagetwo.amount_complete(y,surface->get_h()))
294                                         return false;
295                         }
296                 }
297         }
298         else
299         {
300                 // NEAREST_NEIGHBOR
301                 int x,y,u,v;
302                 Point point,tmp;
303                 for(y=0,point[1]=renddesc.get_tl()[1];y<surface->get_h();y++,pen.inc_y(),pen.dec_x(x),point[1]+=1.0/ph)
304                 {
305                         for(x=0,point[0]=renddesc.get_tl()[0];x<surface->get_w();x++,pen.inc_x(),point[0]+=1.0/pw)
306                         {
307                                 tmp=Point(cos_val*(point[0]-origin[0])+sin_val*(point[1]-origin[1]),-sin_val*(point[0]-origin[0])+cos_val*(point[1]-origin[1])) +origin;
308                                 u=int((tmp[0]-tl[0])*pw);
309                                 v=int((tmp[1]-tl[1])*ph);
310                                 if(u<0)
311                                         u=0;
312                                 if(v<0)
313                                         v=0;
314                                 if(u>=source.get_w())
315                                         u=source.get_w()-1;
316                                 if(v>=source.get_h())
317                                         v=source.get_h()-1;
318                                 //pen.set_value(source[v][u]);
319                                 (*surface)[y][x]=source[v][u];
320                         }
321                         if((y&31)==0 && cb)
322                         {
323                                 if(!stagetwo.amount_complete(y,surface->get_h()))
324                                         return false;
325                         }
326                 }
327         }
328
329         if(cb && !cb->amount_complete(10000,10000)) return false;
330
331         return true;
332 }
333
334 Rect
335 Rotate::get_full_bounding_rect(Context context)const
336 {
337         Rect under(context.get_full_bounding_rect());
338         return get_transform()->perform(under);
339 }
340