729272edad9fa79d6a21e8ba4fc97e4257cbe457
[synfig.git] / synfig-core / src / modules / lyr_std / warp.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file warp.cpp
3 **      \brief Implementation of the "Warp" layer
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007, 2008 Chris Moore
10 **
11 **      This package is free software; you can redistribute it and/or
12 **      modify it under the terms of the GNU General Public License as
13 **      published by the Free Software Foundation; either version 2 of
14 **      the License, or (at your option) any later version.
15 **
16 **      This package is distributed in the hope that it will be useful,
17 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **      General Public License for more details.
20 **      \endlegal
21 **
22 ** === N O T E S ===========================================================
23 **
24 ** ========================================================================= */
25
26 /* === H E A D E R S ======================================================= */
27
28 #ifdef USING_PCH
29 #       include "pch.h"
30 #else
31 #ifdef HAVE_CONFIG_H
32 #       include <config.h>
33 #endif
34
35 #include "warp.h"
36 #include <synfig/string.h>
37 #include <synfig/time.h>
38 #include <synfig/context.h>
39 #include <synfig/paramdesc.h>
40 #include <synfig/renddesc.h>
41 #include <synfig/surface.h>
42 #include <synfig/value.h>
43 #include <synfig/valuenode.h>
44 #include <synfig/transform.h>
45 #include <ETL/misc>
46
47 #endif
48
49 /* === M A C R O S ========================================================= */
50
51 /* === G L O B A L S ======================================================= */
52
53 SYNFIG_LAYER_INIT(Warp);
54 SYNFIG_LAYER_SET_NAME(Warp,"warp");
55 SYNFIG_LAYER_SET_LOCAL_NAME(Warp,N_("Warp"));
56 SYNFIG_LAYER_SET_CATEGORY(Warp,N_("Distortions"));
57 SYNFIG_LAYER_SET_VERSION(Warp,"0.1");
58 SYNFIG_LAYER_SET_CVS_ID(Warp,"$Id$");
59
60 /* === P R O C E D U R E S ================================================= */
61
62 /* === M E T H O D S ======================================================= */
63
64 /* === E N T R Y P O I N T ================================================= */
65
66 Warp::Warp():
67         src_tl  (-2,2),
68         src_br  (2,-2),
69         dest_tl (-1.8,2.1),
70         dest_tr (1.8,2.1),
71         dest_bl (-2.2,-2),
72         dest_br (2.2,-2),
73         clip    (true)
74 {
75         sync();
76         horizon=4;
77         Layer::Vocab voc(get_param_vocab());
78         Layer::fill_static(voc);
79 }
80
81 Warp::~Warp()
82 {
83 }
84
85 inline Point
86 Warp::transform_forward(const Point& p)const
87 {
88         return Point(
89                 (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]),
90                 (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])
91         );
92 }
93
94 inline Point
95 Warp::transform_backward(const Point& p)const
96 {
97         return Point(
98                 (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]),
99                 (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])
100         );
101 }
102
103 inline Real
104 Warp::transform_forward_z(const Point& p)const
105 {
106         return inv_matrix[2][0]*p[0] + inv_matrix[2][1]*p[1] + inv_matrix[2][2];
107 }
108
109 inline Real
110 Warp::transform_backward_z(const Point& p)const
111 {
112         return matrix[2][0]*p[0] + matrix[2][1]*p[1] + matrix[2][2];
113 }
114
115 /*
116 #define transform_forward(p) Point(     \
117                 cache_a*p[0] + cache_b*p[1] + cache_c*p[0]*p[1] + cache_d,      \
118                 cache_e*p[0] + cache_f*p[1] + cache_i*p[0]*p[1] + cache_j )
119
120 #define transform_backward(p) Point(    \
121                 cache_a*p[0] + cache_b*p[1] + cache_c*p[0]*p[1] + cache_d,      \
122                 cache_e*p[0] + cache_f*p[1] + cache_i*p[0]*p[1] + cache_j )
123 */
124
125 #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]))
126 #define quad_area(a,b,c,d) (triangle_area(a,b,c)+triangle_area(a,c,d))
127
128 Real mat3_determinant(Real matrix[3][3])
129 {
130   Real ret;
131
132   ret  = (matrix[0][0] *
133                   (matrix[1][1] * matrix[2][2] -
134                    matrix[1][2] * matrix[2][1]));
135   ret -= (matrix[1][0] *
136                   (matrix[0][1] * matrix[2][2] -
137                    matrix[0][2] * matrix[2][1]));
138   ret += (matrix[2][0] *
139                   (matrix[0][1] * matrix[1][2] -
140                    matrix[0][2] * matrix[1][1]));
141
142 return ret;
143 }
144
145 void mat3_invert(Real in[3][3], Real out[3][3])
146 {
147   Real det(mat3_determinant(in));
148
149         if (det == 0.0)
150     return;
151
152   det = 1.0 / det;
153
154   out[0][0] =   (in[1][1] * in[2][2] -
155                        in[1][2] * in[2][1]) * det;
156
157   out[1][0] = - (in[1][0] * in[2][2] -
158                        in[1][2] * in[2][0]) * det;
159
160   out[2][0] =   (in[1][0] * in[2][1] -
161                        in[1][1] * in[2][0]) * det;
162
163   out[0][1] = - (in[0][1] * in[2][2] -
164                        in[0][2] * in[2][1]) * det;
165
166   out[1][1] =   (in[0][0] * in[2][2] -
167                        in[0][2] * in[2][0]) * det;
168
169   out[2][1] = - (in[0][0] * in[2][1] -
170                        in[0][1] * in[2][0]) * det;
171
172   out[0][2] =   (in[0][1] * in[1][2] -
173                        in[0][2] * in[1][1]) * det;
174
175   out[1][2] = - (in[0][0] * in[1][2] -
176                        in[0][2] * in[1][0]) * det;
177
178   out[2][2] =   (in[0][0] * in[1][1] -
179                        in[0][1] * in[1][0]) * det;
180
181 }
182
183 void
184 Warp::sync()
185 {
186 /*      cache_a=(-dest_tl[0]+dest_tr[0])/(src_br[1]-src_tl[1]);
187         cache_b=(-dest_tl[0]+dest_bl[0])/(src_br[0]-src_tl[0]);
188         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]));
189         cache_d=dest_tl[0];
190
191         cache_e=(-dest_tl[1]+dest_tr[1])/(src_br[0]-src_tl[0]);
192         cache_f=(-dest_tl[1]+dest_bl[1])/(src_br[1]-src_tl[1]);
193         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         cache_j=dest_tl[1];
195 */
196
197 /*      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]));
198         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]));
199         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]));
200
201         matrix[0][0]=-(-dest_tl[1]+dest_tr[1])/(src_br[0]-src_tl[0]);
202         matrix[0][1]=-(-dest_tl[1]+dest_bl[1])/(src_br[1]-src_tl[1]);
203
204         matrix[1][0]=-(-dest_tl[0]+dest_tr[0])/(src_br[1]-src_tl[1]);
205         matrix[1][1]=-(-dest_tl[0]+dest_bl[0])/(src_br[0]-src_tl[0]);
206
207         matrix[0][2]=matrix[0][0]*dest_tl[0] + matrix[0][1]*dest_tl[1];
208         matrix[1][2]=matrix[1][0]*dest_tl[0] + matrix[1][1]*dest_tl[1];
209 */
210
211 #define matrix tmp
212         Real tmp[3][3];
213
214         const Real& x1(min(src_br[0],src_tl[0]));
215         const Real& y1(min(src_br[1],src_tl[1]));
216         const Real& x2(max(src_br[0],src_tl[0]));
217         const Real& y2(max(src_br[1],src_tl[1]));
218
219         Real tx1(dest_bl[0]);
220         Real ty1(dest_bl[1]);
221         Real tx2(dest_br[0]);
222         Real ty2(dest_br[1]);
223         Real tx3(dest_tl[0]);
224         Real ty3(dest_tl[1]);
225         Real tx4(dest_tr[0]);
226         Real ty4(dest_tr[1]);
227
228         if(src_br[0]<src_tl[0])
229                 swap(tx3,tx4),swap(ty3,ty4),swap(tx1,tx2),swap(ty1,ty2);
230
231         if(src_br[1]>src_tl[1])
232                 swap(tx3,tx1),swap(ty3,ty1),swap(tx4,tx2),swap(ty4,ty2);
233
234         Real scalex;
235         Real scaley;
236
237   scalex = scaley = 1.0;
238
239   if ((x2 - x1) > 0)
240     scalex = 1.0 / (Real) (x2 - x1);
241
242   if ((y2 - y1) > 0)
243     scaley = 1.0 / (Real) (y2 - y1);
244
245   /* Determine the perspective transform that maps from
246    * the unit cube to the transformed coordinates
247    */
248   {
249     Real dx1, dx2, dx3, dy1, dy2, dy3;
250
251     dx1 = tx2 - tx4;
252     dx2 = tx3 - tx4;
253     dx3 = tx1 - tx2 + tx4 - tx3;
254
255     dy1 = ty2 - ty4;
256     dy2 = ty3 - ty4;
257     dy3 = ty1 - ty2 + ty4 - ty3;
258
259     /*  Is the mapping affine?  */
260     if ((dx3 == 0.0) && (dy3 == 0.0))
261       {
262         matrix[0][0] = tx2 - tx1;
263         matrix[0][1] = tx4 - tx2;
264         matrix[0][2] = tx1;
265         matrix[1][0] = ty2 - ty1;
266         matrix[1][1] = ty4 - ty2;
267         matrix[1][2] = ty1;
268         matrix[2][0] = 0.0;
269         matrix[2][1] = 0.0;
270       }
271     else
272       {
273         Real det1, det2;
274
275         det1 = dx3 * dy2 - dy3 * dx2;
276         det2 = dx1 * dy2 - dy1 * dx2;
277
278         if (det1 == 0.0 && det2 == 0.0)
279           matrix[2][0] = 1.0;
280         else
281           matrix[2][0] = det1 / det2;
282
283         det1 = dx1 * dy3 - dy1 * dx3;
284
285         if (det1 == 0.0 && det2 == 0.0)
286           matrix[2][1] = 1.0;
287         else
288           matrix[2][1] = det1 / det2;
289
290         matrix[0][0] = tx2 - tx1 + matrix[2][0] * tx2;
291         matrix[0][1] = tx3 - tx1 + matrix[2][1] * tx3;
292         matrix[0][2] = tx1;
293
294         matrix[1][0] = ty2 - ty1 + matrix[2][0] * ty2;
295         matrix[1][1] = ty3 - ty1 + matrix[2][1] * ty3;
296         matrix[1][2] = ty1;
297       }
298
299     matrix[2][2] = 1.0;
300   }
301 #undef matrix
302
303         Real scaletrans[3][3]={
304                         { scalex, 0, -x1*scalex },
305                         { 0, scaley, -y1*scaley },
306                         { 0, 0, 1 }
307         };
308
309         Real t1,t2,t3;
310
311         for (int i = 0; i < 3; i++)
312     {
313       t1 = tmp[i][0];
314       t2 = tmp[i][1];
315       t3 = tmp[i][2];
316
317       for (int j = 0; j < 3; j++)
318         {
319           matrix[i][j]  = t1 * scaletrans[0][j];
320           matrix[i][j] += t2 * scaletrans[1][j];
321           matrix[i][j] += t3 * scaletrans[2][j];
322         }
323     }
324
325         mat3_invert(matrix, inv_matrix);
326 /*
327         gimp_matrix3_identity  (result);
328   gimp_matrix3_translate (result, -x1, -y1);
329   gimp_matrix3_scale     (result, scalex, scaley);
330   gimp_matrix3_mult      (&matrix, result);
331 */
332 }
333
334 bool
335 Warp::set_param(const String & param, const ValueBase &value)
336 {
337         IMPORT_PLUS(src_tl,sync());
338         IMPORT_PLUS(src_br,sync());
339         IMPORT_PLUS(dest_tl,sync());
340         IMPORT_PLUS(dest_tr,sync());
341         IMPORT_PLUS(dest_bl,sync());
342         IMPORT_PLUS(dest_br,sync());
343         IMPORT(clip);
344         IMPORT(horizon);
345
346         return false;
347 }
348
349 ValueBase
350 Warp::get_param(const String &param)const
351 {
352         EXPORT(src_tl);
353         EXPORT(src_br);
354         EXPORT(dest_tl);
355         EXPORT(dest_tr);
356         EXPORT(dest_bl);
357         EXPORT(dest_br);
358         EXPORT(clip);
359         EXPORT(horizon);
360
361         EXPORT_NAME();
362         EXPORT_VERSION();
363
364         return ValueBase();
365 }
366
367 Layer::Vocab
368 Warp::get_param_vocab()const
369 {
370         Layer::Vocab ret;
371
372         ret.push_back(ParamDesc("src_tl")
373                 .set_local_name(_("Source TL"))
374                 .set_box("src_br")
375         );
376
377         ret.push_back(ParamDesc("src_br")
378                 .set_local_name(_("Source BR"))
379         );
380
381         ret.push_back(ParamDesc("dest_tl")
382                 .set_local_name(_("Dest TL"))
383                 .set_connect("dest_tr")
384         );
385
386         ret.push_back(ParamDesc("dest_tr")
387                 .set_local_name(_("Dest TR"))
388                 .set_connect("dest_br")
389         );
390
391         ret.push_back(ParamDesc("dest_br")
392                 .set_local_name(_("Dest BR"))
393                 .set_connect("dest_bl")
394         );
395
396         ret.push_back(ParamDesc("dest_bl")
397                 .set_local_name(_("Dest BL"))
398                 .set_connect("dest_tl")
399         );
400
401         ret.push_back(ParamDesc("clip")
402                 .set_local_name(_("Clip"))
403         );
404
405         ret.push_back(ParamDesc("horizon")
406                 .set_local_name(_("Horizon"))
407         );
408
409         return ret;
410 }
411
412
413 class Warp_Trans : public Transform
414 {
415         etl::handle<const Warp> layer;
416 public:
417         Warp_Trans(const Warp* x):Transform(x->get_guid()),layer(x) { }
418
419         synfig::Vector perform(const synfig::Vector& x)const
420         {
421                 return layer->transform_backward(x);
422                 //Point pos(x-layer->origin);
423                 //return Point(layer->cos_val*pos[0]-layer->sin_val*pos[1],layer->sin_val*pos[0]+layer->cos_val*pos[1])+layer->origin;
424         }
425
426         synfig::Vector unperform(const synfig::Vector& x)const
427         {
428
429                 return layer->transform_forward(x);
430                 //Point pos(x-layer->origin);
431                 //return Point(layer->cos_val*pos[0]+layer->sin_val*pos[1],-layer->sin_val*pos[0]+layer->cos_val*pos[1])+layer->origin;
432         }
433 };
434 etl::handle<Transform>
435 Warp::get_transform()const
436 {
437         return new Warp_Trans(this);
438 }
439
440 synfig::Layer::Handle
441 Warp::hit_check(synfig::Context context, const synfig::Point &p)const
442 {
443         Point newpos(transform_forward(p));
444
445         if(clip)
446         {
447                 Rect rect(src_tl,src_br);
448                 if(!rect.is_inside(newpos))
449                         return 0;
450         }
451
452         return context.hit_check(newpos);
453 }
454
455 Color
456 Warp::get_color(Context context, const Point &p)const
457 {
458         Point newpos(transform_forward(p));
459
460         if(clip)
461         {
462                 Rect rect(src_tl,src_br);
463                 if(!rect.is_inside(newpos))
464                         return Color::alpha();
465         }
466
467         const float z(transform_backward_z(newpos));
468         if(z>0 && z<horizon)
469                 return context.get_color(newpos);
470         else
471                 return Color::alpha();
472 }
473
474 //#define ACCEL_WARP_IS_BROKEN 1
475
476 bool
477 Warp::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
478 {
479         SuperCallback stageone(cb,0,9000,10000);
480         SuperCallback stagetwo(cb,9000,10000,10000);
481
482         Real pw=(renddesc.get_w())/(renddesc.get_br()[0]-renddesc.get_tl()[0]);
483         Real ph=(renddesc.get_h())/(renddesc.get_br()[1]-renddesc.get_tl()[1]);
484
485         if(cb && !cb->amount_complete(0,10000))
486                 return false;
487
488         Point tl(renddesc.get_tl());
489         Point br(renddesc.get_br());
490
491         Rect bounding_rect;
492
493         Rect render_rect(tl,br);
494         Rect clip_rect(Rect::full_plane());
495         Rect dest_rect(dest_tl,dest_br); dest_rect.expand(dest_tr).expand(dest_bl);
496
497         Real zoom_factor(1.0);
498
499         // Quick exclusion clip, if necessary
500         if(clip && !intersect(render_rect,dest_rect))
501         {
502                 surface->set_wh(renddesc.get_w(),renddesc.get_h());
503                 surface->clear();
504                 return true;
505         }
506
507         {
508                 Rect other(render_rect);
509                 if(clip)
510                         other&=dest_rect;
511
512                 Point min(other.get_min());
513                 Point max(other.get_max());
514
515                 bool init_point_set=false;
516
517                 // Point trans_point[4];
518                 Point p;
519                 // Real trans_z[4];
520                 Real z,minz(10000000000000.0f),maxz(0);
521
522                 //! \todo checking the 4 corners for 0<=z<horizon*2 and using
523                 //! only 4 corners which satisfy this condition isn't the
524                 //! right thing to do.  It's possible that none of the 4
525                 //! corners fall within that range, and yet content of the
526                 //! tile does.
527                 p=transform_forward(min);
528                 z=transform_backward_z(p);
529                 if(z>0 && z<horizon*2)
530                 {
531                         if(init_point_set)
532                                 bounding_rect.expand(p);
533                         else
534                                 bounding_rect=Rect(p);
535                         init_point_set=true;
536                         maxz=std::max(maxz,z);
537                         minz=std::min(minz,z);
538                 }
539
540                 p=transform_forward(max);
541                 z=transform_backward_z(p);
542                 if(z>0 && z<horizon*2)
543                 {
544                         if(init_point_set)
545                                 bounding_rect.expand(p);
546                         else
547                                 bounding_rect=Rect(p);
548                         init_point_set=true;
549                         maxz=std::max(maxz,z);
550                         minz=std::min(minz,z);
551                 }
552
553                 swap(min[1],max[1]);
554
555                 p=transform_forward(min);
556                 z=transform_backward_z(p);
557                 if(z>0 && z<horizon*2)
558                 {
559                         if(init_point_set)
560                                 bounding_rect.expand(p);
561                         else
562                                 bounding_rect=Rect(p);
563                         init_point_set=true;
564                         maxz=std::max(maxz,z);
565                         minz=std::min(minz,z);
566                 }
567
568                 p=transform_forward(max);
569                 z=transform_backward_z(p);
570                 if(z>0 && z<horizon*2)
571                 {
572                         if(init_point_set)
573                                 bounding_rect.expand(p);
574                         else
575                                 bounding_rect=Rect(p);
576                         init_point_set=true;
577                         maxz=std::max(maxz,z);
578                         minz=std::min(minz,z);
579                 }
580
581                 if(!init_point_set)
582                 {
583                         surface->set_wh(renddesc.get_w(),renddesc.get_h());
584                         surface->clear();
585                         return true;
586                 }
587                 zoom_factor=(1+(maxz-minz));
588
589         }
590
591 #ifdef ACCEL_WARP_IS_BROKEN
592         return Layer::accelerated_render(context,surface,quality,renddesc, cb);
593 #else
594
595         /*swap(tl[1],br[1]);
596         bounding_rect
597                 .expand(transform_forward(tl))
598                 .expand(transform_forward(br))
599         ;
600         swap(tl[1],br[1]);*/
601
602         //synfig::warning("given window: [%f,%f]-[%f,%f] %dx%d",tl[0],tl[1],br[0],br[1],renddesc.get_w(),renddesc.get_h());
603         //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]);
604
605         // If we are clipping, then go ahead and clip to the
606         // source rectangle
607         if(clip)
608                 clip_rect&=Rect(src_tl,src_br);
609
610         // Bound ourselves to the bounding rectangle of
611         // what is under us
612         clip_rect&=context.get_full_bounding_rect();//.expand_x(abs(zoom_factor/pw)).expand_y(abs(zoom_factor/ph));
613
614         bounding_rect&=clip_rect;
615
616         Point min_point(bounding_rect.get_min());
617         Point max_point(bounding_rect.get_max());
618
619         // we're going to divide by the difference of these pairs soon;
620         // if they're the same, we'll be dividing by zero, and we don't
621         // want to do that!
622         // \todo what should we do in this case?
623         if (min_point[0] == max_point[0]) max_point[0] += 0.001;
624         if (min_point[1] == max_point[1]) max_point[1] += 0.001;
625
626         if(tl[0]>br[0])
627         {
628                 tl[0]=max_point[0];
629                 br[0]=min_point[0];
630         }
631         else
632         {
633                 br[0]=max_point[0];
634                 tl[0]=min_point[0];
635         }
636         if(tl[1]>br[1])
637         {
638                 tl[1]=max_point[1];
639                 br[1]=min_point[1];
640         }
641         else
642         {
643                 br[1]=max_point[1];
644                 tl[1]=min_point[1];
645         }
646
647
648
649         const int tmp_d(max(renddesc.get_w(),renddesc.get_h()));
650         Real src_pw=(tmp_d*zoom_factor)/(br[0]-tl[0]);
651         Real src_ph=(tmp_d*zoom_factor)/(br[1]-tl[1]);
652
653
654         RendDesc desc(renddesc);
655         desc.clear_flags();
656         //desc.set_flags(RendDesc::PX_ASPECT);
657         desc.set_tl(tl);
658         desc.set_br(br);
659         desc.set_wh(ceil_to_int(src_pw*(br[0]-tl[0])),ceil_to_int(src_ph*(br[1]-tl[1])));
660
661         //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());
662         if(desc.get_w()==0 && desc.get_h()==0)
663         {
664                 surface->set_wh(renddesc.get_w(),renddesc.get_h());
665                 surface->clear();
666                 return true;
667         }
668
669         // Recalculate the pixel widths for the src renddesc
670         src_pw=(desc.get_w())/(desc.get_br()[0]-desc.get_tl()[0]);
671         src_ph=(desc.get_h())/(desc.get_br()[1]-desc.get_tl()[1]);
672
673
674         Surface source;
675         source.set_wh(desc.get_w(),desc.get_h());
676
677         if(!context.accelerated_render(&source,quality,desc,&stageone))
678                 return false;
679
680         surface->set_wh(renddesc.get_w(),renddesc.get_h());
681         surface->clear();
682
683         Surface::pen pen(surface->begin());
684
685         if(quality<=4)
686         {
687                 // CUBIC
688                 int x,y;
689                 float u,v;
690                 Point point,tmp;
691                 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)
692                 {
693                         for(x=0,point[0]=renddesc.get_tl()[0];x<surface->get_w();x++,pen.inc_x(),point[0]+=1.0/pw)
694                         {
695                                 tmp=transform_forward(point);
696                                 const float z(transform_backward_z(tmp));
697                                 if(!clip_rect.is_inside(tmp) || !(z>0 && z<horizon))
698                                 {
699                                         (*surface)[y][x]=Color::alpha();
700                                         continue;
701                                 }
702
703                                 u=(tmp[0]-tl[0])*src_pw;
704                                 v=(tmp[1]-tl[1])*src_ph;
705
706                                 if(u<0 || v<0 || u>=source.get_w() || v>=source.get_h() || isnan(u) || isnan(v))
707                                         (*surface)[y][x]=context.get_color(tmp);
708                                 else
709                                         (*surface)[y][x]=source.cubic_sample(u,v);
710                         }
711                         if((y&31)==0 && cb)
712                         {
713                                 if(!stagetwo.amount_complete(y,surface->get_h()))
714                                         return false;
715                         }
716                 }
717         }
718         else
719         if(quality<=6)
720         {
721                 // INTERPOLATION_LINEAR
722                 int x,y;
723                 float u,v;
724                 Point point,tmp;
725                 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)
726                 {
727                         for(x=0,point[0]=renddesc.get_tl()[0];x<surface->get_w();x++,pen.inc_x(),point[0]+=1.0/pw)
728                         {
729                                 tmp=transform_forward(point);
730                                 const float z(transform_backward_z(tmp));
731                                 if(!clip_rect.is_inside(tmp) || !(z>0 && z<horizon))
732                                 {
733                                         (*surface)[y][x]=Color::alpha();
734                                         continue;
735                                 }
736
737                                 u=(tmp[0]-tl[0])*src_pw;
738                                 v=(tmp[1]-tl[1])*src_ph;
739
740                                 if(u<0 || v<0 || u>=source.get_w() || v>=source.get_h() || isnan(u) || isnan(v))
741                                         (*surface)[y][x]=context.get_color(tmp);
742                                 else
743                                         (*surface)[y][x]=source.linear_sample(u,v);
744                         }
745                         if((y&31)==0 && cb)
746                         {
747                                 if(!stagetwo.amount_complete(y,surface->get_h()))
748                                         return false;
749                         }
750                 }
751         }
752         else
753         {
754                 // NEAREST_NEIGHBOR
755                 int x,y;
756                 float u,v;
757                 Point point,tmp;
758                 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)
759                 {
760                         for(x=0,point[0]=renddesc.get_tl()[0];x<surface->get_w();x++,pen.inc_x(),point[0]+=1.0/pw)
761                         {
762                                 tmp=transform_forward(point);
763                                 const float z(transform_backward_z(tmp));
764                                 if(!clip_rect.is_inside(tmp) || !(z>0 && z<horizon))
765                                 {
766                                         (*surface)[y][x]=Color::alpha();
767                                         continue;
768                                 }
769
770                                 u=(tmp[0]-tl[0])*src_pw;
771                                 v=(tmp[1]-tl[1])*src_ph;
772
773                                 if(u<0 || v<0 || u>=source.get_w() || v>=source.get_h() || isnan(u) || isnan(v))
774                                         (*surface)[y][x]=context.get_color(tmp);
775                                 else
776                                         //pen.set_value(source[v][u]);
777                                         (*surface)[y][x]=source[floor_to_int(v)][floor_to_int(u)];
778                         }
779                         if((y&31)==0 && cb)
780                         {
781                                 if(!stagetwo.amount_complete(y,surface->get_h()))
782                                         return false;
783                         }
784                 }
785         }
786
787 #endif
788
789         if(cb && !cb->amount_complete(10000,10000)) return false;
790
791         return true;
792 }
793
794 synfig::Rect
795 Warp::get_bounding_rect()const
796 {
797         return Rect::full_plane();
798 }
799
800 synfig::Rect
801 Warp::get_full_bounding_rect(Context context)const
802 {
803 //      return Rect::full_plane();
804
805         Rect under(context.get_full_bounding_rect());
806
807         if(clip)
808         {
809                 under&=Rect(src_tl,src_br);
810         }
811
812         return get_transform()->perform(under);
813
814         /*
815         Rect under(context.get_full_bounding_rect());
816         Rect ret(Rect::zero());
817
818         if(under.area()==HUGE_VAL)
819                 return Rect::full_plane();
820
821         ret.expand(
822                 transform_backward(
823                         under.get_min()
824                 )
825         );
826         ret.expand(
827                 transform_backward(
828                         under.get_max()
829                 )
830         );
831         ret.expand(
832                 transform_backward(
833                         Vector(
834                                 under.get_min()[0],
835                                 under.get_max()[1]
836                         )
837                 )
838         );
839         ret.expand(
840                 transform_backward(
841                         Vector(
842                                 under.get_max()[0],
843                                 under.get_min()[1]
844                         )
845                 )
846         );
847
848         if(ret.area()==HUGE_VAL)
849                 return Rect::full_plane();
850
851         return ret;
852         */
853 }