1 /*! ========================================================================
4 ** $Id: warp.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(Warp);
49 SYNFIG_LAYER_SET_NAME(Warp,"warp");
50 SYNFIG_LAYER_SET_LOCAL_NAME(Warp,_("Warp"));
51 SYNFIG_LAYER_SET_CATEGORY(Warp,_("Distortions"));
52 SYNFIG_LAYER_SET_VERSION(Warp,"0.1");
53 SYNFIG_LAYER_SET_CVS_ID(Warp,"$Id: warp.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 ================================================= */
79 Warp::transform_forward(const Point& p)const
82 (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]),
83 (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])
88 Warp::transform_backward(const Point& p)const
91 (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]),
92 (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])
97 Warp::transform_forward_z(const Point& p)const
99 return inv_matrix[2][0]*p[0] + inv_matrix[2][1]*p[1] + inv_matrix[2][2];
103 Warp::transform_backward_z(const Point& p)const
105 return matrix[2][0]*p[0] + matrix[2][1]*p[1] + matrix[2][2];
109 #define transform_forward(p) Point( \
110 cache_a*p[0] + cache_b*p[1] + cache_c*p[0]*p[1] + cache_d, \
111 cache_e*p[0] + cache_f*p[1] + cache_i*p[0]*p[1] + cache_j )
113 #define transform_backward(p) Point( \
114 cache_a*p[0] + cache_b*p[1] + cache_c*p[0]*p[1] + cache_d, \
115 cache_e*p[0] + cache_f*p[1] + cache_i*p[0]*p[1] + cache_j )
118 #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]))
119 #define quad_area(a,b,c,d) (triangle_area(a,b,c)+triangle_area(a,c,d))
121 Real mat3_determinant(Real matrix[3][3])
125 ret = (matrix[0][0] *
126 (matrix[1][1] * matrix[2][2] -
127 matrix[1][2] * matrix[2][1]));
128 ret -= (matrix[1][0] *
129 (matrix[0][1] * matrix[2][2] -
130 matrix[0][2] * matrix[2][1]));
131 ret += (matrix[2][0] *
132 (matrix[0][1] * matrix[1][2] -
133 matrix[0][2] * matrix[1][1]));
138 void mat3_invert(Real in[3][3], Real out[3][3])
140 Real det(mat3_determinant(in));
147 out[0][0] = (in[1][1] * in[2][2] -
148 in[1][2] * in[2][1]) * det;
150 out[1][0] = - (in[1][0] * in[2][2] -
151 in[1][2] * in[2][0]) * det;
153 out[2][0] = (in[1][0] * in[2][1] -
154 in[1][1] * in[2][0]) * det;
156 out[0][1] = - (in[0][1] * in[2][2] -
157 in[0][2] * in[2][1]) * det;
159 out[1][1] = (in[0][0] * in[2][2] -
160 in[0][2] * in[2][0]) * det;
162 out[2][1] = - (in[0][0] * in[2][1] -
163 in[0][1] * in[2][0]) * det;
165 out[0][2] = (in[0][1] * in[1][2] -
166 in[0][2] * in[1][1]) * det;
168 out[1][2] = - (in[0][0] * in[1][2] -
169 in[0][2] * in[1][0]) * det;
171 out[2][2] = (in[0][0] * in[1][1] -
172 in[0][1] * in[1][0]) * det;
179 /* cache_a=(-dest_tl[0]+dest_tr[0])/(src_br[1]-src_tl[1]);
180 cache_b=(-dest_tl[0]+dest_bl[0])/(src_br[0]-src_tl[0]);
181 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]));
184 cache_e=(-dest_tl[1]+dest_tr[1])/(src_br[0]-src_tl[0]);
185 cache_f=(-dest_tl[1]+dest_bl[1])/(src_br[1]-src_tl[1]);
186 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]));
190 /* 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]));
191 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]));
192 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]));
194 matrix[0][0]=-(-dest_tl[1]+dest_tr[1])/(src_br[0]-src_tl[0]);
195 matrix[0][1]=-(-dest_tl[1]+dest_bl[1])/(src_br[1]-src_tl[1]);
197 matrix[1][0]=-(-dest_tl[0]+dest_tr[0])/(src_br[1]-src_tl[1]);
198 matrix[1][1]=-(-dest_tl[0]+dest_bl[0])/(src_br[0]-src_tl[0]);
200 matrix[0][2]=matrix[0][0]*dest_tl[0] + matrix[0][1]*dest_tl[1];
201 matrix[1][2]=matrix[1][0]*dest_tl[0] + matrix[1][1]*dest_tl[1];
207 const Real& x1(min(src_br[0],src_tl[0]));
208 const Real& y1(min(src_br[1],src_tl[1]));
209 const Real& x2(max(src_br[0],src_tl[0]));
210 const Real& y2(max(src_br[1],src_tl[1]));
212 Real tx1(dest_bl[0]);
213 Real ty1(dest_bl[1]);
214 Real tx2(dest_br[0]);
215 Real ty2(dest_br[1]);
216 Real tx3(dest_tl[0]);
217 Real ty3(dest_tl[1]);
218 Real tx4(dest_tr[0]);
219 Real ty4(dest_tr[1]);
221 if(src_br[0]<src_tl[0])
222 swap(tx3,tx4),swap(ty3,ty4),swap(tx1,tx2),swap(ty1,ty2);
224 if(src_br[1]>src_tl[1])
225 swap(tx3,tx1),swap(ty3,ty1),swap(tx4,tx2),swap(ty4,ty2);
230 scalex = scaley = 1.0;
233 scalex = 1.0 / (Real) (x2 - x1);
236 scaley = 1.0 / (Real) (y2 - y1);
238 /* Determine the perspective transform that maps from
239 * the unit cube to the transformed coordinates
242 Real dx1, dx2, dx3, dy1, dy2, dy3;
246 dx3 = tx1 - tx2 + tx4 - tx3;
250 dy3 = ty1 - ty2 + ty4 - ty3;
252 /* Is the mapping affine? */
253 if ((dx3 == 0.0) && (dy3 == 0.0))
255 matrix[0][0] = tx2 - tx1;
256 matrix[0][1] = tx4 - tx2;
258 matrix[1][0] = ty2 - ty1;
259 matrix[1][1] = ty4 - ty2;
268 det1 = dx3 * dy2 - dy3 * dx2;
269 det2 = dx1 * dy2 - dy1 * dx2;
271 if (det1 == 0.0 && det2 == 0.0)
274 matrix[2][0] = det1 / det2;
276 det1 = dx1 * dy3 - dy1 * dx3;
278 if (det1 == 0.0 && det2 == 0.0)
281 matrix[2][1] = det1 / det2;
283 matrix[0][0] = tx2 - tx1 + matrix[2][0] * tx2;
284 matrix[0][1] = tx3 - tx1 + matrix[2][1] * tx3;
287 matrix[1][0] = ty2 - ty1 + matrix[2][0] * ty2;
288 matrix[1][1] = ty3 - ty1 + matrix[2][1] * ty3;
296 Real scaletrans[3][3]={
297 { scalex, 0, -x1*scalex },
298 { 0, scaley, -y1*scaley },
304 for (int i = 0; i < 3; i++)
310 for (int j = 0; j < 3; j++)
312 matrix[i][j] = t1 * scaletrans[0][j];
313 matrix[i][j] += t2 * scaletrans[1][j];
314 matrix[i][j] += t3 * scaletrans[2][j];
318 mat3_invert(matrix, inv_matrix);
320 gimp_matrix3_identity (result);
321 gimp_matrix3_translate (result, -x1, -y1);
322 gimp_matrix3_scale (result, scalex, scaley);
323 gimp_matrix3_mult (&matrix, result);
328 Warp::set_param(const String & param, const ValueBase &value)
330 IMPORT_PLUS(src_tl,sync());
331 IMPORT_PLUS(src_br,sync());
332 IMPORT_PLUS(dest_tl,sync());
333 IMPORT_PLUS(dest_tr,sync());
334 IMPORT_PLUS(dest_bl,sync());
335 IMPORT_PLUS(dest_br,sync());
343 Warp::get_param(const String ¶m)const
361 Warp::get_param_vocab()const
365 ret.push_back(ParamDesc("src_tl")
366 .set_local_name(_("Source TL"))
370 ret.push_back(ParamDesc("src_br")
371 .set_local_name(_("Source BR"))
374 ret.push_back(ParamDesc("dest_tl")
375 .set_local_name(_("Dest TL"))
376 .set_connect("dest_tr")
379 ret.push_back(ParamDesc("dest_tr")
380 .set_local_name(_("Dest TR"))
381 .set_connect("dest_br")
384 ret.push_back(ParamDesc("dest_br")
385 .set_local_name(_("Dest BR"))
386 .set_connect("dest_bl")
389 ret.push_back(ParamDesc("dest_bl")
390 .set_local_name(_("Dest BL"))
391 .set_connect("dest_tl")
394 ret.push_back(ParamDesc("clip")
395 .set_local_name(_("Clip"))
398 ret.push_back(ParamDesc("horizon")
399 .set_local_name(_("Horizon"))
406 class Warp_Trans : public Transform
408 etl::handle<const Warp> layer;
410 Warp_Trans(const Warp* x):Transform(x->get_guid()),layer(x) { }
412 synfig::Vector perform(const synfig::Vector& x)const
414 return layer->transform_backward(x);
415 //Point pos(x-layer->origin);
416 //return Point(layer->cos_val*pos[0]-layer->sin_val*pos[1],layer->sin_val*pos[0]+layer->cos_val*pos[1])+layer->origin;
419 synfig::Vector unperform(const synfig::Vector& x)const
422 return layer->transform_forward(x);
423 //Point pos(x-layer->origin);
424 //return Point(layer->cos_val*pos[0]+layer->sin_val*pos[1],-layer->sin_val*pos[0]+layer->cos_val*pos[1])+layer->origin;
427 etl::handle<Transform>
428 Warp::get_transform()const
430 return new Warp_Trans(this);
433 synfig::Layer::Handle
434 Warp::hit_check(synfig::Context context, const synfig::Point &p)const
436 Point newpos(transform_forward(p));
440 Rect rect(src_tl,src_br);
441 if(!rect.is_inside(newpos))
445 return context.hit_check(newpos);
449 Warp::get_color(Context context, const Point &p)const
451 Point newpos(transform_forward(p));
455 Rect rect(src_tl,src_br);
456 if(!rect.is_inside(newpos))
457 return Color::alpha();
460 const float z(transform_backward_z(newpos));
462 return context.get_color(newpos);
464 return Color::alpha();
467 //#define ACCEL_WARP_IS_BROKEN 1
470 Warp::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
472 SuperCallback stageone(cb,0,9000,10000);
473 SuperCallback stagetwo(cb,9000,10000,10000);
475 Real pw=(renddesc.get_w())/(renddesc.get_br()[0]-renddesc.get_tl()[0]);
476 Real ph=(renddesc.get_h())/(renddesc.get_br()[1]-renddesc.get_tl()[1]);
478 if(cb && !cb->amount_complete(0,10000))
481 Point tl(renddesc.get_tl());
482 Point br(renddesc.get_br());
486 Rect render_rect(tl,br);
487 Rect clip_rect(Rect::full_plane());
488 Rect dest_rect(dest_tl,dest_br); dest_rect.expand(dest_tr).expand(dest_bl);
490 Real zoom_factor(1.0);
492 // Quick exclusion clip, if necessary
493 if(clip && !intersect(render_rect,dest_rect))
495 surface->set_wh(renddesc.get_w(),renddesc.get_h());
501 Rect other(render_rect);
505 Point min(other.get_min());
506 Point max(other.get_max());
508 bool init_point_set=false;
510 Point trans_point[4];
513 Real z,minz(10000000000000.0f),maxz(0);
516 p=transform_forward(min);
517 z=transform_backward_z(p);
518 if(z>0 && z<horizon*2)
521 bounding_rect.expand(p);
523 bounding_rect=Rect(p);
525 maxz=std::max(maxz,z);
526 minz=std::min(minz,z);
529 p=transform_forward(max);
530 z=transform_backward_z(p);
531 if(z>0 && z<horizon*2)
534 bounding_rect.expand(p);
536 bounding_rect=Rect(p);
538 maxz=std::max(maxz,z);
539 minz=std::min(minz,z);
544 p=transform_forward(min);
545 z=transform_backward_z(p);
546 if(z>0 && z<horizon*2)
549 bounding_rect.expand(p);
551 bounding_rect=Rect(p);
553 maxz=std::max(maxz,z);
554 minz=std::min(minz,z);
557 p=transform_forward(max);
558 z=transform_backward_z(p);
559 if(z>0 && z<horizon*2)
562 bounding_rect.expand(p);
564 bounding_rect=Rect(p);
566 maxz=std::max(maxz,z);
567 minz=std::min(minz,z);
572 surface->set_wh(renddesc.get_w(),renddesc.get_h());
576 zoom_factor=(1+(maxz-minz));
580 #ifdef ACCEL_WARP_IS_BROKEN
581 return Layer::accelerated_render(context,surface,quality,renddesc, cb);
586 .expand(transform_forward(tl))
587 .expand(transform_forward(br))
591 //synfig::warning("given window: [%f,%f]-[%f,%f] %dx%d",tl[0],tl[1],br[0],br[1],renddesc.get_w(),renddesc.get_h());
592 //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]);
594 // If we are clipping, then go ahead and clip to the
597 clip_rect&=Rect(src_tl,src_br);
599 // Bound ourselves to the bounding rectangle of
601 clip_rect&=context.get_full_bounding_rect();//.expand_x(abs(zoom_factor/pw)).expand_y(abs(zoom_factor/ph));
603 bounding_rect&=clip_rect;
605 Point min_point(bounding_rect.get_min());
606 Point max_point(bounding_rect.get_max());
632 const int tmp_d(max(renddesc.get_w(),renddesc.get_h()));
633 Real src_pw=(tmp_d*zoom_factor)/(br[0]-tl[0]);
634 Real src_ph=(tmp_d*zoom_factor)/(br[1]-tl[1]);
637 RendDesc desc(renddesc);
639 //desc.set_flags(RendDesc::PX_ASPECT);
642 desc.set_wh(ceil_to_int(src_pw*(br[0]-tl[0])),ceil_to_int(src_ph*(br[1]-tl[1])));
644 //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());
645 if(desc.get_w()==0 && desc.get_h()==0)
647 surface->set_wh(renddesc.get_w(),renddesc.get_h());
652 // Recalculate the pixel widths for the src renddesc
653 src_pw=(desc.get_w())/(desc.get_br()[0]-desc.get_tl()[0]);
654 src_ph=(desc.get_h())/(desc.get_br()[1]-desc.get_tl()[1]);
658 source.set_wh(desc.get_w(),desc.get_h());
660 if(!context.accelerated_render(&source,quality,desc,&stageone))
663 surface->set_wh(renddesc.get_w(),renddesc.get_h());
666 Surface::pen pen(surface->begin());
674 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)
676 for(x=0,point[0]=renddesc.get_tl()[0];x<surface->get_w();x++,pen.inc_x(),point[0]+=1.0/pw)
678 tmp=transform_forward(point);
679 const float z(transform_backward_z(tmp));
680 if(!clip_rect.is_inside(tmp) || !(z>0 && z<horizon))
682 (*surface)[y][x]=Color::alpha();
686 u=(tmp[0]-tl[0])*src_pw;
687 v=(tmp[1]-tl[1])*src_ph;
689 if(u<0 || v<0 || u>=source.get_w() || v>=source.get_h() || isnan(u) || isnan(v))
691 (*surface)[y][x]=context.get_color(tmp);
694 (*surface)[y][x]=source.cubic_sample(u,v);
698 if(!stagetwo.amount_complete(y,surface->get_h()))
706 // INTERPOLATION_LINEAR
710 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)
712 for(x=0,point[0]=renddesc.get_tl()[0];x<surface->get_w();x++,pen.inc_x(),point[0]+=1.0/pw)
714 tmp=transform_forward(point);
715 const float z(transform_backward_z(tmp));
716 if(!clip_rect.is_inside(tmp) || !(z>0 && z<horizon))
718 (*surface)[y][x]=Color::alpha();
722 u=(tmp[0]-tl[0])*src_pw;
723 v=(tmp[1]-tl[1])*src_ph;
725 if(u<0 || v<0 || u>=source.get_w() || v>=source.get_h() || isnan(u) || isnan(v))
728 (*surface)[y][x]=Color::alpha();
730 (*surface)[y][x]=context.get_color(tmp);
733 (*surface)[y][x]=source.linear_sample(u,v);
737 if(!stagetwo.amount_complete(y,surface->get_h()))
748 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)
750 for(x=0,point[0]=renddesc.get_tl()[0];x<surface->get_w();x++,pen.inc_x(),point[0]+=1.0/pw)
752 tmp=transform_forward(point);
753 const float z(transform_backward_z(tmp));
754 if(!clip_rect.is_inside(tmp) || !(z>0 && z<horizon))
756 (*surface)[y][x]=Color::alpha();
760 u=(tmp[0]-tl[0])*src_pw;
761 v=(tmp[1]-tl[1])*src_ph;
763 if(u<0 || v<0 || u>=source.get_w() || v>=source.get_h() || isnan(u) || isnan(v))
766 (*surface)[y][x]=Color::alpha();
768 (*surface)[y][x]=context.get_color(tmp);
771 //pen.set_value(source[v][u]);
772 (*surface)[y][x]=source[floor_to_int(v)][floor_to_int(u)];
776 if(!stagetwo.amount_complete(y,surface->get_h()))
784 if(cb && !cb->amount_complete(10000,10000)) return false;
790 Warp::get_bounding_rect()const
792 return Rect::full_plane();
796 Warp::get_full_bounding_rect(Context context)const
798 // return Rect::full_plane();
800 Rect under(context.get_full_bounding_rect());
804 under&=Rect(src_tl,src_br);
807 return get_transform()->perform(under);
810 Rect under(context.get_full_bounding_rect());
811 Rect ret(Rect::zero());
813 if(under.area()==HUGE_VAL)
814 return Rect::full_plane();
843 if(ret.area()==HUGE_VAL)
844 return Rect::full_plane();