1 /* === S Y N F I G ========================================================= */
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
10 ** This package is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU General Public License as
12 ** published by the Free Software Foundation; either version 2 of
13 ** the License, or (at your option) any later version.
15 ** This package is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ** General Public License for more details.
21 ** === N O T E S ===========================================================
23 ** ========================================================================= */
25 /* === H E A D E R S ======================================================= */
35 #include <synfig/string.h>
36 #include <synfig/time.h>
37 #include <synfig/context.h>
38 #include <synfig/paramdesc.h>
39 #include <synfig/renddesc.h>
40 #include <synfig/surface.h>
41 #include <synfig/value.h>
42 #include <synfig/valuenode.h>
43 #include <synfig/transform.h>
48 /* === M A C R O S ========================================================= */
50 /* === G L O B A L S ======================================================= */
52 SYNFIG_LAYER_INIT(Warp);
53 SYNFIG_LAYER_SET_NAME(Warp,"warp");
54 SYNFIG_LAYER_SET_LOCAL_NAME(Warp,_("Warp"));
55 SYNFIG_LAYER_SET_CATEGORY(Warp,_("Distortions"));
56 SYNFIG_LAYER_SET_VERSION(Warp,"0.1");
57 SYNFIG_LAYER_SET_CVS_ID(Warp,"$Id$");
59 /* === P R O C E D U R E S ================================================= */
61 /* === M E T H O D S ======================================================= */
63 /* === E N T R Y P O I N T ================================================= */
83 Warp::transform_forward(const Point& p)const
86 (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]),
87 (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])
92 Warp::transform_backward(const Point& p)const
95 (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]),
96 (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])
101 Warp::transform_forward_z(const Point& p)const
103 return inv_matrix[2][0]*p[0] + inv_matrix[2][1]*p[1] + inv_matrix[2][2];
107 Warp::transform_backward_z(const Point& p)const
109 return matrix[2][0]*p[0] + matrix[2][1]*p[1] + matrix[2][2];
113 #define transform_forward(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 )
117 #define transform_backward(p) Point( \
118 cache_a*p[0] + cache_b*p[1] + cache_c*p[0]*p[1] + cache_d, \
119 cache_e*p[0] + cache_f*p[1] + cache_i*p[0]*p[1] + cache_j )
122 #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]))
123 #define quad_area(a,b,c,d) (triangle_area(a,b,c)+triangle_area(a,c,d))
125 Real mat3_determinant(Real matrix[3][3])
129 ret = (matrix[0][0] *
130 (matrix[1][1] * matrix[2][2] -
131 matrix[1][2] * matrix[2][1]));
132 ret -= (matrix[1][0] *
133 (matrix[0][1] * matrix[2][2] -
134 matrix[0][2] * matrix[2][1]));
135 ret += (matrix[2][0] *
136 (matrix[0][1] * matrix[1][2] -
137 matrix[0][2] * matrix[1][1]));
142 void mat3_invert(Real in[3][3], Real out[3][3])
144 Real det(mat3_determinant(in));
151 out[0][0] = (in[1][1] * in[2][2] -
152 in[1][2] * in[2][1]) * det;
154 out[1][0] = - (in[1][0] * in[2][2] -
155 in[1][2] * in[2][0]) * det;
157 out[2][0] = (in[1][0] * in[2][1] -
158 in[1][1] * in[2][0]) * det;
160 out[0][1] = - (in[0][1] * in[2][2] -
161 in[0][2] * in[2][1]) * det;
163 out[1][1] = (in[0][0] * in[2][2] -
164 in[0][2] * in[2][0]) * det;
166 out[2][1] = - (in[0][0] * in[2][1] -
167 in[0][1] * in[2][0]) * det;
169 out[0][2] = (in[0][1] * in[1][2] -
170 in[0][2] * in[1][1]) * det;
172 out[1][2] = - (in[0][0] * in[1][2] -
173 in[0][2] * in[1][0]) * det;
175 out[2][2] = (in[0][0] * in[1][1] -
176 in[0][1] * in[1][0]) * det;
183 /* cache_a=(-dest_tl[0]+dest_tr[0])/(src_br[1]-src_tl[1]);
184 cache_b=(-dest_tl[0]+dest_bl[0])/(src_br[0]-src_tl[0]);
185 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]));
188 cache_e=(-dest_tl[1]+dest_tr[1])/(src_br[0]-src_tl[0]);
189 cache_f=(-dest_tl[1]+dest_bl[1])/(src_br[1]-src_tl[1]);
190 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]));
194 /* 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]));
195 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]));
196 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]));
198 matrix[0][0]=-(-dest_tl[1]+dest_tr[1])/(src_br[0]-src_tl[0]);
199 matrix[0][1]=-(-dest_tl[1]+dest_bl[1])/(src_br[1]-src_tl[1]);
201 matrix[1][0]=-(-dest_tl[0]+dest_tr[0])/(src_br[1]-src_tl[1]);
202 matrix[1][1]=-(-dest_tl[0]+dest_bl[0])/(src_br[0]-src_tl[0]);
204 matrix[0][2]=matrix[0][0]*dest_tl[0] + matrix[0][1]*dest_tl[1];
205 matrix[1][2]=matrix[1][0]*dest_tl[0] + matrix[1][1]*dest_tl[1];
211 const Real& x1(min(src_br[0],src_tl[0]));
212 const Real& y1(min(src_br[1],src_tl[1]));
213 const Real& x2(max(src_br[0],src_tl[0]));
214 const Real& y2(max(src_br[1],src_tl[1]));
216 Real tx1(dest_bl[0]);
217 Real ty1(dest_bl[1]);
218 Real tx2(dest_br[0]);
219 Real ty2(dest_br[1]);
220 Real tx3(dest_tl[0]);
221 Real ty3(dest_tl[1]);
222 Real tx4(dest_tr[0]);
223 Real ty4(dest_tr[1]);
225 if(src_br[0]<src_tl[0])
226 swap(tx3,tx4),swap(ty3,ty4),swap(tx1,tx2),swap(ty1,ty2);
228 if(src_br[1]>src_tl[1])
229 swap(tx3,tx1),swap(ty3,ty1),swap(tx4,tx2),swap(ty4,ty2);
234 scalex = scaley = 1.0;
237 scalex = 1.0 / (Real) (x2 - x1);
240 scaley = 1.0 / (Real) (y2 - y1);
242 /* Determine the perspective transform that maps from
243 * the unit cube to the transformed coordinates
246 Real dx1, dx2, dx3, dy1, dy2, dy3;
250 dx3 = tx1 - tx2 + tx4 - tx3;
254 dy3 = ty1 - ty2 + ty4 - ty3;
256 /* Is the mapping affine? */
257 if ((dx3 == 0.0) && (dy3 == 0.0))
259 matrix[0][0] = tx2 - tx1;
260 matrix[0][1] = tx4 - tx2;
262 matrix[1][0] = ty2 - ty1;
263 matrix[1][1] = ty4 - ty2;
272 det1 = dx3 * dy2 - dy3 * dx2;
273 det2 = dx1 * dy2 - dy1 * dx2;
275 if (det1 == 0.0 && det2 == 0.0)
278 matrix[2][0] = det1 / det2;
280 det1 = dx1 * dy3 - dy1 * dx3;
282 if (det1 == 0.0 && det2 == 0.0)
285 matrix[2][1] = det1 / det2;
287 matrix[0][0] = tx2 - tx1 + matrix[2][0] * tx2;
288 matrix[0][1] = tx3 - tx1 + matrix[2][1] * tx3;
291 matrix[1][0] = ty2 - ty1 + matrix[2][0] * ty2;
292 matrix[1][1] = ty3 - ty1 + matrix[2][1] * ty3;
300 Real scaletrans[3][3]={
301 { scalex, 0, -x1*scalex },
302 { 0, scaley, -y1*scaley },
308 for (int i = 0; i < 3; i++)
314 for (int j = 0; j < 3; j++)
316 matrix[i][j] = t1 * scaletrans[0][j];
317 matrix[i][j] += t2 * scaletrans[1][j];
318 matrix[i][j] += t3 * scaletrans[2][j];
322 mat3_invert(matrix, inv_matrix);
324 gimp_matrix3_identity (result);
325 gimp_matrix3_translate (result, -x1, -y1);
326 gimp_matrix3_scale (result, scalex, scaley);
327 gimp_matrix3_mult (&matrix, result);
332 Warp::set_param(const String & param, const ValueBase &value)
334 IMPORT_PLUS(src_tl,sync());
335 IMPORT_PLUS(src_br,sync());
336 IMPORT_PLUS(dest_tl,sync());
337 IMPORT_PLUS(dest_tr,sync());
338 IMPORT_PLUS(dest_bl,sync());
339 IMPORT_PLUS(dest_br,sync());
347 Warp::get_param(const String ¶m)const
365 Warp::get_param_vocab()const
369 ret.push_back(ParamDesc("src_tl")
370 .set_local_name(_("Source TL"))
374 ret.push_back(ParamDesc("src_br")
375 .set_local_name(_("Source BR"))
378 ret.push_back(ParamDesc("dest_tl")
379 .set_local_name(_("Dest TL"))
380 .set_connect("dest_tr")
383 ret.push_back(ParamDesc("dest_tr")
384 .set_local_name(_("Dest TR"))
385 .set_connect("dest_br")
388 ret.push_back(ParamDesc("dest_br")
389 .set_local_name(_("Dest BR"))
390 .set_connect("dest_bl")
393 ret.push_back(ParamDesc("dest_bl")
394 .set_local_name(_("Dest BL"))
395 .set_connect("dest_tl")
398 ret.push_back(ParamDesc("clip")
399 .set_local_name(_("Clip"))
402 ret.push_back(ParamDesc("horizon")
403 .set_local_name(_("Horizon"))
410 class Warp_Trans : public Transform
412 etl::handle<const Warp> layer;
414 Warp_Trans(const Warp* x):Transform(x->get_guid()),layer(x) { }
416 synfig::Vector perform(const synfig::Vector& x)const
418 return layer->transform_backward(x);
419 //Point pos(x-layer->origin);
420 //return Point(layer->cos_val*pos[0]-layer->sin_val*pos[1],layer->sin_val*pos[0]+layer->cos_val*pos[1])+layer->origin;
423 synfig::Vector unperform(const synfig::Vector& x)const
426 return layer->transform_forward(x);
427 //Point pos(x-layer->origin);
428 //return Point(layer->cos_val*pos[0]+layer->sin_val*pos[1],-layer->sin_val*pos[0]+layer->cos_val*pos[1])+layer->origin;
431 etl::handle<Transform>
432 Warp::get_transform()const
434 return new Warp_Trans(this);
437 synfig::Layer::Handle
438 Warp::hit_check(synfig::Context context, const synfig::Point &p)const
440 Point newpos(transform_forward(p));
444 Rect rect(src_tl,src_br);
445 if(!rect.is_inside(newpos))
449 return context.hit_check(newpos);
453 Warp::get_color(Context context, const Point &p)const
455 Point newpos(transform_forward(p));
459 Rect rect(src_tl,src_br);
460 if(!rect.is_inside(newpos))
461 return Color::alpha();
464 const float z(transform_backward_z(newpos));
466 return context.get_color(newpos);
468 return Color::alpha();
471 //#define ACCEL_WARP_IS_BROKEN 1
474 Warp::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
476 SuperCallback stageone(cb,0,9000,10000);
477 SuperCallback stagetwo(cb,9000,10000,10000);
479 Real pw=(renddesc.get_w())/(renddesc.get_br()[0]-renddesc.get_tl()[0]);
480 Real ph=(renddesc.get_h())/(renddesc.get_br()[1]-renddesc.get_tl()[1]);
482 if(cb && !cb->amount_complete(0,10000))
485 Point tl(renddesc.get_tl());
486 Point br(renddesc.get_br());
490 Rect render_rect(tl,br);
491 Rect clip_rect(Rect::full_plane());
492 Rect dest_rect(dest_tl,dest_br); dest_rect.expand(dest_tr).expand(dest_bl);
494 Real zoom_factor(1.0);
496 // Quick exclusion clip, if necessary
497 if(clip && !intersect(render_rect,dest_rect))
499 surface->set_wh(renddesc.get_w(),renddesc.get_h());
505 Rect other(render_rect);
509 Point min(other.get_min());
510 Point max(other.get_max());
512 bool init_point_set=false;
514 // Point trans_point[4];
517 Real z,minz(10000000000000.0f),maxz(0);
520 p=transform_forward(min);
521 z=transform_backward_z(p);
522 if(z>0 && z<horizon*2)
525 bounding_rect.expand(p);
527 bounding_rect=Rect(p);
529 maxz=std::max(maxz,z);
530 minz=std::min(minz,z);
533 p=transform_forward(max);
534 z=transform_backward_z(p);
535 if(z>0 && z<horizon*2)
538 bounding_rect.expand(p);
540 bounding_rect=Rect(p);
542 maxz=std::max(maxz,z);
543 minz=std::min(minz,z);
548 p=transform_forward(min);
549 z=transform_backward_z(p);
550 if(z>0 && z<horizon*2)
553 bounding_rect.expand(p);
555 bounding_rect=Rect(p);
557 maxz=std::max(maxz,z);
558 minz=std::min(minz,z);
561 p=transform_forward(max);
562 z=transform_backward_z(p);
563 if(z>0 && z<horizon*2)
566 bounding_rect.expand(p);
568 bounding_rect=Rect(p);
570 maxz=std::max(maxz,z);
571 minz=std::min(minz,z);
576 surface->set_wh(renddesc.get_w(),renddesc.get_h());
580 zoom_factor=(1+(maxz-minz));
584 #ifdef ACCEL_WARP_IS_BROKEN
585 return Layer::accelerated_render(context,surface,quality,renddesc, cb);
590 .expand(transform_forward(tl))
591 .expand(transform_forward(br))
595 //synfig::warning("given window: [%f,%f]-[%f,%f] %dx%d",tl[0],tl[1],br[0],br[1],renddesc.get_w(),renddesc.get_h());
596 //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]);
598 // If we are clipping, then go ahead and clip to the
601 clip_rect&=Rect(src_tl,src_br);
603 // Bound ourselves to the bounding rectangle of
605 clip_rect&=context.get_full_bounding_rect();//.expand_x(abs(zoom_factor/pw)).expand_y(abs(zoom_factor/ph));
607 bounding_rect&=clip_rect;
609 Point min_point(bounding_rect.get_min());
610 Point max_point(bounding_rect.get_max());
636 const int tmp_d(max(renddesc.get_w(),renddesc.get_h()));
637 Real src_pw=(tmp_d*zoom_factor)/(br[0]-tl[0]);
638 Real src_ph=(tmp_d*zoom_factor)/(br[1]-tl[1]);
641 RendDesc desc(renddesc);
643 //desc.set_flags(RendDesc::PX_ASPECT);
646 desc.set_wh(ceil_to_int(src_pw*(br[0]-tl[0])),ceil_to_int(src_ph*(br[1]-tl[1])));
648 //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());
649 if(desc.get_w()==0 && desc.get_h()==0)
651 surface->set_wh(renddesc.get_w(),renddesc.get_h());
656 // Recalculate the pixel widths for the src renddesc
657 src_pw=(desc.get_w())/(desc.get_br()[0]-desc.get_tl()[0]);
658 src_ph=(desc.get_h())/(desc.get_br()[1]-desc.get_tl()[1]);
662 source.set_wh(desc.get_w(),desc.get_h());
664 if(!context.accelerated_render(&source,quality,desc,&stageone))
667 surface->set_wh(renddesc.get_w(),renddesc.get_h());
670 Surface::pen pen(surface->begin());
678 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)
680 for(x=0,point[0]=renddesc.get_tl()[0];x<surface->get_w();x++,pen.inc_x(),point[0]+=1.0/pw)
682 tmp=transform_forward(point);
683 const float z(transform_backward_z(tmp));
684 if(!clip_rect.is_inside(tmp) || !(z>0 && z<horizon))
686 (*surface)[y][x]=Color::alpha();
690 u=(tmp[0]-tl[0])*src_pw;
691 v=(tmp[1]-tl[1])*src_ph;
693 if(u<0 || v<0 || u>=source.get_w() || v>=source.get_h() || isnan(u) || isnan(v))
695 (*surface)[y][x]=context.get_color(tmp);
698 (*surface)[y][x]=source.cubic_sample(u,v);
702 if(!stagetwo.amount_complete(y,surface->get_h()))
710 // INTERPOLATION_LINEAR
714 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)
716 for(x=0,point[0]=renddesc.get_tl()[0];x<surface->get_w();x++,pen.inc_x(),point[0]+=1.0/pw)
718 tmp=transform_forward(point);
719 const float z(transform_backward_z(tmp));
720 if(!clip_rect.is_inside(tmp) || !(z>0 && z<horizon))
722 (*surface)[y][x]=Color::alpha();
726 u=(tmp[0]-tl[0])*src_pw;
727 v=(tmp[1]-tl[1])*src_ph;
729 if(u<0 || v<0 || u>=source.get_w() || v>=source.get_h() || isnan(u) || isnan(v))
732 (*surface)[y][x]=Color::alpha();
734 (*surface)[y][x]=context.get_color(tmp);
737 (*surface)[y][x]=source.linear_sample(u,v);
741 if(!stagetwo.amount_complete(y,surface->get_h()))
752 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)
754 for(x=0,point[0]=renddesc.get_tl()[0];x<surface->get_w();x++,pen.inc_x(),point[0]+=1.0/pw)
756 tmp=transform_forward(point);
757 const float z(transform_backward_z(tmp));
758 if(!clip_rect.is_inside(tmp) || !(z>0 && z<horizon))
760 (*surface)[y][x]=Color::alpha();
764 u=(tmp[0]-tl[0])*src_pw;
765 v=(tmp[1]-tl[1])*src_ph;
767 if(u<0 || v<0 || u>=source.get_w() || v>=source.get_h() || isnan(u) || isnan(v))
770 (*surface)[y][x]=Color::alpha();
772 (*surface)[y][x]=context.get_color(tmp);
775 //pen.set_value(source[v][u]);
776 (*surface)[y][x]=source[floor_to_int(v)][floor_to_int(u)];
780 if(!stagetwo.amount_complete(y,surface->get_h()))
788 if(cb && !cb->amount_complete(10000,10000)) return false;
794 Warp::get_bounding_rect()const
796 return Rect::full_plane();
800 Warp::get_full_bounding_rect(Context context)const
802 // return Rect::full_plane();
804 Rect under(context.get_full_bounding_rect());
808 under&=Rect(src_tl,src_br);
811 return get_transform()->perform(under);
814 Rect under(context.get_full_bounding_rect());
815 Rect ret(Rect::zero());
817 if(under.area()==HUGE_VAL)
818 return Rect::full_plane();
847 if(ret.area()==HUGE_VAL)
848 return Rect::full_plane();