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