a40d0c27e8bd4f161a0407bab8c95883e6b21897
[goodguy/cinelerra.git] / cinelerra-5.1 / plugins / tracer / tracer.C
1 /*
2  * CINELERRA
3  * Copyright (C) 1997-2015 Adam Williams <broadcast at earthling dot net>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  */
20
21 #include<stdio.h>
22 #include<stdint.h>
23 #include<math.h>
24 #include<string.h>
25 #include<float.h>
26
27 #include "arraylist.h"
28 #include "bccmodels.h"
29 #include "bccolors.h"
30 #include "clip.h"
31 #include "edlsession.h"
32 #include "filexml.h"
33 #include "tracer.h"
34 #include "tracerwindow.h"
35 #include "language.h"
36 #include "vframe.h"
37
38 REGISTER_PLUGIN(Tracer)
39
40 void tracer_pgm(const char *fn,VFrame *vfrm)
41 {
42         FILE *fp = fopen(fn,"w");
43         int w = vfrm->get_w(), h = vfrm->get_h();
44         fprintf(fp,"P5\n%d %d\n255\n",w,h);
45         fwrite(vfrm->get_data(),w,h,fp);
46         fclose(fp);
47 }
48
49 TracerPoint::TracerPoint(float x, float y)
50 {
51         this->x = x;      this->y = y;
52 }
53 TracerPoint::~TracerPoint()
54 {
55 }
56
57 TracerConfig::TracerConfig()
58 {
59         drag = draw = 1;  fill = 0;
60         feather = 0;  radius = 1;
61         invert = 0;  selected = -1;
62 }
63 TracerConfig::~TracerConfig()
64 {
65 }
66
67 int TracerConfig::equivalent(TracerConfig &that)
68 {
69         if( this->drag != that.drag ) return 0;
70         if( this->draw != that.draw ) return 0;
71         if( this->fill != that.fill ) return 0;
72         if( this->feather != that.feather ) return 0;
73         if( this->invert != that.invert ) return 0;
74         if( this->radius != that.radius ) return 0;
75         if( this->points.size() != that.points.size() ) return 0;
76         for( int i=0, n=points.size(); i<n; ++i ) {
77                 TracerPoint *ap = this->points[i], *bp = that.points[i];
78                 if( !EQUIV(ap->x, bp->x) ) return 0;
79                 if( !EQUIV(ap->y, bp->y) ) return 0;
80         }
81         return 1;
82 }
83
84 void TracerConfig::copy_from(TracerConfig &that)
85 {
86         this->drag = that.drag;
87         this->draw = that.draw;
88         this->fill = that.fill;
89         this->selected = that.selected;
90         this->feather = that.feather;
91         this->invert = that.invert;
92         this->radius = that.radius;
93         points.remove_all_objects();
94         for( int i=0,n=that.points.size(); i<n; ++i ) {
95                 TracerPoint *pt = that.points[i];
96                 add_point(pt->x, pt->y);
97         }
98 }
99
100 void TracerConfig::interpolate(TracerConfig &prev, TracerConfig &next,
101                 long prev_frame, long next_frame, long current_frame)
102 {
103         copy_from(prev);
104 }
105
106 void TracerConfig::limits()
107 {
108 }
109
110 int TracerConfig::add_point(float x, float y)
111 {
112         int i = points.size();
113         points.append(new TracerPoint(x, y));
114         return i;
115 }
116
117 void TracerConfig::del_point(int i)
118 {
119         points.remove_object_number(i);
120 }
121
122
123 Tracer::Tracer(PluginServer *server)
124  : PluginVClient(server)
125 {
126         frm = 0; frm_rows = 0;
127         msk = 0; msk_rows = 0;
128         edg = 0; edg_rows = 0;
129         w = 0;   w1 = w-1;
130         h = 0;   h1 = h-1;
131         color_model = bpp = 0;
132         is_float = is_yuv = 0;
133         comps = comp = 0;
134         ax = 0;  ay = 0;
135         bx = 0;  by = 0;
136         cx = 0;  cy = 0;
137         ex = 0;  ey = 0;
138 }
139
140 Tracer::~Tracer()
141 {
142         delete edg;
143         delete msk;
144 }
145
146 const char* Tracer::plugin_title() { return N_("Tracer"); }
147 int Tracer::is_realtime() { return 1; }
148
149 NEW_WINDOW_MACRO(Tracer, TracerWindow);
150 int Tracer::load_configuration1()
151 {
152         KeyFrame *prev_keyframe = get_prev_keyframe(get_source_position());
153         if( prev_keyframe->position == get_source_position() ) {
154                 read_data(prev_keyframe);
155                 return 1;
156         }
157         return load_configuration();
158 }
159 LOAD_CONFIGURATION_MACRO(Tracer, TracerConfig);
160
161 int Tracer::new_point()
162 {
163         EDLSession *session = get_edlsession();
164         float x = !session ? 0.f : session->output_w / 2.f;
165         float y = !session ? 0.f : session->output_h / 2.f;
166         return config.add_point(x, y);
167 }
168
169 void Tracer::save_data(KeyFrame *keyframe)
170 {
171         FileXML output;
172
173 // cause data to be stored directly in text
174         output.set_shared_output(keyframe->xbuf);
175
176         output.tag.set_title("TRACER");
177         output.tag.set_property("DRAG", config.drag);
178         output.tag.set_property("DRAW", config.draw);
179         output.tag.set_property("FILL", config.fill);
180         output.tag.set_property("FEATHER", config.feather);
181         output.tag.set_property("RADIUS", config.radius);
182         output.tag.set_property("INVERT", config.invert);
183         output.tag.set_property("SELECTED", config.selected);
184         output.append_tag();
185         output.append_newline();
186         output.tag.set_title("/TRACER");
187         output.append_tag();
188         output.append_newline();
189         for( int i=0, n=config.points.size(); i<n; ++i ) {
190                 TracerPoint *pt = config.points[i];
191                 char point[BCSTRLEN];
192                 sprintf(point,"/POINT_%d",i+1);
193                 output.tag.set_title(point+1);
194                 output.tag.set_property("X", pt->x);
195                 output.tag.set_property("Y", pt->y);
196                 output.append_tag();
197                 output.tag.set_title(point+0);
198                 output.append_tag();
199                 output.append_newline();
200         }
201         output.terminate_string();
202 }
203
204 void Tracer::read_data(KeyFrame *keyframe)
205 {
206         FileXML input;
207         input.set_shared_input(keyframe->xbuf);
208         config.points.remove_all_objects();
209         int result = 0;
210
211         while( !(result=input.read_tag()) ) {
212                 if( input.tag.title_is("TRACER") ) {
213                         config.drag = input.tag.get_property("DRAG", config.drag);
214                         config.draw = input.tag.get_property("DRAW", config.draw);
215                         config.fill = input.tag.get_property("FILL", config.fill);
216                         config.feather = input.tag.get_property("FEATHER", config.feather);
217                         config.radius = input.tag.get_property("RADIUS", config.radius);
218                         config.invert = input.tag.get_property("INVERT", config.invert);
219                         config.selected = input.tag.get_property("SELECTED", 0);
220                         config.limits();
221                 }
222                 else if( !strncmp(input.tag.get_title(),"POINT_",6) ) {
223                         float x = input.tag.get_property("X", 0.f);
224                         float y = input.tag.get_property("Y", 0.f);
225                         config.add_point(x, y);
226                 }
227         }
228 }
229
230 void Tracer::update_gui()
231 {
232         if( !thread ) return;
233         thread->window->lock_window("Tracer::update_gui");
234         TracerWindow *window = (TracerWindow*)thread->window;
235         if( load_configuration1() ) {
236                 window->update_gui();
237                 window->flush();
238         }
239         thread->window->unlock_window();
240 }
241
242 void Tracer::draw_point(TracerPoint *pt)
243 {
244         int d = bmax(w,h) / 200 + 2;
245         int r = d/2+1, x = pt->x, y = pt->y;
246         frm->draw_smooth(x-r,y+0, x-r, y-r, x+0,y-r);
247         frm->draw_smooth(x+0,y-r, x+r, y-r, x+r,y+0);
248         frm->draw_smooth(x+r,y+0, x+r, y+r, x+0,y+r);
249         frm->draw_smooth(x+0,y+r, x-r, y+r, x-r,y+0);
250 }
251
252 void Tracer::draw_points()
253 {
254         for( int i=0, n=config.points.size(); i<n; ++i ) {
255                 TracerPoint *pt = config.points[i];
256                 frm->set_pixel_color(config.selected == i ? GREEN : WHITE);
257                 draw_point(pt);
258         }
259 }
260 void Tracer::draw_edge()
261 {
262         float scale = 1 / 255.0f;
263         int color_model = frm->get_color_model();
264         int bpp = BC_CModels::calculate_pixelsize(color_model);
265         switch( color_model ) {
266         case BC_RGB_FLOAT:
267                 for( int y=0; y<h; ++y ) {
268                         uint8_t *sp = frm_rows[y], *ep = edg_rows[y];
269                         for( int x=0; x<w; ++x,++ep,sp+=bpp ) {
270                                 if( !*ep ) continue;
271                                 float a = *ep * scale;
272                                 float *px = (float *)sp;
273                                 px[0] = px[1] = px[2] = a;
274                         }
275                 }
276                 break;
277         case BC_RGBA_FLOAT:
278                 for( int y=0; y<h; ++y ) {
279                         uint8_t *sp = frm_rows[y], *ep = edg_rows[y];
280                         for( int x=0; x<w; ++x,++ep,sp+=bpp ) {
281                                 if( !*ep ) continue;
282                                 float a = *ep * scale;
283                                 float *px = (float *)sp;
284                                 px[0] = px[1] = px[2] = a;
285                                 px[3] = 1.0f;
286                         }
287                 }
288                 break;
289         case BC_RGB888:
290                 for( int y=0; y<h; ++y ) {
291                         uint8_t *sp = frm_rows[y], *ep = edg_rows[y];
292                         for( int x=0; x<w; ++x,++ep,sp+=bpp ) {
293                                 if( !*ep ) continue;
294                                 float a = *ep * scale;
295                                 uint8_t *px = sp;
296                                 px[0] = px[1] = px[2] = a * 255;
297                         }
298                 }
299                 break;
300         case BC_RGBA8888:
301                 for( int y=0; y<h; ++y ) {
302                         uint8_t *sp = frm_rows[y], *ep = edg_rows[y];
303                         for( int x=0; x<w; ++x,++ep,sp+=bpp ) {
304                                 if( !*ep ) continue;
305                                 float a = *ep * scale;
306                                 uint8_t *px = sp;
307                                 px[0] = px[1] = px[2] = a * 255;
308                                 px[3] = 0xff;
309                         }
310                 }
311                 break;
312         case BC_YUV888:
313                 for( int y=0; y<h; ++y ) {
314                         uint8_t *sp = frm_rows[y], *ep = edg_rows[y];
315                         for( int x=0; x<w; ++x,++ep,sp+=bpp ) {
316                                 if( !*ep ) continue;
317                                 float a = *ep * scale;
318                                 uint8_t *px = sp;
319                                 px[0] = a * 255;
320                                 px[1] = px[2] = 0x80;
321                         }
322                 }
323                 break;
324         case BC_YUVA8888:
325                 for( int y=0; y<h; ++y ) {
326                         uint8_t *sp = frm_rows[y], *ep = edg_rows[y];
327                         for( int x=0; x<w; ++x,++ep,sp+=bpp ) {
328                                 if( !*ep ) continue;
329                                 float a = *ep * scale;
330                                 uint8_t *px = sp;
331                                 px[0] = a * 255;
332                                 px[1] = px[2] = 0x80;
333                                 px[3] = 0xff;
334                         }
335                 }
336                 break;
337         }
338 }
339
340
341 #define PIX_GRADIENT(type, ix, iy) do { \
342         int xi = vx+ix, yi = vy+iy; \
343         if( edg_rows[yi][xi] ) break; \
344         type *px = (type *)(frm_rows[yi] + xi*bpp); \
345         float dv = px[0]-xp[0], v = dv*dv; \
346         for( int c=1; c<comp; ++c ) { \
347                 dv = px[c]-xp[c]; v += dv*dv; \
348         } \
349         v = sqrt(v); \
350         if( vmax < v ) vmax = v; \
351 } while(0)
352 #define ROW_GRADIENT(type, iy) do { \
353         if( vx > 0 ) PIX_GRADIENT(type,-1, iy); \
354         if( iy != 0) PIX_GRADIENT(type, 0, iy); \
355         if( vx < w1) PIX_GRADIENT(type, 1, iy); \
356 } while(0)
357 #define MAX_GRADIENT(type) do { \
358         type *xp = (type *)(frm_rows[vy] + vx*bpp); \
359         if( vy > 0 ) ROW_GRADIENT(type,-1); \
360         ROW_GRADIENT(type, 0); \
361         if( vy < h1 ) ROW_GRADIENT(type, 1); \
362 } while(0)
363
364 #define MAX_PIXEL(type, ix, iy) do { \
365         int vx = cx + ix, vy = cy + iy; \
366         if( edg_rows[vy][vx] ) break; \
367         float vv = FLT_MAX; \
368         int dx = ex-vx, dy = ey-vy; \
369         int rr = dx*dx + dy*dy; \
370         if( rr > dd ) break; \
371         if( rr > 0 ) { \
372                 float r = (float)(ix*dx + iy*dy) / rr; \
373                 float vmax = 0; \
374                 MAX_GRADIENT(type); \
375                 vv = r + vmax; \
376         } \
377         if( maxv < vv ) { \
378                 maxv = vv; \
379                 nx = vx;  ny = vy; \
380         } \
381 } while(0)
382 #define ROW_MAX(type, iy) do { \
383         if( cx >  0 ) MAX_PIXEL(type,-1, iy); \
384         if( iy != 0 ) MAX_PIXEL(type, 0, iy); \
385         if( cx < w1 ) MAX_PIXEL(type, 1, iy); \
386 } while(0)
387
388 int Tracer::step()
389 {
390         int ret = 0;
391         if( !edg_rows[cy][cx] ) {
392                 points.add(cx,cy);
393                 edg_rows[cy][cx] = 0xff;
394         }
395         int dx = ex-cx, dy = ey-cy;
396         int dd = dx*dx + dy*dy;
397         if( !dd ) return ret;
398         int nx = cx, ny = cy;
399         double maxv = -FLT_MAX;
400         if( cy > 0 )  ROW_MAX(uint8_t,-1);
401         ROW_MAX(uint8_t, 0);
402         if( cy < h1 ) ROW_MAX(uint8_t, 1);
403         cx = nx;  cy = ny;
404         return maxv > 0 ? 1 : 0;
405 }
406
407 void Tracer::trace(int i0, int i1)
408 {
409         TracerPoint *pt0 = config.points[i0];
410         TracerPoint *pt1 = config.points[i1];
411         cx = pt0->x;  bclamp(cx, 0, w1);
412         cy = pt0->y;  bclamp(cy, 0, h1);
413         ex = pt1->x;  bclamp(ex, 0, w1);
414         ey = pt1->y;  bclamp(ey, 0, h1);
415         while( step() );
416 }
417
418 int Tracer::smooth()
419 {
420         int &n = points.total, m = 0;
421         if( n < 3 ) return m;
422         TracePoint *ap;
423         TracePoint *bp = &points[0];
424         TracePoint *cp = &points[1];
425         for( ; n>=3; --n ) {
426                 ap = &points[n-1];
427                 if( abs(ap->x-cp->x)<2 && abs(ap->y-cp->y)<2 ) {
428                         edg_rows[bp->y][bp->x] = 0;
429                         ++m;
430                 }
431                 else
432                         break;
433         }
434         if( n < 3 ) return m;
435         ap = &points[0];
436         bp = &points[1];
437         for( int i=2; i<n; ++i ) {
438                 cp = &points[i];
439                 if( abs(ap->x-cp->x)<2 && abs(ap->y-cp->y)<2 &&
440                     ( (bp->x==ap->x || bp->x==cp->x) &&
441                       (bp->y==ap->y || bp->y==cp->y) ) ) {
442                         edg_rows[bp->y][bp->x] = 0;
443                         ++m;
444                 }
445                 else {
446                         ++ap;  ++bp;
447                 }
448                 bp->x = cp->x;  bp->y = cp->y;
449         }
450         n -= m;
451         return m;
452 }
453
454 #if 0
455 int winding2(int x, int y, TracePoints &pts, int n)
456 {
457         int w = 0;
458         int x0 = pts[0].x-x, y0 = pts[0].y-y;
459         for( int x1,y1,i=1; i<n; x0=x1,y0=y1,++i ) { 
460                 x1 = pts[i].x-x;  y1 = pts[i].y-y;
461                 if( y0*y1 < 0 ) {                          // crosses x axis
462                         int xi = x0 - y0*(x1-x0)/(y1-y0);  // x-intercept
463                         if( xi > 0 ) w += y0<0 ? 2 : -2;   // crosses x on plus side
464                 }
465                 else if( !y0 && x0 > 0 ) w += y1>0 ? 1 : -1;
466                 else if( !y1 && x1 > 0 ) w += y0>0 ? 1 : -1;
467         }
468         return w;
469 }
470 #endif
471
472 class FillRegion
473 {
474         class segment { public: int y, lt, rt; };
475         ArrayList<segment> stack;
476
477         void push(int y, int lt, int rt) {
478                 segment &seg = stack.append();
479                 seg.y = y;  seg.lt = lt;  seg.rt = rt;
480         }
481         void pop(int &y, int &lt, int &rt) {
482                 segment &seg = stack.last();
483                 y = seg.y;  lt = seg.lt;  rt = seg.rt;
484                 stack.remove();
485         }
486  
487         int w, h;
488         uint8_t *edg;
489         uint8_t *msk;
490         bool edge_pixel(int i) { return edg[i] > 0; }
491
492 public:
493         void fill(int x, int y);
494         void run();
495
496         FillRegion(VFrame *edg, VFrame *msk);
497 };
498
499 FillRegion::FillRegion(VFrame *edg, VFrame *msk)
500 {
501         this->w = msk->get_w();
502         this->h = msk->get_h();
503         this->msk = (uint8_t*) msk->get_data();
504         this->edg = (uint8_t*) edg->get_data();
505 }
506
507 void FillRegion::fill(int x, int y)
508 {
509         push(y, x, x);
510 }
511
512 void FillRegion::run()
513 {
514         while( stack.size() > 0 ) {
515                 int y, ilt, irt;
516                 pop(y, ilt, irt);
517                 int ofs = y*w + ilt;
518                 for( int x=ilt; x<=irt; ++x,++ofs ) {
519                         if( msk[ofs] ) continue;
520                         msk[ofs] = 0xff;
521                         if( edge_pixel(ofs) ) continue;
522                         int lt = x, rt = x;
523                         int lofs = ofs;
524                         for( int i=lt; --i>=0; lt=i ) {
525                                 if( msk[--lofs] ) break;
526                                 msk[lofs] = 0xff;
527                                 if( edge_pixel(lofs) ) break;
528                         }
529                         int rofs = ofs;
530                         for( int i=rt; ++i< w; rt=i ) {
531                                 if( msk[++rofs] ) break;
532                                 msk[rofs] = 0xff;
533                                 if( edge_pixel(rofs) ) break;
534                         }
535                         if( y+1 <  h ) push(y+1, lt, rt);
536                         if( y-1 >= 0 ) push(y-1, lt, rt);
537                 }
538         }
539 }
540
541 void Tracer::feather(int r, double s)
542 {
543         if( !r ) return;
544         int dir = r < 0 ? (r=-r, -1) : 1;
545         int rr = r * r;
546         int psf[rr];  // pt spot fn
547         float p = powf(10.f, s/2);
548         for( int i=0; i<rr; ++i ) {
549                 float v = powf((float)i/rr,p);
550                 if( dir < 0 ) v = 1-v;
551                 int vv = v*256;
552                 if( vv > 255 ) vv = 255;
553                 psf[i] = vv;
554         }
555         for( int i=0,n=points.size(); i<n; ++i ) {
556                 TracePoint *pt = &points[i];
557                 int xs = pt->x-r, xn=pt->x+r;
558                 bclamp(xs, 0, w);
559                 bclamp(xn, 0, w);
560                 int ys = pt->y-r, yn=pt->y+r;
561                 bclamp(ys, 0, h);
562                 bclamp(yn, 0, h);
563                 for( int y=ys ; y<yn; ++y ) {
564                         for( int x=xs; x<xn; ++x ) {
565                                 int dx = x-pt->x, dy = y-pt->y;
566                                 int dd = dx*dx + dy*dy;
567                                 if( dd >= rr ) continue;
568                                 int v = psf[dd], px = msk_rows[y][x];
569                                 if( dir < 0 ? px<v : px>v ) msk_rows[y][x] = v;
570                         }
571                 }
572         }
573 }
574
575 void Tracer::draw_mask()
576 {
577         switch( color_model ) {
578         case BC_RGB888:
579                 for( int y=0; y<h; ++y ) {
580                         uint8_t *mp = msk_rows[y];
581                         uint8_t *rp = frm_rows[y];
582                         for( int x=0; x<w; rp+=bpp,++x ) {
583                                 int a = !config.invert ? 0xff-mp[x] : mp[x];
584                                 rp[0] = a*rp[0] / 0xff;
585                                 rp[1] = a*rp[1] / 0xff;
586                                 rp[2] = a*rp[2] / 0xff;
587                         }
588                 }
589                 break;
590         case BC_YUV888:
591                 for( int y=0; y<h; ++y ) {
592                         uint8_t *mp = msk_rows[y];
593                         uint8_t *rp = frm_rows[y];
594                         for( int x=0; x<w; rp+=bpp,++x ) {
595                                 int a = !config.invert ? 0xff-mp[x] : mp[x];
596                                 rp[0] = a*rp[0] / 0xff;
597                                 rp[1] = a*(rp[1]-0x80)/0xff + 0x80;
598                                 rp[2] = a*(rp[2]-0x80)/0xff + 0x80;
599                         }
600                 }
601                 break;
602         case BC_RGB_FLOAT:
603                 for( int y=0; y<h; ++y ) {
604                         uint8_t *mp = msk_rows[y];
605                         uint8_t *rp = frm_rows[y];
606                         for( int x=0; x<w; rp+=bpp,++x ) {
607                                 float a = !config.invert ? 1-mp[x]/255.f : mp[x]/255.f;
608                                 float *fp = (float*)rp;
609                                 fp[0] *= a;  fp[1] *= a;  fp[2] *= a;
610                         }
611                 }
612                 break;
613         case BC_RGBA8888:
614         case BC_YUVA8888:
615                 for( int y=0; y<h; ++y ) {
616                         uint8_t *mp = msk_rows[y];
617                         uint8_t *rp = frm_rows[y];
618                         for( int x=0; x<w; rp+=bpp,++x ) {
619                                 rp[3] = !config.invert ? 0xff-mp[x] : mp[x];
620                         }
621                 }
622                 break;
623         case BC_RGBA_FLOAT:
624                 for( int y=0; y<h; ++y ) {
625                         uint8_t *mp = msk_rows[y];
626                         uint8_t *rp = frm_rows[y];
627                         for( int x=0; x<w; rp+=bpp,++x ) {
628                                 float *fp = (float*)rp;
629                                 fp[3] = !config.invert ? 1-mp[x]/255.f : mp[x]/255.f;
630                         }
631                 }
632                 break;
633         }
634 }
635
636 int Tracer::process_buffer(VFrame *frame, int64_t start_position, double frame_rate)
637 {
638         int redraw = load_configuration1();
639         frm = frame;
640         frm_rows = frm->get_rows();
641         w = frm->get_w();  w1 = w-1;
642         h = frm->get_h();  h1 = h-1;
643         color_model = frm->get_color_model();
644         bpp = BC_CModels::calculate_pixelsize(color_model);
645         is_float = BC_CModels::is_float(color_model);
646         is_yuv = BC_CModels::is_yuv(color_model);
647         has_alpha = BC_CModels::has_alpha(color_model);
648         comps = BC_CModels::components(color_model);
649         comp = bmin(comps, 3);
650         read_frame(frm, 0, start_position, frame_rate, 0);
651         if( !edg ) redraw = 1;
652         VFrame::get_temp(edg, w, h, BC_GREY8);
653         if( redraw ) {
654                 edg->clear_frame();
655                 edg_rows = edg->get_rows();
656
657                 int n = config.points.size()-1;
658                 points.clear();
659                 for( int i=0; i<n; ++i )
660                         trace(i,i+1);
661                 if( n > 0 )
662                         trace(n,0);
663                 while( smooth() > 0 );
664
665                 if( config.fill && points.size() > 2 ) {
666                         int l = points.size(), l2 = l/2;
667                         TracePoint *pt0 = &points[0], *pt1 = &points[l2];
668                         int cx = (pt0->x+pt1->x)/2, cy = (pt0->y+pt1->y)/2;
669                         VFrame::get_temp(msk, w, h, BC_GREY8);
670                         msk->clear_frame();
671                         msk_rows = msk->get_rows();
672
673                         FillRegion fill_region(edg, msk);
674                         fill_region.fill(cx, cy);
675                         fill_region.run();
676
677                         feather(config.feather, config.radius);
678                 }
679         }
680         if( config.fill && msk )
681                 draw_mask();
682         if( config.draw && edg )
683                 draw_edge();
684         if( config.drag )
685                 draw_points();
686         return 0;
687 }
688