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