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