1 /*! ========================================================================
4 ** $Id: warp.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(Warp);
50 SYNFIG_LAYER_SET_NAME(Warp,"warp");
51 SYNFIG_LAYER_SET_LOCAL_NAME(Warp,_("Warp"));
52 SYNFIG_LAYER_SET_CATEGORY(Warp,_("Distortions"));
53 SYNFIG_LAYER_SET_VERSION(Warp,"0.1");
54 SYNFIG_LAYER_SET_CVS_ID(Warp,"$Id: warp.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 ================================================= */
80 Warp::transform_forward(const Point& p)const
83 (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]),
84 (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])
89 Warp::transform_backward(const Point& p)const
92 (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]),
93 (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])
98 Warp::transform_forward_z(const Point& p)const
100 return inv_matrix[2][0]*p[0] + inv_matrix[2][1]*p[1] + inv_matrix[2][2];
104 Warp::transform_backward_z(const Point& p)const
106 return matrix[2][0]*p[0] + matrix[2][1]*p[1] + matrix[2][2];
110 #define transform_forward(p) Point( \
111 cache_a*p[0] + cache_b*p[1] + cache_c*p[0]*p[1] + cache_d, \
112 cache_e*p[0] + cache_f*p[1] + cache_i*p[0]*p[1] + cache_j )
114 #define transform_backward(p) Point( \
115 cache_a*p[0] + cache_b*p[1] + cache_c*p[0]*p[1] + cache_d, \
116 cache_e*p[0] + cache_f*p[1] + cache_i*p[0]*p[1] + cache_j )
119 #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]))
120 #define quad_area(a,b,c,d) (triangle_area(a,b,c)+triangle_area(a,c,d))
122 Real mat3_determinant(Real matrix[3][3])
126 ret = (matrix[0][0] *
127 (matrix[1][1] * matrix[2][2] -
128 matrix[1][2] * matrix[2][1]));
129 ret -= (matrix[1][0] *
130 (matrix[0][1] * matrix[2][2] -
131 matrix[0][2] * matrix[2][1]));
132 ret += (matrix[2][0] *
133 (matrix[0][1] * matrix[1][2] -
134 matrix[0][2] * matrix[1][1]));
139 void mat3_invert(Real in[3][3], Real out[3][3])
141 Real det(mat3_determinant(in));
148 out[0][0] = (in[1][1] * in[2][2] -
149 in[1][2] * in[2][1]) * det;
151 out[1][0] = - (in[1][0] * in[2][2] -
152 in[1][2] * in[2][0]) * det;
154 out[2][0] = (in[1][0] * in[2][1] -
155 in[1][1] * in[2][0]) * det;
157 out[0][1] = - (in[0][1] * in[2][2] -
158 in[0][2] * in[2][1]) * det;
160 out[1][1] = (in[0][0] * in[2][2] -
161 in[0][2] * in[2][0]) * det;
163 out[2][1] = - (in[0][0] * in[2][1] -
164 in[0][1] * in[2][0]) * det;
166 out[0][2] = (in[0][1] * in[1][2] -
167 in[0][2] * in[1][1]) * det;
169 out[1][2] = - (in[0][0] * in[1][2] -
170 in[0][2] * in[1][0]) * det;
172 out[2][2] = (in[0][0] * in[1][1] -
173 in[0][1] * in[1][0]) * det;
180 /* cache_a=(-dest_tl[0]+dest_tr[0])/(src_br[1]-src_tl[1]);
181 cache_b=(-dest_tl[0]+dest_bl[0])/(src_br[0]-src_tl[0]);
182 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]));
185 cache_e=(-dest_tl[1]+dest_tr[1])/(src_br[0]-src_tl[0]);
186 cache_f=(-dest_tl[1]+dest_bl[1])/(src_br[1]-src_tl[1]);
187 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]));
191 /* 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]));
192 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]));
193 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]));
195 matrix[0][0]=-(-dest_tl[1]+dest_tr[1])/(src_br[0]-src_tl[0]);
196 matrix[0][1]=-(-dest_tl[1]+dest_bl[1])/(src_br[1]-src_tl[1]);
198 matrix[1][0]=-(-dest_tl[0]+dest_tr[0])/(src_br[1]-src_tl[1]);
199 matrix[1][1]=-(-dest_tl[0]+dest_bl[0])/(src_br[0]-src_tl[0]);
201 matrix[0][2]=matrix[0][0]*dest_tl[0] + matrix[0][1]*dest_tl[1];
202 matrix[1][2]=matrix[1][0]*dest_tl[0] + matrix[1][1]*dest_tl[1];
208 const Real& x1(min(src_br[0],src_tl[0]));
209 const Real& y1(min(src_br[1],src_tl[1]));
210 const Real& x2(max(src_br[0],src_tl[0]));
211 const Real& y2(max(src_br[1],src_tl[1]));
213 Real tx1(dest_bl[0]);
214 Real ty1(dest_bl[1]);
215 Real tx2(dest_br[0]);
216 Real ty2(dest_br[1]);
217 Real tx3(dest_tl[0]);
218 Real ty3(dest_tl[1]);
219 Real tx4(dest_tr[0]);
220 Real ty4(dest_tr[1]);
222 if(src_br[0]<src_tl[0])
223 swap(tx3,tx4),swap(ty3,ty4),swap(tx1,tx2),swap(ty1,ty2);
225 if(src_br[1]>src_tl[1])
226 swap(tx3,tx1),swap(ty3,ty1),swap(tx4,tx2),swap(ty4,ty2);
231 scalex = scaley = 1.0;
234 scalex = 1.0 / (Real) (x2 - x1);
237 scaley = 1.0 / (Real) (y2 - y1);
239 /* Determine the perspective transform that maps from
240 * the unit cube to the transformed coordinates
243 Real dx1, dx2, dx3, dy1, dy2, dy3;
247 dx3 = tx1 - tx2 + tx4 - tx3;
251 dy3 = ty1 - ty2 + ty4 - ty3;
253 /* Is the mapping affine? */
254 if ((dx3 == 0.0) && (dy3 == 0.0))
256 matrix[0][0] = tx2 - tx1;
257 matrix[0][1] = tx4 - tx2;
259 matrix[1][0] = ty2 - ty1;
260 matrix[1][1] = ty4 - ty2;
269 det1 = dx3 * dy2 - dy3 * dx2;
270 det2 = dx1 * dy2 - dy1 * dx2;
272 if (det1 == 0.0 && det2 == 0.0)
275 matrix[2][0] = det1 / det2;
277 det1 = dx1 * dy3 - dy1 * dx3;
279 if (det1 == 0.0 && det2 == 0.0)
282 matrix[2][1] = det1 / det2;
284 matrix[0][0] = tx2 - tx1 + matrix[2][0] * tx2;
285 matrix[0][1] = tx3 - tx1 + matrix[2][1] * tx3;
288 matrix[1][0] = ty2 - ty1 + matrix[2][0] * ty2;
289 matrix[1][1] = ty3 - ty1 + matrix[2][1] * ty3;
297 Real scaletrans[3][3]={
298 { scalex, 0, -x1*scalex },
299 { 0, scaley, -y1*scaley },
305 for (int i = 0; i < 3; i++)
311 for (int j = 0; j < 3; j++)
313 matrix[i][j] = t1 * scaletrans[0][j];
314 matrix[i][j] += t2 * scaletrans[1][j];
315 matrix[i][j] += t3 * scaletrans[2][j];
319 mat3_invert(matrix, inv_matrix);
321 gimp_matrix3_identity (result);
322 gimp_matrix3_translate (result, -x1, -y1);
323 gimp_matrix3_scale (result, scalex, scaley);
324 gimp_matrix3_mult (&matrix, result);
329 Warp::set_param(const String & param, const ValueBase &value)
331 IMPORT_PLUS(src_tl,sync());
332 IMPORT_PLUS(src_br,sync());
333 IMPORT_PLUS(dest_tl,sync());
334 IMPORT_PLUS(dest_tr,sync());
335 IMPORT_PLUS(dest_bl,sync());
336 IMPORT_PLUS(dest_br,sync());
344 Warp::get_param(const String ¶m)const
362 Warp::get_param_vocab()const
366 ret.push_back(ParamDesc("src_tl")
367 .set_local_name(_("Source TL"))
371 ret.push_back(ParamDesc("src_br")
372 .set_local_name(_("Source BR"))
375 ret.push_back(ParamDesc("dest_tl")
376 .set_local_name(_("Dest TL"))
377 .set_connect("dest_tr")
380 ret.push_back(ParamDesc("dest_tr")
381 .set_local_name(_("Dest TR"))
382 .set_connect("dest_br")
385 ret.push_back(ParamDesc("dest_br")
386 .set_local_name(_("Dest BR"))
387 .set_connect("dest_bl")
390 ret.push_back(ParamDesc("dest_bl")
391 .set_local_name(_("Dest BL"))
392 .set_connect("dest_tl")
395 ret.push_back(ParamDesc("clip")
396 .set_local_name(_("Clip"))
399 ret.push_back(ParamDesc("horizon")
400 .set_local_name(_("Horizon"))
407 class Warp_Trans : public Transform
409 etl::handle<const Warp> layer;
411 Warp_Trans(const Warp* x):Transform(x->get_guid()),layer(x) { }
413 synfig::Vector perform(const synfig::Vector& x)const
415 return layer->transform_backward(x);
416 //Point pos(x-layer->origin);
417 //return Point(layer->cos_val*pos[0]-layer->sin_val*pos[1],layer->sin_val*pos[0]+layer->cos_val*pos[1])+layer->origin;
420 synfig::Vector unperform(const synfig::Vector& x)const
423 return layer->transform_forward(x);
424 //Point pos(x-layer->origin);
425 //return Point(layer->cos_val*pos[0]+layer->sin_val*pos[1],-layer->sin_val*pos[0]+layer->cos_val*pos[1])+layer->origin;
428 etl::handle<Transform>
429 Warp::get_transform()const
431 return new Warp_Trans(this);
434 synfig::Layer::Handle
435 Warp::hit_check(synfig::Context context, const synfig::Point &p)const
437 Point newpos(transform_forward(p));
441 Rect rect(src_tl,src_br);
442 if(!rect.is_inside(newpos))
446 return context.hit_check(newpos);
450 Warp::get_color(Context context, const Point &p)const
452 Point newpos(transform_forward(p));
456 Rect rect(src_tl,src_br);
457 if(!rect.is_inside(newpos))
458 return Color::alpha();
461 const float z(transform_backward_z(newpos));
463 return context.get_color(newpos);
465 return Color::alpha();
468 //#define ACCEL_WARP_IS_BROKEN 1
471 Warp::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
473 SuperCallback stageone(cb,0,9000,10000);
474 SuperCallback stagetwo(cb,9000,10000,10000);
476 Real pw=(renddesc.get_w())/(renddesc.get_br()[0]-renddesc.get_tl()[0]);
477 Real ph=(renddesc.get_h())/(renddesc.get_br()[1]-renddesc.get_tl()[1]);
479 if(cb && !cb->amount_complete(0,10000))
482 Point tl(renddesc.get_tl());
483 Point br(renddesc.get_br());
487 Rect render_rect(tl,br);
488 Rect clip_rect(Rect::full_plane());
489 Rect dest_rect(dest_tl,dest_br); dest_rect.expand(dest_tr).expand(dest_bl);
491 Real zoom_factor(1.0);
493 // Quick exclusion clip, if necessary
494 if(clip && !intersect(render_rect,dest_rect))
496 surface->set_wh(renddesc.get_w(),renddesc.get_h());
502 Rect other(render_rect);
506 Point min(other.get_min());
507 Point max(other.get_max());
509 bool init_point_set=false;
511 Point trans_point[4];
514 Real z,minz(10000000000000.0f),maxz(0);
517 p=transform_forward(min);
518 z=transform_backward_z(p);
519 if(z>0 && z<horizon*2)
522 bounding_rect.expand(p);
524 bounding_rect=Rect(p);
526 maxz=std::max(maxz,z);
527 minz=std::min(minz,z);
530 p=transform_forward(max);
531 z=transform_backward_z(p);
532 if(z>0 && z<horizon*2)
535 bounding_rect.expand(p);
537 bounding_rect=Rect(p);
539 maxz=std::max(maxz,z);
540 minz=std::min(minz,z);
545 p=transform_forward(min);
546 z=transform_backward_z(p);
547 if(z>0 && z<horizon*2)
550 bounding_rect.expand(p);
552 bounding_rect=Rect(p);
554 maxz=std::max(maxz,z);
555 minz=std::min(minz,z);
558 p=transform_forward(max);
559 z=transform_backward_z(p);
560 if(z>0 && z<horizon*2)
563 bounding_rect.expand(p);
565 bounding_rect=Rect(p);
567 maxz=std::max(maxz,z);
568 minz=std::min(minz,z);
573 surface->set_wh(renddesc.get_w(),renddesc.get_h());
577 zoom_factor=(1+(maxz-minz));
581 #ifdef ACCEL_WARP_IS_BROKEN
582 return Layer::accelerated_render(context,surface,quality,renddesc, cb);
587 .expand(transform_forward(tl))
588 .expand(transform_forward(br))
592 //synfig::warning("given window: [%f,%f]-[%f,%f] %dx%d",tl[0],tl[1],br[0],br[1],renddesc.get_w(),renddesc.get_h());
593 //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]);
595 // If we are clipping, then go ahead and clip to the
598 clip_rect&=Rect(src_tl,src_br);
600 // Bound ourselves to the bounding rectangle of
602 clip_rect&=context.get_full_bounding_rect();//.expand_x(abs(zoom_factor/pw)).expand_y(abs(zoom_factor/ph));
604 bounding_rect&=clip_rect;
606 Point min_point(bounding_rect.get_min());
607 Point max_point(bounding_rect.get_max());
633 const int tmp_d(max(renddesc.get_w(),renddesc.get_h()));
634 Real src_pw=(tmp_d*zoom_factor)/(br[0]-tl[0]);
635 Real src_ph=(tmp_d*zoom_factor)/(br[1]-tl[1]);
638 RendDesc desc(renddesc);
640 //desc.set_flags(RendDesc::PX_ASPECT);
643 desc.set_wh(ceil_to_int(src_pw*(br[0]-tl[0])),ceil_to_int(src_ph*(br[1]-tl[1])));
645 //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());
646 if(desc.get_w()==0 && desc.get_h()==0)
648 surface->set_wh(renddesc.get_w(),renddesc.get_h());
653 // Recalculate the pixel widths for the src renddesc
654 src_pw=(desc.get_w())/(desc.get_br()[0]-desc.get_tl()[0]);
655 src_ph=(desc.get_h())/(desc.get_br()[1]-desc.get_tl()[1]);
659 source.set_wh(desc.get_w(),desc.get_h());
661 if(!context.accelerated_render(&source,quality,desc,&stageone))
664 surface->set_wh(renddesc.get_w(),renddesc.get_h());
667 Surface::pen pen(surface->begin());
675 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)
677 for(x=0,point[0]=renddesc.get_tl()[0];x<surface->get_w();x++,pen.inc_x(),point[0]+=1.0/pw)
679 tmp=transform_forward(point);
680 const float z(transform_backward_z(tmp));
681 if(!clip_rect.is_inside(tmp) || !(z>0 && z<horizon))
683 (*surface)[y][x]=Color::alpha();
687 u=(tmp[0]-tl[0])*src_pw;
688 v=(tmp[1]-tl[1])*src_ph;
690 if(u<0 || v<0 || u>=source.get_w() || v>=source.get_h() || isnan(u) || isnan(v))
692 (*surface)[y][x]=context.get_color(tmp);
695 (*surface)[y][x]=source.cubic_sample(u,v);
699 if(!stagetwo.amount_complete(y,surface->get_h()))
707 // INTERPOLATION_LINEAR
711 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)
713 for(x=0,point[0]=renddesc.get_tl()[0];x<surface->get_w();x++,pen.inc_x(),point[0]+=1.0/pw)
715 tmp=transform_forward(point);
716 const float z(transform_backward_z(tmp));
717 if(!clip_rect.is_inside(tmp) || !(z>0 && z<horizon))
719 (*surface)[y][x]=Color::alpha();
723 u=(tmp[0]-tl[0])*src_pw;
724 v=(tmp[1]-tl[1])*src_ph;
726 if(u<0 || v<0 || u>=source.get_w() || v>=source.get_h() || isnan(u) || isnan(v))
729 (*surface)[y][x]=Color::alpha();
731 (*surface)[y][x]=context.get_color(tmp);
734 (*surface)[y][x]=source.linear_sample(u,v);
738 if(!stagetwo.amount_complete(y,surface->get_h()))
749 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)
751 for(x=0,point[0]=renddesc.get_tl()[0];x<surface->get_w();x++,pen.inc_x(),point[0]+=1.0/pw)
753 tmp=transform_forward(point);
754 const float z(transform_backward_z(tmp));
755 if(!clip_rect.is_inside(tmp) || !(z>0 && z<horizon))
757 (*surface)[y][x]=Color::alpha();
761 u=(tmp[0]-tl[0])*src_pw;
762 v=(tmp[1]-tl[1])*src_ph;
764 if(u<0 || v<0 || u>=source.get_w() || v>=source.get_h() || isnan(u) || isnan(v))
767 (*surface)[y][x]=Color::alpha();
769 (*surface)[y][x]=context.get_color(tmp);
772 //pen.set_value(source[v][u]);
773 (*surface)[y][x]=source[floor_to_int(v)][floor_to_int(u)];
777 if(!stagetwo.amount_complete(y,surface->get_h()))
785 if(cb && !cb->amount_complete(10000,10000)) return false;
791 Warp::get_bounding_rect()const
793 return Rect::full_plane();
797 Warp::get_full_bounding_rect(Context context)const
799 // return Rect::full_plane();
801 Rect under(context.get_full_bounding_rect());
805 under&=Rect(src_tl,src_br);
808 return get_transform()->perform(under);
811 Rect under(context.get_full_bounding_rect());
812 Rect ret(Rect::zero());
814 if(under.area()==HUGE_VAL)
815 return Rect::full_plane();
844 if(ret.area()==HUGE_VAL)
845 return Rect::full_plane();