1 /*! ========================================================================
4 ** $Id: rotate.cpp,v 1.2 2005/01/24 05:00:18 darco Exp $
6 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
8 ** This software and associated documentation
9 ** are CONFIDENTIAL and PROPRIETARY property of
10 ** the above-mentioned copyright holder.
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.
17 ** === N O T E S ===========================================================
19 ** ========================================================================= */
21 /* === H E A D E R S ======================================================= */
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>
44 /* === M A C R O S ========================================================= */
46 /* === G L O B A L S ======================================================= */
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 $");
55 /* === P R O C E D U R E S ================================================= */
57 /* === M E T H O D S ======================================================= */
59 /* === E N T R Y P O I N T ================================================= */
63 amount (Angle::deg(0)),
74 Rotate::set_param(const String & param, const ValueBase &value)
78 if(param=="amount" && value.same_as(amount))
80 amount=value.get(amount);
81 sin_val=Angle::sin(amount).get();
82 cos_val=Angle::cos(amount).get();
90 Rotate::get_param(const String ¶m)const
102 Rotate::get_param_vocab()const
106 ret.push_back(ParamDesc("origin")
107 .set_local_name(_("Origin"))
108 .set_description(_("Point where you want the origin to be"))
111 ret.push_back(ParamDesc("amount")
112 .set_local_name(_("Amount"))
113 .set_description(_("Amount of rotation"))
114 .set_origin("origin")
120 class Rotate_Trans : public Transform
122 etl::handle<const Rotate> layer;
124 Rotate_Trans(const Rotate* x):Transform(x->get_guid()),layer(x) { }
126 synfig::Vector perform(const synfig::Vector& x)const
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;
132 synfig::Vector unperform(const synfig::Vector& x)const
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;
138 etl::handle<Transform>
139 Rotate::get_transform()const
141 return new Rotate_Trans(this);
144 synfig::Layer::Handle
145 Rotate::hit_check(synfig::Context context, const synfig::Point &p)const
148 Point newpos(cos_val*pos[0]+sin_val*pos[1],-sin_val*pos[0]+cos_val*pos[1]);
150 return context.hit_check(newpos);
154 Rotate::get_color(Context context, const Point &p)const
157 Point newpos(cos_val*pos[0]+sin_val*pos[1],-sin_val*pos[0]+cos_val*pos[1]);
159 return context.get_color(newpos);
163 Rotate::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
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))
169 RendDesc desc(renddesc);
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);
179 SuperCallback stageone(cb,0,9000,10000);
180 SuperCallback stagetwo(cb,9000,10000,10000);
182 if(cb && !cb->amount_complete(0,10000))
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]);
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]));
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]);
223 RendDesc desc(renddesc);
225 //desc.set_flags(RendDesc::PX_ASPECT);
228 desc.set_wh(round_to_int(pw*(br[0]-tl[0])),round_to_int(ph*(br[1]-tl[1])));
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());
234 source.set_wh(desc.get_w(),desc.get_h());
236 if(!context.accelerated_render(&source,quality,desc,&stageone))
239 surface->set_wh(renddesc.get_w(),renddesc.get_h());
241 Surface::pen pen(surface->begin());
246 int x,y;//,u,v,u2,v2;
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)
250 for(x=0,point[0]=renddesc.get_tl()[0];x<surface->get_w();x++,pen.inc_x(),point[0]+=1.0/pw)
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);
257 if(!stagetwo.amount_complete(y,surface->get_h()))
265 // INTERPOLATION_LINEAR
266 int x,y;//,u,v,u2,v2;
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)
270 for(x=0,point[0]=renddesc.get_tl()[0];x<surface->get_w();x++,pen.inc_x(),point[0]+=1.0/pw)
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);
277 if(!stagetwo.amount_complete(y,surface->get_h()))
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)
289 for(x=0,point[0]=renddesc.get_tl()[0];x<surface->get_w();x++,pen.inc_x(),point[0]+=1.0/pw)
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);
298 if(u>=source.get_w())
300 if(v>=source.get_h())
302 //pen.set_value(source[v][u]);
303 (*surface)[y][x]=source[v][u];
307 if(!stagetwo.amount_complete(y,surface->get_h()))
313 if(cb && !cb->amount_complete(10000,10000)) return false;
319 Rotate::get_full_bounding_rect(Context context)const
321 Rect under(context.get_full_bounding_rect());
322 return get_transform()->perform(under);