272bcf0458ec7f4618dac0eaf11080a2786f635e
[goodguy/cinelerra.git] / cinelerra-5.1 / plugins / sketcher / sketcher.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
26 #include "arraylist.h"
27 #include "bccmodels.h"
28 #include "bccolors.h"
29 #include "clip.h"
30 #include "edl.h"
31 #include "edlsession.h"
32 #include "filexml.h"
33 #include "overlayframe.h"
34 #include "pluginserver.h"
35 #include "preferences.h"
36 #include "sketcher.h"
37 #include "sketcherwindow.h"
38 #include "language.h"
39 #include "vframe.h"
40
41 void SketcherPoint::init(int id, int arc, coord x, coord y)
42 {
43         this->id = id;  this->arc = arc;
44         this->x = x;    this->y = y;
45 }
46 SketcherPoint::SketcherPoint(int id)
47 {
48         init(id, ARC_LINE, 0, 0);
49 }
50 SketcherPoint::SketcherPoint(int id, int arc, coord x, coord y)
51 {
52         init(id, arc, x, y);
53 }
54 SketcherPoint::~SketcherPoint()
55 {
56 }
57 SketcherPoint::SketcherPoint(SketcherPoint &pt)
58 {
59         copy_from(pt);
60 }
61 int SketcherPoint::equivalent(SketcherPoint &that)
62 {
63         return this->id == that.id &&
64                 this->arc == that.arc &&
65                 EQUIV(this->x, that.x) &&
66                 EQUIV(this->y, that.y) ? 1 : 0;
67 }
68 void SketcherPoint::copy_from(SketcherPoint &that)
69 {
70         this->id = that.id;  this->arc = that.arc;
71         this->x = that.x;    this->y = that.y;
72 }
73 void SketcherPoint::save_data(FileXML &output)
74 {
75         char point[BCSTRLEN];
76         sprintf(point,"/POINT_%d",id);
77         output.tag.set_title(point+1);
78         output.tag.set_property("TYPE", arc);
79         output.tag.set_property("X", x);
80         output.tag.set_property("Y", y);
81         output.append_tag();
82         output.tag.set_title(point+0);
83         output.append_tag();
84         output.append_newline();
85 }
86 void SketcherPoint::read_data(FileXML &input)
87 {
88         id = atoi(input.tag.get_title() + 6);
89         arc = input.tag.get_property("TYPE", ARC_OFF);
90         x = input.tag.get_property("X", (coord)0);
91         y = input.tag.get_property("Y", (coord)0);
92         bclamp(arc, 0, ARC_SZ-1);
93 }
94
95 void SketcherCurve::init(int id, int pen, int width, int color)
96 {
97         this->id = id;
98         this->width = width;
99         this->pen = pen;
100         this->color = color;
101 }
102 SketcherCurve::SketcherCurve(int id)
103 {
104         init(id, 1, PEN_SQUARE, CV_COLOR);
105 }
106 SketcherCurve::SketcherCurve(int id, int pen, int width, int color)
107 {
108         init(id, pen, width, color);
109 }
110 SketcherCurve::~SketcherCurve()
111 {
112 }
113 SketcherCurve::SketcherCurve(SketcherCurve &cv)
114 {
115         copy_from(cv);
116 }
117 int SketcherCurve::equivalent(SketcherCurve &that)
118 {
119         if( this->id != that.id ) return 0;
120         if( this->pen != that.pen ) return 0;
121         if( this->width != that.width ) return 0;
122         if( this->color != that.color ) return 0;
123         int n = this->points.size();
124         if( n != that.points.size() ) return 0;
125         for( int i=0; i<n; ++i ) {
126                 if( !points[i]->equivalent(*that.points[i]) ) return 0;
127         }
128         return 1;
129 }
130 void SketcherCurve::copy_from(SketcherCurve &that)
131 {
132         this->id = that.id;
133         this->pen = that.pen;
134         this->width = that.width;
135         this->color = that.color;
136         int m = points.size(), n = that.points.size();
137         while( m > n ) points.remove_object_number(--m);
138         while( m < n ) { points.append(new SketcherPoint());  ++m; }
139         for( int i=0; i<n; ++i ) points[i]->copy_from(*that.points[i]);
140 }
141 void SketcherCurve::save_data(FileXML &output)
142 {
143         char curve[BCSTRLEN];
144         sprintf(curve,"/CURVE_%d",id);
145         output.tag.set_title(curve+1);
146         output.tag.set_property("PEN", pen);
147         output.tag.set_property("RADIUS", width);
148         output.tag.set_property("COLOR", color);
149         output.append_tag();
150         output.append_newline();
151         for( int i=0,n=points.size(); i<n; ++i )
152                 points[i]->save_data(output);
153         output.tag.set_title(curve+0);
154         output.append_tag();
155         output.append_newline();
156 }
157 void SketcherCurve::read_data(FileXML &input)
158 {
159         id = atoi(input.tag.get_title() + 6);
160         pen = input.tag.get_property("PEN", PEN_OFF);
161         width = input.tag.get_property("RADIUS", 1.);
162         color = input.tag.get_property("COLOR", CV_COLOR);
163         bclamp(pen, 0, PEN_SZ-1);
164 }
165
166 int Sketcher::new_curve(int pen, int width, int color)
167 {
168         SketcherCurves &curves = config.curves;
169         int k = curves.size(), id = 1;
170         for( int i=k; --i>=0; ) {
171                 int n = config.curves[i]->id;
172                 if( n >= id ) id = n + 1;
173         }
174         SketcherCurve *cv = new SketcherCurve(id, pen, width, color);
175         curves.append(cv);
176         config.cv_selected = k;
177         return k;
178 }
179
180 int Sketcher::new_curve()
181 {
182         return new_curve(PEN_XLANT, 1, CV_COLOR);
183 }
184
185 int Sketcher::new_point(SketcherCurve *cv, int arc, coord x, coord y, int idx)
186 {
187         int id = 1;
188         for( int i=cv->points.size(); --i>=0; ) {
189                 int n = cv->points[i]->id;
190                 if( n >= id ) id = n + 1;
191         }
192         SketcherPoint *pt = new SketcherPoint(id, arc, x, y);
193         int n = cv->points.size();
194         if( idx < 0 || idx > n ) idx = n;
195         cv->points.insert(pt, idx);
196         return idx;
197 }
198
199 int Sketcher::new_point(int idx, int arc)
200 {
201         int ci = config.cv_selected;
202         if( ci < 0 || ci >= config.curves.size() )
203                 return -1;
204         SketcherCurve *cv = config.curves[ci];
205         EDLSession *session = get_edl()->session;
206         coord x = !session ? 0.f : session->output_w / 2.f;
207         coord y = !session ? 0.f : session->output_h / 2.f;
208         return new_point(cv, arc, x, y, idx);
209 }
210
211 double SketcherCurve::nearest_point(int &pi, coord x, coord y)
212 {
213         pi = -1;
214         double dist = DBL_MAX;
215         for( int i=0; i<points.size(); ++i ) {
216                 SketcherPoint *p = points[i];
217                 double d = DISTANCE(x,y, p->x,p->y);
218                 if( d < dist ) { dist = d;  pi = i;  }
219         }
220         return pi >= 0 ? dist : -1.;
221 }
222
223 double SketcherConfig::nearest_point(int &ci, int &pi, coord x, coord y)
224 {
225         double dist = DBL_MAX;
226         ci = -1;  pi = -1;
227         for( int i=0; i<curves.size(); ++i ) {
228                 SketcherCurve *crv = curves[i];
229                 SketcherPoints &points = crv->points;
230                 for( int k=0; k<points.size(); ++k ) {
231                         SketcherPoint *p = points[k];
232                         double d = DISTANCE(x,y, p->x,p->y);
233                         if( d < dist ) {  dist = d; ci = i;  pi = k; }
234                 }
235         }
236         return pi >= 0 ? dist : -1.;
237 }
238
239
240 REGISTER_PLUGIN(Sketcher)
241
242 SketcherConfig::SketcherConfig()
243 {
244         drag = 1;
245         cv_selected = 0;
246         pt_selected = 0;
247 }
248 SketcherConfig::~SketcherConfig()
249 {
250 }
251
252 int SketcherConfig::equivalent(SketcherConfig &that)
253 {
254         if( this->drag != that.drag ) return 0;
255         if( this->cv_selected != that.cv_selected ) return 0;
256         if( this->pt_selected != that.pt_selected ) return 0;
257         if( this->curves.size() != that.curves.size() ) return 0;
258         for( int i=0, n=curves.size(); i<n; ++i ) {
259                 if( !curves[i]->equivalent(*that.curves[i]) ) return 0;
260         }
261         return 1;
262 }
263
264 void SketcherConfig::copy_from(SketcherConfig &that)
265 {
266         this->drag = that.drag;
267         this->cv_selected = that.cv_selected;
268         this->pt_selected = that.pt_selected;
269         int m = curves.size(), n = that.curves.size();
270         while( m > n ) curves.remove_object_number(--m);
271         while( m < n ) { curves.append(new SketcherCurve());  ++m; }
272         for( int i=0; i<n; ++i ) curves[i]->copy_from(*that.curves[i]);
273 }
274
275 void SketcherConfig::interpolate(SketcherConfig &prev, SketcherConfig &next,
276                 long prev_frame, long next_frame, long current_frame)
277 {
278         this->cv_selected = prev.cv_selected;
279         this->pt_selected = prev.pt_selected;
280         this->drag = prev.drag;
281
282         double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
283         double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
284
285         curves.remove_all_objects();
286         int prev_cv_sz = prev.curves.size();
287         int next_cv_sz = next.curves.size();
288         for( int i=0; i<prev_cv_sz; ++i ) {
289                 SketcherCurve *pcv = prev.curves[i], *ncv = 0;
290                 SketcherCurve *cv = curves.append(new SketcherCurve());
291                 int k = next_cv_sz;  // associated by id in next
292                 while( --k >= 0 && pcv->id != (ncv=next.curves[k])->id );
293                 if( k >= 0 ) {
294                         cv->id = pcv->id;
295                         cv->pen = pcv->pen;
296                         cv->width = pcv->width == ncv->width ? pcv->width :
297                                 pcv->width*prev_scale + ncv->width*next_scale + 0.5;
298                         int pr =  (pcv->color>>16)&0xff, nr =  (ncv->color>>16)&0xff;
299                         int pg =  (pcv->color>> 8)&0xff, ng =  (ncv->color>> 8)&0xff;
300                         int pb =  (pcv->color>> 0)&0xff, nb =  (ncv->color>> 0)&0xff;
301                         int pa = (~pcv->color>>24)&0xff, na = (~ncv->color>>24)&0xff;
302                         int r = pr == nr ? pr : pr*prev_scale + nr*next_scale + 0.5;
303                         int g = pg == ng ? pg : pg*prev_scale + ng*next_scale + 0.5;
304                         int b = pb == nb ? pb : pb*prev_scale + nb*next_scale + 0.5;
305                         int a = pa == na ? pa : pa*prev_scale + na*next_scale + 0.5;
306                         bclamp(r,0,255); bclamp(g,0,255); bclamp(b,0,255); bclamp(a,0,255);
307                         cv->color = (~a<<24) | (r<<16) | (g<<8) | (b<<0);
308                         int prev_pt_sz = pcv->points.size(), next_pt_sz = ncv->points.size();
309                         for( int j=0; j<prev_pt_sz; ++j ) {
310                                 SketcherPoint &pt = *pcv->points[j], *nt = 0;
311                                 k = next_pt_sz;  // associated by id in next
312                                 while( --k >= 0 && pt.id != (nt=ncv->points[k])->id );
313                                 coord x = pt.x, y = pt.y;
314                                 if( k >= 0 ) {
315                                         if( x != nt->x )
316                                                 x = x * prev_scale + nt->x * next_scale;
317                                         if( y != nt->y )
318                                                 y = y * prev_scale + nt->y * next_scale;
319                                 }
320                                 cv->points.append(new SketcherPoint(pt.id, pt.arc, x, y));
321                         }
322                 }
323                 else
324                         cv->copy_from(*pcv);
325         }
326 }
327
328 void SketcherConfig::limits()
329 {
330 }
331
332
333 Sketcher::Sketcher(PluginServer *server)
334  : PluginVClient(server)
335 {
336         img = 0;
337         out = 0;
338         overlay_frame = 0;
339 }
340
341 Sketcher::~Sketcher()
342 {
343         delete img;
344         delete out;
345         delete overlay_frame;
346 }
347
348 const char* Sketcher::plugin_title() { return N_("Sketcher"); }
349 int Sketcher::is_realtime() { return 1; }
350 int Sketcher::is_synthesis() { return 1; }
351
352 NEW_WINDOW_MACRO(Sketcher, SketcherWindow);
353 LOAD_CONFIGURATION_MACRO(Sketcher, SketcherConfig)
354
355 void Sketcher::save_data(KeyFrame *keyframe)
356 {
357         FileXML output;
358 // cause data to be stored directly in text
359         output.set_shared_output(keyframe->xbuf);
360
361         output.tag.set_title("SKETCHER");
362         output.tag.set_property("DRAG", config.drag);
363         output.tag.set_property("CV_SELECTED", config.cv_selected);
364         output.tag.set_property("PT_SELECTED", config.pt_selected);
365         output.append_tag();
366         output.append_newline();
367         for( int i=0,n=config.curves.size(); i<n; ++i ) {
368                 config.curves[i]->save_data(output);
369         }
370         output.tag.set_title("/SKETCHER");
371         output.append_tag();
372         output.append_newline();
373         output.terminate_string();
374 }
375
376 void Sketcher::read_data(KeyFrame *keyframe)
377 {
378         FileXML input;
379         input.set_shared_input(keyframe->xbuf);
380         config.curves.remove_all_objects();
381         int result = 0;
382         SketcherCurve *cv = 0;
383
384         while( !(result=input.read_tag()) ) {
385                 if( input.tag.title_is("SKETCHER") ) {
386                         config.drag = input.tag.get_property("DRAG", config.drag);
387                         config.cv_selected = input.tag.get_property("CV_SELECTED", 0);
388                         config.pt_selected = input.tag.get_property("PT_SELECTED", 0);
389                 }
390                 else if( !strncmp(input.tag.get_title(),"CURVE_",6) ) {
391                         cv = new SketcherCurve();
392                         cv->read_data(input);
393                         config.curves.append(cv);
394                 }
395                 else if( !strncmp(input.tag.get_title(),"/CURVE_",7) )
396                         cv = 0;
397                 else if( !strncmp(input.tag.get_title(),"POINT_",6) ) {
398                         if( cv ) {
399                                 SketcherPoint *pt = new SketcherPoint();
400                                 pt->read_data(input);
401                                 cv->points.append(pt);
402                         }
403                         else
404                                 printf("Sketcher::read_data: no curve for point\n");
405                 }
406         }
407
408         if( !config.curves.size() )
409                 new_curve();
410         config.limits();
411 }
412
413 void Sketcher::update_gui()
414 {
415         if( !thread ) return;
416         thread->window->lock_window("Sketcher::update_gui");
417         if( load_configuration() ) {
418                 SketcherWindow *window = (SketcherWindow*)thread->window;
419                 window->update_gui();
420                 window->flush();
421         }
422         thread->window->unlock_window();
423 }
424
425 void Sketcher::draw_point(VFrame *vfrm, SketcherPoint *pt, int color, int d)
426 {
427         int r = d/2+1, x = pt->x, y = pt->y;
428         vfrm->set_pixel_color(color);
429         vfrm->draw_smooth(x-r,y+0, x-r, y-r, x+0,y-r);
430         vfrm->draw_smooth(x+0,y-r, x+r, y-r, x+r,y+0);
431         vfrm->draw_smooth(x+r,y+0, x+r, y+r, x+0,y+r);
432         vfrm->draw_smooth(x+0,y+r, x-r, y+r, x-r,y+0);
433         vfrm->draw_x(pt->x, pt->y, d);
434 }
435 void Sketcher::draw_point(VFrame *vfrm, SketcherPoint *pt, int color)
436 {
437         draw_point(vfrm, pt, color, bmax(w,h)/200 + 2);
438 }
439
440
441 int SketcherVPen::draw_pixel(int x, int y)
442 {
443         if( x >= 0 && x < vfrm->get_w() &&
444             y >= 0 && y < vfrm->get_h() )
445                 msk[vfrm->get_w()*y + x] = 0xff;
446         return 0;
447 }
448
449 int SketcherPenSquare::draw_pixel(int x, int y)
450 {
451         vfrm->draw_line(x-n, y, x+n, y);
452         for( int i=-n; i<n; ++i )
453                 vfrm->draw_line(x-n, y+i, x+n, y+i);
454         return SketcherVPen::draw_pixel(x, y);
455 }
456 int SketcherPenPlus::draw_pixel(int x, int y)
457 {
458         if( n > 1 ) {
459                 vfrm->draw_line(x-n, y, x+n, y);
460                 vfrm->draw_line(x, y-n, x, y+n);
461         }
462         else
463                 vfrm->draw_pixel(x, y);
464         return SketcherVPen::draw_pixel(x, y);
465 }
466 int SketcherPenSlant::draw_pixel(int x, int y)
467 {
468         vfrm->draw_line(x-n,   y+n,   x+n,   y-n);
469         vfrm->draw_line(x-n+1, y+n,   x+n+1, y-n);
470         vfrm->draw_line(x-n,   y+n+1, x+n,   y-n+1);
471         return SketcherVPen::draw_pixel(x, y);
472 }
473 int SketcherPenXlant::draw_pixel(int x, int y)
474 {
475         vfrm->draw_line(x-n,   y+n,   x+n,   y-n);
476         vfrm->draw_line(x-n+1, y+n,   x+n+1, y-n);
477         vfrm->draw_line(x-n,   y+n+1, x+n,   y-n+1);
478         vfrm->draw_line(x-n,   y-n,   x+n,   y+n);
479         vfrm->draw_line(x-n+1, y-n,   x+n+1, y+n);
480         vfrm->draw_line(x-n,   y-n+1, x+n,   y-n+1);
481         return SketcherVPen::draw_pixel(x, y);
482 }
483
484
485 SketcherVPen *SketcherCurve::new_vpen(VFrame *out)
486 {
487         switch( pen ) {
488         case PEN_SQUARE: return new SketcherPenSquare(out, width);
489         case PEN_PLUS:   return new SketcherPenPlus(out, width);
490         case PEN_SLANT:  return new SketcherPenSlant(out, width);
491         case PEN_XLANT:  return new SketcherPenXlant(out, width);
492         }
493         return 0;
494 }
495
496 static int intersects_at(float &x, float &y,
497                 float ax,float ay, float bx, float by, float cx,float cy,  // line slope ab thru c
498                 float dx,float dy, float ex, float ey, float fx,float fy, // line slope de thru f
499                 float mx=0)
500 {
501         float badx = bx - ax, bady = by - ay;
502         float eddx = ex - dx, eddy = ey - dy;
503         float d = badx*eddy - bady*eddx;
504         int ret = 0;
505         if( fabsf(d) < 1 ) { ret = 1;  d = signbit(d) ? -1 : 1; }
506         x = (badx*cy*eddx - badx*eddx*fy + badx*eddy*fx - bady*cx*eddx) / d;
507         y = (badx*cy*eddy - bady*cx*eddy - bady*eddx*fy + bady*eddy*fx) / d;
508         if( mx > 0 ) { bclamp(x, -mx,mx);  bclamp(y, -mx,mx); }
509         return ret;
510 }
511
512 static void smooth_axy(float &ax, float &ay,
513         float bx, float by, float cx, float cy, float dx, float dy)
514 {
515 //middle of bd reflected around ctr
516 // point ctr = (b+d)/2, dv=c-ctr, a=ctr-dv;
517         float xc = (bx+dx)*.5f, yc = (by+dy)*.5f;
518         float xd = cx - xc, yd = cy - yc;
519         ax = xc - xd;  ay = yc - yd;
520 }
521 static void smooth_dxy(float &dx, float &dy,
522         float ax, float ay, float bx, float by, float cx, float cy)
523 {
524 //middle of ac reflected around ctr
525 // point ctr = (a+c)/2, dv=c-ctr, d=ctr-dv;
526         float xc = (ax+cx)*.5f, yc = (ay+cy)*.5f;
527         float xd = bx - xc, yd = by - yc;
528         dx = xc - xd;  dy = yc - yd;
529 }
530
531 static int convex(float ax,float ay, float bx,float by,
532                   float cx,float cy, float dx,float dy)
533 {
534         float abdx = bx-ax, abdy = by-ay;
535         float acdx = cx-ax, acdy = cy-ay;
536         float bcdx = cx-bx, bcdy = cy-by;
537         float bddx = dx-bx, bddy = dy-by;
538         float abc = abdx*acdy - abdy*acdx;
539         float bcd = bcdx*bddy - bcdy*bddx;
540         float v = abc * bcd;
541         return !v ? 0 : v>0 ? 1 : -1;
542 }
543
544 class FillRegion
545 {
546         class segment { public: int y, lt, rt; };
547         ArrayList<segment> stack;
548
549         void push(int y, int lt, int rt) {
550                 segment &seg = stack.append();
551                 seg.y = y;  seg.lt = lt;  seg.rt = rt;
552         }
553         void pop(int &y, int &lt, int &rt) {
554                 segment &seg = stack.last();
555                 y = seg.y;  lt = seg.lt;  rt = seg.rt;
556                 stack.remove();
557         }
558  
559         VFrame *img;
560         uint8_t *msk;
561         int w, h, nxt;
562         SketcherPoints &points;
563 public:
564         SketcherPoint *next();
565         bool exists() { return stack.size() > 0; }
566         void start_at(int x, int y);
567         void run();
568         FillRegion(SketcherPoints &pts, SketcherVPen *vpen);
569         ~FillRegion();
570 };
571
572 FillRegion::FillRegion(SketcherPoints &pts, SketcherVPen *vpen)
573  : points(pts)
574 {
575         this->img = vpen->vfrm;
576         this->msk = vpen->msk;
577         this->w = img->get_w();
578         this->h = img->get_h();
579         nxt = 0;
580 }
581 FillRegion::~FillRegion()
582 {
583 }
584
585 void FillRegion::start_at(int x, int y)
586 {
587         bclamp(x, 0, w-1);
588         bclamp(y, 0, h-1);
589         push(y, x, x);
590 }
591
592 void FillRegion::run()
593 {
594         while( stack.size() > 0 ) {
595                 int y, ilt, irt;
596                 pop(y, ilt, irt);
597                 int ofs = y*w + ilt;
598                 for( int x=ilt; x<=irt; ++x,++ofs ) {
599                         if( msk[ofs] ) continue;
600                         msk[ofs] = 0xff;
601                         img->draw_pixel(x, y);
602                         int lt = x, rt = x;
603                         int lofs = ofs;
604                         for( int i=lt; --i>=0; ) {
605                                 if( msk[--lofs] ) break;
606                                 img->draw_pixel(i, y);
607                                 msk[lofs] = 0xff;  lt = i;
608                         }
609                         int rofs = ofs;
610                         for( int i=rt; ++i< w; ) {
611                                 if( msk[++rofs] ) break;
612                                 img->draw_pixel(i, y);
613                                 msk[rofs] = 0xff;  rt = i;
614                         }
615                         if( y+1 <  h ) push(y+1, lt, rt);
616                         if( y-1 >= 0 ) push(y-1, lt, rt);
617                 }
618         }
619 }
620
621 SketcherPoint *FillRegion::next()
622 {
623         while( nxt < points.size() ) {
624                 SketcherPoint *pt = points[nxt++];
625                 if( pt->arc == ARC_OFF ) continue;
626                 if( pt->arc != ARC_FILL ) return pt;
627                 start_at(pt->x, pt->y);
628         }
629         return 0;
630 }
631
632
633 void SketcherCurve::draw(VFrame *img)
634 {
635         if( !points.size() ) return;
636         const float fmx = 16383;
637         SketcherVPen *vpen = new_vpen(img);
638         FillRegion fill(points, vpen);
639         SketcherPoint *pnt0 = fill.next();
640         SketcherPoint *pnt1 = pnt0 ? fill.next() : 0;
641         SketcherPoint *pnt2 = pnt1 ? fill.next() : 0;
642         if( pnt0 && pnt1 && pnt2 ) {
643                 SketcherPoint *pt0 = pnt0, *pt1 = pnt1, *pt2 = pnt2;
644                 float ax,ay, bx,by, cx,cy, dx,dy, sx,sy;
645                 bx = pt0->x;  by = pt0->y;
646                 cx = pt1->x;  cy = pt1->y;
647                 dx = pt2->x;  dy = pt2->y;
648                 smooth_axy(ax,ay, bx,by, cx,cy, dx,dy);
649                 while( pt2 ) {
650                         dx = pt2->x;  dy = pt2->y;
651                         switch( pt0->arc ) {
652                         case ARC_CURVE:
653                                 if( convex(ax,ay, bx,by, cx,cy, dx,dy) >= 0 ) {
654                                         // s = ac thru b x bd thru c
655                                         intersects_at(sx,sy, ax,ay,cx,cy,bx,by, bx,by,dx,dy,cx,cy,fmx);
656                                         vpen->draw_smooth(bx,by, sx,sy, cx,cy);
657                                         break;
658                                 } // fall thru
659                         case ARC_LINE:
660                                 vpen->draw_line(bx, by, cx, cy);
661                                 break;
662                         }
663                         ax = bx;  ay = by;  pt0 = pt1;
664                         bx = cx;  by = cy;  pt1 = pt2;
665                         cx = dx;  cy = dy;  pt2 = fill.next();
666                 }
667                 switch( pt1->arc ) {
668                 case ARC_LINE:
669                         vpen->draw_line(bx, by, cx, cy);
670                         if( fill.exists() ) {
671                                 dx = pnt0->x;  dy = pnt0->y;
672                                 vpen->draw_line(cx,cy, dx,dy);
673                         }
674                         break;
675                 case ARC_CURVE: {
676                         if( fill.exists() ) {
677                                 dx = pnt0->x;  dy = pnt0->y;
678                                 intersects_at(sx,sy, ax,ay,cx,cy,bx,by, bx,by,dx,dy,cx,cy,fmx);
679                                 vpen->draw_smooth(bx,by, sx,sy, cx,cy);
680                                 ax = bx;  ay = by;
681                                 bx = cx;  by = cy;
682                                 cx = dx;  cy = dy;
683                                 dx = pnt1->x;  dy = pnt1->y;
684                         }
685                         else
686                                 smooth_dxy(dx,dy, ax,ay, bx,by, cx,cy);
687                         intersects_at(sx,sy, ax,ay,cx,cy,bx,by, bx,by,dx,dy,cx,cy,fmx);
688                         vpen->draw_smooth(bx,by, sx,sy, cx,cy);
689                         break; }
690                 }
691                 fill.run();
692         }
693         else if( pnt0 && pnt1 ) {
694                 vpen->draw_line(pnt0->x, pnt0->y, pnt1->x, pnt1->y);
695         }
696         else if( pnt0 ) {
697                 vpen->draw_pixel(pnt0->x, pnt0->y);
698         }
699         delete vpen;
700 }
701
702 int Sketcher::process_realtime(VFrame *input, VFrame *output)
703 {
704         this->input = input;  this->output = output;
705         w = output->get_w();  h = output->get_h();
706
707         load_configuration();
708
709         int out_color_model = output->get_color_model();
710         int color_model = out_color_model;
711         switch( color_model ) { // add alpha if needed
712         case BC_RGB888:         color_model = BC_RGBA8888;      break;
713         case BC_YUV888:         color_model = BC_YUVA8888;      break;
714         case BC_RGB161616:      color_model = BC_RGBA16161616;  break;
715         case BC_YUV161616:      color_model = BC_YUVA16161616;  break;
716         case BC_RGB_FLOAT:      color_model = BC_RGBA_FLOAT;    break;
717         case BC_RGB_FLOATP:     color_model = BC_RGBA_FLOATP;   break;
718         }
719         if( color_model == out_color_model ) {
720                 delete out;  out = output;
721                 if( output != input )
722                         output->transfer_from(input);
723         }
724         else {
725                 VFrame::get_temp(out, w, h, color_model);
726                 out->transfer_from(input);
727         }
728         VFrame::get_temp(img, w, h, color_model);
729         
730         if( !overlay_frame ) {
731                 int cpus = server->preferences->project_smp;
732                 int max = (w*h)/0x80000 + 2;
733                 if( cpus > max ) cpus = max;
734                 overlay_frame = new OverlayFrame(cpus);
735         }
736
737         for( int ci=0, n=config.curves.size(); ci<n; ++ci ) {
738                 SketcherCurve *cv = config.curves[ci];
739                 if( cv->pen == PEN_OFF ) continue;
740                 int m = cv->points.size();
741                 if( !m ) continue;
742                 img->clear_frame();
743                 img->set_pixel_color(cv->color, (~cv->color>>24)&0xff);
744                 cv->draw(img);
745                 overlay_frame->overlay(out, img, 0,0,w,h, 0,0,w,h,
746                                 1.f, TRANSFER_NORMAL, NEAREST_NEIGHBOR);
747         }
748
749         if( config.drag ) {
750                 for( int ci=0, n=config.curves.size(); ci<n; ++ci ) {
751                         SketcherCurve *cv = config.curves[ci];
752                         for( int pi=0,m=cv->points.size(); pi<m; ++pi ) {
753                                 int color = pi==config.pt_selected && ci==config.cv_selected ?
754                                         RED : cv->color ; 
755                                 draw_point(out, cv->points[pi], color);
756                         }
757                 }
758         }
759
760         if( output != out )
761                 output->transfer_from(out);
762         else
763                 out = 0;
764
765         return 0;
766 }
767
768 void SketcherPoints::dump()
769 {
770         for( int i=0; i<size(); ++i ) {
771                 SketcherPoint *pt = get(i);
772                 printf("  Pt %d, id=%d, arc=%s, x=%0.1f, y=%0.1f\n",
773                         i, pt->id, pt_type[pt->arc], pt->x, pt->y);
774         }
775 }
776 void SketcherCurves::dump()
777 {
778         for( int i=0; i<size(); ++i ) {
779                 SketcherCurve *cv = get(i);
780                 printf("Curve %d, id=%d, pen=%s, r=%d, color=%02x%02x%02x%02x, %d points\n",
781                         i, cv->id, cv_pen[cv->pen], cv->width, (~cv->color>>24)&0xff,
782                         (cv->color>>16)&0xff, (cv->color>>8)&0xff, (cv->color>>0)&0xff,
783                         cv->points.size());
784                 cv->points.dump();
785         }
786 }
787 void SketcherConfig::dump()
788 {
789         printf("Config drag=%d, cv_selected=%d, pt_selected=%d %d curves\n",
790                         drag, cv_selected, pt_selected, curves.size());
791         curves.dump();
792 }
793