1 /* === S Y N F I G ========================================================= */
3 ** \brief Template File
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.
19 ** === N O T E S ===========================================================
21 ** ========================================================================= */
23 /* === H E A D E R S ======================================================= */
33 #include <synfig/string.h>
34 #include <synfig/time.h>
35 #include <synfig/context.h>
36 #include <synfig/paramdesc.h>
37 #include <synfig/renddesc.h>
38 #include <synfig/surface.h>
39 #include <synfig/value.h>
40 #include <synfig/valuenode.h>
41 #include <synfig/transform.h>
46 /* === M A C R O S ========================================================= */
48 /* === G L O B A L S ======================================================= */
50 SYNFIG_LAYER_INIT(Warp);
51 SYNFIG_LAYER_SET_NAME(Warp,"warp");
52 SYNFIG_LAYER_SET_LOCAL_NAME(Warp,_("Warp"));
53 SYNFIG_LAYER_SET_CATEGORY(Warp,_("Distortions"));
54 SYNFIG_LAYER_SET_VERSION(Warp,"0.1");
55 SYNFIG_LAYER_SET_CVS_ID(Warp,"$Id: warp.cpp,v 1.2 2005/01/24 05:00:18 darco Exp $");
57 /* === P R O C E D U R E S ================================================= */
59 /* === M E T H O D S ======================================================= */
61 /* === E N T R Y P O I N T ================================================= */
81 Warp::transform_forward(const Point& p)const
84 (inv_matrix[0][0]*p[0] + inv_matrix[0][1]*p[1] + inv_matrix[0][2])/(inv_matrix[2][0]*p[0] + inv_matrix[2][1]*p[1] + inv_matrix[2][2]),
85 (inv_matrix[1][0]*p[0] + inv_matrix[1][1]*p[1] + inv_matrix[1][2])/(inv_matrix[2][0]*p[0] + inv_matrix[2][1]*p[1] + inv_matrix[2][2])
90 Warp::transform_backward(const Point& p)const
93 (matrix[0][0]*p[0] + matrix[0][1]*p[1] + matrix[0][2])/(matrix[2][0]*p[0] + matrix[2][1]*p[1] + matrix[2][2]),
94 (matrix[1][0]*p[0] + matrix[1][1]*p[1] + matrix[1][2])/(matrix[2][0]*p[0] + matrix[2][1]*p[1] + matrix[2][2])
99 Warp::transform_forward_z(const Point& p)const
101 return inv_matrix[2][0]*p[0] + inv_matrix[2][1]*p[1] + inv_matrix[2][2];
105 Warp::transform_backward_z(const Point& p)const
107 return matrix[2][0]*p[0] + matrix[2][1]*p[1] + matrix[2][2];
111 #define transform_forward(p) Point( \
112 cache_a*p[0] + cache_b*p[1] + cache_c*p[0]*p[1] + cache_d, \
113 cache_e*p[0] + cache_f*p[1] + cache_i*p[0]*p[1] + cache_j )
115 #define transform_backward(p) Point( \
116 cache_a*p[0] + cache_b*p[1] + cache_c*p[0]*p[1] + cache_d, \
117 cache_e*p[0] + cache_f*p[1] + cache_i*p[0]*p[1] + cache_j )
120 #define triangle_area(a,b,c) (0.5*(-b[0]*a[1]+c[0]*a[1]+a[0]*b[1]-c[0]*b[1]-a[0]*c[1]+b[0]*c[1]))
121 #define quad_area(a,b,c,d) (triangle_area(a,b,c)+triangle_area(a,c,d))
123 Real mat3_determinant(Real matrix[3][3])
127 ret = (matrix[0][0] *
128 (matrix[1][1] * matrix[2][2] -
129 matrix[1][2] * matrix[2][1]));
130 ret -= (matrix[1][0] *
131 (matrix[0][1] * matrix[2][2] -
132 matrix[0][2] * matrix[2][1]));
133 ret += (matrix[2][0] *
134 (matrix[0][1] * matrix[1][2] -
135 matrix[0][2] * matrix[1][1]));
140 void mat3_invert(Real in[3][3], Real out[3][3])
142 Real det(mat3_determinant(in));
149 out[0][0] = (in[1][1] * in[2][2] -
150 in[1][2] * in[2][1]) * det;
152 out[1][0] = - (in[1][0] * in[2][2] -
153 in[1][2] * in[2][0]) * det;
155 out[2][0] = (in[1][0] * in[2][1] -
156 in[1][1] * in[2][0]) * det;
158 out[0][1] = - (in[0][1] * in[2][2] -
159 in[0][2] * in[2][1]) * det;
161 out[1][1] = (in[0][0] * in[2][2] -
162 in[0][2] * in[2][0]) * det;
164 out[2][1] = - (in[0][0] * in[2][1] -
165 in[0][1] * in[2][0]) * det;
167 out[0][2] = (in[0][1] * in[1][2] -
168 in[0][2] * in[1][1]) * det;
170 out[1][2] = - (in[0][0] * in[1][2] -
171 in[0][2] * in[1][0]) * det;
173 out[2][2] = (in[0][0] * in[1][1] -
174 in[0][1] * in[1][0]) * det;
181 /* cache_a=(-dest_tl[0]+dest_tr[0])/(src_br[1]-src_tl[1]);
182 cache_b=(-dest_tl[0]+dest_bl[0])/(src_br[0]-src_tl[0]);
183 cache_c=(dest_tl[0]-dest_tr[0]+dest_br[0]-dest_bl[0])/((src_br[1]-src_tl[1])*(src_br[0]-src_tl[0]));
186 cache_e=(-dest_tl[1]+dest_tr[1])/(src_br[0]-src_tl[0]);
187 cache_f=(-dest_tl[1]+dest_bl[1])/(src_br[1]-src_tl[1]);
188 cache_i=(dest_tl[1]-dest_tr[1]+dest_br[1]-dest_bl[1])/((src_br[1]-src_tl[1])*(src_br[0]-src_tl[0]));
192 /* matrix[2][0]=(dest_tl[0]-dest_tr[0]+dest_br[0]-dest_bl[0])/((src_br[1]-src_tl[1])*(src_br[0]-src_tl[0]));
193 matrix[2][1]=(dest_tl[1]-dest_tr[1]+dest_br[1]-dest_bl[1])/((src_br[1]-src_tl[1])*(src_br[0]-src_tl[0]));
194 matrix[2][2]=quad_area(dest_tl,dest_tr,dest_br,dest_bl)/((src_br[1]-src_tl[1])*(src_br[0]-src_tl[0]));
196 matrix[0][0]=-(-dest_tl[1]+dest_tr[1])/(src_br[0]-src_tl[0]);
197 matrix[0][1]=-(-dest_tl[1]+dest_bl[1])/(src_br[1]-src_tl[1]);
199 matrix[1][0]=-(-dest_tl[0]+dest_tr[0])/(src_br[1]-src_tl[1]);
200 matrix[1][1]=-(-dest_tl[0]+dest_bl[0])/(src_br[0]-src_tl[0]);
202 matrix[0][2]=matrix[0][0]*dest_tl[0] + matrix[0][1]*dest_tl[1];
203 matrix[1][2]=matrix[1][0]*dest_tl[0] + matrix[1][1]*dest_tl[1];
209 const Real& x1(min(src_br[0],src_tl[0]));
210 const Real& y1(min(src_br[1],src_tl[1]));
211 const Real& x2(max(src_br[0],src_tl[0]));
212 const Real& y2(max(src_br[1],src_tl[1]));
214 Real tx1(dest_bl[0]);
215 Real ty1(dest_bl[1]);
216 Real tx2(dest_br[0]);
217 Real ty2(dest_br[1]);
218 Real tx3(dest_tl[0]);
219 Real ty3(dest_tl[1]);
220 Real tx4(dest_tr[0]);
221 Real ty4(dest_tr[1]);
223 if(src_br[0]<src_tl[0])
224 swap(tx3,tx4),swap(ty3,ty4),swap(tx1,tx2),swap(ty1,ty2);
226 if(src_br[1]>src_tl[1])
227 swap(tx3,tx1),swap(ty3,ty1),swap(tx4,tx2),swap(ty4,ty2);
232 scalex = scaley = 1.0;
235 scalex = 1.0 / (Real) (x2 - x1);
238 scaley = 1.0 / (Real) (y2 - y1);
240 /* Determine the perspective transform that maps from
241 * the unit cube to the transformed coordinates
244 Real dx1, dx2, dx3, dy1, dy2, dy3;
248 dx3 = tx1 - tx2 + tx4 - tx3;
252 dy3 = ty1 - ty2 + ty4 - ty3;
254 /* Is the mapping affine? */
255 if ((dx3 == 0.0) && (dy3 == 0.0))
257 matrix[0][0] = tx2 - tx1;
258 matrix[0][1] = tx4 - tx2;
260 matrix[1][0] = ty2 - ty1;
261 matrix[1][1] = ty4 - ty2;
270 det1 = dx3 * dy2 - dy3 * dx2;
271 det2 = dx1 * dy2 - dy1 * dx2;
273 if (det1 == 0.0 && det2 == 0.0)
276 matrix[2][0] = det1 / det2;
278 det1 = dx1 * dy3 - dy1 * dx3;
280 if (det1 == 0.0 && det2 == 0.0)
283 matrix[2][1] = det1 / det2;
285 matrix[0][0] = tx2 - tx1 + matrix[2][0] * tx2;
286 matrix[0][1] = tx3 - tx1 + matrix[2][1] * tx3;
289 matrix[1][0] = ty2 - ty1 + matrix[2][0] * ty2;
290 matrix[1][1] = ty3 - ty1 + matrix[2][1] * ty3;
298 Real scaletrans[3][3]={
299 { scalex, 0, -x1*scalex },
300 { 0, scaley, -y1*scaley },
306 for (int i = 0; i < 3; i++)
312 for (int j = 0; j < 3; j++)
314 matrix[i][j] = t1 * scaletrans[0][j];
315 matrix[i][j] += t2 * scaletrans[1][j];
316 matrix[i][j] += t3 * scaletrans[2][j];
320 mat3_invert(matrix, inv_matrix);
322 gimp_matrix3_identity (result);
323 gimp_matrix3_translate (result, -x1, -y1);
324 gimp_matrix3_scale (result, scalex, scaley);
325 gimp_matrix3_mult (&matrix, result);
330 Warp::set_param(const String & param, const ValueBase &value)
332 IMPORT_PLUS(src_tl,sync());
333 IMPORT_PLUS(src_br,sync());
334 IMPORT_PLUS(dest_tl,sync());
335 IMPORT_PLUS(dest_tr,sync());
336 IMPORT_PLUS(dest_bl,sync());
337 IMPORT_PLUS(dest_br,sync());
345 Warp::get_param(const String ¶m)const
363 Warp::get_param_vocab()const
367 ret.push_back(ParamDesc("src_tl")
368 .set_local_name(_("Source TL"))
372 ret.push_back(ParamDesc("src_br")
373 .set_local_name(_("Source BR"))
376 ret.push_back(ParamDesc("dest_tl")
377 .set_local_name(_("Dest TL"))
378 .set_connect("dest_tr")
381 ret.push_back(ParamDesc("dest_tr")
382 .set_local_name(_("Dest TR"))
383 .set_connect("dest_br")
386 ret.push_back(ParamDesc("dest_br")
387 .set_local_name(_("Dest BR"))
388 .set_connect("dest_bl")
391 ret.push_back(ParamDesc("dest_bl")
392 .set_local_name(_("Dest BL"))
393 .set_connect("dest_tl")
396 ret.push_back(ParamDesc("clip")
397 .set_local_name(_("Clip"))
400 ret.push_back(ParamDesc("horizon")
401 .set_local_name(_("Horizon"))
408 class Warp_Trans : public Transform
410 etl::handle<const Warp> layer;
412 Warp_Trans(const Warp* x):Transform(x->get_guid()),layer(x) { }
414 synfig::Vector perform(const synfig::Vector& x)const
416 return layer->transform_backward(x);
417 //Point pos(x-layer->origin);
418 //return Point(layer->cos_val*pos[0]-layer->sin_val*pos[1],layer->sin_val*pos[0]+layer->cos_val*pos[1])+layer->origin;
421 synfig::Vector unperform(const synfig::Vector& x)const
424 return layer->transform_forward(x);
425 //Point pos(x-layer->origin);
426 //return Point(layer->cos_val*pos[0]+layer->sin_val*pos[1],-layer->sin_val*pos[0]+layer->cos_val*pos[1])+layer->origin;
429 etl::handle<Transform>
430 Warp::get_transform()const
432 return new Warp_Trans(this);
435 synfig::Layer::Handle
436 Warp::hit_check(synfig::Context context, const synfig::Point &p)const
438 Point newpos(transform_forward(p));
442 Rect rect(src_tl,src_br);
443 if(!rect.is_inside(newpos))
447 return context.hit_check(newpos);
451 Warp::get_color(Context context, const Point &p)const
453 Point newpos(transform_forward(p));
457 Rect rect(src_tl,src_br);
458 if(!rect.is_inside(newpos))
459 return Color::alpha();
462 const float z(transform_backward_z(newpos));
464 return context.get_color(newpos);
466 return Color::alpha();
469 //#define ACCEL_WARP_IS_BROKEN 1
472 Warp::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
474 SuperCallback stageone(cb,0,9000,10000);
475 SuperCallback stagetwo(cb,9000,10000,10000);
477 Real pw=(renddesc.get_w())/(renddesc.get_br()[0]-renddesc.get_tl()[0]);
478 Real ph=(renddesc.get_h())/(renddesc.get_br()[1]-renddesc.get_tl()[1]);
480 if(cb && !cb->amount_complete(0,10000))
483 Point tl(renddesc.get_tl());
484 Point br(renddesc.get_br());
488 Rect render_rect(tl,br);
489 Rect clip_rect(Rect::full_plane());
490 Rect dest_rect(dest_tl,dest_br); dest_rect.expand(dest_tr).expand(dest_bl);
492 Real zoom_factor(1.0);
494 // Quick exclusion clip, if necessary
495 if(clip && !intersect(render_rect,dest_rect))
497 surface->set_wh(renddesc.get_w(),renddesc.get_h());
503 Rect other(render_rect);
507 Point min(other.get_min());
508 Point max(other.get_max());
510 bool init_point_set=false;
512 // Point trans_point[4];
515 Real z,minz(10000000000000.0f),maxz(0);
518 p=transform_forward(min);
519 z=transform_backward_z(p);
520 if(z>0 && z<horizon*2)
523 bounding_rect.expand(p);
525 bounding_rect=Rect(p);
527 maxz=std::max(maxz,z);
528 minz=std::min(minz,z);
531 p=transform_forward(max);
532 z=transform_backward_z(p);
533 if(z>0 && z<horizon*2)
536 bounding_rect.expand(p);
538 bounding_rect=Rect(p);
540 maxz=std::max(maxz,z);
541 minz=std::min(minz,z);
546 p=transform_forward(min);
547 z=transform_backward_z(p);
548 if(z>0 && z<horizon*2)
551 bounding_rect.expand(p);
553 bounding_rect=Rect(p);
555 maxz=std::max(maxz,z);
556 minz=std::min(minz,z);
559 p=transform_forward(max);
560 z=transform_backward_z(p);
561 if(z>0 && z<horizon*2)
564 bounding_rect.expand(p);
566 bounding_rect=Rect(p);
568 maxz=std::max(maxz,z);
569 minz=std::min(minz,z);
574 surface->set_wh(renddesc.get_w(),renddesc.get_h());
578 zoom_factor=(1+(maxz-minz));
582 #ifdef ACCEL_WARP_IS_BROKEN
583 return Layer::accelerated_render(context,surface,quality,renddesc, cb);
588 .expand(transform_forward(tl))
589 .expand(transform_forward(br))
593 //synfig::warning("given window: [%f,%f]-[%f,%f] %dx%d",tl[0],tl[1],br[0],br[1],renddesc.get_w(),renddesc.get_h());
594 //synfig::warning("Projected: [%f,%f]-[%f,%f]",bounding_rect.get_min()[0],bounding_rect.get_min()[1],bounding_rect.get_max()[0],bounding_rect.get_max()[1]);
596 // If we are clipping, then go ahead and clip to the
599 clip_rect&=Rect(src_tl,src_br);
601 // Bound ourselves to the bounding rectangle of
603 clip_rect&=context.get_full_bounding_rect();//.expand_x(abs(zoom_factor/pw)).expand_y(abs(zoom_factor/ph));
605 bounding_rect&=clip_rect;
607 Point min_point(bounding_rect.get_min());
608 Point max_point(bounding_rect.get_max());
634 const int tmp_d(max(renddesc.get_w(),renddesc.get_h()));
635 Real src_pw=(tmp_d*zoom_factor)/(br[0]-tl[0]);
636 Real src_ph=(tmp_d*zoom_factor)/(br[1]-tl[1]);
639 RendDesc desc(renddesc);
641 //desc.set_flags(RendDesc::PX_ASPECT);
644 desc.set_wh(ceil_to_int(src_pw*(br[0]-tl[0])),ceil_to_int(src_ph*(br[1]-tl[1])));
646 //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());
647 if(desc.get_w()==0 && desc.get_h()==0)
649 surface->set_wh(renddesc.get_w(),renddesc.get_h());
654 // Recalculate the pixel widths for the src renddesc
655 src_pw=(desc.get_w())/(desc.get_br()[0]-desc.get_tl()[0]);
656 src_ph=(desc.get_h())/(desc.get_br()[1]-desc.get_tl()[1]);
660 source.set_wh(desc.get_w(),desc.get_h());
662 if(!context.accelerated_render(&source,quality,desc,&stageone))
665 surface->set_wh(renddesc.get_w(),renddesc.get_h());
668 Surface::pen pen(surface->begin());
676 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)
678 for(x=0,point[0]=renddesc.get_tl()[0];x<surface->get_w();x++,pen.inc_x(),point[0]+=1.0/pw)
680 tmp=transform_forward(point);
681 const float z(transform_backward_z(tmp));
682 if(!clip_rect.is_inside(tmp) || !(z>0 && z<horizon))
684 (*surface)[y][x]=Color::alpha();
688 u=(tmp[0]-tl[0])*src_pw;
689 v=(tmp[1]-tl[1])*src_ph;
691 if(u<0 || v<0 || u>=source.get_w() || v>=source.get_h() || isnan(u) || isnan(v))
693 (*surface)[y][x]=context.get_color(tmp);
696 (*surface)[y][x]=source.cubic_sample(u,v);
700 if(!stagetwo.amount_complete(y,surface->get_h()))
708 // INTERPOLATION_LINEAR
712 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)
714 for(x=0,point[0]=renddesc.get_tl()[0];x<surface->get_w();x++,pen.inc_x(),point[0]+=1.0/pw)
716 tmp=transform_forward(point);
717 const float z(transform_backward_z(tmp));
718 if(!clip_rect.is_inside(tmp) || !(z>0 && z<horizon))
720 (*surface)[y][x]=Color::alpha();
724 u=(tmp[0]-tl[0])*src_pw;
725 v=(tmp[1]-tl[1])*src_ph;
727 if(u<0 || v<0 || u>=source.get_w() || v>=source.get_h() || isnan(u) || isnan(v))
730 (*surface)[y][x]=Color::alpha();
732 (*surface)[y][x]=context.get_color(tmp);
735 (*surface)[y][x]=source.linear_sample(u,v);
739 if(!stagetwo.amount_complete(y,surface->get_h()))
750 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)
752 for(x=0,point[0]=renddesc.get_tl()[0];x<surface->get_w();x++,pen.inc_x(),point[0]+=1.0/pw)
754 tmp=transform_forward(point);
755 const float z(transform_backward_z(tmp));
756 if(!clip_rect.is_inside(tmp) || !(z>0 && z<horizon))
758 (*surface)[y][x]=Color::alpha();
762 u=(tmp[0]-tl[0])*src_pw;
763 v=(tmp[1]-tl[1])*src_ph;
765 if(u<0 || v<0 || u>=source.get_w() || v>=source.get_h() || isnan(u) || isnan(v))
768 (*surface)[y][x]=Color::alpha();
770 (*surface)[y][x]=context.get_color(tmp);
773 //pen.set_value(source[v][u]);
774 (*surface)[y][x]=source[floor_to_int(v)][floor_to_int(u)];
778 if(!stagetwo.amount_complete(y,surface->get_h()))
786 if(cb && !cb->amount_complete(10000,10000)) return false;
792 Warp::get_bounding_rect()const
794 return Rect::full_plane();
798 Warp::get_full_bounding_rect(Context context)const
800 // return Rect::full_plane();
802 Rect under(context.get_full_bounding_rect());
806 under&=Rect(src_tl,src_br);
809 return get_transform()->perform(under);
812 Rect under(context.get_full_bounding_rect());
813 Rect ret(Rect::zero());
815 if(under.area()==HUGE_VAL)
816 return Rect::full_plane();
845 if(ret.area()==HUGE_VAL)
846 return Rect::full_plane();