Fixing warnings from doxygen:
[synfig.git] / synfig-core / trunk / src / modules / lyr_std / rotate.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file rotate.cpp
3 **      \brief Template Header
4 **
5 **      \legal
6 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
7 **
8 **      This package is free software; you can redistribute it and/or
9 **      modify it under the terms of the GNU General Public License as
10 **      published by the Free Software Foundation; either version 2 of
11 **      the License, or (at your option) any later version.
12 **
13 **      This package is distributed in the hope that it will be useful,
14 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
15 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 **      General Public License for more details.
17 **      \endlegal
18 **
19 ** === N O T E S ===========================================================
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 "rotate.h"
33 #include <synfig/string.h>
34 #include <synfig/time.h>
35 #include <synfig/context.h>
36 #include <synfig/paramdesc.h>
37 #include <synfig/renddesc.h>
38 #include <synfig/surface.h>
39 #include <synfig/value.h>
40 #include <synfig/valuenode.h>
41 #include <synfig/transform.h>
42 #include <ETL/misc>
43
44 #endif
45
46 /* === M A C R O S ========================================================= */
47
48 /* === G L O B A L S ======================================================= */
49
50 SYNFIG_LAYER_INIT(Rotate);
51 SYNFIG_LAYER_SET_NAME(Rotate,"rotate");
52 SYNFIG_LAYER_SET_LOCAL_NAME(Rotate,_("Rotate"));
53 SYNFIG_LAYER_SET_CATEGORY(Rotate,_("Transform"));
54 SYNFIG_LAYER_SET_VERSION(Rotate,"0.1");
55 SYNFIG_LAYER_SET_CVS_ID(Rotate,"$Id: rotate.cpp,v 1.2 2005/01/24 05:00:18 darco Exp $");
56
57 /* === P R O C E D U R E S ================================================= */
58
59 /* === M E T H O D S ======================================================= */
60
61 /* === E N T R Y P O I N T ================================================= */
62
63 Rotate::Rotate():
64         origin  (0,0),
65         amount  (Angle::deg(0)),
66         sin_val (0),
67         cos_val (1)
68 {
69 }
70
71 Rotate::~Rotate()
72 {
73 }
74
75 bool
76 Rotate::set_param(const String & param, const ValueBase &value)
77 {
78         IMPORT(origin);
79
80         if(param=="amount" && value.same_as(amount))
81         {
82                 amount=value.get(amount);
83                 sin_val=Angle::sin(amount).get();
84                 cos_val=Angle::cos(amount).get();
85                 return true;
86         }
87
88         return false;
89 }
90
91 ValueBase
92 Rotate::get_param(const String &param)const
93 {
94         EXPORT(origin);
95         EXPORT(amount);
96
97         EXPORT_NAME();
98         EXPORT_VERSION();
99
100         return ValueBase();
101 }
102
103 Layer::Vocab
104 Rotate::get_param_vocab()const
105 {
106         Layer::Vocab ret;
107
108         ret.push_back(ParamDesc("origin")
109                 .set_local_name(_("Origin"))
110                 .set_description(_("Point where you want the origin to be"))
111         );
112
113         ret.push_back(ParamDesc("amount")
114                 .set_local_name(_("Amount"))
115                 .set_description(_("Amount of rotation"))
116                 .set_origin("origin")
117         );
118
119         return ret;
120 }
121
122 class Rotate_Trans : public Transform
123 {
124         etl::handle<const Rotate> layer;
125 public:
126         Rotate_Trans(const Rotate* x):Transform(x->get_guid()),layer(x) { }
127
128         synfig::Vector perform(const synfig::Vector& x)const
129         {
130                 Point pos(x-layer->origin);
131                 return Point(layer->cos_val*pos[0]-layer->sin_val*pos[1],layer->sin_val*pos[0]+layer->cos_val*pos[1])+layer->origin;
132         }
133
134         synfig::Vector unperform(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 etl::handle<Transform>
141 Rotate::get_transform()const
142 {
143         return new Rotate_Trans(this);
144 }
145
146 synfig::Layer::Handle
147 Rotate::hit_check(synfig::Context context, const synfig::Point &p)const
148 {
149         Point pos(p-origin);
150         Point newpos(cos_val*pos[0]+sin_val*pos[1],-sin_val*pos[0]+cos_val*pos[1]);
151         newpos+=origin;
152         return context.hit_check(newpos);
153 }
154
155 Color
156 Rotate::get_color(Context context, const Point &p)const
157 {
158         Point pos(p-origin);
159         Point newpos(cos_val*pos[0]+sin_val*pos[1],-sin_val*pos[0]+cos_val*pos[1]);
160         newpos+=origin;
161         return context.get_color(newpos);
162 }
163
164 bool
165 Rotate::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
166 {
167         if(amount.dist(Angle::deg(0))==Angle::deg(0))
168                 return context.accelerated_render(surface,quality,renddesc,cb);
169         if(amount.dist(Angle::deg(180))==Angle::deg(0))
170         {
171                 RendDesc desc(renddesc);
172                 desc.clear_flags();
173                 Point tmp;
174                 tmp=renddesc.get_tl()-origin;
175                 desc.set_tl(Point(-tmp[0],-tmp[1])+origin);
176                 tmp=renddesc.get_br()-origin;
177                 desc.set_br(Point(-tmp[0],-tmp[1])+origin);
178                 return context.accelerated_render(surface,quality,desc,cb);
179         }
180
181         SuperCallback stageone(cb,0,9000,10000);
182         SuperCallback stagetwo(cb,9000,10000,10000);
183
184         if(cb && !cb->amount_complete(0,10000))
185                 return false;
186
187         Point tl(renddesc.get_tl()-origin);
188         Point br(renddesc.get_br()-origin);
189         Point rot_tl(cos_val*tl[0]+sin_val*tl[1],-sin_val*tl[0]+cos_val*tl[1]);
190         Point rot_br(cos_val*br[0]+sin_val*br[1],-sin_val*br[0]+cos_val*br[1]);
191         Point rot_tr(cos_val*br[0]+sin_val*tl[1],-sin_val*br[0]+cos_val*tl[1]);
192         Point rot_bl(cos_val*tl[0]+sin_val*br[1],-sin_val*tl[0]+cos_val*br[1]);
193         rot_tl+=origin;
194         rot_br+=origin;
195         rot_tr+=origin;
196         rot_bl+=origin;
197
198         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]));
199         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]));
200
201         if(tl[0]>br[0])
202         {
203                 tl[0]=max_point[0];
204                 br[0]=min_point[0];
205         }
206         else
207         {
208                 br[0]=max_point[0];
209                 tl[0]=min_point[0];
210         }
211         if(tl[1]>br[1])
212         {
213                 tl[1]=max_point[1];
214                 br[1]=min_point[1];
215         }
216         else
217         {
218                 br[1]=max_point[1];
219                 tl[1]=min_point[1];
220         }
221
222         Real pw=(renddesc.get_w())/(renddesc.get_br()[0]-renddesc.get_tl()[0]);
223         Real ph=(renddesc.get_h())/(renddesc.get_br()[1]-renddesc.get_tl()[1]);
224
225         RendDesc desc(renddesc);
226         desc.clear_flags();
227         //desc.set_flags(RendDesc::PX_ASPECT);
228         desc.set_tl(tl);
229         desc.set_br(br);
230         desc.set_wh(round_to_int(pw*(br[0]-tl[0])),round_to_int(ph*(br[1]-tl[1])));
231
232         //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());
233         //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());
234
235         Surface source;
236         source.set_wh(desc.get_w(),desc.get_h());
237
238         if(!context.accelerated_render(&source,quality,desc,&stageone))
239                 return false;
240
241         surface->set_wh(renddesc.get_w(),renddesc.get_h());
242
243         Surface::pen pen(surface->begin());
244
245         if(quality<=4)
246         {
247                 // CUBIC
248                 int x,y;//,u,v,u2,v2;
249                 Point point,tmp;
250                 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)
251                 {
252                         for(x=0,point[0]=renddesc.get_tl()[0];x<surface->get_w();x++,pen.inc_x(),point[0]+=1.0/pw)
253                         {
254                                 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;
255                                 (*surface)[y][x]=source.cubic_sample((tmp[0]-tl[0])*pw,(tmp[1]-tl[1])*ph);
256                         }
257                         if(y&31==0 && cb)
258                         {
259                                 if(!stagetwo.amount_complete(y,surface->get_h()))
260                                         return false;
261                         }
262                 }
263         }
264         else
265         if(quality<=6)
266         {
267                 // INTERPOLATION_LINEAR
268                 int x,y;//,u,v,u2,v2;
269                 Point point,tmp;
270                 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)
271                 {
272                         for(x=0,point[0]=renddesc.get_tl()[0];x<surface->get_w();x++,pen.inc_x(),point[0]+=1.0/pw)
273                         {
274                                 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;
275                                 (*surface)[y][x]=source.linear_sample((tmp[0]-tl[0])*pw,(tmp[1]-tl[1])*ph);
276                         }
277                         if(y&31==0 && cb)
278                         {
279                                 if(!stagetwo.amount_complete(y,surface->get_h()))
280                                         return false;
281                         }
282                 }
283         }
284         else
285         {
286                 // NEAREST_NEIGHBOR
287                 int x,y,u,v;
288                 Point point,tmp;
289                 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)
290                 {
291                         for(x=0,point[0]=renddesc.get_tl()[0];x<surface->get_w();x++,pen.inc_x(),point[0]+=1.0/pw)
292                         {
293                                 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;
294                                 u=int((tmp[0]-tl[0])*pw);
295                                 v=int((tmp[1]-tl[1])*ph);
296                                 if(u<0)
297                                         u=0;
298                                 if(v<0)
299                                         v=0;
300                                 if(u>=source.get_w())
301                                         u=source.get_w()-1;
302                                 if(v>=source.get_h())
303                                         v=source.get_h()-1;
304                                 //pen.set_value(source[v][u]);
305                                 (*surface)[y][x]=source[v][u];
306                         }
307                         if(y&31==0 && cb)
308                         {
309                                 if(!stagetwo.amount_complete(y,surface->get_h()))
310                                         return false;
311                         }
312                 }
313         }
314
315         if(cb && !cb->amount_complete(10000,10000)) return false;
316
317         return true;
318 }
319
320 Rect
321 Rotate::get_full_bounding_rect(Context context)const
322 {
323         Rect under(context.get_full_bounding_rect());
324         return get_transform()->perform(under);
325 }
326