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