Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / stable / 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 }
73
74 Rotate::~Rotate()
75 {
76 }
77
78 bool
79 Rotate::set_param(const String & param, const ValueBase &value)
80 {
81         IMPORT(origin);
82
83         if(param=="amount" && value.same_type_as(amount))
84         {
85                 amount=value.get(amount);
86                 sin_val=Angle::sin(amount).get();
87                 cos_val=Angle::cos(amount).get();
88                 return true;
89         }
90
91         return false;
92 }
93
94 ValueBase
95 Rotate::get_param(const String &param)const
96 {
97         EXPORT(origin);
98         EXPORT(amount);
99
100         EXPORT_NAME();
101         EXPORT_VERSION();
102
103         return ValueBase();
104 }
105
106 Layer::Vocab
107 Rotate::get_param_vocab()const
108 {
109         Layer::Vocab ret;
110
111         ret.push_back(ParamDesc("origin")
112                 .set_local_name(_("Origin"))
113                 .set_description(_("Point where you want the origin to be"))
114         );
115
116         ret.push_back(ParamDesc("amount")
117                 .set_local_name(_("Amount"))
118                 .set_description(_("Amount of rotation"))
119                 .set_origin("origin")
120         );
121
122         return ret;
123 }
124
125 class Rotate_Trans : public Transform
126 {
127         etl::handle<const Rotate> layer;
128 public:
129         Rotate_Trans(const Rotate* x):Transform(x->get_guid()),layer(x) { }
130
131         synfig::Vector perform(const synfig::Vector& x)const
132         {
133                 Point pos(x-layer->origin);
134                 return Point(layer->cos_val*pos[0]-layer->sin_val*pos[1],layer->sin_val*pos[0]+layer->cos_val*pos[1])+layer->origin;
135         }
136
137         synfig::Vector unperform(const synfig::Vector& x)const
138         {
139                 Point pos(x-layer->origin);
140                 return Point(layer->cos_val*pos[0]+layer->sin_val*pos[1],-layer->sin_val*pos[0]+layer->cos_val*pos[1])+layer->origin;
141         }
142 };
143 etl::handle<Transform>
144 Rotate::get_transform()const
145 {
146         return new Rotate_Trans(this);
147 }
148
149 synfig::Layer::Handle
150 Rotate::hit_check(synfig::Context context, const synfig::Point &p)const
151 {
152         Point pos(p-origin);
153         Point newpos(cos_val*pos[0]+sin_val*pos[1],-sin_val*pos[0]+cos_val*pos[1]);
154         newpos+=origin;
155         return context.hit_check(newpos);
156 }
157
158 Color
159 Rotate::get_color(Context context, const Point &p)const
160 {
161         Point pos(p-origin);
162         Point newpos(cos_val*pos[0]+sin_val*pos[1],-sin_val*pos[0]+cos_val*pos[1]);
163         newpos+=origin;
164         return context.get_color(newpos);
165 }
166
167 bool
168 Rotate::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
169 {
170         if(amount.dist(Angle::deg(0))==Angle::deg(0))
171                 return context.accelerated_render(surface,quality,renddesc,cb);
172         if(amount.dist(Angle::deg(180))==Angle::deg(0))
173         {
174                 RendDesc desc(renddesc);
175                 desc.clear_flags();
176                 Point tmp;
177                 tmp=renddesc.get_tl()-origin;
178                 desc.set_tl(Point(-tmp[0],-tmp[1])+origin);
179                 tmp=renddesc.get_br()-origin;
180                 desc.set_br(Point(-tmp[0],-tmp[1])+origin);
181                 return context.accelerated_render(surface,quality,desc,cb);
182         }
183
184         SuperCallback stageone(cb,0,9000,10000);
185         SuperCallback stagetwo(cb,9000,10000,10000);
186
187         if(cb && !cb->amount_complete(0,10000))
188                 return false;
189
190         Point tl(renddesc.get_tl()-origin);
191         Point br(renddesc.get_br()-origin);
192
193         {
194                 Point rot_tl(cos_val*tl[0]+sin_val*tl[1],-sin_val*tl[0]+cos_val*tl[1]);
195                 Point rot_br(cos_val*br[0]+sin_val*br[1],-sin_val*br[0]+cos_val*br[1]);
196                 Point rot_tr(cos_val*br[0]+sin_val*tl[1],-sin_val*br[0]+cos_val*tl[1]);
197                 Point rot_bl(cos_val*tl[0]+sin_val*br[1],-sin_val*tl[0]+cos_val*br[1]);
198                 rot_tl+=origin;
199                 rot_br+=origin;
200                 rot_tr+=origin;
201                 rot_bl+=origin;
202
203                 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]));
204                 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]));
205
206                 if(tl[0]>br[0])
207                 {
208                         tl[0]=max_point[0];
209                         br[0]=min_point[0];
210                 }
211                 else
212                 {
213                         br[0]=max_point[0];
214                         tl[0]=min_point[0];
215                 }
216                 if(tl[1]>br[1])
217                 {
218                         tl[1]=max_point[1];
219                         br[1]=min_point[1];
220                 }
221                 else
222                 {
223                         br[1]=max_point[1];
224                         tl[1]=min_point[1];
225                 }
226         }
227
228         Real pw=(renddesc.get_w())/(renddesc.get_br()[0]-renddesc.get_tl()[0]);
229         Real ph=(renddesc.get_h())/(renddesc.get_br()[1]-renddesc.get_tl()[1]);
230
231         // we're going to round the canvas size to an integer number of pixels, so round the
232         // tl-br rectangle accordingly - otherwise we see the jittering described in bug 2152666
233         br[0] -= (pw*(br[0]-tl[0]) - round_to_int(pw*(br[0]-tl[0]))) / pw;
234         br[1] -= (ph*(br[1]-tl[1]) - round_to_int(ph*(br[1]-tl[1]))) / ph;
235
236         RendDesc desc(renddesc);
237         desc.clear_flags();
238         //desc.set_flags(RendDesc::PX_ASPECT);
239         desc.set_tl(tl);
240         desc.set_br(br);
241         desc.set_wh(round_to_int(pw*(br[0]-tl[0])),round_to_int(ph*(br[1]-tl[1])));
242
243         //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());
244         //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());
245
246         Surface source;
247         source.set_wh(desc.get_w(),desc.get_h());
248
249         if(!context.accelerated_render(&source,quality,desc,&stageone))
250                 return false;
251
252         surface->set_wh(renddesc.get_w(),renddesc.get_h());
253
254         Surface::pen pen(surface->begin());
255
256         if(quality<=4)
257         {
258                 // CUBIC
259                 int x,y;//,u,v,u2,v2;
260                 Point point,tmp;
261                 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)
262                 {
263                         for(x=0,point[0]=renddesc.get_tl()[0];x<surface->get_w();x++,pen.inc_x(),point[0]+=1.0/pw)
264                         {
265                                 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;
266                                 (*surface)[y][x]=source.cubic_sample((tmp[0]-tl[0])*pw,(tmp[1]-tl[1])*ph);
267                         }
268                         if((y&31)==0 && cb)
269                         {
270                                 if(!stagetwo.amount_complete(y,surface->get_h()))
271                                         return false;
272                         }
273                 }
274         }
275         else
276         if(quality<=6)
277         {
278                 // INTERPOLATION_LINEAR
279                 int x,y;//,u,v,u2,v2;
280                 Point point,tmp;
281                 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)
282                 {
283                         for(x=0,point[0]=renddesc.get_tl()[0];x<surface->get_w();x++,pen.inc_x(),point[0]+=1.0/pw)
284                         {
285                                 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;
286                                 (*surface)[y][x]=source.linear_sample((tmp[0]-tl[0])*pw,(tmp[1]-tl[1])*ph);
287                         }
288                         if((y&31)==0 && cb)
289                         {
290                                 if(!stagetwo.amount_complete(y,surface->get_h()))
291                                         return false;
292                         }
293                 }
294         }
295         else
296         {
297                 // NEAREST_NEIGHBOR
298                 int x,y,u,v;
299                 Point point,tmp;
300                 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)
301                 {
302                         for(x=0,point[0]=renddesc.get_tl()[0];x<surface->get_w();x++,pen.inc_x(),point[0]+=1.0/pw)
303                         {
304                                 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;
305                                 u=int((tmp[0]-tl[0])*pw);
306                                 v=int((tmp[1]-tl[1])*ph);
307                                 if(u<0)
308                                         u=0;
309                                 if(v<0)
310                                         v=0;
311                                 if(u>=source.get_w())
312                                         u=source.get_w()-1;
313                                 if(v>=source.get_h())
314                                         v=source.get_h()-1;
315                                 //pen.set_value(source[v][u]);
316                                 (*surface)[y][x]=source[v][u];
317                         }
318                         if((y&31)==0 && cb)
319                         {
320                                 if(!stagetwo.amount_complete(y,surface->get_h()))
321                                         return false;
322                         }
323                 }
324         }
325
326         if(cb && !cb->amount_complete(10000,10000)) return false;
327
328         return true;
329 }
330
331 Rect
332 Rotate::get_full_bounding_rect(Context context)const
333 {
334         Rect under(context.get_full_bounding_rect());
335         return get_transform()->perform(under);
336 }
337