sketcher tweaks + strengthen, yuv alpha draw_pixel fix
[goodguy/cinelerra.git] / cinelerra-5.1 / plugins / sketcher / sketcher.C
index 954f7a2c64515679b1d80080e36840fe215344a7..9bbdd675e22322929edad0a858d3391c2010f0b8 100644 (file)
 #include "language.h"
 #include "vframe.h"
 
-void SketcherPoint::init(int id, int pty, int x, int y)
+void SketcherPoint::init(int id, int arc, coord x, coord y)
 {
-       this->id = id;  this->pty = pty;
+       this->id = id;  this->arc = arc;
        this->x = x;    this->y = y;
 }
 SketcherPoint::SketcherPoint(int id)
 {
-       init(id, PTY_LINE, 0, 0);
+       init(id, ARC_LINE, 0, 0);
 }
-SketcherPoint::SketcherPoint(int id, int pty, int x, int y)
+SketcherPoint::SketcherPoint(int id, int arc, coord x, coord y)
 {
-       init(id, pty, x, y);
+       init(id, arc, x, y);
 }
 SketcherPoint::~SketcherPoint()
 {
@@ -60,13 +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;
+               this->arc == that.arc &&
+               EQUIV(this->x, that.x) &&
+               EQUIV(this->y, that.y) ? 1 : 0;
 }
 void SketcherPoint::copy_from(SketcherPoint &that)
 {
-       this->id = that.id;  this->pty = that.pty;
+       this->id = that.id;  this->arc = that.arc;
        this->x = that.x;    this->y = that.y;
 }
 void SketcherPoint::save_data(FileXML &output)
@@ -74,7 +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("TYPE", arc);
        output.tag.set_property("X", x);
        output.tag.set_property("Y", y);
        output.append_tag();
@@ -85,26 +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);
+       arc = input.tag.get_property("TYPE", ARC_OFF);
+       x = input.tag.get_property("X", (coord)0);
+       y = input.tag.get_property("Y", (coord)0);
+       bclamp(arc, 0, ARC_SZ-1);
 }
 
-void SketcherCurve::init(int id, int pen, int radius, int color)
+void SketcherCurve::init(int id, int pen, int width, int color)
 {
        this->id = id;
-       this->radius = radius;
+       this->width = width;
        this->pen = pen;
        this->color = color;
 }
 SketcherCurve::SketcherCurve(int id)
 {
-       init(id, 1, PTY_LINE, CV_COLOR);
+       init(id, 1, ARC_LINE, CV_COLOR);
 }
-SketcherCurve::SketcherCurve(int id, int pen, int radius, int color)
+SketcherCurve::SketcherCurve(int id, int pen, int width, int color)
 {
-       init(id, pen, radius, color);
+       init(id, pen, width, color);
 }
 SketcherCurve::~SketcherCurve()
 {
@@ -117,7 +117,7 @@ int SketcherCurve::equivalent(SketcherCurve &that)
 {
        if( this->id != that.id ) return 0;
        if( this->pen != that.pen ) return 0;
-       if( this->radius != that.radius ) return 0;
+       if( this->width != that.width ) return 0;
        if( this->color != that.color ) return 0;
        int n = this->points.size();
        if( n != that.points.size() ) return 0;
@@ -130,7 +130,7 @@ void SketcherCurve::copy_from(SketcherCurve &that)
 {
        this->id = that.id;
        this->pen = that.pen;
-       this->radius = that.radius;
+       this->width = that.width;
        this->color = that.color;
        int m = points.size(), n = that.points.size();
        while( m > n ) points.remove_object_number(--m);
@@ -139,12 +139,11 @@ void SketcherCurve::copy_from(SketcherCurve &that)
 }
 void SketcherCurve::save_data(FileXML &output)
 {
-       this->pen = pen;  this->color = color;
        char curve[BCSTRLEN];
        sprintf(curve,"/CURVE_%d",id);
        output.tag.set_title(curve+1);
        output.tag.set_property("PEN", pen);
-       output.tag.set_property("RADIUS", radius);
+       output.tag.set_property("RADIUS", width);
        output.tag.set_property("COLOR", color);
        output.append_tag();
        output.append_newline();
@@ -157,13 +156,13 @@ void SketcherCurve::save_data(FileXML &output)
 void SketcherCurve::read_data(FileXML &input)
 {
        id = atoi(input.tag.get_title() + 6);
-       pen = input.tag.get_property("PEN", PTY_OFF);
-       radius = input.tag.get_property("RADIUS", 1.);
+       pen = input.tag.get_property("PEN", PEN_OFF);
+       width = input.tag.get_property("RADIUS", 1.);
        color = input.tag.get_property("COLOR", CV_COLOR);
        bclamp(pen, 0, PEN_SZ-1);
 }
 
-int Sketcher::new_curve(int pen, int radius, int color)
+int Sketcher::new_curve(int pen, int width, int color)
 {
        SketcherCurves &curves = config.curves;
        int k = curves.size(), id = 1;
@@ -171,7 +170,7 @@ int Sketcher::new_curve(int pen, int radius, int color)
                int n = config.curves[i]->id;
                if( n >= id ) id = n + 1;
        }
-       SketcherCurve *cv = new SketcherCurve(id, pen, radius, color);
+       SketcherCurve *cv = new SketcherCurve(id, pen, width, color);
        curves.append(cv);
        config.cv_selected = k;
        return k;
@@ -182,33 +181,33 @@ int Sketcher::new_curve()
        return new_curve(PEN_XLANT, 1, CV_COLOR);
 }
 
-int Sketcher::new_point(SketcherCurve *cv, int pty, int x, int y, int idx)
+int Sketcher::new_point(SketcherCurve *cv, int arc, coord x, coord y, int idx)
 {
        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, pty, x, y);
+       SketcherPoint *pt = new SketcherPoint(id, arc, 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 idx)
+int Sketcher::new_point(int idx, int arc)
 {
        int ci = config.cv_selected;
        if( ci < 0 || ci >= config.curves.size() )
                return -1;
        SketcherCurve *cv = config.curves[ci];
        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, PTY_LINE, x, y, idx);
+       coord x = !session ? 0.f : session->output_w / 2.f;
+       coord y = !session ? 0.f : session->output_h / 2.f;
+       return new_point(cv, arc, x, y, idx);
 }
 
-double SketcherCurve::nearest_point(int &pi, float x, float y)
+double SketcherCurve::nearest_point(int &pi, coord x, coord y)
 {
        pi = -1;
        double dist = DBL_MAX;
@@ -220,7 +219,7 @@ double SketcherCurve::nearest_point(int &pi, float x, float y)
        return pi >= 0 ? dist : -1.;
 }
 
-double SketcherConfig::nearest_point(int &ci, int &pi, float x, float y)
+double SketcherConfig::nearest_point(int &ci, int &pi, coord x, coord y)
 {
        double dist = DBL_MAX;
        ci = -1;  pi = -1;
@@ -293,19 +292,31 @@ void SketcherConfig::interpolate(SketcherConfig &prev, SketcherConfig &next,
                if( k >= 0 ) {
                        cv->id = pcv->id;
                        cv->pen = pcv->pen;
-                       cv->radius = pcv->radius;
-                       cv->color = pcv->color;
+                       cv->width = pcv->width == ncv->width ? pcv->width :
+                               pcv->width*prev_scale + ncv->width*next_scale + 0.5;
+                       int pr =  (pcv->color>>16)&0xff, nr =  (ncv->color>>16)&0xff;
+                       int pg =  (pcv->color>> 8)&0xff, ng =  (ncv->color>> 8)&0xff;
+                       int pb =  (pcv->color>> 0)&0xff, nb =  (ncv->color>> 0)&0xff;
+                       int pa = (~pcv->color>>24)&0xff, na = (~ncv->color>>24)&0xff;
+                       int r = pr == nr ? pr : pr*prev_scale + nr*next_scale + 0.5;
+                       int g = pg == ng ? pg : pg*prev_scale + ng*next_scale + 0.5;
+                       int b = pb == nb ? pb : pb*prev_scale + nb*next_scale + 0.5;
+                       int a = pa == na ? pa : pa*prev_scale + na*next_scale + 0.5;
+                       bclamp(r,0,255); bclamp(g,0,255); bclamp(b,0,255); bclamp(a,0,255);
+                       cv->color = (~a<<24) | (r<<16) | (g<<8) | (b<<0);
                        int prev_pt_sz = pcv->points.size(), next_pt_sz = ncv->points.size();
                        for( int j=0; j<prev_pt_sz; ++j ) {
                                SketcherPoint &pt = *pcv->points[j], *nt = 0;
                                k = next_pt_sz;  // associated by id in next
                                while( --k >= 0 && pt.id != (nt=ncv->points[k])->id );
-                               int x = pt.x, y = pt.y;
+                               coord x = pt.x, y = pt.y;
                                if( k >= 0 ) {
-                                       x = x * prev_scale + nt->x * next_scale;
-                                       y = y * prev_scale + nt->y * next_scale;
+                                       if( x != nt->x )
+                                               x = x * prev_scale + nt->x * next_scale;
+                                       if( y != nt->y )
+                                               y = y * prev_scale + nt->y * next_scale;
                                }
-                               cv->points.append(new SketcherPoint(pt.id, pt.pty, x, y));
+                               cv->points.append(new SketcherPoint(pt.id, pt.arc, x, y));
                        }
                }
                else
@@ -412,7 +423,7 @@ 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->set_pixel_color(color);
        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);
@@ -472,10 +483,10 @@ int SketcherPenXlant::draw_pixel(int x, int y)
 SketcherVPen *SketcherCurve::new_vpen(VFrame *out)
 {
        switch( pen ) {
-       case PEN_SQUARE: return new SketcherPenSquare(out, radius);
-       case PEN_PLUS:   return new SketcherPenPlus(out, radius);
-       case PEN_SLANT:  return new SketcherPenSlant(out, radius);
-       case PEN_XLANT:  return new SketcherPenXlant(out, radius);
+       case PEN_SQUARE: return new SketcherPenSquare(out, width);
+       case PEN_PLUS:   return new SketcherPenPlus(out, width);
+       case PEN_SLANT:  return new SketcherPenSlant(out, width);
+       case PEN_XLANT:  return new SketcherPenXlant(out, width);
        }
        return 0;
 }
@@ -612,7 +623,8 @@ SketcherPoint *FillRegion::next()
 {
        while( nxt < points.size() ) {
                SketcherPoint *pt = points[nxt];
-               if( pt->pty != PTY_FILL ) break;
+               if( pt->arc == ARC_OFF ) continue;
+               if( pt->arc != ARC_FILL ) break;
                start_at(pt->x, pt->y);
                ++nxt;
        }
@@ -638,11 +650,11 @@ void SketcherCurve::draw(VFrame *img)
                smooth_axy(ax,ay, bx,by, cx,cy, dx,dy);
                while( pt2 ) {
                        dx = pt2->x;  dy = pt2->y;
-                       switch( pt0->pty ) {
-                       case PTY_LINE:
+                       switch( pt0->arc ) {
+                       case ARC_LINE:
                                vpen->draw_line(bx, by, cx, cy);
                                break;
-                       case PTY_CURVE: {
+                       case ARC_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);
@@ -652,15 +664,15 @@ void SketcherCurve::draw(VFrame *img)
                        bx = cx;  by = cy;  pt1 = pt2;
                        cx = dx;  cy = dy;  pt2 = fill.next();
                }
-               switch( pt1->pty ) {
-               case PTY_LINE:
+               switch( pt1->arc ) {
+               case ARC_LINE:
                        vpen->draw_line(bx, by, cx, cy);
                        if( fill.exists() ) {
                                dx = pnt0->x;  dy = pnt0->y;
                                vpen->draw_line(cx,cy, dx,dy);
                        }
                        break;
-               case PTY_CURVE: {
+               case ARC_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);
@@ -724,10 +736,11 @@ int Sketcher::process_realtime(VFrame *input, VFrame *output)
 
        for( int ci=0, n=config.curves.size(); ci<n; ++ci ) {
                SketcherCurve *cv = config.curves[ci];
+               if( cv->pen == PEN_OFF ) continue;
                int m = cv->points.size();
-               if( !m || cv->pen == PTY_OFF ) continue;
+               if( !m ) continue;
                img->clear_frame();
-               img->set_pixel_color(cv->color);
+               img->set_pixel_color(cv->color, (~cv->color>>24)&0xff);
                cv->draw(img);
                overlay_frame->overlay(out, img, 0,0,w,h, 0,0,w,h,
                                1.f, TRANSFER_NORMAL, NEAREST_NEIGHBOR);
@@ -756,16 +769,16 @@ 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);
+               printf("  Pt %d, id=%d, arc=%s, x=%0.1f, y=%0.1f\n",
+                       i, pt->id, pt_type[pt->arc], pt->x, pt->y);
        }
 }
 void SketcherCurves::dump()
 {
        for( int i=0; i<size(); ++i ) {
                SketcherCurve *cv = get(i);
-               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,
+               printf("Curve %d, id=%d, pen=%s, r=%d, color=%02x%02x%02x%02x, %d points\n",
+                       i, cv->id, cv_pen[cv->pen], cv->width, (~cv->color>>24)&0xff,
                        (cv->color>>16)&0xff, (cv->color>>8)&0xff, (cv->color>>0)&0xff,
                        cv->points.size());
                cv->points.dump();