sketcher add alpha/fill, add alpha to vframe draw_pixel, crikey tweaks
[goodguy/cinelerra.git] / cinelerra-5.1 / plugins / sketcher / sketcher.C
index a4ce51f9e55f215e0f682768e5eec4417c2a3a15..954f7a2c64515679b1d80080e36840fe215344a7 100644 (file)
 #include "clip.h"
 #include "edlsession.h"
 #include "filexml.h"
+#include "overlayframe.h"
+#include "pluginserver.h"
+#include "preferences.h"
 #include "sketcher.h"
 #include "sketcherwindow.h"
 #include "language.h"
 #include "vframe.h"
 
-void SketcherPoint::init(int id, int x, int y)
+void SketcherPoint::init(int id, int pty, int x, int y)
 {
-       this->id = id;
+       this->id = id;  this->pty = pty;
        this->x = x;    this->y = y;
 }
 SketcherPoint::SketcherPoint(int id)
 {
-       init(id, 0, 0);
+       init(id, PTY_LINE, 0, 0);
 }
-SketcherPoint::SketcherPoint(int id, int x, int y)
+SketcherPoint::SketcherPoint(int id, int pty, int x, int y)
 {
-       init(id, x, y);
+       init(id, pty, x, y);
 }
 SketcherPoint::~SketcherPoint()
 {
@@ -57,12 +60,13 @@ SketcherPoint::SketcherPoint(SketcherPoint &pt)
 int SketcherPoint::equivalent(SketcherPoint &that)
 {
        return this->id == that.id &&
+               this->pty == that.pty &&
                this->x == that.x &&
                this->y == that.y ? 1 : 0;
 }
 void SketcherPoint::copy_from(SketcherPoint &that)
 {
-       this->id = that.id;
+       this->id = that.id;  this->pty = that.pty;
        this->x = that.x;    this->y = that.y;
 }
 void SketcherPoint::save_data(FileXML &output)
@@ -70,6 +74,7 @@ void SketcherPoint::save_data(FileXML &output)
        char point[BCSTRLEN];
        sprintf(point,"/POINT_%d",id);
        output.tag.set_title(point+1);
+       output.tag.set_property("TYPE", pty);
        output.tag.set_property("X", x);
        output.tag.set_property("Y", y);
        output.append_tag();
@@ -80,25 +85,26 @@ void SketcherPoint::save_data(FileXML &output)
 void SketcherPoint::read_data(FileXML &input)
 {
        id = atoi(input.tag.get_title() + 6);
+       pty = input.tag.get_property("TYPE", PTY_OFF);
        x = input.tag.get_property("X", 0.f);
        y = input.tag.get_property("Y", 0.f);
+       bclamp(pty, 0, PTY_SZ-1);
 }
 
-void SketcherCurve::init(int id, int ty, int radius, int pen, int color)
+void SketcherCurve::init(int id, int pen, int radius, int color)
 {
        this->id = id;
-       this->ty = ty;
        this->radius = radius;
        this->pen = pen;
        this->color = color;
 }
 SketcherCurve::SketcherCurve(int id)
 {
-       init(id, 0, 1, 0, BLACK);
+       init(id, 1, PTY_LINE, CV_COLOR);
 }
-SketcherCurve::SketcherCurve(int id, int ty,  int radius,int pen, int color)
+SketcherCurve::SketcherCurve(int id, int pen, int radius, int color)
 {
-       init(id, ty, radius, pen, color);
+       init(id, pen, radius, color);
 }
 SketcherCurve::~SketcherCurve()
 {
@@ -110,9 +116,8 @@ SketcherCurve::SketcherCurve(SketcherCurve &cv)
 int SketcherCurve::equivalent(SketcherCurve &that)
 {
        if( this->id != that.id ) return 0;
-       if( this->ty != that.ty ) return 0;
-       if( this->radius != that.radius ) return 0;
        if( this->pen != that.pen ) return 0;
+       if( this->radius != that.radius ) return 0;
        if( this->color != that.color ) return 0;
        int n = this->points.size();
        if( n != that.points.size() ) return 0;
@@ -124,9 +129,8 @@ int SketcherCurve::equivalent(SketcherCurve &that)
 void SketcherCurve::copy_from(SketcherCurve &that)
 {
        this->id = that.id;
-       this->ty = that.ty;
-       this->radius = that.radius;
        this->pen = that.pen;
+       this->radius = that.radius;
        this->color = that.color;
        int m = points.size(), n = that.points.size();
        while( m > n ) points.remove_object_number(--m);
@@ -135,14 +139,12 @@ void SketcherCurve::copy_from(SketcherCurve &that)
 }
 void SketcherCurve::save_data(FileXML &output)
 {
-       this->ty = ty;
        this->pen = pen;  this->color = color;
        char curve[BCSTRLEN];
        sprintf(curve,"/CURVE_%d",id);
        output.tag.set_title(curve+1);
-       output.tag.set_property("TYPE", ty);
-       output.tag.set_property("RADIUS", radius);
        output.tag.set_property("PEN", pen);
+       output.tag.set_property("RADIUS", radius);
        output.tag.set_property("COLOR", color);
        output.append_tag();
        output.append_newline();
@@ -155,13 +157,13 @@ void SketcherCurve::save_data(FileXML &output)
 void SketcherCurve::read_data(FileXML &input)
 {
        id = atoi(input.tag.get_title() + 6);
-       ty = input.tag.get_property("TYPE", 0);
+       pen = input.tag.get_property("PEN", PTY_OFF);
        radius = input.tag.get_property("RADIUS", 1.);
-       pen = input.tag.get_property("PEN", 0);
-       color = input.tag.get_property("COLOR", BLACK);
+       color = input.tag.get_property("COLOR", CV_COLOR);
+       bclamp(pen, 0, PEN_SZ-1);
 }
 
-int Sketcher::new_curve(int ty, int radius, int pen, int color)
+int Sketcher::new_curve(int pen, int radius, int color)
 {
        SketcherCurves &curves = config.curves;
        int k = curves.size(), id = 1;
@@ -169,7 +171,7 @@ int Sketcher::new_curve(int ty, int radius, int pen, int color)
                int n = config.curves[i]->id;
                if( n >= id ) id = n + 1;
        }
-       SketcherCurve *cv = new SketcherCurve(id, ty, radius, pen, color);
+       SketcherCurve *cv = new SketcherCurve(id, pen, radius, color);
        curves.append(cv);
        config.cv_selected = k;
        return k;
@@ -177,22 +179,24 @@ int Sketcher::new_curve(int ty, int radius, int pen, int color)
 
 int Sketcher::new_curve()
 {
-       return new_curve(0, 1, 0, BLACK);
+       return new_curve(PEN_XLANT, 1, CV_COLOR);
 }
 
-int Sketcher::new_point(SketcherCurve *cv, int x, int y)
+int Sketcher::new_point(SketcherCurve *cv, int pty, int x, int y, int idx)
 {
-       int k = cv->points.size(), id = 1;
-       for( int i=k; --i>=0; ) {
+       int id = 1;
+       for( int i=cv->points.size(); --i>=0; ) {
                int n = cv->points[i]->id;
                if( n >= id ) id = n + 1;
        }
-       SketcherPoint *pt = new SketcherPoint(id, x, y);
-       cv->points.append(pt);
-       return k;
+       SketcherPoint *pt = new SketcherPoint(id, pty, x, y);
+       int n = cv->points.size();
+       if( idx < 0 || idx > n ) idx = n;
+       cv->points.insert(pt, idx);
+       return idx;
 }
 
-int Sketcher::new_point()
+int Sketcher::new_point(int idx)
 {
        int ci = config.cv_selected;
        if( ci < 0 || ci >= config.curves.size() )
@@ -201,9 +205,38 @@ int Sketcher::new_point()
        EDLSession *session = get_edlsession();
        int x = !session ? 0.f : session->output_w / 2.f;
        int y = !session ? 0.f : session->output_h / 2.f;
-       return new_point(cv, x, y);
+       return new_point(cv, PTY_LINE, x, y, idx);
 }
 
+double SketcherCurve::nearest_point(int &pi, float x, float y)
+{
+       pi = -1;
+       double dist = DBL_MAX;
+       for( int i=0; i<points.size(); ++i ) {
+               SketcherPoint *p = points[i];
+               double d = DISTANCE(x,y, p->x,p->y);
+               if( d < dist ) { dist = d;  pi = i;  }
+       }
+       return pi >= 0 ? dist : -1.;
+}
+
+double SketcherConfig::nearest_point(int &ci, int &pi, float x, float y)
+{
+       double dist = DBL_MAX;
+       ci = -1;  pi = -1;
+       for( int i=0; i<curves.size(); ++i ) {
+               SketcherCurve *crv = curves[i];
+               SketcherPoints &points = crv->points;
+               for( int k=0; k<points.size(); ++k ) {
+                       SketcherPoint *p = points[k];
+                       double d = DISTANCE(x,y, p->x,p->y);
+                       if( d < dist ) {  dist = d; ci = i;  pi = k; }
+               }
+       }
+       return pi >= 0 ? dist : -1.;
+}
+
+
 REGISTER_PLUGIN(Sketcher)
 
 SketcherConfig::SketcherConfig()
@@ -259,9 +292,8 @@ void SketcherConfig::interpolate(SketcherConfig &prev, SketcherConfig &next,
                while( --k >= 0 && pcv->id != (ncv=next.curves[k])->id );
                if( k >= 0 ) {
                        cv->id = pcv->id;
-                       cv->ty = pcv->ty;
-                       cv->radius = pcv->radius;
                        cv->pen = pcv->pen;
+                       cv->radius = pcv->radius;
                        cv->color = pcv->color;
                        int prev_pt_sz = pcv->points.size(), next_pt_sz = ncv->points.size();
                        for( int j=0; j<prev_pt_sz; ++j ) {
@@ -273,7 +305,7 @@ void SketcherConfig::interpolate(SketcherConfig &prev, SketcherConfig &next,
                                        x = x * prev_scale + nt->x * next_scale;
                                        y = y * prev_scale + nt->y * next_scale;
                                }
-                               cv->points.append(new SketcherPoint(pt.id, x, y));
+                               cv->points.append(new SketcherPoint(pt.id, pt.pty, x, y));
                        }
                }
                else
@@ -289,10 +321,16 @@ void SketcherConfig::limits()
 Sketcher::Sketcher(PluginServer *server)
  : PluginVClient(server)
 {
+       img = 0;
+       out = 0;
+       overlay_frame = 0;
 }
 
 Sketcher::~Sketcher()
 {
+       delete img;
+       delete out;
+       delete overlay_frame;
 }
 
 const char* Sketcher::plugin_title() { return N_("Sketcher"); }
@@ -309,8 +347,8 @@ void Sketcher::save_data(KeyFrame *keyframe)
 
        output.tag.set_title("SKETCHER");
        output.tag.set_property("DRAG", config.drag);
-       output.tag.set_property("CURVE_SELECTED", config.cv_selected);
-       output.tag.set_property("POINT_SELECTED", config.pt_selected);
+       output.tag.set_property("CV_SELECTED", config.cv_selected);
+       output.tag.set_property("PT_SELECTED", config.pt_selected);
        output.append_tag();
        output.append_newline();
        for( int i=0,n=config.curves.size(); i<n; ++i ) {
@@ -354,9 +392,8 @@ void Sketcher::read_data(KeyFrame *keyframe)
                }
        }
 
-       if( !config.curves.size() ) {
-               new_curve(0, 1, 0, BLACK);
-       }
+       if( !config.curves.size() )
+               new_curve();
        config.limits();
 }
 
@@ -375,11 +412,11 @@ void Sketcher::update_gui()
 void Sketcher::draw_point(VFrame *vfrm, SketcherPoint *pt, int color, int d)
 {
        int r = d/2+1, x = pt->x, y = pt->y;
+       vfrm->set_pixel_color(color, 0xff);
        vfrm->draw_smooth(x-r,y+0, x-r, y-r, x+0,y-r);
        vfrm->draw_smooth(x+0,y-r, x+r, y-r, x+r,y+0);
        vfrm->draw_smooth(x+r,y+0, x+r, y+r, x+0,y+r);
        vfrm->draw_smooth(x+0,y+r, x-r, y+r, x-r,y+0);
-       vfrm->set_pixel_color(color);
        vfrm->draw_x(pt->x, pt->y, d);
 }
 void Sketcher::draw_point(VFrame *vfrm, SketcherPoint *pt, int color)
@@ -388,12 +425,20 @@ void Sketcher::draw_point(VFrame *vfrm, SketcherPoint *pt, int color)
 }
 
 
+int SketcherVPen::draw_pixel(int x, int y)
+{
+       if( x >= 0 && x < vfrm->get_w() &&
+           y >= 0 && y < vfrm->get_h() )
+               msk[vfrm->get_w()*y + x] = 0xff;
+       return 0;
+}
+
 int SketcherPenSquare::draw_pixel(int x, int y)
 {
        vfrm->draw_line(x-n, y, x+n, y);
        for( int i=-n; i<n; ++i )
                vfrm->draw_line(x-n, y+i, x+n, y+i);
-       return 0;
+       return SketcherVPen::draw_pixel(x, y);
 }
 int SketcherPenPlus::draw_pixel(int x, int y)
 {
@@ -403,14 +448,14 @@ int SketcherPenPlus::draw_pixel(int x, int y)
        }
        else
                vfrm->draw_pixel(x, y);
-       return 0;
+       return SketcherVPen::draw_pixel(x, y);
 }
 int SketcherPenSlant::draw_pixel(int x, int y)
 {
        vfrm->draw_line(x-n,   y+n,   x+n,   y-n);
        vfrm->draw_line(x-n+1, y+n,   x+n+1, y-n);
        vfrm->draw_line(x-n,   y+n+1, x+n,   y-n+1);
-       return 0;
+       return SketcherVPen::draw_pixel(x, y);
 }
 int SketcherPenXlant::draw_pixel(int x, int y)
 {
@@ -420,11 +465,11 @@ int SketcherPenXlant::draw_pixel(int x, int y)
        vfrm->draw_line(x-n,   y-n,   x+n,   y+n);
        vfrm->draw_line(x-n+1, y-n,   x+n+1, y+n);
        vfrm->draw_line(x-n,   y-n+1, x+n,   y-n+1);
-       return 0;
+       return SketcherVPen::draw_pixel(x, y);
 }
 
 
-VFrame *SketcherCurve::new_vpen(VFrame *out)
+SketcherVPen *SketcherCurve::new_vpen(VFrame *out)
 {
        switch( pen ) {
        case PEN_SQUARE: return new SketcherPenSquare(out, radius);
@@ -435,164 +480,209 @@ VFrame *SketcherCurve::new_vpen(VFrame *out)
        return 0;
 }
 
-void SketcherCurve::draw_line(VFrame *out)
+static int intersects_at(float &x, float &y,
+               float ax,float ay, float bx, float by, float cx,float cy,  // line slope ab thru c
+               float dx,float dy, float ex, float ey, float fx,float fy, // line slope de thru f
+               float mx=0)
 {
-       SketcherPoint *pt0 = points[0];
-       VFrame *vpen = new_vpen(out);
-       out->set_pixel_color(color);
-       int n = points.size();
-       if( n >= 2 ) {
-               for( int pi=1; pi<n; ++pi ) {
-                       SketcherPoint *pt1 = points[pi];
-                       vpen->draw_line(pt0->x, pt0->y, pt1->x, pt1->y);
-                       pt0 = pt1;
-               }
-       }
-       else
-               vpen->draw_pixel(pt0->x, pt0->y);
-       delete vpen;
+       float badx = bx - ax, bady = by - ay;
+       float eddx = ex - dx, eddy = ey - dy;
+       float d = badx*eddy - bady*eddx;
+       int ret = 0;
+       if( fabsf(d) < 1 ) { ret = 1;  d = signbit(d) ? -1 : 1; }
+       x = (badx*cy*eddx - badx*eddx*fy + badx*eddy*fx - bady*cx*eddx) / d;
+       y = (badx*cy*eddy - bady*cx*eddy - bady*eddx*fy + bady*eddy*fx) / d;
+       if( mx > 0 ) { bclamp(x, -mx,mx);  bclamp(y, -mx,mx); }
+       return ret;
 }
 
-/*
-# python
-from sympy import *
-var("x,y, ax,ay, bx,by, cx,cy, dx,dy")
-
-var("abdx,abdy, acdx,acdy, bddx,bddy, cddx,cddy");
-abdx = bx-ax;  abdy = by-ay;
-acdx = cx-ax;  acdy = cy-ay;
-bddx = dx-bx;  bddy = dy-by;
-cddx = dx-cx;  cddy = dy-cy;
-
-var("xc,yc, xd,yd, sx,sy");
-xc = (bx+dx)/2;  yc = (by+dy)/2;
-xd = cx-xc;      yd = cy-yc;
-ax = xc-xd;      ay = yc-yd;
-# line thru b with slope (c-a) intersects line thru c with slope (d-b)
-sx = solve(((x - bx) * acdy/acdx + by) - ((x - cx) * bddy/bddx + cy),x)
-sy = solve(((y - by) * acdx/acdy + bx) - ((y - cy) * bddx/bddy + cx),y)
-
-var("zx,zy, zdx,zdy, sx,sy, px,py, qx,qy");
-# point z = (b+c)/2
-zx = (bx+cx)/2;  zy = (by+cy)/2;
-zdx = (abdx+cddx)/2;  zdy = (abdy+cddy)/2;
-# line thru z with slope (d-a) intersects line thru b with slope (c-a)
-px = solve(((x-zx)*zdy/zdx + zy) - ((x-bx) * acdy/acdx + by),x);
-py = solve(((y-zy)*zdx/zdy + zx) - ((y-by) * acdx/acdy + bx),y);
-# line thru z with slope (c-a + d-b)/2 intersects line thru c with slope (d-b)
-qx = solve(((x-zx)*zdy/zdx + zy) - ((x-cx) * bddy/bddx + cy),x);
-qy = solve(((y-zy)*zdx/zdy + zx) - ((y-cy) * bddx/bddy + cx),y);
-*/
-
-static void smooth_sxy(
-       float ax, float ay, float bx, float by,
-       float cx, float cy, float dx, float dy,
-       float &sx, float &sy)
+static void smooth_axy(float &ax, float &ay,
+       float bx, float by, float cx, float cy, float dx, float dy)
 {
-       float acdx = cx-ax, acdy = cy-ay;
-       float bddx = dx-bx, bddy = dy-by;
-       float d = acdx*bddy - acdy*bddx;
-       if( fabsf(d) < 1 ) d = 1;
-       sx = (acdx*bddx*by - acdx*bddx*cy + acdx*bddy*cx - acdy*bddx*bx) / d;
-       sy = (acdx*bddy*by - acdy*bddx*cy - acdy*bddy*bx + acdy*bddy*cx) / d;
-       bclamp(sx, -4095.f, 4095.f);
-       bclamp(sy, -4095.f, 4095.f);
-}
-
-static void smooth_pxy(
-       float ax, float ay, float bx, float by,
-       float cx, float cy, float dx, float dy,
-       float &px, float &py)
-{
-       float abdx = bx - ax, abdy = by - ay;
-       float acdx = cx - ax, acdy = cy - ay;
-       float cddx = dx - cx, cddy = dy - cy;
-       float d = (2*(abdx*acdy - abdy*acdx - acdx*cddy + acdy*cddx));
-       if( fabsf(d) < 1 ) d = 1;
-       px = (-abdx*acdx*by + abdx*acdx*cy + 2*abdx*acdy*bx - abdy*acdx*bx - abdy*acdx*cx -
-               acdx*bx*cddy - acdx*by*cddx + acdx*cddx*cy - acdx*cddy*cx + 2*acdy*bx*cddx) / d;
-       py = (abdx*acdy*by + abdx*acdy*cy - 2*abdy*acdx*by + abdy*acdy*bx - abdy*acdy*cx -
-                2*acdx*by*cddy + acdy*bx*cddy + acdy*by*cddx + acdy*cddx*cy - acdy*cddy*cx) / d;
-       bclamp(px, -4095.f, 4095.f);
-       bclamp(py, -4095.f, 4095.f);
-}
-static void smooth_qxy(
-       float ax, float ay, float bx, float by,
-       float cx, float cy, float dx, float dy,
-       float &qx, float &qy)
-{
-       float abdx = bx - ax, abdy = by - ay;
-       float bddx = dx - bx, bddy = dy - by;
-       float cddx = dx - cx, cddy = dy - cy;
-       float d = (2*(abdx*bddy - abdy*bddx - bddx*cddy + bddy*cddx));
-       if( fabsf(d) < 1 ) d = 1;
-       qx = (abdx*bddx*by - abdx*bddx*cy + 2*abdx*bddy*cx - abdy*bddx*bx - abdy*bddx*cx -
-               bddx*bx*cddy + bddx*by*cddx - bddx*cddx*cy - bddx*cddy*cx + 2*bddy*cddx*cx) / d;
-       qy = (abdx*bddy*by + abdx*bddy*cy - 2*abdy*bddx*cy - abdy*bddy*bx + abdy*bddy*cx -
-               2*bddx*cddy*cy - bddy*bx*cddy + bddy*by*cddx + bddy*cddx*cy + bddy*cddy*cx) / d;
-       bclamp(qx, -4095.f, 4095.f);
-       bclamp(qy, -4095.f, 4095.f);
+//middle of bd reflected around ctr
+// point ctr = (b+d)/2, dv=c-ctr, a=ctr-dv;
+       float xc = (bx+dx)*.5f, yc = (by+dy)*.5f;
+       float xd = cx - xc, yd = cy - yc;
+       ax = xc - xd;  ay = yc - yd;
+}
+static void smooth_dxy(float &dx, float &dy,
+       float ax, float ay, float bx, float by, float cx, float cy)
+{
+//middle of ac reflected around ctr
+// point ctr = (a+c)/2, dv=c-ctr, d=ctr-dv;
+       float xc = (ax+cx)*.5f, yc = (ay+cy)*.5f;
+       float xd = bx - xc, yd = by - yc;
+       dx = xc - xd;  dy = yc - yd;
 }
 
-
+#if 0
 static int convex(float ax,float ay, float bx,float by,
                  float cx,float cy, float dx,float dy)
 {
-       float abdx = bx - ax, abdy = by - ay;
-       float acdx = cx - ax, acdy = cy - ay;
-       float bcdx = cx - bx, bcdy = cy - by;
-       float bddx = dx - bx, bddy = dy - by;
+       float abdx = bx-ax, abdy = by-ay;
+       float acdx = cx-ax, acdy = cy-ay;
+       float bcdx = cx-bx, bcdy = cy-by;
+       float bddx = dx-bx, bddy = dy-by;
        float abc = abdx*acdy - abdy*acdx;
        float bcd = bcdx*bddy - bcdy*bddx;
        float v = abc * bcd;
        return !v ? 0 : v>0 ? 1 : -1;
 }
+#endif
+
+
+class FillRegion
+{
+       class segment { public: int y, lt, rt; };
+       ArrayList<segment> stack;
 
-void SketcherCurve::draw_smooth(VFrame *out)
-{
-       VFrame *vpen = new_vpen(out);
-       out->set_pixel_color(color);
-       int n = points.size();
-       if( !n ) return;
-       if( n > 2 ) {
-               SketcherPoint *pt0 = points[0], *pt1 = points[1], *pt2 = points[2];
-               float bx = pt0->x, by = pt0->y;
-               float cx = pt1->x, cy = pt1->y;
-               float dx = pt2->x, dy = pt2->y;
-               float xc = (bx+dx)/2.f, yc = (by+dy)/2.f;
-               float xd = cx - xc, yd = cy - yc;
-               float ax = xc - xd, ay = yc - yd;
-               float sx, sy;
-               for( int pi=0,n2=n-2; pi<n2; ++pi ) {
-                       float dx = points[pi+2]->x, dy = points[pi+2]->y;
-                       if( convex(ax,ay, bx,by, cx,cy, dx,dy) >= 0 ) {
-                               smooth_sxy(ax,ay, bx,by, cx,cy, dx,dy, sx, sy);
+       void push(int y, int lt, int rt) {
+               segment &seg = stack.append();
+               seg.y = y;  seg.lt = lt;  seg.rt = rt;
+       }
+       void pop(int &y, int &lt, int &rt) {
+               segment &seg = stack.last();
+               y = seg.y;  lt = seg.lt;  rt = seg.rt;
+               stack.remove();
+       }
+       VFrame *img;
+       uint8_t *msk;
+       int w, h, nxt;
+       SketcherPoints &points;
+public:
+       SketcherPoint *next();
+       bool exists() { return stack.size() > 0; }
+       void start_at(int x, int y);
+       void run();
+       FillRegion(SketcherPoints &pts, SketcherVPen *vpen);
+       ~FillRegion();
+};
+
+FillRegion::FillRegion(SketcherPoints &pts, SketcherVPen *vpen)
+ : points(pts)
+{
+       this->img = vpen->vfrm;
+       this->msk = vpen->msk;
+       this->w = img->get_w();
+       this->h = img->get_h();
+       nxt = 0;
+}
+FillRegion::~FillRegion()
+{
+}
+
+void FillRegion::start_at(int x, int y)
+{
+       bclamp(x, 0, w-1);
+       bclamp(y, 0, h-1);
+       push(y, x, x);
+}
+
+void FillRegion::run()
+{
+       while( stack.size() > 0 ) {
+               int y, ilt, irt;
+               pop(y, ilt, irt);
+               int ofs = y*w + ilt;
+               for( int x=ilt; x<=irt; ++x,++ofs ) {
+                       if( msk[ofs] ) continue;
+                       msk[ofs] = 0xff;
+                       img->draw_pixel(x, y);
+                       int lt = x, rt = x;
+                       int lofs = ofs;
+                       for( int i=lt; --i>=0; ) {
+                               if( msk[--lofs] ) break;
+                               img->draw_pixel(i, y);
+                               msk[lofs] = 0xff;  lt = i;
+                       }
+                       int rofs = ofs;
+                       for( int i=rt; ++i< w; ) {
+                               if( msk[++rofs] ) break;
+                               img->draw_pixel(i, y);
+                               msk[rofs] = 0xff;  rt = i;
+                       }
+                       if( y+1 <  h ) push(y+1, lt, rt);
+                       if( y-1 >= 0 ) push(y-1, lt, rt);
+               }
+       }
+}
+
+SketcherPoint *FillRegion::next()
+{
+       while( nxt < points.size() ) {
+               SketcherPoint *pt = points[nxt];
+               if( pt->pty != PTY_FILL ) break;
+               start_at(pt->x, pt->y);
+               ++nxt;
+       }
+       return nxt < points.size() ? points[nxt++] : 0;
+}
+
+
+void SketcherCurve::draw(VFrame *img)
+{
+       if( !points.size() ) return;
+       const float fmx = 16383;
+       SketcherVPen *vpen = new_vpen(img);
+       FillRegion fill(points, vpen);
+       SketcherPoint *pnt0 = fill.next();
+       SketcherPoint *pnt1 = pnt0 ? fill.next() : 0;
+       SketcherPoint *pnt2 = pnt1 ? fill.next() : 0;
+       if( pnt0 && pnt1 && pnt2 ) {
+               SketcherPoint *pt0 = pnt0, *pt1 = pnt1, *pt2 = pnt2;
+               float ax,ay, bx,by, cx,cy, dx,dy, sx,sy;
+               bx = pt0->x;  by = pt0->y;
+               cx = pt1->x;  cy = pt1->y;
+               dx = pt2->x;  dy = pt2->y;
+               smooth_axy(ax,ay, bx,by, cx,cy, dx,dy);
+               while( pt2 ) {
+                       dx = pt2->x;  dy = pt2->y;
+                       switch( pt0->pty ) {
+                       case PTY_LINE:
+                               vpen->draw_line(bx, by, cx, cy);
+                               break;
+                       case PTY_CURVE: {
+                               // s = ac thru b x bd thru c
+                               intersects_at(sx,sy, ax,ay,cx,cy,bx,by, bx,by,dx,dy,cx,cy,fmx);
                                vpen->draw_smooth(bx,by, sx,sy, cx,cy);
+                               break; }
+                       }
+                       ax = bx;  ay = by;  pt0 = pt1;
+                       bx = cx;  by = cy;  pt1 = pt2;
+                       cx = dx;  cy = dy;  pt2 = fill.next();
+               }
+               switch( pt1->pty ) {
+               case PTY_LINE:
+                       vpen->draw_line(bx, by, cx, cy);
+                       if( fill.exists() ) {
+                               dx = pnt0->x;  dy = pnt0->y;
+                               vpen->draw_line(cx,cy, dx,dy);
                        }
-                       else {
-                               float zx = (bx+cx)/2.f, zy = (by+cy)/2.f;
-                               smooth_pxy(ax,ay, bx,by, cx,cy, dx,dy, sx,sy);
-                               vpen->draw_smooth(bx,by, sx,sy, zx,zy);
-                               smooth_qxy(ax,ay, bx,by, cx,cy, dx,dy, sx,sy);
-                               vpen->draw_smooth(zx,zy, sx,sy, cx,cy);
+                       break;
+               case PTY_CURVE: {
+                       if( fill.exists() ) {
+                               dx = pnt0->x;  dy = pnt0->y;
+                               intersects_at(sx,sy, ax,ay,cx,cy,bx,by, bx,by,dx,dy,cx,cy,fmx);
+                               vpen->draw_smooth(bx,by, sx,sy, cx,cy);
+                               ax = bx;  ay = by;
+                               bx = cx;  by = cy;
+                               cx = dx;  cy = dy;
+                               dx = pnt1->x;  dy = pnt1->y;
                        }
-                       ax = bx;  ay = by;
-                       bx = cx;  by = cy;
-                       cx = dx;  cy = dy;
+                       else
+                               smooth_dxy(dx,dy, ax,ay, bx,by, cx,cy);
+                       intersects_at(sx,sy, ax,ay,cx,cy,bx,by, bx,by,dx,dy,cx,cy,fmx);
+                       vpen->draw_smooth(bx,by, sx,sy, cx,cy);
+                       break; }
                }
-               xc = (ax+cx)/2.f; yc = (ay+cy)/2.f;
-               xd = bx - xc, yd = by - yc;
-               dx = xc - xd, dy = yc - yd;
-               smooth_sxy(ax, ay, bx, by, cx, cy, dx, dy, sx, sy);
-               vpen->draw_smooth(bx, by, sx, sy, cx, cy);
+               fill.run();
        }
-       else if( n == 2 ) {
-               SketcherPoint *pt0 = points[0], *pt1 = points[1];
-               vpen->draw_line(pt0->x, pt0->y, pt1->x, pt1->y);
+       else if( pnt0 && pnt1 ) {
+               vpen->draw_line(pnt0->x, pnt0->y, pnt1->x, pnt1->y);
        }
-       else if( n > 0 ) {
-               SketcherPoint *pt0 = points[0];
-               vpen->draw_pixel(pt0->x, pt0->y);
+       else if( pnt0 ) {
+               vpen->draw_pixel(pnt0->x, pnt0->y);
        }
        delete vpen;
 }
@@ -601,52 +691,90 @@ int Sketcher::process_realtime(VFrame *input, VFrame *output)
 {
        this->input = input;  this->output = output;
        w = output->get_w();  h = output->get_h();
-       if( input != output ) output->transfer_from(input);
 
        load_configuration();
 
+       int out_color_model = output->get_color_model();
+       int color_model = out_color_model;
+       switch( color_model ) { // add alpha if needed
+       case BC_RGB888:         color_model = BC_RGBA8888;      break;
+       case BC_YUV888:         color_model = BC_YUVA8888;      break;
+       case BC_RGB161616:      color_model = BC_RGBA16161616;  break;
+       case BC_YUV161616:      color_model = BC_YUVA16161616;  break;
+       case BC_RGB_FLOAT:      color_model = BC_RGBA_FLOAT;    break;
+       case BC_RGB_FLOATP:     color_model = BC_RGBA_FLOATP;   break;
+       }
+       if( color_model == out_color_model ) {
+               delete out;  out = output;
+               if( output != input )
+                       output->transfer_from(input);
+       }
+       else {
+               VFrame::get_temp(out, w, h, color_model);
+               out->transfer_from(input);
+       }
+       VFrame::get_temp(img, w, h, color_model);
+       
+       if( !overlay_frame ) {
+               int cpus = server->preferences->project_smp;
+               int max = (w*h)/0x80000 + 2;
+               if( cpus > max ) cpus = max;
+               overlay_frame = new OverlayFrame(cpus);
+       }
+
        for( int ci=0, n=config.curves.size(); ci<n; ++ci ) {
                SketcherCurve *cv = config.curves[ci];
                int m = cv->points.size();
-               if( !m ) continue;
-               switch( cv->ty ) {
-               case TYP_OFF:
-                       break;
-               case TYP_SKETCHER:
-                       cv->draw_line(output);
-                       break;
-               case TYP_SMOOTH:
-                       cv->draw_smooth(output);
-                       break;
-               }
+               if( !m || cv->pen == PTY_OFF ) continue;
+               img->clear_frame();
+               img->set_pixel_color(cv->color);
+               cv->draw(img);
+               overlay_frame->overlay(out, img, 0,0,w,h, 0,0,w,h,
+                               1.f, TRANSFER_NORMAL, NEAREST_NEIGHBOR);
        }
 
        if( config.drag ) {
                for( int ci=0, n=config.curves.size(); ci<n; ++ci ) {
                        SketcherCurve *cv = config.curves[ci];
-                       for( int pi=0,m=cv->points.size(); pi<m; ++pi )
-                               draw_point(output, cv->points[pi], cv->color);
+                       for( int pi=0,m=cv->points.size(); pi<m; ++pi ) {
+                               int color = pi==config.pt_selected && ci==config.cv_selected ?
+                                       RED : cv->color ; 
+                               draw_point(out, cv->points[pi], color);
+                       }
                }
        }
 
+       if( output != out )
+               output->transfer_from(out);
+       else
+               out = 0;
+
        return 0;
 }
 
+void SketcherPoints::dump()
+{
+       for( int i=0; i<size(); ++i ) {
+               SketcherPoint *pt = get(i);
+               printf("  Pt %d, id=%d, pty=%s, x=%d, y=%d\n",
+                       i, pt->id, pt_type[pt->pty], pt->x, pt->y);
+       }
+}
 void SketcherCurves::dump()
 {
        for( int i=0; i<size(); ++i ) {
                SketcherCurve *cv = get(i);
-               printf("Curve %d, id=%d, ty=%s, r=%d, pen=%s, color=%02x%02x%02x\n",
-                       i, cv->id, cv_type[cv->ty], cv->radius, cv_pen[cv->pen],
-                       (cv->color>>16)&0xff, (cv->color>>8)&0xff, (cv->color>>0)&0xff);
+               printf("Curve %d, id=%d, pen=%s, r=%d, color=%02x%02x%02x, %d points\n",
+                       i, cv->id, cv_pen[cv->pen], cv->radius,
+                       (cv->color>>16)&0xff, (cv->color>>8)&0xff, (cv->color>>0)&0xff,
+                       cv->points.size());
                cv->points.dump();
        }
 }
-void SketcherPoints::dump()
+void SketcherConfig::dump()
 {
-       for( int i=0; i<size(); ++i ) {
-               SketcherPoint *pt = get(i);
-               printf("  Pt %d, id=%d, x=%d, y=%d\n", i, pt->id, pt->x, pt->y);
-       }
+       printf("Config drag=%d, cv_selected=%d, pt_selected=%d %d curves\n",
+                       drag, cv_selected, pt_selected, curves.size());
+       curves.dump();
 }