1 /*! ========================================================================
4 ** $Id: rotate.cpp,v 1.2 2005/01/24 05:00:18 darco Exp $
6 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
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.
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.
18 ** === N O T E S ===========================================================
20 ** ========================================================================= */
22 /* === H E A D E R S ======================================================= */
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>
45 /* === M A C R O S ========================================================= */
47 /* === G L O B A L S ======================================================= */
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 $");
56 /* === P R O C E D U R E S ================================================= */
58 /* === M E T H O D S ======================================================= */
60 /* === E N T R Y P O I N T ================================================= */
64 amount (Angle::deg(0)),
75 Rotate::set_param(const String & param, const ValueBase &value)
79 if(param=="amount" && value.same_as(amount))
81 amount=value.get(amount);
82 sin_val=Angle::sin(amount).get();
83 cos_val=Angle::cos(amount).get();
91 Rotate::get_param(const String ¶m)const
103 Rotate::get_param_vocab()const
107 ret.push_back(ParamDesc("origin")
108 .set_local_name(_("Origin"))
109 .set_description(_("Point where you want the origin to be"))
112 ret.push_back(ParamDesc("amount")
113 .set_local_name(_("Amount"))
114 .set_description(_("Amount of rotation"))
115 .set_origin("origin")
121 class Rotate_Trans : public Transform
123 etl::handle<const Rotate> layer;
125 Rotate_Trans(const Rotate* x):Transform(x->get_guid()),layer(x) { }
127 synfig::Vector perform(const synfig::Vector& x)const
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;
133 synfig::Vector unperform(const synfig::Vector& x)const
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;
139 etl::handle<Transform>
140 Rotate::get_transform()const
142 return new Rotate_Trans(this);
145 synfig::Layer::Handle
146 Rotate::hit_check(synfig::Context context, const synfig::Point &p)const
149 Point newpos(cos_val*pos[0]+sin_val*pos[1],-sin_val*pos[0]+cos_val*pos[1]);
151 return context.hit_check(newpos);
155 Rotate::get_color(Context context, const Point &p)const
158 Point newpos(cos_val*pos[0]+sin_val*pos[1],-sin_val*pos[0]+cos_val*pos[1]);
160 return context.get_color(newpos);
164 Rotate::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
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))
170 RendDesc desc(renddesc);
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);
180 SuperCallback stageone(cb,0,9000,10000);
181 SuperCallback stagetwo(cb,9000,10000,10000);
183 if(cb && !cb->amount_complete(0,10000))
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]);
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]));
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]);
224 RendDesc desc(renddesc);
226 //desc.set_flags(RendDesc::PX_ASPECT);
229 desc.set_wh(round_to_int(pw*(br[0]-tl[0])),round_to_int(ph*(br[1]-tl[1])));
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());
235 source.set_wh(desc.get_w(),desc.get_h());
237 if(!context.accelerated_render(&source,quality,desc,&stageone))
240 surface->set_wh(renddesc.get_w(),renddesc.get_h());
242 Surface::pen pen(surface->begin());
247 int x,y;//,u,v,u2,v2;
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)
251 for(x=0,point[0]=renddesc.get_tl()[0];x<surface->get_w();x++,pen.inc_x(),point[0]+=1.0/pw)
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);
258 if(!stagetwo.amount_complete(y,surface->get_h()))
266 // INTERPOLATION_LINEAR
267 int x,y;//,u,v,u2,v2;
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)
271 for(x=0,point[0]=renddesc.get_tl()[0];x<surface->get_w();x++,pen.inc_x(),point[0]+=1.0/pw)
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);
278 if(!stagetwo.amount_complete(y,surface->get_h()))
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)
290 for(x=0,point[0]=renddesc.get_tl()[0];x<surface->get_w();x++,pen.inc_x(),point[0]+=1.0/pw)
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);
299 if(u>=source.get_w())
301 if(v>=source.get_h())
303 //pen.set_value(source[v][u]);
304 (*surface)[y][x]=source[v][u];
308 if(!stagetwo.amount_complete(y,surface->get_h()))
314 if(cb && !cb->amount_complete(10000,10000)) return false;
320 Rotate::get_full_bounding_rect(Context context)const
322 Rect under(context.get_full_bounding_rect());
323 return get_transform()->perform(under);