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