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