add stacked edl editing, add sketcher/vframe line anti-aliasing
[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         aliasing = 0;
246         cv_selected = 0;
247         pt_selected = 0;
248 }
249 SketcherConfig::~SketcherConfig()
250 {
251 }
252
253 int SketcherConfig::equivalent(SketcherConfig &that)
254 {
255         if( this->drag != that.drag ) return 0;
256         if( this->aliasing != that.aliasing ) return 0;
257         if( this->cv_selected != that.cv_selected ) return 0;
258         if( this->pt_selected != that.pt_selected ) return 0;
259         if( this->curves.size() != that.curves.size() ) return 0;
260         for( int i=0, n=curves.size(); i<n; ++i ) {
261                 if( !curves[i]->equivalent(*that.curves[i]) ) return 0;
262         }
263         return 1;
264 }
265
266 void SketcherConfig::copy_from(SketcherConfig &that)
267 {
268         this->drag = that.drag;
269         this->aliasing = that.aliasing;
270         this->cv_selected = that.cv_selected;
271         this->pt_selected = that.pt_selected;
272         int m = curves.size(), n = that.curves.size();
273         while( m > n ) curves.remove_object_number(--m);
274         while( m < n ) { curves.append(new SketcherCurve());  ++m; }
275         for( int i=0; i<n; ++i ) curves[i]->copy_from(*that.curves[i]);
276 }
277
278 void SketcherConfig::interpolate(SketcherConfig &prev, SketcherConfig &next,
279                 long prev_frame, long next_frame, long current_frame)
280 {
281         this->cv_selected = prev.cv_selected;
282         this->pt_selected = prev.pt_selected;
283         this->drag = prev.drag;
284         this->aliasing = prev.aliasing;
285
286         double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
287         double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
288
289         curves.remove_all_objects();
290         int prev_cv_sz = prev.curves.size();
291         int next_cv_sz = next.curves.size();
292         for( int i=0; i<prev_cv_sz; ++i ) {
293                 SketcherCurve *pcv = prev.curves[i], *ncv = 0;
294                 SketcherCurve *cv = curves.append(new SketcherCurve());
295                 int k = next_cv_sz;  // associated by id in next
296                 while( --k >= 0 && pcv->id != (ncv=next.curves[k])->id );
297                 if( k >= 0 ) {
298                         cv->id = pcv->id;
299                         cv->pen = pcv->pen;
300                         cv->width = pcv->width == ncv->width ? pcv->width :
301                                 pcv->width*prev_scale + ncv->width*next_scale + 0.5;
302                         int pr =  (pcv->color>>16)&0xff, nr =  (ncv->color>>16)&0xff;
303                         int pg =  (pcv->color>> 8)&0xff, ng =  (ncv->color>> 8)&0xff;
304                         int pb =  (pcv->color>> 0)&0xff, nb =  (ncv->color>> 0)&0xff;
305                         int pa = (~pcv->color>>24)&0xff, na = (~ncv->color>>24)&0xff;
306                         int r = pr == nr ? pr : pr*prev_scale + nr*next_scale + 0.5;
307                         int g = pg == ng ? pg : pg*prev_scale + ng*next_scale + 0.5;
308                         int b = pb == nb ? pb : pb*prev_scale + nb*next_scale + 0.5;
309                         int a = pa == na ? pa : pa*prev_scale + na*next_scale + 0.5;
310                         bclamp(r,0,255); bclamp(g,0,255); bclamp(b,0,255); bclamp(a,0,255);
311                         cv->color = (~a<<24) | (r<<16) | (g<<8) | (b<<0);
312                         int prev_pt_sz = pcv->points.size(), next_pt_sz = ncv->points.size();
313                         for( int j=0; j<prev_pt_sz; ++j ) {
314                                 SketcherPoint &pt = *pcv->points[j], *nt = 0;
315                                 k = next_pt_sz;  // associated by id in next
316                                 while( --k >= 0 && pt.id != (nt=ncv->points[k])->id );
317                                 coord x = pt.x, y = pt.y;
318                                 if( k >= 0 ) {
319                                         if( x != nt->x )
320                                                 x = x * prev_scale + nt->x * next_scale;
321                                         if( y != nt->y )
322                                                 y = y * prev_scale + nt->y * next_scale;
323                                 }
324                                 cv->points.append(new SketcherPoint(pt.id, pt.arc, x, y));
325                         }
326                 }
327                 else
328                         cv->copy_from(*pcv);
329         }
330 }
331
332 void SketcherConfig::limits()
333 {
334 }
335
336
337 Sketcher::Sketcher(PluginServer *server)
338  : PluginVClient(server)
339 {
340         img = 0;
341         out = 0;
342         overlay_frame = 0;
343 }
344
345 Sketcher::~Sketcher()
346 {
347         delete img;
348         delete out;
349         delete overlay_frame;
350 }
351
352 const char* Sketcher::plugin_title() { return N_("Sketcher"); }
353 int Sketcher::is_realtime() { return 1; }
354 int Sketcher::is_synthesis() { return 1; }
355
356 NEW_WINDOW_MACRO(Sketcher, SketcherWindow);
357 LOAD_CONFIGURATION_MACRO(Sketcher, SketcherConfig)
358
359 void Sketcher::save_data(KeyFrame *keyframe)
360 {
361         FileXML output;
362 // cause data to be stored directly in text
363         output.set_shared_output(keyframe->xbuf);
364
365         output.tag.set_title("SKETCHER");
366         output.tag.set_property("DRAG", config.drag);
367         output.tag.set_property("ALIASING", config.aliasing);
368         output.tag.set_property("CV_SELECTED", config.cv_selected);
369         output.tag.set_property("PT_SELECTED", config.pt_selected);
370         output.append_tag();
371         output.append_newline();
372         for( int i=0,n=config.curves.size(); i<n; ++i ) {
373                 config.curves[i]->save_data(output);
374         }
375         output.tag.set_title("/SKETCHER");
376         output.append_tag();
377         output.append_newline();
378         output.terminate_string();
379 }
380
381 void Sketcher::read_data(KeyFrame *keyframe)
382 {
383         FileXML input;
384         input.set_shared_input(keyframe->xbuf);
385         config.curves.remove_all_objects();
386         int result = 0;
387         SketcherCurve *cv = 0;
388
389         while( !(result=input.read_tag()) ) {
390                 if( input.tag.title_is("SKETCHER") ) {
391                         config.drag = input.tag.get_property("DRAG", config.drag);
392                         config.aliasing = input.tag.get_property("ALIASING", config.aliasing);
393                         config.cv_selected = input.tag.get_property("CV_SELECTED", 0);
394                         config.pt_selected = input.tag.get_property("PT_SELECTED", 0);
395                 }
396                 else if( !strncmp(input.tag.get_title(),"CURVE_",6) ) {
397                         cv = new SketcherCurve();
398                         cv->read_data(input);
399                         config.curves.append(cv);
400                 }
401                 else if( !strncmp(input.tag.get_title(),"/CURVE_",7) )
402                         cv = 0;
403                 else if( !strncmp(input.tag.get_title(),"POINT_",6) ) {
404                         if( cv ) {
405                                 SketcherPoint *pt = new SketcherPoint();
406                                 pt->read_data(input);
407                                 cv->points.append(pt);
408                         }
409                         else
410                                 printf("Sketcher::read_data: no curve for point\n");
411                 }
412         }
413
414         if( !config.curves.size() )
415                 new_curve();
416         config.limits();
417 }
418
419 void Sketcher::update_gui()
420 {
421         if( !thread ) return;
422         thread->window->lock_window("Sketcher::update_gui");
423         if( load_configuration() ) {
424                 SketcherWindow *window = (SketcherWindow*)thread->window;
425                 window->update_gui();
426                 window->flush();
427         }
428         thread->window->unlock_window();
429 }
430
431 void Sketcher::draw_point(VFrame *vfrm, SketcherPoint *pt, int color, int d)
432 {
433         int r = d/2+1, x = pt->x, y = pt->y;
434         vfrm->set_pixel_color(color);
435         vfrm->draw_smooth(x-r,y+0, x-r, y-r, x+0,y-r);
436         vfrm->draw_smooth(x+0,y-r, x+r, y-r, x+r,y+0);
437         vfrm->draw_smooth(x+r,y+0, x+r, y+r, x+0,y+r);
438         vfrm->draw_smooth(x+0,y+r, x-r, y+r, x-r,y+0);
439         vfrm->draw_x(pt->x, pt->y, d);
440 }
441 void Sketcher::draw_point(VFrame *vfrm, SketcherPoint *pt, int color)
442 {
443         draw_point(vfrm, pt, color, bmax(w,h)/200 + 2);
444 }
445
446
447 int SketcherVPen::draw_mask(int x, int y)
448 {
449         int w = vfrm->get_w(), h = vfrm->get_h();
450         if( x < 0 || x >= w ) return 1;
451         if( y < 0 || y >= h ) return 1;
452         msk[w * y + x] = 0xff;
453         return 0;
454 }
455
456 int SketcherVPen::draw_pixel(float x, float y, float frac, int axis)
457 {
458         draw_mask(x, y);
459         return VFrame::draw_pixel(x, y, frac, axis);
460 }
461
462 int SketcherPenSquare::draw_pixel(float x, float y, float a)
463 {
464         vfrm->set_draw_alpha(a);
465         vfrm->draw_line(x-n, y, x+n, y);
466         for( int i=-n; i<n; ++i )
467                 vfrm->draw_line(x-n, y+i, x+n, y+i);
468         return 0;
469 }
470 int SketcherPenPlus::draw_pixel(float x, float y, float a)
471 {
472         vfrm->set_draw_alpha(a);
473         if( n > 1 ) {
474                 vfrm->draw_line(x-n, y, x+n, y);
475                 vfrm->draw_line(x, y-n, x, y+n);
476         }
477         else
478                 vfrm->draw_pixel(x, y);
479         return 0;
480 }
481 int SketcherPenSlant::draw_pixel(float x, float y, float a)
482 {
483         vfrm->set_draw_alpha(a);
484         vfrm->draw_line(x-n,   y+n,   x+n,   y-n);
485         vfrm->draw_line(x-n+1, y+n,   x+n+1, y-n);
486         vfrm->draw_line(x-n,   y+n+1, x+n,   y-n+1);
487         return 0;
488 }
489 int SketcherPenXlant::draw_pixel(float x, float y, float a)
490 {
491         vfrm->set_draw_alpha(a);
492         vfrm->draw_line(x-n,   y+n,   x+n,   y-n);
493         vfrm->draw_line(x-n+1, y+n,   x+n+1, y-n);
494         vfrm->draw_line(x-n,   y+n+1, x+n,   y-n+1);
495         vfrm->draw_line(x-n,   y-n,   x+n,   y+n);
496         vfrm->draw_line(x-n+1, y-n,   x+n+1, y+n);
497         vfrm->draw_line(x-n,   y-n+1, x+n,   y-n+1);
498         return 0;
499 }
500
501
502 SketcherVPen *SketcherCurve::new_vpen(VFrame *out)
503 {
504         switch( pen ) {
505         case PEN_SQUARE: return new SketcherPenSquare(out, width);
506         case PEN_PLUS:   return new SketcherPenPlus(out, width);
507         case PEN_SLANT:  return new SketcherPenSlant(out, width);
508         case PEN_XLANT:  return new SketcherPenXlant(out, width);
509         }
510         return 0;
511 }
512
513 static int intersects_at(float &x, float &y,
514                 float ax,float ay, float bx, float by, float cx,float cy,  // line slope ab thru c
515                 float dx,float dy, float ex, float ey, float fx,float fy, // line slope de thru f
516                 float mx=0)
517 {
518         float badx = bx - ax, bady = by - ay;
519         float eddx = ex - dx, eddy = ey - dy;
520         float d = badx*eddy - bady*eddx;
521         int ret = 0;
522         if( fabsf(d) < 1 ) { ret = 1;  d = signbit(d) ? -1 : 1; }
523         x = (badx*cy*eddx - badx*eddx*fy + badx*eddy*fx - bady*cx*eddx) / d;
524         y = (badx*cy*eddy - bady*cx*eddy - bady*eddx*fy + bady*eddy*fx) / d;
525         if( mx > 0 ) { bclamp(x, -mx,mx);  bclamp(y, -mx,mx); }
526         return ret;
527 }
528
529 static void smooth_axy(float &ax, float &ay,
530         float bx, float by, float cx, float cy, float dx, float dy)
531 {
532 //middle of bd reflected around ctr
533 // point ctr = (b+d)/2, dv=c-ctr, a=ctr-dv;
534         float xc = (bx+dx)*.5f, yc = (by+dy)*.5f;
535         float xd = cx - xc, yd = cy - yc;
536         ax = xc - xd;  ay = yc - yd;
537 }
538 static void smooth_dxy(float &dx, float &dy,
539         float ax, float ay, float bx, float by, float cx, float cy)
540 {
541 //middle of ac reflected around ctr
542 // point ctr = (a+c)/2, dv=c-ctr, d=ctr-dv;
543         float xc = (ax+cx)*.5f, yc = (ay+cy)*.5f;
544         float xd = bx - xc, yd = by - yc;
545         dx = xc - xd;  dy = yc - yd;
546 }
547
548 static int convex(float ax,float ay, float bx,float by,
549                   float cx,float cy, float dx,float dy)
550 {
551         float abdx = bx-ax, abdy = by-ay;
552         float acdx = cx-ax, acdy = cy-ay;
553         float bcdx = cx-bx, bcdy = cy-by;
554         float bddx = dx-bx, bddy = dy-by;
555         float abc = abdx*acdy - abdy*acdx;
556         float bcd = bcdx*bddy - bcdy*bddx;
557         float v = abc * bcd;
558         return !v ? 0 : v>0 ? 1 : -1;
559 }
560
561 class FillRegion
562 {
563         class segment { public: int y, lt, rt; };
564         ArrayList<segment> stack;
565
566         void push(int y, int lt, int rt) {
567                 segment &seg = stack.append();
568                 seg.y = y;  seg.lt = lt;  seg.rt = rt;
569         }
570         void pop(int &y, int &lt, int &rt) {
571                 segment &seg = stack.last();
572                 y = seg.y;  lt = seg.lt;  rt = seg.rt;
573                 stack.remove();
574         }
575  
576         VFrame *img;
577         uint8_t *msk;
578         int w, h, nxt;
579         SketcherPoints &points;
580 public:
581         SketcherPoint *next();
582         bool exists() { return stack.size() > 0; }
583         void draw_pixel(int x, int y) { img->draw_pixel(x, y); }
584         void start_at(int x, int y);
585         void run();
586         FillRegion(SketcherPoints &pts, SketcherVPen *vpen);
587         ~FillRegion();
588 };
589
590 FillRegion::FillRegion(SketcherPoints &pts, SketcherVPen *vpen)
591  : points(pts)
592 {
593         this->img = vpen->vfrm;
594         this->msk = vpen->msk;
595         this->w = img->get_w();
596         this->h = img->get_h();
597         nxt = 0;
598 }
599 FillRegion::~FillRegion()
600 {
601 }
602
603 void FillRegion::start_at(int x, int y)
604 {
605         bclamp(x, 0, w-1);
606         bclamp(y, 0, h-1);
607         push(y, x, x);
608 }
609
610 void FillRegion::run()
611 {
612         img->set_draw_alpha(1);
613         while( stack.size() > 0 ) {
614                 int y, ilt, irt;
615                 pop(y, ilt, irt);
616                 int ofs = y*w + ilt;
617                 for( int x=ilt; x<=irt; ++x,++ofs ) {
618                         draw_pixel(x, y);
619                         if( msk[ofs] ) continue;
620                         msk[ofs] = 0xff;
621                         int lt = x, rt = x;
622                         int lofs = ofs;
623                         for( int i=lt; --i>=0; ) {
624                                 draw_pixel(i, y);
625                                 if( msk[--lofs] ) break;
626                                 msk[lofs] = 0xff;  lt = i;
627                         }
628                         int rofs = ofs;
629                         for( int i=rt; ++i< w; ) {
630                                 draw_pixel(i, y);
631                                 if( msk[++rofs] ) break;
632                                 msk[rofs] = 0xff;  rt = i;
633                         }
634                         if( y+1 <  h ) push(y+1, lt, rt);
635                         if( y-1 >= 0 ) push(y-1, lt, rt);
636                 }
637         }
638 }
639
640 SketcherPoint *FillRegion::next()
641 {
642         while( nxt < points.size() ) {
643                 SketcherPoint *pt = points[nxt++];
644                 if( pt->arc == ARC_OFF ) continue;
645                 if( pt->arc != ARC_FILL ) return pt;
646                 start_at(pt->x, pt->y);
647         }
648         return 0;
649 }
650
651 void SketcherCurve::draw(VFrame *img, int alias)
652 {
653         if( !points.size() ) return;
654         img->set_pixel_color(color, (~color>>24)&0xff);
655         const float fmx = 16383;
656         SketcherVPen *vpen = new_vpen(img);
657         vpen->set_draw_flags(alias);
658         FillRegion fill(points, vpen);
659         SketcherPoint *pnt0 = fill.next();
660         SketcherPoint *pnt1 = pnt0 ? fill.next() : 0;
661         SketcherPoint *pnt2 = pnt1 ? fill.next() : 0;
662         if( pnt0 && pnt1 && pnt2 ) {
663                 SketcherPoint *pt0 = pnt0, *pt1 = pnt1, *pt2 = pnt2;
664                 float ax,ay, bx,by, cx,cy, dx,dy, sx,sy;
665                 bx = pt0->x;  by = pt0->y;
666                 cx = pt1->x;  cy = pt1->y;
667                 dx = pt2->x;  dy = pt2->y;
668                 smooth_axy(ax,ay, bx,by, cx,cy, dx,dy);
669                 while( pt2 ) {
670                         dx = pt2->x;  dy = pt2->y;
671                         switch( pt0->arc ) {
672                         case ARC_CURVE:
673                                 if( convex(ax,ay, bx,by, cx,cy, dx,dy) >= 0 ) {
674                                         // s = ac thru b x bd thru c
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                                 } // fall thru
679                         case ARC_LINE:
680                                 vpen->draw_line(bx, by, cx, cy);
681                                 break;
682                         }
683                         ax = bx;  ay = by;  pt0 = pt1;
684                         bx = cx;  by = cy;  pt1 = pt2;
685                         cx = dx;  cy = dy;  pt2 = fill.next();
686                 }
687                 switch( pt1->arc ) {
688                 case ARC_LINE:
689                         vpen->draw_line(bx, by, cx, cy);
690                         if( fill.exists() ) {
691                                 dx = pnt0->x;  dy = pnt0->y;
692                                 vpen->draw_line(cx,cy, dx,dy);
693                         }
694                         break;
695                 case ARC_CURVE: {
696                         if( fill.exists() ) {
697                                 dx = pnt0->x;  dy = pnt0->y;
698                                 intersects_at(sx,sy, ax,ay,cx,cy,bx,by, bx,by,dx,dy,cx,cy,fmx);
699                                 vpen->draw_smooth(bx,by, sx,sy, cx,cy);
700                                 ax = bx;  ay = by;
701                                 bx = cx;  by = cy;
702                                 cx = dx;  cy = dy;
703                                 dx = pnt1->x;  dy = pnt1->y;
704                         }
705                         else
706                                 smooth_dxy(dx,dy, ax,ay, bx,by, cx,cy);
707                         intersects_at(sx,sy, ax,ay,cx,cy,bx,by, bx,by,dx,dy,cx,cy,fmx);
708                         vpen->draw_smooth(bx,by, sx,sy, cx,cy);
709                         break; }
710                 }
711                 fill.run();
712         }
713         else if( pnt0 && pnt1 ) {
714                 vpen->draw_line(pnt0->x, pnt0->y, pnt1->x, pnt1->y);
715         }
716         else if( pnt0 ) {
717                 vpen->draw_pixel(pnt0->x, pnt0->y, 1);
718         }
719         delete vpen;
720 }
721
722 int Sketcher::process_realtime(VFrame *input, VFrame *output)
723 {
724         this->input = input;  this->output = output;
725         w = output->get_w();  h = output->get_h();
726
727         load_configuration();
728
729         int out_color_model = output->get_color_model();
730         int color_model = out_color_model;
731         switch( color_model ) { // add alpha if needed
732         case BC_RGB888:         color_model = BC_RGBA8888;      break;
733         case BC_YUV888:         color_model = BC_YUVA8888;      break;
734         case BC_RGB161616:      color_model = BC_RGBA16161616;  break;
735         case BC_YUV161616:      color_model = BC_YUVA16161616;  break;
736         case BC_RGB_FLOAT:      color_model = BC_RGBA_FLOAT;    break;
737         case BC_RGB_FLOATP:     color_model = BC_RGBA_FLOATP;   break;
738         }
739         if( color_model == out_color_model ) {
740                 delete out;  out = output;
741                 if( output != input )
742                         output->transfer_from(input);
743         }
744         else {
745                 VFrame::get_temp(out, w, h, color_model);
746                 out->transfer_from(input);
747         }
748         VFrame::get_temp(img, w, h, color_model);
749         
750         if( !overlay_frame ) {
751                 int cpus = server->preferences->project_smp;
752                 int max = (w*h)/0x80000 + 2;
753                 if( cpus > max ) cpus = max;
754                 overlay_frame = new OverlayFrame(cpus);
755         }
756
757         for( int ci=0, n=config.curves.size(); ci<n; ++ci ) {
758                 SketcherCurve *cv = config.curves[ci];
759                 if( cv->pen == PEN_OFF ) continue;
760                 int m = cv->points.size();
761                 if( !m ) continue;
762                 img->clear_frame();
763                 int alias = config.aliasing < 0 ? ALIAS_OFF :
764                         config.aliasing > 0 ? ALIAS_DBL : ALIAS_NRM;
765                 cv->draw(img, alias);
766                 overlay_frame->overlay(out, img, 0,0,w,h, 0,0,w,h,
767                                 1.f, TRANSFER_SRC_OVER, NEAREST_NEIGHBOR);
768         }
769
770         if( config.drag ) {
771                 for( int ci=0, n=config.curves.size(); ci<n; ++ci ) {
772                         SketcherCurve *cv = config.curves[ci];
773                         for( int pi=0,m=cv->points.size(); pi<m; ++pi ) {
774                                 int color = pi==config.pt_selected && ci==config.cv_selected ?
775                                         RED : cv->color ; 
776                                 draw_point(out, cv->points[pi], color);
777                         }
778                 }
779         }
780
781         if( output != out )
782                 output->transfer_from(out);
783         else
784                 out = 0;
785
786         return 0;
787 }
788
789 void SketcherPoints::dump()
790 {
791         for( int i=0; i<size(); ++i ) {
792                 SketcherPoint *pt = get(i);
793                 printf("  Pt %d, id=%d, arc=%s, x=%0.1f, y=%0.1f\n",
794                         i, pt->id, pt_type[pt->arc], pt->x, pt->y);
795         }
796 }
797 void SketcherCurves::dump()
798 {
799         for( int i=0; i<size(); ++i ) {
800                 SketcherCurve *cv = get(i);
801                 printf("Curve %d, id=%d, pen=%s, r=%d, color=%02x%02x%02x%02x, %d points\n",
802                         i, cv->id, cv_pen[cv->pen], cv->width, (~cv->color>>24)&0xff,
803                         (cv->color>>16)&0xff, (cv->color>>8)&0xff, (cv->color>>0)&0xff,
804                         cv->points.size());
805                 cv->points.dump();
806         }
807 }
808 void SketcherConfig::dump()
809 {
810         printf("Config drag=%d, cv_selected=%d, pt_selected=%d %d curves\n",
811                         drag, cv_selected, pt_selected, curves.size());
812         curves.dump();
813 }
814